Skip to content

Commit 48d4ba8

Browse files
committed
[SelectionDAG] Compute Known + Sign Bits - merge INSERT_VECTOR_ELT known/unknown index paths
Match the approach in SimplifyDemandedBits where we calculate the demanded elts and then have a common path for the ComputeKnownBits/ComputeNumSignBits call.
1 parent 2f6987b commit 48d4ba8

File tree

1 file changed

+37
-51
lines changed

1 file changed

+37
-51
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3261,38 +3261,31 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
32613261
break;
32623262
}
32633263
case ISD::INSERT_VECTOR_ELT: {
3264+
// If we know the element index, split the demand between the
3265+
// source vector and the inserted element, otherwise assume we need
3266+
// the original demanded vector elements and the value.
32643267
SDValue InVec = Op.getOperand(0);
32653268
SDValue InVal = Op.getOperand(1);
32663269
SDValue EltNo = Op.getOperand(2);
3267-
3268-
ConstantSDNode *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
3270+
bool DemandedVal = true;
3271+
APInt DemandedVecElts = DemandedElts;
3272+
auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
32693273
if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
3270-
// If we know the element index, split the demand between the
3271-
// source vector and the inserted element.
3272-
Known.Zero = Known.One = APInt::getAllOnesValue(BitWidth);
32733274
unsigned EltIdx = CEltNo->getZExtValue();
3274-
3275-
// If we demand the inserted element then add its common known bits.
3276-
if (DemandedElts[EltIdx]) {
3277-
Known2 = computeKnownBits(InVal, Depth + 1);
3278-
Known.One &= Known2.One.zextOrTrunc(Known.One.getBitWidth());
3279-
Known.Zero &= Known2.Zero.zextOrTrunc(Known.Zero.getBitWidth());
3280-
}
3281-
3282-
// If we demand the source vector then add its common known bits, ensuring
3283-
// that we don't demand the inserted element.
3284-
APInt VectorElts = DemandedElts & ~(APInt::getOneBitSet(NumElts, EltIdx));
3285-
if (!!VectorElts) {
3286-
Known2 = computeKnownBits(InVec, VectorElts, Depth + 1);
3287-
Known.One &= Known2.One;
3288-
Known.Zero &= Known2.Zero;
3289-
}
3290-
} else {
3291-
// Unknown element index, so ignore DemandedElts and demand them all.
3292-
Known = computeKnownBits(InVec, Depth + 1);
3275+
DemandedVal = !!DemandedElts[EltIdx];
3276+
DemandedVecElts.clearBit(EltIdx);
3277+
}
3278+
Known.One.setAllBits();
3279+
Known.Zero.setAllBits();
3280+
if (DemandedVal) {
32933281
Known2 = computeKnownBits(InVal, Depth + 1);
3294-
Known.One &= Known2.One.zextOrTrunc(Known.One.getBitWidth());
3295-
Known.Zero &= Known2.Zero.zextOrTrunc(Known.Zero.getBitWidth());
3282+
Known.One &= Known2.One.zextOrTrunc(BitWidth);
3283+
Known.Zero &= Known2.Zero.zextOrTrunc(BitWidth);
3284+
}
3285+
if (!!DemandedVecElts) {
3286+
Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
3287+
Known.One &= Known2.One;
3288+
Known.Zero &= Known2.Zero;
32963289
}
32973290
break;
32983291
}
@@ -3850,39 +3843,32 @@ unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
38503843
return std::max(std::min(KnownSign - rIndex * BitWidth, BitWidth), 0);
38513844
}
38523845
case ISD::INSERT_VECTOR_ELT: {
3846+
// If we know the element index, split the demand between the
3847+
// source vector and the inserted element, otherwise assume we need
3848+
// the original demanded vector elements and the value.
38533849
SDValue InVec = Op.getOperand(0);
38543850
SDValue InVal = Op.getOperand(1);
38553851
SDValue EltNo = Op.getOperand(2);
3856-
3857-
ConstantSDNode *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
3852+
bool DemandedVal = true;
3853+
APInt DemandedVecElts = DemandedElts;
3854+
auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
38583855
if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
3859-
// If we know the element index, split the demand between the
3860-
// source vector and the inserted element.
38613856
unsigned EltIdx = CEltNo->getZExtValue();
3862-
3863-
// If we demand the inserted element then get its sign bits.
3864-
Tmp = std::numeric_limits<unsigned>::max();
3865-
if (DemandedElts[EltIdx]) {
3866-
// TODO - handle implicit truncation of inserted elements.
3867-
if (InVal.getScalarValueSizeInBits() != VTBits)
3868-
break;
3869-
Tmp = ComputeNumSignBits(InVal, Depth + 1);
3870-
}
3871-
3872-
// If we demand the source vector then get its sign bits, and determine
3873-
// the minimum.
3874-
APInt VectorElts = DemandedElts;
3875-
VectorElts.clearBit(EltIdx);
3876-
if (!!VectorElts) {
3877-
Tmp2 = ComputeNumSignBits(InVec, VectorElts, Depth + 1);
3878-
Tmp = std::min(Tmp, Tmp2);
3879-
}
3880-
} else {
3881-
// Unknown element index, so ignore DemandedElts and demand them all.
3882-
Tmp = ComputeNumSignBits(InVec, Depth + 1);
3857+
DemandedVal = !!DemandedElts[EltIdx];
3858+
DemandedVecElts.clearBit(EltIdx);
3859+
}
3860+
Tmp = std::numeric_limits<unsigned>::max();
3861+
if (DemandedVal) {
3862+
// TODO - handle implicit truncation of inserted elements.
3863+
if (InVal.getScalarValueSizeInBits() != VTBits)
3864+
break;
38833865
Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
38843866
Tmp = std::min(Tmp, Tmp2);
38853867
}
3868+
if (!!DemandedVecElts) {
3869+
Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
3870+
Tmp = std::min(Tmp, Tmp2);
3871+
}
38863872
assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
38873873
return Tmp;
38883874
}

0 commit comments

Comments
 (0)