[llvm] b179351 - [SDAG] refactor folds for scalar-to-vector; NFCI
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 25 09:53:52 PDT 2022
Author: Sanjay Patel
Date: 2022-10-25T12:53:46-04:00
New Revision: b179351ad49e59c525c1030670a7a14efd80998b
URL: https://github.com/llvm/llvm-project/commit/b179351ad49e59c525c1030670a7a14efd80998b
DIFF: https://github.com/llvm/llvm-project/commit/b179351ad49e59c525c1030670a7a14efd80998b.diff
LOG: [SDAG] refactor folds for scalar-to-vector; NFCI
Fix typos, add comments, improve variable names,
rearrange code, add early exits.
Added:
Modified:
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Removed:
################################################################################
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index aeccc5e4d9702..9af878646ded1 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -23504,51 +23504,54 @@ SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
}
SDValue DAGCombiner::visitSCALAR_TO_VECTOR(SDNode *N) {
- SDValue InVal = N->getOperand(0);
+ SDValue Scalar = N->getOperand(0);
EVT VT = N->getValueType(0);
+ if (!VT.isFixedLengthVector())
+ return SDValue();
// Replace a SCALAR_TO_VECTOR(EXTRACT_VECTOR_ELT(V,C0)) pattern
// with a VECTOR_SHUFFLE and possible truncate.
- if (InVal.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
- VT.isFixedLengthVector() &&
- InVal->getOperand(0).getValueType().isFixedLengthVector()) {
- SDValue InVec = InVal->getOperand(0);
- SDValue EltNo = InVal->getOperand(1);
- auto InVecT = InVec.getValueType();
- if (ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(EltNo)) {
- SmallVector<int, 8> NewMask(InVecT.getVectorNumElements(), -1);
- int Elt = C0->getZExtValue();
- NewMask[0] = Elt;
- // If we have an implict truncate do truncate here as long as it's legal.
- // if it's not legal, this should
- if (VT.getScalarType() != InVal.getValueType() &&
- InVal.getValueType().isScalarInteger() &&
- isTypeLegal(VT.getScalarType())) {
- SDValue Val =
- DAG.getNode(ISD::TRUNCATE, SDLoc(InVal), VT.getScalarType(), InVal);
- return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), VT, Val);
- }
- if (VT.getScalarType() == InVecT.getScalarType() &&
- VT.getVectorNumElements() <= InVecT.getVectorNumElements()) {
- SDValue LegalShuffle =
- TLI.buildLegalVectorShuffle(InVecT, SDLoc(N), InVec,
- DAG.getUNDEF(InVecT), NewMask, DAG);
- if (LegalShuffle) {
- // If the initial vector is the correct size this shuffle is a
- // valid result.
- if (VT == InVecT)
- return LegalShuffle;
- // If not we must truncate the vector.
- if (VT.getVectorNumElements() != InVecT.getVectorNumElements()) {
- SDValue ZeroIdx = DAG.getVectorIdxConstant(0, SDLoc(N));
- EVT SubVT = EVT::getVectorVT(*DAG.getContext(),
- InVecT.getVectorElementType(),
- VT.getVectorNumElements());
- return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N), SubVT,
- LegalShuffle, ZeroIdx);
- }
- }
- }
+ if (Scalar.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
+ !Scalar.getOperand(0).getValueType().isFixedLengthVector())
+ return SDValue();
+
+ // If we have an implicit truncate, truncate here if it is legal.
+ if (VT.getScalarType() != Scalar.getValueType() &&
+ Scalar.getValueType().isScalarInteger() &&
+ isTypeLegal(VT.getScalarType())) {
+ SDValue Val =
+ DAG.getNode(ISD::TRUNCATE, SDLoc(Scalar), VT.getScalarType(), Scalar);
+ return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), VT, Val);
+ }
+
+ auto *ExtIndexC = dyn_cast<ConstantSDNode>(Scalar.getOperand(1));
+ if (!ExtIndexC)
+ return SDValue();
+
+ SDValue SrcVec = Scalar.getOperand(0);
+ EVT SrcVT = SrcVec.getValueType();
+ unsigned SrcNumElts = SrcVT.getVectorNumElements();
+ unsigned VTNumElts = VT.getVectorNumElements();
+ if (VT.getScalarType() == SrcVT.getScalarType() && VTNumElts <= SrcNumElts) {
+ // Create a shuffle equivalent for scalar-to-vector: {ExtIndex, -1, -1, ...}
+ SmallVector<int, 8> Mask(SrcNumElts, -1);
+ Mask[0] = ExtIndexC->getZExtValue();
+ SDValue LegalShuffle = TLI.buildLegalVectorShuffle(
+ SrcVT, SDLoc(N), SrcVec, DAG.getUNDEF(SrcVT), Mask, DAG);
+ if (!LegalShuffle)
+ return SDValue();
+
+ // If the initial vector is the same size, the shuffle is the result.
+ if (VT == SrcVT)
+ return LegalShuffle;
+
+ // If not, shorten the shuffled vector.
+ if (VTNumElts != SrcNumElts) {
+ SDValue ZeroIdx = DAG.getVectorIdxConstant(0, SDLoc(N));
+ EVT SubVT = EVT::getVectorVT(*DAG.getContext(),
+ SrcVT.getVectorElementType(), VTNumElts);
+ return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N), SubVT, LegalShuffle,
+ ZeroIdx);
}
}
More information about the llvm-commits
mailing list