是否有可能防止g++在跨优化级别的函数开始时推入epb并保存esp ?

Is it possible to prevent g++ from pushing epb and saving esp at the start of a function across optimization levels?

本文关键字:开始时 函数 epb esp 保存 有可能 g++ 优化 是否      更新时间:2023-10-16

考虑以下代码,我将把它放入一个名为Epb.cc的文件中。

void f() {       
    asm("nop");  
}                

现在,我在各种优化级别下编译它。

g++ -c -O0 -o Out0 Ebp.cc
g++ -c -O1 -o Out1 Ebp.cc
g++ -c -O2 -o Out2 Ebp.cc
g++ -c -O3 -o Out3 Ebp.cc

这是objdump -d在每一个上的输出。

$ objdump -d Out*                               
Out0:     file format elf64-x86-64              

Disassembly of section .text:                   
0000000000000000 <_Z1fv>:                       
   0:   55                      push   %rbp     
   1:   48 89 e5                mov    %rsp,%rbp
   4:   90                      nop             
   5:   5d                      pop    %rbp     
   6:   c3                      retq            
Out1:     file format elf64-x86-64              

Disassembly of section .text:                   
0000000000000000 <_Z1fv>:                       
   0:   90                      nop             
   1:   c3                      retq            
Out2:     file format elf64-x86-64              

Disassembly of section .text:                   
0000000000000000 <_Z1fv>:                       
   0:   90                      nop             
   1:   c3                      retq            
Out3:     file format elf64-x86-64              

Disassembly of section .text:                   
0000000000000000 <_Z1fv>:                       
   0:   90                      nop             
   1:   c3                      retq            

是否有可能指示g++在所有优化级别中不为这个特定函数添加三个额外的指令?

正如@MichaelPetch在评论中指出的那样,一个会影响这个函数(以及其他函数)的解决方案是使用-fomit-frame-pointer编译器选项,这将导致g++在找到机会时忽略序言,无论优化级别如何。