LoopInfo.h revision 103289e9383ad1eb66caf28c9b166aebce963a35
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"
39103289e9383ad1eb66caf28c9b166aebce963a35Chris Lattner#include "llvm/Support/raw_ostream.h"
40019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#include <algorithm>
41019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
421d5562f72e3f7d9e17a2bf95afa54a98dac95894Dan Gohmannamespace llvm {
431d5562f72e3f7d9e17a2bf95afa54a98dac95894Dan Gohman
44019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Andersontemplate<typename T>
45019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Andersonstatic void RemoveFromVector(std::vector<T*> &V, T *N) {
46019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N);
47019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  assert(I != V.end() && "N is not in this list!");
48019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  V.erase(I);
49019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson}
500bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
5153c279b1949f7fa626ccbc399ebbe2d7dc9599a4Devang Patelclass DominatorTree;
528fc2f2072de83665ae20e06929e28317f449bcdfChris Lattnerclass LoopInfo;
53c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanclass Loop;
54c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class N, class M> class LoopInfoBase;
55c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class N, class M> class LoopBase;
560bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
570bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//===----------------------------------------------------------------------===//
58131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner/// LoopBase class - Instances of this class are used to represent loops that
59131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner/// are detected in the flow graph
602b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner///
61c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class BlockT, class LoopT>
62019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Andersonclass LoopBase {
63c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *ParentLoop;
64131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  // SubLoops - Loops contained entirely within this one.
65c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  std::vector<LoopT *> SubLoops;
66131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner
67131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  // Blocks - The list of blocks in this loop.  First entry is the header node.
68131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  std::vector<BlockT*> Blocks;
69019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
70c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  // DO NOT IMPLEMENT
71c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopBase(const LoopBase<BlockT, LoopT> &);
72c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  // DO NOT IMPLEMENT
73c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  const LoopBase<BlockT, LoopT>&operator=(const LoopBase<BlockT, LoopT> &);
740bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattnerpublic:
7546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// Loop ctor - This creates an empty loop.
76019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  LoopBase() : ParentLoop(0) {}
77019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  ~LoopBase() {
7834cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng    for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
794e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner      delete SubLoops[i];
804e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  }
810bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
82ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// getLoopDepth - Return the nesting level of this loop.  An outer-most
83ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// loop has depth 1, for consistency with loop depth values used for basic
84ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// blocks, where depth 0 is used for blocks not inside any loops.
85072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  unsigned getLoopDepth() const {
86ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman    unsigned D = 1;
87c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    for (const LoopT *CurLoop = ParentLoop; CurLoop;
88019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson         CurLoop = CurLoop->ParentLoop)
89072b163424491c85df6664a4e056aae5e07dc64dChris Lattner      ++D;
90072b163424491c85df6664a4e056aae5e07dc64dChris Lattner    return D;
91072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  }
92019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  BlockT *getHeader() const { return Blocks.front(); }
93c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *getParentLoop() const { return ParentLoop; }
940bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
95b670a1737bae5c3ecc65042d3a1b10d774ecd252Wojciech Matyjewicz  /// contains - Return true if the specified basic block is in this loop
96fbdf4bf1799bf9ea566fc0fc0507752590a6d559Misha Brukman  ///
97019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  bool contains(const BlockT *BB) const {
98f3ab3a937203d690744357844948faaf96fb73f9Dan Gohman    return std::find(block_begin(), block_end(), BB) != block_end();
99019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1000bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
101329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  /// iterator/begin/end - Return the loops contained entirely within this loop.
1022b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
103c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  const std::vector<LoopT *> &getSubLoops() const { return SubLoops; }
104c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  typedef typename std::vector<LoopT *>::const_iterator iterator;
105329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator begin() const { return SubLoops.begin(); }
106329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator end() const { return SubLoops.end(); }
10721c276d2fa99914d5ed958ac0aec7d78e3dd87cfDan Gohman  bool empty() const { return SubLoops.empty(); }
108fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner
109fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// getBlocks - Get a list of the basic blocks which make up this loop.
110fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  ///
111019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  const std::vector<BlockT*> &getBlocks() const { return Blocks; }
112019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  typedef typename std::vector<BlockT*>::const_iterator block_iterator;
1134e77cc46ac688e7bee98747049f90e19e2902227Chris Lattner  block_iterator block_begin() const { return Blocks.begin(); }
1144e77cc46ac688e7bee98747049f90e19e2902227Chris Lattner  block_iterator block_end() const { return Blocks.end(); }
115fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner
116280a6e607d8eb7401749a92db624a82de47da777Nick Lewycky  /// isLoopExit - True if terminator in the block can branch to another block
117280a6e607d8eb7401749a92db624a82de47da777Nick Lewycky  /// that is outside of the current loop.
118fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  ///
119019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  bool isLoopExit(const BlockT *BB) const {
120d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<BlockT*> BlockTraits;
121d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    for (typename BlockTraits::ChildIteratorType SI =
122d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         BlockTraits::child_begin(const_cast<BlockT*>(BB)),
123d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         SE = BlockTraits::child_end(const_cast<BlockT*>(BB)); SI != SE; ++SI) {
124019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      if (!contains(*SI))
125019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        return true;
126019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    }
127019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return false;
128019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1296b290a54409f6bb6a0cc1c0446cd2b170a4b7addMisha Brukman
130fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// getNumBackEdges - Calculate the number of back edges to the loop header
131fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  ///
132019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  unsigned getNumBackEdges() const {
133019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    unsigned NumBackEdges = 0;
134019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    BlockT *H = getHeader();
135019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
136d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
137d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    for (typename InvBlockTraits::ChildIteratorType I =
138d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         InvBlockTraits::child_begin(const_cast<BlockT*>(H)),
139d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         E = InvBlockTraits::child_end(const_cast<BlockT*>(H)); I != E; ++I)
140019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      if (contains(*I))
141019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        ++NumBackEdges;
142019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
143019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return NumBackEdges;
144019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1459474dd68e8a050193ca4003940ac399e2b17cb6aChris Lattner
146e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //===--------------------------------------------------------------------===//
147e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // APIs for simple analysis of the loop.
148e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //
149e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // Note that all of these methods can fail on general loops (ie, there may not
150e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // be a preheader, etc).  For best success, the loop simplification and
151e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // induction variable canonicalization pass should be used to normalize loops
152e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // for easy analysis.  These methods assume canonical loops.
153e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner
1547466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  /// getExitingBlocks - Return all blocks inside the loop that have successors
1557466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  /// outside of the loop.  These are the blocks _inside of the current loop_
1567466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  /// which branch out.  The returned list is always unique.
1577466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  ///
158019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const {
159019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // Sort the blocks vector so that we can use binary search to do quick
160019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // lookups.
161019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
162019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    std::sort(LoopBBs.begin(), LoopBBs.end());
163019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
164d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<BlockT*> BlockTraits;
165f3ab3a937203d690744357844948faaf96fb73f9Dan Gohman    for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
166d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson      for (typename BlockTraits::ChildIteratorType I =
167d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson          BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
168d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson          I != E; ++I)
169019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) {
170019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          // Not in current loop? It must be an exit block.
171019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          ExitingBlocks.push_back(*BI);
172019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          break;
173019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        }
174019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1757466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner
176c83324682f3409c15dad992cd62928426c9ad83dDan Gohman  /// getExitingBlock - If getExitingBlocks would return exactly one block,
177c83324682f3409c15dad992cd62928426c9ad83dDan Gohman  /// return that block. Otherwise return null.
178c83324682f3409c15dad992cd62928426c9ad83dDan Gohman  BlockT *getExitingBlock() const {
179c83324682f3409c15dad992cd62928426c9ad83dDan Gohman    SmallVector<BlockT*, 8> ExitingBlocks;
180c83324682f3409c15dad992cd62928426c9ad83dDan Gohman    getExitingBlocks(ExitingBlocks);
181c83324682f3409c15dad992cd62928426c9ad83dDan Gohman    if (ExitingBlocks.size() == 1)
182c83324682f3409c15dad992cd62928426c9ad83dDan Gohman      return ExitingBlocks[0];
183c83324682f3409c15dad992cd62928426c9ad83dDan Gohman    return 0;
184c83324682f3409c15dad992cd62928426c9ad83dDan Gohman  }
185c83324682f3409c15dad992cd62928426c9ad83dDan Gohman
186f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// getExitBlocks - Return all of the successor blocks of this loop.  These
187f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// are the blocks _outside of the current loop_ which are branched to.
188f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  ///
189019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
190019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // Sort the blocks vector so that we can use binary search to do quick
191019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // lookups.
192019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
193019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    std::sort(LoopBBs.begin(), LoopBBs.end());
194019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
195d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<BlockT*> BlockTraits;
196f3ab3a937203d690744357844948faaf96fb73f9Dan Gohman    for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
197d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson      for (typename BlockTraits::ChildIteratorType I =
198d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson           BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
199d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson           I != E; ++I)
200019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
201019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          // Not in current loop? It must be an exit block.
202019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          ExitBlocks.push_back(*I);
203019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
204f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner
2051827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman  /// getExitBlock - If getExitBlocks would return exactly one block,
2061827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman  /// return that block. Otherwise return null.
2071827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman  BlockT *getExitBlock() const {
2081827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman    SmallVector<BlockT*, 8> ExitBlocks;
2091827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman    getExitBlocks(ExitBlocks);
2101827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman    if (ExitBlocks.size() == 1)
2111827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman      return ExitBlocks[0];
2121827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman    return 0;
2131827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman  }
2141827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman
21555e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar  /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
21655e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar  /// (Modelled after getExitingBlocks().)
21755e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar  typedef std::pair<const BlockT*,const BlockT*> Edge;
21855e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar  void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const {
21955e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar    // Sort the blocks vector so that we can use binary search to do quick
22055e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar    // lookups.
22155e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar    SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
22255e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar    std::sort(LoopBBs.begin(), LoopBBs.end());
22355e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar
22455e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar    typedef GraphTraits<BlockT*> BlockTraits;
22555e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar    for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
22655e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar      for (typename BlockTraits::ChildIteratorType I =
22755e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar           BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
22855e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar           I != E; ++I)
22955e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar        if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
23055e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar          // Not in current loop? It must be an exit block.
231c43782cf7163805fb6d727382b5f807ea035b2b0Daniel Dunbar          ExitEdges.push_back(std::make_pair(*BI, *I));
23255e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar  }
23355e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar
2344b8f36f10672bbdd747eabfb5708e4758c3d5337Devang Patel  /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
2354b8f36f10672bbdd747eabfb5708e4758c3d5337Devang Patel  /// These are the blocks _outside of the current loop_ which are branched to.
2364b8f36f10672bbdd747eabfb5708e4758c3d5337Devang Patel  /// This assumes that loop is in canonical form.
2374b8f36f10672bbdd747eabfb5708e4758c3d5337Devang Patel  ///
238019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void getUniqueExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
239019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // Sort the blocks vector so that we can use binary search to do quick
240019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // lookups.
241019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
242019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    std::sort(LoopBBs.begin(), LoopBBs.end());
243019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
244019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    std::vector<BlockT*> switchExitBlocks;
245019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
246f3ab3a937203d690744357844948faaf96fb73f9Dan Gohman    for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) {
247019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
248019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      BlockT *current = *BI;
249019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      switchExitBlocks.clear();
250019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
251d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson      typedef GraphTraits<BlockT*> BlockTraits;
252d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson      typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
253d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson      for (typename BlockTraits::ChildIteratorType I =
254d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson           BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
255d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson           I != E; ++I) {
256019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
257019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      // If block is inside the loop then it is not a exit block.
258019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          continue;
259d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson
260d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson        typename InvBlockTraits::ChildIteratorType PI =
261d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson                                                InvBlockTraits::child_begin(*I);
262019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        BlockT *firstPred = *PI;
263019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
264019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // If current basic block is this exit block's first predecessor
265019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // then only insert exit block in to the output ExitBlocks vector.
266019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // This ensures that same exit block is not inserted twice into
267019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // ExitBlocks vector.
268019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        if (current != firstPred)
269019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          continue;
270019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
271019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // If a terminator has more then two successors, for example SwitchInst,
272019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // then it is possible that there are multiple edges from current block
273019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // to one exit block.
274d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson        if (std::distance(BlockTraits::child_begin(current),
275d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson                          BlockTraits::child_end(current)) <= 2) {
276019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          ExitBlocks.push_back(*I);
277019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          continue;
278019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        }
279019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
280019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // In case of multiple edges from current block to exit block, collect
281019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // only one edge in ExitBlocks. Use switchExitBlocks to keep track of
282019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        // duplicate edges.
283019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I)
284019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson            == switchExitBlocks.end()) {
285019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          switchExitBlocks.push_back(*I);
286019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          ExitBlocks.push_back(*I);
287019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        }
288019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      }
289019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    }
290019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
2914b8f36f10672bbdd747eabfb5708e4758c3d5337Devang Patel
292a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman  /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
293a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman  /// block, return that block. Otherwise return null.
294a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman  BlockT *getUniqueExitBlock() const {
295a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman    SmallVector<BlockT*, 8> UniqueExitBlocks;
296a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman    getUniqueExitBlocks(UniqueExitBlocks);
297a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman    if (UniqueExitBlocks.size() == 1)
298a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman      return UniqueExitBlocks[0];
299a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman    return 0;
300a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman  }
301a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman
3022b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// getLoopPreheader - If there is a preheader for this loop, return it.  A
3032b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// loop has a preheader if there is only one edge to the header of the loop
3042b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// from outside of the loop.  If this is the case, the block branching to the
305e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  /// header of the loop is the preheader node.
3062b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
307e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  /// This method returns null if there is no preheader for the loop.
3082b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
309019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  BlockT *getLoopPreheader() const {
310019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // Keep track of nodes outside the loop branching to the header...
311019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    BlockT *Out = 0;
312019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
313019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // Loop over the predecessors of the header node...
314019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    BlockT *Header = getHeader();
315d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<BlockT*> BlockTraits;
316d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
317d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    for (typename InvBlockTraits::ChildIteratorType PI =
318d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         InvBlockTraits::child_begin(Header),
319d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         PE = InvBlockTraits::child_end(Header); PI != PE; ++PI)
320019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      if (!contains(*PI)) {     // If the block is not in the loop...
321019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        if (Out && Out != *PI)
322019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          return 0;             // Multiple predecessors outside the loop
323019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        Out = *PI;
324019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      }
325019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
326019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // Make sure there is only one exit out of the preheader.
327019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(Out && "Header of loop has no predecessors from outside loop?");
328d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
329019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    ++SI;
330d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    if (SI != BlockTraits::child_end(Out))
331019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      return 0;  // Multiple exits from the block, must not be a preheader.
332019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
333131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner    // If there is exactly one preheader, return it.  If there was zero, then
334131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner    // Out is still null.
335019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return Out;
336019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
3372b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner
338b317143ba851ce2853fb262fb2185ef5f1be030dDan Gohman  /// getLoopLatch - If there is a single latch block for this loop, return it.
339b317143ba851ce2853fb262fb2185ef5f1be030dDan Gohman  /// A latch block is a block that contains a branch back to the header.
340b317143ba851ce2853fb262fb2185ef5f1be030dDan Gohman  /// A loop header in normal form has two edges into it: one from a preheader
341b317143ba851ce2853fb262fb2185ef5f1be030dDan Gohman  /// and one from a latch block.
342019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  BlockT *getLoopLatch() const {
343019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    BlockT *Header = getHeader();
344d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
345d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typename InvBlockTraits::ChildIteratorType PI =
346d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson                                            InvBlockTraits::child_begin(Header);
347d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typename InvBlockTraits::ChildIteratorType PE =
348d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson                                              InvBlockTraits::child_end(Header);
349019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    if (PI == PE) return 0;  // no preds?
350019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
351019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    BlockT *Latch = 0;
352019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    if (contains(*PI))
353019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      Latch = *PI;
354019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    ++PI;
355019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    if (PI == PE) return 0;  // only one pred?
356019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
357019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    if (contains(*PI)) {
358019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      if (Latch) return 0;  // multiple backedges
359019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      Latch = *PI;
360019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    }
361019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    ++PI;
362019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    if (PI != PE) return 0;  // more than two preds
363019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
364019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return Latch;
365019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
366e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner
367e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //===--------------------------------------------------------------------===//
368e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // APIs for updating loop information after changing the CFG
369e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //
370e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner
371fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// addBasicBlockToLoop - This method is used by other analyses to update loop
372fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// information.  NewBB is set to be a new member of the current loop.
3732b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// Because of this, it is added as a member of all parent loops, and is added
3742b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// to the specified LoopInfo object as being in the current basic block.  It
3752b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// is not valid to replace the loop header with this method.
3762b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
377c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
3782b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner
37946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// replaceChildLoopWith - This is used when splitting loops up.  It replaces
38046758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// the OldChild entry in our children list with NewChild, and updates the
38146758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// parent pointer of OldChild to be null and the NewChild to be this loop.
38246758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// This updates the loop depth of the new child.
383c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void replaceChildLoopWith(LoopT *OldChild,
384c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                            LoopT *NewChild) {
385019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(OldChild->ParentLoop == this && "This loop is already broken!");
386019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
387c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    typename std::vector<LoopT *>::iterator I =
388019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson                          std::find(SubLoops.begin(), SubLoops.end(), OldChild);
389019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(I != SubLoops.end() && "OldChild not in loop!");
390019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    *I = NewChild;
391019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    OldChild->ParentLoop = 0;
392c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    NewChild->ParentLoop = static_cast<LoopT *>(this);
393019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
39446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
39546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// addChildLoop - Add the specified loop to be a child of this loop.  This
39646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// updates the loop depth of the new child.
39746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  ///
398c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void addChildLoop(LoopT *NewChild) {
399019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
400c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    NewChild->ParentLoop = static_cast<LoopT *>(this);
401019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SubLoops.push_back(NewChild);
402019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
40346758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
40446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// removeChildLoop - This removes the specified child from being a subloop of
40546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// this loop.  The loop is not deleted, as it will presumably be inserted
40646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// into another loop.
407c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *removeChildLoop(iterator I) {
408019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(I != SubLoops.end() && "Cannot remove end iterator!");
409c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *Child = *I;
410019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(Child->ParentLoop == this && "Child is not a child of this loop!");
411019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SubLoops.erase(SubLoops.begin()+(I-begin()));
412019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    Child->ParentLoop = 0;
413019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return Child;
414019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
41546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
41646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// addBlockEntry - This adds a basic block directly to the basic block list.
41746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// This should only be used by transformations that create new loops.  Other
41846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// transformations should use addBasicBlockToLoop.
419019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void addBlockEntry(BlockT *BB) {
42046758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner    Blocks.push_back(BB);
42146758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  }
42246758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
42351a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  /// moveToHeader - This method is used to move BB (which must be part of this
42451a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  /// loop) to be the loop header of the loop (the block that dominates all
42551a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  /// others).
426019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void moveToHeader(BlockT *BB) {
42751a4ad475b095aed49caff176b98c0754e421af4Chris Lattner    if (Blocks[0] == BB) return;
42851a4ad475b095aed49caff176b98c0754e421af4Chris Lattner    for (unsigned i = 0; ; ++i) {
42951a4ad475b095aed49caff176b98c0754e421af4Chris Lattner      assert(i != Blocks.size() && "Loop does not contain BB!");
43051a4ad475b095aed49caff176b98c0754e421af4Chris Lattner      if (Blocks[i] == BB) {
43151a4ad475b095aed49caff176b98c0754e421af4Chris Lattner        Blocks[i] = Blocks[0];
43251a4ad475b095aed49caff176b98c0754e421af4Chris Lattner        Blocks[0] = BB;
43351a4ad475b095aed49caff176b98c0754e421af4Chris Lattner        return;
43451a4ad475b095aed49caff176b98c0754e421af4Chris Lattner      }
43551a4ad475b095aed49caff176b98c0754e421af4Chris Lattner    }
43651a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  }
43751a4ad475b095aed49caff176b98c0754e421af4Chris Lattner
43846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// removeBlockFromLoop - This removes the specified basic block from the
439f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// current loop, updating the Blocks as appropriate.  This does not update
440f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// the mapping in the LoopInfo class.
441019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void removeBlockFromLoop(BlockT *BB) {
442019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    RemoveFromVector(Blocks, BB);
443019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
44446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
44558e0ef1e90c3f6dbae213612b44e56f7d6d65ea7Devang Patel  /// verifyLoop - Verify loop structure
446019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void verifyLoop() const {
447019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#ifndef NDEBUG
448019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert (getHeader() && "Loop header is missing");
449019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert (getLoopPreheader() && "Loop preheader is missing");
450019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert (getLoopLatch() && "Loop latch is missing");
451f3ab3a937203d690744357844948faaf96fb73f9Dan Gohman    for (iterator I = SubLoops.begin(), E = SubLoops.end(); I != E; ++I)
452019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      (*I)->verifyLoop();
453019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#endif
454019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
455019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
456791102fb1192ac9483274e54cbc42480c9b1af10Chris Lattner  void print(raw_ostream &OS, unsigned Depth = 0) const {
457791102fb1192ac9483274e54cbc42480c9b1af10Chris Lattner    OS.indent(Depth*2) << "Loop at depth " << getLoopDepth()
458927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman       << " containing: ";
459019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
460019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    for (unsigned i = 0; i < getBlocks().size(); ++i) {
461019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      if (i) OS << ",";
462927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman      BlockT *BB = getBlocks()[i];
463927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman      WriteAsOperand(OS, BB, false);
464927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman      if (BB == getHeader())    OS << "<header>";
465927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman      if (BB == getLoopLatch()) OS << "<latch>";
466927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman      if (isLoopExit(BB))       OS << "<exit>";
467019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    }
468019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    OS << "\n";
46958e0ef1e90c3f6dbae213612b44e56f7d6d65ea7Devang Patel
470019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    for (iterator I = begin(), E = end(); I != E; ++I)
471019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      (*I)->print(OS, Depth+2);
472019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
473019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
474019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void dump() const {
475791102fb1192ac9483274e54cbc42480c9b1af10Chris Lattner    print(errs());
476019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
477019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
478c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanprotected:
479c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopInfoBase<BlockT, LoopT>;
4801002c0203450620594a85454c6a095ca94b87cb2Dan Gohman  explicit LoopBase(BlockT *BB) : ParentLoop(0) {
481072b163424491c85df6664a4e056aae5e07dc64dChris Lattner    Blocks.push_back(BB);
4820bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
4830bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner};
4840bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
485c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanclass Loop : public LoopBase<BasicBlock, Loop> {
486c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanpublic:
487c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  Loop() {}
48816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
48916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// isLoopInvariant - Return true if the specified value is loop invariant
49016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
49116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  bool isLoopInvariant(Value *V) const;
49216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
493a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// isLoopInvariant - Return true if the specified instruction is
494a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// loop-invariant.
495a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
496a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  bool isLoopInvariant(Instruction *I) const;
497a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman
498206613b289f60b71a76e9190d36b9ea9e47a701eDan Gohman  /// makeLoopInvariant - If the given value is an instruction inside of the
499a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// loop and it can be hoisted, do so to make it trivially loop-invariant.
500a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// Return true if the value after any hoisting is loop invariant. This
501a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// function can be used as a slightly more aggressive replacement for
502a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// isLoopInvariant.
503a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
504a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If InsertPt is specified, it is the point to hoist instructions to.
505a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If null, the terminator of the loop preheader is used.
506a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
507bdc017edacb713119b24ab269d250a82d62fffebDan Gohman  bool makeLoopInvariant(Value *V, bool &Changed,
508bdc017edacb713119b24ab269d250a82d62fffebDan Gohman                         Instruction *InsertPt = 0) const;
509a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman
510a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// makeLoopInvariant - If the given instruction is inside of the
511a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// loop and it can be hoisted, do so to make it trivially loop-invariant.
512a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// Return true if the instruction after any hoisting is loop invariant. This
513a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// function can be used as a slightly more aggressive replacement for
514a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// isLoopInvariant.
515a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
516a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If InsertPt is specified, it is the point to hoist instructions to.
517a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If null, the terminator of the loop preheader is used.
518a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
519bdc017edacb713119b24ab269d250a82d62fffebDan Gohman  bool makeLoopInvariant(Instruction *I, bool &Changed,
520bdc017edacb713119b24ab269d250a82d62fffebDan Gohman                         Instruction *InsertPt = 0) const;
521a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman
52216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// getCanonicalInductionVariable - Check to see if the loop has a canonical
52316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// induction variable: an integer recurrence that starts at 0 and increments
52416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// by one each time through the loop.  If so, return the phi node that
52516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// corresponds to it.
52616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
52716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// The IndVarSimplify pass transforms loops to have a canonical induction
52816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// variable.
52916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
53016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  PHINode *getCanonicalInductionVariable() const;
53116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
53216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// getCanonicalInductionVariableIncrement - Return the LLVM value that holds
53316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// the canonical induction variable value for the "next" iteration of the
53416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// loop.  This always succeeds if getCanonicalInductionVariable succeeds.
53516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
53616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  Instruction *getCanonicalInductionVariableIncrement() const;
53716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
53816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// getTripCount - Return a loop-invariant LLVM value indicating the number of
53916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// times the loop will be executed.  Note that this means that the backedge
54016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// of the loop executes N-1 times.  If the trip-count cannot be determined,
54116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// this returns null.
54216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
54316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// The IndVarSimplify pass transforms loops to have a form that this
54416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// function easily understands.
54516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
54616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  Value *getTripCount() const;
54716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
54816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// getSmallConstantTripCount - Returns the trip count of this loop as a
54916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// normal unsigned value, if possible. Returns 0 if the trip count is unknown
55016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// of not constant. Will also return 0 if the trip count is very large
55116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// (>= 2^32)
55216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  unsigned getSmallConstantTripCount() const;
55316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
55416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// getSmallConstantTripMultiple - Returns the largest constant divisor of the
55516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// trip count of this loop as a normal unsigned value, if possible. This
55616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// means that the actual trip count is always a multiple of the returned
55716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// value (don't forget the trip count could very well be zero as well!).
55816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
55916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// Returns 1 if the trip count is unknown or not guaranteed to be the
56016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// multiple of a constant (which is also the case if the trip count is simply
56116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// constant, use getSmallConstantTripCount for that case), Will also return 1
56216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// if the trip count is very large (>= 2^32).
56316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  unsigned getSmallConstantTripMultiple() const;
56416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
56516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// isLCSSAForm - Return true if the Loop is in LCSSA form
56616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  bool isLCSSAForm() const;
56716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
568937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  /// isLoopSimplifyForm - Return true if the Loop is in the form that
569937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  /// the LoopSimplify form transforms loops to, which is sometimes called
570937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  /// normal form.
571937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  bool isLoopSimplifyForm() const;
572937738649386b8188524d0cd61943214a5b93cf6Dan Gohman
573c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanprivate:
574c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopInfoBase<BasicBlock, Loop>;
575c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
576c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman};
5770bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
5780bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//===----------------------------------------------------------------------===//
5792b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner/// LoopInfo - This class builds and contains all of the top level loop
5802b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner/// structures in the specified function.
5812b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner///
58244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
583c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class BlockT, class LoopT>
58444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonclass LoopInfoBase {
5850bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  // BBMap - Mapping of basic blocks to the inner most loop they occur in
586c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  std::map<BlockT *, LoopT *> BBMap;
587c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  std::vector<LoopT *> TopLevelLoops;
588c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopBase<BlockT, LoopT>;
5899d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman
5909d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  void operator=(const LoopInfoBase &); // do not implement
5919d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  LoopInfoBase(const LoopInfo &);       // do not implement
5920bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattnerpublic:
59344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  LoopInfoBase() { }
59444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ~LoopInfoBase() { releaseMemory(); }
59544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
59644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  void releaseMemory() {
597c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    for (typename std::vector<LoopT *>::iterator I =
59844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson         TopLevelLoops.begin(), E = TopLevelLoops.end(); I != E; ++I)
59944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      delete *I;   // Delete all of the loops...
6000bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
601131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner    BBMap.clear();                           // Reset internal state of analysis
60244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    TopLevelLoops.clear();
60344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
60444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
605329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  /// iterator/begin/end - The interface to the top-level loops in the current
606329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  /// function.
607329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  ///
608c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  typedef typename std::vector<LoopT *>::const_iterator iterator;
609329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator begin() const { return TopLevelLoops.begin(); }
610329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator end() const { return TopLevelLoops.end(); }
611a8c763b3071ae1a58ee8baeb282331245527e004Dan Gohman  bool empty() const { return TopLevelLoops.empty(); }
61244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
6132b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
6142b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// block is in no loop (for example the entry node), null is returned.
6152b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
616c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *getLoopFor(const BlockT *BB) const {
617c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    typename std::map<BlockT *, LoopT *>::const_iterator I=
618d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson      BBMap.find(const_cast<BlockT*>(BB));
6190bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner    return I != BBMap.end() ? I->second : 0;
6200bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
62144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
6222b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// operator[] - same as getLoopFor...
6232b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
624c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  const LoopT *operator[](const BlockT *BB) const {
6250bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner    return getLoopFor(BB);
6260bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
62744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
628ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// getLoopDepth - Return the loop nesting level of the specified block.  A
629ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// depth of 0 means the block is not inside any loop.
6302b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
63144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  unsigned getLoopDepth(const BlockT *BB) const {
632c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    const LoopT *L = getLoopFor(BB);
6330bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner    return L ? L->getLoopDepth() : 0;
6340bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
6350bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
6360bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  // isLoopHeader - True if the block is a loop header node
63744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  bool isLoopHeader(BlockT *BB) const {
638c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    const LoopT *L = getLoopFor(BB);
63950f5490842d501e269a4c6085d0d132cae0d31f8Chris Lattner    return L && L->getHeader() == BB;
6400bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
64144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
64244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// removeLoop - This removes the specified top-level loop from this loop info
64344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// object.  The loop is not deleted, as it will presumably be inserted into
64444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// another loop.
645c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *removeLoop(iterator I) {
64644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(I != end() && "Cannot remove end iterator!");
647c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *L = *I;
64844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(L->getParentLoop() == 0 && "Not a top-level loop!");
64944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
65044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    return L;
65144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
65244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
65344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// changeLoopFor - Change the top-level loop that contains BB to the
65444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// specified loop.  This should be used by transformations that restructure
65544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// the loop hierarchy tree.
656c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void changeLoopFor(BlockT *BB, LoopT *L) {
657c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *&OldLoop = BBMap[BB];
65844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(OldLoop && "Block not in a loop yet!");
65944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    OldLoop = L;
66044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
66144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
66244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// changeTopLevelLoop - Replace the specified loop in the top-level loops
66344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// list with the indicated loop.
664c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void changeTopLevelLoop(LoopT *OldLoop,
665c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                          LoopT *NewLoop) {
666c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    typename std::vector<LoopT *>::iterator I =
66744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson                 std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
66844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(I != TopLevelLoops.end() && "Old loop not at top level!");
66944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    *I = NewLoop;
67044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(NewLoop->ParentLoop == 0 && OldLoop->ParentLoop == 0 &&
67144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson           "Loops already embedded into a subloop!");
67244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
67344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
67444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// addTopLevelLoop - This adds the specified loop to the collection of
67544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// top-level loops.
676c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void addTopLevelLoop(LoopT *New) {
67744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(New->getParentLoop() == 0 && "Loop already in subloop!");
67844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    TopLevelLoops.push_back(New);
67944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
68044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
68144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// removeBlock - This method completely removes BB from all data structures,
68244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// including all of the Loop objects it is nested in and our mapping from
68344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// BasicBlocks to loops.
68444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  void removeBlock(BlockT *BB) {
685c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    typename std::map<BlockT *, LoopT *>::iterator I = BBMap.find(BB);
68644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (I != BBMap.end()) {
687c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman      for (LoopT *L = I->second; L; L = L->getParentLoop())
68844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        L->removeBlockFromLoop(BB);
68944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
69044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      BBMap.erase(I);
69144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    }
69244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
69344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
69444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  // Internals
69544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
696c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
697c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                                      const LoopT *ParentLoop) {
69844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (SubLoop == 0) return true;
69944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (SubLoop == ParentLoop) return false;
70044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
70144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
70244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
703d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson  void Calculate(DominatorTreeBase<BlockT> &DT) {
70444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    BlockT *RootNode = DT.getRootNode()->getBlock();
70544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
70644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    for (df_iterator<BlockT*> NI = df_begin(RootNode),
70744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson           NE = df_end(RootNode); NI != NE; ++NI)
708c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman      if (LoopT *L = ConsiderForLoop(*NI, DT))
70944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        TopLevelLoops.push_back(L);
71044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
71144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
712c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *ConsiderForLoop(BlockT *BB, DominatorTreeBase<BlockT> &DT) {
71344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (BBMap.find(BB) != BBMap.end()) return 0;// Haven't processed this node?
71444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
71544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    std::vector<BlockT *> TodoStack;
71644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
71744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // Scan the predecessors of BB, checking to see if BB dominates any of
71844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // them.  This identifies backedges which target this node...
719d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
720d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    for (typename InvBlockTraits::ChildIteratorType I =
721d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         InvBlockTraits::child_begin(BB), E = InvBlockTraits::child_end(BB);
722d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         I != E; ++I)
72344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      if (DT.dominates(BB, *I))   // If BB dominates it's predecessor...
72444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        TodoStack.push_back(*I);
72544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
72644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (TodoStack.empty()) return 0;  // No backedges to this block...
72744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
72844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // Create a new loop to represent this basic block...
729c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *L = new LoopT(BB);
73044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    BBMap[BB] = L;
73144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
732e4ad9c70e4a1261c212b11623d99e2477ef02783Owen Anderson    BlockT *EntryBlock = BB->getParent()->begin();
73344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
73444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    while (!TodoStack.empty()) {  // Process all the nodes in the loop
73544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      BlockT *X = TodoStack.back();
73644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      TodoStack.pop_back();
73744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
73844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      if (!L->contains(X) &&         // As of yet unprocessed??
73944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          DT.dominates(EntryBlock, X)) {   // X is reachable from entry block?
74044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        // Check to see if this block already belongs to a loop.  If this occurs
741131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner        // then we have a case where a loop that is supposed to be a child of
742131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner        // the current loop was processed before the current loop.  When this
743131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner        // occurs, this child loop gets added to a part of the current loop,
744131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner        // making it a sibling to the current loop.  We have to reparent this
745131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner        // loop.
746c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman        if (LoopT *SubLoop =
747c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman            const_cast<LoopT *>(getLoopFor(X)))
748131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner          if (SubLoop->getHeader() == X && isNotAlreadyContainedIn(SubLoop, L)){
74944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            // Remove the subloop from it's current parent...
75044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            assert(SubLoop->ParentLoop && SubLoop->ParentLoop != L);
751c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman            LoopT *SLP = SubLoop->ParentLoop;  // SubLoopParent
752c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman            typename std::vector<LoopT *>::iterator I =
75344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              std::find(SLP->SubLoops.begin(), SLP->SubLoops.end(), SubLoop);
754131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner            assert(I != SLP->SubLoops.end() &&"SubLoop not a child of parent?");
75544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            SLP->SubLoops.erase(I);   // Remove from parent...
75644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
75744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            // Add the subloop to THIS loop...
75844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            SubLoop->ParentLoop = L;
75944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            L->SubLoops.push_back(SubLoop);
76044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          }
76144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
76244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        // Normal case, add the block to our loop...
76344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        L->Blocks.push_back(X);
764d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson
765d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson        typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
766d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson
76744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        // Add all of the predecessors of X to the end of the work stack...
768d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson        TodoStack.insert(TodoStack.end(), InvBlockTraits::child_begin(X),
769d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson                         InvBlockTraits::child_end(X));
77044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      }
77144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    }
77244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
77344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // If there are any loops nested within this loop, create them now!
77444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(),
77544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson         E = L->Blocks.end(); I != E; ++I)
776c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman      if (LoopT *NewLoop = ConsiderForLoop(*I, DT)) {
77744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        L->SubLoops.push_back(NewLoop);
77844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        NewLoop->ParentLoop = L;
77944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      }
78044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
78144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // Add the basic blocks that comprise this loop to the BBMap so that this
78244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // loop can be found for them.
78344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    //
78444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(),
78544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson           E = L->Blocks.end(); I != E; ++I) {
786c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman      typename std::map<BlockT*, LoopT *>::iterator BBMI = BBMap.find(*I);
787c418bf3dd593b5b2fe2f978930f6d0d6b17e344eDan Gohman      if (BBMI == BBMap.end())                       // Not in map yet...
78844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        BBMap.insert(BBMI, std::make_pair(*I, L));   // Must be at this level
78944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    }
79044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
79144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // Now that we have a list of all of the child loops of this loop, check to
792131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner    // see if any of them should actually be nested inside of each other.  We
793131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner    // can accidentally pull loops our of their parents, so we must make sure to
79444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // organize the loop nests correctly now.
79544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    {
796c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman      std::map<BlockT *, LoopT *> ContainingLoops;
79744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      for (unsigned i = 0; i != L->SubLoops.size(); ++i) {
798c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman        LoopT *Child = L->SubLoops[i];
79944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        assert(Child->getParentLoop() == L && "Not proper child loop?");
80044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
801c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman        if (LoopT *ContainingLoop = ContainingLoops[Child->getHeader()]) {
80244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          // If there is already a loop which contains this loop, move this loop
80344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          // into the containing loop.
80444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          MoveSiblingLoopInto(Child, ContainingLoop);
80544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          --i;  // The loop got removed from the SubLoops list.
80644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        } else {
807131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner          // This is currently considered to be a top-level loop.  Check to see
808131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner          // if any of the contained blocks are loop headers for subloops we
809131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner          // have already processed.
81044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          for (unsigned b = 0, e = Child->Blocks.size(); b != e; ++b) {
811c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman            LoopT *&BlockLoop = ContainingLoops[Child->Blocks[b]];
81244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            if (BlockLoop == 0) {   // Child block not processed yet...
81344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              BlockLoop = Child;
81444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            } else if (BlockLoop != Child) {
815c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman              LoopT *SubLoop = BlockLoop;
81644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              // Reparent all of the blocks which used to belong to BlockLoops
81744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              for (unsigned j = 0, e = SubLoop->Blocks.size(); j != e; ++j)
81844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson                ContainingLoops[SubLoop->Blocks[j]] = Child;
81944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
82044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              // There is already a loop which contains this block, that means
82144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              // that we should reparent the loop which the block is currently
82244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              // considered to belong to to be a child of this loop.
82344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              MoveSiblingLoopInto(SubLoop, Child);
82444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              --i;  // We just shrunk the SubLoops list.
82544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            }
82644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          }
82744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        }
82844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      }
82944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    }
83044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
83144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    return L;
83244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
83344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
834131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  /// MoveSiblingLoopInto - This method moves the NewChild loop to live inside
835131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  /// of the NewParent Loop, instead of being a sibling of it.
836c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void MoveSiblingLoopInto(LoopT *NewChild,
837c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                           LoopT *NewParent) {
838c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *OldParent = NewChild->getParentLoop();
83944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(OldParent && OldParent == NewParent->getParentLoop() &&
84044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson           NewChild != NewParent && "Not sibling loops!");
84144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
84244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // Remove NewChild from being a child of OldParent
843c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    typename std::vector<LoopT *>::iterator I =
844131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner      std::find(OldParent->SubLoops.begin(), OldParent->SubLoops.end(),
845131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner                NewChild);
84644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(I != OldParent->SubLoops.end() && "Parent fields incorrect??");
84744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    OldParent->SubLoops.erase(I);   // Remove from parent's subloops list
84844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    NewChild->ParentLoop = 0;
84944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
85044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    InsertLoopInto(NewChild, NewParent);
85144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
85244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
853131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  /// InsertLoopInto - This inserts loop L into the specified parent loop.  If
854131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  /// the parent loop contains a loop which should contain L, the loop gets
855131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  /// inserted into L instead.
856c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void InsertLoopInto(LoopT *L, LoopT *Parent) {
85744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    BlockT *LHeader = L->getHeader();
858131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner    assert(Parent->contains(LHeader) &&
859131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner           "This loop should not be inserted here!");
86044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
86144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // Check to see if it belongs in a child loop...
86234cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng    for (unsigned i = 0, e = static_cast<unsigned>(Parent->SubLoops.size());
86334cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng         i != e; ++i)
86444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      if (Parent->SubLoops[i]->contains(LHeader)) {
86544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        InsertLoopInto(L, Parent->SubLoops[i]);
86644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        return;
86744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      }
86844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
86944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // If not, insert it here!
87044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    Parent->SubLoops.push_back(L);
87144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    L->ParentLoop = Parent;
87244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
87344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
87444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  // Debugging
87544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
876791102fb1192ac9483274e54cbc42480c9b1af10Chris Lattner  void print(raw_ostream &OS) const {
87744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    for (unsigned i = 0; i < TopLevelLoops.size(); ++i)
87844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      TopLevelLoops[i]->print(OS);
87944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  #if 0
880c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    for (std::map<BasicBlock*, LoopT*>::const_iterator I = BBMap.begin(),
88144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson           E = BBMap.end(); I != E; ++I)
88244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      OS << "BB '" << I->first->getName() << "' level = "
88344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson         << I->second->getLoopDepth() << "\n";
88444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  #endif
88544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
88644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson};
88744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
88844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonclass LoopInfo : public FunctionPass {
889c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopInfoBase<BasicBlock, Loop> LI;
890c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopBase<BasicBlock, Loop>;
8919d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman
8929d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  void operator=(const LoopInfo &); // do not implement
8939d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  LoopInfo(const LoopInfo &);       // do not implement
89444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonpublic:
89544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  static char ID; // Pass identification, replacement for typeid
89644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
8979d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  LoopInfo() : FunctionPass(&ID) {}
89844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
899c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopInfoBase<BasicBlock, Loop>& getBase() { return LI; }
900d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson
90144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// iterator/begin/end - The interface to the top-level loops in the current
90244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// function.
90344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
904c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  typedef LoopInfoBase<BasicBlock, Loop>::iterator iterator;
9059d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  inline iterator begin() const { return LI.begin(); }
9069d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  inline iterator end() const { return LI.end(); }
9079d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  bool empty() const { return LI.empty(); }
90844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
90944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
91044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// block is in no loop (for example the entry node), null is returned.
91144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
91244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline Loop *getLoopFor(const BasicBlock *BB) const {
9139d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.getLoopFor(BB);
91444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
91544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
91644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// operator[] - same as getLoopFor...
91744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
91844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline const Loop *operator[](const BasicBlock *BB) const {
9199d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.getLoopFor(BB);
92044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
92144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
922ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// getLoopDepth - Return the loop nesting level of the specified block.  A
923ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// depth of 0 means the block is not inside any loop.
92444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
92544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline unsigned getLoopDepth(const BasicBlock *BB) const {
9269d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.getLoopDepth(BB);
92744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
92844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
92944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  // isLoopHeader - True if the block is a loop header node
93044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline bool isLoopHeader(BasicBlock *BB) const {
9319d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.isLoopHeader(BB);
93244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
9330bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
9342b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// runOnFunction - Calculate the natural loop information.
9352b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
9367e70829632f82de15db187845666aaca6e04b792Chris Lattner  virtual bool runOnFunction(Function &F);
937facd752d3afaeca7dee46648f2a2ae209a94e5e9Chris Lattner
9389d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  virtual void releaseMemory() { LI.releaseMemory(); }
9395c7e326585f3a543388ba871c3425f7664cd9143Bill Wendling
94045cfe545ec8177262dabc70580ce05feaa1c3880Chris Lattner  virtual void print(raw_ostream &O, const Module* M = 0) const;
941791102fb1192ac9483274e54cbc42480c9b1af10Chris Lattner
942f57b845547302d24ecb6a9e79d7bc386f761a6c9Chris Lattner  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
943f57b845547302d24ecb6a9e79d7bc386f761a6c9Chris Lattner
9444e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  /// removeLoop - This removes the specified top-level loop from this loop info
9454e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  /// object.  The loop is not deleted, as it will presumably be inserted into
9464e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  /// another loop.
9479d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  inline Loop *removeLoop(iterator I) { return LI.removeLoop(I); }
9484e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner
94946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// changeLoopFor - Change the top-level loop that contains BB to the
95046758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// specified loop.  This should be used by transformations that restructure
95146758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// the loop hierarchy tree.
95244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline void changeLoopFor(BasicBlock *BB, Loop *L) {
9539d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.changeLoopFor(BB, L);
95444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
95546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
95646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// changeTopLevelLoop - Replace the specified loop in the top-level loops
95746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// list with the indicated loop.
95844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop) {
9599d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.changeTopLevelLoop(OldLoop, NewLoop);
96044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
96146758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
962072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  /// addTopLevelLoop - This adds the specified loop to the collection of
963072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  /// top-level loops.
96444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline void addTopLevelLoop(Loop *New) {
9659d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.addTopLevelLoop(New);
966072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  }
967072b163424491c85df6664a4e056aae5e07dc64dChris Lattner
9689afb24bf0847b9f2ff0bf3f7f7405dcbe42fa38bChris Lattner  /// removeBlock - This method completely removes BB from all data structures,
9699afb24bf0847b9f2ff0bf3f7f7405dcbe42fa38bChris Lattner  /// including all of the Loop objects it is nested in and our mapping from
9709afb24bf0847b9f2ff0bf3f7f7405dcbe42fa38bChris Lattner  /// BasicBlocks to loops.
97144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  void removeBlock(BasicBlock *BB) {
9729d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.removeBlock(BB);
97344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
974c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman
975c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  static bool isNotAlreadyContainedIn(const Loop *SubLoop,
976c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                                      const Loop *ParentLoop) {
977c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    return
978c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman      LoopInfoBase<BasicBlock, Loop>::isNotAlreadyContainedIn(SubLoop,
979c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                                                              ParentLoop);
980c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  }
9810bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner};
9820bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
983e0b6b78e095f7dea9589e8df5ec4521e346ad005Anand Shukla
9841db0a400370466e187ae06c96a1586c2c21409ddChris Lattner// Allow clients to walk the list of nested loops...
9851db0a400370466e187ae06c96a1586c2c21409ddChris Lattnertemplate <> struct GraphTraits<const Loop*> {
9861db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  typedef const Loop NodeType;
9879d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  typedef LoopInfo::iterator ChildIteratorType;
9881db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
9891db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  static NodeType *getEntryNode(const Loop *L) { return L; }
9909769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_begin(NodeType *N) {
991329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->begin();
9921db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
9939769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_end(NodeType *N) {
994329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->end();
9951db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
9961db0a400370466e187ae06c96a1586c2c21409ddChris Lattner};
9971db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
9981db0a400370466e187ae06c96a1586c2c21409ddChris Lattnertemplate <> struct GraphTraits<Loop*> {
9991db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  typedef Loop NodeType;
10009d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  typedef LoopInfo::iterator ChildIteratorType;
10011db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
10021db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  static NodeType *getEntryNode(Loop *L) { return L; }
10039769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_begin(NodeType *N) {
1004329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->begin();
10051db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
10069769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_end(NodeType *N) {
1007329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->end();
10081db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
10091db0a400370466e187ae06c96a1586c2c21409ddChris Lattner};
10101db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
1011c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class BlockT, class LoopT>
1012c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanvoid
1013c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan GohmanLoopBase<BlockT, LoopT>::addBasicBlockToLoop(BlockT *NewBB,
1014c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                                             LoopInfoBase<BlockT, LoopT> &LIB) {
1015d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson  assert((Blocks.empty() || LIB[getHeader()] == this) &&
1016019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson         "Incorrect LI specified for this loop!");
1017019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  assert(NewBB && "Cannot add a null basic block to the loop!");
1018d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson  assert(LIB[NewBB] == 0 && "BasicBlock already in the loop!");
101944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
1020c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *L = static_cast<LoopT *>(this);
1021c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman
1022019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  // Add the loop mapping to the LoopInfo object...
1023c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LIB.BBMap[NewBB] = L;
1024019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
1025019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  // Add the basic block to this loop and all parent loops...
1026019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  while (L) {
1027019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    L->Blocks.push_back(NewBB);
1028019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    L = L->getParentLoop();
1029019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1030019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson}
1031019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
1032d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
1033d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
10340bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner#endif
1035