[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:53:46 PST 2023


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

Update test


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
  llvm/test/CodeGen/WebAssembly/pr59626.ll


Index: llvm/test/CodeGen/WebAssembly/pr59626.ll
===================================================================
--- llvm/test/CodeGen/WebAssembly/pr59626.ll
+++ llvm/test/CodeGen/WebAssembly/pr59626.ll
@@ -12,12 +12,7 @@
 ; CHECK-32-NEXT:    local.get 0
 ; CHECK-32-NEXT:    i32.const 0
 ; CHECK-32-NEXT:    i32.store16 0
-; CHECK-32-NEXT:    local.get 1
-; CHECK-32-NEXT:    local.get 0
-; CHECK-32-NEXT:    i8x16.splat
-; CHECK-32-NEXT:    v128.store16_lane 0, 0
-; CHECK-32-NEXT:    v128.const 0, 0
-; CHECK-32-NEXT:    i32x4.extract_lane 0
+; CHECK-32-NEXT:    i32.const 0
 ; CHECK-32-NEXT:    # fallthrough-return
 ;
 ; CHECK-64-LABEL: f:
@@ -35,8 +30,7 @@
 ; CHECK-64-NEXT:    i8x16.splat
 ; CHECK-64-NEXT:    v128.store16_lane 0, 0
 ; CHECK-64-NEXT:    drop
-; CHECK-64-NEXT:    v128.const 0, 0
-; CHECK-64-NEXT:    i32x4.extract_lane 0
+; CHECK-64-NEXT:    i32.const 0
 ; CHECK-64-NEXT:    # fallthrough-return
 BB:
   store <3 x i8> zeroinitializer, ptr %0
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.486805.patch
Type: text/x-patch
Size: 4064 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230106/ac05807b/attachment.bin>


More information about the llvm-commits mailing list