[llvm] [InstCombine] Fold comparisons of llvm.usub.sat result with its LHS (PR #214108)

Andreas Jonson via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 5 13:00:01 PDT 2026


================
@@ -3953,6 +3953,36 @@ Instruction *InstCombinerImpl::foldICmpEqIntrinsicWithConstant(
 }
 
 /// Fold an icmp with LLVM intrinsics
+static Instruction *
+foldICmpUSubSatWithLHS(ICmpInst &Cmp) {
+  // For C != 0:
+  // usub.sat(X, C) == X  -->  X == 0
+  // usub.sat(X, C) <  X  -->  X != 0
+  ICmpInst::Predicate Pred = Cmp.getPredicate();
+  if (Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_ULT)
+    return nullptr;
+
+  Value *Op0 = Cmp.getOperand(0), *Op1 = Cmp.getOperand(1);
+  const auto *II = dyn_cast<IntrinsicInst>(Op0);
+  Value *X = Op1;
+  if (!II && Pred == ICmpInst::ICMP_EQ) {
+    II = dyn_cast<IntrinsicInst>(Op1);
+    X = Op0;
+  }
+
+  if (!II || II->getIntrinsicID() != Intrinsic::usub_sat ||
+      II->getArgOperand(0) != X)
+    return nullptr;
+
+  const APInt *C;
+  if (!match(II->getArgOperand(1), m_APInt(C)) || C->isZero())
----------------
andjo403 wrote:

same as `!match(II->getArgOperand(1), m_NonZeroInt())`
but also possible to make more general with call to isKnownNonZero function.

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


More information about the llvm-commits mailing list