Skip to content

Commit cb798f0

Browse files
committed
[DAG] computeKnownBits - Move (most) ISD::SRL handling into KnownBits::lshr
As discussed on D90527, we should be be trying to move shift handling functionality into KnownBits to avoid code duplication in SelectionDAG/GlobalISel/ValueTracking. The refactor to use the KnownBits fixed/min/max constant helpers allows us to hit a couple of cases that we were missing before. We still need the getValidMinimumShiftAmountConstant case as KnownBits doesn't handle per-element vector cases.
1 parent c06c02b commit cb798f0

File tree

5 files changed

+793
-842
lines changed

5 files changed

+793
-842
lines changed

llvm/include/llvm/Support/KnownBits.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ struct KnownBits {
274274
/// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
275275
static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS);
276276

277+
/// Compute known bits for lshr(LHS, RHS).
278+
/// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
279+
static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS);
280+
277281
/// Insert the bits from a smaller known bits starting at bitPosition.
278282
void insertBits(const KnownBits &SubBits, unsigned BitPosition) {
279283
Zero.insertBits(SubBits.Zero, BitPosition);

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2970,19 +2970,8 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
29702970
break;
29712971
case ISD::SRL:
29722972
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
2973-
2974-
if (const APInt *ShAmt = getValidShiftAmountConstant(Op, DemandedElts)) {
2975-
unsigned Shift = ShAmt->getZExtValue();
2976-
Known.Zero.lshrInPlace(Shift);
2977-
Known.One.lshrInPlace(Shift);
2978-
// High bits are known zero.
2979-
Known.Zero.setHighBits(Shift);
2980-
break;
2981-
}
2982-
2983-
// No matter the shift amount, the leading zeros will stay zero.
2984-
Known.Zero = APInt::getHighBitsSet(BitWidth, Known.countMinLeadingZeros());
2985-
Known.One.clearAllBits();
2973+
Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
2974+
Known = KnownBits::lshr(Known, Known2);
29862975

29872976
// Minimum shift high bits are known zero.
29882977
if (const APInt *ShMinAmt =

llvm/lib/Support/KnownBits.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,29 @@ KnownBits KnownBits::shl(const KnownBits &LHS, const KnownBits &RHS) {
169169
return Known;
170170
}
171171

172+
KnownBits KnownBits::lshr(const KnownBits &LHS, const KnownBits &RHS) {
173+
unsigned BitWidth = LHS.getBitWidth();
174+
KnownBits Known(BitWidth);
175+
176+
if (RHS.isConstant() && RHS.getConstant().ult(BitWidth)) {
177+
unsigned Shift = RHS.getConstant().getZExtValue();
178+
Known = LHS;
179+
Known.Zero.lshrInPlace(Shift);
180+
Known.One.lshrInPlace(Shift);
181+
// High bits are known zero.
182+
Known.Zero.setHighBits(Shift);
183+
return Known;
184+
}
185+
186+
// Minimum shift amount high bits are known zero.
187+
if (RHS.getMinValue().ult(BitWidth))
188+
Known.Zero.setHighBits(RHS.getMinValue().getZExtValue());
189+
190+
// No matter the shift amount, the leading zeros will stay zero.
191+
Known.Zero.setHighBits(LHS.countMinLeadingZeros());
192+
return Known;
193+
}
194+
172195
KnownBits KnownBits::abs() const {
173196
// If the source's MSB is zero then we know the rest of the bits already.
174197
if (isNonNegative())

0 commit comments

Comments
 (0)