]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Analysis/LoopInfoImpl.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r302418, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / Analysis / LoopInfoImpl.h
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/DepthFirstIterator.h"
19 #include "llvm/ADT/PostOrderIterator.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/Analysis/LoopInfo.h"
23 #include "llvm/IR/Dominators.h"
24
25 namespace llvm {
26
27 //===----------------------------------------------------------------------===//
28 // APIs for simple analysis of the loop. See header notes.
29
30 /// getExitingBlocks - Return all blocks inside the loop that have successors
31 /// outside of the loop.  These are the blocks _inside of the current loop_
32 /// which branch out.  The returned list is always unique.
33 ///
34 template<class BlockT, class LoopT>
35 void LoopBase<BlockT, LoopT>::
36 getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const {
37   for (const auto BB : blocks())
38     for (const auto &Succ : children<BlockT*>(BB))
39       if (!contains(Succ)) {
40         // Not in current loop? It must be an exit block.
41         ExitingBlocks.push_back(BB);
42         break;
43       }
44 }
45
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];
54   return nullptr;
55 }
56
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.
59 ///
60 template<class BlockT, class LoopT>
61 void LoopBase<BlockT, LoopT>::
62 getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
63   for (const auto BB : blocks())
64     for (const auto &Succ : children<BlockT*>(BB))
65       if (!contains(Succ))
66         // Not in current loop? It must be an exit block.
67         ExitBlocks.push_back(Succ);
68 }
69
70 /// getExitBlock - If getExitBlocks would return exactly one block,
71 /// return that block. Otherwise return null.
72 template<class BlockT, class LoopT>
73 BlockT *LoopBase<BlockT, LoopT>::getExitBlock() const {
74   SmallVector<BlockT*, 8> ExitBlocks;
75   getExitBlocks(ExitBlocks);
76   if (ExitBlocks.size() == 1)
77     return ExitBlocks[0];
78   return nullptr;
79 }
80
81 /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
82 template<class BlockT, class LoopT>
83 void LoopBase<BlockT, LoopT>::
84 getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const {
85   for (const auto BB : blocks())
86     for (const auto &Succ : children<BlockT*>(BB))
87       if (!contains(Succ))
88         // Not in current loop? It must be an exit block.
89         ExitEdges.emplace_back(BB, Succ);
90 }
91
92 /// getLoopPreheader - If there is a preheader for this loop, return it.  A
93 /// loop has a preheader if there is only one edge to the header of the loop
94 /// from outside of the loop.  If this is the case, the block branching to the
95 /// header of the loop is the preheader node.
96 ///
97 /// This method returns null if there is no preheader for the loop.
98 ///
99 template<class BlockT, class LoopT>
100 BlockT *LoopBase<BlockT, LoopT>::getLoopPreheader() const {
101   // Keep track of nodes outside the loop branching to the header...
102   BlockT *Out = getLoopPredecessor();
103   if (!Out) return nullptr;
104
105   // Make sure there is only one exit out of the preheader.
106   typedef GraphTraits<BlockT*> BlockTraits;
107   typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
108   ++SI;
109   if (SI != BlockTraits::child_end(Out))
110     return nullptr;  // Multiple exits from the block, must not be a preheader.
111
112   // The predecessor has exactly one successor, so it is a preheader.
113   return Out;
114 }
115
116 /// getLoopPredecessor - If the given loop's header has exactly one unique
117 /// predecessor outside the loop, return it. Otherwise return null.
118 /// This is less strict that the loop "preheader" concept, which requires
119 /// the predecessor to have exactly one successor.
120 ///
121 template<class BlockT, class LoopT>
122 BlockT *LoopBase<BlockT, LoopT>::getLoopPredecessor() const {
123   // Keep track of nodes outside the loop branching to the header...
124   BlockT *Out = nullptr;
125
126   // Loop over the predecessors of the header node...
127   BlockT *Header = getHeader();
128   for (const auto Pred : children<Inverse<BlockT*>>(Header)) {
129     if (!contains(Pred)) {     // If the block is not in the loop...
130       if (Out && Out != Pred)
131         return nullptr;     // Multiple predecessors outside the loop
132       Out = Pred;
133     }
134   }
135
136   // Make sure there is only one exit out of the preheader.
137   assert(Out && "Header of loop has no predecessors from outside loop?");
138   return Out;
139 }
140
141 /// getLoopLatch - If there is a single latch block for this loop, return it.
142 /// A latch block is a block that contains a branch back to the header.
143 template<class BlockT, class LoopT>
144 BlockT *LoopBase<BlockT, LoopT>::getLoopLatch() const {
145   BlockT *Header = getHeader();
146   BlockT *Latch = nullptr;
147   for (const auto Pred : children<Inverse<BlockT*>>(Header)) {
148     if (contains(Pred)) {
149       if (Latch) return nullptr;
150       Latch = Pred;
151     }
152   }
153
154   return Latch;
155 }
156
157 //===----------------------------------------------------------------------===//
158 // APIs for updating loop information after changing the CFG
159 //
160
161 /// addBasicBlockToLoop - This method is used by other analyses to update loop
162 /// information.  NewBB is set to be a new member of the current loop.
163 /// Because of this, it is added as a member of all parent loops, and is added
164 /// to the specified LoopInfo object as being in the current basic block.  It
165 /// is not valid to replace the loop header with this method.
166 ///
167 template<class BlockT, class LoopT>
168 void LoopBase<BlockT, LoopT>::
169 addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LIB) {
170 #ifndef NDEBUG
171   if (!Blocks.empty()) {
172     auto SameHeader = LIB[getHeader()];
173     assert(contains(SameHeader) && getHeader() == SameHeader->getHeader()
174            && "Incorrect LI specified for this loop!");
175   }
176 #endif
177   assert(NewBB && "Cannot add a null basic block to the loop!");
178   assert(!LIB[NewBB] && "BasicBlock already in the loop!");
179
180   LoopT *L = static_cast<LoopT *>(this);
181
182   // Add the loop mapping to the LoopInfo object...
183   LIB.BBMap[NewBB] = L;
184
185   // Add the basic block to this loop and all parent loops...
186   while (L) {
187     L->addBlockEntry(NewBB);
188     L = L->getParentLoop();
189   }
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 && "NewChild already has a parent!");
201   typename std::vector<LoopT *>::iterator I = find(SubLoops, OldChild);
202   assert(I != SubLoops.end() && "OldChild not in loop!");
203   *I = NewChild;
204   OldChild->ParentLoop = nullptr;
205   NewChild->ParentLoop = static_cast<LoopT *>(this);
206 }
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
214   // Setup for using a depth-first iterator to visit every block in the loop.
215   SmallVector<BlockT*, 8> ExitBBs;
216   getExitBlocks(ExitBBs);
217   df_iterator_default_set<BlockT*> VisitSet;
218   VisitSet.insert(ExitBBs.begin(), ExitBBs.end());
219   df_ext_iterator<BlockT*, df_iterator_default_set<BlockT*>>
220     BI = df_ext_begin(getHeader(), VisitSet),
221     BE = df_ext_end(getHeader(), VisitSet);
222
223   // Keep track of the BBs visited.
224   SmallPtrSet<BlockT*, 8> VisitedBBs;
225
226   // Check the individual blocks.
227   for ( ; BI != BE; ++BI) {
228     BlockT *BB = *BI;
229
230     assert(std::any_of(GraphTraits<BlockT*>::child_begin(BB),
231                        GraphTraits<BlockT*>::child_end(BB),
232                        [&](BlockT *B){return contains(B);}) &&
233            "Loop block has no in-loop successors!");
234
235     assert(std::any_of(GraphTraits<Inverse<BlockT*> >::child_begin(BB),
236                        GraphTraits<Inverse<BlockT*> >::child_end(BB),
237                        [&](BlockT *B){return contains(B);}) &&
238            "Loop block has no in-loop predecessors!");
239
240     SmallVector<BlockT *, 2> OutsideLoopPreds;
241     std::for_each(GraphTraits<Inverse<BlockT*> >::child_begin(BB),
242                   GraphTraits<Inverse<BlockT*> >::child_end(BB),
243                   [&](BlockT *B){if (!contains(B))
244                       OutsideLoopPreds.push_back(B);
245                   });
246
247     if (BB == getHeader()) {
248         assert(!OutsideLoopPreds.empty() && "Loop is unreachable!");
249     } else if (!OutsideLoopPreds.empty()) {
250       // A non-header loop shouldn't be reachable from outside the loop,
251       // though it is permitted if the predecessor is not itself actually
252       // reachable.
253       BlockT *EntryBB = &BB->getParent()->front();
254       for (BlockT *CB : depth_first(EntryBB))
255         for (unsigned i = 0, e = OutsideLoopPreds.size(); i != e; ++i)
256           assert(CB != OutsideLoopPreds[i] &&
257                  "Loop has multiple entry points!");
258     }
259     assert(BB != &getHeader()->getParent()->front() &&
260            "Loop contains function entry block!");
261
262     VisitedBBs.insert(BB);
263   }
264
265   if (VisitedBBs.size() != getNumBlocks()) {
266     dbgs() << "The following blocks are unreachable in the loop: ";
267     for (auto BB : Blocks) {
268       if (!VisitedBBs.count(BB)) {
269         dbgs() << *BB << "\n";
270       }
271     }
272     assert(false && "Unreachable block in loop");
273   }
274
275   // Check the subloops.
276   for (iterator I = begin(), E = end(); I != E; ++I)
277     // Each block in each subloop should be contained within this loop.
278     for (block_iterator BI = (*I)->block_begin(), BE = (*I)->block_end();
279          BI != BE; ++BI) {
280         assert(contains(*BI) &&
281                "Loop does not contain all the blocks of a subloop!");
282     }
283
284   // Check the parent loop pointer.
285   if (ParentLoop) {
286     assert(is_contained(*ParentLoop, this) &&
287            "Loop is not a subloop of its parent!");
288   }
289 #endif
290 }
291
292 /// verifyLoop - Verify loop structure of this loop and all nested loops.
293 template<class BlockT, class LoopT>
294 void LoopBase<BlockT, LoopT>::verifyLoopNest(
295   DenseSet<const LoopT*> *Loops) const {
296   Loops->insert(static_cast<const LoopT *>(this));
297   // Verify this loop.
298   verifyLoop();
299   // Verify the subloops.
300   for (iterator I = begin(), E = end(); I != E; ++I)
301     (*I)->verifyLoopNest(Loops);
302 }
303
304 template<class BlockT, class LoopT>
305 void LoopBase<BlockT, LoopT>::print(raw_ostream &OS, unsigned Depth,
306                                     bool Verbose) const {
307   OS.indent(Depth*2) << "Loop at depth " << getLoopDepth()
308        << " containing: ";
309
310   BlockT *H = getHeader();
311   for (unsigned i = 0; i < getBlocks().size(); ++i) {
312     BlockT *BB = getBlocks()[i];
313     if (!Verbose) {
314       if (i) OS << ",";
315       BB->printAsOperand(OS, false);
316     } else OS << "\n";
317
318     if (BB == H) OS << "<header>";
319     if (isLoopLatch(BB)) OS << "<latch>";
320     if (isLoopExiting(BB)) OS << "<exiting>";
321     if (Verbose)
322       BB->print(OS);
323   }
324   OS << "\n";
325
326   for (iterator I = begin(), E = end(); I != E; ++I)
327     (*I)->print(OS, Depth+2);
328 }
329
330 //===----------------------------------------------------------------------===//
331 /// Stable LoopInfo Analysis - Build a loop tree using stable iterators so the
332 /// result does / not depend on use list (block predecessor) order.
333 ///
334
335 /// Discover a subloop with the specified backedges such that: All blocks within
336 /// this loop are mapped to this loop or a subloop. And all subloops within this
337 /// loop have their parent loop set to this loop or a subloop.
338 template<class BlockT, class LoopT>
339 static void discoverAndMapSubloop(LoopT *L, ArrayRef<BlockT*> Backedges,
340                                   LoopInfoBase<BlockT, LoopT> *LI,
341                                   const DominatorTreeBase<BlockT> &DomTree) {
342   typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
343
344   unsigned NumBlocks = 0;
345   unsigned NumSubloops = 0;
346
347   // Perform a backward CFG traversal using a worklist.
348   std::vector<BlockT *> ReverseCFGWorklist(Backedges.begin(), Backedges.end());
349   while (!ReverseCFGWorklist.empty()) {
350     BlockT *PredBB = ReverseCFGWorklist.back();
351     ReverseCFGWorklist.pop_back();
352
353     LoopT *Subloop = LI->getLoopFor(PredBB);
354     if (!Subloop) {
355       if (!DomTree.isReachableFromEntry(PredBB))
356         continue;
357
358       // This is an undiscovered block. Map it to the current loop.
359       LI->changeLoopFor(PredBB, L);
360       ++NumBlocks;
361       if (PredBB == L->getHeader())
362           continue;
363       // Push all block predecessors on the worklist.
364       ReverseCFGWorklist.insert(ReverseCFGWorklist.end(),
365                                 InvBlockTraits::child_begin(PredBB),
366                                 InvBlockTraits::child_end(PredBB));
367     }
368     else {
369       // This is a discovered block. Find its outermost discovered loop.
370       while (LoopT *Parent = Subloop->getParentLoop())
371         Subloop = Parent;
372
373       // If it is already discovered to be a subloop of this loop, continue.
374       if (Subloop == L)
375         continue;
376
377       // Discover a subloop of this loop.
378       Subloop->setParentLoop(L);
379       ++NumSubloops;
380       NumBlocks += Subloop->getBlocks().capacity();
381       PredBB = Subloop->getHeader();
382       // Continue traversal along predecessors that are not loop-back edges from
383       // within this subloop tree itself. Note that a predecessor may directly
384       // reach another subloop that is not yet discovered to be a subloop of
385       // this loop, which we must traverse.
386       for (const auto Pred : children<Inverse<BlockT*>>(PredBB)) {
387         if (LI->getLoopFor(Pred) != Subloop)
388           ReverseCFGWorklist.push_back(Pred);
389       }
390     }
391   }
392   L->getSubLoopsVector().reserve(NumSubloops);
393   L->reserveBlocks(NumBlocks);
394 }
395
396 /// Populate all loop data in a stable order during a single forward DFS.
397 template<class BlockT, class LoopT>
398 class PopulateLoopsDFS {
399   typedef GraphTraits<BlockT*> BlockTraits;
400   typedef typename BlockTraits::ChildIteratorType SuccIterTy;
401
402   LoopInfoBase<BlockT, LoopT> *LI;
403 public:
404   PopulateLoopsDFS(LoopInfoBase<BlockT, LoopT> *li):
405     LI(li) {}
406
407   void traverse(BlockT *EntryBlock);
408
409 protected:
410   void insertIntoLoop(BlockT *Block);
411 };
412
413 /// Top-level driver for the forward DFS within the loop.
414 template<class BlockT, class LoopT>
415 void PopulateLoopsDFS<BlockT, LoopT>::traverse(BlockT *EntryBlock) {
416   for (BlockT *BB : post_order(EntryBlock))
417     insertIntoLoop(BB);
418 }
419
420 /// Add a single Block to its ancestor loops in PostOrder. If the block is a
421 /// subloop header, add the subloop to its parent in PostOrder, then reverse the
422 /// Block and Subloop vectors of the now complete subloop to achieve RPO.
423 template<class BlockT, class LoopT>
424 void PopulateLoopsDFS<BlockT, LoopT>::insertIntoLoop(BlockT *Block) {
425   LoopT *Subloop = LI->getLoopFor(Block);
426   if (Subloop && Block == Subloop->getHeader()) {
427     // We reach this point once per subloop after processing all the blocks in
428     // the subloop.
429     if (Subloop->getParentLoop())
430       Subloop->getParentLoop()->getSubLoopsVector().push_back(Subloop);
431     else
432       LI->addTopLevelLoop(Subloop);
433
434     // For convenience, Blocks and Subloops are inserted in postorder. Reverse
435     // the lists, except for the loop header, which is always at the beginning.
436     Subloop->reverseBlock(1);
437     std::reverse(Subloop->getSubLoopsVector().begin(),
438                  Subloop->getSubLoopsVector().end());
439
440     Subloop = Subloop->getParentLoop();
441   }
442   for (; Subloop; Subloop = Subloop->getParentLoop())
443     Subloop->addBlockEntry(Block);
444 }
445
446 /// Analyze LoopInfo discovers loops during a postorder DominatorTree traversal
447 /// interleaved with backward CFG traversals within each subloop
448 /// (discoverAndMapSubloop). The backward traversal skips inner subloops, so
449 /// this part of the algorithm is linear in the number of CFG edges. Subloop and
450 /// Block vectors are then populated during a single forward CFG traversal
451 /// (PopulateLoopDFS).
452 ///
453 /// During the two CFG traversals each block is seen three times:
454 /// 1) Discovered and mapped by a reverse CFG traversal.
455 /// 2) Visited during a forward DFS CFG traversal.
456 /// 3) Reverse-inserted in the loop in postorder following forward DFS.
457 ///
458 /// The Block vectors are inclusive, so step 3 requires loop-depth number of
459 /// insertions per block.
460 template<class BlockT, class LoopT>
461 void LoopInfoBase<BlockT, LoopT>::
462 analyze(const DominatorTreeBase<BlockT> &DomTree) {
463
464   // Postorder traversal of the dominator tree.
465   const DomTreeNodeBase<BlockT> *DomRoot = DomTree.getRootNode();
466   for (auto DomNode : post_order(DomRoot)) {
467
468     BlockT *Header = DomNode->getBlock();
469     SmallVector<BlockT *, 4> Backedges;
470
471     // Check each predecessor of the potential loop header.
472     for (const auto Backedge : children<Inverse<BlockT*>>(Header)) {
473       // If Header dominates predBB, this is a new loop. Collect the backedges.
474       if (DomTree.dominates(Header, Backedge)
475           && DomTree.isReachableFromEntry(Backedge)) {
476         Backedges.push_back(Backedge);
477       }
478     }
479     // Perform a backward CFG traversal to discover and map blocks in this loop.
480     if (!Backedges.empty()) {
481       LoopT *L = new LoopT(Header);
482       discoverAndMapSubloop(L, ArrayRef<BlockT*>(Backedges), this, DomTree);
483     }
484   }
485   // Perform a single forward CFG traversal to populate block and subloop
486   // vectors for all loops.
487   PopulateLoopsDFS<BlockT, LoopT> DFS(this);
488   DFS.traverse(DomRoot->getBlock());
489 }
490
491 template <class BlockT, class LoopT>
492 SmallVector<LoopT *, 4> LoopInfoBase<BlockT, LoopT>::getLoopsInPreorder() {
493   SmallVector<LoopT *, 4> PreOrderLoops, PreOrderWorklist;
494   // The outer-most loop actually goes into the result in the same relative
495   // order as we walk it. But LoopInfo stores the top level loops in reverse
496   // program order so for here we reverse it to get forward program order.
497   // FIXME: If we change the order of LoopInfo we will want to remove the
498   // reverse here.
499   for (LoopT *RootL : reverse(*this)) {
500     assert(PreOrderWorklist.empty() &&
501            "Must start with an empty preorder walk worklist.");
502     PreOrderWorklist.push_back(RootL);
503     do {
504       LoopT *L = PreOrderWorklist.pop_back_val();
505       // Sub-loops are stored in forward program order, but will process the
506       // worklist backwards so append them in reverse order.
507       PreOrderWorklist.append(L->rbegin(), L->rend());
508       PreOrderLoops.push_back(L);
509     } while (!PreOrderWorklist.empty());
510   }
511
512   return PreOrderLoops;
513 }
514
515 template <class BlockT, class LoopT>
516 SmallVector<LoopT *, 4>
517 LoopInfoBase<BlockT, LoopT>::getLoopsInReverseSiblingPreorder() {
518   SmallVector<LoopT *, 4> PreOrderLoops, PreOrderWorklist;
519   // The outer-most loop actually goes into the result in the same relative
520   // order as we walk it. LoopInfo stores the top level loops in reverse
521   // program order so we walk in order here.
522   // FIXME: If we change the order of LoopInfo we will want to add a reverse
523   // here.
524   for (LoopT *RootL : *this) {
525     assert(PreOrderWorklist.empty() &&
526            "Must start with an empty preorder walk worklist.");
527     PreOrderWorklist.push_back(RootL);
528     do {
529       LoopT *L = PreOrderWorklist.pop_back_val();
530       // Sub-loops are stored in forward program order, but will process the
531       // worklist backwards so we can just append them in order.
532       PreOrderWorklist.append(L->begin(), L->end());
533       PreOrderLoops.push_back(L);
534     } while (!PreOrderWorklist.empty());
535   }
536
537   return PreOrderLoops;
538 }
539
540 // Debugging
541 template<class BlockT, class LoopT>
542 void LoopInfoBase<BlockT, LoopT>::print(raw_ostream &OS) const {
543   for (unsigned i = 0; i < TopLevelLoops.size(); ++i)
544     TopLevelLoops[i]->print(OS);
545 #if 0
546   for (DenseMap<BasicBlock*, LoopT*>::const_iterator I = BBMap.begin(),
547          E = BBMap.end(); I != E; ++I)
548     OS << "BB '" << I->first->getName() << "' level = "
549        << I->second->getLoopDepth() << "\n";
550 #endif
551 }
552
553 template <typename T>
554 bool compareVectors(std::vector<T> &BB1, std::vector<T> &BB2) {
555   std::sort(BB1.begin(), BB1.end());
556   std::sort(BB2.begin(), BB2.end());
557   return BB1 == BB2;
558 }
559
560 template <class BlockT, class LoopT>
561 void addInnerLoopsToHeadersMap(DenseMap<BlockT *, const LoopT *> &LoopHeaders,
562                                const LoopInfoBase<BlockT, LoopT> &LI,
563                                const LoopT &L) {
564   LoopHeaders[L.getHeader()] = &L;
565   for (LoopT *SL : L)
566     addInnerLoopsToHeadersMap(LoopHeaders, LI, *SL);
567 }
568
569 #ifndef NDEBUG
570 template <class BlockT, class LoopT>
571 static void compareLoops(const LoopT *L, const LoopT *OtherL,
572                          DenseMap<BlockT *, const LoopT *> &OtherLoopHeaders) {
573   BlockT *H = L->getHeader();
574   BlockT *OtherH = OtherL->getHeader();
575   assert(H == OtherH &&
576          "Mismatched headers even though found in the same map entry!");
577
578   assert(L->getLoopDepth() == OtherL->getLoopDepth() &&
579          "Mismatched loop depth!");
580   const LoopT *ParentL = L, *OtherParentL = OtherL;
581   do {
582     assert(ParentL->getHeader() == OtherParentL->getHeader() &&
583            "Mismatched parent loop headers!");
584     ParentL = ParentL->getParentLoop();
585     OtherParentL = OtherParentL->getParentLoop();
586   } while (ParentL);
587
588   for (const LoopT *SubL : *L) {
589     BlockT *SubH = SubL->getHeader();
590     const LoopT *OtherSubL = OtherLoopHeaders.lookup(SubH);
591     assert(OtherSubL && "Inner loop is missing in computed loop info!");
592     OtherLoopHeaders.erase(SubH);
593     compareLoops(SubL, OtherSubL, OtherLoopHeaders);
594   }
595
596   std::vector<BlockT *> BBs = L->getBlocks();
597   std::vector<BlockT *> OtherBBs = OtherL->getBlocks();
598   assert(compareVectors(BBs, OtherBBs) &&
599          "Mismatched basic blocks in the loops!");
600 }
601 #endif
602
603 template <class BlockT, class LoopT>
604 void LoopInfoBase<BlockT, LoopT>::verify(
605     const DominatorTreeBase<BlockT> &DomTree) const {
606   DenseSet<const LoopT*> Loops;
607   for (iterator I = begin(), E = end(); I != E; ++I) {
608     assert(!(*I)->getParentLoop() && "Top-level loop has a parent!");
609     (*I)->verifyLoopNest(&Loops);
610   }
611
612   // Verify that blocks are mapped to valid loops.
613 #ifndef NDEBUG
614   for (auto &Entry : BBMap) {
615     const BlockT *BB = Entry.first;
616     LoopT *L = Entry.second;
617     assert(Loops.count(L) && "orphaned loop");
618     assert(L->contains(BB) && "orphaned block");
619   }
620
621   // Recompute LoopInfo to verify loops structure.
622   LoopInfoBase<BlockT, LoopT> OtherLI;
623   OtherLI.analyze(DomTree);
624
625   // Build a map we can use to move from our LI to the computed one. This
626   // allows us to ignore the particular order in any layer of the loop forest
627   // while still comparing the structure.
628   DenseMap<BlockT *, const LoopT *> OtherLoopHeaders;
629   for (LoopT *L : OtherLI)
630     addInnerLoopsToHeadersMap(OtherLoopHeaders, OtherLI, *L);
631
632   // Walk the top level loops and ensure there is a corresponding top-level
633   // loop in the computed version and then recursively compare those loop
634   // nests.
635   for (LoopT *L : *this) {
636     BlockT *Header = L->getHeader();
637     const LoopT *OtherL = OtherLoopHeaders.lookup(Header);
638     assert(OtherL && "Top level loop is missing in computed loop info!");
639     // Now that we've matched this loop, erase its header from the map.
640     OtherLoopHeaders.erase(Header);
641     // And recursively compare these loops.
642     compareLoops(L, OtherL, OtherLoopHeaders);
643   }
644
645   // Any remaining entries in the map are loops which were found when computing
646   // a fresh LoopInfo but not present in the current one.
647   if (!OtherLoopHeaders.empty()) {
648     for (const auto &HeaderAndLoop : OtherLoopHeaders)
649       dbgs() << "Found new loop: " << *HeaderAndLoop.second << "\n";
650     llvm_unreachable("Found new loops when recomputing LoopInfo!");
651   }
652 #endif
653 }
654
655 } // End llvm namespace
656
657 #endif