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.
/// 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);
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: {
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:
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;
KnownBits KnownSMax(KnownAnd);
KnownBits KnownSMin(KnownAnd);
KnownBits KnownMul(KnownAnd);
+ KnownBits KnownUDiv(KnownAnd);
KnownBits KnownShl(KnownAnd);
KnownBits KnownLShr(KnownAnd);
KnownBits KnownAShr(KnownAnd);
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;
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));