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

            Bug ID: 92559
           Summary: Returning std∷map breaks tail-recursion optimization
           Product: gcc
           Version: 9.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Hi-Angel at yandex dot ru
  Target Milestone: ---

The following code, exploiting tail-recursion, does not get optimized into a
loop with -O3 option, so it crashes if you try to run it.

    #include <map>

    using MyMap = std::map<unsigned, unsigned>;

    MyMap foo(MyMap m) {
        if (m[0] == 0)
            return m;
        else {
            m[0] -= 1;
            return foo(m);
        }
    }

    int main() {
        MyMap m = {{0, 999999}};
        foo(m);
    }

While debugging GCC, I found that `find_tail_calls()` function quits at this
code¹:

          /* If the statement references memory or volatile operands, fail.  */
          if (gimple_references_memory_p (stmt)
          || gimple_has_volatile_ops (stmt))
        return;


1:
https://github.com/gcc-mirror/gcc/blob/38f05dc5db9241d3de8041a683972f086edce561/gcc/tree-tailcall.c#L464

I haven't found any duplicates of this so far.

Reply via email to