Skip to content

Commit 8222c1d

Browse files
committed
[SelectionDAG] Convert to or mask if all insertions are -1
We did this for 0 and and, but we can do this with or and -1.
1 parent d4706e1 commit 8222c1d

File tree

4 files changed

+75
-262
lines changed

4 files changed

+75
-262
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22966,18 +22966,33 @@ SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2296622966
return NewShuffle;
2296722967
}
2296822968

22969-
// If all insertions are zero value, try to convert to AND mask.
22970-
// TODO: Do this for -1 with OR mask?
22971-
if (!LegalOperations && llvm::isNullConstant(InVal) &&
22972-
all_of(Ops, [InVal](SDValue Op) { return !Op || Op == InVal; }) &&
22973-
count_if(Ops, [InVal](SDValue Op) { return Op == InVal; }) >= 2) {
22974-
SDValue Zero = DAG.getConstant(0, DL, MaxEltVT);
22975-
SDValue AllOnes = DAG.getAllOnesConstant(DL, MaxEltVT);
22976-
SmallVector<SDValue, 8> Mask(NumElts);
22977-
for (unsigned I = 0; I != NumElts; ++I)
22978-
Mask[I] = Ops[I] ? Zero : AllOnes;
22979-
return DAG.getNode(ISD::AND, DL, VT, CurVec,
22980-
DAG.getBuildVector(VT, DL, Mask));
22969+
if (!LegalOperations) {
22970+
bool isNull = llvm::isNullConstant(InVal);
22971+
bool isAllOnes = llvm::isAllOnesConstant(InVal);
22972+
// We can convert to AND/OR mask if all insertions are zero or -1
22973+
// respectively.
22974+
if ((isNull || isAllOnes) &&
22975+
all_of(Ops, [InVal](SDValue Op) { return !Op || Op == InVal; }) &&
22976+
count_if(Ops, [InVal](SDValue Op) { return Op == InVal; }) >= 2) {
22977+
SDValue Zero = DAG.getConstant(0, DL, MaxEltVT);
22978+
SDValue AllOnes = DAG.getAllOnesConstant(DL, MaxEltVT);
22979+
SmallVector<SDValue, 8> Mask(NumElts);
22980+
22981+
// Build the mask and return the corresponding DAG node.
22982+
auto buildMaskAndNode = [&](SDValue TrueVal, SDValue FalseVal,
22983+
unsigned MaskOpcode) {
22984+
for (unsigned I = 0; I < NumElts; ++I)
22985+
Mask[I] = Ops[I] ? TrueVal : FalseVal;
22986+
return DAG.getNode(MaskOpcode, DL, VT, CurVec,
22987+
DAG.getBuildVector(VT, DL, Mask));
22988+
};
22989+
22990+
// Use the pre-evaluated boolean value to decide the operation.
22991+
if (isAllOnes)
22992+
return buildMaskAndNode(AllOnes, Zero, ISD::OR);
22993+
22994+
return buildMaskAndNode(Zero, AllOnes, ISD::AND);
22995+
}
2298122996
}
2298222997

2298322998
// Failed to find a match in the chain - bail.

llvm/test/CodeGen/AArch64/vecreduce-and-legalization.ll

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,13 @@ define i8 @test_v3i8(<3 x i8> %a) nounwind {
101101
define i8 @test_v9i8(<9 x i8> %a) nounwind {
102102
; CHECK-LABEL: test_v9i8:
103103
; CHECK: // %bb.0:
104-
; CHECK-NEXT: mov v1.16b, v0.16b
105-
; CHECK-NEXT: mov w8, #-1 // =0xffffffff
106-
; CHECK-NEXT: mov v1.b[9], w8
107-
; CHECK-NEXT: mov v1.b[10], w8
108-
; CHECK-NEXT: mov v1.b[11], w8
109-
; CHECK-NEXT: mov v1.b[12], w8
110-
; CHECK-NEXT: mov v1.b[13], w8
111-
; CHECK-NEXT: mov v1.b[14], w8
112-
; CHECK-NEXT: mov v1.b[15], w8
104+
; CHECK-NEXT: movi v1.2d, #0xffffff00ffffff00
105+
; CHECK-NEXT: fmov x8, d0
106+
; CHECK-NEXT: orr v1.16b, v0.16b, v1.16b
113107
; CHECK-NEXT: ext v1.16b, v1.16b, v1.16b, #8
114108
; CHECK-NEXT: and v0.8b, v0.8b, v1.8b
115-
; CHECK-NEXT: fmov x8, d0
116-
; CHECK-NEXT: and x8, x8, x8, lsr #32
109+
; CHECK-NEXT: fmov x9, d0
110+
; CHECK-NEXT: and x8, x9, x8, lsr #32
117111
; CHECK-NEXT: and x8, x8, x8, lsr #16
118112
; CHECK-NEXT: lsr x9, x8, #8
119113
; CHECK-NEXT: and w0, w8, w9

llvm/test/CodeGen/X86/avx-cvt-3.ll

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,13 @@ define <8 x float> @sitofp_shuffle_zero_v8i32(<8 x i32> %a0) {
4848
define <8 x float> @sitofp_insert_allbits_v8i32(<8 x i32> %a0) {
4949
; X86-LABEL: sitofp_insert_allbits_v8i32:
5050
; X86: # %bb.0:
51-
; X86-NEXT: vxorps %xmm1, %xmm1, %xmm1
52-
; X86-NEXT: vcmptrueps %ymm1, %ymm1, %ymm1
53-
; X86-NEXT: vblendps {{.*#+}} ymm0 = ymm1[0],ymm0[1],ymm1[2],ymm0[3],ymm1[4,5],ymm0[6,7]
51+
; X86-NEXT: vorps {{\.?LCPI[0-9]+_[0-9]+}}, %ymm0, %ymm0
5452
; X86-NEXT: vcvtdq2ps %ymm0, %ymm0
5553
; X86-NEXT: retl
5654
;
5755
; X64-LABEL: sitofp_insert_allbits_v8i32:
5856
; X64: # %bb.0:
59-
; X64-NEXT: vxorps %xmm1, %xmm1, %xmm1
60-
; X64-NEXT: vcmptrueps %ymm1, %ymm1, %ymm1
61-
; X64-NEXT: vblendps {{.*#+}} ymm0 = ymm1[0],ymm0[1],ymm1[2],ymm0[3],ymm1[4,5],ymm0[6,7]
57+
; X64-NEXT: vorps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %ymm0, %ymm0
6258
; X64-NEXT: vcvtdq2ps %ymm0, %ymm0
6359
; X64-NEXT: retq
6460
%1 = insertelement <8 x i32> %a0, i32 -1, i32 0

0 commit comments

Comments
 (0)