]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Analysis/LoopInfo.h
Merge llvm, clang, lld and lldb release_40 branch r292009. Also update
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / Analysis / LoopInfo.h
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.
86   LoopBase() : ParentLoop(nullptr) {}
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.
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   }
102   BlockT *getHeader() const { return Blocks.front(); }
103   LoopT *getParentLoop() const { return ParentLoop; }
104
105   /// This is a raw interface for bypassing addChildLoop.
106   void setParentLoop(LoopT *L) { ParentLoop = L; }
107
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());
113   }
114
115   /// Return true if the specified basic block is in this loop.
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>
122   bool contains(const InstT *Inst) const {
123     return contains(Inst->getParent());
124   }
125
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
131     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(); }
137
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());
145   }
146
147   /// Get the number of blocks in this loop in constant time.
148   unsigned getNumBlocks() const {
149     return Blocks.size();
150   }
151
152   /// Invalidate the loop, indicating that it is no longer a loop.
153   void invalidate() { IsInvalid = true; }
154
155   /// Return true if this loop is no longer valid.
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.
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   /// 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");
177
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;
182   }
183
184   /// Calculate the number of back edges to the loop header.
185   unsigned getNumBackEdges() const {
186     unsigned NumBackEdges = 0;
187     BlockT *H = getHeader();
188
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)
193       if (contains(*I))
194         ++NumBackEdges;
195
196     return NumBackEdges;
197   }
198
199   //===--------------------------------------------------------------------===//
200   // APIs for simple analysis of the loop.
201   //
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.
206
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;
211
212   /// If getExitingBlocks would return exactly one block, return that block.
213   /// Otherwise return null.
214   BlockT *getExitingBlock() const;
215
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;
219
220   /// If getExitBlocks would return exactly one block, return that block.
221   /// Otherwise return null.
222   BlockT *getExitBlock() const;
223
224   /// Edge type.
225   typedef std::pair<const BlockT*, const BlockT*> Edge;
226
227   /// Return all pairs of (_inside_block_,_outside_block_).
228   void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const;
229
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.
234   ///
235   /// This method returns null if there is no preheader for the loop.
236   BlockT *getLoopPreheader() const;
237
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;
243
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;
247
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)
256       if (contains(*I))
257         LoopLatches.push_back(*I);
258   }
259
260   //===--------------------------------------------------------------------===//
261   // APIs for updating loop information after changing the CFG
262   //
263
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);
270
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);
276
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);
283   }
284
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!");
289     LoopT *Child = *I;
290     assert(Child->ParentLoop == this && "Child is not a child of this loop!");
291     SubLoops.erase(SubLoops.begin()+(I-begin()));
292     Child->ParentLoop = nullptr;
293     return Child;
294   }
295
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);
302   }
303
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());
307   }
308
309   /// interface to do reserve() for Blocks
310   void reserveBlocks(unsigned size) {
311     Blocks.reserve(size);
312   }
313
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];
322         Blocks[0] = BB;
323         return;
324       }
325     }
326   }
327
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
330   /// class.
331   void removeBlockFromLoop(BlockT *BB) {
332     auto I = find(Blocks, BB);
333     assert(I != Blocks.end() && "N is not in this list!");
334     Blocks.erase(I);
335
336     DenseBlockSet.erase(BB);
337   }
338
339   /// Verify loop structure
340   void verifyLoop() const;
341
342   /// Verify loop structure of this loop and all nested loops.
343   void verifyLoopNest(DenseSet<const LoopT*> *Loops) const;
344
345   /// Print loop with all the BBs inside it.
346   void print(raw_ostream &OS, unsigned Depth = 0, bool Verbose = false) const;
347
348 protected:
349   friend class LoopInfoBase<BlockT, LoopT>;
350   explicit LoopBase(BlockT *BB) : ParentLoop(nullptr) {
351     Blocks.push_back(BB);
352     DenseBlockSet.insert(BB);
353   }
354 };
355
356 template<class BlockT, class LoopT>
357 raw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
358   Loop.print(OS);
359   return OS;
360 }
361
362 // Implementation in LoopInfoImpl.h
363 extern template class LoopBase<BasicBlock, Loop>;
364
365
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> {
369 public:
370   /// \brief A range representing the start and end location of a loop.
371   class LocRange {
372     DebugLoc Start;
373     DebugLoc End;
374
375   public:
376     LocRange() {}
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)) {}
380
381     const DebugLoc &getStart() const { return Start; }
382     const DebugLoc &getEnd() const { return End; }
383
384     /// \brief Check for null.
385     ///
386     explicit operator bool() const {
387       return Start && End;
388     }
389   };
390
391   Loop() {}
392
393   /// Return true if the specified value is loop invariant.
394   bool isLoopInvariant(const Value *V) const;
395
396   /// Return true if all the operands of the specified instruction are loop
397   /// invariant.
398   bool hasLoopInvariantOperands(const Instruction *I) const;
399
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
404   /// isLoopInvariant.
405   ///
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;
410
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
415   /// isLoopInvariant.
416   ///
417   /// If InsertPt is specified, it is the point to hoist instructions to.
418   /// If null, the terminator of the loop preheader is used.
419   ///
420   bool makeLoopInvariant(Instruction *I, bool &Changed,
421                          Instruction *InsertPt = nullptr) const;
422
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.
426   ///
427   /// The IndVarSimplify pass transforms loops to have a canonical induction
428   /// variable.
429   ///
430   PHINode *getCanonicalInductionVariable() const;
431
432   /// Return true if the Loop is in LCSSA form.
433   bool isLCSSAForm(DominatorTree &DT) const;
434
435   /// Return true if this Loop and all inner subloops are in LCSSA form.
436   bool isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const;
437
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;
441
442   /// Return true if the loop body is safe to clone in practice.
443   bool isSafeToClone() const;
444
445   /// Returns true if the loop is annotated parallel.
446   ///
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
456   /// iterations.
457   bool isAnnotatedParallel() const;
458
459   /// Return the llvm.loop loop id metadata node for this loop if it is present.
460   ///
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
464   /// 0 is returned.
465   MDNode *getLoopID() const;
466   /// Set the llvm.loop loop id metadata for this loop.
467   ///
468   /// The LoopID metadata node will be added to each terminator instruction in
469   /// the loop that branches to the loop header.
470   ///
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;
474
475   /// Return true if no exit block for the loop has a predecessor that is
476   /// outside the loop.
477   bool hasDedicatedExits() const;
478
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;
483
484   /// If getUniqueExitBlocks would return exactly one block, return that block.
485   /// Otherwise return null.
486   BasicBlock *getUniqueExitBlock() const;
487
488   void dump() const;
489   void dumpVerbose() const;
490
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;
497
498   /// Return the source code span of the loop.
499   LocRange getLocRange() const;
500
501   StringRef getName() const {
502     if (BasicBlock *Header = getHeader())
503       if (Header->hasName())
504         return Header->getName();
505     return "<unnamed loop>";
506   }
507
508 private:
509   friend class LoopInfoBase<BasicBlock, Loop>;
510   explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
511 };
512
513 //===----------------------------------------------------------------------===//
514 /// This class builds and contains all of the top-level loop
515 /// structures in the specified function.
516 ///
517
518 template<class BlockT, class LoopT>
519 class LoopInfoBase {
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;
524
525   friend class LoopBase<BlockT, LoopT>;
526   friend class LoopInfo;
527
528   void operator=(const LoopInfoBase &) = delete;
529   LoopInfoBase(const LoopInfoBase &) = delete;
530 public:
531   LoopInfoBase() { }
532   ~LoopInfoBase() { releaseMemory(); }
533
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();
539   }
540   LoopInfoBase &operator=(LoopInfoBase &&RHS) {
541     BBMap = std::move(RHS.BBMap);
542
543     for (auto *L : TopLevelLoops)
544       delete L;
545     TopLevelLoops = std::move(RHS.TopLevelLoops);
546     RHS.TopLevelLoops.clear();
547     return *this;
548   }
549
550   void releaseMemory() {
551     BBMap.clear();
552
553     for (auto *L : TopLevelLoops)
554       delete L;
555     TopLevelLoops.clear();
556     for (auto *L : RemovedLoops)
557       delete L;
558     RemovedLoops.clear();
559   }
560
561   /// iterator/begin/end - The interface to the top-level loops in the current
562   /// function.
563   ///
564   typedef typename std::vector<LoopT *>::const_iterator iterator;
565   typedef typename std::vector<LoopT *>::const_reverse_iterator
566     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(); }
572
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); }
576
577   /// Same as getLoopFor.
578   const LoopT *operator[](const BlockT *BB) const {
579     return getLoopFor(BB);
580   }
581
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;
587   }
588
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;
593   }
594
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
597   /// another loop.
598   LoopT *removeLoop(iterator I) {
599     assert(I != end() && "Cannot remove end iterator!");
600     LoopT *L = *I;
601     assert(!L->getParentLoop() && "Not a top-level loop!");
602     TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
603     return L;
604   }
605
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
608   /// tree.
609   void changeLoopFor(BlockT *BB, LoopT *L) {
610     if (!L) {
611       BBMap.erase(BB);
612       return;
613     }
614     BBMap[BB] = L;
615   }
616
617   /// Replace the specified loop in the top-level loops list with the indicated
618   /// loop.
619   void changeTopLevelLoop(LoopT *OldLoop,
620                           LoopT *NewLoop) {
621     auto I = find(TopLevelLoops, OldLoop);
622     assert(I != TopLevelLoops.end() && "Old loop not at top level!");
623     *I = NewLoop;
624     assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
625            "Loops already embedded into a subloop!");
626   }
627
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);
632   }
633
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);
642
643       BBMap.erase(I);
644     }
645   }
646
647   // Internals
648
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);
654   }
655
656   /// Create the loop forest using a stable algorithm.
657   void analyze(const DominatorTreeBase<BlockT> &DomTree);
658
659   // Debugging
660   void print(raw_ostream &OS) const;
661
662   void verify(const DominatorTreeBase<BlockT> &DomTree) const;
663 };
664
665 // Implementation in LoopInfoImpl.h
666 extern template class LoopInfoBase<BasicBlock, Loop>;
667
668 class LoopInfo : public LoopInfoBase<BasicBlock, Loop> {
669   typedef LoopInfoBase<BasicBlock, Loop> BaseT;
670
671   friend class LoopBase<BasicBlock, Loop>;
672
673   void operator=(const LoopInfo &) = delete;
674   LoopInfo(const LoopInfo &) = delete;
675 public:
676   LoopInfo() {}
677   explicit LoopInfo(const DominatorTreeBase<BasicBlock> &DomTree);
678
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)));
682     return *this;
683   }
684
685   // Most of the public interface is provided via LoopInfoBase.
686
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);
692
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
697     // instruction.
698     Instruction *I = dyn_cast<Instruction>(To);
699     if (!I) return true;
700     // If both instructions are defined in the same basic block then replacement
701     // cannot break LCSSA form.
702     if (I->getParent() == From->getParent())
703       return true;
704     // If the instruction is not defined in a loop then it can safely replace
705     // anything.
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()));
712   }
713
714   /// Checks if moving a specific instruction can break LCSSA in any loop.
715   ///
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
718   /// in LCSSA form.
719   bool movementPreservesLCSSAForm(Instruction *Inst, Instruction *NewLoc) {
720     assert(Inst->getFunction() == NewLoc->getFunction() &&
721            "Can't reason about IPO!");
722
723     auto *OldBB = Inst->getParent();
724     auto *NewBB = NewLoc->getParent();
725
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).
728     if (OldBB == NewBB)
729       return true;
730
731     auto *OldLoop = getLoopFor(OldBB);
732     auto *NewLoop = getLoopFor(NewBB);
733
734     if (OldLoop == NewLoop)
735       return true;
736
737     // Check if Outer contains Inner; with the null loop counting as the
738     // "outermost" loop.
739     auto Contains = [](const Loop *Outer, const Loop *Inner) {
740       return !Outer || Outer->contains(Inner);
741     };
742
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.
746
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.
749
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)
754                                      : UI->getParent();
755         if (UBB != NewBB && getLoopFor(UBB) != NewLoop)
756           return false;
757       }
758     }
759
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.
762
763     if (!Contains(OldLoop, NewLoop)) {
764       // See below on why we can't handle phi nodes here.
765       if (isa<PHINode>(Inst))
766         return false;
767
768       for (Use &U : Inst->operands()) {
769         auto *DefI = dyn_cast<Instruction>(U.get());
770         if (!DefI)
771           return false;
772
773         // This would need adjustment if we allow Inst to be a phi node -- the
774         // new use block won't simply be NewBB.
775
776         auto *DefBlock = DefI->getParent();
777         if (DefBlock != NewBB && getLoopFor(DefBlock) != NewLoop)
778           return false;
779       }
780     }
781
782     return true;
783   }
784 };
785
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;
790
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(); }
794 };
795
796 template <> struct GraphTraits<Loop*> {
797   typedef Loop *NodeRef;
798   typedef LoopInfo::iterator ChildIteratorType;
799
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(); }
803 };
804
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;
809
810 public:
811   typedef LoopInfo Result;
812
813   LoopInfo run(Function &F, FunctionAnalysisManager &AM);
814 };
815
816 /// \brief Printer pass for the \c LoopAnalysis results.
817 class LoopPrinterPass : public PassInfoMixin<LoopPrinterPass> {
818   raw_ostream &OS;
819
820 public:
821   explicit LoopPrinterPass(raw_ostream &OS) : OS(OS) {}
822   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
823 };
824
825 /// \brief Verifier pass for the \c LoopAnalysis results.
826 struct LoopVerifierPass : public PassInfoMixin<LoopVerifierPass> {
827   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
828 };
829
830 /// \brief The legacy pass manager's analysis pass to compute loop information.
831 class LoopInfoWrapperPass : public FunctionPass {
832   LoopInfo LI;
833
834 public:
835   static char ID; // Pass identification, replacement for typeid
836
837   LoopInfoWrapperPass() : FunctionPass(ID) {
838     initializeLoopInfoWrapperPassPass(*PassRegistry::getPassRegistry());
839   }
840
841   LoopInfo &getLoopInfo() { return LI; }
842   const LoopInfo &getLoopInfo() const { return LI; }
843
844   /// \brief Calculate the natural loop information for a given function.
845   bool runOnFunction(Function &F) override;
846
847   void verifyAnalysis() const override;
848
849   void releaseMemory() override { LI.releaseMemory(); }
850
851   void print(raw_ostream &O, const Module *M = nullptr) const override;
852
853   void getAnalysisUsage(AnalysisUsage &AU) const override;
854 };
855
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 = "");
858
859 } // End llvm namespace
860
861 #endif