[llvm] [ValueTracking] Fold max/min when incrementing/decrementing by 1 (PR #142466)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 3 09:33:59 PDT 2025
================
@@ -564,6 +564,43 @@ Instruction *InstCombinerImpl::foldSelectIntoOp(SelectInst &SI, Value *TrueVal,
return nullptr;
}
+/// Try to fold a select to a min/max intrinsic. Many cases are already handled
+/// by matchDecomposedSelectPattern but here we handle the cases where more
+/// exensive modification of the IR is required.
+static Value *foldSelectICmpMinMax(const ICmpInst *Cmp, Value *TVal,
+ Value *FVal,
+ InstCombiner::BuilderTy &Builder) {
+ const Value *CmpLHS = Cmp->getOperand(0);
+ const Value *CmpRHS = Cmp->getOperand(1);
+ const ICmpInst::Predicate Pred = Cmp->getPredicate();
+
+ // (X > Y) ? X : (Y - 1) ==> MIN(X, Y - 1)
+ // (X < Y) ? X : (Y + 1) ==> MAX(X, Y + 1)
+ // This transformation is valid when overflow corresponding to the sign of
+ // the comparison is poison and we must drop the non-matching overflow flag.
+ // Note: that the UMIN case is not possible as we canonicalize to addition.
+ if (CmpLHS == TVal) {
----------------
dtcxzyw wrote:
Missing fold for commuted pattern: https://alive2.llvm.org/ce/z/bKhA-j
https://github.com/llvm/llvm-project/pull/142466
More information about the llvm-commits
mailing list