http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56600
Bug #: 56600 Summary: loop goes indefinite when non-loop integer variable overflows Classification: Unclassified Product: gcc Version: 4.7.2 Status: UNCONFIRMED Severity: major Priority: P3 Component: c AssignedTo: unassig...@gcc.gnu.org ReportedBy: mskye...@naver.com Created attachment 29648 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=29648 intermediate file Hi, the following code compiles to infinite loop specifically with -O2 on gcc 4.7.2. #include <stdio.h> int main (int nargs, char** vargs){ int i=0x7ffffffd; int j=0; for (j=0; j<4; ++j){ ++i; printf("%i ",i>0); //printf("%i ",j); printf("%i\n",i); } return 0; } This may seem related to bug 52833, but it is actually quite different. With -O1, the output is as expected. 1 2147483646 1 2147483647 0 -2147483648 0 -2147483647 With -O3, I assume the expression 'i>0' replaced with 1 and the loop was unrolled. i>0 being 1 is no problem, as overflow is an undefined behavior. 1 2147483646 1 2147483647 1 -2147483648 1 -2147483647 But with -O2, it starts as above, and then goes on indefinitely! Since the loop variable j is not overflowing or modified anywhere else, I think that this bug may have serious implications on loop optimization. j is falsely being ignored. Therefore I marked this bug as major. Moreover, the result becomes the same as -O3 when (1) the printf line is uncommented, and/or (2) i does not overflow. FINDINGS: the intermediate file is exactly the same with all of -O1, -O2 and -O3. infinite loop does not happen on an older version of gcc (4.4.6). CONDITIONS: CPU: Intel i7-2620M (laptop) gcc.x86_64 version 4.7.2-2.fc17 from Fedora repository kernel 3.7.9-104.fc17.x86_64 $ gcc -v -O2 bug.c Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.7.2/lto-wrapper Target: x86_64-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --disable-build-with-cxx --disable-build-poststage1-with-cxx --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux Thread model: posix gcc version 4.7.2 20120921 (Red Hat 4.7.2-2) (GCC) COLLECT_GCC_OPTIONS='-v' '-O2' '-mtune=generic' '-march=x86-64' /usr/libexec/gcc/x86_64-redhat-linux/4.7.2/cc1 -quiet -v bug.c -quiet -dumpbase bug.c -mtune=generic -march=x86-64 -auxbase bug -O2 -version -o /tmp/ccYIOd07.s GNU C (GCC) version 4.7.2 20120921 (Red Hat 4.7.2-2) (x86_64-redhat-linux) compiled by GNU C version 4.7.2 20120921 (Red Hat 4.7.2-2), GMP version 5.0.2, MPFR version 3.1.0, MPC version 0.9 GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.7.2/include-fixed" ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../x86_64-redhat-linux/include" #include "..." search starts here: #include <...> search starts here: /usr/lib/gcc/x86_64-redhat-linux/4.7.2/include /usr/local/include /usr/include End of search list. GNU C (GCC) version 4.7.2 20120921 (Red Hat 4.7.2-2) (x86_64-redhat-linux) compiled by GNU C version 4.7.2 20120921 (Red Hat 4.7.2-2), GMP version 5.0.2, MPFR version 3.1.0, MPC version 0.9 GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: 1ddc7c1a4fba2159f711c3e228ac3970 COLLECT_GCC_OPTIONS='-v' '-O2' '-mtune=generic' '-march=x86-64' as -v --64 -o /tmp/ccq8Xual.o /tmp/ccYIOd07.s GNU assembler version 2.22.52.0.1 (x86_64-redhat-linux) using BFD version version 2.22.52.0.1-10.fc17 20120131 COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/4.7.2/:/usr/libexec/gcc/x86_64-redhat-linux/4.7.2/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/4.7.2/:/usr/lib/gcc/x86_64-redhat-linux/ LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/4.7.2/:/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../:/lib/:/usr/lib/ COLLECT_GCC_OPTIONS='-v' '-O2' '-mtune=generic' '-march=x86-64' /usr/libexec/gcc/x86_64-redhat-linux/4.7.2/collect2 --build-id --no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.7.2/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/4.7.2 -L/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../.. /tmp/ccq8Xual.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-redhat-linux/4.7.2/crtend.o /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crtn.o Thank you!