https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69612
Bug ID: 69612 Summary: Optimizer does not consider overflow Product: gcc Version: 5.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: roarl at pvv dot org Target Milestone: --- I found a short code snippet that produces different results after optimizing with -O2. I believe my code should work as it only uses a simple test to check if incrementation has overflowed. It looks like the optimizer, having established that a variable is >= 0, does not believe the variable can become < 0 after incrementing. However, I am relying on just that to catch the overflow. In the code below, the __attribute__((noinline)) is not necessary to demonstrate the "effect", however without it the "test" routine would have to reside in another compilation unit. % cat bug.c int __attribute__((noinline)) test(int a) { if (a < 0) return 1; a++; if (a < 0) // gcc -O2 thinks this can't happen, since a>=0 from above return 2; return a; } #include <stdio.h> int main() { printf("%d\n", test(0x7FFFFFFF)); } % gcc bug.c -o bug % ./bug 2 % gcc -O2 bug.c -o bug % ./bug -2147483648 % gcc --version gcc (SUSE Linux) 5.2.1 20151008 [gcc-5-branch revision 228597] ...