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