13ca95b02SDimitry Andric //===--------- LoopSimplifyCFG.cpp - Loop CFG Simplification Pass ---------===//
23ca95b02SDimitry Andric //
33ca95b02SDimitry Andric // The LLVM Compiler Infrastructure
43ca95b02SDimitry Andric //
53ca95b02SDimitry Andric // This file is distributed under the University of Illinois Open Source
63ca95b02SDimitry Andric // License. See LICENSE.TXT for details.
73ca95b02SDimitry Andric //
83ca95b02SDimitry Andric //===----------------------------------------------------------------------===//
93ca95b02SDimitry Andric //
103ca95b02SDimitry Andric // This file implements the Loop SimplifyCFG Pass. This pass is responsible for
113ca95b02SDimitry Andric // basic loop CFG cleanup, primarily to assist other loop passes. If you
123ca95b02SDimitry Andric // encounter a noncanonical CFG construct that causes another loop pass to
133ca95b02SDimitry Andric // perform suboptimally, this is the place to fix it up.
143ca95b02SDimitry Andric //
153ca95b02SDimitry Andric //===----------------------------------------------------------------------===//
163ca95b02SDimitry Andric
173ca95b02SDimitry Andric #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
183ca95b02SDimitry Andric #include "llvm/ADT/SmallVector.h"
193ca95b02SDimitry Andric #include "llvm/ADT/Statistic.h"
203ca95b02SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
213ca95b02SDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
22f1a29dd3SDimitry Andric #include "llvm/Analysis/BasicAliasAnalysis.h"
233ca95b02SDimitry Andric #include "llvm/Analysis/DependenceAnalysis.h"
243ca95b02SDimitry Andric #include "llvm/Analysis/GlobalsModRef.h"
253ca95b02SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
263ca95b02SDimitry Andric #include "llvm/Analysis/LoopPass.h"
27*b5893f02SDimitry Andric #include "llvm/Analysis/MemorySSA.h"
28*b5893f02SDimitry Andric #include "llvm/Analysis/MemorySSAUpdater.h"
293ca95b02SDimitry Andric #include "llvm/Analysis/ScalarEvolution.h"
303ca95b02SDimitry Andric #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
313ca95b02SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
32*b5893f02SDimitry Andric #include "llvm/IR/DomTreeUpdater.h"
333ca95b02SDimitry Andric #include "llvm/IR/Dominators.h"
343ca95b02SDimitry Andric #include "llvm/Transforms/Scalar.h"
35f1a29dd3SDimitry Andric #include "llvm/Transforms/Scalar/LoopPassManager.h"
364ba319b5SDimitry Andric #include "llvm/Transforms/Utils.h"
374ba319b5SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
383ca95b02SDimitry Andric #include "llvm/Transforms/Utils/Local.h"
393ca95b02SDimitry Andric #include "llvm/Transforms/Utils/LoopUtils.h"
403ca95b02SDimitry Andric using namespace llvm;
413ca95b02SDimitry Andric
423ca95b02SDimitry Andric #define DEBUG_TYPE "loop-simplifycfg"
433ca95b02SDimitry Andric
44*b5893f02SDimitry Andric static cl::opt<bool> EnableTermFolding("enable-loop-simplifycfg-term-folding",
45*b5893f02SDimitry Andric cl::init(false));
46*b5893f02SDimitry Andric
47*b5893f02SDimitry Andric STATISTIC(NumTerminatorsFolded,
48*b5893f02SDimitry Andric "Number of terminators folded to unconditional branches");
49*b5893f02SDimitry Andric STATISTIC(NumLoopBlocksDeleted,
50*b5893f02SDimitry Andric "Number of loop blocks deleted");
51*b5893f02SDimitry Andric STATISTIC(NumLoopExitsDeleted,
52*b5893f02SDimitry Andric "Number of loop exiting edges deleted");
53*b5893f02SDimitry Andric
54*b5893f02SDimitry Andric /// If \p BB is a switch or a conditional branch, but only one of its successors
55*b5893f02SDimitry Andric /// can be reached from this block in runtime, return this successor. Otherwise,
56*b5893f02SDimitry Andric /// return nullptr.
getOnlyLiveSuccessor(BasicBlock * BB)57*b5893f02SDimitry Andric static BasicBlock *getOnlyLiveSuccessor(BasicBlock *BB) {
58*b5893f02SDimitry Andric Instruction *TI = BB->getTerminator();
59*b5893f02SDimitry Andric if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
60*b5893f02SDimitry Andric if (BI->isUnconditional())
61*b5893f02SDimitry Andric return nullptr;
62*b5893f02SDimitry Andric if (BI->getSuccessor(0) == BI->getSuccessor(1))
63*b5893f02SDimitry Andric return BI->getSuccessor(0);
64*b5893f02SDimitry Andric ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
65*b5893f02SDimitry Andric if (!Cond)
66*b5893f02SDimitry Andric return nullptr;
67*b5893f02SDimitry Andric return Cond->isZero() ? BI->getSuccessor(1) : BI->getSuccessor(0);
68*b5893f02SDimitry Andric }
69*b5893f02SDimitry Andric
70*b5893f02SDimitry Andric if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
71*b5893f02SDimitry Andric auto *CI = dyn_cast<ConstantInt>(SI->getCondition());
72*b5893f02SDimitry Andric if (!CI)
73*b5893f02SDimitry Andric return nullptr;
74*b5893f02SDimitry Andric for (auto Case : SI->cases())
75*b5893f02SDimitry Andric if (Case.getCaseValue() == CI)
76*b5893f02SDimitry Andric return Case.getCaseSuccessor();
77*b5893f02SDimitry Andric return SI->getDefaultDest();
78*b5893f02SDimitry Andric }
79*b5893f02SDimitry Andric
80*b5893f02SDimitry Andric return nullptr;
81*b5893f02SDimitry Andric }
82*b5893f02SDimitry Andric
83*b5893f02SDimitry Andric namespace {
84*b5893f02SDimitry Andric /// Helper class that can turn branches and switches with constant conditions
85*b5893f02SDimitry Andric /// into unconditional branches.
86*b5893f02SDimitry Andric class ConstantTerminatorFoldingImpl {
87*b5893f02SDimitry Andric private:
88*b5893f02SDimitry Andric Loop &L;
89*b5893f02SDimitry Andric LoopInfo &LI;
90*b5893f02SDimitry Andric DominatorTree &DT;
91*b5893f02SDimitry Andric ScalarEvolution &SE;
92*b5893f02SDimitry Andric MemorySSAUpdater *MSSAU;
93*b5893f02SDimitry Andric
94*b5893f02SDimitry Andric // Whether or not the current loop has irreducible CFG.
95*b5893f02SDimitry Andric bool HasIrreducibleCFG = false;
96*b5893f02SDimitry Andric // Whether or not the current loop will still exist after terminator constant
97*b5893f02SDimitry Andric // folding will be done. In theory, there are two ways how it can happen:
98*b5893f02SDimitry Andric // 1. Loop's latch(es) become unreachable from loop header;
99*b5893f02SDimitry Andric // 2. Loop's header becomes unreachable from method entry.
100*b5893f02SDimitry Andric // In practice, the second situation is impossible because we only modify the
101*b5893f02SDimitry Andric // current loop and its preheader and do not affect preheader's reachibility
102*b5893f02SDimitry Andric // from any other block. So this variable set to true means that loop's latch
103*b5893f02SDimitry Andric // has become unreachable from loop header.
104*b5893f02SDimitry Andric bool DeleteCurrentLoop = false;
105*b5893f02SDimitry Andric
106*b5893f02SDimitry Andric // The blocks of the original loop that will still be reachable from entry
107*b5893f02SDimitry Andric // after the constant folding.
108*b5893f02SDimitry Andric SmallPtrSet<BasicBlock *, 8> LiveLoopBlocks;
109*b5893f02SDimitry Andric // The blocks of the original loop that will become unreachable from entry
110*b5893f02SDimitry Andric // after the constant folding.
111*b5893f02SDimitry Andric SmallVector<BasicBlock *, 8> DeadLoopBlocks;
112*b5893f02SDimitry Andric // The exits of the original loop that will still be reachable from entry
113*b5893f02SDimitry Andric // after the constant folding.
114*b5893f02SDimitry Andric SmallPtrSet<BasicBlock *, 8> LiveExitBlocks;
115*b5893f02SDimitry Andric // The exits of the original loop that will become unreachable from entry
116*b5893f02SDimitry Andric // after the constant folding.
117*b5893f02SDimitry Andric SmallVector<BasicBlock *, 8> DeadExitBlocks;
118*b5893f02SDimitry Andric // The blocks that will still be a part of the current loop after folding.
119*b5893f02SDimitry Andric SmallPtrSet<BasicBlock *, 8> BlocksInLoopAfterFolding;
120*b5893f02SDimitry Andric // The blocks that have terminators with constant condition that can be
121*b5893f02SDimitry Andric // folded. Note: fold candidates should be in L but not in any of its
122*b5893f02SDimitry Andric // subloops to avoid complex LI updates.
123*b5893f02SDimitry Andric SmallVector<BasicBlock *, 8> FoldCandidates;
124*b5893f02SDimitry Andric
dump() const125*b5893f02SDimitry Andric void dump() const {
126*b5893f02SDimitry Andric dbgs() << "Constant terminator folding for loop " << L << "\n";
127*b5893f02SDimitry Andric dbgs() << "After terminator constant-folding, the loop will";
128*b5893f02SDimitry Andric if (!DeleteCurrentLoop)
129*b5893f02SDimitry Andric dbgs() << " not";
130*b5893f02SDimitry Andric dbgs() << " be destroyed\n";
131*b5893f02SDimitry Andric auto PrintOutVector = [&](const char *Message,
132*b5893f02SDimitry Andric const SmallVectorImpl<BasicBlock *> &S) {
133*b5893f02SDimitry Andric dbgs() << Message << "\n";
134*b5893f02SDimitry Andric for (const BasicBlock *BB : S)
135*b5893f02SDimitry Andric dbgs() << "\t" << BB->getName() << "\n";
136*b5893f02SDimitry Andric };
137*b5893f02SDimitry Andric auto PrintOutSet = [&](const char *Message,
138*b5893f02SDimitry Andric const SmallPtrSetImpl<BasicBlock *> &S) {
139*b5893f02SDimitry Andric dbgs() << Message << "\n";
140*b5893f02SDimitry Andric for (const BasicBlock *BB : S)
141*b5893f02SDimitry Andric dbgs() << "\t" << BB->getName() << "\n";
142*b5893f02SDimitry Andric };
143*b5893f02SDimitry Andric PrintOutVector("Blocks in which we can constant-fold terminator:",
144*b5893f02SDimitry Andric FoldCandidates);
145*b5893f02SDimitry Andric PrintOutSet("Live blocks from the original loop:", LiveLoopBlocks);
146*b5893f02SDimitry Andric PrintOutVector("Dead blocks from the original loop:", DeadLoopBlocks);
147*b5893f02SDimitry Andric PrintOutSet("Live exit blocks:", LiveExitBlocks);
148*b5893f02SDimitry Andric PrintOutVector("Dead exit blocks:", DeadExitBlocks);
149*b5893f02SDimitry Andric if (!DeleteCurrentLoop)
150*b5893f02SDimitry Andric PrintOutSet("The following blocks will still be part of the loop:",
151*b5893f02SDimitry Andric BlocksInLoopAfterFolding);
152*b5893f02SDimitry Andric }
153*b5893f02SDimitry Andric
154*b5893f02SDimitry Andric /// Whether or not the current loop has irreducible CFG.
hasIrreducibleCFG(LoopBlocksDFS & DFS)155*b5893f02SDimitry Andric bool hasIrreducibleCFG(LoopBlocksDFS &DFS) {
156*b5893f02SDimitry Andric assert(DFS.isComplete() && "DFS is expected to be finished");
157*b5893f02SDimitry Andric // Index of a basic block in RPO traversal.
158*b5893f02SDimitry Andric DenseMap<const BasicBlock *, unsigned> RPO;
159*b5893f02SDimitry Andric unsigned Current = 0;
160*b5893f02SDimitry Andric for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I)
161*b5893f02SDimitry Andric RPO[*I] = Current++;
162*b5893f02SDimitry Andric
163*b5893f02SDimitry Andric for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) {
164*b5893f02SDimitry Andric BasicBlock *BB = *I;
165*b5893f02SDimitry Andric for (auto *Succ : successors(BB))
166*b5893f02SDimitry Andric if (L.contains(Succ) && !LI.isLoopHeader(Succ) && RPO[BB] > RPO[Succ])
167*b5893f02SDimitry Andric // If an edge goes from a block with greater order number into a block
168*b5893f02SDimitry Andric // with lesses number, and it is not a loop backedge, then it can only
169*b5893f02SDimitry Andric // be a part of irreducible non-loop cycle.
170*b5893f02SDimitry Andric return true;
171*b5893f02SDimitry Andric }
172*b5893f02SDimitry Andric return false;
173*b5893f02SDimitry Andric }
174*b5893f02SDimitry Andric
175*b5893f02SDimitry Andric /// Fill all information about status of blocks and exits of the current loop
176*b5893f02SDimitry Andric /// if constant folding of all branches will be done.
analyze()177*b5893f02SDimitry Andric void analyze() {
178*b5893f02SDimitry Andric LoopBlocksDFS DFS(&L);
179*b5893f02SDimitry Andric DFS.perform(&LI);
180*b5893f02SDimitry Andric assert(DFS.isComplete() && "DFS is expected to be finished");
181*b5893f02SDimitry Andric
182*b5893f02SDimitry Andric // TODO: The algorithm below relies on both RPO and Postorder traversals.
183*b5893f02SDimitry Andric // When the loop has only reducible CFG inside, then the invariant "all
184*b5893f02SDimitry Andric // predecessors of X are processed before X in RPO" is preserved. However
185*b5893f02SDimitry Andric // an irreducible loop can break this invariant (e.g. latch does not have to
186*b5893f02SDimitry Andric // be the last block in the traversal in this case, and the algorithm relies
187*b5893f02SDimitry Andric // on this). We can later decide to support such cases by altering the
188*b5893f02SDimitry Andric // algorithms, but so far we just give up analyzing them.
189*b5893f02SDimitry Andric if (hasIrreducibleCFG(DFS)) {
190*b5893f02SDimitry Andric HasIrreducibleCFG = true;
191*b5893f02SDimitry Andric return;
192*b5893f02SDimitry Andric }
193*b5893f02SDimitry Andric
194*b5893f02SDimitry Andric // Collect live and dead loop blocks and exits.
195*b5893f02SDimitry Andric LiveLoopBlocks.insert(L.getHeader());
196*b5893f02SDimitry Andric for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) {
197*b5893f02SDimitry Andric BasicBlock *BB = *I;
198*b5893f02SDimitry Andric
199*b5893f02SDimitry Andric // If a loop block wasn't marked as live so far, then it's dead.
200*b5893f02SDimitry Andric if (!LiveLoopBlocks.count(BB)) {
201*b5893f02SDimitry Andric DeadLoopBlocks.push_back(BB);
202*b5893f02SDimitry Andric continue;
203*b5893f02SDimitry Andric }
204*b5893f02SDimitry Andric
205*b5893f02SDimitry Andric BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB);
206*b5893f02SDimitry Andric
207*b5893f02SDimitry Andric // If a block has only one live successor, it's a candidate on constant
208*b5893f02SDimitry Andric // folding. Only handle blocks from current loop: branches in child loops
209*b5893f02SDimitry Andric // are skipped because if they can be folded, they should be folded during
210*b5893f02SDimitry Andric // the processing of child loops.
211*b5893f02SDimitry Andric if (TheOnlySucc && LI.getLoopFor(BB) == &L)
212*b5893f02SDimitry Andric FoldCandidates.push_back(BB);
213*b5893f02SDimitry Andric
214*b5893f02SDimitry Andric // Handle successors.
215*b5893f02SDimitry Andric for (BasicBlock *Succ : successors(BB))
216*b5893f02SDimitry Andric if (!TheOnlySucc || TheOnlySucc == Succ) {
217*b5893f02SDimitry Andric if (L.contains(Succ))
218*b5893f02SDimitry Andric LiveLoopBlocks.insert(Succ);
219*b5893f02SDimitry Andric else
220*b5893f02SDimitry Andric LiveExitBlocks.insert(Succ);
221*b5893f02SDimitry Andric }
222*b5893f02SDimitry Andric }
223*b5893f02SDimitry Andric
224*b5893f02SDimitry Andric // Sanity check: amount of dead and live loop blocks should match the total
225*b5893f02SDimitry Andric // number of blocks in loop.
226*b5893f02SDimitry Andric assert(L.getNumBlocks() == LiveLoopBlocks.size() + DeadLoopBlocks.size() &&
227*b5893f02SDimitry Andric "Malformed block sets?");
228*b5893f02SDimitry Andric
229*b5893f02SDimitry Andric // Now, all exit blocks that are not marked as live are dead.
230*b5893f02SDimitry Andric SmallVector<BasicBlock *, 8> ExitBlocks;
231*b5893f02SDimitry Andric L.getExitBlocks(ExitBlocks);
232*b5893f02SDimitry Andric for (auto *ExitBlock : ExitBlocks)
233*b5893f02SDimitry Andric if (!LiveExitBlocks.count(ExitBlock))
234*b5893f02SDimitry Andric DeadExitBlocks.push_back(ExitBlock);
235*b5893f02SDimitry Andric
236*b5893f02SDimitry Andric // Whether or not the edge From->To will still be present in graph after the
237*b5893f02SDimitry Andric // folding.
238*b5893f02SDimitry Andric auto IsEdgeLive = [&](BasicBlock *From, BasicBlock *To) {
239*b5893f02SDimitry Andric if (!LiveLoopBlocks.count(From))
240*b5893f02SDimitry Andric return false;
241*b5893f02SDimitry Andric BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(From);
242*b5893f02SDimitry Andric return !TheOnlySucc || TheOnlySucc == To;
243*b5893f02SDimitry Andric };
244*b5893f02SDimitry Andric
245*b5893f02SDimitry Andric // The loop will not be destroyed if its latch is live.
246*b5893f02SDimitry Andric DeleteCurrentLoop = !IsEdgeLive(L.getLoopLatch(), L.getHeader());
247*b5893f02SDimitry Andric
248*b5893f02SDimitry Andric // If we are going to delete the current loop completely, no extra analysis
249*b5893f02SDimitry Andric // is needed.
250*b5893f02SDimitry Andric if (DeleteCurrentLoop)
251*b5893f02SDimitry Andric return;
252*b5893f02SDimitry Andric
253*b5893f02SDimitry Andric // Otherwise, we should check which blocks will still be a part of the
254*b5893f02SDimitry Andric // current loop after the transform.
255*b5893f02SDimitry Andric BlocksInLoopAfterFolding.insert(L.getLoopLatch());
256*b5893f02SDimitry Andric // If the loop is live, then we should compute what blocks are still in
257*b5893f02SDimitry Andric // loop after all branch folding has been done. A block is in loop if
258*b5893f02SDimitry Andric // it has a live edge to another block that is in the loop; by definition,
259*b5893f02SDimitry Andric // latch is in the loop.
260*b5893f02SDimitry Andric auto BlockIsInLoop = [&](BasicBlock *BB) {
261*b5893f02SDimitry Andric return any_of(successors(BB), [&](BasicBlock *Succ) {
262*b5893f02SDimitry Andric return BlocksInLoopAfterFolding.count(Succ) && IsEdgeLive(BB, Succ);
263*b5893f02SDimitry Andric });
264*b5893f02SDimitry Andric };
265*b5893f02SDimitry Andric for (auto I = DFS.beginPostorder(), E = DFS.endPostorder(); I != E; ++I) {
266*b5893f02SDimitry Andric BasicBlock *BB = *I;
267*b5893f02SDimitry Andric if (BlockIsInLoop(BB))
268*b5893f02SDimitry Andric BlocksInLoopAfterFolding.insert(BB);
269*b5893f02SDimitry Andric }
270*b5893f02SDimitry Andric
271*b5893f02SDimitry Andric // Sanity check: header must be in loop.
272*b5893f02SDimitry Andric assert(BlocksInLoopAfterFolding.count(L.getHeader()) &&
273*b5893f02SDimitry Andric "Header not in loop?");
274*b5893f02SDimitry Andric assert(BlocksInLoopAfterFolding.size() <= LiveLoopBlocks.size() &&
275*b5893f02SDimitry Andric "All blocks that stay in loop should be live!");
276*b5893f02SDimitry Andric }
277*b5893f02SDimitry Andric
278*b5893f02SDimitry Andric /// We need to preserve static reachibility of all loop exit blocks (this is)
279*b5893f02SDimitry Andric /// required by loop pass manager. In order to do it, we make the following
280*b5893f02SDimitry Andric /// trick:
281*b5893f02SDimitry Andric ///
282*b5893f02SDimitry Andric /// preheader:
283*b5893f02SDimitry Andric /// <preheader code>
284*b5893f02SDimitry Andric /// br label %loop_header
285*b5893f02SDimitry Andric ///
286*b5893f02SDimitry Andric /// loop_header:
287*b5893f02SDimitry Andric /// ...
288*b5893f02SDimitry Andric /// br i1 false, label %dead_exit, label %loop_block
289*b5893f02SDimitry Andric /// ...
290*b5893f02SDimitry Andric ///
291*b5893f02SDimitry Andric /// We cannot simply remove edge from the loop to dead exit because in this
292*b5893f02SDimitry Andric /// case dead_exit (and its successors) may become unreachable. To avoid that,
293*b5893f02SDimitry Andric /// we insert the following fictive preheader:
294*b5893f02SDimitry Andric ///
295*b5893f02SDimitry Andric /// preheader:
296*b5893f02SDimitry Andric /// <preheader code>
297*b5893f02SDimitry Andric /// switch i32 0, label %preheader-split,
298*b5893f02SDimitry Andric /// [i32 1, label %dead_exit_1],
299*b5893f02SDimitry Andric /// [i32 2, label %dead_exit_2],
300*b5893f02SDimitry Andric /// ...
301*b5893f02SDimitry Andric /// [i32 N, label %dead_exit_N],
302*b5893f02SDimitry Andric ///
303*b5893f02SDimitry Andric /// preheader-split:
304*b5893f02SDimitry Andric /// br label %loop_header
305*b5893f02SDimitry Andric ///
306*b5893f02SDimitry Andric /// loop_header:
307*b5893f02SDimitry Andric /// ...
308*b5893f02SDimitry Andric /// br i1 false, label %dead_exit_N, label %loop_block
309*b5893f02SDimitry Andric /// ...
310*b5893f02SDimitry Andric ///
311*b5893f02SDimitry Andric /// Doing so, we preserve static reachibility of all dead exits and can later
312*b5893f02SDimitry Andric /// remove edges from the loop to these blocks.
handleDeadExits()313*b5893f02SDimitry Andric void handleDeadExits() {
314*b5893f02SDimitry Andric // If no dead exits, nothing to do.
315*b5893f02SDimitry Andric if (DeadExitBlocks.empty())
316*b5893f02SDimitry Andric return;
317*b5893f02SDimitry Andric
318*b5893f02SDimitry Andric // Construct split preheader and the dummy switch to thread edges from it to
319*b5893f02SDimitry Andric // dead exits.
320*b5893f02SDimitry Andric DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
321*b5893f02SDimitry Andric BasicBlock *Preheader = L.getLoopPreheader();
322*b5893f02SDimitry Andric BasicBlock *NewPreheader = Preheader->splitBasicBlock(
323*b5893f02SDimitry Andric Preheader->getTerminator(),
324*b5893f02SDimitry Andric Twine(Preheader->getName()).concat("-split"));
325*b5893f02SDimitry Andric DTU.deleteEdge(Preheader, L.getHeader());
326*b5893f02SDimitry Andric DTU.insertEdge(NewPreheader, L.getHeader());
327*b5893f02SDimitry Andric DTU.insertEdge(Preheader, NewPreheader);
328*b5893f02SDimitry Andric IRBuilder<> Builder(Preheader->getTerminator());
329*b5893f02SDimitry Andric SwitchInst *DummySwitch =
330*b5893f02SDimitry Andric Builder.CreateSwitch(Builder.getInt32(0), NewPreheader);
331*b5893f02SDimitry Andric Preheader->getTerminator()->eraseFromParent();
332*b5893f02SDimitry Andric
333*b5893f02SDimitry Andric unsigned DummyIdx = 1;
334*b5893f02SDimitry Andric for (BasicBlock *BB : DeadExitBlocks) {
335*b5893f02SDimitry Andric SmallVector<Instruction *, 4> DeadPhis;
336*b5893f02SDimitry Andric for (auto &PN : BB->phis())
337*b5893f02SDimitry Andric DeadPhis.push_back(&PN);
338*b5893f02SDimitry Andric
339*b5893f02SDimitry Andric // Eliminate all Phis from dead exits.
340*b5893f02SDimitry Andric for (Instruction *PN : DeadPhis) {
341*b5893f02SDimitry Andric PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
342*b5893f02SDimitry Andric PN->eraseFromParent();
343*b5893f02SDimitry Andric }
344*b5893f02SDimitry Andric assert(DummyIdx != 0 && "Too many dead exits!");
345*b5893f02SDimitry Andric DummySwitch->addCase(Builder.getInt32(DummyIdx++), BB);
346*b5893f02SDimitry Andric DTU.insertEdge(Preheader, BB);
347*b5893f02SDimitry Andric ++NumLoopExitsDeleted;
348*b5893f02SDimitry Andric }
349*b5893f02SDimitry Andric
350*b5893f02SDimitry Andric assert(L.getLoopPreheader() == NewPreheader && "Malformed CFG?");
351*b5893f02SDimitry Andric if (Loop *OuterLoop = LI.getLoopFor(Preheader)) {
352*b5893f02SDimitry Andric OuterLoop->addBasicBlockToLoop(NewPreheader, LI);
353*b5893f02SDimitry Andric
354*b5893f02SDimitry Andric // When we break dead edges, the outer loop may become unreachable from
355*b5893f02SDimitry Andric // the current loop. We need to fix loop info accordingly. For this, we
356*b5893f02SDimitry Andric // find the most nested loop that still contains L and remove L from all
357*b5893f02SDimitry Andric // loops that are inside of it.
358*b5893f02SDimitry Andric Loop *StillReachable = nullptr;
359*b5893f02SDimitry Andric for (BasicBlock *BB : LiveExitBlocks) {
360*b5893f02SDimitry Andric Loop *BBL = LI.getLoopFor(BB);
361*b5893f02SDimitry Andric if (BBL && BBL->contains(L.getHeader()))
362*b5893f02SDimitry Andric if (!StillReachable ||
363*b5893f02SDimitry Andric BBL->getLoopDepth() > StillReachable->getLoopDepth())
364*b5893f02SDimitry Andric StillReachable = BBL;
365*b5893f02SDimitry Andric }
366*b5893f02SDimitry Andric
367*b5893f02SDimitry Andric // Okay, our loop is no longer in the outer loop (and maybe not in some of
368*b5893f02SDimitry Andric // its parents as well). Make the fixup.
369*b5893f02SDimitry Andric if (StillReachable != OuterLoop) {
370*b5893f02SDimitry Andric LI.changeLoopFor(NewPreheader, StillReachable);
371*b5893f02SDimitry Andric for (Loop *NotContaining = OuterLoop; NotContaining != StillReachable;
372*b5893f02SDimitry Andric NotContaining = NotContaining->getParentLoop()) {
373*b5893f02SDimitry Andric NotContaining->removeBlockFromLoop(NewPreheader);
374*b5893f02SDimitry Andric for (auto *BB : L.blocks())
375*b5893f02SDimitry Andric NotContaining->removeBlockFromLoop(BB);
376*b5893f02SDimitry Andric }
377*b5893f02SDimitry Andric OuterLoop->removeChildLoop(&L);
378*b5893f02SDimitry Andric if (StillReachable)
379*b5893f02SDimitry Andric StillReachable->addChildLoop(&L);
380*b5893f02SDimitry Andric else
381*b5893f02SDimitry Andric LI.addTopLevelLoop(&L);
382*b5893f02SDimitry Andric }
383*b5893f02SDimitry Andric }
384*b5893f02SDimitry Andric }
385*b5893f02SDimitry Andric
386*b5893f02SDimitry Andric /// Delete loop blocks that have become unreachable after folding. Make all
387*b5893f02SDimitry Andric /// relevant updates to DT and LI.
deleteDeadLoopBlocks()388*b5893f02SDimitry Andric void deleteDeadLoopBlocks() {
389*b5893f02SDimitry Andric DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
390*b5893f02SDimitry Andric if (MSSAU) {
391*b5893f02SDimitry Andric SmallPtrSet<BasicBlock *, 8> DeadLoopBlocksSet(DeadLoopBlocks.begin(),
392*b5893f02SDimitry Andric DeadLoopBlocks.end());
393*b5893f02SDimitry Andric MSSAU->removeBlocks(DeadLoopBlocksSet);
394*b5893f02SDimitry Andric }
395*b5893f02SDimitry Andric for (auto *BB : DeadLoopBlocks) {
396*b5893f02SDimitry Andric assert(BB != L.getHeader() &&
397*b5893f02SDimitry Andric "Header of the current loop cannot be dead!");
398*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Deleting dead loop block " << BB->getName()
399*b5893f02SDimitry Andric << "\n");
400*b5893f02SDimitry Andric if (LI.isLoopHeader(BB)) {
401*b5893f02SDimitry Andric assert(LI.getLoopFor(BB) != &L && "Attempt to remove current loop!");
402*b5893f02SDimitry Andric LI.erase(LI.getLoopFor(BB));
403*b5893f02SDimitry Andric }
404*b5893f02SDimitry Andric LI.removeBlock(BB);
405*b5893f02SDimitry Andric DeleteDeadBlock(BB, &DTU);
406*b5893f02SDimitry Andric ++NumLoopBlocksDeleted;
407*b5893f02SDimitry Andric }
408*b5893f02SDimitry Andric }
409*b5893f02SDimitry Andric
410*b5893f02SDimitry Andric /// Constant-fold terminators of blocks acculumated in FoldCandidates into the
411*b5893f02SDimitry Andric /// unconditional branches.
foldTerminators()412*b5893f02SDimitry Andric void foldTerminators() {
413*b5893f02SDimitry Andric DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
414*b5893f02SDimitry Andric
415*b5893f02SDimitry Andric for (BasicBlock *BB : FoldCandidates) {
416*b5893f02SDimitry Andric assert(LI.getLoopFor(BB) == &L && "Should be a loop block!");
417*b5893f02SDimitry Andric BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB);
418*b5893f02SDimitry Andric assert(TheOnlySucc && "Should have one live successor!");
419*b5893f02SDimitry Andric
420*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Replacing terminator of " << BB->getName()
421*b5893f02SDimitry Andric << " with an unconditional branch to the block "
422*b5893f02SDimitry Andric << TheOnlySucc->getName() << "\n");
423*b5893f02SDimitry Andric
424*b5893f02SDimitry Andric SmallPtrSet<BasicBlock *, 2> DeadSuccessors;
425*b5893f02SDimitry Andric // Remove all BB's successors except for the live one.
426*b5893f02SDimitry Andric unsigned TheOnlySuccDuplicates = 0;
427*b5893f02SDimitry Andric for (auto *Succ : successors(BB))
428*b5893f02SDimitry Andric if (Succ != TheOnlySucc) {
429*b5893f02SDimitry Andric DeadSuccessors.insert(Succ);
430*b5893f02SDimitry Andric // If our successor lies in a different loop, we don't want to remove
431*b5893f02SDimitry Andric // the one-input Phi because it is a LCSSA Phi.
432*b5893f02SDimitry Andric bool PreserveLCSSAPhi = !L.contains(Succ);
433*b5893f02SDimitry Andric Succ->removePredecessor(BB, PreserveLCSSAPhi);
434*b5893f02SDimitry Andric if (MSSAU)
435*b5893f02SDimitry Andric MSSAU->removeEdge(BB, Succ);
436*b5893f02SDimitry Andric } else
437*b5893f02SDimitry Andric ++TheOnlySuccDuplicates;
438*b5893f02SDimitry Andric
439*b5893f02SDimitry Andric assert(TheOnlySuccDuplicates > 0 && "Should be!");
440*b5893f02SDimitry Andric // If TheOnlySucc was BB's successor more than once, after transform it
441*b5893f02SDimitry Andric // will be its successor only once. Remove redundant inputs from
442*b5893f02SDimitry Andric // TheOnlySucc's Phis.
443*b5893f02SDimitry Andric bool PreserveLCSSAPhi = !L.contains(TheOnlySucc);
444*b5893f02SDimitry Andric for (unsigned Dup = 1; Dup < TheOnlySuccDuplicates; ++Dup)
445*b5893f02SDimitry Andric TheOnlySucc->removePredecessor(BB, PreserveLCSSAPhi);
446*b5893f02SDimitry Andric if (MSSAU && TheOnlySuccDuplicates > 1)
447*b5893f02SDimitry Andric MSSAU->removeDuplicatePhiEdgesBetween(BB, TheOnlySucc);
448*b5893f02SDimitry Andric
449*b5893f02SDimitry Andric IRBuilder<> Builder(BB->getContext());
450*b5893f02SDimitry Andric Instruction *Term = BB->getTerminator();
451*b5893f02SDimitry Andric Builder.SetInsertPoint(Term);
452*b5893f02SDimitry Andric Builder.CreateBr(TheOnlySucc);
453*b5893f02SDimitry Andric Term->eraseFromParent();
454*b5893f02SDimitry Andric
455*b5893f02SDimitry Andric for (auto *DeadSucc : DeadSuccessors)
456*b5893f02SDimitry Andric DTU.deleteEdge(BB, DeadSucc);
457*b5893f02SDimitry Andric
458*b5893f02SDimitry Andric ++NumTerminatorsFolded;
459*b5893f02SDimitry Andric }
460*b5893f02SDimitry Andric }
461*b5893f02SDimitry Andric
462*b5893f02SDimitry Andric public:
ConstantTerminatorFoldingImpl(Loop & L,LoopInfo & LI,DominatorTree & DT,ScalarEvolution & SE,MemorySSAUpdater * MSSAU)463*b5893f02SDimitry Andric ConstantTerminatorFoldingImpl(Loop &L, LoopInfo &LI, DominatorTree &DT,
464*b5893f02SDimitry Andric ScalarEvolution &SE,
465*b5893f02SDimitry Andric MemorySSAUpdater *MSSAU)
466*b5893f02SDimitry Andric : L(L), LI(LI), DT(DT), SE(SE), MSSAU(MSSAU) {}
run()467*b5893f02SDimitry Andric bool run() {
468*b5893f02SDimitry Andric assert(L.getLoopLatch() && "Should be single latch!");
469*b5893f02SDimitry Andric
470*b5893f02SDimitry Andric // Collect all available information about status of blocks after constant
471*b5893f02SDimitry Andric // folding.
472*b5893f02SDimitry Andric analyze();
473*b5893f02SDimitry Andric
474*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "In function " << L.getHeader()->getParent()->getName()
475*b5893f02SDimitry Andric << ": ");
476*b5893f02SDimitry Andric
477*b5893f02SDimitry Andric if (HasIrreducibleCFG) {
478*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Loops with irreducible CFG are not supported!\n");
479*b5893f02SDimitry Andric return false;
480*b5893f02SDimitry Andric }
481*b5893f02SDimitry Andric
482*b5893f02SDimitry Andric // Nothing to constant-fold.
483*b5893f02SDimitry Andric if (FoldCandidates.empty()) {
484*b5893f02SDimitry Andric LLVM_DEBUG(
485*b5893f02SDimitry Andric dbgs() << "No constant terminator folding candidates found in loop "
486*b5893f02SDimitry Andric << L.getHeader()->getName() << "\n");
487*b5893f02SDimitry Andric return false;
488*b5893f02SDimitry Andric }
489*b5893f02SDimitry Andric
490*b5893f02SDimitry Andric // TODO: Support deletion of the current loop.
491*b5893f02SDimitry Andric if (DeleteCurrentLoop) {
492*b5893f02SDimitry Andric LLVM_DEBUG(
493*b5893f02SDimitry Andric dbgs()
494*b5893f02SDimitry Andric << "Give up constant terminator folding in loop "
495*b5893f02SDimitry Andric << L.getHeader()->getName()
496*b5893f02SDimitry Andric << ": we don't currently support deletion of the current loop.\n");
497*b5893f02SDimitry Andric return false;
498*b5893f02SDimitry Andric }
499*b5893f02SDimitry Andric
500*b5893f02SDimitry Andric // TODO: Support blocks that are not dead, but also not in loop after the
501*b5893f02SDimitry Andric // folding.
502*b5893f02SDimitry Andric if (BlocksInLoopAfterFolding.size() + DeadLoopBlocks.size() !=
503*b5893f02SDimitry Andric L.getNumBlocks()) {
504*b5893f02SDimitry Andric LLVM_DEBUG(
505*b5893f02SDimitry Andric dbgs() << "Give up constant terminator folding in loop "
506*b5893f02SDimitry Andric << L.getHeader()->getName()
507*b5893f02SDimitry Andric << ": we don't currently"
508*b5893f02SDimitry Andric " support blocks that are not dead, but will stop "
509*b5893f02SDimitry Andric "being a part of the loop after constant-folding.\n");
510*b5893f02SDimitry Andric return false;
511*b5893f02SDimitry Andric }
512*b5893f02SDimitry Andric
513*b5893f02SDimitry Andric SE.forgetTopmostLoop(&L);
514*b5893f02SDimitry Andric // Dump analysis results.
515*b5893f02SDimitry Andric LLVM_DEBUG(dump());
516*b5893f02SDimitry Andric
517*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Constant-folding " << FoldCandidates.size()
518*b5893f02SDimitry Andric << " terminators in loop " << L.getHeader()->getName()
519*b5893f02SDimitry Andric << "\n");
520*b5893f02SDimitry Andric
521*b5893f02SDimitry Andric // Make the actual transforms.
522*b5893f02SDimitry Andric handleDeadExits();
523*b5893f02SDimitry Andric foldTerminators();
524*b5893f02SDimitry Andric
525*b5893f02SDimitry Andric if (!DeadLoopBlocks.empty()) {
526*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Deleting " << DeadLoopBlocks.size()
527*b5893f02SDimitry Andric << " dead blocks in loop " << L.getHeader()->getName()
528*b5893f02SDimitry Andric << "\n");
529*b5893f02SDimitry Andric deleteDeadLoopBlocks();
530*b5893f02SDimitry Andric }
531*b5893f02SDimitry Andric
532*b5893f02SDimitry Andric #ifndef NDEBUG
533*b5893f02SDimitry Andric // Make sure that we have preserved all data structures after the transform.
534*b5893f02SDimitry Andric DT.verify();
535*b5893f02SDimitry Andric assert(DT.isReachableFromEntry(L.getHeader()));
536*b5893f02SDimitry Andric LI.verify(DT);
537*b5893f02SDimitry Andric #endif
538*b5893f02SDimitry Andric
539*b5893f02SDimitry Andric return true;
540*b5893f02SDimitry Andric }
541*b5893f02SDimitry Andric };
542*b5893f02SDimitry Andric } // namespace
543*b5893f02SDimitry Andric
544*b5893f02SDimitry Andric /// Turn branches and switches with known constant conditions into unconditional
545*b5893f02SDimitry Andric /// branches.
constantFoldTerminators(Loop & L,DominatorTree & DT,LoopInfo & LI,ScalarEvolution & SE,MemorySSAUpdater * MSSAU)546*b5893f02SDimitry Andric static bool constantFoldTerminators(Loop &L, DominatorTree &DT, LoopInfo &LI,
547*b5893f02SDimitry Andric ScalarEvolution &SE,
548*b5893f02SDimitry Andric MemorySSAUpdater *MSSAU) {
549*b5893f02SDimitry Andric if (!EnableTermFolding)
550*b5893f02SDimitry Andric return false;
551*b5893f02SDimitry Andric
552*b5893f02SDimitry Andric // To keep things simple, only process loops with single latch. We
553*b5893f02SDimitry Andric // canonicalize most loops to this form. We can support multi-latch if needed.
554*b5893f02SDimitry Andric if (!L.getLoopLatch())
555*b5893f02SDimitry Andric return false;
556*b5893f02SDimitry Andric
557*b5893f02SDimitry Andric ConstantTerminatorFoldingImpl BranchFolder(L, LI, DT, SE, MSSAU);
558*b5893f02SDimitry Andric return BranchFolder.run();
559*b5893f02SDimitry Andric }
560*b5893f02SDimitry Andric
mergeBlocksIntoPredecessors(Loop & L,DominatorTree & DT,LoopInfo & LI,MemorySSAUpdater * MSSAU)561*b5893f02SDimitry Andric static bool mergeBlocksIntoPredecessors(Loop &L, DominatorTree &DT,
562*b5893f02SDimitry Andric LoopInfo &LI, MemorySSAUpdater *MSSAU) {
5633ca95b02SDimitry Andric bool Changed = false;
564*b5893f02SDimitry Andric DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
5653ca95b02SDimitry Andric // Copy blocks into a temporary array to avoid iterator invalidation issues
5663ca95b02SDimitry Andric // as we remove them.
567f37b6182SDimitry Andric SmallVector<WeakTrackingVH, 16> Blocks(L.blocks());
5683ca95b02SDimitry Andric
5693ca95b02SDimitry Andric for (auto &Block : Blocks) {
5703ca95b02SDimitry Andric // Attempt to merge blocks in the trivial case. Don't modify blocks which
5713ca95b02SDimitry Andric // belong to other loops.
5723ca95b02SDimitry Andric BasicBlock *Succ = cast_or_null<BasicBlock>(Block);
5733ca95b02SDimitry Andric if (!Succ)
5743ca95b02SDimitry Andric continue;
5753ca95b02SDimitry Andric
5763ca95b02SDimitry Andric BasicBlock *Pred = Succ->getSinglePredecessor();
5773ca95b02SDimitry Andric if (!Pred || !Pred->getSingleSuccessor() || LI.getLoopFor(Pred) != &L)
5783ca95b02SDimitry Andric continue;
5793ca95b02SDimitry Andric
5804ba319b5SDimitry Andric // Merge Succ into Pred and delete it.
581*b5893f02SDimitry Andric MergeBlockIntoPredecessor(Succ, &DTU, &LI, MSSAU);
5824ba319b5SDimitry Andric
5833ca95b02SDimitry Andric Changed = true;
5843ca95b02SDimitry Andric }
5853ca95b02SDimitry Andric
5863ca95b02SDimitry Andric return Changed;
5873ca95b02SDimitry Andric }
5883ca95b02SDimitry Andric
simplifyLoopCFG(Loop & L,DominatorTree & DT,LoopInfo & LI,ScalarEvolution & SE,MemorySSAUpdater * MSSAU)589*b5893f02SDimitry Andric static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI,
590*b5893f02SDimitry Andric ScalarEvolution &SE, MemorySSAUpdater *MSSAU) {
591*b5893f02SDimitry Andric bool Changed = false;
592*b5893f02SDimitry Andric
593*b5893f02SDimitry Andric // Constant-fold terminators with known constant conditions.
594*b5893f02SDimitry Andric Changed |= constantFoldTerminators(L, DT, LI, SE, MSSAU);
595*b5893f02SDimitry Andric
596*b5893f02SDimitry Andric // Eliminate unconditional branches by merging blocks into their predecessors.
597*b5893f02SDimitry Andric Changed |= mergeBlocksIntoPredecessors(L, DT, LI, MSSAU);
598*b5893f02SDimitry Andric
599*b5893f02SDimitry Andric if (Changed)
600*b5893f02SDimitry Andric SE.forgetTopmostLoop(&L);
601*b5893f02SDimitry Andric
602*b5893f02SDimitry Andric return Changed;
603*b5893f02SDimitry Andric }
604*b5893f02SDimitry Andric
run(Loop & L,LoopAnalysisManager & AM,LoopStandardAnalysisResults & AR,LPMUpdater &)605f1a29dd3SDimitry Andric PreservedAnalyses LoopSimplifyCFGPass::run(Loop &L, LoopAnalysisManager &AM,
606f1a29dd3SDimitry Andric LoopStandardAnalysisResults &AR,
607f1a29dd3SDimitry Andric LPMUpdater &) {
608*b5893f02SDimitry Andric Optional<MemorySSAUpdater> MSSAU;
609*b5893f02SDimitry Andric if (EnableMSSALoopDependency && AR.MSSA)
610*b5893f02SDimitry Andric MSSAU = MemorySSAUpdater(AR.MSSA);
611*b5893f02SDimitry Andric if (!simplifyLoopCFG(L, AR.DT, AR.LI, AR.SE,
612*b5893f02SDimitry Andric MSSAU.hasValue() ? MSSAU.getPointer() : nullptr))
6133ca95b02SDimitry Andric return PreservedAnalyses::all();
6147a7e6055SDimitry Andric
6153ca95b02SDimitry Andric return getLoopPassPreservedAnalyses();
6163ca95b02SDimitry Andric }
6173ca95b02SDimitry Andric
6183ca95b02SDimitry Andric namespace {
6193ca95b02SDimitry Andric class LoopSimplifyCFGLegacyPass : public LoopPass {
6203ca95b02SDimitry Andric public:
6213ca95b02SDimitry Andric static char ID; // Pass ID, replacement for typeid
LoopSimplifyCFGLegacyPass()6223ca95b02SDimitry Andric LoopSimplifyCFGLegacyPass() : LoopPass(ID) {
6233ca95b02SDimitry Andric initializeLoopSimplifyCFGLegacyPassPass(*PassRegistry::getPassRegistry());
6243ca95b02SDimitry Andric }
6253ca95b02SDimitry Andric
runOnLoop(Loop * L,LPPassManager &)6263ca95b02SDimitry Andric bool runOnLoop(Loop *L, LPPassManager &) override {
6273ca95b02SDimitry Andric if (skipLoop(L))
6283ca95b02SDimitry Andric return false;
6293ca95b02SDimitry Andric
6303ca95b02SDimitry Andric DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
6313ca95b02SDimitry Andric LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
6324ba319b5SDimitry Andric ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
633*b5893f02SDimitry Andric Optional<MemorySSAUpdater> MSSAU;
634*b5893f02SDimitry Andric if (EnableMSSALoopDependency) {
635*b5893f02SDimitry Andric MemorySSA *MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA();
636*b5893f02SDimitry Andric MSSAU = MemorySSAUpdater(MSSA);
637*b5893f02SDimitry Andric if (VerifyMemorySSA)
638*b5893f02SDimitry Andric MSSA->verifyMemorySSA();
639*b5893f02SDimitry Andric }
640*b5893f02SDimitry Andric return simplifyLoopCFG(*L, DT, LI, SE,
641*b5893f02SDimitry Andric MSSAU.hasValue() ? MSSAU.getPointer() : nullptr);
6423ca95b02SDimitry Andric }
6433ca95b02SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const6443ca95b02SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
645*b5893f02SDimitry Andric if (EnableMSSALoopDependency) {
646*b5893f02SDimitry Andric AU.addRequired<MemorySSAWrapperPass>();
647*b5893f02SDimitry Andric AU.addPreserved<MemorySSAWrapperPass>();
648*b5893f02SDimitry Andric }
6493ca95b02SDimitry Andric AU.addPreserved<DependenceAnalysisWrapperPass>();
6503ca95b02SDimitry Andric getLoopAnalysisUsage(AU);
6513ca95b02SDimitry Andric }
6523ca95b02SDimitry Andric };
6533ca95b02SDimitry Andric }
6543ca95b02SDimitry Andric
6553ca95b02SDimitry Andric char LoopSimplifyCFGLegacyPass::ID = 0;
6563ca95b02SDimitry Andric INITIALIZE_PASS_BEGIN(LoopSimplifyCFGLegacyPass, "loop-simplifycfg",
6573ca95b02SDimitry Andric "Simplify loop CFG", false, false)
INITIALIZE_PASS_DEPENDENCY(LoopPass)6583ca95b02SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LoopPass)
659*b5893f02SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
6603ca95b02SDimitry Andric INITIALIZE_PASS_END(LoopSimplifyCFGLegacyPass, "loop-simplifycfg",
6613ca95b02SDimitry Andric "Simplify loop CFG", false, false)
6623ca95b02SDimitry Andric
6633ca95b02SDimitry Andric Pass *llvm::createLoopSimplifyCFGPass() {
6643ca95b02SDimitry Andric return new LoopSimplifyCFGLegacyPass();
6653ca95b02SDimitry Andric }
666