mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-27 22:56:05 +00: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:
parent
b38d897e80
commit
2ed9c4c70b
@ -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;
|
||||
|
||||
|
@ -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.
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user