[llvm] [X86][AVX10.2] Canonicalize narrow FP_TO_{S, U}INT_SAT (PR #186786)

Phoebe Wang via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 16 23:30:16 PDT 2026


================
@@ -58296,31 +58239,67 @@ static SDValue combineFPToSInt(SDNode *N, SelectionDAG &DAG,
   return SDValue();
 }
 
-// Custom handling for VCVTTPS2QQS/VCVTTPS2UQQS
 static SDValue combineFP_TO_xINT_SAT(SDNode *N, SelectionDAG &DAG,
                                      const X86Subtarget &Subtarget) {
+  unsigned Opcode = N->getOpcode();
+  assert((Opcode == ISD::FP_TO_SINT_SAT || Opcode == ISD::FP_TO_UINT_SAT) &&
+         "Unexpected opcode for combineFP_TO_xINT_SAT");
   if (!Subtarget.hasAVX10_2())
     return SDValue();
 
-  bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT_SAT;
-  EVT SrcVT = N->getOperand(0).getValueType();
+  bool IsSigned = Opcode == ISD::FP_TO_SINT_SAT;
+  SDValue Src = N->getOperand(0);
+  EVT SrcVT = Src.getValueType();
   EVT DstVT = N->getValueType(0);
-  SDLoc dl(N);
+  EVT SatVT = cast<VTSDNode>(N->getOperand(1))->getVT();
+  SDLoc DL(N);
 
-  if (SrcVT == MVT::v2f32 && DstVT == MVT::v2i64) {
-    SDValue V2F32Value = DAG.getUNDEF(SrcVT);
+  // Preserve the existing full-width v2f32 -> v2i64 target-specific combine.
+  if (SrcVT == MVT::v2f32 && DstVT == MVT::v2i64 &&
+      SatVT.getScalarSizeInBits() == 64) {
+    SDValue Undef = DAG.getUNDEF(SrcVT);
+    SDValue NewSrc =
+        DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32, Src, Undef);
+    if (IsSigned)
+      return DAG.getNode(X86ISD::FP_TO_SINT_SAT, DL, MVT::v2i64, NewSrc);
+    return DAG.getNode(X86ISD::FP_TO_UINT_SAT, DL, MVT::v2i64, NewSrc);
+  }
 
-    // Concatenate the original v2f32 input and V2F32Value to create v4f32
-    SDValue NewSrc = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v4f32,
-                                 N->getOperand(0), V2F32Value);
+  // Scalar/scalar or vector/vector only.
+  if (SrcVT.isVector() != DstVT.isVector())
+    return SDValue();
 
-    // Select the FP_TO_SINT_SAT/FP_TO_UINT_SAT node
-    if (IsSigned)
-      return DAG.getNode(X86ISD::FP_TO_SINT_SAT, dl, MVT::v2i64, NewSrc);
+  bool IsVector = SrcVT.isVector();
+  if (IsVector &&
+      (!SrcVT.isFixedLengthVector() || !DstVT.isFixedLengthVector() ||
+       SrcVT.getVectorElementCount() != DstVT.getVectorElementCount()))
+    return SDValue();
+
+  EVT SrcEltVT = IsVector ? SrcVT.getVectorElementType() : SrcVT;
----------------
phoebewang wrote:

getScalarType()

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


More information about the llvm-commits mailing list