[llvm] [InstCombine] Fold max/min when incrementing/decrementing by 1 (PR #142466)
Alex MacLean via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 5 09:54:59 PDT 2025
================
@@ -565,6 +565,59 @@ 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 SimplifyQuery &SQ) {
+ const Value *CmpLHS = Cmp->getOperand(0);
+ const Value *CmpRHS = Cmp->getOperand(1);
+ 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.
+ if (CmpRHS == TVal) {
+ std::swap(CmpLHS, CmpRHS);
+ Pred = CmpInst::getSwappedPredicate(Pred);
+ }
+
+ // TODO: consider handeling 'or disjoint' as well, though these would need to
----------------
AlexMaclean wrote:
Fixed
https://github.com/llvm/llvm-project/pull/142466
More information about the llvm-commits
mailing list