[llvm] [LoongArch] Move widenShuffleMask before lowerVECTOR_SHUFFLE_XVPERMI to improve code quality (PR #164219)

Zhaoxin Yang via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 22 03:15:11 PDT 2025


https://github.com/ylzsx updated https://github.com/llvm/llvm-project/pull/164219

>From b190975b6266454a46560b37b54b4521c469d816 Mon Sep 17 00:00:00 2001
From: yangzhaoxin <yangzhaoxin at loongson.cn>
Date: Mon, 20 Oct 2025 16:01:28 +0800
Subject: [PATCH 1/3] [LoongArch] Always try to widen vectors at the beginning
 of lowerVECTOR_SHUFFLE_XVPERMI

---
 llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp  |  6 ++++++
 .../LoongArch/lasx/shuffle-as-permute-and-shuffle.ll | 12 +++---------
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
index f7deeafc9ccfc..21e8179a304ef 100644
--- a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
@@ -2103,6 +2103,12 @@ static SDValue
 lowerVECTOR_SHUFFLE_XVPERMI(const SDLoc &DL, ArrayRef<int> Mask, MVT VT,
                             SDValue V1, SelectionDAG &DAG,
                             const LoongArchSubtarget &Subtarget) {
+  // Before selecting XVPERMI_D, always try to widen vectors to gain more
+  // optimization opportunities.
+  if (SDValue NewShuffle = widenShuffleMask(
+          DL, Mask, VT, V1, DAG.getUNDEF(V1.getValueType()), DAG))
+    return NewShuffle;
+
   // Only consider XVPERMI_D.
   if (Mask.size() != 4 || (VT != MVT::v4i64 && VT != MVT::v4f64))
     return SDValue();
diff --git a/llvm/test/CodeGen/LoongArch/lasx/shuffle-as-permute-and-shuffle.ll b/llvm/test/CodeGen/LoongArch/lasx/shuffle-as-permute-and-shuffle.ll
index 245f76472b844..7149cdb99063a 100644
--- a/llvm/test/CodeGen/LoongArch/lasx/shuffle-as-permute-and-shuffle.ll
+++ b/llvm/test/CodeGen/LoongArch/lasx/shuffle-as-permute-and-shuffle.ll
@@ -32,9 +32,7 @@ define <16 x i16> @shuffle_v16i16(<16 x i16> %a) {
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    pcalau12i $a0, %pc_hi20(.LCPI2_0)
 ; CHECK-NEXT:    xvld $xr1, $a0, %pc_lo12(.LCPI2_0)
-; CHECK-NEXT:    xvpermi.d $xr2, $xr0, 78
-; CHECK-NEXT:    xvshuf.w $xr1, $xr2, $xr0
-; CHECK-NEXT:    xvori.b $xr0, $xr1, 0
+; CHECK-NEXT:    xvperm.w $xr0, $xr0, $xr1
 ; CHECK-NEXT:    ret
   %shuffle = shufflevector <16 x i16> %a, <16 x i16> poison, <16 x i32> <i32 8, i32 9, i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
   ret <16 x i16> %shuffle
@@ -55,9 +53,7 @@ define <16 x i16> @shuffle_v16i16_same_lane(<16 x i16> %a) {
 define <8 x i32> @shuffle_v8i32(<8 x i32> %a) {
 ; CHECK-LABEL: shuffle_v8i32:
 ; CHECK:       # %bb.0:
-; CHECK-NEXT:    pcalau12i $a0, %pc_hi20(.LCPI4_0)
-; CHECK-NEXT:    xvld $xr1, $a0, %pc_lo12(.LCPI4_0)
-; CHECK-NEXT:    xvperm.w $xr0, $xr0, $xr1
+; CHECK-NEXT:    xvpermi.d $xr0, $xr0, 226
 ; CHECK-NEXT:    ret
   %shuffle = shufflevector <8 x i32> %a, <8 x i32> poison, <8 x i32> <i32 4, i32 5, i32 0, i32 1, i32 4, i32 5, i32 6, i32 7>
   ret <8 x i32> %shuffle
@@ -93,9 +89,7 @@ define <4 x i64> @shuffle_v4i64_same_lane(<4 x i64> %a) {
 define <8 x float> @shuffle_v8f32(<8 x float> %a) {
 ; CHECK-LABEL: shuffle_v8f32:
 ; CHECK:       # %bb.0:
-; CHECK-NEXT:    pcalau12i $a0, %pc_hi20(.LCPI8_0)
-; CHECK-NEXT:    xvld $xr1, $a0, %pc_lo12(.LCPI8_0)
-; CHECK-NEXT:    xvperm.w $xr0, $xr0, $xr1
+; CHECK-NEXT:    xvpermi.d $xr0, $xr0, 226
 ; CHECK-NEXT:    ret
   %shuffle = shufflevector <8 x float> %a, <8 x float> poison, <8 x i32> <i32 4, i32 5, i32 0, i32 1, i32 4, i32 5, i32 6, i32 7>
   ret <8 x float> %shuffle

>From bad130fafc68d0a47fec6f323f5f8d03dce31c2c Mon Sep 17 00:00:00 2001
From: yangzhaoxin <yangzhaoxin at loongson.cn>
Date: Tue, 21 Oct 2025 11:35:22 +0800
Subject: [PATCH 2/3] modify according to zhaoqi5's review.

---
 llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
index 21e8179a304ef..491de6553435b 100644
--- a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
@@ -2103,12 +2103,6 @@ static SDValue
 lowerVECTOR_SHUFFLE_XVPERMI(const SDLoc &DL, ArrayRef<int> Mask, MVT VT,
                             SDValue V1, SelectionDAG &DAG,
                             const LoongArchSubtarget &Subtarget) {
-  // Before selecting XVPERMI_D, always try to widen vectors to gain more
-  // optimization opportunities.
-  if (SDValue NewShuffle = widenShuffleMask(
-          DL, Mask, VT, V1, DAG.getUNDEF(V1.getValueType()), DAG))
-    return NewShuffle;
-
   // Only consider XVPERMI_D.
   if (Mask.size() != 4 || (VT != MVT::v4i64 && VT != MVT::v4f64))
     return SDValue();
@@ -2620,6 +2614,10 @@ static SDValue lower256BitShuffle(const SDLoc &DL, ArrayRef<int> Mask, MVT VT,
     if ((Result = lowerVECTOR_SHUFFLE_XVSHUF4I(DL, Mask, VT, V1, V2, DAG,
                                                Subtarget)))
       return Result;
+    // Try to widen vectors to gain more optimization opportunities.
+    if (SDValue NewShuffle = widenShuffleMask(
+            DL, Mask, VT, V1, DAG.getUNDEF(V1.getValueType()), DAG))
+      return NewShuffle;
     if ((Result =
              lowerVECTOR_SHUFFLE_XVPERMI(DL, Mask, VT, V1, DAG, Subtarget)))
       return Result;

>From 6f78c1d47c4dee856270575a92e6eecd694660fb Mon Sep 17 00:00:00 2001
From: yangzhaoxin <yangzhaoxin at loongson.cn>
Date: Wed, 22 Oct 2025 18:14:12 +0800
Subject: [PATCH 3/3] use V2

---
 llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
index 491de6553435b..ca4a655f06587 100644
--- a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
@@ -2615,8 +2615,7 @@ static SDValue lower256BitShuffle(const SDLoc &DL, ArrayRef<int> Mask, MVT VT,
                                                Subtarget)))
       return Result;
     // Try to widen vectors to gain more optimization opportunities.
-    if (SDValue NewShuffle = widenShuffleMask(
-            DL, Mask, VT, V1, DAG.getUNDEF(V1.getValueType()), DAG))
+    if (SDValue NewShuffle = widenShuffleMask(DL, Mask, VT, V1, V2, DAG))
       return NewShuffle;
     if ((Result =
              lowerVECTOR_SHUFFLE_XVPERMI(DL, Mask, VT, V1, DAG, Subtarget)))



More information about the llvm-commits mailing list