[llvm] [InstCombine] Fold unsigned icmp of nuw adds with constants (PR #226685)

Karl-Johan Karlsson via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 10 03:15:58 PDT 2026


================
@@ -5561,12 +5561,25 @@ Instruction *InstCombinerImpl::foldICmpBinOp(ICmpInst &I,
   // if C2 has greater magnitude than C1:
   //  icmp (A + C1), (C + C2) -> icmp A, (C + C3)
   //  s.t. C3 = C2 - C1
+  //
+  // For unsigned predicates, C1 and C2 are compared as unsigned values.
   if (A && C && NoOp0WrapProblem && NoOp1WrapProblem &&
-      (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned()) {
+      (BO0->hasOneUse() || BO1->hasOneUse())) {
     const APInt *AP1, *AP2;
     // TODO: Support non-uniform vectors.
     // TODO: Allow poison passthrough if B or D's element is poison.
-    if (match(B, m_APIntAllowPoison(AP1)) &&
+    if (I.isUnsigned() && match(B, m_APIntAllowPoison(AP1)) &&
+        match(D, m_APIntAllowPoison(AP2))) {
+      // Both adds are nuw here. Subtracting the smaller constant from both
----------------
karka228 wrote:

The comment state that "Both adds are nuw here", but can we add an assert that verify this?


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


More information about the llvm-commits mailing list