1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-26 04:32:44 +01:00

[ConstantRange] Introduce getMinSignedBits() method

Similar to the ConstantRange::getActiveBits(), and to similarly-named
methods in APInt, returns the bitwidth needed to represent
the given signed constant range
This commit is contained in:
Roman Lebedev 2020-09-22 15:51:25 +03:00
parent 8b5ad85b2f
commit 7f7780d776
3 changed files with 38 additions and 0 deletions

View File

@ -265,6 +265,10 @@ public:
/// in this range.
unsigned getActiveBits() const;
/// Compute the maximal number of bits needed to represent every value
/// in this signed range.
unsigned getMinSignedBits() const;
/// Subtract the specified constant from the endpoints of this constant range.
ConstantRange subtract(const APInt &CI) const;

View File

@ -420,6 +420,14 @@ unsigned ConstantRange::getActiveBits() const {
return getUnsignedMax().getActiveBits();
}
unsigned ConstantRange::getMinSignedBits() const {
if (isEmptySet())
return 0;
return std::max(getSignedMin().getMinSignedBits(),
getSignedMax().getMinSignedBits());
}
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

@ -668,6 +668,32 @@ TEST_F(ConstantRangeTest, losslessUnsignedTruncationZeroext) {
});
}
TEST_F(ConstantRangeTest, getMinSignedBits) {
unsigned Bits = 4;
EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) {
unsigned Exact = 0;
ForeachNumInConstantRange(CR, [&](const APInt &N) {
Exact = std::max(Exact, N.getMinSignedBits());
});
unsigned ResultCR = CR.getMinSignedBits();
EXPECT_EQ(Exact, ResultCR);
});
}
TEST_F(ConstantRangeTest, losslessSignedTruncationSignext) {
unsigned Bits = 4;
EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) {
unsigned MinBitWidth = CR.getMinSignedBits();
if (MinBitWidth == 0) {
EXPECT_TRUE(CR.isEmptySet());
return;
}
if (MinBitWidth == Bits)
return;
EXPECT_EQ(CR, CR.truncate(MinBitWidth).signExtend(Bits));
});
}
TEST_F(ConstantRangeTest, SubtractAPInt) {
EXPECT_EQ(Full.subtract(APInt(16, 4)), Full);
EXPECT_EQ(Empty.subtract(APInt(16, 4)), Empty);