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