LoopInfo.h revision d91cbf352a5f25d602667445bcbb132705da286a
1//===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the LoopInfo class that is used to identify natural loops 11// and determine the loop depth of various nodes of the CFG. Note that natural 12// loops may actually be several loops that share the same header node. 13// 14// This analysis calculates the nesting structure of loops in a function. For 15// each natural loop identified, this analysis identifies natural loops 16// contained entirely within the loop and the basic blocks the make up the loop. 17// 18// It can calculate on the fly various bits of information, for example: 19// 20// * whether there is a preheader for the loop 21// * the number of back edges to the header 22// * whether or not a particular block branches out of the loop 23// * the successor blocks of the loop 24// * the loop depth 25// * the trip count 26// * etc... 27// 28//===----------------------------------------------------------------------===// 29 30#ifndef LLVM_ANALYSIS_LOOP_INFO_H 31#define LLVM_ANALYSIS_LOOP_INFO_H 32 33#include "llvm/Pass.h" 34#include "llvm/Constants.h" 35#include "llvm/Instructions.h" 36#include "llvm/ADT/DepthFirstIterator.h" 37#include "llvm/ADT/GraphTraits.h" 38#include "llvm/ADT/SmallPtrSet.h" 39#include "llvm/ADT/SmallVector.h" 40#include "llvm/Analysis/Dominators.h" 41#include "llvm/Support/CFG.h" 42#include "llvm/Support/Streams.h" 43#include <algorithm> 44#include <ostream> 45 46template<typename T> 47static void RemoveFromVector(std::vector<T*> &V, T *N) { 48 typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N); 49 assert(I != V.end() && "N is not in this list!"); 50 V.erase(I); 51} 52 53namespace llvm { 54 55class DominatorTree; 56class LoopInfo; 57class PHINode; 58class Instruction; 59template<class N> class LoopInfoBase; 60 61//===----------------------------------------------------------------------===// 62/// LoopBase class - Instances of this class are used to represent loops that are 63/// detected in the flow graph 64/// 65template<class BlockT> 66class LoopBase { 67 LoopBase<BlockT> *ParentLoop; 68 std::vector<LoopBase<BlockT>*> SubLoops; // Loops contained entirely within this one 69 std::vector<BlockT*> Blocks; // First entry is the header node 70 71 LoopBase(const LoopBase<BlockT> &); // DO NOT IMPLEMENT 72 const LoopBase<BlockT> &operator=(const LoopBase<BlockT> &); // DO NOT IMPLEMENT 73public: 74 /// Loop ctor - This creates an empty loop. 75 LoopBase() : ParentLoop(0) {} 76 ~LoopBase() { 77 for (unsigned i = 0, e = SubLoops.size(); i != e; ++i) 78 delete SubLoops[i]; 79 } 80 81 unsigned getLoopDepth() const { 82 unsigned D = 0; 83 for (const LoopBase<BlockT> *CurLoop = this; CurLoop; 84 CurLoop = CurLoop->ParentLoop) 85 ++D; 86 return D; 87 } 88 BlockT *getHeader() const { return Blocks.front(); } 89 LoopBase<BlockT> *getParentLoop() const { return ParentLoop; } 90 91 /// contains - Return true of the specified basic block is in this loop 92 /// 93 bool contains(const BlockT *BB) const { 94 return std::find(Blocks.begin(), Blocks.end(), BB) != Blocks.end(); 95 } 96 97 /// iterator/begin/end - Return the loops contained entirely within this loop. 98 /// 99 const std::vector<LoopBase<BlockT>*> &getSubLoops() const { return SubLoops; } 100 typedef typename std::vector<LoopBase<BlockT>*>::const_iterator iterator; 101 iterator begin() const { return SubLoops.begin(); } 102 iterator end() const { return SubLoops.end(); } 103 bool empty() const { return SubLoops.empty(); } 104 105 /// getBlocks - Get a list of the basic blocks which make up this loop. 106 /// 107 const std::vector<BlockT*> &getBlocks() const { return Blocks; } 108 typedef typename std::vector<BlockT*>::const_iterator block_iterator; 109 block_iterator block_begin() const { return Blocks.begin(); } 110 block_iterator block_end() const { return Blocks.end(); } 111 112 /// isLoopExit - True if terminator in the block can branch to another block 113 /// that is outside of the current loop. 114 /// 115 bool isLoopExit(const BlockT *BB) const { 116 for (succ_const_iterator SI = succ_begin(BB), SE = succ_end(BB); 117 SI != SE; ++SI) { 118 if (!contains(*SI)) 119 return true; 120 } 121 return false; 122 } 123 124 /// getNumBackEdges - Calculate the number of back edges to the loop header 125 /// 126 unsigned getNumBackEdges() const { 127 unsigned NumBackEdges = 0; 128 BlockT *H = getHeader(); 129 130 for (pred_iterator I = pred_begin(H), E = pred_end(H); I != E; ++I) 131 if (contains(*I)) 132 ++NumBackEdges; 133 134 return NumBackEdges; 135 } 136 137 /// isLoopInvariant - Return true if the specified value is loop invariant 138 /// 139 bool isLoopInvariant(Value *V) const { 140 if (Instruction *I = dyn_cast<Instruction>(V)) 141 return !contains(I->getParent()); 142 return true; // All non-instructions are loop invariant 143 } 144 145 //===--------------------------------------------------------------------===// 146 // APIs for simple analysis of the loop. 147 // 148 // Note that all of these methods can fail on general loops (ie, there may not 149 // be a preheader, etc). For best success, the loop simplification and 150 // induction variable canonicalization pass should be used to normalize loops 151 // for easy analysis. These methods assume canonical loops. 152 153 /// getExitingBlocks - Return all blocks inside the loop that have successors 154 /// outside of the loop. These are the blocks _inside of the current loop_ 155 /// which branch out. The returned list is always unique. 156 /// 157 void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const { 158 // Sort the blocks vector so that we can use binary search to do quick 159 // lookups. 160 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end()); 161 std::sort(LoopBBs.begin(), LoopBBs.end()); 162 163 for (typename std::vector<BlockT*>::const_iterator BI = Blocks.begin(), 164 BE = Blocks.end(); BI != BE; ++BI) 165 for (succ_iterator I = succ_begin(*BI), E = succ_end(*BI); I != E; ++I) 166 if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) { 167 // Not in current loop? It must be an exit block. 168 ExitingBlocks.push_back(*BI); 169 break; 170 } 171 } 172 173 /// getExitBlocks - Return all of the successor blocks of this loop. These 174 /// are the blocks _outside of the current loop_ which are branched to. 175 /// 176 void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const { 177 // Sort the blocks vector so that we can use binary search to do quick 178 // lookups. 179 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end()); 180 std::sort(LoopBBs.begin(), LoopBBs.end()); 181 182 for (typename std::vector<BlockT*>::const_iterator BI = Blocks.begin(), 183 BE = Blocks.end(); BI != BE; ++BI) 184 for (succ_iterator I = succ_begin(*BI), E = succ_end(*BI); I != E; ++I) 185 if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) 186 // Not in current loop? It must be an exit block. 187 ExitBlocks.push_back(*I); 188 } 189 190 /// getUniqueExitBlocks - Return all unique successor blocks of this loop. 191 /// These are the blocks _outside of the current loop_ which are branched to. 192 /// This assumes that loop is in canonical form. 193 /// 194 void getUniqueExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const { 195 // Sort the blocks vector so that we can use binary search to do quick 196 // lookups. 197 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end()); 198 std::sort(LoopBBs.begin(), LoopBBs.end()); 199 200 std::vector<BlockT*> switchExitBlocks; 201 202 for (typename std::vector<BlockT*>::const_iterator BI = Blocks.begin(), 203 BE = Blocks.end(); BI != BE; ++BI) { 204 205 BlockT *current = *BI; 206 switchExitBlocks.clear(); 207 208 for (succ_iterator I = succ_begin(*BI), E = succ_end(*BI); I != E; ++I) { 209 if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) 210 // If block is inside the loop then it is not a exit block. 211 continue; 212 213 pred_iterator PI = pred_begin(*I); 214 BlockT *firstPred = *PI; 215 216 // If current basic block is this exit block's first predecessor 217 // then only insert exit block in to the output ExitBlocks vector. 218 // This ensures that same exit block is not inserted twice into 219 // ExitBlocks vector. 220 if (current != firstPred) 221 continue; 222 223 // If a terminator has more then two successors, for example SwitchInst, 224 // then it is possible that there are multiple edges from current block 225 // to one exit block. 226 if (current->getTerminator()->getNumSuccessors() <= 2) { 227 ExitBlocks.push_back(*I); 228 continue; 229 } 230 231 // In case of multiple edges from current block to exit block, collect 232 // only one edge in ExitBlocks. Use switchExitBlocks to keep track of 233 // duplicate edges. 234 if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I) 235 == switchExitBlocks.end()) { 236 switchExitBlocks.push_back(*I); 237 ExitBlocks.push_back(*I); 238 } 239 } 240 } 241 } 242 243 /// getLoopPreheader - If there is a preheader for this loop, return it. A 244 /// loop has a preheader if there is only one edge to the header of the loop 245 /// from outside of the loop. If this is the case, the block branching to the 246 /// header of the loop is the preheader node. 247 /// 248 /// This method returns null if there is no preheader for the loop. 249 /// 250 BlockT *getLoopPreheader() const { 251 // Keep track of nodes outside the loop branching to the header... 252 BlockT *Out = 0; 253 254 // Loop over the predecessors of the header node... 255 BlockT *Header = getHeader(); 256 for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header); 257 PI != PE; ++PI) 258 if (!contains(*PI)) { // If the block is not in the loop... 259 if (Out && Out != *PI) 260 return 0; // Multiple predecessors outside the loop 261 Out = *PI; 262 } 263 264 // Make sure there is only one exit out of the preheader. 265 assert(Out && "Header of loop has no predecessors from outside loop?"); 266 succ_iterator SI = succ_begin(Out); 267 ++SI; 268 if (SI != succ_end(Out)) 269 return 0; // Multiple exits from the block, must not be a preheader. 270 271 // If there is exactly one preheader, return it. If there was zero, then Out 272 // is still null. 273 return Out; 274 } 275 276 /// getLoopLatch - If there is a latch block for this loop, return it. A 277 /// latch block is the canonical backedge for a loop. A loop header in normal 278 /// form has two edges into it: one from a preheader and one from a latch 279 /// block. 280 BlockT *getLoopLatch() const { 281 BlockT *Header = getHeader(); 282 pred_iterator PI = pred_begin(Header), PE = pred_end(Header); 283 if (PI == PE) return 0; // no preds? 284 285 BlockT *Latch = 0; 286 if (contains(*PI)) 287 Latch = *PI; 288 ++PI; 289 if (PI == PE) return 0; // only one pred? 290 291 if (contains(*PI)) { 292 if (Latch) return 0; // multiple backedges 293 Latch = *PI; 294 } 295 ++PI; 296 if (PI != PE) return 0; // more than two preds 297 298 return Latch; 299 } 300 301 /// getCanonicalInductionVariable - Check to see if the loop has a canonical 302 /// induction variable: an integer recurrence that starts at 0 and increments 303 /// by one each time through the loop. If so, return the phi node that 304 /// corresponds to it. 305 /// 306 PHINode *getCanonicalInductionVariable() const { 307 BlockT *H = getHeader(); 308 309 BlockT *Incoming = 0, *Backedge = 0; 310 pred_iterator PI = pred_begin(H); 311 assert(PI != pred_end(H) && "Loop must have at least one backedge!"); 312 Backedge = *PI++; 313 if (PI == pred_end(H)) return 0; // dead loop 314 Incoming = *PI++; 315 if (PI != pred_end(H)) return 0; // multiple backedges? 316 317 if (contains(Incoming)) { 318 if (contains(Backedge)) 319 return 0; 320 std::swap(Incoming, Backedge); 321 } else if (!contains(Backedge)) 322 return 0; 323 324 // Loop over all of the PHI nodes, looking for a canonical indvar. 325 for (typename BlockT::iterator I = H->begin(); isa<PHINode>(I); ++I) { 326 PHINode *PN = cast<PHINode>(I); 327 if (Instruction *Inc = 328 dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge))) 329 if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN) 330 if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1))) 331 if (CI->equalsInt(1)) 332 return PN; 333 } 334 return 0; 335 } 336 337 /// getCanonicalInductionVariableIncrement - Return the LLVM value that holds 338 /// the canonical induction variable value for the "next" iteration of the 339 /// loop. This always succeeds if getCanonicalInductionVariable succeeds. 340 /// 341 Instruction *getCanonicalInductionVariableIncrement() const { 342 if (PHINode *PN = getCanonicalInductionVariable()) { 343 bool P1InLoop = contains(PN->getIncomingBlock(1)); 344 return cast<Instruction>(PN->getIncomingValue(P1InLoop)); 345 } 346 return 0; 347 } 348 349 /// getTripCount - Return a loop-invariant LLVM value indicating the number of 350 /// times the loop will be executed. Note that this means that the backedge 351 /// of the loop executes N-1 times. If the trip-count cannot be determined, 352 /// this returns null. 353 /// 354 Value *getTripCount() const { 355 // Canonical loops will end with a 'cmp ne I, V', where I is the incremented 356 // canonical induction variable and V is the trip count of the loop. 357 Instruction *Inc = getCanonicalInductionVariableIncrement(); 358 if (Inc == 0) return 0; 359 PHINode *IV = cast<PHINode>(Inc->getOperand(0)); 360 361 BlockT *BackedgeBlock = 362 IV->getIncomingBlock(contains(IV->getIncomingBlock(1))); 363 364 if (BranchInst *BI = dyn_cast<BranchInst>(BackedgeBlock->getTerminator())) 365 if (BI->isConditional()) { 366 if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) { 367 if (ICI->getOperand(0) == Inc) 368 if (BI->getSuccessor(0) == getHeader()) { 369 if (ICI->getPredicate() == ICmpInst::ICMP_NE) 370 return ICI->getOperand(1); 371 } else if (ICI->getPredicate() == ICmpInst::ICMP_EQ) { 372 return ICI->getOperand(1); 373 } 374 } 375 } 376 377 return 0; 378 } 379 380 /// isLCSSAForm - Return true if the Loop is in LCSSA form 381 bool isLCSSAForm() const { 382 // Sort the blocks vector so that we can use binary search to do quick 383 // lookups. 384 SmallPtrSet<BlockT*, 16> LoopBBs(block_begin(), block_end()); 385 386 for (block_iterator BI = block_begin(), E = block_end(); BI != E; ++BI) { 387 BlockT *BB = *BI; 388 for (typename BlockT::iterator I = BB->begin(), E = BB->end(); I != E; ++I) 389 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; 390 ++UI) { 391 BlockT *UserBB = cast<Instruction>(*UI)->getParent(); 392 if (PHINode *P = dyn_cast<PHINode>(*UI)) { 393 unsigned OperandNo = UI.getOperandNo(); 394 UserBB = P->getIncomingBlock(OperandNo/2); 395 } 396 397 // Check the current block, as a fast-path. Most values are used in the 398 // same block they are defined in. 399 if (UserBB != BB && !LoopBBs.count(UserBB)) 400 return false; 401 } 402 } 403 404 return true; 405 } 406 407 //===--------------------------------------------------------------------===// 408 // APIs for updating loop information after changing the CFG 409 // 410 411 /// addBasicBlockToLoop - This method is used by other analyses to update loop 412 /// information. NewBB is set to be a new member of the current loop. 413 /// Because of this, it is added as a member of all parent loops, and is added 414 /// to the specified LoopInfo object as being in the current basic block. It 415 /// is not valid to replace the loop header with this method. 416 /// 417 void addBasicBlockToLoop(BlockT *NewBB, LoopInfo &LI); 418 419 /// replaceChildLoopWith - This is used when splitting loops up. It replaces 420 /// the OldChild entry in our children list with NewChild, and updates the 421 /// parent pointer of OldChild to be null and the NewChild to be this loop. 422 /// This updates the loop depth of the new child. 423 void replaceChildLoopWith(LoopBase<BlockT> *OldChild, 424 LoopBase<BlockT> *NewChild) { 425 assert(OldChild->ParentLoop == this && "This loop is already broken!"); 426 assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!"); 427 typename std::vector<LoopBase<BlockT>*>::iterator I = 428 std::find(SubLoops.begin(), SubLoops.end(), OldChild); 429 assert(I != SubLoops.end() && "OldChild not in loop!"); 430 *I = NewChild; 431 OldChild->ParentLoop = 0; 432 NewChild->ParentLoop = this; 433 } 434 435 /// addChildLoop - Add the specified loop to be a child of this loop. This 436 /// updates the loop depth of the new child. 437 /// 438 void addChildLoop(LoopBase<BlockT> *NewChild) { 439 assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!"); 440 NewChild->ParentLoop = this; 441 SubLoops.push_back(NewChild); 442 } 443 444 /// removeChildLoop - This removes the specified child from being a subloop of 445 /// this loop. The loop is not deleted, as it will presumably be inserted 446 /// into another loop. 447 LoopBase<BlockT> *removeChildLoop(iterator I) { 448 assert(I != SubLoops.end() && "Cannot remove end iterator!"); 449 LoopBase<BlockT> *Child = *I; 450 assert(Child->ParentLoop == this && "Child is not a child of this loop!"); 451 SubLoops.erase(SubLoops.begin()+(I-begin())); 452 Child->ParentLoop = 0; 453 return Child; 454 } 455 456 /// addBlockEntry - This adds a basic block directly to the basic block list. 457 /// This should only be used by transformations that create new loops. Other 458 /// transformations should use addBasicBlockToLoop. 459 void addBlockEntry(BlockT *BB) { 460 Blocks.push_back(BB); 461 } 462 463 /// moveToHeader - This method is used to move BB (which must be part of this 464 /// loop) to be the loop header of the loop (the block that dominates all 465 /// others). 466 void moveToHeader(BlockT *BB) { 467 if (Blocks[0] == BB) return; 468 for (unsigned i = 0; ; ++i) { 469 assert(i != Blocks.size() && "Loop does not contain BB!"); 470 if (Blocks[i] == BB) { 471 Blocks[i] = Blocks[0]; 472 Blocks[0] = BB; 473 return; 474 } 475 } 476 } 477 478 /// removeBlockFromLoop - This removes the specified basic block from the 479 /// current loop, updating the Blocks as appropriate. This does not update 480 /// the mapping in the LoopInfo class. 481 void removeBlockFromLoop(BlockT *BB) { 482 RemoveFromVector(Blocks, BB); 483 } 484 485 /// verifyLoop - Verify loop structure 486 void verifyLoop() const { 487#ifndef NDEBUG 488 assert (getHeader() && "Loop header is missing"); 489 assert (getLoopPreheader() && "Loop preheader is missing"); 490 assert (getLoopLatch() && "Loop latch is missing"); 491 for (typename std::vector<LoopBase<BlockT>*>::const_iterator I = 492 SubLoops.begin(), E = SubLoops.end(); I != E; ++I) 493 (*I)->verifyLoop(); 494#endif 495 } 496 497 void print(std::ostream &OS, unsigned Depth = 0) const { 498 OS << std::string(Depth*2, ' ') << "Loop Containing: "; 499 500 for (unsigned i = 0; i < getBlocks().size(); ++i) { 501 if (i) OS << ","; 502 WriteAsOperand(OS, getBlocks()[i], false); 503 } 504 OS << "\n"; 505 506 for (iterator I = begin(), E = end(); I != E; ++I) 507 (*I)->print(OS, Depth+2); 508 } 509 510 void print(std::ostream *O, unsigned Depth = 0) const { 511 if (O) print(*O, Depth); 512 } 513 514 void dump() const { 515 print(cerr); 516 } 517 518private: 519 friend class LoopInfoBase<BlockT>; 520 LoopBase(BlockT *BB) : ParentLoop(0) { 521 Blocks.push_back(BB); 522 } 523}; 524 525typedef LoopBase<BasicBlock> Loop; 526 527 528//===----------------------------------------------------------------------===// 529/// LoopInfo - This class builds and contains all of the top level loop 530/// structures in the specified function. 531/// 532 533template<class BlockT> 534class LoopInfoBase { 535 // BBMap - Mapping of basic blocks to the inner most loop they occur in 536 std::map<BlockT*, Loop*> BBMap; 537 std::vector<LoopBase<BlockT>*> TopLevelLoops; 538 friend class LoopBase<BlockT>; 539 540public: 541 LoopInfoBase() { } 542 ~LoopInfoBase() { releaseMemory(); } 543 544 void releaseMemory() { 545 for (typename std::vector<LoopBase<BlockT>* >::iterator I = 546 TopLevelLoops.begin(), E = TopLevelLoops.end(); I != E; ++I) 547 delete *I; // Delete all of the loops... 548 549 BBMap.clear(); // Reset internal state of analysis 550 TopLevelLoops.clear(); 551 } 552 553 /// iterator/begin/end - The interface to the top-level loops in the current 554 /// function. 555 /// 556 typedef typename std::vector<LoopBase<BlockT>*>::const_iterator iterator; 557 iterator begin() const { return TopLevelLoops.begin(); } 558 iterator end() const { return TopLevelLoops.end(); } 559 560 /// getLoopFor - Return the inner most loop that BB lives in. If a basic 561 /// block is in no loop (for example the entry node), null is returned. 562 /// 563 LoopBase<BlockT> *getLoopFor(const BlockT *BB) const { 564 typename std::map<BlockT *, LoopBase<BlockT>*>::const_iterator I= 565 BBMap.find(const_cast<BasicBlock*>(BB)); 566 return I != BBMap.end() ? I->second : 0; 567 } 568 569 /// operator[] - same as getLoopFor... 570 /// 571 const LoopBase<BlockT> *operator[](const BlockT *BB) const { 572 return getLoopFor(BB); 573 } 574 575 /// getLoopDepth - Return the loop nesting level of the specified block... 576 /// 577 unsigned getLoopDepth(const BlockT *BB) const { 578 const Loop *L = getLoopFor(BB); 579 return L ? L->getLoopDepth() : 0; 580 } 581 582 // isLoopHeader - True if the block is a loop header node 583 bool isLoopHeader(BlockT *BB) const { 584 const Loop *L = getLoopFor(BB); 585 return L && L->getHeader() == BB; 586 } 587 588 /// removeLoop - This removes the specified top-level loop from this loop info 589 /// object. The loop is not deleted, as it will presumably be inserted into 590 /// another loop. 591 LoopBase<BlockT> *removeLoop(iterator I) { 592 assert(I != end() && "Cannot remove end iterator!"); 593 LoopBase<BlockT> *L = *I; 594 assert(L->getParentLoop() == 0 && "Not a top-level loop!"); 595 TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin())); 596 return L; 597 } 598 599 /// changeLoopFor - Change the top-level loop that contains BB to the 600 /// specified loop. This should be used by transformations that restructure 601 /// the loop hierarchy tree. 602 void changeLoopFor(BlockT *BB, LoopBase<BlockT> *L) { 603 LoopBase<BlockT> *&OldLoop = BBMap[BB]; 604 assert(OldLoop && "Block not in a loop yet!"); 605 OldLoop = L; 606 } 607 608 /// changeTopLevelLoop - Replace the specified loop in the top-level loops 609 /// list with the indicated loop. 610 void changeTopLevelLoop(LoopBase<BlockT> *OldLoop, 611 LoopBase<BlockT> *NewLoop) { 612 typename std::vector<LoopBase<BlockT>*>::iterator I = 613 std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop); 614 assert(I != TopLevelLoops.end() && "Old loop not at top level!"); 615 *I = NewLoop; 616 assert(NewLoop->ParentLoop == 0 && OldLoop->ParentLoop == 0 && 617 "Loops already embedded into a subloop!"); 618 } 619 620 /// addTopLevelLoop - This adds the specified loop to the collection of 621 /// top-level loops. 622 void addTopLevelLoop(LoopBase<BlockT> *New) { 623 assert(New->getParentLoop() == 0 && "Loop already in subloop!"); 624 TopLevelLoops.push_back(New); 625 } 626 627 /// removeBlock - This method completely removes BB from all data structures, 628 /// including all of the Loop objects it is nested in and our mapping from 629 /// BasicBlocks to loops. 630 void removeBlock(BlockT *BB) { 631 typename std::map<BlockT *, LoopBase<BlockT>*>::iterator I = BBMap.find(BB); 632 if (I != BBMap.end()) { 633 for (Loop *L = I->second; L; L = L->getParentLoop()) 634 L->removeBlockFromLoop(BB); 635 636 BBMap.erase(I); 637 } 638 } 639 640 // Internals 641 642 static bool isNotAlreadyContainedIn(Loop *SubLoop, Loop *ParentLoop) { 643 if (SubLoop == 0) return true; 644 if (SubLoop == ParentLoop) return false; 645 return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop); 646 } 647 648 void Calculate(DominatorTree &DT) { 649 BlockT *RootNode = DT.getRootNode()->getBlock(); 650 651 for (df_iterator<BlockT*> NI = df_begin(RootNode), 652 NE = df_end(RootNode); NI != NE; ++NI) 653 if (LoopBase<BlockT> *L = ConsiderForLoop(*NI, DT)) 654 TopLevelLoops.push_back(L); 655 } 656 657 LoopBase<BlockT> *ConsiderForLoop(BlockT *BB, DominatorTree &DT) { 658 if (BBMap.find(BB) != BBMap.end()) return 0;// Haven't processed this node? 659 660 std::vector<BlockT *> TodoStack; 661 662 // Scan the predecessors of BB, checking to see if BB dominates any of 663 // them. This identifies backedges which target this node... 664 for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) 665 if (DT.dominates(BB, *I)) // If BB dominates it's predecessor... 666 TodoStack.push_back(*I); 667 668 if (TodoStack.empty()) return 0; // No backedges to this block... 669 670 // Create a new loop to represent this basic block... 671 LoopBase<BlockT> *L = new LoopBase<BlockT>(BB); 672 BBMap[BB] = L; 673 674 BlockT *EntryBlock = &BB->getParent()->getEntryBlock(); 675 676 while (!TodoStack.empty()) { // Process all the nodes in the loop 677 BlockT *X = TodoStack.back(); 678 TodoStack.pop_back(); 679 680 if (!L->contains(X) && // As of yet unprocessed?? 681 DT.dominates(EntryBlock, X)) { // X is reachable from entry block? 682 // Check to see if this block already belongs to a loop. If this occurs 683 // then we have a case where a loop that is supposed to be a child of the 684 // current loop was processed before the current loop. When this occurs, 685 // this child loop gets added to a part of the current loop, making it a 686 // sibling to the current loop. We have to reparent this loop. 687 if (LoopBase<BlockT> *SubLoop = 688 const_cast<LoopBase<BlockT>*>(getLoopFor(X))) 689 if (SubLoop->getHeader() == X && isNotAlreadyContainedIn(SubLoop, L)) { 690 // Remove the subloop from it's current parent... 691 assert(SubLoop->ParentLoop && SubLoop->ParentLoop != L); 692 LoopBase<BlockT> *SLP = SubLoop->ParentLoop; // SubLoopParent 693 typename std::vector<LoopBase<BlockT>*>::iterator I = 694 std::find(SLP->SubLoops.begin(), SLP->SubLoops.end(), SubLoop); 695 assert(I != SLP->SubLoops.end() && "SubLoop not a child of parent?"); 696 SLP->SubLoops.erase(I); // Remove from parent... 697 698 // Add the subloop to THIS loop... 699 SubLoop->ParentLoop = L; 700 L->SubLoops.push_back(SubLoop); 701 } 702 703 // Normal case, add the block to our loop... 704 L->Blocks.push_back(X); 705 706 // Add all of the predecessors of X to the end of the work stack... 707 TodoStack.insert(TodoStack.end(), pred_begin(X), pred_end(X)); 708 } 709 } 710 711 // If there are any loops nested within this loop, create them now! 712 for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(), 713 E = L->Blocks.end(); I != E; ++I) 714 if (LoopBase<BlockT> *NewLoop = ConsiderForLoop(*I, DT)) { 715 L->SubLoops.push_back(NewLoop); 716 NewLoop->ParentLoop = L; 717 } 718 719 // Add the basic blocks that comprise this loop to the BBMap so that this 720 // loop can be found for them. 721 // 722 for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(), 723 E = L->Blocks.end(); I != E; ++I) { 724 typename std::map<BlockT*, LoopBase<BlockT>*>::iterator BBMI = 725 BBMap.lower_bound(*I); 726 if (BBMI == BBMap.end() || BBMI->first != *I) // Not in map yet... 727 BBMap.insert(BBMI, std::make_pair(*I, L)); // Must be at this level 728 } 729 730 // Now that we have a list of all of the child loops of this loop, check to 731 // see if any of them should actually be nested inside of each other. We can 732 // accidentally pull loops our of their parents, so we must make sure to 733 // organize the loop nests correctly now. 734 { 735 std::map<BlockT*, LoopBase<BlockT>*> ContainingLoops; 736 for (unsigned i = 0; i != L->SubLoops.size(); ++i) { 737 LoopBase<BlockT> *Child = L->SubLoops[i]; 738 assert(Child->getParentLoop() == L && "Not proper child loop?"); 739 740 if (LoopBase<BlockT> *ContainingLoop = 741 ContainingLoops[Child->getHeader()]) { 742 // If there is already a loop which contains this loop, move this loop 743 // into the containing loop. 744 MoveSiblingLoopInto(Child, ContainingLoop); 745 --i; // The loop got removed from the SubLoops list. 746 } else { 747 // This is currently considered to be a top-level loop. Check to see if 748 // any of the contained blocks are loop headers for subloops we have 749 // already processed. 750 for (unsigned b = 0, e = Child->Blocks.size(); b != e; ++b) { 751 LoopBase<BlockT> *&BlockLoop = ContainingLoops[Child->Blocks[b]]; 752 if (BlockLoop == 0) { // Child block not processed yet... 753 BlockLoop = Child; 754 } else if (BlockLoop != Child) { 755 LoopBase<BlockT> *SubLoop = BlockLoop; 756 // Reparent all of the blocks which used to belong to BlockLoops 757 for (unsigned j = 0, e = SubLoop->Blocks.size(); j != e; ++j) 758 ContainingLoops[SubLoop->Blocks[j]] = Child; 759 760 // There is already a loop which contains this block, that means 761 // that we should reparent the loop which the block is currently 762 // considered to belong to to be a child of this loop. 763 MoveSiblingLoopInto(SubLoop, Child); 764 --i; // We just shrunk the SubLoops list. 765 } 766 } 767 } 768 } 769 } 770 771 return L; 772 } 773 774 /// MoveSiblingLoopInto - This method moves the NewChild loop to live inside of 775 /// the NewParent Loop, instead of being a sibling of it. 776 void MoveSiblingLoopInto(LoopBase<BlockT> *NewChild, 777 LoopBase<BlockT> *NewParent) { 778 LoopBase<BlockT> *OldParent = NewChild->getParentLoop(); 779 assert(OldParent && OldParent == NewParent->getParentLoop() && 780 NewChild != NewParent && "Not sibling loops!"); 781 782 // Remove NewChild from being a child of OldParent 783 typename std::vector<LoopBase<BlockT>*>::iterator I = 784 std::find(OldParent->SubLoops.begin(), OldParent->SubLoops.end(), NewChild); 785 assert(I != OldParent->SubLoops.end() && "Parent fields incorrect??"); 786 OldParent->SubLoops.erase(I); // Remove from parent's subloops list 787 NewChild->ParentLoop = 0; 788 789 InsertLoopInto(NewChild, NewParent); 790 } 791 792 /// InsertLoopInto - This inserts loop L into the specified parent loop. If the 793 /// parent loop contains a loop which should contain L, the loop gets inserted 794 /// into L instead. 795 void InsertLoopInto(LoopBase<BlockT> *L, LoopBase<BlockT> *Parent) { 796 BlockT *LHeader = L->getHeader(); 797 assert(Parent->contains(LHeader) && "This loop should not be inserted here!"); 798 799 // Check to see if it belongs in a child loop... 800 for (unsigned i = 0, e = Parent->SubLoops.size(); i != e; ++i) 801 if (Parent->SubLoops[i]->contains(LHeader)) { 802 InsertLoopInto(L, Parent->SubLoops[i]); 803 return; 804 } 805 806 // If not, insert it here! 807 Parent->SubLoops.push_back(L); 808 L->ParentLoop = Parent; 809 } 810 811 // Debugging 812 813 void print(std::ostream &OS, const Module* ) const { 814 for (unsigned i = 0; i < TopLevelLoops.size(); ++i) 815 TopLevelLoops[i]->print(OS); 816 #if 0 817 for (std::map<BasicBlock*, Loop*>::const_iterator I = BBMap.begin(), 818 E = BBMap.end(); I != E; ++I) 819 OS << "BB '" << I->first->getName() << "' level = " 820 << I->second->getLoopDepth() << "\n"; 821 #endif 822 } 823}; 824 825class LoopInfo : public FunctionPass { 826 LoopInfoBase<BasicBlock>* LI; 827 friend class LoopBase<BasicBlock>; 828 829 LoopInfoBase<BasicBlock>& getBase() { return *LI; } 830public: 831 static char ID; // Pass identification, replacement for typeid 832 833 LoopInfo() : FunctionPass(intptr_t(&ID)) { 834 LI = new LoopInfoBase<BasicBlock>(); 835 } 836 837 ~LoopInfo() { delete LI; } 838 839 /// iterator/begin/end - The interface to the top-level loops in the current 840 /// function. 841 /// 842 typedef std::vector<Loop*>::const_iterator iterator; 843 inline iterator begin() const { return LI->begin(); } 844 inline iterator end() const { return LI->end(); } 845 846 /// getLoopFor - Return the inner most loop that BB lives in. If a basic 847 /// block is in no loop (for example the entry node), null is returned. 848 /// 849 inline Loop *getLoopFor(const BasicBlock *BB) const { 850 return LI->getLoopFor(BB); 851 } 852 853 /// operator[] - same as getLoopFor... 854 /// 855 inline const Loop *operator[](const BasicBlock *BB) const { 856 return LI->getLoopFor(BB); 857 } 858 859 /// getLoopDepth - Return the loop nesting level of the specified block... 860 /// 861 inline unsigned getLoopDepth(const BasicBlock *BB) const { 862 return LI->getLoopDepth(BB); 863 } 864 865 // isLoopHeader - True if the block is a loop header node 866 inline bool isLoopHeader(BasicBlock *BB) const { 867 return LI->isLoopHeader(BB); 868 } 869 870 /// runOnFunction - Calculate the natural loop information. 871 /// 872 virtual bool runOnFunction(Function &F); 873 874 virtual void releaseMemory() { LI->releaseMemory(); } 875 876 virtual void print(std::ostream &O, const Module* M = 0) const { 877 if (O) LI->print(O, M); 878 } 879 880 virtual void getAnalysisUsage(AnalysisUsage &AU) const; 881 882 /// removeLoop - This removes the specified top-level loop from this loop info 883 /// object. The loop is not deleted, as it will presumably be inserted into 884 /// another loop. 885 inline Loop *removeLoop(iterator I) { return LI->removeLoop(I); } 886 887 /// changeLoopFor - Change the top-level loop that contains BB to the 888 /// specified loop. This should be used by transformations that restructure 889 /// the loop hierarchy tree. 890 inline void changeLoopFor(BasicBlock *BB, Loop *L) { 891 LI->changeLoopFor(BB, L); 892 } 893 894 /// changeTopLevelLoop - Replace the specified loop in the top-level loops 895 /// list with the indicated loop. 896 inline void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop) { 897 LI->changeTopLevelLoop(OldLoop, NewLoop); 898 } 899 900 /// addTopLevelLoop - This adds the specified loop to the collection of 901 /// top-level loops. 902 inline void addTopLevelLoop(Loop *New) { 903 LI->addTopLevelLoop(New); 904 } 905 906 /// removeBlock - This method completely removes BB from all data structures, 907 /// including all of the Loop objects it is nested in and our mapping from 908 /// BasicBlocks to loops. 909 void removeBlock(BasicBlock *BB) { 910 LI->removeBlock(BB); 911 } 912}; 913 914 915// Allow clients to walk the list of nested loops... 916template <> struct GraphTraits<const Loop*> { 917 typedef const Loop NodeType; 918 typedef std::vector<Loop*>::const_iterator ChildIteratorType; 919 920 static NodeType *getEntryNode(const Loop *L) { return L; } 921 static inline ChildIteratorType child_begin(NodeType *N) { 922 return N->begin(); 923 } 924 static inline ChildIteratorType child_end(NodeType *N) { 925 return N->end(); 926 } 927}; 928 929template <> struct GraphTraits<Loop*> { 930 typedef Loop NodeType; 931 typedef std::vector<Loop*>::const_iterator ChildIteratorType; 932 933 static NodeType *getEntryNode(Loop *L) { return L; } 934 static inline ChildIteratorType child_begin(NodeType *N) { 935 return N->begin(); 936 } 937 static inline ChildIteratorType child_end(NodeType *N) { 938 return N->end(); 939 } 940}; 941 942template<class BlockT> 943void LoopBase<BlockT>::addBasicBlockToLoop(BlockT *NewBB, 944 LoopInfo &LI) { 945 assert((Blocks.empty() || LI[getHeader()] == this) && 946 "Incorrect LI specified for this loop!"); 947 assert(NewBB && "Cannot add a null basic block to the loop!"); 948 assert(LI[NewBB] == 0 && "BasicBlock already in the loop!"); 949 950 LoopInfoBase<BasicBlock>& LIB = LI.getBase(); 951 952 // Add the loop mapping to the LoopInfo object... 953 LIB.BBMap[NewBB] = this; 954 955 // Add the basic block to this loop and all parent loops... 956 LoopBase<BlockT> *L = this; 957 while (L) { 958 L->Blocks.push_back(NewBB); 959 L = L->getParentLoop(); 960 } 961} 962 963} // End llvm namespace 964 965// Make sure that any clients of this file link in LoopInfo.cpp 966FORCE_DEFINING_FILE_TO_BE_LINKED(LoopInfo) 967 968#endif 969