https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126259
Bug ID: 126259
Summary: AArch64: Inefficient code in SAD tail loop under -O2
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: pfustc at gcc dot gnu.org
Target Milestone: ---
The reproducer is below loop with an SAD (Sum of Absolute Difference)
reduction.
#include <stdint.h>
#include <stdlib.h>
int
foo (uint8_t *p1, uint8_t *p2)
{
int sum = 0;
for (int i = 0; i < 20; i++)
sum += abs (p1[i] - p2[i]);
return sum;
}
When compiling under "-O2":
foo:
ldr q29, [x0]
mov x3, 16
ldr q30, [x1]
uabdl2 v31.8h, v29.16b, v30.16b
uabal v31.8h, v29.8b, v30.8b
uaddlv s31, v31.8h
.L2:
ldrb w2, [x0, x3]
ldrb w4, [x1, x3]
add x3, x3, 1
subs w2, w2, w4
csneg w2, w2, w2, pl
fmov s30, w2
add v31.2s, v31.2s, v30.2s
cmp x3, 20
bne .L2
fmov w0, s31
ret
An unnecessary vector add instruction "add v31.2s, v31.2s, v30.2s" is generated
for a scalar add in the tail loop.