https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129239
Bug ID: 129239
Summary: [rs6000] Wrong code for __builtin_dmxor in a loop
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: kishan at gcc dot gnu.org
CC: avinashd at gcc dot gnu.org, jskumari at gcc dot gnu.org,
meissner at gcc dot gnu.org
Target Milestone: ---
Host: powerpc64le-linux-gnu
Target: power*-linux-gnu
Build: powerpc64le-linux-gnu
For testcase below,
void
dmxor_loop (__dmr1024 *dst, __dmr1024 *src, int n)
{
__dmr1024 acc = *dst;
__dmr1024 d1 = *src;
while (n-- > 0)
{
__dmr1024 tmp = acc;
__builtin_dmxor (&tmp, &d1); /* tmp = acc ^ d1 */
d1 = tmp;
}
*dst = d1;
}
https://godbolt.org/z/5n7vPohda
The loop does d1 = acc XOR d1 again and again, and acc stays the same the whole
time. The dmxor instruction writes its answer over its first input.
Generated code
d1 is loaded into %dm0 and acc into %dm1. The main loop is:
.L3:
dmxor %dm1,%dm0 # acc = acc ^ d1 <- acc is overwritten
dmxor %dm1,%dm1 # acc ^ acc = 0
dmxor %dm1,%dm1 # 0
dmxor %dm1,%dm1 # 0
dmmr %dm0,%dm1 # d1 = 0
bdnz .L3
The problem
GCC writes the answer over acc, even though acc is needed again in the next
step. From the second step on, it XORs %dm1 with itself, which is always 0. The
extra steps before the loop (.L15, .L14) overwrite acc in the same way. So the
result is wrong whenever n >= 2.
Expected
acc must keep its value for the whole loop. Every step should compute acc XOR
d1 using the original acc.