register allocation for x86

432 views Asked by At

I am attempting a simple register allocation with x86. Am using the linear register allocator but I have a few questions on some simple things which I am not finding the information about.

int main()
{
    int a, b, c;
    a = 10;
    b = 20;
    c = 2;
    a = a + b;
    b = b * 5;
    c = c * b + a;
    return c;
}

For this simple code, ignoring constant propagation, g++ & msvc generate similar code which pushes all temporaries on the stack and then uses eax, ebx etc only for ALU ops. Clang generates the following code with O0.

main:                                   # @main
    movl    $0, -4(%rsp)
    movl    $10, -8(%rsp)
    movl    $20, -12(%rsp)
    movl    $2, -16(%rsp)
    movl    -8(%rsp), %eax
    addl    -12(%rsp), %eax
    movl    %eax, -8(%rsp)
    imull   $5, -12(%rsp), %eax
    movl    %eax, -12(%rsp)
    movl    -16(%rsp), %eax
    imull   -12(%rsp), %eax
    addl    -8(%rsp), %eax
    movl    %eax, -16(%rsp)
    movl    -16(%rsp), %eax
    ret

As is evident, that all temporaries are on the stack. So my question is based on this same thing. At any point, should anything in eax/ebx etc which are used in add/mul/div operations etc have a lifetime more than 1 instruction. That is, should any live range interval have its value stored in these registers or should they be evicted as soon as the register allocation is done and their values pushed to temporaries ?

Or can we use some of these registers till the next ALU instruction is encountered and do a lazy push to stack when necessary?

0

There are 0 answers