[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 07:51:29 PDT 2025


================
@@ -8388,6 +8388,24 @@ static SelectPatternResult matchMinMax(CmpInst::Predicate Pred,
     }
   }
 
+  // (X > Y) ? X : (Y - 1) ==> MIN(X, Y - 1)
+  // (X < Y) ? X : (Y + 1) ==> MAX(X, Y + 1)
+  // When overflow corresponding to the sign of the comparison is poison.
+  // 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_ConstantInt<1>())))
----------------
AlexMaclean wrote:

Fixed! and I've added vector tests for each case.

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


More information about the llvm-commits mailing list