[PATCH] D159190: [RISCV]Combine to make the fixed vector vadd of splats are scalarized

Liao Chunyu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 30 06:00:25 PDT 2023


liaolucy created this revision.
liaolucy added reviewers: craig.topper, luke.
Herald added subscribers: jobnoorman, sunshaoce, VincentWu, vkmr, frasercrmck, luismarques, apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, shiva0217, kito-cheng, niosHD, sabuasal, simoncook, johnrusso, rbar, asb, hiraditya, arichardson.
Herald added a project: All.
liaolucy requested review of this revision.
Herald added subscribers: llvm-commits, wangpc, eopXD, MaskRay.
Herald added a project: LLVM.

vadd (build_vector x), (build_vector c) -> build_vector (add x, c)
Try to fix https://github.com/llvm/llvm-project/issues/65068
Subsequently, more operands can be extended.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D159190

Files:
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp
  llvm/test/CodeGen/RISCV/rvv/pr65068.ll


Index: llvm/test/CodeGen/RISCV/rvv/pr65068.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/RISCV/rvv/pr65068.ll
@@ -0,0 +1,16 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 3
+; RUN: llc -mtriple=riscv64 -mattr=+v -riscv-v-vector-bits-min=128 -verify-machineinstrs < %s | FileCheck %s --check-prefixes=CHECK-RV64
+
+define <4 x i64> @f_v4i64(<4 x i64> %x, i64 %y) {
+; CHECK-RV64-LABEL: f_v4i64:
+; CHECK-RV64:       # %bb.0:
+; CHECK-RV64-NEXT:    addi a0, a0, 3
+; CHECK-RV64-NEXT:    vsetivli zero, 4, e64, m2, ta, ma
+; CHECK-RV64-NEXT:    vmul.vx v8, v8, a0
+; CHECK-RV64-NEXT:    ret
+  %1 = insertelement <4 x i64> poison, i64 %y, i32 0
+  %2 = shufflevector <4 x i64> %1, <4 x i64> poison, <4 x i32> zeroinitializer
+  %3 = add <4 x i64> %2, <i64 3, i64 3, i64 3, i64 3>
+  %4 = mul <4 x i64> %x, %3
+  ret <4 x i64> %4
+}
Index: llvm/lib/Target/RISCV/RISCVISelLowering.cpp
===================================================================
--- llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -11204,8 +11204,54 @@
                      N0.getOperand(0));
 }
 
+// vadd (build_vector x), (build_vector c)
+// -> build_vector (add x, c)
+static SDValue transformSplatVectorToScalar(SDNode *N, SelectionDAG &DAG,
+                                            const RISCVSubtarget &Subtarget) {
+  SDValue N0 = N->getOperand(0);
+  SDValue N1 = N->getOperand(1);
+  APInt ShAmt;
+  if (N->getOpcode() == ISD::ADD && N0.getOpcode() == ISD::BUILD_VECTOR &&
+      ISD::isConstantSplatVector(N1.getNode(), ShAmt) && N0.hasOneUse() &&
+      N1.hasOneUse()) {
+    EVT VT0 = N0.getValueType();
+    EVT VT1 = N1.getValueType();
+    unsigned NumElts0 = VT0.getVectorNumElements();
+    unsigned NumElts1 = VT1.getVectorNumElements();
+    SDValue Base;
+    SDLoc DL(N);
+    bool AllSame = true;
+    for (unsigned i = 0; i != NumElts0; ++i) {
+      if (!N0.getOperand(i).isUndef()) {
+        Base = N0.getOperand(i);
+        break;
+      }
+    }
+    for (unsigned i = 0; i != NumElts0; ++i) {
+      if (N0.getOperand(i) != Base) {
+        AllSame = false;
+        break;
+      }
+    }
+    MVT XLenVT = Subtarget.getXLenVT();
+    if (AllSame && NumElts0 == NumElts1 && Base.getValueType() == XLenVT) {
+      SDValue NAdd = DAG.getNode(ISD::ADD, DL, XLenVT, Base, N1.getOperand(0));
+      SmallVector<SDValue, 6> NOps;
+      for (unsigned i = 0; i != NumElts0; ++i) {
+        NOps.push_back(NAdd);
+      }
+      return DAG.getNode(ISD::BUILD_VECTOR, DL, VT0, NOps);
+    }
+  }
+  return SDValue();
+}
+
 static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
                                  const RISCVSubtarget &Subtarget) {
+  EVT VT = N->getValueType(0);
+  if (VT.isFixedLengthVector())
+    return transformSplatVectorToScalar(N, DAG, Subtarget);
+
   if (SDValue V = combineAddOfBooleanXor(N, DAG))
     return V;
   if (SDValue V = transformAddImmMulImm(N, DAG, Subtarget))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159190.554682.patch
Type: text/x-patch
Size: 3073 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230830/53f65014/attachment.bin>


More information about the llvm-commits mailing list