Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22231

GCC placing register args on the stack with a gap below local variables?

$
0
0

I tried to look at the assembly code for a very simple program.

int func(int x) {
    int z = 1337;
    return z;
} 

With GCC -O0, every C variable has a memory address that's not optimized away, so gcc spills its register arg: (Godbolt, gcc5.5 -O0 -fverbose-asm)

func:
        pushq   %rbp  #
        movq    %rsp, %rbp      #,
        movl    %edi, -20(%rbp) # x, x
        movl    $1337, -4(%rbp) #, z
        movl    -4(%rbp), %eax  # z, D.2332
        popq    %rbp    #
        ret

What is the reason that the function parameter x gets placed on the stack below the local variables? Why not place it at at -4(%rbp) and the local below that?

And when placing it below the local variables, why not place it at -8(%rbp)?

Why leave a gap, using more of the than necessary? Couldn't this touch a new cache line that wouldn't otherwise have been touched in this leaf function?


Viewing all articles
Browse latest Browse all 22231

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>