[PATCH] D141079: [SelectionDAG] Improve constant folding in the presence of SPLAT_VECTOR

Luke Lau via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 6 03:52:54 PST 2023


luke updated this revision to Diff 486804.
luke added a comment.

Rebase on top of D141075 <https://reviews.llvm.org/D141075>


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141079/new/

https://reviews.llvm.org/D141079

Files:
  llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
  llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp


Index: llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -3030,6 +3030,14 @@
     }
     break;
   }
+  case ISD::SPLAT_VECTOR: {
+    SDValue Scl = Op.getOperand(0);
+    if (Scl.isUndef())
+      KnownUndef.setAllBits();
+    if (isNullConstant(Scl) || isNullFPConstant(Scl))
+      KnownZero.setAllBits();
+    break;
+  }
   case ISD::CONCAT_VECTORS: {
     EVT SubVT = Op.getOperand(0).getValueType();
     unsigned NumSubVecs = Op.getNumOperands();
Index: llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -14042,20 +14042,32 @@
   if (N0.isUndef())
     return DAG.getUNDEF(VT);
 
-  // If the input is a BUILD_VECTOR with all constant elements, fold this now.
-  // Only do this before legalize types, unless both types are integer and the
-  // scalar type is legal. Only do this before legalize ops, since the target
-  // maybe depending on the bitcast.
-  // First check to see if this is all constant.
-  // TODO: Support FP bitcasts after legalize types.
-  if (VT.isVector() &&
+  // If the input is a BUILD_VECTOR with all constant elements or SPLAT_VECTOR
+  // with a constant scalar, fold this now. Only do this before legalize types,
+  // unless both types are integer and the scalar type is legal. Only do this
+  // before legalize ops, since the target maybe depending on the bitcast.
+  // First check to see if this is all constant.  TODO: Support FP bitcasts
+  // after legalize types.
+  if (VT.isVector() && N0->hasOneUse() &&
       (!LegalTypes ||
        (!LegalOperations && VT.isInteger() && N0.getValueType().isInteger() &&
-        TLI.isTypeLegal(VT.getVectorElementType()))) &&
-      N0.getOpcode() == ISD::BUILD_VECTOR && N0->hasOneUse() &&
-      cast<BuildVectorSDNode>(N0)->isConstant())
-    return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(),
-                                             VT.getVectorElementType());
+        TLI.isTypeLegal(VT.getVectorElementType())))) {
+    if (N0.getOpcode() == ISD::BUILD_VECTOR &&
+        cast<BuildVectorSDNode>(N0)->isConstant())
+      return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(),
+                                               VT.getVectorElementType());
+
+    // In the case of SPLAT_VECTOR, convert it back to a BUILD_VECTOR first and
+    // fold it that way
+    if (N0.getOpcode() == ISD::SPLAT_VECTOR &&
+        DAG.isConstantValueOfAnyType(N0.getOperand(0))) {
+
+      auto BVN = DAG.getSplatBuildVector(N0.getValueType(), SDLoc(N0),
+                                         N0.getOperand(0));
+      return ConstantFoldBITCASTofBUILD_VECTOR(BVN.getNode(),
+                                               VT.getVectorElementType());
+    }
+  }
 
   // If the input is a constant, let getNode fold it.
   if (isIntOrFPConstant(N0)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D141079.486804.patch
Type: text/x-patch
Size: 3094 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230106/30a56244/attachment.bin>


More information about the llvm-commits mailing list