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
45ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines// FIXME: Replace this brittle forward declaration with the include of the new
46ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines// PassManager.h when doing so doesn't break the PassManagerBuilder.
47ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinestemplate <typename IRUnitT> class AnalysisManager;
48ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesclass PreservedAnalyses;
49ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
50019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Andersontemplate<typename T>
51305b515c2787f47adecbe120e4b4bef55c5e5525Chandler Carruthinline void RemoveFromVector(std::vector<T*> &V, T *N) {
52019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N);
53019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  assert(I != V.end() && "N is not in this list!");
54019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  V.erase(I);
55019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson}
560bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
5753c279b1949f7fa626ccbc399ebbe2d7dc9599a4Devang Patelclass DominatorTree;
588fc2f2072de83665ae20e06929e28317f449bcdfChris Lattnerclass LoopInfo;
59c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanclass Loop;
60ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmondclass MDNode;
619fc5cdf77c812aaa80419036de27576d45894d0dChris Lattnerclass PHINode;
62209cb5b56bb90f1ceee570efabe9c04121cb0bebJakub Staszakclass raw_ostream;
6336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinestemplate<class N> class DominatorTreeBase;
64c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class N, class M> class LoopInfoBase;
65c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class N, class M> class LoopBase;
660bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
670bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//===----------------------------------------------------------------------===//
68131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner/// LoopBase class - Instances of this class are used to represent loops that
69131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner/// are detected in the flow graph
702b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner///
71c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class BlockT, class LoopT>
72019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Andersonclass LoopBase {
73c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *ParentLoop;
74131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  // SubLoops - Loops contained entirely within this one.
75c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  std::vector<LoopT *> SubLoops;
76131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner
77131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  // Blocks - The list of blocks in this loop.  First entry is the header node.
78131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner  std::vector<BlockT*> Blocks;
79019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
80887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei  SmallPtrSet<const BlockT*, 8> DenseBlockSet;
81887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei
82ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LoopBase(const LoopBase<BlockT, LoopT> &) = delete;
83de8091708f2d5ade958507aa6d37907a6277e9f2Craig Topper  const LoopBase<BlockT, LoopT>&
84ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    operator=(const LoopBase<BlockT, LoopT> &) = delete;
850bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattnerpublic:
8646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// Loop ctor - This creates an empty loop.
87dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  LoopBase() : ParentLoop(nullptr) {}
88019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  ~LoopBase() {
8934cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng    for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
904e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner      delete SubLoops[i];
914e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner  }
920bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
93ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// getLoopDepth - Return the nesting level of this loop.  An outer-most
94ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// loop has depth 1, for consistency with loop depth values used for basic
95ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// blocks, where depth 0 is used for blocks not inside any loops.
96072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  unsigned getLoopDepth() const {
97ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman    unsigned D = 1;
98c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    for (const LoopT *CurLoop = ParentLoop; CurLoop;
99019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson         CurLoop = CurLoop->ParentLoop)
100072b163424491c85df6664a4e056aae5e07dc64dChris Lattner      ++D;
101072b163424491c85df6664a4e056aae5e07dc64dChris Lattner    return D;
102072b163424491c85df6664a4e056aae5e07dc64dChris Lattner  }
103019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  BlockT *getHeader() const { return Blocks.front(); }
104c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *getParentLoop() const { return ParentLoop; }
1050bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
10637aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick  /// setParentLoop is a raw interface for bypassing addChildLoop.
10737aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick  void setParentLoop(LoopT *L) { ParentLoop = L; }
10837aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick
10992329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  /// contains - Return true if the specified loop is contained within in
11092329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  /// this loop.
11192329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  ///
11292329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  bool contains(const LoopT *L) const {
11392329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman    if (L == this) return true;
114dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (!L)        return false;
11592329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman    return contains(L->getParentLoop());
11692329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  }
117fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
11892329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  /// contains - Return true if the specified basic block is in this loop.
119fbdf4bf1799bf9ea566fc0fc0507752590a6d559Misha Brukman  ///
120019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  bool contains(const BlockT *BB) const {
121887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei    return DenseBlockSet.count(BB);
122019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1230bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
12492329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  /// contains - Return true if the specified instruction is in this loop.
12592329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  ///
12692329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  template<class InstT>
12792329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  bool contains(const InstT *Inst) const {
12892329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman    return contains(Inst->getParent());
12992329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman  }
13092329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman
131329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  /// iterator/begin/end - Return the loops contained entirely within this loop.
1322b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
133c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  const std::vector<LoopT *> &getSubLoops() const { return SubLoops; }
13437aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick  std::vector<LoopT *> &getSubLoopsVector() { return SubLoops; }
135c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  typedef typename std::vector<LoopT *>::const_iterator iterator;
136c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  typedef typename std::vector<LoopT *>::const_reverse_iterator
137c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick    reverse_iterator;
138329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator begin() const { return SubLoops.begin(); }
139329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator end() const { return SubLoops.end(); }
140c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  reverse_iterator rbegin() const { return SubLoops.rbegin(); }
141c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  reverse_iterator rend() const { return SubLoops.rend(); }
14221c276d2fa99914d5ed958ac0aec7d78e3dd87cfDan Gohman  bool empty() const { return SubLoops.empty(); }
143fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner
144fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// getBlocks - Get a list of the basic blocks which make up this loop.
145fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  ///
146019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  const std::vector<BlockT*> &getBlocks() const { return Blocks; }
147019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  typedef typename std::vector<BlockT*>::const_iterator block_iterator;
1484e77cc46ac688e7bee98747049f90e19e2902227Chris Lattner  block_iterator block_begin() const { return Blocks.begin(); }
1494e77cc46ac688e7bee98747049f90e19e2902227Chris Lattner  block_iterator block_end() const { return Blocks.end(); }
150fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner
151306afdf443a0e2d5d89c1a3ece00cfe18be1c4c4Andrew Trick  /// getNumBlocks - Get the number of blocks in this loop in constant time.
152b1eede12818d91a32adac928c6fffcf6d2800dc0Andrew Trick  unsigned getNumBlocks() const {
153306afdf443a0e2d5d89c1a3ece00cfe18be1c4c4Andrew Trick    return Blocks.size();
154b1eede12818d91a32adac928c6fffcf6d2800dc0Andrew Trick  }
155b1eede12818d91a32adac928c6fffcf6d2800dc0Andrew Trick
156d146e986c818165cca866ee05751451706ccf36aDan Gohman  /// isLoopExiting - True if terminator in the block can branch to another
157d146e986c818165cca866ee05751451706ccf36aDan Gohman  /// block that is outside of the current loop.
158fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  ///
15932663b719b4996b3a735f22bba80d771d50f96e7Dan Gohman  bool isLoopExiting(const BlockT *BB) const {
16064bf55af6f3cc6c6db985d3840547b5869e57222Jakub Staszak    typedef GraphTraits<const BlockT*> BlockTraits;
161d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    for (typename BlockTraits::ChildIteratorType SI =
16264bf55af6f3cc6c6db985d3840547b5869e57222Jakub Staszak         BlockTraits::child_begin(BB),
16364bf55af6f3cc6c6db985d3840547b5869e57222Jakub Staszak         SE = BlockTraits::child_end(BB); SI != SE; ++SI) {
164019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      if (!contains(*SI))
165019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        return true;
166019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    }
167019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return false;
168019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1696b290a54409f6bb6a0cc1c0446cd2b170a4b7addMisha Brukman
170fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// getNumBackEdges - Calculate the number of back edges to the loop header
171fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  ///
172019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  unsigned getNumBackEdges() const {
173019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    unsigned NumBackEdges = 0;
174019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    BlockT *H = getHeader();
175019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
176d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
177d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson    for (typename InvBlockTraits::ChildIteratorType I =
17864bf55af6f3cc6c6db985d3840547b5869e57222Jakub Staszak         InvBlockTraits::child_begin(H),
17964bf55af6f3cc6c6db985d3840547b5869e57222Jakub Staszak         E = InvBlockTraits::child_end(H); I != E; ++I)
180019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson      if (contains(*I))
181019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson        ++NumBackEdges;
182019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
183019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return NumBackEdges;
184019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
1859474dd68e8a050193ca4003940ac399e2b17cb6aChris Lattner
186e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //===--------------------------------------------------------------------===//
187e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // APIs for simple analysis of the loop.
188e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //
189e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // Note that all of these methods can fail on general loops (ie, there may not
190e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // be a preheader, etc).  For best success, the loop simplification and
191e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // induction variable canonicalization pass should be used to normalize loops
192e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // for easy analysis.  These methods assume canonical loops.
193e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner
1947466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  /// getExitingBlocks - Return all blocks inside the loop that have successors
1957466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  /// outside of the loop.  These are the blocks _inside of the current loop_
1967466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  /// which branch out.  The returned list is always unique.
1977466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner  ///
198cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const;
1997466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner
200c83324682f3409c15dad992cd62928426c9ad83dDan Gohman  /// getExitingBlock - If getExitingBlocks would return exactly one block,
201c83324682f3409c15dad992cd62928426c9ad83dDan Gohman  /// return that block. Otherwise return null.
202cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  BlockT *getExitingBlock() const;
203c83324682f3409c15dad992cd62928426c9ad83dDan Gohman
204f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// getExitBlocks - Return all of the successor blocks of this loop.  These
205f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// are the blocks _outside of the current loop_ which are branched to.
206f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  ///
207cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const;
208f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner
2091827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman  /// getExitBlock - If getExitBlocks would return exactly one block,
2101827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman  /// return that block. Otherwise return null.
211cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  BlockT *getExitBlock() const;
2121827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman
21360f422f894ae9aff2f508f34733be36f5a0ed20aLang Hames  /// Edge type.
214cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  typedef std::pair<const BlockT*, const BlockT*> Edge;
21560f422f894ae9aff2f508f34733be36f5a0ed20aLang Hames
21655e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar  /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
217cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const;
21855e354ac0e294bde258420f80a2cc11ea19db482Daniel Dunbar
2192b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// getLoopPreheader - If there is a preheader for this loop, return it.  A
2202b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// loop has a preheader if there is only one edge to the header of the loop
2212b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// from outside of the loop.  If this is the case, the block branching to the
222e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  /// header of the loop is the preheader node.
2232b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
224e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  /// This method returns null if there is no preheader for the loop.
2252b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
226cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  BlockT *getLoopPreheader() const;
2275cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman
2285cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman  /// getLoopPredecessor - If the given loop's header has exactly one unique
2295cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman  /// predecessor outside the loop, return it. Otherwise return null.
2305cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman  /// This is less strict that the loop "preheader" concept, which requires
2315cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman  /// the predecessor to have exactly one successor.
2325cf27f81f49b4516b1e9d269f070c88fa3228f9eDan Gohman  ///
233cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  BlockT *getLoopPredecessor() const;
2342b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner
235b317143ba851ce2853fb262fb2185ef5f1be030dDan Gohman  /// getLoopLatch - If there is a single latch block for this loop, return it.
236b317143ba851ce2853fb262fb2185ef5f1be030dDan Gohman  /// A latch block is a block that contains a branch back to the header.
237cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  BlockT *getLoopLatch() const;
238e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner
23936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// getLoopLatches - Return all loop latch blocks of this loop. A latch block
24036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// is a block that contains a branch back to the header.
24136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void getLoopLatches(SmallVectorImpl<BlockT *> &LoopLatches) const {
24236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    BlockT *H = getHeader();
24336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
24436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    for (typename InvBlockTraits::ChildIteratorType I =
24536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines         InvBlockTraits::child_begin(H),
24636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines         E = InvBlockTraits::child_end(H); I != E; ++I)
24736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      if (contains(*I))
24836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        LoopLatches.push_back(*I);
24936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
25036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
251e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //===--------------------------------------------------------------------===//
252e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  // APIs for updating loop information after changing the CFG
253e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner  //
254e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner
255fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// addBasicBlockToLoop - This method is used by other analyses to update loop
256fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner  /// information.  NewBB is set to be a new member of the current loop.
2572b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// Because of this, it is added as a member of all parent loops, and is added
2582b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// to the specified LoopInfo object as being in the current basic block.  It
2592b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// is not valid to replace the loop header with this method.
2602b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
261c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
2622b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner
26346758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// replaceChildLoopWith - This is used when splitting loops up.  It replaces
26446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// the OldChild entry in our children list with NewChild, and updates the
26546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// parent pointer of OldChild to be null and the NewChild to be this loop.
26646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// This updates the loop depth of the new child.
267cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild);
26846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
26946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// addChildLoop - Add the specified loop to be a child of this loop.  This
27046758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// updates the loop depth of the new child.
27146758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  ///
272c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void addChildLoop(LoopT *NewChild) {
273dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    assert(!NewChild->ParentLoop && "NewChild already has a parent!");
274c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    NewChild->ParentLoop = static_cast<LoopT *>(this);
275019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SubLoops.push_back(NewChild);
276019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
27746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
27846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// removeChildLoop - This removes the specified child from being a subloop of
27946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// this loop.  The loop is not deleted, as it will presumably be inserted
28046758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// into another loop.
281c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *removeChildLoop(iterator I) {
282019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(I != SubLoops.end() && "Cannot remove end iterator!");
283c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *Child = *I;
284019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    assert(Child->ParentLoop == this && "Child is not a child of this loop!");
285019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    SubLoops.erase(SubLoops.begin()+(I-begin()));
286dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    Child->ParentLoop = nullptr;
287019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    return Child;
288019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
28946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
29046758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// addBlockEntry - This adds a basic block directly to the basic block list.
29146758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// This should only be used by transformations that create new loops.  Other
29246758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// transformations should use addBasicBlockToLoop.
293019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void addBlockEntry(BlockT *BB) {
29446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner    Blocks.push_back(BB);
295887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei    DenseBlockSet.insert(BB);
296887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei  }
297887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei
298887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei  /// reverseBlocks - interface to reverse Blocks[from, end of loop] in this loop
299887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei  void reverseBlock(unsigned from) {
300887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei    std::reverse(Blocks.begin() + from, Blocks.end());
301887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei  }
302887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei
303887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei  /// reserveBlocks- interface to do reserve() for Blocks
304887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei  void reserveBlocks(unsigned size) {
305887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei    Blocks.reserve(size);
30646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  }
30746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
30851a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  /// moveToHeader - This method is used to move BB (which must be part of this
30951a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  /// loop) to be the loop header of the loop (the block that dominates all
31051a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  /// others).
311019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void moveToHeader(BlockT *BB) {
31251a4ad475b095aed49caff176b98c0754e421af4Chris Lattner    if (Blocks[0] == BB) return;
31351a4ad475b095aed49caff176b98c0754e421af4Chris Lattner    for (unsigned i = 0; ; ++i) {
31451a4ad475b095aed49caff176b98c0754e421af4Chris Lattner      assert(i != Blocks.size() && "Loop does not contain BB!");
31551a4ad475b095aed49caff176b98c0754e421af4Chris Lattner      if (Blocks[i] == BB) {
31651a4ad475b095aed49caff176b98c0754e421af4Chris Lattner        Blocks[i] = Blocks[0];
31751a4ad475b095aed49caff176b98c0754e421af4Chris Lattner        Blocks[0] = BB;
31851a4ad475b095aed49caff176b98c0754e421af4Chris Lattner        return;
31951a4ad475b095aed49caff176b98c0754e421af4Chris Lattner      }
32051a4ad475b095aed49caff176b98c0754e421af4Chris Lattner    }
32151a4ad475b095aed49caff176b98c0754e421af4Chris Lattner  }
32251a4ad475b095aed49caff176b98c0754e421af4Chris Lattner
32346758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner  /// removeBlockFromLoop - This removes the specified basic block from the
324f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// current loop, updating the Blocks as appropriate.  This does not update
325f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner  /// the mapping in the LoopInfo class.
326019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  void removeBlockFromLoop(BlockT *BB) {
327019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson    RemoveFromVector(Blocks, BB);
328887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei    DenseBlockSet.erase(BB);
329019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson  }
33046758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner
33158e0ef1e90c3f6dbae213612b44e56f7d6d65ea7Devang Patel  /// verifyLoop - Verify loop structure
332cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void verifyLoop() const;
333019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson
3345c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman  /// verifyLoop - Verify loop structure of this loop and all nested loops.
335cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void verifyLoopNest(DenseSet<const LoopT*> *Loops) const;
33658e0ef1e90c3f6dbae213612b44e56f7d6d65ea7Devang Patel
337cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void print(raw_ostream &OS, unsigned Depth = 0) const;
338dda30cd4af1c5f88fc00fd40b673f8e27c61379dDan Gohman
339c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanprotected:
340c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopInfoBase<BlockT, LoopT>;
341dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  explicit LoopBase(BlockT *BB) : ParentLoop(nullptr) {
342072b163424491c85df6664a4e056aae5e07dc64dChris Lattner    Blocks.push_back(BB);
343887f9c5ec15582aec34aa6c28955d01e4e9961e2Wan Xiaofei    DenseBlockSet.insert(BB);
3440bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
3450bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner};
3460bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
3476a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesentemplate<class BlockT, class LoopT>
3486a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesenraw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
3496a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesen  Loop.print(OS);
3506a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesen  return OS;
3516a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesen}
3526a0dc079efe7acf7e71cc4c0948fe814f35ba091Jakob Stoklund Olesen
35360c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick// Implementation in LoopInfoImpl.h
35460c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick#ifdef __GNUC__
35560c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick__extension__ extern template class LoopBase<BasicBlock, Loop>;
35660c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick#endif
35760c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick
358c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanclass Loop : public LoopBase<BasicBlock, Loop> {
359c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanpublic:
360c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  Loop() {}
36116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
36216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// isLoopInvariant - Return true if the specified value is loop invariant
36316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
36416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  bool isLoopInvariant(Value *V) const;
36516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
366adc799112dc180b3cd099038c05101b85d217716Chris Lattner  /// hasLoopInvariantOperands - Return true if all the operands of the
367fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick  /// specified instruction are loop invariant.
368adc799112dc180b3cd099038c05101b85d217716Chris Lattner  bool hasLoopInvariantOperands(Instruction *I) const;
369a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman
370206613b289f60b71a76e9190d36b9ea9e47a701eDan Gohman  /// makeLoopInvariant - If the given value is an instruction inside of the
371a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// loop and it can be hoisted, do so to make it trivially loop-invariant.
372a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// Return true if the value after any hoisting is loop invariant. This
373a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// function can be used as a slightly more aggressive replacement for
374a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// isLoopInvariant.
375a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
376a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If InsertPt is specified, it is the point to hoist instructions to.
377a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If null, the terminator of the loop preheader is used.
378a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
379bdc017edacb713119b24ab269d250a82d62fffebDan Gohman  bool makeLoopInvariant(Value *V, bool &Changed,
380dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                         Instruction *InsertPt = nullptr) const;
381a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman
382a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// makeLoopInvariant - If the given instruction is inside of the
383a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// loop and it can be hoisted, do so to make it trivially loop-invariant.
384a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// Return true if the instruction after any hoisting is loop invariant. This
385a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// function can be used as a slightly more aggressive replacement for
386a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// isLoopInvariant.
387a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
388a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If InsertPt is specified, it is the point to hoist instructions to.
389a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  /// If null, the terminator of the loop preheader is used.
390a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman  ///
391bdc017edacb713119b24ab269d250a82d62fffebDan Gohman  bool makeLoopInvariant(Instruction *I, bool &Changed,
392dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                         Instruction *InsertPt = nullptr) const;
393a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman
39416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// getCanonicalInductionVariable - Check to see if the loop has a canonical
39516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// induction variable: an integer recurrence that starts at 0 and increments
39616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// by one each time through the loop.  If so, return the phi node that
39716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// corresponds to it.
39816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
39916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// The IndVarSimplify pass transforms loops to have a canonical induction
40016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// variable.
40116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  ///
40216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  PHINode *getCanonicalInductionVariable() const;
40316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
40416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman  /// isLCSSAForm - Return true if the Loop is in LCSSA form
405bbf81d88116d23fb0776412b5916f7d0b8b3ca7eDan Gohman  bool isLCSSAForm(DominatorTree &DT) const;
40616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman
407937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  /// isLoopSimplifyForm - Return true if the Loop is in the form that
408937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  /// the LoopSimplify form transforms loops to, which is sometimes called
409937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  /// normal form.
410937738649386b8188524d0cd61943214a5b93cf6Dan Gohman  bool isLoopSimplifyForm() const;
411937738649386b8188524d0cd61943214a5b93cf6Dan Gohman
412d9fc1ce8096f7138c60edc3a6655583bf209780eAndrew Trick  /// isSafeToClone - Return true if the loop body is safe to clone in practice.
413d9fc1ce8096f7138c60edc3a6655583bf209780eAndrew Trick  bool isSafeToClone() const;
414d9fc1ce8096f7138c60edc3a6655583bf209780eAndrew Trick
4155d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// Returns true if the loop is annotated parallel.
4165d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  ///
4175d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// A parallel loop can be assumed to not contain any dependencies between
4185d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// iterations by the compiler. That is, any loop-carried dependency checking
4195d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// can be skipped completely when parallelizing the loop on the target
4205d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// machine. Thus, if the parallel loop information originates from the
4215d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// programmer, e.g. via the OpenMP parallel for pragma, it is the
4225d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// programmer's responsibility to ensure there are no loop-carried
4235d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// dependencies. The final execution order of the instructions across
4245d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// iterations is not guaranteed, thus, the end result might or might not
4255d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// implement actual concurrent execution of instructions across multiple
4265d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  /// iterations.
4275d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen  bool isAnnotatedParallel() const;
4285d0ce79e26f40141f35cc0002dc5cc6060382359Pekka Jaaskelainen
429ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// Return the llvm.loop loop id metadata node for this loop if it is present.
430ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  ///
431ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// If this loop contains the same llvm.loop metadata on each branch to the
432ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// header then the node is returned. If any latch instruction does not
433ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// contain llvm.loop or or if multiple latches contain different nodes then
434ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// 0 is returned.
435ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  MDNode *getLoopID() const;
436ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// Set the llvm.loop loop id metadata for this loop.
437ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  ///
438ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// The LoopID metadata node will be added to each terminator instruction in
439ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// the loop that branches to the loop header.
440ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  ///
441ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// The LoopID metadata node should have one or more operands and the first
442ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  /// operand should should be the node itself.
443ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond  void setLoopID(MDNode *LoopID) const;
444ee21b6f7b41e3fc19031f6d410b2ebe6a1a2f361Paul Redmond
445f17e9511f15a0e007ff47d0789d1a52502e8c1fbDan Gohman  /// hasDedicatedExits - Return true if no exit block for the loop
446f17e9511f15a0e007ff47d0789d1a52502e8c1fbDan Gohman  /// has a predecessor that is outside the loop.
447f17e9511f15a0e007ff47d0789d1a52502e8c1fbDan Gohman  bool hasDedicatedExits() const;
448f17e9511f15a0e007ff47d0789d1a52502e8c1fbDan Gohman
449fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick  /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
450f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  /// These are the blocks _outside of the current loop_ which are branched to.
451050959cd08db6c0efb8208271a1d64ce58893e20Dan Gohman  /// This assumes that loop exits are in canonical form.
452f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  ///
453f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  void getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const;
454f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman
455f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
456f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  /// block, return that block. Otherwise return null.
457f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman  BasicBlock *getUniqueExitBlock() const;
458f0608d829a7b8929108ac6718bd866adf710e936Dan Gohman
459dda30cd4af1c5f88fc00fd40b673f8e27c61379dDan Gohman  void dump() const;
460fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
461dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// \brief Return the debug location of the start of this loop.
462dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// This looks for a BB terminating instruction with a known debug
463dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// location by looking at the preheader and header blocks. If it
464dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// cannot find a terminating instruction with location information,
465dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// it returns an unknown location.
466dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  DebugLoc getStartLoc() const {
467dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    BasicBlock *HeadBB;
468dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
469dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    // Try the pre-header first.
4702c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar    if ((HeadBB = getLoopPreheader()) != nullptr)
4712c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar      if (DebugLoc DL = HeadBB->getTerminator()->getDebugLoc())
4722c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar        return DL;
473dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
474dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    // If we have no pre-header or there are no instructions with debug
475dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    // info in it, try the header.
476dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    HeadBB = getHeader();
477dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (HeadBB)
4782c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar      return HeadBB->getTerminator()->getDebugLoc();
479dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
4802c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar    return DebugLoc();
481dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
482dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
483c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanprivate:
484c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopInfoBase<BasicBlock, Loop>;
485c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
486c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman};
4870bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
4880bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//===----------------------------------------------------------------------===//
4892b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner/// LoopInfo - This class builds and contains all of the top level loop
4902b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner/// structures in the specified function.
4912b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner///
49244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
493c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class BlockT, class LoopT>
49444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonclass LoopInfoBase {
4950bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  // BBMap - Mapping of basic blocks to the inner most loop they occur in
49672851059224b90a3ae74ecd62fbd927a0c54a9f0Cameron Zwarich  DenseMap<BlockT *, LoopT *> BBMap;
497c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  std::vector<LoopT *> TopLevelLoops;
498c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopBase<BlockT, LoopT>;
4995434c1e73b6e56756719d2aebb952ac7bb3829e0Andrew Trick  friend class LoopInfo;
5009d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman
501ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  void operator=(const LoopInfoBase &) = delete;
502ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LoopInfoBase(const LoopInfoBase &) = delete;
5030bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattnerpublic:
50444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  LoopInfoBase() { }
50544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  ~LoopInfoBase() { releaseMemory(); }
506fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
507ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LoopInfoBase(LoopInfoBase &&Arg)
508ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      : BBMap(std::move(Arg.BBMap)),
509ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines        TopLevelLoops(std::move(Arg.TopLevelLoops)) {
510ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    // We have to clear the arguments top level loops as we've taken ownership.
511ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    Arg.TopLevelLoops.clear();
512ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  }
513ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LoopInfoBase &operator=(LoopInfoBase &&RHS) {
514ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    BBMap = std::move(RHS.BBMap);
515ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
516ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    for (auto *L : TopLevelLoops)
517ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      delete L;
518ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    TopLevelLoops = std::move(RHS.TopLevelLoops);
519ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    RHS.TopLevelLoops.clear();
520ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return *this;
521ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  }
522ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
52344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  void releaseMemory() {
524ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    BBMap.clear();
5250bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
526ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    for (auto *L : TopLevelLoops)
527ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      delete L;
52844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    TopLevelLoops.clear();
52944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
530fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
531329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  /// iterator/begin/end - The interface to the top-level loops in the current
532329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  /// function.
533329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  ///
534c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  typedef typename std::vector<LoopT *>::const_iterator iterator;
535c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  typedef typename std::vector<LoopT *>::const_reverse_iterator
536c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick    reverse_iterator;
537329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator begin() const { return TopLevelLoops.begin(); }
538329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner  iterator end() const { return TopLevelLoops.end(); }
539c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
540c9b1e25493b393013b28e5d457f2fb2845a4dd9fAndrew Trick  reverse_iterator rend() const { return TopLevelLoops.rend(); }
541a8c763b3071ae1a58ee8baeb282331245527e004Dan Gohman  bool empty() const { return TopLevelLoops.empty(); }
542fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
5432b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
5442b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// block is in no loop (for example the entry node), null is returned.
5452b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
546c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *getLoopFor(const BlockT *BB) const {
547da69f3b357097e75fbf9a5a2bbe1e7273d4b4271Benjamin Kramer    return BBMap.lookup(const_cast<BlockT*>(BB));
5480bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
549fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
5502b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  /// operator[] - same as getLoopFor...
5512b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
552c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  const LoopT *operator[](const BlockT *BB) const {
5530bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner    return getLoopFor(BB);
5540bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
555fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
556ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// getLoopDepth - Return the loop nesting level of the specified block.  A
557ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman  /// depth of 0 means the block is not inside any loop.
5582b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner  ///
55944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  unsigned getLoopDepth(const BlockT *BB) const {
560c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    const LoopT *L = getLoopFor(BB);
5610bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner    return L ? L->getLoopDepth() : 0;
5620bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
5630bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
5640bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  // isLoopHeader - True if the block is a loop header node
56544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  bool isLoopHeader(BlockT *BB) const {
566c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    const LoopT *L = getLoopFor(BB);
56750f5490842d501e269a4c6085d0d132cae0d31f8Chris Lattner    return L && L->getHeader() == BB;
5680bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner  }
569fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
57044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// removeLoop - This removes the specified top-level loop from this loop info
57144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// object.  The loop is not deleted, as it will presumably be inserted into
57244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// another loop.
573c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  LoopT *removeLoop(iterator I) {
57444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(I != end() && "Cannot remove end iterator!");
575c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman    LoopT *L = *I;
576dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    assert(!L->getParentLoop() && "Not a top-level loop!");
57744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
57844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    return L;
57944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
580fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
58144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// changeLoopFor - Change the top-level loop that contains BB to the
58244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// specified loop.  This should be used by transformations that restructure
58344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// the loop hierarchy tree.
584c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void changeLoopFor(BlockT *BB, LoopT *L) {
585fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick    if (!L) {
586da69f3b357097e75fbf9a5a2bbe1e7273d4b4271Benjamin Kramer      BBMap.erase(BB);
587fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick      return;
588fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick    }
589fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick    BBMap[BB] = L;
59044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
591fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
59244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// changeTopLevelLoop - Replace the specified loop in the top-level loops
59344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// list with the indicated loop.
594c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void changeTopLevelLoop(LoopT *OldLoop,
595c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                          LoopT *NewLoop) {
596ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    auto I = std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
59744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    assert(I != TopLevelLoops.end() && "Old loop not at top level!");
59844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    *I = NewLoop;
599dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
60044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson           "Loops already embedded into a subloop!");
60144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
602fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
60344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// addTopLevelLoop - This adds the specified loop to the collection of
60444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// top-level loops.
605c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  void addTopLevelLoop(LoopT *New) {
606dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    assert(!New->getParentLoop() && "Loop already in subloop!");
60744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    TopLevelLoops.push_back(New);
60844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
609fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
61044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// removeBlock - This method completely removes BB from all data structures,
61144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// including all of the Loop objects it is nested in and our mapping from
61244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  /// BasicBlocks to loops.
61344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  void removeBlock(BlockT *BB) {
614ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    auto I = BBMap.find(BB);
61544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (I != BBMap.end()) {
616c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman      for (LoopT *L = I->second; L; L = L->getParentLoop())
61744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson        L->removeBlockFromLoop(BB);
61844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
61944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson      BBMap.erase(I);
62044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    }
62144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
622fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
62344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  // Internals
624fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
625c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
626c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman                                      const LoopT *ParentLoop) {
627dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (!SubLoop) return true;
62844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    if (SubLoop == ParentLoop) return false;
62944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson    return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
63044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
631fbfb806e3fe697fd8ee2a59f8fd3e0790ccf0d3cAndrew Trick
63237aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick  /// Create the loop forest using a stable algorithm.
63337aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick  void Analyze(DominatorTreeBase<BlockT> &DomTree);
63437aa33bc11c01a7142bfa2428a5a4d219b07b6c3Andrew Trick
63544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  // Debugging
636cbf24b4e58c2f621f480883c5bb1f2f2b2b8d497Andrew Trick  void print(raw_ostream &OS) const;
637ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
638ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  void verify() const;
63944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson};
64044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
64160c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick// Implementation in LoopInfoImpl.h
64260c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick#ifdef __GNUC__
64360c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick__extension__ extern template class LoopInfoBase<BasicBlock, Loop>;
64460c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick#endif
64560c7d5bc2cbeff9eff16716295ffd6a5374dc6bcAndrew Trick
646ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesclass LoopInfo : public LoopInfoBase<BasicBlock, Loop> {
647ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  typedef LoopInfoBase<BasicBlock, Loop> BaseT;
648ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
649c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman  friend class LoopBase<BasicBlock, Loop>;
6509d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman
651ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  void operator=(const LoopInfo &) = delete;
652ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LoopInfo(const LoopInfo &) = delete;
65344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonpublic:
654ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LoopInfo() {}
655d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson
656ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LoopInfo(LoopInfo &&Arg) : BaseT(std::move(static_cast<BaseT &>(Arg))) {}
657ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LoopInfo &operator=(LoopInfo &&RHS) {
658ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
659ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return *this;
66044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson  }
66144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson
662ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  // Most of the public interface is provided via LoopInfoBase.
663d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands
664fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick  /// updateUnloop - Update LoopInfo after removing the last backedge from a
665fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick  /// loop--now the "unloop". This updates the loop forest and parent loops for
666fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick  /// each block so that Unloop is no longer referenced, but the caller must
667fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick  /// actually delete the Unloop object.
668fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick  void updateUnloop(Loop *Unloop);
669fb62b8deb3c837bc5f4cf98543b89d08e7db9f84Andrew Trick
670d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands  /// replacementPreservesLCSSAForm - Returns true if replacing From with To
671d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands  /// everywhere is guaranteed to preserve LCSSA form.
672d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands  bool replacementPreservesLCSSAForm(Instruction *From, Value *To) {
673d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // Preserving LCSSA form is only problematic if the replacing value is an
674d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // instruction.
675d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    Instruction *I = dyn_cast<Instruction>(To);
676d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    if (!I) return true;
6779bc6a90146417af03144fa2f7dd94f9945b57c06Duncan Sands    // If both instructions are defined in the same basic block then replacement
6789bc6a90146417af03144fa2f7dd94f9945b57c06Duncan Sands    // cannot break LCSSA form.
6799bc6a90146417af03144fa2f7dd94f9945b57c06Duncan Sands    if (I->getParent() == From->getParent())
6809bc6a90146417af03144fa2f7dd94f9945b57c06Duncan Sands      return true;
681d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // If the instruction is not defined in a loop then it can safely replace
682d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // anything.
683d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    Loop *ToLoop = getLoopFor(I->getParent());
684d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    if (!ToLoop) return true;
685d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // If the replacing instruction is defined in the same loop as the original
686d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // instruction, or in a loop that contains it as an inner loop, then using
687d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands    // it as a replacement will not break LCSSA form.
6883b0aed19f0f9ed62362c04f7e18e2ada686f4055Duncan Sands    return ToLoop->contains(getLoopFor(From->getParent()));
689d0c6f3dafd7c3e9137d4e6415014c94137fcd3fcDuncan Sands  }
6900bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner};
6910bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner
6921db0a400370466e187ae06c96a1586c2c21409ddChris Lattner// Allow clients to walk the list of nested loops...
6931db0a400370466e187ae06c96a1586c2c21409ddChris Lattnertemplate <> struct GraphTraits<const Loop*> {
6941db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  typedef const Loop NodeType;
6959d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  typedef LoopInfo::iterator ChildIteratorType;
6961db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
6971db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  static NodeType *getEntryNode(const Loop *L) { return L; }
6989769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_begin(NodeType *N) {
699329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->begin();
7001db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
7019769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_end(NodeType *N) {
702329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->end();
7031db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
7041db0a400370466e187ae06c96a1586c2c21409ddChris Lattner};
7051db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
7061db0a400370466e187ae06c96a1586c2c21409ddChris Lattnertemplate <> struct GraphTraits<Loop*> {
7071db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  typedef Loop NodeType;
7089d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman  typedef LoopInfo::iterator ChildIteratorType;
7091db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
7101db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  static NodeType *getEntryNode(Loop *L) { return L; }
7119769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_begin(NodeType *N) {
712329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->begin();
7131db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
7149769ab22265b313171d201b5928688524a01bd87Misha Brukman  static inline ChildIteratorType child_end(NodeType *N) {
715329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner    return N->end();
7161db0a400370466e187ae06c96a1586c2c21409ddChris Lattner  }
7171db0a400370466e187ae06c96a1586c2c21409ddChris Lattner};
7181db0a400370466e187ae06c96a1586c2c21409ddChris Lattner
719ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// \brief Analysis pass that exposes the \c LoopInfo for a function.
720ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesclass LoopAnalysis {
721ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static char PassID;
722ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
723ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinespublic:
724ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  typedef LoopInfo Result;
725ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
726ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  /// \brief Opaque, unique identifier for this analysis pass.
727ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static void *ID() { return (void *)&PassID; }
728ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
729ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  /// \brief Provide a name for the analysis for debugging and logging.
730ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static StringRef name() { return "LoopAnalysis"; }
731ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
732ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LoopAnalysis() {}
733ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LoopAnalysis(const LoopAnalysis &Arg) {}
734ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LoopAnalysis(LoopAnalysis &&Arg) {}
735ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LoopAnalysis &operator=(const LoopAnalysis &RHS) { return *this; }
736ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LoopAnalysis &operator=(LoopAnalysis &&RHS) { return *this; }
737ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
738ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LoopInfo run(Function &F, AnalysisManager<Function> *AM);
739ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines};
740ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
741ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// \brief Printer pass for the \c LoopAnalysis results.
742ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesclass LoopPrinterPass {
743ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  raw_ostream &OS;
744ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
745ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinespublic:
746ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  explicit LoopPrinterPass(raw_ostream &OS) : OS(OS) {}
747ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  PreservedAnalyses run(Function &F, AnalysisManager<Function> *AM);
748ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
749ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static StringRef name() { return "LoopPrinterPass"; }
750ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines};
751ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
752ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// \brief The legacy pass manager's analysis pass to compute loop information.
753ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesclass LoopInfoWrapperPass : public FunctionPass {
754ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LoopInfo LI;
755ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
756ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinespublic:
757ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static char ID; // Pass identification, replacement for typeid
758ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
759ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LoopInfoWrapperPass() : FunctionPass(ID) {
760ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    initializeLoopInfoWrapperPassPass(*PassRegistry::getPassRegistry());
761ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  }
762ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
763ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LoopInfo &getLoopInfo() { return LI; }
764ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  const LoopInfo &getLoopInfo() const { return LI; }
765ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
766ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  /// \brief Calculate the natural loop information for a given function.
767ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  bool runOnFunction(Function &F) override;
768ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
769ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  void verifyAnalysis() const override;
770ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
771ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  void releaseMemory() override { LI.releaseMemory(); }
772ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
773ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  void print(raw_ostream &O, const Module *M = nullptr) const override;
774ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
775ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  void getAnalysisUsage(AnalysisUsage &AU) const override;
776ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines};
777ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
778d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
779d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
7800bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner#endif
781