Skip to content

Commit e30d29e

Browse files
committed
[X86][SSE] getFauxShuffleMask - peek through TRUNCATE/AEXT/ZEXT for INSERT_VECTOR_ELT(EXTRACT_VECTOR_ELT())
As long we extract from a source vector with smaller elements and we zero-extend the element in the final shuffle mask then we can safely peek through truncations and any/zero-extensions to find the source extraction.
1 parent a5fa5f7 commit e30d29e

File tree

4 files changed

+34
-38
lines changed

4 files changed

+34
-38
lines changed

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7417,15 +7417,20 @@ static bool getFauxShuffleMask(SDValue N, const APInt &DemandedElts,
74177417
}
74187418
}
74197419

7420+
// Peek through trunc/aext/zext.
7421+
// TODO: aext shouldn't require SM_SentinelZero padding.
7422+
// TODO: handle shift of scalars.
7423+
while (Scl.getOpcode() == ISD::TRUNCATE ||
7424+
Scl.getOpcode() == ISD::ANY_EXTEND ||
7425+
Scl.getOpcode() == ISD::ZERO_EXTEND)
7426+
Scl = Scl.getOperand(0);
7427+
74207428
// Attempt to find the source vector the scalar was extracted from.
7421-
// TODO: Handle truncate/zext/shift of scalars.
74227429
SDValue SrcExtract;
7423-
if ((Scl.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
7424-
Scl.getOperand(0).getValueType() == VT) ||
7425-
(Scl.getOpcode() == X86ISD::PEXTRW &&
7426-
Scl.getOperand(0).getValueType() == MVT::v8i16) ||
7427-
(Scl.getOpcode() == X86ISD::PEXTRB &&
7428-
Scl.getOperand(0).getValueType() == MVT::v16i8)) {
7430+
if ((Scl.getOpcode() == ISD::EXTRACT_VECTOR_ELT ||
7431+
Scl.getOpcode() == X86ISD::PEXTRW ||
7432+
Scl.getOpcode() == X86ISD::PEXTRB) &&
7433+
Scl.getOperand(0).getValueSizeInBits() == NumSizeInBits) {
74297434
SrcExtract = Scl;
74307435
}
74317436
if (!SrcExtract || !isa<ConstantSDNode>(SrcExtract.getOperand(1)))
@@ -7437,8 +7442,7 @@ static bool getFauxShuffleMask(SDValue N, const APInt &DemandedElts,
74377442
unsigned NumZeros =
74387443
std::max<int>((NumBitsPerElt / SrcVT.getScalarSizeInBits()) - 1, 0);
74397444

7440-
if (SrcVT.getSizeInBits() != VT.getSizeInBits() ||
7441-
(NumSrcElts % NumElts) != 0)
7445+
if ((NumSrcElts % NumElts) != 0)
74427446
return false;
74437447

74447448
unsigned SrcIdx = SrcExtract.getConstantOperandVal(1);

llvm/test/CodeGen/X86/buildvec-extract.ll

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -293,24 +293,19 @@ define <2 x i64> @extract2_i32_zext_insert1_i64_undef(<4 x i32> %x) {
293293
define <2 x i64> @extract2_i32_zext_insert1_i64_zero(<4 x i32> %x) {
294294
; SSE2-LABEL: extract2_i32_zext_insert1_i64_zero:
295295
; SSE2: # %bb.0:
296-
; SSE2-NEXT: pshufd {{.*#+}} xmm0 = xmm0[2,3,0,1]
297-
; SSE2-NEXT: movd %xmm0, %eax
298-
; SSE2-NEXT: movq %rax, %xmm0
299-
; SSE2-NEXT: pslldq {{.*#+}} xmm0 = zero,zero,zero,zero,zero,zero,zero,zero,xmm0[0,1,2,3,4,5,6,7]
296+
; SSE2-NEXT: andps {{.*}}(%rip), %xmm0
300297
; SSE2-NEXT: retq
301298
;
302299
; SSE41-LABEL: extract2_i32_zext_insert1_i64_zero:
303300
; SSE41: # %bb.0:
304-
; SSE41-NEXT: extractps $2, %xmm0, %eax
305-
; SSE41-NEXT: movq %rax, %xmm0
306-
; SSE41-NEXT: pslldq {{.*#+}} xmm0 = zero,zero,zero,zero,zero,zero,zero,zero,xmm0[0,1,2,3,4,5,6,7]
301+
; SSE41-NEXT: xorps %xmm1, %xmm1
302+
; SSE41-NEXT: blendps {{.*#+}} xmm0 = xmm1[0,1],xmm0[2],xmm1[3]
307303
; SSE41-NEXT: retq
308304
;
309305
; AVX-LABEL: extract2_i32_zext_insert1_i64_zero:
310306
; AVX: # %bb.0:
311-
; AVX-NEXT: vextractps $2, %xmm0, %eax
312-
; AVX-NEXT: vmovq %rax, %xmm0
313-
; AVX-NEXT: vpslldq {{.*#+}} xmm0 = zero,zero,zero,zero,zero,zero,zero,zero,xmm0[0,1,2,3,4,5,6,7]
307+
; AVX-NEXT: vxorps %xmm1, %xmm1, %xmm1
308+
; AVX-NEXT: vblendps {{.*#+}} xmm0 = xmm1[0,1],xmm0[2],xmm1[3]
314309
; AVX-NEXT: retq
315310
%e = extractelement <4 x i32> %x, i32 2
316311
%z = zext i32 %e to i64
@@ -386,16 +381,22 @@ define <2 x i64> @extract0_i16_zext_insert0_i64_undef(<8 x i16> %x) {
386381
}
387382

388383
define <2 x i64> @extract0_i16_zext_insert0_i64_zero(<8 x i16> %x) {
389-
; SSE-LABEL: extract0_i16_zext_insert0_i64_zero:
390-
; SSE: # %bb.0:
391-
; SSE-NEXT: pextrw $0, %xmm0, %eax
392-
; SSE-NEXT: movd %eax, %xmm0
393-
; SSE-NEXT: retq
384+
; SSE2-LABEL: extract0_i16_zext_insert0_i64_zero:
385+
; SSE2: # %bb.0:
386+
; SSE2-NEXT: pextrw $0, %xmm0, %eax
387+
; SSE2-NEXT: movd %eax, %xmm0
388+
; SSE2-NEXT: retq
389+
;
390+
; SSE41-LABEL: extract0_i16_zext_insert0_i64_zero:
391+
; SSE41: # %bb.0:
392+
; SSE41-NEXT: pxor %xmm1, %xmm1
393+
; SSE41-NEXT: pblendw {{.*#+}} xmm0 = xmm0[0],xmm1[1,2,3,4,5,6,7]
394+
; SSE41-NEXT: retq
394395
;
395396
; AVX-LABEL: extract0_i16_zext_insert0_i64_zero:
396397
; AVX: # %bb.0:
397-
; AVX-NEXT: vpextrw $0, %xmm0, %eax
398-
; AVX-NEXT: vmovd %eax, %xmm0
398+
; AVX-NEXT: vpxor %xmm1, %xmm1, %xmm1
399+
; AVX-NEXT: vpblendw {{.*#+}} xmm0 = xmm0[0],xmm1[1,2,3,4,5,6,7]
399400
; AVX-NEXT: retq
400401
%e = extractelement <8 x i16> %x, i32 0
401402
%z = zext i16 %e to i64

llvm/test/CodeGen/X86/buildvec-insertvec.ll

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ define void @foo(<3 x float> %in, <4 x i8>* nocapture %out) nounwind {
2121
; SSE41-LABEL: foo:
2222
; SSE41: # %bb.0:
2323
; SSE41-NEXT: cvttps2dq %xmm0, %xmm0
24-
; SSE41-NEXT: pextrb $8, %xmm0, %eax
25-
; SSE41-NEXT: pextrb $4, %xmm0, %ecx
26-
; SSE41-NEXT: pinsrb $1, %ecx, %xmm0
27-
; SSE41-NEXT: pinsrb $2, %eax, %xmm0
24+
; SSE41-NEXT: pshufb {{.*#+}} xmm0 = xmm0[0,4,8,3,u,u,u,u,u,u,u,u,u,u,u,u]
2825
; SSE41-NEXT: movl $255, %eax
2926
; SSE41-NEXT: pinsrb $3, %eax, %xmm0
3027
; SSE41-NEXT: movd %xmm0, (%rdi)

llvm/test/CodeGen/X86/extract-concat.ll

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ define void @foo(<4 x float> %in, <4 x i8>* %out) {
88
; SSE42-LABEL: foo:
99
; SSE42: # %bb.0:
1010
; SSE42-NEXT: cvttps2dq %xmm0, %xmm0
11-
; SSE42-NEXT: pextrb $8, %xmm0, %eax
12-
; SSE42-NEXT: pextrb $4, %xmm0, %ecx
13-
; SSE42-NEXT: pinsrb $1, %ecx, %xmm0
14-
; SSE42-NEXT: pinsrb $2, %eax, %xmm0
11+
; SSE42-NEXT: pshufb {{.*#+}} xmm0 = xmm0[0,4,8,3,u,u,u,u,u,u,u,u,u,u,u,u]
1512
; SSE42-NEXT: movl $255, %eax
1613
; SSE42-NEXT: pinsrb $3, %eax, %xmm0
1714
; SSE42-NEXT: movd %xmm0, (%rdi)
@@ -20,10 +17,7 @@ define void @foo(<4 x float> %in, <4 x i8>* %out) {
2017
; AVX-LABEL: foo:
2118
; AVX: # %bb.0:
2219
; AVX-NEXT: vcvttps2dq %xmm0, %xmm0
23-
; AVX-NEXT: vpextrb $8, %xmm0, %eax
24-
; AVX-NEXT: vpextrb $4, %xmm0, %ecx
25-
; AVX-NEXT: vpinsrb $1, %ecx, %xmm0, %xmm0
26-
; AVX-NEXT: vpinsrb $2, %eax, %xmm0, %xmm0
20+
; AVX-NEXT: vpshufb {{.*#+}} xmm0 = xmm0[0,4,8,3,u,u,u,u,u,u,u,u,u,u,u,u]
2721
; AVX-NEXT: movl $255, %eax
2822
; AVX-NEXT: vpinsrb $3, %eax, %xmm0, %xmm0
2923
; AVX-NEXT: vmovd %xmm0, (%rdi)

0 commit comments

Comments
 (0)