1 //===- llvm/Analysis/LoopInfoImpl.h - Natural Loop Calculator ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the generic implementation of LoopInfo used for both Loops and
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_LOOPINFOIMPL_H
16 #define LLVM_ANALYSIS_LOOPINFOIMPL_H
18 #include "llvm/ADT/PostOrderIterator.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Analysis/LoopInfo.h"
24 //===----------------------------------------------------------------------===//
25 // APIs for simple analysis of the loop. See header notes.
27 /// getExitingBlocks - Return all blocks inside the loop that have successors
28 /// outside of the loop. These are the blocks _inside of the current loop_
29 /// which branch out. The returned list is always unique.
31 template<class BlockT, class LoopT>
32 void LoopBase<BlockT, LoopT>::
33 getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const {
34 typedef GraphTraits<BlockT*> BlockTraits;
35 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
36 for (typename BlockTraits::ChildIteratorType I =
37 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
40 // Not in current loop? It must be an exit block.
41 ExitingBlocks.push_back(*BI);
46 /// getExitingBlock - If getExitingBlocks would return exactly one block,
47 /// return that block. Otherwise return null.
48 template<class BlockT, class LoopT>
49 BlockT *LoopBase<BlockT, LoopT>::getExitingBlock() const {
50 SmallVector<BlockT*, 8> ExitingBlocks;
51 getExitingBlocks(ExitingBlocks);
52 if (ExitingBlocks.size() == 1)
53 return ExitingBlocks[0];
57 /// getExitBlocks - Return all of the successor blocks of this loop. These
58 /// are the blocks _outside of the current loop_ which are branched to.
60 template<class BlockT, class LoopT>
61 void LoopBase<BlockT, LoopT>::
62 getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
63 typedef GraphTraits<BlockT*> BlockTraits;
64 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
65 for (typename BlockTraits::ChildIteratorType I =
66 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
69 // Not in current loop? It must be an exit block.
70 ExitBlocks.push_back(*I);
73 /// getExitBlock - If getExitBlocks would return exactly one block,
74 /// return that block. Otherwise return null.
75 template<class BlockT, class LoopT>
76 BlockT *LoopBase<BlockT, LoopT>::getExitBlock() const {
77 SmallVector<BlockT*, 8> ExitBlocks;
78 getExitBlocks(ExitBlocks);
79 if (ExitBlocks.size() == 1)
84 /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
85 template<class BlockT, class LoopT>
86 void LoopBase<BlockT, LoopT>::
87 getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const {
88 typedef GraphTraits<BlockT*> BlockTraits;
89 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
90 for (typename BlockTraits::ChildIteratorType I =
91 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
94 // Not in current loop? It must be an exit block.
95 ExitEdges.push_back(Edge(*BI, *I));
98 /// getLoopPreheader - If there is a preheader for this loop, return it. A
99 /// loop has a preheader if there is only one edge to the header of the loop
100 /// from outside of the loop. If this is the case, the block branching to the
101 /// header of the loop is the preheader node.
103 /// This method returns null if there is no preheader for the loop.
105 template<class BlockT, class LoopT>
106 BlockT *LoopBase<BlockT, LoopT>::getLoopPreheader() const {
107 // Keep track of nodes outside the loop branching to the header...
108 BlockT *Out = getLoopPredecessor();
111 // Make sure there is only one exit out of the preheader.
112 typedef GraphTraits<BlockT*> BlockTraits;
113 typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
115 if (SI != BlockTraits::child_end(Out))
116 return 0; // Multiple exits from the block, must not be a preheader.
118 // The predecessor has exactly one successor, so it is a preheader.
122 /// getLoopPredecessor - If the given loop's header has exactly one unique
123 /// predecessor outside the loop, return it. Otherwise return null.
124 /// This is less strict that the loop "preheader" concept, which requires
125 /// the predecessor to have exactly one successor.
127 template<class BlockT, class LoopT>
128 BlockT *LoopBase<BlockT, LoopT>::getLoopPredecessor() const {
129 // Keep track of nodes outside the loop branching to the header...
132 // Loop over the predecessors of the header node...
133 BlockT *Header = getHeader();
134 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
135 for (typename InvBlockTraits::ChildIteratorType PI =
136 InvBlockTraits::child_begin(Header),
137 PE = InvBlockTraits::child_end(Header); PI != PE; ++PI) {
138 typename InvBlockTraits::NodeType *N = *PI;
139 if (!contains(N)) { // If the block is not in the loop...
141 return 0; // Multiple predecessors outside the loop
146 // Make sure there is only one exit out of the preheader.
147 assert(Out && "Header of loop has no predecessors from outside loop?");
151 /// getLoopLatch - If there is a single latch block for this loop, return it.
152 /// A latch block is a block that contains a branch back to the header.
153 template<class BlockT, class LoopT>
154 BlockT *LoopBase<BlockT, LoopT>::getLoopLatch() const {
155 BlockT *Header = getHeader();
156 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
157 typename InvBlockTraits::ChildIteratorType PI =
158 InvBlockTraits::child_begin(Header);
159 typename InvBlockTraits::ChildIteratorType PE =
160 InvBlockTraits::child_end(Header);
162 for (; PI != PE; ++PI) {
163 typename InvBlockTraits::NodeType *N = *PI;
173 //===----------------------------------------------------------------------===//
174 // APIs for updating loop information after changing the CFG
177 /// addBasicBlockToLoop - This method is used by other analyses to update loop
178 /// information. NewBB is set to be a new member of the current loop.
179 /// Because of this, it is added as a member of all parent loops, and is added
180 /// to the specified LoopInfo object as being in the current basic block. It
181 /// is not valid to replace the loop header with this method.
183 template<class BlockT, class LoopT>
184 void LoopBase<BlockT, LoopT>::
185 addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LIB) {
186 assert((Blocks.empty() || LIB[getHeader()] == this) &&
187 "Incorrect LI specified for this loop!");
188 assert(NewBB && "Cannot add a null basic block to the loop!");
189 assert(LIB[NewBB] == 0 && "BasicBlock already in the loop!");
191 LoopT *L = static_cast<LoopT *>(this);
193 // Add the loop mapping to the LoopInfo object...
194 LIB.BBMap[NewBB] = L;
196 // Add the basic block to this loop and all parent loops...
198 L->addBlockEntry(NewBB);
199 L = L->getParentLoop();
203 /// replaceChildLoopWith - This is used when splitting loops up. It replaces
204 /// the OldChild entry in our children list with NewChild, and updates the
205 /// parent pointer of OldChild to be null and the NewChild to be this loop.
206 /// This updates the loop depth of the new child.
207 template<class BlockT, class LoopT>
208 void LoopBase<BlockT, LoopT>::
209 replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild) {
210 assert(OldChild->ParentLoop == this && "This loop is already broken!");
211 assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
212 typename std::vector<LoopT *>::iterator I =
213 std::find(SubLoops.begin(), SubLoops.end(), OldChild);
214 assert(I != SubLoops.end() && "OldChild not in loop!");
216 OldChild->ParentLoop = 0;
217 NewChild->ParentLoop = static_cast<LoopT *>(this);
220 /// verifyLoop - Verify loop structure
221 template<class BlockT, class LoopT>
222 void LoopBase<BlockT, LoopT>::verifyLoop() const {
224 assert(!Blocks.empty() && "Loop header is missing");
226 // Setup for using a depth-first iterator to visit every block in the loop.
227 SmallVector<BlockT*, 8> ExitBBs;
228 getExitBlocks(ExitBBs);
229 llvm::SmallPtrSet<BlockT*, 8> VisitSet;
230 VisitSet.insert(ExitBBs.begin(), ExitBBs.end());
231 df_ext_iterator<BlockT*, llvm::SmallPtrSet<BlockT*, 8> >
232 BI = df_ext_begin(getHeader(), VisitSet),
233 BE = df_ext_end(getHeader(), VisitSet);
235 // Keep track of the number of BBs visited.
236 unsigned NumVisited = 0;
238 // Check the individual blocks.
239 for ( ; BI != BE; ++BI) {
241 bool HasInsideLoopSuccs = false;
242 bool HasInsideLoopPreds = false;
243 SmallVector<BlockT *, 2> OutsideLoopPreds;
245 typedef GraphTraits<BlockT*> BlockTraits;
246 for (typename BlockTraits::ChildIteratorType SI =
247 BlockTraits::child_begin(BB), SE = BlockTraits::child_end(BB);
250 HasInsideLoopSuccs = true;
253 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
254 for (typename InvBlockTraits::ChildIteratorType PI =
255 InvBlockTraits::child_begin(BB), PE = InvBlockTraits::child_end(BB);
259 HasInsideLoopPreds = true;
261 OutsideLoopPreds.push_back(N);
264 if (BB == getHeader()) {
265 assert(!OutsideLoopPreds.empty() && "Loop is unreachable!");
266 } else if (!OutsideLoopPreds.empty()) {
267 // A non-header loop shouldn't be reachable from outside the loop,
268 // though it is permitted if the predecessor is not itself actually
270 BlockT *EntryBB = BB->getParent()->begin();
271 for (df_iterator<BlockT *> NI = df_begin(EntryBB),
272 NE = df_end(EntryBB); NI != NE; ++NI)
273 for (unsigned i = 0, e = OutsideLoopPreds.size(); i != e; ++i)
274 assert(*NI != OutsideLoopPreds[i] &&
275 "Loop has multiple entry points!");
277 assert(HasInsideLoopPreds && "Loop block has no in-loop predecessors!");
278 assert(HasInsideLoopSuccs && "Loop block has no in-loop successors!");
279 assert(BB != getHeader()->getParent()->begin() &&
280 "Loop contains function entry block!");
285 assert(NumVisited == getNumBlocks() && "Unreachable block in loop");
287 // Check the subloops.
288 for (iterator I = begin(), E = end(); I != E; ++I)
289 // Each block in each subloop should be contained within this loop.
290 for (block_iterator BI = (*I)->block_begin(), BE = (*I)->block_end();
292 assert(contains(*BI) &&
293 "Loop does not contain all the blocks of a subloop!");
296 // Check the parent loop pointer.
298 assert(std::find(ParentLoop->begin(), ParentLoop->end(), this) !=
300 "Loop is not a subloop of its parent!");
305 /// verifyLoop - Verify loop structure of this loop and all nested loops.
306 template<class BlockT, class LoopT>
307 void LoopBase<BlockT, LoopT>::verifyLoopNest(
308 DenseSet<const LoopT*> *Loops) const {
309 Loops->insert(static_cast<const LoopT *>(this));
312 // Verify the subloops.
313 for (iterator I = begin(), E = end(); I != E; ++I)
314 (*I)->verifyLoopNest(Loops);
317 template<class BlockT, class LoopT>
318 void LoopBase<BlockT, LoopT>::print(raw_ostream &OS, unsigned Depth) const {
319 OS.indent(Depth*2) << "Loop at depth " << getLoopDepth()
322 for (unsigned i = 0; i < getBlocks().size(); ++i) {
324 BlockT *BB = getBlocks()[i];
325 WriteAsOperand(OS, BB, false);
326 if (BB == getHeader()) OS << "<header>";
327 if (BB == getLoopLatch()) OS << "<latch>";
328 if (isLoopExiting(BB)) OS << "<exiting>";
332 for (iterator I = begin(), E = end(); I != E; ++I)
333 (*I)->print(OS, Depth+2);
336 //===----------------------------------------------------------------------===//
337 /// Stable LoopInfo Analysis - Build a loop tree using stable iterators so the
338 /// result does / not depend on use list (block predecessor) order.
341 /// Discover a subloop with the specified backedges such that: All blocks within
342 /// this loop are mapped to this loop or a subloop. And all subloops within this
343 /// loop have their parent loop set to this loop or a subloop.
344 template<class BlockT, class LoopT>
345 static void discoverAndMapSubloop(LoopT *L, ArrayRef<BlockT*> Backedges,
346 LoopInfoBase<BlockT, LoopT> *LI,
347 DominatorTreeBase<BlockT> &DomTree) {
348 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
350 unsigned NumBlocks = 0;
351 unsigned NumSubloops = 0;
353 // Perform a backward CFG traversal using a worklist.
354 std::vector<BlockT *> ReverseCFGWorklist(Backedges.begin(), Backedges.end());
355 while (!ReverseCFGWorklist.empty()) {
356 BlockT *PredBB = ReverseCFGWorklist.back();
357 ReverseCFGWorklist.pop_back();
359 LoopT *Subloop = LI->getLoopFor(PredBB);
361 if (!DomTree.isReachableFromEntry(PredBB))
364 // This is an undiscovered block. Map it to the current loop.
365 LI->changeLoopFor(PredBB, L);
367 if (PredBB == L->getHeader())
369 // Push all block predecessors on the worklist.
370 ReverseCFGWorklist.insert(ReverseCFGWorklist.end(),
371 InvBlockTraits::child_begin(PredBB),
372 InvBlockTraits::child_end(PredBB));
375 // This is a discovered block. Find its outermost discovered loop.
376 while (LoopT *Parent = Subloop->getParentLoop())
379 // If it is already discovered to be a subloop of this loop, continue.
383 // Discover a subloop of this loop.
384 Subloop->setParentLoop(L);
386 NumBlocks += Subloop->getBlocks().capacity();
387 PredBB = Subloop->getHeader();
388 // Continue traversal along predecessors that are not loop-back edges from
389 // within this subloop tree itself. Note that a predecessor may directly
390 // reach another subloop that is not yet discovered to be a subloop of
391 // this loop, which we must traverse.
392 for (typename InvBlockTraits::ChildIteratorType PI =
393 InvBlockTraits::child_begin(PredBB),
394 PE = InvBlockTraits::child_end(PredBB); PI != PE; ++PI) {
395 if (LI->getLoopFor(*PI) != Subloop)
396 ReverseCFGWorklist.push_back(*PI);
400 L->getSubLoopsVector().reserve(NumSubloops);
401 L->reserveBlocks(NumBlocks);
405 /// Populate all loop data in a stable order during a single forward DFS.
406 template<class BlockT, class LoopT>
407 class PopulateLoopsDFS {
408 typedef GraphTraits<BlockT*> BlockTraits;
409 typedef typename BlockTraits::ChildIteratorType SuccIterTy;
411 LoopInfoBase<BlockT, LoopT> *LI;
412 DenseSet<const BlockT *> VisitedBlocks;
413 std::vector<std::pair<BlockT*, SuccIterTy> > DFSStack;
416 PopulateLoopsDFS(LoopInfoBase<BlockT, LoopT> *li):
419 void traverse(BlockT *EntryBlock);
422 void insertIntoLoop(BlockT *Block);
424 BlockT *dfsSource() { return DFSStack.back().first; }
425 SuccIterTy &dfsSucc() { return DFSStack.back().second; }
426 SuccIterTy dfsSuccEnd() { return BlockTraits::child_end(dfsSource()); }
428 void pushBlock(BlockT *Block) {
429 DFSStack.push_back(std::make_pair(Block, BlockTraits::child_begin(Block)));
434 /// Top-level driver for the forward DFS within the loop.
435 template<class BlockT, class LoopT>
436 void PopulateLoopsDFS<BlockT, LoopT>::traverse(BlockT *EntryBlock) {
437 pushBlock(EntryBlock);
438 VisitedBlocks.insert(EntryBlock);
439 while (!DFSStack.empty()) {
440 // Traverse the leftmost path as far as possible.
441 while (dfsSucc() != dfsSuccEnd()) {
442 BlockT *BB = *dfsSucc();
444 if (!VisitedBlocks.insert(BB).second)
447 // Push the next DFS successor onto the stack.
450 // Visit the top of the stack in postorder and backtrack.
451 insertIntoLoop(dfsSource());
456 /// Add a single Block to its ancestor loops in PostOrder. If the block is a
457 /// subloop header, add the subloop to its parent in PostOrder, then reverse the
458 /// Block and Subloop vectors of the now complete subloop to achieve RPO.
459 template<class BlockT, class LoopT>
460 void PopulateLoopsDFS<BlockT, LoopT>::insertIntoLoop(BlockT *Block) {
461 LoopT *Subloop = LI->getLoopFor(Block);
462 if (Subloop && Block == Subloop->getHeader()) {
463 // We reach this point once per subloop after processing all the blocks in
465 if (Subloop->getParentLoop())
466 Subloop->getParentLoop()->getSubLoopsVector().push_back(Subloop);
468 LI->addTopLevelLoop(Subloop);
470 // For convenience, Blocks and Subloops are inserted in postorder. Reverse
471 // the lists, except for the loop header, which is always at the beginning.
472 Subloop->reverseBlock(1);
473 std::reverse(Subloop->getSubLoopsVector().begin(),
474 Subloop->getSubLoopsVector().end());
476 Subloop = Subloop->getParentLoop();
478 for (; Subloop; Subloop = Subloop->getParentLoop())
479 Subloop->addBlockEntry(Block);
482 /// Analyze LoopInfo discovers loops during a postorder DominatorTree traversal
483 /// interleaved with backward CFG traversals within each subloop
484 /// (discoverAndMapSubloop). The backward traversal skips inner subloops, so
485 /// this part of the algorithm is linear in the number of CFG edges. Subloop and
486 /// Block vectors are then populated during a single forward CFG traversal
487 /// (PopulateLoopDFS).
489 /// During the two CFG traversals each block is seen three times:
490 /// 1) Discovered and mapped by a reverse CFG traversal.
491 /// 2) Visited during a forward DFS CFG traversal.
492 /// 3) Reverse-inserted in the loop in postorder following forward DFS.
494 /// The Block vectors are inclusive, so step 3 requires loop-depth number of
495 /// insertions per block.
496 template<class BlockT, class LoopT>
497 void LoopInfoBase<BlockT, LoopT>::
498 Analyze(DominatorTreeBase<BlockT> &DomTree) {
500 // Postorder traversal of the dominator tree.
501 DomTreeNodeBase<BlockT>* DomRoot = DomTree.getRootNode();
502 for (po_iterator<DomTreeNodeBase<BlockT>*> DomIter = po_begin(DomRoot),
503 DomEnd = po_end(DomRoot); DomIter != DomEnd; ++DomIter) {
505 BlockT *Header = DomIter->getBlock();
506 SmallVector<BlockT *, 4> Backedges;
508 // Check each predecessor of the potential loop header.
509 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
510 for (typename InvBlockTraits::ChildIteratorType PI =
511 InvBlockTraits::child_begin(Header),
512 PE = InvBlockTraits::child_end(Header); PI != PE; ++PI) {
514 BlockT *Backedge = *PI;
516 // If Header dominates predBB, this is a new loop. Collect the backedges.
517 if (DomTree.dominates(Header, Backedge)
518 && DomTree.isReachableFromEntry(Backedge)) {
519 Backedges.push_back(Backedge);
522 // Perform a backward CFG traversal to discover and map blocks in this loop.
523 if (!Backedges.empty()) {
524 LoopT *L = new LoopT(Header);
525 discoverAndMapSubloop(L, ArrayRef<BlockT*>(Backedges), this, DomTree);
528 // Perform a single forward CFG traversal to populate block and subloop
529 // vectors for all loops.
530 PopulateLoopsDFS<BlockT, LoopT> DFS(this);
531 DFS.traverse(DomRoot->getBlock());
535 template<class BlockT, class LoopT>
536 void LoopInfoBase<BlockT, LoopT>::print(raw_ostream &OS) const {
537 for (unsigned i = 0; i < TopLevelLoops.size(); ++i)
538 TopLevelLoops[i]->print(OS);
540 for (DenseMap<BasicBlock*, LoopT*>::const_iterator I = BBMap.begin(),
541 E = BBMap.end(); I != E; ++I)
542 OS << "BB '" << I->first->getName() << "' level = "
543 << I->second->getLoopDepth() << "\n";
547 } // End llvm namespace