public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Andrew Haley <aph@redhat.com>
To: gcc-help@gcc.gnu.org
Subject: Re: Stack allocation
Date: Fri, 18 Nov 2011 19:35:00 -0000	[thread overview]
Message-ID: <4EC67571.2090600@redhat.com> (raw)
In-Reply-To: <CAPhGq=beBVracjGKNuLe9Ykyxs=ZzzTU6i1x37mdcbfeNUDJWQ@mail.gmail.com>

On 11/18/2011 03:03 PM, Alexandru Juncu wrote:
> Hello!
> 
> [I sent an email on the gcc main list by my mistake, and I am moving
> the discussion here]
> 
> I have a curiosity with something I once tested. I took a simple C
> program and made an assembly file with gcc -S.
> 
> The C file looks something like this:
> int main(void)
> {
>   int a=1, b=2;
>   return 0;
> }
> 
> The assembly instructions look like this:
> 
> subl    $16, %esp
> movl    $1, -4(%ebp)
> movl    $2, -8(%ebp)
> 
> The subl $16, means the allocation of local variables on the stack,
> right? 16 bytes are enough for 4 32bit integers.
> If I have 1,2,3 or 4 local variables declared, you get those 16 bytes.
> If I have 5 variables, we have "        subl    $32, %esp". 5,6,7,8 variables ar
> the same. 9, 10,11,12, 48 bytes.
> 
> The observation is that gcc allocates increments of 4 variables (if
> they are integers). If I allocate 8bit chars, increments of 16 chars.
> 
> So the allocation is in increments of 16 bytes no matter what.
> 
> OK, that's the observation... my question is why? What's the reason
> for this, is it an optimization (does is matter what's the -O used?)
> or is it architecture dependent (I ran it on x86) and is this just in
> gcc, just in a certain version of gcc or this is universal?
> 
> I got a response that is related to the cache line alignment, to
> optimize cache hits.
> But I tried to compile the program with the --param l1-cache-size and
> got the same .s file. Is this ok?

You're not optimizing.  Nothing much will happen with optimization options
when you're not optimizing.

This is x86-specific, but other processors have similar needs.

gcc must 16-align the stack because some structures (such as MMX data)
must be aligned.  Given that the data must be aligned, so must the stack.
Also, fetches and stores that straddle cache line boundaries can be very
slow.

Andrew.

  reply	other threads:[~2011-11-18 15:10 UTC|newest]

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

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=4EC67571.2090600@redhat.com \
    --to=aph@redhat.com \
    --cc=gcc-help@gcc.gnu.org \
    /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).