什么时候应该省略帧指针? [英] When should I omit the frame pointer?

查看:625
本文介绍了什么时候应该省略帧指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

省略帧指针时是否有实质性优化?
如果我正确理解了这个页面,当我们想要避免保存,设置和恢复框架指针时,使用 -fomit-frame-pointer



这只是为每个函数调用做的,如果是这样,是真的值得避免每个函数的几个指令吗?
对于优化来说是不重要的。
除了调试限制以外,使用这个选项的实际影响是什么?



我编译了以下C代码,带有和不带这个选项

  int main(void)
{
int i;

i = myf(1,2);
}

int myf(int a,int b)
{
return a + b;
}

 #gcc -S -fomit-frame-pointer code.c -o withoutfp.s 
#gcc -S code.c -o withfp.s





diff -u 显示以下汇编代码:

  
--- withfp.s 2009 -12-22 00:03:59.000000000 +0000
+++ withoutfp.s 2009-12-22 00:04:17.000000000 +0000
@@ -7,17 +7,14 @@
leal 4(%esp),%ecx
andl $ -16,%esp
pushl -4(%ecx)
- pushl%ebp
- movl%esp ,%ebp
pushl%ecx
- subl $ 36,%esp
+ subl $ 24,%esp
movl $ 2,4(%esp)
movl $ 1, %esp)
call myf
- movl%eax,-8(%ebp)
- addl $ 36,%esp
+ movl%eax,20 $ b + addl $ 24,%esp
popl%ecx
- popl%ebp
leal -4(%ecx),%esp
ret
.size main, 。-main
@@ -25,11 +22,8 @@
.globl myf
.type myf,@function
myf:
- pushl%ebp
- movl%esp,%ebp
- movl 12(%ebp),%eax
- addl 8(%ebp),%eax
- popl%ebp $ b b + movl 8(%esp),%eax
+ addl 4(%esp),%eax
ret
.size myf,。-myf
.identGCC: (GNU)4.2.1 20070719

有人可以通过

编辑: objdump(上面的代码中的-fomit-frame-pointer确实有区别吗? 的输出替换为 gcc -S

解决方案

-fomit-frame-pointer 允许一个额外的寄存器可用于通用目的。我认为这只是一个很大的32位x86,这是一个有点饥饿的寄存器。*



人们期望看到EBP不再保存,在每次函数调用时调整,在正常代码中可能还有一些额外的EBP使用,在EBP用作通用寄存器的情况下,堆栈操作较少。



代码太简单,看不到这种优化的任何好处 - 你没有使用足够的寄存器。此外,您尚未打开优化程序,这可能需要看到一些这些效果。



* ISA寄存器,而不是微架构寄存器。


Is there any substantial optimization when omitting the frame pointer? If I have understood correctly by reading this page, -fomit-frame-pointer is used when we want to avoid saving, setting up and restoring frame pointers.

Is this done only for each function call and if so, is it really worth to avoid a few instructions for every function? Isn't it trivial for an optimization. What are the actual implications of using this option apart from the debugging limitations?

I compiled the following C code with and without this option

int main(void)
{
        int i;

        i = myf(1, 2);
}

int myf(int a, int b)
{
        return a + b;
}

,

# gcc -S -fomit-frame-pointer code.c -o withoutfp.s
# gcc -S code.c -o withfp.s

.

diff -u 'ing the two files revealed the following assembly code:


--- withfp.s    2009-12-22 00:03:59.000000000 +0000
+++ withoutfp.s 2009-12-22 00:04:17.000000000 +0000
@@ -7,17 +7,14 @@
        leal    4(%esp), %ecx
        andl    $-16, %esp
        pushl   -4(%ecx)
-       pushl   %ebp
-       movl    %esp, %ebp
        pushl   %ecx
-       subl    $36, %esp
+       subl    $24, %esp
        movl    $2, 4(%esp)
        movl    $1, (%esp)
        call    myf
-       movl    %eax, -8(%ebp)
-       addl    $36, %esp
+       movl    %eax, 20(%esp)
+       addl    $24, %esp
        popl    %ecx
-       popl    %ebp
        leal    -4(%ecx), %esp
        ret
        .size   main, .-main
@@ -25,11 +22,8 @@
 .globl myf
        .type   myf, @function
 myf:
-       pushl   %ebp
-       movl    %esp, %ebp
-       movl    12(%ebp), %eax
-       addl    8(%ebp), %eax
-       popl    %ebp
+       movl    8(%esp), %eax
+       addl    4(%esp), %eax
        ret
        .size   myf, .-myf
        .ident  "GCC: (GNU) 4.2.1 20070719 

Could someone please shed light on the key points of the above code where -fomit-frame-pointer did actually make the difference?

Edit: objdump's output replaced with gcc -S's

解决方案

-fomit-frame-pointer allows one extra register to be available for general-purpose use. I would assume this is really only a big deal on 32-bit x86, which is a bit starved for registers.*

One would expect to see EBP no longer saved and adjusted on every function call, and probably some additional use of EBP in normal code, and fewer stack operations on occasions where EBP gets used as a general-purpose register.

Your code is far too simple to see any benefit from this sort of optimization-- you're not using enough registers. Also, you haven't turned on the optimizer, which might be necessary to see some of these effects.

* ISA registers, not micro-architecture registers.

这篇关于什么时候应该省略帧指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆