[llvm-branch-commits] [llvm] [LoongArch] Use signed vector extract for sign extension (PR #241719)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Nov 11 23:09:57 PST 2026
https://github.com/heiher created https://github.com/llvm/llvm-project/pull/241719
`[x]vpickve2gr.{b/h}` already sign-extends the extracted element to GRLen, so a following `ext.w.{b/h}` is redundant:
```llvm
%e = extractelement <16 x i8> %v, i64 7
%r = sext i8 %e to i32
```
```asm
vpickve2gr.b $a0, $vr0, 7
ext.w.b $a0, $a0
```
Add patterns that fold sext_inreg of a constant-index i8/i16 vector extraction into VPICKVE2GR_B/H, mirroring the existing zero-extension patterns for VPICKVE2GR_BU/HU. The i32 case already needs no extra instruction.
Assisted-By: LLM
>From 25b6831083896b3c35dc8ab3f74ffb442124ea8a Mon Sep 17 00:00:00 2001
From: WANG Rui <wangrui at loongson.cn>
Date: Thu, 12 Nov 2026 15:06:34 +0800
Subject: [PATCH] [LoongArch] Use signed vector extract for sign extension
`[x]vpickve2gr.{b/h}` already sign-extends the extracted element to
GRLen, so a following `ext.w.{b/h}` is redundant:
```llvm
%e = extractelement <16 x i8> %v, i64 7
%r = sext i8 %e to i32
```
```asm
vpickve2gr.b $a0, $vr0, 7
ext.w.b $a0, $a0
```
Add patterns that fold sext_inreg of a constant-index i8/i16 vector
extraction into VPICKVE2GR_B/H, mirroring the existing zero-extension
patterns for VPICKVE2GR_BU/HU. The i32 case already needs no extra
instruction.
Assisted-By: LLM
---
.../Target/LoongArch/LoongArchLASXInstrInfo.td | 18 ++++++++++++++++++
.../Target/LoongArch/LoongArchLSXInstrInfo.td | 6 ++++++
.../lasx/ir-instruction/extractelement.ll | 4 ----
.../lsx/ir-instruction/extractelement.ll | 2 --
4 files changed, 24 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Target/LoongArch/LoongArchLASXInstrInfo.td b/llvm/lib/Target/LoongArch/LoongArchLASXInstrInfo.td
index 69e8de23cc9c9..5985e353dfa64 100644
--- a/llvm/lib/Target/LoongArch/LoongArchLASXInstrInfo.td
+++ b/llvm/lib/Target/LoongArch/LoongArchLASXInstrInfo.td
@@ -2800,6 +2800,24 @@ def : Pat<(GRLenVT (loongarch_bstrpick (vector_extract v8i32:$xj, uimm3:$imm),
(GRLenVT 31), (GRLenVT 0))),
(XVPICKVE2GR_WU v8i32:$xj, uimm3:$imm)>;
+// Vector extraction with sign extension and constant index.
+foreach imm = 16...31 in {
+ defvar Imm = !and(imm, 15);
+ def : Pat<(GRLenVT (sext_inreg (vector_extract v32i8:$xj, imm), i8)),
+ (VPICKVE2GR_B (EXTRACT_SUBREG (XVPERMI_D v32i8:$xj, 14), sub_128),
+ Imm)>;
+}
+foreach imm = 8...15 in {
+ defvar Imm = !and(imm, 7);
+ def : Pat<(GRLenVT (sext_inreg (vector_extract v16i16:$xj, imm), i16)),
+ (VPICKVE2GR_H (EXTRACT_SUBREG (XVPERMI_D v16i16:$xj, 14), sub_128),
+ Imm)>;
+}
+def : Pat<(GRLenVT (sext_inreg (vector_extract v32i8:$xj, uimm4:$imm), i8)),
+ (VPICKVE2GR_B (EXTRACT_SUBREG v32i8:$xj, sub_128), uimm4:$imm)>;
+def : Pat<(GRLenVT (sext_inreg (vector_extract v16i16:$xj, uimm3:$imm), i16)),
+ (VPICKVE2GR_H (EXTRACT_SUBREG v16i16:$xj, sub_128), uimm3:$imm)>;
+
// Vector extraction with constant index.
foreach imm = 16...31 in {
defvar Imm = !and(imm, 15);
diff --git a/llvm/lib/Target/LoongArch/LoongArchLSXInstrInfo.td b/llvm/lib/Target/LoongArch/LoongArchLSXInstrInfo.td
index 25ead5d1deb30..2078d13ca7434 100644
--- a/llvm/lib/Target/LoongArch/LoongArchLSXInstrInfo.td
+++ b/llvm/lib/Target/LoongArch/LoongArchLSXInstrInfo.td
@@ -2977,6 +2977,12 @@ def : Pat<(GRLenVT (loongarch_bstrpick (vector_extract v4i32:$vj, uimm2:$imm),
(GRLenVT 31), (GRLenVT 0))),
(VPICKVE2GR_WU v4i32:$vj, uimm2:$imm)>;
+// Vector extraction with sign extension and constant index.
+def : Pat<(GRLenVT (sext_inreg (vector_extract v16i8:$vj, uimm4:$imm), i8)),
+ (VPICKVE2GR_B v16i8:$vj, uimm4:$imm)>;
+def : Pat<(GRLenVT (sext_inreg (vector_extract v8i16:$vj, uimm3:$imm), i16)),
+ (VPICKVE2GR_H v8i16:$vj, uimm3:$imm)>;
+
// Vector extraction with constant index.
def : Pat<(GRLenVT (vector_extract v16i8:$vj, uimm4:$imm)),
(VPICKVE2GR_B v16i8:$vj, uimm4:$imm)>;
diff --git a/llvm/test/CodeGen/LoongArch/lasx/ir-instruction/extractelement.ll b/llvm/test/CodeGen/LoongArch/lasx/ir-instruction/extractelement.ll
index cab05c8b1aa36..6d0473976ef91 100644
--- a/llvm/test/CodeGen/LoongArch/lasx/ir-instruction/extractelement.ll
+++ b/llvm/test/CodeGen/LoongArch/lasx/ir-instruction/extractelement.ll
@@ -320,7 +320,6 @@ define void @vextract_32xi8_sext(ptr %src, ptr %dst) nounwind {
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: xvld $xr0, $a0, 0
; CHECK-NEXT: vpickve2gr.b $a0, $vr0, 1
-; CHECK-NEXT: ext.w.b $a0, $a0
; CHECK-NEXT: st.w $a0, $a1, 0
; CHECK-NEXT: ret
entry:
@@ -337,7 +336,6 @@ define void @vextract_32xi8_sext_hi(ptr %src, ptr %dst) nounwind {
; CHECK-NEXT: xvld $xr0, $a0, 0
; CHECK-NEXT: xvpermi.d $xr0, $xr0, 14
; CHECK-NEXT: vpickve2gr.b $a0, $vr0, 1
-; CHECK-NEXT: ext.w.b $a0, $a0
; CHECK-NEXT: st.w $a0, $a1, 0
; CHECK-NEXT: ret
entry:
@@ -353,7 +351,6 @@ define void @vextract_16xi16_sext(ptr %src, ptr %dst) nounwind {
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: xvld $xr0, $a0, 0
; CHECK-NEXT: vpickve2gr.h $a0, $vr0, 1
-; CHECK-NEXT: ext.w.h $a0, $a0
; CHECK-NEXT: st.w $a0, $a1, 0
; CHECK-NEXT: ret
entry:
@@ -370,7 +367,6 @@ define void @vextract_16xi16_sext_hi(ptr %src, ptr %dst) nounwind {
; CHECK-NEXT: xvld $xr0, $a0, 0
; CHECK-NEXT: xvpermi.d $xr0, $xr0, 14
; CHECK-NEXT: vpickve2gr.h $a0, $vr0, 1
-; CHECK-NEXT: ext.w.h $a0, $a0
; CHECK-NEXT: st.w $a0, $a1, 0
; CHECK-NEXT: ret
entry:
diff --git a/llvm/test/CodeGen/LoongArch/lsx/ir-instruction/extractelement.ll b/llvm/test/CodeGen/LoongArch/lsx/ir-instruction/extractelement.ll
index 944e26918f23b..8f2c2d0e73575 100644
--- a/llvm/test/CodeGen/LoongArch/lsx/ir-instruction/extractelement.ll
+++ b/llvm/test/CodeGen/LoongArch/lsx/ir-instruction/extractelement.ll
@@ -281,7 +281,6 @@ define void @extract_16xi8_sext(ptr %src, ptr %dst) nounwind {
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: vld $vr0, $a0, 0
; CHECK-NEXT: vpickve2gr.b $a0, $vr0, 1
-; CHECK-NEXT: ext.w.b $a0, $a0
; CHECK-NEXT: st.w $a0, $a1, 0
; CHECK-NEXT: ret
entry:
@@ -297,7 +296,6 @@ define void @extract_8xi16_sext(ptr %src, ptr %dst) nounwind {
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: vld $vr0, $a0, 0
; CHECK-NEXT: vpickve2gr.h $a0, $vr0, 1
-; CHECK-NEXT: ext.w.h $a0, $a0
; CHECK-NEXT: st.w $a0, $a1, 0
; CHECK-NEXT: ret
entry:
More information about the llvm-branch-commits
mailing list