简体   繁体   English

如何在gcc中禁用尾部调用优化

[英]How do I disable tailcall optimizations in gcc

Wondering if anyone knows the flag for gcc to disable tailcall optimizations. 想知道是否有人知道gcc的标志来禁用尾部调用优化。 Basically in a tailcall optimization, gcc will replace a stack frame when the return value from a called function is passed through (via return) or nothing else happens in the function. 基本上,在尾调用优化中,当被调用函数的返回值通过(通过return)传递或函数中没有其他任何事情发生时,gcc将替换堆栈帧。

That is, in 也就是说,在

 void main() {
     foo();
 }

 void foo() {
     bar();
 }

 void bar() {
     /* at this point in code, the foo() stack frame no longer exists! */    
 }

When foo calls bar, gcc emits code that replaces the stack frame for foo, rather than adding a new stack frame. 当foo调用bar时,gcc发出的代码将替换foo的堆栈框架,而不是添加新的堆栈框架。

My company has a stack unwinder that can print out a stack trace from any point in code. 我公司有一个堆栈展开器,可以从代码的任何点打印出堆栈跟踪。 tailcall optimization makes stack frames disappear, which can confuse the stack trace somewhat. 尾调用优化使堆栈帧消失,这可能会使堆栈跟踪有些混乱。

I am compiling for x86-64 using gcc4.3. 我正在使用gcc4.3为x86-64进行编译。

Thanks in advance! 提前致谢! P P

GCC manual: GCC手册:

   -foptimize-sibling-calls
       Optimize sibling and tail recursive calls.

       Enabled at levels -O2, -O3, -Os.

So either compile with -O0 / -O1 , or use -fno-optimize-sibling-calls . 因此,可以使用-O0 / -O1进行编译,也可以使用-fno-optimize-sibling-calls进行编译。

Untested: -fno-optimize-sibling-calls 未经测试: -fno-optimize-sibling-calls

http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM