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

            Bug ID: 127968
           Summary: [16/17 Regression] wrong code at -O1, -O2 and -Os with
                    partially overlapping memcpy in a 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: kyuwoncho18 at gmail dot com
  Target Milestone: ---
            Target: x86_64-pc-linux-gnu

char b[8] = {1, 2, 3, 4};
int c[4];
int main(void) {
  for (int k = 0; k < 4; k++) {
    int t;
    __builtin_memcpy(&t, b, 4);
    __builtin_memcpy(b + k, &t, 4);
    c[k] = t;
  }
  return b[6];
}

godbolt: https://godbolt.org/z/dsceK7GT1

With -O0, -O3 or clang -O2, it returns 1, but with GCC -O1, -O2 or -Os, it
returns 4 (GCC 15.3 returns 1 at all levels; GCC 16.1 and 16.2 return 4 at -O1,
-O2 and -Os). -fno-tree-loop-im avoids it. Still reproduces after r16-6340
(PR123067).

Disclaimer: This test case was generated by a fuzzer, and the report was
written with LLM assistance.

Reply via email to