1 //===- llvm/Analysis/LoopInfoImpl.h - Natural Loop Calculator ---*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This is the generic implementation of LoopInfo used for both Loops and
11 // MachineLoops.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ANALYSIS_LOOPINFOIMPL_H
16 #define LLVM_ANALYSIS_LOOPINFOIMPL_H
17
18 #include "llvm/ADT/PostOrderIterator.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Analysis/LoopInfo.h"
21
22 namespace llvm {
23
24 //===----------------------------------------------------------------------===//
25 // APIs for simple analysis of the loop. See header notes.
26
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.
30 ///
31 template<class BlockT, class LoopT>
32 void LoopBase<BlockT, LoopT>::
getExitingBlocks(SmallVectorImpl<BlockT * > & ExitingBlocks)33 getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const {
34 // Sort the blocks vector so that we can use binary search to do quick
35 // lookups.
36 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
37 std::sort(LoopBBs.begin(), LoopBBs.end());
38
39 typedef GraphTraits<BlockT*> BlockTraits;
40 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
41 for (typename BlockTraits::ChildIteratorType I =
42 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
43 I != E; ++I)
44 if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) {
45 // Not in current loop? It must be an exit block.
46 ExitingBlocks.push_back(*BI);
47 break;
48 }
49 }
50
51 /// getExitingBlock - If getExitingBlocks would return exactly one block,
52 /// return that block. Otherwise return null.
53 template<class BlockT, class LoopT>
getExitingBlock()54 BlockT *LoopBase<BlockT, LoopT>::getExitingBlock() const {
55 SmallVector<BlockT*, 8> ExitingBlocks;
56 getExitingBlocks(ExitingBlocks);
57 if (ExitingBlocks.size() == 1)
58 return ExitingBlocks[0];
59 return 0;
60 }
61
62 /// getExitBlocks - Return all of the successor blocks of this loop. These
63 /// are the blocks _outside of the current loop_ which are branched to.
64 ///
65 template<class BlockT, class LoopT>
66 void LoopBase<BlockT, LoopT>::
getExitBlocks(SmallVectorImpl<BlockT * > & ExitBlocks)67 getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
68 // Sort the blocks vector so that we can use binary search to do quick
69 // lookups.
70 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
71 std::sort(LoopBBs.begin(), LoopBBs.end());
72
73 typedef GraphTraits<BlockT*> BlockTraits;
74 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
75 for (typename BlockTraits::ChildIteratorType I =
76 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
77 I != E; ++I)
78 if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
79 // Not in current loop? It must be an exit block.
80 ExitBlocks.push_back(*I);
81 }
82
83 /// getExitBlock - If getExitBlocks would return exactly one block,
84 /// return that block. Otherwise return null.
85 template<class BlockT, class LoopT>
getExitBlock()86 BlockT *LoopBase<BlockT, LoopT>::getExitBlock() const {
87 SmallVector<BlockT*, 8> ExitBlocks;
88 getExitBlocks(ExitBlocks);
89 if (ExitBlocks.size() == 1)
90 return ExitBlocks[0];
91 return 0;
92 }
93
94 /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
95 template<class BlockT, class LoopT>
96 void LoopBase<BlockT, LoopT>::
getExitEdges(SmallVectorImpl<Edge> & ExitEdges)97 getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const {
98 // Sort the blocks vector so that we can use binary search to do quick
99 // lookups.
100 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
101 array_pod_sort(LoopBBs.begin(), LoopBBs.end());
102
103 typedef GraphTraits<BlockT*> BlockTraits;
104 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
105 for (typename BlockTraits::ChildIteratorType I =
106 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
107 I != E; ++I)
108 if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
109 // Not in current loop? It must be an exit block.
110 ExitEdges.push_back(Edge(*BI, *I));
111 }
112
113 /// getLoopPreheader - If there is a preheader for this loop, return it. A
114 /// loop has a preheader if there is only one edge to the header of the loop
115 /// from outside of the loop. If this is the case, the block branching to the
116 /// header of the loop is the preheader node.
117 ///
118 /// This method returns null if there is no preheader for the loop.
119 ///
120 template<class BlockT, class LoopT>
getLoopPreheader()121 BlockT *LoopBase<BlockT, LoopT>::getLoopPreheader() const {
122 // Keep track of nodes outside the loop branching to the header...
123 BlockT *Out = getLoopPredecessor();
124 if (!Out) return 0;
125
126 // Make sure there is only one exit out of the preheader.
127 typedef GraphTraits<BlockT*> BlockTraits;
128 typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
129 ++SI;
130 if (SI != BlockTraits::child_end(Out))
131 return 0; // Multiple exits from the block, must not be a preheader.
132
133 // The predecessor has exactly one successor, so it is a preheader.
134 return Out;
135 }
136
137 /// getLoopPredecessor - If the given loop's header has exactly one unique
138 /// predecessor outside the loop, return it. Otherwise return null.
139 /// This is less strict that the loop "preheader" concept, which requires
140 /// the predecessor to have exactly one successor.
141 ///
142 template<class BlockT, class LoopT>
getLoopPredecessor()143 BlockT *LoopBase<BlockT, LoopT>::getLoopPredecessor() const {
144 // Keep track of nodes outside the loop branching to the header...
145 BlockT *Out = 0;
146
147 // Loop over the predecessors of the header node...
148 BlockT *Header = getHeader();
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...
155 if (Out && Out != N)
156 return 0; // Multiple predecessors outside the loop
157 Out = N;
158 }
159 }
160
161 // Make sure there is only one exit out of the preheader.
162 assert(Out && "Header of loop has no predecessors from outside loop?");
163 return Out;
164 }
165
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>
getLoopLatch()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);
176 BlockT *Latch = 0;
177 for (; PI != PE; ++PI) {
178 typename InvBlockTraits::NodeType *N = *PI;
179 if (contains(N)) {
180 if (Latch) return 0;
181 Latch = N;
182 }
183 }
184
185 return Latch;
186 }
187
188 //===----------------------------------------------------------------------===//
189 // APIs for updating loop information after changing the CFG
190 //
191
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.
197 ///
198 template<class BlockT, class LoopT>
199 void LoopBase<BlockT, LoopT>::
addBasicBlockToLoop(BlockT * NewBB,LoopInfoBase<BlockT,LoopT> & LIB)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!");
205
206 LoopT *L = static_cast<LoopT *>(this);
207
208 // Add the loop mapping to the LoopInfo object...
209 LIB.BBMap[NewBB] = L;
210
211 // Add the basic block to this loop and all parent loops...
212 while (L) {
213 L->Blocks.push_back(NewBB);
214 L = L->getParentLoop();
215 }
216 }
217
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>::
replaceChildLoopWith(LoopT * OldChild,LoopT * NewChild)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!");
230 *I = NewChild;
231 OldChild->ParentLoop = 0;
232 NewChild->ParentLoop = static_cast<LoopT *>(this);
233 }
234
235 /// verifyLoop - Verify loop structure
236 template<class BlockT, class LoopT>
verifyLoop()237 void LoopBase<BlockT, LoopT>::verifyLoop() const {
238 #ifndef NDEBUG
239 assert(!Blocks.empty() && "Loop header is missing");
240
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);
249
250 // Keep track of the number of BBs visited.
251 unsigned NumVisited = 0;
252
253 // Sort the blocks vector so that we can use binary search to do quick
254 // lookups.
255 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
256 std::sort(LoopBBs.begin(), LoopBBs.end());
257
258 // Check the individual blocks.
259 for ( ; BI != BE; ++BI) {
260 BlockT *BB = *BI;
261 bool HasInsideLoopSuccs = false;
262 bool HasInsideLoopPreds = false;
263 SmallVector<BlockT *, 2> OutsideLoopPreds;
264
265 typedef GraphTraits<BlockT*> BlockTraits;
266 for (typename BlockTraits::ChildIteratorType SI =
267 BlockTraits::child_begin(BB), SE = BlockTraits::child_end(BB);
268 SI != SE; ++SI)
269 if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *SI)) {
270 HasInsideLoopSuccs = true;
271 break;
272 }
273 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
274 for (typename InvBlockTraits::ChildIteratorType PI =
275 InvBlockTraits::child_begin(BB), PE = InvBlockTraits::child_end(BB);
276 PI != PE; ++PI) {
277 BlockT *N = *PI;
278 if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), N))
279 HasInsideLoopPreds = true;
280 else
281 OutsideLoopPreds.push_back(N);
282 }
283
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
289 // reachable.
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!");
296 }
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!");
301
302 NumVisited++;
303 }
304
305 assert(NumVisited == getNumBlocks() && "Unreachable block in loop");
306
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();
311 BI != BE; ++BI) {
312 assert(std::binary_search(LoopBBs.begin(), LoopBBs.end(), *BI) &&
313 "Loop does not contain all the blocks of a subloop!");
314 }
315
316 // Check the parent loop pointer.
317 if (ParentLoop) {
318 assert(std::find(ParentLoop->begin(), ParentLoop->end(), this) !=
319 ParentLoop->end() &&
320 "Loop is not a subloop of its parent!");
321 }
322 #endif
323 }
324
325 /// verifyLoop - Verify loop structure of this loop and all nested loops.
326 template<class BlockT, class LoopT>
verifyLoopNest(DenseSet<const LoopT * > * Loops)327 void LoopBase<BlockT, LoopT>::verifyLoopNest(
328 DenseSet<const LoopT*> *Loops) const {
329 Loops->insert(static_cast<const LoopT *>(this));
330 // Verify this loop.
331 verifyLoop();
332 // Verify the subloops.
333 for (iterator I = begin(), E = end(); I != E; ++I)
334 (*I)->verifyLoopNest(Loops);
335 }
336
337 template<class BlockT, class LoopT>
print(raw_ostream & OS,unsigned Depth)338 void LoopBase<BlockT, LoopT>::print(raw_ostream &OS, unsigned Depth) const {
339 OS.indent(Depth*2) << "Loop at depth " << getLoopDepth()
340 << " containing: ";
341
342 for (unsigned i = 0; i < getBlocks().size(); ++i) {
343 if (i) OS << ",";
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>";
349 }
350 OS << "\n";
351
352 for (iterator I = begin(), E = end(); I != E; ++I)
353 (*I)->print(OS, Depth+2);
354 }
355
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.
359 ///
360
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>
discoverAndMapSubloop(LoopT * L,ArrayRef<BlockT * > Backedges,LoopInfoBase<BlockT,LoopT> * LI,DominatorTreeBase<BlockT> & DomTree)365 static void discoverAndMapSubloop(LoopT *L, ArrayRef<BlockT*> Backedges,
366 LoopInfoBase<BlockT, LoopT> *LI,
367 DominatorTreeBase<BlockT> &DomTree) {
368 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
369
370 unsigned NumBlocks = 0;
371 unsigned NumSubloops = 0;
372
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();
378
379 LoopT *Subloop = LI->getLoopFor(PredBB);
380 if (!Subloop) {
381 if (!DomTree.isReachableFromEntry(PredBB))
382 continue;
383
384 // This is an undiscovered block. Map it to the current loop.
385 LI->changeLoopFor(PredBB, L);
386 ++NumBlocks;
387 if (PredBB == L->getHeader())
388 continue;
389 // Push all block predecessors on the worklist.
390 ReverseCFGWorklist.insert(ReverseCFGWorklist.end(),
391 InvBlockTraits::child_begin(PredBB),
392 InvBlockTraits::child_end(PredBB));
393 }
394 else {
395 // This is a discovered block. Find its outermost discovered loop.
396 while (LoopT *Parent = Subloop->getParentLoop())
397 Subloop = Parent;
398
399 // If it is already discovered to be a subloop of this loop, continue.
400 if (Subloop == L)
401 continue;
402
403 // Discover a subloop of this loop.
404 Subloop->setParentLoop(L);
405 ++NumSubloops;
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);
417 }
418 }
419 }
420 L->getSubLoopsVector().reserve(NumSubloops);
421 L->getBlocksVector().reserve(NumBlocks);
422 }
423
424 namespace {
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;
430
431 LoopInfoBase<BlockT, LoopT> *LI;
432 DenseSet<const BlockT *> VisitedBlocks;
433 std::vector<std::pair<BlockT*, SuccIterTy> > DFSStack;
434
435 public:
PopulateLoopsDFS(LoopInfoBase<BlockT,LoopT> * li)436 PopulateLoopsDFS(LoopInfoBase<BlockT, LoopT> *li):
437 LI(li) {}
438
439 void traverse(BlockT *EntryBlock);
440
441 protected:
442 void insertIntoLoop(BlockT *Block);
443
dfsSource()444 BlockT *dfsSource() { return DFSStack.back().first; }
dfsSucc()445 SuccIterTy &dfsSucc() { return DFSStack.back().second; }
dfsSuccEnd()446 SuccIterTy dfsSuccEnd() { return BlockTraits::child_end(dfsSource()); }
447
pushBlock(BlockT * Block)448 void pushBlock(BlockT *Block) {
449 DFSStack.push_back(std::make_pair(Block, BlockTraits::child_begin(Block)));
450 }
451 };
452 } // anonymous
453
454 /// Top-level driver for the forward DFS within the loop.
455 template<class BlockT, class LoopT>
traverse(BlockT * EntryBlock)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();
463 ++dfsSucc();
464 if (!VisitedBlocks.insert(BB).second)
465 continue;
466
467 // Push the next DFS successor onto the stack.
468 pushBlock(BB);
469 }
470 // Visit the top of the stack in postorder and backtrack.
471 insertIntoLoop(dfsSource());
472 DFSStack.pop_back();
473 }
474 }
475
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>
insertIntoLoop(BlockT * Block)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
484 // the subloop.
485 if (Subloop->getParentLoop())
486 Subloop->getParentLoop()->getSubLoopsVector().push_back(Subloop);
487 else
488 LI->addTopLevelLoop(Subloop);
489
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());
496
497 Subloop = Subloop->getParentLoop();
498 }
499 for (; Subloop; Subloop = Subloop->getParentLoop())
500 Subloop->getBlocksVector().push_back(Block);
501 }
502
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).
509 ///
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.
514 ///
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>::
Analyze(DominatorTreeBase<BlockT> & DomTree)519 Analyze(DominatorTreeBase<BlockT> &DomTree) {
520
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) {
525
526 BlockT *Header = DomIter->getBlock();
527 SmallVector<BlockT *, 4> Backedges;
528
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) {
534
535 BlockT *Backedge = *PI;
536
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);
541 }
542 }
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);
547 }
548 }
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());
553 }
554
555 // Debugging
556 template<class BlockT, class LoopT>
print(raw_ostream & OS)557 void LoopInfoBase<BlockT, LoopT>::print(raw_ostream &OS) const {
558 for (unsigned i = 0; i < TopLevelLoops.size(); ++i)
559 TopLevelLoops[i]->print(OS);
560 #if 0
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";
565 #endif
566 }
567
568 } // End llvm namespace
569
570 #endif
571