Skip to content

Commit

Permalink
[X86] combineSelect - canonicalize (vXi1 bitcast(iX Cond)) with combi…
Browse files Browse the repository at this point in the history
…neToExtendBoolVectorInReg before legalization

This replaces the attempt in 20af71f to use combineToExtendBoolVectorInReg to create X86ISD::BLENDV masks directly, instead we use it to canonicalize the iX bitcast to a sign-extended mask and then truncate it back to vXi1 prior to legalization breaking it apart.

Fixes #53760
  • Loading branch information
RKSimon committed Mar 15, 2022
1 parent e60defb commit f591231
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 466 deletions.
26 changes: 14 additions & 12 deletions llvm/lib/Target/X86/X86ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43742,17 +43742,6 @@ static SDValue combineVSelectToBLENDV(SDNode *N, SelectionDAG &DAG,
if (VT.is512BitVector())
return SDValue();

// PreAVX512, without mask-registers, attempt to sign-extend bool vectors to
// allow us to use BLENDV.
if (!Subtarget.hasAVX512() && BitWidth == 1) {
EVT CondVT = VT.changeVectorElementTypeToInteger();
if (SDValue ExtCond = combineToExtendBoolVectorInReg(
ISD::SIGN_EXTEND, SDLoc(N), CondVT, Cond, DAG, DCI, Subtarget)) {
return DAG.getNode(X86ISD::BLENDV, SDLoc(N), VT, ExtCond,
N->getOperand(1), N->getOperand(2));
}
}

// Don't optimize before the condition has been transformed to a legal type
// and don't ever optimize vector selects that map to AVX512 mask-registers.
if (BitWidth < 8 || BitWidth > 64)
Expand Down Expand Up @@ -44235,7 +44224,7 @@ static SDValue combineSelect(SDNode *N, SelectionDAG &DAG,
// If this an avx512 target we can improve the use of zero masking by
// swapping the operands and inverting the condition.
if (N->getOpcode() == ISD::VSELECT && Cond.hasOneUse() &&
Subtarget.hasAVX512() && CondVT.getVectorElementType() == MVT::i1 &&
Subtarget.hasAVX512() && CondVT.getVectorElementType() == MVT::i1 &&
ISD::isBuildVectorAllZeros(LHS.getNode()) &&
!ISD::isBuildVectorAllZeros(RHS.getNode())) {
// Invert the cond to not(cond) : xor(op,allones)=not(op)
Expand All @@ -44244,6 +44233,19 @@ static SDValue combineSelect(SDNode *N, SelectionDAG &DAG,
return DAG.getSelect(DL, VT, CondNew, RHS, LHS);
}

// Attempt to convert a (vXi1 bitcast(iX Cond)) selection mask before it might
// get split by legalization.
if (N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::BITCAST &&
CondVT.getVectorElementType() == MVT::i1 && Cond.hasOneUse() &&
TLI.isTypeLegal(VT.getScalarType())) {
EVT ExtCondVT = VT.changeVectorElementTypeToInteger();
if (SDValue ExtCond = combineToExtendBoolVectorInReg(
ISD::SIGN_EXTEND, DL, ExtCondVT, Cond, DAG, DCI, Subtarget)) {
ExtCond = DAG.getNode(ISD::TRUNCATE, DL, CondVT, ExtCond);
return DAG.getSelect(DL, VT, ExtCond, LHS, RHS);
}
}

// Early exit check
if (!TLI.isTypeLegal(VT))
return SDValue();
Expand Down
Loading

0 comments on commit f591231

Please sign in to comment.