[Bug c/33498] New: Optimizer (-O2) may convert a normal loop to infinite
gcc-4.2.0 and gcc-4.2.1 cannot compile properly this function if -O2 is selected It generates an infinite loop :( No problem for previous version (gcc-4.1.2 is OK) $ cat bug.c void table_init(int *value) { int i; int val = 0x03020100; for (i = 0; i < 256/4; i++) { value[i] = val; val += 0x04040404; } } $ gcc -O2 -S bug.c $ cat bug.s .file "bug.c" .text .p2align 4,,15 .globl table_init .type table_init, @function table_init: pushl %ebp movl$50462976, %edx movl%esp, %ebp movl$1, %eax movl8(%ebp), %ecx .p2align 4,,7 .L2: movl%edx, -4(%ecx,%eax,4) addl$67372036, %edx addl$1, %eax jmp .L2 .size table_init, .-table_init .ident "GCC: (GNU) 4.2.1" .section.note.GNU-stack,"",@progbits -- Summary: Optimizer (-O2) may convert a normal loop to infinite Product: gcc Version: 4.2.1 Status: UNCONFIRMED Severity: critical Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: dada1 at cosmosbay dot com GCC build triplet: i686-pc-linux-gnu GCC host triplet: i686-pc-linux-gnu GCC target triplet: i686-pc-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33498
[Bug tree-optimization/33498] [4.2/4.3 Regression] Optimizer (-O2) may convert a normal loop to infinite
--- Comment #10 from dada1 at cosmosbay dot com 2007-09-20 08:17 --- > > What happens is that ivopts decide to use val as the variable to use in the > exit compare; they compute what its final value will be (67305984), and > replace > the exit test by val != 67305984. > > There is not much I can do with that in ivopts. I could make ivopts avoid > preserving signed variables appearing in the source code that provably > overflow; but I do not think we want to introduce this kind of hacks to handle > code with undefined behavior. > This code is valid. Integer overflows of a counter may happen in any program. i = 0x7fff, i += 1; /* IS VALID */ /* Here, gcc-4.1.2 can emit some infinite loop because programmer is lazy ! */ At very least, gcc should emit a BIG WARNING or ERROR The integer overflow is not a excuse for a compiler to generate an infinite loop. int i; int some_int = 0; for (i = 0 ; i < 100 ; i++) { some_func(some_int); some_int += 0x4000; /* yes, it can 'overflow'... big deal */ } Are you telling me that *any* integer overflow allows a compiler to generate a buggy code without any notice ? Interesting. -- dada1 at cosmosbay dot com changed: What|Removed |Added Status|RESOLVED|REOPENED Resolution|INVALID | http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33498