diff options
| author | Florian Hahn <florian.hahn@arm.com> | 2018-09-28 09:45:50 +0000 |
|---|---|---|
| committer | Florian Hahn <florian.hahn@arm.com> | 2018-09-28 09:45:50 +0000 |
| commit | 0694c159f74da4ecf9363ace149cc8f95d08d576 (patch) | |
| tree | c8a25ff50da3b2e25a3aa8fb975e6faf8a53ba61 | |
| parent | 417ef40c3954f0a6fa13028e44962a69ee92513a (diff) | |
| download | bcm5719-llvm-0694c159f74da4ecf9363ace149cc8f95d08d576.tar.gz bcm5719-llvm-0694c159f74da4ecf9363ace149cc8f95d08d576.zip | |
[LoopInterchange] Turn into a loop pass.
This patch turns LoopInterchange into a loop pass. It now only
considers top-level loops and tries to move the innermost loop to the
optimal position within the loop nest. By only looking at top-level
loops, we might miss a few opportunities the function pass would get
(e.g. if we have a loop nest of 3 loops, in the function pass
we might process loops at level 1 and 2 and move the inner most loop to
level 1, and then we process loops at levels 0, 1, 2 and interchange
again, because we now have a different inner loop). But I think it would
be better to handle such cases by picking the best inner loop from the
start and avoid re-visiting the same loops again.
The biggest advantage of it being a function pass is that it interacts
nicely with the other loop passes. Without this patch, there are some
performance regressions on AArch64 with loop interchanging enabled,
where no loops were interchanged, but we missed out on some other loop
optimizations.
It also removes the SimplifyCFG run. We are just changing branches, so
the CFG should not be more complicated, besides the additional 'unique'
preheaders this pass might create.
Reviewers: chandlerc, efriedma, mcrosier, javed.absar, xbolva00
Reviewed By: xbolva00
Differential Revision: https://reviews.llvm.org/D51702
llvm-svn: 343308
| -rw-r--r-- | llvm/lib/Transforms/IPO/PassManagerBuilder.cpp | 6 | ||||
| -rw-r--r-- | llvm/lib/Transforms/Scalar/LoopInterchange.cpp | 57 |
2 files changed, 16 insertions, 47 deletions
diff --git a/llvm/lib/Transforms/IPO/PassManagerBuilder.cpp b/llvm/lib/Transforms/IPO/PassManagerBuilder.cpp index 62eb12c37a2..ac4abc89c76 100644 --- a/llvm/lib/Transforms/IPO/PassManagerBuilder.cpp +++ b/llvm/lib/Transforms/IPO/PassManagerBuilder.cpp @@ -375,11 +375,9 @@ void PassManagerBuilder::addFunctionSimplificationPasses( addExtensionsToPM(EP_LateLoopOptimizations, MPM); MPM.add(createLoopDeletionPass()); // Delete dead loops - if (EnableLoopInterchange) { - // FIXME: These are function passes and break the loop pass pipeline. + if (EnableLoopInterchange) MPM.add(createLoopInterchangePass()); // Interchange loops - MPM.add(createCFGSimplificationPass()); - } + if (!DisableUnrollLoops) MPM.add(createSimpleLoopUnrollPass(OptLevel)); // Unroll small loops addExtensionsToPM(EP_LoopOptimizerEnd, MPM); diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp index 3be41646741..586b7ce6ed0 100644 --- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp +++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp @@ -17,9 +17,9 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/Statistic.h" #include "llvm/ADT/StringRef.h" -#include "llvm/Analysis/AliasAnalysis.h" #include "llvm/Analysis/DependenceAnalysis.h" #include "llvm/Analysis/LoopInfo.h" +#include "llvm/Analysis/LoopPass.h" #include "llvm/Analysis/OptimizationRemarkEmitter.h" #include "llvm/Analysis/ScalarEvolution.h" #include "llvm/Analysis/ScalarEvolutionExpressions.h" @@ -271,7 +271,7 @@ static bool isLegalToInterChangeLoops(CharMatrix &DepMatrix, return true; } -static void populateWorklist(Loop &L, SmallVector<LoopVector, 8> &V) { +static LoopVector populateWorklist(Loop &L) { LLVM_DEBUG(dbgs() << "Calling populateWorklist on Func: " << L.getHeader()->getParent()->getName() << " Loop: %" << L.getHeader()->getName() << '\n'); @@ -282,16 +282,15 @@ static void populateWorklist(Loop &L, SmallVector<LoopVector, 8> &V) { // The current loop has multiple subloops in it hence it is not tightly // nested. // Discard all loops above it added into Worklist. - if (Vec->size() != 1) { - LoopList.clear(); - return; - } + if (Vec->size() != 1) + return {}; + LoopList.push_back(CurrentLoop); CurrentLoop = Vec->front(); Vec = &CurrentLoop->getSubLoops(); } LoopList.push_back(CurrentLoop); - V.push_back(std::move(LoopList)); + return LoopList; } static PHINode *getInductionVariable(Loop *L, ScalarEvolution *SE) { @@ -425,7 +424,7 @@ private: }; // Main LoopInterchange Pass. -struct LoopInterchange : public FunctionPass { +struct LoopInterchange : public LoopPass { static char ID; ScalarEvolution *SE = nullptr; LoopInfo *LI = nullptr; @@ -436,50 +435,27 @@ struct LoopInterchange : public FunctionPass { /// Interface to emit optimization remarks. OptimizationRemarkEmitter *ORE; - LoopInterchange() : FunctionPass(ID) { + LoopInterchange() : LoopPass(ID) { initializeLoopInterchangePass(*PassRegistry::getPassRegistry()); } void getAnalysisUsage(AnalysisUsage &AU) const override { - AU.addRequired<ScalarEvolutionWrapperPass>(); - AU.addRequired<AAResultsWrapperPass>(); - AU.addRequired<DominatorTreeWrapperPass>(); - AU.addRequired<LoopInfoWrapperPass>(); AU.addRequired<DependenceAnalysisWrapperPass>(); - AU.addRequiredID(LoopSimplifyID); - AU.addRequiredID(LCSSAID); AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); - AU.addPreserved<DominatorTreeWrapperPass>(); - AU.addPreserved<LoopInfoWrapperPass>(); - AU.addPreserved<ScalarEvolutionWrapperPass>(); - AU.addPreservedID(LCSSAID); + getLoopAnalysisUsage(AU); } - bool runOnFunction(Function &F) override { - if (skipFunction(F)) - return false; + bool runOnLoop(Loop *L, LPPassManager &LPM) override { + if (skipLoop(L) || L->getParentLoop()) SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); DI = &getAnalysis<DependenceAnalysisWrapperPass>().getDI(); DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); - PreserveLCSSA = mustPreserveAnalysisID(LCSSAID); - // Build up a worklist of loop pairs to analyze. - SmallVector<LoopVector, 8> Worklist; - - for (Loop *L : *LI) - populateWorklist(*L, Worklist); - - LLVM_DEBUG(dbgs() << "Worklist size = " << Worklist.size() << "\n"); - bool Changed = true; - while (!Worklist.empty()) { - LoopVector LoopList = Worklist.pop_back_val(); - Changed = processLoopList(LoopList, F); - } - return Changed; + return processLoopList(populateWorklist(*L)); } bool isComputableLoopNest(LoopVector LoopList) { @@ -507,7 +483,7 @@ struct LoopInterchange : public FunctionPass { return LoopList.size() - 1; } - bool processLoopList(LoopVector LoopList, Function &F) { + bool processLoopList(LoopVector LoopList) { bool Changed = false; unsigned LoopNestDepth = LoopList.size(); if (LoopNestDepth < 2) { @@ -1542,13 +1518,8 @@ char LoopInterchange::ID = 0; INITIALIZE_PASS_BEGIN(LoopInterchange, "loop-interchange", "Interchanges loops for cache reuse", false, false) -INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) +INITIALIZE_PASS_DEPENDENCY(LoopPass) INITIALIZE_PASS_DEPENDENCY(DependenceAnalysisWrapperPass) -INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) -INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) -INITIALIZE_PASS_DEPENDENCY(LoopSimplify) -INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass) -INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass) INITIALIZE_PASS_END(LoopInterchange, "loop-interchange", |

