1*9880d681SAndroid Build Coastguard Worker //===--------- LoopSimplifyCFG.cpp - Loop CFG Simplification Pass ---------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the Loop SimplifyCFG Pass. This pass is responsible for
11*9880d681SAndroid Build Coastguard Worker // basic loop CFG cleanup, primarily to assist other loop passes. If you
12*9880d681SAndroid Build Coastguard Worker // encounter a noncanonical CFG construct that causes another loop pass to
13*9880d681SAndroid Build Coastguard Worker // perform suboptimally, this is the place to fix it up.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
16*9880d681SAndroid Build Coastguard Worker
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallVector.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AliasAnalysis.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/BasicAliasAnalysis.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AssumptionCache.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/DependenceAnalysis.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/GlobalsModRef.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/LoopInfo.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/LoopPass.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/LoopPassManager.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ScalarEvolution.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetTransformInfo.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Dominators.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/Local.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/LoopUtils.h"
35*9880d681SAndroid Build Coastguard Worker using namespace llvm;
36*9880d681SAndroid Build Coastguard Worker
37*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "loop-simplifycfg"
38*9880d681SAndroid Build Coastguard Worker
simplifyLoopCFG(Loop & L,DominatorTree & DT,LoopInfo & LI)39*9880d681SAndroid Build Coastguard Worker static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI) {
40*9880d681SAndroid Build Coastguard Worker bool Changed = false;
41*9880d681SAndroid Build Coastguard Worker // Copy blocks into a temporary array to avoid iterator invalidation issues
42*9880d681SAndroid Build Coastguard Worker // as we remove them.
43*9880d681SAndroid Build Coastguard Worker SmallVector<WeakVH, 16> Blocks(L.blocks());
44*9880d681SAndroid Build Coastguard Worker
45*9880d681SAndroid Build Coastguard Worker for (auto &Block : Blocks) {
46*9880d681SAndroid Build Coastguard Worker // Attempt to merge blocks in the trivial case. Don't modify blocks which
47*9880d681SAndroid Build Coastguard Worker // belong to other loops.
48*9880d681SAndroid Build Coastguard Worker BasicBlock *Succ = cast_or_null<BasicBlock>(Block);
49*9880d681SAndroid Build Coastguard Worker if (!Succ)
50*9880d681SAndroid Build Coastguard Worker continue;
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard Worker BasicBlock *Pred = Succ->getSinglePredecessor();
53*9880d681SAndroid Build Coastguard Worker if (!Pred || !Pred->getSingleSuccessor() || LI.getLoopFor(Pred) != &L)
54*9880d681SAndroid Build Coastguard Worker continue;
55*9880d681SAndroid Build Coastguard Worker
56*9880d681SAndroid Build Coastguard Worker // Pred is going to disappear, so we need to update the loop info.
57*9880d681SAndroid Build Coastguard Worker if (L.getHeader() == Pred)
58*9880d681SAndroid Build Coastguard Worker L.moveToHeader(Succ);
59*9880d681SAndroid Build Coastguard Worker LI.removeBlock(Pred);
60*9880d681SAndroid Build Coastguard Worker MergeBasicBlockIntoOnlyPred(Succ, &DT);
61*9880d681SAndroid Build Coastguard Worker Changed = true;
62*9880d681SAndroid Build Coastguard Worker }
63*9880d681SAndroid Build Coastguard Worker
64*9880d681SAndroid Build Coastguard Worker return Changed;
65*9880d681SAndroid Build Coastguard Worker }
66*9880d681SAndroid Build Coastguard Worker
run(Loop & L,AnalysisManager<Loop> & AM)67*9880d681SAndroid Build Coastguard Worker PreservedAnalyses LoopSimplifyCFGPass::run(Loop &L, AnalysisManager<Loop> &AM) {
68*9880d681SAndroid Build Coastguard Worker const auto &FAM =
69*9880d681SAndroid Build Coastguard Worker AM.getResult<FunctionAnalysisManagerLoopProxy>(L).getManager();
70*9880d681SAndroid Build Coastguard Worker Function *F = L.getHeader()->getParent();
71*9880d681SAndroid Build Coastguard Worker
72*9880d681SAndroid Build Coastguard Worker auto *LI = FAM.getCachedResult<LoopAnalysis>(*F);
73*9880d681SAndroid Build Coastguard Worker auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(*F);
74*9880d681SAndroid Build Coastguard Worker assert((LI && DT) && "Analyses for LoopSimplifyCFG not available");
75*9880d681SAndroid Build Coastguard Worker
76*9880d681SAndroid Build Coastguard Worker if (!simplifyLoopCFG(L, *DT, *LI))
77*9880d681SAndroid Build Coastguard Worker return PreservedAnalyses::all();
78*9880d681SAndroid Build Coastguard Worker return getLoopPassPreservedAnalyses();
79*9880d681SAndroid Build Coastguard Worker }
80*9880d681SAndroid Build Coastguard Worker
81*9880d681SAndroid Build Coastguard Worker namespace {
82*9880d681SAndroid Build Coastguard Worker class LoopSimplifyCFGLegacyPass : public LoopPass {
83*9880d681SAndroid Build Coastguard Worker public:
84*9880d681SAndroid Build Coastguard Worker static char ID; // Pass ID, replacement for typeid
LoopSimplifyCFGLegacyPass()85*9880d681SAndroid Build Coastguard Worker LoopSimplifyCFGLegacyPass() : LoopPass(ID) {
86*9880d681SAndroid Build Coastguard Worker initializeLoopSimplifyCFGLegacyPassPass(*PassRegistry::getPassRegistry());
87*9880d681SAndroid Build Coastguard Worker }
88*9880d681SAndroid Build Coastguard Worker
runOnLoop(Loop * L,LPPassManager &)89*9880d681SAndroid Build Coastguard Worker bool runOnLoop(Loop *L, LPPassManager &) override {
90*9880d681SAndroid Build Coastguard Worker if (skipLoop(L))
91*9880d681SAndroid Build Coastguard Worker return false;
92*9880d681SAndroid Build Coastguard Worker
93*9880d681SAndroid Build Coastguard Worker DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
94*9880d681SAndroid Build Coastguard Worker LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
95*9880d681SAndroid Build Coastguard Worker return simplifyLoopCFG(*L, DT, LI);
96*9880d681SAndroid Build Coastguard Worker }
97*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage(AnalysisUsage & AU) const98*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override {
99*9880d681SAndroid Build Coastguard Worker AU.addPreserved<DependenceAnalysisWrapperPass>();
100*9880d681SAndroid Build Coastguard Worker getLoopAnalysisUsage(AU);
101*9880d681SAndroid Build Coastguard Worker }
102*9880d681SAndroid Build Coastguard Worker };
103*9880d681SAndroid Build Coastguard Worker }
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker char LoopSimplifyCFGLegacyPass::ID = 0;
106*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(LoopSimplifyCFGLegacyPass, "loop-simplifycfg",
107*9880d681SAndroid Build Coastguard Worker "Simplify loop CFG", false, false)
INITIALIZE_PASS_DEPENDENCY(LoopPass)108*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(LoopPass)
109*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(LoopSimplifyCFGLegacyPass, "loop-simplifycfg",
110*9880d681SAndroid Build Coastguard Worker "Simplify loop CFG", false, false)
111*9880d681SAndroid Build Coastguard Worker
112*9880d681SAndroid Build Coastguard Worker Pass *llvm::createLoopSimplifyCFGPass() {
113*9880d681SAndroid Build Coastguard Worker return new LoopSimplifyCFGLegacyPass();
114*9880d681SAndroid Build Coastguard Worker }
115