LoopInfoImpl.h revision 887f9c5ec15582aec34aa6c28955d01e4e9961e2
1//===- llvm/Analysis/LoopInfoImpl.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 is the generic implementation of LoopInfo used for both Loops and 11// MachineLoops. 12// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_ANALYSIS_LOOPINFOIMPL_H 16#define LLVM_ANALYSIS_LOOPINFOIMPL_H 17 18#include "llvm/ADT/PostOrderIterator.h" 19#include "llvm/ADT/STLExtras.h" 20#include "llvm/Analysis/LoopInfo.h" 21 22namespace llvm { 23 24//===----------------------------------------------------------------------===// 25// APIs for simple analysis of the loop. See header notes. 26 27/// getExitingBlocks - Return all blocks inside the loop that have successors 28/// outside of the loop. These are the blocks _inside of the current loop_ 29/// which branch out. The returned list is always unique. 30/// 31template<class BlockT, class LoopT> 32void LoopBase<BlockT, LoopT>:: 33getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const { 34 typedef GraphTraits<BlockT*> BlockTraits; 35 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) 36 for (typename BlockTraits::ChildIteratorType I = 37 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI); 38 I != E; ++I) 39 if (!contains(*I)) { 40 // Not in current loop? It must be an exit block. 41 ExitingBlocks.push_back(*BI); 42 break; 43 } 44} 45 46/// getExitingBlock - If getExitingBlocks would return exactly one block, 47/// return that block. Otherwise return null. 48template<class BlockT, class LoopT> 49BlockT *LoopBase<BlockT, LoopT>::getExitingBlock() const { 50 SmallVector<BlockT*, 8> ExitingBlocks; 51 getExitingBlocks(ExitingBlocks); 52 if (ExitingBlocks.size() == 1) 53 return ExitingBlocks[0]; 54 return 0; 55} 56 57/// getExitBlocks - Return all of the successor blocks of this loop. These 58/// are the blocks _outside of the current loop_ which are branched to. 59/// 60template<class BlockT, class LoopT> 61void LoopBase<BlockT, LoopT>:: 62getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const { 63 typedef GraphTraits<BlockT*> BlockTraits; 64 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) 65 for (typename BlockTraits::ChildIteratorType I = 66 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI); 67 I != E; ++I) 68 if (!contains(*I)) 69 // Not in current loop? It must be an exit block. 70 ExitBlocks.push_back(*I); 71} 72 73/// getExitBlock - If getExitBlocks would return exactly one block, 74/// return that block. Otherwise return null. 75template<class BlockT, class LoopT> 76BlockT *LoopBase<BlockT, LoopT>::getExitBlock() const { 77 SmallVector<BlockT*, 8> ExitBlocks; 78 getExitBlocks(ExitBlocks); 79 if (ExitBlocks.size() == 1) 80 return ExitBlocks[0]; 81 return 0; 82} 83 84/// getExitEdges - Return all pairs of (_inside_block_,_outside_block_). 85template<class BlockT, class LoopT> 86void LoopBase<BlockT, LoopT>:: 87getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const { 88 typedef GraphTraits<BlockT*> BlockTraits; 89 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) 90 for (typename BlockTraits::ChildIteratorType I = 91 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI); 92 I != E; ++I) 93 if (!contains(*I)) 94 // Not in current loop? It must be an exit block. 95 ExitEdges.push_back(Edge(*BI, *I)); 96} 97 98/// getLoopPreheader - If there is a preheader for this loop, return it. A 99/// loop has a preheader if there is only one edge to the header of the loop 100/// from outside of the loop. If this is the case, the block branching to the 101/// header of the loop is the preheader node. 102/// 103/// This method returns null if there is no preheader for the loop. 104/// 105template<class BlockT, class LoopT> 106BlockT *LoopBase<BlockT, LoopT>::getLoopPreheader() const { 107 // Keep track of nodes outside the loop branching to the header... 108 BlockT *Out = getLoopPredecessor(); 109 if (!Out) return 0; 110 111 // Make sure there is only one exit out of the preheader. 112 typedef GraphTraits<BlockT*> BlockTraits; 113 typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out); 114 ++SI; 115 if (SI != BlockTraits::child_end(Out)) 116 return 0; // Multiple exits from the block, must not be a preheader. 117 118 // The predecessor has exactly one successor, so it is a preheader. 119 return Out; 120} 121 122/// getLoopPredecessor - If the given loop's header has exactly one unique 123/// predecessor outside the loop, return it. Otherwise return null. 124/// This is less strict that the loop "preheader" concept, which requires 125/// the predecessor to have exactly one successor. 126/// 127template<class BlockT, class LoopT> 128BlockT *LoopBase<BlockT, LoopT>::getLoopPredecessor() const { 129 // Keep track of nodes outside the loop branching to the header... 130 BlockT *Out = 0; 131 132 // Loop over the predecessors of the header node... 133 BlockT *Header = getHeader(); 134 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits; 135 for (typename InvBlockTraits::ChildIteratorType PI = 136 InvBlockTraits::child_begin(Header), 137 PE = InvBlockTraits::child_end(Header); PI != PE; ++PI) { 138 typename InvBlockTraits::NodeType *N = *PI; 139 if (!contains(N)) { // If the block is not in the loop... 140 if (Out && Out != N) 141 return 0; // Multiple predecessors outside the loop 142 Out = N; 143 } 144 } 145 146 // Make sure there is only one exit out of the preheader. 147 assert(Out && "Header of loop has no predecessors from outside loop?"); 148 return Out; 149} 150 151/// getLoopLatch - If there is a single latch block for this loop, return it. 152/// A latch block is a block that contains a branch back to the header. 153template<class BlockT, class LoopT> 154BlockT *LoopBase<BlockT, LoopT>::getLoopLatch() const { 155 BlockT *Header = getHeader(); 156 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits; 157 typename InvBlockTraits::ChildIteratorType PI = 158 InvBlockTraits::child_begin(Header); 159 typename InvBlockTraits::ChildIteratorType PE = 160 InvBlockTraits::child_end(Header); 161 BlockT *Latch = 0; 162 for (; PI != PE; ++PI) { 163 typename InvBlockTraits::NodeType *N = *PI; 164 if (contains(N)) { 165 if (Latch) return 0; 166 Latch = N; 167 } 168 } 169 170 return Latch; 171} 172 173//===----------------------------------------------------------------------===// 174// APIs for updating loop information after changing the CFG 175// 176 177/// addBasicBlockToLoop - This method is used by other analyses to update loop 178/// information. NewBB is set to be a new member of the current loop. 179/// Because of this, it is added as a member of all parent loops, and is added 180/// to the specified LoopInfo object as being in the current basic block. It 181/// is not valid to replace the loop header with this method. 182/// 183template<class BlockT, class LoopT> 184void LoopBase<BlockT, LoopT>:: 185addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LIB) { 186 assert((Blocks.empty() || LIB[getHeader()] == this) && 187 "Incorrect LI specified for this loop!"); 188 assert(NewBB && "Cannot add a null basic block to the loop!"); 189 assert(LIB[NewBB] == 0 && "BasicBlock already in the loop!"); 190 191 LoopT *L = static_cast<LoopT *>(this); 192 193 // Add the loop mapping to the LoopInfo object... 194 LIB.BBMap[NewBB] = L; 195 196 // Add the basic block to this loop and all parent loops... 197 while (L) { 198 L->addBlockEntry(NewBB); 199 L = L->getParentLoop(); 200 } 201} 202 203/// replaceChildLoopWith - This is used when splitting loops up. It replaces 204/// the OldChild entry in our children list with NewChild, and updates the 205/// parent pointer of OldChild to be null and the NewChild to be this loop. 206/// This updates the loop depth of the new child. 207template<class BlockT, class LoopT> 208void LoopBase<BlockT, LoopT>:: 209replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild) { 210 assert(OldChild->ParentLoop == this && "This loop is already broken!"); 211 assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!"); 212 typename std::vector<LoopT *>::iterator I = 213 std::find(SubLoops.begin(), SubLoops.end(), OldChild); 214 assert(I != SubLoops.end() && "OldChild not in loop!"); 215 *I = NewChild; 216 OldChild->ParentLoop = 0; 217 NewChild->ParentLoop = static_cast<LoopT *>(this); 218} 219 220/// verifyLoop - Verify loop structure 221template<class BlockT, class LoopT> 222void LoopBase<BlockT, LoopT>::verifyLoop() const { 223#ifndef NDEBUG 224 assert(!Blocks.empty() && "Loop header is missing"); 225 226 // Setup for using a depth-first iterator to visit every block in the loop. 227 SmallVector<BlockT*, 8> ExitBBs; 228 getExitBlocks(ExitBBs); 229 llvm::SmallPtrSet<BlockT*, 8> VisitSet; 230 VisitSet.insert(ExitBBs.begin(), ExitBBs.end()); 231 df_ext_iterator<BlockT*, llvm::SmallPtrSet<BlockT*, 8> > 232 BI = df_ext_begin(getHeader(), VisitSet), 233 BE = df_ext_end(getHeader(), VisitSet); 234 235 // Keep track of the number of BBs visited. 236 unsigned NumVisited = 0; 237 238 // Check the individual blocks. 239 for ( ; BI != BE; ++BI) { 240 BlockT *BB = *BI; 241 bool HasInsideLoopSuccs = false; 242 bool HasInsideLoopPreds = false; 243 SmallVector<BlockT *, 2> OutsideLoopPreds; 244 245 typedef GraphTraits<BlockT*> BlockTraits; 246 for (typename BlockTraits::ChildIteratorType SI = 247 BlockTraits::child_begin(BB), SE = BlockTraits::child_end(BB); 248 SI != SE; ++SI) 249 if (contains(*SI)) { 250 HasInsideLoopSuccs = true; 251 break; 252 } 253 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits; 254 for (typename InvBlockTraits::ChildIteratorType PI = 255 InvBlockTraits::child_begin(BB), PE = InvBlockTraits::child_end(BB); 256 PI != PE; ++PI) { 257 BlockT *N = *PI; 258 if (contains(N)) 259 HasInsideLoopPreds = true; 260 else 261 OutsideLoopPreds.push_back(N); 262 } 263 264 if (BB == getHeader()) { 265 assert(!OutsideLoopPreds.empty() && "Loop is unreachable!"); 266 } else if (!OutsideLoopPreds.empty()) { 267 // A non-header loop shouldn't be reachable from outside the loop, 268 // though it is permitted if the predecessor is not itself actually 269 // reachable. 270 BlockT *EntryBB = BB->getParent()->begin(); 271 for (df_iterator<BlockT *> NI = df_begin(EntryBB), 272 NE = df_end(EntryBB); NI != NE; ++NI) 273 for (unsigned i = 0, e = OutsideLoopPreds.size(); i != e; ++i) 274 assert(*NI != OutsideLoopPreds[i] && 275 "Loop has multiple entry points!"); 276 } 277 assert(HasInsideLoopPreds && "Loop block has no in-loop predecessors!"); 278 assert(HasInsideLoopSuccs && "Loop block has no in-loop successors!"); 279 assert(BB != getHeader()->getParent()->begin() && 280 "Loop contains function entry block!"); 281 282 NumVisited++; 283 } 284 285 assert(NumVisited == getNumBlocks() && "Unreachable block in loop"); 286 287 // Check the subloops. 288 for (iterator I = begin(), E = end(); I != E; ++I) 289 // Each block in each subloop should be contained within this loop. 290 for (block_iterator BI = (*I)->block_begin(), BE = (*I)->block_end(); 291 BI != BE; ++BI) { 292 assert(contains(*BI) && 293 "Loop does not contain all the blocks of a subloop!"); 294 } 295 296 // Check the parent loop pointer. 297 if (ParentLoop) { 298 assert(std::find(ParentLoop->begin(), ParentLoop->end(), this) != 299 ParentLoop->end() && 300 "Loop is not a subloop of its parent!"); 301 } 302#endif 303} 304 305/// verifyLoop - Verify loop structure of this loop and all nested loops. 306template<class BlockT, class LoopT> 307void LoopBase<BlockT, LoopT>::verifyLoopNest( 308 DenseSet<const LoopT*> *Loops) const { 309 Loops->insert(static_cast<const LoopT *>(this)); 310 // Verify this loop. 311 verifyLoop(); 312 // Verify the subloops. 313 for (iterator I = begin(), E = end(); I != E; ++I) 314 (*I)->verifyLoopNest(Loops); 315} 316 317template<class BlockT, class LoopT> 318void LoopBase<BlockT, LoopT>::print(raw_ostream &OS, unsigned Depth) const { 319 OS.indent(Depth*2) << "Loop at depth " << getLoopDepth() 320 << " containing: "; 321 322 for (unsigned i = 0; i < getBlocks().size(); ++i) { 323 if (i) OS << ","; 324 BlockT *BB = getBlocks()[i]; 325 WriteAsOperand(OS, BB, false); 326 if (BB == getHeader()) OS << "<header>"; 327 if (BB == getLoopLatch()) OS << "<latch>"; 328 if (isLoopExiting(BB)) OS << "<exiting>"; 329 } 330 OS << "\n"; 331 332 for (iterator I = begin(), E = end(); I != E; ++I) 333 (*I)->print(OS, Depth+2); 334} 335 336//===----------------------------------------------------------------------===// 337/// Stable LoopInfo Analysis - Build a loop tree using stable iterators so the 338/// result does / not depend on use list (block predecessor) order. 339/// 340 341/// Discover a subloop with the specified backedges such that: All blocks within 342/// this loop are mapped to this loop or a subloop. And all subloops within this 343/// loop have their parent loop set to this loop or a subloop. 344template<class BlockT, class LoopT> 345static void discoverAndMapSubloop(LoopT *L, ArrayRef<BlockT*> Backedges, 346 LoopInfoBase<BlockT, LoopT> *LI, 347 DominatorTreeBase<BlockT> &DomTree) { 348 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits; 349 350 unsigned NumBlocks = 0; 351 unsigned NumSubloops = 0; 352 353 // Perform a backward CFG traversal using a worklist. 354 std::vector<BlockT *> ReverseCFGWorklist(Backedges.begin(), Backedges.end()); 355 while (!ReverseCFGWorklist.empty()) { 356 BlockT *PredBB = ReverseCFGWorklist.back(); 357 ReverseCFGWorklist.pop_back(); 358 359 LoopT *Subloop = LI->getLoopFor(PredBB); 360 if (!Subloop) { 361 if (!DomTree.isReachableFromEntry(PredBB)) 362 continue; 363 364 // This is an undiscovered block. Map it to the current loop. 365 LI->changeLoopFor(PredBB, L); 366 ++NumBlocks; 367 if (PredBB == L->getHeader()) 368 continue; 369 // Push all block predecessors on the worklist. 370 ReverseCFGWorklist.insert(ReverseCFGWorklist.end(), 371 InvBlockTraits::child_begin(PredBB), 372 InvBlockTraits::child_end(PredBB)); 373 } 374 else { 375 // This is a discovered block. Find its outermost discovered loop. 376 while (LoopT *Parent = Subloop->getParentLoop()) 377 Subloop = Parent; 378 379 // If it is already discovered to be a subloop of this loop, continue. 380 if (Subloop == L) 381 continue; 382 383 // Discover a subloop of this loop. 384 Subloop->setParentLoop(L); 385 ++NumSubloops; 386 NumBlocks += Subloop->getBlocks().capacity(); 387 PredBB = Subloop->getHeader(); 388 // Continue traversal along predecessors that are not loop-back edges from 389 // within this subloop tree itself. Note that a predecessor may directly 390 // reach another subloop that is not yet discovered to be a subloop of 391 // this loop, which we must traverse. 392 for (typename InvBlockTraits::ChildIteratorType PI = 393 InvBlockTraits::child_begin(PredBB), 394 PE = InvBlockTraits::child_end(PredBB); PI != PE; ++PI) { 395 if (LI->getLoopFor(*PI) != Subloop) 396 ReverseCFGWorklist.push_back(*PI); 397 } 398 } 399 } 400 L->getSubLoopsVector().reserve(NumSubloops); 401 L->reserveBlocks(NumBlocks); 402} 403 404namespace { 405/// Populate all loop data in a stable order during a single forward DFS. 406template<class BlockT, class LoopT> 407class PopulateLoopsDFS { 408 typedef GraphTraits<BlockT*> BlockTraits; 409 typedef typename BlockTraits::ChildIteratorType SuccIterTy; 410 411 LoopInfoBase<BlockT, LoopT> *LI; 412 DenseSet<const BlockT *> VisitedBlocks; 413 std::vector<std::pair<BlockT*, SuccIterTy> > DFSStack; 414 415public: 416 PopulateLoopsDFS(LoopInfoBase<BlockT, LoopT> *li): 417 LI(li) {} 418 419 void traverse(BlockT *EntryBlock); 420 421protected: 422 void insertIntoLoop(BlockT *Block); 423 424 BlockT *dfsSource() { return DFSStack.back().first; } 425 SuccIterTy &dfsSucc() { return DFSStack.back().second; } 426 SuccIterTy dfsSuccEnd() { return BlockTraits::child_end(dfsSource()); } 427 428 void pushBlock(BlockT *Block) { 429 DFSStack.push_back(std::make_pair(Block, BlockTraits::child_begin(Block))); 430 } 431}; 432} // anonymous 433 434/// Top-level driver for the forward DFS within the loop. 435template<class BlockT, class LoopT> 436void PopulateLoopsDFS<BlockT, LoopT>::traverse(BlockT *EntryBlock) { 437 pushBlock(EntryBlock); 438 VisitedBlocks.insert(EntryBlock); 439 while (!DFSStack.empty()) { 440 // Traverse the leftmost path as far as possible. 441 while (dfsSucc() != dfsSuccEnd()) { 442 BlockT *BB = *dfsSucc(); 443 ++dfsSucc(); 444 if (!VisitedBlocks.insert(BB).second) 445 continue; 446 447 // Push the next DFS successor onto the stack. 448 pushBlock(BB); 449 } 450 // Visit the top of the stack in postorder and backtrack. 451 insertIntoLoop(dfsSource()); 452 DFSStack.pop_back(); 453 } 454} 455 456/// Add a single Block to its ancestor loops in PostOrder. If the block is a 457/// subloop header, add the subloop to its parent in PostOrder, then reverse the 458/// Block and Subloop vectors of the now complete subloop to achieve RPO. 459template<class BlockT, class LoopT> 460void PopulateLoopsDFS<BlockT, LoopT>::insertIntoLoop(BlockT *Block) { 461 LoopT *Subloop = LI->getLoopFor(Block); 462 if (Subloop && Block == Subloop->getHeader()) { 463 // We reach this point once per subloop after processing all the blocks in 464 // the subloop. 465 if (Subloop->getParentLoop()) 466 Subloop->getParentLoop()->getSubLoopsVector().push_back(Subloop); 467 else 468 LI->addTopLevelLoop(Subloop); 469 470 // For convenience, Blocks and Subloops are inserted in postorder. Reverse 471 // the lists, except for the loop header, which is always at the beginning. 472 Subloop->reverseBlock(1); 473 std::reverse(Subloop->getSubLoopsVector().begin(), 474 Subloop->getSubLoopsVector().end()); 475 476 Subloop = Subloop->getParentLoop(); 477 } 478 for (; Subloop; Subloop = Subloop->getParentLoop()) 479 Subloop->addBlockEntry(Block); 480} 481 482/// Analyze LoopInfo discovers loops during a postorder DominatorTree traversal 483/// interleaved with backward CFG traversals within each subloop 484/// (discoverAndMapSubloop). The backward traversal skips inner subloops, so 485/// this part of the algorithm is linear in the number of CFG edges. Subloop and 486/// Block vectors are then populated during a single forward CFG traversal 487/// (PopulateLoopDFS). 488/// 489/// During the two CFG traversals each block is seen three times: 490/// 1) Discovered and mapped by a reverse CFG traversal. 491/// 2) Visited during a forward DFS CFG traversal. 492/// 3) Reverse-inserted in the loop in postorder following forward DFS. 493/// 494/// The Block vectors are inclusive, so step 3 requires loop-depth number of 495/// insertions per block. 496template<class BlockT, class LoopT> 497void LoopInfoBase<BlockT, LoopT>:: 498Analyze(DominatorTreeBase<BlockT> &DomTree) { 499 500 // Postorder traversal of the dominator tree. 501 DomTreeNodeBase<BlockT>* DomRoot = DomTree.getRootNode(); 502 for (po_iterator<DomTreeNodeBase<BlockT>*> DomIter = po_begin(DomRoot), 503 DomEnd = po_end(DomRoot); DomIter != DomEnd; ++DomIter) { 504 505 BlockT *Header = DomIter->getBlock(); 506 SmallVector<BlockT *, 4> Backedges; 507 508 // Check each predecessor of the potential loop header. 509 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits; 510 for (typename InvBlockTraits::ChildIteratorType PI = 511 InvBlockTraits::child_begin(Header), 512 PE = InvBlockTraits::child_end(Header); PI != PE; ++PI) { 513 514 BlockT *Backedge = *PI; 515 516 // If Header dominates predBB, this is a new loop. Collect the backedges. 517 if (DomTree.dominates(Header, Backedge) 518 && DomTree.isReachableFromEntry(Backedge)) { 519 Backedges.push_back(Backedge); 520 } 521 } 522 // Perform a backward CFG traversal to discover and map blocks in this loop. 523 if (!Backedges.empty()) { 524 LoopT *L = new LoopT(Header); 525 discoverAndMapSubloop(L, ArrayRef<BlockT*>(Backedges), this, DomTree); 526 } 527 } 528 // Perform a single forward CFG traversal to populate block and subloop 529 // vectors for all loops. 530 PopulateLoopsDFS<BlockT, LoopT> DFS(this); 531 DFS.traverse(DomRoot->getBlock()); 532} 533 534// Debugging 535template<class BlockT, class LoopT> 536void LoopInfoBase<BlockT, LoopT>::print(raw_ostream &OS) const { 537 for (unsigned i = 0; i < TopLevelLoops.size(); ++i) 538 TopLevelLoops[i]->print(OS); 539#if 0 540 for (DenseMap<BasicBlock*, LoopT*>::const_iterator I = BBMap.begin(), 541 E = BBMap.end(); I != E; ++I) 542 OS << "BB '" << I->first->getName() << "' level = " 543 << I->second->getLoopDepth() << "\n"; 544#endif 545} 546 547} // End llvm namespace 548 549#endif 550