Search code examples
cgccassemblyllvminstructions

Why does LLVM add two extra instructions for the same program?


I am compiling this C program and comparing the generated assembly code:

int main(){ return 0; }

GCC gives this main function (cc hello.c -S):

_main:
LFB2:
    pushq   %rbp
LCFI0:
    movq    %rsp, %rbp
LCFI1:
    movl    $0, %eax
    leave
    ret

LLVM gives this main function (clang hello.c -S):

_main:
Leh_func_begin0:
    pushq   %rbp
Ltmp0:
    movq    %rsp, %rbp
Ltmp1:
    movl    $0, %eax
    movl    $0, -4(%rbp)
    popq    %rbp
    ret
Leh_func_end0:

What are movl $0, -4(%rbp) and popq %rbp needed for? Moving something on the stack and popping it directly afterwards seems useless to me.


Solution

  • Actually, they're comparable. Leave is a high level instruction:

    From the Intel manual:

    16-bit: C9 LEAVE A Valid Valid Set SP to BP, then pop BP.
    32-bit: C9 LEAVE A N.E. Valid Set ESP to EBP, then pop EBP.
    64-bit: C9 LEAVE A Valid N.E. Set RSP to RBP, then pop RBP.
    

    basically, leave is equivalent to

    movq %rbp, %rsp
    popq %rbp