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

            Bug ID: 128741
           Summary: wrong code at -O3: if-conversion's introduced load of
                    a conditionally stored location uses the store's alias
                    set; VN answers it past a store of another type, and
                    the store is deleted
           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>
#include <new>
int n = 8, x = 3;
static void f(unsigned char *b) {
  for (int k = 0; k < n; k++) {
    new (b + k * 8) long(x);
    if ((k ^ x) & 1)
      new (b + k * 8) short(1);
    else
      new (b + k * 8) long long(2);
  }
}
int main() {
  unsigned char *b = new unsigned char[64];
  f(b);
  printf("%d %d\n", b[0], b[8]);
}

$ g++ -O0 t.cpp && ./a.out
1 2
$ g++ -O3 t.cpp && ./a.out
3 2

int printf(const char *, ...);
void *malloc(unsigned long);
int n = 8, x = 3;
static void f(unsigned char *b) {
  for (int k = 0; k < n; k++) {
    *(long *)(b + k * 8) = x;
    if ((k ^ x) & 1)
      *(short *)(b + k * 8) = 1;
    else
      *(long long *)(b + k * 8) = 2;
  }
}
int main(void) {
  unsigned char *b = malloc(64);
  f(b);
  printf("%d %d\n", b[0], b[8]);
}

$ gcc -O0 t.c && ./a.out
1 2
$ gcc -O3 t.c && ./a.out
3 2

-O3 fails; -O2, -O3 -fno-strict-aliasing, -O3 -fno-tree-loop-vectorize and -O3
-fdisable-tree-ifcvt give 1 2.

Expected 1 2: slot 0 gets the long 3 and then (k = 0, (0 ^ 3) & 1 = 1) the
short 1 in its low 2 bytes, so b[0] = 1. Every store gives its bytes a new
type; the result is read only as unsigned char.

Cause (suspected): predicate_statements (tree-if-conv.cc). The slot is written
on both paths, so each conditional store becomes `tmp = <lhs>; val = c ? rhs :
tmp; <lhs> = val`, with

              lhs = ifc_temp_var (type, unshare_expr (lhs), &gsi);

that is, a new load in the store's own type and alias set, which now also runs
on the path where that store did not run (ifcvt dump: `_ifc__41 = MEM[(short
int *)_13];`, ..., `MEM[(long long int *)...] = _ifc__46;`). The local VN run
after if-conversion then answers the long long load with TBAA: it walks past
the short store (no alias by TBAA) and forwards x. The short store is then dead
before the unconditional 8-byte store and is removed. phiopt's cselim does the
same kind of transform with a ref-all load (`MEM <long long int> [(void *)p]`)
and is correct. Building the introduced load with alias set 0 (ref-all), as
cselim does, should fix it.

This is the same theme as PR128638 / PR128645 (TBAA used on a
compiler-introduced access), in another pass. 12.5 is correct, 13.4 fails (not
bisected).

-O0 -fsanitize=undefined,address is clean (C and C++).

gcc -Wall -Wextra gives no warnings. -fno-strict-aliasing hides it, as expected
for a TBAA bug. The program is still valid: every store gives its bytes a new
effective type in malloc memory (C11 6.5p6; placement new in C++), and the
result is read only as unsigned char. -fwrapv also hides it, but no signed
arithmetic overflows (k * 8 <= 56).

Possible fix: One possible fix is to build the load that predicate_statements
(tree-if-conv.cc) adds for a conditional store with alias set 0 (a ref-all
MEM_REF, as phiopt's cselim does for the same transform), so that the later VN
cannot use TBAA to skip stores of other types for it.

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