• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the LoopInfo class that is used to identify natural loops
11 // and determine the loop depth of various nodes of the CFG.  A natural loop
12 // has exactly one entry-point, which is called the header. Note that natural
13 // loops may actually be several loops that share the same header node.
14 //
15 // This analysis calculates the nesting structure of loops in a function.  For
16 // each natural loop identified, this analysis identifies natural loops
17 // contained entirely within the loop and the basic blocks the make up the loop.
18 //
19 // It can calculate on the fly various bits of information, for example:
20 //
21 //  * whether there is a preheader for the loop
22 //  * the number of back edges to the header
23 //  * whether or not a particular block branches out of the loop
24 //  * the successor blocks of the loop
25 //  * the loop depth
26 //  * etc...
27 //
28 // Note that this analysis specifically identifies *Loops* not cycles or SCCs
29 // in the CFG.  There can be strongly connected 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.
33 //
34 //===----------------------------------------------------------------------===//
35 
36 #ifndef LLVM_ANALYSIS_LOOPINFO_H
37 #define LLVM_ANALYSIS_LOOPINFO_H
38 
39 #include "llvm/ADT/DenseMap.h"
40 #include "llvm/ADT/DenseSet.h"
41 #include "llvm/ADT/GraphTraits.h"
42 #include "llvm/ADT/SmallPtrSet.h"
43 #include "llvm/ADT/SmallVector.h"
44 #include "llvm/IR/CFG.h"
45 #include "llvm/IR/Instruction.h"
46 #include "llvm/IR/Instructions.h"
47 #include "llvm/IR/PassManager.h"
48 #include "llvm/Pass.h"
49 #include <algorithm>
50 
51 namespace llvm {
52 
53 class DominatorTree;
54 class LoopInfo;
55 class Loop;
56 class MDNode;
57 class PHINode;
58 class raw_ostream;
59 template<class N> class DominatorTreeBase;
60 template<class N, class M> class LoopInfoBase;
61 template<class N, class M> class LoopBase;
62 
63 //===----------------------------------------------------------------------===//
64 /// Instances of this class are used to represent loops that are detected in the
65 /// flow graph.
66 ///
67 template<class BlockT, class LoopT>
68 class LoopBase {
69   LoopT *ParentLoop;
70   // Loops contained entirely within this one.
71   std::vector<LoopT *> SubLoops;
72 
73   // The list of blocks in this loop. First entry is the header node.
74   std::vector<BlockT*> Blocks;
75 
76   SmallPtrSet<const BlockT*, 8> DenseBlockSet;
77 
78   /// Indicator that this loop is no longer a valid loop.
79   bool IsInvalid = false;
80 
81   LoopBase(const LoopBase<BlockT, LoopT> &) = delete;
82   const LoopBase<BlockT, LoopT>&
83     operator=(const LoopBase<BlockT, LoopT> &) = delete;
84 public:
85   /// This creates an empty loop.
LoopBase()86   LoopBase() : ParentLoop(nullptr) {}
~LoopBase()87   ~LoopBase() {
88     for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
89       delete SubLoops[i];
90   }
91 
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.
getLoopDepth()95   unsigned getLoopDepth() const {
96     unsigned D = 1;
97     for (const LoopT *CurLoop = ParentLoop; CurLoop;
98          CurLoop = CurLoop->ParentLoop)
99       ++D;
100     return D;
101   }
getHeader()102   BlockT *getHeader() const { return Blocks.front(); }
getParentLoop()103   LoopT *getParentLoop() const { return ParentLoop; }
104 
105   /// This is a raw interface for bypassing addChildLoop.
setParentLoop(LoopT * L)106   void setParentLoop(LoopT *L) { ParentLoop = L; }
107 
108   /// Return true if the specified loop is contained within in this loop.
contains(const LoopT * L)109   bool contains(const LoopT *L) const {
110     if (L == this) return true;
111     if (!L)        return false;
112     return contains(L->getParentLoop());
113   }
114 
115   /// Return true if the specified basic block is in this loop.
contains(const BlockT * BB)116   bool contains(const BlockT *BB) const {
117     return DenseBlockSet.count(BB);
118   }
119 
120   /// Return true if the specified instruction is in this loop.
121   template<class InstT>
contains(const InstT * Inst)122   bool contains(const InstT *Inst) const {
123     return contains(Inst->getParent());
124   }
125 
126   /// Return the loops contained entirely within this loop.
getSubLoops()127   const std::vector<LoopT *> &getSubLoops() const { return SubLoops; }
getSubLoopsVector()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
131     reverse_iterator;
begin()132   iterator begin() const { return SubLoops.begin(); }
end()133   iterator end() const { return SubLoops.end(); }
rbegin()134   reverse_iterator rbegin() const { return SubLoops.rbegin(); }
rend()135   reverse_iterator rend() const { return SubLoops.rend(); }
empty()136   bool empty() const { return SubLoops.empty(); }
137 
138   /// Get a list of the basic blocks which make up this loop.
getBlocks()139   const std::vector<BlockT*> &getBlocks() const { return Blocks; }
140   typedef typename std::vector<BlockT*>::const_iterator block_iterator;
block_begin()141   block_iterator block_begin() const { return Blocks.begin(); }
block_end()142   block_iterator block_end() const { return Blocks.end(); }
blocks()143   inline iterator_range<block_iterator> blocks() const {
144     return make_range(block_begin(), block_end());
145   }
146 
147   /// Get the number of blocks in this loop in constant time.
getNumBlocks()148   unsigned getNumBlocks() const {
149     return Blocks.size();
150   }
151 
152   /// Invalidate the loop, indicating that it is no longer a loop.
invalidate()153   void invalidate() { IsInvalid = true; }
154 
155   /// Return true if this loop is no longer valid.
isInvalid()156   bool isInvalid() { return IsInvalid; }
157 
158   /// True if terminator in the block can branch to another block that is
159   /// outside of the current loop.
isLoopExiting(const BlockT * BB)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) {
165       if (!contains(*SI))
166         return true;
167     }
168     return false;
169   }
170 
171   /// Calculate the number of back edges to the loop header.
getNumBackEdges()172   unsigned getNumBackEdges() const {
173     unsigned NumBackEdges = 0;
174     BlockT *H = getHeader();
175 
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)
180       if (contains(*I))
181         ++NumBackEdges;
182 
183     return NumBackEdges;
184   }
185 
186   //===--------------------------------------------------------------------===//
187   // APIs for simple analysis of the loop.
188   //
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.
193 
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;
198 
199   /// If getExitingBlocks would return exactly one block, return that block.
200   /// Otherwise return null.
201   BlockT *getExitingBlock() const;
202 
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;
206 
207   /// If getExitBlocks would return exactly one block, return that block.
208   /// Otherwise return null.
209   BlockT *getExitBlock() const;
210 
211   /// Edge type.
212   typedef std::pair<const BlockT*, const BlockT*> Edge;
213 
214   /// Return all pairs of (_inside_block_,_outside_block_).
215   void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const;
216 
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.
221   ///
222   /// This method returns null if there is no preheader for the loop.
223   BlockT *getLoopPreheader() const;
224 
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;
230 
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;
234 
235   /// Return all loop latch blocks of this loop. A latch block is a block that
236   /// contains a branch back to the header.
getLoopLatches(SmallVectorImpl<BlockT * > & LoopLatches)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)
243       if (contains(*I))
244         LoopLatches.push_back(*I);
245   }
246 
247   //===--------------------------------------------------------------------===//
248   // APIs for updating loop information after changing the CFG
249   //
250 
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);
257 
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);
263 
264   /// Add the specified loop to be a child of this loop.
265   /// This updates the loop depth of the new child.
addChildLoop(LoopT * NewChild)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);
270   }
271 
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.
removeChildLoop(iterator I)274   LoopT *removeChildLoop(iterator I) {
275     assert(I != SubLoops.end() && "Cannot remove end iterator!");
276     LoopT *Child = *I;
277     assert(Child->ParentLoop == this && "Child is not a child of this loop!");
278     SubLoops.erase(SubLoops.begin()+(I-begin()));
279     Child->ParentLoop = nullptr;
280     return Child;
281   }
282 
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.
addBlockEntry(BlockT * BB)286   void addBlockEntry(BlockT *BB) {
287     Blocks.push_back(BB);
288     DenseBlockSet.insert(BB);
289   }
290 
291   /// interface to reverse Blocks[from, end of loop] in this loop
reverseBlock(unsigned from)292   void reverseBlock(unsigned from) {
293     std::reverse(Blocks.begin() + from, Blocks.end());
294   }
295 
296   /// interface to do reserve() for Blocks
reserveBlocks(unsigned size)297   void reserveBlocks(unsigned size) {
298     Blocks.reserve(size);
299   }
300 
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).
moveToHeader(BlockT * BB)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];
309         Blocks[0] = BB;
310         return;
311       }
312     }
313   }
314 
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
317   /// class.
removeBlockFromLoop(BlockT * BB)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!");
321     Blocks.erase(I);
322 
323     DenseBlockSet.erase(BB);
324   }
325 
326   /// Verify loop structure
327   void verifyLoop() const;
328 
329   /// Verify loop structure of this loop and all nested loops.
330   void verifyLoopNest(DenseSet<const LoopT*> *Loops) const;
331 
332   void print(raw_ostream &OS, unsigned Depth = 0) const;
333 
334 protected:
335   friend class LoopInfoBase<BlockT, LoopT>;
LoopBase(BlockT * BB)336   explicit LoopBase(BlockT *BB) : ParentLoop(nullptr) {
337     Blocks.push_back(BB);
338     DenseBlockSet.insert(BB);
339   }
340 };
341 
342 template<class BlockT, class LoopT>
343 raw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
344   Loop.print(OS);
345   return OS;
346 }
347 
348 // Implementation in LoopInfoImpl.h
349 extern template class LoopBase<BasicBlock, Loop>;
350 
351 
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> {
355 public:
Loop()356   Loop() {}
357 
358   /// Return true if the specified value is loop invariant.
359   bool isLoopInvariant(const Value *V) const;
360 
361   /// Return true if all the operands of the specified instruction are loop
362   /// invariant.
363   bool hasLoopInvariantOperands(const Instruction *I) const;
364 
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
369   /// isLoopInvariant.
370   ///
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;
375 
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
380   /// isLoopInvariant.
381   ///
382   /// If InsertPt is specified, it is the point to hoist instructions to.
383   /// If null, the terminator of the loop preheader is used.
384   ///
385   bool makeLoopInvariant(Instruction *I, bool &Changed,
386                          Instruction *InsertPt = nullptr) const;
387 
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.
391   ///
392   /// The IndVarSimplify pass transforms loops to have a canonical induction
393   /// variable.
394   ///
395   PHINode *getCanonicalInductionVariable() const;
396 
397   /// Return true if the Loop is in LCSSA form.
398   bool isLCSSAForm(DominatorTree &DT) const;
399 
400   /// Return true if this Loop and all inner subloops are in LCSSA form.
401   bool isRecursivelyLCSSAForm(DominatorTree &DT) const;
402 
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;
406 
407   /// Return true if the loop body is safe to clone in practice.
408   bool isSafeToClone() const;
409 
410   /// Returns true if the loop is annotated parallel.
411   ///
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
421   /// iterations.
422   bool isAnnotatedParallel() const;
423 
424   /// Return the llvm.loop loop id metadata node for this loop if it is present.
425   ///
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
429   /// 0 is returned.
430   MDNode *getLoopID() const;
431   /// Set the llvm.loop loop id metadata for this loop.
432   ///
433   /// The LoopID metadata node will be added to each terminator instruction in
434   /// the loop that branches to the loop header.
435   ///
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;
439 
440   /// Return true if no exit block for the loop has a predecessor that is
441   /// outside the loop.
442   bool hasDedicatedExits() const;
443 
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;
448 
449   /// If getUniqueExitBlocks would return exactly one block, return that block.
450   /// Otherwise return null.
451   BasicBlock *getUniqueExitBlock() const;
452 
453   void dump() const;
454 
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;
461 
getName()462   StringRef getName() const {
463     if (BasicBlock *Header = getHeader())
464       if (Header->hasName())
465         return Header->getName();
466     return "<unnamed loop>";
467   }
468 
469 private:
470   friend class LoopInfoBase<BasicBlock, Loop>;
Loop(BasicBlock * BB)471   explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
472 };
473 
474 //===----------------------------------------------------------------------===//
475 /// This class builds and contains all of the top-level loop
476 /// structures in the specified function.
477 ///
478 
479 template<class BlockT, class LoopT>
480 class LoopInfoBase {
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;
485 
486   friend class LoopBase<BlockT, LoopT>;
487   friend class LoopInfo;
488 
489   void operator=(const LoopInfoBase &) = delete;
490   LoopInfoBase(const LoopInfoBase &) = delete;
491 public:
LoopInfoBase()492   LoopInfoBase() { }
~LoopInfoBase()493   ~LoopInfoBase() { releaseMemory(); }
494 
LoopInfoBase(LoopInfoBase && Arg)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();
500   }
501   LoopInfoBase &operator=(LoopInfoBase &&RHS) {
502     BBMap = std::move(RHS.BBMap);
503 
504     for (auto *L : TopLevelLoops)
505       delete L;
506     TopLevelLoops = std::move(RHS.TopLevelLoops);
507     RHS.TopLevelLoops.clear();
508     return *this;
509   }
510 
releaseMemory()511   void releaseMemory() {
512     BBMap.clear();
513 
514     for (auto *L : TopLevelLoops)
515       delete L;
516     TopLevelLoops.clear();
517     for (auto *L : RemovedLoops)
518       delete L;
519     RemovedLoops.clear();
520   }
521 
522   /// iterator/begin/end - The interface to the top-level loops in the current
523   /// function.
524   ///
525   typedef typename std::vector<LoopT *>::const_iterator iterator;
526   typedef typename std::vector<LoopT *>::const_reverse_iterator
527     reverse_iterator;
begin()528   iterator begin() const { return TopLevelLoops.begin(); }
end()529   iterator end() const { return TopLevelLoops.end(); }
rbegin()530   reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
rend()531   reverse_iterator rend() const { return TopLevelLoops.rend(); }
empty()532   bool empty() const { return TopLevelLoops.empty(); }
533 
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.
getLoopFor(const BlockT * BB)536   LoopT *getLoopFor(const BlockT *BB) const { return BBMap.lookup(BB); }
537 
538   /// Same as getLoopFor.
539   const LoopT *operator[](const BlockT *BB) const {
540     return getLoopFor(BB);
541   }
542 
543   /// Return the loop nesting level of the specified block. A depth of 0 means
544   /// the block is not inside any loop.
getLoopDepth(const BlockT * BB)545   unsigned getLoopDepth(const BlockT *BB) const {
546     const LoopT *L = getLoopFor(BB);
547     return L ? L->getLoopDepth() : 0;
548   }
549 
550   // True if the block is a loop header node
isLoopHeader(const BlockT * BB)551   bool isLoopHeader(const BlockT *BB) const {
552     const LoopT *L = getLoopFor(BB);
553     return L && L->getHeader() == BB;
554   }
555 
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
558   /// another loop.
removeLoop(iterator I)559   LoopT *removeLoop(iterator I) {
560     assert(I != end() && "Cannot remove end iterator!");
561     LoopT *L = *I;
562     assert(!L->getParentLoop() && "Not a top-level loop!");
563     TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
564     return L;
565   }
566 
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
569   /// tree.
changeLoopFor(BlockT * BB,LoopT * L)570   void changeLoopFor(BlockT *BB, LoopT *L) {
571     if (!L) {
572       BBMap.erase(BB);
573       return;
574     }
575     BBMap[BB] = L;
576   }
577 
578   /// Replace the specified loop in the top-level loops list with the indicated
579   /// loop.
changeTopLevelLoop(LoopT * OldLoop,LoopT * NewLoop)580   void changeTopLevelLoop(LoopT *OldLoop,
581                           LoopT *NewLoop) {
582     auto I = std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
583     assert(I != TopLevelLoops.end() && "Old loop not at top level!");
584     *I = NewLoop;
585     assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
586            "Loops already embedded into a subloop!");
587   }
588 
589   /// This adds the specified loop to the collection of top-level loops.
addTopLevelLoop(LoopT * New)590   void addTopLevelLoop(LoopT *New) {
591     assert(!New->getParentLoop() && "Loop already in subloop!");
592     TopLevelLoops.push_back(New);
593   }
594 
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.
removeBlock(BlockT * BB)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);
603 
604       BBMap.erase(I);
605     }
606   }
607 
608   // Internals
609 
isNotAlreadyContainedIn(const LoopT * SubLoop,const LoopT * ParentLoop)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);
615   }
616 
617   /// Create the loop forest using a stable algorithm.
618   void analyze(const DominatorTreeBase<BlockT> &DomTree);
619 
620   // Debugging
621   void print(raw_ostream &OS) const;
622 
623   void verify() const;
624 };
625 
626 // Implementation in LoopInfoImpl.h
627 extern template class LoopInfoBase<BasicBlock, Loop>;
628 
629 class LoopInfo : public LoopInfoBase<BasicBlock, Loop> {
630   typedef LoopInfoBase<BasicBlock, Loop> BaseT;
631 
632   friend class LoopBase<BasicBlock, Loop>;
633 
634   void operator=(const LoopInfo &) = delete;
635   LoopInfo(const LoopInfo &) = delete;
636 public:
LoopInfo()637   LoopInfo() {}
638   explicit LoopInfo(const DominatorTreeBase<BasicBlock> &DomTree);
639 
LoopInfo(LoopInfo && Arg)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)));
643     return *this;
644   }
645 
646   // Most of the public interface is provided via LoopInfoBase.
647 
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);
653 
654   /// Returns true if replacing From with To everywhere is guaranteed to
655   /// preserve LCSSA form.
replacementPreservesLCSSAForm(Instruction * From,Value * To)656   bool replacementPreservesLCSSAForm(Instruction *From, Value *To) {
657     // Preserving LCSSA form is only problematic if the replacing value is an
658     // instruction.
659     Instruction *I = dyn_cast<Instruction>(To);
660     if (!I) return true;
661     // If both instructions are defined in the same basic block then replacement
662     // cannot break LCSSA form.
663     if (I->getParent() == From->getParent())
664       return true;
665     // If the instruction is not defined in a loop then it can safely replace
666     // anything.
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()));
673   }
674 
675   /// Checks if moving a specific instruction can break LCSSA in any loop.
676   ///
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
679   /// in LCSSA form.
movementPreservesLCSSAForm(Instruction * Inst,Instruction * NewLoc)680   bool movementPreservesLCSSAForm(Instruction *Inst, Instruction *NewLoc) {
681     assert(Inst->getFunction() == NewLoc->getFunction() &&
682            "Can't reason about IPO!");
683 
684     auto *OldBB = Inst->getParent();
685     auto *NewBB = NewLoc->getParent();
686 
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).
689     if (OldBB == NewBB)
690       return true;
691 
692     auto *OldLoop = getLoopFor(OldBB);
693     auto *NewLoop = getLoopFor(NewBB);
694 
695     if (OldLoop == NewLoop)
696       return true;
697 
698     // Check if Outer contains Inner; with the null loop counting as the
699     // "outermost" loop.
700     auto Contains = [](const Loop *Outer, const Loop *Inner) {
701       return !Outer || Outer->contains(Inner);
702     };
703 
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.
707 
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.
710 
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)
715                                      : UI->getParent();
716         if (UBB != NewBB && getLoopFor(UBB) != NewLoop)
717           return false;
718       }
719     }
720 
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.
723 
724     if (!Contains(OldLoop, NewLoop)) {
725       // See below on why we can't handle phi nodes here.
726       if (isa<PHINode>(Inst))
727         return false;
728 
729       for (Use &U : Inst->operands()) {
730         auto *DefI = dyn_cast<Instruction>(U.get());
731         if (!DefI)
732           return false;
733 
734         // This would need adjustment if we allow Inst to be a phi node -- the
735         // new use block won't simply be NewBB.
736 
737         auto *DefBlock = DefI->getParent();
738         if (DefBlock != NewBB && getLoopFor(DefBlock) != NewLoop)
739           return false;
740       }
741     }
742 
743     return true;
744   }
745 };
746 
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;
751 
752   static NodeType *getEntryNode(const Loop *L) { return L; }
753   static inline ChildIteratorType child_begin(NodeType *N) {
754     return N->begin();
755   }
756   static inline ChildIteratorType child_end(NodeType *N) {
757     return N->end();
758   }
759 };
760 
761 template <> struct GraphTraits<Loop*> {
762   typedef Loop NodeType;
763   typedef LoopInfo::iterator ChildIteratorType;
764 
765   static NodeType *getEntryNode(Loop *L) { return L; }
766   static inline ChildIteratorType child_begin(NodeType *N) {
767     return N->begin();
768   }
769   static inline ChildIteratorType child_end(NodeType *N) {
770     return N->end();
771   }
772 };
773 
774 /// \brief Analysis pass that exposes the \c LoopInfo for a function.
775 class LoopAnalysis : public AnalysisInfoMixin<LoopAnalysis> {
776   friend AnalysisInfoMixin<LoopAnalysis>;
777   static char PassID;
778 
779 public:
780   typedef LoopInfo Result;
781 
782   LoopInfo run(Function &F, AnalysisManager<Function> &AM);
783 };
784 
785 /// \brief Printer pass for the \c LoopAnalysis results.
786 class LoopPrinterPass : public PassInfoMixin<LoopPrinterPass> {
787   raw_ostream &OS;
788 
789 public:
790   explicit LoopPrinterPass(raw_ostream &OS) : OS(OS) {}
791   PreservedAnalyses run(Function &F, AnalysisManager<Function> &AM);
792 };
793 
794 /// \brief The legacy pass manager's analysis pass to compute loop information.
795 class LoopInfoWrapperPass : public FunctionPass {
796   LoopInfo LI;
797 
798 public:
799   static char ID; // Pass identification, replacement for typeid
800 
801   LoopInfoWrapperPass() : FunctionPass(ID) {
802     initializeLoopInfoWrapperPassPass(*PassRegistry::getPassRegistry());
803   }
804 
805   LoopInfo &getLoopInfo() { return LI; }
806   const LoopInfo &getLoopInfo() const { return LI; }
807 
808   /// \brief Calculate the natural loop information for a given function.
809   bool runOnFunction(Function &F) override;
810 
811   void verifyAnalysis() const override;
812 
813   void releaseMemory() override { LI.releaseMemory(); }
814 
815   void print(raw_ostream &O, const Module *M = nullptr) const override;
816 
817   void getAnalysisUsage(AnalysisUsage &AU) const override;
818 };
819 
820 /// \brief Pass for printing a loop's contents as LLVM's text IR assembly.
821 class PrintLoopPass : public PassInfoMixin<PrintLoopPass> {
822   raw_ostream &OS;
823   std::string Banner;
824 
825 public:
826   PrintLoopPass();
827   PrintLoopPass(raw_ostream &OS, const std::string &Banner = "");
828 
829   PreservedAnalyses run(Loop &L, AnalysisManager<Loop> &);
830 };
831 
832 } // End llvm namespace
833 
834 #endif
835