diff --git a/llvm/include/llvm/Analysis/LoopInfo.h b/llvm/include/llvm/Analysis/LoopInfo.h index a4e5349daef3..7e97bbec649a 100644 --- a/llvm/include/llvm/Analysis/LoopInfo.h +++ b/llvm/include/llvm/Analysis/LoopInfo.h @@ -29,7 +29,7 @@ // in the CFG. There can be strongly connected components in the CFG which // this analysis will not recognize and that will not be represented by a Loop // instance. In particular, a Loop might be inside such a non-loop SCC, or a -// non-loop SCC might contain a sub-SCC which is a Loop. +// non-loop SCC might contain a sub-SCC which is a Loop. // //===----------------------------------------------------------------------===// @@ -56,32 +56,31 @@ class Loop; class MDNode; class PHINode; class raw_ostream; -template -class DominatorTreeBase; -template class LoopInfoBase; -template class LoopBase; +template class DominatorTreeBase; +template class LoopInfoBase; +template class LoopBase; //===----------------------------------------------------------------------===// /// Instances of this class are used to represent loops that are detected in the /// flow graph. /// -template -class LoopBase { +template class LoopBase { LoopT *ParentLoop; // Loops contained entirely within this one. std::vector SubLoops; // The list of blocks in this loop. First entry is the header node. - std::vector Blocks; + std::vector Blocks; - SmallPtrSet DenseBlockSet; + SmallPtrSet DenseBlockSet; /// Indicator that this loop is no longer a valid loop. bool IsInvalid = false; LoopBase(const LoopBase &) = delete; - const LoopBase& - operator=(const LoopBase &) = delete; + const LoopBase & + operator=(const LoopBase &) = delete; + public: /// This creates an empty loop. LoopBase() : ParentLoop(nullptr) {} @@ -104,19 +103,18 @@ public: /// Return true if the specified loop is contained within in this loop. bool contains(const LoopT *L) const { - if (L == this) return true; - if (!L) return false; + if (L == this) + return true; + if (!L) + return false; return contains(L->getParentLoop()); } /// Return true if the specified basic block is in this loop. - bool contains(const BlockT *BB) const { - return DenseBlockSet.count(BB); - } + bool contains(const BlockT *BB) const { return DenseBlockSet.count(BB); } /// Return true if the specified instruction is in this loop. - template - bool contains(const InstT *Inst) const { + template bool contains(const InstT *Inst) const { return contains(Inst->getParent()); } @@ -124,8 +122,8 @@ public: const std::vector &getSubLoops() const { return SubLoops; } std::vector &getSubLoopsVector() { return SubLoops; } typedef typename std::vector::const_iterator iterator; - typedef typename std::vector::const_reverse_iterator - reverse_iterator; + typedef + typename std::vector::const_reverse_iterator reverse_iterator; iterator begin() const { return SubLoops.begin(); } iterator end() const { return SubLoops.end(); } reverse_iterator rbegin() const { return SubLoops.rbegin(); } @@ -133,8 +131,8 @@ public: bool empty() const { return SubLoops.empty(); } /// Get a list of the basic blocks which make up this loop. - const std::vector &getBlocks() const { return Blocks; } - typedef typename std::vector::const_iterator block_iterator; + const std::vector &getBlocks() const { return Blocks; } + typedef typename std::vector::const_iterator block_iterator; block_iterator block_begin() const { return Blocks.begin(); } block_iterator block_end() const { return Blocks.end(); } inline iterator_range blocks() const { @@ -142,9 +140,7 @@ public: } /// Get the number of blocks in this loop in constant time. - unsigned getNumBlocks() const { - return Blocks.size(); - } + unsigned getNumBlocks() const { return Blocks.size(); } /// Invalidate the loop, indicating that it is no longer a loop. void invalidate() { IsInvalid = true; } @@ -155,7 +151,7 @@ public: /// True if terminator in the block can branch to another block that is /// outside of the current loop. bool isLoopExiting(const BlockT *BB) const { - for (const auto &Succ : children(BB)) { + for (const auto &Succ : children(BB)) { if (!contains(Succ)) return true; } @@ -170,8 +166,8 @@ public: assert(contains(BB) && "block does not belong to the loop"); BlockT *Header = getHeader(); - auto PredBegin = GraphTraits >::child_begin(Header); - auto PredEnd = GraphTraits >::child_end(Header); + auto PredBegin = GraphTraits>::child_begin(Header); + auto PredEnd = GraphTraits>::child_end(Header); return std::find(PredBegin, PredEnd, BB) != PredEnd; } @@ -180,7 +176,7 @@ public: unsigned NumBackEdges = 0; BlockT *H = getHeader(); - for (const auto Pred : children >(H)) + for (const auto Pred : children>(H)) if (contains(Pred)) ++NumBackEdges; @@ -206,14 +202,14 @@ public: /// Return all of the successor blocks of this loop. These are the blocks /// _outside of the current loop_ which are branched to. - void getExitBlocks(SmallVectorImpl &ExitBlocks) const; + void getExitBlocks(SmallVectorImpl &ExitBlocks) const; /// If getExitBlocks would return exactly one block, return that block. /// Otherwise return null. BlockT *getExitBlock() const; /// Edge type. - typedef std::pair Edge; + typedef std::pair Edge; /// Return all pairs of (_inside_block_,_outside_block_). void getExitEdges(SmallVectorImpl &ExitEdges) const; @@ -240,7 +236,7 @@ public: /// contains a branch back to the header. void getLoopLatches(SmallVectorImpl &LoopLatches) const { BlockT *H = getHeader(); - for (const auto Pred : children>(H)) + for (const auto Pred : children>(H)) if (contains(Pred)) LoopLatches.push_back(Pred); } @@ -276,7 +272,7 @@ public: assert(I != SubLoops.end() && "Cannot remove end iterator!"); LoopT *Child = *I; assert(Child->ParentLoop == this && "Child is not a child of this loop!"); - SubLoops.erase(SubLoops.begin()+(I-begin())); + SubLoops.erase(SubLoops.begin() + (I - begin())); Child->ParentLoop = nullptr; return Child; } @@ -295,15 +291,14 @@ public: } /// interface to do reserve() for Blocks - void reserveBlocks(unsigned size) { - Blocks.reserve(size); - } + void reserveBlocks(unsigned size) { Blocks.reserve(size); } /// This method is used to move BB (which must be part of this loop) to be the /// loop header of the loop (the block that dominates all others). void moveToHeader(BlockT *BB) { - if (Blocks[0] == BB) return; - for (unsigned i = 0; ; ++i) { + if (Blocks[0] == BB) + return; + for (unsigned i = 0;; ++i) { assert(i != Blocks.size() && "Loop does not contain BB!"); if (Blocks[i] == BB) { Blocks[i] = Blocks[0]; @@ -328,7 +323,7 @@ public: void verifyLoop() const; /// Verify loop structure of this loop and all nested loops. - void verifyLoopNest(DenseSet *Loops) const; + void verifyLoopNest(DenseSet *Loops) const; /// Print loop with all the BBs inside it. void print(raw_ostream &OS, unsigned Depth = 0, bool Verbose = false) const; @@ -355,8 +350,8 @@ protected: } }; -template -raw_ostream& operator<<(raw_ostream &OS, const LoopBase &Loop) { +template +raw_ostream &operator<<(raw_ostream &OS, const LoopBase &Loop) { Loop.print(OS); return OS; } @@ -364,7 +359,6 @@ raw_ostream& operator<<(raw_ostream &OS, const LoopBase &Loop) { // Implementation in LoopInfoImpl.h extern template class LoopBase; - /// Represents a single loop in the control flow graph. Note that not all SCCs /// in the CFG are necessarily loops. class Loop : public LoopBase { @@ -377,17 +371,15 @@ public: public: LocRange() {} LocRange(DebugLoc Start) : Start(std::move(Start)), End(std::move(Start)) {} - LocRange(DebugLoc Start, DebugLoc End) : Start(std::move(Start)), - End(std::move(End)) {} + LocRange(DebugLoc Start, DebugLoc End) + : Start(std::move(Start)), End(std::move(End)) {} const DebugLoc &getStart() const { return Start; } const DebugLoc &getEnd() const { return End; } /// \brief Check for null. /// - explicit operator bool() const { - return Start && End; - } + explicit operator bool() const { return Start && End; } }; Loop() {} @@ -520,8 +512,7 @@ private: /// structures in the specified function. /// -template -class LoopInfoBase { +template class LoopInfoBase { // BBMap - Mapping of basic blocks to the inner most loop they occur in DenseMap BBMap; std::vector TopLevelLoops; @@ -532,8 +523,9 @@ class LoopInfoBase { void operator=(const LoopInfoBase &) = delete; LoopInfoBase(const LoopInfoBase &) = delete; + public: - LoopInfoBase() { } + LoopInfoBase() {} ~LoopInfoBase() { releaseMemory(); } LoopInfoBase(LoopInfoBase &&Arg) @@ -567,8 +559,8 @@ public: /// function. /// typedef typename std::vector::const_iterator iterator; - typedef typename std::vector::const_reverse_iterator - reverse_iterator; + typedef + typename std::vector::const_reverse_iterator reverse_iterator; iterator begin() const { return TopLevelLoops.begin(); } iterator end() const { return TopLevelLoops.end(); } reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); } @@ -597,9 +589,7 @@ public: LoopT *getLoopFor(const BlockT *BB) const { return BBMap.lookup(BB); } /// Same as getLoopFor. - const LoopT *operator[](const BlockT *BB) const { - return getLoopFor(BB); - } + const LoopT *operator[](const BlockT *BB) const { return getLoopFor(BB); } /// Return the loop nesting level of the specified block. A depth of 0 means /// the block is not inside any loop. @@ -621,7 +611,7 @@ public: assert(I != end() && "Cannot remove end iterator!"); LoopT *L = *I; assert(!L->getParentLoop() && "Not a top-level loop!"); - TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin())); + TopLevelLoops.erase(TopLevelLoops.begin() + (I - begin())); return L; } @@ -638,8 +628,7 @@ public: /// Replace the specified loop in the top-level loops list with the indicated /// loop. - void changeTopLevelLoop(LoopT *OldLoop, - LoopT *NewLoop) { + void changeTopLevelLoop(LoopT *OldLoop, LoopT *NewLoop) { auto I = find(TopLevelLoops, OldLoop); assert(I != TopLevelLoops.end() && "Old loop not at top level!"); *I = NewLoop; @@ -670,8 +659,10 @@ public: static bool isNotAlreadyContainedIn(const LoopT *SubLoop, const LoopT *ParentLoop) { - if (!SubLoop) return true; - if (SubLoop == ParentLoop) return false; + if (!SubLoop) + return true; + if (SubLoop == ParentLoop) + return false; return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop); } @@ -694,6 +685,7 @@ class LoopInfo : public LoopInfoBase { void operator=(const LoopInfo &) = delete; LoopInfo(const LoopInfo &) = delete; + public: LoopInfo() {} explicit LoopInfo(const DominatorTreeBase &DomTree); @@ -722,7 +714,8 @@ public: // Preserving LCSSA form is only problematic if the replacing value is an // instruction. Instruction *I = dyn_cast(To); - if (!I) return true; + if (!I) + return true; // If both instructions are defined in the same basic block then replacement // cannot break LCSSA form. if (I->getParent() == From->getParent()) @@ -730,7 +723,8 @@ public: // If the instruction is not defined in a loop then it can safely replace // anything. Loop *ToLoop = getLoopFor(I->getParent()); - if (!ToLoop) return true; + if (!ToLoop) + return true; // If the replacing instruction is defined in the same loop as the original // instruction, or in a loop that contains it as an inner loop, then using // it as a replacement will not break LCSSA form. @@ -810,7 +804,7 @@ public: }; // Allow clients to walk the list of nested loops... -template <> struct GraphTraits { +template <> struct GraphTraits { typedef const Loop *NodeRef; typedef LoopInfo::iterator ChildIteratorType; @@ -819,7 +813,7 @@ template <> struct GraphTraits { static ChildIteratorType child_end(NodeRef N) { return N->end(); } }; -template <> struct GraphTraits { +template <> struct GraphTraits { typedef Loop *NodeRef; typedef LoopInfo::iterator ChildIteratorType; diff --git a/llvm/include/llvm/Analysis/LoopInfoImpl.h b/llvm/include/llvm/Analysis/LoopInfoImpl.h index e9177e68ed77..91298feb6fe4 100644 --- a/llvm/include/llvm/Analysis/LoopInfoImpl.h +++ b/llvm/include/llvm/Analysis/LoopInfoImpl.h @@ -31,11 +31,11 @@ namespace llvm { /// outside of the loop. These are the blocks _inside of the current loop_ /// which branch out. The returned list is always unique. /// -template -void LoopBase:: -getExitingBlocks(SmallVectorImpl &ExitingBlocks) const { +template +void LoopBase::getExitingBlocks( + SmallVectorImpl &ExitingBlocks) const { for (const auto BB : blocks()) - for (const auto &Succ : children(BB)) + for (const auto &Succ : children(BB)) if (!contains(Succ)) { // Not in current loop? It must be an exit block. ExitingBlocks.push_back(BB); @@ -45,9 +45,9 @@ getExitingBlocks(SmallVectorImpl &ExitingBlocks) const { /// getExitingBlock - If getExitingBlocks would return exactly one block, /// return that block. Otherwise return null. -template +template BlockT *LoopBase::getExitingBlock() const { - SmallVector ExitingBlocks; + SmallVector ExitingBlocks; getExitingBlocks(ExitingBlocks); if (ExitingBlocks.size() == 1) return ExitingBlocks[0]; @@ -57,11 +57,11 @@ BlockT *LoopBase::getExitingBlock() const { /// getExitBlocks - Return all of the successor blocks of this loop. These /// are the blocks _outside of the current loop_ which are branched to. /// -template -void LoopBase:: -getExitBlocks(SmallVectorImpl &ExitBlocks) const { +template +void LoopBase::getExitBlocks( + SmallVectorImpl &ExitBlocks) const { for (const auto BB : blocks()) - for (const auto &Succ : children(BB)) + for (const auto &Succ : children(BB)) if (!contains(Succ)) // Not in current loop? It must be an exit block. ExitBlocks.push_back(Succ); @@ -69,9 +69,9 @@ getExitBlocks(SmallVectorImpl &ExitBlocks) const { /// getExitBlock - If getExitBlocks would return exactly one block, /// return that block. Otherwise return null. -template +template BlockT *LoopBase::getExitBlock() const { - SmallVector ExitBlocks; + SmallVector ExitBlocks; getExitBlocks(ExitBlocks); if (ExitBlocks.size() == 1) return ExitBlocks[0]; @@ -79,11 +79,11 @@ BlockT *LoopBase::getExitBlock() const { } /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_). -template -void LoopBase:: -getExitEdges(SmallVectorImpl &ExitEdges) const { +template +void LoopBase::getExitEdges( + SmallVectorImpl &ExitEdges) const { for (const auto BB : blocks()) - for (const auto &Succ : children(BB)) + for (const auto &Succ : children(BB)) if (!contains(Succ)) // Not in current loop? It must be an exit block. ExitEdges.emplace_back(BB, Succ); @@ -97,22 +97,23 @@ getExitEdges(SmallVectorImpl &ExitEdges) const { /// /// This method returns null if there is no preheader for the loop. /// -template +template BlockT *LoopBase::getLoopPreheader() const { // Keep track of nodes outside the loop branching to the header... BlockT *Out = getLoopPredecessor(); - if (!Out) return nullptr; + if (!Out) + return nullptr; // Make sure we are allowed to hoist instructions into the predecessor. if (!Out->isLegalToHoistInto()) return nullptr; // Make sure there is only one exit out of the preheader. - typedef GraphTraits BlockTraits; + typedef GraphTraits BlockTraits; typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out); ++SI; if (SI != BlockTraits::child_end(Out)) - return nullptr; // Multiple exits from the block, must not be a preheader. + return nullptr; // Multiple exits from the block, must not be a preheader. // The predecessor has exactly one successor, so it is a preheader. return Out; @@ -123,17 +124,17 @@ BlockT *LoopBase::getLoopPreheader() const { /// This is less strict that the loop "preheader" concept, which requires /// the predecessor to have exactly one successor. /// -template +template BlockT *LoopBase::getLoopPredecessor() const { // Keep track of nodes outside the loop branching to the header... BlockT *Out = nullptr; // Loop over the predecessors of the header node... BlockT *Header = getHeader(); - for (const auto Pred : children>(Header)) { - if (!contains(Pred)) { // If the block is not in the loop... + for (const auto Pred : children>(Header)) { + if (!contains(Pred)) { // If the block is not in the loop... if (Out && Out != Pred) - return nullptr; // Multiple predecessors outside the loop + return nullptr; // Multiple predecessors outside the loop Out = Pred; } } @@ -145,13 +146,14 @@ BlockT *LoopBase::getLoopPredecessor() const { /// getLoopLatch - If there is a single latch block for this loop, return it. /// A latch block is a block that contains a branch back to the header. -template +template BlockT *LoopBase::getLoopLatch() const { BlockT *Header = getHeader(); BlockT *Latch = nullptr; - for (const auto Pred : children>(Header)) { + for (const auto Pred : children>(Header)) { if (contains(Pred)) { - if (Latch) return nullptr; + if (Latch) + return nullptr; Latch = Pred; } } @@ -169,14 +171,14 @@ BlockT *LoopBase::getLoopLatch() const { /// to the specified LoopInfo object as being in the current basic block. It /// is not valid to replace the loop header with this method. /// -template -void LoopBase:: -addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase &LIB) { +template +void LoopBase::addBasicBlockToLoop( + BlockT *NewBB, LoopInfoBase &LIB) { #ifndef NDEBUG if (!Blocks.empty()) { auto SameHeader = LIB[getHeader()]; - assert(contains(SameHeader) && getHeader() == SameHeader->getHeader() - && "Incorrect LI specified for this loop!"); + assert(contains(SameHeader) && getHeader() == SameHeader->getHeader() && + "Incorrect LI specified for this loop!"); } #endif assert(NewBB && "Cannot add a null basic block to the loop!"); @@ -198,9 +200,9 @@ addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase &LIB) { /// the OldChild entry in our children list with NewChild, and updates the /// parent pointer of OldChild to be null and the NewChild to be this loop. /// This updates the loop depth of the new child. -template -void LoopBase:: -replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild) { +template +void LoopBase::replaceChildLoopWith(LoopT *OldChild, + LoopT *NewChild) { assert(OldChild->ParentLoop == this && "This loop is already broken!"); assert(!NewChild->ParentLoop && "NewChild already has a parent!"); typename std::vector::iterator I = find(SubLoops, OldChild); @@ -211,46 +213,47 @@ replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild) { } /// verifyLoop - Verify loop structure -template +template void LoopBase::verifyLoop() const { #ifndef NDEBUG assert(!Blocks.empty() && "Loop header is missing"); // Setup for using a depth-first iterator to visit every block in the loop. - SmallVector ExitBBs; + SmallVector ExitBBs; getExitBlocks(ExitBBs); - df_iterator_default_set VisitSet; + df_iterator_default_set VisitSet; VisitSet.insert(ExitBBs.begin(), ExitBBs.end()); - df_ext_iterator> - BI = df_ext_begin(getHeader(), VisitSet), - BE = df_ext_end(getHeader(), VisitSet); + df_ext_iterator> + BI = df_ext_begin(getHeader(), VisitSet), + BE = df_ext_end(getHeader(), VisitSet); // Keep track of the BBs visited. - SmallPtrSet VisitedBBs; + SmallPtrSet VisitedBBs; // Check the individual blocks. - for ( ; BI != BE; ++BI) { + for (; BI != BE; ++BI) { BlockT *BB = *BI; - assert(std::any_of(GraphTraits::child_begin(BB), - GraphTraits::child_end(BB), - [&](BlockT *B){return contains(B);}) && + assert(std::any_of(GraphTraits::child_begin(BB), + GraphTraits::child_end(BB), + [&](BlockT *B) { return contains(B); }) && "Loop block has no in-loop successors!"); - assert(std::any_of(GraphTraits >::child_begin(BB), - GraphTraits >::child_end(BB), - [&](BlockT *B){return contains(B);}) && + assert(std::any_of(GraphTraits>::child_begin(BB), + GraphTraits>::child_end(BB), + [&](BlockT *B) { return contains(B); }) && "Loop block has no in-loop predecessors!"); SmallVector OutsideLoopPreds; - std::for_each(GraphTraits >::child_begin(BB), - GraphTraits >::child_end(BB), - [&](BlockT *B){if (!contains(B)) + std::for_each(GraphTraits>::child_begin(BB), + GraphTraits>::child_end(BB), + [&](BlockT *B) { + if (!contains(B)) OutsideLoopPreds.push_back(B); }); if (BB == getHeader()) { - assert(!OutsideLoopPreds.empty() && "Loop is unreachable!"); + assert(!OutsideLoopPreds.empty() && "Loop is unreachable!"); } else if (!OutsideLoopPreds.empty()) { // A non-header loop shouldn't be reachable from outside the loop, // though it is permitted if the predecessor is not itself actually @@ -282,8 +285,8 @@ void LoopBase::verifyLoop() const { // Each block in each subloop should be contained within this loop. for (block_iterator BI = (*I)->block_begin(), BE = (*I)->block_end(); BI != BE; ++BI) { - assert(contains(*BI) && - "Loop does not contain all the blocks of a subloop!"); + assert(contains(*BI) && + "Loop does not contain all the blocks of a subloop!"); } // Check the parent loop pointer. @@ -295,9 +298,9 @@ void LoopBase::verifyLoop() const { } /// verifyLoop - Verify loop structure of this loop and all nested loops. -template +template void LoopBase::verifyLoopNest( - DenseSet *Loops) const { + DenseSet *Loops) const { Loops->insert(static_cast(this)); // Verify this loop. verifyLoop(); @@ -306,30 +309,34 @@ void LoopBase::verifyLoopNest( (*I)->verifyLoopNest(Loops); } -template +template void LoopBase::print(raw_ostream &OS, unsigned Depth, bool Verbose) const { - OS.indent(Depth*2) << "Loop at depth " << getLoopDepth() - << " containing: "; + OS.indent(Depth * 2) << "Loop at depth " << getLoopDepth() << " containing: "; BlockT *H = getHeader(); for (unsigned i = 0; i < getBlocks().size(); ++i) { BlockT *BB = getBlocks()[i]; if (!Verbose) { - if (i) OS << ","; + if (i) + OS << ","; BB->printAsOperand(OS, false); - } else OS << "\n"; + } else + OS << "\n"; - if (BB == H) OS << "
"; - if (isLoopLatch(BB)) OS << ""; - if (isLoopExiting(BB)) OS << ""; + if (BB == H) + OS << "
"; + if (isLoopLatch(BB)) + OS << ""; + if (isLoopExiting(BB)) + OS << ""; if (Verbose) BB->print(OS); } OS << "\n"; for (iterator I = begin(), E = end(); I != E; ++I) - (*I)->print(OS, Depth+2); + (*I)->print(OS, Depth + 2); } //===----------------------------------------------------------------------===// @@ -341,10 +348,10 @@ void LoopBase::print(raw_ostream &OS, unsigned Depth, /// this loop are mapped to this loop or a subloop. And all subloops within this /// loop have their parent loop set to this loop or a subloop. template -static void discoverAndMapSubloop( - LoopT *L, ArrayRef Backedges, LoopInfoBase *LI, - const DomTreeBase &DomTree) { - typedef GraphTraits > InvBlockTraits; +static void discoverAndMapSubloop(LoopT *L, ArrayRef Backedges, + LoopInfoBase *LI, + const DomTreeBase &DomTree) { + typedef GraphTraits> InvBlockTraits; unsigned NumBlocks = 0; unsigned NumSubloops = 0; @@ -364,13 +371,12 @@ static void discoverAndMapSubloop( LI->changeLoopFor(PredBB, L); ++NumBlocks; if (PredBB == L->getHeader()) - continue; + continue; // Push all block predecessors on the worklist. ReverseCFGWorklist.insert(ReverseCFGWorklist.end(), InvBlockTraits::child_begin(PredBB), InvBlockTraits::child_end(PredBB)); - } - else { + } else { // This is a discovered block. Find its outermost discovered loop. while (LoopT *Parent = Subloop->getParentLoop()) Subloop = Parent; @@ -388,7 +394,7 @@ static void discoverAndMapSubloop( // within this subloop tree itself. Note that a predecessor may directly // reach another subloop that is not yet discovered to be a subloop of // this loop, which we must traverse. - for (const auto Pred : children>(PredBB)) { + for (const auto Pred : children>(PredBB)) { if (LI->getLoopFor(Pred) != Subloop) ReverseCFGWorklist.push_back(Pred); } @@ -399,15 +405,14 @@ static void discoverAndMapSubloop( } /// Populate all loop data in a stable order during a single forward DFS. -template -class PopulateLoopsDFS { - typedef GraphTraits BlockTraits; +template class PopulateLoopsDFS { + typedef GraphTraits BlockTraits; typedef typename BlockTraits::ChildIteratorType SuccIterTy; LoopInfoBase *LI; + public: - PopulateLoopsDFS(LoopInfoBase *li): - LI(li) {} + PopulateLoopsDFS(LoopInfoBase *li) : LI(li) {} void traverse(BlockT *EntryBlock); @@ -416,7 +421,7 @@ protected: }; /// Top-level driver for the forward DFS within the loop. -template +template void PopulateLoopsDFS::traverse(BlockT *EntryBlock) { for (BlockT *BB : post_order(EntryBlock)) insertIntoLoop(BB); @@ -425,7 +430,7 @@ void PopulateLoopsDFS::traverse(BlockT *EntryBlock) { /// Add a single Block to its ancestor loops in PostOrder. If the block is a /// subloop header, add the subloop to its parent in PostOrder, then reverse the /// Block and Subloop vectors of the now complete subloop to achieve RPO. -template +template void PopulateLoopsDFS::insertIntoLoop(BlockT *Block) { LoopT *Subloop = LI->getLoopFor(Block); if (Subloop && Block == Subloop->getHeader()) { @@ -463,8 +468,7 @@ void PopulateLoopsDFS::insertIntoLoop(BlockT *Block) { /// The Block vectors are inclusive, so step 3 requires loop-depth number of /// insertions per block. template -void LoopInfoBase::analyze( - const DomTreeBase &DomTree) { +void LoopInfoBase::analyze(const DomTreeBase &DomTree) { // Postorder traversal of the dominator tree. const DomTreeNodeBase *DomRoot = DomTree.getRootNode(); for (auto DomNode : post_order(DomRoot)) { @@ -473,17 +477,17 @@ void LoopInfoBase::analyze( SmallVector Backedges; // Check each predecessor of the potential loop header. - for (const auto Backedge : children>(Header)) { + for (const auto Backedge : children>(Header)) { // If Header dominates predBB, this is a new loop. Collect the backedges. - if (DomTree.dominates(Header, Backedge) - && DomTree.isReachableFromEntry(Backedge)) { + if (DomTree.dominates(Header, Backedge) && + DomTree.isReachableFromEntry(Backedge)) { Backedges.push_back(Backedge); } } // Perform a backward CFG traversal to discover and map blocks in this loop. if (!Backedges.empty()) { LoopT *L = new LoopT(Header); - discoverAndMapSubloop(L, ArrayRef(Backedges), this, DomTree); + discoverAndMapSubloop(L, ArrayRef(Backedges), this, DomTree); } } // Perform a single forward CFG traversal to populate block and subloop @@ -542,7 +546,7 @@ LoopInfoBase::getLoopsInReverseSiblingPreorder() { } // Debugging -template +template void LoopInfoBase::print(raw_ostream &OS) const { for (unsigned i = 0; i < TopLevelLoops.size(); ++i) TopLevelLoops[i]->print(OS); @@ -607,13 +611,13 @@ static void compareLoops(const LoopT *L, const LoopT *OtherL, template void LoopInfoBase::verify( const DomTreeBase &DomTree) const { - DenseSet Loops; + DenseSet Loops; for (iterator I = begin(), E = end(); I != E; ++I) { assert(!(*I)->getParentLoop() && "Top-level loop has a parent!"); (*I)->verifyLoopNest(&Loops); } - // Verify that blocks are mapped to valid loops. +// Verify that blocks are mapped to valid loops. #ifndef NDEBUG for (auto &Entry : BBMap) { const BlockT *BB = Entry.first; diff --git a/llvm/lib/Analysis/LoopInfo.cpp b/llvm/lib/Analysis/LoopInfo.cpp index 4b829e4e83d9..e82f552c9d00 100644 --- a/llvm/lib/Analysis/LoopInfo.cpp +++ b/llvm/lib/Analysis/LoopInfo.cpp @@ -44,9 +44,9 @@ bool llvm::VerifyLoopInfo = true; #else bool llvm::VerifyLoopInfo = false; #endif -static cl::opt -VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo), - cl::desc("Verify loop info (time consuming)")); +static cl::opt + VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo), + cl::desc("Verify loop info (time consuming)")); //===----------------------------------------------------------------------===// // Loop implementation @@ -55,7 +55,7 @@ VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo), bool Loop::isLoopInvariant(const Value *V) const { if (const Instruction *I = dyn_cast(V)) return !contains(I); - return true; // All non-instructions are loop invariant + return true; // All non-instructions are loop invariant } bool Loop::hasLoopInvariantOperands(const Instruction *I) const { @@ -66,7 +66,7 @@ bool Loop::makeLoopInvariant(Value *V, bool &Changed, Instruction *InsertPt) const { if (Instruction *I = dyn_cast(V)) return makeLoopInvariant(I, Changed, InsertPt); - return true; // All non-instructions are loop-invariant. + return true; // All non-instructions are loop-invariant. } bool Loop::makeLoopInvariant(Instruction *I, bool &Changed, @@ -112,12 +112,13 @@ PHINode *Loop::getCanonicalInductionVariable() const { BasicBlock *Incoming = nullptr, *Backedge = nullptr; pred_iterator PI = pred_begin(H); - assert(PI != pred_end(H) && - "Loop must have at least one backedge!"); + assert(PI != pred_end(H) && "Loop must have at least one backedge!"); Backedge = *PI++; - if (PI == pred_end(H)) return nullptr; // dead loop + if (PI == pred_end(H)) + return nullptr; // dead loop Incoming = *PI++; - if (PI != pred_end(H)) return nullptr; // multiple backedges? + if (PI != pred_end(H)) + return nullptr; // multiple backedges? if (contains(Incoming)) { if (contains(Backedge)) @@ -130,12 +131,11 @@ PHINode *Loop::getCanonicalInductionVariable() const { for (BasicBlock::iterator I = H->begin(); isa(I); ++I) { PHINode *PN = cast(I); if (ConstantInt *CI = - dyn_cast(PN->getIncomingValueForBlock(Incoming))) + dyn_cast(PN->getIncomingValueForBlock(Incoming))) if (CI->isZero()) if (Instruction *Inc = - dyn_cast(PN->getIncomingValueForBlock(Backedge))) - if (Inc->getOpcode() == Instruction::Add && - Inc->getOperand(0) == PN) + dyn_cast(PN->getIncomingValueForBlock(Backedge))) + if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN) if (ConstantInt *CI = dyn_cast(Inc->getOperand(1))) if (CI->isOne()) return PN; @@ -255,7 +255,8 @@ void Loop::setLoopID(MDNode *LoopID) const { return; } - assert(!getLoopLatch() && "The loop should have no single latch at this point"); + assert(!getLoopLatch() && + "The loop should have no single latch at this point"); BasicBlock *H = getHeader(); for (BasicBlock *BB : this->blocks()) { TerminatorInst *TI = BB->getTerminator(); @@ -270,7 +271,7 @@ bool Loop::isAnnotatedParallel() const { MDNode *DesiredLoopIdMetadata = getLoopID(); if (!DesiredLoopIdMetadata) - return false; + return false; // The loop branch contains the parallel loop metadata. In order to ensure // that any parallel-loop-unaware optimization pass hasn't added loop-carried @@ -307,9 +308,7 @@ bool Loop::isAnnotatedParallel() const { return true; } -DebugLoc Loop::getStartLoc() const { - return getLocRange().getStart(); -} +DebugLoc Loop::getStartLoc() const { return getLocRange().getStart(); } Loop::LocRange Loop::getLocRange() const { // If we have a debug location in the loop ID, then use it. @@ -357,8 +356,8 @@ bool Loop::hasDedicatedExits() const { return true; } -void -Loop::getUniqueExitBlocks(SmallVectorImpl &ExitBlocks) const { +void Loop::getUniqueExitBlocks( + SmallVectorImpl &ExitBlocks) const { assert(hasDedicatedExits() && "getUniqueExitBlocks assumes the loop has canonical form exits!"); @@ -408,12 +407,10 @@ BasicBlock *Loop::getUniqueExitBlock() const { } #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) -LLVM_DUMP_METHOD void Loop::dump() const { - print(dbgs()); -} +LLVM_DUMP_METHOD void Loop::dump() const { print(dbgs()); } LLVM_DUMP_METHOD void Loop::dumpVerbose() const { - print(dbgs(), /*Depth=*/ 0, /*Verbose=*/ true); + print(dbgs(), /*Depth=*/0, /*Verbose=*/true); } #endif @@ -434,15 +431,15 @@ class UnloopUpdater { // loops within these subloops will not change parents. However, an immediate // subloop's new parent will be the nearest loop reachable from either its own // exits *or* any of its nested loop's exits. - DenseMap SubloopParents; + DenseMap SubloopParents; // Flag the presence of an irreducible backedge whose destination is a block // directly contained by the original unloop. bool FoundIB; public: - UnloopUpdater(Loop *UL, LoopInfo *LInfo) : - Unloop(*UL), LI(LInfo), DFS(UL), FoundIB(false) {} + UnloopUpdater(Loop *UL, LoopInfo *LInfo) + : Unloop(*UL), LI(LInfo), DFS(UL), FoundIB(false) {} void updateBlockParents(); @@ -472,8 +469,7 @@ void UnloopUpdater::updateBlockParents() { assert((NL != &Unloop && (!NL || NL->contains(&Unloop))) && "uninitialized successor"); LI->changeLoopFor(POI, NL); - } - else { + } else { // Or the current block is part of a subloop, in which case its parent // is unchanged. assert((FoundIB || Unloop.contains(L)) && "uninitialized successor"); @@ -490,7 +486,8 @@ void UnloopUpdater::updateBlockParents() { // from successors to predecessors as before. Changed = false; for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(), - POE = DFS.endPostorder(); POI != POE; ++POI) { + POE = DFS.endPostorder(); + POI != POE; ++POI) { Loop *L = LI->getLoopFor(*POI); Loop *NL = getNearestLoop(*POI, L); @@ -508,8 +505,8 @@ void UnloopUpdater::updateBlockParents() { void UnloopUpdater::removeBlocksFromAncestors() { // Remove all unloop's blocks (including those in nested subloops) from // ancestors below the new parent loop. - for (Loop::block_iterator BI = Unloop.block_begin(), - BE = Unloop.block_end(); BI != BE; ++BI) { + for (Loop::block_iterator BI = Unloop.block_begin(), BE = Unloop.block_end(); + BI != BE; ++BI) { Loop *OuterParent = LI->getLoopFor(*BI); if (Unloop.contains(OuterParent)) { while (OuterParent->getParentLoop() != &Unloop) @@ -609,9 +606,7 @@ Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) { return NearLoop; } -LoopInfo::LoopInfo(const DomTreeBase &DomTree) { - analyze(DomTree); -} +LoopInfo::LoopInfo(const DomTreeBase &DomTree) { analyze(DomTree); } bool LoopInfo::invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &) { @@ -766,5 +761,7 @@ PreservedAnalyses LoopVerifierPass::run(Function &F, void LoopBlocksDFS::perform(LoopInfo *LI) { LoopBlocksTraversal Traversal(*this, LI); for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(), - POE = Traversal.end(); POI != POE; ++POI) ; + POE = Traversal.end(); + POI != POE; ++POI) + ; }