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

            Bug ID: 128746
           Summary: wrong code at -O2: PRE phi-translation of a load
                    through a pointer PHI ignores a same-type store from
                    an earlier iteration of the enclosing loop
           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: jeffchen006 at gmail dot com
  Target Milestone: ---
            Target: x86_64-pc-linux-gnu

#include <cstdio>
int a[3], h;
int main() {
  for (int n = 0; n < 3; n++) {
    a[1] = 0;
    for (int i = 0; i < n; i++) {
      int *p = &a[0];
      for (int j = i; j < 3; j++) {
        if (j != i)
          p[0] = n;
        h ^= p[1];
        p = &a[1];
      }
    }
  }
  printf("%d\n", h);
}

$ g++ -O0 t.cpp && ./a.out
2
$ g++ -O2 t.cpp && ./a.out
0

-O2 and -O3 fail; -O1, -O2 -fno-tree-pre and -O2 -fno-tree-ch give 2.
-fno-strict-aliasing, -fno-code-hoisting and -fno-tree-partial-pre do not help.
All accesses are int: this is not a TBAA problem.

Expected 2: for n = 2, the i = 0 inner loop stores a[1] = 2 through p == &a[1]
(j = 1). Then for i = 1, j = 1, p is &a[0] and p[1] is a[1], which must be 2.

In the pre dump the load *p in the inner loop header gets `prephitmp = PHI
<pretmp (latch: a[2]), 0 (preheader)>`. The preheader is reached both from
`a[1] = 0` and from the i-loop latch, after the inner loop has stored n through
p == &a[1].

Suspected cause (not confirmed in the code): phi-translation / ANTIC in
tree-ssa-pre.cc. When p[1] is phi-translated to a[1] at the preheader, the
alias walk back through the i loop checks the store *p with p also translated
(to &a[0], its value at the preheader), so the store from an earlier iteration
is judged not to alias a[1]. This looks like the "value classes are
context-free" disconnect in PR128638 comment 6, but here all loads have the
same type and no TBAA is involved.

A form with globals fails at -O2 on 7.5, 8.5, 10.5 and 12.5 to trunk (the small
form above fails from 12.5).

-O0 -fsanitize=undefined,address is clean.


Second testcase (same PR; judge3: same mechanism through partial-partial PRE,
do_pre_partial_partial_insertion):

int printf(const char *, ...);
short C[8];
int a;
long h;
short t;
long f(int b, int n) {
  short *p = C;
  h = p[1];
  for (int i = 0; i < n; i++) {
    if (a != -7)
      p[1] = b;
    h = p[0];
    if (n * b != 0)
      p = &C[1];
  }
  *p = t;
  return h;
}
int main(void) {
  long r = 0;
  for (int it = 0; it < 3; it++)
    r = f(it, it + 1);
  printf("%lx\n", r);
}

$ gcc -O2 t.c && ./a.out
2
$ gcc -O3 t.c && ./a.out
0

-O3 -fno-tree-partial-pre, -fno-tree-pre and -fno-tree-ch give 2. In iteration
1 (p == C) the store p[1] = b writes C[1]; from iteration 2 on p == &C[1] and
p[0] must read b. The pre dump has `prephitmp_34 = PHI <prephitmp_38(6),
_1(7)>`, where bb 7 is `p = &C[1]` and `_1 = MEM[&C + 2B]` is the entry load
from before the loop store `MEM[p_26 + 2B] = (short) b` (p_26 == &C). Fails on
8.5 and 12.5 to trunk at -O3 (10.5 hides it).

gcc -Wall -Wextra gives no warnings. -fno-strict-aliasing -fwrapv does not
change the output.

Possible fix: We are not sure of the right fix. A minimal guard would be to not
phi-translate a load through a pointer PHI past a may-alias store of the same
type that is done through another value of that PHI (that is, to also do the
alias walk with the untranslated pointer), so that a store from an earlier
iteration of the enclosing loop is seen.

gcc version 17.0.0 20261114 (experimental), r17-6275-gfceddda853f,
x86_64-pc-linux-gnu
Configured with: --enable-languages=c,c++ --disable-multilib
--disable-bootstrap --enable-checking=release

Reply via email to