When should I omit the frame pointer?

后端 未结 4 874
悲哀的现实
悲哀的现实 2020-12-14 00:56

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

相关标签:
4条回答
  • 2020-12-14 01:12

    The only downside of omitting it is that debugging is much more difficult.

    The major upside is that there is one extra general purpose register which can make a big difference on performance. Obviously this extra register is used only when needed (probably in your very simple function it isn't); in some functions it makes more difference than in others.

    0 讨论(0)
  • 2020-12-14 01:28

    Profile your program to see if there is a significant difference.

    Next, profile your development process. Is debugging easier or more difficult? Do you spend more time developing or less?

    Optimizations without profiling are a waste of time and money.

    0 讨论(0)
  • 2020-12-14 01:32

    You can often get more meaningful assembly code from GCC by using the -S argument to output the assembly:

    $ gcc code.c -S -o withfp.s
    $ gcc code.c -S -o withoutfp.s -fomit-frame-pointer
    $ diff -u withfp.s withoutfp.s
    

    GCC doesn't care about the address, so we can compare the actual instructions generated directly. For your leaf function, this gives:

     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
    

    GCC doesn't generate the code to push the frame pointer onto the stack, and this changes the relative address of the arguments passed to the function on the stack.

    0 讨论(0)
  • 2020-12-14 01:34

    -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.

    0 讨论(0)
提交回复
热议问题