1 //===- LoopInfo.cpp - Natural Loop Calculator -----------------------------===// 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 the 12 // loops identified may actually be several natural loops that share the same 13 // header node... not just a single natural loop. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "llvm/Analysis/LoopInfo.h" 18 #include "llvm/Constants.h" 19 #include "llvm/Instructions.h" 20 #include "llvm/Analysis/Dominators.h" 21 #include "llvm/Analysis/LoopIterator.h" 22 #include "llvm/Assembly/Writer.h" 23 #include "llvm/Support/CFG.h" 24 #include "llvm/Support/CommandLine.h" 25 #include "llvm/Support/Debug.h" 26 #include "llvm/ADT/DepthFirstIterator.h" 27 #include "llvm/ADT/SmallPtrSet.h" 28 #include <algorithm> 29 using namespace llvm; 30 31 // Always verify loopinfo if expensive checking is enabled. 32 #ifdef XDEBUG 33 static bool VerifyLoopInfo = true; 34 #else 35 static bool VerifyLoopInfo = false; 36 #endif 37 static cl::opt<bool,true> 38 VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo), 39 cl::desc("Verify loop info (time consuming)")); 40 41 char LoopInfo::ID = 0; 42 INITIALIZE_PASS_BEGIN(LoopInfo, "loops", "Natural Loop Information", true, true) 43 INITIALIZE_PASS_DEPENDENCY(DominatorTree) 44 INITIALIZE_PASS_END(LoopInfo, "loops", "Natural Loop Information", true, true) 45 46 //===----------------------------------------------------------------------===// 47 // Loop implementation 48 // 49 50 /// isLoopInvariant - Return true if the specified value is loop invariant 51 /// 52 bool Loop::isLoopInvariant(Value *V) const { 53 if (Instruction *I = dyn_cast<Instruction>(V)) 54 return !contains(I); 55 return true; // All non-instructions are loop invariant 56 } 57 58 /// hasLoopInvariantOperands - Return true if all the operands of the 59 /// specified instruction are loop invariant. 60 bool Loop::hasLoopInvariantOperands(Instruction *I) const { 61 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 62 if (!isLoopInvariant(I->getOperand(i))) 63 return false; 64 65 return true; 66 } 67 68 /// makeLoopInvariant - If the given value is an instruciton inside of the 69 /// loop and it can be hoisted, do so to make it trivially loop-invariant. 70 /// Return true if the value after any hoisting is loop invariant. This 71 /// function can be used as a slightly more aggressive replacement for 72 /// isLoopInvariant. 73 /// 74 /// If InsertPt is specified, it is the point to hoist instructions to. 75 /// If null, the terminator of the loop preheader is used. 76 /// 77 bool Loop::makeLoopInvariant(Value *V, bool &Changed, 78 Instruction *InsertPt) const { 79 if (Instruction *I = dyn_cast<Instruction>(V)) 80 return makeLoopInvariant(I, Changed, InsertPt); 81 return true; // All non-instructions are loop-invariant. 82 } 83 84 /// makeLoopInvariant - If the given instruction is inside of the 85 /// loop and it can be hoisted, do so to make it trivially loop-invariant. 86 /// Return true if the instruction after any hoisting is loop invariant. This 87 /// function can be used as a slightly more aggressive replacement for 88 /// isLoopInvariant. 89 /// 90 /// If InsertPt is specified, it is the point to hoist instructions to. 91 /// If null, the terminator of the loop preheader is used. 92 /// 93 bool Loop::makeLoopInvariant(Instruction *I, bool &Changed, 94 Instruction *InsertPt) const { 95 // Test if the value is already loop-invariant. 96 if (isLoopInvariant(I)) 97 return true; 98 if (!I->isSafeToSpeculativelyExecute()) 99 return false; 100 if (I->mayReadFromMemory()) 101 return false; 102 // The landingpad instruction is immobile. 103 if (isa<LandingPadInst>(I)) 104 return false; 105 // Determine the insertion point, unless one was given. 106 if (!InsertPt) { 107 BasicBlock *Preheader = getLoopPreheader(); 108 // Without a preheader, hoisting is not feasible. 109 if (!Preheader) 110 return false; 111 InsertPt = Preheader->getTerminator(); 112 } 113 // Don't hoist instructions with loop-variant operands. 114 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 115 if (!makeLoopInvariant(I->getOperand(i), Changed, InsertPt)) 116 return false; 117 118 // Hoist. 119 I->moveBefore(InsertPt); 120 Changed = true; 121 return true; 122 } 123 124 /// getCanonicalInductionVariable - Check to see if the loop has a canonical 125 /// induction variable: an integer recurrence that starts at 0 and increments 126 /// by one each time through the loop. If so, return the phi node that 127 /// corresponds to it. 128 /// 129 /// The IndVarSimplify pass transforms loops to have a canonical induction 130 /// variable. 131 /// 132 PHINode *Loop::getCanonicalInductionVariable() const { 133 BasicBlock *H = getHeader(); 134 135 BasicBlock *Incoming = 0, *Backedge = 0; 136 pred_iterator PI = pred_begin(H); 137 assert(PI != pred_end(H) && 138 "Loop must have at least one backedge!"); 139 Backedge = *PI++; 140 if (PI == pred_end(H)) return 0; // dead loop 141 Incoming = *PI++; 142 if (PI != pred_end(H)) return 0; // multiple backedges? 143 144 if (contains(Incoming)) { 145 if (contains(Backedge)) 146 return 0; 147 std::swap(Incoming, Backedge); 148 } else if (!contains(Backedge)) 149 return 0; 150 151 // Loop over all of the PHI nodes, looking for a canonical indvar. 152 for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) { 153 PHINode *PN = cast<PHINode>(I); 154 if (ConstantInt *CI = 155 dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming))) 156 if (CI->isNullValue()) 157 if (Instruction *Inc = 158 dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge))) 159 if (Inc->getOpcode() == Instruction::Add && 160 Inc->getOperand(0) == PN) 161 if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1))) 162 if (CI->equalsInt(1)) 163 return PN; 164 } 165 return 0; 166 } 167 168 /// isLCSSAForm - Return true if the Loop is in LCSSA form 169 bool Loop::isLCSSAForm(DominatorTree &DT) const { 170 // Sort the blocks vector so that we can use binary search to do quick 171 // lookups. 172 SmallPtrSet<BasicBlock*, 16> LoopBBs(block_begin(), block_end()); 173 174 for (block_iterator BI = block_begin(), E = block_end(); BI != E; ++BI) { 175 BasicBlock *BB = *BI; 176 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;++I) 177 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; 178 ++UI) { 179 User *U = *UI; 180 BasicBlock *UserBB = cast<Instruction>(U)->getParent(); 181 if (PHINode *P = dyn_cast<PHINode>(U)) 182 UserBB = P->getIncomingBlock(UI); 183 184 // Check the current block, as a fast-path, before checking whether 185 // the use is anywhere in the loop. Most values are used in the same 186 // block they are defined in. Also, blocks not reachable from the 187 // entry are special; uses in them don't need to go through PHIs. 188 if (UserBB != BB && 189 !LoopBBs.count(UserBB) && 190 DT.isReachableFromEntry(UserBB)) 191 return false; 192 } 193 } 194 195 return true; 196 } 197 198 /// isLoopSimplifyForm - Return true if the Loop is in the form that 199 /// the LoopSimplify form transforms loops to, which is sometimes called 200 /// normal form. 201 bool Loop::isLoopSimplifyForm() const { 202 // Normal-form loops have a preheader, a single backedge, and all of their 203 // exits have all their predecessors inside the loop. 204 return getLoopPreheader() && getLoopLatch() && hasDedicatedExits(); 205 } 206 207 /// hasDedicatedExits - Return true if no exit block for the loop 208 /// has a predecessor that is outside the loop. 209 bool Loop::hasDedicatedExits() const { 210 // Sort the blocks vector so that we can use binary search to do quick 211 // lookups. 212 SmallPtrSet<BasicBlock *, 16> LoopBBs(block_begin(), block_end()); 213 // Each predecessor of each exit block of a normal loop is contained 214 // within the loop. 215 SmallVector<BasicBlock *, 4> ExitBlocks; 216 getExitBlocks(ExitBlocks); 217 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) 218 for (pred_iterator PI = pred_begin(ExitBlocks[i]), 219 PE = pred_end(ExitBlocks[i]); PI != PE; ++PI) 220 if (!LoopBBs.count(*PI)) 221 return false; 222 // All the requirements are met. 223 return true; 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 exits are in canonical form. 229 /// 230 void 231 Loop::getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const { 232 assert(hasDedicatedExits() && 233 "getUniqueExitBlocks assumes the loop has canonical form exits!"); 234 235 // Sort the blocks vector so that we can use binary search to do quick 236 // lookups. 237 SmallVector<BasicBlock *, 128> LoopBBs(block_begin(), block_end()); 238 std::sort(LoopBBs.begin(), LoopBBs.end()); 239 240 SmallVector<BasicBlock *, 32> switchExitBlocks; 241 242 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) { 243 244 BasicBlock *current = *BI; 245 switchExitBlocks.clear(); 246 247 for (succ_iterator I = succ_begin(*BI), E = succ_end(*BI); I != E; ++I) { 248 // If block is inside the loop then it is not a exit block. 249 if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) 250 continue; 251 252 pred_iterator PI = pred_begin(*I); 253 BasicBlock *firstPred = *PI; 254 255 // If current basic block is this exit block's first predecessor 256 // then only insert exit block in to the output ExitBlocks vector. 257 // This ensures that same exit block is not inserted twice into 258 // ExitBlocks vector. 259 if (current != firstPred) 260 continue; 261 262 // If a terminator has more then two successors, for example SwitchInst, 263 // then it is possible that there are multiple edges from current block 264 // to one exit block. 265 if (std::distance(succ_begin(current), succ_end(current)) <= 2) { 266 ExitBlocks.push_back(*I); 267 continue; 268 } 269 270 // In case of multiple edges from current block to exit block, collect 271 // only one edge in ExitBlocks. Use switchExitBlocks to keep track of 272 // duplicate edges. 273 if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I) 274 == switchExitBlocks.end()) { 275 switchExitBlocks.push_back(*I); 276 ExitBlocks.push_back(*I); 277 } 278 } 279 } 280 } 281 282 /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one 283 /// block, return that block. Otherwise return null. 284 BasicBlock *Loop::getUniqueExitBlock() const { 285 SmallVector<BasicBlock *, 8> UniqueExitBlocks; 286 getUniqueExitBlocks(UniqueExitBlocks); 287 if (UniqueExitBlocks.size() == 1) 288 return UniqueExitBlocks[0]; 289 return 0; 290 } 291 292 void Loop::dump() const { 293 print(dbgs()); 294 } 295 296 //===----------------------------------------------------------------------===// 297 // UnloopUpdater implementation 298 // 299 300 namespace { 301 /// Find the new parent loop for all blocks within the "unloop" whose last 302 /// backedges has just been removed. 303 class UnloopUpdater { 304 Loop *Unloop; 305 LoopInfo *LI; 306 307 LoopBlocksDFS DFS; 308 309 // Map unloop's immediate subloops to their nearest reachable parents. Nested 310 // loops within these subloops will not change parents. However, an immediate 311 // subloop's new parent will be the nearest loop reachable from either its own 312 // exits *or* any of its nested loop's exits. 313 DenseMap<Loop*, Loop*> SubloopParents; 314 315 // Flag the presence of an irreducible backedge whose destination is a block 316 // directly contained by the original unloop. 317 bool FoundIB; 318 319 public: 320 UnloopUpdater(Loop *UL, LoopInfo *LInfo) : 321 Unloop(UL), LI(LInfo), DFS(UL), FoundIB(false) {} 322 323 void updateBlockParents(); 324 325 void removeBlocksFromAncestors(); 326 327 void updateSubloopParents(); 328 329 protected: 330 Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop); 331 }; 332 } // end anonymous namespace 333 334 /// updateBlockParents - Update the parent loop for all blocks that are directly 335 /// contained within the original "unloop". 336 void UnloopUpdater::updateBlockParents() { 337 if (Unloop->getNumBlocks()) { 338 // Perform a post order CFG traversal of all blocks within this loop, 339 // propagating the nearest loop from sucessors to predecessors. 340 LoopBlocksTraversal Traversal(DFS, LI); 341 for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(), 342 POE = Traversal.end(); POI != POE; ++POI) { 343 344 Loop *L = LI->getLoopFor(*POI); 345 Loop *NL = getNearestLoop(*POI, L); 346 347 if (NL != L) { 348 // For reducible loops, NL is now an ancestor of Unloop. 349 assert((NL != Unloop && (!NL || NL->contains(Unloop))) && 350 "uninitialized successor"); 351 LI->changeLoopFor(*POI, NL); 352 } 353 else { 354 // Or the current block is part of a subloop, in which case its parent 355 // is unchanged. 356 assert((FoundIB || Unloop->contains(L)) && "uninitialized successor"); 357 } 358 } 359 } 360 // Each irreducible loop within the unloop induces a round of iteration using 361 // the DFS result cached by Traversal. 362 bool Changed = FoundIB; 363 for (unsigned NIters = 0; Changed; ++NIters) { 364 assert(NIters < Unloop->getNumBlocks() && "runaway iterative algorithm"); 365 366 // Iterate over the postorder list of blocks, propagating the nearest loop 367 // from successors to predecessors as before. 368 Changed = false; 369 for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(), 370 POE = DFS.endPostorder(); POI != POE; ++POI) { 371 372 Loop *L = LI->getLoopFor(*POI); 373 Loop *NL = getNearestLoop(*POI, L); 374 if (NL != L) { 375 assert(NL != Unloop && (!NL || NL->contains(Unloop)) && 376 "uninitialized successor"); 377 LI->changeLoopFor(*POI, NL); 378 Changed = true; 379 } 380 } 381 } 382 } 383 384 /// removeBlocksFromAncestors - Remove unloop's blocks from all ancestors below 385 /// their new parents. 386 void UnloopUpdater::removeBlocksFromAncestors() { 387 // Remove all unloop's blocks (including those in nested subloops) from 388 // ancestors below the new parent loop. 389 for (Loop::block_iterator BI = Unloop->block_begin(), 390 BE = Unloop->block_end(); BI != BE; ++BI) { 391 Loop *OuterParent = LI->getLoopFor(*BI); 392 if (Unloop->contains(OuterParent)) { 393 while (OuterParent->getParentLoop() != Unloop) 394 OuterParent = OuterParent->getParentLoop(); 395 OuterParent = SubloopParents[OuterParent]; 396 } 397 // Remove blocks from former Ancestors except Unloop itself which will be 398 // deleted. 399 for (Loop *OldParent = Unloop->getParentLoop(); OldParent != OuterParent; 400 OldParent = OldParent->getParentLoop()) { 401 assert(OldParent && "new loop is not an ancestor of the original"); 402 OldParent->removeBlockFromLoop(*BI); 403 } 404 } 405 } 406 407 /// updateSubloopParents - Update the parent loop for all subloops directly 408 /// nested within unloop. 409 void UnloopUpdater::updateSubloopParents() { 410 while (!Unloop->empty()) { 411 Loop *Subloop = *llvm::prior(Unloop->end()); 412 Unloop->removeChildLoop(llvm::prior(Unloop->end())); 413 414 assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop"); 415 if (SubloopParents[Subloop]) 416 SubloopParents[Subloop]->addChildLoop(Subloop); 417 else 418 LI->addTopLevelLoop(Subloop); 419 } 420 } 421 422 /// getNearestLoop - Return the nearest parent loop among this block's 423 /// successors. If a successor is a subloop header, consider its parent to be 424 /// the nearest parent of the subloop's exits. 425 /// 426 /// For subloop blocks, simply update SubloopParents and return NULL. 427 Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) { 428 429 // Initially for blocks directly contained by Unloop, NearLoop == Unloop and 430 // is considered uninitialized. 431 Loop *NearLoop = BBLoop; 432 433 Loop *Subloop = 0; 434 if (NearLoop != Unloop && Unloop->contains(NearLoop)) { 435 Subloop = NearLoop; 436 // Find the subloop ancestor that is directly contained within Unloop. 437 while (Subloop->getParentLoop() != Unloop) { 438 Subloop = Subloop->getParentLoop(); 439 assert(Subloop && "subloop is not an ancestor of the original loop"); 440 } 441 // Get the current nearest parent of the Subloop exits, initially Unloop. 442 if (!SubloopParents.count(Subloop)) 443 SubloopParents[Subloop] = Unloop; 444 NearLoop = SubloopParents[Subloop]; 445 } 446 447 succ_iterator I = succ_begin(BB), E = succ_end(BB); 448 if (I == E) { 449 assert(!Subloop && "subloop blocks must have a successor"); 450 NearLoop = 0; // unloop blocks may now exit the function. 451 } 452 for (; I != E; ++I) { 453 if (*I == BB) 454 continue; // self loops are uninteresting 455 456 Loop *L = LI->getLoopFor(*I); 457 if (L == Unloop) { 458 // This successor has not been processed. This path must lead to an 459 // irreducible backedge. 460 assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB"); 461 FoundIB = true; 462 } 463 if (L != Unloop && Unloop->contains(L)) { 464 // Successor is in a subloop. 465 if (Subloop) 466 continue; // Branching within subloops. Ignore it. 467 468 // BB branches from the original into a subloop header. 469 assert(L->getParentLoop() == Unloop && "cannot skip into nested loops"); 470 471 // Get the current nearest parent of the Subloop's exits. 472 L = SubloopParents[L]; 473 // L could be Unloop if the only exit was an irreducible backedge. 474 } 475 if (L == Unloop) { 476 continue; 477 } 478 // Handle critical edges from Unloop into a sibling loop. 479 if (L && !L->contains(Unloop)) { 480 L = L->getParentLoop(); 481 } 482 // Remember the nearest parent loop among successors or subloop exits. 483 if (NearLoop == Unloop || !NearLoop || NearLoop->contains(L)) 484 NearLoop = L; 485 } 486 if (Subloop) { 487 SubloopParents[Subloop] = NearLoop; 488 return BBLoop; 489 } 490 return NearLoop; 491 } 492 493 //===----------------------------------------------------------------------===// 494 // LoopInfo implementation 495 // 496 bool LoopInfo::runOnFunction(Function &) { 497 releaseMemory(); 498 LI.Calculate(getAnalysis<DominatorTree>().getBase()); // Update 499 return false; 500 } 501 502 /// updateUnloop - The last backedge has been removed from a loop--now the 503 /// "unloop". Find a new parent for the blocks contained within unloop and 504 /// update the loop tree. We don't necessarily have valid dominators at this 505 /// point, but LoopInfo is still valid except for the removal of this loop. 506 /// 507 /// Note that Unloop may now be an empty loop. Calling Loop::getHeader without 508 /// checking first is illegal. 509 void LoopInfo::updateUnloop(Loop *Unloop) { 510 511 // First handle the special case of no parent loop to simplify the algorithm. 512 if (!Unloop->getParentLoop()) { 513 // Since BBLoop had no parent, Unloop blocks are no longer in a loop. 514 for (Loop::block_iterator I = Unloop->block_begin(), 515 E = Unloop->block_end(); I != E; ++I) { 516 517 // Don't reparent blocks in subloops. 518 if (getLoopFor(*I) != Unloop) 519 continue; 520 521 // Blocks no longer have a parent but are still referenced by Unloop until 522 // the Unloop object is deleted. 523 LI.changeLoopFor(*I, 0); 524 } 525 526 // Remove the loop from the top-level LoopInfo object. 527 for (LoopInfo::iterator I = LI.begin();; ++I) { 528 assert(I != LI.end() && "Couldn't find loop"); 529 if (*I == Unloop) { 530 LI.removeLoop(I); 531 break; 532 } 533 } 534 535 // Move all of the subloops to the top-level. 536 while (!Unloop->empty()) 537 LI.addTopLevelLoop(Unloop->removeChildLoop(llvm::prior(Unloop->end()))); 538 539 return; 540 } 541 542 // Update the parent loop for all blocks within the loop. Blocks within 543 // subloops will not change parents. 544 UnloopUpdater Updater(Unloop, this); 545 Updater.updateBlockParents(); 546 547 // Remove blocks from former ancestor loops. 548 Updater.removeBlocksFromAncestors(); 549 550 // Add direct subloops as children in their new parent loop. 551 Updater.updateSubloopParents(); 552 553 // Remove unloop from its parent loop. 554 Loop *ParentLoop = Unloop->getParentLoop(); 555 for (Loop::iterator I = ParentLoop->begin();; ++I) { 556 assert(I != ParentLoop->end() && "Couldn't find loop"); 557 if (*I == Unloop) { 558 ParentLoop->removeChildLoop(I); 559 break; 560 } 561 } 562 } 563 564 void LoopInfo::verifyAnalysis() const { 565 // LoopInfo is a FunctionPass, but verifying every loop in the function 566 // each time verifyAnalysis is called is very expensive. The 567 // -verify-loop-info option can enable this. In order to perform some 568 // checking by default, LoopPass has been taught to call verifyLoop 569 // manually during loop pass sequences. 570 571 if (!VerifyLoopInfo) return; 572 573 DenseSet<const Loop*> Loops; 574 for (iterator I = begin(), E = end(); I != E; ++I) { 575 assert(!(*I)->getParentLoop() && "Top-level loop has a parent!"); 576 (*I)->verifyLoopNest(&Loops); 577 } 578 579 // Verify that blocks are mapped to valid loops. 580 // 581 // FIXME: With an up-to-date DFS (see LoopIterator.h) and DominatorTree, we 582 // could also verify that the blocks are still in the correct loops. 583 for (DenseMap<BasicBlock*, Loop*>::const_iterator I = LI.BBMap.begin(), 584 E = LI.BBMap.end(); I != E; ++I) { 585 assert(Loops.count(I->second) && "orphaned loop"); 586 assert(I->second->contains(I->first) && "orphaned block"); 587 } 588 } 589 590 void LoopInfo::getAnalysisUsage(AnalysisUsage &AU) const { 591 AU.setPreservesAll(); 592 AU.addRequired<DominatorTree>(); 593 } 594 595 void LoopInfo::print(raw_ostream &OS, const Module*) const { 596 LI.print(OS); 597 } 598 599 //===----------------------------------------------------------------------===// 600 // LoopBlocksDFS implementation 601 // 602 603 /// Traverse the loop blocks and store the DFS result. 604 /// Useful for clients that just want the final DFS result and don't need to 605 /// visit blocks during the initial traversal. 606 void LoopBlocksDFS::perform(LoopInfo *LI) { 607 LoopBlocksTraversal Traversal(*this, LI); 608 for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(), 609 POE = Traversal.end(); POI != POE; ++POI) ; 610 } 611