When a while loop with the true condition (while (1)) contains
an if statement with a modulus evaluated on a counter variable, it
appears that gcc incorrectly optimizes out the modulus check as a
constant (even though the counter variable is updated after the if
statement). If the counter variable is updated _before_ the if
statement, it works properly. Example code is in the how-to-repeat section.

Environment:
System: Linux uktena64 2.6.10-5-amd64-k8 #1 Fri Jun 24 17:08:40 UTC 2005 x86_64
GNU/Linux
Architecture: x86_64


host: x86_64-pc-linux-gnu
build: x86_64-pc-linux-gnu
target: x86_64-pc-linux-gnu
configured with: ../src/configure -v
--enable-languages=c,c++,java,f77,pascal,objc,ada,treelang --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-gxx-include-dir=/usr/include/c++/3.3 --enable-shared --with-system-zlib
--enable-nls --without-included-gettext --enable-__cxa_atexit
--enable-clocale=gnu --enable-debug --enable-java-gc=boehm
--enable-java-awt=xlib --enable-objc-gc --disable-multilib x86_64-linux

How-To-Repeat:
        The following code produces the bug (output is 1 1 1 1 ... instead
of 0 1 2 3 ...):
        void main() { 
          int i=0;
          while (1) {
            if (i%100==0) printf("%d ",i);
            i++;
          }
        }


------- Comment #1 from Nick at idontproperlyfilloutmyemailaddress dot com  
2005-08-31 03:18 -------
Fix:

        If i++; is above the if statement, the code works properly 
        (1 2 3 4 ...).


------- Comment #2 from pinskia at gcc dot gnu dot org  2005-08-31 04:55 -------
Fixed at least in 4.0.2 and above.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
          Component|c                           |tree-optimization
           Keywords|                            |wrong-code
         Resolution|                            |FIXED
            Summary|Bug w\ while (1) loop and   |[4.0 Regression] Bug w\
                   |counter variable w\         |while (1) loop and counter
                   |optimization                |variable w\ optimization
   Target Milestone|---                         |4.0.2


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23647

Reply via email to