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