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

            Bug ID: 116231
           Summary: Missing optimization on global variable
           Product: gcc
           Version: 14.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yyc1992 at gmail dot com
  Target Milestone: ---

```
static int (*ptr)();

static int g()
{
    return 100;
}

void set_ptr()
{
    ptr = g;
}

int k()
{
    return ptr();
}
```

In the code above, even though `ptr` is not statically initialized to `g`, it
could only take the value of either `g` or `NULL` so for the `ptr()` to not be
UB, it could only possibly be `g`. GCC does not seems to be doing this
optimization.

Clang 17 does perform this optimization.

This appears in my code since the initialization code and the implementation of
the function was generated by a different program (cython in this case) and I
haven't found a way to move the initialization into the global scope yet.

Reply via email to