diff options
author | Roman Lebedev <lebedev.ri@gmail.com> | 2020-09-22 15:17:24 +0300 |
---|---|---|
committer | Roman Lebedev <lebedev.ri@gmail.com> | 2020-09-22 21:37:29 +0300 |
commit | 2ed9c4c70bbb36fa12d48a73abc2d89c0af80060 (patch) | |
tree | 393aedd22222ea26e47866d085c0d9cfa38c6615 /llvm/unittests/IR/ConstantRangeTest.cpp | |
parent | b38d897e802664034c7e6e4654328256ed370a61 (diff) | |
download | llvm-2ed9c4c70bbb36fa12d48a73abc2d89c0af80060.zip llvm-2ed9c4c70bbb36fa12d48a73abc2d89c0af80060.tar.gz llvm-2ed9c4c70bbb36fa12d48a73abc2d89c0af80060.tar.bz2 |
[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.
Diffstat (limited to 'llvm/unittests/IR/ConstantRangeTest.cpp')
-rw-r--r-- | llvm/unittests/IR/ConstantRangeTest.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp index 474a94a..6962d34 100644 --- a/llvm/unittests/IR/ConstantRangeTest.cpp +++ b/llvm/unittests/IR/ConstantRangeTest.cpp @@ -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); |