https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86050

            Bug ID: 86050
           Summary: Inline break tail-call optimization
           Product: gcc
           Version: 6.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: asorenji at gmail dot com
  Target Milestone: ---

I have deep tail-recursion and try check how many bytes it consumed. Algorithm
for this very simple - pointer to variable in main function minus pointer to
variable in current recursive call. If function for this check have noinline
attribute, tail-call optimization doing well and my recursion consume very
little amount of memory. But if I remove noinline attribute, optimization don't
work and I get stack overflow.

OS - Debian Stretch.
g++ --version - g++ (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Command line - g++ -O2 main.cpp -o main
Code:

#include <iostream>

using namespace std;

struct Parent
{
    virtual void tailCall(const char*,long )const{}
};

struct Child:Parent
{
    //remove noinline and you get stack overflow
    static __attribute__((noinline)) void stackConsume(const char*stack){
        char value;
        std::cout<<"Consumed "<<stack-&value<<" bytes"<<std::endl;
    }

    void tailCall(const char *stack, long deep) const
    {
        if(deep==1000000000000L)
            return;
        stackConsume(stack);
        next->tailCall(stack,++deep);
    }
    Parent*next=this;
};

int main()
{
    Parent*parent=new Child;
    char stack;
    parent->tailCall(&stack,0);
    return 0;
}

Reply via email to