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 /// Returns true if \p BB is a loop-latch.
172 /// A latch block is a block that contains a branch back to the header.
173 /// This function is useful when there are multiple latches in a loop
174 /// because \fn getLoopLatch will return nullptr in that case.
175 bool isLoopLatch(const BlockT *BB) const {
176 assert(contains(BB) && "block does not belong to the loop");
178 BlockT *Header = getHeader();
179 auto PredBegin = GraphTraits<Inverse<BlockT*> >::child_begin(Header);
180 auto PredEnd = GraphTraits<Inverse<BlockT*> >::child_end(Header);
181 return std::find(PredBegin, PredEnd, BB) != PredEnd;
184 /// Calculate the number of back edges to the loop header.
185 unsigned getNumBackEdges() const {
186 unsigned NumBackEdges = 0;
187 BlockT *H = getHeader();
189 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
190 for (typename InvBlockTraits::ChildIteratorType I =
191 InvBlockTraits::child_begin(H),
192 E = InvBlockTraits::child_end(H); I != E; ++I)
199 //===--------------------------------------------------------------------===//
200 // APIs for simple analysis of the loop.
202 // Note that all of these methods can fail on general loops (ie, there may not
203 // be a preheader, etc). For best success, the loop simplification and
204 // induction variable canonicalization pass should be used to normalize loops
205 // for easy analysis. These methods assume canonical loops.
207 /// Return all blocks inside the loop that have successors outside of the
208 /// loop. These are the blocks _inside of the current loop_ which branch out.
209 /// The returned list is always unique.
210 void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const;
212 /// If getExitingBlocks would return exactly one block, return that block.
213 /// Otherwise return null.
214 BlockT *getExitingBlock() const;
216 /// Return all of the successor blocks of this loop. These are the blocks
217 /// _outside of the current loop_ which are branched to.
218 void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const;
220 /// If getExitBlocks would return exactly one block, return that block.
221 /// Otherwise return null.
222 BlockT *getExitBlock() const;
225 typedef std::pair<const BlockT*, const BlockT*> Edge;
227 /// Return all pairs of (_inside_block_,_outside_block_).
228 void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const;
230 /// If there is a preheader for this loop, return it. A loop has a preheader
231 /// if there is only one edge to the header of the loop from outside of the
232 /// loop. If this is the case, the block branching to the header of the loop
233 /// is the preheader node.
235 /// This method returns null if there is no preheader for the loop.
236 BlockT *getLoopPreheader() const;
238 /// If the given loop's header has exactly one unique predecessor outside the
239 /// loop, return it. Otherwise return null.
240 /// This is less strict that the loop "preheader" concept, which requires
241 /// the predecessor to have exactly one successor.
242 BlockT *getLoopPredecessor() const;
244 /// If there is a single latch block for this loop, return it.
245 /// A latch block is a block that contains a branch back to the header.
246 BlockT *getLoopLatch() const;
248 /// Return all loop latch blocks of this loop. A latch block is a block that
249 /// contains a branch back to the header.
250 void getLoopLatches(SmallVectorImpl<BlockT *> &LoopLatches) const {
251 BlockT *H = getHeader();
252 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
253 for (typename InvBlockTraits::ChildIteratorType I =
254 InvBlockTraits::child_begin(H),
255 E = InvBlockTraits::child_end(H); I != E; ++I)
257 LoopLatches.push_back(*I);
260 //===--------------------------------------------------------------------===//
261 // APIs for updating loop information after changing the CFG
264 /// This method is used by other analyses to update loop information.
265 /// NewBB is set to be a new member of the current loop.
266 /// Because of this, it is added as a member of all parent loops, and is added
267 /// to the specified LoopInfo object as being in the current basic block. It
268 /// is not valid to replace the loop header with this method.
269 void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
271 /// This is used when splitting loops up. It replaces the OldChild entry in
272 /// our children list with NewChild, and updates the parent pointer of
273 /// OldChild to be null and the NewChild to be this loop.
274 /// This updates the loop depth of the new child.
275 void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild);
277 /// Add the specified loop to be a child of this loop.
278 /// This updates the loop depth of the new child.
279 void addChildLoop(LoopT *NewChild) {
280 assert(!NewChild->ParentLoop && "NewChild already has a parent!");
281 NewChild->ParentLoop = static_cast<LoopT *>(this);
282 SubLoops.push_back(NewChild);
285 /// This removes the specified child from being a subloop of this loop. The
286 /// loop is not deleted, as it will presumably be inserted into another loop.
287 LoopT *removeChildLoop(iterator I) {
288 assert(I != SubLoops.end() && "Cannot remove end iterator!");
290 assert(Child->ParentLoop == this && "Child is not a child of this loop!");
291 SubLoops.erase(SubLoops.begin()+(I-begin()));
292 Child->ParentLoop = nullptr;
296 /// This adds a basic block directly to the basic block list.
297 /// This should only be used by transformations that create new loops. Other
298 /// transformations should use addBasicBlockToLoop.
299 void addBlockEntry(BlockT *BB) {
300 Blocks.push_back(BB);
301 DenseBlockSet.insert(BB);
304 /// interface to reverse Blocks[from, end of loop] in this loop
305 void reverseBlock(unsigned from) {
306 std::reverse(Blocks.begin() + from, Blocks.end());
309 /// interface to do reserve() for Blocks
310 void reserveBlocks(unsigned size) {
311 Blocks.reserve(size);
314 /// This method is used to move BB (which must be part of this loop) to be the
315 /// loop header of the loop (the block that dominates all others).
316 void moveToHeader(BlockT *BB) {
317 if (Blocks[0] == BB) return;
318 for (unsigned i = 0; ; ++i) {
319 assert(i != Blocks.size() && "Loop does not contain BB!");
320 if (Blocks[i] == BB) {
321 Blocks[i] = Blocks[0];
328 /// This removes the specified basic block from the current loop, updating the
329 /// Blocks as appropriate. This does not update the mapping in the LoopInfo
331 void removeBlockFromLoop(BlockT *BB) {
332 auto I = find(Blocks, BB);
333 assert(I != Blocks.end() && "N is not in this list!");
336 DenseBlockSet.erase(BB);
339 /// Verify loop structure
340 void verifyLoop() const;
342 /// Verify loop structure of this loop and all nested loops.
343 void verifyLoopNest(DenseSet<const LoopT*> *Loops) const;
345 /// Print loop with all the BBs inside it.
346 void print(raw_ostream &OS, unsigned Depth = 0, bool Verbose = false) const;
349 friend class LoopInfoBase<BlockT, LoopT>;
350 explicit LoopBase(BlockT *BB) : ParentLoop(nullptr) {
351 Blocks.push_back(BB);
352 DenseBlockSet.insert(BB);
356 template<class BlockT, class LoopT>
357 raw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
362 // Implementation in LoopInfoImpl.h
363 extern template class LoopBase<BasicBlock, Loop>;
366 /// Represents a single loop in the control flow graph. Note that not all SCCs
367 /// in the CFG are neccessarily loops.
368 class Loop : public LoopBase<BasicBlock, Loop> {
370 /// \brief A range representing the start and end location of a loop.
377 LocRange(DebugLoc Start) : Start(std::move(Start)), End(std::move(Start)) {}
378 LocRange(DebugLoc Start, DebugLoc End) : Start(std::move(Start)),
379 End(std::move(End)) {}
381 const DebugLoc &getStart() const { return Start; }
382 const DebugLoc &getEnd() const { return End; }
384 /// \brief Check for null.
386 explicit operator bool() const {
393 /// Return true if the specified value is loop invariant.
394 bool isLoopInvariant(const Value *V) const;
396 /// Return true if all the operands of the specified instruction are loop
398 bool hasLoopInvariantOperands(const Instruction *I) const;
400 /// If the given value is an instruction inside of the loop and it can be
401 /// hoisted, do so to make it trivially loop-invariant.
402 /// Return true if the value after any hoisting is loop invariant. This
403 /// function can be used as a slightly more aggressive replacement for
406 /// If InsertPt is specified, it is the point to hoist instructions to.
407 /// If null, the terminator of the loop preheader is used.
408 bool makeLoopInvariant(Value *V, bool &Changed,
409 Instruction *InsertPt = nullptr) const;
411 /// If the given instruction is inside of the loop and it can be hoisted, do
412 /// so to make it trivially loop-invariant.
413 /// Return true if the instruction after any hoisting is loop invariant. This
414 /// function can be used as a slightly more aggressive replacement for
417 /// If InsertPt is specified, it is the point to hoist instructions to.
418 /// If null, the terminator of the loop preheader is used.
420 bool makeLoopInvariant(Instruction *I, bool &Changed,
421 Instruction *InsertPt = nullptr) const;
423 /// Check to see if the loop has a canonical induction variable: an integer
424 /// recurrence that starts at 0 and increments by one each time through the
425 /// loop. If so, return the phi node that corresponds to it.
427 /// The IndVarSimplify pass transforms loops to have a canonical induction
430 PHINode *getCanonicalInductionVariable() const;
432 /// Return true if the Loop is in LCSSA form.
433 bool isLCSSAForm(DominatorTree &DT) const;
435 /// Return true if this Loop and all inner subloops are in LCSSA form.
436 bool isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const;
438 /// Return true if the Loop is in the form that the LoopSimplify form
439 /// transforms loops to, which is sometimes called normal form.
440 bool isLoopSimplifyForm() const;
442 /// Return true if the loop body is safe to clone in practice.
443 bool isSafeToClone() const;
445 /// Returns true if the loop is annotated parallel.
447 /// A parallel loop can be assumed to not contain any dependencies between
448 /// iterations by the compiler. That is, any loop-carried dependency checking
449 /// can be skipped completely when parallelizing the loop on the target
450 /// machine. Thus, if the parallel loop information originates from the
451 /// programmer, e.g. via the OpenMP parallel for pragma, it is the
452 /// programmer's responsibility to ensure there are no loop-carried
453 /// dependencies. The final execution order of the instructions across
454 /// iterations is not guaranteed, thus, the end result might or might not
455 /// implement actual concurrent execution of instructions across multiple
457 bool isAnnotatedParallel() const;
459 /// Return the llvm.loop loop id metadata node for this loop if it is present.
461 /// If this loop contains the same llvm.loop metadata on each branch to the
462 /// header then the node is returned. If any latch instruction does not
463 /// contain llvm.loop or or if multiple latches contain different nodes then
465 MDNode *getLoopID() const;
466 /// Set the llvm.loop loop id metadata for this loop.
468 /// The LoopID metadata node will be added to each terminator instruction in
469 /// the loop that branches to the loop header.
471 /// The LoopID metadata node should have one or more operands and the first
472 /// operand should should be the node itself.
473 void setLoopID(MDNode *LoopID) const;
475 /// Return true if no exit block for the loop has a predecessor that is
476 /// outside the loop.
477 bool hasDedicatedExits() const;
479 /// Return all unique successor blocks of this loop.
480 /// These are the blocks _outside of the current loop_ which are branched to.
481 /// This assumes that loop exits are in canonical form.
482 void getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const;
484 /// If getUniqueExitBlocks would return exactly one block, return that block.
485 /// Otherwise return null.
486 BasicBlock *getUniqueExitBlock() const;
489 void dumpVerbose() const;
491 /// Return the debug location of the start of this loop.
492 /// This looks for a BB terminating instruction with a known debug
493 /// location by looking at the preheader and header blocks. If it
494 /// cannot find a terminating instruction with location information,
495 /// it returns an unknown location.
496 DebugLoc getStartLoc() const;
498 /// Return the source code span of the loop.
499 LocRange getLocRange() const;
501 StringRef getName() const {
502 if (BasicBlock *Header = getHeader())
503 if (Header->hasName())
504 return Header->getName();
505 return "<unnamed loop>";
509 friend class LoopInfoBase<BasicBlock, Loop>;
510 explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
513 //===----------------------------------------------------------------------===//
514 /// This class builds and contains all of the top-level loop
515 /// structures in the specified function.
518 template<class BlockT, class LoopT>
520 // BBMap - Mapping of basic blocks to the inner most loop they occur in
521 DenseMap<const BlockT *, LoopT *> BBMap;
522 std::vector<LoopT *> TopLevelLoops;
523 std::vector<LoopT *> RemovedLoops;
525 friend class LoopBase<BlockT, LoopT>;
526 friend class LoopInfo;
528 void operator=(const LoopInfoBase &) = delete;
529 LoopInfoBase(const LoopInfoBase &) = delete;
532 ~LoopInfoBase() { releaseMemory(); }
534 LoopInfoBase(LoopInfoBase &&Arg)
535 : BBMap(std::move(Arg.BBMap)),
536 TopLevelLoops(std::move(Arg.TopLevelLoops)) {
537 // We have to clear the arguments top level loops as we've taken ownership.
538 Arg.TopLevelLoops.clear();
540 LoopInfoBase &operator=(LoopInfoBase &&RHS) {
541 BBMap = std::move(RHS.BBMap);
543 for (auto *L : TopLevelLoops)
545 TopLevelLoops = std::move(RHS.TopLevelLoops);
546 RHS.TopLevelLoops.clear();
550 void releaseMemory() {
553 for (auto *L : TopLevelLoops)
555 TopLevelLoops.clear();
556 for (auto *L : RemovedLoops)
558 RemovedLoops.clear();
561 /// iterator/begin/end - The interface to the top-level loops in the current
564 typedef typename std::vector<LoopT *>::const_iterator iterator;
565 typedef typename std::vector<LoopT *>::const_reverse_iterator
567 iterator begin() const { return TopLevelLoops.begin(); }
568 iterator end() const { return TopLevelLoops.end(); }
569 reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
570 reverse_iterator rend() const { return TopLevelLoops.rend(); }
571 bool empty() const { return TopLevelLoops.empty(); }
573 /// Return the inner most loop that BB lives in. If a basic block is in no
574 /// loop (for example the entry node), null is returned.
575 LoopT *getLoopFor(const BlockT *BB) const { return BBMap.lookup(BB); }
577 /// Same as getLoopFor.
578 const LoopT *operator[](const BlockT *BB) const {
579 return getLoopFor(BB);
582 /// Return the loop nesting level of the specified block. A depth of 0 means
583 /// the block is not inside any loop.
584 unsigned getLoopDepth(const BlockT *BB) const {
585 const LoopT *L = getLoopFor(BB);
586 return L ? L->getLoopDepth() : 0;
589 // True if the block is a loop header node
590 bool isLoopHeader(const BlockT *BB) const {
591 const LoopT *L = getLoopFor(BB);
592 return L && L->getHeader() == BB;
595 /// This removes the specified top-level loop from this loop info object.
596 /// The loop is not deleted, as it will presumably be inserted into
598 LoopT *removeLoop(iterator I) {
599 assert(I != end() && "Cannot remove end iterator!");
601 assert(!L->getParentLoop() && "Not a top-level loop!");
602 TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
606 /// Change the top-level loop that contains BB to the specified loop.
607 /// This should be used by transformations that restructure the loop hierarchy
609 void changeLoopFor(BlockT *BB, LoopT *L) {
617 /// Replace the specified loop in the top-level loops list with the indicated
619 void changeTopLevelLoop(LoopT *OldLoop,
621 auto I = find(TopLevelLoops, OldLoop);
622 assert(I != TopLevelLoops.end() && "Old loop not at top level!");
624 assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
625 "Loops already embedded into a subloop!");
628 /// This adds the specified loop to the collection of top-level loops.
629 void addTopLevelLoop(LoopT *New) {
630 assert(!New->getParentLoop() && "Loop already in subloop!");
631 TopLevelLoops.push_back(New);
634 /// This method completely removes BB from all data structures,
635 /// including all of the Loop objects it is nested in and our mapping from
636 /// BasicBlocks to loops.
637 void removeBlock(BlockT *BB) {
638 auto I = BBMap.find(BB);
639 if (I != BBMap.end()) {
640 for (LoopT *L = I->second; L; L = L->getParentLoop())
641 L->removeBlockFromLoop(BB);
649 static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
650 const LoopT *ParentLoop) {
651 if (!SubLoop) return true;
652 if (SubLoop == ParentLoop) return false;
653 return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
656 /// Create the loop forest using a stable algorithm.
657 void analyze(const DominatorTreeBase<BlockT> &DomTree);
660 void print(raw_ostream &OS) const;
662 void verify(const DominatorTreeBase<BlockT> &DomTree) const;
665 // Implementation in LoopInfoImpl.h
666 extern template class LoopInfoBase<BasicBlock, Loop>;
668 class LoopInfo : public LoopInfoBase<BasicBlock, Loop> {
669 typedef LoopInfoBase<BasicBlock, Loop> BaseT;
671 friend class LoopBase<BasicBlock, Loop>;
673 void operator=(const LoopInfo &) = delete;
674 LoopInfo(const LoopInfo &) = delete;
677 explicit LoopInfo(const DominatorTreeBase<BasicBlock> &DomTree);
679 LoopInfo(LoopInfo &&Arg) : BaseT(std::move(static_cast<BaseT &>(Arg))) {}
680 LoopInfo &operator=(LoopInfo &&RHS) {
681 BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
685 // Most of the public interface is provided via LoopInfoBase.
687 /// Update LoopInfo after removing the last backedge from a loop. This updates
688 /// the loop forest and parent loops for each block so that \c L is no longer
689 /// referenced, but does not actually delete \c L immediately. The pointer
690 /// will remain valid until this LoopInfo's memory is released.
691 void markAsRemoved(Loop *L);
693 /// Returns true if replacing From with To everywhere is guaranteed to
694 /// preserve LCSSA form.
695 bool replacementPreservesLCSSAForm(Instruction *From, Value *To) {
696 // Preserving LCSSA form is only problematic if the replacing value is an
698 Instruction *I = dyn_cast<Instruction>(To);
700 // If both instructions are defined in the same basic block then replacement
701 // cannot break LCSSA form.
702 if (I->getParent() == From->getParent())
704 // If the instruction is not defined in a loop then it can safely replace
706 Loop *ToLoop = getLoopFor(I->getParent());
707 if (!ToLoop) return true;
708 // If the replacing instruction is defined in the same loop as the original
709 // instruction, or in a loop that contains it as an inner loop, then using
710 // it as a replacement will not break LCSSA form.
711 return ToLoop->contains(getLoopFor(From->getParent()));
714 /// Checks if moving a specific instruction can break LCSSA in any loop.
716 /// Return true if moving \p Inst to before \p NewLoc will break LCSSA,
717 /// assuming that the function containing \p Inst and \p NewLoc is currently
719 bool movementPreservesLCSSAForm(Instruction *Inst, Instruction *NewLoc) {
720 assert(Inst->getFunction() == NewLoc->getFunction() &&
721 "Can't reason about IPO!");
723 auto *OldBB = Inst->getParent();
724 auto *NewBB = NewLoc->getParent();
726 // Movement within the same loop does not break LCSSA (the equality check is
727 // to avoid doing a hashtable lookup in case of intra-block movement).
731 auto *OldLoop = getLoopFor(OldBB);
732 auto *NewLoop = getLoopFor(NewBB);
734 if (OldLoop == NewLoop)
737 // Check if Outer contains Inner; with the null loop counting as the
739 auto Contains = [](const Loop *Outer, const Loop *Inner) {
740 return !Outer || Outer->contains(Inner);
743 // To check that the movement of Inst to before NewLoc does not break LCSSA,
744 // we need to check two sets of uses for possible LCSSA violations at
745 // NewLoc: the users of NewInst, and the operands of NewInst.
747 // If we know we're hoisting Inst out of an inner loop to an outer loop,
748 // then the uses *of* Inst don't need to be checked.
750 if (!Contains(NewLoop, OldLoop)) {
751 for (Use &U : Inst->uses()) {
752 auto *UI = cast<Instruction>(U.getUser());
753 auto *UBB = isa<PHINode>(UI) ? cast<PHINode>(UI)->getIncomingBlock(U)
755 if (UBB != NewBB && getLoopFor(UBB) != NewLoop)
760 // If we know we're sinking Inst from an outer loop into an inner loop, then
761 // the *operands* of Inst don't need to be checked.
763 if (!Contains(OldLoop, NewLoop)) {
764 // See below on why we can't handle phi nodes here.
765 if (isa<PHINode>(Inst))
768 for (Use &U : Inst->operands()) {
769 auto *DefI = dyn_cast<Instruction>(U.get());
773 // This would need adjustment if we allow Inst to be a phi node -- the
774 // new use block won't simply be NewBB.
776 auto *DefBlock = DefI->getParent();
777 if (DefBlock != NewBB && getLoopFor(DefBlock) != NewLoop)
786 // Allow clients to walk the list of nested loops...
787 template <> struct GraphTraits<const Loop*> {
788 typedef const Loop *NodeRef;
789 typedef LoopInfo::iterator ChildIteratorType;
791 static NodeRef getEntryNode(const Loop *L) { return L; }
792 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
793 static ChildIteratorType child_end(NodeRef N) { return N->end(); }
796 template <> struct GraphTraits<Loop*> {
797 typedef Loop *NodeRef;
798 typedef LoopInfo::iterator ChildIteratorType;
800 static NodeRef getEntryNode(Loop *L) { return L; }
801 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
802 static ChildIteratorType child_end(NodeRef N) { return N->end(); }
805 /// \brief Analysis pass that exposes the \c LoopInfo for a function.
806 class LoopAnalysis : public AnalysisInfoMixin<LoopAnalysis> {
807 friend AnalysisInfoMixin<LoopAnalysis>;
808 static AnalysisKey Key;
811 typedef LoopInfo Result;
813 LoopInfo run(Function &F, FunctionAnalysisManager &AM);
816 /// \brief Printer pass for the \c LoopAnalysis results.
817 class LoopPrinterPass : public PassInfoMixin<LoopPrinterPass> {
821 explicit LoopPrinterPass(raw_ostream &OS) : OS(OS) {}
822 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
825 /// \brief Verifier pass for the \c LoopAnalysis results.
826 struct LoopVerifierPass : public PassInfoMixin<LoopVerifierPass> {
827 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
830 /// \brief The legacy pass manager's analysis pass to compute loop information.
831 class LoopInfoWrapperPass : public FunctionPass {
835 static char ID; // Pass identification, replacement for typeid
837 LoopInfoWrapperPass() : FunctionPass(ID) {
838 initializeLoopInfoWrapperPassPass(*PassRegistry::getPassRegistry());
841 LoopInfo &getLoopInfo() { return LI; }
842 const LoopInfo &getLoopInfo() const { return LI; }
844 /// \brief Calculate the natural loop information for a given function.
845 bool runOnFunction(Function &F) override;
847 void verifyAnalysis() const override;
849 void releaseMemory() override { LI.releaseMemory(); }
851 void print(raw_ostream &O, const Module *M = nullptr) const override;
853 void getAnalysisUsage(AnalysisUsage &AU) const override;
856 /// Function to print a loop's contents as LLVM's text IR assembly.
857 void printLoop(Loop &L, raw_ostream &OS, const std::string &Banner = "");
859 } // End llvm namespace