1 //===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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.
19 // It can calculate on the fly various bits of information, for example:
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
28 //===----------------------------------------------------------------------===//
30 #ifndef LLVM_ANALYSIS_LOOPINFO_H
31 #define LLVM_ANALYSIS_LOOPINFO_H
33 #include "llvm/ADT/DenseMap.h"
34 #include "llvm/ADT/DenseSet.h"
35 #include "llvm/ADT/GraphTraits.h"
36 #include "llvm/ADT/SmallPtrSet.h"
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/IR/CFG.h"
39 #include "llvm/IR/Instruction.h"
40 #include "llvm/Pass.h"
45 // FIXME: Replace this brittle forward declaration with the include of the new
46 // PassManager.h when doing so doesn't break the PassManagerBuilder.
47 template <typename IRUnitT> class AnalysisManager;
48 class PreservedAnalyses;
56 template<class N> class DominatorTreeBase;
57 template<class N, class M> class LoopInfoBase;
58 template<class N, class M> class LoopBase;
60 //===----------------------------------------------------------------------===//
61 /// LoopBase class - Instances of this class are used to represent loops that
62 /// are detected in the flow graph
64 template<class BlockT, class LoopT>
67 // SubLoops - Loops contained entirely within this one.
68 std::vector<LoopT *> SubLoops;
70 // Blocks - The list of blocks in this loop. First entry is the header node.
71 std::vector<BlockT*> Blocks;
73 SmallPtrSet<const BlockT*, 8> DenseBlockSet;
75 LoopBase(const LoopBase<BlockT, LoopT> &) = delete;
76 const LoopBase<BlockT, LoopT>&
77 operator=(const LoopBase<BlockT, LoopT> &) = delete;
79 /// Loop ctor - This creates an empty loop.
80 LoopBase() : ParentLoop(nullptr) {}
82 for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
86 /// getLoopDepth - Return the nesting level of this loop. An outer-most
87 /// loop has depth 1, for consistency with loop depth values used for basic
88 /// blocks, where depth 0 is used for blocks not inside any loops.
89 unsigned getLoopDepth() const {
91 for (const LoopT *CurLoop = ParentLoop; CurLoop;
92 CurLoop = CurLoop->ParentLoop)
96 BlockT *getHeader() const { return Blocks.front(); }
97 LoopT *getParentLoop() const { return ParentLoop; }
99 /// setParentLoop is a raw interface for bypassing addChildLoop.
100 void setParentLoop(LoopT *L) { ParentLoop = L; }
102 /// contains - Return true if the specified loop is contained within in
105 bool contains(const LoopT *L) const {
106 if (L == this) return true;
107 if (!L) return false;
108 return contains(L->getParentLoop());
111 /// contains - Return true if the specified basic block is in this loop.
113 bool contains(const BlockT *BB) const {
114 return DenseBlockSet.count(BB);
117 /// contains - Return true if the specified instruction is in this loop.
119 template<class InstT>
120 bool contains(const InstT *Inst) const {
121 return contains(Inst->getParent());
124 /// iterator/begin/end - Return the loops contained entirely within this loop.
126 const std::vector<LoopT *> &getSubLoops() const { return SubLoops; }
127 std::vector<LoopT *> &getSubLoopsVector() { return SubLoops; }
128 typedef typename std::vector<LoopT *>::const_iterator iterator;
129 typedef typename std::vector<LoopT *>::const_reverse_iterator
131 iterator begin() const { return SubLoops.begin(); }
132 iterator end() const { return SubLoops.end(); }
133 reverse_iterator rbegin() const { return SubLoops.rbegin(); }
134 reverse_iterator rend() const { return SubLoops.rend(); }
135 bool empty() const { return SubLoops.empty(); }
137 /// getBlocks - Get a list of the basic blocks which make up this loop.
139 const std::vector<BlockT*> &getBlocks() const { return Blocks; }
140 typedef typename std::vector<BlockT*>::const_iterator block_iterator;
141 block_iterator block_begin() const { return Blocks.begin(); }
142 block_iterator block_end() const { return Blocks.end(); }
144 /// getNumBlocks - Get the number of blocks in this loop in constant time.
145 unsigned getNumBlocks() const {
146 return Blocks.size();
149 /// isLoopExiting - True if terminator in the block can branch to another
150 /// block that is outside of the current loop.
152 bool isLoopExiting(const BlockT *BB) const {
153 typedef GraphTraits<const BlockT*> BlockTraits;
154 for (typename BlockTraits::ChildIteratorType SI =
155 BlockTraits::child_begin(BB),
156 SE = BlockTraits::child_end(BB); SI != SE; ++SI) {
163 /// getNumBackEdges - Calculate the number of back edges to the loop header
165 unsigned getNumBackEdges() const {
166 unsigned NumBackEdges = 0;
167 BlockT *H = getHeader();
169 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
170 for (typename InvBlockTraits::ChildIteratorType I =
171 InvBlockTraits::child_begin(H),
172 E = InvBlockTraits::child_end(H); I != E; ++I)
179 //===--------------------------------------------------------------------===//
180 // APIs for simple analysis of the loop.
182 // Note that all of these methods can fail on general loops (ie, there may not
183 // be a preheader, etc). For best success, the loop simplification and
184 // induction variable canonicalization pass should be used to normalize loops
185 // for easy analysis. These methods assume canonical loops.
187 /// getExitingBlocks - Return all blocks inside the loop that have successors
188 /// outside of the loop. These are the blocks _inside of the current loop_
189 /// which branch out. The returned list is always unique.
191 void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const;
193 /// getExitingBlock - If getExitingBlocks would return exactly one block,
194 /// return that block. Otherwise return null.
195 BlockT *getExitingBlock() const;
197 /// getExitBlocks - Return all of the successor blocks of this loop. These
198 /// are the blocks _outside of the current loop_ which are branched to.
200 void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const;
202 /// getExitBlock - If getExitBlocks would return exactly one block,
203 /// return that block. Otherwise return null.
204 BlockT *getExitBlock() const;
207 typedef std::pair<const BlockT*, const BlockT*> Edge;
209 /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
210 void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const;
212 /// getLoopPreheader - If there is a preheader for this loop, return it. A
213 /// loop has a preheader if there is only one edge to the header of the loop
214 /// from outside of the loop. If this is the case, the block branching to the
215 /// header of the loop is the preheader node.
217 /// This method returns null if there is no preheader for the loop.
219 BlockT *getLoopPreheader() const;
221 /// getLoopPredecessor - If the given loop's header has exactly one unique
222 /// predecessor outside the loop, return it. Otherwise return null.
223 /// This is less strict that the loop "preheader" concept, which requires
224 /// the predecessor to have exactly one successor.
226 BlockT *getLoopPredecessor() const;
228 /// getLoopLatch - If there is a single latch block for this loop, return it.
229 /// A latch block is a block that contains a branch back to the header.
230 BlockT *getLoopLatch() const;
232 /// getLoopLatches - Return all loop latch blocks of this loop. A latch block
233 /// is a block that contains a branch back to the header.
234 void getLoopLatches(SmallVectorImpl<BlockT *> &LoopLatches) const {
235 BlockT *H = getHeader();
236 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
237 for (typename InvBlockTraits::ChildIteratorType I =
238 InvBlockTraits::child_begin(H),
239 E = InvBlockTraits::child_end(H); I != E; ++I)
241 LoopLatches.push_back(*I);
244 //===--------------------------------------------------------------------===//
245 // APIs for updating loop information after changing the CFG
248 /// addBasicBlockToLoop - This method is used by other analyses to update loop
249 /// information. NewBB is set to be a new member of the current loop.
250 /// Because of this, it is added as a member of all parent loops, and is added
251 /// to the specified LoopInfo object as being in the current basic block. It
252 /// is not valid to replace the loop header with this method.
254 void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
256 /// replaceChildLoopWith - This is used when splitting loops up. It replaces
257 /// the OldChild entry in our children list with NewChild, and updates the
258 /// parent pointer of OldChild to be null and the NewChild to be this loop.
259 /// This updates the loop depth of the new child.
260 void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild);
262 /// addChildLoop - Add the specified loop to be a child of this loop. This
263 /// updates the loop depth of the new child.
265 void addChildLoop(LoopT *NewChild) {
266 assert(!NewChild->ParentLoop && "NewChild already has a parent!");
267 NewChild->ParentLoop = static_cast<LoopT *>(this);
268 SubLoops.push_back(NewChild);
271 /// removeChildLoop - This removes the specified child from being a subloop of
272 /// this loop. The loop is not deleted, as it will presumably be inserted
273 /// into another loop.
274 LoopT *removeChildLoop(iterator I) {
275 assert(I != SubLoops.end() && "Cannot remove end iterator!");
277 assert(Child->ParentLoop == this && "Child is not a child of this loop!");
278 SubLoops.erase(SubLoops.begin()+(I-begin()));
279 Child->ParentLoop = nullptr;
283 /// addBlockEntry - This adds a basic block directly to the basic block list.
284 /// This should only be used by transformations that create new loops. Other
285 /// transformations should use addBasicBlockToLoop.
286 void addBlockEntry(BlockT *BB) {
287 Blocks.push_back(BB);
288 DenseBlockSet.insert(BB);
291 /// reverseBlocks - interface to reverse Blocks[from, end of loop] in this loop
292 void reverseBlock(unsigned from) {
293 std::reverse(Blocks.begin() + from, Blocks.end());
296 /// reserveBlocks- interface to do reserve() for Blocks
297 void reserveBlocks(unsigned size) {
298 Blocks.reserve(size);
301 /// moveToHeader - This method is used to move BB (which must be part of this
302 /// loop) to be the loop header of the loop (the block that dominates all
304 void moveToHeader(BlockT *BB) {
305 if (Blocks[0] == BB) return;
306 for (unsigned i = 0; ; ++i) {
307 assert(i != Blocks.size() && "Loop does not contain BB!");
308 if (Blocks[i] == BB) {
309 Blocks[i] = Blocks[0];
316 /// removeBlockFromLoop - This removes the specified basic block from the
317 /// current loop, updating the Blocks as appropriate. This does not update
318 /// the mapping in the LoopInfo class.
319 void removeBlockFromLoop(BlockT *BB) {
320 auto I = std::find(Blocks.begin(), Blocks.end(), BB);
321 assert(I != Blocks.end() && "N is not in this list!");
324 DenseBlockSet.erase(BB);
327 /// verifyLoop - Verify loop structure
328 void verifyLoop() const;
330 /// verifyLoop - Verify loop structure of this loop and all nested loops.
331 void verifyLoopNest(DenseSet<const LoopT*> *Loops) const;
333 void print(raw_ostream &OS, unsigned Depth = 0) const;
336 friend class LoopInfoBase<BlockT, LoopT>;
337 explicit LoopBase(BlockT *BB) : ParentLoop(nullptr) {
338 Blocks.push_back(BB);
339 DenseBlockSet.insert(BB);
343 template<class BlockT, class LoopT>
344 raw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
349 // Implementation in LoopInfoImpl.h
351 __extension__ extern template class LoopBase<BasicBlock, Loop>;
354 class Loop : public LoopBase<BasicBlock, Loop> {
358 /// isLoopInvariant - Return true if the specified value is loop invariant
360 bool isLoopInvariant(const Value *V) const;
362 /// hasLoopInvariantOperands - Return true if all the operands of the
363 /// specified instruction are loop invariant.
364 bool hasLoopInvariantOperands(const Instruction *I) const;
366 /// makeLoopInvariant - If the given value is an instruction inside of the
367 /// loop and it can be hoisted, do so to make it trivially loop-invariant.
368 /// Return true if the value after any hoisting is loop invariant. This
369 /// function can be used as a slightly more aggressive replacement for
372 /// If InsertPt is specified, it is the point to hoist instructions to.
373 /// If null, the terminator of the loop preheader is used.
375 bool makeLoopInvariant(Value *V, bool &Changed,
376 Instruction *InsertPt = nullptr) const;
378 /// makeLoopInvariant - If the given instruction is inside of the
379 /// loop and it can be hoisted, do so to make it trivially loop-invariant.
380 /// Return true if the instruction after any hoisting is loop invariant. This
381 /// function can be used as a slightly more aggressive replacement for
384 /// If InsertPt is specified, it is the point to hoist instructions to.
385 /// If null, the terminator of the loop preheader is used.
387 bool makeLoopInvariant(Instruction *I, bool &Changed,
388 Instruction *InsertPt = nullptr) const;
390 /// getCanonicalInductionVariable - Check to see if the loop has a canonical
391 /// induction variable: an integer recurrence that starts at 0 and increments
392 /// by one each time through the loop. If so, return the phi node that
393 /// corresponds to it.
395 /// The IndVarSimplify pass transforms loops to have a canonical induction
398 PHINode *getCanonicalInductionVariable() const;
400 /// isLCSSAForm - Return true if the Loop is in LCSSA form
401 bool isLCSSAForm(DominatorTree &DT) const;
403 /// isLoopSimplifyForm - Return true if the Loop is in the form that
404 /// the LoopSimplify form transforms loops to, which is sometimes called
406 bool isLoopSimplifyForm() const;
408 /// isSafeToClone - Return true if the loop body is safe to clone in practice.
409 bool isSafeToClone() const;
411 /// Returns true if the loop is annotated parallel.
413 /// A parallel loop can be assumed to not contain any dependencies between
414 /// iterations by the compiler. That is, any loop-carried dependency checking
415 /// can be skipped completely when parallelizing the loop on the target
416 /// machine. Thus, if the parallel loop information originates from the
417 /// programmer, e.g. via the OpenMP parallel for pragma, it is the
418 /// programmer's responsibility to ensure there are no loop-carried
419 /// dependencies. The final execution order of the instructions across
420 /// iterations is not guaranteed, thus, the end result might or might not
421 /// implement actual concurrent execution of instructions across multiple
423 bool isAnnotatedParallel() const;
425 /// Return the llvm.loop loop id metadata node for this loop if it is present.
427 /// If this loop contains the same llvm.loop metadata on each branch to the
428 /// header then the node is returned. If any latch instruction does not
429 /// contain llvm.loop or or if multiple latches contain different nodes then
431 MDNode *getLoopID() const;
432 /// Set the llvm.loop loop id metadata for this loop.
434 /// The LoopID metadata node will be added to each terminator instruction in
435 /// the loop that branches to the loop header.
437 /// The LoopID metadata node should have one or more operands and the first
438 /// operand should should be the node itself.
439 void setLoopID(MDNode *LoopID) const;
441 /// hasDedicatedExits - Return true if no exit block for the loop
442 /// has a predecessor that is outside the loop.
443 bool hasDedicatedExits() const;
445 /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
446 /// These are the blocks _outside of the current loop_ which are branched to.
447 /// This assumes that loop exits are in canonical form.
449 void getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const;
451 /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
452 /// block, return that block. Otherwise return null.
453 BasicBlock *getUniqueExitBlock() const;
457 /// \brief Return the debug location of the start of this loop.
458 /// This looks for a BB terminating instruction with a known debug
459 /// location by looking at the preheader and header blocks. If it
460 /// cannot find a terminating instruction with location information,
461 /// it returns an unknown location.
462 DebugLoc getStartLoc() const {
465 // Try the pre-header first.
466 if ((HeadBB = getLoopPreheader()) != nullptr)
467 if (DebugLoc DL = HeadBB->getTerminator()->getDebugLoc())
470 // If we have no pre-header or there are no instructions with debug
471 // info in it, try the header.
472 HeadBB = getHeader();
474 return HeadBB->getTerminator()->getDebugLoc();
480 friend class LoopInfoBase<BasicBlock, Loop>;
481 explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
484 //===----------------------------------------------------------------------===//
485 /// LoopInfo - This class builds and contains all of the top level loop
486 /// structures in the specified function.
489 template<class BlockT, class LoopT>
491 // BBMap - Mapping of basic blocks to the inner most loop they occur in
492 DenseMap<const BlockT *, LoopT *> BBMap;
493 std::vector<LoopT *> TopLevelLoops;
494 friend class LoopBase<BlockT, LoopT>;
495 friend class LoopInfo;
497 void operator=(const LoopInfoBase &) = delete;
498 LoopInfoBase(const LoopInfoBase &) = delete;
501 ~LoopInfoBase() { releaseMemory(); }
503 LoopInfoBase(LoopInfoBase &&Arg)
504 : BBMap(std::move(Arg.BBMap)),
505 TopLevelLoops(std::move(Arg.TopLevelLoops)) {
506 // We have to clear the arguments top level loops as we've taken ownership.
507 Arg.TopLevelLoops.clear();
509 LoopInfoBase &operator=(LoopInfoBase &&RHS) {
510 BBMap = std::move(RHS.BBMap);
512 for (auto *L : TopLevelLoops)
514 TopLevelLoops = std::move(RHS.TopLevelLoops);
515 RHS.TopLevelLoops.clear();
519 void releaseMemory() {
522 for (auto *L : TopLevelLoops)
524 TopLevelLoops.clear();
527 /// iterator/begin/end - The interface to the top-level loops in the current
530 typedef typename std::vector<LoopT *>::const_iterator iterator;
531 typedef typename std::vector<LoopT *>::const_reverse_iterator
533 iterator begin() const { return TopLevelLoops.begin(); }
534 iterator end() const { return TopLevelLoops.end(); }
535 reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
536 reverse_iterator rend() const { return TopLevelLoops.rend(); }
537 bool empty() const { return TopLevelLoops.empty(); }
539 /// getLoopFor - Return the inner most loop that BB lives in. If a basic
540 /// block is in no loop (for example the entry node), null is returned.
542 LoopT *getLoopFor(const BlockT *BB) const { return BBMap.lookup(BB); }
544 /// operator[] - same as getLoopFor...
546 const LoopT *operator[](const BlockT *BB) const {
547 return getLoopFor(BB);
550 /// getLoopDepth - Return the loop nesting level of the specified block. A
551 /// depth of 0 means the block is not inside any loop.
553 unsigned getLoopDepth(const BlockT *BB) const {
554 const LoopT *L = getLoopFor(BB);
555 return L ? L->getLoopDepth() : 0;
558 // isLoopHeader - True if the block is a loop header node
559 bool isLoopHeader(const BlockT *BB) const {
560 const LoopT *L = getLoopFor(BB);
561 return L && L->getHeader() == BB;
564 /// removeLoop - This removes the specified top-level loop from this loop info
565 /// object. The loop is not deleted, as it will presumably be inserted into
567 LoopT *removeLoop(iterator I) {
568 assert(I != end() && "Cannot remove end iterator!");
570 assert(!L->getParentLoop() && "Not a top-level loop!");
571 TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
575 /// changeLoopFor - Change the top-level loop that contains BB to the
576 /// specified loop. This should be used by transformations that restructure
577 /// the loop hierarchy tree.
578 void changeLoopFor(BlockT *BB, LoopT *L) {
586 /// changeTopLevelLoop - Replace the specified loop in the top-level loops
587 /// list with the indicated loop.
588 void changeTopLevelLoop(LoopT *OldLoop,
590 auto I = std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
591 assert(I != TopLevelLoops.end() && "Old loop not at top level!");
593 assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
594 "Loops already embedded into a subloop!");
597 /// addTopLevelLoop - This adds the specified loop to the collection of
599 void addTopLevelLoop(LoopT *New) {
600 assert(!New->getParentLoop() && "Loop already in subloop!");
601 TopLevelLoops.push_back(New);
604 /// removeBlock - This method completely removes BB from all data structures,
605 /// including all of the Loop objects it is nested in and our mapping from
606 /// BasicBlocks to loops.
607 void removeBlock(BlockT *BB) {
608 auto I = BBMap.find(BB);
609 if (I != BBMap.end()) {
610 for (LoopT *L = I->second; L; L = L->getParentLoop())
611 L->removeBlockFromLoop(BB);
619 static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
620 const LoopT *ParentLoop) {
621 if (!SubLoop) return true;
622 if (SubLoop == ParentLoop) return false;
623 return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
626 /// Create the loop forest using a stable algorithm.
627 void Analyze(DominatorTreeBase<BlockT> &DomTree);
630 void print(raw_ostream &OS) const;
635 // Implementation in LoopInfoImpl.h
637 __extension__ extern template class LoopInfoBase<BasicBlock, Loop>;
640 class LoopInfo : public LoopInfoBase<BasicBlock, Loop> {
641 typedef LoopInfoBase<BasicBlock, Loop> BaseT;
643 friend class LoopBase<BasicBlock, Loop>;
645 void operator=(const LoopInfo &) = delete;
646 LoopInfo(const LoopInfo &) = delete;
650 LoopInfo(LoopInfo &&Arg) : BaseT(std::move(static_cast<BaseT &>(Arg))) {}
651 LoopInfo &operator=(LoopInfo &&RHS) {
652 BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
656 // Most of the public interface is provided via LoopInfoBase.
658 /// updateUnloop - Update LoopInfo after removing the last backedge from a
659 /// loop--now the "unloop". This updates the loop forest and parent loops for
660 /// each block so that Unloop is no longer referenced, but the caller must
661 /// actually delete the Unloop object.
662 void updateUnloop(Loop *Unloop);
664 /// replacementPreservesLCSSAForm - Returns true if replacing From with To
665 /// everywhere is guaranteed to preserve LCSSA form.
666 bool replacementPreservesLCSSAForm(Instruction *From, Value *To) {
667 // Preserving LCSSA form is only problematic if the replacing value is an
669 Instruction *I = dyn_cast<Instruction>(To);
671 // If both instructions are defined in the same basic block then replacement
672 // cannot break LCSSA form.
673 if (I->getParent() == From->getParent())
675 // If the instruction is not defined in a loop then it can safely replace
677 Loop *ToLoop = getLoopFor(I->getParent());
678 if (!ToLoop) return true;
679 // If the replacing instruction is defined in the same loop as the original
680 // instruction, or in a loop that contains it as an inner loop, then using
681 // it as a replacement will not break LCSSA form.
682 return ToLoop->contains(getLoopFor(From->getParent()));
686 // Allow clients to walk the list of nested loops...
687 template <> struct GraphTraits<const Loop*> {
688 typedef const Loop NodeType;
689 typedef LoopInfo::iterator ChildIteratorType;
691 static NodeType *getEntryNode(const Loop *L) { return L; }
692 static inline ChildIteratorType child_begin(NodeType *N) {
695 static inline ChildIteratorType child_end(NodeType *N) {
700 template <> struct GraphTraits<Loop*> {
701 typedef Loop NodeType;
702 typedef LoopInfo::iterator ChildIteratorType;
704 static NodeType *getEntryNode(Loop *L) { return L; }
705 static inline ChildIteratorType child_begin(NodeType *N) {
708 static inline ChildIteratorType child_end(NodeType *N) {
713 /// \brief Analysis pass that exposes the \c LoopInfo for a function.
718 typedef LoopInfo Result;
720 /// \brief Opaque, unique identifier for this analysis pass.
721 static void *ID() { return (void *)&PassID; }
723 /// \brief Provide a name for the analysis for debugging and logging.
724 static StringRef name() { return "LoopAnalysis"; }
726 LoopInfo run(Function &F, AnalysisManager<Function> *AM);
729 /// \brief Printer pass for the \c LoopAnalysis results.
730 class LoopPrinterPass {
734 explicit LoopPrinterPass(raw_ostream &OS) : OS(OS) {}
735 PreservedAnalyses run(Function &F, AnalysisManager<Function> *AM);
737 static StringRef name() { return "LoopPrinterPass"; }
740 /// \brief The legacy pass manager's analysis pass to compute loop information.
741 class LoopInfoWrapperPass : public FunctionPass {
745 static char ID; // Pass identification, replacement for typeid
747 LoopInfoWrapperPass() : FunctionPass(ID) {
748 initializeLoopInfoWrapperPassPass(*PassRegistry::getPassRegistry());
751 LoopInfo &getLoopInfo() { return LI; }
752 const LoopInfo &getLoopInfo() const { return LI; }
754 /// \brief Calculate the natural loop information for a given function.
755 bool runOnFunction(Function &F) override;
757 void verifyAnalysis() const override;
759 void releaseMemory() override { LI.releaseMemory(); }
761 void print(raw_ostream &O, const Module *M = nullptr) const override;
763 void getAnalysisUsage(AnalysisUsage &AU) const override;