This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: loop invariant code motion bug


| 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.

Declare globalvar to be volatile, which says that the variable is modified in
ways unknown to the compiler (ie, by another thread in this case).  Any
reasonable C book should talk about volatile and when you need to use it.

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

Umm, GCC's optimization is still correct by ISO C rules.

| 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.

No, all have the same optimization.

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

Not our problem.

--
Michael Meissner, Cygnus Solutions (Massachusetts office)
4th floor, 955 Massachusetts Avenue, Cambridge, MA 02139, USA
meissner@cygnus.com,	617-354-5416 (office),	617-354-7161 (fax)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]