------- Comment #1 from rguenth at gcc dot gnu dot org 2009-06-29 09:10 ------- The following loop invokes undefined behavior:
long x = -2147483645; int i; for (i = 0; i < 4; ++i) { p = itos(x); printf("i=%d p=%s\n",i,p); --x; } because at iteration with i == 3 x is decremented and underflows. This can be avoided as follows, which still shows the bug: extern void abort (void); static char * __attribute__((noinline)) itos(int num) { return (char *)0; } static void __attribute__((noinline)) foo(int i, const char *x) { if (i >= 4) abort (); } int main() { int x = -__INT_MAX__ + 3; int i; for (i = 0; i < 4; ++i) { char *p; --x; p = itos(x); foo(i, p); } return 0; } Fails when VRP and IVOPTs are enabled. IVOPTs expresses i in terms of x: <bb 3>: # x_16 = PHI <x_5(4), -2147483644(2)> D.1693_15 = (unsigned int) x_16; D.1694_14 = 2147483652 - D.1693_15; i_13 = (int) D.1694_14; i_17 = i_13; x_5 = x_16 + -1; p_6 = itos (x_5); foo (i_17, p_6); if (x_5 != -2147483648) goto <bb 4>; else goto <bb 5>; from which VRP concludes that the test is never true: Value ranges after VRP: x_5: [-INF, -2147483645] ... Folding statement: if (x_5 != -2147483648) Folding predicate x_5 != -2147483648 to 1 huh. -- rguenth at gcc dot gnu dot org changed: What |Removed |Added ---------------------------------------------------------------------------- Status|UNCONFIRMED |NEW Component|c |tree-optimization Ever Confirmed|0 |1 Keywords| |wrong-code Known to fail| |4.3.3 4.4.0 4.5.0 Known to work| |4.2.4 Last reconfirmed|0000-00-00 00:00:00 |2009-06-29 09:10:49 date| | Summary|gcc -O2 optimization causes |[4.3/4.4/4.5 Regression] gcc |infinite loop and wrong |-O2 optimization causes |output |infinite loop and wrong | |output Target Milestone|--- |4.3.4 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40579