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