[llvm] 5797ed6 - [GISel][SDAG] Avoid push_back in loops for some shuffle mask handling. (#119434)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 10 22:18:49 PST 2024
Author: Craig Topper
Date: 2024-12-10T22:18:46-08:00
New Revision: 5797ed660ab0c1847bd6229af262f841b2f753ae
URL: https://github.com/llvm/llvm-project/commit/5797ed660ab0c1847bd6229af262f841b2f753ae
DIFF: https://github.com/llvm/llvm-project/commit/5797ed660ab0c1847bd6229af262f841b2f753ae.diff
LOG: [GISel][SDAG] Avoid push_back in loops for some shuffle mask handling. (#119434)
Each call to push_back contains a check to see if the vector needs to
grow. Using resize or giving the size to the constructor can reduce
the number of checks for growing.
Added:
Modified:
llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
Removed:
################################################################################
diff --git a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
index 2544f2479ddc68..f56be39838ba7d 100644
--- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
@@ -6172,9 +6172,8 @@ LegalizerHelper::equalizeVectorShuffleLengths(MachineInstr &MI) {
if (MaskNumElts < SrcNumElts) {
// Extend mask to match new destination vector size with
// undef values.
- SmallVector<int, 16> NewMask(Mask);
- for (unsigned I = MaskNumElts; I < SrcNumElts; ++I)
- NewMask.push_back(-1);
+ SmallVector<int, 16> NewMask(SrcNumElts, -1);
+ llvm::copy(Mask, NewMask.begin());
moreElementsVectorDst(MI, SrcTy, 0);
MIRBuilder.setInstrAndDebugLoc(MI);
@@ -6254,16 +6253,14 @@ LegalizerHelper::moreElementsVectorShuffle(MachineInstr &MI,
moreElementsVectorSrc(MI, MoreTy, 2);
// Adjust mask based on new input vector length.
- SmallVector<int, 16> NewMask;
+ SmallVector<int, 16> NewMask(WidenNumElts, -1);
for (unsigned I = 0; I != NumElts; ++I) {
int Idx = Mask[I];
if (Idx < static_cast<int>(NumElts))
- NewMask.push_back(Idx);
+ NewMask[I] = Idx;
else
- NewMask.push_back(Idx - NumElts + WidenNumElts);
+ NewMask[I] = Idx - NumElts + WidenNumElts;
}
- for (unsigned I = NumElts; I != WidenNumElts; ++I)
- NewMask.push_back(-1);
moreElementsVectorDst(MI, MoreTy, 0);
MIRBuilder.setInstrAndDebugLoc(MI);
MIRBuilder.buildShuffleVector(MI.getOperand(0).getReg(),
diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
index 465128099f4447..107454a92e356c 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
@@ -6421,16 +6421,14 @@ SDValue DAGTypeLegalizer::WidenVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N) {
SDValue InOp2 = GetWidenedVector(N->getOperand(1));
// Adjust mask based on new input vector length.
- SmallVector<int, 16> NewMask;
+ SmallVector<int, 16> NewMask(WidenNumElts, -1);
for (unsigned i = 0; i != NumElts; ++i) {
int Idx = N->getMaskElt(i);
if (Idx < (int)NumElts)
- NewMask.push_back(Idx);
+ NewMask[i] = Idx;
else
- NewMask.push_back(Idx - NumElts + WidenNumElts);
+ NewMask[i] = Idx - NumElts + WidenNumElts;
}
- for (unsigned i = NumElts; i != WidenNumElts; ++i)
- NewMask.push_back(-1);
return DAG.getVectorShuffle(WidenVT, dl, InOp1, InOp2, NewMask);
}
@@ -6478,12 +6476,8 @@ SDValue DAGTypeLegalizer::WidenVecRes_VECTOR_REVERSE(SDNode *N) {
// Use VECTOR_SHUFFLE to combine new vector from 'ReverseVal' for
// fixed-vectors.
- SmallVector<int, 16> Mask;
- for (unsigned i = 0; i != VTNumElts; ++i) {
- Mask.push_back(IdxVal + i);
- }
- for (unsigned i = VTNumElts; i != WidenNumElts; ++i)
- Mask.push_back(-1);
+ SmallVector<int, 16> Mask(WidenNumElts, -1);
+ std::iota(Mask.begin(), Mask.begin() + VTNumElts, IdxVal);
return DAG.getVectorShuffle(WidenVT, dl, ReverseVal, DAG.getUNDEF(WidenVT),
Mask);
More information about the llvm-commits
mailing list