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