为什么 GCC 在 64 位上会丢弃帧指针?
默认情况下在 64 位架构上删除帧指针的基本原理是什么?我很清楚它可以被启用,但为什么 GCC 在为 32 位启用它时首先禁用它?毕竟64位CPU比32位CPU有更多的寄存器。
编辑:
看起来当使用更新的 GCC 版本时,x86 的帧指针也会被删除。从手册:
从 GCC 版本 4.6 开始,32 位 Linux x86 和 32 位 Darwin x86 目标的默认设置(未优化大小时)已更改为 -fomit-frame-pointer。通过使用
--enable-frame-pointer
配置选项配置 GCC,可以将默认值恢复为-fno-omit-frame-pointer
。
但为什么?
What's the rationale behind dropping the frame pointer on 64-bit architectures by default? I'm well aware that it can be enabled but why does GCC disable it in the first place while having it enabled for 32-bit? After all, 64-bit has more registers than 32-bit CPUs.
Edit:
Looks like the frame pointer will be also dropped for x86 when using a more recent GCC version. From the manual:
Starting with GCC version 4.6, the default setting (when not optimizing for size) for 32-bit Linux x86 and 32-bit Darwin x86 targets has been changed to -fomit-frame-pointer. The default can be reverted to
-fno-omit-frame-pointer
by configuring GCC with the--enable-frame-pointer
configure option.
But why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 x86-64,ABI (PDF) 鼓励不使用帧指针。其基本原理或多或少是“我们现在有了 DWARF,因此没有必要进行调试或异常展开;如果我们从第一天起就将其设为可选,那么没有软件将依赖于它的存在。”
x86-64 确实比 x86-32 有更多的寄存器,但它仍然不够。从编译器的角度来看,释放更多通用寄存器始终是一件好事。是的,需要堆栈抓取的操作速度较慢,但它们是罕见的事件,因此这是一个很好的权衡,可以减少每个子例程调用的几个周期,并减少堆栈溢出。
For x86-64, the ABI (PDF) encourages the absence of a frame pointer. The rationale is more or less "we have DWARF now, so it's not necessary for debugging or exception unwinding; if we make it optional from day one, then no software will come to depend on its existence."
x86-64 does have more registers than x86-32, but it still doesn't have enough. Freeing up more general-purpose registers is always a Good Thing from a compiler's point of view. The operations that require a stack crawl are slower, yes, but they are rare events, so it's a good tradeoff for shaving a few cycles off every subroutine call plus fewer stack spills.