Skip to content

Commit

Permalink
[KnownBits] Move ValueTracking/SelectionDAG UDIV KnownBits handling t…
Browse files Browse the repository at this point in the history
…o KnownBits::udiv. NFCI.

Both these have the same implementation - so move them to a single KnownBits copy.

GlobalISel will be able to use this as well with minimal effort.
  • Loading branch information
RKSimon committed Nov 5, 2020
1 parent 2f84b59 commit 32bee18
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
3 changes: 3 additions & 0 deletions llvm/include/llvm/Support/KnownBits.h
Expand Up @@ -258,6 +258,9 @@ struct KnownBits {
/// Compute known bits resulting from multiplying LHS and RHS.
static KnownBits computeForMul(const KnownBits &LHS, const KnownBits &RHS);

/// Compute known bits for udiv(LHS, RHS).
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS);

/// Compute known bits for umax(LHS, RHS).
static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS);

Expand Down
14 changes: 2 additions & 12 deletions llvm/lib/Analysis/ValueTracking.cpp
Expand Up @@ -1127,19 +1127,9 @@ static void computeKnownBitsFromOperator(const Operator *I,
break;
}
case Instruction::UDiv: {
// For the purposes of computing leading zeros we can conservatively
// treat a udiv as a logical right shift by the power of 2 known to
// be less than the denominator.
computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
unsigned LeadZ = Known2.countMinLeadingZeros();

Known2.resetAll();
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
unsigned RHSMaxLeadingZeros = Known2.countMaxLeadingZeros();
if (RHSMaxLeadingZeros != BitWidth)
LeadZ = std::min(BitWidth, LeadZ + BitWidth - RHSMaxLeadingZeros - 1);

Known.Zero.setHighBits(LeadZ);
Known = KnownBits::udiv(Known, Known2);
break;
}
case Instruction::Select: {
Expand Down
13 changes: 2 additions & 11 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Expand Up @@ -2884,18 +2884,9 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
break;
}
case ISD::UDIV: {
// For the purposes of computing leading zeros we can conservatively
// treat a udiv as a logical right shift by the power of 2 known to
// be less than the denominator.
Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
unsigned LeadZ = Known2.countMinLeadingZeros();

Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
unsigned RHSMaxLeadingZeros = Known2.countMaxLeadingZeros();
if (RHSMaxLeadingZeros != BitWidth)
LeadZ = std::min(BitWidth, LeadZ + BitWidth - RHSMaxLeadingZeros - 1);

Known.Zero.setHighBits(LeadZ);
Known = KnownBits::udiv(Known, Known2);
break;
}
case ISD::SELECT:
Expand Down
18 changes: 18 additions & 0 deletions llvm/lib/Support/KnownBits.cpp
Expand Up @@ -306,6 +306,24 @@ KnownBits KnownBits::computeForMul(const KnownBits &LHS, const KnownBits &RHS) {
return Res;
}

KnownBits KnownBits::udiv(const KnownBits &LHS, const KnownBits &RHS) {
unsigned BitWidth = LHS.getBitWidth();
assert(!LHS.hasConflict() && !RHS.hasConflict());
KnownBits Known(BitWidth);

// For the purposes of computing leading zeros we can conservatively
// treat a udiv as a logical right shift by the power of 2 known to
// be less than the denominator.
unsigned LeadZ = LHS.countMinLeadingZeros();
unsigned RHSMaxLeadingZeros = RHS.countMaxLeadingZeros();

if (RHSMaxLeadingZeros != BitWidth)
LeadZ = std::min(BitWidth, LeadZ + BitWidth - RHSMaxLeadingZeros - 1);

Known.Zero.setHighBits(LeadZ);
return Known;
}

KnownBits &KnownBits::operator&=(const KnownBits &RHS) {
// Result bit is 0 if either operand bit is 0.
Zero |= RHS.Zero;
Expand Down
11 changes: 11 additions & 0 deletions llvm/unittests/Support/KnownBitsTest.cpp
Expand Up @@ -113,6 +113,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
KnownBits KnownSMax(KnownAnd);
KnownBits KnownSMin(KnownAnd);
KnownBits KnownMul(KnownAnd);
KnownBits KnownUDiv(KnownAnd);
KnownBits KnownShl(KnownAnd);
KnownBits KnownLShr(KnownAnd);
KnownBits KnownAShr(KnownAnd);
Expand Down Expand Up @@ -153,6 +154,12 @@ TEST(KnownBitsTest, BinaryExhaustive) {
KnownMul.One &= Res;
KnownMul.Zero &= ~Res;

if (!N2.isNullValue()) {
Res = N1.udiv(N2);
KnownUDiv.One &= Res;
KnownUDiv.Zero &= ~Res;
}

if (N2.ult(1ULL << N1.getBitWidth())) {
Res = N1.shl(N2);
KnownShl.One &= Res;
Expand Down Expand Up @@ -207,6 +214,10 @@ TEST(KnownBitsTest, BinaryExhaustive) {
EXPECT_TRUE(ComputedMul.Zero.isSubsetOf(KnownMul.Zero));
EXPECT_TRUE(ComputedMul.One.isSubsetOf(KnownMul.One));

KnownBits ComputedUDiv = KnownBits::udiv(Known1, Known2);
EXPECT_TRUE(ComputedUDiv.Zero.isSubsetOf(KnownUDiv.Zero));
EXPECT_TRUE(ComputedUDiv.One.isSubsetOf(KnownUDiv.One));

KnownBits ComputedShl = KnownBits::shl(Known1, Known2);
EXPECT_TRUE(ComputedShl.Zero.isSubsetOf(KnownShl.Zero));
EXPECT_TRUE(ComputedShl.One.isSubsetOf(KnownShl.One));
Expand Down

0 comments on commit 32bee18

Please sign in to comment.