Skip to content

Commit 6174da2

Browse files
committed
[InstCombine] reduce code duplication in foldICmpMulConstant(); NFC
1 parent 67e2298 commit 6174da2

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,80 +2053,78 @@ Instruction *InstCombinerImpl::foldICmpOrConstant(ICmpInst &Cmp,
20532053
Instruction *InstCombinerImpl::foldICmpMulConstant(ICmpInst &Cmp,
20542054
BinaryOperator *Mul,
20552055
const APInt &C) {
2056+
ICmpInst::Predicate Pred = Cmp.getPredicate();
2057+
Type *MulTy = Mul->getType();
2058+
Value *X = Mul->getOperand(0);
2059+
20562060
// If there's no overflow:
20572061
// X * X == 0 --> X == 0
20582062
// X * X != 0 --> X != 0
2059-
Type *MulTy = Mul->getType();
2060-
if (Cmp.isEquality() && C.isZero() &&
2061-
Mul->getOperand(0) == Mul->getOperand(1) &&
2063+
if (Cmp.isEquality() && C.isZero() && X == Mul->getOperand(1) &&
20622064
(Mul->hasNoUnsignedWrap() || Mul->hasNoSignedWrap()))
2063-
return new ICmpInst(Cmp.getPredicate(), Mul->getOperand(0),
2064-
ConstantInt::getNullValue(MulTy));
2065+
return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));
20652066

20662067
const APInt *MulC;
20672068
if (!match(Mul->getOperand(1), m_APInt(MulC)))
20682069
return nullptr;
20692070

20702071
// If this is a test of the sign bit and the multiply is sign-preserving with
2071-
// a constant operand, use the multiply LHS operand instead.
2072-
ICmpInst::Predicate Pred = Cmp.getPredicate();
2072+
// a constant operand, use the multiply LHS operand instead:
2073+
// (X * +MulC) < 0 --> X < 0
2074+
// (X * -MulC) < 0 --> X > 0
20732075
if (isSignTest(Pred, C) && Mul->hasNoSignedWrap()) {
20742076
if (MulC->isNegative())
20752077
Pred = ICmpInst::getSwappedPredicate(Pred);
2076-
return new ICmpInst(Pred, Mul->getOperand(0),
2077-
Constant::getNullValue(Mul->getType()));
2078+
return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));
20782079
}
20792080

2080-
if (MulC->isZero() || !(Mul->hasNoSignedWrap() || Mul->hasNoUnsignedWrap()))
2081+
if (MulC->isZero() || (!Mul->hasNoSignedWrap() && !Mul->hasNoUnsignedWrap()))
20812082
return nullptr;
20822083

20832084
// If the multiply does not wrap, try to divide the compare constant by the
20842085
// multiplication factor.
20852086
if (Cmp.isEquality()) {
20862087
// (mul nsw X, MulC) == C --> X == C /s MulC
20872088
if (Mul->hasNoSignedWrap() && C.srem(*MulC).isZero()) {
2088-
Constant *NewC = ConstantInt::get(Mul->getType(), C.sdiv(*MulC));
2089-
return new ICmpInst(Pred, Mul->getOperand(0), NewC);
2089+
Constant *NewC = ConstantInt::get(MulTy, C.sdiv(*MulC));
2090+
return new ICmpInst(Pred, X, NewC);
20902091
}
20912092
// (mul nuw X, MulC) == C --> X == C /u MulC
20922093
if (Mul->hasNoUnsignedWrap() && C.urem(*MulC).isZero()) {
2093-
Constant *NewC = ConstantInt::get(Mul->getType(), C.udiv(*MulC));
2094-
return new ICmpInst(Pred, Mul->getOperand(0), NewC);
2094+
Constant *NewC = ConstantInt::get(MulTy, C.udiv(*MulC));
2095+
return new ICmpInst(Pred, X, NewC);
20952096
}
20962097
}
20972098

2099+
// With a matching no-overflow guarantee, fold the constants:
2100+
// (X * MulC) < C --> X < (C / MulC)
2101+
// (X * MulC) > C --> X > (C / MulC)
2102+
// TODO: Assert that Pred is not equal to SGE, SLE, UGE, ULE?
20982103
Constant *NewC = nullptr;
2099-
2100-
// FIXME: Add assert that Pred is not equal to ICMP_SGE, ICMP_SLE,
2101-
// ICMP_UGE, ICMP_ULE.
2102-
21032104
if (Mul->hasNoSignedWrap()) {
2104-
if (MulC->isNegative()) {
2105-
// MININT / -1 --> overflow.
2106-
if (C.isMinSignedValue() && MulC->isAllOnes())
2107-
return nullptr;
2105+
// MININT / -1 --> overflow.
2106+
if (C.isMinSignedValue() && MulC->isAllOnes())
2107+
return nullptr;
2108+
if (MulC->isNegative())
21082109
Pred = ICmpInst::getSwappedPredicate(Pred);
2109-
}
2110+
21102111
if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE)
21112112
NewC = ConstantInt::get(
2112-
Mul->getType(),
2113-
APIntOps::RoundingSDiv(C, *MulC, APInt::Rounding::UP));
2113+
MulTy, APIntOps::RoundingSDiv(C, *MulC, APInt::Rounding::UP));
21142114
if (Pred == ICmpInst::ICMP_SLE || Pred == ICmpInst::ICMP_SGT)
21152115
NewC = ConstantInt::get(
2116-
Mul->getType(),
2117-
APIntOps::RoundingSDiv(C, *MulC, APInt::Rounding::DOWN));
2118-
} else if (Mul->hasNoUnsignedWrap()) {
2116+
MulTy, APIntOps::RoundingSDiv(C, *MulC, APInt::Rounding::DOWN));
2117+
} else {
2118+
assert(Mul->hasNoUnsignedWrap() && "Expected mul nuw");
21192119
if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)
21202120
NewC = ConstantInt::get(
2121-
Mul->getType(),
2122-
APIntOps::RoundingUDiv(C, *MulC, APInt::Rounding::UP));
2121+
MulTy, APIntOps::RoundingUDiv(C, *MulC, APInt::Rounding::UP));
21232122
if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT)
21242123
NewC = ConstantInt::get(
2125-
Mul->getType(),
2126-
APIntOps::RoundingUDiv(C, *MulC, APInt::Rounding::DOWN));
2124+
MulTy, APIntOps::RoundingUDiv(C, *MulC, APInt::Rounding::DOWN));
21272125
}
21282126

2129-
return NewC ? new ICmpInst(Pred, Mul->getOperand(0), NewC) : nullptr;
2127+
return NewC ? new ICmpInst(Pred, X, NewC) : nullptr;
21302128
}
21312129

21322130
/// Fold icmp (shl 1, Y), C.

0 commit comments

Comments
 (0)