Remove #includes from the commonly used LoopInfo.h.
[oota-llvm.git] / include / llvm / Analysis / LoopInfo.h
1 //===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the LoopInfo class that is used to identify natural loops
11 // and determine the loop depth of various nodes of the CFG.  A natural loop
12 // has exactly one entry-point, which is called the header. Note that natural
13 // loops may actually be several loops that share the same header node.
14 //
15 // This analysis calculates the nesting structure of loops in a function.  For
16 // each natural loop identified, this analysis identifies natural loops
17 // contained entirely within the loop and the basic blocks the make up the loop.
18 //
19 // It can calculate on the fly various bits of information, for example:
20 //
21 //  * whether there is a preheader for the loop
22 //  * the number of back edges to the header
23 //  * whether or not a particular block branches out of the loop
24 //  * the successor blocks of the loop
25 //  * the loop depth
26 //  * etc...
27 //
28 //===----------------------------------------------------------------------===//
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/DepthFirstIterator.h"
36 #include "llvm/ADT/GraphTraits.h"
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/Analysis/Dominators.h"
39 #include "llvm/Pass.h"
40 #include "llvm/Support/CFG.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include <algorithm>
43
44 namespace llvm {
45
46 template<typename T>
47 inline void RemoveFromVector(std::vector<T*> &V, T *N) {
48   typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N);
49   assert(I != V.end() && "N is not in this list!");
50   V.erase(I);
51 }
52
53 class DominatorTree;
54 class LoopInfo;
55 class Loop;
56 class PHINode;
57 template<class N, class M> class LoopInfoBase;
58 template<class N, class M> class LoopBase;
59
60 //===----------------------------------------------------------------------===//
61 /// LoopBase class - Instances of this class are used to represent loops that
62 /// are detected in the flow graph
63 ///
64 template<class BlockT, class LoopT>
65 class LoopBase {
66   LoopT *ParentLoop;
67   // SubLoops - Loops contained entirely within this one.
68   std::vector<LoopT *> SubLoops;
69
70   // Blocks - The list of blocks in this loop.  First entry is the header node.
71   std::vector<BlockT*> Blocks;
72
73   LoopBase(const LoopBase<BlockT, LoopT> &) LLVM_DELETED_FUNCTION;
74   const LoopBase<BlockT, LoopT>&
75     operator=(const LoopBase<BlockT, LoopT> &) LLVM_DELETED_FUNCTION;
76 public:
77   /// Loop ctor - This creates an empty loop.
78   LoopBase() : ParentLoop(0) {}
79   ~LoopBase() {
80     for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
81       delete SubLoops[i];
82   }
83
84   /// getLoopDepth - Return the nesting level of this loop.  An outer-most
85   /// loop has depth 1, for consistency with loop depth values used for basic
86   /// blocks, where depth 0 is used for blocks not inside any loops.
87   unsigned getLoopDepth() const {
88     unsigned D = 1;
89     for (const LoopT *CurLoop = ParentLoop; CurLoop;
90          CurLoop = CurLoop->ParentLoop)
91       ++D;
92     return D;
93   }
94   BlockT *getHeader() const { return Blocks.front(); }
95   LoopT *getParentLoop() const { return ParentLoop; }
96
97   /// setParentLoop is a raw interface for bypassing addChildLoop.
98   void setParentLoop(LoopT *L) { ParentLoop = L; }
99
100   /// contains - Return true if the specified loop is contained within in
101   /// this loop.
102   ///
103   bool contains(const LoopT *L) const {
104     if (L == this) return true;
105     if (L == 0)    return false;
106     return contains(L->getParentLoop());
107   }
108
109   /// contains - Return true if the specified basic block is in this loop.
110   ///
111   bool contains(const BlockT *BB) const {
112     return std::find(block_begin(), block_end(), BB) != block_end();
113   }
114
115   /// contains - Return true if the specified instruction is in this loop.
116   ///
117   template<class InstT>
118   bool contains(const InstT *Inst) const {
119     return contains(Inst->getParent());
120   }
121
122   /// iterator/begin/end - Return the loops contained entirely within this loop.
123   ///
124   const std::vector<LoopT *> &getSubLoops() const { return SubLoops; }
125   std::vector<LoopT *> &getSubLoopsVector() { return SubLoops; }
126   typedef typename std::vector<LoopT *>::const_iterator iterator;
127   typedef typename std::vector<LoopT *>::const_reverse_iterator
128     reverse_iterator;
129   iterator begin() const { return SubLoops.begin(); }
130   iterator end() const { return SubLoops.end(); }
131   reverse_iterator rbegin() const { return SubLoops.rbegin(); }
132   reverse_iterator rend() const { return SubLoops.rend(); }
133   bool empty() const { return SubLoops.empty(); }
134
135   /// getBlocks - Get a list of the basic blocks which make up this loop.
136   ///
137   const std::vector<BlockT*> &getBlocks() const { return Blocks; }
138   std::vector<BlockT*> &getBlocksVector() { 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<BlockT*> BlockTraits;
153     for (typename BlockTraits::ChildIteratorType SI =
154          BlockTraits::child_begin(const_cast<BlockT*>(BB)),
155          SE = BlockTraits::child_end(const_cast<BlockT*>(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(const_cast<BlockT*>(H)),
171          E = InvBlockTraits::child_end(const_cast<BlockT*>(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.
261   LoopT *removeChildLoop(iterator I) {
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   }
276
277   /// moveToHeader - This method is used to move BB (which must be part of this
278   /// loop) to be the loop header of the loop (the block that dominates all
279   /// others).
280   void moveToHeader(BlockT *BB) {
281     if (Blocks[0] == BB) return;
282     for (unsigned i = 0; ; ++i) {
283       assert(i != Blocks.size() && "Loop does not contain BB!");
284       if (Blocks[i] == BB) {
285         Blocks[i] = Blocks[0];
286         Blocks[0] = BB;
287         return;
288       }
289     }
290   }
291
292   /// removeBlockFromLoop - This removes the specified basic block from the
293   /// current loop, updating the Blocks as appropriate.  This does not update
294   /// the mapping in the LoopInfo class.
295   void removeBlockFromLoop(BlockT *BB) {
296     RemoveFromVector(Blocks, BB);
297   }
298
299   /// verifyLoop - Verify loop structure
300   void verifyLoop() const;
301
302   /// verifyLoop - Verify loop structure of this loop and all nested loops.
303   void verifyLoopNest(DenseSet<const LoopT*> *Loops) const;
304
305   void print(raw_ostream &OS, unsigned Depth = 0) const;
306
307 protected:
308   friend class LoopInfoBase<BlockT, LoopT>;
309   explicit LoopBase(BlockT *BB) : ParentLoop(0) {
310     Blocks.push_back(BB);
311   }
312 };
313
314 template<class BlockT, class LoopT>
315 raw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
316   Loop.print(OS);
317   return OS;
318 }
319
320 // Implementation in LoopInfoImpl.h
321 #ifdef __GNUC__
322 __extension__ extern template class LoopBase<BasicBlock, Loop>;
323 #endif
324
325 class Loop : public LoopBase<BasicBlock, Loop> {
326 public:
327   Loop() {}
328
329   /// isLoopInvariant - Return true if the specified value is loop invariant
330   ///
331   bool isLoopInvariant(Value *V) const;
332
333   /// hasLoopInvariantOperands - Return true if all the operands of the
334   /// specified instruction are loop invariant.
335   bool hasLoopInvariantOperands(Instruction *I) const;
336
337   /// makeLoopInvariant - If the given value is an instruction inside of the
338   /// loop and it can be hoisted, do so to make it trivially loop-invariant.
339   /// Return true if the value after any hoisting is loop invariant. This
340   /// function can be used as a slightly more aggressive replacement for
341   /// isLoopInvariant.
342   ///
343   /// If InsertPt is specified, it is the point to hoist instructions to.
344   /// If null, the terminator of the loop preheader is used.
345   ///
346   bool makeLoopInvariant(Value *V, bool &Changed,
347                          Instruction *InsertPt = 0) const;
348
349   /// makeLoopInvariant - If the given instruction is inside of the
350   /// loop and it can be hoisted, do so to make it trivially loop-invariant.
351   /// Return true if the instruction after any hoisting is loop invariant. This
352   /// function can be used as a slightly more aggressive replacement for
353   /// isLoopInvariant.
354   ///
355   /// If InsertPt is specified, it is the point to hoist instructions to.
356   /// If null, the terminator of the loop preheader is used.
357   ///
358   bool makeLoopInvariant(Instruction *I, bool &Changed,
359                          Instruction *InsertPt = 0) const;
360
361   /// getCanonicalInductionVariable - Check to see if the loop has a canonical
362   /// induction variable: an integer recurrence that starts at 0 and increments
363   /// by one each time through the loop.  If so, return the phi node that
364   /// corresponds to it.
365   ///
366   /// The IndVarSimplify pass transforms loops to have a canonical induction
367   /// variable.
368   ///
369   PHINode *getCanonicalInductionVariable() const;
370
371   /// isLCSSAForm - Return true if the Loop is in LCSSA form
372   bool isLCSSAForm(DominatorTree &DT) const;
373
374   /// isLoopSimplifyForm - Return true if the Loop is in the form that
375   /// the LoopSimplify form transforms loops to, which is sometimes called
376   /// normal form.
377   bool isLoopSimplifyForm() const;
378
379   /// isSafeToClone - Return true if the loop body is safe to clone in practice.
380   bool isSafeToClone() const;
381
382   /// hasDedicatedExits - Return true if no exit block for the loop
383   /// has a predecessor that is outside the loop.
384   bool hasDedicatedExits() const;
385
386   /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
387   /// These are the blocks _outside of the current loop_ which are branched to.
388   /// This assumes that loop exits are in canonical form.
389   ///
390   void getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const;
391
392   /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
393   /// block, return that block. Otherwise return null.
394   BasicBlock *getUniqueExitBlock() const;
395
396   void dump() const;
397
398 private:
399   friend class LoopInfoBase<BasicBlock, Loop>;
400   explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
401 };
402
403 //===----------------------------------------------------------------------===//
404 /// LoopInfo - This class builds and contains all of the top level loop
405 /// structures in the specified function.
406 ///
407
408 template<class BlockT, class LoopT>
409 class LoopInfoBase {
410   // BBMap - Mapping of basic blocks to the inner most loop they occur in
411   DenseMap<BlockT *, LoopT *> BBMap;
412   std::vector<LoopT *> TopLevelLoops;
413   friend class LoopBase<BlockT, LoopT>;
414   friend class LoopInfo;
415
416   void operator=(const LoopInfoBase &) LLVM_DELETED_FUNCTION;
417   LoopInfoBase(const LoopInfo &) LLVM_DELETED_FUNCTION;
418 public:
419   LoopInfoBase() { }
420   ~LoopInfoBase() { releaseMemory(); }
421
422   void releaseMemory() {
423     for (typename std::vector<LoopT *>::iterator I =
424          TopLevelLoops.begin(), E = TopLevelLoops.end(); I != E; ++I)
425       delete *I;   // Delete all of the loops...
426
427     BBMap.clear();                           // Reset internal state of analysis
428     TopLevelLoops.clear();
429   }
430
431   /// iterator/begin/end - The interface to the top-level loops in the current
432   /// function.
433   ///
434   typedef typename std::vector<LoopT *>::const_iterator iterator;
435   typedef typename std::vector<LoopT *>::const_reverse_iterator
436     reverse_iterator;
437   iterator begin() const { return TopLevelLoops.begin(); }
438   iterator end() const { return TopLevelLoops.end(); }
439   reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
440   reverse_iterator rend() const { return TopLevelLoops.rend(); }
441   bool empty() const { return TopLevelLoops.empty(); }
442
443   /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
444   /// block is in no loop (for example the entry node), null is returned.
445   ///
446   LoopT *getLoopFor(const BlockT *BB) const {
447     return BBMap.lookup(const_cast<BlockT*>(BB));
448   }
449
450   /// operator[] - same as getLoopFor...
451   ///
452   const LoopT *operator[](const BlockT *BB) const {
453     return getLoopFor(BB);
454   }
455
456   /// getLoopDepth - Return the loop nesting level of the specified block.  A
457   /// depth of 0 means the block is not inside any loop.
458   ///
459   unsigned getLoopDepth(const BlockT *BB) const {
460     const LoopT *L = getLoopFor(BB);
461     return L ? L->getLoopDepth() : 0;
462   }
463
464   // isLoopHeader - True if the block is a loop header node
465   bool isLoopHeader(BlockT *BB) const {
466     const LoopT *L = getLoopFor(BB);
467     return L && L->getHeader() == BB;
468   }
469
470   /// removeLoop - This removes the specified top-level loop from this loop info
471   /// object.  The loop is not deleted, as it will presumably be inserted into
472   /// another loop.
473   LoopT *removeLoop(iterator I) {
474     assert(I != end() && "Cannot remove end iterator!");
475     LoopT *L = *I;
476     assert(L->getParentLoop() == 0 && "Not a top-level loop!");
477     TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
478     return L;
479   }
480
481   /// changeLoopFor - Change the top-level loop that contains BB to the
482   /// specified loop.  This should be used by transformations that restructure
483   /// the loop hierarchy tree.
484   void changeLoopFor(BlockT *BB, LoopT *L) {
485     if (!L) {
486       BBMap.erase(BB);
487       return;
488     }
489     BBMap[BB] = L;
490   }
491
492   /// changeTopLevelLoop - Replace the specified loop in the top-level loops
493   /// list with the indicated loop.
494   void changeTopLevelLoop(LoopT *OldLoop,
495                           LoopT *NewLoop) {
496     typename std::vector<LoopT *>::iterator I =
497                  std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
498     assert(I != TopLevelLoops.end() && "Old loop not at top level!");
499     *I = NewLoop;
500     assert(NewLoop->ParentLoop == 0 && OldLoop->ParentLoop == 0 &&
501            "Loops already embedded into a subloop!");
502   }
503
504   /// addTopLevelLoop - This adds the specified loop to the collection of
505   /// top-level loops.
506   void addTopLevelLoop(LoopT *New) {
507     assert(New->getParentLoop() == 0 && "Loop already in subloop!");
508     TopLevelLoops.push_back(New);
509   }
510
511   /// removeBlock - This method completely removes BB from all data structures,
512   /// including all of the Loop objects it is nested in and our mapping from
513   /// BasicBlocks to loops.
514   void removeBlock(BlockT *BB) {
515     typename DenseMap<BlockT *, LoopT *>::iterator I = BBMap.find(BB);
516     if (I != BBMap.end()) {
517       for (LoopT *L = I->second; L; L = L->getParentLoop())
518         L->removeBlockFromLoop(BB);
519
520       BBMap.erase(I);
521     }
522   }
523
524   // Internals
525
526   static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
527                                       const LoopT *ParentLoop) {
528     if (SubLoop == 0) return true;
529     if (SubLoop == ParentLoop) return false;
530     return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
531   }
532
533   /// Create the loop forest using a stable algorithm.
534   void Analyze(DominatorTreeBase<BlockT> &DomTree);
535
536   // Debugging
537
538   void print(raw_ostream &OS) const;
539 };
540
541 // Implementation in LoopInfoImpl.h
542 #ifdef __GNUC__
543 __extension__ extern template class LoopInfoBase<BasicBlock, Loop>;
544 #endif
545
546 class LoopInfo : public FunctionPass {
547   LoopInfoBase<BasicBlock, Loop> LI;
548   friend class LoopBase<BasicBlock, Loop>;
549
550   void operator=(const LoopInfo &) LLVM_DELETED_FUNCTION;
551   LoopInfo(const LoopInfo &) LLVM_DELETED_FUNCTION;
552 public:
553   static char ID; // Pass identification, replacement for typeid
554
555   LoopInfo() : FunctionPass(ID) {
556     initializeLoopInfoPass(*PassRegistry::getPassRegistry());
557   }
558
559   LoopInfoBase<BasicBlock, Loop>& getBase() { return LI; }
560
561   /// iterator/begin/end - The interface to the top-level loops in the current
562   /// function.
563   ///
564   typedef LoopInfoBase<BasicBlock, Loop>::iterator iterator;
565   typedef LoopInfoBase<BasicBlock, Loop>::reverse_iterator reverse_iterator;
566   inline iterator begin() const { return LI.begin(); }
567   inline iterator end() const { return LI.end(); }
568   inline reverse_iterator rbegin() const { return LI.rbegin(); }
569   inline reverse_iterator rend() const { return LI.rend(); }
570   bool empty() const { return LI.empty(); }
571
572   /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
573   /// block is in no loop (for example the entry node), null is returned.
574   ///
575   inline Loop *getLoopFor(const BasicBlock *BB) const {
576     return LI.getLoopFor(BB);
577   }
578
579   /// operator[] - same as getLoopFor...
580   ///
581   inline const Loop *operator[](const BasicBlock *BB) const {
582     return LI.getLoopFor(BB);
583   }
584
585   /// getLoopDepth - Return the loop nesting level of the specified block.  A
586   /// depth of 0 means the block is not inside any loop.
587   ///
588   inline unsigned getLoopDepth(const BasicBlock *BB) const {
589     return LI.getLoopDepth(BB);
590   }
591
592   // isLoopHeader - True if the block is a loop header node
593   inline bool isLoopHeader(BasicBlock *BB) const {
594     return LI.isLoopHeader(BB);
595   }
596
597   /// runOnFunction - Calculate the natural loop information.
598   ///
599   virtual bool runOnFunction(Function &F);
600
601   virtual void verifyAnalysis() const;
602
603   virtual void releaseMemory() { LI.releaseMemory(); }
604
605   virtual void print(raw_ostream &O, const Module* M = 0) const;
606
607   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
608
609   /// removeLoop - This removes the specified top-level loop from this loop info
610   /// object.  The loop is not deleted, as it will presumably be inserted into
611   /// another loop.
612   inline Loop *removeLoop(iterator I) { return LI.removeLoop(I); }
613
614   /// changeLoopFor - Change the top-level loop that contains BB to the
615   /// specified loop.  This should be used by transformations that restructure
616   /// the loop hierarchy tree.
617   inline void changeLoopFor(BasicBlock *BB, Loop *L) {
618     LI.changeLoopFor(BB, L);
619   }
620
621   /// changeTopLevelLoop - Replace the specified loop in the top-level loops
622   /// list with the indicated loop.
623   inline void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop) {
624     LI.changeTopLevelLoop(OldLoop, NewLoop);
625   }
626
627   /// addTopLevelLoop - This adds the specified loop to the collection of
628   /// top-level loops.
629   inline void addTopLevelLoop(Loop *New) {
630     LI.addTopLevelLoop(New);
631   }
632
633   /// removeBlock - This method completely removes BB from all data structures,
634   /// including all of the Loop objects it is nested in and our mapping from
635   /// BasicBlocks to loops.
636   void removeBlock(BasicBlock *BB) {
637     LI.removeBlock(BB);
638   }
639
640   /// updateUnloop - Update LoopInfo after removing the last backedge from a
641   /// loop--now the "unloop". This updates the loop forest and parent loops for
642   /// each block so that Unloop is no longer referenced, but the caller must
643   /// actually delete the Unloop object.
644   void updateUnloop(Loop *Unloop);
645
646   /// replacementPreservesLCSSAForm - Returns true if replacing From with To
647   /// everywhere is guaranteed to preserve LCSSA form.
648   bool replacementPreservesLCSSAForm(Instruction *From, Value *To) {
649     // Preserving LCSSA form is only problematic if the replacing value is an
650     // instruction.
651     Instruction *I = dyn_cast<Instruction>(To);
652     if (!I) return true;
653     // If both instructions are defined in the same basic block then replacement
654     // cannot break LCSSA form.
655     if (I->getParent() == From->getParent())
656       return true;
657     // If the instruction is not defined in a loop then it can safely replace
658     // anything.
659     Loop *ToLoop = getLoopFor(I->getParent());
660     if (!ToLoop) return true;
661     // If the replacing instruction is defined in the same loop as the original
662     // instruction, or in a loop that contains it as an inner loop, then using
663     // it as a replacement will not break LCSSA form.
664     return ToLoop->contains(getLoopFor(From->getParent()));
665   }
666 };
667
668
669 // Allow clients to walk the list of nested loops...
670 template <> struct GraphTraits<const Loop*> {
671   typedef const Loop NodeType;
672   typedef LoopInfo::iterator ChildIteratorType;
673
674   static NodeType *getEntryNode(const Loop *L) { return L; }
675   static inline ChildIteratorType child_begin(NodeType *N) {
676     return N->begin();
677   }
678   static inline ChildIteratorType child_end(NodeType *N) {
679     return N->end();
680   }
681 };
682
683 template <> struct GraphTraits<Loop*> {
684   typedef Loop NodeType;
685   typedef LoopInfo::iterator ChildIteratorType;
686
687   static NodeType *getEntryNode(Loop *L) { return L; }
688   static inline ChildIteratorType child_begin(NodeType *N) {
689     return N->begin();
690   }
691   static inline ChildIteratorType child_end(NodeType *N) {
692     return N->end();
693   }
694 };
695
696 } // End llvm namespace
697
698 #endif