This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Tail calls and stack frames
- To: gcc at gcc dot gnu dot org
- Subject: Tail calls and stack frames
- From: "Marcin 'Qrczak' Kowalczyk" <qrczak at knm dot org dot pl>
- Date: Thu, 30 Aug 2001 18:21:57 +0200
I have a question about gcc generating silly code. Here is a source:
void f (int x);
void g (int x);
void f (int x) {g (x+1);}
void g (int x) {f (x+1);}
Here is the result of a compilation by gcc-3.0.1 -O2 -fomit-frame-pointer
(Linux, i686):
.file "2.c"
.text
.align 16
.globl f
.type f,@function
f:
subl $12, %esp
incl 16(%esp)
addl $12, %esp
jmp g
.Lfe1:
.size f,.Lfe1-f
.align 16
.globl g
.type g,@function
g:
subl $40, %esp
movl 44(%esp), %eax
addl $2, %eax
pushl %eax
call g
addl $44, %esp
ret
.Lfe2:
.size g,.Lfe2-g
.ident "GCC: (GNU) 3.0.1"
Why there are 12 bytes of stack wasted in f and 40 bytes wasted in g?
Is this a bug or it can be avoided?
I see that it folded two functions into one for g. Why there is no tail
call optimization in this case? The tail call is compiled well to a jmp
when there is more stuff in these functions so they are not folded, and
it's also compiled well when there is one recursive function, but it's
as bad as here when two functions are folded into one by the compiler.
Why the assymetry? Even if g is static, g is chosen as a loop breaker,
even though f would be a better choice. I understand that this might
be a more tricky optimization, but IMHO wrt. points above gcc's code
should really be improved.
I'm not subscribed to the list, so please Cc: replies to me. Or should
I subscribe?
--
__("< Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/
\__/
^^ SYGNATURA ZASTĘPCZA
QRCZAK