https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128995
Bug ID: 128995
Summary: arm: wrong code at -O2: peephole2 merges two stores of
one register into an strd with a stale constant
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: shimizu2486 at gmail dot com
Target Milestone: ---
Target: arm-linux-gnueabihf
Live reproducer: https://godbolt.org/z/dfG77hPGc
$ cat t1.c
#include <stdio.h>
_BitInt(96) a[2];
unsigned long long b;
int d, m[4][4];
unsigned g;
__attribute__((noipa)) long long arg(void) { return -1; }
int main(void) {
long long x = arg();
int z = x;
int r;
_BitInt(96) v = g = __builtin_sub_overflow(0, (int)x, &r) ?: r;
if (x) {
v = 65537 >= (unsigned)a || m[0][0] || x >= z;
int l = 0, q;
while (__builtin_mul_overflow(l, (int)g, &q) ?: q)
l = v;
v = l;
} else
z = 0;
m[g & 3][m[0][v & 3] & 3] = 0;
d = m[z & 3][0];
b = v ? -g : 0;
printf("%llu\n", (long long)((a[1] >> 1) + b));
}
$ arm-linux-gnueabihf-gcc -marm -march=armv7-a+fp -mfpu=vfpv3-d16 -O0 -static
t1.c -o t1 && qemu-arm ./t1
0 (correct)
$ arm-linux-gnueabihf-gcc -marm -march=armv7-a+fp -mfpu=vfpv3-d16 -O2 -static
t1.c -o t1 && qemu-arm ./t1
4294967295 (wrong)
arm trunk (2f817b3ea9e9) wrong
arm 16.2.0 wrong
-O2 and -O3 are wrong. -O0, -O1 and -Os are correct, and so is -O2 with
-mthumb.
-fno-peephole2, -fno-crossjumping or -fno-dce gives 0 correctly.
Without peephole2 the two word stores are
```
movne r3, #1
movne r3, #0
strne r3, [sp]
strne r3, [sp, #4]
```
with it they become
```
movne r3, #0
movne r2, #1
strdne r2, [sp]
```
so the low word of v gets 1.