Skip to content

Commit ca833d4

Browse files
committed
Replace most uses of for_each with range-for loops.
This removes a bit of complexity from the code, where it doesn't seem to be justified.
1 parent 70e2acf commit ca833d4

File tree

1 file changed

+54
-48
lines changed

1 file changed

+54
-48
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14584,13 +14584,13 @@ InstructionCost BoUpSLP::getTreeCost(ArrayRef<Value *> VectorizedVals,
1458414584
};
1458514585
if (!ValueToExtUses) {
1458614586
ValueToExtUses.emplace();
14587-
for_each(enumerate(ExternalUses), [&](const auto &P) {
14587+
for (const auto &P : enumerate(ExternalUses)) {
1458814588
// Ignore phis in loops.
1458914589
if (IsPhiInLoop(P.value()))
14590-
return;
14590+
continue;
1459114591

1459214592
ValueToExtUses->try_emplace(P.value().Scalar, P.index());
14593-
});
14593+
}
1459414594
}
1459514595
// Can use original instruction, if no operands vectorized or they are
1459614596
// marked as externally used already.
@@ -14668,13 +14668,13 @@ InstructionCost BoUpSLP::getTreeCost(ArrayRef<Value *> VectorizedVals,
1466814668
}
1466914669
if (KeepScalar) {
1467014670
ExternalUsesAsOriginalScalar.insert(EU.Scalar);
14671-
for_each(Inst->operands(), [&](Value *V) {
14671+
for (Value *V : Inst->operands()) {
1467214672
auto It = ValueToExtUses->find(V);
1467314673
if (It != ValueToExtUses->end()) {
1467414674
// Replace all uses to avoid compiler crash.
1467514675
ExternalUses[It->second].User = nullptr;
1467614676
}
14677-
});
14677+
}
1467814678
ExtraCost = ScalarCost;
1467914679
if (!IsPhiInLoop(EU))
1468014680
ExtractsCount[Entry].insert(Inst);
@@ -14683,13 +14683,13 @@ InstructionCost BoUpSLP::getTreeCost(ArrayRef<Value *> VectorizedVals,
1468314683
// Update the users of the operands of the cast operand to avoid
1468414684
// compiler crash.
1468514685
if (auto *IOp = dyn_cast<Instruction>(Inst->getOperand(0))) {
14686-
for_each(IOp->operands(), [&](Value *V) {
14686+
for (Value *V : IOp->operands()) {
1468714687
auto It = ValueToExtUses->find(V);
1468814688
if (It != ValueToExtUses->end()) {
1468914689
// Replace all uses to avoid compiler crash.
1469014690
ExternalUses[It->second].User = nullptr;
1469114691
}
14692-
});
14692+
}
1469314693
}
1469414694
}
1469514695
}
@@ -15325,7 +15325,9 @@ BoUpSLP::isGatherShuffledSingleRegisterEntry(
1532515325
// tree.
1532615326
Entries.push_back(FirstEntries.front());
1532715327
// Update mapping between values and corresponding tree entries.
15328-
for_each(UsedValuesEntry, [&](auto &P) { P.second = 0; });
15328+
for (auto &P : UsedValuesEntry) {
15329+
P.second = 0;
15330+
}
1532915331
VF = FirstEntries.front()->getVectorFactor();
1533015332
} else {
1533115333
// Try to find nodes with the same vector factor.
@@ -15375,13 +15377,13 @@ BoUpSLP::isGatherShuffledSingleRegisterEntry(
1537515377
ValuesToEntries.emplace_back().insert(E->Scalars.begin(),
1537615378
E->Scalars.end());
1537715379
// Update mapping between values and corresponding tree entries.
15378-
for_each(UsedValuesEntry, [&](auto &P) {
15380+
for (auto &P : UsedValuesEntry) {
1537915381
for (unsigned Idx : seq<unsigned>(ValuesToEntries.size()))
1538015382
if (ValuesToEntries[Idx].contains(P.first)) {
1538115383
P.second = Idx;
1538215384
break;
1538315385
}
15384-
});
15386+
}
1538515387
}
1538615388

1538715389
bool IsSplatOrUndefs = isSplat(VL) || all_of(VL, IsaPred<UndefValue>);
@@ -15527,12 +15529,12 @@ BoUpSLP::isGatherShuffledSingleRegisterEntry(
1552715529
(MaxElement % VF) -
1552815530
(MinElement % VF) + 1));
1552915531
if (NewVF < VF) {
15530-
for_each(SubMask, [&](int &Idx) {
15532+
for (int &Idx : SubMask) {
1553115533
if (Idx == PoisonMaskElem)
15532-
return;
15534+
continue;
1553315535
Idx = ((Idx % VF) - (((MinElement % VF) / NewVF) * NewVF)) % NewVF +
1553415536
(Idx >= static_cast<int>(VF) ? NewVF : 0);
15535-
});
15537+
}
1553615538
} else {
1553715539
NewVF = VF;
1553815540
}
@@ -19304,8 +19306,11 @@ BoUpSLP::BlockScheduling::tryScheduleBundle(ArrayRef<Value *> VL, BoUpSLP *SLP,
1930419306
// whole bundle might not be ready.
1930519307
ReadyInsts.remove(BundleMember);
1930619308
if (ArrayRef<ScheduleBundle *> Bundles = getScheduleBundles(V);
19307-
!Bundles.empty())
19308-
for_each(Bundles, [&](ScheduleBundle *B) { ReadyInsts.remove(B); });
19309+
!Bundles.empty()) {
19310+
for (ScheduleBundle *B : Bundles) {
19311+
ReadyInsts.remove(B);
19312+
}
19313+
}
1930919314

1931019315
if (!BundleMember->isScheduled())
1931119316
continue;
@@ -19630,23 +19635,23 @@ void BoUpSLP::BlockScheduling::calculateDependencies(ScheduleBundle &Bundle,
1963019635
}
1963119636
continue;
1963219637
}
19633-
for_each(Bundles, [&](ScheduleBundle *Bundle) {
19638+
for (ScheduleBundle *Bundle : Bundles) {
1963419639
if (!Visited.insert(Bundle).second || Bundle->hasValidDependencies())
19635-
return;
19640+
continue;
1963619641
assert(isInSchedulingRegion(*Bundle) &&
1963719642
"ScheduleData not in scheduling region");
1963819643
for_each(Bundle->getBundle(), ProcessNode);
19639-
});
19644+
}
1964019645
if (InsertInReadyList && SD->isReady()) {
19641-
for_each(Bundles, [&](ScheduleBundle *Bundle) {
19646+
for (ScheduleBundle *Bundle : Bundles) {
1964219647
assert(isInSchedulingRegion(*Bundle) &&
1964319648
"ScheduleData not in scheduling region");
1964419649
if (!Bundle->isReady())
19645-
return;
19650+
continue;
1964619651
ReadyInsts.insert(Bundle);
1964719652
LLVM_DEBUG(dbgs() << "SLP: gets ready on update: " << *Bundle
1964819653
<< "\n");
19649-
});
19654+
}
1965019655
}
1965119656
}
1965219657
}
@@ -20030,8 +20035,9 @@ bool BoUpSLP::collectValuesToDemote(
2003020035
if (Operands.empty()) {
2003120036
if (!IsTruncRoot)
2003220037
MaxDepthLevel = 1;
20033-
(void)for_each(E.Scalars, std::bind(IsPotentiallyTruncated, _1,
20034-
std::ref(BitWidth)));
20038+
for (Value *V : E.Scalars) {
20039+
(void)IsPotentiallyTruncated(V, BitWidth);
20040+
}
2003520041
} else {
2003620042
// Several vectorized uses? Check if we can truncate it, otherwise -
2003720043
// exit.
@@ -21032,17 +21038,17 @@ bool SLPVectorizerPass::vectorizeStores(
2103221038
unsigned Sz = 1 + Log2_32(MaxVF) - Log2_32(MinVF);
2103321039
SmallVector<unsigned> CandidateVFs(Sz + (NonPowerOf2VF > 0 ? 1 : 0));
2103421040
unsigned Size = MinVF;
21035-
for_each(reverse(CandidateVFs), [&](unsigned &VF) {
21041+
for (unsigned &VF : reverse(CandidateVFs)) {
2103621042
VF = Size > MaxVF ? NonPowerOf2VF : Size;
2103721043
Size *= 2;
21038-
});
21044+
}
2103921045
unsigned End = Operands.size();
2104021046
unsigned Repeat = 0;
2104121047
constexpr unsigned MaxAttempts = 4;
2104221048
OwningArrayRef<std::pair<unsigned, unsigned>> RangeSizes(Operands.size());
21043-
for_each(RangeSizes, [](std::pair<unsigned, unsigned> &P) {
21049+
for (std::pair<unsigned, unsigned> &P : RangeSizes) {
2104421050
P.first = P.second = 1;
21045-
});
21051+
}
2104621052
DenseMap<Value *, std::pair<unsigned, unsigned>> NonSchedulable;
2104721053
auto IsNotVectorized = [](bool First,
2104821054
const std::pair<unsigned, unsigned> &P) {
@@ -21118,22 +21124,22 @@ bool SLPVectorizerPass::vectorizeStores(
2111821124
AnyProfitableGraph = RepeatChanged = Changed = true;
2111921125
// If we vectorized initial block, no need to try to vectorize
2112021126
// it again.
21121-
for_each(RangeSizes.slice(Cnt, Size),
21122-
[](std::pair<unsigned, unsigned> &P) {
21123-
P.first = P.second = 0;
21124-
});
21127+
for (std::pair<unsigned, unsigned> &P :
21128+
RangeSizes.slice(Cnt, Size)) {
21129+
P.first = P.second = 0;
21130+
}
2112521131
if (Cnt < StartIdx + MinVF) {
21126-
for_each(RangeSizes.slice(StartIdx, Cnt - StartIdx),
21127-
[](std::pair<unsigned, unsigned> &P) {
21128-
P.first = P.second = 0;
21129-
});
21132+
for (std::pair<unsigned, unsigned> &P :
21133+
RangeSizes.slice(StartIdx, Cnt - StartIdx)) {
21134+
P.first = P.second = 0;
21135+
}
2113021136
StartIdx = Cnt + Size;
2113121137
}
2113221138
if (Cnt > Sz - Size - MinVF) {
21133-
for_each(RangeSizes.slice(Cnt + Size, Sz - (Cnt + Size)),
21134-
[](std::pair<unsigned, unsigned> &P) {
21135-
P.first = P.second = 0;
21136-
});
21139+
for (std::pair<unsigned, unsigned> &P :
21140+
RangeSizes.slice(Cnt + Size, Sz - (Cnt + Size))) {
21141+
P.first = P.second = 0;
21142+
}
2113721143
if (Sz == End)
2113821144
End = Cnt;
2113921145
Sz = Cnt;
@@ -21159,13 +21165,13 @@ bool SLPVectorizerPass::vectorizeStores(
2115921165
continue;
2116021166
}
2116121167
if (TreeSize > 1)
21162-
for_each(RangeSizes.slice(Cnt, Size),
21163-
[&](std::pair<unsigned, unsigned> &P) {
21164-
if (Size >= MaxRegVF)
21165-
P.second = std::max(P.second, TreeSize);
21166-
else
21167-
P.first = std::max(P.first, TreeSize);
21168-
});
21168+
for (std::pair<unsigned, unsigned> &P :
21169+
RangeSizes.slice(Cnt, Size)) {
21170+
if (Size >= MaxRegVF)
21171+
P.second = std::max(P.second, TreeSize);
21172+
else
21173+
P.first = std::max(P.first, TreeSize);
21174+
}
2116921175
++Cnt;
2117021176
AnyProfitableGraph = true;
2117121177
}
@@ -21207,10 +21213,10 @@ bool SLPVectorizerPass::vectorizeStores(
2120721213
CandidateVFs.push_back(Limit);
2120821214
if (VF > MaxTotalNum || VF >= StoresLimit)
2120921215
break;
21210-
for_each(RangeSizes, [&](std::pair<unsigned, unsigned> &P) {
21216+
for (std::pair<unsigned, unsigned> &P : RangeSizes) {
2121121217
if (P.first != 0)
2121221218
P.first = std::max(P.second, P.first);
21213-
});
21219+
}
2121421220
// Last attempt to vectorize max number of elements, if all previous
2121521221
// attempts were unsuccessful because of the cost issues.
2121621222
CandidateVFs.push_back(VF);

0 commit comments

Comments
 (0)