This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Tail calls and stack frames

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"
	.align 16
.globl f
	.type	f,@function
	subl	$12, %esp
	incl	16(%esp)
	addl	$12, %esp
	jmp	g
	.size	f,.Lfe1-f
	.align 16
.globl g
	.type	g,@function
	subl	$40, %esp
	movl	44(%esp), %eax
	addl	$2, %eax
	pushl	%eax
	call	g
	addl	$44, %esp
	.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 *
  ^^                      SYGNATURA ZASTĘPCZA

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]