Skip to content

Commit b179351

Browse files
committed
[SDAG] refactor folds for scalar-to-vector; NFCI
Fix typos, add comments, improve variable names, rearrange code, add early exits.
1 parent 0057756 commit b179351

File tree

1 file changed

+44
-41
lines changed

1 file changed

+44
-41
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23504,51 +23504,54 @@ SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
2350423504
}
2350523505

2350623506
SDValue DAGCombiner::visitSCALAR_TO_VECTOR(SDNode *N) {
23507-
SDValue InVal = N->getOperand(0);
23507+
SDValue Scalar = N->getOperand(0);
2350823508
EVT VT = N->getValueType(0);
23509+
if (!VT.isFixedLengthVector())
23510+
return SDValue();
2350923511

2351023512
// Replace a SCALAR_TO_VECTOR(EXTRACT_VECTOR_ELT(V,C0)) pattern
2351123513
// with a VECTOR_SHUFFLE and possible truncate.
23512-
if (InVal.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
23513-
VT.isFixedLengthVector() &&
23514-
InVal->getOperand(0).getValueType().isFixedLengthVector()) {
23515-
SDValue InVec = InVal->getOperand(0);
23516-
SDValue EltNo = InVal->getOperand(1);
23517-
auto InVecT = InVec.getValueType();
23518-
if (ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(EltNo)) {
23519-
SmallVector<int, 8> NewMask(InVecT.getVectorNumElements(), -1);
23520-
int Elt = C0->getZExtValue();
23521-
NewMask[0] = Elt;
23522-
// If we have an implict truncate do truncate here as long as it's legal.
23523-
// if it's not legal, this should
23524-
if (VT.getScalarType() != InVal.getValueType() &&
23525-
InVal.getValueType().isScalarInteger() &&
23526-
isTypeLegal(VT.getScalarType())) {
23527-
SDValue Val =
23528-
DAG.getNode(ISD::TRUNCATE, SDLoc(InVal), VT.getScalarType(), InVal);
23529-
return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), VT, Val);
23530-
}
23531-
if (VT.getScalarType() == InVecT.getScalarType() &&
23532-
VT.getVectorNumElements() <= InVecT.getVectorNumElements()) {
23533-
SDValue LegalShuffle =
23534-
TLI.buildLegalVectorShuffle(InVecT, SDLoc(N), InVec,
23535-
DAG.getUNDEF(InVecT), NewMask, DAG);
23536-
if (LegalShuffle) {
23537-
// If the initial vector is the correct size this shuffle is a
23538-
// valid result.
23539-
if (VT == InVecT)
23540-
return LegalShuffle;
23541-
// If not we must truncate the vector.
23542-
if (VT.getVectorNumElements() != InVecT.getVectorNumElements()) {
23543-
SDValue ZeroIdx = DAG.getVectorIdxConstant(0, SDLoc(N));
23544-
EVT SubVT = EVT::getVectorVT(*DAG.getContext(),
23545-
InVecT.getVectorElementType(),
23546-
VT.getVectorNumElements());
23547-
return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N), SubVT,
23548-
LegalShuffle, ZeroIdx);
23549-
}
23550-
}
23551-
}
23514+
if (Scalar.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
23515+
!Scalar.getOperand(0).getValueType().isFixedLengthVector())
23516+
return SDValue();
23517+
23518+
// If we have an implicit truncate, truncate here if it is legal.
23519+
if (VT.getScalarType() != Scalar.getValueType() &&
23520+
Scalar.getValueType().isScalarInteger() &&
23521+
isTypeLegal(VT.getScalarType())) {
23522+
SDValue Val =
23523+
DAG.getNode(ISD::TRUNCATE, SDLoc(Scalar), VT.getScalarType(), Scalar);
23524+
return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), VT, Val);
23525+
}
23526+
23527+
auto *ExtIndexC = dyn_cast<ConstantSDNode>(Scalar.getOperand(1));
23528+
if (!ExtIndexC)
23529+
return SDValue();
23530+
23531+
SDValue SrcVec = Scalar.getOperand(0);
23532+
EVT SrcVT = SrcVec.getValueType();
23533+
unsigned SrcNumElts = SrcVT.getVectorNumElements();
23534+
unsigned VTNumElts = VT.getVectorNumElements();
23535+
if (VT.getScalarType() == SrcVT.getScalarType() && VTNumElts <= SrcNumElts) {
23536+
// Create a shuffle equivalent for scalar-to-vector: {ExtIndex, -1, -1, ...}
23537+
SmallVector<int, 8> Mask(SrcNumElts, -1);
23538+
Mask[0] = ExtIndexC->getZExtValue();
23539+
SDValue LegalShuffle = TLI.buildLegalVectorShuffle(
23540+
SrcVT, SDLoc(N), SrcVec, DAG.getUNDEF(SrcVT), Mask, DAG);
23541+
if (!LegalShuffle)
23542+
return SDValue();
23543+
23544+
// If the initial vector is the same size, the shuffle is the result.
23545+
if (VT == SrcVT)
23546+
return LegalShuffle;
23547+
23548+
// If not, shorten the shuffled vector.
23549+
if (VTNumElts != SrcNumElts) {
23550+
SDValue ZeroIdx = DAG.getVectorIdxConstant(0, SDLoc(N));
23551+
EVT SubVT = EVT::getVectorVT(*DAG.getContext(),
23552+
SrcVT.getVectorElementType(), VTNumElts);
23553+
return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N), SubVT, LegalShuffle,
23554+
ZeroIdx);
2355223555
}
2355323556
}
2355423557

0 commit comments

Comments
 (0)