LoopInfo.h revision adc799112dc180b3cd099038c05101b85d217716
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
11b7532e254810aba26da9b65ee2c65788695f8c30Dan Gohman// and determine the loop depth of various nodes of the CFG.  A natural loop
12b7532e254810aba26da9b65ee2c65788695f8c30Dan Gohman// has exactly one entry-point, which is called the header. Note that natural
1346758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner// loops may actually be several loops that share the same header node.
14fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner//
15fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner// This analysis calculates the nesting structure of loops in a function.  For
16fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner// each natural loop identified, this analysis identifies natural loops
17072b163424491c85df6664a4e056aae5e07dc64dChris Lattner// contained entirely within the loop and the basic blocks the make up the loop.
18fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner//
19072b163424491c85df6664a4e056aae5e07dc64dChris Lattner// It can calculate on the fly various bits of information, for example:
20072b163424491c85df6664a4e056aae5e07dc64dChris Lattner//
21072b163424491c85df6664a4e056aae5e07dc64dChris Lattner//  * whether there is a preheader for the loop
22072b163424491c85df6664a4e056aae5e07dc64dChris Lattner//  * the number of back edges to the header
23072b163424491c85df6664a4e056aae5e07dc64dChris Lattner//  * whether or not a particular block branches out of the loop
24072b163424491c85df6664a4e056aae5e07dc64dChris Lattner//  * the successor blocks of the loop
25072b163424491c85df6664a4e056aae5e07dc64dChris Lattner//  * the loop depth
26072b163424491c85df6664a4e056aae5e07dc64dChris Lattner//  * the trip count
27072b163424491c85df6664a4e056aae5e07dc64dChris Lattner//  * etc...
280bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//
290bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//===----------------------------------------------------------------------===//
300bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
310bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner#ifndef LLVM_ANALYSIS_LOOP_INFO_H
320bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner#define LLVM_ANALYSIS_LOOP_INFO_H
330bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
34facd752d3afaeca7dee46648f2a2ae209a94e5e9Chris Lattner#include "llvm/Pass.h"
3544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson#include "llvm/ADT/DepthFirstIterator.h"
36551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/GraphTraits.h"
37b7211a2ce13a0365e0e1dd2f27adda2ee3d1288bDevang Patel#include "llvm/ADT/SmallVector.h"
3892583187306cec18d73db362681ea4f8e2b41909Lang Hames#include "llvm/ADT/STLExtras.h"
3944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson#include "llvm/Analysis/Dominators.h"
40019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#include "llvm/Support/CFG.h"
41103289e9383ad1eb66caf28c9b166aebce963a35Chris Lattner#include "llvm/Support/raw_ostream.h"
42019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#include <algorithm>
43019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
441d5562f72e3f7d9e17a2bf95afa54a98dac95894Dan Gohmannamespace llvm {
451d5562f72e3f7d9e17a2bf95afa54a98dac95894Dan Gohman
46019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Andersontemplate<typename T>
47019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Andersonstatic void RemoveFromVector(std::vector<T*> &V, T *N) {
48019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N);
49019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  assert(I != V.end() && "N is not in this list!");
50019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  V.erase(I);
51019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson}
520bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
5353c279b1949f7fa626ccbc399ebbe2d7dc9599a4Devang Patelclass DominatorTree;
548fc2f2072de83665ae20e06929e28317f449bcdfChris Lattnerclass LoopInfo;
55c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanclass Loop;
56c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class N, class M> class LoopInfoBase;
57c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class N, class M> class LoopBase;
580bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
590bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//===----------------------------------------------------------------------===//
60131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner/// LoopBase class - Instances of this class are used to represent loops that
61131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner/// are detected in the flow graph
622b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner///
63c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class BlockT, class LoopT>
64019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Andersonclass LoopBase {
65c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *ParentLoop;
66131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  // SubLoops - Loops contained entirely within this one.
67c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  std::vector<LoopT *> SubLoops;
68131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner
69131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  // Blocks - The list of blocks in this loop.  First entry is the header node.
70131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  std::vector<BlockT*> Blocks;
71019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
72c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  // DO NOT IMPLEMENT
73c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopBase(const LoopBase<BlockT, LoopT> &);
74c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  // DO NOT IMPLEMENT
75c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  const LoopBase<BlockT, LoopT>&operator=(const LoopBase<BlockT, LoopT> &);
760bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattnerpublic:
7746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// Loop ctor - This creates an empty loop.
78019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  LoopBase() : ParentLoop(0) {}
79019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  ~LoopBase() {
8034cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng    for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
814e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner      delete SubLoops[i];
824e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  }
830bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
84ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// getLoopDepth - Return the nesting level of this loop.  An outer-most
85ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// loop has depth 1, for consistency with loop depth values used for basic
86ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// blocks, where depth 0 is used for blocks not inside any loops.
87072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  unsigned getLoopDepth() const {
88ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman    unsigned D = 1;
89c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    for (const LoopT *CurLoop = ParentLoop; CurLoop;
90019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson         CurLoop = CurLoop->ParentLoop)
91072b163424491c85df6664a4e056aae5e07dc64dChris Lattner      ++D;
92072b163424491c85df6664a4e056aae5e07dc64dChris Lattner    return D;
93072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  }
94019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  BlockT *getHeader() const { return Blocks.front(); }
95c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *getParentLoop() const { return ParentLoop; }
960bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
9792329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  /// contains - Return true if the specified loop is contained within in
9892329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  /// this loop.
9992329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  ///
10092329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  bool contains(const LoopT *L) const {
10192329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman    if (L == this) return true;
10292329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman    if (L == 0)    return false;
10392329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman    return contains(L->getParentLoop());
10492329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  }
10592329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman
10692329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  /// contains - Return true if the specified basic block is in this loop.
107fbdf4bf1799bf9ea566fc0fc0507752590a6d559Misha Brukman  ///
108019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  bool contains(const BlockT *BB) const {
109f3ab3a937203d690744357844948faaf96fb73f9Dan Gohman    return std::find(block_begin(), block_end(), BB) != block_end();
110019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1110bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
11292329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  /// contains - Return true if the specified instruction is in this loop.
11392329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  ///
11492329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  template<class InstT>
11592329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  bool contains(const InstT *Inst) const {
11692329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman    return contains(Inst->getParent());
11792329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  }
11892329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman
119329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  /// iterator/begin/end - Return the loops contained entirely within this loop.
1202b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
121c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  const std::vector<LoopT *> &getSubLoops() const { return SubLoops; }
122c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  typedef typename std::vector<LoopT *>::const_iterator iterator;
123329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator begin() const { return SubLoops.begin(); }
124329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator end() const { return SubLoops.end(); }
12521c276d2fa99914d5ed958ac0aec7d78e3dd87cfDan Gohman  bool empty() const { return SubLoops.empty(); }
126fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner
127fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// getBlocks - Get a list of the basic blocks which make up this loop.
128fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  ///
129019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  const std::vector<BlockT*> &getBlocks() const { return Blocks; }
130019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  typedef typename std::vector<BlockT*>::const_iterator block_iterator;
1314e77cc46ac688e7bee98747049f90e19e2902227Chris Lattner  block_iterator block_begin() const { return Blocks.begin(); }
1324e77cc46ac688e7bee98747049f90e19e2902227Chris Lattner  block_iterator block_end() const { return Blocks.end(); }
133fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner
134d146e986c818165cca866ee05751451706ccf36aDan Gohman  /// isLoopExiting - True if terminator in the block can branch to another
135d146e986c818165cca866ee05751451706ccf36aDan Gohman  /// block that is outside of the current loop.
136fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  ///
13732663b719b4996b3a735f22bba80d771d50f96e7Dan Gohman  bool isLoopExiting(const BlockT *BB) const {
138d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<BlockT*> BlockTraits;
139d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    for (typename BlockTraits::ChildIteratorType SI =
140d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         BlockTraits::child_begin(const_cast<BlockT*>(BB)),
141d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         SE = BlockTraits::child_end(const_cast<BlockT*>(BB)); SI != SE; ++SI) {
142019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      if (!contains(*SI))
143019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        return true;
144019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    }
145019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return false;
146019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1476b290a54409f6bb6a0cc1c0446cd2b170a4b7addMisha Brukman
148fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// getNumBackEdges - Calculate the number of back edges to the loop header
149fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  ///
150019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  unsigned getNumBackEdges() const {
151019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    unsigned NumBackEdges = 0;
152019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    BlockT *H = getHeader();
153019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
154d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
155d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    for (typename InvBlockTraits::ChildIteratorType I =
156d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         InvBlockTraits::child_begin(const_cast<BlockT*>(H)),
157d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         E = InvBlockTraits::child_end(const_cast<BlockT*>(H)); I != E; ++I)
158019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      if (contains(*I))
159019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        ++NumBackEdges;
160019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
161019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return NumBackEdges;
162019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1639474dd68e8a050193ca4003940ac399e2b17cb6aChris Lattner
164e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //===--------------------------------------------------------------------===//
165e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // APIs for simple analysis of the loop.
166e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //
167e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // Note that all of these methods can fail on general loops (ie, there may not
168e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // be a preheader, etc).  For best success, the loop simplification and
169e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // induction variable canonicalization pass should be used to normalize loops
170e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // for easy analysis.  These methods assume canonical loops.
171e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner
1727466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  /// getExitingBlocks - Return all blocks inside the loop that have successors
1737466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  /// outside of the loop.  These are the blocks _inside of the current loop_
1747466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  /// which branch out.  The returned list is always unique.
1757466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  ///
176019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const {
177019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // Sort the blocks vector so that we can use binary search to do quick
178019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // lookups.
179019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
180019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    std::sort(LoopBBs.begin(), LoopBBs.end());
181019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
182d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<BlockT*> BlockTraits;
183f3ab3a937203d690744357844948faaf96fb73f9Dan Gohman    for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
184d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson      for (typename BlockTraits::ChildIteratorType I =
185d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson          BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
186d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson          I != E; ++I)
187019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) {
188019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          // Not in current loop? It must be an exit block.
189019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          ExitingBlocks.push_back(*BI);
190019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          break;
191019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        }
192019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1937466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner
194c83324682f3409c15dad992cd62928426c9ad83dDan Gohman  /// getExitingBlock - If getExitingBlocks would return exactly one block,
195c83324682f3409c15dad992cd62928426c9ad83dDan Gohman  /// return that block. Otherwise return null.
196c83324682f3409c15dad992cd62928426c9ad83dDan Gohman  BlockT *getExitingBlock() const {
197c83324682f3409c15dad992cd62928426c9ad83dDan Gohman    SmallVector<BlockT*, 8> ExitingBlocks;
198c83324682f3409c15dad992cd62928426c9ad83dDan Gohman    getExitingBlocks(ExitingBlocks);
199c83324682f3409c15dad992cd62928426c9ad83dDan Gohman    if (ExitingBlocks.size() == 1)
200c83324682f3409c15dad992cd62928426c9ad83dDan Gohman      return ExitingBlocks[0];
201c83324682f3409c15dad992cd62928426c9ad83dDan Gohman    return 0;
202c83324682f3409c15dad992cd62928426c9ad83dDan Gohman  }
203c83324682f3409c15dad992cd62928426c9ad83dDan Gohman
204f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// getExitBlocks - Return all of the successor blocks of this loop.  These
205f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// are the blocks _outside of the current loop_ which are branched to.
206f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  ///
207019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
208019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // Sort the blocks vector so that we can use binary search to do quick
209019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // lookups.
210019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
211019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    std::sort(LoopBBs.begin(), LoopBBs.end());
212019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
213d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<BlockT*> BlockTraits;
214f3ab3a937203d690744357844948faaf96fb73f9Dan Gohman    for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
215d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson      for (typename BlockTraits::ChildIteratorType I =
216d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson           BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
217d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson           I != E; ++I)
218019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
219019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          // Not in current loop? It must be an exit block.
220019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          ExitBlocks.push_back(*I);
221019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
222f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner
2231827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman  /// getExitBlock - If getExitBlocks would return exactly one block,
2241827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman  /// return that block. Otherwise return null.
2251827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman  BlockT *getExitBlock() const {
2261827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman    SmallVector<BlockT*, 8> ExitBlocks;
2271827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman    getExitBlocks(ExitBlocks);
2281827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman    if (ExitBlocks.size() == 1)
2291827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman      return ExitBlocks[0];
2301827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman    return 0;
2311827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman  }
2321827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman
23360f422f894ae9aff2f508f34733be36f5a0ed20aLang Hames  /// Edge type.
23460f422f894ae9aff2f508f34733be36f5a0ed20aLang Hames  typedef std::pair<BlockT*, BlockT*> Edge;
23560f422f894ae9aff2f508f34733be36f5a0ed20aLang Hames
23655e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar  /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
23760f422f894ae9aff2f508f34733be36f5a0ed20aLang Hames  template <typename EdgeT>
23860f422f894ae9aff2f508f34733be36f5a0ed20aLang Hames  void getExitEdges(SmallVectorImpl<EdgeT> &ExitEdges) const {
23955e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar    // Sort the blocks vector so that we can use binary search to do quick
24055e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar    // lookups.
24155e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar    SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
24292583187306cec18d73db362681ea4f8e2b41909Lang Hames    array_pod_sort(LoopBBs.begin(), LoopBBs.end());
24355e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar
24455e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar    typedef GraphTraits<BlockT*> BlockTraits;
24555e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar    for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
24655e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar      for (typename BlockTraits::ChildIteratorType I =
24755e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar           BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
24855e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar           I != E; ++I)
24955e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar        if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
25055e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar          // Not in current loop? It must be an exit block.
25160f422f894ae9aff2f508f34733be36f5a0ed20aLang Hames          ExitEdges.push_back(EdgeT(*BI, *I));
25255e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar  }
25355e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar
2542b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// getLoopPreheader - If there is a preheader for this loop, return it.  A
2552b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// loop has a preheader if there is only one edge to the header of the loop
2562b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// from outside of the loop.  If this is the case, the block branching to the
257e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  /// header of the loop is the preheader node.
2582b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
259e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  /// This method returns null if there is no preheader for the loop.
2602b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
261019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  BlockT *getLoopPreheader() const {
262019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // Keep track of nodes outside the loop branching to the header...
2635cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman    BlockT *Out = getLoopPredecessor();
2645cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman    if (!Out) return 0;
2655cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman
2665cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman    // Make sure there is only one exit out of the preheader.
2675cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman    typedef GraphTraits<BlockT*> BlockTraits;
2685cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman    typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
2695cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman    ++SI;
2705cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman    if (SI != BlockTraits::child_end(Out))
2715cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman      return 0;  // Multiple exits from the block, must not be a preheader.
2725cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman
2735cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman    // The predecessor has exactly one successor, so it is a preheader.
2745cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman    return Out;
2755cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman  }
2765cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman
2775cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman  /// getLoopPredecessor - If the given loop's header has exactly one unique
2785cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman  /// predecessor outside the loop, return it. Otherwise return null.
2795cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman  /// This is less strict that the loop "preheader" concept, which requires
2805cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman  /// the predecessor to have exactly one successor.
2815cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman  ///
2825cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman  BlockT *getLoopPredecessor() const {
2835cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman    // Keep track of nodes outside the loop branching to the header...
284019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    BlockT *Out = 0;
285019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
286019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // Loop over the predecessors of the header node...
287019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    BlockT *Header = getHeader();
288d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<BlockT*> BlockTraits;
289d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
290d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    for (typename InvBlockTraits::ChildIteratorType PI =
291d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         InvBlockTraits::child_begin(Header),
2924fd6b397031d7b5baa01849c065ad7ef70882d83Gabor Greif         PE = InvBlockTraits::child_end(Header); PI != PE; ++PI) {
2934fd6b397031d7b5baa01849c065ad7ef70882d83Gabor Greif      typename InvBlockTraits::NodeType *N = *PI;
2944fd6b397031d7b5baa01849c065ad7ef70882d83Gabor Greif      if (!contains(N)) {     // If the block is not in the loop...
2954fd6b397031d7b5baa01849c065ad7ef70882d83Gabor Greif        if (Out && Out != N)
296019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson          return 0;             // Multiple predecessors outside the loop
2974fd6b397031d7b5baa01849c065ad7ef70882d83Gabor Greif        Out = N;
298019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      }
2994fd6b397031d7b5baa01849c065ad7ef70882d83Gabor Greif    }
300019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
301019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    // Make sure there is only one exit out of the preheader.
302019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(Out && "Header of loop has no predecessors from outside loop?");
303019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return Out;
304019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
3052b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner
306b317143ba851ce2853fb262fb2185ef5f1be030dDan Gohman  /// getLoopLatch - If there is a single latch block for this loop, return it.
307b317143ba851ce2853fb262fb2185ef5f1be030dDan Gohman  /// A latch block is a block that contains a branch back to the header.
308019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  BlockT *getLoopLatch() const {
309019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    BlockT *Header = getHeader();
310d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
311d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typename InvBlockTraits::ChildIteratorType PI =
312d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson                                            InvBlockTraits::child_begin(Header);
313d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typename InvBlockTraits::ChildIteratorType PE =
314d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson                                              InvBlockTraits::child_end(Header);
315019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    BlockT *Latch = 0;
3168fe5ccc5850af3248992d182c5b7c079b50120fcGabor Greif    for (; PI != PE; ++PI) {
3178fe5ccc5850af3248992d182c5b7c079b50120fcGabor Greif      typename InvBlockTraits::NodeType *N = *PI;
3188fe5ccc5850af3248992d182c5b7c079b50120fcGabor Greif      if (contains(N)) {
319a58a04921deba911d6ead8d24f495cec234681c1Dan Gohman        if (Latch) return 0;
3208fe5ccc5850af3248992d182c5b7c079b50120fcGabor Greif        Latch = N;
321a58a04921deba911d6ead8d24f495cec234681c1Dan Gohman      }
3228fe5ccc5850af3248992d182c5b7c079b50120fcGabor Greif    }
323019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
324019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return Latch;
325019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
326e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner
327e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //===--------------------------------------------------------------------===//
328e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // APIs for updating loop information after changing the CFG
329e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //
330e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner
331fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// addBasicBlockToLoop - This method is used by other analyses to update loop
332fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// information.  NewBB is set to be a new member of the current loop.
3332b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// Because of this, it is added as a member of all parent loops, and is added
3342b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// to the specified LoopInfo object as being in the current basic block.  It
3352b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// is not valid to replace the loop header with this method.
3362b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
337c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
3382b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner
33946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// replaceChildLoopWith - This is used when splitting loops up.  It replaces
34046758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// the OldChild entry in our children list with NewChild, and updates the
34146758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// parent pointer of OldChild to be null and the NewChild to be this loop.
34246758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// This updates the loop depth of the new child.
343c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void replaceChildLoopWith(LoopT *OldChild,
344c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                            LoopT *NewChild) {
345019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(OldChild->ParentLoop == this && "This loop is already broken!");
346019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
347c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    typename std::vector<LoopT *>::iterator I =
348019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson                          std::find(SubLoops.begin(), SubLoops.end(), OldChild);
349019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(I != SubLoops.end() && "OldChild not in loop!");
350019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    *I = NewChild;
351019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    OldChild->ParentLoop = 0;
352c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    NewChild->ParentLoop = static_cast<LoopT *>(this);
353019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
35446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
35546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// addChildLoop - Add the specified loop to be a child of this loop.  This
35646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// updates the loop depth of the new child.
35746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  ///
358c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void addChildLoop(LoopT *NewChild) {
359019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
360c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    NewChild->ParentLoop = static_cast<LoopT *>(this);
361019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SubLoops.push_back(NewChild);
362019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
36346758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
36446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// removeChildLoop - This removes the specified child from being a subloop of
36546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// this loop.  The loop is not deleted, as it will presumably be inserted
36646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// into another loop.
367c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *removeChildLoop(iterator I) {
368019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(I != SubLoops.end() && "Cannot remove end iterator!");
369c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *Child = *I;
370019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(Child->ParentLoop == this && "Child is not a child of this loop!");
371019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SubLoops.erase(SubLoops.begin()+(I-begin()));
372019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    Child->ParentLoop = 0;
373019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return Child;
374019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
37546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
37646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// addBlockEntry - This adds a basic block directly to the basic block list.
37746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// This should only be used by transformations that create new loops.  Other
37846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// transformations should use addBasicBlockToLoop.
379019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void addBlockEntry(BlockT *BB) {
38046758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner    Blocks.push_back(BB);
38146758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  }
38246758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
38351a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  /// moveToHeader - This method is used to move BB (which must be part of this
38451a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  /// loop) to be the loop header of the loop (the block that dominates all
38551a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  /// others).
386019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void moveToHeader(BlockT *BB) {
38751a4ad475b095aed49caff176b98c0754e421af4Chris Lattner    if (Blocks[0] == BB) return;
38851a4ad475b095aed49caff176b98c0754e421af4Chris Lattner    for (unsigned i = 0; ; ++i) {
38951a4ad475b095aed49caff176b98c0754e421af4Chris Lattner      assert(i != Blocks.size() && "Loop does not contain BB!");
39051a4ad475b095aed49caff176b98c0754e421af4Chris Lattner      if (Blocks[i] == BB) {
39151a4ad475b095aed49caff176b98c0754e421af4Chris Lattner        Blocks[i] = Blocks[0];
39251a4ad475b095aed49caff176b98c0754e421af4Chris Lattner        Blocks[0] = BB;
39351a4ad475b095aed49caff176b98c0754e421af4Chris Lattner        return;
39451a4ad475b095aed49caff176b98c0754e421af4Chris Lattner      }
39551a4ad475b095aed49caff176b98c0754e421af4Chris Lattner    }
39651a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  }
39751a4ad475b095aed49caff176b98c0754e421af4Chris Lattner
39846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// removeBlockFromLoop - This removes the specified basic block from the
399f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// current loop, updating the Blocks as appropriate.  This does not update
400f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// the mapping in the LoopInfo class.
401019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void removeBlockFromLoop(BlockT *BB) {
402019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    RemoveFromVector(Blocks, BB);
403019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
40446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
40558e0ef1e90c3f6dbae213612b44e56f7d6d65ea7Devang Patel  /// verifyLoop - Verify loop structure
406019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void verifyLoop() const {
407019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#ifndef NDEBUG
4085c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman    assert(!Blocks.empty() && "Loop header is missing");
4095c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman
4105c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman    // Sort the blocks vector so that we can use binary search to do quick
4115c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman    // lookups.
4125c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman    SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
4135c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman    std::sort(LoopBBs.begin(), LoopBBs.end());
4145c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman
4155c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman    // Check the individual blocks.
4165c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman    for (block_iterator I = block_begin(), E = block_end(); I != E; ++I) {
4175c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      BlockT *BB = *I;
4185c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      bool HasInsideLoopSuccs = false;
4195c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      bool HasInsideLoopPreds = false;
4205c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      SmallVector<BlockT *, 2> OutsideLoopPreds;
4215c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman
4225c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      typedef GraphTraits<BlockT*> BlockTraits;
4235c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      for (typename BlockTraits::ChildIteratorType SI =
4245c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman           BlockTraits::child_begin(BB), SE = BlockTraits::child_end(BB);
4255c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman           SI != SE; ++SI)
4265c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman        if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *SI)) {
4275c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman          HasInsideLoopSuccs = true;
4285c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman          break;
4295c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman        }
4305c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
4315c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      for (typename InvBlockTraits::ChildIteratorType PI =
4325c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman           InvBlockTraits::child_begin(BB), PE = InvBlockTraits::child_end(BB);
4335c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman           PI != PE; ++PI) {
4348fe5ccc5850af3248992d182c5b7c079b50120fcGabor Greif        typename InvBlockTraits::NodeType *N = *PI;
4358fe5ccc5850af3248992d182c5b7c079b50120fcGabor Greif        if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), N))
4365c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman          HasInsideLoopPreds = true;
4375c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman        else
4388fe5ccc5850af3248992d182c5b7c079b50120fcGabor Greif          OutsideLoopPreds.push_back(N);
4395c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      }
4405c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman
4415c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      if (BB == getHeader()) {
4425c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman        assert(!OutsideLoopPreds.empty() && "Loop is unreachable!");
4435c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      } else if (!OutsideLoopPreds.empty()) {
4445c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman        // A non-header loop shouldn't be reachable from outside the loop,
4455c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman        // though it is permitted if the predecessor is not itself actually
4465c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman        // reachable.
4475c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman        BlockT *EntryBB = BB->getParent()->begin();
4485c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman        for (df_iterator<BlockT *> NI = df_begin(EntryBB),
4495c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman             NE = df_end(EntryBB); NI != NE; ++NI)
4505c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman          for (unsigned i = 0, e = OutsideLoopPreds.size(); i != e; ++i)
4515c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman            assert(*NI != OutsideLoopPreds[i] &&
4525c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman                   "Loop has multiple entry points!");
4535c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      }
4545c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      assert(HasInsideLoopPreds && "Loop block has no in-loop predecessors!");
4555c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      assert(HasInsideLoopSuccs && "Loop block has no in-loop successors!");
4565c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      assert(BB != getHeader()->getParent()->begin() &&
4575c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman             "Loop contains function entry block!");
4585c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman    }
4595c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman
4605c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman    // Check the subloops.
4615c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman    for (iterator I = begin(), E = end(); I != E; ++I)
4625c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      // Each block in each subloop should be contained within this loop.
4635c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      for (block_iterator BI = (*I)->block_begin(), BE = (*I)->block_end();
4645c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman           BI != BE; ++BI) {
4655c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman        assert(std::binary_search(LoopBBs.begin(), LoopBBs.end(), *BI) &&
4665c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman               "Loop does not contain all the blocks of a subloop!");
4675c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      }
4685c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman
4695c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman    // Check the parent loop pointer.
4705c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman    if (ParentLoop) {
4715c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      assert(std::find(ParentLoop->begin(), ParentLoop->end(), this) !=
4725c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman               ParentLoop->end() &&
4735c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman             "Loop is not a subloop of its parent!");
4745c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman    }
475019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#endif
476019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
477019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
4785c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman  /// verifyLoop - Verify loop structure of this loop and all nested loops.
4795c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman  void verifyLoopNest() const {
4805c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman    // Verify this loop.
4815c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman    verifyLoop();
4825c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman    // Verify the subloops.
4835c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman    for (iterator I = begin(), E = end(); I != E; ++I)
4845c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      (*I)->verifyLoopNest();
4855c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman  }
4865c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman
487791102fb1192ac9483274e54cbc42480c9b1af10Chris Lattner  void print(raw_ostream &OS, unsigned Depth = 0) const {
488791102fb1192ac9483274e54cbc42480c9b1af10Chris Lattner    OS.indent(Depth*2) << "Loop at depth " << getLoopDepth()
489927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman       << " containing: ";
490019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
491019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    for (unsigned i = 0; i < getBlocks().size(); ++i) {
492019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      if (i) OS << ",";
493927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman      BlockT *BB = getBlocks()[i];
494927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman      WriteAsOperand(OS, BB, false);
495927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman      if (BB == getHeader())    OS << "<header>";
496927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman      if (BB == getLoopLatch()) OS << "<latch>";
49732663b719b4996b3a735f22bba80d771d50f96e7Dan Gohman      if (isLoopExiting(BB))    OS << "<exiting>";
498019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    }
499019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    OS << "\n";
50058e0ef1e90c3f6dbae213612b44e56f7d6d65ea7Devang Patel
501019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    for (iterator I = begin(), E = end(); I != E; ++I)
502019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      (*I)->print(OS, Depth+2);
503019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
504dda30cd4af1c5f88fc00fd40b673f8e27c61379dDan Gohman
505c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanprotected:
506c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopInfoBase<BlockT, LoopT>;
5071002c0203450620594a85454c6a095ca94b87cb2Dan Gohman  explicit LoopBase(BlockT *BB) : ParentLoop(0) {
508072b163424491c85df6664a4e056aae5e07dc64dChris Lattner    Blocks.push_back(BB);
5090bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
5100bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner};
5110bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
5126a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesentemplate<class BlockT, class LoopT>
5136a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesenraw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
5146a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesen  Loop.print(OS);
5156a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesen  return OS;
5166a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesen}
5176a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesen
518c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanclass Loop : public LoopBase<BasicBlock, Loop> {
519c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanpublic:
520c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  Loop() {}
52116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
52216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// isLoopInvariant - Return true if the specified value is loop invariant
52316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
52416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  bool isLoopInvariant(Value *V) const;
52516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
526adc799112dc180b3cd099038c05101b85d217716Chris Lattner  /// hasLoopInvariantOperands - Return true if all the operands of the
527adc799112dc180b3cd099038c05101b85d217716Chris Lattner  /// specified instruction are loop invariant.
528adc799112dc180b3cd099038c05101b85d217716Chris Lattner  bool hasLoopInvariantOperands(Instruction *I) const;
529a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman
530206613b289f60b71a76e9190d36b9ea9e47a701eDan Gohman  /// makeLoopInvariant - If the given value is an instruction inside of the
531a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// loop and it can be hoisted, do so to make it trivially loop-invariant.
532a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// Return true if the value after any hoisting is loop invariant. This
533a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// function can be used as a slightly more aggressive replacement for
534a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// isLoopInvariant.
535a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
536a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If InsertPt is specified, it is the point to hoist instructions to.
537a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If null, the terminator of the loop preheader is used.
538a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
539bdc017edacb713119b24ab269d250a82d62fffebDan Gohman  bool makeLoopInvariant(Value *V, bool &Changed,
540bdc017edacb713119b24ab269d250a82d62fffebDan Gohman                         Instruction *InsertPt = 0) const;
541a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman
542a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// makeLoopInvariant - If the given instruction is inside of the
543a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// loop and it can be hoisted, do so to make it trivially loop-invariant.
544a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// Return true if the instruction after any hoisting is loop invariant. This
545a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// function can be used as a slightly more aggressive replacement for
546a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// isLoopInvariant.
547a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
548a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If InsertPt is specified, it is the point to hoist instructions to.
549a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If null, the terminator of the loop preheader is used.
550a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
551bdc017edacb713119b24ab269d250a82d62fffebDan Gohman  bool makeLoopInvariant(Instruction *I, bool &Changed,
552bdc017edacb713119b24ab269d250a82d62fffebDan Gohman                         Instruction *InsertPt = 0) const;
553a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman
55416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// getCanonicalInductionVariable - Check to see if the loop has a canonical
55516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// induction variable: an integer recurrence that starts at 0 and increments
55616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// by one each time through the loop.  If so, return the phi node that
55716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// corresponds to it.
55816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
55916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// The IndVarSimplify pass transforms loops to have a canonical induction
56016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// variable.
56116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
56216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  PHINode *getCanonicalInductionVariable() const;
56316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
56416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// getTripCount - Return a loop-invariant LLVM value indicating the number of
56516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// times the loop will be executed.  Note that this means that the backedge
56616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// of the loop executes N-1 times.  If the trip-count cannot be determined,
56716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// this returns null.
56816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
56916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// The IndVarSimplify pass transforms loops to have a form that this
57016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// function easily understands.
57116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
57216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  Value *getTripCount() const;
57316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
57416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// getSmallConstantTripCount - Returns the trip count of this loop as a
57516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// normal unsigned value, if possible. Returns 0 if the trip count is unknown
57616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// of not constant. Will also return 0 if the trip count is very large
57716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// (>= 2^32)
578b14bc1f6f82c7f650226f39ed0256d8189eb2a1bDan Gohman  ///
579b14bc1f6f82c7f650226f39ed0256d8189eb2a1bDan Gohman  /// The IndVarSimplify pass transforms loops to have a form that this
580b14bc1f6f82c7f650226f39ed0256d8189eb2a1bDan Gohman  /// function easily understands.
581b14bc1f6f82c7f650226f39ed0256d8189eb2a1bDan Gohman  ///
58216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  unsigned getSmallConstantTripCount() const;
58316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
58416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// getSmallConstantTripMultiple - Returns the largest constant divisor of the
58516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// trip count of this loop as a normal unsigned value, if possible. This
58616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// means that the actual trip count is always a multiple of the returned
58716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// value (don't forget the trip count could very well be zero as well!).
58816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
58916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// Returns 1 if the trip count is unknown or not guaranteed to be the
59016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// multiple of a constant (which is also the case if the trip count is simply
59116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// constant, use getSmallConstantTripCount for that case), Will also return 1
59216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// if the trip count is very large (>= 2^32).
59316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  unsigned getSmallConstantTripMultiple() const;
59416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
59516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// isLCSSAForm - Return true if the Loop is in LCSSA form
596bbf81d88116d23fb0776412b5916f7d0b8b3ca7eDan Gohman  bool isLCSSAForm(DominatorTree &DT) const;
59716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
598937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  /// isLoopSimplifyForm - Return true if the Loop is in the form that
599937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  /// the LoopSimplify form transforms loops to, which is sometimes called
600937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  /// normal form.
601937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  bool isLoopSimplifyForm() const;
602937738649386b8188524d0cd61943214a5b93cf6Dan Gohman
603f17e9511f15a0e007ff47d0789d1a52502e8c1fbDan Gohman  /// hasDedicatedExits - Return true if no exit block for the loop
604f17e9511f15a0e007ff47d0789d1a52502e8c1fbDan Gohman  /// has a predecessor that is outside the loop.
605f17e9511f15a0e007ff47d0789d1a52502e8c1fbDan Gohman  bool hasDedicatedExits() const;
606f17e9511f15a0e007ff47d0789d1a52502e8c1fbDan Gohman
607f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
608f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  /// These are the blocks _outside of the current loop_ which are branched to.
609050959cd08db6c0efb8208271a1d64ce58893e20Dan Gohman  /// This assumes that loop exits are in canonical form.
610f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  ///
611f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  void getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const;
612f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman
613f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
614f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  /// block, return that block. Otherwise return null.
615f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  BasicBlock *getUniqueExitBlock() const;
616f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman
617dda30cd4af1c5f88fc00fd40b673f8e27c61379dDan Gohman  void dump() const;
618dda30cd4af1c5f88fc00fd40b673f8e27c61379dDan Gohman
619c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanprivate:
620c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopInfoBase<BasicBlock, Loop>;
621c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
622c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman};
6230bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
6240bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//===----------------------------------------------------------------------===//
6252b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner/// LoopInfo - This class builds and contains all of the top level loop
6262b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner/// structures in the specified function.
6272b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner///
62844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
629c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class BlockT, class LoopT>
63044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonclass LoopInfoBase {
6310bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  // BBMap - Mapping of basic blocks to the inner most loop they occur in
632c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  std::map<BlockT *, LoopT *> BBMap;
633c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  std::vector<LoopT *> TopLevelLoops;
634c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopBase<BlockT, LoopT>;
6359d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman
6369d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  void operator=(const LoopInfoBase &); // do not implement
6379d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  LoopInfoBase(const LoopInfo &);       // do not implement
6380bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattnerpublic:
63944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  LoopInfoBase() { }
64044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ~LoopInfoBase() { releaseMemory(); }
64144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
64244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  void releaseMemory() {
643c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    for (typename std::vector<LoopT *>::iterator I =
64444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson         TopLevelLoops.begin(), E = TopLevelLoops.end(); I != E; ++I)
64544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      delete *I;   // Delete all of the loops...
6460bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
647131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner    BBMap.clear();                           // Reset internal state of analysis
64844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    TopLevelLoops.clear();
64944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
65044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
651329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  /// iterator/begin/end - The interface to the top-level loops in the current
652329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  /// function.
653329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  ///
654c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  typedef typename std::vector<LoopT *>::const_iterator iterator;
655329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator begin() const { return TopLevelLoops.begin(); }
656329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator end() const { return TopLevelLoops.end(); }
657a8c763b3071ae1a58ee8baeb282331245527e004Dan Gohman  bool empty() const { return TopLevelLoops.empty(); }
65844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
6592b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
6602b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// block is in no loop (for example the entry node), null is returned.
6612b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
662c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *getLoopFor(const BlockT *BB) const {
663c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    typename std::map<BlockT *, LoopT *>::const_iterator I=
664d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson      BBMap.find(const_cast<BlockT*>(BB));
6650bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner    return I != BBMap.end() ? I->second : 0;
6660bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
66744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
6682b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// operator[] - same as getLoopFor...
6692b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
670c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  const LoopT *operator[](const BlockT *BB) const {
6710bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner    return getLoopFor(BB);
6720bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
67344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
674ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// getLoopDepth - Return the loop nesting level of the specified block.  A
675ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// depth of 0 means the block is not inside any loop.
6762b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
67744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  unsigned getLoopDepth(const BlockT *BB) const {
678c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    const LoopT *L = getLoopFor(BB);
6790bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner    return L ? L->getLoopDepth() : 0;
6800bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
6810bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
6820bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  // isLoopHeader - True if the block is a loop header node
68344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  bool isLoopHeader(BlockT *BB) const {
684c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    const LoopT *L = getLoopFor(BB);
68550f5490842d501e269a4c6085d0d132cae0d31f8Chris Lattner    return L && L->getHeader() == BB;
6860bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
68744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
68844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// removeLoop - This removes the specified top-level loop from this loop info
68944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// object.  The loop is not deleted, as it will presumably be inserted into
69044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// another loop.
691c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *removeLoop(iterator I) {
69244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(I != end() && "Cannot remove end iterator!");
693c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *L = *I;
69444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(L->getParentLoop() == 0 && "Not a top-level loop!");
69544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
69644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    return L;
69744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
69844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
69944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// changeLoopFor - Change the top-level loop that contains BB to the
70044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// specified loop.  This should be used by transformations that restructure
70144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// the loop hierarchy tree.
702c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void changeLoopFor(BlockT *BB, LoopT *L) {
703c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *&OldLoop = BBMap[BB];
70444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(OldLoop && "Block not in a loop yet!");
70544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    OldLoop = L;
70644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
70744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
70844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// changeTopLevelLoop - Replace the specified loop in the top-level loops
70944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// list with the indicated loop.
710c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void changeTopLevelLoop(LoopT *OldLoop,
711c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                          LoopT *NewLoop) {
712c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    typename std::vector<LoopT *>::iterator I =
71344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson                 std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
71444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(I != TopLevelLoops.end() && "Old loop not at top level!");
71544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    *I = NewLoop;
71644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(NewLoop->ParentLoop == 0 && OldLoop->ParentLoop == 0 &&
71744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson           "Loops already embedded into a subloop!");
71844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
71944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
72044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// addTopLevelLoop - This adds the specified loop to the collection of
72144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// top-level loops.
722c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void addTopLevelLoop(LoopT *New) {
72344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(New->getParentLoop() == 0 && "Loop already in subloop!");
72444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    TopLevelLoops.push_back(New);
72544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
72644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
72744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// removeBlock - This method completely removes BB from all data structures,
72844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// including all of the Loop objects it is nested in and our mapping from
72944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// BasicBlocks to loops.
73044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  void removeBlock(BlockT *BB) {
731c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    typename std::map<BlockT *, LoopT *>::iterator I = BBMap.find(BB);
73244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (I != BBMap.end()) {
733c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman      for (LoopT *L = I->second; L; L = L->getParentLoop())
73444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        L->removeBlockFromLoop(BB);
73544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
73644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      BBMap.erase(I);
73744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    }
73844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
73944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
74044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  // Internals
74144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
742c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
743c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                                      const LoopT *ParentLoop) {
74444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (SubLoop == 0) return true;
74544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (SubLoop == ParentLoop) return false;
74644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
74744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
74844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
749d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson  void Calculate(DominatorTreeBase<BlockT> &DT) {
75044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    BlockT *RootNode = DT.getRootNode()->getBlock();
75144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
75244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    for (df_iterator<BlockT*> NI = df_begin(RootNode),
75344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson           NE = df_end(RootNode); NI != NE; ++NI)
754c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman      if (LoopT *L = ConsiderForLoop(*NI, DT))
75544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        TopLevelLoops.push_back(L);
75644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
75744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
758c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *ConsiderForLoop(BlockT *BB, DominatorTreeBase<BlockT> &DT) {
75944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (BBMap.find(BB) != BBMap.end()) return 0;// Haven't processed this node?
76044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
76144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    std::vector<BlockT *> TodoStack;
76244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
76344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // Scan the predecessors of BB, checking to see if BB dominates any of
76444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // them.  This identifies backedges which target this node...
765d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
766d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    for (typename InvBlockTraits::ChildIteratorType I =
767d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         InvBlockTraits::child_begin(BB), E = InvBlockTraits::child_end(BB);
768f6c166078f8f10006dcbdde081603eccccfcefbeGabor Greif         I != E; ++I) {
769f6c166078f8f10006dcbdde081603eccccfcefbeGabor Greif      typename InvBlockTraits::NodeType *N = *I;
770f6c166078f8f10006dcbdde081603eccccfcefbeGabor Greif      if (DT.dominates(BB, N))   // If BB dominates its predecessor...
771f6c166078f8f10006dcbdde081603eccccfcefbeGabor Greif          TodoStack.push_back(N);
772f6c166078f8f10006dcbdde081603eccccfcefbeGabor Greif    }
77344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
77444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (TodoStack.empty()) return 0;  // No backedges to this block...
77544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
77644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // Create a new loop to represent this basic block...
777c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *L = new LoopT(BB);
77844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    BBMap[BB] = L;
77944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
780e4ad9c70e4a1261c212b11623d99e2477ef02783Owen Anderson    BlockT *EntryBlock = BB->getParent()->begin();
78144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
78244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    while (!TodoStack.empty()) {  // Process all the nodes in the loop
78344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      BlockT *X = TodoStack.back();
78444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      TodoStack.pop_back();
78544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
78644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      if (!L->contains(X) &&         // As of yet unprocessed??
78744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          DT.dominates(EntryBlock, X)) {   // X is reachable from entry block?
78844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        // Check to see if this block already belongs to a loop.  If this occurs
789131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner        // then we have a case where a loop that is supposed to be a child of
790131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner        // the current loop was processed before the current loop.  When this
791131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner        // occurs, this child loop gets added to a part of the current loop,
792131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner        // making it a sibling to the current loop.  We have to reparent this
793131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner        // loop.
794c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman        if (LoopT *SubLoop =
795c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman            const_cast<LoopT *>(getLoopFor(X)))
796131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner          if (SubLoop->getHeader() == X && isNotAlreadyContainedIn(SubLoop, L)){
79775144f93eb7e4dbf22d308d21581ae255dd520c6Dan Gohman            // Remove the subloop from its current parent...
79844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            assert(SubLoop->ParentLoop && SubLoop->ParentLoop != L);
799c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman            LoopT *SLP = SubLoop->ParentLoop;  // SubLoopParent
800c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman            typename std::vector<LoopT *>::iterator I =
80144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              std::find(SLP->SubLoops.begin(), SLP->SubLoops.end(), SubLoop);
802131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner            assert(I != SLP->SubLoops.end() &&"SubLoop not a child of parent?");
80344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            SLP->SubLoops.erase(I);   // Remove from parent...
80444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
80544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            // Add the subloop to THIS loop...
80644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            SubLoop->ParentLoop = L;
80744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            L->SubLoops.push_back(SubLoop);
80844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          }
80944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
81044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        // Normal case, add the block to our loop...
81144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        L->Blocks.push_back(X);
812d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson
813d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson        typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
814d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson
81544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        // Add all of the predecessors of X to the end of the work stack...
816d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson        TodoStack.insert(TodoStack.end(), InvBlockTraits::child_begin(X),
817d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson                         InvBlockTraits::child_end(X));
81844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      }
81944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    }
82044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
82144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // If there are any loops nested within this loop, create them now!
82244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(),
82344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson         E = L->Blocks.end(); I != E; ++I)
824c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman      if (LoopT *NewLoop = ConsiderForLoop(*I, DT)) {
82544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        L->SubLoops.push_back(NewLoop);
82644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        NewLoop->ParentLoop = L;
82744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      }
82844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
82944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // Add the basic blocks that comprise this loop to the BBMap so that this
83044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // loop can be found for them.
83144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    //
83244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(),
833c75cd159cd5e428c9b0c8fb761b83c86844cd287Dan Gohman           E = L->Blocks.end(); I != E; ++I)
834c75cd159cd5e428c9b0c8fb761b83c86844cd287Dan Gohman      BBMap.insert(std::make_pair(*I, L));
83544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
83644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // Now that we have a list of all of the child loops of this loop, check to
837131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner    // see if any of them should actually be nested inside of each other.  We
838131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner    // can accidentally pull loops our of their parents, so we must make sure to
83944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // organize the loop nests correctly now.
84044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    {
841c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman      std::map<BlockT *, LoopT *> ContainingLoops;
84244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      for (unsigned i = 0; i != L->SubLoops.size(); ++i) {
843c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman        LoopT *Child = L->SubLoops[i];
84444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        assert(Child->getParentLoop() == L && "Not proper child loop?");
84544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
846c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman        if (LoopT *ContainingLoop = ContainingLoops[Child->getHeader()]) {
84744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          // If there is already a loop which contains this loop, move this loop
84844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          // into the containing loop.
84944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          MoveSiblingLoopInto(Child, ContainingLoop);
85044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          --i;  // The loop got removed from the SubLoops list.
85144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        } else {
852131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner          // This is currently considered to be a top-level loop.  Check to see
853131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner          // if any of the contained blocks are loop headers for subloops we
854131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner          // have already processed.
85544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          for (unsigned b = 0, e = Child->Blocks.size(); b != e; ++b) {
856c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman            LoopT *&BlockLoop = ContainingLoops[Child->Blocks[b]];
85744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            if (BlockLoop == 0) {   // Child block not processed yet...
85844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              BlockLoop = Child;
85944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            } else if (BlockLoop != Child) {
860c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman              LoopT *SubLoop = BlockLoop;
86144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              // Reparent all of the blocks which used to belong to BlockLoops
86231a95c524c82e36867761404d902b94f7afc695eDan Gohman              for (unsigned j = 0, f = SubLoop->Blocks.size(); j != f; ++j)
86344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson                ContainingLoops[SubLoop->Blocks[j]] = Child;
86444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
86544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              // There is already a loop which contains this block, that means
86644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              // that we should reparent the loop which the block is currently
86744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              // considered to belong to to be a child of this loop.
86844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              MoveSiblingLoopInto(SubLoop, Child);
86944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson              --i;  // We just shrunk the SubLoops list.
87044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson            }
87144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson          }
87244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        }
87344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      }
87444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    }
87544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
87644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    return L;
87744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
87844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
879131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  /// MoveSiblingLoopInto - This method moves the NewChild loop to live inside
880131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  /// of the NewParent Loop, instead of being a sibling of it.
881c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void MoveSiblingLoopInto(LoopT *NewChild,
882c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                           LoopT *NewParent) {
883c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *OldParent = NewChild->getParentLoop();
88444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(OldParent && OldParent == NewParent->getParentLoop() &&
88544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson           NewChild != NewParent && "Not sibling loops!");
88644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
88744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // Remove NewChild from being a child of OldParent
888c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    typename std::vector<LoopT *>::iterator I =
889131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner      std::find(OldParent->SubLoops.begin(), OldParent->SubLoops.end(),
890131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner                NewChild);
89144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(I != OldParent->SubLoops.end() && "Parent fields incorrect??");
89244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    OldParent->SubLoops.erase(I);   // Remove from parent's subloops list
89344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    NewChild->ParentLoop = 0;
89444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
89544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    InsertLoopInto(NewChild, NewParent);
89644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
89744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
898131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  /// InsertLoopInto - This inserts loop L into the specified parent loop.  If
899131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  /// the parent loop contains a loop which should contain L, the loop gets
900131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  /// inserted into L instead.
901c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void InsertLoopInto(LoopT *L, LoopT *Parent) {
90244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    BlockT *LHeader = L->getHeader();
903131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner    assert(Parent->contains(LHeader) &&
904131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner           "This loop should not be inserted here!");
90544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
90644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // Check to see if it belongs in a child loop...
90734cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng    for (unsigned i = 0, e = static_cast<unsigned>(Parent->SubLoops.size());
90834cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng         i != e; ++i)
90944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      if (Parent->SubLoops[i]->contains(LHeader)) {
91044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        InsertLoopInto(L, Parent->SubLoops[i]);
91144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        return;
91244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      }
91344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
91444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    // If not, insert it here!
91544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    Parent->SubLoops.push_back(L);
91644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    L->ParentLoop = Parent;
91744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
91844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
91944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  // Debugging
92044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
921791102fb1192ac9483274e54cbc42480c9b1af10Chris Lattner  void print(raw_ostream &OS) const {
92244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    for (unsigned i = 0; i < TopLevelLoops.size(); ++i)
92344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      TopLevelLoops[i]->print(OS);
92444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  #if 0
925c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    for (std::map<BasicBlock*, LoopT*>::const_iterator I = BBMap.begin(),
92644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson           E = BBMap.end(); I != E; ++I)
92744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      OS << "BB '" << I->first->getName() << "' level = "
92844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson         << I->second->getLoopDepth() << "\n";
92944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  #endif
93044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
93144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson};
93244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
93344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonclass LoopInfo : public FunctionPass {
934c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopInfoBase<BasicBlock, Loop> LI;
935c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopBase<BasicBlock, Loop>;
9369d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman
9379d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  void operator=(const LoopInfo &); // do not implement
9389d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  LoopInfo(const LoopInfo &);       // do not implement
93944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonpublic:
94044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  static char ID; // Pass identification, replacement for typeid
94144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
94290c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson  LoopInfo() : FunctionPass(ID) {}
94344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
944c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopInfoBase<BasicBlock, Loop>& getBase() { return LI; }
945d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson
94644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// iterator/begin/end - The interface to the top-level loops in the current
94744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// function.
94844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
949c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  typedef LoopInfoBase<BasicBlock, Loop>::iterator iterator;
9509d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  inline iterator begin() const { return LI.begin(); }
9519d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  inline iterator end() const { return LI.end(); }
9529d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  bool empty() const { return LI.empty(); }
95344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
95444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
95544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// block is in no loop (for example the entry node), null is returned.
95644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
95744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline Loop *getLoopFor(const BasicBlock *BB) const {
9589d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.getLoopFor(BB);
95944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
96044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
96144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// operator[] - same as getLoopFor...
96244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
96344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline const Loop *operator[](const BasicBlock *BB) const {
9649d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.getLoopFor(BB);
96544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
96644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
967ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// getLoopDepth - Return the loop nesting level of the specified block.  A
968ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// depth of 0 means the block is not inside any loop.
96944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
97044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline unsigned getLoopDepth(const BasicBlock *BB) const {
9719d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.getLoopDepth(BB);
97244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
97344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
97444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  // isLoopHeader - True if the block is a loop header node
97544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline bool isLoopHeader(BasicBlock *BB) const {
9769d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.isLoopHeader(BB);
97744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
9780bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
9792b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// runOnFunction - Calculate the natural loop information.
9802b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
9817e70829632f82de15db187845666aaca6e04b792Chris Lattner  virtual bool runOnFunction(Function &F);
982facd752d3afaeca7dee46648f2a2ae209a94e5e9Chris Lattner
9835c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman  virtual void verifyAnalysis() const;
9845c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman
9859d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  virtual void releaseMemory() { LI.releaseMemory(); }
9865c7e326585f3a543388ba871c3425f7664cd9143Bill Wendling
98745cfe545ec8177262dabc70580ce05feaa1c3880Chris Lattner  virtual void print(raw_ostream &O, const Module* M = 0) const;
988791102fb1192ac9483274e54cbc42480c9b1af10Chris Lattner
989f57b845547302d24ecb6a9e79d7bc386f761a6c9Chris Lattner  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
990f57b845547302d24ecb6a9e79d7bc386f761a6c9Chris Lattner
9914e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  /// removeLoop - This removes the specified top-level loop from this loop info
9924e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  /// object.  The loop is not deleted, as it will presumably be inserted into
9934e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  /// another loop.
9949d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  inline Loop *removeLoop(iterator I) { return LI.removeLoop(I); }
9954e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner
99646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// changeLoopFor - Change the top-level loop that contains BB to the
99746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// specified loop.  This should be used by transformations that restructure
99846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// the loop hierarchy tree.
99944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline void changeLoopFor(BasicBlock *BB, Loop *L) {
10009d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.changeLoopFor(BB, L);
100144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
100246758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
100346758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// changeTopLevelLoop - Replace the specified loop in the top-level loops
100446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// list with the indicated loop.
100544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop) {
10069d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.changeTopLevelLoop(OldLoop, NewLoop);
100744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
100846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
1009072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  /// addTopLevelLoop - This adds the specified loop to the collection of
1010072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  /// top-level loops.
101144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline void addTopLevelLoop(Loop *New) {
10129d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.addTopLevelLoop(New);
1013072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  }
1014072b163424491c85df6664a4e056aae5e07dc64dChris Lattner
10159afb24bf0847b9f2ff0bf3f7f7405dcbe42fa38bChris Lattner  /// removeBlock - This method completely removes BB from all data structures,
10169afb24bf0847b9f2ff0bf3f7f7405dcbe42fa38bChris Lattner  /// including all of the Loop objects it is nested in and our mapping from
10179afb24bf0847b9f2ff0bf3f7f7405dcbe42fa38bChris Lattner  /// BasicBlocks to loops.
101844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  void removeBlock(BasicBlock *BB) {
10199d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.removeBlock(BB);
102044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
10210bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner};
10220bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
1023e0b6b78e095f7dea9589e8df5ec4521e346ad005Anand Shukla
10241db0a400370466e187ae06c96a1586c2c21409ddChris Lattner// Allow clients to walk the list of nested loops...
10251db0a400370466e187ae06c96a1586c2c21409ddChris Lattnertemplate <> struct GraphTraits<const Loop*> {
10261db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  typedef const Loop NodeType;
10279d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  typedef LoopInfo::iterator ChildIteratorType;
10281db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
10291db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  static NodeType *getEntryNode(const Loop *L) { return L; }
10309769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_begin(NodeType *N) {
1031329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->begin();
10321db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
10339769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_end(NodeType *N) {
1034329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->end();
10351db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
10361db0a400370466e187ae06c96a1586c2c21409ddChris Lattner};
10371db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
10381db0a400370466e187ae06c96a1586c2c21409ddChris Lattnertemplate <> struct GraphTraits<Loop*> {
10391db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  typedef Loop NodeType;
10409d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  typedef LoopInfo::iterator ChildIteratorType;
10411db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
10421db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  static NodeType *getEntryNode(Loop *L) { return L; }
10439769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_begin(NodeType *N) {
1044329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->begin();
10451db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
10469769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_end(NodeType *N) {
1047329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->end();
10481db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
10491db0a400370466e187ae06c96a1586c2c21409ddChris Lattner};
10501db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
1051c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class BlockT, class LoopT>
1052c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanvoid
1053c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan GohmanLoopBase<BlockT, LoopT>::addBasicBlockToLoop(BlockT *NewBB,
1054c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                                             LoopInfoBase<BlockT, LoopT> &LIB) {
1055d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson  assert((Blocks.empty() || LIB[getHeader()] == this) &&
1056019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson         "Incorrect LI specified for this loop!");
1057019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  assert(NewBB && "Cannot add a null basic block to the loop!");
1058d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson  assert(LIB[NewBB] == 0 && "BasicBlock already in the loop!");
105944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
1060c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *L = static_cast<LoopT *>(this);
1061c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman
1062019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  // Add the loop mapping to the LoopInfo object...
1063c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LIB.BBMap[NewBB] = L;
1064019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
1065019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  // Add the basic block to this loop and all parent loops...
1066019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  while (L) {
1067019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    L->Blocks.push_back(NewBB);
1068019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    L = L->getParentLoop();
1069019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1070019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson}
1071019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
1072d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
1073d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
10740bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner#endif
1075