Skip to content

[X86] combineConcatVectorOps - concat per-lane v2f64/v4f64 shuffles into vXf64 vshufpd #143017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 69 additions & 20 deletions llvm/lib/Target/X86/X86ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58493,14 +58493,23 @@ static SDValue combineConcatVectorOps(const SDLoc &DL, MVT VT,
const APInt &SrcIdx0 = Src0.getConstantOperandAPInt(1);
const APInt &SrcIdx1 = Src1.getConstantOperandAPInt(1);
// concat(extract_subvector(v0), extract_subvector(v1)) -> vperm2x128.
// Only concat of subvector high halves which vperm2x128 is best at.
// Only concat of subvector high halves which vperm2x128 is best at or if
// it should fold into a subvector broadcast.
if (VT.is256BitVector() && SrcVT0.is256BitVector() &&
SrcVT1.is256BitVector() && SrcIdx0 == (NumSrcElts0 / 2) &&
SrcIdx1 == (NumSrcElts1 / 2)) {
return DAG.getNode(X86ISD::VPERM2X128, DL, VT,
DAG.getBitcast(VT, Src0.getOperand(0)),
DAG.getBitcast(VT, Src1.getOperand(0)),
DAG.getTargetConstant(0x31, DL, MVT::i8));
SrcVT1.is256BitVector()) {
assert((SrcIdx0 == 0 || SrcIdx0 == (NumSrcElts0 / 2)) &&
(SrcIdx1 == 0 || SrcIdx1 == (NumSrcElts1 / 2)) &&
"Bad subvector index");
if ((SrcIdx0 == (NumSrcElts0 / 2) && SrcIdx1 == (NumSrcElts1 / 2)) ||
(IsSplat && ISD::isNormalLoad(Src0.getOperand(0).getNode()))) {
unsigned Index = 0;
Index |= SrcIdx0 == 0 ? 0x00 : 0x01;
Index |= SrcIdx1 == 0 ? 0x20 : 0x30;
return DAG.getNode(X86ISD::VPERM2X128, DL, VT,
DAG.getBitcast(VT, Src0.getOperand(0)),
DAG.getBitcast(VT, Src1.getOperand(0)),
DAG.getTargetConstant(Index, DL, MVT::i8));
}
}
// Widen extract_subvector
// concat(extract_subvector(x,lo), extract_subvector(x,hi))
Expand Down Expand Up @@ -58662,7 +58671,6 @@ static SDValue combineConcatVectorOps(const SDLoc &DL, MVT VT,
break;
}
case X86ISD::SHUFP: {
// TODO: Add SHUFPD support if/when necessary.
if (!IsSplat &&
(VT == MVT::v8f32 ||
(VT == MVT::v16f32 && Subtarget.useAVX512Regs())) &&
Expand Down Expand Up @@ -58731,18 +58739,6 @@ static SDValue combineConcatVectorOps(const SDLoc &DL, MVT VT,
DAG.getNode(X86ISD::VPERMILPI, DL, FloatVT, Res, Op0.getOperand(1));
return DAG.getBitcast(VT, Res);
}
if (!IsSplat && (VT == MVT::v4f64 || VT == MVT::v8f64)) {
unsigned NumSubElts = Op0.getValueType().getVectorNumElements();
uint64_t Mask = (1ULL << NumSubElts) - 1;
uint64_t Idx = 0;
for (unsigned I = 0; I != NumOps; ++I) {
uint64_t SubIdx = Ops[I].getConstantOperandVal(1);
Idx |= (SubIdx & Mask) << (I * NumSubElts);
}
return DAG.getNode(X86ISD::VPERMILPI, DL, VT,
ConcatSubOperand(VT, Ops, 0),
DAG.getTargetConstant(Idx, DL, MVT::i8));
}
break;
case X86ISD::VPERMILPV:
if (!IsSplat && (VT.is256BitVector() ||
Expand Down Expand Up @@ -59313,6 +59309,59 @@ static SDValue combineConcatVectorOps(const SDLoc &DL, MVT VT,
return DAG.getBitcast(VT, Res);
}

// We can always convert per-lane vXf64 shuffles into VSHUFPD.
if (!IsSplat &&
(VT == MVT::v4f64 || (VT == MVT::v8f64 && Subtarget.useAVX512Regs())) &&
all_of(Ops, [](SDValue Op) {
return Op.hasOneUse() && (Op.getOpcode() == X86ISD::MOVDDUP ||
Op.getOpcode() == X86ISD::SHUFP ||
Op.getOpcode() == X86ISD::VPERMILPI ||
Op.getOpcode() == X86ISD::BLENDI ||
Op.getOpcode() == X86ISD::UNPCKL ||
Op.getOpcode() == X86ISD::UNPCKH);
})) {
// Collect the individual per-lane v2f64/v4f64 shuffles.
MVT OpVT = Ops[0].getSimpleValueType();
unsigned NumOpElts = OpVT.getVectorNumElements();
SmallVector<SmallVector<SDValue, 2>, 4> SrcOps(NumOps);
SmallVector<SmallVector<int, 8>, 4> SrcMasks(NumOps);
if (all_of(seq<int>(NumOps), [&](int I) {
return getTargetShuffleMask(Ops[I], /*AllowSentinelZero=*/false,
SrcOps[I], SrcMasks[I]) &&
!is128BitLaneCrossingShuffleMask(OpVT, SrcMasks[I]) &&
SrcMasks[I].size() == NumOpElts &&
all_of(SrcOps[I], [&OpVT](SDValue V) {
return V.getValueType() == OpVT;
});
})) {
// Concatenate the shuffle masks into SHUFPD mask and collect subops.
bool Unary = true;
unsigned SHUFPDMask = 0;
SmallVector<SDValue, 4> LHS(NumOps), RHS(NumOps);
for (unsigned I = 0; I != NumOps; ++I) {
LHS[I] = SrcOps[I][SrcMasks[I][0] / NumOpElts];
RHS[I] = SrcOps[I][SrcMasks[I][1] / NumOpElts];
Unary &= LHS[I] == RHS[I];
for (unsigned J = 0; J != NumOpElts; ++J)
SHUFPDMask |= (SrcMasks[I][J] & 1) << ((I * NumOpElts) + J);
}
// Concat SHUFPD LHS/RHS operands - if they match then it will become a
// PERMILPD mask and we can always profitably concatenate them.
SDValue Concat0 =
combineConcatVectorOps(DL, VT, LHS, DAG, Subtarget, Depth + 1);
SDValue Concat1 =
combineConcatVectorOps(DL, VT, RHS, DAG, Subtarget, Depth + 1);
if (Unary || Concat0 || Concat1) {
Concat0 =
Concat0 ? Concat0 : DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, LHS);
Concat1 =
Concat1 ? Concat1 : DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, RHS);
return DAG.getNode(X86ISD::SHUFP, DL, VT, Concat0, Concat1,
DAG.getTargetConstant(SHUFPDMask, DL, MVT::i8));
}
}
}

return SDValue();
}

Expand Down
Loading