diff --git a/include/llvm/Support/KnownBits.h b/include/llvm/Support/KnownBits.h index 2e720bdde59..2937fced601 100644 --- a/include/llvm/Support/KnownBits.h +++ b/include/llvm/Support/KnownBits.h @@ -285,6 +285,11 @@ public: return KnownBits(LHS.Zero & RHS.Zero, LHS.One & RHS.One); } + /// Return true if LHS and RHS have no common bits set. + static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS) { + return (LHS.Zero | RHS.Zero).isAllOnesValue(); + } + /// Compute known bits resulting from adding LHS, RHS and a 1-bit Carry. static KnownBits computeForAddCarry( const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry); diff --git a/lib/Analysis/ValueTracking.cpp b/lib/Analysis/ValueTracking.cpp index cffbe927adb..0559c68a87c 100644 --- a/lib/Analysis/ValueTracking.cpp +++ b/lib/Analysis/ValueTracking.cpp @@ -301,7 +301,7 @@ bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS, KnownBits RHSKnown(IT->getBitWidth()); computeKnownBits(LHS, LHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo); computeKnownBits(RHS, RHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo); - return (LHSKnown.Zero | RHSKnown.Zero).isAllOnesValue(); + return KnownBits::haveNoCommonBitsSet(LHSKnown, RHSKnown); } bool llvm::isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI) { diff --git a/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/lib/CodeGen/SelectionDAG/SelectionDAG.cpp index 064f8cb8597..1e46a4066b6 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -4351,7 +4351,8 @@ bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const { bool SelectionDAG::haveNoCommonBitsSet(SDValue A, SDValue B) const { assert(A.getValueType() == B.getValueType() && "Values must have the same type"); - return (computeKnownBits(A).Zero | computeKnownBits(B).Zero).isAllOnesValue(); + return KnownBits::haveNoCommonBitsSet(computeKnownBits(A), + computeKnownBits(B)); } static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step, diff --git a/unittests/Support/KnownBitsTest.cpp b/unittests/Support/KnownBitsTest.cpp index 5f2133165d2..784bc6db70c 100644 --- a/unittests/Support/KnownBitsTest.cpp +++ b/unittests/Support/KnownBitsTest.cpp @@ -463,4 +463,20 @@ TEST(KnownBitsTest, SExtInReg) { } } +TEST(KnownBitsTest, CommonBitsSet) { + unsigned Bits = 4; + ForeachKnownBits(Bits, [&](const KnownBits &Known1) { + ForeachKnownBits(Bits, [&](const KnownBits &Known2) { + bool HasCommonBitsSet = false; + ForeachNumInKnownBits(Known1, [&](const APInt &N1) { + ForeachNumInKnownBits(Known2, [&](const APInt &N2) { + HasCommonBitsSet |= N1.intersects(N2); + }); + }); + EXPECT_EQ(!HasCommonBitsSet, + KnownBits::haveNoCommonBitsSet(Known1, Known2)); + }); + }); +} + } // end anonymous namespace