[ACCEPTED]-When should I omit the frame pointer?-compiler-construction

Accepted answer
Score: 35

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

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

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

* ISA 1 registers, not micro-architecture registers.

Score: 11

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

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

Score: 7

You can often get more meaningful assembly 9 code from GCC by using the -S argument to 8 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 7 the address, so we can compare the actual 6 instructions generated directly. For your 5 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 4 the code to push the frame pointer onto 3 the stack, and this changes the relative 2 address of the arguments passed to the function 1 on the stack.

Score: 5

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

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

Optimizations 2 without profiling are a waste of time and 1 money.

More Related questions