admin管理员组

文章数量:1584215

# gcc  -fomit-frame-pointer -o main main.c

大多数较小的函数不需要帧指针,而较大的函数可能需要。

这实际上取决于编译器如何跟踪堆栈的使用情况,以及堆栈上的位置(局部变量、传递给当前函数的参数,以及为即将被调用的函数准备的参数)。我认为要确定哪些函数需要或不需要帧指针并不容易(从技术上讲,没有哪个函数必须有帧指针--更多的情况是 “如果编译器认为有必要使用帧指针来降低其他代码的复杂性”)。

我不认为你应该把 “试图让函数不需要框架指针 ”作为编码策略的一部分--就像我说的,简单的函数不需要框架指针,所以使用-fomit-frame-pointer,你就能为寄存器分配器多获得一个可用寄存器,并在函数的进入/退出时节省 1-3 条指令。如果你的函数需要框架指针,那是因为编译器认为这比不使用框架指针更好。我们的目标不是让函数不使用帧指针,而是让代码既能正确运行又能快速运行。

需要注意的是,“不使用帧指针 ”应该会带来更好的性能,但这并不是什么能带来巨大改进的灵丹妙药,尤其是在 x86-64 上,因为它一开始就已经有 16 个寄存器了。在 32 位 x86 上,由于只有 8 个寄存器,其中一个是堆栈指针,占用另一个作为帧指针意味着占用了 25% 的寄存器空间。将这一比例改为 12.5%,是一个相当大的改进。当然,64 位编译也会有很大帮助。

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 - Trying to understand gcc option -fomit-frame-pointer - Stack Overflow


"-fomit-frame-pointer" 选项是 GCC(GNU 编译器集)和 Clang 等其他编译器中使用的编译器标志。启用该选项后,编译器会在汇编代码中省略帧指针的生成。

什么是帧指针?

帧指针(通常是 x86 中的寄存器 `%ebp` 或 x86-64 中的寄存器 `%rbp`)用于在函数执行过程中指向当前堆栈帧的底部。堆栈框架存放局部变量、函数参数和返回地址。

"-fomit-frame-pointer" 有什么作用?

使用 `-fomit-frame-pointer` 时,编译器不会为每次函数调用生成设置和维护帧指针寄存器的代码。相反,编译器可以将该寄存器用于其他目的,例如保存变量或中间值,这样可以使代码在速度和内存使用方面都略微高效一些。

优点

- 性能: 通过释放寄存器,编译器可以更灵活地优化代码,从而加快代码执行速度。

- 减少代码量: 由于无需设置和删除帧指针,因此生成的代码更少。

缺点

- 调试困难: 如果没有帧指针,堆栈跟踪就很难解释,从而使调试变得更具挑战性。调试器和剖析器等工具需要依赖帧指针才能轻松查看调用堆栈。

- 兼容性问题: 一些旧系统或工具可能需要帧指针,没有帧指针可能会出现问题。

典型用法:

- 该选项通常用于生产构建,因为生产构建对性能要求很高,而调试信息的优先级较低。对于调试至关重要的开发构建,通常会保留帧指针。

总之,“-fomit-frame-pointer” 是一个有用的优化工具,但在使用时应考虑到对调试和堆栈跟踪生成的潜在影响。

The `-fomit-frame-pointer` option is a compiler flag used in GCC (GNU Compiler Collection) and other compilers like Clang. When this option is enabled, the compiler omits the generation of a frame pointer in the assembly code.

What is a Frame Pointer?

A frame pointer (typically the register `%ebp` in x86 or `%rbp` in x86-64) is used to point to the base of the current stack frame during function execution. The stack frame holds local variables, function arguments, and return addresses.

What Does `-fomit-frame-pointer` Do?

When you use `-fomit-frame-pointer`, the compiler does not generate code to set up and maintain the frame pointer register for each function call. Instead, it can use that register for other purposes, such as holding variables or intermediate values, which can make the code slightly more efficient in terms of both speed and memory usage.

Advantages:

- Performance: By freeing up a register, the compiler has more flexibility in optimizing code, potentially leading to faster execution.

- Reduced code size: Less code is generated because there’s no need to set up and tear down the frame pointer.

Disadvantages:

- Debugging difficulty: Without a frame pointer, stack traces can be harder to interpret, making debugging more challenging. Tools like debuggers and profilers rely on the frame pointer to walk the call stack easily.

- Compatibility: Some older systems or tools might expect a frame pointer and could have issues without it.

Typical Usage:

- This option is commonly used in production builds where performance is critical, and debugging information is less of a priority. For development builds where debugging is essential, the frame pointer is usually retained.

In summary, `-fomit-frame-pointer` is a useful optimization tool but should be used with consideration of the potential impact on debugging and stack trace generation.

本文标签: 选项omitframepointer