[llvm] [InstCombine] Fold comparisons of llvm.usub.sat result with its LHS (PR #214108)
Haram Jeong via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 7 17:55:49 PDT 2026
================
@@ -7861,6 +7861,20 @@ Instruction *InstCombinerImpl::foldICmpCommutative(CmpPredicate Pred,
}
}
+ {
+ // For a nonzero constant C:
+ // usub.sat(X, C) == X --> X == 0
+ // usub.sat(X, C) != X --> X != 0
+ // usub.sat(X, C) < X --> X != 0
+ if (match(Op0, m_Intrinsic<Intrinsic::usub_sat>(m_Specific(Op1),
----------------
haramj wrote:
> Would it make sense to move this into a helper like `foldICmpIntrinsicWithOperand()` for patterns of the form `icmp Pred intrinsic(X, ...), X`?
I checked the upstream code and don't think this refactoring is appropriate at this time:
- `foldICmpIntrinsicWithOperand()` does not exist anywhere in the codebase.
- The similar `abs(X) vs X` fold (lines 7823-7862 in the same `foldICmpCommutative` function) is also an `icmp Pred intrinsic(X, ...), X` pattern and is handled inline, not via a separate helper.
- All other intrinsic-vs-operand folds in `foldICmpCommutative` (udiv, sdiv, lshr, ashr, shl) are also inline.
- The only extractions in this function are for more complex patterns (GEP, select, minmax) that have broader applicability.
- Creating a helper for a single 12-line block with one call site would be premature abstraction inconsistent with the existing convention in this function.
https://github.com/llvm/llvm-project/pull/214108
More information about the llvm-commits
mailing list