Optimization bug
Carlo Wood
carlo@alinoe.com
Fri Aug 11 15:44:00 GMT 2000
Hi,
at least gcc version 2.95.1 and 2.96-20000402 have an optimization bug
as described below. I did not test other versions, but I seems likely
that all versions of gcc have this bug.
The problem is gcc is confused near `return' with its local variables
on the stack: returning from a function exits the scope of the local
variables, so that when a recursive call to the same function is
performed, this call is replaced by a jmp to the start of the function
and the same stack space is used for the local variables.
This is incorrect however when a pointer to a local variable is
passed in the recursive call. This particular bug breaks a part of
binutils (I found it in objdump) in supposedly rare circumstances
(and they refuse to write a work around when it is a compiler bug ;).
I wrote a little test case to demonstrate the problem:
-----------------------------------------------------
int foo(int* p)
{
int i = 1;
*p += 2;
if (*p == 3)
return i;
return foo(&i);
}
int main(void)
{
int i = 0;
return foo(&i);
}
-----------------------------------------------------
>gcc -v
Reading specs from /usr/lib/gcc-lib/i686-redhat-linux/2.95.1/specs
gcc version 2.95.1 19990816/Linux (release)
>gcc-cvs -v
Reading specs from /usr/local/egcs/lib/gcc-lib/i686-pc-linux-gnu/2.96/specs
gcc version 2.96 20000402 (experimental)
>gcc -O0 20000812.c
>a.out; echo $?
1
>gcc -O1 20000812.c
>a.out; echo $?
3
>gcc-cvs -O0 20000812.c
>a.out; echo $?
1
>gcc-cvs -O1 20000812.c
>a.out; echo $?
1
>gcc-cvs -O2 20000812.c
>a.out; echo $?
3
Regards,
--
Carlo Wood <carlo@alinoe.com> -=- Jesus Loves you -=-
More information about the Gcc-bugs
mailing list