Bug#698950: gcc-4.7: Miscompilation of simple loop depending on integer overflow for termination
Package: gcc-4.7
Version: 4.7.2-5
Severity: normal
With the following very simple test program:
extern int foo(int);
extern void bar();
void test_fn() {
int i;
for (i = 1; 0 < i; i *= 2)
foo(i);
bar();
}
and compiling with "gcc -S -O2 -Wall bugtest.c -o -" (on amd64) I get output:
.file "bugtest.c"
.text
.p2align 4,,15
.globl test_fn
.type test_fn, @function
test_fn:
.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, %edi
addl %ebx, %ebx
call foo
jmp .L2
.cfi_endproc
.LFE0:
.size test_fn, .-test_fn
.ident "GCC: (Debian 4.7.2-5) 4.7.2"
.section .note.GNU-stack,"",@progbits
So it misoptimized this into an infinite loop, and then also optimized
out the code after the loop. With -O1 it appears to produce a correct
loop executing the body 31 times. This bug also happens with 4.7.2-19
from experimental.
(I found this issue while tracking down why a "checking for working
mktime" autoconf check was timing out.)
--
Daniel Schepler
Reply to: