LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LoopInfo.h
Go to the documentation of this file.
1 //===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the LoopInfo class that is used to identify natural loops
11 // and determine the loop depth of various nodes of the CFG. A natural loop
12 // has exactly one entry-point, which is called the header. Note that natural
13 // loops may actually be several loops that share the same header node.
14 //
15 // This analysis calculates the nesting structure of loops in a function. For
16 // each natural loop identified, this analysis identifies natural loops
17 // contained entirely within the loop and the basic blocks the make up the loop.
18 //
19 // It can calculate on the fly various bits of information, for example:
20 //
21 // * whether there is a preheader for the loop
22 // * the number of back edges to the header
23 // * whether or not a particular block branches out of the loop
24 // * the successor blocks of the loop
25 // * the loop depth
26 // * etc...
27 //
28 //===----------------------------------------------------------------------===//
29 
30 #ifndef LLVM_ANALYSIS_LOOPINFO_H
31 #define LLVM_ANALYSIS_LOOPINFO_H
32 
33 #include "llvm/ADT/DenseMap.h"
34 #include "llvm/ADT/DenseSet.h"
35 #include "llvm/ADT/GraphTraits.h"
36 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/Pass.h"
39 #include <algorithm>
40 
41 namespace llvm {
42 
43 template<typename T>
44 inline void RemoveFromVector(std::vector<T*> &V, T *N) {
45  typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N);
46  assert(I != V.end() && "N is not in this list!");
47  V.erase(I);
48 }
49 
50 class DominatorTree;
51 class LoopInfo;
52 class Loop;
53 class MDNode;
54 class PHINode;
55 class raw_ostream;
56 template<class N, class M> class LoopInfoBase;
57 template<class N, class M> class LoopBase;
58 
59 //===----------------------------------------------------------------------===//
60 /// LoopBase class - Instances of this class are used to represent loops that
61 /// are detected in the flow graph
62 ///
63 template<class BlockT, class LoopT>
64 class LoopBase {
65  LoopT *ParentLoop;
66  // SubLoops - Loops contained entirely within this one.
67  std::vector<LoopT *> SubLoops;
68 
69  // Blocks - The list of blocks in this loop. First entry is the header node.
70  std::vector<BlockT*> Blocks;
71 
72  SmallPtrSet<const BlockT*, 8> DenseBlockSet;
73 
75  const LoopBase<BlockT, LoopT>&
76  operator=(const LoopBase<BlockT, LoopT> &) LLVM_DELETED_FUNCTION;
77 public:
78  /// Loop ctor - This creates an empty loop.
79  LoopBase() : ParentLoop(0) {}
81  for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
82  delete SubLoops[i];
83  }
84 
85  /// getLoopDepth - Return the nesting level of this loop. An outer-most
86  /// loop has depth 1, for consistency with loop depth values used for basic
87  /// blocks, where depth 0 is used for blocks not inside any loops.
88  unsigned getLoopDepth() const {
89  unsigned D = 1;
90  for (const LoopT *CurLoop = ParentLoop; CurLoop;
91  CurLoop = CurLoop->ParentLoop)
92  ++D;
93  return D;
94  }
95  BlockT *getHeader() const { return Blocks.front(); }
96  LoopT *getParentLoop() const { return ParentLoop; }
97 
98  /// setParentLoop is a raw interface for bypassing addChildLoop.
99  void setParentLoop(LoopT *L) { ParentLoop = L; }
100 
101  /// contains - Return true if the specified loop is contained within in
102  /// this loop.
103  ///
104  bool contains(const LoopT *L) const {
105  if (L == this) return true;
106  if (L == 0) return false;
107  return contains(L->getParentLoop());
108  }
109 
110  /// contains - Return true if the specified basic block is in this loop.
111  ///
112  bool contains(const BlockT *BB) const {
113  return DenseBlockSet.count(BB);
114  }
115 
116  /// contains - Return true if the specified instruction is in this loop.
117  ///
118  template<class InstT>
119  bool contains(const InstT *Inst) const {
120  return contains(Inst->getParent());
121  }
122 
123  /// iterator/begin/end - Return the loops contained entirely within this loop.
124  ///
125  const std::vector<LoopT *> &getSubLoops() const { return SubLoops; }
126  std::vector<LoopT *> &getSubLoopsVector() { return SubLoops; }
127  typedef typename std::vector<LoopT *>::const_iterator iterator;
128  typedef typename std::vector<LoopT *>::const_reverse_iterator
130  iterator begin() const { return SubLoops.begin(); }
131  iterator end() const { return SubLoops.end(); }
132  reverse_iterator rbegin() const { return SubLoops.rbegin(); }
133  reverse_iterator rend() const { return SubLoops.rend(); }
134  bool empty() const { return SubLoops.empty(); }
135 
136  /// getBlocks - Get a list of the basic blocks which make up this loop.
137  ///
138  const std::vector<BlockT*> &getBlocks() const { return Blocks; }
139  typedef typename std::vector<BlockT*>::const_iterator block_iterator;
140  block_iterator block_begin() const { return Blocks.begin(); }
141  block_iterator block_end() const { return Blocks.end(); }
142 
143  /// getNumBlocks - Get the number of blocks in this loop in constant time.
144  unsigned getNumBlocks() const {
145  return Blocks.size();
146  }
147 
148  /// isLoopExiting - True if terminator in the block can branch to another
149  /// block that is outside of the current loop.
150  ///
151  bool isLoopExiting(const BlockT *BB) const {
152  typedef GraphTraits<const BlockT*> BlockTraits;
153  for (typename BlockTraits::ChildIteratorType SI =
154  BlockTraits::child_begin(BB),
155  SE = BlockTraits::child_end(BB); SI != SE; ++SI) {
156  if (!contains(*SI))
157  return true;
158  }
159  return false;
160  }
161 
162  /// getNumBackEdges - Calculate the number of back edges to the loop header
163  ///
164  unsigned getNumBackEdges() const {
165  unsigned NumBackEdges = 0;
166  BlockT *H = getHeader();
167 
168  typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
169  for (typename InvBlockTraits::ChildIteratorType I =
170  InvBlockTraits::child_begin(H),
171  E = InvBlockTraits::child_end(H); I != E; ++I)
172  if (contains(*I))
173  ++NumBackEdges;
174 
175  return NumBackEdges;
176  }
177 
178  //===--------------------------------------------------------------------===//
179  // APIs for simple analysis of the loop.
180  //
181  // Note that all of these methods can fail on general loops (ie, there may not
182  // be a preheader, etc). For best success, the loop simplification and
183  // induction variable canonicalization pass should be used to normalize loops
184  // for easy analysis. These methods assume canonical loops.
185 
186  /// getExitingBlocks - Return all blocks inside the loop that have successors
187  /// outside of the loop. These are the blocks _inside of the current loop_
188  /// which branch out. The returned list is always unique.
189  ///
190  void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const;
191 
192  /// getExitingBlock - If getExitingBlocks would return exactly one block,
193  /// return that block. Otherwise return null.
194  BlockT *getExitingBlock() const;
195 
196  /// getExitBlocks - Return all of the successor blocks of this loop. These
197  /// are the blocks _outside of the current loop_ which are branched to.
198  ///
199  void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const;
200 
201  /// getExitBlock - If getExitBlocks would return exactly one block,
202  /// return that block. Otherwise return null.
203  BlockT *getExitBlock() const;
204 
205  /// Edge type.
206  typedef std::pair<const BlockT*, const BlockT*> Edge;
207 
208  /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
209  void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const;
210 
211  /// getLoopPreheader - If there is a preheader for this loop, return it. A
212  /// loop has a preheader if there is only one edge to the header of the loop
213  /// from outside of the loop. If this is the case, the block branching to the
214  /// header of the loop is the preheader node.
215  ///
216  /// This method returns null if there is no preheader for the loop.
217  ///
218  BlockT *getLoopPreheader() const;
219 
220  /// getLoopPredecessor - If the given loop's header has exactly one unique
221  /// predecessor outside the loop, return it. Otherwise return null.
222  /// This is less strict that the loop "preheader" concept, which requires
223  /// the predecessor to have exactly one successor.
224  ///
225  BlockT *getLoopPredecessor() const;
226 
227  /// getLoopLatch - If there is a single latch block for this loop, return it.
228  /// A latch block is a block that contains a branch back to the header.
229  BlockT *getLoopLatch() const;
230 
231  //===--------------------------------------------------------------------===//
232  // APIs for updating loop information after changing the CFG
233  //
234 
235  /// addBasicBlockToLoop - This method is used by other analyses to update loop
236  /// information. NewBB is set to be a new member of the current loop.
237  /// Because of this, it is added as a member of all parent loops, and is added
238  /// to the specified LoopInfo object as being in the current basic block. It
239  /// is not valid to replace the loop header with this method.
240  ///
241  void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
242 
243  /// replaceChildLoopWith - This is used when splitting loops up. It replaces
244  /// the OldChild entry in our children list with NewChild, and updates the
245  /// parent pointer of OldChild to be null and the NewChild to be this loop.
246  /// This updates the loop depth of the new child.
247  void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild);
248 
249  /// addChildLoop - Add the specified loop to be a child of this loop. This
250  /// updates the loop depth of the new child.
251  ///
252  void addChildLoop(LoopT *NewChild) {
253  assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
254  NewChild->ParentLoop = static_cast<LoopT *>(this);
255  SubLoops.push_back(NewChild);
256  }
257 
258  /// removeChildLoop - This removes the specified child from being a subloop of
259  /// this loop. The loop is not deleted, as it will presumably be inserted
260  /// into another loop.
262  assert(I != SubLoops.end() && "Cannot remove end iterator!");
263  LoopT *Child = *I;
264  assert(Child->ParentLoop == this && "Child is not a child of this loop!");
265  SubLoops.erase(SubLoops.begin()+(I-begin()));
266  Child->ParentLoop = 0;
267  return Child;
268  }
269 
270  /// addBlockEntry - This adds a basic block directly to the basic block list.
271  /// This should only be used by transformations that create new loops. Other
272  /// transformations should use addBasicBlockToLoop.
273  void addBlockEntry(BlockT *BB) {
274  Blocks.push_back(BB);
275  DenseBlockSet.insert(BB);
276  }
277 
278  /// reverseBlocks - interface to reverse Blocks[from, end of loop] in this loop
279  void reverseBlock(unsigned from) {
280  std::reverse(Blocks.begin() + from, Blocks.end());
281  }
282 
283  /// reserveBlocks- interface to do reserve() for Blocks
284  void reserveBlocks(unsigned size) {
285  Blocks.reserve(size);
286  }
287 
288  /// moveToHeader - This method is used to move BB (which must be part of this
289  /// loop) to be the loop header of the loop (the block that dominates all
290  /// others).
291  void moveToHeader(BlockT *BB) {
292  if (Blocks[0] == BB) return;
293  for (unsigned i = 0; ; ++i) {
294  assert(i != Blocks.size() && "Loop does not contain BB!");
295  if (Blocks[i] == BB) {
296  Blocks[i] = Blocks[0];
297  Blocks[0] = BB;
298  return;
299  }
300  }
301  }
302 
303  /// removeBlockFromLoop - This removes the specified basic block from the
304  /// current loop, updating the Blocks as appropriate. This does not update
305  /// the mapping in the LoopInfo class.
306  void removeBlockFromLoop(BlockT *BB) {
307  RemoveFromVector(Blocks, BB);
308  DenseBlockSet.erase(BB);
309  }
310 
311  /// verifyLoop - Verify loop structure
312  void verifyLoop() const;
313 
314  /// verifyLoop - Verify loop structure of this loop and all nested loops.
316 
317  void print(raw_ostream &OS, unsigned Depth = 0) const;
318 
319 protected:
320  friend class LoopInfoBase<BlockT, LoopT>;
321  explicit LoopBase(BlockT *BB) : ParentLoop(0) {
322  Blocks.push_back(BB);
323  DenseBlockSet.insert(BB);
324  }
325 };
326 
327 template<class BlockT, class LoopT>
328 raw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
329  Loop.print(OS);
330  return OS;
331 }
332 
333 // Implementation in LoopInfoImpl.h
334 #ifdef __GNUC__
335 __extension__ extern template class LoopBase<BasicBlock, Loop>;
336 #endif
337 
339 public:
340  Loop() {}
341 
342  /// isLoopInvariant - Return true if the specified value is loop invariant
343  ///
344  bool isLoopInvariant(Value *V) const;
345 
346  /// hasLoopInvariantOperands - Return true if all the operands of the
347  /// specified instruction are loop invariant.
348  bool hasLoopInvariantOperands(Instruction *I) const;
349 
350  /// makeLoopInvariant - If the given value is an instruction inside of the
351  /// loop and it can be hoisted, do so to make it trivially loop-invariant.
352  /// Return true if the value after any hoisting is loop invariant. This
353  /// function can be used as a slightly more aggressive replacement for
354  /// isLoopInvariant.
355  ///
356  /// If InsertPt is specified, it is the point to hoist instructions to.
357  /// If null, the terminator of the loop preheader is used.
358  ///
359  bool makeLoopInvariant(Value *V, bool &Changed,
360  Instruction *InsertPt = 0) const;
361 
362  /// makeLoopInvariant - If the given instruction is inside of the
363  /// loop and it can be hoisted, do so to make it trivially loop-invariant.
364  /// Return true if the instruction after any hoisting is loop invariant. This
365  /// function can be used as a slightly more aggressive replacement for
366  /// isLoopInvariant.
367  ///
368  /// If InsertPt is specified, it is the point to hoist instructions to.
369  /// If null, the terminator of the loop preheader is used.
370  ///
371  bool makeLoopInvariant(Instruction *I, bool &Changed,
372  Instruction *InsertPt = 0) const;
373 
374  /// getCanonicalInductionVariable - Check to see if the loop has a canonical
375  /// induction variable: an integer recurrence that starts at 0 and increments
376  /// by one each time through the loop. If so, return the phi node that
377  /// corresponds to it.
378  ///
379  /// The IndVarSimplify pass transforms loops to have a canonical induction
380  /// variable.
381  ///
382  PHINode *getCanonicalInductionVariable() const;
383 
384  /// isLCSSAForm - Return true if the Loop is in LCSSA form
385  bool isLCSSAForm(DominatorTree &DT) const;
386 
387  /// isLoopSimplifyForm - Return true if the Loop is in the form that
388  /// the LoopSimplify form transforms loops to, which is sometimes called
389  /// normal form.
390  bool isLoopSimplifyForm() const;
391 
392  /// isSafeToClone - Return true if the loop body is safe to clone in practice.
393  bool isSafeToClone() const;
394 
395  /// Returns true if the loop is annotated parallel.
396  ///
397  /// A parallel loop can be assumed to not contain any dependencies between
398  /// iterations by the compiler. That is, any loop-carried dependency checking
399  /// can be skipped completely when parallelizing the loop on the target
400  /// machine. Thus, if the parallel loop information originates from the
401  /// programmer, e.g. via the OpenMP parallel for pragma, it is the
402  /// programmer's responsibility to ensure there are no loop-carried
403  /// dependencies. The final execution order of the instructions across
404  /// iterations is not guaranteed, thus, the end result might or might not
405  /// implement actual concurrent execution of instructions across multiple
406  /// iterations.
407  bool isAnnotatedParallel() const;
408 
409  /// Return the llvm.loop loop id metadata node for this loop if it is present.
410  ///
411  /// If this loop contains the same llvm.loop metadata on each branch to the
412  /// header then the node is returned. If any latch instruction does not
413  /// contain llvm.loop or or if multiple latches contain different nodes then
414  /// 0 is returned.
415  MDNode *getLoopID() const;
416  /// Set the llvm.loop loop id metadata for this loop.
417  ///
418  /// The LoopID metadata node will be added to each terminator instruction in
419  /// the loop that branches to the loop header.
420  ///
421  /// The LoopID metadata node should have one or more operands and the first
422  /// operand should should be the node itself.
423  void setLoopID(MDNode *LoopID) const;
424 
425  /// hasDedicatedExits - Return true if no exit block for the loop
426  /// has a predecessor that is outside the loop.
427  bool hasDedicatedExits() const;
428 
429  /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
430  /// These are the blocks _outside of the current loop_ which are branched to.
431  /// This assumes that loop exits are in canonical form.
432  ///
433  void getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const;
434 
435  /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
436  /// block, return that block. Otherwise return null.
437  BasicBlock *getUniqueExitBlock() const;
438 
439  void dump() const;
440 
441 private:
443  explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
444 };
445 
446 //===----------------------------------------------------------------------===//
447 /// LoopInfo - This class builds and contains all of the top level loop
448 /// structures in the specified function.
449 ///
450 
451 template<class BlockT, class LoopT>
452 class LoopInfoBase {
453  // BBMap - Mapping of basic blocks to the inner most loop they occur in
454  DenseMap<BlockT *, LoopT *> BBMap;
455  std::vector<LoopT *> TopLevelLoops;
457  friend class LoopInfo;
458 
459  void operator=(const LoopInfoBase &) LLVM_DELETED_FUNCTION;
460  LoopInfoBase(const LoopInfo &) LLVM_DELETED_FUNCTION;
461 public:
463  ~LoopInfoBase() { releaseMemory(); }
464 
465  void releaseMemory() {
466  for (typename std::vector<LoopT *>::iterator I =
467  TopLevelLoops.begin(), E = TopLevelLoops.end(); I != E; ++I)
468  delete *I; // Delete all of the loops...
469 
470  BBMap.clear(); // Reset internal state of analysis
471  TopLevelLoops.clear();
472  }
473 
474  /// iterator/begin/end - The interface to the top-level loops in the current
475  /// function.
476  ///
477  typedef typename std::vector<LoopT *>::const_iterator iterator;
478  typedef typename std::vector<LoopT *>::const_reverse_iterator
480  iterator begin() const { return TopLevelLoops.begin(); }
481  iterator end() const { return TopLevelLoops.end(); }
482  reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
483  reverse_iterator rend() const { return TopLevelLoops.rend(); }
484  bool empty() const { return TopLevelLoops.empty(); }
485 
486  /// getLoopFor - Return the inner most loop that BB lives in. If a basic
487  /// block is in no loop (for example the entry node), null is returned.
488  ///
489  LoopT *getLoopFor(const BlockT *BB) const {
490  return BBMap.lookup(const_cast<BlockT*>(BB));
491  }
492 
493  /// operator[] - same as getLoopFor...
494  ///
495  const LoopT *operator[](const BlockT *BB) const {
496  return getLoopFor(BB);
497  }
498 
499  /// getLoopDepth - Return the loop nesting level of the specified block. A
500  /// depth of 0 means the block is not inside any loop.
501  ///
502  unsigned getLoopDepth(const BlockT *BB) const {
503  const LoopT *L = getLoopFor(BB);
504  return L ? L->getLoopDepth() : 0;
505  }
506 
507  // isLoopHeader - True if the block is a loop header node
508  bool isLoopHeader(BlockT *BB) const {
509  const LoopT *L = getLoopFor(BB);
510  return L && L->getHeader() == BB;
511  }
512 
513  /// removeLoop - This removes the specified top-level loop from this loop info
514  /// object. The loop is not deleted, as it will presumably be inserted into
515  /// another loop.
516  LoopT *removeLoop(iterator I) {
517  assert(I != end() && "Cannot remove end iterator!");
518  LoopT *L = *I;
519  assert(L->getParentLoop() == 0 && "Not a top-level loop!");
520  TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
521  return L;
522  }
523 
524  /// changeLoopFor - Change the top-level loop that contains BB to the
525  /// specified loop. This should be used by transformations that restructure
526  /// the loop hierarchy tree.
527  void changeLoopFor(BlockT *BB, LoopT *L) {
528  if (!L) {
529  BBMap.erase(BB);
530  return;
531  }
532  BBMap[BB] = L;
533  }
534 
535  /// changeTopLevelLoop - Replace the specified loop in the top-level loops
536  /// list with the indicated loop.
537  void changeTopLevelLoop(LoopT *OldLoop,
538  LoopT *NewLoop) {
539  typename std::vector<LoopT *>::iterator I =
540  std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
541  assert(I != TopLevelLoops.end() && "Old loop not at top level!");
542  *I = NewLoop;
543  assert(NewLoop->ParentLoop == 0 && OldLoop->ParentLoop == 0 &&
544  "Loops already embedded into a subloop!");
545  }
546 
547  /// addTopLevelLoop - This adds the specified loop to the collection of
548  /// top-level loops.
549  void addTopLevelLoop(LoopT *New) {
550  assert(New->getParentLoop() == 0 && "Loop already in subloop!");
551  TopLevelLoops.push_back(New);
552  }
553 
554  /// removeBlock - This method completely removes BB from all data structures,
555  /// including all of the Loop objects it is nested in and our mapping from
556  /// BasicBlocks to loops.
557  void removeBlock(BlockT *BB) {
558  typename DenseMap<BlockT *, LoopT *>::iterator I = BBMap.find(BB);
559  if (I != BBMap.end()) {
560  for (LoopT *L = I->second; L; L = L->getParentLoop())
561  L->removeBlockFromLoop(BB);
562 
563  BBMap.erase(I);
564  }
565  }
566 
567  // Internals
568 
569  static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
570  const LoopT *ParentLoop) {
571  if (SubLoop == 0) return true;
572  if (SubLoop == ParentLoop) return false;
573  return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
574  }
575 
576  /// Create the loop forest using a stable algorithm.
577  void Analyze(DominatorTreeBase<BlockT> &DomTree);
578 
579  // Debugging
580 
581  void print(raw_ostream &OS) const;
582 };
583 
584 // Implementation in LoopInfoImpl.h
585 #ifdef __GNUC__
586 __extension__ extern template class LoopInfoBase<BasicBlock, Loop>;
587 #endif
588 
589 class LoopInfo : public FunctionPass {
592 
593  void operator=(const LoopInfo &) LLVM_DELETED_FUNCTION;
594  LoopInfo(const LoopInfo &) LLVM_DELETED_FUNCTION;
595 public:
596  static char ID; // Pass identification, replacement for typeid
597 
600  }
601 
603 
604  /// iterator/begin/end - The interface to the top-level loops in the current
605  /// function.
606  ///
609  inline iterator begin() const { return LI.begin(); }
610  inline iterator end() const { return LI.end(); }
611  inline reverse_iterator rbegin() const { return LI.rbegin(); }
612  inline reverse_iterator rend() const { return LI.rend(); }
613  bool empty() const { return LI.empty(); }
614 
615  /// getLoopFor - Return the inner most loop that BB lives in. If a basic
616  /// block is in no loop (for example the entry node), null is returned.
617  ///
618  inline Loop *getLoopFor(const BasicBlock *BB) const {
619  return LI.getLoopFor(BB);
620  }
621 
622  /// operator[] - same as getLoopFor...
623  ///
624  inline const Loop *operator[](const BasicBlock *BB) const {
625  return LI.getLoopFor(BB);
626  }
627 
628  /// getLoopDepth - Return the loop nesting level of the specified block. A
629  /// depth of 0 means the block is not inside any loop.
630  ///
631  inline unsigned getLoopDepth(const BasicBlock *BB) const {
632  return LI.getLoopDepth(BB);
633  }
634 
635  // isLoopHeader - True if the block is a loop header node
636  inline bool isLoopHeader(BasicBlock *BB) const {
637  return LI.isLoopHeader(BB);
638  }
639 
640  /// runOnFunction - Calculate the natural loop information.
641  ///
642  virtual bool runOnFunction(Function &F);
643 
644  virtual void verifyAnalysis() const;
645 
646  virtual void releaseMemory() { LI.releaseMemory(); }
647 
648  virtual void print(raw_ostream &O, const Module* M = 0) const;
649 
650  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
651 
652  /// removeLoop - This removes the specified top-level loop from this loop info
653  /// object. The loop is not deleted, as it will presumably be inserted into
654  /// another loop.
655  inline Loop *removeLoop(iterator I) { return LI.removeLoop(I); }
656 
657  /// changeLoopFor - Change the top-level loop that contains BB to the
658  /// specified loop. This should be used by transformations that restructure
659  /// the loop hierarchy tree.
660  inline void changeLoopFor(BasicBlock *BB, Loop *L) {
661  LI.changeLoopFor(BB, L);
662  }
663 
664  /// changeTopLevelLoop - Replace the specified loop in the top-level loops
665  /// list with the indicated loop.
666  inline void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop) {
667  LI.changeTopLevelLoop(OldLoop, NewLoop);
668  }
669 
670  /// addTopLevelLoop - This adds the specified loop to the collection of
671  /// top-level loops.
672  inline void addTopLevelLoop(Loop *New) {
673  LI.addTopLevelLoop(New);
674  }
675 
676  /// removeBlock - This method completely removes BB from all data structures,
677  /// including all of the Loop objects it is nested in and our mapping from
678  /// BasicBlocks to loops.
680  LI.removeBlock(BB);
681  }
682 
683  /// updateUnloop - Update LoopInfo after removing the last backedge from a
684  /// loop--now the "unloop". This updates the loop forest and parent loops for
685  /// each block so that Unloop is no longer referenced, but the caller must
686  /// actually delete the Unloop object.
687  void updateUnloop(Loop *Unloop);
688 
689  /// replacementPreservesLCSSAForm - Returns true if replacing From with To
690  /// everywhere is guaranteed to preserve LCSSA form.
692  // Preserving LCSSA form is only problematic if the replacing value is an
693  // instruction.
695  if (!I) return true;
696  // If both instructions are defined in the same basic block then replacement
697  // cannot break LCSSA form.
698  if (I->getParent() == From->getParent())
699  return true;
700  // If the instruction is not defined in a loop then it can safely replace
701  // anything.
702  Loop *ToLoop = getLoopFor(I->getParent());
703  if (!ToLoop) return true;
704  // If the replacing instruction is defined in the same loop as the original
705  // instruction, or in a loop that contains it as an inner loop, then using
706  // it as a replacement will not break LCSSA form.
707  return ToLoop->contains(getLoopFor(From->getParent()));
708  }
709 };
710 
711 
712 // Allow clients to walk the list of nested loops...
713 template <> struct GraphTraits<const Loop*> {
714  typedef const Loop NodeType;
716 
717  static NodeType *getEntryNode(const Loop *L) { return L; }
718  static inline ChildIteratorType child_begin(NodeType *N) {
719  return N->begin();
720  }
721  static inline ChildIteratorType child_end(NodeType *N) {
722  return N->end();
723  }
724 };
725 
726 template <> struct GraphTraits<Loop*> {
727  typedef Loop NodeType;
729 
730  static NodeType *getEntryNode(Loop *L) { return L; }
731  static inline ChildIteratorType child_begin(NodeType *N) {
732  return N->begin();
733  }
734  static inline ChildIteratorType child_end(NodeType *N) {
735  return N->end();
736  }
737 };
738 
739 } // End llvm namespace
740 
741 #endif
unsigned getNumBackEdges() const
Definition: LoopInfo.h:164
LoopInfo::iterator ChildIteratorType
Definition: LoopInfo.h:715
void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop)
Definition: LoopInfo.h:666
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:181
bool empty() const
Definition: LoopInfo.h:484
unsigned getLoopDepth(const BlockT *BB) const
Definition: LoopInfo.h:502
static PassRegistry * getPassRegistry()
virtual void releaseMemory()
Definition: LoopInfo.h:646
static bool isLoopInvariant(Value *V, const Loop *L, const DominatorTree *DT)
void removeBlock(BlockT *BB)
Definition: LoopInfo.h:557
LoopT * removeLoop(iterator I)
Definition: LoopInfo.h:516
iterator begin() const
Definition: LoopInfo.h:480
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
LoopInfoBase< BasicBlock, Loop >::iterator iterator
Definition: LoopInfo.h:607
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:266
const LoopT * operator[](const BlockT *BB) const
Definition: LoopInfo.h:495
bool insert(PtrType Ptr)
Definition: SmallPtrSet.h:253
std::vector< LoopT * >::const_reverse_iterator reverse_iterator
Definition: LoopInfo.h:479
static bool isNotAlreadyContainedIn(const LoopT *SubLoop, const LoopT *ParentLoop)
Definition: LoopInfo.h:569
bool isLoopExiting(const BlockT *BB) const
Definition: LoopInfo.h:151
void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild)
Definition: LoopInfoImpl.h:209
const_iterator begin(StringRef path)
Get begin iterator over path.
Definition: Path.cpp:173
void changeLoopFor(BlockT *BB, LoopT *L)
Definition: LoopInfo.h:527
LoopT * getParentLoop() const
Definition: LoopInfo.h:96
MDNode - a tuple of other values.
Definition: Metadata.h:69
F(f)
LoopT * getLoopFor(const BlockT *BB) const
Definition: LoopInfo.h:489
BlockT * getExitBlock() const
Definition: LoopInfoImpl.h:76
const std::vector< BlockT * > & getBlocks() const
Definition: LoopInfo.h:138
void initializeLoopInfoPass(PassRegistry &)
reverse_iterator rbegin() const
Definition: LoopInfo.h:611
BlockT * getHeader() const
Definition: LoopInfo.h:95
LoopInfoBase< BlockT, LoopT > * LI
Definition: LoopInfoImpl.h:411
LoopT * removeChildLoop(iterator I)
Definition: LoopInfo.h:261
std::vector< LoopT * >::const_iterator iterator
Definition: LoopInfo.h:477
BlockT * getLoopLatch() const
Definition: LoopInfoImpl.h:154
void getExitEdges(SmallVectorImpl< Edge > &ExitEdges) const
getExitEdges - Return all pairs of (inside_block,outside_block).
Definition: LoopInfoImpl.h:87
void print(raw_ostream &OS, unsigned Depth=0) const
Definition: LoopInfoImpl.h:318
Hexagon Hardware Loops
static ChildIteratorType child_begin(NodeType *N)
Definition: LoopInfo.h:731
void getExitingBlocks(SmallVectorImpl< BlockT * > &ExitingBlocks) const
Definition: LoopInfoImpl.h:33
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
void getExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
Definition: LoopInfoImpl.h:62
reverse_iterator rbegin() const
Definition: LoopInfo.h:132
bool count(PtrType Ptr) const
count - Return true if the specified pointer is in the set.
Definition: SmallPtrSet.h:264
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
Definition: LoopInfoImpl.h:185
bool contains(const InstT *Inst) const
Definition: LoopInfo.h:119
iterator begin() const
Definition: LoopInfo.h:609
void addChildLoop(LoopT *NewChild)
Definition: LoopInfo.h:252
iterator end() const
Definition: LoopInfo.h:481
Loop * getLoopFor(const BasicBlock *BB) const
Definition: LoopInfo.h:618
Loop * removeLoop(iterator I)
Definition: LoopInfo.h:655
LoopBase(BlockT *BB)
Definition: LoopInfo.h:321
void verifyLoop() const
verifyLoop - Verify loop structure
Definition: LoopInfoImpl.h:222
std::vector< LoopT * >::const_reverse_iterator reverse_iterator
Definition: LoopInfo.h:129
iterator begin() const
Definition: LoopInfo.h:130
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
BlockT * getLoopPreheader() const
Definition: LoopInfoImpl.h:106
void setParentLoop(LoopT *L)
setParentLoop is a raw interface for bypassing addChildLoop.
Definition: LoopInfo.h:99
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
void releaseMemory()
Definition: LoopInfo.h:465
void RemoveFromVector(std::vector< T * > &V, T *N)
Definition: LoopInfo.h:44
#define H(x, y, z)
Definition: MD5.cpp:53
Instr is a loop (backwards branch).
Definition: GCMetadata.h:51
iterator end() const
Definition: LoopInfo.h:131
static ChildIteratorType child_end(NodeType *N)
Definition: LoopInfo.h:734
bool contains(const LoopT *L) const
Definition: LoopInfo.h:104
BlockT * getExitingBlock() const
Definition: LoopInfoImpl.h:49
void addBlockEntry(BlockT *BB)
Definition: LoopInfo.h:273
iterator end() const
Definition: LoopInfo.h:610
bool isLoopHeader(BasicBlock *BB) const
Definition: LoopInfo.h:636
bool isLoopHeader(BlockT *BB) const
Definition: LoopInfo.h:508
reverse_iterator rend() const
Definition: LoopInfo.h:483
void removeBlockFromLoop(BlockT *BB)
Definition: LoopInfo.h:306
LoopInfoBase< BasicBlock, Loop >::reverse_iterator reverse_iterator
Definition: LoopInfo.h:608
std::pair< const BlockT *, const BlockT * > Edge
Edge type.
Definition: LoopInfo.h:206
bool erase(PtrType Ptr)
Definition: SmallPtrSet.h:259
reverse_iterator rend() const
Definition: LoopInfo.h:133
bool contains(const BlockT *BB) const
Definition: LoopInfo.h:112
static NodeType * getEntryNode(const Loop *L)
Definition: LoopInfo.h:717
void addTopLevelLoop(LoopT *New)
Definition: LoopInfo.h:549
static ChildIteratorType child_begin(NodeType *N)
Definition: LoopInfo.h:718
#define LLVM_DELETED_FUNCTION
Definition: Compiler.h:137
void reserveBlocks(unsigned size)
reserveBlocks- interface to do reserve() for Blocks
Definition: LoopInfo.h:284
unsigned getLoopDepth(const BasicBlock *BB) const
Definition: LoopInfo.h:631
LoopInfo::iterator ChildIteratorType
Definition: LoopInfo.h:728
std::vector< BlockT * >::const_iterator block_iterator
Definition: LoopInfo.h:139
void verifyLoopNest(DenseSet< const LoopT * > *Loops) const
verifyLoop - Verify loop structure of this loop and all nested loops.
Definition: LoopInfoImpl.h:307
block_iterator block_end() const
Definition: LoopInfo.h:141
static NodeType * getEntryNode(Loop *L)
Definition: LoopInfo.h:730
unsigned getNumBlocks() const
getNumBlocks - Get the number of blocks in this loop in constant time.
Definition: LoopInfo.h:144
void moveToHeader(BlockT *BB)
Definition: LoopInfo.h:291
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
BlockT * getLoopPredecessor() const
Definition: LoopInfoImpl.h:128
void addTopLevelLoop(Loop *New)
Definition: LoopInfo.h:672
friend class LoopInfo
Definition: LoopInfo.h:457
void removeBlock(BasicBlock *BB)
Definition: LoopInfo.h:679
std::vector< LoopT * > & getSubLoopsVector()
Definition: LoopInfo.h:126
bool empty() const
Definition: LoopInfo.h:613
void changeLoopFor(BasicBlock *BB, Loop *L)
Definition: LoopInfo.h:660
static ChildIteratorType child_end(NodeType *N)
Definition: LoopInfo.h:721
LLVM Value Representation.
Definition: Value.h:66
LoopBase()
Loop ctor - This creates an empty loop.
Definition: LoopInfo.h:79
void changeTopLevelLoop(LoopT *OldLoop, LoopT *NewLoop)
Definition: LoopInfo.h:537
bool empty() const
Definition: LoopInfo.h:134
const std::vector< LoopT * > & getSubLoops() const
Definition: LoopInfo.h:125
block_iterator block_begin() const
Definition: LoopInfo.h:140
bool replacementPreservesLCSSAForm(Instruction *From, Value *To)
Definition: LoopInfo.h:691
std::vector< LoopT * >::const_iterator iterator
Definition: LoopInfo.h:127
reverse_iterator rbegin() const
Definition: LoopInfo.h:482
unsigned getLoopDepth() const
Definition: LoopInfo.h:88
void reverseBlock(unsigned from)
reverseBlocks - interface to reverse Blocks[from, end of loop] in this loop
Definition: LoopInfo.h:279
const Loop * operator[](const BasicBlock *BB) const
Definition: LoopInfo.h:624
LoopInfoBase< BasicBlock, Loop > & getBase()
Definition: LoopInfo.h:602
reverse_iterator rend() const
Definition: LoopInfo.h:612
const BasicBlock * getParent() const
Definition: Instruction.h:52