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