1f4a2713aSLionel Sambuc //===-- LCSSA.cpp - Convert loops into loop-closed SSA form ---------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This pass transforms loops by placing phi nodes at the end of the loops for
11f4a2713aSLionel Sambuc // all values that are live across the loop boundary.  For example, it turns
12f4a2713aSLionel Sambuc // the left into the right code:
13f4a2713aSLionel Sambuc //
14f4a2713aSLionel Sambuc // for (...)                for (...)
15f4a2713aSLionel Sambuc //   if (c)                   if (c)
16f4a2713aSLionel Sambuc //     X1 = ...                 X1 = ...
17f4a2713aSLionel Sambuc //   else                     else
18f4a2713aSLionel Sambuc //     X2 = ...                 X2 = ...
19f4a2713aSLionel Sambuc //   X3 = phi(X1, X2)         X3 = phi(X1, X2)
20f4a2713aSLionel Sambuc // ... = X3 + 4             X4 = phi(X3)
21f4a2713aSLionel Sambuc //                          ... = X4 + 4
22f4a2713aSLionel Sambuc //
23f4a2713aSLionel Sambuc // This is still valid LLVM; the extra phi nodes are purely redundant, and will
24f4a2713aSLionel Sambuc // be trivially eliminated by InstCombine.  The major benefit of this
25f4a2713aSLionel Sambuc // transformation is that it makes many other loop optimizations, such as
26f4a2713aSLionel Sambuc // LoopUnswitching, simpler.
27f4a2713aSLionel Sambuc //
28f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
29f4a2713aSLionel Sambuc 
30f4a2713aSLionel Sambuc #include "llvm/Transforms/Scalar.h"
31f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h"
32f4a2713aSLionel Sambuc #include "llvm/ADT/Statistic.h"
33*0a6a1f1dSLionel Sambuc #include "llvm/Analysis/AliasAnalysis.h"
34f4a2713aSLionel Sambuc #include "llvm/Analysis/LoopPass.h"
35f4a2713aSLionel Sambuc #include "llvm/Analysis/ScalarEvolution.h"
36f4a2713aSLionel Sambuc #include "llvm/IR/Constants.h"
37*0a6a1f1dSLionel Sambuc #include "llvm/IR/Dominators.h"
38f4a2713aSLionel Sambuc #include "llvm/IR/Function.h"
39f4a2713aSLionel Sambuc #include "llvm/IR/Instructions.h"
40*0a6a1f1dSLionel Sambuc #include "llvm/IR/PredIteratorCache.h"
41f4a2713aSLionel Sambuc #include "llvm/Pass.h"
42*0a6a1f1dSLionel Sambuc #include "llvm/Transforms/Utils/LoopUtils.h"
43f4a2713aSLionel Sambuc #include "llvm/Transforms/Utils/SSAUpdater.h"
44f4a2713aSLionel Sambuc using namespace llvm;
45f4a2713aSLionel Sambuc 
46*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "lcssa"
47*0a6a1f1dSLionel Sambuc 
48f4a2713aSLionel Sambuc STATISTIC(NumLCSSA, "Number of live out of a loop variables");
49f4a2713aSLionel Sambuc 
50*0a6a1f1dSLionel Sambuc /// Return true if the specified block is in the list.
isExitBlock(BasicBlock * BB,const SmallVectorImpl<BasicBlock * > & ExitBlocks)51f4a2713aSLionel Sambuc static bool isExitBlock(BasicBlock *BB,
52f4a2713aSLionel Sambuc                         const SmallVectorImpl<BasicBlock *> &ExitBlocks) {
53f4a2713aSLionel Sambuc   for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
54f4a2713aSLionel Sambuc     if (ExitBlocks[i] == BB)
55f4a2713aSLionel Sambuc       return true;
56f4a2713aSLionel Sambuc   return false;
57f4a2713aSLionel Sambuc }
58f4a2713aSLionel Sambuc 
59*0a6a1f1dSLionel Sambuc /// Given an instruction in the loop, check to see if it has any uses that are
60*0a6a1f1dSLionel Sambuc /// outside the current loop.  If so, insert LCSSA PHI nodes and rewrite the
61*0a6a1f1dSLionel Sambuc /// uses.
processInstruction(Loop & L,Instruction & Inst,DominatorTree & DT,const SmallVectorImpl<BasicBlock * > & ExitBlocks,PredIteratorCache & PredCache,LoopInfo * LI)62*0a6a1f1dSLionel Sambuc static bool processInstruction(Loop &L, Instruction &Inst, DominatorTree &DT,
63*0a6a1f1dSLionel Sambuc                                const SmallVectorImpl<BasicBlock *> &ExitBlocks,
64*0a6a1f1dSLionel Sambuc                                PredIteratorCache &PredCache, LoopInfo *LI) {
65f4a2713aSLionel Sambuc   SmallVector<Use *, 16> UsesToRewrite;
66f4a2713aSLionel Sambuc 
67*0a6a1f1dSLionel Sambuc   BasicBlock *InstBB = Inst.getParent();
68f4a2713aSLionel Sambuc 
69*0a6a1f1dSLionel Sambuc   for (Use &U : Inst.uses()) {
70*0a6a1f1dSLionel Sambuc     Instruction *User = cast<Instruction>(U.getUser());
71*0a6a1f1dSLionel Sambuc     BasicBlock *UserBB = User->getParent();
72*0a6a1f1dSLionel Sambuc     if (PHINode *PN = dyn_cast<PHINode>(User))
73*0a6a1f1dSLionel Sambuc       UserBB = PN->getIncomingBlock(U);
74f4a2713aSLionel Sambuc 
75*0a6a1f1dSLionel Sambuc     if (InstBB != UserBB && !L.contains(UserBB))
76*0a6a1f1dSLionel Sambuc       UsesToRewrite.push_back(&U);
77f4a2713aSLionel Sambuc   }
78f4a2713aSLionel Sambuc 
79f4a2713aSLionel Sambuc   // If there are no uses outside the loop, exit with no change.
80*0a6a1f1dSLionel Sambuc   if (UsesToRewrite.empty())
81*0a6a1f1dSLionel Sambuc     return false;
82f4a2713aSLionel Sambuc 
83f4a2713aSLionel Sambuc   ++NumLCSSA; // We are applying the transformation
84f4a2713aSLionel Sambuc 
85f4a2713aSLionel Sambuc   // Invoke instructions are special in that their result value is not available
86*0a6a1f1dSLionel Sambuc   // along their unwind edge. The code below tests to see whether DomBB
87*0a6a1f1dSLionel Sambuc   // dominates
88f4a2713aSLionel Sambuc   // the value, so adjust DomBB to the normal destination block, which is
89f4a2713aSLionel Sambuc   // effectively where the value is first usable.
90*0a6a1f1dSLionel Sambuc   BasicBlock *DomBB = Inst.getParent();
91*0a6a1f1dSLionel Sambuc   if (InvokeInst *Inv = dyn_cast<InvokeInst>(&Inst))
92f4a2713aSLionel Sambuc     DomBB = Inv->getNormalDest();
93f4a2713aSLionel Sambuc 
94*0a6a1f1dSLionel Sambuc   DomTreeNode *DomNode = DT.getNode(DomBB);
95f4a2713aSLionel Sambuc 
96f4a2713aSLionel Sambuc   SmallVector<PHINode *, 16> AddedPHIs;
97*0a6a1f1dSLionel Sambuc   SmallVector<PHINode *, 8> PostProcessPHIs;
98f4a2713aSLionel Sambuc 
99f4a2713aSLionel Sambuc   SSAUpdater SSAUpdate;
100*0a6a1f1dSLionel Sambuc   SSAUpdate.Initialize(Inst.getType(), Inst.getName());
101f4a2713aSLionel Sambuc 
102f4a2713aSLionel Sambuc   // Insert the LCSSA phi's into all of the exit blocks dominated by the
103f4a2713aSLionel Sambuc   // value, and add them to the Phi's map.
104f4a2713aSLionel Sambuc   for (SmallVectorImpl<BasicBlock *>::const_iterator BBI = ExitBlocks.begin(),
105*0a6a1f1dSLionel Sambuc                                                      BBE = ExitBlocks.end();
106*0a6a1f1dSLionel Sambuc        BBI != BBE; ++BBI) {
107f4a2713aSLionel Sambuc     BasicBlock *ExitBB = *BBI;
108*0a6a1f1dSLionel Sambuc     if (!DT.dominates(DomNode, DT.getNode(ExitBB)))
109*0a6a1f1dSLionel Sambuc       continue;
110f4a2713aSLionel Sambuc 
111f4a2713aSLionel Sambuc     // If we already inserted something for this BB, don't reprocess it.
112*0a6a1f1dSLionel Sambuc     if (SSAUpdate.HasValueForBlock(ExitBB))
113*0a6a1f1dSLionel Sambuc       continue;
114f4a2713aSLionel Sambuc 
115*0a6a1f1dSLionel Sambuc     PHINode *PN = PHINode::Create(Inst.getType(), PredCache.GetNumPreds(ExitBB),
116*0a6a1f1dSLionel Sambuc                                   Inst.getName() + ".lcssa", ExitBB->begin());
117f4a2713aSLionel Sambuc 
118f4a2713aSLionel Sambuc     // Add inputs from inside the loop for this PHI.
119f4a2713aSLionel Sambuc     for (BasicBlock **PI = PredCache.GetPreds(ExitBB); *PI; ++PI) {
120*0a6a1f1dSLionel Sambuc       PN->addIncoming(&Inst, *PI);
121f4a2713aSLionel Sambuc 
122f4a2713aSLionel Sambuc       // If the exit block has a predecessor not within the loop, arrange for
123f4a2713aSLionel Sambuc       // the incoming value use corresponding to that predecessor to be
124f4a2713aSLionel Sambuc       // rewritten in terms of a different LCSSA PHI.
125*0a6a1f1dSLionel Sambuc       if (!L.contains(*PI))
126f4a2713aSLionel Sambuc         UsesToRewrite.push_back(
127*0a6a1f1dSLionel Sambuc             &PN->getOperandUse(PN->getOperandNumForIncomingValue(
128*0a6a1f1dSLionel Sambuc                  PN->getNumIncomingValues() - 1)));
129f4a2713aSLionel Sambuc     }
130f4a2713aSLionel Sambuc 
131f4a2713aSLionel Sambuc     AddedPHIs.push_back(PN);
132f4a2713aSLionel Sambuc 
133f4a2713aSLionel Sambuc     // Remember that this phi makes the value alive in this block.
134f4a2713aSLionel Sambuc     SSAUpdate.AddAvailableValue(ExitBB, PN);
135*0a6a1f1dSLionel Sambuc 
136*0a6a1f1dSLionel Sambuc     // LoopSimplify might fail to simplify some loops (e.g. when indirect
137*0a6a1f1dSLionel Sambuc     // branches are involved). In such situations, it might happen that an exit
138*0a6a1f1dSLionel Sambuc     // for Loop L1 is the header of a disjoint Loop L2. Thus, when we create
139*0a6a1f1dSLionel Sambuc     // PHIs in such an exit block, we are also inserting PHIs into L2's header.
140*0a6a1f1dSLionel Sambuc     // This could break LCSSA form for L2 because these inserted PHIs can also
141*0a6a1f1dSLionel Sambuc     // have uses outside of L2. Remember all PHIs in such situation as to
142*0a6a1f1dSLionel Sambuc     // revisit than later on. FIXME: Remove this if indirectbr support into
143*0a6a1f1dSLionel Sambuc     // LoopSimplify gets improved.
144*0a6a1f1dSLionel Sambuc     if (auto *OtherLoop = LI->getLoopFor(ExitBB))
145*0a6a1f1dSLionel Sambuc       if (!L.contains(OtherLoop))
146*0a6a1f1dSLionel Sambuc         PostProcessPHIs.push_back(PN);
147f4a2713aSLionel Sambuc   }
148f4a2713aSLionel Sambuc 
149f4a2713aSLionel Sambuc   // Rewrite all uses outside the loop in terms of the new PHIs we just
150f4a2713aSLionel Sambuc   // inserted.
151f4a2713aSLionel Sambuc   for (unsigned i = 0, e = UsesToRewrite.size(); i != e; ++i) {
152f4a2713aSLionel Sambuc     // If this use is in an exit block, rewrite to use the newly inserted PHI.
153f4a2713aSLionel Sambuc     // This is required for correctness because SSAUpdate doesn't handle uses in
154f4a2713aSLionel Sambuc     // the same block.  It assumes the PHI we inserted is at the end of the
155f4a2713aSLionel Sambuc     // block.
156f4a2713aSLionel Sambuc     Instruction *User = cast<Instruction>(UsesToRewrite[i]->getUser());
157f4a2713aSLionel Sambuc     BasicBlock *UserBB = User->getParent();
158f4a2713aSLionel Sambuc     if (PHINode *PN = dyn_cast<PHINode>(User))
159f4a2713aSLionel Sambuc       UserBB = PN->getIncomingBlock(*UsesToRewrite[i]);
160f4a2713aSLionel Sambuc 
161*0a6a1f1dSLionel Sambuc     if (isa<PHINode>(UserBB->begin()) && isExitBlock(UserBB, ExitBlocks)) {
162f4a2713aSLionel Sambuc       // Tell the VHs that the uses changed. This updates SCEV's caches.
163f4a2713aSLionel Sambuc       if (UsesToRewrite[i]->get()->hasValueHandle())
164f4a2713aSLionel Sambuc         ValueHandleBase::ValueIsRAUWd(*UsesToRewrite[i], UserBB->begin());
165f4a2713aSLionel Sambuc       UsesToRewrite[i]->set(UserBB->begin());
166f4a2713aSLionel Sambuc       continue;
167f4a2713aSLionel Sambuc     }
168f4a2713aSLionel Sambuc 
169f4a2713aSLionel Sambuc     // Otherwise, do full PHI insertion.
170f4a2713aSLionel Sambuc     SSAUpdate.RewriteUse(*UsesToRewrite[i]);
171f4a2713aSLionel Sambuc   }
172f4a2713aSLionel Sambuc 
173*0a6a1f1dSLionel Sambuc   // Post process PHI instructions that were inserted into another disjoint loop
174*0a6a1f1dSLionel Sambuc   // and update their exits properly.
175*0a6a1f1dSLionel Sambuc   for (auto *I : PostProcessPHIs) {
176*0a6a1f1dSLionel Sambuc     if (I->use_empty())
177*0a6a1f1dSLionel Sambuc       continue;
178*0a6a1f1dSLionel Sambuc 
179*0a6a1f1dSLionel Sambuc     BasicBlock *PHIBB = I->getParent();
180*0a6a1f1dSLionel Sambuc     Loop *OtherLoop = LI->getLoopFor(PHIBB);
181*0a6a1f1dSLionel Sambuc     SmallVector<BasicBlock *, 8> EBs;
182*0a6a1f1dSLionel Sambuc     OtherLoop->getExitBlocks(EBs);
183*0a6a1f1dSLionel Sambuc     if (EBs.empty())
184*0a6a1f1dSLionel Sambuc       continue;
185*0a6a1f1dSLionel Sambuc 
186*0a6a1f1dSLionel Sambuc     // Recurse and re-process each PHI instruction. FIXME: we should really
187*0a6a1f1dSLionel Sambuc     // convert this entire thing to a worklist approach where we process a
188*0a6a1f1dSLionel Sambuc     // vector of instructions...
189*0a6a1f1dSLionel Sambuc     processInstruction(*OtherLoop, *I, DT, EBs, PredCache, LI);
190*0a6a1f1dSLionel Sambuc   }
191*0a6a1f1dSLionel Sambuc 
192f4a2713aSLionel Sambuc   // Remove PHI nodes that did not have any uses rewritten.
193f4a2713aSLionel Sambuc   for (unsigned i = 0, e = AddedPHIs.size(); i != e; ++i) {
194f4a2713aSLionel Sambuc     if (AddedPHIs[i]->use_empty())
195f4a2713aSLionel Sambuc       AddedPHIs[i]->eraseFromParent();
196f4a2713aSLionel Sambuc   }
197f4a2713aSLionel Sambuc 
198f4a2713aSLionel Sambuc   return true;
199f4a2713aSLionel Sambuc }
200f4a2713aSLionel Sambuc 
201*0a6a1f1dSLionel Sambuc /// Return true if the specified block dominates at least
202*0a6a1f1dSLionel Sambuc /// one of the blocks in the specified list.
203*0a6a1f1dSLionel Sambuc static bool
blockDominatesAnExit(BasicBlock * BB,DominatorTree & DT,const SmallVectorImpl<BasicBlock * > & ExitBlocks)204*0a6a1f1dSLionel Sambuc blockDominatesAnExit(BasicBlock *BB,
205*0a6a1f1dSLionel Sambuc                      DominatorTree &DT,
206*0a6a1f1dSLionel Sambuc                      const SmallVectorImpl<BasicBlock *> &ExitBlocks) {
207*0a6a1f1dSLionel Sambuc   DomTreeNode *DomNode = DT.getNode(BB);
208*0a6a1f1dSLionel Sambuc   for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
209*0a6a1f1dSLionel Sambuc     if (DT.dominates(DomNode, DT.getNode(ExitBlocks[i])))
210*0a6a1f1dSLionel Sambuc       return true;
211*0a6a1f1dSLionel Sambuc 
212*0a6a1f1dSLionel Sambuc   return false;
213*0a6a1f1dSLionel Sambuc }
214*0a6a1f1dSLionel Sambuc 
formLCSSA(Loop & L,DominatorTree & DT,LoopInfo * LI,ScalarEvolution * SE)215*0a6a1f1dSLionel Sambuc bool llvm::formLCSSA(Loop &L, DominatorTree &DT, LoopInfo *LI,
216*0a6a1f1dSLionel Sambuc                      ScalarEvolution *SE) {
217*0a6a1f1dSLionel Sambuc   bool Changed = false;
218*0a6a1f1dSLionel Sambuc 
219*0a6a1f1dSLionel Sambuc   // Get the set of exiting blocks.
220*0a6a1f1dSLionel Sambuc   SmallVector<BasicBlock *, 8> ExitBlocks;
221*0a6a1f1dSLionel Sambuc   L.getExitBlocks(ExitBlocks);
222*0a6a1f1dSLionel Sambuc 
223*0a6a1f1dSLionel Sambuc   if (ExitBlocks.empty())
224*0a6a1f1dSLionel Sambuc     return false;
225*0a6a1f1dSLionel Sambuc 
226*0a6a1f1dSLionel Sambuc   PredIteratorCache PredCache;
227*0a6a1f1dSLionel Sambuc 
228*0a6a1f1dSLionel Sambuc   // Look at all the instructions in the loop, checking to see if they have uses
229*0a6a1f1dSLionel Sambuc   // outside the loop.  If so, rewrite those uses.
230*0a6a1f1dSLionel Sambuc   for (Loop::block_iterator BBI = L.block_begin(), BBE = L.block_end();
231*0a6a1f1dSLionel Sambuc        BBI != BBE; ++BBI) {
232*0a6a1f1dSLionel Sambuc     BasicBlock *BB = *BBI;
233*0a6a1f1dSLionel Sambuc 
234*0a6a1f1dSLionel Sambuc     // For large loops, avoid use-scanning by using dominance information:  In
235*0a6a1f1dSLionel Sambuc     // particular, if a block does not dominate any of the loop exits, then none
236*0a6a1f1dSLionel Sambuc     // of the values defined in the block could be used outside the loop.
237*0a6a1f1dSLionel Sambuc     if (!blockDominatesAnExit(BB, DT, ExitBlocks))
238*0a6a1f1dSLionel Sambuc       continue;
239*0a6a1f1dSLionel Sambuc 
240*0a6a1f1dSLionel Sambuc     for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
241*0a6a1f1dSLionel Sambuc       // Reject two common cases fast: instructions with no uses (like stores)
242*0a6a1f1dSLionel Sambuc       // and instructions with one use that is in the same block as this.
243*0a6a1f1dSLionel Sambuc       if (I->use_empty() ||
244*0a6a1f1dSLionel Sambuc           (I->hasOneUse() && I->user_back()->getParent() == BB &&
245*0a6a1f1dSLionel Sambuc            !isa<PHINode>(I->user_back())))
246*0a6a1f1dSLionel Sambuc         continue;
247*0a6a1f1dSLionel Sambuc 
248*0a6a1f1dSLionel Sambuc       Changed |= processInstruction(L, *I, DT, ExitBlocks, PredCache, LI);
249*0a6a1f1dSLionel Sambuc     }
250*0a6a1f1dSLionel Sambuc   }
251*0a6a1f1dSLionel Sambuc 
252*0a6a1f1dSLionel Sambuc   // If we modified the code, remove any caches about the loop from SCEV to
253*0a6a1f1dSLionel Sambuc   // avoid dangling entries.
254*0a6a1f1dSLionel Sambuc   // FIXME: This is a big hammer, can we clear the cache more selectively?
255*0a6a1f1dSLionel Sambuc   if (SE && Changed)
256*0a6a1f1dSLionel Sambuc     SE->forgetLoop(&L);
257*0a6a1f1dSLionel Sambuc 
258*0a6a1f1dSLionel Sambuc   assert(L.isLCSSAForm(DT));
259*0a6a1f1dSLionel Sambuc 
260*0a6a1f1dSLionel Sambuc   return Changed;
261*0a6a1f1dSLionel Sambuc }
262*0a6a1f1dSLionel Sambuc 
263*0a6a1f1dSLionel Sambuc /// Process a loop nest depth first.
formLCSSARecursively(Loop & L,DominatorTree & DT,LoopInfo * LI,ScalarEvolution * SE)264*0a6a1f1dSLionel Sambuc bool llvm::formLCSSARecursively(Loop &L, DominatorTree &DT, LoopInfo *LI,
265*0a6a1f1dSLionel Sambuc                                 ScalarEvolution *SE) {
266*0a6a1f1dSLionel Sambuc   bool Changed = false;
267*0a6a1f1dSLionel Sambuc 
268*0a6a1f1dSLionel Sambuc   // Recurse depth-first through inner loops.
269*0a6a1f1dSLionel Sambuc   for (Loop::iterator I = L.begin(), E = L.end(); I != E; ++I)
270*0a6a1f1dSLionel Sambuc     Changed |= formLCSSARecursively(**I, DT, LI, SE);
271*0a6a1f1dSLionel Sambuc 
272*0a6a1f1dSLionel Sambuc   Changed |= formLCSSA(L, DT, LI, SE);
273*0a6a1f1dSLionel Sambuc   return Changed;
274*0a6a1f1dSLionel Sambuc }
275*0a6a1f1dSLionel Sambuc 
276*0a6a1f1dSLionel Sambuc namespace {
277*0a6a1f1dSLionel Sambuc struct LCSSA : public FunctionPass {
278*0a6a1f1dSLionel Sambuc   static char ID; // Pass identification, replacement for typeid
LCSSA__anon1800486f0111::LCSSA279*0a6a1f1dSLionel Sambuc   LCSSA() : FunctionPass(ID) {
280*0a6a1f1dSLionel Sambuc     initializeLCSSAPass(*PassRegistry::getPassRegistry());
281*0a6a1f1dSLionel Sambuc   }
282*0a6a1f1dSLionel Sambuc 
283*0a6a1f1dSLionel Sambuc   // Cached analysis information for the current function.
284*0a6a1f1dSLionel Sambuc   DominatorTree *DT;
285*0a6a1f1dSLionel Sambuc   LoopInfo *LI;
286*0a6a1f1dSLionel Sambuc   ScalarEvolution *SE;
287*0a6a1f1dSLionel Sambuc 
288*0a6a1f1dSLionel Sambuc   bool runOnFunction(Function &F) override;
289*0a6a1f1dSLionel Sambuc 
290*0a6a1f1dSLionel Sambuc   /// This transformation requires natural loop information & requires that
291*0a6a1f1dSLionel Sambuc   /// loop preheaders be inserted into the CFG.  It maintains both of these,
292*0a6a1f1dSLionel Sambuc   /// as well as the CFG.  It also requires dominator information.
getAnalysisUsage__anon1800486f0111::LCSSA293*0a6a1f1dSLionel Sambuc   void getAnalysisUsage(AnalysisUsage &AU) const override {
294*0a6a1f1dSLionel Sambuc     AU.setPreservesCFG();
295*0a6a1f1dSLionel Sambuc 
296*0a6a1f1dSLionel Sambuc     AU.addRequired<DominatorTreeWrapperPass>();
297*0a6a1f1dSLionel Sambuc     AU.addRequired<LoopInfo>();
298*0a6a1f1dSLionel Sambuc     AU.addPreservedID(LoopSimplifyID);
299*0a6a1f1dSLionel Sambuc     AU.addPreserved<AliasAnalysis>();
300*0a6a1f1dSLionel Sambuc     AU.addPreserved<ScalarEvolution>();
301*0a6a1f1dSLionel Sambuc   }
302*0a6a1f1dSLionel Sambuc 
303*0a6a1f1dSLionel Sambuc private:
304*0a6a1f1dSLionel Sambuc   void verifyAnalysis() const override;
305*0a6a1f1dSLionel Sambuc };
306*0a6a1f1dSLionel Sambuc }
307*0a6a1f1dSLionel Sambuc 
308*0a6a1f1dSLionel Sambuc char LCSSA::ID = 0;
309*0a6a1f1dSLionel Sambuc INITIALIZE_PASS_BEGIN(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)310*0a6a1f1dSLionel Sambuc INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
311*0a6a1f1dSLionel Sambuc INITIALIZE_PASS_DEPENDENCY(LoopInfo)
312*0a6a1f1dSLionel Sambuc INITIALIZE_PASS_END(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false)
313*0a6a1f1dSLionel Sambuc 
314*0a6a1f1dSLionel Sambuc Pass *llvm::createLCSSAPass() { return new LCSSA(); }
315*0a6a1f1dSLionel Sambuc char &llvm::LCSSAID = LCSSA::ID;
316*0a6a1f1dSLionel Sambuc 
317*0a6a1f1dSLionel Sambuc 
318*0a6a1f1dSLionel Sambuc /// Process all loops in the function, inner-most out.
runOnFunction(Function & F)319*0a6a1f1dSLionel Sambuc bool LCSSA::runOnFunction(Function &F) {
320*0a6a1f1dSLionel Sambuc   bool Changed = false;
321*0a6a1f1dSLionel Sambuc   LI = &getAnalysis<LoopInfo>();
322*0a6a1f1dSLionel Sambuc   DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
323*0a6a1f1dSLionel Sambuc   SE = getAnalysisIfAvailable<ScalarEvolution>();
324*0a6a1f1dSLionel Sambuc 
325*0a6a1f1dSLionel Sambuc   // Simplify each loop nest in the function.
326*0a6a1f1dSLionel Sambuc   for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
327*0a6a1f1dSLionel Sambuc     Changed |= formLCSSARecursively(**I, *DT, LI, SE);
328*0a6a1f1dSLionel Sambuc 
329*0a6a1f1dSLionel Sambuc   return Changed;
330*0a6a1f1dSLionel Sambuc }
331*0a6a1f1dSLionel Sambuc 
verifyLoop(Loop & L,DominatorTree & DT)332*0a6a1f1dSLionel Sambuc static void verifyLoop(Loop &L, DominatorTree &DT) {
333*0a6a1f1dSLionel Sambuc   // Recurse depth-first through inner loops.
334*0a6a1f1dSLionel Sambuc   for (Loop::iterator LI = L.begin(), LE = L.end(); LI != LE; ++LI)
335*0a6a1f1dSLionel Sambuc     verifyLoop(**LI, DT);
336*0a6a1f1dSLionel Sambuc 
337*0a6a1f1dSLionel Sambuc   // Check the special guarantees that LCSSA makes.
338*0a6a1f1dSLionel Sambuc   //assert(L.isLCSSAForm(DT) && "LCSSA form not preserved!");
339*0a6a1f1dSLionel Sambuc }
340*0a6a1f1dSLionel Sambuc 
verifyAnalysis() const341*0a6a1f1dSLionel Sambuc void LCSSA::verifyAnalysis() const {
342*0a6a1f1dSLionel Sambuc   // Verify each loop nest in the function, assuming LI still points at that
343*0a6a1f1dSLionel Sambuc   // function's loop info.
344*0a6a1f1dSLionel Sambuc   for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
345*0a6a1f1dSLionel Sambuc     verifyLoop(**I, *DT);
346*0a6a1f1dSLionel Sambuc }
347