1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 11:02:59 +02:00

[ConstantRange] Introduce getActiveBits() method

Much like APInt::getActiveBits(), computes how many bits are needed
to be able to represent every value in this constant range,
treating the values as unsigned.
This commit is contained in:
Roman Lebedev 2020-09-22 15:17:24 +03:00
parent 753cbd3c43
commit ebabbd05e9
3 changed files with 38 additions and 0 deletions

View File

@ -261,6 +261,10 @@ public:
return !operator==(CR);
}
/// Compute the maximal number of active bits needed to represent every value
/// in this range.
unsigned getActiveBits() const;
/// Subtract the specified constant from the endpoints of this constant range.
ConstantRange subtract(const APInt &CI) const;

View File

@ -413,6 +413,13 @@ bool ConstantRange::contains(const ConstantRange &Other) const {
return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
}
unsigned ConstantRange::getActiveBits() const {
if (isEmptySet())
return 0;
return getUnsignedMax().getActiveBits();
}
ConstantRange ConstantRange::subtract(const APInt &Val) const {
assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
// If the set is empty or full, don't modify the endpoints.

View File

@ -641,6 +641,33 @@ TEST_F(ConstantRangeTest, SetDifference) {
EXPECT_EQ(E.difference(A), F);
}
TEST_F(ConstantRangeTest, getActiveBits) {
unsigned Bits = 4;
EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) {
unsigned Exact = 0;
ForeachNumInConstantRange(CR, [&](const APInt &N) {
Exact = std::max(Exact, N.getActiveBits());
});
unsigned ResultCR = CR.getActiveBits();
EXPECT_EQ(Exact, ResultCR);
});
}
TEST_F(ConstantRangeTest, losslessUnsignedTruncationZeroext) {
unsigned Bits = 4;
EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) {
unsigned MinBitWidth = CR.getActiveBits();
if (MinBitWidth == 0) {
EXPECT_TRUE(CR.isEmptySet() || (CR.isSingleElement() &&
CR.getSingleElement()->isNullValue()));
return;
}
if (MinBitWidth == Bits)
return;
EXPECT_EQ(CR, CR.truncate(MinBitWidth).zeroExtend(Bits));
});
}
TEST_F(ConstantRangeTest, SubtractAPInt) {
EXPECT_EQ(Full.subtract(APInt(16, 4)), Full);
EXPECT_EQ(Empty.subtract(APInt(16, 4)), Empty);