vs2010 c++ tail call optimization

时光怂恿深爱的人放手 提交于 2019-12-04 03:05:13

when the original is compiled, the assembly at the callsite has partial inlining of fac_aux, specifically the x - 1 part, which is required for the tail recursion, but using fac_aux prevents the partial inlining and thus the tail recursion optimization:

TestThin.fac_aux 013B1000   CMP ECX,1
013B1003                    JE SHORT TestThin.013B100E
013B1005                    IMUL EAX,ECX
013B1008                    DEC ECX
013B1009                    CMP ECX,1
013B100C                    JNZ SHORT TestThin.013B1005
013B100E                    RETN
013B100F                    INT3
TestThin.main 013B1010      MOV EAX,32
013B1015                    LEA ECX,DWORD PTR DS:[EAX-1] ;notice the partial inlining of x - 1
013B1018                    CALL TestThin.fac_aux

I tried the following code

#include "stdafx.h"

int f( size_t i, int x )
{
    return ( ( i < 2 ) ? x : f( i - 1, i * x ) );
}

int f( size_t i )
{
    return ( f( i, 1 ) );
}

int _tmain(int argc, _TCHAR* argv[])
{
    {
        f( 0 );
    }

    return 0;
}

and used the full optimization /Ox but I did not get the tail recursion. So it seems that MS VC++ 2010 does not support the tail recursion.

Try making the functions explicitly inline – furthermore, what optimization level are you using?

I don't know if it will work, but try to replace if ... else with single return statement:

return (x == 1) ? res : fac_aux( x - 1, res * x );

Looks weird, are you doing some kind of incremental compile. Other than that, it might be the fact that compiler gets confused by the multiple parameters, in the working version there's effectively only one parameter, somehow the optimization doesn't qualify anymore.

You could try making the res parameter a global, I its know messy bad practice, but it might work.

Sounds like a compiler bug/feature.

/Tony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!