[llvm] [ValueTracking] Fold max/min when incrementing/decrementing by 1 (PR #142466)
Alex MacLean via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 3 09:16:14 PDT 2025
================
@@ -8388,6 +8388,31 @@ static SelectPatternResult matchMinMax(CmpInst::Predicate Pred,
}
}
+ // (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 == TrueVal) {
+ if (Pred == CmpInst::ICMP_SGT &&
+ match(FalseVal, m_NSWAddLike(m_Specific(CmpRHS), m_One()))) {
+ cast<Instruction>(FalseVal)->setHasNoUnsignedWrap(false);
----------------
AlexMaclean wrote:
I've moved this logic to InstCombine. Do you think it would make sense to still include it in ValueTracking but instead of removing the flag only perform the transformation if the flag is already not present? It seems that the ValueTracking version is used in a few additional places beyond IC.
https://github.com/llvm/llvm-project/pull/142466
More information about the llvm-commits
mailing list