Skip to content

Commit 3d453f4

Browse files
RKSimonmemfrob
authored andcommitted
[KnownBits] Add KnownBits::haveNoCommonBitsSet helper. NFCI.
Include exhaustive test coverage.
1 parent 82eb168 commit 3d453f4

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

llvm/include/llvm/Support/KnownBits.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@ struct KnownBits {
285285
return KnownBits(LHS.Zero & RHS.Zero, LHS.One & RHS.One);
286286
}
287287

288+
/// Return true if LHS and RHS have no common bits set.
289+
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS) {
290+
return (LHS.Zero | RHS.Zero).isAllOnesValue();
291+
}
292+
288293
/// Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
289294
static KnownBits computeForAddCarry(
290295
const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry);

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS,
301301
KnownBits RHSKnown(IT->getBitWidth());
302302
computeKnownBits(LHS, LHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo);
303303
computeKnownBits(RHS, RHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo);
304-
return (LHSKnown.Zero | RHSKnown.Zero).isAllOnesValue();
304+
return KnownBits::haveNoCommonBitsSet(LHSKnown, RHSKnown);
305305
}
306306

307307
bool llvm::isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI) {

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4351,7 +4351,8 @@ bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
43514351
bool SelectionDAG::haveNoCommonBitsSet(SDValue A, SDValue B) const {
43524352
assert(A.getValueType() == B.getValueType() &&
43534353
"Values must have the same type");
4354-
return (computeKnownBits(A).Zero | computeKnownBits(B).Zero).isAllOnesValue();
4354+
return KnownBits::haveNoCommonBitsSet(computeKnownBits(A),
4355+
computeKnownBits(B));
43554356
}
43564357

43574358
static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,

llvm/unittests/Support/KnownBitsTest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,4 +463,20 @@ TEST(KnownBitsTest, SExtInReg) {
463463
}
464464
}
465465

466+
TEST(KnownBitsTest, CommonBitsSet) {
467+
unsigned Bits = 4;
468+
ForeachKnownBits(Bits, [&](const KnownBits &Known1) {
469+
ForeachKnownBits(Bits, [&](const KnownBits &Known2) {
470+
bool HasCommonBitsSet = false;
471+
ForeachNumInKnownBits(Known1, [&](const APInt &N1) {
472+
ForeachNumInKnownBits(Known2, [&](const APInt &N2) {
473+
HasCommonBitsSet |= N1.intersects(N2);
474+
});
475+
});
476+
EXPECT_EQ(!HasCommonBitsSet,
477+
KnownBits::haveNoCommonBitsSet(Known1, Known2));
478+
});
479+
});
480+
}
481+
466482
} // end anonymous namespace

0 commit comments

Comments
 (0)