Skip to content

Commit e9b88c7

Browse files
committed
[DAG] computeKnownBits - Move ISD::SRA handling into KnownBits::ashr
As discussed on D90527, we should be trying to move shift handling functionality into KnownBits to avoid code duplication in SelectionDAG/GlobalISel/ValueTracking.
1 parent 00eff96 commit e9b88c7

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

llvm/include/llvm/Support/KnownBits.h

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

281+
/// Compute known bits for ashr(LHS, RHS).
282+
/// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
283+
static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS);
284+
281285
/// Insert the bits from a smaller known bits starting at bitPosition.
282286
void insertBits(const KnownBits &SubBits, unsigned BitPosition) {
283287
Zero.insertBits(SubBits.Zero, BitPosition);

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2979,13 +2979,10 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
29792979
Known.Zero.setHighBits(ShMinAmt->getZExtValue());
29802980
break;
29812981
case ISD::SRA:
2982-
if (const APInt *ShAmt = getValidShiftAmountConstant(Op, DemandedElts)) {
2983-
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
2984-
unsigned Shift = ShAmt->getZExtValue();
2985-
// Sign extend known zero/one bit (else is unknown).
2986-
Known.Zero.ashrInPlace(Shift);
2987-
Known.One.ashrInPlace(Shift);
2988-
}
2982+
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
2983+
Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
2984+
Known = KnownBits::ashr(Known, Known2);
2985+
// TODO: Add minimum shift high known sign bits.
29892986
break;
29902987
case ISD::FSHL:
29912988
case ISD::FSHR:

llvm/lib/Support/KnownBits.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,23 @@ KnownBits KnownBits::lshr(const KnownBits &LHS, const KnownBits &RHS) {
192192
return Known;
193193
}
194194

195+
KnownBits KnownBits::ashr(const KnownBits &LHS, const KnownBits &RHS) {
196+
unsigned BitWidth = LHS.getBitWidth();
197+
KnownBits Known(BitWidth);
198+
199+
if (RHS.isConstant() && RHS.getConstant().ult(BitWidth)) {
200+
unsigned Shift = RHS.getConstant().getZExtValue();
201+
Known = LHS;
202+
Known.Zero.ashrInPlace(Shift);
203+
Known.One.ashrInPlace(Shift);
204+
return Known;
205+
}
206+
207+
// TODO: Minimum shift amount high bits are known sign bits.
208+
// TODO: No matter the shift amount, the leading sign bits will stay.
209+
return Known;
210+
}
211+
195212
KnownBits KnownBits::abs() const {
196213
// If the source's MSB is zero then we know the rest of the bits already.
197214
if (isNonNegative())

0 commit comments

Comments
 (0)