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

            Bug ID: 128839
           Summary: static chain and nested function
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: uecker at gcc dot gnu.org
  Target Milestone: ---

The following example makes sense, but fails at -O2 because tail call
optimization assumes the static chain is the same.


// [[gnu::noinline,gnu::noipa]]
int cb(int (*fp)(void *), void *p)
{
    return (*fp)(p);
}

int foo(int n)
{
    int bar(void *p)
    {
        if (p) 
            return __builtin_call_with_static_chain(bar((void*)0), p);
        return n;
    }

    return cb(__builtin_call_code_address(bar),
              __builtin_call_static_chain(bar));
}

int main()
{
    return foo(5);
}


Tentative fix:

gcc/tree-tailcall.cc
@@ -1052,7 +1052,12 @@ find_tail_calls (basic_block bb, edge esucc, struct
tailcall **ret,
       if (idx == gimple_call_num_args (call) && !param)
        tail_recursion = true;
     }
+  if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl
+      != gimple_call_chain (call))
+    tail_recursion = false;
   if (only_tailr && !tail_recursion)
     return;

Reply via email to