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
30674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_ANALYSIS_LOOPINFO_H
31674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_ANALYSIS_LOOPINFO_H
320bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
3372851059224b90a3ae74ecd62fbd927a0c54a9f0Cameron Zwarich#include "llvm/ADT/DenseMap.h"
345434c1e73b6e56756719d2aebb952ac7bb3829e0Andrew Trick#include "llvm/ADT/DenseSet.h"
35551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/GraphTraits.h"
3636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/ADT/SmallPtrSet.h"
37255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include "llvm/ADT/SmallVector.h"
3836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/CFG.h"
3936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/Instruction.h"
40255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include "llvm/Pass.h"
41019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#include <algorithm>
42019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
431d5562f72e3f7d9e17a2bf95afa54a98dac95894Dan Gohmannamespace llvm {
441d5562f72e3f7d9e17a2bf95afa54a98dac95894Dan Gohman
45019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Andersontemplate<typename T>
46305b515c2787f47adecbe120e4b4bef55c5e5525Chandler Carruthinline void RemoveFromVector(std::vector<T*> &V, T *N) {
47019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N);
48019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  assert(I != V.end() && "N is not in this list!");
49019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  V.erase(I);
50019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson}
510bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
5253c279b1949f7fa626ccbc399ebbe2d7dc9599a4Devang Patelclass DominatorTree;
538fc2f2072de83665ae20e06929e28317f449bcdfChris Lattnerclass LoopInfo;
54c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanclass Loop;
55ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmondclass MDNode;
569fc5cdf77c812aaa80419036de27576d45894d0dChris Lattnerclass PHINode;
57209cb5b56bb90f1ceee570efabe9c04121cb0bebJakub Staszakclass raw_ostream;
5836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinestemplate<class N> class DominatorTreeBase;
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
75887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei  SmallPtrSet<const BlockT*, 8> DenseBlockSet;
76887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei
77de8091708f2d5ade958507aa6d37907a6277e9f2Craig Topper  LoopBase(const LoopBase<BlockT, LoopT> &) LLVM_DELETED_FUNCTION;
78de8091708f2d5ade958507aa6d37907a6277e9f2Craig Topper  const LoopBase<BlockT, LoopT>&
79de8091708f2d5ade958507aa6d37907a6277e9f2Craig Topper    operator=(const LoopBase<BlockT, LoopT> &) LLVM_DELETED_FUNCTION;
800bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattnerpublic:
8146758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// Loop ctor - This creates an empty loop.
82dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  LoopBase() : ParentLoop(nullptr) {}
83019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  ~LoopBase() {
8434cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng    for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
854e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner      delete SubLoops[i];
864e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  }
870bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
88ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// getLoopDepth - Return the nesting level of this loop.  An outer-most
89ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// loop has depth 1, for consistency with loop depth values used for basic
90ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// blocks, where depth 0 is used for blocks not inside any loops.
91072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  unsigned getLoopDepth() const {
92ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman    unsigned D = 1;
93c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    for (const LoopT *CurLoop = ParentLoop; CurLoop;
94019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson         CurLoop = CurLoop->ParentLoop)
95072b163424491c85df6664a4e056aae5e07dc64dChris Lattner      ++D;
96072b163424491c85df6664a4e056aae5e07dc64dChris Lattner    return D;
97072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  }
98019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  BlockT *getHeader() const { return Blocks.front(); }
99c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *getParentLoop() const { return ParentLoop; }
1000bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
10137aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick  /// setParentLoop is a raw interface for bypassing addChildLoop.
10237aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick  void setParentLoop(LoopT *L) { ParentLoop = L; }
10337aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick
10492329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  /// contains - Return true if the specified loop is contained within in
10592329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  /// this loop.
10692329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  ///
10792329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  bool contains(const LoopT *L) const {
10892329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman    if (L == this) return true;
109dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (!L)        return false;
11092329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman    return contains(L->getParentLoop());
11192329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  }
112fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
11392329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  /// contains - Return true if the specified basic block is in this loop.
114fbdf4bf1799bf9ea566fc0fc0507752590a6d559Misha Brukman  ///
115019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  bool contains(const BlockT *BB) const {
116887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei    return DenseBlockSet.count(BB);
117019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1180bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
11992329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  /// contains - Return true if the specified instruction is in this loop.
12092329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  ///
12192329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  template<class InstT>
12292329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  bool contains(const InstT *Inst) const {
12392329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman    return contains(Inst->getParent());
12492329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  }
12592329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman
126329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  /// iterator/begin/end - Return the loops contained entirely within this loop.
1272b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
128c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  const std::vector<LoopT *> &getSubLoops() const { return SubLoops; }
12937aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick  std::vector<LoopT *> &getSubLoopsVector() { return SubLoops; }
130c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  typedef typename std::vector<LoopT *>::const_iterator iterator;
131c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  typedef typename std::vector<LoopT *>::const_reverse_iterator
132c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick    reverse_iterator;
133329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator begin() const { return SubLoops.begin(); }
134329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator end() const { return SubLoops.end(); }
135c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  reverse_iterator rbegin() const { return SubLoops.rbegin(); }
136c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  reverse_iterator rend() const { return SubLoops.rend(); }
13721c276d2fa99914d5ed958ac0aec7d78e3dd87cfDan Gohman  bool empty() const { return SubLoops.empty(); }
138fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner
139fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// getBlocks - Get a list of the basic blocks which make up this loop.
140fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  ///
141019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  const std::vector<BlockT*> &getBlocks() const { 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 {
15564bf55af6f3cc6c6db985d3840547b5869e57222Jakub Staszak    typedef GraphTraits<const BlockT*> BlockTraits;
156d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    for (typename BlockTraits::ChildIteratorType SI =
15764bf55af6f3cc6c6db985d3840547b5869e57222Jakub Staszak         BlockTraits::child_begin(BB),
15864bf55af6f3cc6c6db985d3840547b5869e57222Jakub Staszak         SE = BlockTraits::child_end(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 =
17364bf55af6f3cc6c6db985d3840547b5869e57222Jakub Staszak         InvBlockTraits::child_begin(H),
17464bf55af6f3cc6c6db985d3840547b5869e57222Jakub Staszak         E = InvBlockTraits::child_end(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
23436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// getLoopLatches - Return all loop latch blocks of this loop. A latch block
23536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// is a block that contains a branch back to the header.
23636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void getLoopLatches(SmallVectorImpl<BlockT *> &LoopLatches) const {
23736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    BlockT *H = getHeader();
23836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
23936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    for (typename InvBlockTraits::ChildIteratorType I =
24036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines         InvBlockTraits::child_begin(H),
24136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines         E = InvBlockTraits::child_end(H); I != E; ++I)
24236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      if (contains(*I))
24336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        LoopLatches.push_back(*I);
24436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
24536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
246e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //===--------------------------------------------------------------------===//
247e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // APIs for updating loop information after changing the CFG
248e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //
249e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner
250fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// addBasicBlockToLoop - This method is used by other analyses to update loop
251fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// information.  NewBB is set to be a new member of the current loop.
2522b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// Because of this, it is added as a member of all parent loops, and is added
2532b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// to the specified LoopInfo object as being in the current basic block.  It
2542b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// is not valid to replace the loop header with this method.
2552b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
256c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
2572b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner
25846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// replaceChildLoopWith - This is used when splitting loops up.  It replaces
25946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// the OldChild entry in our children list with NewChild, and updates the
26046758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// parent pointer of OldChild to be null and the NewChild to be this loop.
26146758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// This updates the loop depth of the new child.
262cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild);
26346758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
26446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// addChildLoop - Add the specified loop to be a child of this loop.  This
26546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// updates the loop depth of the new child.
26646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  ///
267c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void addChildLoop(LoopT *NewChild) {
268dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    assert(!NewChild->ParentLoop && "NewChild already has a parent!");
269c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    NewChild->ParentLoop = static_cast<LoopT *>(this);
270019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SubLoops.push_back(NewChild);
271019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
27246758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
27346758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// removeChildLoop - This removes the specified child from being a subloop of
27446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// this loop.  The loop is not deleted, as it will presumably be inserted
27546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// into another loop.
276c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *removeChildLoop(iterator I) {
277019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(I != SubLoops.end() && "Cannot remove end iterator!");
278c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *Child = *I;
279019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(Child->ParentLoop == this && "Child is not a child of this loop!");
280019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SubLoops.erase(SubLoops.begin()+(I-begin()));
281dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    Child->ParentLoop = nullptr;
282019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return Child;
283019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
28446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
28546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// addBlockEntry - This adds a basic block directly to the basic block list.
28646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// This should only be used by transformations that create new loops.  Other
28746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// transformations should use addBasicBlockToLoop.
288019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void addBlockEntry(BlockT *BB) {
28946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner    Blocks.push_back(BB);
290887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei    DenseBlockSet.insert(BB);
291887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei  }
292887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei
293887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei  /// reverseBlocks - interface to reverse Blocks[from, end of loop] in this loop
294887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei  void reverseBlock(unsigned from) {
295887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei    std::reverse(Blocks.begin() + from, Blocks.end());
296887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei  }
297887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei
298887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei  /// reserveBlocks- interface to do reserve() for Blocks
299887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei  void reserveBlocks(unsigned size) {
300887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei    Blocks.reserve(size);
30146758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  }
30246758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
30351a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  /// moveToHeader - This method is used to move BB (which must be part of this
30451a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  /// loop) to be the loop header of the loop (the block that dominates all
30551a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  /// others).
306019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void moveToHeader(BlockT *BB) {
30751a4ad475b095aed49caff176b98c0754e421af4Chris Lattner    if (Blocks[0] == BB) return;
30851a4ad475b095aed49caff176b98c0754e421af4Chris Lattner    for (unsigned i = 0; ; ++i) {
30951a4ad475b095aed49caff176b98c0754e421af4Chris Lattner      assert(i != Blocks.size() && "Loop does not contain BB!");
31051a4ad475b095aed49caff176b98c0754e421af4Chris Lattner      if (Blocks[i] == BB) {
31151a4ad475b095aed49caff176b98c0754e421af4Chris Lattner        Blocks[i] = Blocks[0];
31251a4ad475b095aed49caff176b98c0754e421af4Chris Lattner        Blocks[0] = BB;
31351a4ad475b095aed49caff176b98c0754e421af4Chris Lattner        return;
31451a4ad475b095aed49caff176b98c0754e421af4Chris Lattner      }
31551a4ad475b095aed49caff176b98c0754e421af4Chris Lattner    }
31651a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  }
31751a4ad475b095aed49caff176b98c0754e421af4Chris Lattner
31846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// removeBlockFromLoop - This removes the specified basic block from the
319f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// current loop, updating the Blocks as appropriate.  This does not update
320f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// the mapping in the LoopInfo class.
321019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void removeBlockFromLoop(BlockT *BB) {
322019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    RemoveFromVector(Blocks, BB);
323887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei    DenseBlockSet.erase(BB);
324019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
32546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
32658e0ef1e90c3f6dbae213612b44e56f7d6d65ea7Devang Patel  /// verifyLoop - Verify loop structure
327cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void verifyLoop() const;
328019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
3295c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman  /// verifyLoop - Verify loop structure of this loop and all nested loops.
330cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void verifyLoopNest(DenseSet<const LoopT*> *Loops) const;
33158e0ef1e90c3f6dbae213612b44e56f7d6d65ea7Devang Patel
332cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void print(raw_ostream &OS, unsigned Depth = 0) const;
333dda30cd4af1c5f88fc00fd40b673f8e27c61379dDan Gohman
334c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanprotected:
335c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopInfoBase<BlockT, LoopT>;
336dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  explicit LoopBase(BlockT *BB) : ParentLoop(nullptr) {
337072b163424491c85df6664a4e056aae5e07dc64dChris Lattner    Blocks.push_back(BB);
338887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei    DenseBlockSet.insert(BB);
3390bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
3400bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner};
3410bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
3426a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesentemplate<class BlockT, class LoopT>
3436a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesenraw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
3446a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesen  Loop.print(OS);
3456a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesen  return OS;
3466a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesen}
3476a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesen
34860c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick// Implementation in LoopInfoImpl.h
34960c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick#ifdef __GNUC__
35060c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick__extension__ extern template class LoopBase<BasicBlock, Loop>;
35160c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick#endif
35260c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick
353c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanclass Loop : public LoopBase<BasicBlock, Loop> {
354c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanpublic:
355c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  Loop() {}
35616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
35716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// isLoopInvariant - Return true if the specified value is loop invariant
35816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
35916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  bool isLoopInvariant(Value *V) const;
36016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
361adc799112dc180b3cd099038c05101b85d217716Chris Lattner  /// hasLoopInvariantOperands - Return true if all the operands of the
362fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick  /// specified instruction are loop invariant.
363adc799112dc180b3cd099038c05101b85d217716Chris Lattner  bool hasLoopInvariantOperands(Instruction *I) const;
364a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman
365206613b289f60b71a76e9190d36b9ea9e47a701eDan Gohman  /// makeLoopInvariant - If the given value is an instruction inside of the
366a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// loop and it can be hoisted, do so to make it trivially loop-invariant.
367a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// Return true if the value after any hoisting is loop invariant. This
368a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// function can be used as a slightly more aggressive replacement for
369a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// isLoopInvariant.
370a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
371a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If InsertPt is specified, it is the point to hoist instructions to.
372a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If null, the terminator of the loop preheader is used.
373a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
374bdc017edacb713119b24ab269d250a82d62fffebDan Gohman  bool makeLoopInvariant(Value *V, bool &Changed,
375dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                         Instruction *InsertPt = nullptr) const;
376a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman
377a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// makeLoopInvariant - If the given instruction is inside of the
378a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// loop and it can be hoisted, do so to make it trivially loop-invariant.
379a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// Return true if the instruction after any hoisting is loop invariant. This
380a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// function can be used as a slightly more aggressive replacement for
381a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// isLoopInvariant.
382a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
383a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If InsertPt is specified, it is the point to hoist instructions to.
384a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If null, the terminator of the loop preheader is used.
385a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
386bdc017edacb713119b24ab269d250a82d62fffebDan Gohman  bool makeLoopInvariant(Instruction *I, bool &Changed,
387dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                         Instruction *InsertPt = nullptr) const;
388a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman
38916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// getCanonicalInductionVariable - Check to see if the loop has a canonical
39016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// induction variable: an integer recurrence that starts at 0 and increments
39116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// by one each time through the loop.  If so, return the phi node that
39216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// corresponds to it.
39316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
39416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// The IndVarSimplify pass transforms loops to have a canonical induction
39516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// variable.
39616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
39716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  PHINode *getCanonicalInductionVariable() const;
39816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
39916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// isLCSSAForm - Return true if the Loop is in LCSSA form
400bbf81d88116d23fb0776412b5916f7d0b8b3ca7eDan Gohman  bool isLCSSAForm(DominatorTree &DT) const;
40116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
402937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  /// isLoopSimplifyForm - Return true if the Loop is in the form that
403937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  /// the LoopSimplify form transforms loops to, which is sometimes called
404937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  /// normal form.
405937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  bool isLoopSimplifyForm() const;
406937738649386b8188524d0cd61943214a5b93cf6Dan Gohman
407d9fc1ce8096f7138c60edc3a6655583bf209780eAndrew Trick  /// isSafeToClone - Return true if the loop body is safe to clone in practice.
408d9fc1ce8096f7138c60edc3a6655583bf209780eAndrew Trick  bool isSafeToClone() const;
409d9fc1ce8096f7138c60edc3a6655583bf209780eAndrew Trick
4105d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// Returns true if the loop is annotated parallel.
4115d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  ///
4125d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// A parallel loop can be assumed to not contain any dependencies between
4135d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// iterations by the compiler. That is, any loop-carried dependency checking
4145d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// can be skipped completely when parallelizing the loop on the target
4155d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// machine. Thus, if the parallel loop information originates from the
4165d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// programmer, e.g. via the OpenMP parallel for pragma, it is the
4175d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// programmer's responsibility to ensure there are no loop-carried
4185d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// dependencies. The final execution order of the instructions across
4195d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// iterations is not guaranteed, thus, the end result might or might not
4205d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// implement actual concurrent execution of instructions across multiple
4215d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// iterations.
4225d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  bool isAnnotatedParallel() const;
4235d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen
424ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// Return the llvm.loop loop id metadata node for this loop if it is present.
425ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  ///
426ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// If this loop contains the same llvm.loop metadata on each branch to the
427ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// header then the node is returned. If any latch instruction does not
428ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// contain llvm.loop or or if multiple latches contain different nodes then
429ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// 0 is returned.
430ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  MDNode *getLoopID() const;
431ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// Set the llvm.loop loop id metadata for this loop.
432ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  ///
433ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// The LoopID metadata node will be added to each terminator instruction in
434ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// the loop that branches to the loop header.
435ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  ///
436ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// The LoopID metadata node should have one or more operands and the first
437ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// operand should should be the node itself.
438ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  void setLoopID(MDNode *LoopID) const;
439ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond
440f17e9511f15a0e007ff47d0789d1a52502e8c1fbDan Gohman  /// hasDedicatedExits - Return true if no exit block for the loop
441f17e9511f15a0e007ff47d0789d1a52502e8c1fbDan Gohman  /// has a predecessor that is outside the loop.
442f17e9511f15a0e007ff47d0789d1a52502e8c1fbDan Gohman  bool hasDedicatedExits() const;
443f17e9511f15a0e007ff47d0789d1a52502e8c1fbDan Gohman
444fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick  /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
445f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  /// These are the blocks _outside of the current loop_ which are branched to.
446050959cd08db6c0efb8208271a1d64ce58893e20Dan Gohman  /// This assumes that loop exits are in canonical form.
447f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  ///
448f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  void getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const;
449f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman
450f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
451f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  /// block, return that block. Otherwise return null.
452f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  BasicBlock *getUniqueExitBlock() const;
453f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman
454dda30cd4af1c5f88fc00fd40b673f8e27c61379dDan Gohman  void dump() const;
455fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
456dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// \brief Return the debug location of the start of this loop.
457dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// This looks for a BB terminating instruction with a known debug
458dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// location by looking at the preheader and header blocks. If it
459dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// cannot find a terminating instruction with location information,
460dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// it returns an unknown location.
461dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  DebugLoc getStartLoc() const {
462dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    DebugLoc StartLoc;
463dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    BasicBlock *HeadBB;
464dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
465dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    // Try the pre-header first.
466dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if ((HeadBB = getLoopPreheader()) != nullptr) {
467dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      StartLoc = HeadBB->getTerminator()->getDebugLoc();
468dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      if (!StartLoc.isUnknown())
469dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        return StartLoc;
470dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    }
471dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
472dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    // If we have no pre-header or there are no instructions with debug
473dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    // info in it, try the header.
474dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    HeadBB = getHeader();
475dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (HeadBB)
476dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      StartLoc = HeadBB->getTerminator()->getDebugLoc();
477dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
478dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return StartLoc;
479dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
480dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
481c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanprivate:
482c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopInfoBase<BasicBlock, Loop>;
483c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
484c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman};
4850bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
4860bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//===----------------------------------------------------------------------===//
4872b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner/// LoopInfo - This class builds and contains all of the top level loop
4882b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner/// structures in the specified function.
4892b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner///
49044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
491c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class BlockT, class LoopT>
49244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonclass LoopInfoBase {
4930bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  // BBMap - Mapping of basic blocks to the inner most loop they occur in
49472851059224b90a3ae74ecd62fbd927a0c54a9f0Cameron Zwarich  DenseMap<BlockT *, LoopT *> BBMap;
495c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  std::vector<LoopT *> TopLevelLoops;
496c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopBase<BlockT, LoopT>;
4975434c1e73b6e56756719d2aebb952ac7bb3829e0Andrew Trick  friend class LoopInfo;
4989d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman
499de8091708f2d5ade958507aa6d37907a6277e9f2Craig Topper  void operator=(const LoopInfoBase &) LLVM_DELETED_FUNCTION;
500de8091708f2d5ade958507aa6d37907a6277e9f2Craig Topper  LoopInfoBase(const LoopInfo &) LLVM_DELETED_FUNCTION;
5010bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattnerpublic:
50244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  LoopInfoBase() { }
50344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ~LoopInfoBase() { releaseMemory(); }
504fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
50544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  void releaseMemory() {
506c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    for (typename std::vector<LoopT *>::iterator I =
50744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson         TopLevelLoops.begin(), E = TopLevelLoops.end(); I != E; ++I)
50844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      delete *I;   // Delete all of the loops...
5090bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
510131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner    BBMap.clear();                           // Reset internal state of analysis
51144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    TopLevelLoops.clear();
51244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
513fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
514329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  /// iterator/begin/end - The interface to the top-level loops in the current
515329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  /// function.
516329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  ///
517c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  typedef typename std::vector<LoopT *>::const_iterator iterator;
518c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  typedef typename std::vector<LoopT *>::const_reverse_iterator
519c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick    reverse_iterator;
520329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator begin() const { return TopLevelLoops.begin(); }
521329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator end() const { return TopLevelLoops.end(); }
522c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
523c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  reverse_iterator rend() const { return TopLevelLoops.rend(); }
524a8c763b3071ae1a58ee8baeb282331245527e004Dan Gohman  bool empty() const { return TopLevelLoops.empty(); }
525fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
5262b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
5272b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// block is in no loop (for example the entry node), null is returned.
5282b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
529c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *getLoopFor(const BlockT *BB) const {
530da69f3b357097e75fbf9a5a2bbe1e7273d4b4271Benjamin Kramer    return BBMap.lookup(const_cast<BlockT*>(BB));
5310bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
532fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
5332b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// operator[] - same as getLoopFor...
5342b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
535c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  const LoopT *operator[](const BlockT *BB) const {
5360bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner    return getLoopFor(BB);
5370bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
538fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
539ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// getLoopDepth - Return the loop nesting level of the specified block.  A
540ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// depth of 0 means the block is not inside any loop.
5412b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
54244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  unsigned getLoopDepth(const BlockT *BB) const {
543c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    const LoopT *L = getLoopFor(BB);
5440bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner    return L ? L->getLoopDepth() : 0;
5450bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
5460bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
5470bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  // isLoopHeader - True if the block is a loop header node
54844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  bool isLoopHeader(BlockT *BB) const {
549c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    const LoopT *L = getLoopFor(BB);
55050f5490842d501e269a4c6085d0d132cae0d31f8Chris Lattner    return L && L->getHeader() == BB;
5510bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
552fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
55344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// removeLoop - This removes the specified top-level loop from this loop info
55444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// object.  The loop is not deleted, as it will presumably be inserted into
55544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// another loop.
556c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *removeLoop(iterator I) {
55744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(I != end() && "Cannot remove end iterator!");
558c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *L = *I;
559dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    assert(!L->getParentLoop() && "Not a top-level loop!");
56044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
56144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    return L;
56244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
563fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
56444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// changeLoopFor - Change the top-level loop that contains BB to the
56544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// specified loop.  This should be used by transformations that restructure
56644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// the loop hierarchy tree.
567c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void changeLoopFor(BlockT *BB, LoopT *L) {
568fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick    if (!L) {
569da69f3b357097e75fbf9a5a2bbe1e7273d4b4271Benjamin Kramer      BBMap.erase(BB);
570fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick      return;
571fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick    }
572fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick    BBMap[BB] = L;
57344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
574fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
57544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// changeTopLevelLoop - Replace the specified loop in the top-level loops
57644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// list with the indicated loop.
577c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void changeTopLevelLoop(LoopT *OldLoop,
578c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                          LoopT *NewLoop) {
579c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    typename std::vector<LoopT *>::iterator I =
58044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson                 std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
58144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(I != TopLevelLoops.end() && "Old loop not at top level!");
58244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    *I = NewLoop;
583dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
58444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson           "Loops already embedded into a subloop!");
58544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
586fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
58744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// addTopLevelLoop - This adds the specified loop to the collection of
58844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// top-level loops.
589c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void addTopLevelLoop(LoopT *New) {
590dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    assert(!New->getParentLoop() && "Loop already in subloop!");
59144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    TopLevelLoops.push_back(New);
59244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
593fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
59444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// removeBlock - This method completely removes BB from all data structures,
59544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// including all of the Loop objects it is nested in and our mapping from
59644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// BasicBlocks to loops.
59744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  void removeBlock(BlockT *BB) {
59872851059224b90a3ae74ecd62fbd927a0c54a9f0Cameron Zwarich    typename DenseMap<BlockT *, LoopT *>::iterator I = BBMap.find(BB);
59944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (I != BBMap.end()) {
600c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman      for (LoopT *L = I->second; L; L = L->getParentLoop())
60144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        L->removeBlockFromLoop(BB);
60244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
60344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      BBMap.erase(I);
60444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    }
60544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
606fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
60744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  // Internals
608fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
609c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
610c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                                      const LoopT *ParentLoop) {
611dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (!SubLoop) return true;
61244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (SubLoop == ParentLoop) return false;
61344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
61444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
615fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
61637aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick  /// Create the loop forest using a stable algorithm.
61737aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick  void Analyze(DominatorTreeBase<BlockT> &DomTree);
61837aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick
61944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  // Debugging
620fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
621cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void print(raw_ostream &OS) const;
62244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson};
62344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
62460c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick// Implementation in LoopInfoImpl.h
62560c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick#ifdef __GNUC__
62660c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick__extension__ extern template class LoopInfoBase<BasicBlock, Loop>;
62760c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick#endif
62860c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick
62944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonclass LoopInfo : public FunctionPass {
630c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopInfoBase<BasicBlock, Loop> LI;
631c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopBase<BasicBlock, Loop>;
6329d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman
633de8091708f2d5ade958507aa6d37907a6277e9f2Craig Topper  void operator=(const LoopInfo &) LLVM_DELETED_FUNCTION;
634de8091708f2d5ade958507aa6d37907a6277e9f2Craig Topper  LoopInfo(const LoopInfo &) LLVM_DELETED_FUNCTION;
63544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonpublic:
63644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  static char ID; // Pass identification, replacement for typeid
63744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
638081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson  LoopInfo() : FunctionPass(ID) {
639081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson    initializeLoopInfoPass(*PassRegistry::getPassRegistry());
640081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson  }
64144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
642c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopInfoBase<BasicBlock, Loop>& getBase() { return LI; }
643d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson
64444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// iterator/begin/end - The interface to the top-level loops in the current
64544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// function.
64644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
647c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  typedef LoopInfoBase<BasicBlock, Loop>::iterator iterator;
648c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  typedef LoopInfoBase<BasicBlock, Loop>::reverse_iterator reverse_iterator;
6499d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  inline iterator begin() const { return LI.begin(); }
6509d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  inline iterator end() const { return LI.end(); }
651c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  inline reverse_iterator rbegin() const { return LI.rbegin(); }
652c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  inline reverse_iterator rend() const { return LI.rend(); }
6539d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  bool empty() const { return LI.empty(); }
65444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
65544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
65644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// block is in no loop (for example the entry node), null is returned.
65744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
65844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline Loop *getLoopFor(const BasicBlock *BB) const {
6599d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.getLoopFor(BB);
66044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
66144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
66244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// operator[] - same as getLoopFor...
66344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
66444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline const Loop *operator[](const BasicBlock *BB) const {
6659d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.getLoopFor(BB);
66644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
66744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
668ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// getLoopDepth - Return the loop nesting level of the specified block.  A
669ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// depth of 0 means the block is not inside any loop.
67044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ///
67144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline unsigned getLoopDepth(const BasicBlock *BB) const {
6729d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.getLoopDepth(BB);
67344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
67444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
67544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  // isLoopHeader - True if the block is a loop header node
67644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline bool isLoopHeader(BasicBlock *BB) const {
6779d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    return LI.isLoopHeader(BB);
67844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
6790bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
6802b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// runOnFunction - Calculate the natural loop information.
6812b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
68236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  bool runOnFunction(Function &F) override;
683facd752d3afaeca7dee46648f2a2ae209a94e5e9Chris Lattner
68436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void verifyAnalysis() const override;
6855c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman
68636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void releaseMemory() override { LI.releaseMemory(); }
6875c7e326585f3a543388ba871c3425f7664cd9143Bill Wendling
688dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  void print(raw_ostream &O, const Module* M = nullptr) const override;
689fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
69036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void getAnalysisUsage(AnalysisUsage &AU) const override;
691f57b845547302d24ecb6a9e79d7bc386f761a6c9Chris Lattner
6924e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  /// removeLoop - This removes the specified top-level loop from this loop info
6934e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  /// object.  The loop is not deleted, as it will presumably be inserted into
6944e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  /// another loop.
6959d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  inline Loop *removeLoop(iterator I) { return LI.removeLoop(I); }
6964e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner
69746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// changeLoopFor - Change the top-level loop that contains BB to the
69846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// specified loop.  This should be used by transformations that restructure
69946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// the loop hierarchy tree.
70044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline void changeLoopFor(BasicBlock *BB, Loop *L) {
7019d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.changeLoopFor(BB, L);
70244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
70346758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
70446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// changeTopLevelLoop - Replace the specified loop in the top-level loops
70546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// list with the indicated loop.
70644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop) {
7079d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.changeTopLevelLoop(OldLoop, NewLoop);
70844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
70946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
710072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  /// addTopLevelLoop - This adds the specified loop to the collection of
711072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  /// top-level loops.
71244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  inline void addTopLevelLoop(Loop *New) {
7139d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.addTopLevelLoop(New);
714072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  }
715072b163424491c85df6664a4e056aae5e07dc64dChris Lattner
7169afb24bf0847b9f2ff0bf3f7f7405dcbe42fa38bChris Lattner  /// removeBlock - This method completely removes BB from all data structures,
7179afb24bf0847b9f2ff0bf3f7f7405dcbe42fa38bChris Lattner  /// including all of the Loop objects it is nested in and our mapping from
7189afb24bf0847b9f2ff0bf3f7f7405dcbe42fa38bChris Lattner  /// BasicBlocks to loops.
71944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  void removeBlock(BasicBlock *BB) {
7209d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman    LI.removeBlock(BB);
72144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
722d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands
723fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick  /// updateUnloop - Update LoopInfo after removing the last backedge from a
724fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick  /// loop--now the "unloop". This updates the loop forest and parent loops for
725fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick  /// each block so that Unloop is no longer referenced, but the caller must
726fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick  /// actually delete the Unloop object.
727fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick  void updateUnloop(Loop *Unloop);
728fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick
729d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands  /// replacementPreservesLCSSAForm - Returns true if replacing From with To
730d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands  /// everywhere is guaranteed to preserve LCSSA form.
731d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands  bool replacementPreservesLCSSAForm(Instruction *From, Value *To) {
732d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // Preserving LCSSA form is only problematic if the replacing value is an
733d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // instruction.
734d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    Instruction *I = dyn_cast<Instruction>(To);
735d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    if (!I) return true;
7369bc6a90146417af03144fa2f7dd94f9945b57c06Duncan Sands    // If both instructions are defined in the same basic block then replacement
7379bc6a90146417af03144fa2f7dd94f9945b57c06Duncan Sands    // cannot break LCSSA form.
7389bc6a90146417af03144fa2f7dd94f9945b57c06Duncan Sands    if (I->getParent() == From->getParent())
7399bc6a90146417af03144fa2f7dd94f9945b57c06Duncan Sands      return true;
740d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // If the instruction is not defined in a loop then it can safely replace
741d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // anything.
742d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    Loop *ToLoop = getLoopFor(I->getParent());
743d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    if (!ToLoop) return true;
744d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // If the replacing instruction is defined in the same loop as the original
745d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // instruction, or in a loop that contains it as an inner loop, then using
746d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // it as a replacement will not break LCSSA form.
7473b0aed19f0f9ed62362c04f7e18e2ada686f4055Duncan Sands    return ToLoop->contains(getLoopFor(From->getParent()));
748d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands  }
7490bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner};
7500bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
751e0b6b78e095f7dea9589e8df5ec4521e346ad005Anand Shukla
7521db0a400370466e187ae06c96a1586c2c21409ddChris Lattner// Allow clients to walk the list of nested loops...
7531db0a400370466e187ae06c96a1586c2c21409ddChris Lattnertemplate <> struct GraphTraits<const Loop*> {
7541db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  typedef const Loop NodeType;
7559d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  typedef LoopInfo::iterator ChildIteratorType;
7561db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
7571db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  static NodeType *getEntryNode(const Loop *L) { return L; }
7589769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_begin(NodeType *N) {
759329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->begin();
7601db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
7619769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_end(NodeType *N) {
762329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->end();
7631db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
7641db0a400370466e187ae06c96a1586c2c21409ddChris Lattner};
7651db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
7661db0a400370466e187ae06c96a1586c2c21409ddChris Lattnertemplate <> struct GraphTraits<Loop*> {
7671db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  typedef Loop NodeType;
7689d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  typedef LoopInfo::iterator ChildIteratorType;
7691db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
7701db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  static NodeType *getEntryNode(Loop *L) { return L; }
7719769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_begin(NodeType *N) {
772329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->begin();
7731db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
7749769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_end(NodeType *N) {
775329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->end();
7761db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
7771db0a400370466e187ae06c96a1586c2c21409ddChris Lattner};
7781db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
779d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
780d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
7810bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner#endif
782