[llvm-branch-commits] [llvm] [RISCV][P-ext] Make v2i32 insert_vector_elt with constant indices legal for RV32. (PR #241035)
Craig Topper via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Nov 11 21:14:10 PST 2026
https://github.com/topperc updated https://github.com/llvm/llvm-project/pull/241035
>From 355676ba1aa27cf7c1d8763bf82a5403a42507ce 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] [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 fae5037580672..70ef715f9e601 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
More information about the llvm-branch-commits
mailing list