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 // * etc... 27 // 28 //===----------------------------------------------------------------------===// 29 30 #ifndef LLVM_ANALYSIS_LOOPINFO_H 31 #define LLVM_ANALYSIS_LOOPINFO_H 32 33 #include "llvm/ADT/DenseMap.h" 34 #include "llvm/ADT/DenseSet.h" 35 #include "llvm/ADT/GraphTraits.h" 36 #include "llvm/ADT/SmallVector.h" 37 #include "llvm/Analysis/Dominators.h" 38 #include "llvm/Pass.h" 39 #include <algorithm> 40 41 namespace llvm { 42 43 template<typename T> 44 inline void RemoveFromVector(std::vector<T*> &V, T *N) { 45 typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N); 46 assert(I != V.end() && "N is not in this list!"); 47 V.erase(I); 48 } 49 50 class DominatorTree; 51 class LoopInfo; 52 class Loop; 53 class PHINode; 54 class raw_ostream; 55 template<class N, class M> class LoopInfoBase; 56 template<class N, class M> class LoopBase; 57 58 //===----------------------------------------------------------------------===// 59 /// LoopBase class - Instances of this class are used to represent loops that 60 /// are detected in the flow graph 61 /// 62 template<class BlockT, class LoopT> 63 class LoopBase { 64 LoopT *ParentLoop; 65 // SubLoops - Loops contained entirely within this one. 66 std::vector<LoopT *> SubLoops; 67 68 // Blocks - The list of blocks in this loop. First entry is the header node. 69 std::vector<BlockT*> Blocks; 70 71 LoopBase(const LoopBase<BlockT, LoopT> &) LLVM_DELETED_FUNCTION; 72 const LoopBase<BlockT, LoopT>& 73 operator=(const LoopBase<BlockT, LoopT> &) LLVM_DELETED_FUNCTION; 74 public: 75 /// Loop ctor - This creates an empty loop. 76 LoopBase() : ParentLoop(0) {} 77 ~LoopBase() { 78 for (size_t i = 0, e = SubLoops.size(); i != e; ++i) 79 delete SubLoops[i]; 80 } 81 82 /// getLoopDepth - Return the nesting level of this loop. An outer-most 83 /// loop has depth 1, for consistency with loop depth values used for basic 84 /// blocks, where depth 0 is used for blocks not inside any loops. 85 unsigned getLoopDepth() const { 86 unsigned D = 1; 87 for (const LoopT *CurLoop = ParentLoop; CurLoop; 88 CurLoop = CurLoop->ParentLoop) 89 ++D; 90 return D; 91 } 92 BlockT *getHeader() const { return Blocks.front(); } 93 LoopT *getParentLoop() const { return ParentLoop; } 94 95 /// setParentLoop is a raw interface for bypassing addChildLoop. 96 void setParentLoop(LoopT *L) { ParentLoop = L; } 97 98 /// contains - Return true if the specified loop is contained within in 99 /// this loop. 100 /// 101 bool contains(const LoopT *L) const { 102 if (L == this) return true; 103 if (L == 0) return false; 104 return contains(L->getParentLoop()); 105 } 106 107 /// contains - Return true if the specified basic block is in this loop. 108 /// 109 bool contains(const BlockT *BB) const { 110 return std::find(block_begin(), block_end(), BB) != block_end(); 111 } 112 113 /// contains - Return true if the specified instruction is in this loop. 114 /// 115 template<class InstT> 116 bool contains(const InstT *Inst) const { 117 return contains(Inst->getParent()); 118 } 119 120 /// iterator/begin/end - Return the loops contained entirely within this loop. 121 /// 122 const std::vector<LoopT *> &getSubLoops() const { return SubLoops; } 123 std::vector<LoopT *> &getSubLoopsVector() { return SubLoops; } 124 typedef typename std::vector<LoopT *>::const_iterator iterator; 125 typedef typename std::vector<LoopT *>::const_reverse_iterator 126 reverse_iterator; 127 iterator begin() const { return SubLoops.begin(); } 128 iterator end() const { return SubLoops.end(); } 129 reverse_iterator rbegin() const { return SubLoops.rbegin(); } 130 reverse_iterator rend() const { return SubLoops.rend(); } 131 bool empty() const { return SubLoops.empty(); } 132 133 /// getBlocks - Get a list of the basic blocks which make up this loop. 134 /// 135 const std::vector<BlockT*> &getBlocks() const { return Blocks; } 136 std::vector<BlockT*> &getBlocksVector() { return Blocks; } 137 typedef typename std::vector<BlockT*>::const_iterator block_iterator; 138 block_iterator block_begin() const { return Blocks.begin(); } 139 block_iterator block_end() const { return Blocks.end(); } 140 141 /// getNumBlocks - Get the number of blocks in this loop in constant time. 142 unsigned getNumBlocks() const { 143 return Blocks.size(); 144 } 145 146 /// isLoopExiting - True if terminator in the block can branch to another 147 /// block that is outside of the current loop. 148 /// 149 bool isLoopExiting(const BlockT *BB) const { 150 typedef GraphTraits<const BlockT*> BlockTraits; 151 for (typename BlockTraits::ChildIteratorType SI = 152 BlockTraits::child_begin(BB), 153 SE = BlockTraits::child_end(BB); SI != SE; ++SI) { 154 if (!contains(*SI)) 155 return true; 156 } 157 return false; 158 } 159 160 /// getNumBackEdges - Calculate the number of back edges to the loop header 161 /// 162 unsigned getNumBackEdges() const { 163 unsigned NumBackEdges = 0; 164 BlockT *H = getHeader(); 165 166 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits; 167 for (typename InvBlockTraits::ChildIteratorType I = 168 InvBlockTraits::child_begin(H), 169 E = InvBlockTraits::child_end(H); I != E; ++I) 170 if (contains(*I)) 171 ++NumBackEdges; 172 173 return NumBackEdges; 174 } 175 176 //===--------------------------------------------------------------------===// 177 // APIs for simple analysis of the loop. 178 // 179 // Note that all of these methods can fail on general loops (ie, there may not 180 // be a preheader, etc). For best success, the loop simplification and 181 // induction variable canonicalization pass should be used to normalize loops 182 // for easy analysis. These methods assume canonical loops. 183 184 /// getExitingBlocks - Return all blocks inside the loop that have successors 185 /// outside of the loop. These are the blocks _inside of the current loop_ 186 /// which branch out. The returned list is always unique. 187 /// 188 void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const; 189 190 /// getExitingBlock - If getExitingBlocks would return exactly one block, 191 /// return that block. Otherwise return null. 192 BlockT *getExitingBlock() const; 193 194 /// getExitBlocks - Return all of the successor blocks of this loop. These 195 /// are the blocks _outside of the current loop_ which are branched to. 196 /// 197 void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const; 198 199 /// getExitBlock - If getExitBlocks would return exactly one block, 200 /// return that block. Otherwise return null. 201 BlockT *getExitBlock() const; 202 203 /// Edge type. 204 typedef std::pair<const BlockT*, const BlockT*> Edge; 205 206 /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_). 207 void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const; 208 209 /// getLoopPreheader - If there is a preheader for this loop, return it. A 210 /// loop has a preheader if there is only one edge to the header of the loop 211 /// from outside of the loop. If this is the case, the block branching to the 212 /// header of the loop is the preheader node. 213 /// 214 /// This method returns null if there is no preheader for the loop. 215 /// 216 BlockT *getLoopPreheader() const; 217 218 /// getLoopPredecessor - If the given loop's header has exactly one unique 219 /// predecessor outside the loop, return it. Otherwise return null. 220 /// This is less strict that the loop "preheader" concept, which requires 221 /// the predecessor to have exactly one successor. 222 /// 223 BlockT *getLoopPredecessor() const; 224 225 /// getLoopLatch - If there is a single latch block for this loop, return it. 226 /// A latch block is a block that contains a branch back to the header. 227 BlockT *getLoopLatch() const; 228 229 //===--------------------------------------------------------------------===// 230 // APIs for updating loop information after changing the CFG 231 // 232 233 /// addBasicBlockToLoop - This method is used by other analyses to update loop 234 /// information. NewBB is set to be a new member of the current loop. 235 /// Because of this, it is added as a member of all parent loops, and is added 236 /// to the specified LoopInfo object as being in the current basic block. It 237 /// is not valid to replace the loop header with this method. 238 /// 239 void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI); 240 241 /// replaceChildLoopWith - This is used when splitting loops up. It replaces 242 /// the OldChild entry in our children list with NewChild, and updates the 243 /// parent pointer of OldChild to be null and the NewChild to be this loop. 244 /// This updates the loop depth of the new child. 245 void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild); 246 247 /// addChildLoop - Add the specified loop to be a child of this loop. This 248 /// updates the loop depth of the new child. 249 /// 250 void addChildLoop(LoopT *NewChild) { 251 assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!"); 252 NewChild->ParentLoop = static_cast<LoopT *>(this); 253 SubLoops.push_back(NewChild); 254 } 255 256 /// removeChildLoop - This removes the specified child from being a subloop of 257 /// this loop. The loop is not deleted, as it will presumably be inserted 258 /// into another loop. 259 LoopT *removeChildLoop(iterator I) { 260 assert(I != SubLoops.end() && "Cannot remove end iterator!"); 261 LoopT *Child = *I; 262 assert(Child->ParentLoop == this && "Child is not a child of this loop!"); 263 SubLoops.erase(SubLoops.begin()+(I-begin())); 264 Child->ParentLoop = 0; 265 return Child; 266 } 267 268 /// addBlockEntry - This adds a basic block directly to the basic block list. 269 /// This should only be used by transformations that create new loops. Other 270 /// transformations should use addBasicBlockToLoop. 271 void addBlockEntry(BlockT *BB) { 272 Blocks.push_back(BB); 273 } 274 275 /// moveToHeader - This method is used to move BB (which must be part of this 276 /// loop) to be the loop header of the loop (the block that dominates all 277 /// others). 278 void moveToHeader(BlockT *BB) { 279 if (Blocks[0] == BB) return; 280 for (unsigned i = 0; ; ++i) { 281 assert(i != Blocks.size() && "Loop does not contain BB!"); 282 if (Blocks[i] == BB) { 283 Blocks[i] = Blocks[0]; 284 Blocks[0] = BB; 285 return; 286 } 287 } 288 } 289 290 /// removeBlockFromLoop - This removes the specified basic block from the 291 /// current loop, updating the Blocks as appropriate. This does not update 292 /// the mapping in the LoopInfo class. 293 void removeBlockFromLoop(BlockT *BB) { 294 RemoveFromVector(Blocks, BB); 295 } 296 297 /// verifyLoop - Verify loop structure 298 void verifyLoop() const; 299 300 /// verifyLoop - Verify loop structure of this loop and all nested loops. 301 void verifyLoopNest(DenseSet<const LoopT*> *Loops) const; 302 303 void print(raw_ostream &OS, unsigned Depth = 0) const; 304 305 protected: 306 friend class LoopInfoBase<BlockT, LoopT>; 307 explicit LoopBase(BlockT *BB) : ParentLoop(0) { 308 Blocks.push_back(BB); 309 } 310 }; 311 312 template<class BlockT, class LoopT> 313 raw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) { 314 Loop.print(OS); 315 return OS; 316 } 317 318 // Implementation in LoopInfoImpl.h 319 #ifdef __GNUC__ 320 __extension__ extern template class LoopBase<BasicBlock, Loop>; 321 #endif 322 323 class Loop : public LoopBase<BasicBlock, Loop> { 324 public: 325 Loop() {} 326 327 /// isLoopInvariant - Return true if the specified value is loop invariant 328 /// 329 bool isLoopInvariant(Value *V) const; 330 331 /// hasLoopInvariantOperands - Return true if all the operands of the 332 /// specified instruction are loop invariant. 333 bool hasLoopInvariantOperands(Instruction *I) const; 334 335 /// makeLoopInvariant - If the given value is an instruction inside of the 336 /// loop and it can be hoisted, do so to make it trivially loop-invariant. 337 /// Return true if the value after any hoisting is loop invariant. This 338 /// function can be used as a slightly more aggressive replacement for 339 /// isLoopInvariant. 340 /// 341 /// If InsertPt is specified, it is the point to hoist instructions to. 342 /// If null, the terminator of the loop preheader is used. 343 /// 344 bool makeLoopInvariant(Value *V, bool &Changed, 345 Instruction *InsertPt = 0) const; 346 347 /// makeLoopInvariant - If the given instruction is inside of the 348 /// loop and it can be hoisted, do so to make it trivially loop-invariant. 349 /// Return true if the instruction after any hoisting is loop invariant. This 350 /// function can be used as a slightly more aggressive replacement for 351 /// isLoopInvariant. 352 /// 353 /// If InsertPt is specified, it is the point to hoist instructions to. 354 /// If null, the terminator of the loop preheader is used. 355 /// 356 bool makeLoopInvariant(Instruction *I, bool &Changed, 357 Instruction *InsertPt = 0) const; 358 359 /// getCanonicalInductionVariable - Check to see if the loop has a canonical 360 /// induction variable: an integer recurrence that starts at 0 and increments 361 /// by one each time through the loop. If so, return the phi node that 362 /// corresponds to it. 363 /// 364 /// The IndVarSimplify pass transforms loops to have a canonical induction 365 /// variable. 366 /// 367 PHINode *getCanonicalInductionVariable() const; 368 369 /// isLCSSAForm - Return true if the Loop is in LCSSA form 370 bool isLCSSAForm(DominatorTree &DT) const; 371 372 /// isLoopSimplifyForm - Return true if the Loop is in the form that 373 /// the LoopSimplify form transforms loops to, which is sometimes called 374 /// normal form. 375 bool isLoopSimplifyForm() const; 376 377 /// isSafeToClone - Return true if the loop body is safe to clone in practice. 378 bool isSafeToClone() const; 379 380 /// Returns true if the loop is annotated parallel. 381 /// 382 /// A parallel loop can be assumed to not contain any dependencies between 383 /// iterations by the compiler. That is, any loop-carried dependency checking 384 /// can be skipped completely when parallelizing the loop on the target 385 /// machine. Thus, if the parallel loop information originates from the 386 /// programmer, e.g. via the OpenMP parallel for pragma, it is the 387 /// programmer's responsibility to ensure there are no loop-carried 388 /// dependencies. The final execution order of the instructions across 389 /// iterations is not guaranteed, thus, the end result might or might not 390 /// implement actual concurrent execution of instructions across multiple 391 /// iterations. 392 bool isAnnotatedParallel() const; 393 394 /// hasDedicatedExits - Return true if no exit block for the loop 395 /// has a predecessor that is outside the loop. 396 bool hasDedicatedExits() const; 397 398 /// getUniqueExitBlocks - Return all unique successor blocks of this loop. 399 /// These are the blocks _outside of the current loop_ which are branched to. 400 /// This assumes that loop exits are in canonical form. 401 /// 402 void getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const; 403 404 /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one 405 /// block, return that block. Otherwise return null. 406 BasicBlock *getUniqueExitBlock() const; 407 408 void dump() const; 409 410 private: 411 friend class LoopInfoBase<BasicBlock, Loop>; 412 explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {} 413 }; 414 415 //===----------------------------------------------------------------------===// 416 /// LoopInfo - This class builds and contains all of the top level loop 417 /// structures in the specified function. 418 /// 419 420 template<class BlockT, class LoopT> 421 class LoopInfoBase { 422 // BBMap - Mapping of basic blocks to the inner most loop they occur in 423 DenseMap<BlockT *, LoopT *> BBMap; 424 std::vector<LoopT *> TopLevelLoops; 425 friend class LoopBase<BlockT, LoopT>; 426 friend class LoopInfo; 427 428 void operator=(const LoopInfoBase &) LLVM_DELETED_FUNCTION; 429 LoopInfoBase(const LoopInfo &) LLVM_DELETED_FUNCTION; 430 public: 431 LoopInfoBase() { } 432 ~LoopInfoBase() { releaseMemory(); } 433 434 void releaseMemory() { 435 for (typename std::vector<LoopT *>::iterator I = 436 TopLevelLoops.begin(), E = TopLevelLoops.end(); I != E; ++I) 437 delete *I; // Delete all of the loops... 438 439 BBMap.clear(); // Reset internal state of analysis 440 TopLevelLoops.clear(); 441 } 442 443 /// iterator/begin/end - The interface to the top-level loops in the current 444 /// function. 445 /// 446 typedef typename std::vector<LoopT *>::const_iterator iterator; 447 typedef typename std::vector<LoopT *>::const_reverse_iterator 448 reverse_iterator; 449 iterator begin() const { return TopLevelLoops.begin(); } 450 iterator end() const { return TopLevelLoops.end(); } 451 reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); } 452 reverse_iterator rend() const { return TopLevelLoops.rend(); } 453 bool empty() const { return TopLevelLoops.empty(); } 454 455 /// getLoopFor - Return the inner most loop that BB lives in. If a basic 456 /// block is in no loop (for example the entry node), null is returned. 457 /// 458 LoopT *getLoopFor(const BlockT *BB) const { 459 return BBMap.lookup(const_cast<BlockT*>(BB)); 460 } 461 462 /// operator[] - same as getLoopFor... 463 /// 464 const LoopT *operator[](const BlockT *BB) const { 465 return getLoopFor(BB); 466 } 467 468 /// getLoopDepth - Return the loop nesting level of the specified block. A 469 /// depth of 0 means the block is not inside any loop. 470 /// 471 unsigned getLoopDepth(const BlockT *BB) const { 472 const LoopT *L = getLoopFor(BB); 473 return L ? L->getLoopDepth() : 0; 474 } 475 476 // isLoopHeader - True if the block is a loop header node 477 bool isLoopHeader(BlockT *BB) const { 478 const LoopT *L = getLoopFor(BB); 479 return L && L->getHeader() == BB; 480 } 481 482 /// removeLoop - This removes the specified top-level loop from this loop info 483 /// object. The loop is not deleted, as it will presumably be inserted into 484 /// another loop. 485 LoopT *removeLoop(iterator I) { 486 assert(I != end() && "Cannot remove end iterator!"); 487 LoopT *L = *I; 488 assert(L->getParentLoop() == 0 && "Not a top-level loop!"); 489 TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin())); 490 return L; 491 } 492 493 /// changeLoopFor - Change the top-level loop that contains BB to the 494 /// specified loop. This should be used by transformations that restructure 495 /// the loop hierarchy tree. 496 void changeLoopFor(BlockT *BB, LoopT *L) { 497 if (!L) { 498 BBMap.erase(BB); 499 return; 500 } 501 BBMap[BB] = L; 502 } 503 504 /// changeTopLevelLoop - Replace the specified loop in the top-level loops 505 /// list with the indicated loop. 506 void changeTopLevelLoop(LoopT *OldLoop, 507 LoopT *NewLoop) { 508 typename std::vector<LoopT *>::iterator I = 509 std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop); 510 assert(I != TopLevelLoops.end() && "Old loop not at top level!"); 511 *I = NewLoop; 512 assert(NewLoop->ParentLoop == 0 && OldLoop->ParentLoop == 0 && 513 "Loops already embedded into a subloop!"); 514 } 515 516 /// addTopLevelLoop - This adds the specified loop to the collection of 517 /// top-level loops. 518 void addTopLevelLoop(LoopT *New) { 519 assert(New->getParentLoop() == 0 && "Loop already in subloop!"); 520 TopLevelLoops.push_back(New); 521 } 522 523 /// removeBlock - This method completely removes BB from all data structures, 524 /// including all of the Loop objects it is nested in and our mapping from 525 /// BasicBlocks to loops. 526 void removeBlock(BlockT *BB) { 527 typename DenseMap<BlockT *, LoopT *>::iterator I = BBMap.find(BB); 528 if (I != BBMap.end()) { 529 for (LoopT *L = I->second; L; L = L->getParentLoop()) 530 L->removeBlockFromLoop(BB); 531 532 BBMap.erase(I); 533 } 534 } 535 536 // Internals 537 538 static bool isNotAlreadyContainedIn(const LoopT *SubLoop, 539 const LoopT *ParentLoop) { 540 if (SubLoop == 0) return true; 541 if (SubLoop == ParentLoop) return false; 542 return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop); 543 } 544 545 /// Create the loop forest using a stable algorithm. 546 void Analyze(DominatorTreeBase<BlockT> &DomTree); 547 548 // Debugging 549 550 void print(raw_ostream &OS) const; 551 }; 552 553 // Implementation in LoopInfoImpl.h 554 #ifdef __GNUC__ 555 __extension__ extern template class LoopInfoBase<BasicBlock, Loop>; 556 #endif 557 558 class LoopInfo : public FunctionPass { 559 LoopInfoBase<BasicBlock, Loop> LI; 560 friend class LoopBase<BasicBlock, Loop>; 561 562 void operator=(const LoopInfo &) LLVM_DELETED_FUNCTION; 563 LoopInfo(const LoopInfo &) LLVM_DELETED_FUNCTION; 564 public: 565 static char ID; // Pass identification, replacement for typeid 566 567 LoopInfo() : FunctionPass(ID) { 568 initializeLoopInfoPass(*PassRegistry::getPassRegistry()); 569 } 570 571 LoopInfoBase<BasicBlock, Loop>& getBase() { return LI; } 572 573 /// iterator/begin/end - The interface to the top-level loops in the current 574 /// function. 575 /// 576 typedef LoopInfoBase<BasicBlock, Loop>::iterator iterator; 577 typedef LoopInfoBase<BasicBlock, Loop>::reverse_iterator reverse_iterator; 578 inline iterator begin() const { return LI.begin(); } 579 inline iterator end() const { return LI.end(); } 580 inline reverse_iterator rbegin() const { return LI.rbegin(); } 581 inline reverse_iterator rend() const { return LI.rend(); } 582 bool empty() const { return LI.empty(); } 583 584 /// getLoopFor - Return the inner most loop that BB lives in. If a basic 585 /// block is in no loop (for example the entry node), null is returned. 586 /// 587 inline Loop *getLoopFor(const BasicBlock *BB) const { 588 return LI.getLoopFor(BB); 589 } 590 591 /// operator[] - same as getLoopFor... 592 /// 593 inline const Loop *operator[](const BasicBlock *BB) const { 594 return LI.getLoopFor(BB); 595 } 596 597 /// getLoopDepth - Return the loop nesting level of the specified block. A 598 /// depth of 0 means the block is not inside any loop. 599 /// 600 inline unsigned getLoopDepth(const BasicBlock *BB) const { 601 return LI.getLoopDepth(BB); 602 } 603 604 // isLoopHeader - True if the block is a loop header node 605 inline bool isLoopHeader(BasicBlock *BB) const { 606 return LI.isLoopHeader(BB); 607 } 608 609 /// runOnFunction - Calculate the natural loop information. 610 /// 611 virtual bool runOnFunction(Function &F); 612 613 virtual void verifyAnalysis() const; 614 615 virtual void releaseMemory() { LI.releaseMemory(); } 616 617 virtual void print(raw_ostream &O, const Module* M = 0) const; 618 619 virtual void getAnalysisUsage(AnalysisUsage &AU) const; 620 621 /// removeLoop - This removes the specified top-level loop from this loop info 622 /// object. The loop is not deleted, as it will presumably be inserted into 623 /// another loop. 624 inline Loop *removeLoop(iterator I) { return LI.removeLoop(I); } 625 626 /// changeLoopFor - Change the top-level loop that contains BB to the 627 /// specified loop. This should be used by transformations that restructure 628 /// the loop hierarchy tree. 629 inline void changeLoopFor(BasicBlock *BB, Loop *L) { 630 LI.changeLoopFor(BB, L); 631 } 632 633 /// changeTopLevelLoop - Replace the specified loop in the top-level loops 634 /// list with the indicated loop. 635 inline void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop) { 636 LI.changeTopLevelLoop(OldLoop, NewLoop); 637 } 638 639 /// addTopLevelLoop - This adds the specified loop to the collection of 640 /// top-level loops. 641 inline void addTopLevelLoop(Loop *New) { 642 LI.addTopLevelLoop(New); 643 } 644 645 /// removeBlock - This method completely removes BB from all data structures, 646 /// including all of the Loop objects it is nested in and our mapping from 647 /// BasicBlocks to loops. 648 void removeBlock(BasicBlock *BB) { 649 LI.removeBlock(BB); 650 } 651 652 /// updateUnloop - Update LoopInfo after removing the last backedge from a 653 /// loop--now the "unloop". This updates the loop forest and parent loops for 654 /// each block so that Unloop is no longer referenced, but the caller must 655 /// actually delete the Unloop object. 656 void updateUnloop(Loop *Unloop); 657 658 /// replacementPreservesLCSSAForm - Returns true if replacing From with To 659 /// everywhere is guaranteed to preserve LCSSA form. 660 bool replacementPreservesLCSSAForm(Instruction *From, Value *To) { 661 // Preserving LCSSA form is only problematic if the replacing value is an 662 // instruction. 663 Instruction *I = dyn_cast<Instruction>(To); 664 if (!I) return true; 665 // If both instructions are defined in the same basic block then replacement 666 // cannot break LCSSA form. 667 if (I->getParent() == From->getParent()) 668 return true; 669 // If the instruction is not defined in a loop then it can safely replace 670 // anything. 671 Loop *ToLoop = getLoopFor(I->getParent()); 672 if (!ToLoop) return true; 673 // If the replacing instruction is defined in the same loop as the original 674 // instruction, or in a loop that contains it as an inner loop, then using 675 // it as a replacement will not break LCSSA form. 676 return ToLoop->contains(getLoopFor(From->getParent())); 677 } 678 }; 679 680 681 // Allow clients to walk the list of nested loops... 682 template <> struct GraphTraits<const Loop*> { 683 typedef const Loop NodeType; 684 typedef LoopInfo::iterator ChildIteratorType; 685 686 static NodeType *getEntryNode(const Loop *L) { return L; } 687 static inline ChildIteratorType child_begin(NodeType *N) { 688 return N->begin(); 689 } 690 static inline ChildIteratorType child_end(NodeType *N) { 691 return N->end(); 692 } 693 }; 694 695 template <> struct GraphTraits<Loop*> { 696 typedef Loop NodeType; 697 typedef LoopInfo::iterator ChildIteratorType; 698 699 static NodeType *getEntryNode(Loop *L) { return L; } 700 static inline ChildIteratorType child_begin(NodeType *N) { 701 return N->begin(); 702 } 703 static inline ChildIteratorType child_end(NodeType *N) { 704 return N->end(); 705 } 706 }; 707 708 } // End llvm namespace 709 710 #endif 711