Re: stack allocation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




matt smith wrote:
Why the discrepancy?

I think I might have found the reason for this; here's what I've been experimenting with today:

extern int i;
extern void f2();
void f()
{
        f2();
        i = 3;
}

If I compile with "gcc-4.0 -O2" I get this:  (on x86)

f:      pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        call    f2
        movl    $3, %eax
        movl    %eax, i
        leave
        ret

The pushed %ebp uses 4 bytes on the stack and GCC reserves another
8 bytes (which are never used) for a total of 12 bytes.

Now if I compile the same with the option "-fomit-frame-pointer"
added I get this:

f:      subl    $12, %esp
        call    f2
        movl    $3, %eax
        movl    %eax, i
        addl    $12, %esp
        ret

No more %ebp on the stack, but now GCC reserves 12 bytes.

In both cases the function f() uses 12 bytes of stack and together
with the 4 bytes of return address being on the stack already, it
totals to 16 bytes, which is a nice alignment.  And as you know,
proper alignment makes code faster.  If f() does not call any
function, GCC does not reserve any unnecessary space.

In your sample code, you didn't use optimization at all, so it
probably did the alignment anyway, even if no other function gets
called from your function.  This might be the reason why it
allocates 40 bytes instead of only what it requires for storage.

Then I did some measurements and apparently, calling a function
with the stack not aligned to 16-bytes is slower.  So GCC actually
does a good job here.

Voilà, I hope this wasn't non-sense.  :)

jlh

Attachment: signature.asc
Description: OpenPGP digital signature


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux