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

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 9 23:15:05 PDT 2026


================
@@ -7741,6 +7740,24 @@ Instruction *InstCombinerImpl::foldICmpCommutative(CmpPredicate Pred,
   }
 
   const SimplifyQuery Q = SQ.getWithInstruction(&CxtI);
+
+  {
+    Value *Y;
+    // For Y != 0:
+    // usub.sat(X, Y) == X  --> X == 0
+    // usub.sat(X, Y) != X  --> X != 0
+    // usub.sat(X, Y) <  X  --> X != 0
+    if (match(Op0, m_Intrinsic<Intrinsic::usub_sat>(m_Specific(Op1),
+                                                    m_Value(Y))) &&
----------------
dtcxzyw wrote:

```suggestion
                                                    m_NonZeroInt())) &&
```
I checked all folded cases in https://github.com/dtcxzyw/llvm-opt-benchmark-nightly/pull/854/files. It is not worth handling variable cases.


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


More information about the llvm-commits mailing list