[PATCH] D126532: [SVE] Add a DAG combiner fold to visitADD for vscale with truncate

Allen zhong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 12 19:39:20 PDT 2022


Allen updated this revision to Diff 436266.
Allen added a comment.

Add the check **Level > AfterLegalizeVectorOps**


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

https://reviews.llvm.org/D126532

Files:
  llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
  llvm/test/CodeGen/AArch64/sve-vscale-combine.ll


Index: llvm/test/CodeGen/AArch64/sve-vscale-combine.ll
===================================================================
--- llvm/test/CodeGen/AArch64/sve-vscale-combine.ll
+++ llvm/test/CodeGen/AArch64/sve-vscale-combine.ll
@@ -95,3 +95,17 @@
  %shl = shl i32 %vscale, 4
  ret i32 %shl
 }
+
+; Fold a+truncate(vscale(c1))+truncate(vscale(c2)) to a+truncate(vscale(c1+c2))
+define i32 @combine_add_vscale_C_i32(i32 %index) nounwind {
+; CHECK-LABEL: combine_add_vscale_C_i32:
+; CHECK-NEXT:    cntd x8, all, mul #5
+; CHECK-NEXT:    add w0, w0, w8
+; CHECK-NEXT:    ret
+  %vscale = call i32 @llvm.vscale.i32()
+  %mul8 = mul i32 %vscale, 8
+  %mul2 = mul i32 %vscale, 2
+  %index.next = add nuw i32 %index, %mul8
+  %add = add nuw i32 %index.next, %mul2
+  ret i32 %add
+}
Index: llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -2627,6 +2627,21 @@
     return DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(0), VS);
   }
 
+  // fold a+truncate(vscale(c1))+truncate(vscale(c2))
+  // to a+truncate(vscale(c1+c2))
+  if (Level > AfterLegalizeVectorOps && (N0.getOpcode() == ISD::ADD) &&
+      (N0.getOperand(1).getOpcode() == ISD::TRUNCATE) &&
+      (N0.getOperand(1).getOperand(0).getOpcode() == ISD::VSCALE) &&
+      (N1.getOpcode() == ISD::TRUNCATE) &&
+      (N1.getOperand(0).getOpcode() == ISD::VSCALE)) {
+    EVT VSVT = N1.getOperand(0).getValueType();
+    const APInt &VS0 =
+        N0.getOperand(1).getOperand(0)->getConstantOperandAPInt(0);
+    const APInt &VS1 = N1.getOperand(0)->getConstantOperandAPInt(0);
+    SDValue VS = DAG.getZExtOrTrunc(DAG.getVScale(DL, VSVT, VS0 + VS1), DL, VT);
+    return DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(0), VS);
+  }
+
   // Fold (add step_vector(c1), step_vector(c2)  to step_vector(c1+c2))
   if (N0.getOpcode() == ISD::STEP_VECTOR &&
       N1.getOpcode() == ISD::STEP_VECTOR) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126532.436266.patch
Type: text/x-patch
Size: 2012 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220613/e24639bb/attachment.bin>


More information about the llvm-commits mailing list