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

            Bug ID: 128074
           Summary: wrong code at -O3 for a self-recursive call that
                    passes a struct to a const struct parameter
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kyuwoncho18 at gmail dot com
  Target Milestone: ---
            Target: x86_64-pc-linux-gnu

struct S { int x; };

__attribute__((noinline)) int f(const struct S a, int n)
{
  if (n) {
    struct S t = { a.x + 1 };
    return f(t, n - 1);
  }
  return a.x;
}

int main(void)
{
  volatile int n = 7;
  struct S a = { 0 };
  if (f(a, n) != 7)
    __builtin_abort();
}

godbolt: https://godbolt.org/z/TsKb38WWa

Each recursive call gets a new t with t.x = a.x + 1, so f(a, 7) returns 7. With
-O0, -O2, -O3 -fno-ipa-cp or -O3 -fno-optimize-sibling-calls the program exits
normally; with -O3 (or -O2 -fipa-cp-clone) f returns 0 and the program aborts.

Reply via email to