[llvm] [InstCombine] Fold `icmp samesign u{gt/lt} (X +nsw C2), C` -> `icmp s{gt/lt} X, (C - C2)` (PR #169960)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 6 09:15:21 PST 2025


================
@@ -3167,20 +3167,21 @@ Instruction *InstCombinerImpl::foldICmpAddConstant(ICmpInst &Cmp,
 
   // If the add does not wrap, we can always adjust the compare by subtracting
   // the constants. Equality comparisons are handled elsewhere. SGE/SLE/UGE/ULE
-  // are canonicalized to SGT/SLT/UGT/ULT.
-  if ((Add->hasNoSignedWrap() &&
-       (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT)) ||
-      (Add->hasNoUnsignedWrap() &&
-       (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT))) {
+  // has been canonicalized to SGT/SLT/UGT/ULT.
+  CmpInst::Predicate ChosenPred = Pred.getPreferredSignedPredicate();
+  if ((Add->hasNoSignedWrap() && (ChosenPred == ICmpInst::ICMP_SGT ||
+                                  ChosenPred == ICmpInst::ICMP_SLT)) ||
+      (Add->hasNoUnsignedWrap() && (ChosenPred == ICmpInst::ICMP_UGT ||
+                                    ChosenPred == ICmpInst::ICMP_ULT))) {
     bool Overflow;
-    APInt NewC =
-        Cmp.isSigned() ? C.ssub_ov(*C2, Overflow) : C.usub_ov(*C2, Overflow);
+    APInt NewC = ICmpInst::isSigned(ChosenPred) ? C.ssub_ov(*C2, Overflow)
+                                                : C.usub_ov(*C2, Overflow);
     // If there is overflow, the result must be true or false.
     // TODO: Can we assert there is no overflow because InstSimplify always
     // handles those cases?
----------------
nikic wrote:

I'm fine with dropping this comment. I don't think there is value in making this an assert, only additional risk...

https://github.com/llvm/llvm-project/pull/169960


More information about the llvm-commits mailing list