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