diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 1e4b1c3693e..166b4844403 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -4793,7 +4793,24 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { if (isa(Op1)) // X icmp undef -> undef return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); - + + // (icmp cond (sub m A) 0) -> + // (icmp cond m A) + { + ConstantInt *C1, *C2; + Value *A; + // Check both arguments of the compare for a matching subtract. + if (match(Op0, m_ConstantInt(C1)) && C1->getValue() == 0 && + match(Op1, m_Sub(m_ConstantInt(C2), m_Value(A)))) { + // We managed to fold the add into the RHS of the select condition. + return new ICmpInst(I.getPredicate(), A, C2); + } else if (match(Op1, m_ConstantInt(C1)) && C1->getValue() == 0 && + match(Op0, m_Sub(m_ConstantInt(C2), m_Value(A)))) { + // We managed to fold the add into the LHS of the select condition. + return new ICmpInst(I.getPredicate(), C2, A); + } + } + // icmp , - Global/Stack value // addresses never equal each other! We already know that Op0 != Op1. if ((isa(Op0) || isa(Op0) ||