aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Scalar/LoopDistribute.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2023-02-11 12:38:04 +0000
committerDimitry Andric <dim@FreeBSD.org>2023-02-11 12:38:11 +0000
commite3b557809604d036af6e00c60f012c2025b59a5e (patch)
tree8a11ba2269a3b669601e2fd41145b174008f4da8 /llvm/lib/Transforms/Scalar/LoopDistribute.cpp
parent08e8dd7b9db7bb4a9de26d44c1cbfd24e869c014 (diff)
Diffstat (limited to 'llvm/lib/Transforms/Scalar/LoopDistribute.cpp')
-rw-r--r--llvm/lib/Transforms/Scalar/LoopDistribute.cpp85
1 files changed, 33 insertions, 52 deletions
diff --git a/llvm/lib/Transforms/Scalar/LoopDistribute.cpp b/llvm/lib/Transforms/Scalar/LoopDistribute.cpp
index b178bcae3b0e..7b52b7dca85f 100644
--- a/llvm/lib/Transforms/Scalar/LoopDistribute.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopDistribute.cpp
@@ -25,7 +25,6 @@
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/ADT/EquivalenceClasses.h"
-#include "llvm/ADT/Optional.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
@@ -397,7 +396,7 @@ public:
continue;
auto PartI = I->getData();
- for (auto PartJ : make_range(std::next(ToBeMerged.member_begin(I)),
+ for (auto *PartJ : make_range(std::next(ToBeMerged.member_begin(I)),
ToBeMerged.member_end())) {
PartJ->moveTo(*PartI);
}
@@ -461,16 +460,14 @@ public:
// update PH to point to the newly added preheader.
BasicBlock *TopPH = OrigPH;
unsigned Index = getSize() - 1;
- for (auto I = std::next(PartitionContainer.rbegin()),
- E = PartitionContainer.rend();
- I != E; ++I, --Index, TopPH = NewLoop->getLoopPreheader()) {
- auto *Part = &*I;
-
- NewLoop = Part->cloneLoopWithPreheader(TopPH, Pred, Index, LI, DT);
-
- Part->getVMap()[ExitBlock] = TopPH;
- Part->remapInstructions();
- setNewLoopID(OrigLoopID, Part);
+ for (auto &Part : llvm::drop_begin(llvm::reverse(PartitionContainer))) {
+ NewLoop = Part.cloneLoopWithPreheader(TopPH, Pred, Index, LI, DT);
+
+ Part.getVMap()[ExitBlock] = TopPH;
+ Part.remapInstructions();
+ setNewLoopID(OrigLoopID, &Part);
+ --Index;
+ TopPH = NewLoop->getLoopPreheader();
}
Pred->getTerminator()->replaceUsesOfWith(OrigPH, TopPH);
@@ -595,14 +592,14 @@ private:
/// Assign new LoopIDs for the partition's cloned loop.
void setNewLoopID(MDNode *OrigLoopID, InstPartition *Part) {
- Optional<MDNode *> PartitionID = makeFollowupLoopID(
+ std::optional<MDNode *> PartitionID = makeFollowupLoopID(
OrigLoopID,
{LLVMLoopDistributeFollowupAll,
Part->hasDepCycle() ? LLVMLoopDistributeFollowupSequential
: LLVMLoopDistributeFollowupCoincident});
if (PartitionID) {
Loop *NewLoop = Part->getDistributedLoop();
- NewLoop->setLoopID(PartitionID.value());
+ NewLoop->setLoopID(*PartitionID);
}
}
};
@@ -635,7 +632,7 @@ public:
Accesses.append(Instructions.begin(), Instructions.end());
LLVM_DEBUG(dbgs() << "Backward dependences:\n");
- for (auto &Dep : Dependences)
+ for (const auto &Dep : Dependences)
if (Dep.isPossiblyBackward()) {
// Note that the designations source and destination follow the program
// order, i.e. source is always first. (The direction is given by the
@@ -655,13 +652,14 @@ private:
class LoopDistributeForLoop {
public:
LoopDistributeForLoop(Loop *L, Function *F, LoopInfo *LI, DominatorTree *DT,
- ScalarEvolution *SE, OptimizationRemarkEmitter *ORE)
- : L(L), F(F), LI(LI), DT(DT), SE(SE), ORE(ORE) {
+ ScalarEvolution *SE, LoopAccessInfoManager &LAIs,
+ OptimizationRemarkEmitter *ORE)
+ : L(L), F(F), LI(LI), DT(DT), SE(SE), LAIs(LAIs), ORE(ORE) {
setForced();
}
/// Try to distribute an inner-most loop.
- bool processLoop(std::function<const LoopAccessInfo &(Loop &)> &GetLAA) {
+ bool processLoop() {
assert(L->isInnermost() && "Only process inner loops.");
LLVM_DEBUG(dbgs() << "\nLDist: In \""
@@ -679,7 +677,7 @@ public:
BasicBlock *PH = L->getLoopPreheader();
- LAI = &GetLAA(*L);
+ LAI = &LAIs.getInfo(*L);
// Currently, we only distribute to isolate the part of the loop with
// dependence cycles to enable partial vectorization.
@@ -717,7 +715,7 @@ public:
*Dependences);
int NumUnsafeDependencesActive = 0;
- for (auto &InstDep : MID) {
+ for (const auto &InstDep : MID) {
Instruction *I = InstDep.Inst;
// We update NumUnsafeDependencesActive post-instruction, catch the
// start of a dependence directly via NumUnsafeDependencesStartOrEnd.
@@ -821,12 +819,10 @@ public:
// The unversioned loop will not be changed, so we inherit all attributes
// from the original loop, but remove the loop distribution metadata to
// avoid to distribute it again.
- MDNode *UnversionedLoopID =
- makeFollowupLoopID(OrigLoopID,
- {LLVMLoopDistributeFollowupAll,
- LLVMLoopDistributeFollowupFallback},
- "llvm.loop.distribute.", true)
- .value();
+ MDNode *UnversionedLoopID = *makeFollowupLoopID(
+ OrigLoopID,
+ {LLVMLoopDistributeFollowupAll, LLVMLoopDistributeFollowupFallback},
+ "llvm.loop.distribute.", true);
LVer.getNonVersionedLoop()->setLoopID(UnversionedLoopID);
}
@@ -893,7 +889,7 @@ public:
/// If the optional has a value, it indicates whether distribution was forced
/// to be enabled (true) or disabled (false). If the optional has no value
/// distribution was not forced either way.
- const Optional<bool> &isForced() const { return IsForced; }
+ const std::optional<bool> &isForced() const { return IsForced; }
private:
/// Filter out checks between pointers from the same partition.
@@ -937,7 +933,7 @@ private:
/// Check whether the loop metadata is forcing distribution to be
/// enabled/disabled.
void setForced() {
- Optional<const MDOperand *> Value =
+ std::optional<const MDOperand *> Value =
findStringMetadataForLoop(L, "llvm.loop.distribute.enable");
if (!Value)
return;
@@ -955,6 +951,7 @@ private:
const LoopAccessInfo *LAI = nullptr;
DominatorTree *DT;
ScalarEvolution *SE;
+ LoopAccessInfoManager &LAIs;
OptimizationRemarkEmitter *ORE;
/// Indicates whether distribution is forced to be enabled/disabled for
@@ -963,7 +960,7 @@ private:
/// If the optional has a value, it indicates whether distribution was forced
/// to be enabled (true) or disabled (false). If the optional has no value
/// distribution was not forced either way.
- Optional<bool> IsForced;
+ std::optional<bool> IsForced;
};
} // end anonymous namespace
@@ -971,7 +968,7 @@ private:
/// Shared implementation between new and old PMs.
static bool runImpl(Function &F, LoopInfo *LI, DominatorTree *DT,
ScalarEvolution *SE, OptimizationRemarkEmitter *ORE,
- std::function<const LoopAccessInfo &(Loop &)> &GetLAA) {
+ LoopAccessInfoManager &LAIs) {
// Build up a worklist of inner-loops to vectorize. This is necessary as the
// act of distributing a loop creates new loops and can invalidate iterators
// across the loops.
@@ -986,12 +983,12 @@ static bool runImpl(Function &F, LoopInfo *LI, DominatorTree *DT,
// Now walk the identified inner loops.
bool Changed = false;
for (Loop *L : Worklist) {
- LoopDistributeForLoop LDL(L, &F, LI, DT, SE, ORE);
+ LoopDistributeForLoop LDL(L, &F, LI, DT, SE, LAIs, ORE);
// If distribution was forced for the specific loop to be
// enabled/disabled, follow that. Otherwise use the global flag.
if (LDL.isForced().value_or(EnableLoopDistribute))
- Changed |= LDL.processLoop(GetLAA);
+ Changed |= LDL.processLoop();
}
// Process each loop nest in the function.
@@ -1015,14 +1012,12 @@ public:
return false;
auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
- auto *LAA = &getAnalysis<LoopAccessLegacyAnalysis>();
auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
auto *ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
- std::function<const LoopAccessInfo &(Loop &)> GetLAA =
- [&](Loop &L) -> const LoopAccessInfo & { return LAA->getInfo(&L); };
+ auto &LAIs = getAnalysis<LoopAccessLegacyAnalysis>().getLAIs();
- return runImpl(F, LI, DT, SE, ORE, GetLAA);
+ return runImpl(F, LI, DT, SE, ORE, LAIs);
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
@@ -1046,22 +1041,8 @@ PreservedAnalyses LoopDistributePass::run(Function &F,
auto &SE = AM.getResult<ScalarEvolutionAnalysis>(F);
auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
- // We don't directly need these analyses but they're required for loop
- // analyses so provide them below.
- auto &AA = AM.getResult<AAManager>(F);
- auto &AC = AM.getResult<AssumptionAnalysis>(F);
- auto &TTI = AM.getResult<TargetIRAnalysis>(F);
- auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
-
- auto &LAM = AM.getResult<LoopAnalysisManagerFunctionProxy>(F).getManager();
- std::function<const LoopAccessInfo &(Loop &)> GetLAA =
- [&](Loop &L) -> const LoopAccessInfo & {
- LoopStandardAnalysisResults AR = {AA, AC, DT, LI, SE,
- TLI, TTI, nullptr, nullptr, nullptr};
- return LAM.getResult<LoopAccessAnalysis>(L, AR);
- };
-
- bool Changed = runImpl(F, &LI, &DT, &SE, &ORE, GetLAA);
+ LoopAccessInfoManager &LAIs = AM.getResult<LoopAccessAnalysis>(F);
+ bool Changed = runImpl(F, &LI, &DT, &SE, &ORE, LAIs);
if (!Changed)
return PreservedAnalyses::all();
PreservedAnalyses PA;