Fix 12513: Loop unrolling breaks with indirect branches.
[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_LOOP_INFO_H
31 #define LLVM_ANALYSIS_LOOP_INFO_H
32
33 #include "llvm/Pass.h"
34 #include "llvm/ADT/DenseMap.h"
35 #include "llvm/ADT/DenseSet.h"
36 #include "llvm/ADT/DepthFirstIterator.h"
37 #include "llvm/ADT/GraphTraits.h"
38 #include "llvm/ADT/SmallVector.h"
39 #include "llvm/ADT/STLExtras.h"
40 #include "llvm/Analysis/Dominators.h"
41 #include "llvm/Support/CFG.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include <algorithm>
44 #include <map>
45
46 namespace llvm {
47
48 template<typename T>
49 static void RemoveFromVector(std::vector<T*> &V, T *N) {
50   typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N);
51   assert(I != V.end() && "N is not in this list!");
52   V.erase(I);
53 }
54
55 class DominatorTree;
56 class LoopInfo;
57 class Loop;
58 class PHINode;
59 template<class N, class M> class LoopInfoBase;
60 template<class N, class M> class LoopBase;
61
62 //===----------------------------------------------------------------------===//
63 /// LoopBase class - Instances of this class are used to represent loops that
64 /// are detected in the flow graph
65 ///
66 template<class BlockT, class LoopT>
67 class LoopBase {
68   LoopT *ParentLoop;
69   // SubLoops - Loops contained entirely within this one.
70   std::vector<LoopT *> SubLoops;
71
72   // Blocks - The list of blocks in this loop.  First entry is the header node.
73   std::vector<BlockT*> Blocks;
74
75   // DO NOT IMPLEMENT
76   LoopBase(const LoopBase<BlockT, LoopT> &);
77   // DO NOT IMPLEMENT
78   const LoopBase<BlockT, LoopT>&operator=(const LoopBase<BlockT, LoopT> &);
79 public:
80   /// Loop ctor - This creates an empty loop.
81   LoopBase() : ParentLoop(0) {}
82   ~LoopBase() {
83     for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
84       delete SubLoops[i];
85   }
86
87   /// getLoopDepth - Return the nesting level of this loop.  An outer-most
88   /// loop has depth 1, for consistency with loop depth values used for basic
89   /// blocks, where depth 0 is used for blocks not inside any loops.
90   unsigned getLoopDepth() const {
91     unsigned D = 1;
92     for (const LoopT *CurLoop = ParentLoop; CurLoop;
93          CurLoop = CurLoop->ParentLoop)
94       ++D;
95     return D;
96   }
97   BlockT *getHeader() const { return Blocks.front(); }
98   LoopT *getParentLoop() const { return ParentLoop; }
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   typedef typename std::vector<LoopT *>::const_iterator iterator;
126   iterator begin() const { return SubLoops.begin(); }
127   iterator end() const { return SubLoops.end(); }
128   bool empty() const { return SubLoops.empty(); }
129
130   /// getBlocks - Get a list of the basic blocks which make up this loop.
131   ///
132   const std::vector<BlockT*> &getBlocks() const { return Blocks; }
133   typedef typename std::vector<BlockT*>::const_iterator block_iterator;
134   block_iterator block_begin() const { return Blocks.begin(); }
135   block_iterator block_end() const { return Blocks.end(); }
136
137   /// getNumBlocks - Get the number of blocks in this loop in constant time.
138   unsigned getNumBlocks() const {
139     return Blocks.size();
140   }
141
142   /// isLoopExiting - True if terminator in the block can branch to another
143   /// block that is outside of the current loop.
144   ///
145   bool isLoopExiting(const BlockT *BB) const {
146     typedef GraphTraits<BlockT*> BlockTraits;
147     for (typename BlockTraits::ChildIteratorType SI =
148          BlockTraits::child_begin(const_cast<BlockT*>(BB)),
149          SE = BlockTraits::child_end(const_cast<BlockT*>(BB)); SI != SE; ++SI) {
150       if (!contains(*SI))
151         return true;
152     }
153     return false;
154   }
155
156   /// getNumBackEdges - Calculate the number of back edges to the loop header
157   ///
158   unsigned getNumBackEdges() const {
159     unsigned NumBackEdges = 0;
160     BlockT *H = getHeader();
161
162     typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
163     for (typename InvBlockTraits::ChildIteratorType I =
164          InvBlockTraits::child_begin(const_cast<BlockT*>(H)),
165          E = InvBlockTraits::child_end(const_cast<BlockT*>(H)); I != E; ++I)
166       if (contains(*I))
167         ++NumBackEdges;
168
169     return NumBackEdges;
170   }
171
172   //===--------------------------------------------------------------------===//
173   // APIs for simple analysis of the loop.
174   //
175   // Note that all of these methods can fail on general loops (ie, there may not
176   // be a preheader, etc).  For best success, the loop simplification and
177   // induction variable canonicalization pass should be used to normalize loops
178   // for easy analysis.  These methods assume canonical loops.
179
180   /// getExitingBlocks - Return all blocks inside the loop that have successors
181   /// outside of the loop.  These are the blocks _inside of the current loop_
182   /// which branch out.  The returned list is always unique.
183   ///
184   void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const {
185     // Sort the blocks vector so that we can use binary search to do quick
186     // lookups.
187     SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
188     std::sort(LoopBBs.begin(), LoopBBs.end());
189
190     typedef GraphTraits<BlockT*> BlockTraits;
191     for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
192       for (typename BlockTraits::ChildIteratorType I =
193           BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
194           I != E; ++I)
195         if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) {
196           // Not in current loop? It must be an exit block.
197           ExitingBlocks.push_back(*BI);
198           break;
199         }
200   }
201
202   /// getExitingBlock - If getExitingBlocks would return exactly one block,
203   /// return that block. Otherwise return null.
204   BlockT *getExitingBlock() const {
205     SmallVector<BlockT*, 8> ExitingBlocks;
206     getExitingBlocks(ExitingBlocks);
207     if (ExitingBlocks.size() == 1)
208       return ExitingBlocks[0];
209     return 0;
210   }
211
212   /// getExitBlocks - Return all of the successor blocks of this loop.  These
213   /// are the blocks _outside of the current loop_ which are branched to.
214   ///
215   void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
216     // Sort the blocks vector so that we can use binary search to do quick
217     // lookups.
218     SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
219     std::sort(LoopBBs.begin(), LoopBBs.end());
220
221     typedef GraphTraits<BlockT*> BlockTraits;
222     for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
223       for (typename BlockTraits::ChildIteratorType I =
224            BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
225            I != E; ++I)
226         if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
227           // Not in current loop? It must be an exit block.
228           ExitBlocks.push_back(*I);
229   }
230
231   /// getExitBlock - If getExitBlocks would return exactly one block,
232   /// return that block. Otherwise return null.
233   BlockT *getExitBlock() const {
234     SmallVector<BlockT*, 8> ExitBlocks;
235     getExitBlocks(ExitBlocks);
236     if (ExitBlocks.size() == 1)
237       return ExitBlocks[0];
238     return 0;
239   }
240
241   /// Edge type.
242   typedef std::pair<BlockT*, BlockT*> Edge;
243
244   /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
245   template <typename EdgeT>
246   void getExitEdges(SmallVectorImpl<EdgeT> &ExitEdges) const {
247     // Sort the blocks vector so that we can use binary search to do quick
248     // lookups.
249     SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
250     array_pod_sort(LoopBBs.begin(), LoopBBs.end());
251
252     typedef GraphTraits<BlockT*> BlockTraits;
253     for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
254       for (typename BlockTraits::ChildIteratorType I =
255            BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
256            I != E; ++I)
257         if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
258           // Not in current loop? It must be an exit block.
259           ExitEdges.push_back(EdgeT(*BI, *I));
260   }
261
262   /// getLoopPreheader - If there is a preheader for this loop, return it.  A
263   /// loop has a preheader if there is only one edge to the header of the loop
264   /// from outside of the loop.  If this is the case, the block branching to the
265   /// header of the loop is the preheader node.
266   ///
267   /// This method returns null if there is no preheader for the loop.
268   ///
269   BlockT *getLoopPreheader() const {
270     // Keep track of nodes outside the loop branching to the header...
271     BlockT *Out = getLoopPredecessor();
272     if (!Out) return 0;
273
274     // Make sure there is only one exit out of the preheader.
275     typedef GraphTraits<BlockT*> BlockTraits;
276     typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
277     ++SI;
278     if (SI != BlockTraits::child_end(Out))
279       return 0;  // Multiple exits from the block, must not be a preheader.
280
281     // The predecessor has exactly one successor, so it is a preheader.
282     return Out;
283   }
284
285   /// getLoopPredecessor - If the given loop's header has exactly one unique
286   /// predecessor outside the loop, return it. Otherwise return null.
287   /// This is less strict that the loop "preheader" concept, which requires
288   /// the predecessor to have exactly one successor.
289   ///
290   BlockT *getLoopPredecessor() const {
291     // Keep track of nodes outside the loop branching to the header...
292     BlockT *Out = 0;
293
294     // Loop over the predecessors of the header node...
295     BlockT *Header = getHeader();
296     typedef GraphTraits<BlockT*> BlockTraits;
297     typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
298     for (typename InvBlockTraits::ChildIteratorType PI =
299          InvBlockTraits::child_begin(Header),
300          PE = InvBlockTraits::child_end(Header); PI != PE; ++PI) {
301       typename InvBlockTraits::NodeType *N = *PI;
302       if (!contains(N)) {     // If the block is not in the loop...
303         if (Out && Out != N)
304           return 0;             // Multiple predecessors outside the loop
305         Out = N;
306       }
307     }
308
309     // Make sure there is only one exit out of the preheader.
310     assert(Out && "Header of loop has no predecessors from outside loop?");
311     return Out;
312   }
313
314   /// getLoopLatch - If there is a single latch block for this loop, return it.
315   /// A latch block is a block that contains a branch back to the header.
316   BlockT *getLoopLatch() const {
317     BlockT *Header = getHeader();
318     typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
319     typename InvBlockTraits::ChildIteratorType PI =
320                                             InvBlockTraits::child_begin(Header);
321     typename InvBlockTraits::ChildIteratorType PE =
322                                               InvBlockTraits::child_end(Header);
323     BlockT *Latch = 0;
324     for (; PI != PE; ++PI) {
325       typename InvBlockTraits::NodeType *N = *PI;
326       if (contains(N)) {
327         if (Latch) return 0;
328         Latch = N;
329       }
330     }
331
332     return Latch;
333   }
334
335   //===--------------------------------------------------------------------===//
336   // APIs for updating loop information after changing the CFG
337   //
338
339   /// addBasicBlockToLoop - This method is used by other analyses to update loop
340   /// information.  NewBB is set to be a new member of the current loop.
341   /// Because of this, it is added as a member of all parent loops, and is added
342   /// to the specified LoopInfo object as being in the current basic block.  It
343   /// is not valid to replace the loop header with this method.
344   ///
345   void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
346
347   /// replaceChildLoopWith - This is used when splitting loops up.  It replaces
348   /// the OldChild entry in our children list with NewChild, and updates the
349   /// parent pointer of OldChild to be null and the NewChild to be this loop.
350   /// This updates the loop depth of the new child.
351   void replaceChildLoopWith(LoopT *OldChild,
352                             LoopT *NewChild) {
353     assert(OldChild->ParentLoop == this && "This loop is already broken!");
354     assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
355     typename std::vector<LoopT *>::iterator I =
356                           std::find(SubLoops.begin(), SubLoops.end(), OldChild);
357     assert(I != SubLoops.end() && "OldChild not in loop!");
358     *I = NewChild;
359     OldChild->ParentLoop = 0;
360     NewChild->ParentLoop = static_cast<LoopT *>(this);
361   }
362
363   /// addChildLoop - Add the specified loop to be a child of this loop.  This
364   /// updates the loop depth of the new child.
365   ///
366   void addChildLoop(LoopT *NewChild) {
367     assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
368     NewChild->ParentLoop = static_cast<LoopT *>(this);
369     SubLoops.push_back(NewChild);
370   }
371
372   /// removeChildLoop - This removes the specified child from being a subloop of
373   /// this loop.  The loop is not deleted, as it will presumably be inserted
374   /// into another loop.
375   LoopT *removeChildLoop(iterator I) {
376     assert(I != SubLoops.end() && "Cannot remove end iterator!");
377     LoopT *Child = *I;
378     assert(Child->ParentLoop == this && "Child is not a child of this loop!");
379     SubLoops.erase(SubLoops.begin()+(I-begin()));
380     Child->ParentLoop = 0;
381     return Child;
382   }
383
384   /// addBlockEntry - This adds a basic block directly to the basic block list.
385   /// This should only be used by transformations that create new loops.  Other
386   /// transformations should use addBasicBlockToLoop.
387   void addBlockEntry(BlockT *BB) {
388     Blocks.push_back(BB);
389   }
390
391   /// moveToHeader - This method is used to move BB (which must be part of this
392   /// loop) to be the loop header of the loop (the block that dominates all
393   /// others).
394   void moveToHeader(BlockT *BB) {
395     if (Blocks[0] == BB) return;
396     for (unsigned i = 0; ; ++i) {
397       assert(i != Blocks.size() && "Loop does not contain BB!");
398       if (Blocks[i] == BB) {
399         Blocks[i] = Blocks[0];
400         Blocks[0] = BB;
401         return;
402       }
403     }
404   }
405
406   /// removeBlockFromLoop - This removes the specified basic block from the
407   /// current loop, updating the Blocks as appropriate.  This does not update
408   /// the mapping in the LoopInfo class.
409   void removeBlockFromLoop(BlockT *BB) {
410     RemoveFromVector(Blocks, BB);
411   }
412
413   /// verifyLoop - Verify loop structure
414   void verifyLoop() const {
415 #ifndef NDEBUG
416     assert(!Blocks.empty() && "Loop header is missing");
417
418     // Setup for using a depth-first iterator to visit every block in the loop.
419     SmallVector<BlockT*, 8> ExitBBs;
420     getExitBlocks(ExitBBs);
421     llvm::SmallPtrSet<BlockT*, 8> VisitSet;
422     VisitSet.insert(ExitBBs.begin(), ExitBBs.end());
423     df_ext_iterator<BlockT*, llvm::SmallPtrSet<BlockT*, 8> >
424         BI = df_ext_begin(getHeader(), VisitSet),
425         BE = df_ext_end(getHeader(), VisitSet);
426
427     // Keep track of the number of BBs visited.
428     unsigned NumVisited = 0;
429
430     // Sort the blocks vector so that we can use binary search to do quick
431     // lookups.
432     SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
433     std::sort(LoopBBs.begin(), LoopBBs.end());
434
435     // Check the individual blocks.
436     for ( ; BI != BE; ++BI) {
437       BlockT *BB = *BI;
438       bool HasInsideLoopSuccs = false;
439       bool HasInsideLoopPreds = false;
440       SmallVector<BlockT *, 2> OutsideLoopPreds;
441
442       typedef GraphTraits<BlockT*> BlockTraits;
443       for (typename BlockTraits::ChildIteratorType SI =
444            BlockTraits::child_begin(BB), SE = BlockTraits::child_end(BB);
445            SI != SE; ++SI)
446         if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *SI)) {
447           HasInsideLoopSuccs = true;
448           break;
449         }
450       typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
451       for (typename InvBlockTraits::ChildIteratorType PI =
452            InvBlockTraits::child_begin(BB), PE = InvBlockTraits::child_end(BB);
453            PI != PE; ++PI) {
454         BlockT *N = *PI;
455         if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), N))
456           HasInsideLoopPreds = true;
457         else
458           OutsideLoopPreds.push_back(N);
459       }
460
461       if (BB == getHeader()) {
462         assert(!OutsideLoopPreds.empty() && "Loop is unreachable!");
463       } else if (!OutsideLoopPreds.empty()) {
464         // A non-header loop shouldn't be reachable from outside the loop,
465         // though it is permitted if the predecessor is not itself actually
466         // reachable.
467         BlockT *EntryBB = BB->getParent()->begin();
468         for (df_iterator<BlockT *> NI = df_begin(EntryBB),
469              NE = df_end(EntryBB); NI != NE; ++NI)
470           for (unsigned i = 0, e = OutsideLoopPreds.size(); i != e; ++i)
471             assert(*NI != OutsideLoopPreds[i] &&
472                    "Loop has multiple entry points!");
473       }
474       assert(HasInsideLoopPreds && "Loop block has no in-loop predecessors!");
475       assert(HasInsideLoopSuccs && "Loop block has no in-loop successors!");
476       assert(BB != getHeader()->getParent()->begin() &&
477              "Loop contains function entry block!");
478
479       NumVisited++;
480     }
481
482     assert(NumVisited == getNumBlocks() && "Unreachable block in loop");
483
484     // Check the subloops.
485     for (iterator I = begin(), E = end(); I != E; ++I)
486       // Each block in each subloop should be contained within this loop.
487       for (block_iterator BI = (*I)->block_begin(), BE = (*I)->block_end();
488            BI != BE; ++BI) {
489         assert(std::binary_search(LoopBBs.begin(), LoopBBs.end(), *BI) &&
490                "Loop does not contain all the blocks of a subloop!");
491       }
492
493     // Check the parent loop pointer.
494     if (ParentLoop) {
495       assert(std::find(ParentLoop->begin(), ParentLoop->end(), this) !=
496                ParentLoop->end() &&
497              "Loop is not a subloop of its parent!");
498     }
499 #endif
500   }
501
502   /// verifyLoop - Verify loop structure of this loop and all nested loops.
503   void verifyLoopNest(DenseSet<const LoopT*> *Loops) const {
504     Loops->insert(static_cast<const LoopT *>(this));
505     // Verify this loop.
506     verifyLoop();
507     // Verify the subloops.
508     for (iterator I = begin(), E = end(); I != E; ++I)
509       (*I)->verifyLoopNest(Loops);
510   }
511
512   void print(raw_ostream &OS, unsigned Depth = 0) const {
513     OS.indent(Depth*2) << "Loop at depth " << getLoopDepth()
514        << " containing: ";
515
516     for (unsigned i = 0; i < getBlocks().size(); ++i) {
517       if (i) OS << ",";
518       BlockT *BB = getBlocks()[i];
519       WriteAsOperand(OS, BB, false);
520       if (BB == getHeader())    OS << "<header>";
521       if (BB == getLoopLatch()) OS << "<latch>";
522       if (isLoopExiting(BB))    OS << "<exiting>";
523     }
524     OS << "\n";
525
526     for (iterator I = begin(), E = end(); I != E; ++I)
527       (*I)->print(OS, Depth+2);
528   }
529
530 protected:
531   friend class LoopInfoBase<BlockT, LoopT>;
532   explicit LoopBase(BlockT *BB) : ParentLoop(0) {
533     Blocks.push_back(BB);
534   }
535 };
536
537 template<class BlockT, class LoopT>
538 raw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
539   Loop.print(OS);
540   return OS;
541 }
542
543 class Loop : public LoopBase<BasicBlock, Loop> {
544 public:
545   Loop() {}
546
547   /// isLoopInvariant - Return true if the specified value is loop invariant
548   ///
549   bool isLoopInvariant(Value *V) const;
550
551   /// hasLoopInvariantOperands - Return true if all the operands of the
552   /// specified instruction are loop invariant.
553   bool hasLoopInvariantOperands(Instruction *I) const;
554
555   /// makeLoopInvariant - If the given value is an instruction inside of the
556   /// loop and it can be hoisted, do so to make it trivially loop-invariant.
557   /// Return true if the value after any hoisting is loop invariant. This
558   /// function can be used as a slightly more aggressive replacement for
559   /// isLoopInvariant.
560   ///
561   /// If InsertPt is specified, it is the point to hoist instructions to.
562   /// If null, the terminator of the loop preheader is used.
563   ///
564   bool makeLoopInvariant(Value *V, bool &Changed,
565                          Instruction *InsertPt = 0) const;
566
567   /// makeLoopInvariant - If the given instruction is inside of the
568   /// loop and it can be hoisted, do so to make it trivially loop-invariant.
569   /// Return true if the instruction after any hoisting is loop invariant. This
570   /// function can be used as a slightly more aggressive replacement for
571   /// isLoopInvariant.
572   ///
573   /// If InsertPt is specified, it is the point to hoist instructions to.
574   /// If null, the terminator of the loop preheader is used.
575   ///
576   bool makeLoopInvariant(Instruction *I, bool &Changed,
577                          Instruction *InsertPt = 0) const;
578
579   /// getCanonicalInductionVariable - Check to see if the loop has a canonical
580   /// induction variable: an integer recurrence that starts at 0 and increments
581   /// by one each time through the loop.  If so, return the phi node that
582   /// corresponds to it.
583   ///
584   /// The IndVarSimplify pass transforms loops to have a canonical induction
585   /// variable.
586   ///
587   PHINode *getCanonicalInductionVariable() const;
588
589   /// isLCSSAForm - Return true if the Loop is in LCSSA form
590   bool isLCSSAForm(DominatorTree &DT) const;
591
592   /// isLoopSimplifyForm - Return true if the Loop is in the form that
593   /// the LoopSimplify form transforms loops to, which is sometimes called
594   /// normal form.
595   bool isLoopSimplifyForm() const;
596
597   /// isSafeToClone - Return true if the loop body is safe to clone in practice.
598   bool isSafeToClone() const;
599
600   /// hasDedicatedExits - Return true if no exit block for the loop
601   /// has a predecessor that is outside the loop.
602   bool hasDedicatedExits() const;
603
604   /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
605   /// These are the blocks _outside of the current loop_ which are branched to.
606   /// This assumes that loop exits are in canonical form.
607   ///
608   void getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const;
609
610   /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
611   /// block, return that block. Otherwise return null.
612   BasicBlock *getUniqueExitBlock() const;
613
614   void dump() const;
615
616 private:
617   friend class LoopInfoBase<BasicBlock, Loop>;
618   explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
619 };
620
621 //===----------------------------------------------------------------------===//
622 /// LoopInfo - This class builds and contains all of the top level loop
623 /// structures in the specified function.
624 ///
625
626 template<class BlockT, class LoopT>
627 class LoopInfoBase {
628   // BBMap - Mapping of basic blocks to the inner most loop they occur in
629   DenseMap<BlockT *, LoopT *> BBMap;
630   std::vector<LoopT *> TopLevelLoops;
631   friend class LoopBase<BlockT, LoopT>;
632   friend class LoopInfo;
633
634   void operator=(const LoopInfoBase &); // do not implement
635   LoopInfoBase(const LoopInfo &);       // do not implement
636 public:
637   LoopInfoBase() { }
638   ~LoopInfoBase() { releaseMemory(); }
639
640   void releaseMemory() {
641     for (typename std::vector<LoopT *>::iterator I =
642          TopLevelLoops.begin(), E = TopLevelLoops.end(); I != E; ++I)
643       delete *I;   // Delete all of the loops...
644
645     BBMap.clear();                           // Reset internal state of analysis
646     TopLevelLoops.clear();
647   }
648
649   /// iterator/begin/end - The interface to the top-level loops in the current
650   /// function.
651   ///
652   typedef typename std::vector<LoopT *>::const_iterator iterator;
653   iterator begin() const { return TopLevelLoops.begin(); }
654   iterator end() const { return TopLevelLoops.end(); }
655   bool empty() const { return TopLevelLoops.empty(); }
656
657   /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
658   /// block is in no loop (for example the entry node), null is returned.
659   ///
660   LoopT *getLoopFor(const BlockT *BB) const {
661     return BBMap.lookup(const_cast<BlockT*>(BB));
662   }
663
664   /// operator[] - same as getLoopFor...
665   ///
666   const LoopT *operator[](const BlockT *BB) const {
667     return getLoopFor(BB);
668   }
669
670   /// getLoopDepth - Return the loop nesting level of the specified block.  A
671   /// depth of 0 means the block is not inside any loop.
672   ///
673   unsigned getLoopDepth(const BlockT *BB) const {
674     const LoopT *L = getLoopFor(BB);
675     return L ? L->getLoopDepth() : 0;
676   }
677
678   // isLoopHeader - True if the block is a loop header node
679   bool isLoopHeader(BlockT *BB) const {
680     const LoopT *L = getLoopFor(BB);
681     return L && L->getHeader() == BB;
682   }
683
684   /// removeLoop - This removes the specified top-level loop from this loop info
685   /// object.  The loop is not deleted, as it will presumably be inserted into
686   /// another loop.
687   LoopT *removeLoop(iterator I) {
688     assert(I != end() && "Cannot remove end iterator!");
689     LoopT *L = *I;
690     assert(L->getParentLoop() == 0 && "Not a top-level loop!");
691     TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
692     return L;
693   }
694
695   /// changeLoopFor - Change the top-level loop that contains BB to the
696   /// specified loop.  This should be used by transformations that restructure
697   /// the loop hierarchy tree.
698   void changeLoopFor(BlockT *BB, LoopT *L) {
699     if (!L) {
700       BBMap.erase(BB);
701       return;
702     }
703     BBMap[BB] = L;
704   }
705
706   /// changeTopLevelLoop - Replace the specified loop in the top-level loops
707   /// list with the indicated loop.
708   void changeTopLevelLoop(LoopT *OldLoop,
709                           LoopT *NewLoop) {
710     typename std::vector<LoopT *>::iterator I =
711                  std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
712     assert(I != TopLevelLoops.end() && "Old loop not at top level!");
713     *I = NewLoop;
714     assert(NewLoop->ParentLoop == 0 && OldLoop->ParentLoop == 0 &&
715            "Loops already embedded into a subloop!");
716   }
717
718   /// addTopLevelLoop - This adds the specified loop to the collection of
719   /// top-level loops.
720   void addTopLevelLoop(LoopT *New) {
721     assert(New->getParentLoop() == 0 && "Loop already in subloop!");
722     TopLevelLoops.push_back(New);
723   }
724
725   /// removeBlock - This method completely removes BB from all data structures,
726   /// including all of the Loop objects it is nested in and our mapping from
727   /// BasicBlocks to loops.
728   void removeBlock(BlockT *BB) {
729     typename DenseMap<BlockT *, LoopT *>::iterator I = BBMap.find(BB);
730     if (I != BBMap.end()) {
731       for (LoopT *L = I->second; L; L = L->getParentLoop())
732         L->removeBlockFromLoop(BB);
733
734       BBMap.erase(I);
735     }
736   }
737
738   // Internals
739
740   static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
741                                       const LoopT *ParentLoop) {
742     if (SubLoop == 0) return true;
743     if (SubLoop == ParentLoop) return false;
744     return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
745   }
746
747   void Calculate(DominatorTreeBase<BlockT> &DT) {
748     BlockT *RootNode = DT.getRootNode()->getBlock();
749
750     for (df_iterator<BlockT*> NI = df_begin(RootNode),
751            NE = df_end(RootNode); NI != NE; ++NI)
752       if (LoopT *L = ConsiderForLoop(*NI, DT))
753         TopLevelLoops.push_back(L);
754   }
755
756   LoopT *ConsiderForLoop(BlockT *BB, DominatorTreeBase<BlockT> &DT) {
757     if (BBMap.count(BB)) return 0; // Haven't processed this node?
758
759     std::vector<BlockT *> TodoStack;
760
761     // Scan the predecessors of BB, checking to see if BB dominates any of
762     // them.  This identifies backedges which target this node...
763     typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
764     for (typename InvBlockTraits::ChildIteratorType I =
765          InvBlockTraits::child_begin(BB), E = InvBlockTraits::child_end(BB);
766          I != E; ++I) {
767       typename InvBlockTraits::NodeType *N = *I;
768       // If BB dominates its predecessor...
769       if (DT.dominates(BB, N) && DT.isReachableFromEntry(N))
770           TodoStack.push_back(N);
771     }
772
773     if (TodoStack.empty()) return 0;  // No backedges to this block...
774
775     // Create a new loop to represent this basic block...
776     LoopT *L = new LoopT(BB);
777     BBMap[BB] = L;
778
779     while (!TodoStack.empty()) {  // Process all the nodes in the loop
780       BlockT *X = TodoStack.back();
781       TodoStack.pop_back();
782
783       if (!L->contains(X) &&         // As of yet unprocessed??
784           DT.isReachableFromEntry(X)) {
785         // Check to see if this block already belongs to a loop.  If this occurs
786         // then we have a case where a loop that is supposed to be a child of
787         // the current loop was processed before the current loop.  When this
788         // occurs, this child loop gets added to a part of the current loop,
789         // making it a sibling to the current loop.  We have to reparent this
790         // loop.
791         if (LoopT *SubLoop =
792             const_cast<LoopT *>(getLoopFor(X)))
793           if (SubLoop->getHeader() == X && isNotAlreadyContainedIn(SubLoop, L)){
794             // Remove the subloop from its current parent...
795             assert(SubLoop->ParentLoop && SubLoop->ParentLoop != L);
796             LoopT *SLP = SubLoop->ParentLoop;  // SubLoopParent
797             typename std::vector<LoopT *>::iterator I =
798               std::find(SLP->SubLoops.begin(), SLP->SubLoops.end(), SubLoop);
799             assert(I != SLP->SubLoops.end() &&"SubLoop not a child of parent?");
800             SLP->SubLoops.erase(I);   // Remove from parent...
801
802             // Add the subloop to THIS loop...
803             SubLoop->ParentLoop = L;
804             L->SubLoops.push_back(SubLoop);
805           }
806
807         // Normal case, add the block to our loop...
808         L->Blocks.push_back(X);
809
810         typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
811
812         // Add all of the predecessors of X to the end of the work stack...
813         TodoStack.insert(TodoStack.end(), InvBlockTraits::child_begin(X),
814                          InvBlockTraits::child_end(X));
815       }
816     }
817
818     // If there are any loops nested within this loop, create them now!
819     for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(),
820          E = L->Blocks.end(); I != E; ++I)
821       if (LoopT *NewLoop = ConsiderForLoop(*I, DT)) {
822         L->SubLoops.push_back(NewLoop);
823         NewLoop->ParentLoop = L;
824       }
825
826     // Add the basic blocks that comprise this loop to the BBMap so that this
827     // loop can be found for them.
828     //
829     for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(),
830            E = L->Blocks.end(); I != E; ++I)
831       BBMap.insert(std::make_pair(*I, L));
832
833     // Now that we have a list of all of the child loops of this loop, check to
834     // see if any of them should actually be nested inside of each other.  We
835     // can accidentally pull loops our of their parents, so we must make sure to
836     // organize the loop nests correctly now.
837     {
838       std::map<BlockT *, LoopT *> ContainingLoops;
839       for (unsigned i = 0; i != L->SubLoops.size(); ++i) {
840         LoopT *Child = L->SubLoops[i];
841         assert(Child->getParentLoop() == L && "Not proper child loop?");
842
843         if (LoopT *ContainingLoop = ContainingLoops[Child->getHeader()]) {
844           // If there is already a loop which contains this loop, move this loop
845           // into the containing loop.
846           MoveSiblingLoopInto(Child, ContainingLoop);
847           --i;  // The loop got removed from the SubLoops list.
848         } else {
849           // This is currently considered to be a top-level loop.  Check to see
850           // if any of the contained blocks are loop headers for subloops we
851           // have already processed.
852           for (unsigned b = 0, e = Child->Blocks.size(); b != e; ++b) {
853             LoopT *&BlockLoop = ContainingLoops[Child->Blocks[b]];
854             if (BlockLoop == 0) {   // Child block not processed yet...
855               BlockLoop = Child;
856             } else if (BlockLoop != Child) {
857               LoopT *SubLoop = BlockLoop;
858               // Reparent all of the blocks which used to belong to BlockLoops
859               for (unsigned j = 0, f = SubLoop->Blocks.size(); j != f; ++j)
860                 ContainingLoops[SubLoop->Blocks[j]] = Child;
861
862               // There is already a loop which contains this block, that means
863               // that we should reparent the loop which the block is currently
864               // considered to belong to to be a child of this loop.
865               MoveSiblingLoopInto(SubLoop, Child);
866               --i;  // We just shrunk the SubLoops list.
867             }
868           }
869         }
870       }
871     }
872
873     return L;
874   }
875
876   /// MoveSiblingLoopInto - This method moves the NewChild loop to live inside
877   /// of the NewParent Loop, instead of being a sibling of it.
878   void MoveSiblingLoopInto(LoopT *NewChild,
879                            LoopT *NewParent) {
880     LoopT *OldParent = NewChild->getParentLoop();
881     assert(OldParent && OldParent == NewParent->getParentLoop() &&
882            NewChild != NewParent && "Not sibling loops!");
883
884     // Remove NewChild from being a child of OldParent
885     typename std::vector<LoopT *>::iterator I =
886       std::find(OldParent->SubLoops.begin(), OldParent->SubLoops.end(),
887                 NewChild);
888     assert(I != OldParent->SubLoops.end() && "Parent fields incorrect??");
889     OldParent->SubLoops.erase(I);   // Remove from parent's subloops list
890     NewChild->ParentLoop = 0;
891
892     InsertLoopInto(NewChild, NewParent);
893   }
894
895   /// InsertLoopInto - This inserts loop L into the specified parent loop.  If
896   /// the parent loop contains a loop which should contain L, the loop gets
897   /// inserted into L instead.
898   void InsertLoopInto(LoopT *L, LoopT *Parent) {
899     BlockT *LHeader = L->getHeader();
900     assert(Parent->contains(LHeader) &&
901            "This loop should not be inserted here!");
902
903     // Check to see if it belongs in a child loop...
904     for (unsigned i = 0, e = static_cast<unsigned>(Parent->SubLoops.size());
905          i != e; ++i)
906       if (Parent->SubLoops[i]->contains(LHeader)) {
907         InsertLoopInto(L, Parent->SubLoops[i]);
908         return;
909       }
910
911     // If not, insert it here!
912     Parent->SubLoops.push_back(L);
913     L->ParentLoop = Parent;
914   }
915
916   // Debugging
917
918   void print(raw_ostream &OS) const {
919     for (unsigned i = 0; i < TopLevelLoops.size(); ++i)
920       TopLevelLoops[i]->print(OS);
921   #if 0
922     for (DenseMap<BasicBlock*, LoopT*>::const_iterator I = BBMap.begin(),
923            E = BBMap.end(); I != E; ++I)
924       OS << "BB '" << I->first->getName() << "' level = "
925          << I->second->getLoopDepth() << "\n";
926   #endif
927   }
928 };
929
930 class LoopInfo : public FunctionPass {
931   LoopInfoBase<BasicBlock, Loop> LI;
932   friend class LoopBase<BasicBlock, Loop>;
933
934   void operator=(const LoopInfo &); // do not implement
935   LoopInfo(const LoopInfo &);       // do not implement
936 public:
937   static char ID; // Pass identification, replacement for typeid
938
939   LoopInfo() : FunctionPass(ID) {
940     initializeLoopInfoPass(*PassRegistry::getPassRegistry());
941   }
942
943   LoopInfoBase<BasicBlock, Loop>& getBase() { return LI; }
944
945   /// iterator/begin/end - The interface to the top-level loops in the current
946   /// function.
947   ///
948   typedef LoopInfoBase<BasicBlock, Loop>::iterator iterator;
949   inline iterator begin() const { return LI.begin(); }
950   inline iterator end() const { return LI.end(); }
951   bool empty() const { return LI.empty(); }
952
953   /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
954   /// block is in no loop (for example the entry node), null is returned.
955   ///
956   inline Loop *getLoopFor(const BasicBlock *BB) const {
957     return LI.getLoopFor(BB);
958   }
959
960   /// operator[] - same as getLoopFor...
961   ///
962   inline const Loop *operator[](const BasicBlock *BB) const {
963     return LI.getLoopFor(BB);
964   }
965
966   /// getLoopDepth - Return the loop nesting level of the specified block.  A
967   /// depth of 0 means the block is not inside any loop.
968   ///
969   inline unsigned getLoopDepth(const BasicBlock *BB) const {
970     return LI.getLoopDepth(BB);
971   }
972
973   // isLoopHeader - True if the block is a loop header node
974   inline bool isLoopHeader(BasicBlock *BB) const {
975     return LI.isLoopHeader(BB);
976   }
977
978   /// runOnFunction - Calculate the natural loop information.
979   ///
980   virtual bool runOnFunction(Function &F);
981
982   virtual void verifyAnalysis() const;
983
984   virtual void releaseMemory() { LI.releaseMemory(); }
985
986   virtual void print(raw_ostream &O, const Module* M = 0) const;
987
988   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
989
990   /// removeLoop - This removes the specified top-level loop from this loop info
991   /// object.  The loop is not deleted, as it will presumably be inserted into
992   /// another loop.
993   inline Loop *removeLoop(iterator I) { return LI.removeLoop(I); }
994
995   /// changeLoopFor - Change the top-level loop that contains BB to the
996   /// specified loop.  This should be used by transformations that restructure
997   /// the loop hierarchy tree.
998   inline void changeLoopFor(BasicBlock *BB, Loop *L) {
999     LI.changeLoopFor(BB, L);
1000   }
1001
1002   /// changeTopLevelLoop - Replace the specified loop in the top-level loops
1003   /// list with the indicated loop.
1004   inline void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop) {
1005     LI.changeTopLevelLoop(OldLoop, NewLoop);
1006   }
1007
1008   /// addTopLevelLoop - This adds the specified loop to the collection of
1009   /// top-level loops.
1010   inline void addTopLevelLoop(Loop *New) {
1011     LI.addTopLevelLoop(New);
1012   }
1013
1014   /// removeBlock - This method completely removes BB from all data structures,
1015   /// including all of the Loop objects it is nested in and our mapping from
1016   /// BasicBlocks to loops.
1017   void removeBlock(BasicBlock *BB) {
1018     LI.removeBlock(BB);
1019   }
1020
1021   /// updateUnloop - Update LoopInfo after removing the last backedge from a
1022   /// loop--now the "unloop". This updates the loop forest and parent loops for
1023   /// each block so that Unloop is no longer referenced, but the caller must
1024   /// actually delete the Unloop object.
1025   void updateUnloop(Loop *Unloop);
1026
1027   /// replacementPreservesLCSSAForm - Returns true if replacing From with To
1028   /// everywhere is guaranteed to preserve LCSSA form.
1029   bool replacementPreservesLCSSAForm(Instruction *From, Value *To) {
1030     // Preserving LCSSA form is only problematic if the replacing value is an
1031     // instruction.
1032     Instruction *I = dyn_cast<Instruction>(To);
1033     if (!I) return true;
1034     // If both instructions are defined in the same basic block then replacement
1035     // cannot break LCSSA form.
1036     if (I->getParent() == From->getParent())
1037       return true;
1038     // If the instruction is not defined in a loop then it can safely replace
1039     // anything.
1040     Loop *ToLoop = getLoopFor(I->getParent());
1041     if (!ToLoop) return true;
1042     // If the replacing instruction is defined in the same loop as the original
1043     // instruction, or in a loop that contains it as an inner loop, then using
1044     // it as a replacement will not break LCSSA form.
1045     return ToLoop->contains(getLoopFor(From->getParent()));
1046   }
1047 };
1048
1049
1050 // Allow clients to walk the list of nested loops...
1051 template <> struct GraphTraits<const Loop*> {
1052   typedef const Loop NodeType;
1053   typedef LoopInfo::iterator ChildIteratorType;
1054
1055   static NodeType *getEntryNode(const Loop *L) { return L; }
1056   static inline ChildIteratorType child_begin(NodeType *N) {
1057     return N->begin();
1058   }
1059   static inline ChildIteratorType child_end(NodeType *N) {
1060     return N->end();
1061   }
1062 };
1063
1064 template <> struct GraphTraits<Loop*> {
1065   typedef Loop NodeType;
1066   typedef LoopInfo::iterator ChildIteratorType;
1067
1068   static NodeType *getEntryNode(Loop *L) { return L; }
1069   static inline ChildIteratorType child_begin(NodeType *N) {
1070     return N->begin();
1071   }
1072   static inline ChildIteratorType child_end(NodeType *N) {
1073     return N->end();
1074   }
1075 };
1076
1077 template<class BlockT, class LoopT>
1078 void
1079 LoopBase<BlockT, LoopT>::addBasicBlockToLoop(BlockT *NewBB,
1080                                              LoopInfoBase<BlockT, LoopT> &LIB) {
1081   assert((Blocks.empty() || LIB[getHeader()] == this) &&
1082          "Incorrect LI specified for this loop!");
1083   assert(NewBB && "Cannot add a null basic block to the loop!");
1084   assert(LIB[NewBB] == 0 && "BasicBlock already in the loop!");
1085
1086   LoopT *L = static_cast<LoopT *>(this);
1087
1088   // Add the loop mapping to the LoopInfo object...
1089   LIB.BBMap[NewBB] = L;
1090
1091   // Add the basic block to this loop and all parent loops...
1092   while (L) {
1093     L->Blocks.push_back(NewBB);
1094     L = L->getParentLoop();
1095   }
1096 }
1097
1098 } // End llvm namespace
1099
1100 #endif