[PATCH] D116500: [Support] Make KnownBits::countMinSignBits() always return at least 1.

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 2 21:06:02 PST 2022


craig.topper updated this revision to Diff 396988.
craig.topper added a comment.

Add KnownBits::countMinSignBits()


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116500/new/

https://reviews.llvm.org/D116500

Files:
  llvm/include/llvm/Support/KnownBits.h
  llvm/unittests/Support/KnownBitsTest.cpp


Index: llvm/unittests/Support/KnownBitsTest.cpp
===================================================================
--- llvm/unittests/Support/KnownBitsTest.cpp
+++ llvm/unittests/Support/KnownBitsTest.cpp
@@ -442,6 +442,17 @@
   });
 }
 
+TEST(KnownBitsTest, CountMaxSignedBits) {
+  unsigned Bits = 4;
+  ForeachKnownBits(Bits, [&](const KnownBits &Known) {
+    unsigned Expected = 0;
+    ForeachNumInKnownBits(Known, [&](const APInt &N) {
+      Expected = std::max(Expected, N.getMinSignedBits());
+    });
+    EXPECT_EQ(Expected, Known.countMaxSignedBits());
+  });
+}
+
 TEST(KnownBitsTest, SExtOrTrunc) {
   const unsigned NarrowerSize = 4;
   const unsigned BaseSize = 6;
Index: llvm/include/llvm/Support/KnownBits.h
===================================================================
--- llvm/include/llvm/Support/KnownBits.h
+++ llvm/include/llvm/Support/KnownBits.h
@@ -249,7 +249,14 @@
       return countMinLeadingZeros();
     if (isNegative())
       return countMinLeadingOnes();
-    return 0;
+    // Every value has at least 1 sign bit.
+    return 1;
+  }
+
+  /// Returns the maximum number of bits needed to represent all possible
+  /// signed values with these known bits.
+  unsigned countMaxSignedBits() const {
+    return getBitWidth() - countMinSignBits() + 1;
   }
 
   /// Returns the maximum number of trailing zero bits possible.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116500.396988.patch
Type: text/x-patch
Size: 1366 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220103/a2bbeea4/attachment.bin>


More information about the llvm-commits mailing list