The current gcc trunk and gcc 4.8 produce wrong code that hangs for the following testcase on x86_64-linux when compiled at -O3 (in both 32-bit and 64-bit modes). This is a regression from 4.7.x. The testcase also triggers a different issue at -O2: a spurious "undefined behavior" warning (see below). $ gcc-trunk -v Using built-in specs. COLLECT_GCC=gcc-trunk COLLECT_LTO_WRAPPER=/usr/local/gcc-trunk/libexec/gcc/x86_64-unknown-linux-gnu/4.9.0/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: ../gcc-trunk/configure --enable-languages=c,c++,objc,obj-c++,fortran,lto --disable-werror --enable-checking=release --with-gmp=/usr/local/gcc-trunk --with-mpfr=/usr/local/gcc-trunk --with-mpc=/usr/local/gcc-trunk --with-cloog=/usr/local/gcc-trunk --prefix=/usr/local/gcc-trunk Thread model: posix gcc version 4.9.0 20131014 (experimental) [trunk revision 203572] (GCC) $ $ gcc-trunk -O2 small.c; a.out small.c: In function ‘main’: cc1: warning: iteration 5u invokes undefined behavior [-Waggressive-loop-optimizations] small.c:6:3: note: containing loop for (b = 4; b > -30; b--) ^ $ $ gcc-trunk -Os small.c; a.out $ $ gcc-trunk -O3 small.c; a.out ^C $ gcc-4.8 -O3 small.c; a.out ^C $ $ gcc-4.7 -O3 small.c; a.out $ ---------------------------------- int a, b, c, d, e; int main () { for (b = 4; b > -30; b--) for (; c;) for (;;) { e = a > 2147483647 - b; if (d) break; } return 0; }
int a, b, c, d, e; int main () { for (b = 4; b > -30; b--) for (; c;) for (;;) { e = a > 2147483647 - b; the issue here is that PRE (and LIM) move 2147483647 - b outside the for (;c;) loop entry test and thus make it always execute in the outermost loop, triggering the undefined behavior at iteration 5. Also warns at -O1 with -fstrict-overflow. PR58134 and PR58227 are related (if not dups).
Disabling LIM fixes this, disabling PRE not (and PRE alone is not enough to trigger it - even though it clearly may). *** This bug has been marked as a duplicate of bug 58143 ***