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 // Note that this analysis specifically identifies *Loops* not cycles or SCCs
29 // in the CFG. There can be strongly connected compontents 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.
34 //===----------------------------------------------------------------------===//
36 #ifndef LLVM_ANALYSIS_LOOPINFO_H
37 #define LLVM_ANALYSIS_LOOPINFO_H
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"
59 template<class N> class DominatorTreeBase;
60 template<class N, class M> class LoopInfoBase;
61 template<class N, class M> class LoopBase;
63 //===----------------------------------------------------------------------===//
64 /// Instances of this class are used to represent loops that are detected in the
67 template<class BlockT, class LoopT>
70 // Loops contained entirely within this one.
71 std::vector<LoopT *> SubLoops;
73 // The list of blocks in this loop. First entry is the header node.
74 std::vector<BlockT*> Blocks;
76 SmallPtrSet<const BlockT*, 8> DenseBlockSet;
78 /// Indicator that this loop is no longer a valid loop.
79 bool IsInvalid = false;
81 LoopBase(const LoopBase<BlockT, LoopT> &) = delete;
82 const LoopBase<BlockT, LoopT>&
83 operator=(const LoopBase<BlockT, LoopT> &) = delete;
85 /// This creates an empty loop.
86 LoopBase() : ParentLoop(nullptr) {}
88 for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
92 /// Return the nesting level of this loop. An outer-most loop has depth 1,
93 /// for consistency with loop depth values used for basic blocks, where depth
94 /// 0 is used for blocks not inside any loops.
95 unsigned getLoopDepth() const {
97 for (const LoopT *CurLoop = ParentLoop; CurLoop;
98 CurLoop = CurLoop->ParentLoop)
102 BlockT *getHeader() const { return Blocks.front(); }
103 LoopT *getParentLoop() const { return ParentLoop; }
105 /// This is a raw interface for bypassing addChildLoop.
106 void setParentLoop(LoopT *L) { ParentLoop = L; }
108 /// Return true if the specified loop is contained within in this loop.
109 bool contains(const LoopT *L) const {
110 if (L == this) return true;
111 if (!L) return false;
112 return contains(L->getParentLoop());
115 /// Return true if the specified basic block is in this loop.
116 bool contains(const BlockT *BB) const {
117 return DenseBlockSet.count(BB);
120 /// Return true if the specified instruction is in this loop.
121 template<class InstT>
122 bool contains(const InstT *Inst) const {
123 return contains(Inst->getParent());
126 /// Return the loops contained entirely within this loop.
127 const std::vector<LoopT *> &getSubLoops() const { return SubLoops; }
128 std::vector<LoopT *> &getSubLoopsVector() { return SubLoops; }
129 typedef typename std::vector<LoopT *>::const_iterator iterator;
130 typedef typename std::vector<LoopT *>::const_reverse_iterator
132 iterator begin() const { return SubLoops.begin(); }
133 iterator end() const { return SubLoops.end(); }
134 reverse_iterator rbegin() const { return SubLoops.rbegin(); }
135 reverse_iterator rend() const { return SubLoops.rend(); }
136 bool empty() const { return SubLoops.empty(); }
138 /// 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(); }
143 inline iterator_range<block_iterator> blocks() const {
144 return make_range(block_begin(), block_end());
147 /// Get the number of blocks in this loop in constant time.
148 unsigned getNumBlocks() const {
149 return Blocks.size();
152 /// Invalidate the loop, indicating that it is no longer a loop.
153 void invalidate() { IsInvalid = true; }
155 /// Return true if this loop is no longer valid.
156 bool isInvalid() { return IsInvalid; }
158 /// True if terminator in the block can branch to another block that is
159 /// outside of the current loop.
160 bool isLoopExiting(const BlockT *BB) const {
161 typedef GraphTraits<const BlockT*> BlockTraits;
162 for (typename BlockTraits::ChildIteratorType SI =
163 BlockTraits::child_begin(BB),
164 SE = BlockTraits::child_end(BB); SI != SE; ++SI) {
171 /// Calculate the number of back edges to the loop header.
172 unsigned getNumBackEdges() const {
173 unsigned NumBackEdges = 0;
174 BlockT *H = getHeader();
176 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
177 for (typename InvBlockTraits::ChildIteratorType I =
178 InvBlockTraits::child_begin(H),
179 E = InvBlockTraits::child_end(H); I != E; ++I)
186 //===--------------------------------------------------------------------===//
187 // APIs for simple analysis of the loop.
189 // Note that all of these methods can fail on general loops (ie, there may not
190 // be a preheader, etc). For best success, the loop simplification and
191 // induction variable canonicalization pass should be used to normalize loops
192 // for easy analysis. These methods assume canonical loops.
194 /// Return all blocks inside the loop that have successors outside of the
195 /// loop. These are the blocks _inside of the current loop_ which branch out.
196 /// The returned list is always unique.
197 void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const;
199 /// If getExitingBlocks would return exactly one block, return that block.
200 /// Otherwise return null.
201 BlockT *getExitingBlock() const;
203 /// Return all of the successor blocks of this loop. These are the blocks
204 /// _outside of the current loop_ which are branched to.
205 void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const;
207 /// If getExitBlocks would return exactly one block, return that block.
208 /// Otherwise return null.
209 BlockT *getExitBlock() const;
212 typedef std::pair<const BlockT*, const BlockT*> Edge;
214 /// Return all pairs of (_inside_block_,_outside_block_).
215 void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const;
217 /// If there is a preheader for this loop, return it. A loop has a preheader
218 /// if there is only one edge to the header of the loop from outside of the
219 /// loop. If this is the case, the block branching to the header of the loop
220 /// is the preheader node.
222 /// This method returns null if there is no preheader for the loop.
223 BlockT *getLoopPreheader() const;
225 /// If the given loop's header has exactly one unique predecessor outside the
226 /// loop, return it. Otherwise return null.
227 /// This is less strict that the loop "preheader" concept, which requires
228 /// the predecessor to have exactly one successor.
229 BlockT *getLoopPredecessor() const;
231 /// If there is a single latch block for this loop, return it.
232 /// A latch block is a block that contains a branch back to the header.
233 BlockT *getLoopLatch() const;
235 /// Return all loop latch blocks of this loop. A latch block is a block that
236 /// contains a branch back to the header.
237 void getLoopLatches(SmallVectorImpl<BlockT *> &LoopLatches) const {
238 BlockT *H = getHeader();
239 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
240 for (typename InvBlockTraits::ChildIteratorType I =
241 InvBlockTraits::child_begin(H),
242 E = InvBlockTraits::child_end(H); I != E; ++I)
244 LoopLatches.push_back(*I);
247 //===--------------------------------------------------------------------===//
248 // APIs for updating loop information after changing the CFG
251 /// This method is used by other analyses to update loop information.
252 /// NewBB is set to be a new member of the current loop.
253 /// Because of this, it is added as a member of all parent loops, and is added
254 /// to the specified LoopInfo object as being in the current basic block. It
255 /// is not valid to replace the loop header with this method.
256 void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
258 /// This is used when splitting loops up. It replaces the OldChild entry in
259 /// our children list with NewChild, and updates the parent pointer of
260 /// OldChild to be null and the NewChild to be this loop.
261 /// This updates the loop depth of the new child.
262 void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild);
264 /// Add the specified loop to be a child of this loop.
265 /// This updates the loop depth of the new child.
266 void addChildLoop(LoopT *NewChild) {
267 assert(!NewChild->ParentLoop && "NewChild already has a parent!");
268 NewChild->ParentLoop = static_cast<LoopT *>(this);
269 SubLoops.push_back(NewChild);
272 /// This removes the specified child from being a subloop of this loop. The
273 /// loop is not deleted, as it will presumably be inserted 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 /// 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 /// 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 /// interface to do reserve() for Blocks
297 void reserveBlocks(unsigned size) {
298 Blocks.reserve(size);
301 /// This method is used to move BB (which must be part of this loop) to be the
302 /// loop header of the loop (the block that dominates all others).
303 void moveToHeader(BlockT *BB) {
304 if (Blocks[0] == BB) return;
305 for (unsigned i = 0; ; ++i) {
306 assert(i != Blocks.size() && "Loop does not contain BB!");
307 if (Blocks[i] == BB) {
308 Blocks[i] = Blocks[0];
315 /// This removes the specified basic block from the current loop, updating the
316 /// Blocks as appropriate. This does not update the mapping in the LoopInfo
318 void removeBlockFromLoop(BlockT *BB) {
319 auto I = std::find(Blocks.begin(), Blocks.end(), BB);
320 assert(I != Blocks.end() && "N is not in this list!");
323 DenseBlockSet.erase(BB);
326 /// Verify loop structure
327 void verifyLoop() const;
329 /// Verify loop structure of this loop and all nested loops.
330 void verifyLoopNest(DenseSet<const LoopT*> *Loops) const;
332 void print(raw_ostream &OS, unsigned Depth = 0) const;
335 friend class LoopInfoBase<BlockT, LoopT>;
336 explicit LoopBase(BlockT *BB) : ParentLoop(nullptr) {
337 Blocks.push_back(BB);
338 DenseBlockSet.insert(BB);
342 template<class BlockT, class LoopT>
343 raw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
348 // Implementation in LoopInfoImpl.h
349 extern template class LoopBase<BasicBlock, Loop>;
352 /// Represents a single loop in the control flow graph. Note that not all SCCs
353 /// in the CFG are neccessarily loops.
354 class Loop : public LoopBase<BasicBlock, Loop> {
358 /// Return true if the specified value is loop invariant.
359 bool isLoopInvariant(const Value *V) const;
361 /// Return true if all the operands of the specified instruction are loop
363 bool hasLoopInvariantOperands(const Instruction *I) const;
365 /// If the given value is an instruction inside of the loop and it can be
366 /// hoisted, do so to make it trivially loop-invariant.
367 /// Return true if the value after any hoisting is loop invariant. This
368 /// function can be used as a slightly more aggressive replacement for
371 /// If InsertPt is specified, it is the point to hoist instructions to.
372 /// If null, the terminator of the loop preheader is used.
373 bool makeLoopInvariant(Value *V, bool &Changed,
374 Instruction *InsertPt = nullptr) const;
376 /// If the given instruction is inside of the loop and it can be hoisted, do
377 /// so to make it trivially loop-invariant.
378 /// Return true if the instruction after any hoisting is loop invariant. This
379 /// function can be used as a slightly more aggressive replacement for
382 /// If InsertPt is specified, it is the point to hoist instructions to.
383 /// If null, the terminator of the loop preheader is used.
385 bool makeLoopInvariant(Instruction *I, bool &Changed,
386 Instruction *InsertPt = nullptr) const;
388 /// Check to see if the loop has a canonical induction variable: an integer
389 /// recurrence that starts at 0 and increments by one each time through the
390 /// loop. If so, return the phi node that corresponds to it.
392 /// The IndVarSimplify pass transforms loops to have a canonical induction
395 PHINode *getCanonicalInductionVariable() const;
397 /// Return true if the Loop is in LCSSA form.
398 bool isLCSSAForm(DominatorTree &DT) const;
400 /// Return true if this Loop and all inner subloops are in LCSSA form.
401 bool isRecursivelyLCSSAForm(DominatorTree &DT) const;
403 /// Return true if the Loop is in the form that the LoopSimplify form
404 /// transforms loops to, which is sometimes called normal form.
405 bool isLoopSimplifyForm() const;
407 /// Return true if the loop body is safe to clone in practice.
408 bool isSafeToClone() const;
410 /// Returns true if the loop is annotated parallel.
412 /// A parallel loop can be assumed to not contain any dependencies between
413 /// iterations by the compiler. That is, any loop-carried dependency checking
414 /// can be skipped completely when parallelizing the loop on the target
415 /// machine. Thus, if the parallel loop information originates from the
416 /// programmer, e.g. via the OpenMP parallel for pragma, it is the
417 /// programmer's responsibility to ensure there are no loop-carried
418 /// dependencies. The final execution order of the instructions across
419 /// iterations is not guaranteed, thus, the end result might or might not
420 /// implement actual concurrent execution of instructions across multiple
422 bool isAnnotatedParallel() const;
424 /// Return the llvm.loop loop id metadata node for this loop if it is present.
426 /// If this loop contains the same llvm.loop metadata on each branch to the
427 /// header then the node is returned. If any latch instruction does not
428 /// contain llvm.loop or or if multiple latches contain different nodes then
430 MDNode *getLoopID() const;
431 /// Set the llvm.loop loop id metadata for this loop.
433 /// The LoopID metadata node will be added to each terminator instruction in
434 /// the loop that branches to the loop header.
436 /// The LoopID metadata node should have one or more operands and the first
437 /// operand should should be the node itself.
438 void setLoopID(MDNode *LoopID) const;
440 /// Return true if no exit block for the loop has a predecessor that is
441 /// outside the loop.
442 bool hasDedicatedExits() const;
444 /// Return all unique successor blocks of this loop.
445 /// These are the blocks _outside of the current loop_ which are branched to.
446 /// This assumes that loop exits are in canonical form.
447 void getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const;
449 /// If getUniqueExitBlocks would return exactly one block, return that block.
450 /// Otherwise return null.
451 BasicBlock *getUniqueExitBlock() const;
455 /// Return the debug location of the start of this loop.
456 /// This looks for a BB terminating instruction with a known debug
457 /// location by looking at the preheader and header blocks. If it
458 /// cannot find a terminating instruction with location information,
459 /// it returns an unknown location.
460 DebugLoc getStartLoc() const;
462 StringRef getName() const {
463 if (BasicBlock *Header = getHeader())
464 if (Header->hasName())
465 return Header->getName();
466 return "<unnamed loop>";
470 friend class LoopInfoBase<BasicBlock, Loop>;
471 explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
474 //===----------------------------------------------------------------------===//
475 /// This class builds and contains all of the top-level loop
476 /// structures in the specified function.
479 template<class BlockT, class LoopT>
481 // BBMap - Mapping of basic blocks to the inner most loop they occur in
482 DenseMap<const BlockT *, LoopT *> BBMap;
483 std::vector<LoopT *> TopLevelLoops;
484 std::vector<LoopT *> RemovedLoops;
486 friend class LoopBase<BlockT, LoopT>;
487 friend class LoopInfo;
489 void operator=(const LoopInfoBase &) = delete;
490 LoopInfoBase(const LoopInfoBase &) = delete;
493 ~LoopInfoBase() { releaseMemory(); }
495 LoopInfoBase(LoopInfoBase &&Arg)
496 : BBMap(std::move(Arg.BBMap)),
497 TopLevelLoops(std::move(Arg.TopLevelLoops)) {
498 // We have to clear the arguments top level loops as we've taken ownership.
499 Arg.TopLevelLoops.clear();
501 LoopInfoBase &operator=(LoopInfoBase &&RHS) {
502 BBMap = std::move(RHS.BBMap);
504 for (auto *L : TopLevelLoops)
506 TopLevelLoops = std::move(RHS.TopLevelLoops);
507 RHS.TopLevelLoops.clear();
511 void releaseMemory() {
514 for (auto *L : TopLevelLoops)
516 TopLevelLoops.clear();
517 for (auto *L : RemovedLoops)
519 RemovedLoops.clear();
522 /// iterator/begin/end - The interface to the top-level loops in the current
525 typedef typename std::vector<LoopT *>::const_iterator iterator;
526 typedef typename std::vector<LoopT *>::const_reverse_iterator
528 iterator begin() const { return TopLevelLoops.begin(); }
529 iterator end() const { return TopLevelLoops.end(); }
530 reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
531 reverse_iterator rend() const { return TopLevelLoops.rend(); }
532 bool empty() const { return TopLevelLoops.empty(); }
534 /// Return the inner most loop that BB lives in. If a basic block is in no
535 /// loop (for example the entry node), null is returned.
536 LoopT *getLoopFor(const BlockT *BB) const { return BBMap.lookup(BB); }
538 /// Same as getLoopFor.
539 const LoopT *operator[](const BlockT *BB) const {
540 return getLoopFor(BB);
543 /// Return the loop nesting level of the specified block. A depth of 0 means
544 /// the block is not inside any loop.
545 unsigned getLoopDepth(const BlockT *BB) const {
546 const LoopT *L = getLoopFor(BB);
547 return L ? L->getLoopDepth() : 0;
550 // True if the block is a loop header node
551 bool isLoopHeader(const BlockT *BB) const {
552 const LoopT *L = getLoopFor(BB);
553 return L && L->getHeader() == BB;
556 /// This removes the specified top-level loop from this loop info object.
557 /// The loop is not deleted, as it will presumably be inserted into
559 LoopT *removeLoop(iterator I) {
560 assert(I != end() && "Cannot remove end iterator!");
562 assert(!L->getParentLoop() && "Not a top-level loop!");
563 TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
567 /// Change the top-level loop that contains BB to the specified loop.
568 /// This should be used by transformations that restructure the loop hierarchy
570 void changeLoopFor(BlockT *BB, LoopT *L) {
578 /// Replace the specified loop in the top-level loops list with the indicated
580 void changeTopLevelLoop(LoopT *OldLoop,
582 auto I = std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
583 assert(I != TopLevelLoops.end() && "Old loop not at top level!");
585 assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
586 "Loops already embedded into a subloop!");
589 /// This adds the specified loop to the collection of top-level loops.
590 void addTopLevelLoop(LoopT *New) {
591 assert(!New->getParentLoop() && "Loop already in subloop!");
592 TopLevelLoops.push_back(New);
595 /// This method completely removes BB from all data structures,
596 /// including all of the Loop objects it is nested in and our mapping from
597 /// BasicBlocks to loops.
598 void removeBlock(BlockT *BB) {
599 auto I = BBMap.find(BB);
600 if (I != BBMap.end()) {
601 for (LoopT *L = I->second; L; L = L->getParentLoop())
602 L->removeBlockFromLoop(BB);
610 static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
611 const LoopT *ParentLoop) {
612 if (!SubLoop) return true;
613 if (SubLoop == ParentLoop) return false;
614 return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
617 /// Create the loop forest using a stable algorithm.
618 void analyze(const DominatorTreeBase<BlockT> &DomTree);
621 void print(raw_ostream &OS) const;
626 // Implementation in LoopInfoImpl.h
627 extern template class LoopInfoBase<BasicBlock, Loop>;
629 class LoopInfo : public LoopInfoBase<BasicBlock, Loop> {
630 typedef LoopInfoBase<BasicBlock, Loop> BaseT;
632 friend class LoopBase<BasicBlock, Loop>;
634 void operator=(const LoopInfo &) = delete;
635 LoopInfo(const LoopInfo &) = delete;
638 explicit LoopInfo(const DominatorTreeBase<BasicBlock> &DomTree);
640 LoopInfo(LoopInfo &&Arg) : BaseT(std::move(static_cast<BaseT &>(Arg))) {}
641 LoopInfo &operator=(LoopInfo &&RHS) {
642 BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
646 // Most of the public interface is provided via LoopInfoBase.
648 /// Update LoopInfo after removing the last backedge from a loop. This updates
649 /// the loop forest and parent loops for each block so that \c L is no longer
650 /// referenced, but does not actually delete \c L immediately. The pointer
651 /// will remain valid until this LoopInfo's memory is released.
652 void markAsRemoved(Loop *L);
654 /// Returns true if replacing From with To everywhere is guaranteed to
655 /// preserve LCSSA form.
656 bool replacementPreservesLCSSAForm(Instruction *From, Value *To) {
657 // Preserving LCSSA form is only problematic if the replacing value is an
659 Instruction *I = dyn_cast<Instruction>(To);
661 // If both instructions are defined in the same basic block then replacement
662 // cannot break LCSSA form.
663 if (I->getParent() == From->getParent())
665 // If the instruction is not defined in a loop then it can safely replace
667 Loop *ToLoop = getLoopFor(I->getParent());
668 if (!ToLoop) return true;
669 // If the replacing instruction is defined in the same loop as the original
670 // instruction, or in a loop that contains it as an inner loop, then using
671 // it as a replacement will not break LCSSA form.
672 return ToLoop->contains(getLoopFor(From->getParent()));
675 /// Checks if moving a specific instruction can break LCSSA in any loop.
677 /// Return true if moving \p Inst to before \p NewLoc will break LCSSA,
678 /// assuming that the function containing \p Inst and \p NewLoc is currently
680 bool movementPreservesLCSSAForm(Instruction *Inst, Instruction *NewLoc) {
681 assert(Inst->getFunction() == NewLoc->getFunction() &&
682 "Can't reason about IPO!");
684 auto *OldBB = Inst->getParent();
685 auto *NewBB = NewLoc->getParent();
687 // Movement within the same loop does not break LCSSA (the equality check is
688 // to avoid doing a hashtable lookup in case of intra-block movement).
692 auto *OldLoop = getLoopFor(OldBB);
693 auto *NewLoop = getLoopFor(NewBB);
695 if (OldLoop == NewLoop)
698 // Check if Outer contains Inner; with the null loop counting as the
700 auto Contains = [](const Loop *Outer, const Loop *Inner) {
701 return !Outer || Outer->contains(Inner);
704 // To check that the movement of Inst to before NewLoc does not break LCSSA,
705 // we need to check two sets of uses for possible LCSSA violations at
706 // NewLoc: the users of NewInst, and the operands of NewInst.
708 // If we know we're hoisting Inst out of an inner loop to an outer loop,
709 // then the uses *of* Inst don't need to be checked.
711 if (!Contains(NewLoop, OldLoop)) {
712 for (Use &U : Inst->uses()) {
713 auto *UI = cast<Instruction>(U.getUser());
714 auto *UBB = isa<PHINode>(UI) ? cast<PHINode>(UI)->getIncomingBlock(U)
716 if (UBB != NewBB && getLoopFor(UBB) != NewLoop)
721 // If we know we're sinking Inst from an outer loop into an inner loop, then
722 // the *operands* of Inst don't need to be checked.
724 if (!Contains(OldLoop, NewLoop)) {
725 // See below on why we can't handle phi nodes here.
726 if (isa<PHINode>(Inst))
729 for (Use &U : Inst->operands()) {
730 auto *DefI = dyn_cast<Instruction>(U.get());
734 // This would need adjustment if we allow Inst to be a phi node -- the
735 // new use block won't simply be NewBB.
737 auto *DefBlock = DefI->getParent();
738 if (DefBlock != NewBB && getLoopFor(DefBlock) != NewLoop)
747 // Allow clients to walk the list of nested loops...
748 template <> struct GraphTraits<const Loop*> {
749 typedef const Loop NodeType;
750 typedef LoopInfo::iterator ChildIteratorType;
752 static NodeType *getEntryNode(const Loop *L) { return L; }
753 static inline ChildIteratorType child_begin(NodeType *N) {
756 static inline ChildIteratorType child_end(NodeType *N) {
761 template <> struct GraphTraits<Loop*> {
762 typedef Loop NodeType;
763 typedef LoopInfo::iterator ChildIteratorType;
765 static NodeType *getEntryNode(Loop *L) { return L; }
766 static inline ChildIteratorType child_begin(NodeType *N) {
769 static inline ChildIteratorType child_end(NodeType *N) {
774 /// \brief Analysis pass that exposes the \c LoopInfo for a function.
775 class LoopAnalysis : public AnalysisInfoMixin<LoopAnalysis> {
776 friend AnalysisInfoMixin<LoopAnalysis>;
780 typedef LoopInfo Result;
782 LoopInfo run(Function &F, AnalysisManager<Function> &AM);
785 /// \brief Printer pass for the \c LoopAnalysis results.
786 class LoopPrinterPass : public PassInfoMixin<LoopPrinterPass> {
790 explicit LoopPrinterPass(raw_ostream &OS) : OS(OS) {}
791 PreservedAnalyses run(Function &F, AnalysisManager<Function> &AM);
794 /// \brief The legacy pass manager's analysis pass to compute loop information.
795 class LoopInfoWrapperPass : public FunctionPass {
799 static char ID; // Pass identification, replacement for typeid
801 LoopInfoWrapperPass() : FunctionPass(ID) {
802 initializeLoopInfoWrapperPassPass(*PassRegistry::getPassRegistry());
805 LoopInfo &getLoopInfo() { return LI; }
806 const LoopInfo &getLoopInfo() const { return LI; }
808 /// \brief Calculate the natural loop information for a given function.
809 bool runOnFunction(Function &F) override;
811 void verifyAnalysis() const override;
813 void releaseMemory() override { LI.releaseMemory(); }
815 void print(raw_ostream &O, const Module *M = nullptr) const override;
817 void getAnalysisUsage(AnalysisUsage &AU) const override;
820 /// \brief Pass for printing a loop's contents as LLVM's text IR assembly.
821 class PrintLoopPass : public PassInfoMixin<PrintLoopPass> {
827 PrintLoopPass(raw_ostream &OS, const std::string &Banner = "");
829 PreservedAnalyses run(Loop &L, AnalysisManager<Loop> &);
832 } // End llvm namespace