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//  * etc...
270bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//
280bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//===----------------------------------------------------------------------===//
290bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
300bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner#ifndef LLVM_ANALYSIS_LOOP_INFO_H
310bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner#define LLVM_ANALYSIS_LOOP_INFO_H
320bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
33facd752d3afaeca7dee46648f2a2ae209a94e5e9Chris Lattner#include "llvm/Pass.h"
3472851059224b90a3ae74ecd62fbd927a0c54a9f0Cameron Zwarich#include "llvm/ADT/DenseMap.h"
355434c1e73b6e56756719d2aebb952ac7bb3829e0Andrew Trick#include "llvm/ADT/DenseSet.h"
3644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson#include "llvm/ADT/DepthFirstIterator.h"
37551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/GraphTraits.h"
38b7211a2ce13a0365e0e1dd2f27adda2ee3d1288bDevang Patel#include "llvm/ADT/SmallVector.h"
3992583187306cec18d73db362681ea4f8e2b41909Lang Hames#include "llvm/ADT/STLExtras.h"
4044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson#include "llvm/Analysis/Dominators.h"
41019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#include "llvm/Support/CFG.h"
42103289e9383ad1eb66caf28c9b166aebce963a35Chris Lattner#include "llvm/Support/raw_ostream.h"
43019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#include <algorithm>
449fc5cdf77c812aaa80419036de27576d45894d0dChris Lattner#include <map>
45019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
461d5562f72e3f7d9e17a2bf95afa54a98dac95894Dan Gohmannamespace llvm {
471d5562f72e3f7d9e17a2bf95afa54a98dac95894Dan Gohman
48019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Andersontemplate<typename T>
49305b515c2787f47adecbe120e4b4bef55c5e5525Chandler Carruthinline void RemoveFromVector(std::vector<T*> &V, T *N) {
50019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N);
51019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  assert(I != V.end() && "N is not in this list!");
52019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  V.erase(I);
53019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson}
540bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
5553c279b1949f7fa626ccbc399ebbe2d7dc9599a4Devang Patelclass DominatorTree;
568fc2f2072de83665ae20e06929e28317f449bcdfChris Lattnerclass LoopInfo;
57c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanclass Loop;
589fc5cdf77c812aaa80419036de27576d45894d0dChris Lattnerclass PHINode;
59c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class N, class M> class LoopInfoBase;
60c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class N, class M> class LoopBase;
610bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
620bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//===----------------------------------------------------------------------===//
63131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner/// LoopBase class - Instances of this class are used to represent loops that
64131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner/// are detected in the flow graph
652b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner///
66c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class BlockT, class LoopT>
67019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Andersonclass LoopBase {
68c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *ParentLoop;
69131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  // SubLoops - Loops contained entirely within this one.
70c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  std::vector<LoopT *> SubLoops;
71131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner
72131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  // Blocks - The list of blocks in this loop.  First entry is the header node.
73131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  std::vector<BlockT*> Blocks;
74019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
75c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  // DO NOT IMPLEMENT
76c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopBase(const LoopBase<BlockT, LoopT> &);
77c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  // DO NOT IMPLEMENT
78c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  const LoopBase<BlockT, LoopT>&operator=(const LoopBase<BlockT, LoopT> &);
790bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattnerpublic:
8046758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// Loop ctor - This creates an empty loop.
81019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  LoopBase() : ParentLoop(0) {}
82019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  ~LoopBase() {
8334cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng    for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
844e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner      delete SubLoops[i];
854e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  }
860bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
87ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// getLoopDepth - Return the nesting level of this loop.  An outer-most
88ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// loop has depth 1, for consistency with loop depth values used for basic
89ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// blocks, where depth 0 is used for blocks not inside any loops.
90072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  unsigned getLoopDepth() const {
91ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman    unsigned D = 1;
92c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    for (const LoopT *CurLoop = ParentLoop; CurLoop;
93019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson         CurLoop = CurLoop->ParentLoop)
94072b163424491c85df6664a4e056aae5e07dc64dChris Lattner      ++D;
95072b163424491c85df6664a4e056aae5e07dc64dChris Lattner    return D;
96072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  }
97019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  BlockT *getHeader() const { return Blocks.front(); }
98c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *getParentLoop() const { return ParentLoop; }
990bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
10037aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick  /// setParentLoop is a raw interface for bypassing addChildLoop.
10137aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick  void setParentLoop(LoopT *L) { ParentLoop = L; }
10237aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick
10392329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  /// contains - Return true if the specified loop is contained within in
10492329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  /// this loop.
10592329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  ///
10692329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  bool contains(const LoopT *L) const {
10792329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman    if (L == this) return true;
10892329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman    if (L == 0)    return false;
10992329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman    return contains(L->getParentLoop());
11092329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  }
111fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
11292329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  /// contains - Return true if the specified basic block is in this loop.
113fbdf4bf1799bf9ea566fc0fc0507752590a6d559Misha Brukman  ///
114019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  bool contains(const BlockT *BB) const {
115f3ab3a937203d690744357844948faaf96fb73f9Dan Gohman    return std::find(block_begin(), block_end(), BB) != block_end();
116019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1170bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
11892329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  /// contains - Return true if the specified instruction is in this loop.
11992329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  ///
12092329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  template<class InstT>
12192329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  bool contains(const InstT *Inst) const {
12292329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman    return contains(Inst->getParent());
12392329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  }
12492329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman
125329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  /// iterator/begin/end - Return the loops contained entirely within this loop.
1262b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
127c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  const std::vector<LoopT *> &getSubLoops() const { return SubLoops; }
12837aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick  std::vector<LoopT *> &getSubLoopsVector() { return SubLoops; }
129c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  typedef typename std::vector<LoopT *>::const_iterator iterator;
130c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  typedef typename std::vector<LoopT *>::const_reverse_iterator
131c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick    reverse_iterator;
132329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator begin() const { return SubLoops.begin(); }
133329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator end() const { return SubLoops.end(); }
134c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  reverse_iterator rbegin() const { return SubLoops.rbegin(); }
135c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  reverse_iterator rend() const { return SubLoops.rend(); }
13621c276d2fa99914d5ed958ac0aec7d78e3dd87cfDan Gohman  bool empty() const { return SubLoops.empty(); }
137fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner
138fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// getBlocks - Get a list of the basic blocks which make up this loop.
139fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  ///
140019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  const std::vector<BlockT*> &getBlocks() const { return Blocks; }
14137aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick  std::vector<BlockT*> &getBlocksVector() { return Blocks; }
142019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  typedef typename std::vector<BlockT*>::const_iterator block_iterator;
1434e77cc46ac688e7bee98747049f90e19e2902227Chris Lattner  block_iterator block_begin() const { return Blocks.begin(); }
1444e77cc46ac688e7bee98747049f90e19e2902227Chris Lattner  block_iterator block_end() const { return Blocks.end(); }
145fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner
146306afdf443a0e2d5d89c1a3ece00cfe18be1c4c4Andrew Trick  /// getNumBlocks - Get the number of blocks in this loop in constant time.
147b1eede12818d91a32adac928c6fffcf6d2800dc0Andrew Trick  unsigned getNumBlocks() const {
148306afdf443a0e2d5d89c1a3ece00cfe18be1c4c4Andrew Trick    return Blocks.size();
149b1eede12818d91a32adac928c6fffcf6d2800dc0Andrew Trick  }
150b1eede12818d91a32adac928c6fffcf6d2800dc0Andrew Trick
151d146e986c818165cca866ee05751451706ccf36aDan Gohman  /// isLoopExiting - True if terminator in the block can branch to another
152d146e986c818165cca866ee05751451706ccf36aDan Gohman  /// block that is outside of the current loop.
153fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  ///
15432663b719b4996b3a735f22bba80d771d50f96e7Dan Gohman  bool isLoopExiting(const BlockT *BB) const {
155d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<BlockT*> BlockTraits;
156d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    for (typename BlockTraits::ChildIteratorType SI =
157d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         BlockTraits::child_begin(const_cast<BlockT*>(BB)),
158d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         SE = BlockTraits::child_end(const_cast<BlockT*>(BB)); SI != SE; ++SI) {
159019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      if (!contains(*SI))
160019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        return true;
161019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    }
162019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return false;
163019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1646b290a54409f6bb6a0cc1c0446cd2b170a4b7addMisha Brukman
165fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// getNumBackEdges - Calculate the number of back edges to the loop header
166fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  ///
167019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  unsigned getNumBackEdges() const {
168019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    unsigned NumBackEdges = 0;
169019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    BlockT *H = getHeader();
170019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
171d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
172d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    for (typename InvBlockTraits::ChildIteratorType I =
173d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         InvBlockTraits::child_begin(const_cast<BlockT*>(H)),
174d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson         E = InvBlockTraits::child_end(const_cast<BlockT*>(H)); I != E; ++I)
175019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      if (contains(*I))
176019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        ++NumBackEdges;
177019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
178019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return NumBackEdges;
179019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1809474dd68e8a050193ca4003940ac399e2b17cb6aChris Lattner
181e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //===--------------------------------------------------------------------===//
182e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // APIs for simple analysis of the loop.
183e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //
184e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // Note that all of these methods can fail on general loops (ie, there may not
185e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // be a preheader, etc).  For best success, the loop simplification and
186e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // induction variable canonicalization pass should be used to normalize loops
187e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // for easy analysis.  These methods assume canonical loops.
188e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner
1897466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  /// getExitingBlocks - Return all blocks inside the loop that have successors
1907466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  /// outside of the loop.  These are the blocks _inside of the current loop_
1917466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  /// which branch out.  The returned list is always unique.
1927466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  ///
193cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const;
1947466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner
195c83324682f3409c15dad992cd62928426c9ad83dDan Gohman  /// getExitingBlock - If getExitingBlocks would return exactly one block,
196c83324682f3409c15dad992cd62928426c9ad83dDan Gohman  /// return that block. Otherwise return null.
197cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  BlockT *getExitingBlock() const;
198c83324682f3409c15dad992cd62928426c9ad83dDan Gohman
199f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// getExitBlocks - Return all of the successor blocks of this loop.  These
200f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// are the blocks _outside of the current loop_ which are branched to.
201f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  ///
202cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const;
203f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner
2041827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman  /// getExitBlock - If getExitBlocks would return exactly one block,
2051827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman  /// return that block. Otherwise return null.
206cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  BlockT *getExitBlock() const;
2071827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman
20860f422f894ae9aff2f508f34733be36f5a0ed20aLang Hames  /// Edge type.
209cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  typedef std::pair<const BlockT*, const BlockT*> Edge;
21060f422f894ae9aff2f508f34733be36f5a0ed20aLang Hames
21155e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar  /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
212cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const;
21355e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar
2142b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// getLoopPreheader - If there is a preheader for this loop, return it.  A
2152b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// loop has a preheader if there is only one edge to the header of the loop
2162b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// from outside of the loop.  If this is the case, the block branching to the
217e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  /// header of the loop is the preheader node.
2182b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
219e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  /// This method returns null if there is no preheader for the loop.
2202b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
221cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  BlockT *getLoopPreheader() const;
2225cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman
2235cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman  /// getLoopPredecessor - If the given loop's header has exactly one unique
2245cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman  /// predecessor outside the loop, return it. Otherwise return null.
2255cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman  /// This is less strict that the loop "preheader" concept, which requires
2265cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman  /// the predecessor to have exactly one successor.
2275cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman  ///
228cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  BlockT *getLoopPredecessor() const;
2292b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner
230b317143ba851ce2853fb262fb2185ef5f1be030dDan Gohman  /// getLoopLatch - If there is a single latch block for this loop, return it.
231b317143ba851ce2853fb262fb2185ef5f1be030dDan Gohman  /// A latch block is a block that contains a branch back to the header.
232cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  BlockT *getLoopLatch() const;
233e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner
234e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //===--------------------------------------------------------------------===//
235e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // APIs for updating loop information after changing the CFG
236e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //
237e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner
238fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// addBasicBlockToLoop - This method is used by other analyses to update loop
239fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// information.  NewBB is set to be a new member of the current loop.
2402b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// Because of this, it is added as a member of all parent loops, and is added
2412b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// to the specified LoopInfo object as being in the current basic block.  It
2422b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// is not valid to replace the loop header with this method.
2432b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
244c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
2452b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner
24646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// replaceChildLoopWith - This is used when splitting loops up.  It replaces
24746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// the OldChild entry in our children list with NewChild, and updates the
24846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// parent pointer of OldChild to be null and the NewChild to be this loop.
24946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// This updates the loop depth of the new child.
250cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild);
25146758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
25246758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// addChildLoop - Add the specified loop to be a child of this loop.  This
25346758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// updates the loop depth of the new child.
25446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  ///
255c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void addChildLoop(LoopT *NewChild) {
256019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
257c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    NewChild->ParentLoop = static_cast<LoopT *>(this);
258019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SubLoops.push_back(NewChild);
259019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
26046758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
26146758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// removeChildLoop - This removes the specified child from being a subloop of
26246758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// this loop.  The loop is not deleted, as it will presumably be inserted
26346758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// into another loop.
264c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *removeChildLoop(iterator I) {
265019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(I != SubLoops.end() && "Cannot remove end iterator!");
266c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *Child = *I;
267019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(Child->ParentLoop == this && "Child is not a child of this loop!");
268019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SubLoops.erase(SubLoops.begin()+(I-begin()));
269019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    Child->ParentLoop = 0;
270019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return Child;
271019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
27246758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
27346758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// addBlockEntry - This adds a basic block directly to the basic block list.
27446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// This should only be used by transformations that create new loops.  Other
27546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// transformations should use addBasicBlockToLoop.
276019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void addBlockEntry(BlockT *BB) {
27746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner    Blocks.push_back(BB);
27846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  }
27946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
28051a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  /// moveToHeader - This method is used to move BB (which must be part of this
28151a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  /// loop) to be the loop header of the loop (the block that dominates all
28251a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  /// others).
283019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void moveToHeader(BlockT *BB) {
28451a4ad475b095aed49caff176b98c0754e421af4Chris Lattner    if (Blocks[0] == BB) return;
28551a4ad475b095aed49caff176b98c0754e421af4Chris Lattner    for (unsigned i = 0; ; ++i) {
28651a4ad475b095aed49caff176b98c0754e421af4Chris Lattner      assert(i != Blocks.size() && "Loop does not contain BB!");
28751a4ad475b095aed49caff176b98c0754e421af4Chris Lattner      if (Blocks[i] == BB) {
28851a4ad475b095aed49caff176b98c0754e421af4Chris Lattner        Blocks[i] = Blocks[0];
28951a4ad475b095aed49caff176b98c0754e421af4Chris Lattner        Blocks[0] = BB;
29051a4ad475b095aed49caff176b98c0754e421af4Chris Lattner        return;
29151a4ad475b095aed49caff176b98c0754e421af4Chris Lattner      }
29251a4ad475b095aed49caff176b98c0754e421af4Chris Lattner    }
29351a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  }
29451a4ad475b095aed49caff176b98c0754e421af4Chris Lattner
29546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// removeBlockFromLoop - This removes the specified basic block from the
296f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// current loop, updating the Blocks as appropriate.  This does not update
297f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// the mapping in the LoopInfo class.
298019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void removeBlockFromLoop(BlockT *BB) {
299019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    RemoveFromVector(Blocks, BB);
300019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
30146758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
30258e0ef1e90c3f6dbae213612b44e56f7d6d65ea7Devang Patel  /// verifyLoop - Verify loop structure
303cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void verifyLoop() const;
304019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
3055c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman  /// verifyLoop - Verify loop structure of this loop and all nested loops.
306cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void verifyLoopNest(DenseSet<const LoopT*> *Loops) const;
30758e0ef1e90c3f6dbae213612b44e56f7d6d65ea7Devang Patel
308cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void print(raw_ostream &OS, unsigned Depth = 0) const;
309dda30cd4af1c5f88fc00fd40b673f8e27c61379dDan Gohman
310c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanprotected:
311c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopInfoBase<BlockT, LoopT>;
3121002c0203450620594a85454c6a095ca94b87cb2Dan Gohman  explicit LoopBase(BlockT *BB) : ParentLoop(0) {
313072b163424491c85df6664a4e056aae5e07dc64dChris Lattner    Blocks.push_back(BB);
3140bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
3150bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner};
3160bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
3176a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesentemplate<class BlockT, class LoopT>
3186a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesenraw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
3196a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesen  Loop.print(OS);
3206a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesen  return OS;
3216a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesen}
3226a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesen
32360c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick// Implementation in LoopInfoImpl.h
32460c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick#ifdef __GNUC__
32560c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick__extension__ extern template class LoopBase<BasicBlock, Loop>;
32660c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick#endif
32760c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick
328c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanclass Loop : public LoopBase<BasicBlock, Loop> {
329c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanpublic:
330c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  Loop() {}
33116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
33216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// isLoopInvariant - Return true if the specified value is loop invariant
33316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
33416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  bool isLoopInvariant(Value *V) const;
33516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
336adc799112dc180b3cd099038c05101b85d217716Chris Lattner  /// hasLoopInvariantOperands - Return true if all the operands of the
337fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick  /// specified instruction are loop invariant.
338adc799112dc180b3cd099038c05101b85d217716Chris Lattner  bool hasLoopInvariantOperands(Instruction *I) const;
339a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman
340206613b289f60b71a76e9190d36b9ea9e47a701eDan Gohman  /// makeLoopInvariant - If the given value is an instruction inside of the
341a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// loop and it can be hoisted, do so to make it trivially loop-invariant.
342a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// Return true if the value after any hoisting is loop invariant. This
343a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// function can be used as a slightly more aggressive replacement for
344a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// isLoopInvariant.
345a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
346a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If InsertPt is specified, it is the point to hoist instructions to.
347a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If null, the terminator of the loop preheader is used.
348a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
349bdc017edacb713119b24ab269d250a82d62fffebDan Gohman  bool makeLoopInvariant(Value *V, bool &Changed,
350bdc017edacb713119b24ab269d250a82d62fffebDan Gohman                         Instruction *InsertPt = 0) const;
351a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman
352a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// makeLoopInvariant - If the given instruction is inside of the
353a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// loop and it can be hoisted, do so to make it trivially loop-invariant.
354a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// Return true if the instruction after any hoisting is loop invariant. This
355a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// function can be used as a slightly more aggressive replacement for
356a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// isLoopInvariant.
357a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
358a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If InsertPt is specified, it is the point to hoist instructions to.
359a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If null, the terminator of the loop preheader is used.
360a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
361bdc017edacb713119b24ab269d250a82d62fffebDan Gohman  bool makeLoopInvariant(Instruction *I, bool &Changed,
362bdc017edacb713119b24ab269d250a82d62fffebDan Gohman                         Instruction *InsertPt = 0) const;
363a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman
36416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// getCanonicalInductionVariable - Check to see if the loop has a canonical
36516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// induction variable: an integer recurrence that starts at 0 and increments
36616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// by one each time through the loop.  If so, return the phi node that
36716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// corresponds to it.
36816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
36916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// The IndVarSimplify pass transforms loops to have a canonical induction
37016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// variable.
37116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
37216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  PHINode *getCanonicalInductionVariable() const;
37316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
37416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// isLCSSAForm - Return true if the Loop is in LCSSA form
375bbf81d88116d23fb0776412b5916f7d0b8b3ca7eDan Gohman  bool isLCSSAForm(DominatorTree &DT) const;
37616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
377937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  /// isLoopSimplifyForm - Return true if the Loop is in the form that
378937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  /// the LoopSimplify form transforms loops to, which is sometimes called
379937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  /// normal form.
380937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  bool isLoopSimplifyForm() const;
381937738649386b8188524d0cd61943214a5b93cf6Dan Gohman
382d9fc1ce8096f7138c60edc3a6655583bf209780eAndrew Trick  /// isSafeToClone - Return true if the loop body is safe to clone in practice.
383d9fc1ce8096f7138c60edc3a6655583bf209780eAndrew Trick  bool isSafeToClone() const;
384d9fc1ce8096f7138c60edc3a6655583bf209780eAndrew Trick
385f17e9511f15a0e007ff47d0789d1a52502e8c1fbDan Gohman  /// hasDedicatedExits - Return true if no exit block for the loop
386f17e9511f15a0e007ff47d0789d1a52502e8c1fbDan Gohman  /// has a predecessor that is outside the loop.
387f17e9511f15a0e007ff47d0789d1a52502e8c1fbDan Gohman  bool hasDedicatedExits() const;
388f17e9511f15a0e007ff47d0789d1a52502e8c1fbDan Gohman
389fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick  /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
390f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  /// These are the blocks _outside of the current loop_ which are branched to.
391050959cd08db6c0efb8208271a1d64ce58893e20Dan Gohman  /// This assumes that loop exits are in canonical form.
392f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  ///
393f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  void getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const;
394f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman
395f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
396f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  /// block, return that block. Otherwise return null.
397f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  BasicBlock *getUniqueExitBlock() const;
398f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman
399dda30cd4af1c5f88fc00fd40b673f8e27c61379dDan Gohman  void dump() const;
400fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
401c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanprivate:
402c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopInfoBase<BasicBlock, Loop>;
403c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
404c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman};
4050bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
4060bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//===----------------------------------------------------------------------===//
4072b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner/// LoopInfo - This class builds and contains all of the top level loop
4082b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner/// structures in the specified function.
4092b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner///
41044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
411c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class BlockT, class LoopT>
41244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonclass LoopInfoBase {
4130bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  // BBMap - Mapping of basic blocks to the inner most loop they occur in
41472851059224b90a3ae74ecd62fbd927a0c54a9f0Cameron Zwarich  DenseMap<BlockT *, LoopT *> BBMap;
415c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  std::vector<LoopT *> TopLevelLoops;
416c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopBase<BlockT, LoopT>;
4175434c1e73b6e56756719d2aebb952ac7bb3829e0Andrew Trick  friend class LoopInfo;
4189d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman
4199d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  void operator=(const LoopInfoBase &); // do not implement
4209d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  LoopInfoBase(const LoopInfo &);       // do not implement
4210bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattnerpublic:
42244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  LoopInfoBase() { }
42344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ~LoopInfoBase() { releaseMemory(); }
424fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
42544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  void releaseMemory() {
426c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    for (typename std::vector<LoopT *>::iterator I =
42744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson         TopLevelLoops.begin(), E = TopLevelLoops.end(); I != E; ++I)
42844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      delete *I;   // Delete all of the loops...
4290bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
430131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner    BBMap.clear();                           // Reset internal state of analysis
43144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    TopLevelLoops.clear();
43244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
433fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
434329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  /// iterator/begin/end - The interface to the top-level loops in the current
435329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  /// function.
436329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  ///
437c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  typedef typename std::vector<LoopT *>::const_iterator iterator;
438c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  typedef typename std::vector<LoopT *>::const_reverse_iterator
439c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick    reverse_iterator;
440329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator begin() const { return TopLevelLoops.begin(); }
441329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator end() const { return TopLevelLoops.end(); }
442c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
443c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  reverse_iterator rend() const { return TopLevelLoops.rend(); }
444a8c763b3071ae1a58ee8baeb282331245527e004Dan Gohman  bool empty() const { return TopLevelLoops.empty(); }
445fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
4462b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
4472b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// block is in no loop (for example the entry node), null is returned.
4482b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
449c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *getLoopFor(const BlockT *BB) const {
450da69f3b357097e75fbf9a5a2bbe1e7273d4b4271Benjamin Kramer    return BBMap.lookup(const_cast<BlockT*>(BB));
4510bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
452fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
4532b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// operator[] - same as getLoopFor...
4542b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
455c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  const LoopT *operator[](const BlockT *BB) const {
4560bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner    return getLoopFor(BB);
4570bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
458fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
459ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// getLoopDepth - Return the loop nesting level of the specified block.  A
460ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// depth of 0 means the block is not inside any loop.
4612b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
46244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  unsigned getLoopDepth(const BlockT *BB) const {
463c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    const LoopT *L = getLoopFor(BB);
4640bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner    return L ? L->getLoopDepth() : 0;
4650bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
4660bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
4670bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  // isLoopHeader - True if the block is a loop header node
46844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  bool isLoopHeader(BlockT *BB) const {
469c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    const LoopT *L = getLoopFor(BB);
47050f5490842d501e269a4c6085d0d132cae0d31f8Chris Lattner    return L && L->getHeader() == BB;
4710bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
472fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
47344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// removeLoop - This removes the specified top-level loop from this loop info
47444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// object.  The loop is not deleted, as it will presumably be inserted into
47544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// another loop.
476c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *removeLoop(iterator I) {
47744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(I != end() && "Cannot remove end iterator!");
478c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *L = *I;
47944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(L->getParentLoop() == 0 && "Not a top-level loop!");
48044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
48144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    return L;
48244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
483fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
48444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// changeLoopFor - Change the top-level loop that contains BB to the
48544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// specified loop.  This should be used by transformations that restructure
48644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// the loop hierarchy tree.
487c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void changeLoopFor(BlockT *BB, LoopT *L) {
488fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick    if (!L) {
489da69f3b357097e75fbf9a5a2bbe1e7273d4b4271Benjamin Kramer      BBMap.erase(BB);
490fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick      return;
491fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick    }
492fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick    BBMap[BB] = L;
49344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
494fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
49544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// changeTopLevelLoop - Replace the specified loop in the top-level loops
49644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// list with the indicated loop.
497c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void changeTopLevelLoop(LoopT *OldLoop,
498c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                          LoopT *NewLoop) {
499c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    typename std::vector<LoopT *>::iterator I =
50044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson                 std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
50144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(I != TopLevelLoops.end() && "Old loop not at top level!");
50244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    *I = NewLoop;
50344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(NewLoop->ParentLoop == 0 && OldLoop->ParentLoop == 0 &&
50444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson           "Loops already embedded into a subloop!");
50544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
506fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
50744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// addTopLevelLoop - This adds the specified loop to the collection of
50844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// top-level loops.
509c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void addTopLevelLoop(LoopT *New) {
51044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(New->getParentLoop() == 0 && "Loop already in subloop!");
51144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    TopLevelLoops.push_back(New);
51244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
513fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
51444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// removeBlock - This method completely removes BB from all data structures,
51544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// including all of the Loop objects it is nested in and our mapping from
51644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// BasicBlocks to loops.
51744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  void removeBlock(BlockT *BB) {
51872851059224b90a3ae74ecd62fbd927a0c54a9f0Cameron Zwarich    typename DenseMap<BlockT *, LoopT *>::iterator I = BBMap.find(BB);
51944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (I != BBMap.end()) {
520c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman      for (LoopT *L = I->second; L; L = L->getParentLoop())
52144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        L->removeBlockFromLoop(BB);
52244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
52344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      BBMap.erase(I);
52444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    }
52544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
526fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
52744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  // Internals
528fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
529c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
530c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                                      const LoopT *ParentLoop) {
53144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (SubLoop == 0) return true;
53244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (SubLoop == ParentLoop) return false;
53344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
53444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
535fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
53637aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick  /// Create the loop forest using a stable algorithm.
53737aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick  void Analyze(DominatorTreeBase<BlockT> &DomTree);
53837aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick
53944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  // Debugging
540fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
541cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void print(raw_ostream &OS) const;
54244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson};
54344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
54460c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick// Implementation in LoopInfoImpl.h
54560c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick#ifdef __GNUC__
54660c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick__extension__ extern template class LoopInfoBase<BasicBlock, Loop>;
54760c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick#endif
54860c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick
54944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonclass LoopInfo : public FunctionPass {
550c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopInfoBase<BasicBlock, Loop> LI;
551c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopBase<BasicBlock, Loop>;
5529d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman
5539d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  void operator=(const LoopInfo &); // do not implement
5549d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  LoopInfo(const LoopInfo &);       // do not implement
55544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonpublic:
55644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  static char ID; // Pass identification, replacement for typeid
55744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
558081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson  LoopInfo() : FunctionPass(ID) {
559081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson    initializeLoopInfoPass(*PassRegistry::getPassRegistry());
560081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson  }
56144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
562c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopInfoBase<BasicBlock, Loop>& getBase() { return LI; }
563d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson
56444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// iterator/begin/end - The interface to the top-level loops in the current
56544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// function.
56644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
567c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  typedef LoopInfoBase<BasicBlock, Loop>::iterator iterator;
568c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  typedef LoopInfoBase<BasicBlock, Loop>::reverse_iterator reverse_iterator;
5699d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  inline iterator begin() const { return LI.begin(); }
5709d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  inline iterator end() const { return LI.end(); }
571c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  inline reverse_iterator rbegin() const { return LI.rbegin(); }
572c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  inline reverse_iterator rend() const { return LI.rend(); }
5739d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  bool empty() const { return LI.empty(); }
57444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
57544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
57644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// block is in no loop (for example the entry node), null is returned.
57744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
57844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline Loop *getLoopFor(const BasicBlock *BB) const {
5799d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.getLoopFor(BB);
58044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
58144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
58244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// operator[] - same as getLoopFor...
58344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
58444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline const Loop *operator[](const BasicBlock *BB) const {
5859d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.getLoopFor(BB);
58644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
58744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
588ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// getLoopDepth - Return the loop nesting level of the specified block.  A
589ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// depth of 0 means the block is not inside any loop.
59044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
59144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline unsigned getLoopDepth(const BasicBlock *BB) const {
5929d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.getLoopDepth(BB);
59344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
59444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
59544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  // isLoopHeader - True if the block is a loop header node
59644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline bool isLoopHeader(BasicBlock *BB) const {
5979d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.isLoopHeader(BB);
59844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
5990bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
6002b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// runOnFunction - Calculate the natural loop information.
6012b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
6027e70829632f82de15db187845666aaca6e04b792Chris Lattner  virtual bool runOnFunction(Function &F);
603facd752d3afaeca7dee46648f2a2ae209a94e5e9Chris Lattner
6045c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman  virtual void verifyAnalysis() const;
6055c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman
6069d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  virtual void releaseMemory() { LI.releaseMemory(); }
6075c7e326585f3a543388ba871c3425f7664cd9143Bill Wendling
60845cfe545ec8177262dabc70580ce05feaa1c3880Chris Lattner  virtual void print(raw_ostream &O, const Module* M = 0) const;
609fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
610f57b845547302d24ecb6a9e79d7bc386f761a6c9Chris Lattner  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
611f57b845547302d24ecb6a9e79d7bc386f761a6c9Chris Lattner
6124e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  /// removeLoop - This removes the specified top-level loop from this loop info
6134e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  /// object.  The loop is not deleted, as it will presumably be inserted into
6144e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  /// another loop.
6159d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  inline Loop *removeLoop(iterator I) { return LI.removeLoop(I); }
6164e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner
61746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// changeLoopFor - Change the top-level loop that contains BB to the
61846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// specified loop.  This should be used by transformations that restructure
61946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// the loop hierarchy tree.
62044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline void changeLoopFor(BasicBlock *BB, Loop *L) {
6219d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.changeLoopFor(BB, L);
62244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
62346758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
62446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// changeTopLevelLoop - Replace the specified loop in the top-level loops
62546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// list with the indicated loop.
62644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop) {
6279d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.changeTopLevelLoop(OldLoop, NewLoop);
62844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
62946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
630072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  /// addTopLevelLoop - This adds the specified loop to the collection of
631072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  /// top-level loops.
63244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline void addTopLevelLoop(Loop *New) {
6339d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.addTopLevelLoop(New);
634072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  }
635072b163424491c85df6664a4e056aae5e07dc64dChris Lattner
6369afb24bf0847b9f2ff0bf3f7f7405dcbe42fa38bChris Lattner  /// removeBlock - This method completely removes BB from all data structures,
6379afb24bf0847b9f2ff0bf3f7f7405dcbe42fa38bChris Lattner  /// including all of the Loop objects it is nested in and our mapping from
6389afb24bf0847b9f2ff0bf3f7f7405dcbe42fa38bChris Lattner  /// BasicBlocks to loops.
63944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  void removeBlock(BasicBlock *BB) {
6409d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.removeBlock(BB);
64144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
642d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands
643fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick  /// updateUnloop - Update LoopInfo after removing the last backedge from a
644fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick  /// loop--now the "unloop". This updates the loop forest and parent loops for
645fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick  /// each block so that Unloop is no longer referenced, but the caller must
646fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick  /// actually delete the Unloop object.
647fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick  void updateUnloop(Loop *Unloop);
648fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick
649d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands  /// replacementPreservesLCSSAForm - Returns true if replacing From with To
650d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands  /// everywhere is guaranteed to preserve LCSSA form.
651d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands  bool replacementPreservesLCSSAForm(Instruction *From, Value *To) {
652d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // Preserving LCSSA form is only problematic if the replacing value is an
653d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // instruction.
654d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    Instruction *I = dyn_cast<Instruction>(To);
655d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    if (!I) return true;
6569bc6a90146417af03144fa2f7dd94f9945b57c06Duncan Sands    // If both instructions are defined in the same basic block then replacement
6579bc6a90146417af03144fa2f7dd94f9945b57c06Duncan Sands    // cannot break LCSSA form.
6589bc6a90146417af03144fa2f7dd94f9945b57c06Duncan Sands    if (I->getParent() == From->getParent())
6599bc6a90146417af03144fa2f7dd94f9945b57c06Duncan Sands      return true;
660d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // If the instruction is not defined in a loop then it can safely replace
661d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // anything.
662d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    Loop *ToLoop = getLoopFor(I->getParent());
663d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    if (!ToLoop) return true;
664d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // If the replacing instruction is defined in the same loop as the original
665d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // instruction, or in a loop that contains it as an inner loop, then using
666d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // it as a replacement will not break LCSSA form.
6673b0aed19f0f9ed62362c04f7e18e2ada686f4055Duncan Sands    return ToLoop->contains(getLoopFor(From->getParent()));
668d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands  }
6690bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner};
6700bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
671e0b6b78e095f7dea9589e8df5ec4521e346ad005Anand Shukla
6721db0a400370466e187ae06c96a1586c2c21409ddChris Lattner// Allow clients to walk the list of nested loops...
6731db0a400370466e187ae06c96a1586c2c21409ddChris Lattnertemplate <> struct GraphTraits<const Loop*> {
6741db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  typedef const Loop NodeType;
6759d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  typedef LoopInfo::iterator ChildIteratorType;
6761db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
6771db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  static NodeType *getEntryNode(const Loop *L) { return L; }
6789769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_begin(NodeType *N) {
679329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->begin();
6801db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
6819769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_end(NodeType *N) {
682329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->end();
6831db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
6841db0a400370466e187ae06c96a1586c2c21409ddChris Lattner};
6851db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
6861db0a400370466e187ae06c96a1586c2c21409ddChris Lattnertemplate <> struct GraphTraits<Loop*> {
6871db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  typedef Loop NodeType;
6889d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  typedef LoopInfo::iterator ChildIteratorType;
6891db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
6901db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  static NodeType *getEntryNode(Loop *L) { return L; }
6919769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_begin(NodeType *N) {
692329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->begin();
6931db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
6949769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_end(NodeType *N) {
695329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->end();
6961db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
6971db0a400370466e187ae06c96a1586c2c21409ddChris Lattner};
6981db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
699d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
700d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
7010bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner#endif
702