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 for udiv(LHS, RHS).
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS);
+ /// Compute known bits for urem(LHS, RHS).
+ static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS);
+
/// Compute known bits for umax(LHS, RHS).
static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS);
Known.makeNonNegative();
break;
- case Instruction::URem: {
- if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
- const APInt &RA = Rem->getValue();
- if (RA.isPowerOf2()) {
- APInt LowBits = (RA - 1);
- computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
- Known.Zero |= ~LowBits;
- Known.One &= LowBits;
- break;
- }
- }
-
- // Since the result is less than or equal to either operand, any leading
- // zero bits in either operand must also exist in the result.
+ case Instruction::URem:
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
-
- unsigned Leaders =
- std::max(Known.countMinLeadingZeros(), Known2.countMinLeadingZeros());
- Known.resetAll();
- Known.Zero.setHighBits(Leaders);
+ Known = KnownBits::urem(Known, Known2);
break;
- }
case Instruction::Alloca:
Known.Zero.setLowBits(Log2(cast<AllocaInst>(I)->getAlign()));
break;
}
break;
case ISD::UREM: {
- if (ConstantSDNode *Rem = isConstOrConstSplat(Op.getOperand(1))) {
- const APInt &RA = Rem->getAPIntValue();
- if (RA.isPowerOf2()) {
- APInt LowBits = (RA - 1);
- Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
-
- // The upper bits are all zero, the lower ones are unchanged.
- Known.Zero = Known2.Zero | ~LowBits;
- Known.One = Known2.One & LowBits;
- break;
- }
- }
-
- // Since the result is less than or equal to either operand, any leading
- // zero bits in either operand must also exist in the result.
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
-
- uint32_t Leaders =
- std::max(Known.countMinLeadingZeros(), Known2.countMinLeadingZeros());
- Known.resetAll();
- Known.Zero.setHighBits(Leaders);
+ Known = KnownBits::urem(Known, Known2);
break;
}
case ISD::EXTRACT_ELEMENT: {
return Known;
}
+KnownBits KnownBits::urem(const KnownBits &LHS, const KnownBits &RHS) {
+ unsigned BitWidth = LHS.getBitWidth();
+ assert(!LHS.hasConflict() && !RHS.hasConflict());
+ KnownBits Known(BitWidth);
+
+ if (RHS.isConstant() && RHS.getConstant().isPowerOf2()) {
+ // The upper bits are all zero, the lower ones are unchanged.
+ APInt LowBits = RHS.getConstant() - 1;
+ Known.Zero = LHS.Zero | ~LowBits;
+ Known.One = LHS.One & LowBits;
+ return Known;
+ }
+
+ // Since the result is less than or equal to either operand, any leading
+ // zero bits in either operand must also exist in the result.
+ uint32_t Leaders =
+ std::max(LHS.countMinLeadingZeros(), RHS.countMinLeadingZeros());
+ Known.Zero.setHighBits(Leaders);
+ return Known;
+}
+
KnownBits &KnownBits::operator&=(const KnownBits &RHS) {
// Result bit is 0 if either operand bit is 0.
Zero |= RHS.Zero;
KnownBits KnownSMin(KnownAnd);
KnownBits KnownMul(KnownAnd);
KnownBits KnownUDiv(KnownAnd);
+ KnownBits KnownURem(KnownAnd);
KnownBits KnownShl(KnownAnd);
KnownBits KnownLShr(KnownAnd);
KnownBits KnownAShr(KnownAnd);
Res = N1.udiv(N2);
KnownUDiv.One &= Res;
KnownUDiv.Zero &= ~Res;
+
+ Res = N1.urem(N2);
+ KnownURem.One &= Res;
+ KnownURem.Zero &= ~Res;
}
if (N2.ult(1ULL << N1.getBitWidth())) {
EXPECT_TRUE(ComputedUDiv.Zero.isSubsetOf(KnownUDiv.Zero));
EXPECT_TRUE(ComputedUDiv.One.isSubsetOf(KnownUDiv.One));
+ KnownBits ComputedURem = KnownBits::urem(Known1, Known2);
+ EXPECT_TRUE(ComputedURem.Zero.isSubsetOf(KnownURem.Zero));
+ EXPECT_TRUE(ComputedURem.One.isSubsetOf(KnownURem.One));
+
KnownBits ComputedShl = KnownBits::shl(Known1, Known2);
EXPECT_TRUE(ComputedShl.Zero.isSubsetOf(KnownShl.Zero));
EXPECT_TRUE(ComputedShl.One.isSubsetOf(KnownShl.One));