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

            Bug ID: 128627
           Summary: wrong code at -O2: loop distribution moves memset of
                    float stores above int reads of later malloc elements
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: alias, wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jeffchen006 at gmail dot com
  Target Milestone: ---

int printf(const char *, ...);
void *malloc(unsigned long);
int n = 4;
int o[4];
int main(void) {
  int *p = malloc(20);
  for (int i = 0; i < 5; i++) p[i] = i;
  for (int k = 0; k < n; k++) {
    ((float *)p)[k] = 0;
    o[k] = p[k + 1];
  }
  printf("%d\n", o[0]);
}

$ gcc -O0 t.c && ./a.out
1
$ gcc -O2 t.c && ./a.out
0

Expected: 1. o[0] reads the int at element 1 before element 1 gets the float 0
in the next iteration, so o[0] = 1. -fopt-info-loop: "Loop 2 distributed: split
to 0 loops and 2 library calls" (memset, then memcpy, in the wrong order). -O3
also fails on 8.5.0.

Suspected cause: tree-data-ref dr_may_alias_p uses TBAA for a write/read pair
that is a WAR dependence across iterations (rule from PR62031, 2014).

Avoided by: -fno-tree-loop-distribute-patterns, -fno-strict-aliasing.

Known to fail: 10.5.0, 11.4.0, 12.5.0, 13.4.0, 14.3.0, 15.2.0, 16.2.0, 17.0.

-O0 -fsanitize=undefined,address is clean.

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

Reply via email to