diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -1338,11 +1338,39 @@ if (Instruction *Res = processUGT_ADDCST_ADD(Cmp, A, B, CI2, CI, *this)) return Res; - // icmp(phi(C1, C2, ...), C) -> phi(icmp(C1, C), icmp(C2, C), ...). Constant *C = dyn_cast(Op1); if (!C) return nullptr; + // Fold icmp (Positive1 * a * b + Positive2) --> icmp (a * b) + if (match(Op0, + m_c_Or(m_c_Mul(m_c_Mul(m_Value(A), m_ConstantInt(CI)), m_Value(B)), + m_ConstantInt(CI2)))) { + if (CI->getValue().isStrictlyPositive() && + CI2->getValue().isStrictlyPositive()) { + auto Relax = [&](CmpInst::Predicate Pred, Value *A, Value *B) { + Value *Mul = Builder.CreateMul(A, B, "mul"); + Value *NewCmp = Builder.CreateICmp( + Pred, Mul, ConstantInt::getNullValue(A->getType())); + return replaceInstUsesWith(Cmp, NewCmp); + }; + CmpInst::Predicate NewPred; + switch (Pred) { + case ICmpInst::ICMP_SGE: + case ICmpInst::ICMP_SGT: + NewPred = ICmpInst::ICMP_SGE; + return Relax(NewPred, A, B); + case ICmpInst::ICMP_SLT: + case ICmpInst::ICMP_SLE: + NewPred = ICmpInst::ICMP_SLT; + return Relax(NewPred, A, B); + default: + break; + } + } + } + + // icmp(phi(C1, C2, ...), C) -> phi(icmp(C1, C), icmp(C2, C), ...). if (auto *Phi = dyn_cast(Op0)) if (all_of(Phi->operands(), [](Value *V) { return isa(V); })) { Type *Ty = Cmp.getType(); diff --git a/llvm/test/Transforms/InstCombine/icmp.ll b/llvm/test/Transforms/InstCombine/icmp.ll --- a/llvm/test/Transforms/InstCombine/icmp.ll +++ b/llvm/test/Transforms/InstCombine/icmp.ll @@ -4630,9 +4630,8 @@ define i1 @mul_constant_add_constant_sge(i8 %a, i8 %b) { ; CHECK-LABEL: @mul_constant_add_constant_sge( -; CHECK-NEXT: [[MUL:%.*]] = mul nsw i8 [[A:%.*]], 42 -; CHECK-NEXT: [[MUL1:%.*]] = mul nsw i8 [[MUL]], [[B:%.*]] -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[MUL1]], -1 +; CHECK-NEXT: [[MUL2:%.*]] = mul i8 [[A:%.*]], [[B:%.*]] +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[MUL2]], -1 ; CHECK-NEXT: ret i1 [[CMP]] ; %mul = mul nsw i8 %a, 42 @@ -4644,10 +4643,8 @@ define i1 @mul_constant_add_constant_sgt(i8 %a, i8 %b) { ; CHECK-LABEL: @mul_constant_add_constant_sgt( -; CHECK-NEXT: [[MUL:%.*]] = mul nsw i8 [[A:%.*]], 42 -; CHECK-NEXT: [[MUL1:%.*]] = mul nsw i8 [[MUL]], [[B:%.*]] -; CHECK-NEXT: [[ADD:%.*]] = or i8 [[MUL1]], 24 -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[ADD]], 0 +; CHECK-NEXT: [[MUL2:%.*]] = mul i8 [[A:%.*]], [[B:%.*]] +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[MUL2]], -1 ; CHECK-NEXT: ret i1 [[CMP]] ; %mul = mul nsw i8 %a, 42 @@ -4659,10 +4656,8 @@ define i1 @mul_constant_add_constant_sle(i8 %a, i8 %b) { ; CHECK-LABEL: @mul_constant_add_constant_sle( -; CHECK-NEXT: [[MUL:%.*]] = mul nsw i8 [[A:%.*]], 42 -; CHECK-NEXT: [[MUL1:%.*]] = mul nsw i8 [[MUL]], [[B:%.*]] -; CHECK-NEXT: [[ADD:%.*]] = or i8 [[MUL1]], 24 -; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[ADD]], 1 +; CHECK-NEXT: [[MUL2:%.*]] = mul i8 [[A:%.*]], [[B:%.*]] +; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[MUL2]], 0 ; CHECK-NEXT: ret i1 [[CMP]] ; %mul = mul nsw i8 %a, 42 @@ -4674,9 +4669,8 @@ define i1 @mul_constant_add_constant_slt(i8 %a, i8 %b) { ; CHECK-LABEL: @mul_constant_add_constant_slt( -; CHECK-NEXT: [[MUL:%.*]] = mul nsw i8 [[A:%.*]], 42 -; CHECK-NEXT: [[MUL1:%.*]] = mul nsw i8 [[MUL]], [[B:%.*]] -; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[MUL1]], 0 +; CHECK-NEXT: [[MUL2:%.*]] = mul i8 [[A:%.*]], [[B:%.*]] +; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[MUL2]], 0 ; CHECK-NEXT: ret i1 [[CMP]] ; %mul = mul nsw i8 %a, 42