LoopInfo.h revision 45cfe545ec8177262dabc70580ce05feaa1c3880
148486893f46d2e12e926682a3ecb908716bc66c4Chris Lattner//===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===//
29769ab22265b313171d201b5928688524a01bd87Misha Brukman//
36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//                     The LLVM Compiler Infrastructure
46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
79769ab22265b313171d201b5928688524a01bd87Misha Brukman//
86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===//
90bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//
100bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner// This file defines the LoopInfo class that is used to identify natural loops
11fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner// and determine the loop depth of various nodes of the CFG.  Note that natural
1246758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner// loops may actually be several loops that share the same header node.
13fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner//
14fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner// This analysis calculates the nesting structure of loops in a function.  For
15fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner// each natural loop identified, this analysis identifies natural loops
16072b163424491c85df6664a4e056aae5e07dc64dChris Lattner// contained entirely within the loop and the basic blocks the make up the loop.
17fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner//
18072b163424491c85df6664a4e056aae5e07dc64dChris Lattner// It can calculate on the fly various bits of information, for example:
19072b163424491c85df6664a4e056aae5e07dc64dChris Lattner//
20072b163424491c85df6664a4e056aae5e07dc64dChris Lattner//  * whether there is a preheader for the loop
21072b163424491c85df6664a4e056aae5e07dc64dChris Lattner//  * the number of back edges to the header
22072b163424491c85df6664a4e056aae5e07dc64dChris Lattner//  * whether or not a particular block branches out of the loop
23072b163424491c85df6664a4e056aae5e07dc64dChris Lattner//  * the successor blocks of the loop
24072b163424491c85df6664a4e056aae5e07dc64dChris Lattner//  * the loop depth
25072b163424491c85df6664a4e056aae5e07dc64dChris Lattner//  * the trip count
26072b163424491c85df6664a4e056aae5e07dc64dChris Lattner//  * etc...
270bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//
280bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//===----------------------------------------------------------------------===//
290bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
300bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner#ifndef LLVM_ANALYSIS_LOOP_INFO_H
310bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner#define LLVM_ANALYSIS_LOOP_INFO_H
320bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
33facd752d3afaeca7dee46648f2a2ae209a94e5e9Chris Lattner#include "llvm/Pass.h"
3444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson#include "llvm/ADT/DepthFirstIterator.h"
35551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/GraphTraits.h"
36b7211a2ce13a0365e0e1dd2f27adda2ee3d1288bDevang Patel#include "llvm/ADT/SmallVector.h"
3744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson#include "llvm/Analysis/Dominators.h"
38019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#include "llvm/Support/CFG.h"
39019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#include "llvm/Support/Streams.h"
40019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#include <algorithm>
41019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#include <ostream>
42019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
431d5562f72e3f7d9e17a2bf95afa54a98dac95894Dan Gohmannamespace llvm {
441d5562f72e3f7d9e17a2bf95afa54a98dac95894Dan Gohman
45019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Andersontemplate<typename T>
46019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Andersonstatic void RemoveFromVector(std::vector<T*> &V, T *N) {
47019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N);
48019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  assert(I != V.end() && "N is not in this list!");
49019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  V.erase(I);
50019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson}
510bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
5253c279b1949f7fa626ccbc399ebbe2d7dc9599a4Devang Patelclass DominatorTree;
538fc2f2072de83665ae20e06929e28317f449bcdfChris Lattnerclass LoopInfo;
54c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanclass Loop;
55c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class N, class M> class LoopInfoBase;
56c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class N, class M> class LoopBase;
570bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
580bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//===----------------------------------------------------------------------===//
59131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner/// LoopBase class - Instances of this class are used to represent loops that
60131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner/// are detected in the flow graph
612b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner///
62c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class BlockT, class LoopT>
63019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Andersonclass LoopBase {
64c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *ParentLoop;
65131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  // SubLoops - Loops contained entirely within this one.
66c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  std::vector<LoopT *> SubLoops;
67131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner
68131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  // Blocks - The list of blocks in this loop.  First entry is the header node.
69131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  std::vector<BlockT*> Blocks;
70019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
71c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  // DO NOT IMPLEMENT
72c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopBase(const LoopBase<BlockT, LoopT> &);
73c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  // DO NOT IMPLEMENT
74c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  const LoopBase<BlockT, LoopT>&operator=(const LoopBase<BlockT, LoopT> &);
750bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattnerpublic:
7646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// Loop ctor - This creates an empty loop.
77019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  LoopBase() : ParentLoop(0) {}
78019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  ~LoopBase() {
7934cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng    for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
804e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner      delete SubLoops[i];
814e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  }
820bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
83ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// getLoopDepth - Return the nesting level of this loop.  An outer-most
84ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// loop has depth 1, for consistency with loop depth values used for basic
85ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// blocks, where depth 0 is used for blocks not inside any loops.
86072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  unsigned getLoopDepth() const {
87ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman    unsigned D = 1;
88c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    for (const LoopT *CurLoop = ParentLoop; CurLoop;
89019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson         CurLoop = CurLoop->ParentLoop)
90072b163424491c85df6664a4e056aae5e07dc64dChris Lattner      ++D;
91072b163424491c85df6664a4e056aae5e07dc64dChris Lattner    return D;
92072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  }
93019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  BlockT *getHeader() const { return Blocks.front(); }
94c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *getParentLoop() const { return ParentLoop; }
950bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
96b670a1737bae5c3ecc65042d3a1b10d774ecd252Wojciech Matyjewicz  /// contains - Return true if the specified basic block is in this loop
97fbdf4bf1799bf9ea566fc0fc0507752590a6d559Misha Brukman  ///
98019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  bool contains(const BlockT *BB) const {
99f3ab3a937203d690744357844948faaf96fb73f9Dan Gohman    return std::find(block_begin(), block_end(), BB) != block_end();
100019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1010bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
102329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  /// iterator/begin/end - Return the loops contained entirely within this loop.
1032b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
104c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  const std::vector<LoopT *> &getSubLoops() const { return SubLoops; }
105c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  typedef typename std::vector<LoopT *>::const_iterator iterator;
106329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator begin() const { return SubLoops.begin(); }
107329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator end() const { return SubLoops.end(); }
10821c276d2fa99914d5ed958ac0aec7d78e3dd87cfDan Gohman  bool empty() const { return SubLoops.empty(); }
109fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner
110fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// getBlocks - Get a list of the basic blocks which make up this loop.
111fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  ///
112019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  const std::vector<BlockT*> &getBlocks() const { return Blocks; }
113019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  typedef typename std::vector<BlockT*>::const_iterator block_iterator;
1144e77cc46ac688e7bee98747049f90e19e2902227Chris Lattner  block_iterator block_begin() const { return Blocks.begin(); }
1154e77cc46ac688e7bee98747049f90e19e2902227Chris Lattner  block_iterator block_end() const { return Blocks.end(); }
116fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner
117280a6e607d8eb7401749a92db624a82de47da777Nick Lewycky  /// isLoopExit - True if terminator in the block can branch to another block
118280a6e607d8eb7401749a92db624a82de47da777Nick Lewycky  /// that is outside of the current loop.
119fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  ///
120019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  bool isLoopExit(const BlockT *BB) const {
121d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<BlockT*> BlockTraits;
122d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    for (typename BlockTraits::ChildIteratorType SI =
123d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         BlockTraits::child_begin(const_cast<BlockT*>(BB)),
124d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         SE = BlockTraits::child_end(const_cast<BlockT*>(BB)); SI != SE; ++SI) {
125019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      if (!contains(*SI))
126019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        return true;
127019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    }
128019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return false;
129019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1306b290a54409f6bb6a0cc1c0446cd2b170a4b7addMisha Brukman
131fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// getNumBackEdges - Calculate the number of back edges to the loop header
132fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  ///
133019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  unsigned getNumBackEdges() const {
134019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    unsigned NumBackEdges = 0;
135019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    BlockT *H = getHeader();
136019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
137d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
138d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    for (typename InvBlockTraits::ChildIteratorType I =
139d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         InvBlockTraits::child_begin(const_cast<BlockT*>(H)),
140d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         E = InvBlockTraits::child_end(const_cast<BlockT*>(H)); I != E; ++I)
141019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      if (contains(*I))
142019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        ++NumBackEdges;
143019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
144019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return NumBackEdges;
145019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1469474dd68e8a050193ca4003940ac399e2b17cb6aChris Lattner
147e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //===--------------------------------------------------------------------===//
148e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // APIs for simple analysis of the loop.
149e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //
150e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // Note that all of these methods can fail on general loops (ie, there may not
151e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // be a preheader, etc).  For best success, the loop simplification and
152e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // induction variable canonicalization pass should be used to normalize loops
153e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // for easy analysis.  These methods assume canonical loops.
154e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner
1557466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  /// getExitingBlocks - Return all blocks inside the loop that have successors
1567466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  /// outside of the loop.  These are the blocks _inside of the current loop_
1577466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  /// which branch out.  The returned list is always unique.
1587466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  ///
159019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const {
160019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // Sort the blocks vector so that we can use binary search to do quick
161019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // lookups.
162019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
163019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    std::sort(LoopBBs.begin(), LoopBBs.end());
164019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
165d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<BlockT*> BlockTraits;
166f3ab3a937203d690744357844948faaf96fb73f9Dan Gohman    for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
167d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson      for (typename BlockTraits::ChildIteratorType I =
168d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson          BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
169d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson          I != E; ++I)
170019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) {
171019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          // Not in current loop? It must be an exit block.
172019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          ExitingBlocks.push_back(*BI);
173019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          break;
174019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        }
175019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1767466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner
177c83324682f3409c15dad992cd62928426c9ad83dDan Gohman  /// getExitingBlock - If getExitingBlocks would return exactly one block,
178c83324682f3409c15dad992cd62928426c9ad83dDan Gohman  /// return that block. Otherwise return null.
179c83324682f3409c15dad992cd62928426c9ad83dDan Gohman  BlockT *getExitingBlock() const {
180c83324682f3409c15dad992cd62928426c9ad83dDan Gohman    SmallVector<BlockT*, 8> ExitingBlocks;
181c83324682f3409c15dad992cd62928426c9ad83dDan Gohman    getExitingBlocks(ExitingBlocks);
182c83324682f3409c15dad992cd62928426c9ad83dDan Gohman    if (ExitingBlocks.size() == 1)
183c83324682f3409c15dad992cd62928426c9ad83dDan Gohman      return ExitingBlocks[0];
184c83324682f3409c15dad992cd62928426c9ad83dDan Gohman    return 0;
185c83324682f3409c15dad992cd62928426c9ad83dDan Gohman  }
186c83324682f3409c15dad992cd62928426c9ad83dDan Gohman
187f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// getExitBlocks - Return all of the successor blocks of this loop.  These
188f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// are the blocks _outside of the current loop_ which are branched to.
189f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  ///
190019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
191019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // Sort the blocks vector so that we can use binary search to do quick
192019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // lookups.
193019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
194019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    std::sort(LoopBBs.begin(), LoopBBs.end());
195019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
196d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<BlockT*> BlockTraits;
197f3ab3a937203d690744357844948faaf96fb73f9Dan Gohman    for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
198d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson      for (typename BlockTraits::ChildIteratorType I =
199d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson           BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
200d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson           I != E; ++I)
201019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
202019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          // Not in current loop? It must be an exit block.
203019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          ExitBlocks.push_back(*I);
204019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
205f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner
2061827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman  /// getExitBlock - If getExitBlocks would return exactly one block,
2071827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman  /// return that block. Otherwise return null.
2081827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman  BlockT *getExitBlock() const {
2091827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman    SmallVector<BlockT*, 8> ExitBlocks;
2101827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman    getExitBlocks(ExitBlocks);
2111827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman    if (ExitBlocks.size() == 1)
2121827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman      return ExitBlocks[0];
2131827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman    return 0;
2141827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman  }
2151827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman
21655e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar  /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
21755e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar  /// (Modelled after getExitingBlocks().)
21855e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar  typedef std::pair<const BlockT*,const BlockT*> Edge;
21955e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar  void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const {
22055e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar    // Sort the blocks vector so that we can use binary search to do quick
22155e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar    // lookups.
22255e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar    SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
22355e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar    std::sort(LoopBBs.begin(), LoopBBs.end());
22455e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar
22555e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar    typedef GraphTraits<BlockT*> BlockTraits;
22655e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar    for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
22755e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar      for (typename BlockTraits::ChildIteratorType I =
22855e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar           BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
22955e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar           I != E; ++I)
23055e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar        if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
23155e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar          // Not in current loop? It must be an exit block.
232c43782cf7163805fb6d727382b5f807ea035b2b0Daniel Dunbar          ExitEdges.push_back(std::make_pair(*BI, *I));
23355e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar  }
23455e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar
2354b8f36f10672bbdd747eabfb5708e4758c3d5337Devang Patel  /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
2364b8f36f10672bbdd747eabfb5708e4758c3d5337Devang Patel  /// These are the blocks _outside of the current loop_ which are branched to.
2374b8f36f10672bbdd747eabfb5708e4758c3d5337Devang Patel  /// This assumes that loop is in canonical form.
2384b8f36f10672bbdd747eabfb5708e4758c3d5337Devang Patel  ///
239019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void getUniqueExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
240019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // Sort the blocks vector so that we can use binary search to do quick
241019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // lookups.
242019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
243019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    std::sort(LoopBBs.begin(), LoopBBs.end());
244019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
245019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    std::vector<BlockT*> switchExitBlocks;
246019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
247f3ab3a937203d690744357844948faaf96fb73f9Dan Gohman    for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) {
248019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
249019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      BlockT *current = *BI;
250019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      switchExitBlocks.clear();
251019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
252d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson      typedef GraphTraits<BlockT*> BlockTraits;
253d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson      typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
254d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson      for (typename BlockTraits::ChildIteratorType I =
255d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson           BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
256d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson           I != E; ++I) {
257019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
258019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      // If block is inside the loop then it is not a exit block.
259019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          continue;
260d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson
261d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson        typename InvBlockTraits::ChildIteratorType PI =
262d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson                                                InvBlockTraits::child_begin(*I);
263019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        BlockT *firstPred = *PI;
264019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
265019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // If current basic block is this exit block's first predecessor
266019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // then only insert exit block in to the output ExitBlocks vector.
267019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // This ensures that same exit block is not inserted twice into
268019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // ExitBlocks vector.
269019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        if (current != firstPred)
270019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          continue;
271019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
272019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // If a terminator has more then two successors, for example SwitchInst,
273019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // then it is possible that there are multiple edges from current block
274019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // to one exit block.
275d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson        if (std::distance(BlockTraits::child_begin(current),
276d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson                          BlockTraits::child_end(current)) <= 2) {
277019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          ExitBlocks.push_back(*I);
278019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          continue;
279019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        }
280019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
281019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // In case of multiple edges from current block to exit block, collect
282019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // only one edge in ExitBlocks. Use switchExitBlocks to keep track of
283019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // duplicate edges.
284019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I)
285019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson            == switchExitBlocks.end()) {
286019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          switchExitBlocks.push_back(*I);
287019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          ExitBlocks.push_back(*I);
288019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        }
289019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      }
290019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    }
291019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
2924b8f36f10672bbdd747eabfb5708e4758c3d5337Devang Patel
293a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman  /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
294a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman  /// block, return that block. Otherwise return null.
295a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman  BlockT *getUniqueExitBlock() const {
296a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman    SmallVector<BlockT*, 8> UniqueExitBlocks;
297a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman    getUniqueExitBlocks(UniqueExitBlocks);
298a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman    if (UniqueExitBlocks.size() == 1)
299a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman      return UniqueExitBlocks[0];
300a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman    return 0;
301a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman  }
302a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman
3032b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// getLoopPreheader - If there is a preheader for this loop, return it.  A
3042b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// loop has a preheader if there is only one edge to the header of the loop
3052b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// from outside of the loop.  If this is the case, the block branching to the
306e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  /// header of the loop is the preheader node.
3072b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
308e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  /// This method returns null if there is no preheader for the loop.
3092b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
310019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  BlockT *getLoopPreheader() const {
311019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // Keep track of nodes outside the loop branching to the header...
312019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    BlockT *Out = 0;
313019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
314019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // Loop over the predecessors of the header node...
315019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    BlockT *Header = getHeader();
316d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<BlockT*> BlockTraits;
317d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
318d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    for (typename InvBlockTraits::ChildIteratorType PI =
319d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         InvBlockTraits::child_begin(Header),
320d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         PE = InvBlockTraits::child_end(Header); PI != PE; ++PI)
321019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      if (!contains(*PI)) {     // If the block is not in the loop...
322019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        if (Out && Out != *PI)
323019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          return 0;             // Multiple predecessors outside the loop
324019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        Out = *PI;
325019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      }
326019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
327019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // Make sure there is only one exit out of the preheader.
328019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(Out && "Header of loop has no predecessors from outside loop?");
329d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
330019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    ++SI;
331d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    if (SI != BlockTraits::child_end(Out))
332019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      return 0;  // Multiple exits from the block, must not be a preheader.
333019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
334131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner    // If there is exactly one preheader, return it.  If there was zero, then
335131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner    // Out is still null.
336019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return Out;
337019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
3382b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner
339b317143ba851ce2853fb262fb2185ef5f1be030dDan Gohman  /// getLoopLatch - If there is a single latch block for this loop, return it.
340b317143ba851ce2853fb262fb2185ef5f1be030dDan Gohman  /// A latch block is a block that contains a branch back to the header.
341b317143ba851ce2853fb262fb2185ef5f1be030dDan Gohman  /// A loop header in normal form has two edges into it: one from a preheader
342b317143ba851ce2853fb262fb2185ef5f1be030dDan Gohman  /// and one from a latch block.
343019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  BlockT *getLoopLatch() const {
344019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    BlockT *Header = getHeader();
345d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
346d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typename InvBlockTraits::ChildIteratorType PI =
347d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson                                            InvBlockTraits::child_begin(Header);
348d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typename InvBlockTraits::ChildIteratorType PE =
349d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson                                              InvBlockTraits::child_end(Header);
350019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    if (PI == PE) return 0;  // no preds?
351019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
352019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    BlockT *Latch = 0;
353019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    if (contains(*PI))
354019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      Latch = *PI;
355019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    ++PI;
356019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    if (PI == PE) return 0;  // only one pred?
357019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
358019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    if (contains(*PI)) {
359019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      if (Latch) return 0;  // multiple backedges
360019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      Latch = *PI;
361019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    }
362019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    ++PI;
363019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    if (PI != PE) return 0;  // more than two preds
364019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
365019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return Latch;
366019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
367e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner
368e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //===--------------------------------------------------------------------===//
369e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // APIs for updating loop information after changing the CFG
370e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //
371e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner
372fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// addBasicBlockToLoop - This method is used by other analyses to update loop
373fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// information.  NewBB is set to be a new member of the current loop.
3742b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// Because of this, it is added as a member of all parent loops, and is added
3752b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// to the specified LoopInfo object as being in the current basic block.  It
3762b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// is not valid to replace the loop header with this method.
3772b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
378c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
3792b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner
38046758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// replaceChildLoopWith - This is used when splitting loops up.  It replaces
38146758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// the OldChild entry in our children list with NewChild, and updates the
38246758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// parent pointer of OldChild to be null and the NewChild to be this loop.
38346758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// This updates the loop depth of the new child.
384c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void replaceChildLoopWith(LoopT *OldChild,
385c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                            LoopT *NewChild) {
386019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(OldChild->ParentLoop == this && "This loop is already broken!");
387019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
388c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    typename std::vector<LoopT *>::iterator I =
389019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson                          std::find(SubLoops.begin(), SubLoops.end(), OldChild);
390019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(I != SubLoops.end() && "OldChild not in loop!");
391019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    *I = NewChild;
392019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    OldChild->ParentLoop = 0;
393c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    NewChild->ParentLoop = static_cast<LoopT *>(this);
394019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
39546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
39646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// addChildLoop - Add the specified loop to be a child of this loop.  This
39746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// updates the loop depth of the new child.
39846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  ///
399c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void addChildLoop(LoopT *NewChild) {
400019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
401c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    NewChild->ParentLoop = static_cast<LoopT *>(this);
402019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SubLoops.push_back(NewChild);
403019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
40446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
40546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// removeChildLoop - This removes the specified child from being a subloop of
40646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// this loop.  The loop is not deleted, as it will presumably be inserted
40746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// into another loop.
408c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *removeChildLoop(iterator I) {
409019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(I != SubLoops.end() && "Cannot remove end iterator!");
410c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *Child = *I;
411019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(Child->ParentLoop == this && "Child is not a child of this loop!");
412019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SubLoops.erase(SubLoops.begin()+(I-begin()));
413019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    Child->ParentLoop = 0;
414019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return Child;
415019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
41646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
41746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// addBlockEntry - This adds a basic block directly to the basic block list.
41846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// This should only be used by transformations that create new loops.  Other
41946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// transformations should use addBasicBlockToLoop.
420019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void addBlockEntry(BlockT *BB) {
42146758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner    Blocks.push_back(BB);
42246758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  }
42346758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
42451a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  /// moveToHeader - This method is used to move BB (which must be part of this
42551a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  /// loop) to be the loop header of the loop (the block that dominates all
42651a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  /// others).
427019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void moveToHeader(BlockT *BB) {
42851a4ad475b095aed49caff176b98c0754e421af4Chris Lattner    if (Blocks[0] == BB) return;
42951a4ad475b095aed49caff176b98c0754e421af4Chris Lattner    for (unsigned i = 0; ; ++i) {
43051a4ad475b095aed49caff176b98c0754e421af4Chris Lattner      assert(i != Blocks.size() && "Loop does not contain BB!");
43151a4ad475b095aed49caff176b98c0754e421af4Chris Lattner      if (Blocks[i] == BB) {
43251a4ad475b095aed49caff176b98c0754e421af4Chris Lattner        Blocks[i] = Blocks[0];
43351a4ad475b095aed49caff176b98c0754e421af4Chris Lattner        Blocks[0] = BB;
43451a4ad475b095aed49caff176b98c0754e421af4Chris Lattner        return;
43551a4ad475b095aed49caff176b98c0754e421af4Chris Lattner      }
43651a4ad475b095aed49caff176b98c0754e421af4Chris Lattner    }
43751a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  }
43851a4ad475b095aed49caff176b98c0754e421af4Chris Lattner
43946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// removeBlockFromLoop - This removes the specified basic block from the
440f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// current loop, updating the Blocks as appropriate.  This does not update
441f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// the mapping in the LoopInfo class.
442019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void removeBlockFromLoop(BlockT *BB) {
443019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    RemoveFromVector(Blocks, BB);
444019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
44546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
44658e0ef1e90c3f6dbae213612b44e56f7d6d65ea7Devang Patel  /// verifyLoop - Verify loop structure
447019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void verifyLoop() const {
448019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#ifndef NDEBUG
449019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert (getHeader() && "Loop header is missing");
450019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert (getLoopPreheader() && "Loop preheader is missing");
451019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert (getLoopLatch() && "Loop latch is missing");
452f3ab3a937203d690744357844948faaf96fb73f9Dan Gohman    for (iterator I = SubLoops.begin(), E = SubLoops.end(); I != E; ++I)
453019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      (*I)->verifyLoop();
454019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#endif
455019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
456019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
457791102fb1192ac9483274e54cbc42480c9b1af10Chris Lattner  void print(raw_ostream &OS, unsigned Depth = 0) const {
458791102fb1192ac9483274e54cbc42480c9b1af10Chris Lattner    OS.indent(Depth*2) << "Loop at depth " << getLoopDepth()
459927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman       << " containing: ";
460019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
461019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    for (unsigned i = 0; i < getBlocks().size(); ++i) {
462019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      if (i) OS << ",";
463927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman      BlockT *BB = getBlocks()[i];
464927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman      WriteAsOperand(OS, BB, false);
465927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman      if (BB == getHeader())    OS << "<header>";
466927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman      if (BB == getLoopLatch()) OS << "<latch>";
467927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman      if (isLoopExit(BB))       OS << "<exit>";
468019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    }
469019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    OS << "\n";
47058e0ef1e90c3f6dbae213612b44e56f7d6d65ea7Devang Patel
471019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    for (iterator I = begin(), E = end(); I != E; ++I)
472019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      (*I)->print(OS, Depth+2);
473019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
474019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
475019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void dump() const {
476791102fb1192ac9483274e54cbc42480c9b1af10Chris Lattner    print(errs());
477019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
478019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
479c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanprotected:
480c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopInfoBase<BlockT, LoopT>;
4811002c0203450620594a85454c6a095ca94b87cb2Dan Gohman  explicit LoopBase(BlockT *BB) : ParentLoop(0) {
482072b163424491c85df6664a4e056aae5e07dc64dChris Lattner    Blocks.push_back(BB);
4830bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
4840bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner};
4850bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
486c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanclass Loop : public LoopBase<BasicBlock, Loop> {
487c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanpublic:
488c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  Loop() {}
48916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
49016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// isLoopInvariant - Return true if the specified value is loop invariant
49116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
49216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  bool isLoopInvariant(Value *V) const;
49316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
494a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// isLoopInvariant - Return true if the specified instruction is
495a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// loop-invariant.
496a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
497a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  bool isLoopInvariant(Instruction *I) const;
498a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman
499206613b289f60b71a76e9190d36b9ea9e47a701eDan Gohman  /// makeLoopInvariant - If the given value is an instruction inside of the
500a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// loop and it can be hoisted, do so to make it trivially loop-invariant.
501a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// Return true if the value after any hoisting is loop invariant. This
502a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// function can be used as a slightly more aggressive replacement for
503a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// isLoopInvariant.
504a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
505a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If InsertPt is specified, it is the point to hoist instructions to.
506a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If null, the terminator of the loop preheader is used.
507a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
508bdc017edacb713119b24ab269d250a82d62fffebDan Gohman  bool makeLoopInvariant(Value *V, bool &Changed,
509bdc017edacb713119b24ab269d250a82d62fffebDan Gohman                         Instruction *InsertPt = 0) const;
510a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman
511a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// makeLoopInvariant - If the given instruction is inside of the
512a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// loop and it can be hoisted, do so to make it trivially loop-invariant.
513a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// Return true if the instruction after any hoisting is loop invariant. This
514a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// function can be used as a slightly more aggressive replacement for
515a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// isLoopInvariant.
516a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
517a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If InsertPt is specified, it is the point to hoist instructions to.
518a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If null, the terminator of the loop preheader is used.
519a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
520bdc017edacb713119b24ab269d250a82d62fffebDan Gohman  bool makeLoopInvariant(Instruction *I, bool &Changed,
521bdc017edacb713119b24ab269d250a82d62fffebDan Gohman                         Instruction *InsertPt = 0) const;
522a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman
52316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// getCanonicalInductionVariable - Check to see if the loop has a canonical
52416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// induction variable: an integer recurrence that starts at 0 and increments
52516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// by one each time through the loop.  If so, return the phi node that
52616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// corresponds to it.
52716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
52816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// The IndVarSimplify pass transforms loops to have a canonical induction
52916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// variable.
53016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
53116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  PHINode *getCanonicalInductionVariable() const;
53216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
53316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// getCanonicalInductionVariableIncrement - Return the LLVM value that holds
53416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// the canonical induction variable value for the "next" iteration of the
53516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// loop.  This always succeeds if getCanonicalInductionVariable succeeds.
53616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
53716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  Instruction *getCanonicalInductionVariableIncrement() const;
53816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
53916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// getTripCount - Return a loop-invariant LLVM value indicating the number of
54016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// times the loop will be executed.  Note that this means that the backedge
54116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// of the loop executes N-1 times.  If the trip-count cannot be determined,
54216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// this returns null.
54316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
54416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// The IndVarSimplify pass transforms loops to have a form that this
54516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// function easily understands.
54616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
54716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  Value *getTripCount() const;
54816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
54916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// getSmallConstantTripCount - Returns the trip count of this loop as a
55016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// normal unsigned value, if possible. Returns 0 if the trip count is unknown
55116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// of not constant. Will also return 0 if the trip count is very large
55216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// (>= 2^32)
55316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  unsigned getSmallConstantTripCount() const;
55416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
55516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// getSmallConstantTripMultiple - Returns the largest constant divisor of the
55616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// trip count of this loop as a normal unsigned value, if possible. This
55716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// means that the actual trip count is always a multiple of the returned
55816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// value (don't forget the trip count could very well be zero as well!).
55916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
56016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// Returns 1 if the trip count is unknown or not guaranteed to be the
56116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// multiple of a constant (which is also the case if the trip count is simply
56216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// constant, use getSmallConstantTripCount for that case), Will also return 1
56316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// if the trip count is very large (>= 2^32).
56416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  unsigned getSmallConstantTripMultiple() const;
56516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
56616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// isLCSSAForm - Return true if the Loop is in LCSSA form
56716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  bool isLCSSAForm() const;
56816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
569937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  /// isLoopSimplifyForm - Return true if the Loop is in the form that
570937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  /// the LoopSimplify form transforms loops to, which is sometimes called
571937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  /// normal form.
572937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  bool isLoopSimplifyForm() const;
573937738649386b8188524d0cd61943214a5b93cf6Dan Gohman
574c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanprivate:
575c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopInfoBase<BasicBlock, Loop>;
576c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
577c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman};
5780bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
5790bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//===----------------------------------------------------------------------===//
5802b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner/// LoopInfo - This class builds and contains all of the top level loop
5812b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner/// structures in the specified function.
5822b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner///
58344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
584c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class BlockT, class LoopT>
58544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonclass LoopInfoBase {
5860bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  // BBMap - Mapping of basic blocks to the inner most loop they occur in
587c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  std::map<BlockT *, LoopT *> BBMap;
588c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  std::vector<LoopT *> TopLevelLoops;
589c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopBase<BlockT, LoopT>;
5909d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman
5919d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  void operator=(const LoopInfoBase &); // do not implement
5929d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  LoopInfoBase(const LoopInfo &);       // do not implement
5930bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattnerpublic:
59444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  LoopInfoBase() { }
59544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ~LoopInfoBase() { releaseMemory(); }
59644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
59744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  void releaseMemory() {
598c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    for (typename std::vector<LoopT *>::iterator I =
59944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson         TopLevelLoops.begin(), E = TopLevelLoops.end(); I != E; ++I)
60044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      delete *I;   // Delete all of the loops...
6010bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
602131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner    BBMap.clear();                           // Reset internal state of analysis
60344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    TopLevelLoops.clear();
60444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
60544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
606329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  /// iterator/begin/end - The interface to the top-level loops in the current
607329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  /// function.
608329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  ///
609c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  typedef typename std::vector<LoopT *>::const_iterator iterator;
610329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator begin() const { return TopLevelLoops.begin(); }
611329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator end() const { return TopLevelLoops.end(); }
612a8c763b3071ae1a58ee8baeb282331245527e004Dan Gohman  bool empty() const { return TopLevelLoops.empty(); }
61344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
6142b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
6152b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// block is in no loop (for example the entry node), null is returned.
6162b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
617c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *getLoopFor(const BlockT *BB) const {
618c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    typename std::map<BlockT *, LoopT *>::const_iterator I=
619d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson      BBMap.find(const_cast<BlockT*>(BB));
6200bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner    return I != BBMap.end() ? I->second : 0;
6210bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
62244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
6232b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// operator[] - same as getLoopFor...
6242b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
625c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  const LoopT *operator[](const BlockT *BB) const {
6260bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner    return getLoopFor(BB);
6270bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
62844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
629ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// getLoopDepth - Return the loop nesting level of the specified block.  A
630ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// depth of 0 means the block is not inside any loop.
6312b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
63244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  unsigned getLoopDepth(const BlockT *BB) const {
633c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    const LoopT *L = getLoopFor(BB);
6340bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner    return L ? L->getLoopDepth() : 0;
6350bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
6360bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
6370bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  // isLoopHeader - True if the block is a loop header node
63844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  bool isLoopHeader(BlockT *BB) const {
639c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    const LoopT *L = getLoopFor(BB);
64050f5490842d501e269a4c6085d0d132cae0d31f8Chris Lattner    return L && L->getHeader() == BB;
6410bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
64244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
64344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// removeLoop - This removes the specified top-level loop from this loop info
64444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// object.  The loop is not deleted, as it will presumably be inserted into
64544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// another loop.
646c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *removeLoop(iterator I) {
64744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(I != end() && "Cannot remove end iterator!");
648c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *L = *I;
64944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(L->getParentLoop() == 0 && "Not a top-level loop!");
65044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
65144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    return L;
65244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
65344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
65444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// changeLoopFor - Change the top-level loop that contains BB to the
65544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// specified loop.  This should be used by transformations that restructure
65644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// the loop hierarchy tree.
657c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void changeLoopFor(BlockT *BB, LoopT *L) {
658c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *&OldLoop = BBMap[BB];
65944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(OldLoop && "Block not in a loop yet!");
66044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    OldLoop = L;
66144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
66244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
66344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// changeTopLevelLoop - Replace the specified loop in the top-level loops
66444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// list with the indicated loop.
665c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void changeTopLevelLoop(LoopT *OldLoop,
666c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                          LoopT *NewLoop) {
667c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    typename std::vector<LoopT *>::iterator I =
66844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson                 std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
66944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(I != TopLevelLoops.end() && "Old loop not at top level!");
67044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    *I = NewLoop;
67144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(NewLoop->ParentLoop == 0 && OldLoop->ParentLoop == 0 &&
67244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson           "Loops already embedded into a subloop!");
67344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
67444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
67544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// addTopLevelLoop - This adds the specified loop to the collection of
67644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// top-level loops.
677c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void addTopLevelLoop(LoopT *New) {
67844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(New->getParentLoop() == 0 && "Loop already in subloop!");
67944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    TopLevelLoops.push_back(New);
68044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
68144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
68244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// removeBlock - This method completely removes BB from all data structures,
68344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// including all of the Loop objects it is nested in and our mapping from
68444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// BasicBlocks to loops.
68544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  void removeBlock(BlockT *BB) {
686c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    typename std::map<BlockT *, LoopT *>::iterator I = BBMap.find(BB);
68744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (I != BBMap.end()) {
688c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman      for (LoopT *L = I->second; L; L = L->getParentLoop())
68944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        L->removeBlockFromLoop(BB);
69044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
69144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      BBMap.erase(I);
69244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    }
69344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
69444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
69544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  // Internals
69644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
697c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
698c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                                      const LoopT *ParentLoop) {
69944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (SubLoop == 0) return true;
70044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (SubLoop == ParentLoop) return false;
70144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
70244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
70344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
704d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson  void Calculate(DominatorTreeBase<BlockT> &DT) {
70544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    BlockT *RootNode = DT.getRootNode()->getBlock();
70644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
70744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    for (df_iterator<BlockT*> NI = df_begin(RootNode),
70844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson           NE = df_end(RootNode); NI != NE; ++NI)
709c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman      if (LoopT *L = ConsiderForLoop(*NI, DT))
71044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        TopLevelLoops.push_back(L);
71144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
71244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
713c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *ConsiderForLoop(BlockT *BB, DominatorTreeBase<BlockT> &DT) {
71444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (BBMap.find(BB) != BBMap.end()) return 0;// Haven't processed this node?
71544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
71644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    std::vector<BlockT *> TodoStack;
71744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
71844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // Scan the predecessors of BB, checking to see if BB dominates any of
71944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // them.  This identifies backedges which target this node...
720d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
721d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    for (typename InvBlockTraits::ChildIteratorType I =
722d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         InvBlockTraits::child_begin(BB), E = InvBlockTraits::child_end(BB);
723d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         I != E; ++I)
72444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      if (DT.dominates(BB, *I))   // If BB dominates it's predecessor...
72544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        TodoStack.push_back(*I);
72644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
72744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (TodoStack.empty()) return 0;  // No backedges to this block...
72844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
72944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // Create a new loop to represent this basic block...
730c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *L = new LoopT(BB);
73144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    BBMap[BB] = L;
73244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
733e4ad9c70e4a1261c212b11623d99e2477ef02783Owen Anderson    BlockT *EntryBlock = BB->getParent()->begin();
73444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
73544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    while (!TodoStack.empty()) {  // Process all the nodes in the loop
73644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      BlockT *X = TodoStack.back();
73744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      TodoStack.pop_back();
73844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
73944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      if (!L->contains(X) &&         // As of yet unprocessed??
74044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          DT.dominates(EntryBlock, X)) {   // X is reachable from entry block?
74144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        // Check to see if this block already belongs to a loop.  If this occurs
742131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner        // then we have a case where a loop that is supposed to be a child of
743131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner        // the current loop was processed before the current loop.  When this
744131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner        // occurs, this child loop gets added to a part of the current loop,
745131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner        // making it a sibling to the current loop.  We have to reparent this
746131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner        // loop.
747c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman        if (LoopT *SubLoop =
748c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman            const_cast<LoopT *>(getLoopFor(X)))
749131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner          if (SubLoop->getHeader() == X && isNotAlreadyContainedIn(SubLoop, L)){
75044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            // Remove the subloop from it's current parent...
75144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            assert(SubLoop->ParentLoop && SubLoop->ParentLoop != L);
752c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman            LoopT *SLP = SubLoop->ParentLoop;  // SubLoopParent
753c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman            typename std::vector<LoopT *>::iterator I =
75444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              std::find(SLP->SubLoops.begin(), SLP->SubLoops.end(), SubLoop);
755131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner            assert(I != SLP->SubLoops.end() &&"SubLoop not a child of parent?");
75644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            SLP->SubLoops.erase(I);   // Remove from parent...
75744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
75844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            // Add the subloop to THIS loop...
75944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            SubLoop->ParentLoop = L;
76044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            L->SubLoops.push_back(SubLoop);
76144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          }
76244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
76344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        // Normal case, add the block to our loop...
76444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        L->Blocks.push_back(X);
765d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson
766d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson        typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
767d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson
76844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        // Add all of the predecessors of X to the end of the work stack...
769d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson        TodoStack.insert(TodoStack.end(), InvBlockTraits::child_begin(X),
770d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson                         InvBlockTraits::child_end(X));
77144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      }
77244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    }
77344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
77444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // If there are any loops nested within this loop, create them now!
77544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(),
77644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson         E = L->Blocks.end(); I != E; ++I)
777c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman      if (LoopT *NewLoop = ConsiderForLoop(*I, DT)) {
77844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        L->SubLoops.push_back(NewLoop);
77944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        NewLoop->ParentLoop = L;
78044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      }
78144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
78244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // Add the basic blocks that comprise this loop to the BBMap so that this
78344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // loop can be found for them.
78444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    //
78544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(),
78644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson           E = L->Blocks.end(); I != E; ++I) {
787c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman      typename std::map<BlockT*, LoopT *>::iterator BBMI = BBMap.find(*I);
788c418bf3dd593b5b2fe2f978930f6d0d6b17e344eDan Gohman      if (BBMI == BBMap.end())                       // Not in map yet...
78944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        BBMap.insert(BBMI, std::make_pair(*I, L));   // Must be at this level
79044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    }
79144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
79244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // Now that we have a list of all of the child loops of this loop, check to
793131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner    // see if any of them should actually be nested inside of each other.  We
794131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner    // can accidentally pull loops our of their parents, so we must make sure to
79544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // organize the loop nests correctly now.
79644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    {
797c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman      std::map<BlockT *, LoopT *> ContainingLoops;
79844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      for (unsigned i = 0; i != L->SubLoops.size(); ++i) {
799c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman        LoopT *Child = L->SubLoops[i];
80044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        assert(Child->getParentLoop() == L && "Not proper child loop?");
80144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
802c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman        if (LoopT *ContainingLoop = ContainingLoops[Child->getHeader()]) {
80344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          // If there is already a loop which contains this loop, move this loop
80444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          // into the containing loop.
80544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          MoveSiblingLoopInto(Child, ContainingLoop);
80644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          --i;  // The loop got removed from the SubLoops list.
80744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        } else {
808131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner          // This is currently considered to be a top-level loop.  Check to see
809131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner          // if any of the contained blocks are loop headers for subloops we
810131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner          // have already processed.
81144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          for (unsigned b = 0, e = Child->Blocks.size(); b != e; ++b) {
812c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman            LoopT *&BlockLoop = ContainingLoops[Child->Blocks[b]];
81344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            if (BlockLoop == 0) {   // Child block not processed yet...
81444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              BlockLoop = Child;
81544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            } else if (BlockLoop != Child) {
816c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman              LoopT *SubLoop = BlockLoop;
81744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              // Reparent all of the blocks which used to belong to BlockLoops
81844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              for (unsigned j = 0, e = SubLoop->Blocks.size(); j != e; ++j)
81944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson                ContainingLoops[SubLoop->Blocks[j]] = Child;
82044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
82144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              // There is already a loop which contains this block, that means
82244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              // that we should reparent the loop which the block is currently
82344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              // considered to belong to to be a child of this loop.
82444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              MoveSiblingLoopInto(SubLoop, Child);
82544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              --i;  // We just shrunk the SubLoops list.
82644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            }
82744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          }
82844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        }
82944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      }
83044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    }
83144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
83244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    return L;
83344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
83444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
835131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  /// MoveSiblingLoopInto - This method moves the NewChild loop to live inside
836131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  /// of the NewParent Loop, instead of being a sibling of it.
837c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void MoveSiblingLoopInto(LoopT *NewChild,
838c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                           LoopT *NewParent) {
839c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *OldParent = NewChild->getParentLoop();
84044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(OldParent && OldParent == NewParent->getParentLoop() &&
84144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson           NewChild != NewParent && "Not sibling loops!");
84244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
84344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // Remove NewChild from being a child of OldParent
844c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    typename std::vector<LoopT *>::iterator I =
845131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner      std::find(OldParent->SubLoops.begin(), OldParent->SubLoops.end(),
846131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner                NewChild);
84744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(I != OldParent->SubLoops.end() && "Parent fields incorrect??");
84844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    OldParent->SubLoops.erase(I);   // Remove from parent's subloops list
84944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    NewChild->ParentLoop = 0;
85044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
85144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    InsertLoopInto(NewChild, NewParent);
85244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
85344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
854131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  /// InsertLoopInto - This inserts loop L into the specified parent loop.  If
855131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  /// the parent loop contains a loop which should contain L, the loop gets
856131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  /// inserted into L instead.
857c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void InsertLoopInto(LoopT *L, LoopT *Parent) {
85844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    BlockT *LHeader = L->getHeader();
859131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner    assert(Parent->contains(LHeader) &&
860131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner           "This loop should not be inserted here!");
86144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
86244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // Check to see if it belongs in a child loop...
86334cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng    for (unsigned i = 0, e = static_cast<unsigned>(Parent->SubLoops.size());
86434cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng         i != e; ++i)
86544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      if (Parent->SubLoops[i]->contains(LHeader)) {
86644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        InsertLoopInto(L, Parent->SubLoops[i]);
86744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        return;
86844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      }
86944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
87044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // If not, insert it here!
87144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    Parent->SubLoops.push_back(L);
87244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    L->ParentLoop = Parent;
87344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
87444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
87544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  // Debugging
87644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
877791102fb1192ac9483274e54cbc42480c9b1af10Chris Lattner  void print(raw_ostream &OS) const {
87844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    for (unsigned i = 0; i < TopLevelLoops.size(); ++i)
87944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      TopLevelLoops[i]->print(OS);
88044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  #if 0
881c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    for (std::map<BasicBlock*, LoopT*>::const_iterator I = BBMap.begin(),
88244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson           E = BBMap.end(); I != E; ++I)
88344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      OS << "BB '" << I->first->getName() << "' level = "
88444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson         << I->second->getLoopDepth() << "\n";
88544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  #endif
88644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
88744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson};
88844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
88944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonclass LoopInfo : public FunctionPass {
890c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopInfoBase<BasicBlock, Loop> LI;
891c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopBase<BasicBlock, Loop>;
8929d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman
8939d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  void operator=(const LoopInfo &); // do not implement
8949d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  LoopInfo(const LoopInfo &);       // do not implement
89544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonpublic:
89644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  static char ID; // Pass identification, replacement for typeid
89744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
8989d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  LoopInfo() : FunctionPass(&ID) {}
89944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
900c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopInfoBase<BasicBlock, Loop>& getBase() { return LI; }
901d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson
90244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// iterator/begin/end - The interface to the top-level loops in the current
90344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// function.
90444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
905c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  typedef LoopInfoBase<BasicBlock, Loop>::iterator iterator;
9069d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  inline iterator begin() const { return LI.begin(); }
9079d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  inline iterator end() const { return LI.end(); }
9089d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  bool empty() const { return LI.empty(); }
90944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
91044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
91144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// block is in no loop (for example the entry node), null is returned.
91244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
91344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline Loop *getLoopFor(const BasicBlock *BB) const {
9149d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.getLoopFor(BB);
91544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
91644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
91744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// operator[] - same as getLoopFor...
91844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
91944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline const Loop *operator[](const BasicBlock *BB) const {
9209d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.getLoopFor(BB);
92144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
92244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
923ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// getLoopDepth - Return the loop nesting level of the specified block.  A
924ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// depth of 0 means the block is not inside any loop.
92544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
92644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline unsigned getLoopDepth(const BasicBlock *BB) const {
9279d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.getLoopDepth(BB);
92844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
92944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
93044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  // isLoopHeader - True if the block is a loop header node
93144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline bool isLoopHeader(BasicBlock *BB) const {
9329d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.isLoopHeader(BB);
93344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
9340bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
9352b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// runOnFunction - Calculate the natural loop information.
9362b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
9377e70829632f82de15db187845666aaca6e04b792Chris Lattner  virtual bool runOnFunction(Function &F);
938facd752d3afaeca7dee46648f2a2ae209a94e5e9Chris Lattner
9399d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  virtual void releaseMemory() { LI.releaseMemory(); }
9405c7e326585f3a543388ba871c3425f7664cd9143Bill Wendling
94145cfe545ec8177262dabc70580ce05feaa1c3880Chris Lattner  virtual void print(raw_ostream &O, const Module* M = 0) const;
942791102fb1192ac9483274e54cbc42480c9b1af10Chris Lattner
943f57b845547302d24ecb6a9e79d7bc386f761a6c9Chris Lattner  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
944f57b845547302d24ecb6a9e79d7bc386f761a6c9Chris Lattner
9454e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  /// removeLoop - This removes the specified top-level loop from this loop info
9464e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  /// object.  The loop is not deleted, as it will presumably be inserted into
9474e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  /// another loop.
9489d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  inline Loop *removeLoop(iterator I) { return LI.removeLoop(I); }
9494e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner
95046758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// changeLoopFor - Change the top-level loop that contains BB to the
95146758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// specified loop.  This should be used by transformations that restructure
95246758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// the loop hierarchy tree.
95344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline void changeLoopFor(BasicBlock *BB, Loop *L) {
9549d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.changeLoopFor(BB, L);
95544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
95646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
95746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// changeTopLevelLoop - Replace the specified loop in the top-level loops
95846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// list with the indicated loop.
95944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop) {
9609d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.changeTopLevelLoop(OldLoop, NewLoop);
96144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
96246758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
963072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  /// addTopLevelLoop - This adds the specified loop to the collection of
964072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  /// top-level loops.
96544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline void addTopLevelLoop(Loop *New) {
9669d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.addTopLevelLoop(New);
967072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  }
968072b163424491c85df6664a4e056aae5e07dc64dChris Lattner
9699afb24bf0847b9f2ff0bf3f7f7405dcbe42fa38bChris Lattner  /// removeBlock - This method completely removes BB from all data structures,
9709afb24bf0847b9f2ff0bf3f7f7405dcbe42fa38bChris Lattner  /// including all of the Loop objects it is nested in and our mapping from
9719afb24bf0847b9f2ff0bf3f7f7405dcbe42fa38bChris Lattner  /// BasicBlocks to loops.
97244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  void removeBlock(BasicBlock *BB) {
9739d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.removeBlock(BB);
97444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
975c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman
976c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  static bool isNotAlreadyContainedIn(const Loop *SubLoop,
977c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                                      const Loop *ParentLoop) {
978c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    return
979c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman      LoopInfoBase<BasicBlock, Loop>::isNotAlreadyContainedIn(SubLoop,
980c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                                                              ParentLoop);
981c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  }
9820bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner};
9830bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
984e0b6b78e095f7dea9589e8df5ec4521e346ad005Anand Shukla
9851db0a400370466e187ae06c96a1586c2c21409ddChris Lattner// Allow clients to walk the list of nested loops...
9861db0a400370466e187ae06c96a1586c2c21409ddChris Lattnertemplate <> struct GraphTraits<const Loop*> {
9871db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  typedef const Loop NodeType;
9889d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  typedef LoopInfo::iterator ChildIteratorType;
9891db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
9901db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  static NodeType *getEntryNode(const Loop *L) { return L; }
9919769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_begin(NodeType *N) {
992329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->begin();
9931db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
9949769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_end(NodeType *N) {
995329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->end();
9961db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
9971db0a400370466e187ae06c96a1586c2c21409ddChris Lattner};
9981db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
9991db0a400370466e187ae06c96a1586c2c21409ddChris Lattnertemplate <> struct GraphTraits<Loop*> {
10001db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  typedef Loop NodeType;
10019d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  typedef LoopInfo::iterator ChildIteratorType;
10021db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
10031db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  static NodeType *getEntryNode(Loop *L) { return L; }
10049769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_begin(NodeType *N) {
1005329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->begin();
10061db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
10079769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_end(NodeType *N) {
1008329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->end();
10091db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
10101db0a400370466e187ae06c96a1586c2c21409ddChris Lattner};
10111db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
1012c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class BlockT, class LoopT>
1013c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanvoid
1014c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan GohmanLoopBase<BlockT, LoopT>::addBasicBlockToLoop(BlockT *NewBB,
1015c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                                             LoopInfoBase<BlockT, LoopT> &LIB) {
1016d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson  assert((Blocks.empty() || LIB[getHeader()] == this) &&
1017019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson         "Incorrect LI specified for this loop!");
1018019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  assert(NewBB && "Cannot add a null basic block to the loop!");
1019d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson  assert(LIB[NewBB] == 0 && "BasicBlock already in the loop!");
102044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
1021c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *L = static_cast<LoopT *>(this);
1022c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman
1023019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  // Add the loop mapping to the LoopInfo object...
1024c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LIB.BBMap[NewBB] = L;
1025019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
1026019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  // Add the basic block to this loop and all parent loops...
1027019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  while (L) {
1028019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    L->Blocks.push_back(NewBB);
1029019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    L = L->getParentLoop();
1030019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1031019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson}
1032019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
1033d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
1034d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
10350bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner#endif
1036