Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

570 lines
21 KiB

  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. #ifndef LLVM_ANALYSIS_LOOPINFOIMPL_H
  15. #define LLVM_ANALYSIS_LOOPINFOIMPL_H
  16. #include "llvm/ADT/PostOrderIterator.h"
  17. #include "llvm/ADT/STLExtras.h"
  18. #include "llvm/Analysis/LoopInfo.h"
  19. namespace llvm {
  20. //===----------------------------------------------------------------------===//
  21. // APIs for simple analysis of the loop. See header notes.
  22. /// getExitingBlocks - Return all blocks inside the loop that have successors
  23. /// outside of the loop. These are the blocks _inside of the current loop_
  24. /// which branch out. The returned list is always unique.
  25. ///
  26. template<class BlockT, class LoopT>
  27. void LoopBase<BlockT, LoopT>::
  28. getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const {
  29. // Sort the blocks vector so that we can use binary search to do quick
  30. // lookups.
  31. SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
  32. std::sort(LoopBBs.begin(), LoopBBs.end());
  33. typedef GraphTraits<BlockT*> BlockTraits;
  34. for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
  35. for (typename BlockTraits::ChildIteratorType I =
  36. BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
  37. I != E; ++I)
  38. if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) {
  39. // Not in current loop? It must be an exit block.
  40. ExitingBlocks.push_back(*BI);
  41. break;
  42. }
  43. }
  44. /// getExitingBlock - If getExitingBlocks would return exactly one block,
  45. /// return that block. Otherwise return null.
  46. template<class BlockT, class LoopT>
  47. BlockT *LoopBase<BlockT, LoopT>::getExitingBlock() const {
  48. SmallVector<BlockT*, 8> ExitingBlocks;
  49. getExitingBlocks(ExitingBlocks);
  50. if (ExitingBlocks.size() == 1)
  51. return ExitingBlocks[0];
  52. return 0;
  53. }
  54. /// getExitBlocks - Return all of the successor blocks of this loop. These
  55. /// are the blocks _outside of the current loop_ which are branched to.
  56. ///
  57. template<class BlockT, class LoopT>
  58. void LoopBase<BlockT, LoopT>::
  59. getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
  60. // Sort the blocks vector so that we can use binary search to do quick
  61. // lookups.
  62. SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
  63. std::sort(LoopBBs.begin(), LoopBBs.end());
  64. typedef GraphTraits<BlockT*> BlockTraits;
  65. for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
  66. for (typename BlockTraits::ChildIteratorType I =
  67. BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
  68. I != E; ++I)
  69. if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
  70. // Not in current loop? It must be an exit block.
  71. ExitBlocks.push_back(*I);
  72. }
  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)
  80. return ExitBlocks[0];
  81. return 0;
  82. }
  83. /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
  84. template<class BlockT, class LoopT>
  85. void LoopBase<BlockT, LoopT>::
  86. getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const {
  87. // Sort the blocks vector so that we can use binary search to do quick
  88. // lookups.
  89. SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
  90. array_pod_sort(LoopBBs.begin(), LoopBBs.end());
  91. typedef GraphTraits<BlockT*> BlockTraits;
  92. for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
  93. for (typename BlockTraits::ChildIteratorType I =
  94. BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
  95. I != E; ++I)
  96. if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
  97. // Not in current loop? It must be an exit block.
  98. ExitEdges.push_back(Edge(*BI, *I));
  99. }
  100. /// getLoopPreheader - If there is a preheader for this loop, return it. A
  101. /// loop has a preheader if there is only one edge to the header of the loop
  102. /// from outside of the loop. If this is the case, the block branching to the
  103. /// header of the loop is the preheader node.
  104. ///
  105. /// This method returns null if there is no preheader for the loop.
  106. ///
  107. template<class BlockT, class LoopT>
  108. BlockT *LoopBase<BlockT, LoopT>::getLoopPreheader() const {
  109. // Keep track of nodes outside the loop branching to the header...
  110. BlockT *Out = getLoopPredecessor();
  111. if (!Out) return 0;
  112. // Make sure there is only one exit out of the preheader.
  113. typedef GraphTraits<BlockT*> BlockTraits;
  114. typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
  115. ++SI;
  116. if (SI != BlockTraits::child_end(Out))
  117. return 0; // Multiple exits from the block, must not be a preheader.
  118. // The predecessor has exactly one successor, so it is a preheader.
  119. return Out;
  120. }
  121. /// getLoopPredecessor - If the given loop's header has exactly one unique
  122. /// predecessor outside the loop, return it. Otherwise return null.
  123. /// This is less strict that the loop "preheader" concept, which requires
  124. /// the predecessor to have exactly one successor.
  125. ///
  126. template<class BlockT, class LoopT>
  127. BlockT *LoopBase<BlockT, LoopT>::getLoopPredecessor() const {
  128. // Keep track of nodes outside the loop branching to the header...
  129. BlockT *Out = 0;
  130. // Loop over the predecessors of the header node...
  131. BlockT *Header = getHeader();
  132. typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
  133. for (typename InvBlockTraits::ChildIteratorType PI =
  134. InvBlockTraits::child_begin(Header),
  135. PE = InvBlockTraits::child_end(Header); PI != PE; ++PI) {
  136. typename InvBlockTraits::NodeType *N = *PI;
  137. if (!contains(N)) { // If the block is not in the loop...
  138. if (Out && Out != N)
  139. return 0; // Multiple predecessors outside the loop
  140. Out = N;
  141. }
  142. }
  143. // Make sure there is only one exit out of the preheader.
  144. assert(Out && "Header of loop has no predecessors from outside loop?");
  145. return Out;
  146. }
  147. /// getLoopLatch - If there is a single latch block for this loop, return it.
  148. /// A latch block is a block that contains a branch back to the header.
  149. template<class BlockT, class LoopT>
  150. BlockT *LoopBase<BlockT, LoopT>::getLoopLatch() const {
  151. BlockT *Header = getHeader();
  152. typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
  153. typename InvBlockTraits::ChildIteratorType PI =
  154. InvBlockTraits::child_begin(Header);
  155. typename InvBlockTraits::ChildIteratorType PE =
  156. InvBlockTraits::child_end(Header);
  157. BlockT *Latch = 0;
  158. for (; PI != PE; ++PI) {
  159. typename InvBlockTraits::NodeType *N = *PI;
  160. if (contains(N)) {
  161. if (Latch) return 0;
  162. Latch = N;
  163. }
  164. }
  165. return Latch;
  166. }
  167. //===----------------------------------------------------------------------===//
  168. // APIs for updating loop information after changing the CFG
  169. //
  170. /// addBasicBlockToLoop - This method is used by other analyses to update loop
  171. /// information. NewBB is set to be a new member of the current loop.
  172. /// Because of this, it is added as a member of all parent loops, and is added
  173. /// to the specified LoopInfo object as being in the current basic block. It
  174. /// is not valid to replace the loop header with this method.
  175. ///
  176. template<class BlockT, class LoopT>
  177. void LoopBase<BlockT, LoopT>::
  178. addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LIB) {
  179. assert((Blocks.empty() || LIB[getHeader()] == this) &&
  180. "Incorrect LI specified for this loop!");
  181. assert(NewBB && "Cannot add a null basic block to the loop!");
  182. assert(LIB[NewBB] == 0 && "BasicBlock already in the loop!");
  183. LoopT *L = static_cast<LoopT *>(this);
  184. // Add the loop mapping to the LoopInfo object...
  185. LIB.BBMap[NewBB] = L;
  186. // Add the basic block to this loop and all parent loops...
  187. while (L) {
  188. L->Blocks.push_back(NewBB);
  189. L = L->getParentLoop();
  190. }
  191. }
  192. /// replaceChildLoopWith - This is used when splitting loops up. It replaces
  193. /// the OldChild entry in our children list with NewChild, and updates the
  194. /// parent pointer of OldChild to be null and the NewChild to be this loop.
  195. /// This updates the loop depth of the new child.
  196. template<class BlockT, class LoopT>
  197. void LoopBase<BlockT, LoopT>::
  198. replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild) {
  199. assert(OldChild->ParentLoop == this && "This loop is already broken!");
  200. assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
  201. typename std::vector<LoopT *>::iterator I =
  202. std::find(SubLoops.begin(), SubLoops.end(), OldChild);
  203. assert(I != SubLoops.end() && "OldChild not in loop!");
  204. *I = NewChild;
  205. OldChild->ParentLoop = 0;
  206. NewChild->ParentLoop = static_cast<LoopT *>(this);
  207. }
  208. /// verifyLoop - Verify loop structure
  209. template<class BlockT, class LoopT>
  210. void LoopBase<BlockT, LoopT>::verifyLoop() const {
  211. #ifndef NDEBUG
  212. assert(!Blocks.empty() && "Loop header is missing");
  213. // Setup for using a depth-first iterator to visit every block in the loop.
  214. SmallVector<BlockT*, 8> ExitBBs;
  215. getExitBlocks(ExitBBs);
  216. llvm::SmallPtrSet<BlockT*, 8> VisitSet;
  217. VisitSet.insert(ExitBBs.begin(), ExitBBs.end());
  218. df_ext_iterator<BlockT*, llvm::SmallPtrSet<BlockT*, 8> >
  219. BI = df_ext_begin(getHeader(), VisitSet),
  220. BE = df_ext_end(getHeader(), VisitSet);
  221. // Keep track of the number of BBs visited.
  222. unsigned NumVisited = 0;
  223. // Sort the blocks vector so that we can use binary search to do quick
  224. // lookups.
  225. SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
  226. std::sort(LoopBBs.begin(), LoopBBs.end());
  227. // Check the individual blocks.
  228. for ( ; BI != BE; ++BI) {
  229. BlockT *BB = *BI;
  230. bool HasInsideLoopSuccs = false;
  231. bool HasInsideLoopPreds = false;
  232. SmallVector<BlockT *, 2> OutsideLoopPreds;
  233. typedef GraphTraits<BlockT*> BlockTraits;
  234. for (typename BlockTraits::ChildIteratorType SI =
  235. BlockTraits::child_begin(BB), SE = BlockTraits::child_end(BB);
  236. SI != SE; ++SI)
  237. if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *SI)) {
  238. HasInsideLoopSuccs = true;
  239. break;
  240. }
  241. typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
  242. for (typename InvBlockTraits::ChildIteratorType PI =
  243. InvBlockTraits::child_begin(BB), PE = InvBlockTraits::child_end(BB);
  244. PI != PE; ++PI) {
  245. BlockT *N = *PI;
  246. if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), N))
  247. HasInsideLoopPreds = true;
  248. else
  249. OutsideLoopPreds.push_back(N);
  250. }
  251. if (BB == getHeader()) {
  252. assert(!OutsideLoopPreds.empty() && "Loop is unreachable!");
  253. } else if (!OutsideLoopPreds.empty()) {
  254. // A non-header loop shouldn't be reachable from outside the loop,
  255. // though it is permitted if the predecessor is not itself actually
  256. // reachable.
  257. BlockT *EntryBB = BB->getParent()->begin();
  258. for (df_iterator<BlockT *> NI = df_begin(EntryBB),
  259. NE = df_end(EntryBB); NI != NE; ++NI)
  260. for (unsigned i = 0, e = OutsideLoopPreds.size(); i != e; ++i)
  261. assert(*NI != OutsideLoopPreds[i] &&
  262. "Loop has multiple entry points!");
  263. }
  264. assert(HasInsideLoopPreds && "Loop block has no in-loop predecessors!");
  265. assert(HasInsideLoopSuccs && "Loop block has no in-loop successors!");
  266. assert(BB != getHeader()->getParent()->begin() &&
  267. "Loop contains function entry block!");
  268. NumVisited++;
  269. }
  270. assert(NumVisited == getNumBlocks() && "Unreachable block in loop");
  271. // Check the subloops.
  272. for (iterator I = begin(), E = end(); I != E; ++I)
  273. // Each block in each subloop should be contained within this loop.
  274. for (block_iterator BI = (*I)->block_begin(), BE = (*I)->block_end();
  275. BI != BE; ++BI) {
  276. assert(std::binary_search(LoopBBs.begin(), LoopBBs.end(), *BI) &&
  277. "Loop does not contain all the blocks of a subloop!");
  278. }
  279. // Check the parent loop pointer.
  280. if (ParentLoop) {
  281. assert(std::find(ParentLoop->begin(), ParentLoop->end(), this) !=
  282. ParentLoop->end() &&
  283. "Loop is not a subloop of its parent!");
  284. }
  285. #endif
  286. }
  287. /// verifyLoop - Verify loop structure of this loop and all nested loops.
  288. template<class BlockT, class LoopT>
  289. void LoopBase<BlockT, LoopT>::verifyLoopNest(
  290. DenseSet<const LoopT*> *Loops) const {
  291. Loops->insert(static_cast<const LoopT *>(this));
  292. // Verify this loop.
  293. verifyLoop();
  294. // Verify the subloops.
  295. for (iterator I = begin(), E = end(); I != E; ++I)
  296. (*I)->verifyLoopNest(Loops);
  297. }
  298. template<class BlockT, class LoopT>
  299. void LoopBase<BlockT, LoopT>::print(raw_ostream &OS, unsigned Depth) const {
  300. OS.indent(Depth*2) << "Loop at depth " << getLoopDepth()
  301. << " containing: ";
  302. for (unsigned i = 0; i < getBlocks().size(); ++i) {
  303. if (i) OS << ",";
  304. BlockT *BB = getBlocks()[i];
  305. WriteAsOperand(OS, BB, false);
  306. if (BB == getHeader()) OS << "<header>";
  307. if (BB == getLoopLatch()) OS << "<latch>";
  308. if (isLoopExiting(BB)) OS << "<exiting>";
  309. }
  310. OS << "\n";
  311. for (iterator I = begin(), E = end(); I != E; ++I)
  312. (*I)->print(OS, Depth+2);
  313. }
  314. //===----------------------------------------------------------------------===//
  315. /// Stable LoopInfo Analysis - Build a loop tree using stable iterators so the
  316. /// result does / not depend on use list (block predecessor) order.
  317. ///
  318. /// Discover a subloop with the specified backedges such that: All blocks within
  319. /// this loop are mapped to this loop or a subloop. And all subloops within this
  320. /// loop have their parent loop set to this loop or a subloop.
  321. template<class BlockT, class LoopT>
  322. static void discoverAndMapSubloop(LoopT *L, ArrayRef<BlockT*> Backedges,
  323. LoopInfoBase<BlockT, LoopT> *LI,
  324. DominatorTreeBase<BlockT> &DomTree) {
  325. typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
  326. unsigned NumBlocks = 0;
  327. unsigned NumSubloops = 0;
  328. // Perform a backward CFG traversal using a worklist.
  329. std::vector<BlockT *> ReverseCFGWorklist(Backedges.begin(), Backedges.end());
  330. while (!ReverseCFGWorklist.empty()) {
  331. BlockT *PredBB = ReverseCFGWorklist.back();
  332. ReverseCFGWorklist.pop_back();
  333. LoopT *Subloop = LI->getLoopFor(PredBB);
  334. if (!Subloop) {
  335. if (!DomTree.isReachableFromEntry(PredBB))
  336. continue;
  337. // This is an undiscovered block. Map it to the current loop.
  338. LI->changeLoopFor(PredBB, L);
  339. ++NumBlocks;
  340. if (PredBB == L->getHeader())
  341. continue;
  342. // Push all block predecessors on the worklist.
  343. ReverseCFGWorklist.insert(ReverseCFGWorklist.end(),
  344. InvBlockTraits::child_begin(PredBB),
  345. InvBlockTraits::child_end(PredBB));
  346. }
  347. else {
  348. // This is a discovered block. Find its outermost discovered loop.
  349. while (LoopT *Parent = Subloop->getParentLoop())
  350. Subloop = Parent;
  351. // If it is already discovered to be a subloop of this loop, continue.
  352. if (Subloop == L)
  353. continue;
  354. // Discover a subloop of this loop.
  355. Subloop->setParentLoop(L);
  356. ++NumSubloops;
  357. NumBlocks += Subloop->getBlocks().capacity();
  358. PredBB = Subloop->getHeader();
  359. // Continue traversal along predecessors that are not loop-back edges from
  360. // within this subloop tree itself. Note that a predecessor may directly
  361. // reach another subloop that is not yet discovered to be a subloop of
  362. // this loop, which we must traverse.
  363. for (typename InvBlockTraits::ChildIteratorType PI =
  364. InvBlockTraits::child_begin(PredBB),
  365. PE = InvBlockTraits::child_end(PredBB); PI != PE; ++PI) {
  366. if (LI->getLoopFor(*PI) != Subloop)
  367. ReverseCFGWorklist.push_back(*PI);
  368. }
  369. }
  370. }
  371. L->getSubLoopsVector().reserve(NumSubloops);
  372. L->getBlocksVector().reserve(NumBlocks);
  373. }
  374. namespace {
  375. /// Populate all loop data in a stable order during a single forward DFS.
  376. template<class BlockT, class LoopT>
  377. class PopulateLoopsDFS {
  378. typedef GraphTraits<BlockT*> BlockTraits;
  379. typedef typename BlockTraits::ChildIteratorType SuccIterTy;
  380. LoopInfoBase<BlockT, LoopT> *LI;
  381. DenseSet<const BlockT *> VisitedBlocks;
  382. std::vector<std::pair<BlockT*, SuccIterTy> > DFSStack;
  383. public:
  384. PopulateLoopsDFS(LoopInfoBase<BlockT, LoopT> *li):
  385. LI(li) {}
  386. void traverse(BlockT *EntryBlock);
  387. protected:
  388. void insertIntoLoop(BlockT *Block);
  389. BlockT *dfsSource() { return DFSStack.back().first; }
  390. SuccIterTy &dfsSucc() { return DFSStack.back().second; }
  391. SuccIterTy dfsSuccEnd() { return BlockTraits::child_end(dfsSource()); }
  392. void pushBlock(BlockT *Block) {
  393. DFSStack.push_back(std::make_pair(Block, BlockTraits::child_begin(Block)));
  394. }
  395. };
  396. } // anonymous
  397. /// Top-level driver for the forward DFS within the loop.
  398. template<class BlockT, class LoopT>
  399. void PopulateLoopsDFS<BlockT, LoopT>::traverse(BlockT *EntryBlock) {
  400. pushBlock(EntryBlock);
  401. VisitedBlocks.insert(EntryBlock);
  402. while (!DFSStack.empty()) {
  403. // Traverse the leftmost path as far as possible.
  404. while (dfsSucc() != dfsSuccEnd()) {
  405. BlockT *BB = *dfsSucc();
  406. ++dfsSucc();
  407. if (!VisitedBlocks.insert(BB).second)
  408. continue;
  409. // Push the next DFS successor onto the stack.
  410. pushBlock(BB);
  411. }
  412. // Visit the top of the stack in postorder and backtrack.
  413. insertIntoLoop(dfsSource());
  414. DFSStack.pop_back();
  415. }
  416. }
  417. /// Add a single Block to its ancestor loops in PostOrder. If the block is a
  418. /// subloop header, add the subloop to its parent in PostOrder, then reverse the
  419. /// Block and Subloop vectors of the now complete subloop to achieve RPO.
  420. template<class BlockT, class LoopT>
  421. void PopulateLoopsDFS<BlockT, LoopT>::insertIntoLoop(BlockT *Block) {
  422. LoopT *Subloop = LI->getLoopFor(Block);
  423. if (Subloop && Block == Subloop->getHeader()) {
  424. // We reach this point once per subloop after processing all the blocks in
  425. // the subloop.
  426. if (Subloop->getParentLoop())
  427. Subloop->getParentLoop()->getSubLoopsVector().push_back(Subloop);
  428. else
  429. LI->addTopLevelLoop(Subloop);
  430. // For convenience, Blocks and Subloops are inserted in postorder. Reverse
  431. // the lists, except for the loop header, which is always at the beginning.
  432. std::reverse(Subloop->getBlocksVector().begin()+1,
  433. Subloop->getBlocksVector().end());
  434. std::reverse(Subloop->getSubLoopsVector().begin(),
  435. Subloop->getSubLoopsVector().end());
  436. Subloop = Subloop->getParentLoop();
  437. }
  438. for (; Subloop; Subloop = Subloop->getParentLoop())
  439. Subloop->getBlocksVector().push_back(Block);
  440. }
  441. /// Analyze LoopInfo discovers loops during a postorder DominatorTree traversal
  442. /// interleaved with backward CFG traversals within each subloop
  443. /// (discoverAndMapSubloop). The backward traversal skips inner subloops, so
  444. /// this part of the algorithm is linear in the number of CFG edges. Subloop and
  445. /// Block vectors are then populated during a single forward CFG traversal
  446. /// (PopulateLoopDFS).
  447. ///
  448. /// During the two CFG traversals each block is seen three times:
  449. /// 1) Discovered and mapped by a reverse CFG traversal.
  450. /// 2) Visited during a forward DFS CFG traversal.
  451. /// 3) Reverse-inserted in the loop in postorder following forward DFS.
  452. ///
  453. /// The Block vectors are inclusive, so step 3 requires loop-depth number of
  454. /// insertions per block.
  455. template<class BlockT, class LoopT>
  456. void LoopInfoBase<BlockT, LoopT>::
  457. Analyze(DominatorTreeBase<BlockT> &DomTree) {
  458. // Postorder traversal of the dominator tree.
  459. DomTreeNodeBase<BlockT>* DomRoot = DomTree.getRootNode();
  460. for (po_iterator<DomTreeNodeBase<BlockT>*> DomIter = po_begin(DomRoot),
  461. DomEnd = po_end(DomRoot); DomIter != DomEnd; ++DomIter) {
  462. BlockT *Header = DomIter->getBlock();
  463. SmallVector<BlockT *, 4> Backedges;
  464. // Check each predecessor of the potential loop header.
  465. typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
  466. for (typename InvBlockTraits::ChildIteratorType PI =
  467. InvBlockTraits::child_begin(Header),
  468. PE = InvBlockTraits::child_end(Header); PI != PE; ++PI) {
  469. BlockT *Backedge = *PI;
  470. // If Header dominates predBB, this is a new loop. Collect the backedges.
  471. if (DomTree.dominates(Header, Backedge)
  472. && DomTree.isReachableFromEntry(Backedge)) {
  473. Backedges.push_back(Backedge);
  474. }
  475. }
  476. // Perform a backward CFG traversal to discover and map blocks in this loop.
  477. if (!Backedges.empty()) {
  478. LoopT *L = new LoopT(Header);
  479. discoverAndMapSubloop(L, ArrayRef<BlockT*>(Backedges), this, DomTree);
  480. }
  481. }
  482. // Perform a single forward CFG traversal to populate block and subloop
  483. // vectors for all loops.
  484. PopulateLoopsDFS<BlockT, LoopT> DFS(this);
  485. DFS.traverse(DomRoot->getBlock());
  486. }
  487. // Debugging
  488. template<class BlockT, class LoopT>
  489. void LoopInfoBase<BlockT, LoopT>::print(raw_ostream &OS) const {
  490. for (unsigned i = 0; i < TopLevelLoops.size(); ++i)
  491. TopLevelLoops[i]->print(OS);
  492. #if 0
  493. for (DenseMap<BasicBlock*, LoopT*>::const_iterator I = BBMap.begin(),
  494. E = BBMap.end(); I != E; ++I)
  495. OS << "BB '" << I->first->getName() << "' level = "
  496. << I->second->getLoopDepth() << "\n";
  497. #endif
  498. }
  499. } // End llvm namespace
  500. #endif