This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Optimization bug
- To: gcc-bugs at gcc dot gnu dot org
- Subject: Optimization bug
- From: Carlo Wood <carlo at alinoe dot com>
- Date: Sat, 12 Aug 2000 00:44:18 +0200
- Cc: carlo at alinoe dot com, bug-gnu-utils at gnu dot org, hjl at lucon dot org, nickc at redhat dot com
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 -=-