[llvm] SelectionDAG: Fold saturations into min/max (PR #201899)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 6 07:21:04 PDT 2026


================
@@ -6284,6 +6287,37 @@ SDValue DAGCombiner::visitIMINMAX(SDNode *N) {
       !DAG.isConstantIntBuildVectorOrConstantInt(N1))
     return DAG.getNode(Opcode, DL, VT, N1, N0);
 
+  // smax(smin(X, MinC), MaxC) -> smin(smax(X, MaxC), MinC) if MinC s> MaxC
+  // umax(umin(X, MinC), MaxC) -> umin(umax(X, MaxC), MinC) if MinC u> MaxC
+  if ((Opcode == ISD::SMAX || Opcode == ISD::UMAX) && N0.hasOneUse()) {
+    unsigned InnerMinOpc = Opcode == ISD::SMAX ? ISD::SMIN : ISD::UMIN;
+    if (N0.getOpcode() == InnerMinOpc) {
+      unsigned BitWidth = VT.getScalarSizeInBits();
+      auto getMinMaxClampConstant = [&](SDValue Op, APInt &Val) -> bool {
----------------
RKSimon wrote:

Replace with all this with m_ConstInt inside the sd_match above

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


More information about the llvm-commits mailing list