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 // Note that this analysis specifically identifies *Loops* not cycles or SCCs 29 // in the CFG. There can be strongly connected components in the CFG which 30 // this analysis will not recognize and that will not be represented by a Loop 31 // instance. In particular, a Loop might be inside such a non-loop SCC, or a 32 // non-loop SCC might contain a sub-SCC which is a Loop. 33 // 34 //===----------------------------------------------------------------------===// 35 36 #ifndef LLVM_ANALYSIS_LOOPINFO_H 37 #define LLVM_ANALYSIS_LOOPINFO_H 38 39 #include "llvm/ADT/DenseMap.h" 40 #include "llvm/ADT/DenseSet.h" 41 #include "llvm/ADT/GraphTraits.h" 42 #include "llvm/ADT/SmallPtrSet.h" 43 #include "llvm/ADT/SmallVector.h" 44 #include "llvm/IR/CFG.h" 45 #include "llvm/IR/Instruction.h" 46 #include "llvm/IR/Instructions.h" 47 #include "llvm/IR/PassManager.h" 48 #include "llvm/Pass.h" 49 #include "llvm/Support/Allocator.h" 50 #include <algorithm> 51 #include <utility> 52 53 namespace llvm { 54 55 class DominatorTree; 56 class LoopInfo; 57 class Loop; 58 class MDNode; 59 class PHINode; 60 class raw_ostream; 61 template <class N, bool IsPostDom> class DominatorTreeBase; 62 template <class N, class M> class LoopInfoBase; 63 template <class N, class M> class LoopBase; 64 65 //===----------------------------------------------------------------------===// 66 /// Instances of this class are used to represent loops that are detected in the 67 /// flow graph. 68 /// 69 template <class BlockT, class LoopT> class LoopBase { 70 LoopT *ParentLoop; 71 // Loops contained entirely within this one. 72 std::vector<LoopT *> SubLoops; 73 74 // The list of blocks in this loop. First entry is the header node. 75 std::vector<BlockT *> Blocks; 76 77 SmallPtrSet<const BlockT *, 8> DenseBlockSet; 78 79 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 80 /// Indicator that this loop is no longer a valid loop. 81 bool IsInvalid = false; 82 #endif 83 84 LoopBase(const LoopBase<BlockT, LoopT> &) = delete; 85 const LoopBase<BlockT, LoopT> & 86 operator=(const LoopBase<BlockT, LoopT> &) = delete; 87 88 public: 89 /// Return the nesting level of this loop. An outer-most loop has depth 1, 90 /// for consistency with loop depth values used for basic blocks, where depth 91 /// 0 is used for blocks not inside any loops. getLoopDepth()92 unsigned getLoopDepth() const { 93 assert(!isInvalid() && "Loop not in a valid state!"); 94 unsigned D = 1; 95 for (const LoopT *CurLoop = ParentLoop; CurLoop; 96 CurLoop = CurLoop->ParentLoop) 97 ++D; 98 return D; 99 } getHeader()100 BlockT *getHeader() const { return getBlocks().front(); } getParentLoop()101 LoopT *getParentLoop() const { return ParentLoop; } 102 103 /// This is a raw interface for bypassing addChildLoop. setParentLoop(LoopT * L)104 void setParentLoop(LoopT *L) { 105 assert(!isInvalid() && "Loop not in a valid state!"); 106 ParentLoop = L; 107 } 108 109 /// Return true if the specified loop is contained within in this loop. contains(const LoopT * L)110 bool contains(const LoopT *L) const { 111 assert(!isInvalid() && "Loop not in a valid state!"); 112 if (L == this) 113 return true; 114 if (!L) 115 return false; 116 return contains(L->getParentLoop()); 117 } 118 119 /// Return true if the specified basic block is in this loop. contains(const BlockT * BB)120 bool contains(const BlockT *BB) const { 121 assert(!isInvalid() && "Loop not in a valid state!"); 122 return DenseBlockSet.count(BB); 123 } 124 125 /// Return true if the specified instruction is in this loop. contains(const InstT * Inst)126 template <class InstT> bool contains(const InstT *Inst) const { 127 return contains(Inst->getParent()); 128 } 129 130 /// Return the loops contained entirely within this loop. getSubLoops()131 const std::vector<LoopT *> &getSubLoops() const { 132 assert(!isInvalid() && "Loop not in a valid state!"); 133 return SubLoops; 134 } getSubLoopsVector()135 std::vector<LoopT *> &getSubLoopsVector() { 136 assert(!isInvalid() && "Loop not in a valid state!"); 137 return SubLoops; 138 } 139 typedef typename std::vector<LoopT *>::const_iterator iterator; 140 typedef 141 typename std::vector<LoopT *>::const_reverse_iterator reverse_iterator; begin()142 iterator begin() const { return getSubLoops().begin(); } end()143 iterator end() const { return getSubLoops().end(); } rbegin()144 reverse_iterator rbegin() const { return getSubLoops().rbegin(); } rend()145 reverse_iterator rend() const { return getSubLoops().rend(); } empty()146 bool empty() const { return getSubLoops().empty(); } 147 148 /// Get a list of the basic blocks which make up this loop. getBlocks()149 ArrayRef<BlockT *> getBlocks() const { 150 assert(!isInvalid() && "Loop not in a valid state!"); 151 return Blocks; 152 } 153 typedef typename ArrayRef<BlockT *>::const_iterator block_iterator; block_begin()154 block_iterator block_begin() const { return getBlocks().begin(); } block_end()155 block_iterator block_end() const { return getBlocks().end(); } blocks()156 inline iterator_range<block_iterator> blocks() const { 157 assert(!isInvalid() && "Loop not in a valid state!"); 158 return make_range(block_begin(), block_end()); 159 } 160 161 /// Get the number of blocks in this loop in constant time. 162 /// Invalidate the loop, indicating that it is no longer a loop. getNumBlocks()163 unsigned getNumBlocks() const { 164 assert(!isInvalid() && "Loop not in a valid state!"); 165 return Blocks.size(); 166 } 167 168 /// Return a direct, mutable handle to the blocks vector so that we can 169 /// mutate it efficiently with techniques like `std::remove`. getBlocksVector()170 std::vector<BlockT *> &getBlocksVector() { 171 assert(!isInvalid() && "Loop not in a valid state!"); 172 return Blocks; 173 } 174 /// Return a direct, mutable handle to the blocks set so that we can 175 /// mutate it efficiently. getBlocksSet()176 SmallPtrSetImpl<const BlockT *> &getBlocksSet() { 177 assert(!isInvalid() && "Loop not in a valid state!"); 178 return DenseBlockSet; 179 } 180 181 /// Return a direct, immutable handle to the blocks set. getBlocksSet()182 const SmallPtrSetImpl<const BlockT *> &getBlocksSet() const { 183 assert(!isInvalid() && "Loop not in a valid state!"); 184 return DenseBlockSet; 185 } 186 187 /// Return true if this loop is no longer valid. The only valid use of this 188 /// helper is "assert(L.isInvalid())" or equivalent, since IsInvalid is set to 189 /// true by the destructor. In other words, if this accessor returns true, 190 /// the caller has already triggered UB by calling this accessor; and so it 191 /// can only be called in a context where a return value of true indicates a 192 /// programmer error. isInvalid()193 bool isInvalid() const { 194 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 195 return IsInvalid; 196 #else 197 return false; 198 #endif 199 } 200 201 /// True if terminator in the block can branch to another block that is 202 /// outside of the current loop. isLoopExiting(const BlockT * BB)203 bool isLoopExiting(const BlockT *BB) const { 204 assert(!isInvalid() && "Loop not in a valid state!"); 205 for (const auto &Succ : children<const BlockT *>(BB)) { 206 if (!contains(Succ)) 207 return true; 208 } 209 return false; 210 } 211 212 /// Returns true if \p BB is a loop-latch. 213 /// A latch block is a block that contains a branch back to the header. 214 /// This function is useful when there are multiple latches in a loop 215 /// because \fn getLoopLatch will return nullptr in that case. isLoopLatch(const BlockT * BB)216 bool isLoopLatch(const BlockT *BB) const { 217 assert(!isInvalid() && "Loop not in a valid state!"); 218 assert(contains(BB) && "block does not belong to the loop"); 219 220 BlockT *Header = getHeader(); 221 auto PredBegin = GraphTraits<Inverse<BlockT *>>::child_begin(Header); 222 auto PredEnd = GraphTraits<Inverse<BlockT *>>::child_end(Header); 223 return std::find(PredBegin, PredEnd, BB) != PredEnd; 224 } 225 226 /// Calculate the number of back edges to the loop header. getNumBackEdges()227 unsigned getNumBackEdges() const { 228 assert(!isInvalid() && "Loop not in a valid state!"); 229 unsigned NumBackEdges = 0; 230 BlockT *H = getHeader(); 231 232 for (const auto Pred : children<Inverse<BlockT *>>(H)) 233 if (contains(Pred)) 234 ++NumBackEdges; 235 236 return NumBackEdges; 237 } 238 239 //===--------------------------------------------------------------------===// 240 // APIs for simple analysis of the loop. 241 // 242 // Note that all of these methods can fail on general loops (ie, there may not 243 // be a preheader, etc). For best success, the loop simplification and 244 // induction variable canonicalization pass should be used to normalize loops 245 // for easy analysis. These methods assume canonical loops. 246 247 /// Return all blocks inside the loop that have successors outside of the 248 /// loop. These are the blocks _inside of the current loop_ which branch out. 249 /// The returned list is always unique. 250 void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const; 251 252 /// If getExitingBlocks would return exactly one block, return that block. 253 /// Otherwise return null. 254 BlockT *getExitingBlock() const; 255 256 /// Return all of the successor blocks of this loop. These are the blocks 257 /// _outside of the current loop_ which are branched to. 258 void getExitBlocks(SmallVectorImpl<BlockT *> &ExitBlocks) const; 259 260 /// If getExitBlocks would return exactly one block, return that block. 261 /// Otherwise return null. 262 BlockT *getExitBlock() const; 263 264 /// Return true if no exit block for the loop has a predecessor that is 265 /// outside the loop. 266 bool hasDedicatedExits() const; 267 268 /// Return all unique successor blocks of this loop. 269 /// These are the blocks _outside of the current loop_ which are branched to. 270 /// This assumes that loop exits are in canonical form, i.e. all exits are 271 /// dedicated exits. 272 void getUniqueExitBlocks(SmallVectorImpl<BlockT *> &ExitBlocks) const; 273 274 /// If getUniqueExitBlocks would return exactly one block, return that block. 275 /// Otherwise return null. 276 BlockT *getUniqueExitBlock() const; 277 278 /// Edge type. 279 typedef std::pair<const BlockT *, const BlockT *> Edge; 280 281 /// Return all pairs of (_inside_block_,_outside_block_). 282 void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const; 283 284 /// If there is a preheader for this loop, return it. A loop has a preheader 285 /// if there is only one edge to the header of the loop from outside of the 286 /// loop. If this is the case, the block branching to the header of the loop 287 /// is the preheader node. 288 /// 289 /// This method returns null if there is no preheader for the loop. 290 BlockT *getLoopPreheader() const; 291 292 /// If the given loop's header has exactly one unique predecessor outside the 293 /// loop, return it. Otherwise return null. 294 /// This is less strict that the loop "preheader" concept, which requires 295 /// the predecessor to have exactly one successor. 296 BlockT *getLoopPredecessor() const; 297 298 /// If there is a single latch block for this loop, return it. 299 /// A latch block is a block that contains a branch back to the header. 300 BlockT *getLoopLatch() const; 301 302 /// Return all loop latch blocks of this loop. A latch block is a block that 303 /// contains a branch back to the header. getLoopLatches(SmallVectorImpl<BlockT * > & LoopLatches)304 void getLoopLatches(SmallVectorImpl<BlockT *> &LoopLatches) const { 305 assert(!isInvalid() && "Loop not in a valid state!"); 306 BlockT *H = getHeader(); 307 for (const auto Pred : children<Inverse<BlockT *>>(H)) 308 if (contains(Pred)) 309 LoopLatches.push_back(Pred); 310 } 311 312 //===--------------------------------------------------------------------===// 313 // APIs for updating loop information after changing the CFG 314 // 315 316 /// This method is used by other analyses to update loop information. 317 /// NewBB is set to be a new member of the current loop. 318 /// Because of this, it is added as a member of all parent loops, and is added 319 /// to the specified LoopInfo object as being in the current basic block. It 320 /// is not valid to replace the loop header with this method. 321 void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI); 322 323 /// This is used when splitting loops up. It replaces the OldChild entry in 324 /// our children list with NewChild, and updates the parent pointer of 325 /// OldChild to be null and the NewChild to be this loop. 326 /// This updates the loop depth of the new child. 327 void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild); 328 329 /// Add the specified loop to be a child of this loop. 330 /// This updates the loop depth of the new child. addChildLoop(LoopT * NewChild)331 void addChildLoop(LoopT *NewChild) { 332 assert(!isInvalid() && "Loop not in a valid state!"); 333 assert(!NewChild->ParentLoop && "NewChild already has a parent!"); 334 NewChild->ParentLoop = static_cast<LoopT *>(this); 335 SubLoops.push_back(NewChild); 336 } 337 338 /// This removes the specified child from being a subloop of this loop. The 339 /// loop is not deleted, as it will presumably be inserted into another loop. removeChildLoop(iterator I)340 LoopT *removeChildLoop(iterator I) { 341 assert(!isInvalid() && "Loop not in a valid state!"); 342 assert(I != SubLoops.end() && "Cannot remove end iterator!"); 343 LoopT *Child = *I; 344 assert(Child->ParentLoop == this && "Child is not a child of this loop!"); 345 SubLoops.erase(SubLoops.begin() + (I - begin())); 346 Child->ParentLoop = nullptr; 347 return Child; 348 } 349 350 /// This removes the specified child from being a subloop of this loop. The 351 /// loop is not deleted, as it will presumably be inserted into another loop. removeChildLoop(LoopT * Child)352 LoopT *removeChildLoop(LoopT *Child) { 353 return removeChildLoop(llvm::find(*this, Child)); 354 } 355 356 /// This adds a basic block directly to the basic block list. 357 /// This should only be used by transformations that create new loops. Other 358 /// transformations should use addBasicBlockToLoop. addBlockEntry(BlockT * BB)359 void addBlockEntry(BlockT *BB) { 360 assert(!isInvalid() && "Loop not in a valid state!"); 361 Blocks.push_back(BB); 362 DenseBlockSet.insert(BB); 363 } 364 365 /// interface to reverse Blocks[from, end of loop] in this loop reverseBlock(unsigned from)366 void reverseBlock(unsigned from) { 367 assert(!isInvalid() && "Loop not in a valid state!"); 368 std::reverse(Blocks.begin() + from, Blocks.end()); 369 } 370 371 /// interface to do reserve() for Blocks reserveBlocks(unsigned size)372 void reserveBlocks(unsigned size) { 373 assert(!isInvalid() && "Loop not in a valid state!"); 374 Blocks.reserve(size); 375 } 376 377 /// This method is used to move BB (which must be part of this loop) to be the 378 /// loop header of the loop (the block that dominates all others). moveToHeader(BlockT * BB)379 void moveToHeader(BlockT *BB) { 380 assert(!isInvalid() && "Loop not in a valid state!"); 381 if (Blocks[0] == BB) 382 return; 383 for (unsigned i = 0;; ++i) { 384 assert(i != Blocks.size() && "Loop does not contain BB!"); 385 if (Blocks[i] == BB) { 386 Blocks[i] = Blocks[0]; 387 Blocks[0] = BB; 388 return; 389 } 390 } 391 } 392 393 /// This removes the specified basic block from the current loop, updating the 394 /// Blocks as appropriate. This does not update the mapping in the LoopInfo 395 /// class. removeBlockFromLoop(BlockT * BB)396 void removeBlockFromLoop(BlockT *BB) { 397 assert(!isInvalid() && "Loop not in a valid state!"); 398 auto I = find(Blocks, BB); 399 assert(I != Blocks.end() && "N is not in this list!"); 400 Blocks.erase(I); 401 402 DenseBlockSet.erase(BB); 403 } 404 405 /// Verify loop structure 406 void verifyLoop() const; 407 408 /// Verify loop structure of this loop and all nested loops. 409 void verifyLoopNest(DenseSet<const LoopT *> *Loops) const; 410 411 /// Returns true if the loop is annotated parallel. 412 /// 413 /// Derived classes can override this method using static template 414 /// polymorphism. isAnnotatedParallel()415 bool isAnnotatedParallel() const { return false; } 416 417 /// Print loop with all the BBs inside it. 418 void print(raw_ostream &OS, unsigned Depth = 0, bool Verbose = false) const; 419 420 protected: 421 friend class LoopInfoBase<BlockT, LoopT>; 422 423 /// This creates an empty loop. LoopBase()424 LoopBase() : ParentLoop(nullptr) {} 425 LoopBase(BlockT * BB)426 explicit LoopBase(BlockT *BB) : ParentLoop(nullptr) { 427 Blocks.push_back(BB); 428 DenseBlockSet.insert(BB); 429 } 430 431 // Since loop passes like SCEV are allowed to key analysis results off of 432 // `Loop` pointers, we cannot re-use pointers within a loop pass manager. 433 // This means loop passes should not be `delete` ing `Loop` objects directly 434 // (and risk a later `Loop` allocation re-using the address of a previous one) 435 // but should be using LoopInfo::markAsRemoved, which keeps around the `Loop` 436 // pointer till the end of the lifetime of the `LoopInfo` object. 437 // 438 // To make it easier to follow this rule, we mark the destructor as 439 // non-public. ~LoopBase()440 ~LoopBase() { 441 for (auto *SubLoop : SubLoops) 442 SubLoop->~LoopT(); 443 444 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 445 IsInvalid = true; 446 #endif 447 SubLoops.clear(); 448 Blocks.clear(); 449 DenseBlockSet.clear(); 450 ParentLoop = nullptr; 451 } 452 }; 453 454 template <class BlockT, class LoopT> 455 raw_ostream &operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) { 456 Loop.print(OS); 457 return OS; 458 } 459 460 // Implementation in LoopInfoImpl.h 461 extern template class LoopBase<BasicBlock, Loop>; 462 463 /// Represents a single loop in the control flow graph. Note that not all SCCs 464 /// in the CFG are necessarily loops. 465 class Loop : public LoopBase<BasicBlock, Loop> { 466 public: 467 /// A range representing the start and end location of a loop. 468 class LocRange { 469 DebugLoc Start; 470 DebugLoc End; 471 472 public: LocRange()473 LocRange() {} LocRange(DebugLoc Start)474 LocRange(DebugLoc Start) : Start(std::move(Start)), End(std::move(Start)) {} LocRange(DebugLoc Start,DebugLoc End)475 LocRange(DebugLoc Start, DebugLoc End) 476 : Start(std::move(Start)), End(std::move(End)) {} 477 getStart()478 const DebugLoc &getStart() const { return Start; } getEnd()479 const DebugLoc &getEnd() const { return End; } 480 481 /// Check for null. 482 /// 483 explicit operator bool() const { return Start && End; } 484 }; 485 486 /// Return true if the specified value is loop invariant. 487 bool isLoopInvariant(const Value *V) const; 488 489 /// Return true if all the operands of the specified instruction are loop 490 /// invariant. 491 bool hasLoopInvariantOperands(const Instruction *I) const; 492 493 /// If the given value is an instruction inside of the loop and it can be 494 /// hoisted, do so to make it trivially loop-invariant. 495 /// Return true if the value after any hoisting is loop invariant. This 496 /// function can be used as a slightly more aggressive replacement for 497 /// isLoopInvariant. 498 /// 499 /// If InsertPt is specified, it is the point to hoist instructions to. 500 /// If null, the terminator of the loop preheader is used. 501 bool makeLoopInvariant(Value *V, bool &Changed, 502 Instruction *InsertPt = nullptr) const; 503 504 /// If the given instruction is inside of the loop and it can be hoisted, do 505 /// so to make it trivially loop-invariant. 506 /// Return true if the instruction after any hoisting is loop invariant. This 507 /// function can be used as a slightly more aggressive replacement for 508 /// isLoopInvariant. 509 /// 510 /// If InsertPt is specified, it is the point to hoist instructions to. 511 /// If null, the terminator of the loop preheader is used. 512 /// 513 bool makeLoopInvariant(Instruction *I, bool &Changed, 514 Instruction *InsertPt = nullptr) const; 515 516 /// Check to see if the loop has a canonical induction variable: an integer 517 /// recurrence that starts at 0 and increments by one each time through the 518 /// loop. If so, return the phi node that corresponds to it. 519 /// 520 /// The IndVarSimplify pass transforms loops to have a canonical induction 521 /// variable. 522 /// 523 PHINode *getCanonicalInductionVariable() const; 524 525 /// Return true if the Loop is in LCSSA form. 526 bool isLCSSAForm(DominatorTree &DT) const; 527 528 /// Return true if this Loop and all inner subloops are in LCSSA form. 529 bool isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const; 530 531 /// Return true if the Loop is in the form that the LoopSimplify form 532 /// transforms loops to, which is sometimes called normal form. 533 bool isLoopSimplifyForm() const; 534 535 /// Return true if the loop body is safe to clone in practice. 536 bool isSafeToClone() const; 537 538 /// Returns true if the loop is annotated parallel. 539 /// 540 /// A parallel loop can be assumed to not contain any dependencies between 541 /// iterations by the compiler. That is, any loop-carried dependency checking 542 /// can be skipped completely when parallelizing the loop on the target 543 /// machine. Thus, if the parallel loop information originates from the 544 /// programmer, e.g. via the OpenMP parallel for pragma, it is the 545 /// programmer's responsibility to ensure there are no loop-carried 546 /// dependencies. The final execution order of the instructions across 547 /// iterations is not guaranteed, thus, the end result might or might not 548 /// implement actual concurrent execution of instructions across multiple 549 /// iterations. 550 bool isAnnotatedParallel() const; 551 552 /// Return the llvm.loop loop id metadata node for this loop if it is present. 553 /// 554 /// If this loop contains the same llvm.loop metadata on each branch to the 555 /// header then the node is returned. If any latch instruction does not 556 /// contain llvm.loop or if multiple latches contain different nodes then 557 /// 0 is returned. 558 MDNode *getLoopID() const; 559 /// Set the llvm.loop loop id metadata for this loop. 560 /// 561 /// The LoopID metadata node will be added to each terminator instruction in 562 /// the loop that branches to the loop header. 563 /// 564 /// The LoopID metadata node should have one or more operands and the first 565 /// operand should be the node itself. 566 void setLoopID(MDNode *LoopID) const; 567 568 /// Add llvm.loop.unroll.disable to this loop's loop id metadata. 569 /// 570 /// Remove existing unroll metadata and add unroll disable metadata to 571 /// indicate the loop has already been unrolled. This prevents a loop 572 /// from being unrolled more than is directed by a pragma if the loop 573 /// unrolling pass is run more than once (which it generally is). 574 void setLoopAlreadyUnrolled(); 575 576 void dump() const; 577 void dumpVerbose() const; 578 579 /// Return the debug location of the start of this loop. 580 /// This looks for a BB terminating instruction with a known debug 581 /// location by looking at the preheader and header blocks. If it 582 /// cannot find a terminating instruction with location information, 583 /// it returns an unknown location. 584 DebugLoc getStartLoc() const; 585 586 /// Return the source code span of the loop. 587 LocRange getLocRange() const; 588 getName()589 StringRef getName() const { 590 if (BasicBlock *Header = getHeader()) 591 if (Header->hasName()) 592 return Header->getName(); 593 return "<unnamed loop>"; 594 } 595 596 private: 597 Loop() = default; 598 599 friend class LoopInfoBase<BasicBlock, Loop>; 600 friend class LoopBase<BasicBlock, Loop>; Loop(BasicBlock * BB)601 explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {} 602 ~Loop() = default; 603 }; 604 605 //===----------------------------------------------------------------------===// 606 /// This class builds and contains all of the top-level loop 607 /// structures in the specified function. 608 /// 609 610 template <class BlockT, class LoopT> class LoopInfoBase { 611 // BBMap - Mapping of basic blocks to the inner most loop they occur in 612 DenseMap<const BlockT *, LoopT *> BBMap; 613 std::vector<LoopT *> TopLevelLoops; 614 BumpPtrAllocator LoopAllocator; 615 616 friend class LoopBase<BlockT, LoopT>; 617 friend class LoopInfo; 618 619 void operator=(const LoopInfoBase &) = delete; 620 LoopInfoBase(const LoopInfoBase &) = delete; 621 622 public: LoopInfoBase()623 LoopInfoBase() {} ~LoopInfoBase()624 ~LoopInfoBase() { releaseMemory(); } 625 LoopInfoBase(LoopInfoBase && Arg)626 LoopInfoBase(LoopInfoBase &&Arg) 627 : BBMap(std::move(Arg.BBMap)), 628 TopLevelLoops(std::move(Arg.TopLevelLoops)), 629 LoopAllocator(std::move(Arg.LoopAllocator)) { 630 // We have to clear the arguments top level loops as we've taken ownership. 631 Arg.TopLevelLoops.clear(); 632 } 633 LoopInfoBase &operator=(LoopInfoBase &&RHS) { 634 BBMap = std::move(RHS.BBMap); 635 636 for (auto *L : TopLevelLoops) 637 L->~LoopT(); 638 639 TopLevelLoops = std::move(RHS.TopLevelLoops); 640 LoopAllocator = std::move(RHS.LoopAllocator); 641 RHS.TopLevelLoops.clear(); 642 return *this; 643 } 644 releaseMemory()645 void releaseMemory() { 646 BBMap.clear(); 647 648 for (auto *L : TopLevelLoops) 649 L->~LoopT(); 650 TopLevelLoops.clear(); 651 LoopAllocator.Reset(); 652 } 653 AllocateLoop(ArgsTy &&...Args)654 template <typename... ArgsTy> LoopT *AllocateLoop(ArgsTy &&... Args) { 655 LoopT *Storage = LoopAllocator.Allocate<LoopT>(); 656 return new (Storage) LoopT(std::forward<ArgsTy>(Args)...); 657 } 658 659 /// iterator/begin/end - The interface to the top-level loops in the current 660 /// function. 661 /// 662 typedef typename std::vector<LoopT *>::const_iterator iterator; 663 typedef 664 typename std::vector<LoopT *>::const_reverse_iterator reverse_iterator; begin()665 iterator begin() const { return TopLevelLoops.begin(); } end()666 iterator end() const { return TopLevelLoops.end(); } rbegin()667 reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); } rend()668 reverse_iterator rend() const { return TopLevelLoops.rend(); } empty()669 bool empty() const { return TopLevelLoops.empty(); } 670 671 /// Return all of the loops in the function in preorder across the loop 672 /// nests, with siblings in forward program order. 673 /// 674 /// Note that because loops form a forest of trees, preorder is equivalent to 675 /// reverse postorder. 676 SmallVector<LoopT *, 4> getLoopsInPreorder(); 677 678 /// Return all of the loops in the function in preorder across the loop 679 /// nests, with siblings in *reverse* program order. 680 /// 681 /// Note that because loops form a forest of trees, preorder is equivalent to 682 /// reverse postorder. 683 /// 684 /// Also note that this is *not* a reverse preorder. Only the siblings are in 685 /// reverse program order. 686 SmallVector<LoopT *, 4> getLoopsInReverseSiblingPreorder(); 687 688 /// Return the inner most loop that BB lives in. If a basic block is in no 689 /// loop (for example the entry node), null is returned. getLoopFor(const BlockT * BB)690 LoopT *getLoopFor(const BlockT *BB) const { return BBMap.lookup(BB); } 691 692 /// Same as getLoopFor. 693 const LoopT *operator[](const BlockT *BB) const { return getLoopFor(BB); } 694 695 /// Return the loop nesting level of the specified block. A depth of 0 means 696 /// the block is not inside any loop. getLoopDepth(const BlockT * BB)697 unsigned getLoopDepth(const BlockT *BB) const { 698 const LoopT *L = getLoopFor(BB); 699 return L ? L->getLoopDepth() : 0; 700 } 701 702 // True if the block is a loop header node isLoopHeader(const BlockT * BB)703 bool isLoopHeader(const BlockT *BB) const { 704 const LoopT *L = getLoopFor(BB); 705 return L && L->getHeader() == BB; 706 } 707 708 /// This removes the specified top-level loop from this loop info object. 709 /// The loop is not deleted, as it will presumably be inserted into 710 /// another loop. removeLoop(iterator I)711 LoopT *removeLoop(iterator I) { 712 assert(I != end() && "Cannot remove end iterator!"); 713 LoopT *L = *I; 714 assert(!L->getParentLoop() && "Not a top-level loop!"); 715 TopLevelLoops.erase(TopLevelLoops.begin() + (I - begin())); 716 return L; 717 } 718 719 /// Change the top-level loop that contains BB to the specified loop. 720 /// This should be used by transformations that restructure the loop hierarchy 721 /// tree. changeLoopFor(BlockT * BB,LoopT * L)722 void changeLoopFor(BlockT *BB, LoopT *L) { 723 if (!L) { 724 BBMap.erase(BB); 725 return; 726 } 727 BBMap[BB] = L; 728 } 729 730 /// Replace the specified loop in the top-level loops list with the indicated 731 /// loop. changeTopLevelLoop(LoopT * OldLoop,LoopT * NewLoop)732 void changeTopLevelLoop(LoopT *OldLoop, LoopT *NewLoop) { 733 auto I = find(TopLevelLoops, OldLoop); 734 assert(I != TopLevelLoops.end() && "Old loop not at top level!"); 735 *I = NewLoop; 736 assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop && 737 "Loops already embedded into a subloop!"); 738 } 739 740 /// This adds the specified loop to the collection of top-level loops. addTopLevelLoop(LoopT * New)741 void addTopLevelLoop(LoopT *New) { 742 assert(!New->getParentLoop() && "Loop already in subloop!"); 743 TopLevelLoops.push_back(New); 744 } 745 746 /// This method completely removes BB from all data structures, 747 /// including all of the Loop objects it is nested in and our mapping from 748 /// BasicBlocks to loops. removeBlock(BlockT * BB)749 void removeBlock(BlockT *BB) { 750 auto I = BBMap.find(BB); 751 if (I != BBMap.end()) { 752 for (LoopT *L = I->second; L; L = L->getParentLoop()) 753 L->removeBlockFromLoop(BB); 754 755 BBMap.erase(I); 756 } 757 } 758 759 // Internals 760 isNotAlreadyContainedIn(const LoopT * SubLoop,const LoopT * ParentLoop)761 static bool isNotAlreadyContainedIn(const LoopT *SubLoop, 762 const LoopT *ParentLoop) { 763 if (!SubLoop) 764 return true; 765 if (SubLoop == ParentLoop) 766 return false; 767 return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop); 768 } 769 770 /// Create the loop forest using a stable algorithm. 771 void analyze(const DominatorTreeBase<BlockT, false> &DomTree); 772 773 // Debugging 774 void print(raw_ostream &OS) const; 775 776 void verify(const DominatorTreeBase<BlockT, false> &DomTree) const; 777 778 /// Destroy a loop that has been removed from the `LoopInfo` nest. 779 /// 780 /// This runs the destructor of the loop object making it invalid to 781 /// reference afterward. The memory is retained so that the *pointer* to the 782 /// loop remains valid. 783 /// 784 /// The caller is responsible for removing this loop from the loop nest and 785 /// otherwise disconnecting it from the broader `LoopInfo` data structures. 786 /// Callers that don't naturally handle this themselves should probably call 787 /// `erase' instead. destroy(LoopT * L)788 void destroy(LoopT *L) { 789 L->~LoopT(); 790 791 // Since LoopAllocator is a BumpPtrAllocator, this Deallocate only poisons 792 // \c L, but the pointer remains valid for non-dereferencing uses. 793 LoopAllocator.Deallocate(L); 794 } 795 }; 796 797 // Implementation in LoopInfoImpl.h 798 extern template class LoopInfoBase<BasicBlock, Loop>; 799 800 class LoopInfo : public LoopInfoBase<BasicBlock, Loop> { 801 typedef LoopInfoBase<BasicBlock, Loop> BaseT; 802 803 friend class LoopBase<BasicBlock, Loop>; 804 805 void operator=(const LoopInfo &) = delete; 806 LoopInfo(const LoopInfo &) = delete; 807 808 public: LoopInfo()809 LoopInfo() {} 810 explicit LoopInfo(const DominatorTreeBase<BasicBlock, false> &DomTree); 811 LoopInfo(LoopInfo && Arg)812 LoopInfo(LoopInfo &&Arg) : BaseT(std::move(static_cast<BaseT &>(Arg))) {} 813 LoopInfo &operator=(LoopInfo &&RHS) { 814 BaseT::operator=(std::move(static_cast<BaseT &>(RHS))); 815 return *this; 816 } 817 818 /// Handle invalidation explicitly. 819 bool invalidate(Function &F, const PreservedAnalyses &PA, 820 FunctionAnalysisManager::Invalidator &); 821 822 // Most of the public interface is provided via LoopInfoBase. 823 824 /// Update LoopInfo after removing the last backedge from a loop. This updates 825 /// the loop forest and parent loops for each block so that \c L is no longer 826 /// referenced, but does not actually delete \c L immediately. The pointer 827 /// will remain valid until this LoopInfo's memory is released. 828 void erase(Loop *L); 829 830 /// Returns true if replacing From with To everywhere is guaranteed to 831 /// preserve LCSSA form. replacementPreservesLCSSAForm(Instruction * From,Value * To)832 bool replacementPreservesLCSSAForm(Instruction *From, Value *To) { 833 // Preserving LCSSA form is only problematic if the replacing value is an 834 // instruction. 835 Instruction *I = dyn_cast<Instruction>(To); 836 if (!I) 837 return true; 838 // If both instructions are defined in the same basic block then replacement 839 // cannot break LCSSA form. 840 if (I->getParent() == From->getParent()) 841 return true; 842 // If the instruction is not defined in a loop then it can safely replace 843 // anything. 844 Loop *ToLoop = getLoopFor(I->getParent()); 845 if (!ToLoop) 846 return true; 847 // If the replacing instruction is defined in the same loop as the original 848 // instruction, or in a loop that contains it as an inner loop, then using 849 // it as a replacement will not break LCSSA form. 850 return ToLoop->contains(getLoopFor(From->getParent())); 851 } 852 853 /// Checks if moving a specific instruction can break LCSSA in any loop. 854 /// 855 /// Return true if moving \p Inst to before \p NewLoc will break LCSSA, 856 /// assuming that the function containing \p Inst and \p NewLoc is currently 857 /// in LCSSA form. movementPreservesLCSSAForm(Instruction * Inst,Instruction * NewLoc)858 bool movementPreservesLCSSAForm(Instruction *Inst, Instruction *NewLoc) { 859 assert(Inst->getFunction() == NewLoc->getFunction() && 860 "Can't reason about IPO!"); 861 862 auto *OldBB = Inst->getParent(); 863 auto *NewBB = NewLoc->getParent(); 864 865 // Movement within the same loop does not break LCSSA (the equality check is 866 // to avoid doing a hashtable lookup in case of intra-block movement). 867 if (OldBB == NewBB) 868 return true; 869 870 auto *OldLoop = getLoopFor(OldBB); 871 auto *NewLoop = getLoopFor(NewBB); 872 873 if (OldLoop == NewLoop) 874 return true; 875 876 // Check if Outer contains Inner; with the null loop counting as the 877 // "outermost" loop. 878 auto Contains = [](const Loop *Outer, const Loop *Inner) { 879 return !Outer || Outer->contains(Inner); 880 }; 881 882 // To check that the movement of Inst to before NewLoc does not break LCSSA, 883 // we need to check two sets of uses for possible LCSSA violations at 884 // NewLoc: the users of NewInst, and the operands of NewInst. 885 886 // If we know we're hoisting Inst out of an inner loop to an outer loop, 887 // then the uses *of* Inst don't need to be checked. 888 889 if (!Contains(NewLoop, OldLoop)) { 890 for (Use &U : Inst->uses()) { 891 auto *UI = cast<Instruction>(U.getUser()); 892 auto *UBB = isa<PHINode>(UI) ? cast<PHINode>(UI)->getIncomingBlock(U) 893 : UI->getParent(); 894 if (UBB != NewBB && getLoopFor(UBB) != NewLoop) 895 return false; 896 } 897 } 898 899 // If we know we're sinking Inst from an outer loop into an inner loop, then 900 // the *operands* of Inst don't need to be checked. 901 902 if (!Contains(OldLoop, NewLoop)) { 903 // See below on why we can't handle phi nodes here. 904 if (isa<PHINode>(Inst)) 905 return false; 906 907 for (Use &U : Inst->operands()) { 908 auto *DefI = dyn_cast<Instruction>(U.get()); 909 if (!DefI) 910 return false; 911 912 // This would need adjustment if we allow Inst to be a phi node -- the 913 // new use block won't simply be NewBB. 914 915 auto *DefBlock = DefI->getParent(); 916 if (DefBlock != NewBB && getLoopFor(DefBlock) != NewLoop) 917 return false; 918 } 919 } 920 921 return true; 922 } 923 }; 924 925 // Allow clients to walk the list of nested loops... 926 template <> struct GraphTraits<const Loop *> { 927 typedef const Loop *NodeRef; 928 typedef LoopInfo::iterator ChildIteratorType; 929 930 static NodeRef getEntryNode(const Loop *L) { return L; } 931 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); } 932 static ChildIteratorType child_end(NodeRef N) { return N->end(); } 933 }; 934 935 template <> struct GraphTraits<Loop *> { 936 typedef Loop *NodeRef; 937 typedef LoopInfo::iterator ChildIteratorType; 938 939 static NodeRef getEntryNode(Loop *L) { return L; } 940 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); } 941 static ChildIteratorType child_end(NodeRef N) { return N->end(); } 942 }; 943 944 /// Analysis pass that exposes the \c LoopInfo for a function. 945 class LoopAnalysis : public AnalysisInfoMixin<LoopAnalysis> { 946 friend AnalysisInfoMixin<LoopAnalysis>; 947 static AnalysisKey Key; 948 949 public: 950 typedef LoopInfo Result; 951 952 LoopInfo run(Function &F, FunctionAnalysisManager &AM); 953 }; 954 955 /// Printer pass for the \c LoopAnalysis results. 956 class LoopPrinterPass : public PassInfoMixin<LoopPrinterPass> { 957 raw_ostream &OS; 958 959 public: 960 explicit LoopPrinterPass(raw_ostream &OS) : OS(OS) {} 961 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); 962 }; 963 964 /// Verifier pass for the \c LoopAnalysis results. 965 struct LoopVerifierPass : public PassInfoMixin<LoopVerifierPass> { 966 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); 967 }; 968 969 /// The legacy pass manager's analysis pass to compute loop information. 970 class LoopInfoWrapperPass : public FunctionPass { 971 LoopInfo LI; 972 973 public: 974 static char ID; // Pass identification, replacement for typeid 975 976 LoopInfoWrapperPass() : FunctionPass(ID) { 977 initializeLoopInfoWrapperPassPass(*PassRegistry::getPassRegistry()); 978 } 979 980 LoopInfo &getLoopInfo() { return LI; } 981 const LoopInfo &getLoopInfo() const { return LI; } 982 983 /// Calculate the natural loop information for a given function. 984 bool runOnFunction(Function &F) override; 985 986 void verifyAnalysis() const override; 987 988 void releaseMemory() override { LI.releaseMemory(); } 989 990 void print(raw_ostream &O, const Module *M = nullptr) const override; 991 992 void getAnalysisUsage(AnalysisUsage &AU) const override; 993 }; 994 995 /// Function to print a loop's contents as LLVM's text IR assembly. 996 void printLoop(Loop &L, raw_ostream &OS, const std::string &Banner = ""); 997 998 /// Find and return the loop attribute node for the attribute @p Name in 999 /// @p LoopID. Return nullptr if there is no such attribute. 1000 MDNode *findOptionMDForLoopID(MDNode *LoopID, StringRef Name); 1001 1002 /// Find string metadata for a loop. 1003 /// 1004 /// Returns the MDNode where the first operand is the metadata's name. The 1005 /// following operands are the metadata's values. If no metadata with @p Name is 1006 /// found, return nullptr. 1007 MDNode *findOptionMDForLoop(const Loop *TheLoop, StringRef Name); 1008 1009 /// Return whether an MDNode might represent an access group. 1010 /// 1011 /// Access group metadata nodes have to be distinct and empty. Being 1012 /// always-empty ensures that it never needs to be changed (which -- because 1013 /// MDNodes are designed immutable -- would require creating a new MDNode). Note 1014 /// that this is not a sufficient condition: not every distinct and empty NDNode 1015 /// is representing an access group. 1016 bool isValidAsAccessGroup(MDNode *AccGroup); 1017 1018 } // End llvm namespace 1019 1020 #endif 1021