public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: jlh <jlh@gmx.ch>
To: matt smith <infinite_south@yahoo.com>,  gcc-help@gcc.gnu.org
Subject: Re: stack allocation
Date: Thu, 16 Dec 2004 19:08:00 -0000	[thread overview]
Message-ID: <41C1DD2D.9010108@gmx.ch> (raw)
In-Reply-To: <20041212192105.94719.qmail@web50706.mail.yahoo.com>

[-- Attachment #1: Type: text/plain, Size: 1624 bytes --]


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 #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 252 bytes --]

  reply	other threads:[~2004-12-16 19:08 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-12 19:21 matt smith
2004-12-16 19:08 ` jlh [this message]
     [not found] <CAPhGq=bY8hS0DF2rf7_5E8ycYS52uJR8UH=Yjb0NiDBdaSR+6Q@mail.gmail.com>
2011-11-18 17:57 ` Stack allocation Alexandru Juncu
2011-11-18 19:35   ` Andrew Haley

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=41C1DD2D.9010108@gmx.ch \
    --to=jlh@gmx.ch \
    --cc=gcc-help@gcc.gnu.org \
    --cc=infinite_south@yahoo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).