[llvm] 2ed9c4c - [ConstantRange] Introduce getActiveBits() method

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 22 11:37:59 PDT 2020


Author: Roman Lebedev
Date: 2020-09-22T21:37:29+03:00
New Revision: 2ed9c4c70bbb36fa12d48a73abc2d89c0af80060

URL: https://github.com/llvm/llvm-project/commit/2ed9c4c70bbb36fa12d48a73abc2d89c0af80060
DIFF: https://github.com/llvm/llvm-project/commit/2ed9c4c70bbb36fa12d48a73abc2d89c0af80060.diff

LOG: [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.

Added: 
    

Modified: 
    llvm/include/llvm/IR/ConstantRange.h
    llvm/lib/IR/ConstantRange.cpp
    llvm/unittests/IR/ConstantRangeTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/IR/ConstantRange.h b/llvm/include/llvm/IR/ConstantRange.h
index 494a14a10cdb..1b7c1b92a573 100644
--- a/llvm/include/llvm/IR/ConstantRange.h
+++ b/llvm/include/llvm/IR/ConstantRange.h
@@ -261,6 +261,10 @@ class LLVM_NODISCARD ConstantRange {
     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;
 

diff  --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp
index 7b8dd66b993c..d2132583e8f7 100644
--- a/llvm/lib/IR/ConstantRange.cpp
+++ b/llvm/lib/IR/ConstantRange.cpp
@@ -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.

diff  --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp
index 474a94af3932..6962d349f5bc 100644
--- a/llvm/unittests/IR/ConstantRangeTest.cpp
+++ b/llvm/unittests/IR/ConstantRangeTest.cpp
@@ -641,6 +641,33 @@ TEST_F(ConstantRangeTest, SetDifference) {
   EXPECT_EQ(E.
diff erence(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);


        


More information about the llvm-commits mailing list