[llvm] a694546 - [KnownBits] Add operator==

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue May 17 00:39:04 PDT 2022


Author: Nikita Popov
Date: 2022-05-17T09:38:13+02:00
New Revision: a694546f7cdf6fbcacddf229eeff5316e4bd0eae

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

LOG: [KnownBits] Add operator==

Checking whether two KnownBits are the same is somewhat common,
mainly in test code.

I don't think there is a lot of room for confusion with "determine
what the KnownBits for an icmp eq would be", as that has a
different result type (this is what the eq() method implements,
which returns Optional<bool>).

Differential Revision: https://reviews.llvm.org/D125692

Added: 
    

Modified: 
    llvm/include/llvm/Support/KnownBits.h
    llvm/lib/Support/KnownBits.cpp
    llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
    llvm/unittests/IR/ConstantRangeTest.cpp
    llvm/unittests/Support/KnownBitsTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index 1af7130afd9af..84e095e2bbabe 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -415,6 +415,12 @@ struct KnownBits {
     return KnownBits(Zero.reverseBits(), One.reverseBits());
   }
 
+  bool operator==(const KnownBits &Other) const {
+    return Zero == Other.Zero && One == Other.One;
+  }
+
+  bool operator!=(const KnownBits &Other) const { return !(*this == Other); }
+
   void print(raw_ostream &OS) const;
   void dump() const;
 };

diff  --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 5ec85061e8501..9f200c2e8e465 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -417,9 +417,8 @@ KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,
   unsigned BitWidth = LHS.getBitWidth();
   assert(BitWidth == RHS.getBitWidth() && !LHS.hasConflict() &&
          !RHS.hasConflict() && "Operand mismatch");
-  assert(
-      (!NoUndefSelfMultiply || (LHS.One == RHS.One && LHS.Zero == RHS.Zero)) &&
-      "Self multiplication knownbits mismatch");
+  assert((!NoUndefSelfMultiply || LHS == RHS) &&
+         "Self multiplication knownbits mismatch");
 
   // Compute the high known-0 bits by multiplying the unsigned max of each side.
   // Conservatively, M active bits * N active bits results in M + N bits in the

diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index dc3f32dc90cf0..2418ee29f2ee0 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -1077,7 +1077,7 @@ Instruction *InstCombinerImpl::transformZExtICmp(ICmpInst *Cmp, ZExtInst &Zext)
       KnownBits KnownLHS = computeKnownBits(LHS, 0, &Zext);
       KnownBits KnownRHS = computeKnownBits(RHS, 0, &Zext);
 
-      if (KnownLHS.Zero == KnownRHS.Zero && KnownLHS.One == KnownRHS.One) {
+      if (KnownLHS == KnownRHS) {
         APInt KnownBits = KnownLHS.Zero | KnownLHS.One;
         APInt UnknownBit = ~KnownBits;
         if (UnknownBit.countPopulation() == 1) {

diff  --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp
index 9aee84eb1d064..b230a21aecd98 100644
--- a/llvm/unittests/IR/ConstantRangeTest.cpp
+++ b/llvm/unittests/IR/ConstantRangeTest.cpp
@@ -2379,8 +2379,7 @@ TEST_F(ConstantRangeTest, ToKnownBits) {
     });
     // For an empty CR any result would be legal.
     if (!CR.isEmptySet()) {
-      EXPECT_EQ(ExpectedKnown.One, Known.One);
-      EXPECT_EQ(ExpectedKnown.Zero, Known.Zero);
+      EXPECT_EQ(ExpectedKnown, Known);
     }
   });
 }

diff  --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp
index c8d27b9a24600..9ef62bb90fcc4 100644
--- a/llvm/unittests/Support/KnownBitsTest.cpp
+++ b/llvm/unittests/Support/KnownBitsTest.cpp
@@ -41,10 +41,9 @@ TEST(KnownBitsTest, AddCarryExhaustive) {
           });
         });
 
-        KnownBits KnownComputed = KnownBits::computeForAddCarry(
-            Known1, Known2, KnownCarry);
-        EXPECT_EQ(Known.Zero, KnownComputed.Zero);
-        EXPECT_EQ(Known.One, KnownComputed.One);
+        KnownBits KnownComputed =
+            KnownBits::computeForAddCarry(Known1, Known2, KnownCarry);
+        EXPECT_EQ(Known, KnownComputed);
       });
     });
   });
@@ -79,10 +78,9 @@ static void TestAddSubExhaustive(bool IsAdd) {
         });
       });
 
-      KnownBits KnownComputed = KnownBits::computeForAddSub(
-          IsAdd, /*NSW*/false, Known1, Known2);
-      EXPECT_EQ(Known.Zero, KnownComputed.Zero);
-      EXPECT_EQ(Known.One, KnownComputed.One);
+      KnownBits KnownComputed =
+          KnownBits::computeForAddSub(IsAdd, /*NSW*/ false, Known1, Known2);
+      EXPECT_EQ(Known, KnownComputed);
 
       // The NSW calculation is not precise, only check that it's
       // conservatively correct.
@@ -201,32 +199,25 @@ TEST(KnownBitsTest, BinaryExhaustive) {
       });
 
       KnownBits ComputedAnd = Known1 & Known2;
-      EXPECT_EQ(KnownAnd.Zero, ComputedAnd.Zero);
-      EXPECT_EQ(KnownAnd.One, ComputedAnd.One);
+      EXPECT_EQ(KnownAnd, ComputedAnd);
 
       KnownBits ComputedOr = Known1 | Known2;
-      EXPECT_EQ(KnownOr.Zero, ComputedOr.Zero);
-      EXPECT_EQ(KnownOr.One, ComputedOr.One);
+      EXPECT_EQ(KnownOr, ComputedOr);
 
       KnownBits ComputedXor = Known1 ^ Known2;
-      EXPECT_EQ(KnownXor.Zero, ComputedXor.Zero);
-      EXPECT_EQ(KnownXor.One, ComputedXor.One);
+      EXPECT_EQ(KnownXor, ComputedXor);
 
       KnownBits ComputedUMax = KnownBits::umax(Known1, Known2);
-      EXPECT_EQ(KnownUMax.Zero, ComputedUMax.Zero);
-      EXPECT_EQ(KnownUMax.One, ComputedUMax.One);
+      EXPECT_EQ(KnownUMax, ComputedUMax);
 
       KnownBits ComputedUMin = KnownBits::umin(Known1, Known2);
-      EXPECT_EQ(KnownUMin.Zero, ComputedUMin.Zero);
-      EXPECT_EQ(KnownUMin.One, ComputedUMin.One);
+      EXPECT_EQ(KnownUMin, ComputedUMin);
 
       KnownBits ComputedSMax = KnownBits::smax(Known1, Known2);
-      EXPECT_EQ(KnownSMax.Zero, ComputedSMax.Zero);
-      EXPECT_EQ(KnownSMax.One, ComputedSMax.One);
+      EXPECT_EQ(KnownSMax, ComputedSMax);
 
       KnownBits ComputedSMin = KnownBits::smin(Known1, Known2);
-      EXPECT_EQ(KnownSMin.Zero, ComputedSMin.Zero);
-      EXPECT_EQ(KnownSMin.One, ComputedSMin.One);
+      EXPECT_EQ(KnownSMin, ComputedSMin);
 
       // The following are conservatively correct, but not guaranteed to be
       // precise.
@@ -476,8 +467,7 @@ TEST(KnownBitsTest, SExtOrTrunc) {
       KnownBits Baseline;
       InitKnownBits(Baseline, Input.sextOrTrunc(Size));
       Test = Test.sextOrTrunc(Size);
-      EXPECT_EQ(Test.One, Baseline.One);
-      EXPECT_EQ(Test.Zero, Baseline.Zero);
+      EXPECT_EQ(Test, Baseline);
     }
   }
 }


        


More information about the llvm-commits mailing list