Skip to content

Commit

Permalink
[X86] Attempt to canonicalize vXf64 SHUFPD shuffle masks with undef e…
Browse files Browse the repository at this point in the history
…lts to improve further folding (#116419)

Currently when creating a SHUFPD immediate mask, any undef shuffle elements are set to 0, which can limit options for further shuffle combining.

This patch attempts to canonicalize the mask to improve folding: first by detecting a per-lane broadcast style mask (which can allow us to fold to UNPCK instead), and second ensure any undef elements are set to an 'inplace' value to improve chances of the SHUFPD later folding to a BLENDPD (or be bypassed in a SimplifyMultipleUseDemandedVectorElts call).

This is very similar to canonicalization we already attempt in getV4X86ShuffleImm for vXi32/vXf32 SHUFPS/SHUFD shuffles.
  • Loading branch information
RKSimon authored Nov 19, 2024
1 parent 6f53ae6 commit 95ab426
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 244 deletions.
59 changes: 48 additions & 11 deletions llvm/lib/Target/X86/X86ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9926,6 +9926,42 @@ static SDValue getV4X86ShuffleImm8ForMask(ArrayRef<int> Mask, const SDLoc &DL,
return DAG.getTargetConstant(getV4X86ShuffleImm(Mask), DL, MVT::i8);
}

// Canonicalize SHUFPD mask to improve chances of further folding.
// Mask elements are assumed to be -1, 0 or 1 to match the SHUFPD lo/hi pattern.
static unsigned getSHUFPDImm(ArrayRef<int> Mask) {
assert((Mask.size() == 2 || Mask.size() == 4 || Mask.size() == 8) &&
"Unexpected SHUFPD mask size");
assert(all_of(Mask, [](int M) { return -1 <= M && M <= 1; }) &&
"Unexpected SHUFPD mask elements");

// If the mask only uses one non-undef element, then fully 'splat' it to
// improve later broadcast matching.
int FirstIndex = find_if(Mask, [](int M) { return M >= 0; }) - Mask.begin();
assert(0 <= FirstIndex && FirstIndex < 4 && "All undef shuffle mask");

int FirstElt = Mask[FirstIndex];
if (all_of(Mask, [FirstElt](int M) { return M < 0 || M == FirstElt; }) &&
count_if(Mask, [FirstElt](int M) { return M == FirstElt; }) > 1) {
unsigned Imm = 0;
for (unsigned I = 0, E = Mask.size(); I != E; ++I)
Imm |= FirstElt << I;
return Imm;
}

// Attempt to keep any undef elements in place to improve chances of the
// shuffle becoming a (commutative) blend.
unsigned Imm = 0;
for (unsigned I = 0, E = Mask.size(); I != E; ++I)
Imm |= (Mask[I] < 0 ? (I & 1) : Mask[I]) << I;

return Imm;
}

static SDValue getSHUFPDImmForMask(ArrayRef<int> Mask, const SDLoc &DL,
SelectionDAG &DAG) {
return DAG.getTargetConstant(getSHUFPDImm(Mask), DL, MVT::i8);
}

// The Shuffle result is as follow:
// 0*a[0]0*a[1]...0*a[n] , n >=0 where a[] elements in a ascending order.
// Each Zeroable's element correspond to a particular Mask's element.
Expand Down Expand Up @@ -14871,7 +14907,7 @@ static SDValue lowerShuffleAsLanePermuteAndSHUFP(const SDLoc &DL, MVT VT,

int LHSMask[4] = {-1, -1, -1, -1};
int RHSMask[4] = {-1, -1, -1, -1};
unsigned SHUFPMask = 0;
int SHUFPDMask[4] = {-1, -1, -1, -1};

// As SHUFPD uses a single LHS/RHS element per lane, we can always
// perform the shuffle once the lanes have been shuffled in place.
Expand All @@ -14882,13 +14918,13 @@ static SDValue lowerShuffleAsLanePermuteAndSHUFP(const SDLoc &DL, MVT VT,
int LaneBase = i & ~1;
auto &LaneMask = (i & 1) ? RHSMask : LHSMask;
LaneMask[LaneBase + (M & 1)] = M;
SHUFPMask |= (M & 1) << i;
SHUFPDMask[i] = M & 1;
}

SDValue LHS = DAG.getVectorShuffle(VT, DL, V1, V2, LHSMask);
SDValue RHS = DAG.getVectorShuffle(VT, DL, V1, V2, RHSMask);
return DAG.getNode(X86ISD::SHUFP, DL, VT, LHS, RHS,
DAG.getTargetConstant(SHUFPMask, DL, MVT::i8));
getSHUFPDImmForMask(SHUFPDMask, DL, DAG));
}

/// Lower a vector shuffle crossing multiple 128-bit lanes as
Expand Down Expand Up @@ -15800,9 +15836,9 @@ static bool matchShuffleWithSHUFPD(MVT VT, SDValue &V1, SDValue &V2,

// Mask for V8F64: 0/1, 8/9, 2/3, 10/11, 4/5, ..
// Mask for V4F64; 0/1, 4/5, 2/3, 6/7..
ShuffleImm = 0;
bool ShufpdMask = true;
bool CommutableMask = true;
bool IsSHUFPD = true;
bool IsCommutable = true;
SmallVector<int, 8> SHUFPDMask(NumElts, -1);
for (int i = 0; i < NumElts; ++i) {
if (Mask[i] == SM_SentinelUndef || ZeroLane[i & 1])
continue;
Expand All @@ -15811,20 +15847,21 @@ static bool matchShuffleWithSHUFPD(MVT VT, SDValue &V1, SDValue &V2,
int Val = (i & 6) + NumElts * (i & 1);
int CommutVal = (i & 0xe) + NumElts * ((i & 1) ^ 1);
if (Mask[i] < Val || Mask[i] > Val + 1)
ShufpdMask = false;
IsSHUFPD = false;
if (Mask[i] < CommutVal || Mask[i] > CommutVal + 1)
CommutableMask = false;
ShuffleImm |= (Mask[i] % 2) << i;
IsCommutable = false;
SHUFPDMask[i] = Mask[i] % 2;
}

if (!ShufpdMask && !CommutableMask)
if (!IsSHUFPD && !IsCommutable)
return false;

if (!ShufpdMask && CommutableMask)
if (!IsSHUFPD && IsCommutable)
std::swap(V1, V2);

ForceV1Zero = ZeroLane[0];
ForceV2Zero = ZeroLane[1];
ShuffleImm = getSHUFPDImm(SHUFPDMask);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4756,7 +4756,7 @@ define void @vec384_i64_widen_to_i128_factor2_broadcast_to_v3i128_factor3(ptr %i
; AVX-NEXT: vpaddb 48(%rsi), %xmm2, %xmm2
; AVX-NEXT: vpaddb (%rsi), %xmm0, %xmm0
; AVX-NEXT: vinsertf128 $1, %xmm0, %ymm0, %ymm3
; AVX-NEXT: vshufpd {{.*#+}} ymm2 = ymm3[0],ymm2[1],ymm3[2],ymm2[2]
; AVX-NEXT: vblendps {{.*#+}} ymm2 = ymm3[0,1],ymm2[2,3],ymm3[4,5],ymm2[6,7]
; AVX-NEXT: vextractf128 $1, %ymm2, %xmm3
; AVX-NEXT: vpaddb 16(%rdx), %xmm3, %xmm3
; AVX-NEXT: vpaddb (%rdx), %xmm2, %xmm2
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/CodeGen/X86/subvector-broadcast.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1667,13 +1667,13 @@ define <8 x float> @broadcast_v8f32_v2f32_u1uu0uEu(ptr %vp, <8 x float> %default
; X86: # %bb.0:
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
; X86-NEXT: vbroadcastsd (%eax), %ymm1
; X86-NEXT: vshufpd {{.*#+}} ymm0 = ymm1[0],ymm0[0],ymm1[2],ymm0[3]
; X86-NEXT: vunpckhpd {{.*#+}} ymm0 = ymm1[1],ymm0[1],ymm1[3],ymm0[3]
; X86-NEXT: retl
;
; X64-LABEL: broadcast_v8f32_v2f32_u1uu0uEu:
; X64: # %bb.0:
; X64-NEXT: vbroadcastsd (%rdi), %ymm1
; X64-NEXT: vshufpd {{.*#+}} ymm0 = ymm1[0],ymm0[0],ymm1[2],ymm0[3]
; X64-NEXT: vunpckhpd {{.*#+}} ymm0 = ymm1[1],ymm0[1],ymm1[3],ymm0[3]
; X64-NEXT: retq
%vec = load <2 x float>, ptr %vp
%shuf = shufflevector <2 x float> %vec, <2 x float> undef, <8 x i32> <i32 undef, i32 1, i32 undef, i32 undef, i32 0, i32 2, i32 3, i32 undef>
Expand Down
Loading

0 comments on commit 95ab426

Please sign in to comment.