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 of each function, an activation record will be created in the 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 the frame pointer in a 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 improve the performance noticeably in an application where there are many functions.

推荐答案

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 a 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 not using a frame pointer. It's not a goal to have functions without a frame pointer, it's a goal to have code that works both correctly and fast.

Note that "not having a frame pointer" 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 the stack pointer, 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.

C++相关问答推荐

修改pGM使用指针填充2-D数组但不起作用

以c格式打印时间戳

为什么写入系统调用打印的字符数不正确?

exit(EXIT_FAILURE):Tcl C API类似功能

返回一个包含数组的 struct

使用NameSurname扫描到两个单独的字符串

使用额外的公共参数自定义printf

无效指针值在函数调用之间莫名其妙地改变

正确的TCP/IP数据包 struct

_泛型控制表达式涉及数组碰撞警告的L值转换错误?

平均程序编译,但结果不好

错误Cygwin_Except::Open_stackdupfile:正在转储堆栈跟踪是什么?

如何使解释器存储变量

用C语言计算文本文件中的整数个数

OpenSSL:如何将吊销列表与SSL_CTX_LOAD_VERIFY_LOCATIONS一起使用?

处理来自浏览器的HTTP请求

将字符串数组传递给C中的函数:`str[dim1][str_size]`vs`*str[dim1]`

将char*铸造为空**

我该如何处理这个 C 90 代码中的内存泄漏?

从寄存器移动到频繁访问的变量时性能意外降低