http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54475
Bug #: 54475 Summary: -O2 overoptimizes Classification: Unclassified Product: gcc Version: 4.7.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization AssignedTo: unassig...@gcc.gnu.org ReportedBy: bdu...@linuxfromscratch.org Note: This is *not* my code, but found inside a ./configure file circa 2008. $ cat t.c int main() { int j; for (j = 1; 0 < j; j *= 2) printf( "%i\n", j ); } $ gcc -o t t.c Works fine. $ gcc -o t -O2 t.c Loops forever and prints 0 after j reaches 1073741824. Looking a the asm for the optimized code, there is no test for j<0. It appears to have been optimized away. ----------- with -O2 main: .LFB0: .cfi_startproc pushq %rbx .cfi_def_cfa_offset 16 .cfi_offset 3, -16 movl $1, %ebx .p2align 4,,10 .p2align 3 .L2: movl %ebx, %esi movl $.LC0, %edi xorl %eax, %eax call printf addl %ebx, %ebx jmp .L2 .cfi_endproc .LFE0: .size main, .-main .ident "GCC: (GNU) 4.7.1" .section .note.GNU-stack,"",@progbits