[llvm-branch-commits] [llvm] Users/topperc/insert subvector (PR #241139)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Nov 10 21:54:22 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-risc-v

Author: Craig Topper (topperc)

<details>
<summary>Changes</summary>

The extract_subvector will become an extract_subreg and concat_vectors will become a reg_sequence.

I tried to make insert_subvector legal and isel to insert_subreg, but that ended up regressing the inserting into zero cases. We would materialize a GPRPair of 0 and then insert into it. The extract_subvector+concat_vectos avoids this by shrinking the zero vector.

Assisted-by: Claude

---

Patch is 22.50 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/241139.diff


5 Files Affected:

- (modified) llvm/lib/Target/RISCV/RISCVISelLowering.cpp (+49-32) 
- (modified) llvm/lib/Target/RISCV/RISCVInstrInfoP.td (+5) 
- (modified) llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll (+127) 
- (modified) llvm/test/CodeGen/RISCV/rvp-simd-32.ll (+37-6) 
- (modified) llvm/test/CodeGen/RISCV/rvp-simd-64.ll (+102-30) 


``````````diff
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index eef8d5237af87..01d9b55ea105e 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -618,9 +618,8 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
       setOperationAction(ISD::SPLAT_VECTOR, P64VecVTs, Legal);
       setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Legal);
       setOperationAction(ISD::BUILD_VECTOR, {MVT::v4i16, MVT::v8i8}, Custom);
-      setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Legal);
       setOperationAction({ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT},
-                         {MVT::v4i16, MVT::v8i8}, Custom);
+                         P64VecVTs, Custom);
       setOperationAction(ISD::CONCAT_VECTORS, {MVT::v4i16, MVT::v8i8}, Legal);
       setOperationAction(ISD::EXTRACT_SUBVECTOR, {MVT::v2i16, MVT::v4i8},
                          Legal);
@@ -11692,6 +11691,11 @@ SDValue RISCVTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
     unsigned NumElts = VecVT.getVectorNumElements();
     MVT EltVT = VecVT.getVectorElementType();
 
+    // v2i32 on RV32 lives in a GPR pair; constant index inserts are selected
+    // with INSERT_SUBREG.
+    if (!Subtarget.is64Bit() && VecVT == MVT::v2i32)
+      return Op;
+
     if (!Subtarget.is64Bit() && (VecVT == MVT::v4i16 || VecVT == MVT::v8i8)) {
       unsigned HalfNumElts = NumElts / 2;
       auto [Lo, Hi] = DAG.SplitVector(Vec, DL);
@@ -11707,23 +11711,17 @@ SDValue RISCVTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
       return DAG.getNode(ISD::CONCAT_VECTORS, DL, VecVT, Lo, NewHi);
     }
 
-    Vec = DAG.getBitcast(XLenVT, Vec);
     SDValue ExtVal = DAG.getNode(ISD::ANY_EXTEND, DL, XLenVT, Val);
 
-    // For 2-element vectors, BUILD_VECTOR is more efficient since it only needs
-    // at most 2 instructions.
+    // A 2-element vector is a single PPAIR of the old vector and the new
+    // element.
     if (NumElts == 2) {
-      unsigned EltBits = EltVT.getSizeInBits();
-      SDValue Elt0, Elt1;
-      if (IdxVal == 0) {
-        Elt0 = ExtVal;
-        Elt1 = DAG.getNode(ISD::SRL, DL, XLenVT, Vec,
-                           DAG.getConstant(EltBits, DL, XLenVT));
-      } else {
-        Elt0 = Vec;
-        Elt1 = ExtVal;
-      }
-      return DAG.getNode(ISD::BUILD_VECTOR, DL, VecVT, Elt0, Elt1);
+      SDValue NewElt = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VecVT, ExtVal);
+      // Index 0: take element 0 from NewElt and element 1 from Vec.
+      if (IdxVal == 0)
+        return DAG.getNode(RISCVISD::PPAIREO, DL, VecVT, NewElt, Vec);
+      // Index 1: take element 0 from Vec and element 0 of NewElt.
+      return DAG.getNode(RISCVISD::PPAIRE, DL, VecVT, Vec, NewElt);
     }
 
     // For 4/8-element vectors, use MVM(or MERGE) instruction which does bitwise
@@ -11737,6 +11735,7 @@ SDValue RISCVTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
       SDValue ShiftedVal = DAG.getNode(ISD::SHL, DL, XLenVT, ExtVal,
                                        DAG.getConstant(ShiftAmt, DL, XLenVT));
       SDValue Mask = DAG.getConstant(PosMask, DL, XLenVT);
+      Vec = DAG.getBitcast(XLenVT, Vec);
       SDValue Result =
           DAG.getNode(RISCVISD::MERGE, DL, XLenVT, Mask, Vec, ShiftedVal);
       return DAG.getBitcast(VecVT, Result);
@@ -11979,10 +11978,30 @@ SDValue RISCVTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
         VecVT != MVT::v4i8 && VecVT != MVT::v2i32)
       return SDValue();
 
-    if (!Subtarget.is64Bit() && (VecVT == MVT::v4i16 || VecVT == MVT::v8i8)) {
+    // v2i32 on RV32 lives in a GPR pair; constant index extracts are selected
+    // with EXTRACT_SUBREG.
+    if (!Subtarget.is64Bit() && VecVT.getSizeInBits() == 64) {
       auto *IdxC = dyn_cast<ConstantSDNode>(Idx);
-      if (!IdxC)
-        return SDValue();
+      if (!IdxC) {
+        // Shift the pair right by the bit offset of the element. NSRL reads 6
+        // bits of shift amount so it handles offsets >= 32.
+        SDValue Lo, Hi;
+        if (VecVT == MVT::v2i32) {
+          Lo = DAG.getExtractVectorElt(DL, MVT::i32, Vec, 0);
+          Hi = DAG.getExtractVectorElt(DL, MVT::i32, Vec, 1);
+        } else {
+          std::tie(Lo, Hi) = DAG.SplitVector(Vec, DL);
+          Lo = DAG.getBitcast(XLenVT, Lo);
+          Hi = DAG.getBitcast(XLenVT, Hi);
+        }
+        unsigned ElemWidth = VecVT.getVectorElementType().getSizeInBits();
+        SDValue Shamt = DAG.getNode(ISD::MUL, DL, XLenVT, Idx,
+                                    DAG.getConstant(ElemWidth, DL, XLenVT));
+        return DAG.getNode(RISCVISD::NSRL, DL, XLenVT, Lo, Hi, Shamt);
+      }
+      // v2i32 constant index extracts are selected with EXTRACT_SUBREG.
+      if (VecVT == MVT::v2i32)
+        return Op;
       unsigned IdxVal = IdxC->getZExtValue();
       unsigned HalfNumElts = VecVT.getVectorNumElements() / 2;
       auto [Lo, Hi] = DAG.SplitVector(Vec, DL);
@@ -14743,20 +14762,18 @@ SDValue RISCVTargetLowering::lowerINSERT_SUBVECTOR(SDValue Op,
   unsigned OrigIdx = Op.getConstantOperandVal(2);
   const RISCVRegisterInfo *TRI = Subtarget.getRegisterInfo();
 
-  bool IsPExtInsert =
-      Subtarget.hasStdExtP() &&
-      (!Subtarget.is64Bit() && (VecVT == MVT::v4i16 || VecVT == MVT::v8i8));
-
-  // Fold insert of a 32-bit packed type into a zero-filled 64-bit packed vector
-  // at index 0 (a zero-extend) to avoid scalarizing it into a byte-wise repack.
-  if (IsPExtInsert) {
-    if ((VecVT != MVT::v4i16 && VecVT != MVT::v8i8) ||
-        SubVecVT.getSizeInBits() != 32 || OrigIdx != 0 ||
-        !ISD::isConstantSplatVectorAllZeros(Vec.getNode()))
+  // Lower a 32-bit insert into a 64-bit packed vector on RV32 to an
+  // extract_subvector of the other half and a concat_vectors.
+  if (Subtarget.hasStdExtP() && !Subtarget.is64Bit() &&
+      (VecVT == MVT::v4i16 || VecVT == MVT::v8i8)) {
+    unsigned HalfElts = VecVT.getVectorNumElements() / 2;
+    if (SubVecVT.getSizeInBits() != 32 || (OrigIdx != 0 && OrigIdx != HalfElts))
       return SDValue();
-
-    SDValue Zero = DAG.getConstant(0, DL, SubVecVT);
-    return DAG.getNode(ISD::CONCAT_VECTORS, DL, VecVT, SubVec, Zero);
+    SDValue Other =
+        DAG.getExtractSubvector(DL, SubVecVT, Vec, OrigIdx == 0 ? HalfElts : 0);
+    return OrigIdx == 0
+               ? DAG.getNode(ISD::CONCAT_VECTORS, DL, VecVT, SubVec, Other)
+               : DAG.getNode(ISD::CONCAT_VECTORS, DL, VecVT, Other, SubVec);
   }
 
   if (OrigIdx == 0 && Vec.isUndef())
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoP.td b/llvm/lib/Target/RISCV/RISCVInstrInfoP.td
index 2ecec70322234..0a98afd622b45 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfoP.td
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfoP.td
@@ -3264,6 +3264,11 @@ let append Predicates = [IsRV32] in {
   def : Pat<(i32 (extractelt (v2i32 GPRPair:$vec), (i32 1))),
             (EXTRACT_SUBREG GPRPair:$vec, sub_gpr_odd)>;
 
+  def : Pat<(v2i32 (vector_insert (v2i32 GPRPair:$vec), (i32 GPR:$val), (i32 0))),
+            (INSERT_SUBREG GPRPair:$vec, GPR:$val, sub_gpr_even)>;
+  def : Pat<(v2i32 (vector_insert (v2i32 GPRPair:$vec), (i32 GPR:$val), (i32 1))),
+            (INSERT_SUBREG GPRPair:$vec, GPR:$val, sub_gpr_odd)>;
+
   def : Pat<(v2i16 (extract_subvector (v4i16 GPRPair:$vec), (i32 0))),
             (EXTRACT_SUBREG GPRPair:$vec, sub_gpr_even)>;
   def : Pat<(v2i16 (extract_subvector (v4i16 GPRPair:$vec), (i32 2))),
diff --git a/llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll b/llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll
index 6ba26ca595835..88368d8496ce3 100644
--- a/llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll
+++ b/llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll
@@ -43,3 +43,130 @@ define <2 x i32> @insert_zero_v1i32(<1 x i32> %v) {
   %r = call <2 x i32> @llvm.vector.insert.v2i32.v1i32(<2 x i32> zeroinitializer, <1 x i32> %v, i64 0)
   ret <2 x i32> %r
 }
+
+define <8 x i8> @insert_lo_v4i8(<8 x i8> %v, <4 x i8> %s) {
+; RV32-LABEL: insert_lo_v4i8:
+; RV32:       # %bb.0:
+; RV32-NEXT:    mv a0, a2
+; RV32-NEXT:    ret
+;
+; RV64-LABEL: insert_lo_v4i8:
+; RV64:       # %bb.0:
+; RV64-NEXT:    li a2, 255
+; RV64-NEXT:    mvm a0, a1, a2
+; RV64-NEXT:    lui a3, 16
+; RV64-NEXT:    andi a4, a1, -256
+; RV64-NEXT:    addi a3, a3, -256
+; RV64-NEXT:    mvm a0, a4, a3
+; RV64-NEXT:    lui a3, 1048560
+; RV64-NEXT:    and a3, a1, a3
+; RV64-NEXT:    lui a4, 4080
+; RV64-NEXT:    mvm a0, a3, a4
+; RV64-NEXT:    lui a3, 1044480
+; RV64-NEXT:    and a1, a1, a3
+; RV64-NEXT:    slli a2, a2, 24
+; RV64-NEXT:    mvm a0, a1, a2
+; RV64-NEXT:    ret
+  %r = call <8 x i8> @llvm.vector.insert.v8i8.v4i8(<8 x i8> %v, <4 x i8> %s, i64 0)
+  ret <8 x i8> %r
+}
+
+define <8 x i8> @insert_hi_v4i8(<8 x i8> %v, <4 x i8> %s) {
+; RV32-LABEL: insert_hi_v4i8:
+; RV32:       # %bb.0:
+; RV32-NEXT:    mv a1, a2
+; RV32-NEXT:    ret
+;
+; RV64-LABEL: insert_hi_v4i8:
+; RV64:       # %bb.0:
+; RV64-NEXT:    li a2, 255
+; RV64-NEXT:    slli a1, a1, 32
+; RV64-NEXT:    slli a3, a2, 32
+; RV64-NEXT:    mvm a0, a1, a3
+; RV64-NEXT:    srli a3, a1, 40
+; RV64-NEXT:    slli a3, a3, 40
+; RV64-NEXT:    slli a4, a2, 40
+; RV64-NEXT:    mvm a0, a3, a4
+; RV64-NEXT:    srli a3, a1, 48
+; RV64-NEXT:    slli a3, a3, 48
+; RV64-NEXT:    li a4, -1
+; RV64-NEXT:    slli a2, a2, 48
+; RV64-NEXT:    slli a4, a4, 56
+; RV64-NEXT:    mvm a0, a3, a2
+; RV64-NEXT:    and a1, a1, a4
+; RV64-NEXT:    mvm a0, a1, a4
+; RV64-NEXT:    ret
+  %r = call <8 x i8> @llvm.vector.insert.v8i8.v4i8(<8 x i8> %v, <4 x i8> %s, i64 4)
+  ret <8 x i8> %r
+}
+
+define <4 x i16> @insert_lo_v2i16(<4 x i16> %v, <2 x i16> %s) {
+; RV32-LABEL: insert_lo_v2i16:
+; RV32:       # %bb.0:
+; RV32-NEXT:    mv a0, a2
+; RV32-NEXT:    ret
+;
+; RV64-LABEL: insert_lo_v2i16:
+; RV64:       # %bb.0:
+; RV64-NEXT:    lui a2, 16
+; RV64-NEXT:    addi a2, a2, -1
+; RV64-NEXT:    lui a3, 1048560
+; RV64-NEXT:    mvm a0, a1, a2
+; RV64-NEXT:    lui a2, 65535
+; RV64-NEXT:    and a1, a1, a3
+; RV64-NEXT:    slli a2, a2, 4
+; RV64-NEXT:    mvm a0, a1, a2
+; RV64-NEXT:    ret
+  %r = call <4 x i16> @llvm.vector.insert.v4i16.v2i16(<4 x i16> %v, <2 x i16> %s, i64 0)
+  ret <4 x i16> %r
+}
+
+define <4 x i16> @insert_hi_v2i16(<4 x i16> %v, <2 x i16> %s) {
+; RV32-LABEL: insert_hi_v2i16:
+; RV32:       # %bb.0:
+; RV32-NEXT:    mv a1, a2
+; RV32-NEXT:    ret
+;
+; RV64-LABEL: insert_hi_v2i16:
+; RV64:       # %bb.0:
+; RV64-NEXT:    lui a2, 65535
+; RV64-NEXT:    slli a1, a1, 32
+; RV64-NEXT:    li a3, -1
+; RV64-NEXT:    slli a2, a2, 20
+; RV64-NEXT:    slli a3, a3, 48
+; RV64-NEXT:    mvm a0, a1, a2
+; RV64-NEXT:    and a1, a1, a3
+; RV64-NEXT:    mvm a0, a1, a3
+; RV64-NEXT:    ret
+  %r = call <4 x i16> @llvm.vector.insert.v4i16.v2i16(<4 x i16> %v, <2 x i16> %s, i64 2)
+  ret <4 x i16> %r
+}
+
+define <8 x i8> @insert_hi_undef_v4i8(<4 x i8> %s) {
+; RV32-LABEL: insert_hi_undef_v4i8:
+; RV32:       # %bb.0:
+; RV32-NEXT:    mv a1, a0
+; RV32-NEXT:    ret
+;
+; RV64-LABEL: insert_hi_undef_v4i8:
+; RV64:       # %bb.0:
+; RV64-NEXT:    ppairoe.w a0, a0, a0
+; RV64-NEXT:    ret
+  %r = call <8 x i8> @llvm.vector.insert.v8i8.v4i8(<8 x i8> poison, <4 x i8> %s, i64 4)
+  ret <8 x i8> %r
+}
+
+define <4 x i16> @insert_hi_zero_v2i16(<2 x i16> %s) {
+; RV32-LABEL: insert_hi_zero_v2i16:
+; RV32:       # %bb.0:
+; RV32-NEXT:    mv a1, a0
+; RV32-NEXT:    li a0, 0
+; RV32-NEXT:    ret
+;
+; RV64-LABEL: insert_hi_zero_v2i16:
+; RV64:       # %bb.0:
+; RV64-NEXT:    pack a0, zero, a0
+; RV64-NEXT:    ret
+  %r = call <4 x i16> @llvm.vector.insert.v4i16.v2i16(<4 x i16> zeroinitializer, <2 x i16> %s, i64 2)
+  ret <4 x i16> %r
+}
diff --git a/llvm/test/CodeGen/RISCV/rvp-simd-32.ll b/llvm/test/CodeGen/RISCV/rvp-simd-32.ll
index fc5a17924177b..83e3568ffe8f8 100644
--- a/llvm/test/CodeGen/RISCV/rvp-simd-32.ll
+++ b/llvm/test/CodeGen/RISCV/rvp-simd-32.ll
@@ -564,6 +564,22 @@ define i16 @test_extract_vector_16_elem1(<2 x i16> %a) {
   ret i16 %extracted
 }
 
+define i16 @test_extract_vector_16_variable(<2 x i16> %a, i32 %idx) {
+; RV32-LABEL: test_extract_vector_16_variable:
+; RV32:       # %bb.0:
+; RV32-NEXT:    slli a1, a1, 4
+; RV32-NEXT:    srl a0, a0, a1
+; RV32-NEXT:    ret
+;
+; RV64-LABEL: test_extract_vector_16_variable:
+; RV64:       # %bb.0:
+; RV64-NEXT:    slli.uw a1, a1, 4
+; RV64-NEXT:    srl a0, a0, a1
+; RV64-NEXT:    ret
+  %res = extractelement <2 x i16> %a, i32 %idx
+  ret i16 %res
+}
+
 define i8 @test_extract_vector_8(<4 x i8> %a) {
 ; CHECK-LABEL: test_extract_vector_8:
 ; CHECK:       # %bb.0:
@@ -581,11 +597,26 @@ define i8 @test_extract_vector_8_elem1(<4 x i8> %a) {
   ret i8 %extracted
 }
 
+define i8 @test_extract_vector_8_variable(<4 x i8> %a, i32 %idx) {
+; RV32-LABEL: test_extract_vector_8_variable:
+; RV32:       # %bb.0:
+; RV32-NEXT:    slli a1, a1, 3
+; RV32-NEXT:    srl a0, a0, a1
+; RV32-NEXT:    ret
+;
+; RV64-LABEL: test_extract_vector_8_variable:
+; RV64:       # %bb.0:
+; RV64-NEXT:    slli.uw a1, a1, 3
+; RV64-NEXT:    srl a0, a0, a1
+; RV64-NEXT:    ret
+  %res = extractelement <4 x i8> %a, i32 %idx
+  ret i8 %res
+}
+
 define <2 x i16> @test_insert_vector_16(<2 x i16> %a, i16 %val) {
 ; RV32-LABEL: test_insert_vector_16:
 ; RV32:       # %bb.0:
-; RV32-NEXT:    srli a0, a0, 16
-; RV32-NEXT:    pack a0, a1, a0
+; RV32-NEXT:    ppaireo.h a0, a1, a0
 ; RV32-NEXT:    ret
 ;
 ; RV64-LABEL: test_insert_vector_16:
@@ -2608,10 +2639,10 @@ define <2 x i16> @test_select_v2i16(i1 %cond, <2 x i16> %a, <2 x i16> %b) {
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    andi a3, a0, 1
 ; CHECK-NEXT:    mv a0, a1
-; CHECK-NEXT:    bnez a3, .LBB184_2
+; CHECK-NEXT:    bnez a3, .LBB186_2
 ; CHECK-NEXT:  # %bb.1:
 ; CHECK-NEXT:    mv a0, a2
-; CHECK-NEXT:  .LBB184_2:
+; CHECK-NEXT:  .LBB186_2:
 ; CHECK-NEXT:    ret
   %res = select i1 %cond, <2 x i16> %a, <2 x i16> %b
   ret <2 x i16> %res
@@ -2622,10 +2653,10 @@ define <4 x i8> @test_select_v4i8(i1 %cond, <4 x i8> %a, <4 x i8> %b) {
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    andi a3, a0, 1
 ; CHECK-NEXT:    mv a0, a1
-; CHECK-NEXT:    bnez a3, .LBB185_2
+; CHECK-NEXT:    bnez a3, .LBB187_2
 ; CHECK-NEXT:  # %bb.1:
 ; CHECK-NEXT:    mv a0, a2
-; CHECK-NEXT:  .LBB185_2:
+; CHECK-NEXT:  .LBB187_2:
 ; CHECK-NEXT:    ret
   %res = select i1 %cond, <4 x i8> %a, <4 x i8> %b
   ret <4 x i8> %res
diff --git a/llvm/test/CodeGen/RISCV/rvp-simd-64.ll b/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
index ff48642ba8272..fed6937428c9c 100644
--- a/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
+++ b/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
@@ -1162,6 +1162,22 @@ define i16 @test_extract_vector_16(<4 x i16> %a) {
   ret i16 %extracted
 }
 
+define i16 @test_extract_vector_16_variable(<4 x i16> %a, i32 %idx) {
+; RV32-LABEL: test_extract_vector_16_variable:
+; RV32:       # %bb.0:
+; RV32-NEXT:    slli a2, a2, 4
+; RV32-NEXT:    nsrl a0, a0, a2
+; RV32-NEXT:    ret
+;
+; RV64-LABEL: test_extract_vector_16_variable:
+; RV64:       # %bb.0:
+; RV64-NEXT:    slli.uw a1, a1, 4
+; RV64-NEXT:    srl a0, a0, a1
+; RV64-NEXT:    ret
+  %res = extractelement <4 x i16> %a, i32 %idx
+  ret i16 %res
+}
+
 define i8 @test_extract_vector_8(<8 x i8> %a) {
 ; CHECK-LABEL: test_extract_vector_8:
 ; CHECK:       # %bb.0:
@@ -1170,6 +1186,22 @@ define i8 @test_extract_vector_8(<8 x i8> %a) {
   ret i8 %extracted
 }
 
+define i8 @test_extract_vector_8_variable(<8 x i8> %a, i32 %idx) {
+; RV32-LABEL: test_extract_vector_8_variable:
+; RV32:       # %bb.0:
+; RV32-NEXT:    slli a2, a2, 3
+; RV32-NEXT:    nsrl a0, a0, a2
+; RV32-NEXT:    ret
+;
+; RV64-LABEL: test_extract_vector_8_variable:
+; RV64:       # %bb.0:
+; RV64-NEXT:    slli.uw a1, a1, 3
+; RV64-NEXT:    srl a0, a0, a1
+; RV64-NEXT:    ret
+  %res = extractelement <8 x i8> %a, i32 %idx
+  ret i8 %res
+}
+
 define i32 @test_extract_vector_32(<2 x i32> %a) {
 ; CHECK-LABEL: test_extract_vector_32:
 ; CHECK:       # %bb.0:
@@ -1192,11 +1224,26 @@ define i32 @test_extract_vector_32_elem1(<2 x i32> %a) {
   ret i32 %extracted
 }
 
+define i32 @test_extract_vector_32_variable(<2 x i32> %a, i32 %idx) {
+; RV32-LABEL: test_extract_vector_32_variable:
+; RV32:       # %bb.0:
+; RV32-NEXT:    slli a2, a2, 5
+; RV32-NEXT:    nsrl a0, a0, a2
+; RV32-NEXT:    ret
+;
+; RV64-LABEL: test_extract_vector_32_variable:
+; RV64:       # %bb.0:
+; RV64-NEXT:    slli.uw a1, a1, 5
+; RV64-NEXT:    srl a0, a0, a1
+; RV64-NEXT:    ret
+  %res = extractelement <2 x i32> %a, i32 %idx
+  ret i32 %res
+}
+
 define <4 x i16> @test_insert_vector_16(<4 x i16> %a, i16 %val) {
 ; RV32-LABEL: test_insert_vector_16:
 ; RV32:       # %bb.0:
-; RV32-NEXT:    srli a0, a0, 16
-; RV32-NEXT:    pack a0, a2, a0
+; RV32-NEXT:    ppaireo.h a0, a2, a0
 ; RV32-NEXT:    ret
 ;
 ; RV64-LABEL: test_insert_vector_16:
@@ -1212,8 +1259,7 @@ define <4 x i16> @test_insert_vector_16(<4 x i16> %a, i16 %val) {
 define <4 x i16> @test_insert_vector_16_elem2(<4 x i16> %a, i16 %val) {
 ; RV32-LABEL: test_insert_vector_16_elem2:
 ; RV32:       # %bb.0:
-; RV32-NEXT:    srli a1, a1, 16
-; RV32-NEXT:    pack a1, a2, a1
+; RV32-NEXT:    ppaireo.h a1, a2, a1
 ; RV32-NEXT:    ret
 ;
 ; RV64-LABEL: test_insert_vector_16_elem2:
@@ -1265,11 +1311,7 @@ define <8 x i8> @test_insert_vector_8_elem3(<8 x i8> %a, i8 %val) {
 define <2 x i32> @test_insert_vector_32(<2 x i32> %a, i32 %val) {
 ; RV32-LABEL: test_insert_vector_32:
 ; RV32:       # %bb.0:
-; RV32-NEXT:    addi sp, sp, -16
-; RV32-NEXT:    .cfi_def_cfa_offset 16
 ; RV32-NEXT:    mv a0, a2
-; RV32-NEXT:    addi sp, sp, 16
-; RV32-NEXT:    .cfi_def_cfa_offset 0
 ; RV32-NEXT:    ret
 ;
 ; RV64-LABEL: test_insert_vector_32:
@@ -1283,11 +1325,7 @@ define <2 x i32> @test_insert_vector_32(<2 x i32> %a, i32 %val) {
 define <2 x i32> @test_insert_vector_32_elem1(<2 x i32> %a, i32 %val) {
 ; RV32-LABEL: test_insert_vector_32_elem1:
 ; RV32:       # %bb.0:
-; RV32-NEXT:    addi sp, sp, -16
-; RV32-NEXT:    .cfi_def_cfa_offset 16
 ; RV32-NEXT:    mv a1, a2
-; RV32-NEXT:    addi sp, sp, 16
-; RV32-NEXT:    .cfi_def_cfa_offset 0
 ; RV32-NEXT:    ret
 ;
 ; RV64-LABEL: test_insert_vector_32_elem1:
@@ -1298,6 +1336,40 @@ define <2 x i32> @test_insert_vector_32_elem1(<2 x i32> %a, i32 %val) {
   ret <2 x i32> %res
 }
 
+define <2 x i32> @test_insert_vector_32_variable(<2 x i32> %a, i32 %val, i32 %idx) {
+; RV32-LABEL: test_insert_vector_32_variable:
+; RV32:       # %bb.0:
+; RV32-NEXT:    addi sp, sp, -16
+; RV32-NEXT:    .cfi_def_cfa_offset 16
+; RV32-NEXT:    andi a3, a3, 1
+; RV32-NEXT:    addi a4, sp, 8
+; RV32-NEXT:    sh2add a3, a3, a4
+; RV32-NEXT:    sw a0, 8(sp)
+; RV32-NEXT:    sw a1, 12(sp)
+; RV32-NEXT:    sw a2, 0(a3)
+; RV32-NEXT:    lw a0, 8(sp)
+; RV32-NEXT:    lw a1, 12(sp)
+; RV32-NEXT:    addi sp, sp, 16
+; RV32-NEXT:    .cfi_def_cfa_offset 0
+; RV32-NEXT:    ret
+;
+; RV64-LABEL: test_insert_vector_32_variable:
+; RV64:       # %bb.0:
+; RV64-NEXT:    addi sp, sp, -16
+; RV64-NEXT:    .cfi_def_cfa_offset 16
+; RV64-NEXT:    andi a2, a2, 1
+; RV64-NEXT:    addi a3, sp, 8
+; RV64-NEXT:    sh2add a2, a2, a3
+; RV64-NEXT:    sd a0, 8(sp)
+; RV64-NEXT:    sw a1, 0(a2)
+; RV64-NEXT:    ld a0, 8(sp)
+; RV64-NEXT:    addi sp, sp, 16
+; RV64-NEXT:    .cfi_def_cfa_offset 0
+; RV64-NEXT:    ret
+  %res = insertelement <2 x i32> %a, i32 %val, i32 %idx
+  ret <2 x i32> %res
+}
+
 ; Test basic add/sub operations for v2i32 (RV64 only)
 define <2 x i32> @test_padd_w(<2 x i32> %a, <2 x i32> %b) {
 ; RV32-LABEL: test_padd_w:
@@ -4947,12 +5019,12 @@ define <4 x i16> @test_select_v4i16(i1 %cond, <4 x i16> %a, <4 x i16> %b) {
 ; RV32-LABEL: test_select_v4i16:
 ; RV32:       # %bb.0:
 ; RV32-NEXT:    andi a5, a0, 1
-; RV32-NEXT:    bnez a5, .LBB266_2
+; RV32-NEXT:    bnez a5, .LBB270_2
 ; RV32-NEXT:  # %bb.1:
 ; RV32-NEXT:    mv a0, a3
 ; RV32-NEXT:    mv a1, a4
 ; RV32-NEXT:    ret
-; RV32-NEXT:  .LBB266_2:
+; RV32-NEXT:  .LBB270_2:
 ; RV32-NEXT:    mv a0, a1
 ; RV32-NEXT:    mv a1, a2
 ; RV32-NEXT:    ret
@@ -4961,10 +5033,10 @@ define <4 x i16> @test_select_v4i16(i1 %cond, <4 x i16> %a, <4 x i16> %b) {
 ; RV64:       # %bb.0:
 ; RV64-NEXT:    andi a3, a0, 1
 ; RV64-NEXT:    mv a0, a1
-; RV64-NEXT:    bnez a3, .LBB266_2
+; RV64-NEXT:    bnez a3, .LBB270_2
 ; RV64-NEXT:  # %bb.1:
 ; RV64-NEXT:    mv a0, a2
-; RV64-NEXT:  .LBB266_2:
+; RV64-NEXT:  .LBB270_2:
 ; RV64-NEXT:    ret
   %res = select i1 %cond, <4 x i16> %a, <4 x i16> %b
   ret <4 x i16> %res
@@ -4974,12 +5046,12 @@ define <8 x i8> @test_sel...
[truncated]

``````````

</details>


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


More information about the llvm-branch-commits mailing list