[llvm] 26ba308 - [SLP][NFC] Simplify range/type accessors
via llvm-commits
llvm-commits at lists.llvm.org
Sat Apr 18 15:02:57 PDT 2026
Author: Alexey Bataev
Date: 2026-04-18T18:02:53-04:00
New Revision: 26ba308be0eeb652ecbc0ad3c660ef69404c0185
URL: https://github.com/llvm/llvm-project/commit/26ba308be0eeb652ecbc0ad3c660ef69404c0185
DIFF: https://github.com/llvm/llvm-project/commit/26ba308be0eeb652ecbc0ad3c660ef69404c0185.diff
LOG: [SLP][NFC] Simplify range/type accessors
Use the range-friendly llvm::accumulate() overload instead of the
two-iterator std::accumulate() form, drop redundant cast<>/dyn_cast<>
calls where the source already provides a typed accessor.
Reviewers:
Pull Request: https://github.com/llvm/llvm-project/pull/192811
Added:
Modified:
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 6625384616c26..dd9cd8b6a7307 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -834,16 +834,15 @@ isFixedVectorShuffle(ArrayRef<Value *> VL, SmallVectorImpl<int> &Mask,
const auto *It = find_if(VL, IsaPred<ExtractElementInst>);
if (It == VL.end())
return std::nullopt;
- unsigned Size =
- std::accumulate(VL.begin(), VL.end(), 0u, [](unsigned S, Value *V) {
- auto *EI = dyn_cast<ExtractElementInst>(V);
- if (!EI)
- return S;
- auto *VTy = dyn_cast<FixedVectorType>(EI->getVectorOperandType());
- if (!VTy)
- return S;
- return std::max(S, VTy->getNumElements());
- });
+ unsigned Size = accumulate(VL, 0u, [](unsigned S, Value *V) {
+ auto *EI = dyn_cast<ExtractElementInst>(V);
+ if (!EI)
+ return S;
+ auto *VTy = dyn_cast<FixedVectorType>(EI->getVectorOperandType());
+ if (!VTy)
+ return S;
+ return std::max(S, VTy->getNumElements());
+ });
Value *Vec1 = nullptr;
Value *Vec2 = nullptr;
@@ -6743,7 +6742,7 @@ BoUpSLP::findReusedOrderedScalars(const BoUpSLP::TreeEntry &TE,
auto *EI = dyn_cast<ExtractElementInst>(TE.Scalars[K]);
if (!EI)
continue;
- VF = std::max(VF, cast<VectorType>(EI->getVectorOperandType())
+ VF = std::max(VF, EI->getVectorOperandType()
->getElementCount()
.getKnownMinValue());
}
@@ -7182,11 +7181,11 @@ static bool isMaskedLoadCompress(
Instruction::Load, CostKind, ScalarTy, LoadVecTy);
// The cost of scalar loads.
InstructionCost ScalarLoadsCost =
- std::accumulate(VL.begin(), VL.end(), InstructionCost(),
- [&](InstructionCost C, Value *V) {
- return C + TTI.getInstructionCost(cast<Instruction>(V),
- CostKind);
- }) +
+ accumulate(VL, InstructionCost(),
+ [&](InstructionCost C, Value *V) {
+ return C + TTI.getInstructionCost(cast<Instruction>(V),
+ CostKind);
+ }) +
ScalarGEPCost;
APInt DemandedElts = APInt::getAllOnes(Sz);
InstructionCost GatherCost =
@@ -7741,11 +7740,11 @@ BoUpSLP::LoadsState BoUpSLP::canVectorizeLoads(
::getShuffleCost(TTI, TTI::SK_Broadcast, PtrVecTy, {}, CostKind);
// The cost of scalar loads.
InstructionCost ScalarLoadsCost =
- std::accumulate(VL.begin(), VL.end(), InstructionCost(),
- [&](InstructionCost C, Value *V) {
- return C + TTI.getInstructionCost(
- cast<Instruction>(V), CostKind);
- }) +
+ accumulate(VL, InstructionCost(),
+ [&](InstructionCost C, Value *V) {
+ return C + TTI.getInstructionCost(cast<Instruction>(V),
+ CostKind);
+ }) +
ScalarGEPCost;
// The cost of masked gather.
InstructionCost MaskedGatherCost =
@@ -8087,8 +8086,7 @@ static bool areTwoInsertFromSameBuildVector(
// Go through the vector operand of insertelement instructions trying to find
// either VU as the original vector for IE2 or V as the original vector for
// IE1.
- SmallBitVector ReusedIdx(
- cast<VectorType>(VU->getType())->getElementCount().getKnownMinValue());
+ SmallBitVector ReusedIdx(VU->getType()->getElementCount().getKnownMinValue());
bool IsReusedIdx = false;
do {
if (IE2 == VU && !IE1)
@@ -10284,8 +10282,8 @@ void BoUpSLP::tryToVectorizeGatheredLoads(
const auto &Ref = GLs.second;
SmallVector<LoadInst *> NonVectorized = ProcessGatheredLoads(Ref);
if (!Ref.empty() && !NonVectorized.empty() &&
- std::accumulate(
- Ref.begin(), Ref.end(), 0u,
+ accumulate(
+ Ref, 0u,
[](unsigned S, ArrayRef<std::pair<LoadInst *, int64_t>> LoadsDists)
-> unsigned { return S + LoadsDists.size(); }) !=
NonVectorized.size() &&
@@ -14958,16 +14956,15 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
ArrayRef<std::optional<TTI::ShuffleKind>> ShuffleKinds,
unsigned NumParts) {
assert(VL.size() > NumParts && "Unexpected scalarized shuffle.");
- unsigned NumElts =
- std::accumulate(VL.begin(), VL.end(), 0, [](unsigned Sz, Value *V) {
- auto *EE = dyn_cast<ExtractElementInst>(V);
- if (!EE)
- return Sz;
- auto *VecTy = dyn_cast<FixedVectorType>(EE->getVectorOperandType());
- if (!VecTy)
- return Sz;
- return std::max(Sz, VecTy->getNumElements());
- });
+ unsigned NumElts = accumulate(VL, 0, [](unsigned Sz, Value *V) {
+ auto *EE = dyn_cast<ExtractElementInst>(V);
+ if (!EE)
+ return Sz;
+ auto *VecTy = dyn_cast<FixedVectorType>(EE->getVectorOperandType());
+ if (!VecTy)
+ return Sz;
+ return std::max(Sz, VecTy->getNumElements());
+ });
// FIXME: this must be moved to TTI for better estimation.
unsigned EltsPerVector = getPartNumElems(VL.size(), NumParts);
auto CheckPerRegistersShuffle = [&](MutableArrayRef<int> Mask,
@@ -14976,14 +14973,13 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
-> std::optional<TTI::ShuffleKind> {
if (NumElts <= EltsPerVector)
return std::nullopt;
- int OffsetReg0 =
- alignDown(std::accumulate(Mask.begin(), Mask.end(), INT_MAX,
- [](int S, int I) {
- if (I == PoisonMaskElem)
- return S;
- return std::min(S, I);
- }),
- EltsPerVector);
+ int OffsetReg0 = alignDown(accumulate(Mask, INT_MAX,
+ [](int S, int I) {
+ if (I == PoisonMaskElem)
+ return S;
+ return std::min(S, I);
+ }),
+ EltsPerVector);
int OffsetReg1 = OffsetReg0;
DenseSet<int> RegIndices;
// Check that if trying to permute same single/2 input vectors.
@@ -18753,10 +18749,9 @@ InstructionCost BoUpSLP::getTreeCost(InstructionCost TreeCost,
[&](User *U) {
auto *PHIUser = dyn_cast<PHINode>(U);
return (!PHIUser ||
- PHIUser->getParent() !=
- cast<Instruction>(
- VectorizableTree.front()->getMainOp())
- ->getParent()) &&
+ PHIUser->getParent() != VectorizableTree.front()
+ ->getMainOp()
+ ->getParent()) &&
!isVectorized(U);
}) &&
count_if(Entry->Scalars, [&](Value *V) {
@@ -20704,8 +20699,8 @@ class BoUpSLP::ShuffleInstructionBuilder final : public BaseShuffleAnalysis {
constexpr int MaxBases = 2;
SmallVector<Value *, MaxBases> Bases(MaxBases);
auto VLMask = zip(SubVL, SubMask);
- const unsigned VF = std::accumulate(
- VLMask.begin(), VLMask.end(), 0U, [&](unsigned S, const auto &D) {
+ const unsigned VF =
+ accumulate(VLMask, 0U, [&](unsigned S, const auto &D) {
if (std::get<1>(D) == PoisonMaskElem)
return S;
Value *VecOp =
@@ -22386,10 +22381,8 @@ Value *BoUpSLP::vectorizeTree(TreeEntry *E) {
assert(SLPReVec && "FixedVectorType is not expected.");
// CreateMaskedGather expects VecTy and VecPtr have same size. We need
// to expand VecPtr if ScalarTy is a vector type.
- unsigned ScalarTyNumElements =
- cast<FixedVectorType>(ScalarTy)->getNumElements();
- unsigned VecTyNumElements =
- cast<FixedVectorType>(VecTy)->getNumElements();
+ unsigned ScalarTyNumElements = getNumElements(ScalarTy);
+ unsigned VecTyNumElements = getNumElements(VecTy);
assert(VecTyNumElements % ScalarTyNumElements == 0 &&
"Cannot expand getelementptr.");
unsigned VF = VecTyNumElements / ScalarTyNumElements;
@@ -23383,7 +23376,7 @@ Value *BoUpSLP::vectorizeTree(
if (*It == II)
++It;
else
- Inserts.push_back(cast<Instruction>(II));
+ Inserts.push_back(II);
II = dyn_cast<InsertElementInst>(II->getOperand(0));
}
for (Instruction *II : reverse(Inserts)) {
@@ -26226,18 +26219,18 @@ bool StoreChainContext::checkTreeSizes(const unsigned SliceStartIdx,
uint64_t Mean = Sum / Num;
if (Mean == 0)
return true;
- uint64_t Dev = std::accumulate(
- Sizes.begin(), Sizes.end(), static_cast<uint64_t>(0),
- [&](uint64_t V, const std::pair<unsigned, unsigned> &Val) {
- unsigned P =
- Val.first == StoreChainContext::LocallyUnvectorizable
- ? 0
- : RangeSizesByIdx[Val.first];
- if (P == 1)
- return V;
- return V + (P - Mean) * (P - Mean);
- }) /
- Num;
+ uint64_t Dev =
+ accumulate(Sizes, static_cast<uint64_t>(0),
+ [&](uint64_t V, const std::pair<unsigned, unsigned> &Val) {
+ unsigned P =
+ Val.first == StoreChainContext::LocallyUnvectorizable
+ ? 0
+ : RangeSizesByIdx[Val.first];
+ if (P == 1)
+ return V;
+ return V + (P - Mean) * (P - Mean);
+ }) /
+ Num;
return Dev * 96 / (Mean * Mean) == 0;
}
@@ -27500,17 +27493,19 @@ class HorizontalReduction {
// If there are a sufficient number of reduction values, reduce
// to a nearby power-of-2. We can safely generate oversized
// vectors and rely on the backend to split them to legal sizes.
- if (unsigned NumReducedVals = std::accumulate(
- ReducedVals.begin(), ReducedVals.end(), 0,
- [](unsigned Num, ArrayRef<Value *> Vals) -> unsigned {
- if (!isGoodForReduction(Vals))
- return Num;
- return Num + Vals.size();
- });
+ if (unsigned NumReducedVals =
+ accumulate(ReducedVals, 0,
+ [](unsigned Num, ArrayRef<Value *> Vals) -> unsigned {
+ if (!isGoodForReduction(Vals))
+ return Num;
+ return Num + Vals.size();
+ });
NumReducedVals < ReductionLimit &&
- all_of(ReducedVals, [](ArrayRef<Value *> RedV) {
- return RedV.size() < 2 || !allConstant(RedV) || !isSplat(RedV);
- })) {
+ all_of(
+ ReducedVals,
+ [](ArrayRef<Value *> RedV) {
+ return RedV.size() < 2 || !allConstant(RedV) || !isSplat(RedV);
+ })) {
for (ReductionOpsType &RdxOps : ReductionOps)
for (Value *RdxOp : RdxOps)
V.analyzedReductionRoot(cast<Instruction>(RdxOp));
More information about the llvm-commits
mailing list