LoopInfo.h revision 937738649386b8188524d0cd61943214a5b93cf6
148486893f46d2e12e926682a3ecb908716bc66c4Chris Lattner//===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===// 29769ab22265b313171d201b5928688524a01bd87Misha Brukman// 36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// The LLVM Compiler Infrastructure 46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// 57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source 67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details. 79769ab22265b313171d201b5928688524a01bd87Misha Brukman// 86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===// 90bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner// 100bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner// This file defines the LoopInfo class that is used to identify natural loops 11fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner// and determine the loop depth of various nodes of the CFG. Note that natural 1246758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner// loops may actually be several loops that share the same header node. 13fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner// 14fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner// This analysis calculates the nesting structure of loops in a function. For 15fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner// each natural loop identified, this analysis identifies natural loops 16072b163424491c85df6664a4e056aae5e07dc64dChris Lattner// contained entirely within the loop and the basic blocks the make up the loop. 17fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner// 18072b163424491c85df6664a4e056aae5e07dc64dChris Lattner// It can calculate on the fly various bits of information, for example: 19072b163424491c85df6664a4e056aae5e07dc64dChris Lattner// 20072b163424491c85df6664a4e056aae5e07dc64dChris Lattner// * whether there is a preheader for the loop 21072b163424491c85df6664a4e056aae5e07dc64dChris Lattner// * the number of back edges to the header 22072b163424491c85df6664a4e056aae5e07dc64dChris Lattner// * whether or not a particular block branches out of the loop 23072b163424491c85df6664a4e056aae5e07dc64dChris Lattner// * the successor blocks of the loop 24072b163424491c85df6664a4e056aae5e07dc64dChris Lattner// * the loop depth 25072b163424491c85df6664a4e056aae5e07dc64dChris Lattner// * the trip count 26072b163424491c85df6664a4e056aae5e07dc64dChris Lattner// * etc... 270bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner// 280bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//===----------------------------------------------------------------------===// 290bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner 300bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner#ifndef LLVM_ANALYSIS_LOOP_INFO_H 310bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner#define LLVM_ANALYSIS_LOOP_INFO_H 320bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner 33facd752d3afaeca7dee46648f2a2ae209a94e5e9Chris Lattner#include "llvm/Pass.h" 3444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson#include "llvm/ADT/DepthFirstIterator.h" 35551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/GraphTraits.h" 36b7211a2ce13a0365e0e1dd2f27adda2ee3d1288bDevang Patel#include "llvm/ADT/SmallVector.h" 3744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson#include "llvm/Analysis/Dominators.h" 38019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#include "llvm/Support/CFG.h" 39019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#include "llvm/Support/Streams.h" 40019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#include <algorithm> 41019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#include <ostream> 42019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 431d5562f72e3f7d9e17a2bf95afa54a98dac95894Dan Gohmannamespace llvm { 441d5562f72e3f7d9e17a2bf95afa54a98dac95894Dan Gohman 45019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Andersontemplate<typename T> 46019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Andersonstatic 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; 55c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class N, class M> class LoopInfoBase; 56c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class N, class M> class LoopBase; 570bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner 580bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//===----------------------------------------------------------------------===// 59131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner/// LoopBase class - Instances of this class are used to represent loops that 60131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner/// are detected in the flow graph 612b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner/// 62c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class BlockT, class LoopT> 63019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Andersonclass LoopBase { 64c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopT *ParentLoop; 65131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner // SubLoops - Loops contained entirely within this one. 66c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman std::vector<LoopT *> SubLoops; 67131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner 68131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner // Blocks - The list of blocks in this loop. First entry is the header node. 69131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner std::vector<BlockT*> Blocks; 70019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 71c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman // DO NOT IMPLEMENT 72c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopBase(const LoopBase<BlockT, LoopT> &); 73c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman // DO NOT IMPLEMENT 74c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman const LoopBase<BlockT, LoopT>&operator=(const LoopBase<BlockT, LoopT> &); 750bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattnerpublic: 7646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// Loop ctor - This creates an empty loop. 77019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson LoopBase() : ParentLoop(0) {} 78019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson ~LoopBase() { 7934cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng for (size_t i = 0, e = SubLoops.size(); i != e; ++i) 804e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner delete SubLoops[i]; 814e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner } 820bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner 83ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman /// getLoopDepth - Return the nesting level of this loop. An outer-most 84ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman /// loop has depth 1, for consistency with loop depth values used for basic 85ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman /// blocks, where depth 0 is used for blocks not inside any loops. 86072b163424491c85df6664a4e056aae5e07dc64dChris Lattner unsigned getLoopDepth() const { 87ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman unsigned D = 1; 88c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman for (const LoopT *CurLoop = ParentLoop; CurLoop; 89019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson CurLoop = CurLoop->ParentLoop) 90072b163424491c85df6664a4e056aae5e07dc64dChris Lattner ++D; 91072b163424491c85df6664a4e056aae5e07dc64dChris Lattner return D; 92072b163424491c85df6664a4e056aae5e07dc64dChris Lattner } 93019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson BlockT *getHeader() const { return Blocks.front(); } 94c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopT *getParentLoop() const { return ParentLoop; } 950bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner 96b670a1737bae5c3ecc65042d3a1b10d774ecd252Wojciech Matyjewicz /// contains - Return true if the specified basic block is in this loop 97fbdf4bf1799bf9ea566fc0fc0507752590a6d559Misha Brukman /// 98019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson bool contains(const BlockT *BB) const { 99f3ab3a937203d690744357844948faaf96fb73f9Dan Gohman return std::find(block_begin(), block_end(), BB) != block_end(); 100019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 1010bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner 102329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner /// iterator/begin/end - Return the loops contained entirely within this loop. 1032b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner /// 104c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman const std::vector<LoopT *> &getSubLoops() const { return SubLoops; } 105c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman typedef typename std::vector<LoopT *>::const_iterator iterator; 106329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner iterator begin() const { return SubLoops.begin(); } 107329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner iterator end() const { return SubLoops.end(); } 10821c276d2fa99914d5ed958ac0aec7d78e3dd87cfDan Gohman bool empty() const { return SubLoops.empty(); } 109fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner 110fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner /// getBlocks - Get a list of the basic blocks which make up this loop. 111fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner /// 112019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson const std::vector<BlockT*> &getBlocks() const { return Blocks; } 113019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson typedef typename std::vector<BlockT*>::const_iterator block_iterator; 1144e77cc46ac688e7bee98747049f90e19e2902227Chris Lattner block_iterator block_begin() const { return Blocks.begin(); } 1154e77cc46ac688e7bee98747049f90e19e2902227Chris Lattner block_iterator block_end() const { return Blocks.end(); } 116fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner 117280a6e607d8eb7401749a92db624a82de47da777Nick Lewycky /// isLoopExit - True if terminator in the block can branch to another block 118280a6e607d8eb7401749a92db624a82de47da777Nick Lewycky /// that is outside of the current loop. 119fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner /// 120019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson bool isLoopExit(const BlockT *BB) const { 121d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson typedef GraphTraits<BlockT*> BlockTraits; 122d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson for (typename BlockTraits::ChildIteratorType SI = 123d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson BlockTraits::child_begin(const_cast<BlockT*>(BB)), 124d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson SE = BlockTraits::child_end(const_cast<BlockT*>(BB)); SI != SE; ++SI) { 125019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson if (!contains(*SI)) 126019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson return true; 127019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 128019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson return false; 129019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 1306b290a54409f6bb6a0cc1c0446cd2b170a4b7addMisha Brukman 131fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner /// getNumBackEdges - Calculate the number of back edges to the loop header 132fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner /// 133019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson unsigned getNumBackEdges() const { 134019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson unsigned NumBackEdges = 0; 135019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson BlockT *H = getHeader(); 136019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 137d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits; 138d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson for (typename InvBlockTraits::ChildIteratorType I = 139d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson InvBlockTraits::child_begin(const_cast<BlockT*>(H)), 140d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson E = InvBlockTraits::child_end(const_cast<BlockT*>(H)); I != E; ++I) 141019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson if (contains(*I)) 142019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson ++NumBackEdges; 143019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 144019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson return NumBackEdges; 145019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 1469474dd68e8a050193ca4003940ac399e2b17cb6aChris Lattner 147e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner //===--------------------------------------------------------------------===// 148e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner // APIs for simple analysis of the loop. 149e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner // 150e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner // Note that all of these methods can fail on general loops (ie, there may not 151e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner // be a preheader, etc). For best success, the loop simplification and 152e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner // induction variable canonicalization pass should be used to normalize loops 153e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner // for easy analysis. These methods assume canonical loops. 154e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner 1557466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner /// getExitingBlocks - Return all blocks inside the loop that have successors 1567466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner /// outside of the loop. These are the blocks _inside of the current loop_ 1577466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner /// which branch out. The returned list is always unique. 1587466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner /// 159019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const { 160019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // Sort the blocks vector so that we can use binary search to do quick 161019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // lookups. 162019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end()); 163019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson std::sort(LoopBBs.begin(), LoopBBs.end()); 164019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 165d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson typedef GraphTraits<BlockT*> BlockTraits; 166f3ab3a937203d690744357844948faaf96fb73f9Dan Gohman for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) 167d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson for (typename BlockTraits::ChildIteratorType I = 168d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI); 169d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson I != E; ++I) 170019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) { 171019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // Not in current loop? It must be an exit block. 172019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson ExitingBlocks.push_back(*BI); 173019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson break; 174019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 175019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 1767466ebf045fa5097ee0d7d2728eed7fd5945c8bcChris Lattner 177c83324682f3409c15dad992cd62928426c9ad83dDan Gohman /// getExitingBlock - If getExitingBlocks would return exactly one block, 178c83324682f3409c15dad992cd62928426c9ad83dDan Gohman /// return that block. Otherwise return null. 179c83324682f3409c15dad992cd62928426c9ad83dDan Gohman BlockT *getExitingBlock() const { 180c83324682f3409c15dad992cd62928426c9ad83dDan Gohman SmallVector<BlockT*, 8> ExitingBlocks; 181c83324682f3409c15dad992cd62928426c9ad83dDan Gohman getExitingBlocks(ExitingBlocks); 182c83324682f3409c15dad992cd62928426c9ad83dDan Gohman if (ExitingBlocks.size() == 1) 183c83324682f3409c15dad992cd62928426c9ad83dDan Gohman return ExitingBlocks[0]; 184c83324682f3409c15dad992cd62928426c9ad83dDan Gohman return 0; 185c83324682f3409c15dad992cd62928426c9ad83dDan Gohman } 186c83324682f3409c15dad992cd62928426c9ad83dDan Gohman 187f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner /// getExitBlocks - Return all of the successor blocks of this loop. These 188f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner /// are the blocks _outside of the current loop_ which are branched to. 189f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner /// 190019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const { 191019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // Sort the blocks vector so that we can use binary search to do quick 192019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // lookups. 193019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end()); 194019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson std::sort(LoopBBs.begin(), LoopBBs.end()); 195019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 196d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson typedef GraphTraits<BlockT*> BlockTraits; 197f3ab3a937203d690744357844948faaf96fb73f9Dan Gohman for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) 198d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson for (typename BlockTraits::ChildIteratorType I = 199d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI); 200d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson I != E; ++I) 201019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) 202019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // Not in current loop? It must be an exit block. 203019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson ExitBlocks.push_back(*I); 204019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 205f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner 2061827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman /// getExitBlock - If getExitBlocks would return exactly one block, 2071827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman /// return that block. Otherwise return null. 2081827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman BlockT *getExitBlock() const { 2091827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman SmallVector<BlockT*, 8> ExitBlocks; 2101827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman getExitBlocks(ExitBlocks); 2111827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman if (ExitBlocks.size() == 1) 2121827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman return ExitBlocks[0]; 2131827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman return 0; 2141827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman } 2151827e8263c9cb5dc29eea4999d8729f7376af4e1Dan Gohman 2164b8f36f10672bbdd747eabfb5708e4758c3d5337Devang Patel /// getUniqueExitBlocks - Return all unique successor blocks of this loop. 2174b8f36f10672bbdd747eabfb5708e4758c3d5337Devang Patel /// These are the blocks _outside of the current loop_ which are branched to. 2184b8f36f10672bbdd747eabfb5708e4758c3d5337Devang Patel /// This assumes that loop is in canonical form. 2194b8f36f10672bbdd747eabfb5708e4758c3d5337Devang Patel /// 220019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson void getUniqueExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const { 221019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // Sort the blocks vector so that we can use binary search to do quick 222019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // lookups. 223019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end()); 224019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson std::sort(LoopBBs.begin(), LoopBBs.end()); 225019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 226019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson std::vector<BlockT*> switchExitBlocks; 227019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 228f3ab3a937203d690744357844948faaf96fb73f9Dan Gohman for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) { 229019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 230019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson BlockT *current = *BI; 231019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson switchExitBlocks.clear(); 232019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 233d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson typedef GraphTraits<BlockT*> BlockTraits; 234d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits; 235d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson for (typename BlockTraits::ChildIteratorType I = 236d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI); 237d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson I != E; ++I) { 238019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) 239019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // If block is inside the loop then it is not a exit block. 240019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson continue; 241d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson 242d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson typename InvBlockTraits::ChildIteratorType PI = 243d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson InvBlockTraits::child_begin(*I); 244019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson BlockT *firstPred = *PI; 245019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 246019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // If current basic block is this exit block's first predecessor 247019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // then only insert exit block in to the output ExitBlocks vector. 248019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // This ensures that same exit block is not inserted twice into 249019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // ExitBlocks vector. 250019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson if (current != firstPred) 251019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson continue; 252019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 253019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // If a terminator has more then two successors, for example SwitchInst, 254019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // then it is possible that there are multiple edges from current block 255019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // to one exit block. 256d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson if (std::distance(BlockTraits::child_begin(current), 257d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson BlockTraits::child_end(current)) <= 2) { 258019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson ExitBlocks.push_back(*I); 259019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson continue; 260019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 261019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 262019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // In case of multiple edges from current block to exit block, collect 263019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // only one edge in ExitBlocks. Use switchExitBlocks to keep track of 264019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // duplicate edges. 265019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I) 266019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson == switchExitBlocks.end()) { 267019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson switchExitBlocks.push_back(*I); 268019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson ExitBlocks.push_back(*I); 269019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 270019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 271019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 272019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 2734b8f36f10672bbdd747eabfb5708e4758c3d5337Devang Patel 274a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one 275a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman /// block, return that block. Otherwise return null. 276a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman BlockT *getUniqueExitBlock() const { 277a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman SmallVector<BlockT*, 8> UniqueExitBlocks; 278a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman getUniqueExitBlocks(UniqueExitBlocks); 279a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman if (UniqueExitBlocks.size() == 1) 280a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman return UniqueExitBlocks[0]; 281a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman return 0; 282a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman } 283a278f3f55212e38c0647909f51ee7d2d6e41799aDan Gohman 2842b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner /// getLoopPreheader - If there is a preheader for this loop, return it. A 2852b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner /// loop has a preheader if there is only one edge to the header of the loop 2862b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner /// from outside of the loop. If this is the case, the block branching to the 287e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner /// header of the loop is the preheader node. 2882b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner /// 289e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner /// This method returns null if there is no preheader for the loop. 2902b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner /// 291019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson BlockT *getLoopPreheader() const { 292019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // Keep track of nodes outside the loop branching to the header... 293019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson BlockT *Out = 0; 294019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 295019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // Loop over the predecessors of the header node... 296019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson BlockT *Header = getHeader(); 297d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson typedef GraphTraits<BlockT*> BlockTraits; 298d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits; 299d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson for (typename InvBlockTraits::ChildIteratorType PI = 300d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson InvBlockTraits::child_begin(Header), 301d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson PE = InvBlockTraits::child_end(Header); PI != PE; ++PI) 302019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson if (!contains(*PI)) { // If the block is not in the loop... 303019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson if (Out && Out != *PI) 304019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson return 0; // Multiple predecessors outside the loop 305019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson Out = *PI; 306019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 307019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 308019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // Make sure there is only one exit out of the preheader. 309019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson assert(Out && "Header of loop has no predecessors from outside loop?"); 310d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out); 311019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson ++SI; 312d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson if (SI != BlockTraits::child_end(Out)) 313019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson return 0; // Multiple exits from the block, must not be a preheader. 314019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 315131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner // If there is exactly one preheader, return it. If there was zero, then 316131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner // Out is still null. 317019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson return Out; 318019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 3192b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner 320b317143ba851ce2853fb262fb2185ef5f1be030dDan Gohman /// getLoopLatch - If there is a single latch block for this loop, return it. 321b317143ba851ce2853fb262fb2185ef5f1be030dDan Gohman /// A latch block is a block that contains a branch back to the header. 322b317143ba851ce2853fb262fb2185ef5f1be030dDan Gohman /// A loop header in normal form has two edges into it: one from a preheader 323b317143ba851ce2853fb262fb2185ef5f1be030dDan Gohman /// and one from a latch block. 324019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson BlockT *getLoopLatch() const { 325019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson BlockT *Header = getHeader(); 326d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits; 327d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson typename InvBlockTraits::ChildIteratorType PI = 328d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson InvBlockTraits::child_begin(Header); 329d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson typename InvBlockTraits::ChildIteratorType PE = 330d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson InvBlockTraits::child_end(Header); 331019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson if (PI == PE) return 0; // no preds? 332019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 333019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson BlockT *Latch = 0; 334019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson if (contains(*PI)) 335019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson Latch = *PI; 336019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson ++PI; 337019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson if (PI == PE) return 0; // only one pred? 338019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 339019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson if (contains(*PI)) { 340019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson if (Latch) return 0; // multiple backedges 341019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson Latch = *PI; 342019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 343019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson ++PI; 344019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson if (PI != PE) return 0; // more than two preds 345019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 346019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson return Latch; 347019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 348e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner 349e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner //===--------------------------------------------------------------------===// 350e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner // APIs for updating loop information after changing the CFG 351e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner // 352e725cb0d5a8e017b66768eaf186718b36ffea193Chris Lattner 353fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner /// addBasicBlockToLoop - This method is used by other analyses to update loop 354fe3ae1ed660925f06159ec89460d92148e049ffdChris Lattner /// information. NewBB is set to be a new member of the current loop. 3552b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner /// Because of this, it is added as a member of all parent loops, and is added 3562b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner /// to the specified LoopInfo object as being in the current basic block. It 3572b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner /// is not valid to replace the loop header with this method. 3582b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner /// 359c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI); 3602b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner 36146758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// replaceChildLoopWith - This is used when splitting loops up. It replaces 36246758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// the OldChild entry in our children list with NewChild, and updates the 36346758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// parent pointer of OldChild to be null and the NewChild to be this loop. 36446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// This updates the loop depth of the new child. 365c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman void replaceChildLoopWith(LoopT *OldChild, 366c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopT *NewChild) { 367019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson assert(OldChild->ParentLoop == this && "This loop is already broken!"); 368019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!"); 369c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman typename std::vector<LoopT *>::iterator I = 370019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson std::find(SubLoops.begin(), SubLoops.end(), OldChild); 371019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson assert(I != SubLoops.end() && "OldChild not in loop!"); 372019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson *I = NewChild; 373019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson OldChild->ParentLoop = 0; 374c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman NewChild->ParentLoop = static_cast<LoopT *>(this); 375019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 37646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner 37746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// addChildLoop - Add the specified loop to be a child of this loop. This 37846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// updates the loop depth of the new child. 37946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// 380c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman void addChildLoop(LoopT *NewChild) { 381019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!"); 382c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman NewChild->ParentLoop = static_cast<LoopT *>(this); 383019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson SubLoops.push_back(NewChild); 384019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 38546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner 38646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// removeChildLoop - This removes the specified child from being a subloop of 38746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// this loop. The loop is not deleted, as it will presumably be inserted 38846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// into another loop. 389c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopT *removeChildLoop(iterator I) { 390019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson assert(I != SubLoops.end() && "Cannot remove end iterator!"); 391c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopT *Child = *I; 392019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson assert(Child->ParentLoop == this && "Child is not a child of this loop!"); 393019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson SubLoops.erase(SubLoops.begin()+(I-begin())); 394019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson Child->ParentLoop = 0; 395019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson return Child; 396019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 39746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner 39846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// addBlockEntry - This adds a basic block directly to the basic block list. 39946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// This should only be used by transformations that create new loops. Other 40046758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// transformations should use addBasicBlockToLoop. 401019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson void addBlockEntry(BlockT *BB) { 40246758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner Blocks.push_back(BB); 40346758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner } 40446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner 40551a4ad475b095aed49caff176b98c0754e421af4Chris Lattner /// moveToHeader - This method is used to move BB (which must be part of this 40651a4ad475b095aed49caff176b98c0754e421af4Chris Lattner /// loop) to be the loop header of the loop (the block that dominates all 40751a4ad475b095aed49caff176b98c0754e421af4Chris Lattner /// others). 408019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson void moveToHeader(BlockT *BB) { 40951a4ad475b095aed49caff176b98c0754e421af4Chris Lattner if (Blocks[0] == BB) return; 41051a4ad475b095aed49caff176b98c0754e421af4Chris Lattner for (unsigned i = 0; ; ++i) { 41151a4ad475b095aed49caff176b98c0754e421af4Chris Lattner assert(i != Blocks.size() && "Loop does not contain BB!"); 41251a4ad475b095aed49caff176b98c0754e421af4Chris Lattner if (Blocks[i] == BB) { 41351a4ad475b095aed49caff176b98c0754e421af4Chris Lattner Blocks[i] = Blocks[0]; 41451a4ad475b095aed49caff176b98c0754e421af4Chris Lattner Blocks[0] = BB; 41551a4ad475b095aed49caff176b98c0754e421af4Chris Lattner return; 41651a4ad475b095aed49caff176b98c0754e421af4Chris Lattner } 41751a4ad475b095aed49caff176b98c0754e421af4Chris Lattner } 41851a4ad475b095aed49caff176b98c0754e421af4Chris Lattner } 41951a4ad475b095aed49caff176b98c0754e421af4Chris Lattner 42046758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// removeBlockFromLoop - This removes the specified basic block from the 421f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner /// current loop, updating the Blocks as appropriate. This does not update 422f1ab4b4eac5603d19c20f4a508f93a118a52bdd5Chris Lattner /// the mapping in the LoopInfo class. 423019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson void removeBlockFromLoop(BlockT *BB) { 424019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson RemoveFromVector(Blocks, BB); 425019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 42646758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner 42758e0ef1e90c3f6dbae213612b44e56f7d6d65ea7Devang Patel /// verifyLoop - Verify loop structure 428019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson void verifyLoop() const { 429019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#ifndef NDEBUG 430019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson assert (getHeader() && "Loop header is missing"); 431019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson assert (getLoopPreheader() && "Loop preheader is missing"); 432019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson assert (getLoopLatch() && "Loop latch is missing"); 433f3ab3a937203d690744357844948faaf96fb73f9Dan Gohman for (iterator I = SubLoops.begin(), E = SubLoops.end(); I != E; ++I) 434019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson (*I)->verifyLoop(); 435019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson#endif 436019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 437019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 438019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson void print(std::ostream &OS, unsigned Depth = 0) const { 439927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman OS << std::string(Depth*2, ' ') << "Loop at depth " << getLoopDepth() 440927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman << " containing: "; 441019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 442019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson for (unsigned i = 0; i < getBlocks().size(); ++i) { 443019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson if (i) OS << ","; 444927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman BlockT *BB = getBlocks()[i]; 445927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman WriteAsOperand(OS, BB, false); 446927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman if (BB == getHeader()) OS << "<header>"; 447927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman if (BB == getLoopLatch()) OS << "<latch>"; 448927793b6a165148af6d079ac03f97d13d296ff0dDan Gohman if (isLoopExit(BB)) OS << "<exit>"; 449019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 450019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson OS << "\n"; 45158e0ef1e90c3f6dbae213612b44e56f7d6d65ea7Devang Patel 452019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson for (iterator I = begin(), E = end(); I != E; ++I) 453019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson (*I)->print(OS, Depth+2); 454019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 455019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 4565c7e326585f3a543388ba871c3425f7664cd9143Bill Wendling void print(std::ostream *O, unsigned Depth = 0) const { 4575c7e326585f3a543388ba871c3425f7664cd9143Bill Wendling if (O) print(*O, Depth); 4585c7e326585f3a543388ba871c3425f7664cd9143Bill Wendling } 459019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 460019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson void dump() const { 461019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson print(cerr); 462019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 463019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 464c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanprotected: 465c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman friend class LoopInfoBase<BlockT, LoopT>; 4661002c0203450620594a85454c6a095ca94b87cb2Dan Gohman explicit LoopBase(BlockT *BB) : ParentLoop(0) { 467072b163424491c85df6664a4e056aae5e07dc64dChris Lattner Blocks.push_back(BB); 4680bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner } 4690bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner}; 4700bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner 471c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanclass Loop : public LoopBase<BasicBlock, Loop> { 472c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanpublic: 473c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman Loop() {} 47416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman 47516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// isLoopInvariant - Return true if the specified value is loop invariant 47616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// 47716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman bool isLoopInvariant(Value *V) const; 47816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman 479a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// isLoopInvariant - Return true if the specified instruction is 480a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// loop-invariant. 481a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// 482a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman bool isLoopInvariant(Instruction *I) const; 483a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman 484206613b289f60b71a76e9190d36b9ea9e47a701eDan Gohman /// makeLoopInvariant - If the given value is an instruction inside of the 485a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// loop and it can be hoisted, do so to make it trivially loop-invariant. 486a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// Return true if the value after any hoisting is loop invariant. This 487a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// function can be used as a slightly more aggressive replacement for 488a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// isLoopInvariant. 489a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// 490a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// If InsertPt is specified, it is the point to hoist instructions to. 491a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// If null, the terminator of the loop preheader is used. 492a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// 493bdc017edacb713119b24ab269d250a82d62fffebDan Gohman bool makeLoopInvariant(Value *V, bool &Changed, 494bdc017edacb713119b24ab269d250a82d62fffebDan Gohman Instruction *InsertPt = 0) const; 495a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman 496a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// makeLoopInvariant - If the given instruction is inside of the 497a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// loop and it can be hoisted, do so to make it trivially loop-invariant. 498a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// Return true if the instruction after any hoisting is loop invariant. This 499a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// function can be used as a slightly more aggressive replacement for 500a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// isLoopInvariant. 501a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// 502a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// If InsertPt is specified, it is the point to hoist instructions to. 503a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// If null, the terminator of the loop preheader is used. 504a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman /// 505bdc017edacb713119b24ab269d250a82d62fffebDan Gohman bool makeLoopInvariant(Instruction *I, bool &Changed, 506bdc017edacb713119b24ab269d250a82d62fffebDan Gohman Instruction *InsertPt = 0) const; 507a342026504e65e2c8dc5600dab4b45ab4f94026dDan Gohman 50816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// getCanonicalInductionVariable - Check to see if the loop has a canonical 50916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// induction variable: an integer recurrence that starts at 0 and increments 51016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// by one each time through the loop. If so, return the phi node that 51116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// corresponds to it. 51216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// 51316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// The IndVarSimplify pass transforms loops to have a canonical induction 51416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// variable. 51516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// 51616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman PHINode *getCanonicalInductionVariable() const; 51716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman 51816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// getCanonicalInductionVariableIncrement - Return the LLVM value that holds 51916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// the canonical induction variable value for the "next" iteration of the 52016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// loop. This always succeeds if getCanonicalInductionVariable succeeds. 52116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// 52216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman Instruction *getCanonicalInductionVariableIncrement() const; 52316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman 52416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// getTripCount - Return a loop-invariant LLVM value indicating the number of 52516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// times the loop will be executed. Note that this means that the backedge 52616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// of the loop executes N-1 times. If the trip-count cannot be determined, 52716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// this returns null. 52816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// 52916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// The IndVarSimplify pass transforms loops to have a form that this 53016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// function easily understands. 53116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// 53216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman Value *getTripCount() const; 53316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman 53416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// getSmallConstantTripCount - Returns the trip count of this loop as a 53516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// normal unsigned value, if possible. Returns 0 if the trip count is unknown 53616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// of not constant. Will also return 0 if the trip count is very large 53716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// (>= 2^32) 53816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman unsigned getSmallConstantTripCount() const; 53916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman 54016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// getSmallConstantTripMultiple - Returns the largest constant divisor of the 54116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// trip count of this loop as a normal unsigned value, if possible. This 54216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// means that the actual trip count is always a multiple of the returned 54316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// value (don't forget the trip count could very well be zero as well!). 54416a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// 54516a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// Returns 1 if the trip count is unknown or not guaranteed to be the 54616a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// multiple of a constant (which is also the case if the trip count is simply 54716a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// constant, use getSmallConstantTripCount for that case), Will also return 1 54816a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// if the trip count is very large (>= 2^32). 54916a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman unsigned getSmallConstantTripMultiple() const; 55016a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman 55116a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman /// isLCSSAForm - Return true if the Loop is in LCSSA form 55216a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman bool isLCSSAForm() const; 55316a2c927e95c29a316d0271c93e0490ce3bc06ceDan Gohman 554937738649386b8188524d0cd61943214a5b93cf6Dan Gohman /// isLoopSimplifyForm - Return true if the Loop is in the form that 555937738649386b8188524d0cd61943214a5b93cf6Dan Gohman /// the LoopSimplify form transforms loops to, which is sometimes called 556937738649386b8188524d0cd61943214a5b93cf6Dan Gohman /// normal form. 557937738649386b8188524d0cd61943214a5b93cf6Dan Gohman bool isLoopSimplifyForm() const; 558937738649386b8188524d0cd61943214a5b93cf6Dan Gohman 559c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanprivate: 560c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman friend class LoopInfoBase<BasicBlock, Loop>; 561c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {} 562c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman}; 5630bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner 5640bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner//===----------------------------------------------------------------------===// 5652b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner/// LoopInfo - This class builds and contains all of the top level loop 5662b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner/// structures in the specified function. 5672b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner/// 56844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 569c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class BlockT, class LoopT> 57044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonclass LoopInfoBase { 5710bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner // BBMap - Mapping of basic blocks to the inner most loop they occur in 572c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman std::map<BlockT *, LoopT *> BBMap; 573c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman std::vector<LoopT *> TopLevelLoops; 574c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman friend class LoopBase<BlockT, LoopT>; 5759d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman 5769d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman void operator=(const LoopInfoBase &); // do not implement 5779d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman LoopInfoBase(const LoopInfo &); // do not implement 5780bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattnerpublic: 57944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson LoopInfoBase() { } 58044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson ~LoopInfoBase() { releaseMemory(); } 58144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 58244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson void releaseMemory() { 583c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman for (typename std::vector<LoopT *>::iterator I = 58444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson TopLevelLoops.begin(), E = TopLevelLoops.end(); I != E; ++I) 58544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson delete *I; // Delete all of the loops... 5860bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner 587131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner BBMap.clear(); // Reset internal state of analysis 58844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson TopLevelLoops.clear(); 58944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 59044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 591329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner /// iterator/begin/end - The interface to the top-level loops in the current 592329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner /// function. 593329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner /// 594c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman typedef typename std::vector<LoopT *>::const_iterator iterator; 595329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner iterator begin() const { return TopLevelLoops.begin(); } 596329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner iterator end() const { return TopLevelLoops.end(); } 597a8c763b3071ae1a58ee8baeb282331245527e004Dan Gohman bool empty() const { return TopLevelLoops.empty(); } 59844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 5992b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner /// getLoopFor - Return the inner most loop that BB lives in. If a basic 6002b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner /// block is in no loop (for example the entry node), null is returned. 6012b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner /// 602c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopT *getLoopFor(const BlockT *BB) const { 603c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman typename std::map<BlockT *, LoopT *>::const_iterator I= 604d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson BBMap.find(const_cast<BlockT*>(BB)); 6050bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner return I != BBMap.end() ? I->second : 0; 6060bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner } 60744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 6082b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner /// operator[] - same as getLoopFor... 6092b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner /// 610c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman const LoopT *operator[](const BlockT *BB) const { 6110bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner return getLoopFor(BB); 6120bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner } 61344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 614ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman /// getLoopDepth - Return the loop nesting level of the specified block. A 615ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman /// depth of 0 means the block is not inside any loop. 6162b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner /// 61744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson unsigned getLoopDepth(const BlockT *BB) const { 618c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman const LoopT *L = getLoopFor(BB); 6190bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner return L ? L->getLoopDepth() : 0; 6200bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner } 6210bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner 6220bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner // isLoopHeader - True if the block is a loop header node 62344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson bool isLoopHeader(BlockT *BB) const { 624c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman const LoopT *L = getLoopFor(BB); 62550f5490842d501e269a4c6085d0d132cae0d31f8Chris Lattner return L && L->getHeader() == BB; 6260bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner } 62744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 62844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// removeLoop - This removes the specified top-level loop from this loop info 62944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// object. The loop is not deleted, as it will presumably be inserted into 63044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// another loop. 631c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopT *removeLoop(iterator I) { 63244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson assert(I != end() && "Cannot remove end iterator!"); 633c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopT *L = *I; 63444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson assert(L->getParentLoop() == 0 && "Not a top-level loop!"); 63544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin())); 63644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson return L; 63744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 63844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 63944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// changeLoopFor - Change the top-level loop that contains BB to the 64044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// specified loop. This should be used by transformations that restructure 64144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// the loop hierarchy tree. 642c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman void changeLoopFor(BlockT *BB, LoopT *L) { 643c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopT *&OldLoop = BBMap[BB]; 64444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson assert(OldLoop && "Block not in a loop yet!"); 64544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson OldLoop = L; 64644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 64744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 64844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// changeTopLevelLoop - Replace the specified loop in the top-level loops 64944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// list with the indicated loop. 650c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman void changeTopLevelLoop(LoopT *OldLoop, 651c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopT *NewLoop) { 652c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman typename std::vector<LoopT *>::iterator I = 65344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop); 65444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson assert(I != TopLevelLoops.end() && "Old loop not at top level!"); 65544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson *I = NewLoop; 65644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson assert(NewLoop->ParentLoop == 0 && OldLoop->ParentLoop == 0 && 65744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson "Loops already embedded into a subloop!"); 65844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 65944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 66044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// addTopLevelLoop - This adds the specified loop to the collection of 66144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// top-level loops. 662c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman void addTopLevelLoop(LoopT *New) { 66344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson assert(New->getParentLoop() == 0 && "Loop already in subloop!"); 66444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson TopLevelLoops.push_back(New); 66544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 66644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 66744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// removeBlock - This method completely removes BB from all data structures, 66844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// including all of the Loop objects it is nested in and our mapping from 66944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// BasicBlocks to loops. 67044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson void removeBlock(BlockT *BB) { 671c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman typename std::map<BlockT *, LoopT *>::iterator I = BBMap.find(BB); 67244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson if (I != BBMap.end()) { 673c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman for (LoopT *L = I->second; L; L = L->getParentLoop()) 67444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson L->removeBlockFromLoop(BB); 67544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 67644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson BBMap.erase(I); 67744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 67844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 67944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 68044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // Internals 68144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 682c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman static bool isNotAlreadyContainedIn(const LoopT *SubLoop, 683c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman const LoopT *ParentLoop) { 68444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson if (SubLoop == 0) return true; 68544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson if (SubLoop == ParentLoop) return false; 68644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop); 68744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 68844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 689d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson void Calculate(DominatorTreeBase<BlockT> &DT) { 69044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson BlockT *RootNode = DT.getRootNode()->getBlock(); 69144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 69244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson for (df_iterator<BlockT*> NI = df_begin(RootNode), 69344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson NE = df_end(RootNode); NI != NE; ++NI) 694c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman if (LoopT *L = ConsiderForLoop(*NI, DT)) 69544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson TopLevelLoops.push_back(L); 69644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 69744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 698c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopT *ConsiderForLoop(BlockT *BB, DominatorTreeBase<BlockT> &DT) { 69944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson if (BBMap.find(BB) != BBMap.end()) return 0;// Haven't processed this node? 70044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 70144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson std::vector<BlockT *> TodoStack; 70244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 70344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // Scan the predecessors of BB, checking to see if BB dominates any of 70444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // them. This identifies backedges which target this node... 705d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits; 706d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson for (typename InvBlockTraits::ChildIteratorType I = 707d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson InvBlockTraits::child_begin(BB), E = InvBlockTraits::child_end(BB); 708d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson I != E; ++I) 70944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson if (DT.dominates(BB, *I)) // If BB dominates it's predecessor... 71044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson TodoStack.push_back(*I); 71144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 71244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson if (TodoStack.empty()) return 0; // No backedges to this block... 71344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 71444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // Create a new loop to represent this basic block... 715c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopT *L = new LoopT(BB); 71644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson BBMap[BB] = L; 71744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 718e4ad9c70e4a1261c212b11623d99e2477ef02783Owen Anderson BlockT *EntryBlock = BB->getParent()->begin(); 71944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 72044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson while (!TodoStack.empty()) { // Process all the nodes in the loop 72144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson BlockT *X = TodoStack.back(); 72244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson TodoStack.pop_back(); 72344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 72444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson if (!L->contains(X) && // As of yet unprocessed?? 72544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson DT.dominates(EntryBlock, X)) { // X is reachable from entry block? 72644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // Check to see if this block already belongs to a loop. If this occurs 727131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner // then we have a case where a loop that is supposed to be a child of 728131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner // the current loop was processed before the current loop. When this 729131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner // occurs, this child loop gets added to a part of the current loop, 730131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner // making it a sibling to the current loop. We have to reparent this 731131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner // loop. 732c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman if (LoopT *SubLoop = 733c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman const_cast<LoopT *>(getLoopFor(X))) 734131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner if (SubLoop->getHeader() == X && isNotAlreadyContainedIn(SubLoop, L)){ 73544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // Remove the subloop from it's current parent... 73644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson assert(SubLoop->ParentLoop && SubLoop->ParentLoop != L); 737c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopT *SLP = SubLoop->ParentLoop; // SubLoopParent 738c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman typename std::vector<LoopT *>::iterator I = 73944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson std::find(SLP->SubLoops.begin(), SLP->SubLoops.end(), SubLoop); 740131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner assert(I != SLP->SubLoops.end() &&"SubLoop not a child of parent?"); 74144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson SLP->SubLoops.erase(I); // Remove from parent... 74244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 74344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // Add the subloop to THIS loop... 74444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson SubLoop->ParentLoop = L; 74544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson L->SubLoops.push_back(SubLoop); 74644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 74744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 74844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // Normal case, add the block to our loop... 74944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson L->Blocks.push_back(X); 750d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson 751d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits; 752d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson 75344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // Add all of the predecessors of X to the end of the work stack... 754d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson TodoStack.insert(TodoStack.end(), InvBlockTraits::child_begin(X), 755d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson InvBlockTraits::child_end(X)); 75644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 75744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 75844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 75944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // If there are any loops nested within this loop, create them now! 76044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(), 76144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson E = L->Blocks.end(); I != E; ++I) 762c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman if (LoopT *NewLoop = ConsiderForLoop(*I, DT)) { 76344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson L->SubLoops.push_back(NewLoop); 76444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson NewLoop->ParentLoop = L; 76544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 76644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 76744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // Add the basic blocks that comprise this loop to the BBMap so that this 76844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // loop can be found for them. 76944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // 77044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(), 77144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson E = L->Blocks.end(); I != E; ++I) { 772c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman typename std::map<BlockT*, LoopT *>::iterator BBMI = BBMap.find(*I); 773c418bf3dd593b5b2fe2f978930f6d0d6b17e344eDan Gohman if (BBMI == BBMap.end()) // Not in map yet... 77444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson BBMap.insert(BBMI, std::make_pair(*I, L)); // Must be at this level 77544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 77644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 77744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // Now that we have a list of all of the child loops of this loop, check to 778131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner // see if any of them should actually be nested inside of each other. We 779131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner // can accidentally pull loops our of their parents, so we must make sure to 78044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // organize the loop nests correctly now. 78144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson { 782c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman std::map<BlockT *, LoopT *> ContainingLoops; 78344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson for (unsigned i = 0; i != L->SubLoops.size(); ++i) { 784c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopT *Child = L->SubLoops[i]; 78544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson assert(Child->getParentLoop() == L && "Not proper child loop?"); 78644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 787c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman if (LoopT *ContainingLoop = ContainingLoops[Child->getHeader()]) { 78844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // If there is already a loop which contains this loop, move this loop 78944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // into the containing loop. 79044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson MoveSiblingLoopInto(Child, ContainingLoop); 79144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson --i; // The loop got removed from the SubLoops list. 79244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } else { 793131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner // This is currently considered to be a top-level loop. Check to see 794131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner // if any of the contained blocks are loop headers for subloops we 795131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner // have already processed. 79644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson for (unsigned b = 0, e = Child->Blocks.size(); b != e; ++b) { 797c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopT *&BlockLoop = ContainingLoops[Child->Blocks[b]]; 79844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson if (BlockLoop == 0) { // Child block not processed yet... 79944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson BlockLoop = Child; 80044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } else if (BlockLoop != Child) { 801c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopT *SubLoop = BlockLoop; 80244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // Reparent all of the blocks which used to belong to BlockLoops 80344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson for (unsigned j = 0, e = SubLoop->Blocks.size(); j != e; ++j) 80444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson ContainingLoops[SubLoop->Blocks[j]] = Child; 80544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 80644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // There is already a loop which contains this block, that means 80744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // that we should reparent the loop which the block is currently 80844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // considered to belong to to be a child of this loop. 80944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson MoveSiblingLoopInto(SubLoop, Child); 81044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson --i; // We just shrunk the SubLoops list. 81144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 81244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 81344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 81444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 81544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 81644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 81744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson return L; 81844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 81944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 820131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner /// MoveSiblingLoopInto - This method moves the NewChild loop to live inside 821131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner /// of the NewParent Loop, instead of being a sibling of it. 822c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman void MoveSiblingLoopInto(LoopT *NewChild, 823c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopT *NewParent) { 824c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopT *OldParent = NewChild->getParentLoop(); 82544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson assert(OldParent && OldParent == NewParent->getParentLoop() && 82644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson NewChild != NewParent && "Not sibling loops!"); 82744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 82844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // Remove NewChild from being a child of OldParent 829c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman typename std::vector<LoopT *>::iterator I = 830131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner std::find(OldParent->SubLoops.begin(), OldParent->SubLoops.end(), 831131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner NewChild); 83244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson assert(I != OldParent->SubLoops.end() && "Parent fields incorrect??"); 83344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson OldParent->SubLoops.erase(I); // Remove from parent's subloops list 83444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson NewChild->ParentLoop = 0; 83544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 83644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson InsertLoopInto(NewChild, NewParent); 83744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 83844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 839131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner /// InsertLoopInto - This inserts loop L into the specified parent loop. If 840131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner /// the parent loop contains a loop which should contain L, the loop gets 841131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner /// inserted into L instead. 842c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman void InsertLoopInto(LoopT *L, LoopT *Parent) { 84344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson BlockT *LHeader = L->getHeader(); 844131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner assert(Parent->contains(LHeader) && 845131bd2ecf721749666111ec2dd1d32a20ae049b2Chris Lattner "This loop should not be inserted here!"); 84644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 84744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // Check to see if it belongs in a child loop... 84834cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng for (unsigned i = 0, e = static_cast<unsigned>(Parent->SubLoops.size()); 84934cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng i != e; ++i) 85044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson if (Parent->SubLoops[i]->contains(LHeader)) { 85144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson InsertLoopInto(L, Parent->SubLoops[i]); 85244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson return; 85344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 85444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 85544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // If not, insert it here! 85644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson Parent->SubLoops.push_back(L); 85744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson L->ParentLoop = Parent; 85844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 85944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 86044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // Debugging 86144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 86244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson void print(std::ostream &OS, const Module* ) const { 86344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson for (unsigned i = 0; i < TopLevelLoops.size(); ++i) 86444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson TopLevelLoops[i]->print(OS); 86544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson #if 0 866c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman for (std::map<BasicBlock*, LoopT*>::const_iterator I = BBMap.begin(), 86744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson E = BBMap.end(); I != E; ++I) 86844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson OS << "BB '" << I->first->getName() << "' level = " 86944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson << I->second->getLoopDepth() << "\n"; 87044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson #endif 87144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 87244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson}; 87344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 87444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonclass LoopInfo : public FunctionPass { 875c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopInfoBase<BasicBlock, Loop> LI; 876c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman friend class LoopBase<BasicBlock, Loop>; 8779d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman 8789d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman void operator=(const LoopInfo &); // do not implement 8799d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman LoopInfo(const LoopInfo &); // do not implement 88044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Andersonpublic: 88144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson static char ID; // Pass identification, replacement for typeid 88244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 8839d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman LoopInfo() : FunctionPass(&ID) {} 88444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 885c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopInfoBase<BasicBlock, Loop>& getBase() { return LI; } 886d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson 88744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// iterator/begin/end - The interface to the top-level loops in the current 88844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// function. 88944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// 890c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman typedef LoopInfoBase<BasicBlock, Loop>::iterator iterator; 8919d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman inline iterator begin() const { return LI.begin(); } 8929d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman inline iterator end() const { return LI.end(); } 8939d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman bool empty() const { return LI.empty(); } 89444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 89544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// getLoopFor - Return the inner most loop that BB lives in. If a basic 89644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// block is in no loop (for example the entry node), null is returned. 89744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// 89844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson inline Loop *getLoopFor(const BasicBlock *BB) const { 8999d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman return LI.getLoopFor(BB); 90044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 90144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 90244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// operator[] - same as getLoopFor... 90344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// 90444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson inline const Loop *operator[](const BasicBlock *BB) const { 9059d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman return LI.getLoopFor(BB); 90644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 90744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 908ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman /// getLoopDepth - Return the loop nesting level of the specified block. A 909ba42d2b937160c970c8c6ea57573113c9265325fDan Gohman /// depth of 0 means the block is not inside any loop. 91044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson /// 91144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson inline unsigned getLoopDepth(const BasicBlock *BB) const { 9129d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman return LI.getLoopDepth(BB); 91344a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 91444a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 91544a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson // isLoopHeader - True if the block is a loop header node 91644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson inline bool isLoopHeader(BasicBlock *BB) const { 9179d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman return LI.isLoopHeader(BB); 91844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 9190bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner 9202b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner /// runOnFunction - Calculate the natural loop information. 9212b7bb7a986545b5ec877416278dc126a35ab6970Chris Lattner /// 9227e70829632f82de15db187845666aaca6e04b792Chris Lattner virtual bool runOnFunction(Function &F); 923facd752d3afaeca7dee46648f2a2ae209a94e5e9Chris Lattner 9249d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman virtual void releaseMemory() { LI.releaseMemory(); } 9255c7e326585f3a543388ba871c3425f7664cd9143Bill Wendling 92644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson virtual void print(std::ostream &O, const Module* M = 0) const { 9279d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman LI.print(O, M); 9285c7e326585f3a543388ba871c3425f7664cd9143Bill Wendling } 929918c4ecb0c1c85adad760fb9d7faae088171d324Chris Lattner 930f57b845547302d24ecb6a9e79d7bc386f761a6c9Chris Lattner virtual void getAnalysisUsage(AnalysisUsage &AU) const; 931f57b845547302d24ecb6a9e79d7bc386f761a6c9Chris Lattner 9324e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner /// removeLoop - This removes the specified top-level loop from this loop info 9334e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner /// object. The loop is not deleted, as it will presumably be inserted into 9344e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner /// another loop. 9359d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman inline Loop *removeLoop(iterator I) { return LI.removeLoop(I); } 9364e55b7d2c62de7efa0147e0579980de8b1df9123Chris Lattner 93746758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// changeLoopFor - Change the top-level loop that contains BB to the 93846758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// specified loop. This should be used by transformations that restructure 93946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// the loop hierarchy tree. 94044a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson inline void changeLoopFor(BasicBlock *BB, Loop *L) { 9419d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman LI.changeLoopFor(BB, L); 94244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 94346758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner 94446758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// changeTopLevelLoop - Replace the specified loop in the top-level loops 94546758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner /// list with the indicated loop. 94644a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson inline void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop) { 9479d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman LI.changeTopLevelLoop(OldLoop, NewLoop); 94844a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 94946758a894f5d9ca7adc8ec03dd6adeb36b7eadb3Chris Lattner 950072b163424491c85df6664a4e056aae5e07dc64dChris Lattner /// addTopLevelLoop - This adds the specified loop to the collection of 951072b163424491c85df6664a4e056aae5e07dc64dChris Lattner /// top-level loops. 95244a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson inline void addTopLevelLoop(Loop *New) { 9539d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman LI.addTopLevelLoop(New); 954072b163424491c85df6664a4e056aae5e07dc64dChris Lattner } 955072b163424491c85df6664a4e056aae5e07dc64dChris Lattner 9569afb24bf0847b9f2ff0bf3f7f7405dcbe42fa38bChris Lattner /// removeBlock - This method completely removes BB from all data structures, 9579afb24bf0847b9f2ff0bf3f7f7405dcbe42fa38bChris Lattner /// including all of the Loop objects it is nested in and our mapping from 9589afb24bf0847b9f2ff0bf3f7f7405dcbe42fa38bChris Lattner /// BasicBlocks to loops. 95944a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson void removeBlock(BasicBlock *BB) { 9609d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman LI.removeBlock(BB); 96144a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson } 962c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman 963c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman static bool isNotAlreadyContainedIn(const Loop *SubLoop, 964c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman const Loop *ParentLoop) { 965c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman return 966c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopInfoBase<BasicBlock, Loop>::isNotAlreadyContainedIn(SubLoop, 967c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman ParentLoop); 968c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman } 9690bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner}; 9700bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner 971e0b6b78e095f7dea9589e8df5ec4521e346ad005Anand Shukla 9721db0a400370466e187ae06c96a1586c2c21409ddChris Lattner// Allow clients to walk the list of nested loops... 9731db0a400370466e187ae06c96a1586c2c21409ddChris Lattnertemplate <> struct GraphTraits<const Loop*> { 9741db0a400370466e187ae06c96a1586c2c21409ddChris Lattner typedef const Loop NodeType; 9759d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman typedef LoopInfo::iterator ChildIteratorType; 9761db0a400370466e187ae06c96a1586c2c21409ddChris Lattner 9771db0a400370466e187ae06c96a1586c2c21409ddChris Lattner static NodeType *getEntryNode(const Loop *L) { return L; } 9789769ab22265b313171d201b5928688524a01bd87Misha Brukman static inline ChildIteratorType child_begin(NodeType *N) { 979329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner return N->begin(); 9801db0a400370466e187ae06c96a1586c2c21409ddChris Lattner } 9819769ab22265b313171d201b5928688524a01bd87Misha Brukman static inline ChildIteratorType child_end(NodeType *N) { 982329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner return N->end(); 9831db0a400370466e187ae06c96a1586c2c21409ddChris Lattner } 9841db0a400370466e187ae06c96a1586c2c21409ddChris Lattner}; 9851db0a400370466e187ae06c96a1586c2c21409ddChris Lattner 9861db0a400370466e187ae06c96a1586c2c21409ddChris Lattnertemplate <> struct GraphTraits<Loop*> { 9871db0a400370466e187ae06c96a1586c2c21409ddChris Lattner typedef Loop NodeType; 9889d59d9f8495b0361c9ffd1dc82888d8e7ba5070eDan Gohman typedef LoopInfo::iterator ChildIteratorType; 9891db0a400370466e187ae06c96a1586c2c21409ddChris Lattner 9901db0a400370466e187ae06c96a1586c2c21409ddChris Lattner static NodeType *getEntryNode(Loop *L) { return L; } 9919769ab22265b313171d201b5928688524a01bd87Misha Brukman static inline ChildIteratorType child_begin(NodeType *N) { 992329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner return N->begin(); 9931db0a400370466e187ae06c96a1586c2c21409ddChris Lattner } 9949769ab22265b313171d201b5928688524a01bd87Misha Brukman static inline ChildIteratorType child_end(NodeType *N) { 995329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner return N->end(); 9961db0a400370466e187ae06c96a1586c2c21409ddChris Lattner } 9971db0a400370466e187ae06c96a1586c2c21409ddChris Lattner}; 9981db0a400370466e187ae06c96a1586c2c21409ddChris Lattner 999c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmantemplate<class BlockT, class LoopT> 1000c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohmanvoid 1001c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan GohmanLoopBase<BlockT, LoopT>::addBasicBlockToLoop(BlockT *NewBB, 1002c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopInfoBase<BlockT, LoopT> &LIB) { 1003d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson assert((Blocks.empty() || LIB[getHeader()] == this) && 1004019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson "Incorrect LI specified for this loop!"); 1005019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson assert(NewBB && "Cannot add a null basic block to the loop!"); 1006d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson assert(LIB[NewBB] == 0 && "BasicBlock already in the loop!"); 100744a95e06cc0bb3a2d617fe94235aee92b1951910Owen Anderson 1008c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LoopT *L = static_cast<LoopT *>(this); 1009c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman 1010019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // Add the loop mapping to the LoopInfo object... 1011c8d76d5afb023a1c6b439941be3b62789fcc0ed3Dan Gohman LIB.BBMap[NewBB] = L; 1012019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 1013019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson // Add the basic block to this loop and all parent loops... 1014019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson while (L) { 1015019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson L->Blocks.push_back(NewBB); 1016019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson L = L->getParentLoop(); 1017019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson } 1018019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson} 1019019b92a70c11319f5ab96c9f5e66e4e111a972f8Owen Anderson 1020d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace 1021d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 10220bbe58f073b4b4a6f68b3e2ee6074fc314e8d19fChris Lattner#endif 1023