http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52067

             Bug #: 52067
           Summary: force sibling call optimization
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: trashyan...@wp.pl


code:
-------------------------------
-------------------------------
template<typename T>
int loop(int a) __attribute__((optimize(3)));

template<typename T>
int loop(int a)
{
    T zz;
    a -= 1;
    if(a>0)
        return loop<T>(a);
    return a;
}

class pod
{
};
class not_pod
{
public:
    not_pod() {}
};

void main()
{
    loop<pod>(10); //use tail call
    loop<not_pod>(10); //dont use tail call
}
-------------------------------
-------------------------------

I want use sibling call optimization and build around this my system.
But because C++ types can break this optimization without any waring, I could
get unexpected stack overflow.

It would be good if GCC have attribute that force function to use sibling call
and if that is impossible, break compilation with error.

something like this:
-------------------------------
-------------------------------
int foo(int a) __attribute__((siblingcall));
int foo(int a)
{
    std::string b = "abc"; //Error, have destructor
    {
        std::string b2 = "zzz"; //OK, destroyed before any tail call
    }
    if(a==1)
        return foo(a+1); //OK
    if(a==2)
        return 1+foo(a+1); //Error, except tail call
    if(a==3)
    {
        int c = foo(a+1); //OK, normal call
        return c;
    }
    return 0; //OK
}
-------------------------------
-------------------------------

Reply via email to