Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:31

0001 //===- GenericLoopInfo - Generic Loop Info for graphs -----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the LoopInfoBase class that is used to identify natural
0010 // loops and determine the loop depth of various nodes in a generic graph of
0011 // blocks.  A natural loop has exactly one entry-point, which is called the
0012 // header. Note that natural loops may actually be several loops that share the
0013 // same header node.
0014 //
0015 // This analysis calculates the nesting structure of loops in a function.  For
0016 // each natural loop identified, this analysis identifies natural loops
0017 // contained entirely within the loop and the basic blocks that make up the
0018 // loop.
0019 //
0020 // It can calculate on the fly various bits of information, for example:
0021 //
0022 //  * whether there is a preheader for the loop
0023 //  * the number of back edges to the header
0024 //  * whether or not a particular block branches out of the loop
0025 //  * the successor blocks of the loop
0026 //  * the loop depth
0027 //  * etc...
0028 //
0029 // Note that this analysis specifically identifies *Loops* not cycles or SCCs
0030 // in the graph.  There can be strongly connected components in the graph which
0031 // this analysis will not recognize and that will not be represented by a Loop
0032 // instance.  In particular, a Loop might be inside such a non-loop SCC, or a
0033 // non-loop SCC might contain a sub-SCC which is a Loop.
0034 //
0035 // For an overview of terminology used in this API (and thus all of our loop
0036 // analyses or transforms), see docs/LoopTerminology.rst.
0037 //
0038 //===----------------------------------------------------------------------===//
0039 
0040 #ifndef LLVM_SUPPORT_GENERICLOOPINFO_H
0041 #define LLVM_SUPPORT_GENERICLOOPINFO_H
0042 
0043 #include "llvm/ADT/DenseSet.h"
0044 #include "llvm/ADT/PostOrderIterator.h"
0045 #include "llvm/ADT/STLExtras.h"
0046 #include "llvm/ADT/SetOperations.h"
0047 #include "llvm/Support/Allocator.h"
0048 #include "llvm/Support/GenericDomTree.h"
0049 
0050 namespace llvm {
0051 
0052 template <class N, class M> class LoopInfoBase;
0053 template <class N, class M> class LoopBase;
0054 
0055 //===----------------------------------------------------------------------===//
0056 /// Instances of this class are used to represent loops that are detected in the
0057 /// flow graph.
0058 ///
0059 template <class BlockT, class LoopT> class LoopBase {
0060   LoopT *ParentLoop;
0061   // Loops contained entirely within this one.
0062   std::vector<LoopT *> SubLoops;
0063 
0064   // The list of blocks in this loop. First entry is the header node.
0065   std::vector<BlockT *> Blocks;
0066 
0067   SmallPtrSet<const BlockT *, 8> DenseBlockSet;
0068 
0069 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
0070   /// Indicator that this loop is no longer a valid loop.
0071   bool IsInvalid = false;
0072 #endif
0073 
0074   LoopBase(const LoopBase<BlockT, LoopT> &) = delete;
0075   const LoopBase<BlockT, LoopT> &
0076   operator=(const LoopBase<BlockT, LoopT> &) = delete;
0077 
0078 public:
0079   /// Return the nesting level of this loop.  An outer-most loop has depth 1,
0080   /// for consistency with loop depth values used for basic blocks, where depth
0081   /// 0 is used for blocks not inside any loops.
0082   unsigned getLoopDepth() const {
0083     assert(!isInvalid() && "Loop not in a valid state!");
0084     unsigned D = 1;
0085     for (const LoopT *CurLoop = ParentLoop; CurLoop;
0086          CurLoop = CurLoop->ParentLoop)
0087       ++D;
0088     return D;
0089   }
0090   BlockT *getHeader() const { return getBlocks().front(); }
0091   /// Return the parent loop if it exists or nullptr for top
0092   /// level loops.
0093 
0094   /// A loop is either top-level in a function (that is, it is not
0095   /// contained in any other loop) or it is entirely enclosed in
0096   /// some other loop.
0097   /// If a loop is top-level, it has no parent, otherwise its
0098   /// parent is the innermost loop in which it is enclosed.
0099   LoopT *getParentLoop() const { return ParentLoop; }
0100 
0101   /// Get the outermost loop in which this loop is contained.
0102   /// This may be the loop itself, if it already is the outermost loop.
0103   const LoopT *getOutermostLoop() const {
0104     const LoopT *L = static_cast<const LoopT *>(this);
0105     while (L->ParentLoop)
0106       L = L->ParentLoop;
0107     return L;
0108   }
0109 
0110   LoopT *getOutermostLoop() {
0111     LoopT *L = static_cast<LoopT *>(this);
0112     while (L->ParentLoop)
0113       L = L->ParentLoop;
0114     return L;
0115   }
0116 
0117   /// This is a raw interface for bypassing addChildLoop.
0118   void setParentLoop(LoopT *L) {
0119     assert(!isInvalid() && "Loop not in a valid state!");
0120     ParentLoop = L;
0121   }
0122 
0123   /// Return true if the specified loop is contained within in this loop.
0124   bool contains(const LoopT *L) const {
0125     assert(!isInvalid() && "Loop not in a valid state!");
0126     if (L == this)
0127       return true;
0128     if (!L)
0129       return false;
0130     return contains(L->getParentLoop());
0131   }
0132 
0133   /// Return true if the specified basic block is in this loop.
0134   bool contains(const BlockT *BB) const {
0135     assert(!isInvalid() && "Loop not in a valid state!");
0136     return DenseBlockSet.count(BB);
0137   }
0138 
0139   /// Return true if the specified instruction is in this loop.
0140   template <class InstT> bool contains(const InstT *Inst) const {
0141     return contains(Inst->getParent());
0142   }
0143 
0144   /// Return the loops contained entirely within this loop.
0145   const std::vector<LoopT *> &getSubLoops() const {
0146     assert(!isInvalid() && "Loop not in a valid state!");
0147     return SubLoops;
0148   }
0149   std::vector<LoopT *> &getSubLoopsVector() {
0150     assert(!isInvalid() && "Loop not in a valid state!");
0151     return SubLoops;
0152   }
0153   typedef typename std::vector<LoopT *>::const_iterator iterator;
0154   typedef
0155       typename std::vector<LoopT *>::const_reverse_iterator reverse_iterator;
0156   iterator begin() const { return getSubLoops().begin(); }
0157   iterator end() const { return getSubLoops().end(); }
0158   reverse_iterator rbegin() const { return getSubLoops().rbegin(); }
0159   reverse_iterator rend() const { return getSubLoops().rend(); }
0160 
0161   // LoopInfo does not detect irreducible control flow, just natural
0162   // loops. That is, it is possible that there is cyclic control
0163   // flow within the "innermost loop" or around the "outermost
0164   // loop".
0165 
0166   /// Return true if the loop does not contain any (natural) loops.
0167   bool isInnermost() const { return getSubLoops().empty(); }
0168   /// Return true if the loop does not have a parent (natural) loop
0169   // (i.e. it is outermost, which is the same as top-level).
0170   bool isOutermost() const { return getParentLoop() == nullptr; }
0171 
0172   /// Get a list of the basic blocks which make up this loop.
0173   ArrayRef<BlockT *> getBlocks() const {
0174     assert(!isInvalid() && "Loop not in a valid state!");
0175     return Blocks;
0176   }
0177   typedef typename ArrayRef<BlockT *>::const_iterator block_iterator;
0178   block_iterator block_begin() const { return getBlocks().begin(); }
0179   block_iterator block_end() const { return getBlocks().end(); }
0180   inline iterator_range<block_iterator> blocks() const {
0181     assert(!isInvalid() && "Loop not in a valid state!");
0182     return make_range(block_begin(), block_end());
0183   }
0184 
0185   /// Get the number of blocks in this loop in constant time.
0186   /// Invalidate the loop, indicating that it is no longer a loop.
0187   unsigned getNumBlocks() const {
0188     assert(!isInvalid() && "Loop not in a valid state!");
0189     return Blocks.size();
0190   }
0191 
0192   /// Return a direct, mutable handle to the blocks vector so that we can
0193   /// mutate it efficiently with techniques like `std::remove`.
0194   std::vector<BlockT *> &getBlocksVector() {
0195     assert(!isInvalid() && "Loop not in a valid state!");
0196     return Blocks;
0197   }
0198   /// Return a direct, mutable handle to the blocks set so that we can
0199   /// mutate it efficiently.
0200   SmallPtrSetImpl<const BlockT *> &getBlocksSet() {
0201     assert(!isInvalid() && "Loop not in a valid state!");
0202     return DenseBlockSet;
0203   }
0204 
0205   /// Return a direct, immutable handle to the blocks set.
0206   const SmallPtrSetImpl<const BlockT *> &getBlocksSet() const {
0207     assert(!isInvalid() && "Loop not in a valid state!");
0208     return DenseBlockSet;
0209   }
0210 
0211   /// Return true if this loop is no longer valid.  The only valid use of this
0212   /// helper is "assert(L.isInvalid())" or equivalent, since IsInvalid is set to
0213   /// true by the destructor.  In other words, if this accessor returns true,
0214   /// the caller has already triggered UB by calling this accessor; and so it
0215   /// can only be called in a context where a return value of true indicates a
0216   /// programmer error.
0217   bool isInvalid() const {
0218 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
0219     return IsInvalid;
0220 #else
0221     return false;
0222 #endif
0223   }
0224 
0225   /// True if terminator in the block can branch to another block that is
0226   /// outside of the current loop. \p BB must be inside the loop.
0227   bool isLoopExiting(const BlockT *BB) const {
0228     assert(!isInvalid() && "Loop not in a valid state!");
0229     assert(contains(BB) && "Exiting block must be part of the loop");
0230     for (const auto *Succ : children<const BlockT *>(BB)) {
0231       if (!contains(Succ))
0232         return true;
0233     }
0234     return false;
0235   }
0236 
0237   /// Returns true if \p BB is a loop-latch.
0238   /// A latch block is a block that contains a branch back to the header.
0239   /// This function is useful when there are multiple latches in a loop
0240   /// because \fn getLoopLatch will return nullptr in that case.
0241   bool isLoopLatch(const BlockT *BB) const {
0242     assert(!isInvalid() && "Loop not in a valid state!");
0243     assert(contains(BB) && "block does not belong to the loop");
0244     return llvm::is_contained(inverse_children<BlockT *>(getHeader()), BB);
0245   }
0246 
0247   /// Calculate the number of back edges to the loop header.
0248   unsigned getNumBackEdges() const {
0249     assert(!isInvalid() && "Loop not in a valid state!");
0250     return llvm::count_if(inverse_children<BlockT *>(getHeader()),
0251                           [&](BlockT *Pred) { return contains(Pred); });
0252   }
0253 
0254   //===--------------------------------------------------------------------===//
0255   // APIs for simple analysis of the loop.
0256   //
0257   // Note that all of these methods can fail on general loops (ie, there may not
0258   // be a preheader, etc).  For best success, the loop simplification and
0259   // induction variable canonicalization pass should be used to normalize loops
0260   // for easy analysis.  These methods assume canonical loops.
0261 
0262   /// Return all blocks inside the loop that have successors outside of the
0263   /// loop. These are the blocks _inside of the current loop_ which branch out.
0264   /// The returned list is always unique.
0265   void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const;
0266 
0267   /// If getExitingBlocks would return exactly one block, return that block.
0268   /// Otherwise return null.
0269   BlockT *getExitingBlock() const;
0270 
0271   /// Return all of the successor blocks of this loop. These are the blocks
0272   /// _outside of the current loop_ which are branched to.
0273   void getExitBlocks(SmallVectorImpl<BlockT *> &ExitBlocks) const;
0274 
0275   /// If getExitBlocks would return exactly one block, return that block.
0276   /// Otherwise return null.
0277   BlockT *getExitBlock() const;
0278 
0279   /// Return true if no exit block for the loop has a predecessor that is
0280   /// outside the loop.
0281   bool hasDedicatedExits() const;
0282 
0283   /// Return all unique successor blocks of this loop.
0284   /// These are the blocks _outside of the current loop_ which are branched to.
0285   void getUniqueExitBlocks(SmallVectorImpl<BlockT *> &ExitBlocks) const;
0286 
0287   /// Return all unique successor blocks of this loop except successors from
0288   /// Latch block are not considered. If the exit comes from Latch has also
0289   /// non Latch predecessor in a loop it will be added to ExitBlocks.
0290   /// These are the blocks _outside of the current loop_ which are branched to.
0291   void getUniqueNonLatchExitBlocks(SmallVectorImpl<BlockT *> &ExitBlocks) const;
0292 
0293   /// If getUniqueExitBlocks would return exactly one block, return that block.
0294   /// Otherwise return null.
0295   BlockT *getUniqueExitBlock() const;
0296 
0297   /// Return the unique exit block for the latch, or null if there are multiple
0298   /// different exit blocks or the latch is not exiting.
0299   BlockT *getUniqueLatchExitBlock() const;
0300 
0301   /// Return true if this loop does not have any exit blocks.
0302   bool hasNoExitBlocks() const;
0303 
0304   /// Edge type.
0305   typedef std::pair<BlockT *, BlockT *> Edge;
0306 
0307   /// Return all pairs of (_inside_block_,_outside_block_).
0308   void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const;
0309 
0310   /// If there is a preheader for this loop, return it. A loop has a preheader
0311   /// if there is only one edge to the header of the loop from outside of the
0312   /// loop. If this is the case, the block branching to the header of the loop
0313   /// is the preheader node.
0314   ///
0315   /// This method returns null if there is no preheader for the loop.
0316   BlockT *getLoopPreheader() const;
0317 
0318   /// If the given loop's header has exactly one unique predecessor outside the
0319   /// loop, return it. Otherwise return null.
0320   ///  This is less strict that the loop "preheader" concept, which requires
0321   /// the predecessor to have exactly one successor.
0322   BlockT *getLoopPredecessor() const;
0323 
0324   /// If there is a single latch block for this loop, return it.
0325   /// A latch block is a block that contains a branch back to the header.
0326   BlockT *getLoopLatch() const;
0327 
0328   /// Return all loop latch blocks of this loop. A latch block is a block that
0329   /// contains a branch back to the header.
0330   void getLoopLatches(SmallVectorImpl<BlockT *> &LoopLatches) const {
0331     assert(!isInvalid() && "Loop not in a valid state!");
0332     BlockT *H = getHeader();
0333     for (const auto Pred : inverse_children<BlockT *>(H))
0334       if (contains(Pred))
0335         LoopLatches.push_back(Pred);
0336   }
0337 
0338   /// Return all inner loops in the loop nest rooted by the loop in preorder,
0339   /// with siblings in forward program order.
0340   template <class Type>
0341   static void getInnerLoopsInPreorder(const LoopT &L,
0342                                       SmallVectorImpl<Type> &PreOrderLoops) {
0343     SmallVector<LoopT *, 4> PreOrderWorklist;
0344     PreOrderWorklist.append(L.rbegin(), L.rend());
0345 
0346     while (!PreOrderWorklist.empty()) {
0347       LoopT *L = PreOrderWorklist.pop_back_val();
0348       // Sub-loops are stored in forward program order, but will process the
0349       // worklist backwards so append them in reverse order.
0350       PreOrderWorklist.append(L->rbegin(), L->rend());
0351       PreOrderLoops.push_back(L);
0352     }
0353   }
0354 
0355   /// Return all loops in the loop nest rooted by the loop in preorder, with
0356   /// siblings in forward program order.
0357   SmallVector<const LoopT *, 4> getLoopsInPreorder() const {
0358     SmallVector<const LoopT *, 4> PreOrderLoops;
0359     const LoopT *CurLoop = static_cast<const LoopT *>(this);
0360     PreOrderLoops.push_back(CurLoop);
0361     getInnerLoopsInPreorder(*CurLoop, PreOrderLoops);
0362     return PreOrderLoops;
0363   }
0364   SmallVector<LoopT *, 4> getLoopsInPreorder() {
0365     SmallVector<LoopT *, 4> PreOrderLoops;
0366     LoopT *CurLoop = static_cast<LoopT *>(this);
0367     PreOrderLoops.push_back(CurLoop);
0368     getInnerLoopsInPreorder(*CurLoop, PreOrderLoops);
0369     return PreOrderLoops;
0370   }
0371 
0372   //===--------------------------------------------------------------------===//
0373   // APIs for updating loop information after changing the CFG
0374   //
0375 
0376   /// This method is used by other analyses to update loop information.
0377   /// NewBB is set to be a new member of the current loop.
0378   /// Because of this, it is added as a member of all parent loops, and is added
0379   /// to the specified LoopInfo object as being in the current basic block.  It
0380   /// is not valid to replace the loop header with this method.
0381   void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
0382 
0383   /// This is used when splitting loops up. It replaces the OldChild entry in
0384   /// our children list with NewChild, and updates the parent pointer of
0385   /// OldChild to be null and the NewChild to be this loop.
0386   /// This updates the loop depth of the new child.
0387   void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild);
0388 
0389   /// Add the specified loop to be a child of this loop.
0390   /// This updates the loop depth of the new child.
0391   void addChildLoop(LoopT *NewChild) {
0392     assert(!isInvalid() && "Loop not in a valid state!");
0393     assert(!NewChild->ParentLoop && "NewChild already has a parent!");
0394     NewChild->ParentLoop = static_cast<LoopT *>(this);
0395     SubLoops.push_back(NewChild);
0396   }
0397 
0398   /// This removes the specified child from being a subloop of this loop. The
0399   /// loop is not deleted, as it will presumably be inserted into another loop.
0400   LoopT *removeChildLoop(iterator I) {
0401     assert(!isInvalid() && "Loop not in a valid state!");
0402     assert(I != SubLoops.end() && "Cannot remove end iterator!");
0403     LoopT *Child = *I;
0404     assert(Child->ParentLoop == this && "Child is not a child of this loop!");
0405     SubLoops.erase(SubLoops.begin() + (I - begin()));
0406     Child->ParentLoop = nullptr;
0407     return Child;
0408   }
0409 
0410   /// This removes the specified child from being a subloop of this loop. The
0411   /// loop is not deleted, as it will presumably be inserted into another loop.
0412   LoopT *removeChildLoop(LoopT *Child) {
0413     return removeChildLoop(llvm::find(*this, Child));
0414   }
0415 
0416   /// This adds a basic block directly to the basic block list.
0417   /// This should only be used by transformations that create new loops.  Other
0418   /// transformations should use addBasicBlockToLoop.
0419   void addBlockEntry(BlockT *BB) {
0420     assert(!isInvalid() && "Loop not in a valid state!");
0421     Blocks.push_back(BB);
0422     DenseBlockSet.insert(BB);
0423   }
0424 
0425   /// interface to reverse Blocks[from, end of loop] in this loop
0426   void reverseBlock(unsigned from) {
0427     assert(!isInvalid() && "Loop not in a valid state!");
0428     std::reverse(Blocks.begin() + from, Blocks.end());
0429   }
0430 
0431   /// interface to do reserve() for Blocks
0432   void reserveBlocks(unsigned size) {
0433     assert(!isInvalid() && "Loop not in a valid state!");
0434     Blocks.reserve(size);
0435   }
0436 
0437   /// This method is used to move BB (which must be part of this loop) to be the
0438   /// loop header of the loop (the block that dominates all others).
0439   void moveToHeader(BlockT *BB) {
0440     assert(!isInvalid() && "Loop not in a valid state!");
0441     if (Blocks[0] == BB)
0442       return;
0443     for (unsigned i = 0;; ++i) {
0444       assert(i != Blocks.size() && "Loop does not contain BB!");
0445       if (Blocks[i] == BB) {
0446         Blocks[i] = Blocks[0];
0447         Blocks[0] = BB;
0448         return;
0449       }
0450     }
0451   }
0452 
0453   /// This removes the specified basic block from the current loop, updating the
0454   /// Blocks as appropriate. This does not update the mapping in the LoopInfo
0455   /// class.
0456   void removeBlockFromLoop(BlockT *BB) {
0457     assert(!isInvalid() && "Loop not in a valid state!");
0458     auto I = find(Blocks, BB);
0459     assert(I != Blocks.end() && "N is not in this list!");
0460     Blocks.erase(I);
0461 
0462     DenseBlockSet.erase(BB);
0463   }
0464 
0465   /// Verify loop structure
0466   void verifyLoop() const;
0467 
0468   /// Verify loop structure of this loop and all nested loops.
0469   void verifyLoopNest(DenseSet<const LoopT *> *Loops) const;
0470 
0471   /// Returns true if the loop is annotated parallel.
0472   ///
0473   /// Derived classes can override this method using static template
0474   /// polymorphism.
0475   bool isAnnotatedParallel() const { return false; }
0476 
0477   /// Print loop with all the BBs inside it.
0478   void print(raw_ostream &OS, bool Verbose = false, bool PrintNested = true,
0479              unsigned Depth = 0) const;
0480 
0481 protected:
0482   friend class LoopInfoBase<BlockT, LoopT>;
0483 
0484   /// This creates an empty loop.
0485   LoopBase() : ParentLoop(nullptr) {}
0486 
0487   explicit LoopBase(BlockT *BB) : ParentLoop(nullptr) {
0488     Blocks.push_back(BB);
0489     DenseBlockSet.insert(BB);
0490   }
0491 
0492   // Since loop passes like SCEV are allowed to key analysis results off of
0493   // `Loop` pointers, we cannot re-use pointers within a loop pass manager.
0494   // This means loop passes should not be `delete` ing `Loop` objects directly
0495   // (and risk a later `Loop` allocation re-using the address of a previous one)
0496   // but should be using LoopInfo::markAsRemoved, which keeps around the `Loop`
0497   // pointer till the end of the lifetime of the `LoopInfo` object.
0498   //
0499   // To make it easier to follow this rule, we mark the destructor as
0500   // non-public.
0501   ~LoopBase() {
0502     for (auto *SubLoop : SubLoops)
0503       SubLoop->~LoopT();
0504 
0505 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
0506     IsInvalid = true;
0507 #endif
0508     SubLoops.clear();
0509     Blocks.clear();
0510     DenseBlockSet.clear();
0511     ParentLoop = nullptr;
0512   }
0513 };
0514 
0515 template <class BlockT, class LoopT>
0516 raw_ostream &operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
0517   Loop.print(OS);
0518   return OS;
0519 }
0520 
0521 //===----------------------------------------------------------------------===//
0522 /// This class builds and contains all of the top-level loop
0523 /// structures in the specified function.
0524 ///
0525 
0526 template <class BlockT, class LoopT> class LoopInfoBase {
0527   // BBMap - Mapping of basic blocks to the inner most loop they occur in
0528   DenseMap<const BlockT *, LoopT *> BBMap;
0529   std::vector<LoopT *> TopLevelLoops;
0530   BumpPtrAllocator LoopAllocator;
0531 
0532   friend class LoopBase<BlockT, LoopT>;
0533   friend class LoopInfo;
0534 
0535   void operator=(const LoopInfoBase &) = delete;
0536   LoopInfoBase(const LoopInfoBase &) = delete;
0537 
0538 public:
0539   LoopInfoBase() = default;
0540   ~LoopInfoBase() { releaseMemory(); }
0541 
0542   LoopInfoBase(LoopInfoBase &&Arg)
0543       : BBMap(std::move(Arg.BBMap)),
0544         TopLevelLoops(std::move(Arg.TopLevelLoops)),
0545         LoopAllocator(std::move(Arg.LoopAllocator)) {
0546     // We have to clear the arguments top level loops as we've taken ownership.
0547     Arg.TopLevelLoops.clear();
0548   }
0549   LoopInfoBase &operator=(LoopInfoBase &&RHS) {
0550     BBMap = std::move(RHS.BBMap);
0551 
0552     for (auto *L : TopLevelLoops)
0553       L->~LoopT();
0554 
0555     TopLevelLoops = std::move(RHS.TopLevelLoops);
0556     LoopAllocator = std::move(RHS.LoopAllocator);
0557     RHS.TopLevelLoops.clear();
0558     return *this;
0559   }
0560 
0561   void releaseMemory() {
0562     BBMap.clear();
0563 
0564     for (auto *L : TopLevelLoops)
0565       L->~LoopT();
0566     TopLevelLoops.clear();
0567     LoopAllocator.Reset();
0568   }
0569 
0570   template <typename... ArgsTy> LoopT *AllocateLoop(ArgsTy &&...Args) {
0571     LoopT *Storage = LoopAllocator.Allocate<LoopT>();
0572     return new (Storage) LoopT(std::forward<ArgsTy>(Args)...);
0573   }
0574 
0575   /// iterator/begin/end - The interface to the top-level loops in the current
0576   /// function.
0577   ///
0578   typedef typename std::vector<LoopT *>::const_iterator iterator;
0579   typedef
0580       typename std::vector<LoopT *>::const_reverse_iterator reverse_iterator;
0581   iterator begin() const { return TopLevelLoops.begin(); }
0582   iterator end() const { return TopLevelLoops.end(); }
0583   reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
0584   reverse_iterator rend() const { return TopLevelLoops.rend(); }
0585   bool empty() const { return TopLevelLoops.empty(); }
0586 
0587   /// Return all of the loops in the function in preorder across the loop
0588   /// nests, with siblings in forward program order.
0589   ///
0590   /// Note that because loops form a forest of trees, preorder is equivalent to
0591   /// reverse postorder.
0592   SmallVector<LoopT *, 4> getLoopsInPreorder() const;
0593 
0594   /// Return all of the loops in the function in preorder across the loop
0595   /// nests, with siblings in *reverse* program order.
0596   ///
0597   /// Note that because loops form a forest of trees, preorder is equivalent to
0598   /// reverse postorder.
0599   ///
0600   /// Also note that this is *not* a reverse preorder. Only the siblings are in
0601   /// reverse program order.
0602   SmallVector<LoopT *, 4> getLoopsInReverseSiblingPreorder() const;
0603 
0604   /// Return the inner most loop that BB lives in. If a basic block is in no
0605   /// loop (for example the entry node), null is returned.
0606   LoopT *getLoopFor(const BlockT *BB) const { return BBMap.lookup(BB); }
0607 
0608   /// Same as getLoopFor.
0609   const LoopT *operator[](const BlockT *BB) const { return getLoopFor(BB); }
0610 
0611   /// Return the loop nesting level of the specified block. A depth of 0 means
0612   /// the block is not inside any loop.
0613   unsigned getLoopDepth(const BlockT *BB) const {
0614     const LoopT *L = getLoopFor(BB);
0615     return L ? L->getLoopDepth() : 0;
0616   }
0617 
0618   // True if the block is a loop header node
0619   bool isLoopHeader(const BlockT *BB) const {
0620     const LoopT *L = getLoopFor(BB);
0621     return L && L->getHeader() == BB;
0622   }
0623 
0624   /// Return the top-level loops.
0625   const std::vector<LoopT *> &getTopLevelLoops() const { return TopLevelLoops; }
0626 
0627   /// Return the top-level loops.
0628   std::vector<LoopT *> &getTopLevelLoopsVector() { return TopLevelLoops; }
0629 
0630   /// This removes the specified top-level loop from this loop info object.
0631   /// The loop is not deleted, as it will presumably be inserted into
0632   /// another loop.
0633   LoopT *removeLoop(iterator I) {
0634     assert(I != end() && "Cannot remove end iterator!");
0635     LoopT *L = *I;
0636     assert(L->isOutermost() && "Not a top-level loop!");
0637     TopLevelLoops.erase(TopLevelLoops.begin() + (I - begin()));
0638     return L;
0639   }
0640 
0641   /// Change the top-level loop that contains BB to the specified loop.
0642   /// This should be used by transformations that restructure the loop hierarchy
0643   /// tree.
0644   void changeLoopFor(BlockT *BB, LoopT *L) {
0645     if (!L) {
0646       BBMap.erase(BB);
0647       return;
0648     }
0649     BBMap[BB] = L;
0650   }
0651 
0652   /// Replace the specified loop in the top-level loops list with the indicated
0653   /// loop.
0654   void changeTopLevelLoop(LoopT *OldLoop, LoopT *NewLoop) {
0655     auto I = find(TopLevelLoops, OldLoop);
0656     assert(I != TopLevelLoops.end() && "Old loop not at top level!");
0657     *I = NewLoop;
0658     assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
0659            "Loops already embedded into a subloop!");
0660   }
0661 
0662   /// This adds the specified loop to the collection of top-level loops.
0663   void addTopLevelLoop(LoopT *New) {
0664     assert(New->isOutermost() && "Loop already in subloop!");
0665     TopLevelLoops.push_back(New);
0666   }
0667 
0668   /// This method completely removes BB from all data structures,
0669   /// including all of the Loop objects it is nested in and our mapping from
0670   /// BasicBlocks to loops.
0671   void removeBlock(BlockT *BB) {
0672     auto I = BBMap.find(BB);
0673     if (I != BBMap.end()) {
0674       for (LoopT *L = I->second; L; L = L->getParentLoop())
0675         L->removeBlockFromLoop(BB);
0676 
0677       BBMap.erase(I);
0678     }
0679   }
0680 
0681   // Internals
0682 
0683   static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
0684                                       const LoopT *ParentLoop) {
0685     if (!SubLoop)
0686       return true;
0687     if (SubLoop == ParentLoop)
0688       return false;
0689     return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
0690   }
0691 
0692   /// Create the loop forest using a stable algorithm.
0693   void analyze(const DominatorTreeBase<BlockT, false> &DomTree);
0694 
0695   // Debugging
0696   void print(raw_ostream &OS) const;
0697 
0698   void verify(const DominatorTreeBase<BlockT, false> &DomTree) const;
0699 
0700   /// Destroy a loop that has been removed from the `LoopInfo` nest.
0701   ///
0702   /// This runs the destructor of the loop object making it invalid to
0703   /// reference afterward. The memory is retained so that the *pointer* to the
0704   /// loop remains valid.
0705   ///
0706   /// The caller is responsible for removing this loop from the loop nest and
0707   /// otherwise disconnecting it from the broader `LoopInfo` data structures.
0708   /// Callers that don't naturally handle this themselves should probably call
0709   /// `erase' instead.
0710   void destroy(LoopT *L) {
0711     L->~LoopT();
0712 
0713     // Since LoopAllocator is a BumpPtrAllocator, this Deallocate only poisons
0714     // \c L, but the pointer remains valid for non-dereferencing uses.
0715     LoopAllocator.Deallocate(L);
0716   }
0717 };
0718 
0719 } // namespace llvm
0720 
0721 #endif // LLVM_SUPPORT_GENERICLOOPINFO_H