[llvm] [X86] Fold expand(splat,passthrough,mask) -> select(splat,passthrough,mask) (PR #180238)
Simon Pilgrim via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 9 07:47:17 PST 2026
https://github.com/RKSimon updated https://github.com/llvm/llvm-project/pull/180238
>From 4484c145e4d84cd27105a00c776c22b68e5bca43 Mon Sep 17 00:00:00 2001
From: Simon Pilgrim <llvm-dev at redking.me.uk>
Date: Fri, 6 Feb 2026 16:30:22 +0000
Subject: [PATCH 1/2] [X86] Fold expand(splat,passthrough,mask) to
select(splat,passthrough,mask)
If all elements of the expansion vector are already splatted in place then we can use a vselect directly
---
llvm/lib/Target/X86/X86ISelLowering.cpp | 4 ++++
.../X86/vector-shuffle-combining-avx512f.ll | 21 ++++++++-----------
2 files changed, 13 insertions(+), 12 deletions(-)
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 3ec2bf9b19360..ce48b7d6a2b92 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -43593,6 +43593,10 @@ static SDValue combineTargetShuffle(SDValue N, const SDLoc &DL,
if (auto *Msk = dyn_cast<ConstantSDNode>(peekThroughBitcasts(ExpMask)))
if (Msk->getAPIntValue().isMask())
return DAG.getSelect(DL, VT, ExpMask, ExpVec, PassThru);
+ // If ExpVec is splat value, then all elements are already in place - just
+ // use a select.
+ if (DAG.isSplatValue(ExpVec, /*AllowUndefs=*/false))
+ return DAG.getSelect(DL, VT, ExpMask, ExpVec, PassThru);
return SDValue();
}
case X86ISD::VPERMV: {
diff --git a/llvm/test/CodeGen/X86/vector-shuffle-combining-avx512f.ll b/llvm/test/CodeGen/X86/vector-shuffle-combining-avx512f.ll
index 2eae92b7117e9..79b46d9a6e84c 100644
--- a/llvm/test/CodeGen/X86/vector-shuffle-combining-avx512f.ll
+++ b/llvm/test/CodeGen/X86/vector-shuffle-combining-avx512f.ll
@@ -1035,28 +1035,25 @@ define <8 x double> @concat_vpermilvar_v8f64_v4f64(<4 x double> %a0, <4 x double
ret <8 x double> %res
}
-define <16 x float> @combine_vexpandd_of_broadcast(float %x, <16 x float> %y, i16 %m) {
-; X86-LABEL: combine_vexpandd_of_broadcast:
+define <16 x float> @combine_vexpand_of_broadcast(float %x, <16 x float> %y, i16 %m) {
+; X86-LABEL: combine_vexpand_of_broadcast:
; X86: # %bb.0:
-; X86-NEXT: vpbroadcastd {{[0-9]+}}(%esp), %zmm1
; X86-NEXT: kmovw {{[0-9]+}}(%esp), %k1
-; X86-NEXT: vexpandps %zmm1, %zmm0 {%k1}
+; X86-NEXT: vbroadcastss {{[0-9]+}}(%esp), %zmm0 {%k1}
; X86-NEXT: retl
;
-; X64-AVX512F-LABEL: combine_vexpandd_of_broadcast:
+; X64-AVX512F-LABEL: combine_vexpand_of_broadcast:
; X64-AVX512F: # %bb.0:
-; X64-AVX512F-NEXT: vpbroadcastd %xmm0, %zmm0
; X64-AVX512F-NEXT: kmovw %edi, %k1
-; X64-AVX512F-NEXT: vexpandps %zmm0, %zmm1 {%k1}
-; X64-AVX512F-NEXT: vmovdqa64 %zmm1, %zmm0
+; X64-AVX512F-NEXT: vbroadcastss %xmm0, %zmm1 {%k1}
+; X64-AVX512F-NEXT: vmovaps %zmm1, %zmm0
; X64-AVX512F-NEXT: retq
;
-; X64-AVX512BW-LABEL: combine_vexpandd_of_broadcast:
+; X64-AVX512BW-LABEL: combine_vexpand_of_broadcast:
; X64-AVX512BW: # %bb.0:
-; X64-AVX512BW-NEXT: vpbroadcastd %xmm0, %zmm0
; X64-AVX512BW-NEXT: kmovd %edi, %k1
-; X64-AVX512BW-NEXT: vexpandps %zmm0, %zmm1 {%k1}
-; X64-AVX512BW-NEXT: vmovdqa64 %zmm1, %zmm0
+; X64-AVX512BW-NEXT: vbroadcastss %xmm0, %zmm1 {%k1}
+; X64-AVX512BW-NEXT: vmovaps %zmm1, %zmm0
; X64-AVX512BW-NEXT: retq
%xx = insertelement <16 x float> poison, float %x, i32 0
%vx = shufflevector <16 x float> %xx, <16 x float> poison, <16 x i32> zeroinitializer
>From 9b5d729273edbdb216591d6824b367e8c13c270e Mon Sep 17 00:00:00 2001
From: Simon Pilgrim <llvm-dev at redking.me.uk>
Date: Mon, 9 Feb 2026 15:46:26 +0000
Subject: [PATCH 2/2] Fix test name
---
llvm/test/CodeGen/X86/vector-shuffle-combining-avx512f.ll | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/llvm/test/CodeGen/X86/vector-shuffle-combining-avx512f.ll b/llvm/test/CodeGen/X86/vector-shuffle-combining-avx512f.ll
index 79b46d9a6e84c..3d4af023de799 100644
--- a/llvm/test/CodeGen/X86/vector-shuffle-combining-avx512f.ll
+++ b/llvm/test/CodeGen/X86/vector-shuffle-combining-avx512f.ll
@@ -1035,21 +1035,21 @@ define <8 x double> @concat_vpermilvar_v8f64_v4f64(<4 x double> %a0, <4 x double
ret <8 x double> %res
}
-define <16 x float> @combine_vexpand_of_broadcast(float %x, <16 x float> %y, i16 %m) {
-; X86-LABEL: combine_vexpand_of_broadcast:
+define <16 x float> @combine_vexpandps_of_broadcast(float %x, <16 x float> %y, i16 %m) {
+; X86-LABEL: combine_vexpandps_of_broadcast:
; X86: # %bb.0:
; X86-NEXT: kmovw {{[0-9]+}}(%esp), %k1
; X86-NEXT: vbroadcastss {{[0-9]+}}(%esp), %zmm0 {%k1}
; X86-NEXT: retl
;
-; X64-AVX512F-LABEL: combine_vexpand_of_broadcast:
+; X64-AVX512F-LABEL: combine_vexpandps_of_broadcast:
; X64-AVX512F: # %bb.0:
; X64-AVX512F-NEXT: kmovw %edi, %k1
; X64-AVX512F-NEXT: vbroadcastss %xmm0, %zmm1 {%k1}
; X64-AVX512F-NEXT: vmovaps %zmm1, %zmm0
; X64-AVX512F-NEXT: retq
;
-; X64-AVX512BW-LABEL: combine_vexpand_of_broadcast:
+; X64-AVX512BW-LABEL: combine_vexpandps_of_broadcast:
; X64-AVX512BW: # %bb.0:
; X64-AVX512BW-NEXT: kmovd %edi, %k1
; X64-AVX512BW-NEXT: vbroadcastss %xmm0, %zmm1 {%k1}
More information about the llvm-commits
mailing list