gcc argument register spilling on x86-64

后端 未结 2 1736
春和景丽
春和景丽 2020-12-19 07:56

I\'m doing some experimenting with x86-64 assembly. Having compiled this dummy function:

long myfunc(long a, long b, long c, long d,
            long e, long         


        
相关标签:
2条回答
  • 2020-12-19 08:36

    I am by no means a GCC internals expert, but I'll give it a shot. Unfortunately most of the information on GCCs register allocation and spilling seems to be out of date (referencing files like local-alloc.c that don't exist anymore).

    I'm looking at the source code of gcc-4.5-20110825.

    In GNU C Compiler Internals it is mentioned that the initial function code is generated by expand_function_start in gcc/function.c. There we find the following for handling parameters:

    4462   /* Initialize rtx for parameters and local variables.
    4463      In some cases this requires emitting insns.  */
    4464   assign_parms (subr);
    

    In assign_parms the code that handles where each arguments is stored is the following:

    3207       if (assign_parm_setup_block_p (&data))
    3208         assign_parm_setup_block (&all, parm, &data);
    3209       else if (data.passed_pointer || use_register_for_decl (parm))
    3210         assign_parm_setup_reg (&all, parm, &data);
    3211       else
    3212         assign_parm_setup_stack (&all, parm, &data);
    

    assign_parm_setup_block_p handles aggregate data types and is not applicable in this case and since the data is not passed as a pointer GCC checks use_register_for_decl.

    Here the relevant part is:

    1972   if (optimize)
    1973     return true;
    1974 
    1975   if (!DECL_REGISTER (decl))
    1976     return false;
    

    DECL_REGISTER tests whether the variable was declared with the register keyword. And now we have our answer: Most parameters live on the stack when optimizations are not enabled, and are then handled by assign_parm_setup_stack. The route taken through the source code before it ends up spilling the value is slightly more complicated for pointer arguments, but can be traced in the same file if you're curious.

    Why does GCC spill all arguments and local variables with optimizations disabled? To help debugging. Consider this simple function:

    1 extern int bar(int);
    2 int foo(int a) {
    3         int b = bar(a | 1);
    4         b += 42;
    5         return b;
    6 }
    

    Compiled with gcc -O1 -c this generates the following on my machine:

     0: 48 83 ec 08             sub    $0x8,%rsp
     4: 83 cf 01                or     $0x1,%edi
     7: e8 00 00 00 00          callq  c <foo+0xc>
     c: 83 c0 2a                add    $0x2a,%eax
     f: 48 83 c4 08             add    $0x8,%rsp
    13: c3                      retq   
    

    Which is fine except if you break on line 5 and try to print the value of a, you get

    (gdb) print a
    $1 = <value optimized out>
    

    As the argument gets overwritten since it's not used after the call to bar.

    0 讨论(0)
  • 2020-12-19 08:58

    A couple of reasons:

    1. In the general case, an argument to a function has to be treated like a local variable because it could be stored to or have its address taken within the function. Therefore, it is simplest to just allocate a stack slot for every arguments.
    2. Debug information becomes much simpler to emit with stack locations: the argument's value is always at some specific location, instead of moving around between registers and memory.

    When you're looking at -O0 code in general, consider that the compiler's top priorities are reducing compile-time as much as possible and generating high-quality debugging information.

    0 讨论(0)
提交回复
热议问题