https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129048
Bug ID: 129048
Summary: [15/16/17 Regression] wrong code at -O3 -mmovbe: store
into a scalar_storage_order("big-endian") struct
scheduled after a memcpy load that reads it
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: 0599jiangyc at gmail dot com
Target Milestone: ---
The following code:
```c
/* GCC 15/trunk: wrong code at -O3 -mmovbe (also -O3 -march=x86-64-v3, -O3
-flto).
* The store t.a[i] += 1 into a big-endian scalar_storage_order struct is
scheduled
* after the 8-byte load that reads it back through memcpy.
* Correct at -O0/-O1/-O2, with -fno-strict-aliasing, -fstack-reuse=none,
-fno-dse or
* -fno-schedule-insns2, and with GCC 14.2.
* Godbolt: x86-64 gcc (trunk), flags: -O3 -mmovbe
* EXPECTED: prints 4965f6d458b1411d, exit 0. BUGGY: abort.
*/
#include <string.h>
struct __attribute__((scalar_storage_order("big-endian"))) S { unsigned a[2];
int b; };
__attribute__((noinline)) unsigned long long f(int i) {
static const struct S C = { { 0x7304, 0xd50d }, 0x14ac };
struct { struct S s; unsigned char b[12]; } u; memset(&u, 0, sizeof u);
{ struct S t = u.s; t.a[i] += 1; u.s = t; }
unsigned long long r = u.s.b + C.a[i], t;
memcpy(&t, &u.s, 8); r ^= t;
u.s = C;
memcpy(u.b, &u.s, 12);
for (int j = 0; j < 12; j++) r = r * 257 + u.b[j];
return r;
}
int main(void) {
unsigned long long r = f(0);
__builtin_printf("%llx\n", r);
if (r != 0x4965f6d458b1411dULL) __builtin_abort();
return 0;
}
```
Resulted in this output:
```
$ gcc -O3 -mmovbe repro.c && ./a.out
Aborted
(gcc -O0/-O1/-O2, -O3 -mmovbe -fno-strict-aliasing, -fstack-reuse=none,
-fno-dse or -fno-schedule-insns2, and GCC 14.2 print 4965f6d458b1411d)
```
Trunk: https://godbolt.org/z/Kzdbc1Tq7 (x86-64 gcc trunk, -O3 -mmovbe,
executed; reproduces as of 2026-11-13; also GCC 15.2, not GCC 14.2)