The following test wrongly fails with a floating point exception (division by integer 0) when built with -O2 for i686-pc-linux-gnu. Based on an example posted by Robert Seacord to the WG14 reflector. It works with 3.4 and earlier, fails with 4.0 and later. The % operation may trap if it is % 0 (or INT_MIN % -1, cf bug 30484 and another recent reflector discussion suggesting INT_MIN % -1 should in fact be undefined in ISO C), so cannot be hoisted above the loop unless it is guaranteed to be executed at least once.
extern void exit (int); extern void abort (void); volatile int a = 1; volatile int b = 0; volatile int x = 2; volatile signed int r = 8; void __attribute__((noinline)) foo (void) { exit (0); } int main (void) { int si1 = a; int si2 = b; int i; for (i = 0; i < 100; ++i) { foo (); if (x == 8) i++; r += i + si1 % si2; } abort (); } -- Summary: [4.2/4.3/4.4 Regression] trapping expression wrongly hoisted out of loop Product: gcc Version: 4.4.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: jsm28 at gcc dot gnu dot org http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38819