From a694546f7cdf6fbcacddf229eeff5316e4bd0eae Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Mon, 16 May 2022 17:20:19 +0200 Subject: [PATCH] [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). Differential Revision: https://reviews.llvm.org/D125692 --- llvm/include/llvm/Support/KnownBits.h | 6 ++++ llvm/lib/Support/KnownBits.cpp | 5 ++- .../Transforms/InstCombine/InstCombineCasts.cpp | 2 +- llvm/unittests/IR/ConstantRangeTest.cpp | 3 +- llvm/unittests/Support/KnownBitsTest.cpp | 38 ++++++++-------------- 5 files changed, 24 insertions(+), 30 deletions(-) diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h index 1af7130..84e095e 100644 --- a/llvm/include/llvm/Support/KnownBits.h +++ b/llvm/include/llvm/Support/KnownBits.h @@ -415,6 +415,12 @@ public: 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 5ec8506..9f200c2 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 dc3f32d..2418ee2 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 9aee84e..b230a21a 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 c8d27b9..9ef62bb 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); } } } -- 2.7.4