1 //===- llvm/Analysis/LoopInfoImpl.h - Natural Loop Calculator ---*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This is the generic implementation of LoopInfo used for both Loops and
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_LOOPINFOIMPL_H
15 #define LLVM_ANALYSIS_LOOPINFOIMPL_H
17 #include "llvm/ADT/DepthFirstIterator.h"
18 #include "llvm/ADT/PostOrderIterator.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/Analysis/LoopInfo.h"
22 #include "llvm/IR/Dominators.h"
26 //===----------------------------------------------------------------------===//
27 // APIs for simple analysis of the loop. See header notes.
29 /// getExitingBlocks - Return all blocks inside the loop that have successors
30 /// outside of the loop. These are the blocks _inside of the current loop_
31 /// which branch out. The returned list is always unique.
33 template <class BlockT
, class LoopT
>
34 void LoopBase
<BlockT
, LoopT
>::getExitingBlocks(
35 SmallVectorImpl
<BlockT
*> &ExitingBlocks
) const {
36 assert(!isInvalid() && "Loop not in a valid state!");
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
);
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 assert(!isInvalid() && "Loop not in a valid state!");
51 SmallVector
<BlockT
*, 8> ExitingBlocks
;
52 getExitingBlocks(ExitingBlocks
);
53 if (ExitingBlocks
.size() == 1)
54 return ExitingBlocks
[0];
58 /// getExitBlocks - Return all of the successor blocks of this loop. These
59 /// are the blocks _outside of the current loop_ which are branched to.
61 template <class BlockT
, class LoopT
>
62 void LoopBase
<BlockT
, LoopT
>::getExitBlocks(
63 SmallVectorImpl
<BlockT
*> &ExitBlocks
) const {
64 assert(!isInvalid() && "Loop not in a valid state!");
65 for (const auto BB
: blocks())
66 for (const auto &Succ
: children
<BlockT
*>(BB
))
68 // Not in current loop? It must be an exit block.
69 ExitBlocks
.push_back(Succ
);
72 /// getExitBlock - If getExitBlocks would return exactly one block,
73 /// return that block. Otherwise return null.
74 template <class BlockT
, class LoopT
>
75 BlockT
*LoopBase
<BlockT
, LoopT
>::getExitBlock() const {
76 assert(!isInvalid() && "Loop not in a valid state!");
77 SmallVector
<BlockT
*, 8> ExitBlocks
;
78 getExitBlocks(ExitBlocks
);
79 if (ExitBlocks
.size() == 1)
84 template <class BlockT
, class LoopT
>
85 bool LoopBase
<BlockT
, LoopT
>::hasDedicatedExits() const {
86 // Each predecessor of each exit block of a normal loop is contained
88 SmallVector
<BlockT
*, 4> ExitBlocks
;
89 getExitBlocks(ExitBlocks
);
90 for (BlockT
*EB
: ExitBlocks
)
91 for (BlockT
*Predecessor
: children
<Inverse
<BlockT
*>>(EB
))
92 if (!contains(Predecessor
))
94 // All the requirements are met.
98 template <class BlockT
, class LoopT
>
99 void LoopBase
<BlockT
, LoopT
>::getUniqueExitBlocks(
100 SmallVectorImpl
<BlockT
*> &ExitBlocks
) const {
101 typedef GraphTraits
<BlockT
*> BlockTraits
;
102 typedef GraphTraits
<Inverse
<BlockT
*>> InvBlockTraits
;
104 assert(hasDedicatedExits() &&
105 "getUniqueExitBlocks assumes the loop has canonical form exits!");
107 SmallVector
<BlockT
*, 32> SwitchExitBlocks
;
108 for (BlockT
*Block
: this->blocks()) {
109 SwitchExitBlocks
.clear();
110 for (BlockT
*Successor
: children
<BlockT
*>(Block
)) {
111 // If block is inside the loop then it is not an exit block.
112 if (contains(Successor
))
115 BlockT
*FirstPred
= *InvBlockTraits::child_begin(Successor
);
117 // If current basic block is this exit block's first predecessor then only
118 // insert exit block in to the output ExitBlocks vector. This ensures that
119 // same exit block is not inserted twice into ExitBlocks vector.
120 if (Block
!= FirstPred
)
123 // If a terminator has more then two successors, for example SwitchInst,
124 // then it is possible that there are multiple edges from current block to
126 if (std::distance(BlockTraits::child_begin(Block
),
127 BlockTraits::child_end(Block
)) <= 2) {
128 ExitBlocks
.push_back(Successor
);
132 // In case of multiple edges from current block to exit block, collect
133 // only one edge in ExitBlocks. Use switchExitBlocks to keep track of
135 if (!is_contained(SwitchExitBlocks
, Successor
)) {
136 SwitchExitBlocks
.push_back(Successor
);
137 ExitBlocks
.push_back(Successor
);
143 template <class BlockT
, class LoopT
>
144 BlockT
*LoopBase
<BlockT
, LoopT
>::getUniqueExitBlock() const {
145 SmallVector
<BlockT
*, 8> UniqueExitBlocks
;
146 getUniqueExitBlocks(UniqueExitBlocks
);
147 if (UniqueExitBlocks
.size() == 1)
148 return UniqueExitBlocks
[0];
152 /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
153 template <class BlockT
, class LoopT
>
154 void LoopBase
<BlockT
, LoopT
>::getExitEdges(
155 SmallVectorImpl
<Edge
> &ExitEdges
) const {
156 assert(!isInvalid() && "Loop not in a valid state!");
157 for (const auto BB
: blocks())
158 for (const auto &Succ
: children
<BlockT
*>(BB
))
160 // Not in current loop? It must be an exit block.
161 ExitEdges
.emplace_back(BB
, Succ
);
164 /// getLoopPreheader - If there is a preheader for this loop, return it. A
165 /// loop has a preheader if there is only one edge to the header of the loop
166 /// from outside of the loop and it is legal to hoist instructions into the
167 /// predecessor. If this is the case, the block branching to the header of the
168 /// loop is the preheader node.
170 /// This method returns null if there is no preheader for the loop.
172 template <class BlockT
, class LoopT
>
173 BlockT
*LoopBase
<BlockT
, LoopT
>::getLoopPreheader() const {
174 assert(!isInvalid() && "Loop not in a valid state!");
175 // Keep track of nodes outside the loop branching to the header...
176 BlockT
*Out
= getLoopPredecessor();
180 // Make sure we are allowed to hoist instructions into the predecessor.
181 if (!Out
->isLegalToHoistInto())
184 // Make sure there is only one exit out of the preheader.
185 typedef GraphTraits
<BlockT
*> BlockTraits
;
186 typename
BlockTraits::ChildIteratorType SI
= BlockTraits::child_begin(Out
);
188 if (SI
!= BlockTraits::child_end(Out
))
189 return nullptr; // Multiple exits from the block, must not be a preheader.
191 // The predecessor has exactly one successor, so it is a preheader.
195 /// getLoopPredecessor - If the given loop's header has exactly one unique
196 /// predecessor outside the loop, return it. Otherwise return null.
197 /// This is less strict that the loop "preheader" concept, which requires
198 /// the predecessor to have exactly one successor.
200 template <class BlockT
, class LoopT
>
201 BlockT
*LoopBase
<BlockT
, LoopT
>::getLoopPredecessor() const {
202 assert(!isInvalid() && "Loop not in a valid state!");
203 // Keep track of nodes outside the loop branching to the header...
204 BlockT
*Out
= nullptr;
206 // Loop over the predecessors of the header node...
207 BlockT
*Header
= getHeader();
208 for (const auto Pred
: children
<Inverse
<BlockT
*>>(Header
)) {
209 if (!contains(Pred
)) { // If the block is not in the loop...
210 if (Out
&& Out
!= Pred
)
211 return nullptr; // Multiple predecessors outside the loop
216 // Make sure there is only one exit out of the preheader.
217 assert(Out
&& "Header of loop has no predecessors from outside loop?");
221 /// getLoopLatch - If there is a single latch block for this loop, return it.
222 /// A latch block is a block that contains a branch back to the header.
223 template <class BlockT
, class LoopT
>
224 BlockT
*LoopBase
<BlockT
, LoopT
>::getLoopLatch() const {
225 assert(!isInvalid() && "Loop not in a valid state!");
226 BlockT
*Header
= getHeader();
227 BlockT
*Latch
= nullptr;
228 for (const auto Pred
: children
<Inverse
<BlockT
*>>(Header
)) {
229 if (contains(Pred
)) {
239 //===----------------------------------------------------------------------===//
240 // APIs for updating loop information after changing the CFG
243 /// addBasicBlockToLoop - This method is used by other analyses to update loop
244 /// information. NewBB is set to be a new member of the current loop.
245 /// Because of this, it is added as a member of all parent loops, and is added
246 /// to the specified LoopInfo object as being in the current basic block. It
247 /// is not valid to replace the loop header with this method.
249 template <class BlockT
, class LoopT
>
250 void LoopBase
<BlockT
, LoopT
>::addBasicBlockToLoop(
251 BlockT
*NewBB
, LoopInfoBase
<BlockT
, LoopT
> &LIB
) {
252 assert(!isInvalid() && "Loop not in a valid state!");
254 if (!Blocks
.empty()) {
255 auto SameHeader
= LIB
[getHeader()];
256 assert(contains(SameHeader
) && getHeader() == SameHeader
->getHeader() &&
257 "Incorrect LI specified for this loop!");
260 assert(NewBB
&& "Cannot add a null basic block to the loop!");
261 assert(!LIB
[NewBB
] && "BasicBlock already in the loop!");
263 LoopT
*L
= static_cast<LoopT
*>(this);
265 // Add the loop mapping to the LoopInfo object...
266 LIB
.BBMap
[NewBB
] = L
;
268 // Add the basic block to this loop and all parent loops...
270 L
->addBlockEntry(NewBB
);
271 L
= L
->getParentLoop();
275 /// replaceChildLoopWith - This is used when splitting loops up. It replaces
276 /// the OldChild entry in our children list with NewChild, and updates the
277 /// parent pointer of OldChild to be null and the NewChild to be this loop.
278 /// This updates the loop depth of the new child.
279 template <class BlockT
, class LoopT
>
280 void LoopBase
<BlockT
, LoopT
>::replaceChildLoopWith(LoopT
*OldChild
,
282 assert(!isInvalid() && "Loop not in a valid state!");
283 assert(OldChild
->ParentLoop
== this && "This loop is already broken!");
284 assert(!NewChild
->ParentLoop
&& "NewChild already has a parent!");
285 typename
std::vector
<LoopT
*>::iterator I
= find(SubLoops
, OldChild
);
286 assert(I
!= SubLoops
.end() && "OldChild not in loop!");
288 OldChild
->ParentLoop
= nullptr;
289 NewChild
->ParentLoop
= static_cast<LoopT
*>(this);
292 /// verifyLoop - Verify loop structure
293 template <class BlockT
, class LoopT
>
294 void LoopBase
<BlockT
, LoopT
>::verifyLoop() const {
295 assert(!isInvalid() && "Loop not in a valid state!");
297 assert(!Blocks
.empty() && "Loop header is missing");
299 // Setup for using a depth-first iterator to visit every block in the loop.
300 SmallVector
<BlockT
*, 8> ExitBBs
;
301 getExitBlocks(ExitBBs
);
302 df_iterator_default_set
<BlockT
*> VisitSet
;
303 VisitSet
.insert(ExitBBs
.begin(), ExitBBs
.end());
304 df_ext_iterator
<BlockT
*, df_iterator_default_set
<BlockT
*>>
305 BI
= df_ext_begin(getHeader(), VisitSet
),
306 BE
= df_ext_end(getHeader(), VisitSet
);
308 // Keep track of the BBs visited.
309 SmallPtrSet
<BlockT
*, 8> VisitedBBs
;
311 // Check the individual blocks.
312 for (; BI
!= BE
; ++BI
) {
315 assert(std::any_of(GraphTraits
<BlockT
*>::child_begin(BB
),
316 GraphTraits
<BlockT
*>::child_end(BB
),
317 [&](BlockT
*B
) { return contains(B
); }) &&
318 "Loop block has no in-loop successors!");
320 assert(std::any_of(GraphTraits
<Inverse
<BlockT
*>>::child_begin(BB
),
321 GraphTraits
<Inverse
<BlockT
*>>::child_end(BB
),
322 [&](BlockT
*B
) { return contains(B
); }) &&
323 "Loop block has no in-loop predecessors!");
325 SmallVector
<BlockT
*, 2> OutsideLoopPreds
;
326 std::for_each(GraphTraits
<Inverse
<BlockT
*>>::child_begin(BB
),
327 GraphTraits
<Inverse
<BlockT
*>>::child_end(BB
),
330 OutsideLoopPreds
.push_back(B
);
333 if (BB
== getHeader()) {
334 assert(!OutsideLoopPreds
.empty() && "Loop is unreachable!");
335 } else if (!OutsideLoopPreds
.empty()) {
336 // A non-header loop shouldn't be reachable from outside the loop,
337 // though it is permitted if the predecessor is not itself actually
339 BlockT
*EntryBB
= &BB
->getParent()->front();
340 for (BlockT
*CB
: depth_first(EntryBB
))
341 for (unsigned i
= 0, e
= OutsideLoopPreds
.size(); i
!= e
; ++i
)
342 assert(CB
!= OutsideLoopPreds
[i
] &&
343 "Loop has multiple entry points!");
345 assert(BB
!= &getHeader()->getParent()->front() &&
346 "Loop contains function entry block!");
348 VisitedBBs
.insert(BB
);
351 if (VisitedBBs
.size() != getNumBlocks()) {
352 dbgs() << "The following blocks are unreachable in the loop: ";
353 for (auto BB
: Blocks
) {
354 if (!VisitedBBs
.count(BB
)) {
355 dbgs() << *BB
<< "\n";
358 assert(false && "Unreachable block in loop");
361 // Check the subloops.
362 for (iterator I
= begin(), E
= end(); I
!= E
; ++I
)
363 // Each block in each subloop should be contained within this loop.
364 for (block_iterator BI
= (*I
)->block_begin(), BE
= (*I
)->block_end();
366 assert(contains(*BI
) &&
367 "Loop does not contain all the blocks of a subloop!");
370 // Check the parent loop pointer.
372 assert(is_contained(*ParentLoop
, this) &&
373 "Loop is not a subloop of its parent!");
378 /// verifyLoop - Verify loop structure of this loop and all nested loops.
379 template <class BlockT
, class LoopT
>
380 void LoopBase
<BlockT
, LoopT
>::verifyLoopNest(
381 DenseSet
<const LoopT
*> *Loops
) const {
382 assert(!isInvalid() && "Loop not in a valid state!");
383 Loops
->insert(static_cast<const LoopT
*>(this));
386 // Verify the subloops.
387 for (iterator I
= begin(), E
= end(); I
!= E
; ++I
)
388 (*I
)->verifyLoopNest(Loops
);
391 template <class BlockT
, class LoopT
>
392 void LoopBase
<BlockT
, LoopT
>::print(raw_ostream
&OS
, unsigned Depth
,
393 bool Verbose
) const {
394 OS
.indent(Depth
* 2);
395 if (static_cast<const LoopT
*>(this)->isAnnotatedParallel())
397 OS
<< "Loop at depth " << getLoopDepth() << " containing: ";
399 BlockT
*H
= getHeader();
400 for (unsigned i
= 0; i
< getBlocks().size(); ++i
) {
401 BlockT
*BB
= getBlocks()[i
];
405 BB
->printAsOperand(OS
, false);
413 if (isLoopExiting(BB
))
420 for (iterator I
= begin(), E
= end(); I
!= E
; ++I
)
421 (*I
)->print(OS
, Depth
+ 2);
424 //===----------------------------------------------------------------------===//
425 /// Stable LoopInfo Analysis - Build a loop tree using stable iterators so the
426 /// result does / not depend on use list (block predecessor) order.
429 /// Discover a subloop with the specified backedges such that: All blocks within
430 /// this loop are mapped to this loop or a subloop. And all subloops within this
431 /// loop have their parent loop set to this loop or a subloop.
432 template <class BlockT
, class LoopT
>
433 static void discoverAndMapSubloop(LoopT
*L
, ArrayRef
<BlockT
*> Backedges
,
434 LoopInfoBase
<BlockT
, LoopT
> *LI
,
435 const DomTreeBase
<BlockT
> &DomTree
) {
436 typedef GraphTraits
<Inverse
<BlockT
*>> InvBlockTraits
;
438 unsigned NumBlocks
= 0;
439 unsigned NumSubloops
= 0;
441 // Perform a backward CFG traversal using a worklist.
442 std::vector
<BlockT
*> ReverseCFGWorklist(Backedges
.begin(), Backedges
.end());
443 while (!ReverseCFGWorklist
.empty()) {
444 BlockT
*PredBB
= ReverseCFGWorklist
.back();
445 ReverseCFGWorklist
.pop_back();
447 LoopT
*Subloop
= LI
->getLoopFor(PredBB
);
449 if (!DomTree
.isReachableFromEntry(PredBB
))
452 // This is an undiscovered block. Map it to the current loop.
453 LI
->changeLoopFor(PredBB
, L
);
455 if (PredBB
== L
->getHeader())
457 // Push all block predecessors on the worklist.
458 ReverseCFGWorklist
.insert(ReverseCFGWorklist
.end(),
459 InvBlockTraits::child_begin(PredBB
),
460 InvBlockTraits::child_end(PredBB
));
462 // This is a discovered block. Find its outermost discovered loop.
463 while (LoopT
*Parent
= Subloop
->getParentLoop())
466 // If it is already discovered to be a subloop of this loop, continue.
470 // Discover a subloop of this loop.
471 Subloop
->setParentLoop(L
);
473 NumBlocks
+= Subloop
->getBlocksVector().capacity();
474 PredBB
= Subloop
->getHeader();
475 // Continue traversal along predecessors that are not loop-back edges from
476 // within this subloop tree itself. Note that a predecessor may directly
477 // reach another subloop that is not yet discovered to be a subloop of
478 // this loop, which we must traverse.
479 for (const auto Pred
: children
<Inverse
<BlockT
*>>(PredBB
)) {
480 if (LI
->getLoopFor(Pred
) != Subloop
)
481 ReverseCFGWorklist
.push_back(Pred
);
485 L
->getSubLoopsVector().reserve(NumSubloops
);
486 L
->reserveBlocks(NumBlocks
);
489 /// Populate all loop data in a stable order during a single forward DFS.
490 template <class BlockT
, class LoopT
> class PopulateLoopsDFS
{
491 typedef GraphTraits
<BlockT
*> BlockTraits
;
492 typedef typename
BlockTraits::ChildIteratorType SuccIterTy
;
494 LoopInfoBase
<BlockT
, LoopT
> *LI
;
497 PopulateLoopsDFS(LoopInfoBase
<BlockT
, LoopT
> *li
) : LI(li
) {}
499 void traverse(BlockT
*EntryBlock
);
502 void insertIntoLoop(BlockT
*Block
);
505 /// Top-level driver for the forward DFS within the loop.
506 template <class BlockT
, class LoopT
>
507 void PopulateLoopsDFS
<BlockT
, LoopT
>::traverse(BlockT
*EntryBlock
) {
508 for (BlockT
*BB
: post_order(EntryBlock
))
512 /// Add a single Block to its ancestor loops in PostOrder. If the block is a
513 /// subloop header, add the subloop to its parent in PostOrder, then reverse the
514 /// Block and Subloop vectors of the now complete subloop to achieve RPO.
515 template <class BlockT
, class LoopT
>
516 void PopulateLoopsDFS
<BlockT
, LoopT
>::insertIntoLoop(BlockT
*Block
) {
517 LoopT
*Subloop
= LI
->getLoopFor(Block
);
518 if (Subloop
&& Block
== Subloop
->getHeader()) {
519 // We reach this point once per subloop after processing all the blocks in
521 if (Subloop
->getParentLoop())
522 Subloop
->getParentLoop()->getSubLoopsVector().push_back(Subloop
);
524 LI
->addTopLevelLoop(Subloop
);
526 // For convenience, Blocks and Subloops are inserted in postorder. Reverse
527 // the lists, except for the loop header, which is always at the beginning.
528 Subloop
->reverseBlock(1);
529 std::reverse(Subloop
->getSubLoopsVector().begin(),
530 Subloop
->getSubLoopsVector().end());
532 Subloop
= Subloop
->getParentLoop();
534 for (; Subloop
; Subloop
= Subloop
->getParentLoop())
535 Subloop
->addBlockEntry(Block
);
538 /// Analyze LoopInfo discovers loops during a postorder DominatorTree traversal
539 /// interleaved with backward CFG traversals within each subloop
540 /// (discoverAndMapSubloop). The backward traversal skips inner subloops, so
541 /// this part of the algorithm is linear in the number of CFG edges. Subloop and
542 /// Block vectors are then populated during a single forward CFG traversal
543 /// (PopulateLoopDFS).
545 /// During the two CFG traversals each block is seen three times:
546 /// 1) Discovered and mapped by a reverse CFG traversal.
547 /// 2) Visited during a forward DFS CFG traversal.
548 /// 3) Reverse-inserted in the loop in postorder following forward DFS.
550 /// The Block vectors are inclusive, so step 3 requires loop-depth number of
551 /// insertions per block.
552 template <class BlockT
, class LoopT
>
553 void LoopInfoBase
<BlockT
, LoopT
>::analyze(const DomTreeBase
<BlockT
> &DomTree
) {
554 // Postorder traversal of the dominator tree.
555 const DomTreeNodeBase
<BlockT
> *DomRoot
= DomTree
.getRootNode();
556 for (auto DomNode
: post_order(DomRoot
)) {
558 BlockT
*Header
= DomNode
->getBlock();
559 SmallVector
<BlockT
*, 4> Backedges
;
561 // Check each predecessor of the potential loop header.
562 for (const auto Backedge
: children
<Inverse
<BlockT
*>>(Header
)) {
563 // If Header dominates predBB, this is a new loop. Collect the backedges.
564 if (DomTree
.dominates(Header
, Backedge
) &&
565 DomTree
.isReachableFromEntry(Backedge
)) {
566 Backedges
.push_back(Backedge
);
569 // Perform a backward CFG traversal to discover and map blocks in this loop.
570 if (!Backedges
.empty()) {
571 LoopT
*L
= AllocateLoop(Header
);
572 discoverAndMapSubloop(L
, ArrayRef
<BlockT
*>(Backedges
), this, DomTree
);
575 // Perform a single forward CFG traversal to populate block and subloop
576 // vectors for all loops.
577 PopulateLoopsDFS
<BlockT
, LoopT
> DFS(this);
578 DFS
.traverse(DomRoot
->getBlock());
581 template <class BlockT
, class LoopT
>
582 SmallVector
<LoopT
*, 4> LoopInfoBase
<BlockT
, LoopT
>::getLoopsInPreorder() {
583 SmallVector
<LoopT
*, 4> PreOrderLoops
, PreOrderWorklist
;
584 // The outer-most loop actually goes into the result in the same relative
585 // order as we walk it. But LoopInfo stores the top level loops in reverse
586 // program order so for here we reverse it to get forward program order.
587 // FIXME: If we change the order of LoopInfo we will want to remove the
589 for (LoopT
*RootL
: reverse(*this)) {
590 assert(PreOrderWorklist
.empty() &&
591 "Must start with an empty preorder walk worklist.");
592 PreOrderWorklist
.push_back(RootL
);
594 LoopT
*L
= PreOrderWorklist
.pop_back_val();
595 // Sub-loops are stored in forward program order, but will process the
596 // worklist backwards so append them in reverse order.
597 PreOrderWorklist
.append(L
->rbegin(), L
->rend());
598 PreOrderLoops
.push_back(L
);
599 } while (!PreOrderWorklist
.empty());
602 return PreOrderLoops
;
605 template <class BlockT
, class LoopT
>
606 SmallVector
<LoopT
*, 4>
607 LoopInfoBase
<BlockT
, LoopT
>::getLoopsInReverseSiblingPreorder() {
608 SmallVector
<LoopT
*, 4> PreOrderLoops
, PreOrderWorklist
;
609 // The outer-most loop actually goes into the result in the same relative
610 // order as we walk it. LoopInfo stores the top level loops in reverse
611 // program order so we walk in order here.
612 // FIXME: If we change the order of LoopInfo we will want to add a reverse
614 for (LoopT
*RootL
: *this) {
615 assert(PreOrderWorklist
.empty() &&
616 "Must start with an empty preorder walk worklist.");
617 PreOrderWorklist
.push_back(RootL
);
619 LoopT
*L
= PreOrderWorklist
.pop_back_val();
620 // Sub-loops are stored in forward program order, but will process the
621 // worklist backwards so we can just append them in order.
622 PreOrderWorklist
.append(L
->begin(), L
->end());
623 PreOrderLoops
.push_back(L
);
624 } while (!PreOrderWorklist
.empty());
627 return PreOrderLoops
;
631 template <class BlockT
, class LoopT
>
632 void LoopInfoBase
<BlockT
, LoopT
>::print(raw_ostream
&OS
) const {
633 for (unsigned i
= 0; i
< TopLevelLoops
.size(); ++i
)
634 TopLevelLoops
[i
]->print(OS
);
636 for (DenseMap
<BasicBlock
*, LoopT
*>::const_iterator I
= BBMap
.begin(),
637 E
= BBMap
.end(); I
!= E
; ++I
)
638 OS
<< "BB '" << I
->first
->getName() << "' level = "
639 << I
->second
->getLoopDepth() << "\n";
643 template <typename T
>
644 bool compareVectors(std::vector
<T
> &BB1
, std::vector
<T
> &BB2
) {
650 template <class BlockT
, class LoopT
>
651 void addInnerLoopsToHeadersMap(DenseMap
<BlockT
*, const LoopT
*> &LoopHeaders
,
652 const LoopInfoBase
<BlockT
, LoopT
> &LI
,
654 LoopHeaders
[L
.getHeader()] = &L
;
656 addInnerLoopsToHeadersMap(LoopHeaders
, LI
, *SL
);
660 template <class BlockT
, class LoopT
>
661 static void compareLoops(const LoopT
*L
, const LoopT
*OtherL
,
662 DenseMap
<BlockT
*, const LoopT
*> &OtherLoopHeaders
) {
663 BlockT
*H
= L
->getHeader();
664 BlockT
*OtherH
= OtherL
->getHeader();
665 assert(H
== OtherH
&&
666 "Mismatched headers even though found in the same map entry!");
668 assert(L
->getLoopDepth() == OtherL
->getLoopDepth() &&
669 "Mismatched loop depth!");
670 const LoopT
*ParentL
= L
, *OtherParentL
= OtherL
;
672 assert(ParentL
->getHeader() == OtherParentL
->getHeader() &&
673 "Mismatched parent loop headers!");
674 ParentL
= ParentL
->getParentLoop();
675 OtherParentL
= OtherParentL
->getParentLoop();
678 for (const LoopT
*SubL
: *L
) {
679 BlockT
*SubH
= SubL
->getHeader();
680 const LoopT
*OtherSubL
= OtherLoopHeaders
.lookup(SubH
);
681 assert(OtherSubL
&& "Inner loop is missing in computed loop info!");
682 OtherLoopHeaders
.erase(SubH
);
683 compareLoops(SubL
, OtherSubL
, OtherLoopHeaders
);
686 std::vector
<BlockT
*> BBs
= L
->getBlocks();
687 std::vector
<BlockT
*> OtherBBs
= OtherL
->getBlocks();
688 assert(compareVectors(BBs
, OtherBBs
) &&
689 "Mismatched basic blocks in the loops!");
691 const SmallPtrSetImpl
<const BlockT
*> &BlocksSet
= L
->getBlocksSet();
692 const SmallPtrSetImpl
<const BlockT
*> &OtherBlocksSet
= L
->getBlocksSet();
693 assert(BlocksSet
.size() == OtherBlocksSet
.size() &&
694 std::all_of(BlocksSet
.begin(), BlocksSet
.end(),
695 [&OtherBlocksSet
](const BlockT
*BB
) {
696 return OtherBlocksSet
.count(BB
);
698 "Mismatched basic blocks in BlocksSets!");
702 template <class BlockT
, class LoopT
>
703 void LoopInfoBase
<BlockT
, LoopT
>::verify(
704 const DomTreeBase
<BlockT
> &DomTree
) const {
705 DenseSet
<const LoopT
*> Loops
;
706 for (iterator I
= begin(), E
= end(); I
!= E
; ++I
) {
707 assert(!(*I
)->getParentLoop() && "Top-level loop has a parent!");
708 (*I
)->verifyLoopNest(&Loops
);
711 // Verify that blocks are mapped to valid loops.
713 for (auto &Entry
: BBMap
) {
714 const BlockT
*BB
= Entry
.first
;
715 LoopT
*L
= Entry
.second
;
716 assert(Loops
.count(L
) && "orphaned loop");
717 assert(L
->contains(BB
) && "orphaned block");
718 for (LoopT
*ChildLoop
: *L
)
719 assert(!ChildLoop
->contains(BB
) &&
720 "BBMap should point to the innermost loop containing BB");
723 // Recompute LoopInfo to verify loops structure.
724 LoopInfoBase
<BlockT
, LoopT
> OtherLI
;
725 OtherLI
.analyze(DomTree
);
727 // Build a map we can use to move from our LI to the computed one. This
728 // allows us to ignore the particular order in any layer of the loop forest
729 // while still comparing the structure.
730 DenseMap
<BlockT
*, const LoopT
*> OtherLoopHeaders
;
731 for (LoopT
*L
: OtherLI
)
732 addInnerLoopsToHeadersMap(OtherLoopHeaders
, OtherLI
, *L
);
734 // Walk the top level loops and ensure there is a corresponding top-level
735 // loop in the computed version and then recursively compare those loop
737 for (LoopT
*L
: *this) {
738 BlockT
*Header
= L
->getHeader();
739 const LoopT
*OtherL
= OtherLoopHeaders
.lookup(Header
);
740 assert(OtherL
&& "Top level loop is missing in computed loop info!");
741 // Now that we've matched this loop, erase its header from the map.
742 OtherLoopHeaders
.erase(Header
);
743 // And recursively compare these loops.
744 compareLoops(L
, OtherL
, OtherLoopHeaders
);
747 // Any remaining entries in the map are loops which were found when computing
748 // a fresh LoopInfo but not present in the current one.
749 if (!OtherLoopHeaders
.empty()) {
750 for (const auto &HeaderAndLoop
: OtherLoopHeaders
)
751 dbgs() << "Found new loop: " << *HeaderAndLoop
.second
<< "\n";
752 llvm_unreachable("Found new loops when recomputing LoopInfo!");
757 } // End llvm namespace