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

David Green via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 7 01:46:58 PDT 2024


================
@@ -14915,6 +14920,171 @@ SDValue DAGCombiner::visitEXTEND_VECTOR_INREG(SDNode *N) {
   return SDValue();
 }
 
+SDValue DAGCombiner::visitTRUNCATE_USAT(SDNode *N) {
+  EVT VT = N->getValueType(0);
+  SDValue N0 = N->getOperand(0);
+
+  std::function<SDValue(SDValue)> MatchFPTOINT = [&](SDValue Val) -> SDValue {
+    if (Val.getOpcode() == ISD::FP_TO_UINT)
+      return Val;
+    if (Val.getOpcode() == ISD::SMAX) {
+      for (unsigned I = 0; I < Val.getNumOperands(); I++)
+        if (SDValue Matched = MatchFPTOINT(Val.getOperand(I)))
+          return Matched;
+    }
+    return SDValue();
+  };
+
+  SDValue FPInstr = MatchFPTOINT(N0);
+  if (!FPInstr)
+    return SDValue();
+
+  EVT FPVT = FPInstr.getOperand(0).getValueType();
+  if (!DAG.getTargetLoweringInfo().shouldConvertFpToSat(ISD::FP_TO_UINT_SAT,
+                                                        FPVT, VT))
+    return SDValue();
+  return DAG.getNode(ISD::FP_TO_UINT_SAT, SDLoc(FPInstr), VT,
+                     FPInstr.getOperand(0),
+                     DAG.getValueType(VT.getScalarType()));
+}
+
+// Match min and return limit value as a parameter.
+static SDValue matchMinMax(SDValue V, unsigned Opcode, APInt &Limit) {
+  if (V.getOpcode() == Opcode) {
+    APInt C;
+    if (ISD::isConstantSplatVector(V.getOperand(1).getNode(), C)) {
+      if (C == Limit)
+        return V.getOperand(0);
+      if (V.getOpcode() == ISD::SMAX && !Limit.isNegative() && C.uge(Limit) &&
+          Limit != 0)
+        return V.getOperand(0);
----------------
davemgreen wrote:

Remove this SMAX special case.

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


More information about the llvm-commits mailing list