https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106346
Bug ID: 106346
Summary: Potential regression on vectorization of left shift
with constants
Product: gcc
Version: 13.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: manolis.tsamis at vrull dot eu
Target Milestone: ---
Target: aarch64
Created attachment 53317
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53317&action=edit
Does not vectorize on GCC > 10.3
The following test case:
void foo (uint32_t dst[8], uint8_t src1[8], uint8_t src2[8])
{
uint16_t diff_e0 = src1[0] - src2[0];
uint16_t diff_e1 = src1[1] - src2[1];
uint16_t diff_e2 = src1[2] - src2[2];
uint16_t diff_e3 = src1[3] - src2[3];
uint16_t diff_e4 = src1[4] - src2[4];
uint16_t diff_e5 = src1[5] - src2[5];
uint16_t diff_e6 = src1[6] - src2[6];
uint16_t diff_e7 = src1[7] - src2[7];
uint32_t a0 = diff_e0 << 1;
uint32_t a1 = diff_e1 << 3;
uint32_t a2 = diff_e2 << 4;
uint32_t a3 = diff_e3 << 2;
uint32_t a4 = diff_e4 << 12;
uint32_t a5 = diff_e5 << 11;
uint32_t a6 = diff_e6 << 9;
uint32_t a7 = diff_e7 << 3;
dst[0] = a0;
dst[1] = a1;
dst[2] = a2;
dst[3] = a3;
dst[4] = a4;
dst[5] = a5;
dst[6] = a6;
dst[7] = a7;
}
Compiles at -O3 to nice vectorized code by loading the constants from memory in
GCC 10.3:
ldr d0, [x1]
adrp x3, .LC0
ldr d1, [x2]
adrp x1, .LC1
ldr q3, [x3, #:lo12:.LC0]
usubl v0.8h, v0.8b, v1.8b
ldr q2, [x1, #:lo12:.LC1]
uxtl v1.4s, v0.4h
uxtl2 v0.4s, v0.8h
sshl v1.4s, v1.4s, v3.4s
sshl v0.4s, v0.4s, v2.4s
stp q1, q0, [x0]
ret
But this has regressed in later releases, with GCC still loading the constants
from memory but also emitting a lot of scalar code before that. For example GCC
13 produces:
adrp x3, .LC0
ldrb w6, [x1, 4]
fmov d0, x6
ldrb w7, [x1]
ldr q5, [x3, #:lo12:.LC0]
fmov d1, x7
ldrb w3, [x1, 5]
ldrb w4, [x1, 1]
ldrb w8, [x2, 4]
ldrb w5, [x2, 5]
ins v0.h[1], w3
ldrb w6, [x2]
fmov d2, x8
ldrb w3, [x2, 1]
fmov d3, x6
ins v2.h[1], w5
ins v1.h[1], w4
ldrb w9, [x1, 2]
ins v3.h[1], w3
ldrb w8, [x1, 6]
ldrb w7, [x2, 2]
ldrb w6, [x2, 6]
ins v1.h[2], w9
ins v0.h[2], w8
ldrb w5, [x1, 3]
ins v3.h[2], w7
ldrb w4, [x1, 7]
ins v2.h[2], w6
ldrb w1, [x2, 7]
ldrb w3, [x2, 3]
ins v1.h[3], w5
ins v0.h[3], w4
ins v2.h[3], w1
ins v3.h[3], w3
adrp x1, .LC1
ldr q4, [x1, #:lo12:.LC1]
sub v1.4h, v1.4h, v3.4h
sub v0.4h, v0.4h, v2.4h
uxtl v1.4s, v1.4h
uxtl v0.4s, v0.4h
sshl v1.4s, v1.4s, v5.4s
sshl v0.4s, v0.4s, v4.4s
stp q1, q0, [x0]
ret
Interestingly, this happens only with left shift and not with right shift.
GCC 10.3 vs trunk comparison: https://godbolt.org/z/xWbfGdfen