LLVM  10.0.0svn
LoopInfo.h
Go to the documentation of this file.
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;
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.
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  }
105  BlockT *getHeader() const { return getBlocks().front(); }
106  LoopT *getParentLoop() const { return ParentLoop; }
107 
108  /// This is a raw interface for bypassing addChildLoop.
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.
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.
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.
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.
136  const std::vector<LoopT *> &getSubLoops() const {
137  assert(!isInvalid() && "Loop not in a valid state!");
138  return SubLoops;
139  }
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;
147  iterator begin() const { return getSubLoops().begin(); }
148  iterator end() const { return getSubLoops().end(); }
149  reverse_iterator rbegin() const { return getSubLoops().rbegin(); }
150  reverse_iterator rend() const { return getSubLoops().rend(); }
151  bool empty() const { return getSubLoops().empty(); }
152 
153  /// Get a list of the basic blocks which make up this loop.
155  assert(!isInvalid() && "Loop not in a valid state!");
156  return Blocks;
157  }
159  block_iterator block_begin() const { return getBlocks().begin(); }
160  block_iterator block_end() const { return getBlocks().end(); }
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.
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`.
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.
182  assert(!isInvalid() && "Loop not in a valid state!");
183  return DenseBlockSet;
184  }
185 
186  /// Return a direct, immutable handle to the blocks set.
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.
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.
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.
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.
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.
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.
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>
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.
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  }
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.
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.
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.
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.
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
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
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).
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.
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.
454 
455  /// Returns true if the loop is annotated parallel.
456  ///
457  /// Derived classes can override this method using static template
458  /// polymorphism.
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.
468  LoopBase() : ParentLoop(nullptr) {}
469 
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.
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.
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:
517  LocRange() {}
518  LocRange(DebugLoc Start) : Start(Start), End(Start) {}
520  : Start(std::move(Start)), End(std::move(End)) {}
521 
522  const DebugLoc &getStart() const { return Start; }
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.
626  Value &getInitialIVValue() const { return InitialIVValue; }
627 
628  /// Get the instruction that updates the loop induction variable.
629  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.
633  Value *getStepValue() const { return StepValue; }
634 
635  /// Get the final value of the loop induction variable.
636  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:
683  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).
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.
757  bool isGuarded() const { return (getLoopGuardBranch() != nullptr); }
758 
759  /// Return true if the loop induction variable starts at zero and increments
760  /// by one each time through the loop.
761  bool isCanonical(ScalarEvolution &SE) const;
762 
763  /// Return true if the Loop is in LCSSA form.
764  bool isLCSSAForm(DominatorTree &DT) const;
765 
766  /// Return true if this Loop and all inner subloops are in LCSSA form.
767  bool isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const;
768 
769  /// Return true if the Loop is in the form that the LoopSimplify form
770  /// transforms loops to, which is sometimes called normal form.
771  bool isLoopSimplifyForm() const;
772 
773  /// Return true if the loop body is safe to clone in practice.
774  bool isSafeToClone() const;
775 
776  /// Returns true if the loop is annotated parallel.
777  ///
778  /// A parallel loop can be assumed to not contain any dependencies between
779  /// iterations by the compiler. That is, any loop-carried dependency checking
780  /// can be skipped completely when parallelizing the loop on the target
781  /// machine. Thus, if the parallel loop information originates from the
782  /// programmer, e.g. via the OpenMP parallel for pragma, it is the
783  /// programmer's responsibility to ensure there are no loop-carried
784  /// dependencies. The final execution order of the instructions across
785  /// iterations is not guaranteed, thus, the end result might or might not
786  /// implement actual concurrent execution of instructions across multiple
787  /// iterations.
788  bool isAnnotatedParallel() const;
789 
790  /// Return the llvm.loop loop id metadata node for this loop if it is present.
791  ///
792  /// If this loop contains the same llvm.loop metadata on each branch to the
793  /// header then the node is returned. If any latch instruction does not
794  /// contain llvm.loop or if multiple latches contain different nodes then
795  /// 0 is returned.
796  MDNode *getLoopID() const;
797  /// Set the llvm.loop loop id metadata for this loop.
798  ///
799  /// The LoopID metadata node will be added to each terminator instruction in
800  /// the loop that branches to the loop header.
801  ///
802  /// The LoopID metadata node should have one or more operands and the first
803  /// operand should be the node itself.
804  void setLoopID(MDNode *LoopID) const;
805 
806  /// Add llvm.loop.unroll.disable to this loop's loop id metadata.
807  ///
808  /// Remove existing unroll metadata and add unroll disable metadata to
809  /// indicate the loop has already been unrolled. This prevents a loop
810  /// from being unrolled more than is directed by a pragma if the loop
811  /// unrolling pass is run more than once (which it generally is).
812  void setLoopAlreadyUnrolled();
813 
814  void dump() const;
815  void dumpVerbose() const;
816 
817  /// Return the debug location of the start of this loop.
818  /// This looks for a BB terminating instruction with a known debug
819  /// location by looking at the preheader and header blocks. If it
820  /// cannot find a terminating instruction with location information,
821  /// it returns an unknown location.
822  DebugLoc getStartLoc() const;
823 
824  /// Return the source code span of the loop.
825  LocRange getLocRange() const;
826 
827  StringRef getName() const {
828  if (BasicBlock *Header = getHeader())
829  if (Header->hasName())
830  return Header->getName();
831  return "<unnamed loop>";
832  }
833 
834 private:
835  Loop() = default;
836 
839  explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
840  ~Loop() = default;
841 };
842 
843 //===----------------------------------------------------------------------===//
844 /// This class builds and contains all of the top-level loop
845 /// structures in the specified function.
846 ///
847 
848 template <class BlockT, class LoopT> class LoopInfoBase {
849  // BBMap - Mapping of basic blocks to the inner most loop they occur in
851  std::vector<LoopT *> TopLevelLoops;
852  BumpPtrAllocator LoopAllocator;
853 
855  friend class LoopInfo;
856 
857  void operator=(const LoopInfoBase &) = delete;
858  LoopInfoBase(const LoopInfoBase &) = delete;
859 
860 public:
862  ~LoopInfoBase() { releaseMemory(); }
863 
865  : BBMap(std::move(Arg.BBMap)),
866  TopLevelLoops(std::move(Arg.TopLevelLoops)),
867  LoopAllocator(std::move(Arg.LoopAllocator)) {
868  // We have to clear the arguments top level loops as we've taken ownership.
869  Arg.TopLevelLoops.clear();
870  }
872  BBMap = std::move(RHS.BBMap);
873 
874  for (auto *L : TopLevelLoops)
875  L->~LoopT();
876 
877  TopLevelLoops = std::move(RHS.TopLevelLoops);
878  LoopAllocator = std::move(RHS.LoopAllocator);
879  RHS.TopLevelLoops.clear();
880  return *this;
881  }
882 
883  void releaseMemory() {
884  BBMap.clear();
885 
886  for (auto *L : TopLevelLoops)
887  L->~LoopT();
888  TopLevelLoops.clear();
889  LoopAllocator.Reset();
890  }
891 
892  template <typename... ArgsTy> LoopT *AllocateLoop(ArgsTy &&... Args) {
893  LoopT *Storage = LoopAllocator.Allocate<LoopT>();
894  return new (Storage) LoopT(std::forward<ArgsTy>(Args)...);
895  }
896 
897  /// iterator/begin/end - The interface to the top-level loops in the current
898  /// function.
899  ///
900  typedef typename std::vector<LoopT *>::const_iterator iterator;
901  typedef
902  typename std::vector<LoopT *>::const_reverse_iterator reverse_iterator;
903  iterator begin() const { return TopLevelLoops.begin(); }
904  iterator end() const { return TopLevelLoops.end(); }
905  reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
906  reverse_iterator rend() const { return TopLevelLoops.rend(); }
907  bool empty() const { return TopLevelLoops.empty(); }
908 
909  /// Return all of the loops in the function in preorder across the loop
910  /// nests, with siblings in forward program order.
911  ///
912  /// Note that because loops form a forest of trees, preorder is equivalent to
913  /// reverse postorder.
915 
916  /// Return all of the loops in the function in preorder across the loop
917  /// nests, with siblings in *reverse* program order.
918  ///
919  /// Note that because loops form a forest of trees, preorder is equivalent to
920  /// reverse postorder.
921  ///
922  /// Also note that this is *not* a reverse preorder. Only the siblings are in
923  /// reverse program order.
924  SmallVector<LoopT *, 4> getLoopsInReverseSiblingPreorder();
925 
926  /// Return the inner most loop that BB lives in. If a basic block is in no
927  /// loop (for example the entry node), null is returned.
928  LoopT *getLoopFor(const BlockT *BB) const { return BBMap.lookup(BB); }
929 
930  /// Same as getLoopFor.
931  const LoopT *operator[](const BlockT *BB) const { return getLoopFor(BB); }
932 
933  /// Return the loop nesting level of the specified block. A depth of 0 means
934  /// the block is not inside any loop.
935  unsigned getLoopDepth(const BlockT *BB) const {
936  const LoopT *L = getLoopFor(BB);
937  return L ? L->getLoopDepth() : 0;
938  }
939 
940  // True if the block is a loop header node
941  bool isLoopHeader(const BlockT *BB) const {
942  const LoopT *L = getLoopFor(BB);
943  return L && L->getHeader() == BB;
944  }
945 
946  /// This removes the specified top-level loop from this loop info object.
947  /// The loop is not deleted, as it will presumably be inserted into
948  /// another loop.
949  LoopT *removeLoop(iterator I) {
950  assert(I != end() && "Cannot remove end iterator!");
951  LoopT *L = *I;
952  assert(!L->getParentLoop() && "Not a top-level loop!");
953  TopLevelLoops.erase(TopLevelLoops.begin() + (I - begin()));
954  return L;
955  }
956 
957  /// Change the top-level loop that contains BB to the specified loop.
958  /// This should be used by transformations that restructure the loop hierarchy
959  /// tree.
960  void changeLoopFor(BlockT *BB, LoopT *L) {
961  if (!L) {
962  BBMap.erase(BB);
963  return;
964  }
965  BBMap[BB] = L;
966  }
967 
968  /// Replace the specified loop in the top-level loops list with the indicated
969  /// loop.
970  void changeTopLevelLoop(LoopT *OldLoop, LoopT *NewLoop) {
971  auto I = find(TopLevelLoops, OldLoop);
972  assert(I != TopLevelLoops.end() && "Old loop not at top level!");
973  *I = NewLoop;
974  assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
975  "Loops already embedded into a subloop!");
976  }
977 
978  /// This adds the specified loop to the collection of top-level loops.
979  void addTopLevelLoop(LoopT *New) {
980  assert(!New->getParentLoop() && "Loop already in subloop!");
981  TopLevelLoops.push_back(New);
982  }
983 
984  /// This method completely removes BB from all data structures,
985  /// including all of the Loop objects it is nested in and our mapping from
986  /// BasicBlocks to loops.
987  void removeBlock(BlockT *BB) {
988  auto I = BBMap.find(BB);
989  if (I != BBMap.end()) {
990  for (LoopT *L = I->second; L; L = L->getParentLoop())
991  L->removeBlockFromLoop(BB);
992 
993  BBMap.erase(I);
994  }
995  }
996 
997  // Internals
998 
999  static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
1000  const LoopT *ParentLoop) {
1001  if (!SubLoop)
1002  return true;
1003  if (SubLoop == ParentLoop)
1004  return false;
1005  return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
1006  }
1007 
1008  /// Create the loop forest using a stable algorithm.
1009  void analyze(const DominatorTreeBase<BlockT, false> &DomTree);
1010 
1011  // Debugging
1012  void print(raw_ostream &OS) const;
1013 
1014  void verify(const DominatorTreeBase<BlockT, false> &DomTree) const;
1015 
1016  /// Destroy a loop that has been removed from the `LoopInfo` nest.
1017  ///
1018  /// This runs the destructor of the loop object making it invalid to
1019  /// reference afterward. The memory is retained so that the *pointer* to the
1020  /// loop remains valid.
1021  ///
1022  /// The caller is responsible for removing this loop from the loop nest and
1023  /// otherwise disconnecting it from the broader `LoopInfo` data structures.
1024  /// Callers that don't naturally handle this themselves should probably call
1025  /// `erase' instead.
1026  void destroy(LoopT *L) {
1027  L->~LoopT();
1028 
1029  // Since LoopAllocator is a BumpPtrAllocator, this Deallocate only poisons
1030  // \c L, but the pointer remains valid for non-dereferencing uses.
1031  LoopAllocator.Deallocate(L);
1032  }
1033 };
1034 
1035 // Implementation in LoopInfoImpl.h
1036 extern template class LoopInfoBase<BasicBlock, Loop>;
1037 
1040 
1042 
1043  void operator=(const LoopInfo &) = delete;
1044  LoopInfo(const LoopInfo &) = delete;
1045 
1046 public:
1048  explicit LoopInfo(const DominatorTreeBase<BasicBlock, false> &DomTree);
1049 
1050  LoopInfo(LoopInfo &&Arg) : BaseT(std::move(static_cast<BaseT &>(Arg))) {}
1052  BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
1053  return *this;
1054  }
1055 
1056  /// Handle invalidation explicitly.
1057  bool invalidate(Function &F, const PreservedAnalyses &PA,
1059 
1060  // Most of the public interface is provided via LoopInfoBase.
1061 
1062  /// Update LoopInfo after removing the last backedge from a loop. This updates
1063  /// the loop forest and parent loops for each block so that \c L is no longer
1064  /// referenced, but does not actually delete \c L immediately. The pointer
1065  /// will remain valid until this LoopInfo's memory is released.
1066  void erase(Loop *L);
1067 
1068  /// Returns true if replacing From with To everywhere is guaranteed to
1069  /// preserve LCSSA form.
1071  // Preserving LCSSA form is only problematic if the replacing value is an
1072  // instruction.
1074  if (!I)
1075  return true;
1076  // If both instructions are defined in the same basic block then replacement
1077  // cannot break LCSSA form.
1078  if (I->getParent() == From->getParent())
1079  return true;
1080  // If the instruction is not defined in a loop then it can safely replace
1081  // anything.
1082  Loop *ToLoop = getLoopFor(I->getParent());
1083  if (!ToLoop)
1084  return true;
1085  // If the replacing instruction is defined in the same loop as the original
1086  // instruction, or in a loop that contains it as an inner loop, then using
1087  // it as a replacement will not break LCSSA form.
1088  return ToLoop->contains(getLoopFor(From->getParent()));
1089  }
1090 
1091  /// Checks if moving a specific instruction can break LCSSA in any loop.
1092  ///
1093  /// Return true if moving \p Inst to before \p NewLoc will break LCSSA,
1094  /// assuming that the function containing \p Inst and \p NewLoc is currently
1095  /// in LCSSA form.
1097  assert(Inst->getFunction() == NewLoc->getFunction() &&
1098  "Can't reason about IPO!");
1099 
1100  auto *OldBB = Inst->getParent();
1101  auto *NewBB = NewLoc->getParent();
1102 
1103  // Movement within the same loop does not break LCSSA (the equality check is
1104  // to avoid doing a hashtable lookup in case of intra-block movement).
1105  if (OldBB == NewBB)
1106  return true;
1107 
1108  auto *OldLoop = getLoopFor(OldBB);
1109  auto *NewLoop = getLoopFor(NewBB);
1110 
1111  if (OldLoop == NewLoop)
1112  return true;
1113 
1114  // Check if Outer contains Inner; with the null loop counting as the
1115  // "outermost" loop.
1116  auto Contains = [](const Loop *Outer, const Loop *Inner) {
1117  return !Outer || Outer->contains(Inner);
1118  };
1119 
1120  // To check that the movement of Inst to before NewLoc does not break LCSSA,
1121  // we need to check two sets of uses for possible LCSSA violations at
1122  // NewLoc: the users of NewInst, and the operands of NewInst.
1123 
1124  // If we know we're hoisting Inst out of an inner loop to an outer loop,
1125  // then the uses *of* Inst don't need to be checked.
1126 
1127  if (!Contains(NewLoop, OldLoop)) {
1128  for (Use &U : Inst->uses()) {
1129  auto *UI = cast<Instruction>(U.getUser());
1130  auto *UBB = isa<PHINode>(UI) ? cast<PHINode>(UI)->getIncomingBlock(U)
1131  : UI->getParent();
1132  if (UBB != NewBB && getLoopFor(UBB) != NewLoop)
1133  return false;
1134  }
1135  }
1136 
1137  // If we know we're sinking Inst from an outer loop into an inner loop, then
1138  // the *operands* of Inst don't need to be checked.
1139 
1140  if (!Contains(OldLoop, NewLoop)) {
1141  // See below on why we can't handle phi nodes here.
1142  if (isa<PHINode>(Inst))
1143  return false;
1144 
1145  for (Use &U : Inst->operands()) {
1146  auto *DefI = dyn_cast<Instruction>(U.get());
1147  if (!DefI)
1148  return false;
1149 
1150  // This would need adjustment if we allow Inst to be a phi node -- the
1151  // new use block won't simply be NewBB.
1152 
1153  auto *DefBlock = DefI->getParent();
1154  if (DefBlock != NewBB && getLoopFor(DefBlock) != NewLoop)
1155  return false;
1156  }
1157  }
1158 
1159  return true;
1160  }
1161 };
1162 
1163 // Allow clients to walk the list of nested loops...
1164 template <> struct GraphTraits<const Loop *> {
1165  typedef const Loop *NodeRef;
1167 
1168  static NodeRef getEntryNode(const Loop *L) { return L; }
1169  static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
1170  static ChildIteratorType child_end(NodeRef N) { return N->end(); }
1171 };
1172 
1173 template <> struct GraphTraits<Loop *> {
1174  typedef Loop *NodeRef;
1176 
1177  static NodeRef getEntryNode(Loop *L) { return L; }
1178  static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
1179  static ChildIteratorType child_end(NodeRef N) { return N->end(); }
1180 };
1181 
1182 /// Analysis pass that exposes the \c LoopInfo for a function.
1183 class LoopAnalysis : public AnalysisInfoMixin<LoopAnalysis> {
1185  static AnalysisKey Key;
1186 
1187 public:
1188  typedef LoopInfo Result;
1189 
1191 };
1192 
1193 /// Printer pass for the \c LoopAnalysis results.
1194 class LoopPrinterPass : public PassInfoMixin<LoopPrinterPass> {
1195  raw_ostream &OS;
1196 
1197 public:
1198  explicit LoopPrinterPass(raw_ostream &OS) : OS(OS) {}
1200 };
1201 
1202 /// Verifier pass for the \c LoopAnalysis results.
1203 struct LoopVerifierPass : public PassInfoMixin<LoopVerifierPass> {
1205 };
1206 
1207 /// The legacy pass manager's analysis pass to compute loop information.
1209  LoopInfo LI;
1210 
1211 public:
1212  static char ID; // Pass identification, replacement for typeid
1213 
1216  }
1217 
1218  LoopInfo &getLoopInfo() { return LI; }
1219  const LoopInfo &getLoopInfo() const { return LI; }
1220 
1221  /// Calculate the natural loop information for a given function.
1222  bool runOnFunction(Function &F) override;
1223 
1224  void verifyAnalysis() const override;
1225 
1226  void releaseMemory() override { LI.releaseMemory(); }
1227 
1228  void print(raw_ostream &O, const Module *M = nullptr) const override;
1229 
1230  void getAnalysisUsage(AnalysisUsage &AU) const override;
1231 };
1232 
1233 /// Function to print a loop's contents as LLVM's text IR assembly.
1234 void printLoop(Loop &L, raw_ostream &OS, const std::string &Banner = "");
1235 
1236 /// Find and return the loop attribute node for the attribute @p Name in
1237 /// @p LoopID. Return nullptr if there is no such attribute.
1239 
1240 /// Find string metadata for a loop.
1241 ///
1242 /// Returns the MDNode where the first operand is the metadata's name. The
1243 /// following operands are the metadata's values. If no metadata with @p Name is
1244 /// found, return nullptr.
1245 MDNode *findOptionMDForLoop(const Loop *TheLoop, StringRef Name);
1246 
1247 /// Return whether an MDNode might represent an access group.
1248 ///
1249 /// Access group metadata nodes have to be distinct and empty. Being
1250 /// always-empty ensures that it never needs to be changed (which -- because
1251 /// MDNodes are designed immutable -- would require creating a new MDNode). Note
1252 /// that this is not a sufficient condition: not every distinct and empty NDNode
1253 /// is representing an access group.
1254 bool isValidAsAccessGroup(MDNode *AccGroup);
1255 
1256 /// Create a new LoopID after the loop has been transformed.
1257 ///
1258 /// This can be used when no follow-up loop attributes are defined
1259 /// (llvm::makeFollowupLoopID returning None) to stop transformations to be
1260 /// applied again.
1261 ///
1262 /// @param Context The LLVMContext in which to create the new LoopID.
1263 /// @param OrigLoopID The original LoopID; can be nullptr if the original
1264 /// loop has no LoopID.
1265 /// @param RemovePrefixes Remove all loop attributes that have these prefixes.
1266 /// Use to remove metadata of the transformation that has
1267 /// been applied.
1268 /// @param AddAttrs Add these loop attributes to the new LoopID.
1269 ///
1270 /// @return A new LoopID that can be applied using Loop::setLoopID().
1271 llvm::MDNode *
1273  llvm::ArrayRef<llvm::StringRef> RemovePrefixes,
1275 
1276 } // End llvm namespace
1277 
1278 #endif
LoopInfo::iterator ChildIteratorType
Definition: LoopInfo.h:1166
SmallVector< const LoopT *, 4 > getLoopsInPreorder() const
Return all loops in the loop nest rooted by the loop in preorder, with siblings in forward program or...
Definition: LoopInfo.h:341
void destroy(LoopT *L)
Destroy a loop that has been removed from the LoopInfo nest.
Definition: LoopInfo.h:1026
iterator_range< typename GraphTraits< GraphType >::ChildIteratorType > children(const typename GraphTraits< GraphType >::NodeRef &G)
Definition: GraphTraits.h:121
iterator_range< use_iterator > uses()
Definition: Value.h:374
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
Definition: LoopInfoImpl.h:211
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
std::vector< BlockT * > & getBlocksVector()
Return a direct, mutable handle to the blocks vector so that we can mutate it efficiently with techni...
Definition: LoopInfo.h:175
LLVMContext & Context
bool empty() const
Definition: LoopInfo.h:907
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void setParentLoop(LoopT *L)
This is a raw interface for bypassing addChildLoop.
Definition: LoopInfo.h:109
ArrayRef< BlockT * >::const_iterator block_iterator
Definition: LoopInfo.h:158
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
bool hasDedicatedExits() const
Return true if no exit block for the loop has a predecessor that is outside the loop.
Definition: LoopInfoImpl.h:85
unsigned getLoopDepth(const BlockT *BB) const
Return the loop nesting level of the specified block.
Definition: LoopInfo.h:935
Implements a dense probed hash-table based set.
Definition: DenseSet.h:249
unsigned getLoopDepth() const
Return the nesting level of this loop.
Definition: LoopInfo.h:97
void push_back(const T &Elt)
Definition: SmallVector.h:211
void moveToHeader(BlockT *BB)
This method is used to move BB (which must be part of this loop) to be the loop header of the loop (t...
Definition: LoopInfo.h:423
llvm::MDNode * makePostTransformationMetadata(llvm::LLVMContext &Context, MDNode *OrigLoopID, llvm::ArrayRef< llvm::StringRef > RemovePrefixes, llvm::ArrayRef< llvm::MDNode *> AddAttrs)
Create a new LoopID after the loop has been transformed.
Definition: LoopInfo.cpp:1006
void reserveBlocks(unsigned size)
interface to do reserve() for Blocks
Definition: LoopInfo.h:416
LoopT * removeChildLoop(iterator I)
This removes the specified child from being a subloop of this loop.
Definition: LoopInfo.h:384
The main scalar evolution driver.
SmallVector< LoopT *, 4 > getLoopsInPreorder()
Definition: LoopInfo.h:348
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
Definition: LoopInfoImpl.h:160
void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild)
This is used when splitting loops up.
Definition: LoopInfoImpl.h:267
std::vector< LoopT * > & getSubLoopsVector()
Definition: LoopInfo.h:140
static bool isCanonical(const MDString *S)
A debug info location.
Definition: DebugLoc.h:33
Metadata node.
Definition: Metadata.h:863
F(f)
const SmallPtrSetImpl< const BlockT * > & getBlocksSet() const
Return a direct, immutable handle to the blocks set.
Definition: LoopInfo.h:187
static void getInnerLoopsInPreorder(const LoopT &L, SmallVectorImpl< Type > &PreOrderLoops)
Return all inner loops in the loop nest rooted by the loop in preorder, with siblings in forward prog...
Definition: LoopInfo.h:325
LoopBase()
This creates an empty loop.
Definition: LoopInfo.h:468
Instances of this class are used to represent loops that are detected in the flow graph...
Definition: LoopInfo.h:68
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
bool isInvalid() const
Return true if this loop is no longer valid.
Definition: LoopInfo.h:198
void print(raw_ostream &OS, unsigned Depth=0, bool Verbose=false) const
Print loop with all the BBs inside it.
Definition: LoopInfoImpl.h:379
Hexagon Hardware Loops
Definition: BitVector.h:937
unsigned getNumBackEdges() const
Calculate the number of back edges to the loop header.
Definition: LoopInfo.h:233
void initializeLoopInfoWrapperPassPass(PassRegistry &)
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it...
Definition: Allocator.h:194
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Definition: LoopInfo.h:928
SmallPtrSetImpl< const BlockT * > & getBlocksSet()
Return a direct, mutable handle to the blocks set so that we can mutate it efficiently.
Definition: LoopInfo.h:181
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
void addBlockEntry(BlockT *BB)
This adds a basic block directly to the basic block list.
Definition: LoopInfo.h:403
static bool isNotAlreadyContainedIn(const LoopT *SubLoop, const LoopT *ParentLoop)
Definition: LoopInfo.h:999
std::vector< LoopT * >::const_iterator iterator
iterator/begin/end - The interface to the top-level loops in the current function.
Definition: LoopInfo.h:900
void getLoopLatches(SmallVectorImpl< BlockT *> &LoopLatches) const
Return all loop latch blocks of this loop.
Definition: LoopInfo.h:314
bool isAnnotatedParallel() const
Returns true if the loop is annotated parallel.
Definition: LoopInfo.h:459
Printer pass for the LoopAnalysis results.
Definition: LoopInfo.h:1194
Direction
An enum for the direction of the loop.
Definition: LoopInfo.h:677
Analysis pass that exposes the LoopInfo for a function.
Definition: LoopInfo.h:1183
void Deallocate(const void *Ptr, size_t Size)
Definition: Allocator.h:277
BlockT * getHeader() const
Definition: LoopInfo.h:105
void getExitBlocks(SmallVectorImpl< BlockT *> &ExitBlocks) const
Return all of the successor blocks of this loop.
Definition: LoopInfoImpl.h:62
const LoopInfo & getLoopInfo() const
Definition: LoopInfo.h:1219
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Definition: STLExtras.h:261
std::vector< LoopT * >::const_iterator iterator
Definition: LoopInfo.h:144
Key
PAL metadata keys.
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
This method is used by other analyses to update loop information.
Definition: LoopInfoImpl.h:237
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:372
MDNode * findOptionMDForLoop(const Loop *TheLoop, StringRef Name)
Find string metadata for a loop.
Definition: LoopInfo.cpp:998
void addTopLevelLoop(LoopT *New)
This adds the specified loop to the collection of top-level loops.
Definition: LoopInfo.h:979
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
LoopInfo & operator=(LoopInfo &&RHS)
Definition: LoopInfo.h:1051
LocRange(DebugLoc Start, DebugLoc End)
Definition: LoopInfo.h:519
const DebugLoc & getEnd() const
Definition: LoopInfo.h:523
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
Definition: LoopInfo.h:1226
Core dominator tree base class.
Definition: LoopInfo.h:66
static bool runOnFunction(Function &F, bool PostInlining)
reverse_iterator rend() const
Definition: LoopInfo.h:150
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
reverse_iterator rbegin() const
Definition: LoopInfo.h:149
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
bool isLoopExiting(const BlockT *BB) const
True if terminator in the block can branch to another block that is outside of the current loop...
Definition: LoopInfo.h:208
Value & getFinalIVValue() const
Get the final value of the loop induction variable.
Definition: LoopInfo.h:636
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:140
Conditional or Unconditional Branch instruction.
std::pair< BlockT *, BlockT * > Edge
Edge type.
Definition: LoopInfo.h:289
static ChildIteratorType child_begin(NodeRef N)
Definition: LoopInfo.h:1169
iterator end() const
Definition: LoopInfo.h:904
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
Allocate space at the specified alignment.
Definition: Allocator.h:214
#define H(x, y, z)
Definition: MD5.cpp:57
LoopPrinterPass(raw_ostream &OS)
Definition: LoopInfo.h:1198
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:370
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:389
Represent the analysis usage information of a pass.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:732
bool contains(const BlockT *BB) const
Return true if the specified basic block is in this loop.
Definition: LoopInfo.h:125
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
op_range operands()
Definition: User.h:237
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:381
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:59
BlockT * getExitBlock() const
If getExitBlocks would return exactly one block, return that block.
Definition: LoopInfoImpl.h:75
void getExitingBlocks(SmallVectorImpl< BlockT *> &ExitingBlocks) const
Return all blocks inside the loop that have successors outside of the loop.
Definition: LoopInfoImpl.h:34
BlockT * getUniqueExitBlock() const
If getUniqueExitBlocks would return exactly one block, return that block.
Definition: LoopInfoImpl.h:131
A range representing the start and end location of a loop.
Definition: LoopInfo.h:512
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1186
void getUniqueNonLatchExitBlocks(SmallVectorImpl< BlockT *> &ExitBlocks) const
Return all unique successor blocks of this loop except successors from Latch block are not considered...
Definition: LoopInfoImpl.h:122
bool verify(const TargetRegisterInfo &TRI) const
Check that information hold by this instance make sense for the given TRI.
Value & getInitialIVValue() const
Get the initial value of the loop induction variable.
Definition: LoopInfo.h:626
void getExitEdges(SmallVectorImpl< Edge > &ExitEdges) const
Return all pairs of (inside_block,outside_block).
Definition: LoopInfoImpl.h:141
Instruction & getStepInst() const
Get the instruction that updates the loop induction variable.
Definition: LoopInfo.h:629
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LoopInfoBase & operator=(LoopInfoBase &&RHS)
Definition: LoopInfo.h:871
BlockT * getLoopPredecessor() const
If the given loop&#39;s header has exactly one unique predecessor outside the loop, return it...
Definition: LoopInfoImpl.h:188
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:115
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1146
A struct for saving information about induction variables.
BlockVerifier::State From
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false...
Definition: SmallPtrSet.h:377
bool movementPreservesLCSSAForm(Instruction *Inst, Instruction *NewLoc)
Checks if moving a specific instruction can break LCSSA in any loop.
Definition: LoopInfo.h:1096
reverse_iterator rend() const
Definition: LoopInfo.h:906
Verifier pass for the LoopAnalysis results.
Definition: LoopInfo.h:1203
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
const T * const_iterator
Definition: ArrayRef.h:44
iterator begin() const
Definition: LoopInfo.h:147
LoopInfo & getLoopInfo()
Definition: LoopInfo.h:1218
LoopT * removeLoop(iterator I)
This removes the specified top-level loop from this loop info object.
Definition: LoopInfo.h:949
bool contains(const InstT *Inst) const
Return true if the specified instruction is in this loop.
Definition: LoopInfo.h:131
MDNode * findOptionMDForLoopID(MDNode *LoopID, StringRef Name)
Find and return the loop attribute node for the attribute Name in LoopID.
Definition: LoopInfo.cpp:972
static NodeRef getEntryNode(const Loop *L)
Definition: LoopInfo.h:1168
LoopT * AllocateLoop(ArgsTy &&... Args)
Definition: LoopInfo.h:892
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:374
LoopInfo(LoopInfo &&Arg)
Definition: LoopInfo.h:1050
static ChildIteratorType child_end(NodeRef N)
Definition: LoopInfo.h:1170
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
std::vector< LoopT * >::const_reverse_iterator reverse_iterator
Definition: LoopInfo.h:146
bool isLoopLatch(const BlockT *BB) const
Definition: LoopInfo.h:222
iterator begin() const
Definition: LoopInfo.h:903
A range adaptor for a pair of iterators.
LoopInfo::iterator ChildIteratorType
Definition: LoopInfo.h:1175
bool isLoopHeader(const BlockT *BB) const
Definition: LoopInfo.h:941
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:387
reverse_iterator rbegin() const
Definition: LoopInfo.h:905
LoopT * getParentLoop() const
Definition: LoopInfo.h:106
const std::vector< LoopT * > & getSubLoops() const
Return the loops contained entirely within this loop.
Definition: LoopInfo.h:136
unsigned getNumBlocks() const
Get the number of blocks in this loop in constant time.
Definition: LoopInfo.h:168
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
void verifyLoopNest(DenseSet< const LoopT *> *Loops) const
Verify loop structure of this loop and all nested loops.
Definition: LoopInfoImpl.h:367
static NodeRef getEntryNode(Loop *L)
Definition: LoopInfo.h:1177
const DebugLoc & getStart() const
Definition: LoopInfo.h:522
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
Definition: LoopInfo.h:375
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
StringRef getName() const
Definition: LoopInfo.h:827
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:509
ArrayRef< BlockT * > getBlocks() const
Get a list of the basic blocks which make up this loop.
Definition: LoopInfo.h:154
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
Below are some utilities to get the loop guard, loop bounds and induction variable, and to check if a given phinode is an auxiliary induction variable, if the loop is guarded, and if the loop is canonical.
Definition: LoopInfo.h:614
static ChildIteratorType child_end(NodeRef N)
Definition: LoopInfo.h:1179
void changeTopLevelLoop(LoopT *OldLoop, LoopT *NewLoop)
Replace the specified loop in the top-level loops list with the indicated loop.
Definition: LoopInfo.h:970
iterator end() const
Definition: LoopInfo.h:148
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
block_iterator block_end() const
Definition: LoopInfo.h:160
void changeLoopFor(BlockT *BB, LoopT *L)
Change the top-level loop that contains BB to the specified loop.
Definition: LoopInfo.h:960
friend class LoopInfo
Definition: LoopInfo.h:855
void removeBlockFromLoop(BlockT *BB)
This removes the specified basic block from the current loop, updating the Blocks as appropriate...
Definition: LoopInfo.h:440
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:185
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:648
bool empty() const
Definition: LoopInfo.h:151
LoopT * removeChildLoop(LoopT *Child)
This removes the specified child from being a subloop of this loop.
Definition: LoopInfo.h:396
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
aarch64 promote const
LLVM Value Representation.
Definition: Value.h:73
const LoopT * operator[](const BlockT *BB) const
Same as getLoopFor.
Definition: LoopInfo.h:931
void reverseBlock(unsigned from)
interface to reverse Blocks[from, end of loop] in this loop
Definition: LoopInfo.h:410
Value * getStepValue() const
Get the step that the loop induction variable gets updated by in each loop iteration.
Definition: LoopInfo.h:633
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
The legacy pass manager&#39;s analysis pass to compute loop information.
Definition: LoopInfo.h:1208
void verifyLoop() const
Verify loop structure.
Definition: LoopInfoImpl.h:281
void getUniqueExitBlocks(SmallVectorImpl< BlockT *> &ExitBlocks) const
Return all unique successor blocks of this loop.
Definition: LoopInfoImpl.h:115
LoopInfoBase(LoopInfoBase &&Arg)
Definition: LoopInfo.h:864
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
A container for analyses that lazily runs them and caches their results.
void releaseMemory()
Definition: LoopInfo.h:883
std::vector< LoopT * >::const_reverse_iterator reverse_iterator
Definition: LoopInfo.h:902
bool replacementPreservesLCSSAForm(Instruction *From, Value *To)
Returns true if replacing From with To everywhere is guaranteed to preserve LCSSA form...
Definition: LoopInfo.h:1070
This header defines various interfaces for pass management in LLVM.
iterator_range< block_iterator > blocks() const
Definition: LoopInfo.h:161
void printLoop(Loop &L, raw_ostream &OS, const std::string &Banner="")
Function to print a loop&#39;s contents as LLVM&#39;s text IR assembly.
Definition: LoopInfo.cpp:932
bool isGuarded() const
Return true iff the loop is.
Definition: LoopInfo.h:757
block_iterator block_begin() const
Definition: LoopInfo.h:159
BlockT * getExitingBlock() const
If getExitingBlocks would return exactly one block, return that block.
Definition: LoopInfoImpl.h:49
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:70
static ChildIteratorType child_begin(NodeRef N)
Definition: LoopInfo.h:1178
void removeBlock(BlockT *BB)
This method completely removes BB from all data structures, including all of the Loop objects it is n...
Definition: LoopInfo.h:987
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
bool isValidAsAccessGroup(MDNode *AccGroup)
Return whether an MDNode might represent an access group.
Definition: LoopInfo.cpp:1002
const BasicBlock * getParent() const
Definition: Instruction.h:66
LoopBase(BlockT *BB)
Definition: LoopInfo.h:470
static PHINode * getInductionVariable(Loop *L, ScalarEvolution *SE)
LocRange(DebugLoc Start)
Definition: LoopInfo.h:518
This class builds and contains all of the top-level loop structures in the specified function...
Definition: LoopInfo.h:67