GCC Return optimiztion

前端 未结 1 1134
无人共我
无人共我 2021-01-14 17:05

I\'d like to know if GCC can optimize code like

int foo(args) {
    if(is_true) {
        do_smth;
        n = call_func(args);
        do_smth;
        retu         


        
相关标签:
1条回答
  • 2021-01-14 17:11

    This is called tail-call optimization (it is not specified in any C standard; in contrast, some languages -e.g. Scheme and Ocaml- specify that tail-call optimization is required to happen). In some cases, recent GCC compilers can do that optimization.

    But it really depends upon the details, in particular of the actual arguments passed to call_func

    If you depend on it, please comment your code and check with gcc -fverbose-asm -O2 -S that your compiler is doing that.

    Notice that this optimization is not required, and might be compiler, compilation flags, processor and ABI specific.

    (so it could work on x86-64 but not on 32 bits ia32 or ARM; you really should check!)

    0 讨论(0)
提交回复
热议问题