@@ -3132,7 +3132,7 @@ Instruction *InstCombinerImpl::foldICmpAddConstant(ICmpInst &Cmp,
31323132
31333133 Value *Op0, *Op1;
31343134 Instruction *Ext0, *Ext1;
3135- const CmpInst::Predicate Pred = Cmp.getPredicate ( );
3135+ const CmpPredicate Pred ( Cmp.getCmpPredicate () );
31363136 if (match (Add,
31373137 m_Add (m_CombineAnd (m_Instruction (Ext0), m_ZExtOrSExt (m_Value (Op0))),
31383138 m_CombineAnd (m_Instruction (Ext1),
@@ -3167,20 +3167,21 @@ Instruction *InstCombinerImpl::foldICmpAddConstant(ICmpInst &Cmp,
31673167
31683168 // If the add does not wrap, we can always adjust the compare by subtracting
31693169 // the constants. Equality comparisons are handled elsewhere. SGE/SLE/UGE/ULE
3170- // are canonicalized to SGT/SLT/UGT/ULT.
3171- if ((Add->hasNoSignedWrap () &&
3172- (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT)) ||
3173- (Add->hasNoUnsignedWrap () &&
3174- (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT))) {
3170+ // has been canonicalized to SGT/SLT/UGT/ULT.
3171+ CmpInst::Predicate ChosenPred = Pred.getPreferredSignedPredicate ();
3172+ if ((Add->hasNoSignedWrap () && (ChosenPred == ICmpInst::ICMP_SGT ||
3173+ ChosenPred == ICmpInst::ICMP_SLT)) ||
3174+ (Add->hasNoUnsignedWrap () && (ChosenPred == ICmpInst::ICMP_UGT ||
3175+ ChosenPred == ICmpInst::ICMP_ULT))) {
31753176 bool Overflow;
3176- APInt NewC =
3177- Cmp. isSigned () ? C. ssub_ov (*C2, Overflow) : C.usub_ov (*C2, Overflow);
3177+ APInt NewC = ICmpInst::isSigned (ChosenPred) ? C. ssub_ov (*C2, Overflow)
3178+ : C.usub_ov (*C2, Overflow);
31783179 // If there is overflow, the result must be true or false.
31793180 // TODO: Can we assert there is no overflow because InstSimplify always
31803181 // handles those cases?
31813182 if (!Overflow)
31823183 // icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2)
3183- return new ICmpInst (Pred , X, ConstantInt::get (Ty, NewC));
3184+ return new ICmpInst (ChosenPred , X, ConstantInt::get (Ty, NewC));
31843185 }
31853186
31863187 if (ICmpInst::isUnsigned (Pred) && Add->hasNoSignedWrap () &&
0 commit comments