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

            Bug ID: 128944
           Summary: [16/17 Regression] wrong code at -O1: initialization
                    of a local array read by a callee is removed
           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: shimizu2486 at gmail dot com
  Target Milestone: ---

Live reproducer: https://godbolt.org/z/8WKMoecPT

This is reduced from the testcase of PR126658, with the array initialized to 0
instead of 1096435691.

$ cat t1.c
#include <stdio.h>
long b;
int e;
int f(int *p1, int i, int *p3) {
  int *g, c = 0;
  while (i--)
    g = p1 + 1;
  while (g != p1) {
    --g;
    b = g - p1;
    if (p3[b])
      c = 1;
  }
  return c;
}
int main(void) {
  int j = 0;
  printf("%d\n", f(&e, 1, &j));
}

f only reads p3[0], which is j = 0, so c stays 0 and the program should print
0.

$ gcc -O0 t1.c -o t1 && ./t1
0                                   (correct)
$ gcc -O1 t1.c -o t1 && ./t1
1                                   (wrong)

  x86_64 trunk (cdb8a10e1a82)    wrong
  x86_64 16.2.0                  wrong
  x86_64 15.3.0, 14.4.0, 13.5.0  correct

-O1 is wrong. -O0, -O2, -O3, -Os and -Og are correct
-fno-tree-dse or -fno-ivopts makes -O1 print 0 correctly.
The store j = 0 in main seems to be removed after dse2, so f reads an
uninitialized stack slot.

Reply via email to