* Extra 8 bytes on stack in gcc 4.3 (64-bit)
@ 2009-04-10 3:56 Bob Plantz
2009-04-10 10:19 ` John (Eljay) Love-Jensen
0 siblings, 1 reply; 4+ messages in thread
From: Bob Plantz @ 2009-04-10 3:56 UTC (permalink / raw)
To: gcc-help
When I use gcc -S in an x86-64 environment running in 64-bit mode on the
following C function:
int main( void )
{
long sum = foo( 3, 4, 5, 6, 7 );
}
gcc-4.1 allocates 16 bytes on the stack for the local variable, sum (my
comments added):
main:
.LFB3:
pushq %rbp # save caller base pointer
.LCFI2:
movq %rsp, %rbp # establish our base pointer
.LCFI3:
subq $16, %rsp # space for local variable
.LCFI4:
movl $7, %r8d # pass 5th argument
.........
The ABI specifies that the stack pointer will be "16-byte aligned at
process entry." So even though sum only takes 8 bytes (longs in 64-bit
mode are 8 bytes), I assume that the compiler allocates 16 bytes to
maintain the 16-byte alignment.
But with gcc-4.3 I get 24 bytes:
main:
.LFB3:
pushq %rbp # save caller base pointer
.LCFI2:
movq %rsp, %rbp # establish our base pointer
.LCFI3:
subq $24, %rsp # space for local variable
.LCFI4:
movl $7, %r8d # pass 5th argument
.........
Now the stack is no longer 16-byte aligned. Furthermore, this stack
alignment could have been accomplished by allocating only the 8 bytes
needed for the local variable, sum.
What am I missing here?
--Bob
^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: Extra 8 bytes on stack in gcc 4.3 (64-bit)
2009-04-10 3:56 Extra 8 bytes on stack in gcc 4.3 (64-bit) Bob Plantz
@ 2009-04-10 10:19 ` John (Eljay) Love-Jensen
2009-04-10 15:53 ` Bob Plantz
0 siblings, 1 reply; 4+ messages in thread
From: John (Eljay) Love-Jensen @ 2009-04-10 10:19 UTC (permalink / raw)
To: Bob Plantz, gcc-help
Hi Bob,
> Now the stack is no longer 16-byte aligned.
Leaf functions do not need to maintain stack alignment.
> Furthermore, this stack alignment could have been accomplished by allocating only the 8 bytes needed for the local variable, sum.
Optimized code, or non-optimized?
If it is non-optimized (-O0, which is the default if not specified), non-optimized code may be non-optimal.
HTH,
--Eljay
^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: Extra 8 bytes on stack in gcc 4.3 (64-bit)
2009-04-10 10:19 ` John (Eljay) Love-Jensen
@ 2009-04-10 15:53 ` Bob Plantz
2009-04-10 21:37 ` Ian Lance Taylor
0 siblings, 1 reply; 4+ messages in thread
From: Bob Plantz @ 2009-04-10 15:53 UTC (permalink / raw)
To: John (Eljay) Love-Jensen; +Cc: gcc-help
On Fri, 2009-04-10 at 03:16 -0700, John (Eljay) Love-Jensen wrote:
> Hi Bob,
>
> > Now the stack is no longer 16-byte aligned.
>
> Leaf functions do not need to maintain stack alignment.
>
main is not a leaf function here. It calls foo.
> > Furthermore, this stack alignment could have been accomplished by allocating only the 8 bytes needed for the local variable, sum.
>
> Optimized code, or non-optimized?
>
> If it is non-optimized (-O0, which is the default if not specified), non-optimized code may be non-optimal.
>
It was non-optimized. But I still expected the compiler not to allocate
16 bytes on the local stack frame for no reason. Oddly (to me)
int main( void )
{
long sum = foo( 3, 4, 5, 6, 7 );
return 0;
}
produces:
main:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
int main( void )
{
long sum = foo( 3, 4, 5, 6, 7 );
}
produces:
main:
pushq %rbp
movq %rsp, %rbp
subq $24, %rsp
and
void main( void )
{
long sum = foo( 3, 4, 5, 6, 7 );
}
produces:
main:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
My "application" here is an x86-64 textbook I have written. The point of
my book is to show how the machine works at the instruction set level
when code is written at the C/C++ level. So I start my examples with C/C
++ code. The book serves as an introduction to architecture. It's not an
assembly language book, nor a compiler book. But I would like to be able
to explain things in the book that the student might see when he/she
replicates my examples. I do have a "your mileage may vary" due to
different gcc/as versions.
Bob
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Extra 8 bytes on stack in gcc 4.3 (64-bit)
2009-04-10 15:53 ` Bob Plantz
@ 2009-04-10 21:37 ` Ian Lance Taylor
0 siblings, 0 replies; 4+ messages in thread
From: Ian Lance Taylor @ 2009-04-10 21:37 UTC (permalink / raw)
To: Bob Plantz; +Cc: John (Eljay) Love-Jensen, gcc-help
Bob Plantz <plantz@cds1.net> writes:
> It was non-optimized. But I still expected the compiler not to allocate
> 16 bytes on the local stack frame for no reason.
You unfortunately can not make any such assumptions about the
unoptimized compiler.
Ian
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2009-04-10 21:37 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-10 3:56 Extra 8 bytes on stack in gcc 4.3 (64-bit) Bob Plantz
2009-04-10 10:19 ` John (Eljay) Love-Jensen
2009-04-10 15:53 ` Bob Plantz
2009-04-10 21:37 ` Ian Lance Taylor
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).