• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the LoopInfo class that is used to identify natural loops
10 // and determine the loop depth of various nodes of the CFG.  A natural loop
11 // has exactly one entry-point, which is called the header. Note that natural
12 // loops may actually be several loops that share the same header node.
13 //
14 // This analysis calculates the nesting structure of loops in a function.  For
15 // each natural loop identified, this analysis identifies natural loops
16 // contained entirely within the loop and the basic blocks the make up the loop.
17 //
18 // It can calculate on the fly various bits of information, for example:
19 //
20 //  * whether there is a preheader for the loop
21 //  * the number of back edges to the header
22 //  * whether or not a particular block branches out of the loop
23 //  * the successor blocks of the loop
24 //  * the loop depth
25 //  * etc...
26 //
27 // Note that this analysis specifically identifies *Loops* not cycles or SCCs
28 // in the CFG.  There can be strongly connected components in the CFG which
29 // this analysis will not recognize and that will not be represented by a Loop
30 // instance.  In particular, a Loop might be inside such a non-loop SCC, or a
31 // non-loop SCC might contain a sub-SCC which is a Loop.
32 //
33 // For an overview of terminology used in this API (and thus all of our loop
34 // analyses or transforms), see docs/LoopTerminology.rst.
35 //
36 //===----------------------------------------------------------------------===//
37 
38 #ifndef LLVM_ANALYSIS_LOOPINFO_H
39 #define LLVM_ANALYSIS_LOOPINFO_H
40 
41 #include "llvm/ADT/DenseMap.h"
42 #include "llvm/ADT/DenseSet.h"
43 #include "llvm/ADT/GraphTraits.h"
44 #include "llvm/ADT/SmallPtrSet.h"
45 #include "llvm/ADT/SmallVector.h"
46 #include "llvm/IR/CFG.h"
47 #include "llvm/IR/Instruction.h"
48 #include "llvm/IR/Instructions.h"
49 #include "llvm/IR/PassManager.h"
50 #include "llvm/Pass.h"
51 #include "llvm/Support/Allocator.h"
52 #include <algorithm>
53 #include <utility>
54 
55 namespace llvm {
56 
57 class DominatorTree;
58 class LoopInfo;
59 class Loop;
60 class InductionDescriptor;
61 class MDNode;
62 class MemorySSAUpdater;
63 class PHINode;
64 class ScalarEvolution;
65 class raw_ostream;
66 template <class N, bool IsPostDom> class DominatorTreeBase;
67 template <class N, class M> class LoopInfoBase;
68 template <class N, class M> class LoopBase;
69 
70 //===----------------------------------------------------------------------===//
71 /// Instances of this class are used to represent loops that are detected in the
72 /// flow graph.
73 ///
74 template <class BlockT, class LoopT> class LoopBase {
75   LoopT *ParentLoop;
76   // Loops contained entirely within this one.
77   std::vector<LoopT *> SubLoops;
78 
79   // The list of blocks in this loop. First entry is the header node.
80   std::vector<BlockT *> Blocks;
81 
82   SmallPtrSet<const BlockT *, 8> DenseBlockSet;
83 
84 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
85   /// Indicator that this loop is no longer a valid loop.
86   bool IsInvalid = false;
87 #endif
88 
89   LoopBase(const LoopBase<BlockT, LoopT> &) = delete;
90   const LoopBase<BlockT, LoopT> &
91   operator=(const LoopBase<BlockT, LoopT> &) = delete;
92 
93 public:
94   /// Return the nesting level of this loop.  An outer-most loop has depth 1,
95   /// for consistency with loop depth values used for basic blocks, where depth
96   /// 0 is used for blocks not inside any loops.
getLoopDepth()97   unsigned getLoopDepth() const {
98     assert(!isInvalid() && "Loop not in a valid state!");
99     unsigned D = 1;
100     for (const LoopT *CurLoop = ParentLoop; CurLoop;
101          CurLoop = CurLoop->ParentLoop)
102       ++D;
103     return D;
104   }
getHeader()105   BlockT *getHeader() const { return getBlocks().front(); }
getParentLoop()106   LoopT *getParentLoop() const { return ParentLoop; }
107 
108   /// This is a raw interface for bypassing addChildLoop.
setParentLoop(LoopT * L)109   void setParentLoop(LoopT *L) {
110     assert(!isInvalid() && "Loop not in a valid state!");
111     ParentLoop = L;
112   }
113 
114   /// Return true if the specified loop is contained within in this loop.
contains(const LoopT * L)115   bool contains(const LoopT *L) const {
116     assert(!isInvalid() && "Loop not in a valid state!");
117     if (L == this)
118       return true;
119     if (!L)
120       return false;
121     return contains(L->getParentLoop());
122   }
123 
124   /// Return true if the specified basic block is in this loop.
contains(const BlockT * BB)125   bool contains(const BlockT *BB) const {
126     assert(!isInvalid() && "Loop not in a valid state!");
127     return DenseBlockSet.count(BB);
128   }
129 
130   /// Return true if the specified instruction is in this loop.
contains(const InstT * Inst)131   template <class InstT> bool contains(const InstT *Inst) const {
132     return contains(Inst->getParent());
133   }
134 
135   /// Return the loops contained entirely within this loop.
getSubLoops()136   const std::vector<LoopT *> &getSubLoops() const {
137     assert(!isInvalid() && "Loop not in a valid state!");
138     return SubLoops;
139   }
getSubLoopsVector()140   std::vector<LoopT *> &getSubLoopsVector() {
141     assert(!isInvalid() && "Loop not in a valid state!");
142     return SubLoops;
143   }
144   typedef typename std::vector<LoopT *>::const_iterator iterator;
145   typedef
146       typename std::vector<LoopT *>::const_reverse_iterator reverse_iterator;
begin()147   iterator begin() const { return getSubLoops().begin(); }
end()148   iterator end() const { return getSubLoops().end(); }
rbegin()149   reverse_iterator rbegin() const { return getSubLoops().rbegin(); }
rend()150   reverse_iterator rend() const { return getSubLoops().rend(); }
empty()151   bool empty() const { return getSubLoops().empty(); }
152 
153   /// Get a list of the basic blocks which make up this loop.
getBlocks()154   ArrayRef<BlockT *> getBlocks() const {
155     assert(!isInvalid() && "Loop not in a valid state!");
156     return Blocks;
157   }
158   typedef typename ArrayRef<BlockT *>::const_iterator block_iterator;
block_begin()159   block_iterator block_begin() const { return getBlocks().begin(); }
block_end()160   block_iterator block_end() const { return getBlocks().end(); }
blocks()161   inline iterator_range<block_iterator> blocks() const {
162     assert(!isInvalid() && "Loop not in a valid state!");
163     return make_range(block_begin(), block_end());
164   }
165 
166   /// Get the number of blocks in this loop in constant time.
167   /// Invalidate the loop, indicating that it is no longer a loop.
getNumBlocks()168   unsigned getNumBlocks() const {
169     assert(!isInvalid() && "Loop not in a valid state!");
170     return Blocks.size();
171   }
172 
173   /// Return a direct, mutable handle to the blocks vector so that we can
174   /// mutate it efficiently with techniques like `std::remove`.
getBlocksVector()175   std::vector<BlockT *> &getBlocksVector() {
176     assert(!isInvalid() && "Loop not in a valid state!");
177     return Blocks;
178   }
179   /// Return a direct, mutable handle to the blocks set so that we can
180   /// mutate it efficiently.
getBlocksSet()181   SmallPtrSetImpl<const BlockT *> &getBlocksSet() {
182     assert(!isInvalid() && "Loop not in a valid state!");
183     return DenseBlockSet;
184   }
185 
186   /// Return a direct, immutable handle to the blocks set.
getBlocksSet()187   const SmallPtrSetImpl<const BlockT *> &getBlocksSet() const {
188     assert(!isInvalid() && "Loop not in a valid state!");
189     return DenseBlockSet;
190   }
191 
192   /// Return true if this loop is no longer valid.  The only valid use of this
193   /// helper is "assert(L.isInvalid())" or equivalent, since IsInvalid is set to
194   /// true by the destructor.  In other words, if this accessor returns true,
195   /// the caller has already triggered UB by calling this accessor; and so it
196   /// can only be called in a context where a return value of true indicates a
197   /// programmer error.
isInvalid()198   bool isInvalid() const {
199 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
200     return IsInvalid;
201 #else
202     return false;
203 #endif
204   }
205 
206   /// True if terminator in the block can branch to another block that is
207   /// outside of the current loop. \p BB must be inside the loop.
isLoopExiting(const BlockT * BB)208   bool isLoopExiting(const BlockT *BB) const {
209     assert(!isInvalid() && "Loop not in a valid state!");
210     assert(contains(BB) && "Exiting block must be part of the loop");
211     for (const auto *Succ : children<const BlockT *>(BB)) {
212       if (!contains(Succ))
213         return true;
214     }
215     return false;
216   }
217 
218   /// Returns true if \p BB is a loop-latch.
219   /// A latch block is a block that contains a branch back to the header.
220   /// This function is useful when there are multiple latches in a loop
221   /// because \fn getLoopLatch will return nullptr in that case.
isLoopLatch(const BlockT * BB)222   bool isLoopLatch(const BlockT *BB) const {
223     assert(!isInvalid() && "Loop not in a valid state!");
224     assert(contains(BB) && "block does not belong to the loop");
225 
226     BlockT *Header = getHeader();
227     auto PredBegin = GraphTraits<Inverse<BlockT *>>::child_begin(Header);
228     auto PredEnd = GraphTraits<Inverse<BlockT *>>::child_end(Header);
229     return std::find(PredBegin, PredEnd, BB) != PredEnd;
230   }
231 
232   /// Calculate the number of back edges to the loop header.
getNumBackEdges()233   unsigned getNumBackEdges() const {
234     assert(!isInvalid() && "Loop not in a valid state!");
235     unsigned NumBackEdges = 0;
236     BlockT *H = getHeader();
237 
238     for (const auto Pred : children<Inverse<BlockT *>>(H))
239       if (contains(Pred))
240         ++NumBackEdges;
241 
242     return NumBackEdges;
243   }
244 
245   //===--------------------------------------------------------------------===//
246   // APIs for simple analysis of the loop.
247   //
248   // Note that all of these methods can fail on general loops (ie, there may not
249   // be a preheader, etc).  For best success, the loop simplification and
250   // induction variable canonicalization pass should be used to normalize loops
251   // for easy analysis.  These methods assume canonical loops.
252 
253   /// Return all blocks inside the loop that have successors outside of the
254   /// loop. These are the blocks _inside of the current loop_ which branch out.
255   /// The returned list is always unique.
256   void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const;
257 
258   /// If getExitingBlocks would return exactly one block, return that block.
259   /// Otherwise return null.
260   BlockT *getExitingBlock() const;
261 
262   /// Return all of the successor blocks of this loop. These are the blocks
263   /// _outside of the current loop_ which are branched to.
264   void getExitBlocks(SmallVectorImpl<BlockT *> &ExitBlocks) const;
265 
266   /// If getExitBlocks would return exactly one block, return that block.
267   /// Otherwise return null.
268   BlockT *getExitBlock() const;
269 
270   /// Return true if no exit block for the loop has a predecessor that is
271   /// outside the loop.
272   bool hasDedicatedExits() const;
273 
274   /// Return all unique successor blocks of this loop.
275   /// These are the blocks _outside of the current loop_ which are branched to.
276   void getUniqueExitBlocks(SmallVectorImpl<BlockT *> &ExitBlocks) const;
277 
278   /// Return all unique successor blocks of this loop except successors from
279   /// Latch block are not considered. If the exit comes from Latch has also
280   /// non Latch predecessor in a loop it will be added to ExitBlocks.
281   /// These are the blocks _outside of the current loop_ which are branched to.
282   void getUniqueNonLatchExitBlocks(SmallVectorImpl<BlockT *> &ExitBlocks) const;
283 
284   /// If getUniqueExitBlocks would return exactly one block, return that block.
285   /// Otherwise return null.
286   BlockT *getUniqueExitBlock() const;
287 
288   /// Edge type.
289   typedef std::pair<BlockT *, BlockT *> Edge;
290 
291   /// Return all pairs of (_inside_block_,_outside_block_).
292   void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const;
293 
294   /// If there is a preheader for this loop, return it. A loop has a preheader
295   /// if there is only one edge to the header of the loop from outside of the
296   /// loop. If this is the case, the block branching to the header of the loop
297   /// is the preheader node.
298   ///
299   /// This method returns null if there is no preheader for the loop.
300   BlockT *getLoopPreheader() const;
301 
302   /// If the given loop's header has exactly one unique predecessor outside the
303   /// loop, return it. Otherwise return null.
304   ///  This is less strict that the loop "preheader" concept, which requires
305   /// the predecessor to have exactly one successor.
306   BlockT *getLoopPredecessor() const;
307 
308   /// If there is a single latch block for this loop, return it.
309   /// A latch block is a block that contains a branch back to the header.
310   BlockT *getLoopLatch() const;
311 
312   /// Return all loop latch blocks of this loop. A latch block is a block that
313   /// contains a branch back to the header.
getLoopLatches(SmallVectorImpl<BlockT * > & LoopLatches)314   void getLoopLatches(SmallVectorImpl<BlockT *> &LoopLatches) const {
315     assert(!isInvalid() && "Loop not in a valid state!");
316     BlockT *H = getHeader();
317     for (const auto Pred : children<Inverse<BlockT *>>(H))
318       if (contains(Pred))
319         LoopLatches.push_back(Pred);
320   }
321 
322   /// Return all inner loops in the loop nest rooted by the loop in preorder,
323   /// with siblings in forward program order.
324   template <class Type>
getInnerLoopsInPreorder(const LoopT & L,SmallVectorImpl<Type> & PreOrderLoops)325   static void getInnerLoopsInPreorder(const LoopT &L,
326                                       SmallVectorImpl<Type> &PreOrderLoops) {
327     SmallVector<LoopT *, 4> PreOrderWorklist;
328     PreOrderWorklist.append(L.rbegin(), L.rend());
329 
330     while (!PreOrderWorklist.empty()) {
331       LoopT *L = PreOrderWorklist.pop_back_val();
332       // Sub-loops are stored in forward program order, but will process the
333       // worklist backwards so append them in reverse order.
334       PreOrderWorklist.append(L->rbegin(), L->rend());
335       PreOrderLoops.push_back(L);
336     }
337   }
338 
339   /// Return all loops in the loop nest rooted by the loop in preorder, with
340   /// siblings in forward program order.
getLoopsInPreorder()341   SmallVector<const LoopT *, 4> getLoopsInPreorder() const {
342     SmallVector<const LoopT *, 4> PreOrderLoops;
343     const LoopT *CurLoop = static_cast<const LoopT *>(this);
344     PreOrderLoops.push_back(CurLoop);
345     getInnerLoopsInPreorder(*CurLoop, PreOrderLoops);
346     return PreOrderLoops;
347   }
getLoopsInPreorder()348   SmallVector<LoopT *, 4> getLoopsInPreorder() {
349     SmallVector<LoopT *, 4> PreOrderLoops;
350     LoopT *CurLoop = static_cast<LoopT *>(this);
351     PreOrderLoops.push_back(CurLoop);
352     getInnerLoopsInPreorder(*CurLoop, PreOrderLoops);
353     return PreOrderLoops;
354   }
355 
356   //===--------------------------------------------------------------------===//
357   // APIs for updating loop information after changing the CFG
358   //
359 
360   /// This method is used by other analyses to update loop information.
361   /// NewBB is set to be a new member of the current loop.
362   /// Because of this, it is added as a member of all parent loops, and is added
363   /// to the specified LoopInfo object as being in the current basic block.  It
364   /// is not valid to replace the loop header with this method.
365   void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
366 
367   /// This is used when splitting loops up. It replaces the OldChild entry in
368   /// our children list with NewChild, and updates the parent pointer of
369   /// OldChild to be null and the NewChild to be this loop.
370   /// This updates the loop depth of the new child.
371   void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild);
372 
373   /// Add the specified loop to be a child of this loop.
374   /// This updates the loop depth of the new child.
addChildLoop(LoopT * NewChild)375   void addChildLoop(LoopT *NewChild) {
376     assert(!isInvalid() && "Loop not in a valid state!");
377     assert(!NewChild->ParentLoop && "NewChild already has a parent!");
378     NewChild->ParentLoop = static_cast<LoopT *>(this);
379     SubLoops.push_back(NewChild);
380   }
381 
382   /// This removes the specified child from being a subloop of this loop. The
383   /// loop is not deleted, as it will presumably be inserted into another loop.
removeChildLoop(iterator I)384   LoopT *removeChildLoop(iterator I) {
385     assert(!isInvalid() && "Loop not in a valid state!");
386     assert(I != SubLoops.end() && "Cannot remove end iterator!");
387     LoopT *Child = *I;
388     assert(Child->ParentLoop == this && "Child is not a child of this loop!");
389     SubLoops.erase(SubLoops.begin() + (I - begin()));
390     Child->ParentLoop = nullptr;
391     return Child;
392   }
393 
394   /// This removes the specified child from being a subloop of this loop. The
395   /// loop is not deleted, as it will presumably be inserted into another loop.
removeChildLoop(LoopT * Child)396   LoopT *removeChildLoop(LoopT *Child) {
397     return removeChildLoop(llvm::find(*this, Child));
398   }
399 
400   /// This adds a basic block directly to the basic block list.
401   /// This should only be used by transformations that create new loops.  Other
402   /// transformations should use addBasicBlockToLoop.
addBlockEntry(BlockT * BB)403   void addBlockEntry(BlockT *BB) {
404     assert(!isInvalid() && "Loop not in a valid state!");
405     Blocks.push_back(BB);
406     DenseBlockSet.insert(BB);
407   }
408 
409   /// interface to reverse Blocks[from, end of loop] in this loop
reverseBlock(unsigned from)410   void reverseBlock(unsigned from) {
411     assert(!isInvalid() && "Loop not in a valid state!");
412     std::reverse(Blocks.begin() + from, Blocks.end());
413   }
414 
415   /// interface to do reserve() for Blocks
reserveBlocks(unsigned size)416   void reserveBlocks(unsigned size) {
417     assert(!isInvalid() && "Loop not in a valid state!");
418     Blocks.reserve(size);
419   }
420 
421   /// This method is used to move BB (which must be part of this loop) to be the
422   /// loop header of the loop (the block that dominates all others).
moveToHeader(BlockT * BB)423   void moveToHeader(BlockT *BB) {
424     assert(!isInvalid() && "Loop not in a valid state!");
425     if (Blocks[0] == BB)
426       return;
427     for (unsigned i = 0;; ++i) {
428       assert(i != Blocks.size() && "Loop does not contain BB!");
429       if (Blocks[i] == BB) {
430         Blocks[i] = Blocks[0];
431         Blocks[0] = BB;
432         return;
433       }
434     }
435   }
436 
437   /// This removes the specified basic block from the current loop, updating the
438   /// Blocks as appropriate. This does not update the mapping in the LoopInfo
439   /// class.
removeBlockFromLoop(BlockT * BB)440   void removeBlockFromLoop(BlockT *BB) {
441     assert(!isInvalid() && "Loop not in a valid state!");
442     auto I = find(Blocks, BB);
443     assert(I != Blocks.end() && "N is not in this list!");
444     Blocks.erase(I);
445 
446     DenseBlockSet.erase(BB);
447   }
448 
449   /// Verify loop structure
450   void verifyLoop() const;
451 
452   /// Verify loop structure of this loop and all nested loops.
453   void verifyLoopNest(DenseSet<const LoopT *> *Loops) const;
454 
455   /// Returns true if the loop is annotated parallel.
456   ///
457   /// Derived classes can override this method using static template
458   /// polymorphism.
isAnnotatedParallel()459   bool isAnnotatedParallel() const { return false; }
460 
461   /// Print loop with all the BBs inside it.
462   void print(raw_ostream &OS, unsigned Depth = 0, bool Verbose = false) const;
463 
464 protected:
465   friend class LoopInfoBase<BlockT, LoopT>;
466 
467   /// This creates an empty loop.
LoopBase()468   LoopBase() : ParentLoop(nullptr) {}
469 
LoopBase(BlockT * BB)470   explicit LoopBase(BlockT *BB) : ParentLoop(nullptr) {
471     Blocks.push_back(BB);
472     DenseBlockSet.insert(BB);
473   }
474 
475   // Since loop passes like SCEV are allowed to key analysis results off of
476   // `Loop` pointers, we cannot re-use pointers within a loop pass manager.
477   // This means loop passes should not be `delete` ing `Loop` objects directly
478   // (and risk a later `Loop` allocation re-using the address of a previous one)
479   // but should be using LoopInfo::markAsRemoved, which keeps around the `Loop`
480   // pointer till the end of the lifetime of the `LoopInfo` object.
481   //
482   // To make it easier to follow this rule, we mark the destructor as
483   // non-public.
~LoopBase()484   ~LoopBase() {
485     for (auto *SubLoop : SubLoops)
486       SubLoop->~LoopT();
487 
488 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
489     IsInvalid = true;
490 #endif
491     SubLoops.clear();
492     Blocks.clear();
493     DenseBlockSet.clear();
494     ParentLoop = nullptr;
495   }
496 };
497 
498 template <class BlockT, class LoopT>
499 raw_ostream &operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
500   Loop.print(OS);
501   return OS;
502 }
503 
504 // Implementation in LoopInfoImpl.h
505 extern template class LoopBase<BasicBlock, Loop>;
506 
507 /// Represents a single loop in the control flow graph.  Note that not all SCCs
508 /// in the CFG are necessarily loops.
509 class Loop : public LoopBase<BasicBlock, Loop> {
510 public:
511   /// A range representing the start and end location of a loop.
512   class LocRange {
513     DebugLoc Start;
514     DebugLoc End;
515 
516   public:
LocRange()517     LocRange() {}
LocRange(DebugLoc Start)518     LocRange(DebugLoc Start) : Start(Start), End(Start) {}
LocRange(DebugLoc Start,DebugLoc End)519     LocRange(DebugLoc Start, DebugLoc End)
520         : Start(std::move(Start)), End(std::move(End)) {}
521 
getStart()522     const DebugLoc &getStart() const { return Start; }
getEnd()523     const DebugLoc &getEnd() const { return End; }
524 
525     /// Check for null.
526     ///
527     explicit operator bool() const { return Start && End; }
528   };
529 
530   /// Return true if the specified value is loop invariant.
531   bool isLoopInvariant(const Value *V) const;
532 
533   /// Return true if all the operands of the specified instruction are loop
534   /// invariant.
535   bool hasLoopInvariantOperands(const Instruction *I) const;
536 
537   /// If the given value is an instruction inside of the loop and it can be
538   /// hoisted, do so to make it trivially loop-invariant.
539   /// Return true if the value after any hoisting is loop invariant. This
540   /// function can be used as a slightly more aggressive replacement for
541   /// isLoopInvariant.
542   ///
543   /// If InsertPt is specified, it is the point to hoist instructions to.
544   /// If null, the terminator of the loop preheader is used.
545   bool makeLoopInvariant(Value *V, bool &Changed,
546                          Instruction *InsertPt = nullptr,
547                          MemorySSAUpdater *MSSAU = nullptr) const;
548 
549   /// If the given instruction is inside of the loop and it can be hoisted, do
550   /// so to make it trivially loop-invariant.
551   /// Return true if the instruction after any hoisting is loop invariant. This
552   /// function can be used as a slightly more aggressive replacement for
553   /// isLoopInvariant.
554   ///
555   /// If InsertPt is specified, it is the point to hoist instructions to.
556   /// If null, the terminator of the loop preheader is used.
557   ///
558   bool makeLoopInvariant(Instruction *I, bool &Changed,
559                          Instruction *InsertPt = nullptr,
560                          MemorySSAUpdater *MSSAU = nullptr) const;
561 
562   /// Check to see if the loop has a canonical induction variable: an integer
563   /// recurrence that starts at 0 and increments by one each time through the
564   /// loop. If so, return the phi node that corresponds to it.
565   ///
566   /// The IndVarSimplify pass transforms loops to have a canonical induction
567   /// variable.
568   ///
569   PHINode *getCanonicalInductionVariable() const;
570 
571   /// Obtain the unique incoming and back edge. Return false if they are
572   /// non-unique or the loop is dead; otherwise, return true.
573   bool getIncomingAndBackEdge(BasicBlock *&Incoming,
574                               BasicBlock *&Backedge) const;
575 
576   /// Below are some utilities to get the loop guard, loop bounds and induction
577   /// variable, and to check if a given phinode is an auxiliary induction
578   /// variable, if the loop is guarded, and if the loop is canonical.
579   ///
580   /// Here is an example:
581   /// \code
582   /// for (int i = lb; i < ub; i+=step)
583   ///   <loop body>
584   /// --- pseudo LLVMIR ---
585   /// beforeloop:
586   ///   guardcmp = (lb < ub)
587   ///   if (guardcmp) goto preheader; else goto afterloop
588   /// preheader:
589   /// loop:
590   ///   i_1 = phi[{lb, preheader}, {i_2, latch}]
591   ///   <loop body>
592   ///   i_2 = i_1 + step
593   /// latch:
594   ///   cmp = (i_2 < ub)
595   ///   if (cmp) goto loop
596   /// exit:
597   /// afterloop:
598   /// \endcode
599   ///
600   /// - getBounds
601   ///   - getInitialIVValue      --> lb
602   ///   - getStepInst            --> i_2 = i_1 + step
603   ///   - getStepValue           --> step
604   ///   - getFinalIVValue        --> ub
605   ///   - getCanonicalPredicate  --> '<'
606   ///   - getDirection           --> Increasing
607   ///
608   /// - getInductionVariable            --> i_1
609   /// - isAuxiliaryInductionVariable(x) --> true if x == i_1
610   /// - getLoopGuardBranch()
611   ///                 --> `if (guardcmp) goto preheader; else goto afterloop`
612   /// - isGuarded()                     --> true
613   /// - isCanonical                     --> false
614   struct LoopBounds {
615     /// Return the LoopBounds object if
616     /// - the given \p IndVar is an induction variable
617     /// - the initial value of the induction variable can be found
618     /// - the step instruction of the induction variable can be found
619     /// - the final value of the induction variable can be found
620     ///
621     /// Else None.
622     static Optional<Loop::LoopBounds> getBounds(const Loop &L, PHINode &IndVar,
623                                                 ScalarEvolution &SE);
624 
625     /// Get the initial value of the loop induction variable.
getInitialIVValueLoopBounds626     Value &getInitialIVValue() const { return InitialIVValue; }
627 
628     /// Get the instruction that updates the loop induction variable.
getStepInstLoopBounds629     Instruction &getStepInst() const { return StepInst; }
630 
631     /// Get the step that the loop induction variable gets updated by in each
632     /// loop iteration. Return nullptr if not found.
getStepValueLoopBounds633     Value *getStepValue() const { return StepValue; }
634 
635     /// Get the final value of the loop induction variable.
getFinalIVValueLoopBounds636     Value &getFinalIVValue() const { return FinalIVValue; }
637 
638     /// Return the canonical predicate for the latch compare instruction, if
639     /// able to be calcuated. Else BAD_ICMP_PREDICATE.
640     ///
641     /// A predicate is considered as canonical if requirements below are all
642     /// satisfied:
643     /// 1. The first successor of the latch branch is the loop header
644     ///    If not, inverse the predicate.
645     /// 2. One of the operands of the latch comparison is StepInst
646     ///    If not, and
647     ///    - if the current calcuated predicate is not ne or eq, flip the
648     ///      predicate.
649     ///    - else if the loop is increasing, return slt
650     ///      (notice that it is safe to change from ne or eq to sign compare)
651     ///    - else if the loop is decreasing, return sgt
652     ///      (notice that it is safe to change from ne or eq to sign compare)
653     ///
654     /// Here is an example when both (1) and (2) are not satisfied:
655     /// \code
656     /// loop.header:
657     ///  %iv = phi [%initialiv, %loop.preheader], [%inc, %loop.header]
658     ///  %inc = add %iv, %step
659     ///  %cmp = slt %iv, %finaliv
660     ///  br %cmp, %loop.exit, %loop.header
661     /// loop.exit:
662     /// \endcode
663     /// - The second successor of the latch branch is the loop header instead
664     ///   of the first successor (slt -> sge)
665     /// - The first operand of the latch comparison (%cmp) is the IndVar (%iv)
666     ///   instead of the StepInst (%inc) (sge -> sgt)
667     ///
668     /// The predicate would be sgt if both (1) and (2) are satisfied.
669     /// getCanonicalPredicate() returns sgt for this example.
670     /// Note: The IR is not changed.
671     ICmpInst::Predicate getCanonicalPredicate() const;
672 
673     /// An enum for the direction of the loop
674     /// - for (int i = 0; i < ub; ++i)  --> Increasing
675     /// - for (int i = ub; i > 0; --i)  --> Descresing
676     /// - for (int i = x; i != y; i+=z) --> Unknown
677     enum class Direction { Increasing, Decreasing, Unknown };
678 
679     /// Get the direction of the loop.
680     Direction getDirection() const;
681 
682   private:
LoopBoundsLoopBounds683     LoopBounds(const Loop &Loop, Value &I, Instruction &SI, Value *SV, Value &F,
684                ScalarEvolution &SE)
685         : L(Loop), InitialIVValue(I), StepInst(SI), StepValue(SV),
686           FinalIVValue(F), SE(SE) {}
687 
688     const Loop &L;
689 
690     // The initial value of the loop induction variable
691     Value &InitialIVValue;
692 
693     // The instruction that updates the loop induction variable
694     Instruction &StepInst;
695 
696     // The value that the loop induction variable gets updated by in each loop
697     // iteration
698     Value *StepValue;
699 
700     // The final value of the loop induction variable
701     Value &FinalIVValue;
702 
703     ScalarEvolution &SE;
704   };
705 
706   /// Return the struct LoopBounds collected if all struct members are found,
707   /// else None.
708   Optional<LoopBounds> getBounds(ScalarEvolution &SE) const;
709 
710   /// Return the loop induction variable if found, else return nullptr.
711   /// An instruction is considered as the loop induction variable if
712   /// - it is an induction variable of the loop; and
713   /// - it is used to determine the condition of the branch in the loop latch
714   ///
715   /// Note: the induction variable doesn't need to be canonical, i.e. starts at
716   /// zero and increments by one each time through the loop (but it can be).
717   PHINode *getInductionVariable(ScalarEvolution &SE) const;
718 
719   /// Get the loop induction descriptor for the loop induction variable. Return
720   /// true if the loop induction variable is found.
721   bool getInductionDescriptor(ScalarEvolution &SE,
722                               InductionDescriptor &IndDesc) const;
723 
724   /// Return true if the given PHINode \p AuxIndVar is
725   /// - in the loop header
726   /// - not used outside of the loop
727   /// - incremented by a loop invariant step for each loop iteration
728   /// - step instruction opcode should be add or sub
729   /// Note: auxiliary induction variable is not required to be used in the
730   ///       conditional branch in the loop latch. (but it can be)
731   bool isAuxiliaryInductionVariable(PHINode &AuxIndVar,
732                                     ScalarEvolution &SE) const;
733 
734   /// Return the loop guard branch, if it exists.
735   ///
736   /// This currently only works on simplified loop, as it requires a preheader
737   /// and a latch to identify the guard. It will work on loops of the form:
738   /// \code
739   /// GuardBB:
740   ///   br cond1, Preheader, ExitSucc <== GuardBranch
741   /// Preheader:
742   ///   br Header
743   /// Header:
744   ///  ...
745   ///   br Latch
746   /// Latch:
747   ///   br cond2, Header, ExitBlock
748   /// ExitBlock:
749   ///   br ExitSucc
750   /// ExitSucc:
751   /// \endcode
752   BranchInst *getLoopGuardBranch() const;
753 
754   /// Return true iff the loop is
755   /// - in simplify rotated form, and
756   /// - guarded by a loop guard branch.
isGuarded()757   bool isGuarded() const { return (getLoopGuardBranch() != nullptr); }
758 
759   /// Return true if the loop is in rotated form.
760   ///
761   /// This does not check if the loop was rotated by loop rotation, instead it
762   /// only checks if the loop is in rotated form (has a valid latch that exists
763   /// the loop).
isRotatedForm()764   bool isRotatedForm() const {
765     assert(!isInvalid() && "Loop not in a valid state!");
766     BasicBlock *Latch = getLoopLatch();
767     return Latch && isLoopExiting(Latch);
768   }
769 
770   /// Return true if the loop induction variable starts at zero and increments
771   /// by one each time through the loop.
772   bool isCanonical(ScalarEvolution &SE) const;
773 
774   /// Return true if the Loop is in LCSSA form.
775   bool isLCSSAForm(DominatorTree &DT) const;
776 
777   /// Return true if this Loop and all inner subloops are in LCSSA form.
778   bool isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const;
779 
780   /// Return true if the Loop is in the form that the LoopSimplify form
781   /// transforms loops to, which is sometimes called normal form.
782   bool isLoopSimplifyForm() const;
783 
784   /// Return true if the loop body is safe to clone in practice.
785   bool isSafeToClone() const;
786 
787   /// Returns true if the loop is annotated parallel.
788   ///
789   /// A parallel loop can be assumed to not contain any dependencies between
790   /// iterations by the compiler. That is, any loop-carried dependency checking
791   /// can be skipped completely when parallelizing the loop on the target
792   /// machine. Thus, if the parallel loop information originates from the
793   /// programmer, e.g. via the OpenMP parallel for pragma, it is the
794   /// programmer's responsibility to ensure there are no loop-carried
795   /// dependencies. The final execution order of the instructions across
796   /// iterations is not guaranteed, thus, the end result might or might not
797   /// implement actual concurrent execution of instructions across multiple
798   /// iterations.
799   bool isAnnotatedParallel() const;
800 
801   /// Return the llvm.loop loop id metadata node for this loop if it is present.
802   ///
803   /// If this loop contains the same llvm.loop metadata on each branch to the
804   /// header then the node is returned. If any latch instruction does not
805   /// contain llvm.loop or if multiple latches contain different nodes then
806   /// 0 is returned.
807   MDNode *getLoopID() const;
808   /// Set the llvm.loop loop id metadata for this loop.
809   ///
810   /// The LoopID metadata node will be added to each terminator instruction in
811   /// the loop that branches to the loop header.
812   ///
813   /// The LoopID metadata node should have one or more operands and the first
814   /// operand should be the node itself.
815   void setLoopID(MDNode *LoopID) const;
816 
817   /// Add llvm.loop.unroll.disable to this loop's loop id metadata.
818   ///
819   /// Remove existing unroll metadata and add unroll disable metadata to
820   /// indicate the loop has already been unrolled.  This prevents a loop
821   /// from being unrolled more than is directed by a pragma if the loop
822   /// unrolling pass is run more than once (which it generally is).
823   void setLoopAlreadyUnrolled();
824 
825   void dump() const;
826   void dumpVerbose() const;
827 
828   /// Return the debug location of the start of this loop.
829   /// This looks for a BB terminating instruction with a known debug
830   /// location by looking at the preheader and header blocks. If it
831   /// cannot find a terminating instruction with location information,
832   /// it returns an unknown location.
833   DebugLoc getStartLoc() const;
834 
835   /// Return the source code span of the loop.
836   LocRange getLocRange() const;
837 
getName()838   StringRef getName() const {
839     if (BasicBlock *Header = getHeader())
840       if (Header->hasName())
841         return Header->getName();
842     return "<unnamed loop>";
843   }
844 
845 private:
846   Loop() = default;
847 
848   friend class LoopInfoBase<BasicBlock, Loop>;
849   friend class LoopBase<BasicBlock, Loop>;
Loop(BasicBlock * BB)850   explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
851   ~Loop() = default;
852 };
853 
854 //===----------------------------------------------------------------------===//
855 /// This class builds and contains all of the top-level loop
856 /// structures in the specified function.
857 ///
858 
859 template <class BlockT, class LoopT> class LoopInfoBase {
860   // BBMap - Mapping of basic blocks to the inner most loop they occur in
861   DenseMap<const BlockT *, LoopT *> BBMap;
862   std::vector<LoopT *> TopLevelLoops;
863   BumpPtrAllocator LoopAllocator;
864 
865   friend class LoopBase<BlockT, LoopT>;
866   friend class LoopInfo;
867 
868   void operator=(const LoopInfoBase &) = delete;
869   LoopInfoBase(const LoopInfoBase &) = delete;
870 
871 public:
LoopInfoBase()872   LoopInfoBase() {}
~LoopInfoBase()873   ~LoopInfoBase() { releaseMemory(); }
874 
LoopInfoBase(LoopInfoBase && Arg)875   LoopInfoBase(LoopInfoBase &&Arg)
876       : BBMap(std::move(Arg.BBMap)),
877         TopLevelLoops(std::move(Arg.TopLevelLoops)),
878         LoopAllocator(std::move(Arg.LoopAllocator)) {
879     // We have to clear the arguments top level loops as we've taken ownership.
880     Arg.TopLevelLoops.clear();
881   }
882   LoopInfoBase &operator=(LoopInfoBase &&RHS) {
883     BBMap = std::move(RHS.BBMap);
884 
885     for (auto *L : TopLevelLoops)
886       L->~LoopT();
887 
888     TopLevelLoops = std::move(RHS.TopLevelLoops);
889     LoopAllocator = std::move(RHS.LoopAllocator);
890     RHS.TopLevelLoops.clear();
891     return *this;
892   }
893 
releaseMemory()894   void releaseMemory() {
895     BBMap.clear();
896 
897     for (auto *L : TopLevelLoops)
898       L->~LoopT();
899     TopLevelLoops.clear();
900     LoopAllocator.Reset();
901   }
902 
AllocateLoop(ArgsTy &&...Args)903   template <typename... ArgsTy> LoopT *AllocateLoop(ArgsTy &&... Args) {
904     LoopT *Storage = LoopAllocator.Allocate<LoopT>();
905     return new (Storage) LoopT(std::forward<ArgsTy>(Args)...);
906   }
907 
908   /// iterator/begin/end - The interface to the top-level loops in the current
909   /// function.
910   ///
911   typedef typename std::vector<LoopT *>::const_iterator iterator;
912   typedef
913       typename std::vector<LoopT *>::const_reverse_iterator reverse_iterator;
begin()914   iterator begin() const { return TopLevelLoops.begin(); }
end()915   iterator end() const { return TopLevelLoops.end(); }
rbegin()916   reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
rend()917   reverse_iterator rend() const { return TopLevelLoops.rend(); }
empty()918   bool empty() const { return TopLevelLoops.empty(); }
919 
920   /// Return all of the loops in the function in preorder across the loop
921   /// nests, with siblings in forward program order.
922   ///
923   /// Note that because loops form a forest of trees, preorder is equivalent to
924   /// reverse postorder.
925   SmallVector<LoopT *, 4> getLoopsInPreorder();
926 
927   /// Return all of the loops in the function in preorder across the loop
928   /// nests, with siblings in *reverse* program order.
929   ///
930   /// Note that because loops form a forest of trees, preorder is equivalent to
931   /// reverse postorder.
932   ///
933   /// Also note that this is *not* a reverse preorder. Only the siblings are in
934   /// reverse program order.
935   SmallVector<LoopT *, 4> getLoopsInReverseSiblingPreorder();
936 
937   /// Return the inner most loop that BB lives in. If a basic block is in no
938   /// loop (for example the entry node), null is returned.
getLoopFor(const BlockT * BB)939   LoopT *getLoopFor(const BlockT *BB) const { return BBMap.lookup(BB); }
940 
941   /// Same as getLoopFor.
942   const LoopT *operator[](const BlockT *BB) const { return getLoopFor(BB); }
943 
944   /// Return the loop nesting level of the specified block. A depth of 0 means
945   /// the block is not inside any loop.
getLoopDepth(const BlockT * BB)946   unsigned getLoopDepth(const BlockT *BB) const {
947     const LoopT *L = getLoopFor(BB);
948     return L ? L->getLoopDepth() : 0;
949   }
950 
951   // True if the block is a loop header node
isLoopHeader(const BlockT * BB)952   bool isLoopHeader(const BlockT *BB) const {
953     const LoopT *L = getLoopFor(BB);
954     return L && L->getHeader() == BB;
955   }
956 
957   /// This removes the specified top-level loop from this loop info object.
958   /// The loop is not deleted, as it will presumably be inserted into
959   /// another loop.
removeLoop(iterator I)960   LoopT *removeLoop(iterator I) {
961     assert(I != end() && "Cannot remove end iterator!");
962     LoopT *L = *I;
963     assert(!L->getParentLoop() && "Not a top-level loop!");
964     TopLevelLoops.erase(TopLevelLoops.begin() + (I - begin()));
965     return L;
966   }
967 
968   /// Change the top-level loop that contains BB to the specified loop.
969   /// This should be used by transformations that restructure the loop hierarchy
970   /// tree.
changeLoopFor(BlockT * BB,LoopT * L)971   void changeLoopFor(BlockT *BB, LoopT *L) {
972     if (!L) {
973       BBMap.erase(BB);
974       return;
975     }
976     BBMap[BB] = L;
977   }
978 
979   /// Replace the specified loop in the top-level loops list with the indicated
980   /// loop.
changeTopLevelLoop(LoopT * OldLoop,LoopT * NewLoop)981   void changeTopLevelLoop(LoopT *OldLoop, LoopT *NewLoop) {
982     auto I = find(TopLevelLoops, OldLoop);
983     assert(I != TopLevelLoops.end() && "Old loop not at top level!");
984     *I = NewLoop;
985     assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
986            "Loops already embedded into a subloop!");
987   }
988 
989   /// This adds the specified loop to the collection of top-level loops.
addTopLevelLoop(LoopT * New)990   void addTopLevelLoop(LoopT *New) {
991     assert(!New->getParentLoop() && "Loop already in subloop!");
992     TopLevelLoops.push_back(New);
993   }
994 
995   /// This method completely removes BB from all data structures,
996   /// including all of the Loop objects it is nested in and our mapping from
997   /// BasicBlocks to loops.
removeBlock(BlockT * BB)998   void removeBlock(BlockT *BB) {
999     auto I = BBMap.find(BB);
1000     if (I != BBMap.end()) {
1001       for (LoopT *L = I->second; L; L = L->getParentLoop())
1002         L->removeBlockFromLoop(BB);
1003 
1004       BBMap.erase(I);
1005     }
1006   }
1007 
1008   // Internals
1009 
isNotAlreadyContainedIn(const LoopT * SubLoop,const LoopT * ParentLoop)1010   static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
1011                                       const LoopT *ParentLoop) {
1012     if (!SubLoop)
1013       return true;
1014     if (SubLoop == ParentLoop)
1015       return false;
1016     return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
1017   }
1018 
1019   /// Create the loop forest using a stable algorithm.
1020   void analyze(const DominatorTreeBase<BlockT, false> &DomTree);
1021 
1022   // Debugging
1023   void print(raw_ostream &OS) const;
1024 
1025   void verify(const DominatorTreeBase<BlockT, false> &DomTree) const;
1026 
1027   /// Destroy a loop that has been removed from the `LoopInfo` nest.
1028   ///
1029   /// This runs the destructor of the loop object making it invalid to
1030   /// reference afterward. The memory is retained so that the *pointer* to the
1031   /// loop remains valid.
1032   ///
1033   /// The caller is responsible for removing this loop from the loop nest and
1034   /// otherwise disconnecting it from the broader `LoopInfo` data structures.
1035   /// Callers that don't naturally handle this themselves should probably call
1036   /// `erase' instead.
destroy(LoopT * L)1037   void destroy(LoopT *L) {
1038     L->~LoopT();
1039 
1040     // Since LoopAllocator is a BumpPtrAllocator, this Deallocate only poisons
1041     // \c L, but the pointer remains valid for non-dereferencing uses.
1042     LoopAllocator.Deallocate(L);
1043   }
1044 };
1045 
1046 // Implementation in LoopInfoImpl.h
1047 extern template class LoopInfoBase<BasicBlock, Loop>;
1048 
1049 class LoopInfo : public LoopInfoBase<BasicBlock, Loop> {
1050   typedef LoopInfoBase<BasicBlock, Loop> BaseT;
1051 
1052   friend class LoopBase<BasicBlock, Loop>;
1053 
1054   void operator=(const LoopInfo &) = delete;
1055   LoopInfo(const LoopInfo &) = delete;
1056 
1057 public:
LoopInfo()1058   LoopInfo() {}
1059   explicit LoopInfo(const DominatorTreeBase<BasicBlock, false> &DomTree);
1060 
LoopInfo(LoopInfo && Arg)1061   LoopInfo(LoopInfo &&Arg) : BaseT(std::move(static_cast<BaseT &>(Arg))) {}
1062   LoopInfo &operator=(LoopInfo &&RHS) {
1063     BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
1064     return *this;
1065   }
1066 
1067   /// Handle invalidation explicitly.
1068   bool invalidate(Function &F, const PreservedAnalyses &PA,
1069                   FunctionAnalysisManager::Invalidator &);
1070 
1071   // Most of the public interface is provided via LoopInfoBase.
1072 
1073   /// Update LoopInfo after removing the last backedge from a loop. This updates
1074   /// the loop forest and parent loops for each block so that \c L is no longer
1075   /// referenced, but does not actually delete \c L immediately. The pointer
1076   /// will remain valid until this LoopInfo's memory is released.
1077   void erase(Loop *L);
1078 
1079   /// Returns true if replacing From with To everywhere is guaranteed to
1080   /// preserve LCSSA form.
replacementPreservesLCSSAForm(Instruction * From,Value * To)1081   bool replacementPreservesLCSSAForm(Instruction *From, Value *To) {
1082     // Preserving LCSSA form is only problematic if the replacing value is an
1083     // instruction.
1084     Instruction *I = dyn_cast<Instruction>(To);
1085     if (!I)
1086       return true;
1087     // If both instructions are defined in the same basic block then replacement
1088     // cannot break LCSSA form.
1089     if (I->getParent() == From->getParent())
1090       return true;
1091     // If the instruction is not defined in a loop then it can safely replace
1092     // anything.
1093     Loop *ToLoop = getLoopFor(I->getParent());
1094     if (!ToLoop)
1095       return true;
1096     // If the replacing instruction is defined in the same loop as the original
1097     // instruction, or in a loop that contains it as an inner loop, then using
1098     // it as a replacement will not break LCSSA form.
1099     return ToLoop->contains(getLoopFor(From->getParent()));
1100   }
1101 
1102   /// Checks if moving a specific instruction can break LCSSA in any loop.
1103   ///
1104   /// Return true if moving \p Inst to before \p NewLoc will break LCSSA,
1105   /// assuming that the function containing \p Inst and \p NewLoc is currently
1106   /// in LCSSA form.
movementPreservesLCSSAForm(Instruction * Inst,Instruction * NewLoc)1107   bool movementPreservesLCSSAForm(Instruction *Inst, Instruction *NewLoc) {
1108     assert(Inst->getFunction() == NewLoc->getFunction() &&
1109            "Can't reason about IPO!");
1110 
1111     auto *OldBB = Inst->getParent();
1112     auto *NewBB = NewLoc->getParent();
1113 
1114     // Movement within the same loop does not break LCSSA (the equality check is
1115     // to avoid doing a hashtable lookup in case of intra-block movement).
1116     if (OldBB == NewBB)
1117       return true;
1118 
1119     auto *OldLoop = getLoopFor(OldBB);
1120     auto *NewLoop = getLoopFor(NewBB);
1121 
1122     if (OldLoop == NewLoop)
1123       return true;
1124 
1125     // Check if Outer contains Inner; with the null loop counting as the
1126     // "outermost" loop.
1127     auto Contains = [](const Loop *Outer, const Loop *Inner) {
1128       return !Outer || Outer->contains(Inner);
1129     };
1130 
1131     // To check that the movement of Inst to before NewLoc does not break LCSSA,
1132     // we need to check two sets of uses for possible LCSSA violations at
1133     // NewLoc: the users of NewInst, and the operands of NewInst.
1134 
1135     // If we know we're hoisting Inst out of an inner loop to an outer loop,
1136     // then the uses *of* Inst don't need to be checked.
1137 
1138     if (!Contains(NewLoop, OldLoop)) {
1139       for (Use &U : Inst->uses()) {
1140         auto *UI = cast<Instruction>(U.getUser());
1141         auto *UBB = isa<PHINode>(UI) ? cast<PHINode>(UI)->getIncomingBlock(U)
1142                                      : UI->getParent();
1143         if (UBB != NewBB && getLoopFor(UBB) != NewLoop)
1144           return false;
1145       }
1146     }
1147 
1148     // If we know we're sinking Inst from an outer loop into an inner loop, then
1149     // the *operands* of Inst don't need to be checked.
1150 
1151     if (!Contains(OldLoop, NewLoop)) {
1152       // See below on why we can't handle phi nodes here.
1153       if (isa<PHINode>(Inst))
1154         return false;
1155 
1156       for (Use &U : Inst->operands()) {
1157         auto *DefI = dyn_cast<Instruction>(U.get());
1158         if (!DefI)
1159           return false;
1160 
1161         // This would need adjustment if we allow Inst to be a phi node -- the
1162         // new use block won't simply be NewBB.
1163 
1164         auto *DefBlock = DefI->getParent();
1165         if (DefBlock != NewBB && getLoopFor(DefBlock) != NewLoop)
1166           return false;
1167       }
1168     }
1169 
1170     return true;
1171   }
1172 };
1173 
1174 // Allow clients to walk the list of nested loops...
1175 template <> struct GraphTraits<const Loop *> {
1176   typedef const Loop *NodeRef;
1177   typedef LoopInfo::iterator ChildIteratorType;
1178 
1179   static NodeRef getEntryNode(const Loop *L) { return L; }
1180   static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
1181   static ChildIteratorType child_end(NodeRef N) { return N->end(); }
1182 };
1183 
1184 template <> struct GraphTraits<Loop *> {
1185   typedef Loop *NodeRef;
1186   typedef LoopInfo::iterator ChildIteratorType;
1187 
1188   static NodeRef getEntryNode(Loop *L) { return L; }
1189   static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
1190   static ChildIteratorType child_end(NodeRef N) { return N->end(); }
1191 };
1192 
1193 /// Analysis pass that exposes the \c LoopInfo for a function.
1194 class LoopAnalysis : public AnalysisInfoMixin<LoopAnalysis> {
1195   friend AnalysisInfoMixin<LoopAnalysis>;
1196   static AnalysisKey Key;
1197 
1198 public:
1199   typedef LoopInfo Result;
1200 
1201   LoopInfo run(Function &F, FunctionAnalysisManager &AM);
1202 };
1203 
1204 /// Printer pass for the \c LoopAnalysis results.
1205 class LoopPrinterPass : public PassInfoMixin<LoopPrinterPass> {
1206   raw_ostream &OS;
1207 
1208 public:
1209   explicit LoopPrinterPass(raw_ostream &OS) : OS(OS) {}
1210   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
1211 };
1212 
1213 /// Verifier pass for the \c LoopAnalysis results.
1214 struct LoopVerifierPass : public PassInfoMixin<LoopVerifierPass> {
1215   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
1216 };
1217 
1218 /// The legacy pass manager's analysis pass to compute loop information.
1219 class LoopInfoWrapperPass : public FunctionPass {
1220   LoopInfo LI;
1221 
1222 public:
1223   static char ID; // Pass identification, replacement for typeid
1224 
1225   LoopInfoWrapperPass();
1226 
1227   LoopInfo &getLoopInfo() { return LI; }
1228   const LoopInfo &getLoopInfo() const { return LI; }
1229 
1230   /// Calculate the natural loop information for a given function.
1231   bool runOnFunction(Function &F) override;
1232 
1233   void verifyAnalysis() const override;
1234 
1235   void releaseMemory() override { LI.releaseMemory(); }
1236 
1237   void print(raw_ostream &O, const Module *M = nullptr) const override;
1238 
1239   void getAnalysisUsage(AnalysisUsage &AU) const override;
1240 };
1241 
1242 /// Function to print a loop's contents as LLVM's text IR assembly.
1243 void printLoop(Loop &L, raw_ostream &OS, const std::string &Banner = "");
1244 
1245 /// Find and return the loop attribute node for the attribute @p Name in
1246 /// @p LoopID. Return nullptr if there is no such attribute.
1247 MDNode *findOptionMDForLoopID(MDNode *LoopID, StringRef Name);
1248 
1249 /// Find string metadata for a loop.
1250 ///
1251 /// Returns the MDNode where the first operand is the metadata's name. The
1252 /// following operands are the metadata's values. If no metadata with @p Name is
1253 /// found, return nullptr.
1254 MDNode *findOptionMDForLoop(const Loop *TheLoop, StringRef Name);
1255 
1256 /// Return whether an MDNode might represent an access group.
1257 ///
1258 /// Access group metadata nodes have to be distinct and empty. Being
1259 /// always-empty ensures that it never needs to be changed (which -- because
1260 /// MDNodes are designed immutable -- would require creating a new MDNode). Note
1261 /// that this is not a sufficient condition: not every distinct and empty NDNode
1262 /// is representing an access group.
1263 bool isValidAsAccessGroup(MDNode *AccGroup);
1264 
1265 /// Create a new LoopID after the loop has been transformed.
1266 ///
1267 /// This can be used when no follow-up loop attributes are defined
1268 /// (llvm::makeFollowupLoopID returning None) to stop transformations to be
1269 /// applied again.
1270 ///
1271 /// @param Context        The LLVMContext in which to create the new LoopID.
1272 /// @param OrigLoopID     The original LoopID; can be nullptr if the original
1273 ///                       loop has no LoopID.
1274 /// @param RemovePrefixes Remove all loop attributes that have these prefixes.
1275 ///                       Use to remove metadata of the transformation that has
1276 ///                       been applied.
1277 /// @param AddAttrs       Add these loop attributes to the new LoopID.
1278 ///
1279 /// @return A new LoopID that can be applied using Loop::setLoopID().
1280 llvm::MDNode *
1281 makePostTransformationMetadata(llvm::LLVMContext &Context, MDNode *OrigLoopID,
1282                                llvm::ArrayRef<llvm::StringRef> RemovePrefixes,
1283                                llvm::ArrayRef<llvm::MDNode *> AddAttrs);
1284 
1285 } // End llvm namespace
1286 
1287 #endif
1288