[llvm-branch-commits] [llvm] [RISCV][P-ext] Lower RV32 insert_subvector to extract_subector+concat_vectors. (PR #241139)

Craig Topper via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Nov 11 11:54:33 PST 2026


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

>From fa8e6e13157df6ff9120167e6a537144f58645b0 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 10 Nov 2026 14:19:32 -0800
Subject: [PATCH 1/7] [RISCV][P-ext] Lower 2 element insert_vector_elt to
 scalar_to_vec+ppaire/ppaireo.

This avoids needing to match the srl+build_vec later which we only
do on RV64.
---
 llvm/lib/Target/RISCV/RISCVISelLowering.cpp | 23 ++++++++-------------
 llvm/test/CodeGen/RISCV/rvp-simd-32.ll      |  3 +--
 llvm/test/CodeGen/RISCV/rvp-simd-64.ll      |  6 ++----
 3 files changed, 12 insertions(+), 20 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index eef8d5237af87..75d40af674125 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -11707,23 +11707,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 +11731,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);
diff --git a/llvm/test/CodeGen/RISCV/rvp-simd-32.ll b/llvm/test/CodeGen/RISCV/rvp-simd-32.ll
index fc5a17924177b..cbc545a46aa2d 100644
--- a/llvm/test/CodeGen/RISCV/rvp-simd-32.ll
+++ b/llvm/test/CodeGen/RISCV/rvp-simd-32.ll
@@ -584,8 +584,7 @@ define i8 @test_extract_vector_8_elem1(<4 x i8> %a) {
 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:
diff --git a/llvm/test/CodeGen/RISCV/rvp-simd-64.ll b/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
index ff48642ba8272..f28efd59e6124 100644
--- a/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
+++ b/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
@@ -1195,8 +1195,7 @@ define i32 @test_extract_vector_32_elem1(<2 x i32> %a) {
 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 +1211,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:

>From 4b412da1500a08c1900b7e5fd5a01e0c9bfd3587 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 10 Nov 2026 14:34:44 -0800
Subject: [PATCH 2/7] [RISCV][P-ext] Make v2i32 insert_vector_elt with constant
 indices legal for RV32.

We can select to an insert_subreg.

The default lowering created a store+load to stack that got folded,
but left behind the stack slot allocation.

Assisted-by: Claude
---
 llvm/lib/Target/RISCV/RISCVISelLowering.cpp |  6 ++
 llvm/lib/Target/RISCV/RISCVInstrInfoP.td    |  5 ++
 llvm/test/CodeGen/RISCV/rvp-simd-64.ll      | 78 ++++++++++++++-------
 3 files changed, 63 insertions(+), 26 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index 75d40af674125..90408de82dcd5 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -621,6 +621,7 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
       setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Legal);
       setOperationAction({ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT},
                          {MVT::v4i16, MVT::v8i8}, Custom);
+      setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i32, Custom);
       setOperationAction(ISD::CONCAT_VECTORS, {MVT::v4i16, MVT::v8i8}, Legal);
       setOperationAction(ISD::EXTRACT_SUBVECTOR, {MVT::v2i16, MVT::v4i8},
                          Legal);
@@ -11692,6 +11693,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);
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-simd-64.ll b/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
index f28efd59e6124..071bb15dbd570 100644
--- a/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
+++ b/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
@@ -1263,11 +1263,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:
@@ -1281,11 +1277,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:
@@ -1296,6 +1288,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:
@@ -4945,12 +4971,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, .LBB267_2
 ; RV32-NEXT:  # %bb.1:
 ; RV32-NEXT:    mv a0, a3
 ; RV32-NEXT:    mv a1, a4
 ; RV32-NEXT:    ret
-; RV32-NEXT:  .LBB266_2:
+; RV32-NEXT:  .LBB267_2:
 ; RV32-NEXT:    mv a0, a1
 ; RV32-NEXT:    mv a1, a2
 ; RV32-NEXT:    ret
@@ -4959,10 +4985,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, .LBB267_2
 ; RV64-NEXT:  # %bb.1:
 ; RV64-NEXT:    mv a0, a2
-; RV64-NEXT:  .LBB266_2:
+; RV64-NEXT:  .LBB267_2:
 ; RV64-NEXT:    ret
   %res = select i1 %cond, <4 x i16> %a, <4 x i16> %b
   ret <4 x i16> %res
@@ -4972,12 +4998,12 @@ define <8 x i8> @test_select_v8i8(i1 %cond, <8 x i8> %a, <8 x i8> %b) {
 ; RV32-LABEL: test_select_v8i8:
 ; RV32:       # %bb.0:
 ; RV32-NEXT:    andi a5, a0, 1
-; RV32-NEXT:    bnez a5, .LBB267_2
+; RV32-NEXT:    bnez a5, .LBB268_2
 ; RV32-NEXT:  # %bb.1:
 ; RV32-NEXT:    mv a0, a3
 ; RV32-NEXT:    mv a1, a4
 ; RV32-NEXT:    ret
-; RV32-NEXT:  .LBB267_2:
+; RV32-NEXT:  .LBB268_2:
 ; RV32-NEXT:    mv a0, a1
 ; RV32-NEXT:    mv a1, a2
 ; RV32-NEXT:    ret
@@ -4986,10 +5012,10 @@ define <8 x i8> @test_select_v8i8(i1 %cond, <8 x i8> %a, <8 x i8> %b) {
 ; RV64:       # %bb.0:
 ; RV64-NEXT:    andi a3, a0, 1
 ; RV64-NEXT:    mv a0, a1
-; RV64-NEXT:    bnez a3, .LBB267_2
+; RV64-NEXT:    bnez a3, .LBB268_2
 ; RV64-NEXT:  # %bb.1:
 ; RV64-NEXT:    mv a0, a2
-; RV64-NEXT:  .LBB267_2:
+; RV64-NEXT:  .LBB268_2:
 ; RV64-NEXT:    ret
   %res = select i1 %cond, <8 x i8> %a, <8 x i8> %b
   ret <8 x i8> %res
@@ -4999,12 +5025,12 @@ define <2 x i32> @test_select_v2i32(i1 %cond, <2 x i32> %a, <2 x i32> %b) {
 ; RV32-LABEL: test_select_v2i32:
 ; RV32:       # %bb.0:
 ; RV32-NEXT:    andi a5, a0, 1
-; RV32-NEXT:    bnez a5, .LBB268_2
+; RV32-NEXT:    bnez a5, .LBB269_2
 ; RV32-NEXT:  # %bb.1:
 ; RV32-NEXT:    mv a0, a3
 ; RV32-NEXT:    mv a1, a4
 ; RV32-NEXT:    ret
-; RV32-NEXT:  .LBB268_2:
+; RV32-NEXT:  .LBB269_2:
 ; RV32-NEXT:    mv a0, a1
 ; RV32-NEXT:    mv a1, a2
 ; RV32-NEXT:    ret
@@ -5013,10 +5039,10 @@ define <2 x i32> @test_select_v2i32(i1 %cond, <2 x i32> %a, <2 x i32> %b) {
 ; RV64:       # %bb.0:
 ; RV64-NEXT:    andi a3, a0, 1
 ; RV64-NEXT:    mv a0, a1
-; RV64-NEXT:    bnez a3, .LBB268_2
+; RV64-NEXT:    bnez a3, .LBB269_2
 ; RV64-NEXT:  # %bb.1:
 ; RV64-NEXT:    mv a0, a2
-; RV64-NEXT:  .LBB268_2:
+; RV64-NEXT:  .LBB269_2:
 ; RV64-NEXT:    ret
   %res = select i1 %cond, <2 x i32> %a, <2 x i32> %b
   ret <2 x i32> %res
@@ -5064,16 +5090,16 @@ define <2 x i32> @test_vselect_v2i32(<2 x i32> %a, <2 x i32> %b, <2 x i32> %c) {
 ; RV32:       # %bb.0:
 ; RV32-NEXT:    pmslt.dw a6, a2, a0
 ; RV32-NEXT:    mv a0, a4
-; RV32-NEXT:    beqz a7, .LBB271_3
+; RV32-NEXT:    beqz a7, .LBB272_3
 ; RV32-NEXT:  # %bb.1:
-; RV32-NEXT:    beqz a6, .LBB271_4
-; RV32-NEXT:  .LBB271_2:
+; RV32-NEXT:    beqz a6, .LBB272_4
+; RV32-NEXT:  .LBB272_2:
 ; RV32-NEXT:    mv a1, a5
 ; RV32-NEXT:    ret
-; RV32-NEXT:  .LBB271_3:
+; RV32-NEXT:  .LBB272_3:
 ; RV32-NEXT:    mv a5, a3
-; RV32-NEXT:    bnez a6, .LBB271_2
-; RV32-NEXT:  .LBB271_4:
+; RV32-NEXT:    bnez a6, .LBB272_2
+; RV32-NEXT:  .LBB272_4:
 ; RV32-NEXT:    mv a0, a2
 ; RV32-NEXT:    mv a1, a5
 ; RV32-NEXT:    ret

>From 54b2a5791172675264e9e1d0103d25e1f7e993b0 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 10 Nov 2026 14:57:12 -0800
Subject: [PATCH 3/7] [RISCV][P-ext] Fix crash on v2i32 extract_vector_elt with
 a variable index on RV32.

Assisted-by: Claude
---
 llvm/lib/Target/RISCV/RISCVISelLowering.cpp |  9 ++--
 llvm/test/CodeGen/RISCV/rvp-simd-64.ll      | 60 ++++++++++++++-------
 2 files changed, 48 insertions(+), 21 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index 90408de82dcd5..753419122a45b 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -618,10 +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);
-      setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i32, Custom);
+                         P64VecVTs, Custom);
       setOperationAction(ISD::CONCAT_VECTORS, {MVT::v4i16, MVT::v8i8}, Legal);
       setOperationAction(ISD::EXTRACT_SUBVECTOR, {MVT::v2i16, MVT::v4i8},
                          Legal);
@@ -11980,6 +11978,11 @@ SDValue RISCVTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
         VecVT != MVT::v4i8 && VecVT != MVT::v2i32)
       return SDValue();
 
+    // v2i32 on RV32 lives in a GPR pair; constant index extracts are selected
+    // with EXTRACT_SUBREG.
+    if (!Subtarget.is64Bit() && VecVT == MVT::v2i32)
+      return isa<ConstantSDNode>(Idx) ? Op : SDValue();
+
     if (!Subtarget.is64Bit() && (VecVT == MVT::v4i16 || VecVT == MVT::v8i8)) {
       auto *IdxC = dyn_cast<ConstantSDNode>(Idx);
       if (!IdxC)
diff --git a/llvm/test/CodeGen/RISCV/rvp-simd-64.ll b/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
index 071bb15dbd570..7848ead1f8fcf 100644
--- a/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
+++ b/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
@@ -1192,6 +1192,30 @@ 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:    addi sp, sp, -16
+; RV32-NEXT:    .cfi_def_cfa_offset 16
+; RV32-NEXT:    andi a2, a2, 1
+; RV32-NEXT:    addi a3, sp, 8
+; RV32-NEXT:    sh2add a2, a2, a3
+; RV32-NEXT:    sw a0, 8(sp)
+; RV32-NEXT:    sw a1, 12(sp)
+; RV32-NEXT:    lw a0, 0(a2)
+; RV32-NEXT:    addi sp, sp, 16
+; RV32-NEXT:    .cfi_def_cfa_offset 0
+; 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:
@@ -4971,12 +4995,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, .LBB267_2
+; RV32-NEXT:    bnez a5, .LBB268_2
 ; RV32-NEXT:  # %bb.1:
 ; RV32-NEXT:    mv a0, a3
 ; RV32-NEXT:    mv a1, a4
 ; RV32-NEXT:    ret
-; RV32-NEXT:  .LBB267_2:
+; RV32-NEXT:  .LBB268_2:
 ; RV32-NEXT:    mv a0, a1
 ; RV32-NEXT:    mv a1, a2
 ; RV32-NEXT:    ret
@@ -4985,10 +5009,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, .LBB267_2
+; RV64-NEXT:    bnez a3, .LBB268_2
 ; RV64-NEXT:  # %bb.1:
 ; RV64-NEXT:    mv a0, a2
-; RV64-NEXT:  .LBB267_2:
+; RV64-NEXT:  .LBB268_2:
 ; RV64-NEXT:    ret
   %res = select i1 %cond, <4 x i16> %a, <4 x i16> %b
   ret <4 x i16> %res
@@ -4998,12 +5022,12 @@ define <8 x i8> @test_select_v8i8(i1 %cond, <8 x i8> %a, <8 x i8> %b) {
 ; RV32-LABEL: test_select_v8i8:
 ; RV32:       # %bb.0:
 ; RV32-NEXT:    andi a5, a0, 1
-; RV32-NEXT:    bnez a5, .LBB268_2
+; RV32-NEXT:    bnez a5, .LBB269_2
 ; RV32-NEXT:  # %bb.1:
 ; RV32-NEXT:    mv a0, a3
 ; RV32-NEXT:    mv a1, a4
 ; RV32-NEXT:    ret
-; RV32-NEXT:  .LBB268_2:
+; RV32-NEXT:  .LBB269_2:
 ; RV32-NEXT:    mv a0, a1
 ; RV32-NEXT:    mv a1, a2
 ; RV32-NEXT:    ret
@@ -5012,10 +5036,10 @@ define <8 x i8> @test_select_v8i8(i1 %cond, <8 x i8> %a, <8 x i8> %b) {
 ; RV64:       # %bb.0:
 ; RV64-NEXT:    andi a3, a0, 1
 ; RV64-NEXT:    mv a0, a1
-; RV64-NEXT:    bnez a3, .LBB268_2
+; RV64-NEXT:    bnez a3, .LBB269_2
 ; RV64-NEXT:  # %bb.1:
 ; RV64-NEXT:    mv a0, a2
-; RV64-NEXT:  .LBB268_2:
+; RV64-NEXT:  .LBB269_2:
 ; RV64-NEXT:    ret
   %res = select i1 %cond, <8 x i8> %a, <8 x i8> %b
   ret <8 x i8> %res
@@ -5025,12 +5049,12 @@ define <2 x i32> @test_select_v2i32(i1 %cond, <2 x i32> %a, <2 x i32> %b) {
 ; RV32-LABEL: test_select_v2i32:
 ; RV32:       # %bb.0:
 ; RV32-NEXT:    andi a5, a0, 1
-; RV32-NEXT:    bnez a5, .LBB269_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:  .LBB269_2:
+; RV32-NEXT:  .LBB270_2:
 ; RV32-NEXT:    mv a0, a1
 ; RV32-NEXT:    mv a1, a2
 ; RV32-NEXT:    ret
@@ -5039,10 +5063,10 @@ define <2 x i32> @test_select_v2i32(i1 %cond, <2 x i32> %a, <2 x i32> %b) {
 ; RV64:       # %bb.0:
 ; RV64-NEXT:    andi a3, a0, 1
 ; RV64-NEXT:    mv a0, a1
-; RV64-NEXT:    bnez a3, .LBB269_2
+; RV64-NEXT:    bnez a3, .LBB270_2
 ; RV64-NEXT:  # %bb.1:
 ; RV64-NEXT:    mv a0, a2
-; RV64-NEXT:  .LBB269_2:
+; RV64-NEXT:  .LBB270_2:
 ; RV64-NEXT:    ret
   %res = select i1 %cond, <2 x i32> %a, <2 x i32> %b
   ret <2 x i32> %res
@@ -5090,16 +5114,16 @@ define <2 x i32> @test_vselect_v2i32(<2 x i32> %a, <2 x i32> %b, <2 x i32> %c) {
 ; RV32:       # %bb.0:
 ; RV32-NEXT:    pmslt.dw a6, a2, a0
 ; RV32-NEXT:    mv a0, a4
-; RV32-NEXT:    beqz a7, .LBB272_3
+; RV32-NEXT:    beqz a7, .LBB273_3
 ; RV32-NEXT:  # %bb.1:
-; RV32-NEXT:    beqz a6, .LBB272_4
-; RV32-NEXT:  .LBB272_2:
+; RV32-NEXT:    beqz a6, .LBB273_4
+; RV32-NEXT:  .LBB273_2:
 ; RV32-NEXT:    mv a1, a5
 ; RV32-NEXT:    ret
-; RV32-NEXT:  .LBB272_3:
+; RV32-NEXT:  .LBB273_3:
 ; RV32-NEXT:    mv a5, a3
-; RV32-NEXT:    bnez a6, .LBB272_2
-; RV32-NEXT:  .LBB272_4:
+; RV32-NEXT:    bnez a6, .LBB273_2
+; RV32-NEXT:  .LBB273_4:
 ; RV32-NEXT:    mv a0, a2
 ; RV32-NEXT:    mv a1, a5
 ; RV32-NEXT:    ret

>From 77cf68fa3cbc1cfb1804c1dfa830ad1ab4cddddd Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 10 Nov 2026 15:03:06 -0800
Subject: [PATCH 4/7] [RISCV][P-ext] Add tests for extractelement with a
 variable index. NFC

Assisted-by: Claude
---
 llvm/test/CodeGen/RISCV/rvp-simd-32.ll | 40 ++++++++++--
 llvm/test/CodeGen/RISCV/rvp-simd-64.ll | 84 ++++++++++++++++++++------
 2 files changed, 102 insertions(+), 22 deletions(-)

diff --git a/llvm/test/CodeGen/RISCV/rvp-simd-32.ll b/llvm/test/CodeGen/RISCV/rvp-simd-32.ll
index cbc545a46aa2d..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,6 +597,22 @@ 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:
@@ -2607,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
@@ -2621,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 7848ead1f8fcf..f83e0d2a6fd1c 100644
--- a/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
+++ b/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
@@ -1162,6 +1162,30 @@ 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:    addi sp, sp, -16
+; RV32-NEXT:    .cfi_def_cfa_offset 16
+; RV32-NEXT:    andi a2, a2, 3
+; RV32-NEXT:    addi a3, sp, 8
+; RV32-NEXT:    sh1add a2, a2, a3
+; RV32-NEXT:    sw a0, 8(sp)
+; RV32-NEXT:    sw a1, 12(sp)
+; RV32-NEXT:    lh a0, 0(a2)
+; RV32-NEXT:    addi sp, sp, 16
+; RV32-NEXT:    .cfi_def_cfa_offset 0
+; 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 +1194,30 @@ 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:    addi sp, sp, -16
+; RV32-NEXT:    .cfi_def_cfa_offset 16
+; RV32-NEXT:    andi a2, a2, 7
+; RV32-NEXT:    addi a3, sp, 8
+; RV32-NEXT:    or a2, a3, a2
+; RV32-NEXT:    sw a0, 8(sp)
+; RV32-NEXT:    sw a1, 12(sp)
+; RV32-NEXT:    lbu a0, 0(a2)
+; RV32-NEXT:    addi sp, sp, 16
+; RV32-NEXT:    .cfi_def_cfa_offset 0
+; 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:
@@ -4995,12 +5043,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, .LBB268_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:  .LBB268_2:
+; RV32-NEXT:  .LBB270_2:
 ; RV32-NEXT:    mv a0, a1
 ; RV32-NEXT:    mv a1, a2
 ; RV32-NEXT:    ret
@@ -5009,10 +5057,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, .LBB268_2
+; RV64-NEXT:    bnez a3, .LBB270_2
 ; RV64-NEXT:  # %bb.1:
 ; RV64-NEXT:    mv a0, a2
-; RV64-NEXT:  .LBB268_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
@@ -5022,12 +5070,12 @@ define <8 x i8> @test_select_v8i8(i1 %cond, <8 x i8> %a, <8 x i8> %b) {
 ; RV32-LABEL: test_select_v8i8:
 ; RV32:       # %bb.0:
 ; RV32-NEXT:    andi a5, a0, 1
-; RV32-NEXT:    bnez a5, .LBB269_2
+; RV32-NEXT:    bnez a5, .LBB271_2
 ; RV32-NEXT:  # %bb.1:
 ; RV32-NEXT:    mv a0, a3
 ; RV32-NEXT:    mv a1, a4
 ; RV32-NEXT:    ret
-; RV32-NEXT:  .LBB269_2:
+; RV32-NEXT:  .LBB271_2:
 ; RV32-NEXT:    mv a0, a1
 ; RV32-NEXT:    mv a1, a2
 ; RV32-NEXT:    ret
@@ -5036,10 +5084,10 @@ define <8 x i8> @test_select_v8i8(i1 %cond, <8 x i8> %a, <8 x i8> %b) {
 ; RV64:       # %bb.0:
 ; RV64-NEXT:    andi a3, a0, 1
 ; RV64-NEXT:    mv a0, a1
-; RV64-NEXT:    bnez a3, .LBB269_2
+; RV64-NEXT:    bnez a3, .LBB271_2
 ; RV64-NEXT:  # %bb.1:
 ; RV64-NEXT:    mv a0, a2
-; RV64-NEXT:  .LBB269_2:
+; RV64-NEXT:  .LBB271_2:
 ; RV64-NEXT:    ret
   %res = select i1 %cond, <8 x i8> %a, <8 x i8> %b
   ret <8 x i8> %res
@@ -5049,12 +5097,12 @@ define <2 x i32> @test_select_v2i32(i1 %cond, <2 x i32> %a, <2 x i32> %b) {
 ; RV32-LABEL: test_select_v2i32:
 ; RV32:       # %bb.0:
 ; RV32-NEXT:    andi a5, a0, 1
-; RV32-NEXT:    bnez a5, .LBB270_2
+; RV32-NEXT:    bnez a5, .LBB272_2
 ; RV32-NEXT:  # %bb.1:
 ; RV32-NEXT:    mv a0, a3
 ; RV32-NEXT:    mv a1, a4
 ; RV32-NEXT:    ret
-; RV32-NEXT:  .LBB270_2:
+; RV32-NEXT:  .LBB272_2:
 ; RV32-NEXT:    mv a0, a1
 ; RV32-NEXT:    mv a1, a2
 ; RV32-NEXT:    ret
@@ -5063,10 +5111,10 @@ define <2 x i32> @test_select_v2i32(i1 %cond, <2 x i32> %a, <2 x i32> %b) {
 ; RV64:       # %bb.0:
 ; RV64-NEXT:    andi a3, a0, 1
 ; RV64-NEXT:    mv a0, a1
-; RV64-NEXT:    bnez a3, .LBB270_2
+; RV64-NEXT:    bnez a3, .LBB272_2
 ; RV64-NEXT:  # %bb.1:
 ; RV64-NEXT:    mv a0, a2
-; RV64-NEXT:  .LBB270_2:
+; RV64-NEXT:  .LBB272_2:
 ; RV64-NEXT:    ret
   %res = select i1 %cond, <2 x i32> %a, <2 x i32> %b
   ret <2 x i32> %res
@@ -5114,16 +5162,16 @@ define <2 x i32> @test_vselect_v2i32(<2 x i32> %a, <2 x i32> %b, <2 x i32> %c) {
 ; RV32:       # %bb.0:
 ; RV32-NEXT:    pmslt.dw a6, a2, a0
 ; RV32-NEXT:    mv a0, a4
-; RV32-NEXT:    beqz a7, .LBB273_3
+; RV32-NEXT:    beqz a7, .LBB275_3
 ; RV32-NEXT:  # %bb.1:
-; RV32-NEXT:    beqz a6, .LBB273_4
-; RV32-NEXT:  .LBB273_2:
+; RV32-NEXT:    beqz a6, .LBB275_4
+; RV32-NEXT:  .LBB275_2:
 ; RV32-NEXT:    mv a1, a5
 ; RV32-NEXT:    ret
-; RV32-NEXT:  .LBB273_3:
+; RV32-NEXT:  .LBB275_3:
 ; RV32-NEXT:    mv a5, a3
-; RV32-NEXT:    bnez a6, .LBB273_2
-; RV32-NEXT:  .LBB273_4:
+; RV32-NEXT:    bnez a6, .LBB275_2
+; RV32-NEXT:  .LBB275_4:
 ; RV32-NEXT:    mv a0, a2
 ; RV32-NEXT:    mv a1, a5
 ; RV32-NEXT:    ret

>From 14605569dbb9ab428591a09363d92a24538a6ad1 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 10 Nov 2026 15:56:10 -0800
Subject: [PATCH 5/7] [RISCV][P-ext] Use nsrl to for 64-bit extract_vector_elt
 with variable index on RV32.

---
 llvm/lib/Target/RISCV/RISCVISelLowering.cpp | 27 ++++++++++++----
 llvm/test/CodeGen/RISCV/rvp-simd-64.ll      | 36 ++++-----------------
 2 files changed, 27 insertions(+), 36 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index 753419122a45b..b79f6594baaed 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -11980,13 +11980,28 @@ SDValue RISCVTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
 
     // v2i32 on RV32 lives in a GPR pair; constant index extracts are selected
     // with EXTRACT_SUBREG.
-    if (!Subtarget.is64Bit() && VecVT == MVT::v2i32)
-      return isa<ConstantSDNode>(Idx) ? Op : SDValue();
-
-    if (!Subtarget.is64Bit() && (VecVT == MVT::v4i16 || VecVT == MVT::v8i8)) {
+    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);
diff --git a/llvm/test/CodeGen/RISCV/rvp-simd-64.ll b/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
index f83e0d2a6fd1c..fed6937428c9c 100644
--- a/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
+++ b/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
@@ -1165,16 +1165,8 @@ define i16 @test_extract_vector_16(<4 x i16> %a) {
 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:    addi sp, sp, -16
-; RV32-NEXT:    .cfi_def_cfa_offset 16
-; RV32-NEXT:    andi a2, a2, 3
-; RV32-NEXT:    addi a3, sp, 8
-; RV32-NEXT:    sh1add a2, a2, a3
-; RV32-NEXT:    sw a0, 8(sp)
-; RV32-NEXT:    sw a1, 12(sp)
-; RV32-NEXT:    lh a0, 0(a2)
-; RV32-NEXT:    addi sp, sp, 16
-; RV32-NEXT:    .cfi_def_cfa_offset 0
+; RV32-NEXT:    slli a2, a2, 4
+; RV32-NEXT:    nsrl a0, a0, a2
 ; RV32-NEXT:    ret
 ;
 ; RV64-LABEL: test_extract_vector_16_variable:
@@ -1197,16 +1189,8 @@ define i8 @test_extract_vector_8(<8 x i8> %a) {
 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:    addi sp, sp, -16
-; RV32-NEXT:    .cfi_def_cfa_offset 16
-; RV32-NEXT:    andi a2, a2, 7
-; RV32-NEXT:    addi a3, sp, 8
-; RV32-NEXT:    or a2, a3, a2
-; RV32-NEXT:    sw a0, 8(sp)
-; RV32-NEXT:    sw a1, 12(sp)
-; RV32-NEXT:    lbu a0, 0(a2)
-; RV32-NEXT:    addi sp, sp, 16
-; RV32-NEXT:    .cfi_def_cfa_offset 0
+; RV32-NEXT:    slli a2, a2, 3
+; RV32-NEXT:    nsrl a0, a0, a2
 ; RV32-NEXT:    ret
 ;
 ; RV64-LABEL: test_extract_vector_8_variable:
@@ -1243,16 +1227,8 @@ define i32 @test_extract_vector_32_elem1(<2 x i32> %a) {
 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:    addi sp, sp, -16
-; RV32-NEXT:    .cfi_def_cfa_offset 16
-; RV32-NEXT:    andi a2, a2, 1
-; RV32-NEXT:    addi a3, sp, 8
-; RV32-NEXT:    sh2add a2, a2, a3
-; RV32-NEXT:    sw a0, 8(sp)
-; RV32-NEXT:    sw a1, 12(sp)
-; RV32-NEXT:    lw a0, 0(a2)
-; RV32-NEXT:    addi sp, sp, 16
-; RV32-NEXT:    .cfi_def_cfa_offset 0
+; RV32-NEXT:    slli a2, a2, 5
+; RV32-NEXT:    nsrl a0, a0, a2
 ; RV32-NEXT:    ret
 ;
 ; RV64-LABEL: test_extract_vector_32_variable:

>From 05085ae6f7250b899c4df81e158889306eac0f25 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 10 Nov 2026 21:43:50 -0800
Subject: [PATCH 6/7] Pre-commit tests

---
 .../CodeGen/RISCV/rvp-insert-subvector.ll     | 154 ++++++++++++++++++
 1 file changed, 154 insertions(+)

diff --git a/llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll b/llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll
index 6ba26ca595835..de3474740a275 100644
--- a/llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll
+++ b/llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll
@@ -43,3 +43,157 @@ 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:    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: 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:    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: 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:    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: 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:    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: 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:    addi sp, sp, -16
+; RV32-NEXT:    .cfi_def_cfa_offset 16
+; RV32-NEXT:    lw a1, 8(sp)
+; RV32-NEXT:    mv a2, a0
+; RV32-NEXT:    mv a0, a1
+; RV32-NEXT:    mv a1, a2
+; RV32-NEXT:    addi sp, sp, 16
+; RV32-NEXT:    .cfi_def_cfa_offset 0
+; 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:    addi sp, sp, -16
+; RV32-NEXT:    .cfi_def_cfa_offset 16
+; RV32-NEXT:    mv a1, a0
+; RV32-NEXT:    li a0, 0
+; RV32-NEXT:    addi sp, sp, 16
+; RV32-NEXT:    .cfi_def_cfa_offset 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
+}

>From ed1e1dc49c3dbed0e07e4a805ff38dc2794d6e1b Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 10 Nov 2026 21:46:42 -0800
Subject: [PATCH 7/7] [RISCV][P-ext] Lower RV32 insert_subvector to
 extract_subector+concat_vectors.

Assisted-by: Claude
---
 llvm/lib/Target/RISCV/RISCVISelLowering.cpp   | 24 +++++++--------
 .../CodeGen/RISCV/rvp-insert-subvector.ll     | 29 +------------------
 2 files changed, 12 insertions(+), 41 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index b79f6594baaed..01d9b55ea105e 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -14762,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/test/CodeGen/RISCV/rvp-insert-subvector.ll b/llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll
index de3474740a275..88368d8496ce3 100644
--- a/llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll
+++ b/llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll
@@ -47,11 +47,7 @@ define <2 x i32> @insert_zero_v1i32(<1 x i32> %v) {
 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:    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: insert_lo_v4i8:
@@ -78,11 +74,7 @@ define <8 x i8> @insert_lo_v4i8(<8 x i8> %v, <4 x i8> %s) {
 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:    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: insert_hi_v4i8:
@@ -111,11 +103,7 @@ define <8 x i8> @insert_hi_v4i8(<8 x i8> %v, <4 x i8> %s) {
 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:    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: insert_lo_v2i16:
@@ -136,11 +124,7 @@ define <4 x i16> @insert_lo_v2i16(<4 x i16> %v, <2 x i16> %s) {
 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:    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: insert_hi_v2i16:
@@ -161,14 +145,7 @@ define <4 x i16> @insert_hi_v2i16(<4 x i16> %v, <2 x i16> %s) {
 define <8 x i8> @insert_hi_undef_v4i8(<4 x i8> %s) {
 ; RV32-LABEL: insert_hi_undef_v4i8:
 ; RV32:       # %bb.0:
-; RV32-NEXT:    addi sp, sp, -16
-; RV32-NEXT:    .cfi_def_cfa_offset 16
-; RV32-NEXT:    lw a1, 8(sp)
-; RV32-NEXT:    mv a2, a0
-; RV32-NEXT:    mv a0, a1
-; RV32-NEXT:    mv a1, a2
-; RV32-NEXT:    addi sp, sp, 16
-; RV32-NEXT:    .cfi_def_cfa_offset 0
+; RV32-NEXT:    mv a1, a0
 ; RV32-NEXT:    ret
 ;
 ; RV64-LABEL: insert_hi_undef_v4i8:
@@ -182,12 +159,8 @@ define <8 x i8> @insert_hi_undef_v4i8(<4 x i8> %s) {
 define <4 x i16> @insert_hi_zero_v2i16(<2 x i16> %s) {
 ; RV32-LABEL: insert_hi_zero_v2i16:
 ; RV32:       # %bb.0:
-; RV32-NEXT:    addi sp, sp, -16
-; RV32-NEXT:    .cfi_def_cfa_offset 16
 ; RV32-NEXT:    mv a1, a0
 ; RV32-NEXT:    li a0, 0
-; RV32-NEXT:    addi sp, sp, 16
-; RV32-NEXT:    .cfi_def_cfa_offset 0
 ; RV32-NEXT:    ret
 ;
 ; RV64-LABEL: insert_hi_zero_v2i16:



More information about the llvm-branch-commits mailing list