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

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

int main(){ return 0; } 

GCC provides this core function ( cc hello.c -S ):

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

LLVM provides this core 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 ? Moving something on the stack and popping it immediately afterwards seems useless to me.

+8
c assembly gcc instructions llvm
source share
3 answers

Actually, they are comparable. Leave - high-level instruction:

In the Intel manual:

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

basically leave tantamount to

 movq %rbp, %rsp popq %rbp 
+9
source share

The movl $0, -4(%rbp) is dead because it is not optimized code. Try switching to -O to both compilers to see what changes.

+9
source share

LLVM seems to use the traditional proog / epilog function, while GCC uses the fact that the entry point does not need to be cleared

+2
source share

All Articles