[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 14:05:31 PST 2022


craig.topper created this revision.
craig.topper added reviewers: RKSimon, spatel, lebedev.ri.
Herald added subscribers: foad, dexonsmith.
craig.topper requested review of this revision.
Herald added a project: LLVM.

Even if we don't have any known bits, we can assume that there is
1 at least 1 sign bit. This is consistent with ComputeNumSignBits
which always returns at least.

Split from D116469 <https://reviews.llvm.org/D116469>.


Repository:
  rG LLVM Github Monorepo

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, CountMinSignedBits) {
+  const unsigned Bits = 4;
+  ForeachKnownBits(Bits, [&](const KnownBits &Known) {
+    unsigned Expected = Bits;
+    ForeachNumInKnownBits(Known, [&](const APInt &N) {
+      Expected = std::min(Expected, N.getNumSignBits());
+    });
+    EXPECT_EQ(Expected, Known.countMinSignBits());
+  });
+}
+
 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,8 @@
       return countMinLeadingZeros();
     if (isNegative())
       return countMinLeadingOnes();
-    return 0;
+    // Every value has at least 1 sign bit.
+    return 1;
   }
 
   /// Returns the maximum number of trailing zero bits possible.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116500.396946.patch
Type: text/x-patch
Size: 1151 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220102/03b7b439/attachment.bin>


More information about the llvm-commits mailing list