Skip to content

Commit 9082c13

Browse files
committed
[Support] Add KnownBits::concat method
Add a method for the various cases where we need to concatenate 2 KnownBits together (BUILD_PAIR and SHIFT_PARTS in particular) - uses the existing APInt::concat 'HiBits.concat(LoBits)' convention Differential Revision: https://reviews.llvm.org/D130557
1 parent 039fb3e commit 9082c13

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

llvm/include/llvm/Support/KnownBits.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ struct KnownBits {
218218
One.extractBits(NumBits, BitPosition));
219219
}
220220

221+
/// Concatenate the bits from \p Lo onto the bottom of *this. This is
222+
/// equivalent to:
223+
/// (this->zext(NewWidth) << Lo.getBitWidth()) | Lo.zext(NewWidth)
224+
KnownBits concat(const KnownBits &Lo) const {
225+
return KnownBits(Zero.concat(Lo.Zero), One.concat(Lo.One));
226+
}
227+
221228
/// Return KnownBits based on this, but updated given that the underlying
222229
/// value is known to be greater than or equal to Val.
223230
KnownBits makeGE(const APInt &Val) const;

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3293,13 +3293,11 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
32933293
assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
32943294

32953295
// Collect lo/hi source values and concatenate.
3296-
// TODO: Would a KnownBits::concatBits helper be useful?
32973296
unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
32983297
unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
32993298
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
33003299
Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3301-
Known = Known.anyext(LoBits + HiBits);
3302-
Known.insertBits(Known2, LoBits);
3300+
Known = Known2.concat(Known);
33033301

33043302
// Collect shift amount.
33053303
Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,11 +2235,7 @@ bool TargetLowering::SimplifyDemandedBits(
22352235
if (SimplifyDemandedBits(Op.getOperand(1), MaskHi, KnownHi, TLO, Depth + 1))
22362236
return true;
22372237

2238-
Known.Zero = KnownLo.Zero.zext(BitWidth) |
2239-
KnownHi.Zero.zext(BitWidth).shl(HalfBitWidth);
2240-
2241-
Known.One = KnownLo.One.zext(BitWidth) |
2242-
KnownHi.One.zext(BitWidth).shl(HalfBitWidth);
2238+
Known = KnownHi.concat(KnownLo);
22432239
break;
22442240
}
22452241
case ISD::ZERO_EXTEND:

llvm/unittests/Support/KnownBitsTest.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,4 +508,29 @@ TEST(KnownBitsTest, CommonBitsSet) {
508508
});
509509
}
510510

511+
TEST(KnownBitsTest, ConcatBits) {
512+
unsigned Bits = 4;
513+
for (unsigned LoBits = 1; LoBits < Bits; ++LoBits) {
514+
unsigned HiBits = Bits - LoBits;
515+
ForeachKnownBits(LoBits, [&](const KnownBits &KnownLo) {
516+
ForeachKnownBits(HiBits, [&](const KnownBits &KnownHi) {
517+
KnownBits KnownAll = KnownHi.concat(KnownLo);
518+
519+
EXPECT_EQ(KnownLo.countMinPopulation() + KnownHi.countMinPopulation(),
520+
KnownAll.countMinPopulation());
521+
EXPECT_EQ(KnownLo.countMaxPopulation() + KnownHi.countMaxPopulation(),
522+
KnownAll.countMaxPopulation());
523+
524+
KnownBits ExtractLo = KnownAll.extractBits(LoBits, 0);
525+
KnownBits ExtractHi = KnownAll.extractBits(HiBits, LoBits);
526+
527+
EXPECT_EQ(KnownLo.One.getZExtValue(), ExtractLo.One.getZExtValue());
528+
EXPECT_EQ(KnownHi.One.getZExtValue(), ExtractHi.One.getZExtValue());
529+
EXPECT_EQ(KnownLo.Zero.getZExtValue(), ExtractLo.Zero.getZExtValue());
530+
EXPECT_EQ(KnownHi.Zero.getZExtValue(), ExtractHi.Zero.getZExtValue());
531+
});
532+
});
533+
}
534+
}
535+
511536
} // end anonymous namespace

0 commit comments

Comments
 (0)