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