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_LOOP_INFO_IMPL_H
16 #define LLVM_ANALYSIS_LOOP_INFO_IMPL_H
18 #include "llvm/Analysis/LoopInfo.h"
19 #include "llvm/ADT/PostOrderIterator.h"
23 //===----------------------------------------------------------------------===//
24 // APIs for simple analysis of the loop. See header notes.
26 /// getExitingBlocks - Return all blocks inside the loop that have successors
27 /// outside of the loop. These are the blocks _inside of the current loop_
28 /// which branch out. The returned list is always unique.
30 template<class BlockT, class LoopT>
31 void LoopBase<BlockT, LoopT>::
32 getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const {
33 // Sort the blocks vector so that we can use binary search to do quick
35 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
36 std::sort(LoopBBs.begin(), LoopBBs.end());
38 typedef GraphTraits<BlockT*> BlockTraits;
39 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
40 for (typename BlockTraits::ChildIteratorType I =
41 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
43 if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) {
44 // Not in current loop? It must be an exit block.
45 ExitingBlocks.push_back(*BI);
50 /// getExitingBlock - If getExitingBlocks would return exactly one block,
51 /// return that block. Otherwise return null.
52 template<class BlockT, class LoopT>
53 BlockT *LoopBase<BlockT, LoopT>::getExitingBlock() const {
54 SmallVector<BlockT*, 8> ExitingBlocks;
55 getExitingBlocks(ExitingBlocks);
56 if (ExitingBlocks.size() == 1)
57 return ExitingBlocks[0];
61 /// getExitBlocks - Return all of the successor blocks of this loop. These
62 /// are the blocks _outside of the current loop_ which are branched to.
64 template<class BlockT, class LoopT>
65 void LoopBase<BlockT, LoopT>::
66 getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
67 // Sort the blocks vector so that we can use binary search to do quick
69 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
70 std::sort(LoopBBs.begin(), LoopBBs.end());
72 typedef GraphTraits<BlockT*> BlockTraits;
73 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
74 for (typename BlockTraits::ChildIteratorType I =
75 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
77 if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
78 // Not in current loop? It must be an exit block.
79 ExitBlocks.push_back(*I);
82 /// getExitBlock - If getExitBlocks would return exactly one block,
83 /// return that block. Otherwise return null.
84 template<class BlockT, class LoopT>
85 BlockT *LoopBase<BlockT, LoopT>::getExitBlock() const {
86 SmallVector<BlockT*, 8> ExitBlocks;
87 getExitBlocks(ExitBlocks);
88 if (ExitBlocks.size() == 1)
93 /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
94 template<class BlockT, class LoopT>
95 void LoopBase<BlockT, LoopT>::
96 getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const {
97 // Sort the blocks vector so that we can use binary search to do quick
99 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
100 array_pod_sort(LoopBBs.begin(), LoopBBs.end());
102 typedef GraphTraits<BlockT*> BlockTraits;
103 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
104 for (typename BlockTraits::ChildIteratorType I =
105 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
107 if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
108 // Not in current loop? It must be an exit block.
109 ExitEdges.push_back(Edge(*BI, *I));
112 /// getLoopPreheader - If there is a preheader for this loop, return it. A
113 /// loop has a preheader if there is only one edge to the header of the loop
114 /// from outside of the loop. If this is the case, the block branching to the
115 /// header of the loop is the preheader node.
117 /// This method returns null if there is no preheader for the loop.
119 template<class BlockT, class LoopT>
120 BlockT *LoopBase<BlockT, LoopT>::getLoopPreheader() const {
121 // Keep track of nodes outside the loop branching to the header...
122 BlockT *Out = getLoopPredecessor();
125 // Make sure there is only one exit out of the preheader.
126 typedef GraphTraits<BlockT*> BlockTraits;
127 typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
129 if (SI != BlockTraits::child_end(Out))
130 return 0; // Multiple exits from the block, must not be a preheader.
132 // The predecessor has exactly one successor, so it is a preheader.
136 /// getLoopPredecessor - If the given loop's header has exactly one unique
137 /// predecessor outside the loop, return it. Otherwise return null.
138 /// This is less strict that the loop "preheader" concept, which requires
139 /// the predecessor to have exactly one successor.
141 template<class BlockT, class LoopT>
142 BlockT *LoopBase<BlockT, LoopT>::getLoopPredecessor() const {
143 // Keep track of nodes outside the loop branching to the header...
146 // Loop over the predecessors of the header node...
147 BlockT *Header = getHeader();
148 typedef GraphTraits<BlockT*> BlockTraits;
149 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
150 for (typename InvBlockTraits::ChildIteratorType PI =
151 InvBlockTraits::child_begin(Header),
152 PE = InvBlockTraits::child_end(Header); PI != PE; ++PI) {
153 typename InvBlockTraits::NodeType *N = *PI;
154 if (!contains(N)) { // If the block is not in the loop...
156 return 0; // Multiple predecessors outside the loop
161 // Make sure there is only one exit out of the preheader.
162 assert(Out && "Header of loop has no predecessors from outside loop?");
166 /// getLoopLatch - If there is a single latch block for this loop, return it.
167 /// A latch block is a block that contains a branch back to the header.
168 template<class BlockT, class LoopT>
169 BlockT *LoopBase<BlockT, LoopT>::getLoopLatch() const {
170 BlockT *Header = getHeader();
171 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
172 typename InvBlockTraits::ChildIteratorType PI =
173 InvBlockTraits::child_begin(Header);
174 typename InvBlockTraits::ChildIteratorType PE =
175 InvBlockTraits::child_end(Header);
177 for (; PI != PE; ++PI) {
178 typename InvBlockTraits::NodeType *N = *PI;
188 //===----------------------------------------------------------------------===//
189 // APIs for updating loop information after changing the CFG
192 /// addBasicBlockToLoop - This method is used by other analyses to update loop
193 /// information. NewBB is set to be a new member of the current loop.
194 /// Because of this, it is added as a member of all parent loops, and is added
195 /// to the specified LoopInfo object as being in the current basic block. It
196 /// is not valid to replace the loop header with this method.
198 template<class BlockT, class LoopT>
199 void LoopBase<BlockT, LoopT>::
200 addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LIB) {
201 assert((Blocks.empty() || LIB[getHeader()] == this) &&
202 "Incorrect LI specified for this loop!");
203 assert(NewBB && "Cannot add a null basic block to the loop!");
204 assert(LIB[NewBB] == 0 && "BasicBlock already in the loop!");
206 LoopT *L = static_cast<LoopT *>(this);
208 // Add the loop mapping to the LoopInfo object...
209 LIB.BBMap[NewBB] = L;
211 // Add the basic block to this loop and all parent loops...
213 L->Blocks.push_back(NewBB);
214 L = L->getParentLoop();
218 /// replaceChildLoopWith - This is used when splitting loops up. It replaces
219 /// the OldChild entry in our children list with NewChild, and updates the
220 /// parent pointer of OldChild to be null and the NewChild to be this loop.
221 /// This updates the loop depth of the new child.
222 template<class BlockT, class LoopT>
223 void LoopBase<BlockT, LoopT>::
224 replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild) {
225 assert(OldChild->ParentLoop == this && "This loop is already broken!");
226 assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
227 typename std::vector<LoopT *>::iterator I =
228 std::find(SubLoops.begin(), SubLoops.end(), OldChild);
229 assert(I != SubLoops.end() && "OldChild not in loop!");
231 OldChild->ParentLoop = 0;
232 NewChild->ParentLoop = static_cast<LoopT *>(this);
235 /// verifyLoop - Verify loop structure
236 template<class BlockT, class LoopT>
237 void LoopBase<BlockT, LoopT>::verifyLoop() const {
239 assert(!Blocks.empty() && "Loop header is missing");
241 // Setup for using a depth-first iterator to visit every block in the loop.
242 SmallVector<BlockT*, 8> ExitBBs;
243 getExitBlocks(ExitBBs);
244 llvm::SmallPtrSet<BlockT*, 8> VisitSet;
245 VisitSet.insert(ExitBBs.begin(), ExitBBs.end());
246 df_ext_iterator<BlockT*, llvm::SmallPtrSet<BlockT*, 8> >
247 BI = df_ext_begin(getHeader(), VisitSet),
248 BE = df_ext_end(getHeader(), VisitSet);
250 // Keep track of the number of BBs visited.
251 unsigned NumVisited = 0;
253 // Sort the blocks vector so that we can use binary search to do quick
255 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
256 std::sort(LoopBBs.begin(), LoopBBs.end());
258 // Check the individual blocks.
259 for ( ; BI != BE; ++BI) {
261 bool HasInsideLoopSuccs = false;
262 bool HasInsideLoopPreds = false;
263 SmallVector<BlockT *, 2> OutsideLoopPreds;
265 typedef GraphTraits<BlockT*> BlockTraits;
266 for (typename BlockTraits::ChildIteratorType SI =
267 BlockTraits::child_begin(BB), SE = BlockTraits::child_end(BB);
269 if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *SI)) {
270 HasInsideLoopSuccs = true;
273 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
274 for (typename InvBlockTraits::ChildIteratorType PI =
275 InvBlockTraits::child_begin(BB), PE = InvBlockTraits::child_end(BB);
278 if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), N))
279 HasInsideLoopPreds = true;
281 OutsideLoopPreds.push_back(N);
284 if (BB == getHeader()) {
285 assert(!OutsideLoopPreds.empty() && "Loop is unreachable!");
286 } else if (!OutsideLoopPreds.empty()) {
287 // A non-header loop shouldn't be reachable from outside the loop,
288 // though it is permitted if the predecessor is not itself actually
290 BlockT *EntryBB = BB->getParent()->begin();
291 for (df_iterator<BlockT *> NI = df_begin(EntryBB),
292 NE = df_end(EntryBB); NI != NE; ++NI)
293 for (unsigned i = 0, e = OutsideLoopPreds.size(); i != e; ++i)
294 assert(*NI != OutsideLoopPreds[i] &&
295 "Loop has multiple entry points!");
297 assert(HasInsideLoopPreds && "Loop block has no in-loop predecessors!");
298 assert(HasInsideLoopSuccs && "Loop block has no in-loop successors!");
299 assert(BB != getHeader()->getParent()->begin() &&
300 "Loop contains function entry block!");
305 assert(NumVisited == getNumBlocks() && "Unreachable block in loop");
307 // Check the subloops.
308 for (iterator I = begin(), E = end(); I != E; ++I)
309 // Each block in each subloop should be contained within this loop.
310 for (block_iterator BI = (*I)->block_begin(), BE = (*I)->block_end();
312 assert(std::binary_search(LoopBBs.begin(), LoopBBs.end(), *BI) &&
313 "Loop does not contain all the blocks of a subloop!");
316 // Check the parent loop pointer.
318 assert(std::find(ParentLoop->begin(), ParentLoop->end(), this) !=
320 "Loop is not a subloop of its parent!");
325 /// verifyLoop - Verify loop structure of this loop and all nested loops.
326 template<class BlockT, class LoopT>
327 void LoopBase<BlockT, LoopT>::verifyLoopNest(
328 DenseSet<const LoopT*> *Loops) const {
329 Loops->insert(static_cast<const LoopT *>(this));
332 // Verify the subloops.
333 for (iterator I = begin(), E = end(); I != E; ++I)
334 (*I)->verifyLoopNest(Loops);
337 template<class BlockT, class LoopT>
338 void LoopBase<BlockT, LoopT>::print(raw_ostream &OS, unsigned Depth) const {
339 OS.indent(Depth*2) << "Loop at depth " << getLoopDepth()
342 for (unsigned i = 0; i < getBlocks().size(); ++i) {
344 BlockT *BB = getBlocks()[i];
345 WriteAsOperand(OS, BB, false);
346 if (BB == getHeader()) OS << "<header>";
347 if (BB == getLoopLatch()) OS << "<latch>";
348 if (isLoopExiting(BB)) OS << "<exiting>";
352 for (iterator I = begin(), E = end(); I != E; ++I)
353 (*I)->print(OS, Depth+2);
356 //===----------------------------------------------------------------------===//
357 /// Stable LoopInfo Analysis - Build a loop tree using stable iterators so the
358 /// result does / not depend on use list (block predecessor) order.
361 /// Discover a subloop with the specified backedges such that: All blocks within
362 /// this loop are mapped to this loop or a subloop. And all subloops within this
363 /// loop have their parent loop set to this loop or a subloop.
364 template<class BlockT, class LoopT>
365 static void discoverAndMapSubloop(LoopT *L, ArrayRef<BlockT*> Backedges,
366 LoopInfoBase<BlockT, LoopT> *LI,
367 DominatorTreeBase<BlockT> &DomTree) {
368 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
370 unsigned NumBlocks = 0;
371 unsigned NumSubloops = 0;
373 // Perform a backward CFG traversal using a worklist.
374 std::vector<BlockT *> ReverseCFGWorklist(Backedges.begin(), Backedges.end());
375 while (!ReverseCFGWorklist.empty()) {
376 BlockT *PredBB = ReverseCFGWorklist.back();
377 ReverseCFGWorklist.pop_back();
379 LoopT *Subloop = LI->getLoopFor(PredBB);
381 if (!DomTree.isReachableFromEntry(PredBB))
384 // This is an undiscovered block. Map it to the current loop.
385 LI->changeLoopFor(PredBB, L);
387 if (PredBB == L->getHeader())
389 // Push all block predecessors on the worklist.
390 ReverseCFGWorklist.insert(ReverseCFGWorklist.end(),
391 InvBlockTraits::child_begin(PredBB),
392 InvBlockTraits::child_end(PredBB));
395 // This is a discovered block. Find its outermost discovered loop.
396 while (LoopT *Parent = Subloop->getParentLoop())
399 // If it is already discovered to be a subloop of this loop, continue.
403 // Discover a subloop of this loop.
404 Subloop->setParentLoop(L);
406 NumBlocks += Subloop->getBlocks().capacity();
407 PredBB = Subloop->getHeader();
408 // Continue traversal along predecessors that are not loop-back edges from
409 // within this subloop tree itself. Note that a predecessor may directly
410 // reach another subloop that is not yet discovered to be a subloop of
411 // this loop, which we must traverse.
412 for (typename InvBlockTraits::ChildIteratorType PI =
413 InvBlockTraits::child_begin(PredBB),
414 PE = InvBlockTraits::child_end(PredBB); PI != PE; ++PI) {
415 if (LI->getLoopFor(*PI) != Subloop)
416 ReverseCFGWorklist.push_back(*PI);
420 L->getSubLoopsVector().reserve(NumSubloops);
421 L->getBlocksVector().reserve(NumBlocks);
425 /// Populate all loop data in a stable order during a single forward DFS.
426 template<class BlockT, class LoopT>
427 class PopulateLoopsDFS {
428 typedef GraphTraits<BlockT*> BlockTraits;
429 typedef typename BlockTraits::ChildIteratorType SuccIterTy;
431 LoopInfoBase<BlockT, LoopT> *LI;
432 DenseSet<const BlockT *> VisitedBlocks;
433 std::vector<std::pair<BlockT*, SuccIterTy> > DFSStack;
436 PopulateLoopsDFS(LoopInfoBase<BlockT, LoopT> *li):
439 void traverse(BlockT *EntryBlock);
442 void insertIntoLoop(BlockT *Block);
444 BlockT *dfsSource() { return DFSStack.back().first; }
445 SuccIterTy &dfsSucc() { return DFSStack.back().second; }
446 SuccIterTy dfsSuccEnd() { return BlockTraits::child_end(dfsSource()); }
448 void pushBlock(BlockT *Block) {
449 DFSStack.push_back(std::make_pair(Block, BlockTraits::child_begin(Block)));
454 /// Top-level driver for the forward DFS within the loop.
455 template<class BlockT, class LoopT>
456 void PopulateLoopsDFS<BlockT, LoopT>::traverse(BlockT *EntryBlock) {
457 pushBlock(EntryBlock);
458 VisitedBlocks.insert(EntryBlock);
459 while (!DFSStack.empty()) {
460 // Traverse the leftmost path as far as possible.
461 while (dfsSucc() != dfsSuccEnd()) {
462 BlockT *BB = *dfsSucc();
464 if (!VisitedBlocks.insert(BB).second)
467 // Push the next DFS successor onto the stack.
470 // Visit the top of the stack in postorder and backtrack.
471 insertIntoLoop(dfsSource());
476 /// Add a single Block to its ancestor loops in PostOrder. If the block is a
477 /// subloop header, add the subloop to its parent in PostOrder, then reverse the
478 /// Block and Subloop vectors of the now complete subloop to achieve RPO.
479 template<class BlockT, class LoopT>
480 void PopulateLoopsDFS<BlockT, LoopT>::insertIntoLoop(BlockT *Block) {
481 LoopT *Subloop = LI->getLoopFor(Block);
482 if (Subloop && Block == Subloop->getHeader()) {
483 // We reach this point once per subloop after processing all the blocks in
485 if (Subloop->getParentLoop())
486 Subloop->getParentLoop()->getSubLoopsVector().push_back(Subloop);
488 LI->addTopLevelLoop(Subloop);
490 // For convenience, Blocks and Subloops are inserted in postorder. Reverse
491 // the lists, except for the loop header, which is always at the beginning.
492 std::reverse(Subloop->getBlocksVector().begin()+1,
493 Subloop->getBlocksVector().end());
494 std::reverse(Subloop->getSubLoopsVector().begin(),
495 Subloop->getSubLoopsVector().end());
497 Subloop = Subloop->getParentLoop();
499 for (; Subloop; Subloop = Subloop->getParentLoop())
500 Subloop->getBlocksVector().push_back(Block);
503 /// Analyze LoopInfo discovers loops during a postorder DominatorTree traversal
504 /// interleaved with backward CFG traversals within each subloop
505 /// (discoverAndMapSubloop). The backward traversal skips inner subloops, so
506 /// this part of the algorithm is linear in the number of CFG edges. Subloop and
507 /// Block vectors are then populated during a single forward CFG traversal
508 /// (PopulateLoopDFS).
510 /// During the two CFG traversals each block is seen three times:
511 /// 1) Discovered and mapped by a reverse CFG traversal.
512 /// 2) Visited during a forward DFS CFG traversal.
513 /// 3) Reverse-inserted in the loop in postorder following forward DFS.
515 /// The Block vectors are inclusive, so step 3 requires loop-depth number of
516 /// insertions per block.
517 template<class BlockT, class LoopT>
518 void LoopInfoBase<BlockT, LoopT>::
519 Analyze(DominatorTreeBase<BlockT> &DomTree) {
521 // Postorder traversal of the dominator tree.
522 DomTreeNodeBase<BlockT>* DomRoot = DomTree.getRootNode();
523 for (po_iterator<DomTreeNodeBase<BlockT>*> DomIter = po_begin(DomRoot),
524 DomEnd = po_end(DomRoot); DomIter != DomEnd; ++DomIter) {
526 BlockT *Header = DomIter->getBlock();
527 SmallVector<BlockT *, 4> Backedges;
529 // Check each predecessor of the potential loop header.
530 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
531 for (typename InvBlockTraits::ChildIteratorType PI =
532 InvBlockTraits::child_begin(Header),
533 PE = InvBlockTraits::child_end(Header); PI != PE; ++PI) {
535 BlockT *Backedge = *PI;
537 // If Header dominates predBB, this is a new loop. Collect the backedges.
538 if (DomTree.dominates(Header, Backedge)
539 && DomTree.isReachableFromEntry(Backedge)) {
540 Backedges.push_back(Backedge);
543 // Perform a backward CFG traversal to discover and map blocks in this loop.
544 if (!Backedges.empty()) {
545 LoopT *L = new LoopT(Header);
546 discoverAndMapSubloop(L, ArrayRef<BlockT*>(Backedges), this, DomTree);
549 // Perform a single forward CFG traversal to populate block and subloop
550 // vectors for all loops.
551 PopulateLoopsDFS<BlockT, LoopT> DFS(this);
552 DFS.traverse(DomRoot->getBlock());
556 template<class BlockT, class LoopT>
557 void LoopInfoBase<BlockT, LoopT>::print(raw_ostream &OS) const {
558 for (unsigned i = 0; i < TopLevelLoops.size(); ++i)
559 TopLevelLoops[i]->print(OS);
561 for (DenseMap<BasicBlock*, LoopT*>::const_iterator I = BBMap.begin(),
562 E = BBMap.end(); I != E; ++I)
563 OS << "BB '" << I->first->getName() << "' level = "
564 << I->second->getLoopDepth() << "\n";
568 } // End llvm namespace