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

            Bug ID: 128650
           Summary: [14/15/16/17 Regression] wrong code at -O3: loop
                    distribution splits memcpy and element store with
                    overlapping byte ranges
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: needs-bisection, 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 *memcpy(void *, const void *, unsigned long);
long long a[12];
int n = 5;
int main(void) {
  for (int k = 1; k < n; k++) {
    memcpy((char *)a + 20 + k * 4, (char *)a + 12 + k * 4, 8);
    a[3 + 2 * k] = 7;
  }
  printf("%lld\n", a[5]);
}

$ gcc -O0 t.c && ./a.out
0
$ gcc -O3 t.c && ./a.out
7

Expected: 0. a[5] is stored (7) at k = 1. At k = 4 the memcpy writes bytes
36..43 (the low half of a[5]) with zero bytes, so a[5] = 0. "Loop distributed:
split to 2 loops".

Suspected cause: loop distribution does not see the byte-range dependence
between the memcpy partition and the element store.

Avoided by: -fno-tree-loop-distribution.

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

Known to work: 7.5.0, 8.5.0, 10.5.0, 11.4.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
  • [Bug tree-optimization/128650] N... jeffchen006 at gmail dot com via Gcc-bugs

Reply via email to