Skip to content

Commit d172842

Browse files
committed
[DAG] SimplifyDemandedVectorElts - adjust demanded elements for selection mask for known zero results
If an element is known zero from both selections then it shouldn't matter what the selection mask element is.
1 parent 20d253e commit d172842

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11053,6 +11053,9 @@ SDValue DAGCombiner::visitVSELECT(SDNode *N) {
1105311053
if (SDValue V = foldVSelectToSignBitSplatMask(N, DAG))
1105411054
return V;
1105511055

11056+
if (SimplifyDemandedVectorElts(SDValue(N, 0)))
11057+
return SDValue(N, 0);
11058+
1105611059
return SDValue();
1105711060
}
1105811061

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3052,31 +3052,40 @@ bool TargetLowering::SimplifyDemandedVectorElts(
30523052
break;
30533053
}
30543054
case ISD::VSELECT: {
3055+
SDValue Sel = Op.getOperand(0);
3056+
SDValue LHS = Op.getOperand(1);
3057+
SDValue RHS = Op.getOperand(2);
3058+
30553059
// Try to transform the select condition based on the current demanded
30563060
// elements.
3057-
// TODO: If a condition element is undef, we can choose from one arm of the
3058-
// select (and if one arm is undef, then we can propagate that to the
3059-
// result).
3060-
// TODO - add support for constant vselect masks (see IR version of this).
3061-
APInt UnusedUndef, UnusedZero;
3062-
if (SimplifyDemandedVectorElts(Op.getOperand(0), DemandedElts, UnusedUndef,
3063-
UnusedZero, TLO, Depth + 1))
3061+
APInt UndefSel, UndefZero;
3062+
if (SimplifyDemandedVectorElts(Sel, DemandedElts, UndefSel, UndefZero, TLO,
3063+
Depth + 1))
30643064
return true;
30653065

30663066
// See if we can simplify either vselect operand.
30673067
APInt DemandedLHS(DemandedElts);
30683068
APInt DemandedRHS(DemandedElts);
30693069
APInt UndefLHS, ZeroLHS;
30703070
APInt UndefRHS, ZeroRHS;
3071-
if (SimplifyDemandedVectorElts(Op.getOperand(1), DemandedLHS, UndefLHS,
3072-
ZeroLHS, TLO, Depth + 1))
3071+
if (SimplifyDemandedVectorElts(LHS, DemandedLHS, UndefLHS, ZeroLHS, TLO,
3072+
Depth + 1))
30733073
return true;
3074-
if (SimplifyDemandedVectorElts(Op.getOperand(2), DemandedRHS, UndefRHS,
3075-
ZeroRHS, TLO, Depth + 1))
3074+
if (SimplifyDemandedVectorElts(RHS, DemandedRHS, UndefRHS, ZeroRHS, TLO,
3075+
Depth + 1))
30763076
return true;
30773077

30783078
KnownUndef = UndefLHS & UndefRHS;
30793079
KnownZero = ZeroLHS & ZeroRHS;
3080+
3081+
// If we know that the selected element is always zero, we don't need the
3082+
// select value element.
3083+
APInt DemandedSel = DemandedElts & ~KnownZero;
3084+
if (DemandedSel != DemandedElts)
3085+
if (SimplifyDemandedVectorElts(Sel, DemandedSel, UndefSel, UndefZero, TLO,
3086+
Depth + 1))
3087+
return true;
3088+
30803089
break;
30813090
}
30823091
case ISD::VECTOR_SHUFFLE: {

llvm/test/CodeGen/X86/vselect-constants.ll

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -282,24 +282,17 @@ define i32 @wrong_min_signbits(<2 x i16> %x) {
282282
; SSE-NEXT: pxor %xmm1, %xmm1
283283
; SSE-NEXT: pcmpeqw %xmm0, %xmm1
284284
; SSE-NEXT: movdqa {{.*#+}} xmm0 = [1,0,0,0]
285-
; SSE-NEXT: pandn %xmm0, %xmm1
286-
; SSE-NEXT: psllw $15, %xmm1
287-
; SSE-NEXT: psraw $15, %xmm1
288-
; SSE-NEXT: movdqa %xmm1, %xmm2
289-
; SSE-NEXT: pandn %xmm0, %xmm2
290-
; SSE-NEXT: pand {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm1
291-
; SSE-NEXT: por %xmm2, %xmm1
285+
; SSE-NEXT: pand %xmm1, %xmm0
286+
; SSE-NEXT: pandn {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm1
287+
; SSE-NEXT: por %xmm0, %xmm1
292288
; SSE-NEXT: movd %xmm1, %eax
293289
; SSE-NEXT: retq
294290
;
295291
; AVX-LABEL: wrong_min_signbits:
296292
; AVX: # %bb.0:
297293
; AVX-NEXT: vpxor %xmm1, %xmm1, %xmm1
298294
; AVX-NEXT: vpcmpeqw %xmm1, %xmm0, %xmm0
299-
; AVX-NEXT: vmovdqa {{.*#+}} xmm1 = [1,0,0,0]
300-
; AVX-NEXT: vpandn %xmm1, %xmm0, %xmm0
301-
; AVX-NEXT: vpsllw $15, %xmm0, %xmm0
302-
; AVX-NEXT: vpsraw $15, %xmm0, %xmm0
295+
; AVX-NEXT: vmovdqa {{.*#+}} xmm1 = [2,0,0,0]
303296
; AVX-NEXT: vpblendvb %xmm0, {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm1, %xmm0
304297
; AVX-NEXT: vmovd %xmm0, %eax
305298
; AVX-NEXT: retq

0 commit comments

Comments
 (0)