[llvm] [DAG] Support saturated truncate (PR #99418)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 8 07:52:23 PDT 2024


================
@@ -14992,50 +14978,46 @@ static SDValue detectSSatSPattern(SDValue In, EVT VT) {
   // Saturation with truncation. We truncate from InVT to VT.
   assert(NumSrcBits > NumDstBits && "Unexpected types for truncate operation");
 
-  APInt SignedMax, SignedMin;
-  SDValue Max, Min;
-  SignedMax = APInt::getSignedMaxValue(NumDstBits).sext(NumSrcBits);
-  SignedMin = APInt::getSignedMinValue(NumDstBits).sext(NumSrcBits);
-  if (Min = matchMinMax(In, ISD::SMIN, SignedMax))
-    if (Max = matchMinMax(Min, ISD::SMAX, SignedMin))
-      return Max;
+  SDValue Val;
+  APInt SignedMax = APInt::getSignedMaxValue(NumDstBits).sext(NumSrcBits);
+  APInt SignedMin = APInt::getSignedMinValue(NumDstBits).sext(NumSrcBits);
 
-  if (Max = matchMinMax(In, ISD::SMAX, SignedMin))
-    if (Min = matchMinMax(Max, ISD::SMIN, SignedMax))
-      return Min;
+  if (sd_match(In, m_SMin(m_SMax(m_Value(Val), m_SpecificInt(SignedMin)),
+                          m_SpecificInt(SignedMax))))
+    return Val;
+
+  if (sd_match(In, m_SMax(m_SMin(m_Value(Val), m_SpecificInt(SignedMax)),
+                          m_SpecificInt(SignedMin))))
+    return Val;
 
   return SDValue();
 }
 
 /// Detect patterns of truncation with unsigned saturation:
 static SDValue detectSSatUPattern(SDValue In, EVT VT, SelectionDAG &DAG,
                                   const SDLoc &DL) {
-  EVT InVT = In.getValueType();
   unsigned NumDstBits = VT.getScalarSizeInBits();
   unsigned NumSrcBits = In.getScalarValueSizeInBits();
   // Saturation with truncation. We truncate from InVT to VT.
   assert(NumSrcBits > NumDstBits && "Unexpected types for truncate operation");
 
-  APInt UnsignedMax, Zero, One;
-  SDValue Max, Min;
+  SDValue Val;
+  APInt UnsignedMax, Zero;
   UnsignedMax = APInt::getMaxValue(NumDstBits).zext(NumSrcBits);
   Zero = APInt::getZero(NumSrcBits);
-  One = APInt(NumSrcBits, 1);
 
   // Min == 0, Max is unsigned max of destination type.
-  if (Max = matchMinMax(In, ISD::SMAX, Zero)) {
-    if (Min = matchMinMax(Max, ISD::SMIN, UnsignedMax))
-      return Max.getOperand(0);
-  }
-  // Min >= 0, Max is unsigned max of destination type.
-  if (Min = matchMinMax(In, ISD::SMIN, UnsignedMax)) {
-    if (Max = matchMinMax(Min, ISD::SMAX, Zero))
-      return Min.getOperand(0);
-  }
-  if (Min = matchMinMax(In, ISD::UMIN, UnsignedMax)) {
-    if (Max = matchMinMax(Min, ISD::SMAX, Zero))
-      return Min.getOperand(0);
-  }
+  if (sd_match(In, m_SMax(m_SMin(m_Value(Val), m_SpecificInt(UnsignedMax)),
+                          m_SpecificInt(Zero))))
----------------
RKSimon wrote:

You can use m_Zero() instead of m_SpecificInt(Zero)

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


More information about the llvm-commits mailing list