为什么Apple Clang在-O1/2/3下留下冗余堆栈推送pop指令?

为什么Apple Clang在-O1/2/3下留下冗余堆栈推送pop指令?,c,optimization,clang,C,Optimization,Clang,给出了一个简单的函数 intadd(inta,intb){ 返回a+b; } 用clang-O3-c-o test.o test.c编译它。编译器版本为 Apple clang version 11.0.3 (clang-1103.0.32.62) Target: x86_64-apple-darwin19.5.0 对象文件的反汇编显示 test.o: file format Mach-O 64-bit x86-64 Disassembly of section __TEXT,__text:

给出了一个简单的函数

intadd(inta,intb){
返回a+b;
}
clang-O3-c-o test.o test.c
编译它。编译器版本为

Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.5.0
对象文件的反汇编显示

test.o: file format Mach-O 64-bit x86-64
Disassembly of section __TEXT,__text:

0000000000000000 _add:
       0: 55                            pushq   %rbp
       1: 48 89 e5                      movq    %rsp, %rbp
       4: 8d 04 37                      leal    (%rdi,%rsi), %eax
       7: 5d                            popq    %rbp
       8: c3                            retq
显然,
pushq
movq
popq
指令只会浪费CPU时间

在Linux上使用
clang 7.0.1-8版编译同一段代码(tags/RELEASE_701/final)
目标:x86_64-pc-linux-gnu
产生下面真正优化的指令

test.o:     file format elf64-x86-64
Disassembly of section .text:

0000000000000000 <add>:
   0:   8d 04 37                lea    (%rdi,%rsi,1),%eax
   3:   c3                      retq
test.o:文件格式elf64-x86-64
第节的分解。正文:
0000000000000000

但是这里的答案并没有解决我的问题。

它设置了一个堆栈框架。这显然是OSX上的默认设置,可能是因为它使调试更容易。看见尝试
-fomit frame pointer
使其消失。