raja ashok raja ashok - 21 days ago 5
C Question

Trying to understand gcc option -fomit-frame-pointer

I asked Google to give me the meaning of the

gcc
option
-fomit-frame-pointer
, which redirects me to the below statement.


-fomit-frame-pointer
Don't keep the frame pointer in a register for functions that don't
need one. This avoids the instructions to save, set up and restore frame
pointers; it also makes an extra register available in many functions. It
also makes debugging impossible on some machines.


As per my knowledge for each function, an activation record will be created in stack of the process memory to keep all local variables and some more information. I hope this frame pointer means the address of the activation record of a function.

In this case what are the type of functions, for which it doesn't need to keep frame pointer in register? If I get this information, I will try to design the new function based on that (if possible) because if the frame pointer is not kept in registers, some instructions will be omitted in binary. This will really improves the performance noticeably in an application where there are many functions.

Answer

Most smaller functions don't need a frame-pointer - larger functions MAY need one.

It's really about how well the compiler manages to track how the stack is used, and where things are on the stack (local variables, arguments passed to the current function and arguments being prepared for a function about to be called). I don't think it's easy to characterize the functions that need or don't need a frame-pointer [technically, NO function HAS to have a frame-pointer - it's more a case of "if the compiler deems it necessary to reduce the complexity of other code"].

I don't think you should "attempt to make functions not have frame-pointer" as part of your strategy for coding - like I said, simple functions don't need them, so use -fomit-frame-pointer, and you'll get one more register available for the register allocator, and save 1-3 instructions on entry/exit to functions. If your function needs a frame-pointer, it's because the compiler decides that's a better option than using frame pointer. It's not a goal to have functions without frame pointer, it's a goal to have code that works both correctly and fast.

Note that "not having framepointer" should give better performance, but it's not some magic bullet that gives enormous improvements - particularly not on x86-64, which already has 16 registers to start with. On 32-bit x86, since it only has 8 registers, one of which is stackpointer, and taking up another as the frame-pointer means 25% of register-space is taken. To change that to 12.5% is quite an improvement. Of course, compiling for 64-bit will help quite a lot too.