[llvm] [InstCombine] Fold `min(X+1, Y) - min(X, Y) --> zext X < Y` (PR #157782)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 10 07:03:34 PDT 2025


================
@@ -2719,6 +2719,24 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
     return BinaryOperator::CreateSub(X, Not);
   }
 
+  // min(X+1, Y) - min(X, Y) --> zext X < Y
+  // Replacing a sub and at least one min with an icmp
+  // and a zext is a potential improvement.
+  if (match(Op0, m_c_SMin(m_c_NSWAdd(m_Value(X), m_One()), m_Value(Y))) &&
+      match(Op1, m_c_SMin(m_Value(X), m_Value(Y))) &&
----------------
dtcxzyw wrote:

```suggestion
      match(Op1, m_c_SMin(m_Specific(X), m_Specific(Y))) &&
```
`m_Value` will overwrite `X` assigned by the last call to `m_Value(X)`.
Please also add a test with mismatched operands.


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


More information about the llvm-commits mailing list