X86: Inefficient lowering of constant vector variable shl
| Bugzilla Link | 15141 |
| Version | trunk |
| OS | All |
| Depends On | llvm/llvm-bugzilla-archive#30787 llvm/llvm-bugzilla-archive#36319 |
| Reporter | LLVM Bugzilla Contributor |
| CC | @d0k,@delena,@RKSimon,@ZviRackover |
Extended Description
The code below is lowered to the following sequence.
define <4 x i32> @f22oo(<4 x i32> %in) {
entry:
%T = shl <4 x i32> %in, <i32 0, i32 1, i32 2, i32 4>
ret <4 x i32> %T
}
_f22oo: ## @f22oo
.cfi_startproc
## BB#0: ## %entry
vmovdqa LCPI2_0(%rip), %xmm1
vpslld $23, %xmm1, %xmm1
vpaddd LCPI2_1(%rip), %xmm1, %xmm1
vcvttps2dq %xmm1, %xmm1
vpmulld %xmm0, %xmm1, %xmm0
ret
.cfi_endproc
We could convert this code into "mul <1 , 2, 4, 8>".
mentioned in issue llvm/llvm-bugzilla-archive#36319
mentioned in issue llvm/llvm-bugzilla-archive#30787
Current Codegen: https://c.godbolt.org/z/hxcYb9T99
The original test case was fixed long ago.
The "{1,1,1,1} << {i, i+1, i+2, i+3}" case still has the problem identified in [Bug #30787] where we fail to vectorize the 'i' element as 'i+0'.
I am also worried about the cast of "1 << K", where 1 is a vector of 1s and K is a consecutive mask (i, i+1, i+2, i+3). We see this in a spec benchmark.
If we can confirm (i >= 0) then https://reviews.llvm.org/D26000 should help with this.
On AVX2 targets you do get VPSLLVD:
vpsllvd LCPI0_0(%rip), %xmm0, %xmm0
retq
The vpmulld codegen is for an AVX1 only target (btver2).
Older (pre-SSE4.1) targets are having to do the multiply the hard way but still appears to give better results than scalarization (but not by much - almost in the noise):
movdqa LCPI0_0(%rip), %xmm1 ## xmm1 = [1,2,4,16]
pshufd $245, %xmm0, %xmm2 ## xmm2 = xmm0[1,1,3,3]
pmuludq %xmm1, %xmm0
pshufd $232, %xmm0, %xmm0 ## xmm0 = xmm0[0,2,2,3]
pshufd $245, %xmm1, %xmm1 ## xmm1 = xmm1[1,1,3,3]
pmuludq %xmm2, %xmm1
pshufd $232, %xmm1, %xmm1 ## xmm1 = xmm1[0,2,2,3]
punpckldq %xmm1, %xmm0 ## xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
retq
On AVX2 we should use variable shift VPSLLVD xmm1, xmm2, xmm3/m128
Found this as I'm currently looking at missing vector constant folding. r213798 fixed the missing FP_TO_SINT vector constant fold:
vpmulld
LCPI0_0(%rip), %xmm0, %xmm0 retq
Nadav - I'm curious about optimizing the
consecutive mask shifts, can you point me to the particular spec benchmark that does this please?
Found this as I'm currently looking at missing vector constant folding. r213798 fixed the missing FP_TO_SINT vector constant fold:
vpmulld LCPI0_0(%rip), %xmm0, %xmm0 retq
Nadav - I'm curious about optimizing the consecutive mask shifts, can you point me to the particular spec benchmark that does this please?
I am also worried about the cast of "1 << K", where 1 is a vector of 1s and K is a consecutive mask (i, i+1, i+2, i+3). We see this in a spec benchmark.
With r174327 we're a lot closer:
cvttps2dq LCPI0_0(%rip), %xmm1
pmulld %xmm0, %xmm1
movdqa %xmm1, %xmm0
ret
there's still a missing vector FP_TO_SINT constant folding opportunity.