loop invariant code motion bug

Nick Mitchell mitchell@cs.ucsd.edu
Thu May 28 14:09:00 GMT 1998


gcc moves loads/compares for global variables out of loops in
instances where it should not.

I have a loop which basically does this

   while(globalvar < localvar) {
      /* modifies neither globalvar nor localvar */
   }

normally, it would be fine to move loads/compares of the two variables
out of the loop. However, if the program is *multi-threaded*, this
code motion may not be correct:

 in particular, if another thread writes to _globalvar_, you would
  like this update to be known to other threads.

with optimizations off, obviously the code is correct, but with -O1
and higher, the problem arises.

I have tested this on multiple versions: gcc-2.7.2.3 (alpha-osf4.0b),
gcc-2.7.2.1 (pentium2-linux2), egcs-1.0.2 (alpha-osf4.0b,
pentium2-linux2) and egcs-1.0.3a (alpha-osf4.0b). All have the same
problem.

interestingly enough, Solaris cc with -xO4 (i tried version 4.2) does
*not* have this problem.

thanks,
nick mitchell



More information about the Gcc-bugs mailing list