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