[llvm] [AArch64][GlobalISel] Add support for shuffle(v, undef) -> trn(v, v) transformation (PR #220914)
Joshua Rodriguez via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 3 08:52:22 PDT 2026
https://github.com/JoshdRod updated https://github.com/llvm/llvm-project/pull/220914
>From 45a84f5f251f6f0f8b3b505cf6355cac67d5f7fa Mon Sep 17 00:00:00 2001
From: Josh Rodriguez <josh.rodriguez at arm.com>
Date: Thu, 3 Sep 2026 12:34:58 +0000
Subject: [PATCH 1/6] [AArch64][GlobalISel] Add shuffle v, undef -> trn v, v
transformation
---
.../Target/AArch64/AArch64ISelLowering.cpp | 22 +++----------------
.../Target/AArch64/AArch64PerfectShuffle.h | 16 ++++++++++++++
.../GISel/AArch64PostLegalizerLowering.cpp | 5 +++--
3 files changed, 22 insertions(+), 21 deletions(-)
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 22198cd122fc7..6ba59504194ae 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -15518,22 +15518,6 @@ static bool isEXTMaskWithSplat(ArrayRef<int> M, EVT VT, unsigned SplatOperand,
return false;
}
-/// isTRN_v_undef_Mask - Special case of isTRNMask for canonical form of
-/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
-/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
-static bool isTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
- unsigned NumElts = VT.getVectorNumElements();
- if (NumElts % 2 != 0)
- return false;
- WhichResult = (M[0] == 0 ? 0 : 1);
- for (unsigned i = 0; i < NumElts; i += 2) {
- if ((M[i] >= 0 && (unsigned)M[i] != i + WhichResult) ||
- (M[i + 1] >= 0 && (unsigned)M[i + 1] != i + WhichResult))
- return false;
- }
- return true;
-}
-
static bool isINSMask(ArrayRef<int> M, int NumInputElements,
bool &DstIsLeft, int &Anomaly) {
if (M.size() != static_cast<size_t>(NumInputElements))
@@ -16255,7 +16239,7 @@ SDValue AArch64TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
unsigned Opc = (WhichResult == 0) ? AArch64ISD::UZP1 : AArch64ISD::UZP2;
return DAG.getNode(Opc, DL, V1.getValueType(), V1, V1);
}
- if (isTRN_v_undef_Mask(ShuffleMask, VT, WhichResult)) {
+ if (isTRN_v_undef_Mask(ShuffleMask, NumElts, WhichResult)) {
unsigned Opc = (WhichResult == 0) ? AArch64ISD::TRN1 : AArch64ISD::TRN2;
return DAG.getNode(Opc, DL, V1.getValueType(), V1, V1);
}
@@ -18038,7 +18022,7 @@ bool AArch64TargetLowering::isShuffleMaskLegal(ArrayRef<int> M, EVT VT) const {
isTRNMask(M, NumElts, DummyUnsigned, DummyUnsigned) ||
isUZPMask(M, NumElts, DummyUnsigned) ||
isZIPMask(M, NumElts, DummyUnsigned, DummyUnsigned) ||
- isTRN_v_undef_Mask(M, VT, DummyUnsigned) ||
+ isTRN_v_undef_Mask(M, NumElts, DummyUnsigned) ||
isUZP_v_undef_Mask(M, NumElts, DummyUnsigned) ||
isZIP_v_undef_Mask(M, NumElts, DummyUnsigned) ||
isINSMask(M, NumElts, DummyBool, DummyInt) ||
@@ -35618,7 +35602,7 @@ SDValue AArch64TargetLowering::LowerFixedLengthVECTOR_SHUFFLEToSVE(
return convertFromScalableVector(
DAG, VT, DAG.getNode(AArch64ISD::ZIP1, DL, ContainerVT, Op1, Op1));
- if (isTRN_v_undef_Mask(ShuffleMask, VT, WhichResult)) {
+ if (isTRN_v_undef_Mask(ShuffleMask, NumElts, WhichResult)) {
unsigned Opc = (WhichResult == 0) ? AArch64ISD::TRN1 : AArch64ISD::TRN2;
return convertFromScalableVector(
DAG, VT, DAG.getNode(Opc, DL, ContainerVT, Op1, Op1));
diff --git a/llvm/lib/Target/AArch64/AArch64PerfectShuffle.h b/llvm/lib/Target/AArch64/AArch64PerfectShuffle.h
index 9683248b2e66b..04c91e51bd13e 100644
--- a/llvm/lib/Target/AArch64/AArch64PerfectShuffle.h
+++ b/llvm/lib/Target/AArch64/AArch64PerfectShuffle.h
@@ -221,6 +221,22 @@ inline bool isTRNMask(ArrayRef<int> M, unsigned NumElts,
return true;
}
+/// isTRN_v_undef_Mask - Special case of isTRNMask for canonical form of
+/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
+/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
+static bool isTRN_v_undef_Mask(ArrayRef<int> M, unsigned NumElts,
+ unsigned &WhichResult) {
+ if (NumElts % 2 != 0)
+ return false;
+ WhichResult = (M[0] == 0 ? 0 : 1);
+ for (unsigned i = 0; i < NumElts; i += 2) {
+ if ((M[i] >= 0 && (unsigned)M[i] != i + WhichResult) ||
+ (M[i + 1] >= 0 && (unsigned)M[i + 1] != i + WhichResult))
+ return false;
+ }
+ return true;
+}
+
/// isREVMask - Check if a vector shuffle corresponds to a REV
/// instruction with the specified blocksize. (The order of the elements
/// within each block of the vector is reversed.)
diff --git a/llvm/lib/Target/AArch64/GISel/AArch64PostLegalizerLowering.cpp b/llvm/lib/Target/AArch64/GISel/AArch64PostLegalizerLowering.cpp
index 283e0cfbebfe1..6256d78da324d 100644
--- a/llvm/lib/Target/AArch64/GISel/AArch64PostLegalizerLowering.cpp
+++ b/llvm/lib/Target/AArch64/GISel/AArch64PostLegalizerLowering.cpp
@@ -192,11 +192,12 @@ bool matchTRN(MachineInstr &MI, MachineRegisterInfo &MRI,
ShuffleVectorPseudo &MatchInfo) {
assert(MI.getOpcode() == TargetOpcode::G_SHUFFLE_VECTOR);
unsigned WhichResult;
- unsigned OperandOrder;
+ unsigned OperandOrder = 0;
ArrayRef<int> ShuffleMask = MI.getOperand(3).getShuffleMask();
Register Dst = MI.getOperand(0).getReg();
unsigned NumElts = MRI.getType(Dst).getNumElements();
- if (!isTRNMask(ShuffleMask, NumElts, WhichResult, OperandOrder))
+ if (!isTRNMask(ShuffleMask, NumElts, WhichResult, OperandOrder) &&
+ !isTRN_v_undef_Mask(ShuffleMask, NumElts, WhichResult))
return false;
unsigned Opc = (WhichResult == 0) ? AArch64::G_TRN1 : AArch64::G_TRN2;
Register V1 = MI.getOperand(OperandOrder == 0 ? 1 : 2).getReg();
>From f39a02e9cb373e94f5e7931714ab7edbe4a31f54 Mon Sep 17 00:00:00 2001
From: Josh Rodriguez <josh.rodriguez at arm.com>
Date: Thu, 3 Sep 2026 13:26:08 +0000
Subject: [PATCH 2/6] [AArch64][GlobalISel] Add unit test for <8 x i8>
Also add GlobalISel check lines to arm64-trn.ll.
---
llvm/test/CodeGen/AArch64/arm64-trn.ll | 48 ++++++++++++++++++++++----
1 file changed, 41 insertions(+), 7 deletions(-)
diff --git a/llvm/test/CodeGen/AArch64/arm64-trn.ll b/llvm/test/CodeGen/AArch64/arm64-trn.ll
index 85a56c042136c..db288a94e87cf 100644
--- a/llvm/test/CodeGen/AArch64/arm64-trn.ll
+++ b/llvm/test/CodeGen/AArch64/arm64-trn.ll
@@ -1,5 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
-; RUN: llc < %s -mtriple=aarch64 | FileCheck %s --check-prefixes=CHECKLE
+; RUN: llc < %s -mtriple=aarch64 | FileCheck %s --check-prefixes=CHECKLE,CHECKLE-SD
+; RUN: llc < %s -mtriple=aarch64 --global-isel --global-isel-abort=1 | FileCheck %s --check-prefixes=CHECKLE,CHECKLE-GI
; RUN: llc < %s -mtriple=aarch64_be | FileCheck %s --check-prefixes=CHECKBE
define <8 x i8> @vtrni8(ptr %A, ptr %B) nounwind {
@@ -57,12 +58,23 @@ define <4 x i16> @vtrni16(ptr %A, ptr %B) nounwind {
}
define <8 x i8> @vtrni16_viabitcast(ptr %A, ptr %B) nounwind {
-; CHECKLE-LABEL: vtrni16_viabitcast:
-; CHECKLE: // %bb.0:
-; CHECKLE-NEXT: ldr d0, [x0]
-; CHECKLE-NEXT: ldr d1, [x1]
-; CHECKLE-NEXT: trn1 v0.4h, v0.4h, v1.4h
-; CHECKLE-NEXT: ret
+; CHECKLE-SD-LABEL: vtrni16_viabitcast:
+; CHECKLE-SD: // %bb.0:
+; CHECKLE-SD-NEXT: ldr d0, [x0]
+; CHECKLE-SD-NEXT: ldr d1, [x1]
+; CHECKLE-SD-NEXT: trn1 v0.4h, v0.4h, v1.4h
+; CHECKLE-SD-NEXT: ret
+;
+; CHECKLE-GI-LABEL: vtrni16_viabitcast:
+; CHECKLE-GI: // %bb.0:
+; CHECKLE-GI-NEXT: ldr d0, [x0]
+; CHECKLE-GI-NEXT: ldr d1, [x1]
+; CHECKLE-GI-NEXT: adrp x8, .LCPI2_0
+; CHECKLE-GI-NEXT: mov v0.d[1], v1.d[0]
+; CHECKLE-GI-NEXT: ldr d1, [x8, :lo12:.LCPI2_0]
+; CHECKLE-GI-NEXT: tbl v0.16b, { v0.16b }, v1.16b
+; CHECKLE-GI-NEXT: // kill: def $d0 killed $d0 killed $q0
+; CHECKLE-GI-NEXT: ret
;
; CHECKBE-LABEL: vtrni16_viabitcast:
; CHECKBE: // %bb.0:
@@ -463,3 +475,25 @@ define <16 x i8> @vtrnQi8_undef_012(ptr %A, ptr %B) nounwind {
%tmp5 = add <16 x i8> %tmp3, %tmp4
ret <16 x i8> %tmp5
}
+
+define <8 x i8> @vtrnDi8_poison(<8 x i8> %a) {
+; CHECKLE-LABEL: vtrnDi8_poison:
+; CHECKLE: // %bb.0:
+; CHECKLE-NEXT: trn1 v1.8b, v0.8b, v0.8b
+; CHECKLE-NEXT: trn2 v0.8b, v0.8b, v0.8b
+; CHECKLE-NEXT: eor v0.8b, v1.8b, v0.8b
+; CHECKLE-NEXT: ret
+;
+; CHECKBE-LABEL: vtrnDi8_poison:
+; CHECKBE: // %bb.0:
+; CHECKBE-NEXT: rev64 v0.8b, v0.8b
+; CHECKBE-NEXT: trn1 v1.8b, v0.8b, v0.8b
+; CHECKBE-NEXT: trn2 v0.8b, v0.8b, v0.8b
+; CHECKBE-NEXT: eor v0.8b, v1.8b, v0.8b
+; CHECKBE-NEXT: rev64 v0.8b, v0.8b
+; CHECKBE-NEXT: ret
+ %tmp3 = shufflevector <8 x i8> %a, <8 x i8> poison, <8 x i32> <i32 0, i32 0, i32 2, i32 2, i32 4, i32 4, i32 6, i32 6>
+ %tmp4 = shufflevector <8 x i8> %a, <8 x i8> poison, <8 x i32> <i32 1, i32 1, i32 3, i32 3, i32 5, i32 5, i32 7, i32 7>
+ %ret = xor <8 x i8> %tmp3, %tmp4
+ ret <8 x i8> %ret
+}
>From d057f7800ec7571f5f47fe9d6a764a4d7183235b Mon Sep 17 00:00:00 2001
From: Josh Rodriguez <josh.rodriguez at arm.com>
Date: Thu, 3 Sep 2026 13:33:25 +0000
Subject: [PATCH 3/6] [AArch64][GlobalISel] Add unit test for <8 x i16>
---
llvm/test/CodeGen/AArch64/arm64-trn.ll | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/llvm/test/CodeGen/AArch64/arm64-trn.ll b/llvm/test/CodeGen/AArch64/arm64-trn.ll
index db288a94e87cf..aba55be1b99ab 100644
--- a/llvm/test/CodeGen/AArch64/arm64-trn.ll
+++ b/llvm/test/CodeGen/AArch64/arm64-trn.ll
@@ -497,3 +497,27 @@ define <8 x i8> @vtrnDi8_poison(<8 x i8> %a) {
%ret = xor <8 x i8> %tmp3, %tmp4
ret <8 x i8> %ret
}
+
+define <8 x i16> @vtrnQi8_poison(<8 x i16> %a) {
+; CHECKLE-LABEL: vtrnQi8_poison:
+; CHECKLE: // %bb.0:
+; CHECKLE-NEXT: trn1 v1.8h, v0.8h, v0.8h
+; CHECKLE-NEXT: trn2 v0.8h, v0.8h, v0.8h
+; CHECKLE-NEXT: eor v0.16b, v1.16b, v0.16b
+; CHECKLE-NEXT: ret
+;
+; CHECKBE-LABEL: vtrnQi8_poison:
+; CHECKBE: // %bb.0:
+; CHECKBE-NEXT: rev64 v0.8h, v0.8h
+; CHECKBE-NEXT: ext v0.16b, v0.16b, v0.16b, #8
+; CHECKBE-NEXT: trn1 v1.8h, v0.8h, v0.8h
+; CHECKBE-NEXT: trn2 v0.8h, v0.8h, v0.8h
+; CHECKBE-NEXT: eor v0.16b, v1.16b, v0.16b
+; CHECKBE-NEXT: rev64 v0.8h, v0.8h
+; CHECKBE-NEXT: ext v0.16b, v0.16b, v0.16b, #8
+; CHECKBE-NEXT: ret
+ %tmp3 = shufflevector <8 x i16> %a, <8 x i16> poison, <8 x i32> <i32 0, i32 0, i32 2, i32 2, i32 4, i32 4, i32 6, i32 6>
+ %tmp4 = shufflevector <8 x i16> %a, <8 x i16> poison, <8 x i32> <i32 1, i32 1, i32 3, i32 3, i32 5, i32 5, i32 7, i32 7>
+ %ret = xor <8 x i16> %tmp3, %tmp4
+ ret <8 x i16> %ret
+}
>From 528adc5b53218d1b7f3491c758eddf8eb72f4ce8 Mon Sep 17 00:00:00 2001
From: Josh Rodriguez <josh.rodriguez at arm.com>
Date: Thu, 3 Sep 2026 13:46:32 +0000
Subject: [PATCH 4/6] [AArch64][GlobalISel] Make isTRN_v_undef_Mask inline
Previously, the function was static. To avoid each translation unit getting its own copy of isTRN_v_undef_Mask, make the function inline.
Note (for me \!): A translation unit is a cpp file + all the headers it includes. Different files are compiled separately - and so each separate group is called a translation unit.
---
llvm/lib/Target/AArch64/AArch64PerfectShuffle.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/Target/AArch64/AArch64PerfectShuffle.h b/llvm/lib/Target/AArch64/AArch64PerfectShuffle.h
index 04c91e51bd13e..00d9edbbc02ea 100644
--- a/llvm/lib/Target/AArch64/AArch64PerfectShuffle.h
+++ b/llvm/lib/Target/AArch64/AArch64PerfectShuffle.h
@@ -224,7 +224,7 @@ inline bool isTRNMask(ArrayRef<int> M, unsigned NumElts,
/// isTRN_v_undef_Mask - Special case of isTRNMask for canonical form of
/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
-static bool isTRN_v_undef_Mask(ArrayRef<int> M, unsigned NumElts,
+inline bool isTRN_v_undef_Mask(ArrayRef<int> M, unsigned NumElts,
unsigned &WhichResult) {
if (NumElts % 2 != 0)
return false;
>From 1f5dbbb1ba72dc4a9aaadc5d8870ac788a58bdc6 Mon Sep 17 00:00:00 2001
From: Josh Rodriguez <josh.rodriguez at arm.com>
Date: Thu, 3 Sep 2026 15:44:49 +0000
Subject: [PATCH 5/6] [AArch64][GlobalISel] Set V2 = V1
For shuffle x, y situations, the transformation can either be:
1. trn x, x
2. trn y, y
But for shuffle x, undef situation, there's only one possible transformation:
trn x, x.
Hence, make the second parameter of trn always come from the first parameter of the shuffle.
---
.../Target/AArch64/GISel/AArch64PostLegalizerLowering.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Target/AArch64/GISel/AArch64PostLegalizerLowering.cpp b/llvm/lib/Target/AArch64/GISel/AArch64PostLegalizerLowering.cpp
index 6256d78da324d..f6f6008003af6 100644
--- a/llvm/lib/Target/AArch64/GISel/AArch64PostLegalizerLowering.cpp
+++ b/llvm/lib/Target/AArch64/GISel/AArch64PostLegalizerLowering.cpp
@@ -196,12 +196,12 @@ bool matchTRN(MachineInstr &MI, MachineRegisterInfo &MRI,
ArrayRef<int> ShuffleMask = MI.getOperand(3).getShuffleMask();
Register Dst = MI.getOperand(0).getReg();
unsigned NumElts = MRI.getType(Dst).getNumElements();
- if (!isTRNMask(ShuffleMask, NumElts, WhichResult, OperandOrder) &&
- !isTRN_v_undef_Mask(ShuffleMask, NumElts, WhichResult))
+ bool TRNMask = isTRNMask(ShuffleMask, NumElts, WhichResult, OperandOrder);
+ if (!TRNMask && !isTRN_v_undef_Mask(ShuffleMask, NumElts, WhichResult))
return false;
unsigned Opc = (WhichResult == 0) ? AArch64::G_TRN1 : AArch64::G_TRN2;
Register V1 = MI.getOperand(OperandOrder == 0 ? 1 : 2).getReg();
- Register V2 = MI.getOperand(OperandOrder == 0 ? 2 : 1).getReg();
+ Register V2 = MI.getOperand(OperandOrder == 0 && TRNMask ? 2 : 1).getReg();
MatchInfo = ShuffleVectorPseudo(Opc, Dst, {V1, V2});
return true;
}
>From d7a0e49cd3774aaf3f19d04ea8a49aaed5d01969 Mon Sep 17 00:00:00 2001
From: Josh Rodriguez <josh.rodriguez at arm.com>
Date: Thu, 3 Sep 2026 15:51:42 +0000
Subject: [PATCH 6/6] [AArch64][GlobalISel] Remove --global-isel in test check
---
llvm/test/CodeGen/AArch64/arm64-trn.ll | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/test/CodeGen/AArch64/arm64-trn.ll b/llvm/test/CodeGen/AArch64/arm64-trn.ll
index aba55be1b99ab..85144262f107b 100644
--- a/llvm/test/CodeGen/AArch64/arm64-trn.ll
+++ b/llvm/test/CodeGen/AArch64/arm64-trn.ll
@@ -1,6 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc < %s -mtriple=aarch64 | FileCheck %s --check-prefixes=CHECKLE,CHECKLE-SD
-; RUN: llc < %s -mtriple=aarch64 --global-isel --global-isel-abort=1 | FileCheck %s --check-prefixes=CHECKLE,CHECKLE-GI
+; RUN: llc < %s -mtriple=aarch64 --global-isel | FileCheck %s --check-prefixes=CHECKLE,CHECKLE-GI
; RUN: llc < %s -mtriple=aarch64_be | FileCheck %s --check-prefixes=CHECKBE
define <8 x i8> @vtrni8(ptr %A, ptr %B) nounwind {
More information about the llvm-commits
mailing list