[llvm] [InstCombine] Canonicalize `smax(smin(X, MinC), MaxC) -> smin(smax(X, MaxC), MinC)` (PR #136665)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 22 02:45:00 PDT 2025


================
@@ -1924,6 +1924,40 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
       }
     }
 
+    // Canonicalize smax(smin(X, MinC), MaxC) to smin(smax(X, MaxC), MinC)
+    // if MinC s>= MaxC.
+    if (IID == Intrinsic::smax) {
+      Constant *MinC, *MaxC;
+      if (match(I0, m_OneUse(m_Intrinsic<Intrinsic::smin>(
+                        m_Value(X), m_ImmConstant(MinC)))) &&
+          match(I1, m_ImmConstant(MaxC))) {
+
+        bool MinSgeMax = false;
+
+        ConstantInt *MinCI = dyn_cast<ConstantInt>(MinC);
+        ConstantInt *MaxCI = dyn_cast<ConstantInt>(MaxC);
+        if (MinCI && MaxCI && MinCI->getValue().sge(MaxCI->getValue())) {
+          MinSgeMax = true;
+        } else if (MinC->getType()->isVectorTy()) {
+          ConstantInt *MinSplat =
+              dyn_cast_or_null<ConstantInt>(MinC->getSplatValue());
+          ConstantInt *MaxSplat =
+              dyn_cast_or_null<ConstantInt>(MaxC->getSplatValue());
+          if (MinSplat && MaxSplat &&
+              MinSplat->getValue().sge(MaxSplat->getValue())) {
+            MinSgeMax = true;
+          }
+        }
----------------
dtcxzyw wrote:

You can use `m_APInt` to handle both scalar and splat constants.


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


More information about the llvm-commits mailing list