[llvm] [AMDGPU] Add folding ISD::SELECT from vXiY into vZi32 with X * Y = Z * 32 (PR #173328)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 6 09:08:59 PDT 2026


================
@@ -6989,6 +6989,83 @@ static SDValue combineSelectAsExtAnd(SDValue Cond, SDValue T, SDValue F,
   return DAG.getNode(ISD::AND, DL, OpVT, CondMask, T.getOperand(0));
 }
 
+// Widen int vector is not power of 2 in length.
+static SDValue widenIntVectorSelect(SDNode *N, SelectionDAG &DAG,
+                                    const TargetLowering &TLI, SDValue Cond,
+                                    SDValue TrueVal, SDValue FalseVal) {
+  EVT ResultVT = N->getValueType(0);
+  if (ResultVT.isSimple() || !ResultVT.isVector() ||
+      ResultVT.isPow2VectorType() ||
+      TLI.getTypeAction(*DAG.getContext(), ResultVT) !=
+          TargetLowering::TypeWidenVector)
+    return SDValue();
+
+  EVT EltVT = ResultVT.getVectorElementType();
+  if (!EltVT.isInteger() || ResultVT.getVectorElementCount().isScalar())
+    return SDValue();
+
+  SDValue WidenTrue = DAG.WidenVector(TrueVal, SDLoc(TrueVal));
+  SDValue WidenFalse = DAG.WidenVector(FalseVal, SDLoc(FalseVal));
+
+  EVT WidenVT = WidenTrue.getValueType();
+  SDValue WidenSelect =
+      DAG.getNode(ISD::SELECT, SDLoc(N), WidenVT, Cond, WidenTrue, WidenFalse);
+  return DAG.getExtractSubvector(SDLoc(N), ResultVT, WidenSelect, 0);
+}
+
+// Try to convert vXiY into vPiQ with:
+// 1. vXiY is not legal type
+// 2. vPiQ is legal type
+// 3. X * Y = P * Q
+// This prevent promotion of integer vectors like v32i4 to v32i16
+// which can create extra operations on type casting.
+static SDValue castIntVectorSelect(SDNode *N, SelectionDAG &DAG,
+                                   const TargetLowering &TLI, SDValue Cond,
+                                   SDValue TrueVal, SDValue FalseVal) {
+  EVT ResultVT = N->getValueType(0);
+  if (!ResultVT.isVector() || ResultVT.isScalableVector() ||
+      TLI.getTypeAction(*DAG.getContext(), ResultVT) ==
+          TargetLowering::TypeLegal)
+    return SDValue();
+
+  EVT EltVT = ResultVT.getVectorElementType();
+  if (!EltVT.isInteger() || ResultVT.getVectorElementCount().isScalar())
----------------
arsenm wrote:

This isScalar check should be pulled to the start or out of the function 

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


More information about the llvm-commits mailing list