LCOV - code coverage report
Current view: top level - include/llvm/Analysis - LoopInfo.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 126 199 63.3 %
Date: 2018-06-17 00:07:59 Functions: 36 130 27.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- llvm/Analysis/LoopInfo.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 file defines the LoopInfo class that is used to identify natural loops
      11             : // and determine the loop depth of various nodes of the CFG.  A natural loop
      12             : // has exactly one entry-point, which is called the header. Note that natural
      13             : // loops may actually be several loops that share the same header node.
      14             : //
      15             : // This analysis calculates the nesting structure of loops in a function.  For
      16             : // each natural loop identified, this analysis identifies natural loops
      17             : // contained entirely within the loop and the basic blocks the make up the loop.
      18             : //
      19             : // It can calculate on the fly various bits of information, for example:
      20             : //
      21             : //  * whether there is a preheader for the loop
      22             : //  * the number of back edges to the header
      23             : //  * whether or not a particular block branches out of the loop
      24             : //  * the successor blocks of the loop
      25             : //  * the loop depth
      26             : //  * etc...
      27             : //
      28             : // Note that this analysis specifically identifies *Loops* not cycles or SCCs
      29             : // in the CFG.  There can be strongly connected components in the CFG which
      30             : // this analysis will not recognize and that will not be represented by a Loop
      31             : // instance.  In particular, a Loop might be inside such a non-loop SCC, or a
      32             : // non-loop SCC might contain a sub-SCC which is a Loop.
      33             : //
      34             : //===----------------------------------------------------------------------===//
      35             : 
      36             : #ifndef LLVM_ANALYSIS_LOOPINFO_H
      37             : #define LLVM_ANALYSIS_LOOPINFO_H
      38             : 
      39             : #include "llvm/ADT/DenseMap.h"
      40             : #include "llvm/ADT/DenseSet.h"
      41             : #include "llvm/ADT/GraphTraits.h"
      42             : #include "llvm/ADT/SmallPtrSet.h"
      43             : #include "llvm/ADT/SmallVector.h"
      44             : #include "llvm/IR/CFG.h"
      45             : #include "llvm/IR/Instruction.h"
      46             : #include "llvm/IR/Instructions.h"
      47             : #include "llvm/IR/PassManager.h"
      48             : #include "llvm/Pass.h"
      49             : #include "llvm/Support/Allocator.h"
      50             : #include <algorithm>
      51             : #include <utility>
      52             : 
      53             : namespace llvm {
      54             : 
      55             : class DominatorTree;
      56             : class LoopInfo;
      57             : class Loop;
      58             : class MDNode;
      59             : class PHINode;
      60             : class raw_ostream;
      61             : template <class N, bool IsPostDom> class DominatorTreeBase;
      62             : template <class N, class M> class LoopInfoBase;
      63             : template <class N, class M> class LoopBase;
      64             : 
      65             : //===----------------------------------------------------------------------===//
      66             : /// Instances of this class are used to represent loops that are detected in the
      67             : /// flow graph.
      68             : ///
      69             : template <class BlockT, class LoopT> class LoopBase {
      70             :   LoopT *ParentLoop;
      71             :   // Loops contained entirely within this one.
      72             :   std::vector<LoopT *> SubLoops;
      73             : 
      74             :   // The list of blocks in this loop. First entry is the header node.
      75             :   std::vector<BlockT *> Blocks;
      76             : 
      77             :   SmallPtrSet<const BlockT *, 8> DenseBlockSet;
      78             : 
      79             : #if LLVM_ENABLE_ABI_BREAKING_CHECKS
      80             :   /// Indicator that this loop is no longer a valid loop.
      81             :   bool IsInvalid = false;
      82             : #endif
      83             : 
      84             :   LoopBase(const LoopBase<BlockT, LoopT> &) = delete;
      85             :   const LoopBase<BlockT, LoopT> &
      86             :   operator=(const LoopBase<BlockT, LoopT> &) = delete;
      87             : 
      88             : public:
      89             :   /// Return the nesting level of this loop.  An outer-most loop has depth 1,
      90             :   /// for consistency with loop depth values used for basic blocks, where depth
      91             :   /// 0 is used for blocks not inside any loops.
      92           0 :   unsigned getLoopDepth() const {
      93             :     assert(!isInvalid() && "Loop not in a valid state!");
      94             :     unsigned D = 1;
      95     1999265 :     for (const LoopT *CurLoop = ParentLoop; CurLoop;
      96             :          CurLoop = CurLoop->ParentLoop)
      97       19316 :       ++D;
      98           0 :     return D;
      99             :   }
     100     5100055 :   BlockT *getHeader() const { return getBlocks().front(); }
     101      941010 :   LoopT *getParentLoop() const { return ParentLoop; }
     102             : 
     103             :   /// This is a raw interface for bypassing addChildLoop.
     104           0 :   void setParentLoop(LoopT *L) {
     105             :     assert(!isInvalid() && "Loop not in a valid state!");
     106       17912 :     ParentLoop = L;
     107           0 :   }
     108             : 
     109             :   /// Return true if the specified loop is contained within in this loop.
     110           0 :   bool contains(const LoopT *L) const {
     111             :     assert(!isInvalid() && "Loop not in a valid state!");
     112     1237968 :     if (L == this)
     113             :       return true;
     114      673861 :     if (!L)
     115             :       return false;
     116      391192 :     return contains(L->getParentLoop());
     117             :   }
     118             : 
     119             :   /// Return true if the specified basic block is in this loop.
     120           0 :   bool contains(const BlockT *BB) const {
     121             :     assert(!isInvalid() && "Loop not in a valid state!");
     122    12882180 :     return DenseBlockSet.count(BB);
     123             :   }
     124             : 
     125             :   /// Return true if the specified instruction is in this loop.
     126             :   template <class InstT> bool contains(const InstT *Inst) const {
     127     1806225 :     return contains(Inst->getParent());
     128             :   }
     129             : 
     130             :   /// Return the loops contained entirely within this loop.
     131           0 :   const std::vector<LoopT *> &getSubLoops() const {
     132             :     assert(!isInvalid() && "Loop not in a valid state!");
     133          41 :     return SubLoops;
     134             :   }
     135           0 :   std::vector<LoopT *> &getSubLoopsVector() {
     136             :     assert(!isInvalid() && "Loop not in a valid state!");
     137      137399 :     return SubLoops;
     138             :   }
     139             :   typedef typename std::vector<LoopT *>::const_iterator iterator;
     140             :   typedef
     141             :       typename std::vector<LoopT *>::const_reverse_iterator reverse_iterator;
     142      162614 :   iterator begin() const { return getSubLoops().begin(); }
     143      163706 :   iterator end() const { return getSubLoops().end(); }
     144           0 :   reverse_iterator rbegin() const { return getSubLoops().rbegin(); }
     145           0 :   reverse_iterator rend() const { return getSubLoops().rend(); }
     146           0 :   bool empty() const { return getSubLoops().empty(); }
     147             : 
     148             :   /// Get a list of the basic blocks which make up this loop.
     149           0 :   ArrayRef<BlockT *> getBlocks() const {
     150             :     assert(!isInvalid() && "Loop not in a valid state!");
     151           0 :     return Blocks;
     152             :   }
     153             :   typedef typename ArrayRef<BlockT *>::const_iterator block_iterator;
     154           0 :   block_iterator block_begin() const { return getBlocks().begin(); }
     155           0 :   block_iterator block_end() const { return getBlocks().end(); }
     156           0 :   inline iterator_range<block_iterator> blocks() const {
     157             :     assert(!isInvalid() && "Loop not in a valid state!");
     158           0 :     return make_range(block_begin(), block_end());
     159             :   }
     160             : 
     161             :   /// Get the number of blocks in this loop in constant time.
     162             :   /// Invalidate the loop, indicating that it is no longer a loop.
     163           0 :   unsigned getNumBlocks() const {
     164             :     assert(!isInvalid() && "Loop not in a valid state!");
     165       49576 :     return Blocks.size();
     166             :   }
     167             : 
     168             :   /// Return a direct, mutable handle to the blocks vector so that we can
     169             :   /// mutate it efficiently with techniques like `std::remove`.
     170           0 :   std::vector<BlockT *> &getBlocksVector() {
     171             :     assert(!isInvalid() && "Loop not in a valid state!");
     172         210 :     return Blocks;
     173             :   }
     174             :   /// Return a direct, mutable handle to the blocks set so that we can
     175             :   /// mutate it efficiently.
     176           0 :   SmallPtrSetImpl<const BlockT *> &getBlocksSet() {
     177             :     assert(!isInvalid() && "Loop not in a valid state!");
     178           0 :     return DenseBlockSet;
     179             :   }
     180             : 
     181             :   /// Return a direct, immutable handle to the blocks set.
     182           0 :   const SmallPtrSetImpl<const BlockT *> &getBlocksSet() const {
     183             :     assert(!isInvalid() && "Loop not in a valid state!");
     184           0 :     return DenseBlockSet;
     185             :   }
     186             : 
     187             :   /// Return true if this loop is no longer valid.  The only valid use of this
     188             :   /// helper is "assert(L.isInvalid())" or equivalent, since IsInvalid is set to
     189             :   /// true by the destructor.  In other words, if this accessor returns true,
     190             :   /// the caller has already triggered UB by calling this accessor; and so it
     191             :   /// can only be called in a context where a return value of true indicates a
     192             :   /// programmer error.
     193           0 :   bool isInvalid() const {
     194             : #if LLVM_ENABLE_ABI_BREAKING_CHECKS
     195             :     return IsInvalid;
     196             : #else
     197           0 :     return false;
     198             : #endif
     199             :   }
     200             : 
     201             :   /// True if terminator in the block can branch to another block that is
     202             :   /// outside of the current loop.
     203        7839 :   bool isLoopExiting(const BlockT *BB) const {
     204             :     assert(!isInvalid() && "Loop not in a valid state!");
     205      472990 :     for (const auto &Succ : children<const BlockT *>(BB)) {
     206      742714 :       if (!contains(Succ))
     207             :         return true;
     208             :     }
     209        1497 :     return false;
     210             :   }
     211             : 
     212             :   /// Returns true if \p BB is a loop-latch.
     213             :   /// A latch block is a block that contains a branch back to the header.
     214             :   /// This function is useful when there are multiple latches in a loop
     215             :   /// because \fn getLoopLatch will return nullptr in that case.
     216        1419 :   bool isLoopLatch(const BlockT *BB) const {
     217             :     assert(!isInvalid() && "Loop not in a valid state!");
     218             :     assert(contains(BB) && "block does not belong to the loop");
     219             : 
     220             :     BlockT *Header = getHeader();
     221           0 :     auto PredBegin = GraphTraits<Inverse<BlockT *>>::child_begin(Header);
     222             :     auto PredEnd = GraphTraits<Inverse<BlockT *>>::child_end(Header);
     223        1419 :     return std::find(PredBegin, PredEnd, BB) != PredEnd;
     224             :   }
     225             : 
     226             :   /// Calculate the number of back edges to the loop header.
     227          30 :   unsigned getNumBackEdges() const {
     228             :     assert(!isInvalid() && "Loop not in a valid state!");
     229             :     unsigned NumBackEdges = 0;
     230             :     BlockT *H = getHeader();
     231             : 
     232         120 :     for (const auto Pred : children<Inverse<BlockT *>>(H))
     233          60 :       if (contains(Pred))
     234          30 :         ++NumBackEdges;
     235             : 
     236          30 :     return NumBackEdges;
     237             :   }
     238             : 
     239             :   //===--------------------------------------------------------------------===//
     240             :   // APIs for simple analysis of the loop.
     241             :   //
     242             :   // Note that all of these methods can fail on general loops (ie, there may not
     243             :   // be a preheader, etc).  For best success, the loop simplification and
     244             :   // induction variable canonicalization pass should be used to normalize loops
     245             :   // for easy analysis.  These methods assume canonical loops.
     246             : 
     247             :   /// Return all blocks inside the loop that have successors outside of the
     248             :   /// loop. These are the blocks _inside of the current loop_ which branch out.
     249             :   /// The returned list is always unique.
     250             :   void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const;
     251             : 
     252             :   /// If getExitingBlocks would return exactly one block, return that block.
     253             :   /// Otherwise return null.
     254             :   BlockT *getExitingBlock() const;
     255             : 
     256             :   /// Return all of the successor blocks of this loop. These are the blocks
     257             :   /// _outside of the current loop_ which are branched to.
     258             :   void getExitBlocks(SmallVectorImpl<BlockT *> &ExitBlocks) const;
     259             : 
     260             :   /// If getExitBlocks would return exactly one block, return that block.
     261             :   /// Otherwise return null.
     262             :   BlockT *getExitBlock() const;
     263             : 
     264             :   /// Edge type.
     265             :   typedef std::pair<const BlockT *, const BlockT *> Edge;
     266             : 
     267             :   /// Return all pairs of (_inside_block_,_outside_block_).
     268             :   void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const;
     269             : 
     270             :   /// If there is a preheader for this loop, return it. A loop has a preheader
     271             :   /// if there is only one edge to the header of the loop from outside of the
     272             :   /// loop. If this is the case, the block branching to the header of the loop
     273             :   /// is the preheader node.
     274             :   ///
     275             :   /// This method returns null if there is no preheader for the loop.
     276             :   BlockT *getLoopPreheader() const;
     277             : 
     278             :   /// If the given loop's header has exactly one unique predecessor outside the
     279             :   /// loop, return it. Otherwise return null.
     280             :   ///  This is less strict that the loop "preheader" concept, which requires
     281             :   /// the predecessor to have exactly one successor.
     282             :   BlockT *getLoopPredecessor() const;
     283             : 
     284             :   /// If there is a single latch block for this loop, return it.
     285             :   /// A latch block is a block that contains a branch back to the header.
     286             :   BlockT *getLoopLatch() const;
     287             : 
     288             :   /// Return all loop latch blocks of this loop. A latch block is a block that
     289             :   /// contains a branch back to the header.
     290        2526 :   void getLoopLatches(SmallVectorImpl<BlockT *> &LoopLatches) const {
     291             :     assert(!isInvalid() && "Loop not in a valid state!");
     292             :     BlockT *H = getHeader();
     293       15160 :     for (const auto Pred : children<Inverse<BlockT *>>(H))
     294        5054 :       if (contains(Pred))
     295        2528 :         LoopLatches.push_back(Pred);
     296        2526 :   }
     297             : 
     298             :   //===--------------------------------------------------------------------===//
     299             :   // APIs for updating loop information after changing the CFG
     300             :   //
     301             : 
     302             :   /// This method is used by other analyses to update loop information.
     303             :   /// NewBB is set to be a new member of the current loop.
     304             :   /// Because of this, it is added as a member of all parent loops, and is added
     305             :   /// to the specified LoopInfo object as being in the current basic block.  It
     306             :   /// is not valid to replace the loop header with this method.
     307             :   void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
     308             : 
     309             :   /// This is used when splitting loops up. It replaces the OldChild entry in
     310             :   /// our children list with NewChild, and updates the parent pointer of
     311             :   /// OldChild to be null and the NewChild to be this loop.
     312             :   /// This updates the loop depth of the new child.
     313             :   void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild);
     314             : 
     315             :   /// Add the specified loop to be a child of this loop.
     316             :   /// This updates the loop depth of the new child.
     317           0 :   void addChildLoop(LoopT *NewChild) {
     318             :     assert(!isInvalid() && "Loop not in a valid state!");
     319             :     assert(!NewChild->ParentLoop && "NewChild already has a parent!");
     320         352 :     NewChild->ParentLoop = static_cast<LoopT *>(this);
     321         352 :     SubLoops.push_back(NewChild);
     322           0 :   }
     323             : 
     324             :   /// This removes the specified child from being a subloop of this loop. The
     325             :   /// loop is not deleted, as it will presumably be inserted into another loop.
     326           0 :   LoopT *removeChildLoop(iterator I) {
     327             :     assert(!isInvalid() && "Loop not in a valid state!");
     328             :     assert(I != SubLoops.end() && "Cannot remove end iterator!");
     329          78 :     LoopT *Child = *I;
     330             :     assert(Child->ParentLoop == this && "Child is not a child of this loop!");
     331         168 :     SubLoops.erase(SubLoops.begin() + (I - begin()));
     332         158 :     Child->ParentLoop = nullptr;
     333           0 :     return Child;
     334             :   }
     335             : 
     336             :   /// This removes the specified child from being a subloop of this loop. The
     337             :   /// loop is not deleted, as it will presumably be inserted into another loop.
     338           6 :   LoopT *removeChildLoop(LoopT *Child) {
     339           6 :     return removeChildLoop(llvm::find(*this, Child));
     340             :   }
     341             : 
     342             :   /// This adds a basic block directly to the basic block list.
     343             :   /// This should only be used by transformations that create new loops.  Other
     344             :   /// transformations should use addBasicBlockToLoop.
     345      552820 :   void addBlockEntry(BlockT *BB) {
     346             :     assert(!isInvalid() && "Loop not in a valid state!");
     347      552820 :     Blocks.push_back(BB);
     348      552820 :     DenseBlockSet.insert(BB);
     349      552820 :   }
     350             : 
     351             :   /// interface to reverse Blocks[from, end of loop] in this loop
     352           0 :   void reverseBlock(unsigned from) {
     353             :     assert(!isInvalid() && "Loop not in a valid state!");
     354             :     std::reverse(Blocks.begin() + from, Blocks.end());
     355           0 :   }
     356             : 
     357             :   /// interface to do reserve() for Blocks
     358           0 :   void reserveBlocks(unsigned size) {
     359             :     assert(!isInvalid() && "Loop not in a valid state!");
     360      119257 :     Blocks.reserve(size);
     361           0 :   }
     362             : 
     363             :   /// This method is used to move BB (which must be part of this loop) to be the
     364             :   /// loop header of the loop (the block that dominates all others).
     365           0 :   void moveToHeader(BlockT *BB) {
     366             :     assert(!isInvalid() && "Loop not in a valid state!");
     367        2084 :     if (Blocks[0] == BB)
     368             :       return;
     369        2498 :     for (unsigned i = 0;; ++i) {
     370           0 :       assert(i != Blocks.size() && "Loop does not contain BB!");
     371        9164 :       if (Blocks[i] == BB) {
     372        2084 :         Blocks[i] = Blocks[0];
     373        2084 :         Blocks[0] = BB;
     374           0 :         return;
     375             :       }
     376             :     }
     377             :   }
     378             : 
     379             :   /// This removes the specified basic block from the current loop, updating the
     380             :   /// Blocks as appropriate. This does not update the mapping in the LoopInfo
     381             :   /// class.
     382         175 :   void removeBlockFromLoop(BlockT *BB) {
     383             :     assert(!isInvalid() && "Loop not in a valid state!");
     384             :     auto I = find(Blocks, BB);
     385             :     assert(I != Blocks.end() && "N is not in this list!");
     386         175 :     Blocks.erase(I);
     387             : 
     388         175 :     DenseBlockSet.erase(BB);
     389         175 :   }
     390             : 
     391             :   /// Verify loop structure
     392             :   void verifyLoop() const;
     393             : 
     394             :   /// Verify loop structure of this loop and all nested loops.
     395             :   void verifyLoopNest(DenseSet<const LoopT *> *Loops) const;
     396             : 
     397             :   /// Print loop with all the BBs inside it.
     398             :   void print(raw_ostream &OS, unsigned Depth = 0, bool Verbose = false) const;
     399             : 
     400             : protected:
     401             :   friend class LoopInfoBase<BlockT, LoopT>;
     402             : 
     403             :   /// This creates an empty loop.
     404        1905 :   LoopBase() : ParentLoop(nullptr) {}
     405             : 
     406      238378 :   explicit LoopBase(BlockT *BB) : ParentLoop(nullptr) {
     407      119189 :     Blocks.push_back(BB);
     408      119189 :     DenseBlockSet.insert(BB);
     409      119189 :   }
     410             : 
     411             :   // Since loop passes like SCEV are allowed to key analysis results off of
     412             :   // `Loop` pointers, we cannot re-use pointers within a loop pass manager.
     413             :   // This means loop passes should not be `delete` ing `Loop` objects directly
     414             :   // (and risk a later `Loop` allocation re-using the address of a previous one)
     415             :   // but should be using LoopInfo::markAsRemoved, which keeps around the `Loop`
     416             :   // pointer till the end of the lifetime of the `LoopInfo` object.
     417             :   //
     418             :   // To make it easier to follow this rule, we mark the destructor as
     419             :   // non-public.
     420      119967 :   ~LoopBase() {
     421      137841 :     for (auto *SubLoop : SubLoops)
     422       17874 :       SubLoop->~LoopT();
     423             : 
     424             : #if LLVM_ENABLE_ABI_BREAKING_CHECKS
     425             :     IsInvalid = true;
     426             : #endif
     427             :     SubLoops.clear();
     428             :     Blocks.clear();
     429      119967 :     DenseBlockSet.clear();
     430      119967 :     ParentLoop = nullptr;
     431      119967 :   }
     432             : };
     433             : 
     434             : template <class BlockT, class LoopT>
     435             : raw_ostream &operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
     436         431 :   Loop.print(OS);
     437             :   return OS;
     438             : }
     439             : 
     440             : // Implementation in LoopInfoImpl.h
     441             : extern template class LoopBase<BasicBlock, Loop>;
     442             : 
     443             : /// Represents a single loop in the control flow graph.  Note that not all SCCs
     444             : /// in the CFG are necessarily loops.
     445             : class Loop : public LoopBase<BasicBlock, Loop> {
     446             : public:
     447             :   /// A range representing the start and end location of a loop.
     448        9804 :   class LocRange {
     449             :     DebugLoc Start;
     450             :     DebugLoc End;
     451             : 
     452             :   public:
     453             :     LocRange() {}
     454        2460 :     LocRange(DebugLoc Start) : Start(std::move(Start)), End(std::move(Start)) {}
     455        3672 :     LocRange(DebugLoc Start, DebugLoc End)
     456        7344 :         : Start(std::move(Start)), End(std::move(End)) {}
     457             : 
     458             :     const DebugLoc &getStart() const { return Start; }
     459             :     const DebugLoc &getEnd() const { return End; }
     460             : 
     461             :     /// Check for null.
     462             :     ///
     463             :     explicit operator bool() const { return Start && End; }
     464             :   };
     465             : 
     466             :   /// Return true if the specified value is loop invariant.
     467             :   bool isLoopInvariant(const Value *V) const;
     468             : 
     469             :   /// Return true if all the operands of the specified instruction are loop
     470             :   /// invariant.
     471             :   bool hasLoopInvariantOperands(const Instruction *I) const;
     472             : 
     473             :   /// If the given value is an instruction inside of the loop and it can be
     474             :   /// hoisted, do so to make it trivially loop-invariant.
     475             :   /// Return true if the value after any hoisting is loop invariant. This
     476             :   /// function can be used as a slightly more aggressive replacement for
     477             :   /// isLoopInvariant.
     478             :   ///
     479             :   /// If InsertPt is specified, it is the point to hoist instructions to.
     480             :   /// If null, the terminator of the loop preheader is used.
     481             :   bool makeLoopInvariant(Value *V, bool &Changed,
     482             :                          Instruction *InsertPt = nullptr) const;
     483             : 
     484             :   /// If the given instruction is inside of the loop and it can be hoisted, do
     485             :   /// so to make it trivially loop-invariant.
     486             :   /// Return true if the instruction after any hoisting is loop invariant. This
     487             :   /// function can be used as a slightly more aggressive replacement for
     488             :   /// isLoopInvariant.
     489             :   ///
     490             :   /// If InsertPt is specified, it is the point to hoist instructions to.
     491             :   /// If null, the terminator of the loop preheader is used.
     492             :   ///
     493             :   bool makeLoopInvariant(Instruction *I, bool &Changed,
     494             :                          Instruction *InsertPt = nullptr) const;
     495             : 
     496             :   /// Check to see if the loop has a canonical induction variable: an integer
     497             :   /// recurrence that starts at 0 and increments by one each time through the
     498             :   /// loop. If so, return the phi node that corresponds to it.
     499             :   ///
     500             :   /// The IndVarSimplify pass transforms loops to have a canonical induction
     501             :   /// variable.
     502             :   ///
     503             :   PHINode *getCanonicalInductionVariable() const;
     504             : 
     505             :   /// Return true if the Loop is in LCSSA form.
     506             :   bool isLCSSAForm(DominatorTree &DT) const;
     507             : 
     508             :   /// Return true if this Loop and all inner subloops are in LCSSA form.
     509             :   bool isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const;
     510             : 
     511             :   /// Return true if the Loop is in the form that the LoopSimplify form
     512             :   /// transforms loops to, which is sometimes called normal form.
     513             :   bool isLoopSimplifyForm() const;
     514             : 
     515             :   /// Return true if the loop body is safe to clone in practice.
     516             :   bool isSafeToClone() const;
     517             : 
     518             :   /// Returns true if the loop is annotated parallel.
     519             :   ///
     520             :   /// A parallel loop can be assumed to not contain any dependencies between
     521             :   /// iterations by the compiler. That is, any loop-carried dependency checking
     522             :   /// can be skipped completely when parallelizing the loop on the target
     523             :   /// machine. Thus, if the parallel loop information originates from the
     524             :   /// programmer, e.g. via the OpenMP parallel for pragma, it is the
     525             :   /// programmer's responsibility to ensure there are no loop-carried
     526             :   /// dependencies. The final execution order of the instructions across
     527             :   /// iterations is not guaranteed, thus, the end result might or might not
     528             :   /// implement actual concurrent execution of instructions across multiple
     529             :   /// iterations.
     530             :   bool isAnnotatedParallel() const;
     531             : 
     532             :   /// Return the llvm.loop loop id metadata node for this loop if it is present.
     533             :   ///
     534             :   /// If this loop contains the same llvm.loop metadata on each branch to the
     535             :   /// header then the node is returned. If any latch instruction does not
     536             :   /// contain llvm.loop or if multiple latches contain different nodes then
     537             :   /// 0 is returned.
     538             :   MDNode *getLoopID() const;
     539             :   /// Set the llvm.loop loop id metadata for this loop.
     540             :   ///
     541             :   /// The LoopID metadata node will be added to each terminator instruction in
     542             :   /// the loop that branches to the loop header.
     543             :   ///
     544             :   /// The LoopID metadata node should have one or more operands and the first
     545             :   /// operand should be the node itself.
     546             :   void setLoopID(MDNode *LoopID) const;
     547             : 
     548             :   /// Add llvm.loop.unroll.disable to this loop's loop id metadata.
     549             :   ///
     550             :   /// Remove existing unroll metadata and add unroll disable metadata to
     551             :   /// indicate the loop has already been unrolled.  This prevents a loop
     552             :   /// from being unrolled more than is directed by a pragma if the loop
     553             :   /// unrolling pass is run more than once (which it generally is).
     554             :   void setLoopAlreadyUnrolled();
     555             : 
     556             :   /// Return true if no exit block for the loop has a predecessor that is
     557             :   /// outside the loop.
     558             :   bool hasDedicatedExits() const;
     559             : 
     560             :   /// Return all unique successor blocks of this loop.
     561             :   /// These are the blocks _outside of the current loop_ which are branched to.
     562             :   /// This assumes that loop exits are in canonical form, i.e. all exits are
     563             :   /// dedicated exits.
     564             :   void getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const;
     565             : 
     566             :   /// If getUniqueExitBlocks would return exactly one block, return that block.
     567             :   /// Otherwise return null.
     568             :   BasicBlock *getUniqueExitBlock() const;
     569             : 
     570             :   void dump() const;
     571             :   void dumpVerbose() const;
     572             : 
     573             :   /// Return the debug location of the start of this loop.
     574             :   /// This looks for a BB terminating instruction with a known debug
     575             :   /// location by looking at the preheader and header blocks. If it
     576             :   /// cannot find a terminating instruction with location information,
     577             :   /// it returns an unknown location.
     578             :   DebugLoc getStartLoc() const;
     579             : 
     580             :   /// Return the source code span of the loop.
     581             :   LocRange getLocRange() const;
     582             : 
     583       19732 :   StringRef getName() const {
     584       19732 :     if (BasicBlock *Header = getHeader())
     585       19732 :       if (Header->hasName())
     586        6298 :         return Header->getName();
     587       13434 :     return "<unnamed loop>";
     588             :   }
     589             : 
     590             : private:
     591             :   Loop() = default;
     592             : 
     593             :   friend class LoopInfoBase<BasicBlock, Loop>;
     594             :   friend class LoopBase<BasicBlock, Loop>;
     595       75844 :   explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
     596       76646 :   ~Loop() = default;
     597             : };
     598             : 
     599             : //===----------------------------------------------------------------------===//
     600             : /// This class builds and contains all of the top-level loop
     601             : /// structures in the specified function.
     602             : ///
     603             : 
     604             : template <class BlockT, class LoopT> class LoopInfoBase {
     605             :   // BBMap - Mapping of basic blocks to the inner most loop they occur in
     606             :   DenseMap<const BlockT *, LoopT *> BBMap;
     607             :   std::vector<LoopT *> TopLevelLoops;
     608             :   BumpPtrAllocator LoopAllocator;
     609             : 
     610             :   friend class LoopBase<BlockT, LoopT>;
     611             :   friend class LoopInfo;
     612             : 
     613             :   void operator=(const LoopInfoBase &) = delete;
     614             :   LoopInfoBase(const LoopInfoBase &) = delete;
     615             : 
     616             : public:
     617      407028 :   LoopInfoBase() {}
     618      747344 :   ~LoopInfoBase() { releaseMemory(); }
     619             : 
     620           0 :   LoopInfoBase(LoopInfoBase &&Arg)
     621             :       : BBMap(std::move(Arg.BBMap)),
     622             :         TopLevelLoops(std::move(Arg.TopLevelLoops)),
     623           0 :         LoopAllocator(std::move(Arg.LoopAllocator)) {
     624             :     // We have to clear the arguments top level loops as we've taken ownership.
     625             :     Arg.TopLevelLoops.clear();
     626           0 :   }
     627           0 :   LoopInfoBase &operator=(LoopInfoBase &&RHS) {
     628           0 :     BBMap = std::move(RHS.BBMap);
     629             : 
     630           0 :     for (auto *L : TopLevelLoops)
     631             :       L->~LoopT();
     632             : 
     633           0 :     TopLevelLoops = std::move(RHS.TopLevelLoops);
     634           0 :     LoopAllocator = std::move(RHS.LoopAllocator);
     635             :     RHS.TopLevelLoops.clear();
     636           0 :     return *this;
     637             :   }
     638             : 
     639     4417631 :   void releaseMemory() {
     640     4417631 :     BBMap.clear();
     641             : 
     642     4519059 :     for (auto *L : TopLevelLoops)
     643             :       L->~LoopT();
     644             :     TopLevelLoops.clear();
     645     4417631 :     LoopAllocator.Reset();
     646     4417631 :   }
     647             : 
     648      121094 :   template <typename... ArgsTy> LoopT *AllocateLoop(ArgsTy &&... Args) {
     649      121094 :     LoopT *Storage = LoopAllocator.Allocate<LoopT>();
     650      240283 :     return new (Storage) LoopT(std::forward<ArgsTy>(Args)...);
     651             :   }
     652             : 
     653             :   /// iterator/begin/end - The interface to the top-level loops in the current
     654             :   /// function.
     655             :   ///
     656             :   typedef typename std::vector<LoopT *>::const_iterator iterator;
     657             :   typedef
     658             :       typename std::vector<LoopT *>::const_reverse_iterator reverse_iterator;
     659     1489126 :   iterator begin() const { return TopLevelLoops.begin(); }
     660     1488729 :   iterator end() const { return TopLevelLoops.end(); }
     661           0 :   reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
     662           0 :   reverse_iterator rend() const { return TopLevelLoops.rend(); }
     663           0 :   bool empty() const { return TopLevelLoops.empty(); }
     664             : 
     665             :   /// Return all of the loops in the function in preorder across the loop
     666             :   /// nests, with siblings in forward program order.
     667             :   ///
     668             :   /// Note that because loops form a forest of trees, preorder is equivalent to
     669             :   /// reverse postorder.
     670             :   SmallVector<LoopT *, 4> getLoopsInPreorder();
     671             : 
     672             :   /// Return all of the loops in the function in preorder across the loop
     673             :   /// nests, with siblings in *reverse* program order.
     674             :   ///
     675             :   /// Note that because loops form a forest of trees, preorder is equivalent to
     676             :   /// reverse postorder.
     677             :   ///
     678             :   /// Also note that this is *not* a reverse preorder. Only the siblings are in
     679             :   /// reverse program order.
     680             :   SmallVector<LoopT *, 4> getLoopsInReverseSiblingPreorder();
     681             : 
     682             :   /// Return the inner most loop that BB lives in. If a basic block is in no
     683             :   /// loop (for example the entry node), null is returned.
     684    14144012 :   LoopT *getLoopFor(const BlockT *BB) const { return BBMap.lookup(BB); }
     685             : 
     686             :   /// Same as getLoopFor.
     687           0 :   const LoopT *operator[](const BlockT *BB) const { return getLoopFor(BB); }
     688             : 
     689             :   /// Return the loop nesting level of the specified block. A depth of 0 means
     690             :   /// the block is not inside any loop.
     691     1872924 :   unsigned getLoopDepth(const BlockT *BB) const {
     692             :     const LoopT *L = getLoopFor(BB);
     693     3745155 :     return L ? L->getLoopDepth() : 0;
     694             :   }
     695             : 
     696             :   // True if the block is a loop header node
     697        7879 :   bool isLoopHeader(const BlockT *BB) const {
     698             :     const LoopT *L = getLoopFor(BB);
     699        2416 :     return L && L->getHeader() == BB;
     700             :   }
     701             : 
     702             :   /// This removes the specified top-level loop from this loop info object.
     703             :   /// The loop is not deleted, as it will presumably be inserted into
     704             :   /// another loop.
     705           0 :   LoopT *removeLoop(iterator I) {
     706             :     assert(I != end() && "Cannot remove end iterator!");
     707           0 :     LoopT *L = *I;
     708             :     assert(!L->getParentLoop() && "Not a top-level loop!");
     709         402 :     TopLevelLoops.erase(TopLevelLoops.begin() + (I - begin()));
     710           0 :     return L;
     711             :   }
     712             : 
     713             :   /// Change the top-level loop that contains BB to the specified loop.
     714             :   /// This should be used by transformations that restructure the loop hierarchy
     715             :   /// tree.
     716      541407 :   void changeLoopFor(BlockT *BB, LoopT *L) {
     717      541407 :     if (!L) {
     718        1749 :       BBMap.erase(BB);
     719          24 :       return;
     720             :     }
     721     1082766 :     BBMap[BB] = L;
     722             :   }
     723             : 
     724             :   /// Replace the specified loop in the top-level loops list with the indicated
     725             :   /// loop.
     726           0 :   void changeTopLevelLoop(LoopT *OldLoop, LoopT *NewLoop) {
     727             :     auto I = find(TopLevelLoops, OldLoop);
     728             :     assert(I != TopLevelLoops.end() && "Old loop not at top level!");
     729          33 :     *I = NewLoop;
     730             :     assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
     731             :            "Loops already embedded into a subloop!");
     732           0 :   }
     733             : 
     734             :   /// This adds the specified loop to the collection of top-level loops.
     735           0 :   void addTopLevelLoop(LoopT *New) {
     736             :     assert(!New->getParentLoop() && "Loop already in subloop!");
     737      102916 :     TopLevelLoops.push_back(New);
     738           0 :   }
     739             : 
     740             :   /// This method completely removes BB from all data structures,
     741             :   /// including all of the Loop objects it is nested in and our mapping from
     742             :   /// BasicBlocks to loops.
     743          91 :   void removeBlock(BlockT *BB) {
     744          91 :     auto I = BBMap.find(BB);
     745          91 :     if (I != BBMap.end()) {
     746         227 :       for (LoopT *L = I->second; L; L = L->getParentLoop())
     747         136 :         L->removeBlockFromLoop(BB);
     748             : 
     749             :       BBMap.erase(I);
     750             :     }
     751          91 :   }
     752             : 
     753             :   // Internals
     754             : 
     755           0 :   static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
     756             :                                       const LoopT *ParentLoop) {
     757           0 :     if (!SubLoop)
     758             :       return true;
     759           0 :     if (SubLoop == ParentLoop)
     760             :       return false;
     761           0 :     return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
     762             :   }
     763             : 
     764             :   /// Create the loop forest using a stable algorithm.
     765             :   void analyze(const DominatorTreeBase<BlockT, false> &DomTree);
     766             : 
     767             :   // Debugging
     768             :   void print(raw_ostream &OS) const;
     769             : 
     770             :   void verify(const DominatorTreeBase<BlockT, false> &DomTree) const;
     771             : 
     772             :   /// Destroy a loop that has been removed from the `LoopInfo` nest.
     773             :   ///
     774             :   /// This runs the destructor of the loop object making it invalid to
     775             :   /// reference afterward. The memory is retained so that the *pointer* to the
     776             :   /// loop remains valid.
     777             :   ///
     778             :   /// The caller is responsible for removing this loop from the loop nest and
     779             :   /// otherwise disconnecting it from the broader `LoopInfo` data structures.
     780             :   /// Callers that don't naturally handle this themselves should probably call
     781             :   /// `erase' instead.
     782           0 :   void destroy(LoopT *L) {
     783             :     L->~LoopT();
     784             : 
     785             :     // Since LoopAllocator is a BumpPtrAllocator, this Deallocate only poisons
     786             :     // \c L, but the pointer remains valid for non-dereferencing uses.
     787             :     LoopAllocator.Deallocate(L);
     788           0 :   }
     789             : };
     790             : 
     791             : // Implementation in LoopInfoImpl.h
     792             : extern template class LoopInfoBase<BasicBlock, Loop>;
     793             : 
     794      112688 : class LoopInfo : public LoopInfoBase<BasicBlock, Loop> {
     795             :   typedef LoopInfoBase<BasicBlock, Loop> BaseT;
     796             : 
     797             :   friend class LoopBase<BasicBlock, Loop>;
     798             : 
     799             :   void operator=(const LoopInfo &) = delete;
     800             :   LoopInfo(const LoopInfo &) = delete;
     801             : 
     802             : public:
     803      109521 :   LoopInfo() {}
     804             :   explicit LoopInfo(const DominatorTreeBase<BasicBlock, false> &DomTree);
     805             : 
     806        3262 :   LoopInfo(LoopInfo &&Arg) : BaseT(std::move(static_cast<BaseT &>(Arg))) {}
     807             :   LoopInfo &operator=(LoopInfo &&RHS) {
     808             :     BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
     809             :     return *this;
     810             :   }
     811             : 
     812             :   /// Handle invalidation explicitly.
     813             :   bool invalidate(Function &F, const PreservedAnalyses &PA,
     814             :                   FunctionAnalysisManager::Invalidator &);
     815             : 
     816             :   // Most of the public interface is provided via LoopInfoBase.
     817             : 
     818             :   /// Update LoopInfo after removing the last backedge from a loop. This updates
     819             :   /// the loop forest and parent loops for each block so that \c L is no longer
     820             :   /// referenced, but does not actually delete \c L immediately. The pointer
     821             :   /// will remain valid until this LoopInfo's memory is released.
     822             :   void erase(Loop *L);
     823             : 
     824             :   /// Returns true if replacing From with To everywhere is guaranteed to
     825             :   /// preserve LCSSA form.
     826       31271 :   bool replacementPreservesLCSSAForm(Instruction *From, Value *To) {
     827             :     // Preserving LCSSA form is only problematic if the replacing value is an
     828             :     // instruction.
     829             :     Instruction *I = dyn_cast<Instruction>(To);
     830             :     if (!I)
     831             :       return true;
     832             :     // If both instructions are defined in the same basic block then replacement
     833             :     // cannot break LCSSA form.
     834        1261 :     if (I->getParent() == From->getParent())
     835             :       return true;
     836             :     // If the instruction is not defined in a loop then it can safely replace
     837             :     // anything.
     838             :     Loop *ToLoop = getLoopFor(I->getParent());
     839         853 :     if (!ToLoop)
     840             :       return true;
     841             :     // If the replacing instruction is defined in the same loop as the original
     842             :     // instruction, or in a loop that contains it as an inner loop, then using
     843             :     // it as a replacement will not break LCSSA form.
     844         853 :     return ToLoop->contains(getLoopFor(From->getParent()));
     845             :   }
     846             : 
     847             :   /// Checks if moving a specific instruction can break LCSSA in any loop.
     848             :   ///
     849             :   /// Return true if moving \p Inst to before \p NewLoc will break LCSSA,
     850             :   /// assuming that the function containing \p Inst and \p NewLoc is currently
     851             :   /// in LCSSA form.
     852         269 :   bool movementPreservesLCSSAForm(Instruction *Inst, Instruction *NewLoc) {
     853             :     assert(Inst->getFunction() == NewLoc->getFunction() &&
     854             :            "Can't reason about IPO!");
     855             : 
     856         269 :     auto *OldBB = Inst->getParent();
     857         269 :     auto *NewBB = NewLoc->getParent();
     858             : 
     859             :     // Movement within the same loop does not break LCSSA (the equality check is
     860             :     // to avoid doing a hashtable lookup in case of intra-block movement).
     861         269 :     if (OldBB == NewBB)
     862             :       return true;
     863             : 
     864             :     auto *OldLoop = getLoopFor(OldBB);
     865             :     auto *NewLoop = getLoopFor(NewBB);
     866             : 
     867          26 :     if (OldLoop == NewLoop)
     868             :       return true;
     869             : 
     870             :     // Check if Outer contains Inner; with the null loop counting as the
     871             :     // "outermost" loop.
     872             :     auto Contains = [](const Loop *Outer, const Loop *Inner) {
     873           0 :       return !Outer || Outer->contains(Inner);
     874             :     };
     875             : 
     876             :     // To check that the movement of Inst to before NewLoc does not break LCSSA,
     877             :     // we need to check two sets of uses for possible LCSSA violations at
     878             :     // NewLoc: the users of NewInst, and the operands of NewInst.
     879             : 
     880             :     // If we know we're hoisting Inst out of an inner loop to an outer loop,
     881             :     // then the uses *of* Inst don't need to be checked.
     882             : 
     883             :     if (!Contains(NewLoop, OldLoop)) {
     884           0 :       for (Use &U : Inst->uses()) {
     885           0 :         auto *UI = cast<Instruction>(U.getUser());
     886           0 :         auto *UBB = isa<PHINode>(UI) ? cast<PHINode>(UI)->getIncomingBlock(U)
     887             :                                      : UI->getParent();
     888           0 :         if (UBB != NewBB && getLoopFor(UBB) != NewLoop)
     889             :           return false;
     890             :       }
     891             :     }
     892             : 
     893             :     // If we know we're sinking Inst from an outer loop into an inner loop, then
     894             :     // the *operands* of Inst don't need to be checked.
     895             : 
     896             :     if (!Contains(OldLoop, NewLoop)) {
     897             :       // See below on why we can't handle phi nodes here.
     898           0 :       if (isa<PHINode>(Inst))
     899             :         return false;
     900             : 
     901           0 :       for (Use &U : Inst->operands()) {
     902           0 :         auto *DefI = dyn_cast<Instruction>(U.get());
     903             :         if (!DefI)
     904             :           return false;
     905             : 
     906             :         // This would need adjustment if we allow Inst to be a phi node -- the
     907             :         // new use block won't simply be NewBB.
     908             : 
     909           0 :         auto *DefBlock = DefI->getParent();
     910           0 :         if (DefBlock != NewBB && getLoopFor(DefBlock) != NewLoop)
     911             :           return false;
     912             :       }
     913             :     }
     914             : 
     915             :     return true;
     916             :   }
     917             : };
     918             : 
     919             : // Allow clients to walk the list of nested loops...
     920             : template <> struct GraphTraits<const Loop *> {
     921             :   typedef const Loop *NodeRef;
     922             :   typedef LoopInfo::iterator ChildIteratorType;
     923             : 
     924             :   static NodeRef getEntryNode(const Loop *L) { return L; }
     925             :   static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
     926             :   static ChildIteratorType child_end(NodeRef N) { return N->end(); }
     927             : };
     928             : 
     929             : template <> struct GraphTraits<Loop *> {
     930             :   typedef Loop *NodeRef;
     931             :   typedef LoopInfo::iterator ChildIteratorType;
     932             : 
     933             :   static NodeRef getEntryNode(Loop *L) { return L; }
     934             :   static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
     935             :   static ChildIteratorType child_end(NodeRef N) { return N->end(); }
     936             : };
     937             : 
     938             : /// Analysis pass that exposes the \c LoopInfo for a function.
     939             : class LoopAnalysis : public AnalysisInfoMixin<LoopAnalysis> {
     940             :   friend AnalysisInfoMixin<LoopAnalysis>;
     941             :   static AnalysisKey Key;
     942             : 
     943             : public:
     944             :   typedef LoopInfo Result;
     945             : 
     946             :   LoopInfo run(Function &F, FunctionAnalysisManager &AM);
     947             : };
     948             : 
     949             : /// Printer pass for the \c LoopAnalysis results.
     950             : class LoopPrinterPass : public PassInfoMixin<LoopPrinterPass> {
     951             :   raw_ostream &OS;
     952             : 
     953             : public:
     954           1 :   explicit LoopPrinterPass(raw_ostream &OS) : OS(OS) {}
     955             :   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
     956             : };
     957             : 
     958             : /// Verifier pass for the \c LoopAnalysis results.
     959             : struct LoopVerifierPass : public PassInfoMixin<LoopVerifierPass> {
     960             :   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
     961             : };
     962             : 
     963             : /// The legacy pass manager's analysis pass to compute loop information.
     964      214284 : class LoopInfoWrapperPass : public FunctionPass {
     965             :   LoopInfo LI;
     966             : 
     967             : public:
     968             :   static char ID; // Pass identification, replacement for typeid
     969             : 
     970      215274 :   LoopInfoWrapperPass() : FunctionPass(ID) {
     971      107637 :     initializeLoopInfoWrapperPassPass(*PassRegistry::getPassRegistry());
     972      107637 :   }
     973             : 
     974     3799123 :   LoopInfo &getLoopInfo() { return LI; }
     975             :   const LoopInfo &getLoopInfo() const { return LI; }
     976             : 
     977             :   /// Calculate the natural loop information for a given function.
     978             :   bool runOnFunction(Function &F) override;
     979             : 
     980             :   void verifyAnalysis() const override;
     981             : 
     982     2349819 :   void releaseMemory() override { LI.releaseMemory(); }
     983             : 
     984             :   void print(raw_ostream &O, const Module *M = nullptr) const override;
     985             : 
     986             :   void getAnalysisUsage(AnalysisUsage &AU) const override;
     987             : };
     988             : 
     989             : /// Function to print a loop's contents as LLVM's text IR assembly.
     990             : void printLoop(Loop &L, raw_ostream &OS, const std::string &Banner = "");
     991             : 
     992             : } // End llvm namespace
     993             : 
     994             : #endif

Generated by: LCOV version 1.13