LoopInfoImpl.h 20 KB

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