LLVM  4.0.0
LoopInfo.cpp
Go to the documentation of this file.
1 //===- LoopInfo.cpp - Natural Loop Calculator -----------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the LoopInfo class that is used to identify natural loops
11 // and determine the loop depth of various nodes of the CFG. Note that the
12 // loops identified may actually be several natural loops that share the same
13 // header node... not just a single natural loop.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "llvm/Analysis/LoopInfo.h"
19 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/IR/CFG.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DebugLoc.h"
26 #include "llvm/IR/Dominators.h"
27 #include "llvm/IR/Instructions.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/Metadata.h"
30 #include "llvm/IR/PassManager.h"
32 #include "llvm/Support/Debug.h"
34 #include <algorithm>
35 using namespace llvm;
36 
37 // Explicitly instantiate methods in LoopInfoImpl.h for IR-level Loops.
40 
41 // Always verify loopinfo if expensive checking is enabled.
42 #ifdef EXPENSIVE_CHECKS
43 static bool VerifyLoopInfo = true;
44 #else
45 static bool VerifyLoopInfo = false;
46 #endif
47 static cl::opt<bool,true>
48 VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo),
49  cl::desc("Verify loop info (time consuming)"));
50 
51 //===----------------------------------------------------------------------===//
52 // Loop implementation
53 //
54 
55 bool Loop::isLoopInvariant(const Value *V) const {
56  if (const Instruction *I = dyn_cast<Instruction>(V))
57  return !contains(I);
58  return true; // All non-instructions are loop invariant
59 }
60 
62  return all_of(I->operands(), [this](Value *V) { return isLoopInvariant(V); });
63 }
64 
65 bool Loop::makeLoopInvariant(Value *V, bool &Changed,
66  Instruction *InsertPt) const {
67  if (Instruction *I = dyn_cast<Instruction>(V))
68  return makeLoopInvariant(I, Changed, InsertPt);
69  return true; // All non-instructions are loop-invariant.
70 }
71 
72 bool Loop::makeLoopInvariant(Instruction *I, bool &Changed,
73  Instruction *InsertPt) const {
74  // Test if the value is already loop-invariant.
75  if (isLoopInvariant(I))
76  return true;
78  return false;
79  if (I->mayReadFromMemory())
80  return false;
81  // EH block instructions are immobile.
82  if (I->isEHPad())
83  return false;
84  // Determine the insertion point, unless one was given.
85  if (!InsertPt) {
86  BasicBlock *Preheader = getLoopPreheader();
87  // Without a preheader, hoisting is not feasible.
88  if (!Preheader)
89  return false;
90  InsertPt = Preheader->getTerminator();
91  }
92  // Don't hoist instructions with loop-variant operands.
93  for (Value *Operand : I->operands())
94  if (!makeLoopInvariant(Operand, Changed, InsertPt))
95  return false;
96 
97  // Hoist.
98  I->moveBefore(InsertPt);
99 
100  // There is possibility of hoisting this instruction above some arbitrary
101  // condition. Any metadata defined on it can be control dependent on this
102  // condition. Conservatively strip it here so that we don't give any wrong
103  // information to the optimizer.
105 
106  Changed = true;
107  return true;
108 }
109 
111  BasicBlock *H = getHeader();
112 
113  BasicBlock *Incoming = nullptr, *Backedge = nullptr;
114  pred_iterator PI = pred_begin(H);
115  assert(PI != pred_end(H) &&
116  "Loop must have at least one backedge!");
117  Backedge = *PI++;
118  if (PI == pred_end(H)) return nullptr; // dead loop
119  Incoming = *PI++;
120  if (PI != pred_end(H)) return nullptr; // multiple backedges?
121 
122  if (contains(Incoming)) {
123  if (contains(Backedge))
124  return nullptr;
125  std::swap(Incoming, Backedge);
126  } else if (!contains(Backedge))
127  return nullptr;
128 
129  // Loop over all of the PHI nodes, looking for a canonical indvar.
130  for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) {
131  PHINode *PN = cast<PHINode>(I);
132  if (ConstantInt *CI =
133  dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming)))
134  if (CI->isNullValue())
135  if (Instruction *Inc =
136  dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
137  if (Inc->getOpcode() == Instruction::Add &&
138  Inc->getOperand(0) == PN)
139  if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
140  if (CI->equalsInt(1))
141  return PN;
142  }
143  return nullptr;
144 }
145 
146 // Check that 'BB' doesn't have any uses outside of the 'L'
147 static bool isBlockInLCSSAForm(const Loop &L, const BasicBlock &BB,
148  DominatorTree &DT) {
149  for (const Instruction &I : BB) {
150  // Tokens can't be used in PHI nodes and live-out tokens prevent loop
151  // optimizations, so for the purposes of considered LCSSA form, we
152  // can ignore them.
153  if (I.getType()->isTokenTy())
154  continue;
155 
156  for (const Use &U : I.uses()) {
157  const Instruction *UI = cast<Instruction>(U.getUser());
158  const BasicBlock *UserBB = UI->getParent();
159  if (const PHINode *P = dyn_cast<PHINode>(UI))
160  UserBB = P->getIncomingBlock(U);
161 
162  // Check the current block, as a fast-path, before checking whether
163  // the use is anywhere in the loop. Most values are used in the same
164  // block they are defined in. Also, blocks not reachable from the
165  // entry are special; uses in them don't need to go through PHIs.
166  if (UserBB != &BB && !L.contains(UserBB) &&
167  DT.isReachableFromEntry(UserBB))
168  return false;
169  }
170  }
171  return true;
172 }
173 
175  // For each block we check that it doesn't have any uses outside of this loop.
176  return all_of(this->blocks(), [&](const BasicBlock *BB) {
177  return isBlockInLCSSAForm(*this, *BB, DT);
178  });
179 }
180 
182  // For each block we check that it doesn't have any uses outside of its
183  // innermost loop. This process will transitively guarantee that the current
184  // loop and all of the nested loops are in LCSSA form.
185  return all_of(this->blocks(), [&](const BasicBlock *BB) {
186  return isBlockInLCSSAForm(*LI.getLoopFor(BB), *BB, DT);
187  });
188 }
189 
191  // Normal-form loops have a preheader, a single backedge, and all of their
192  // exits have all their predecessors inside the loop.
194 }
195 
196 // Routines that reform the loop CFG and split edges often fail on indirectbr.
197 bool Loop::isSafeToClone() const {
198  // Return false if any loop blocks contain indirectbrs, or there are any calls
199  // to noduplicate functions.
200  for (BasicBlock *BB : this->blocks()) {
201  if (isa<IndirectBrInst>(BB->getTerminator()))
202  return false;
203 
204  for (Instruction &I : *BB)
205  if (auto CS = CallSite(&I))
206  if (CS.cannotDuplicate())
207  return false;
208  }
209  return true;
210 }
211 
213  MDNode *LoopID = nullptr;
214  if (isLoopSimplifyForm()) {
215  LoopID = getLoopLatch()->getTerminator()->getMetadata(LLVMContext::MD_loop);
216  } else {
217  // Go through each predecessor of the loop header and check the
218  // terminator for the metadata.
219  BasicBlock *H = getHeader();
220  for (BasicBlock *BB : this->blocks()) {
221  TerminatorInst *TI = BB->getTerminator();
222  MDNode *MD = nullptr;
223 
224  // Check if this terminator branches to the loop header.
225  for (BasicBlock *Successor : TI->successors()) {
226  if (Successor == H) {
228  break;
229  }
230  }
231  if (!MD)
232  return nullptr;
233 
234  if (!LoopID)
235  LoopID = MD;
236  else if (MD != LoopID)
237  return nullptr;
238  }
239  }
240  if (!LoopID || LoopID->getNumOperands() == 0 ||
241  LoopID->getOperand(0) != LoopID)
242  return nullptr;
243  return LoopID;
244 }
245 
246 void Loop::setLoopID(MDNode *LoopID) const {
247  assert(LoopID && "Loop ID should not be null");
248  assert(LoopID->getNumOperands() > 0 && "Loop ID needs at least one operand");
249  assert(LoopID->getOperand(0) == LoopID && "Loop ID should refer to itself");
250 
251  if (isLoopSimplifyForm()) {
252  getLoopLatch()->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID);
253  return;
254  }
255 
256  BasicBlock *H = getHeader();
257  for (BasicBlock *BB : this->blocks()) {
258  TerminatorInst *TI = BB->getTerminator();
259  for (BasicBlock *Successor : TI->successors()) {
260  if (Successor == H)
261  TI->setMetadata(LLVMContext::MD_loop, LoopID);
262  }
263  }
264 }
265 
267  MDNode *DesiredLoopIdMetadata = getLoopID();
268 
269  if (!DesiredLoopIdMetadata)
270  return false;
271 
272  // The loop branch contains the parallel loop metadata. In order to ensure
273  // that any parallel-loop-unaware optimization pass hasn't added loop-carried
274  // dependencies (thus converted the loop back to a sequential loop), check
275  // that all the memory instructions in the loop contain parallelism metadata
276  // that point to the same unique "loop id metadata" the loop branch does.
277  for (BasicBlock *BB : this->blocks()) {
278  for (Instruction &I : *BB) {
279  if (!I.mayReadOrWriteMemory())
280  continue;
281 
282  // The memory instruction can refer to the loop identifier metadata
283  // directly or indirectly through another list metadata (in case of
284  // nested parallel loops). The loop identifier metadata refers to
285  // itself so we can check both cases with the same routine.
286  MDNode *LoopIdMD =
288 
289  if (!LoopIdMD)
290  return false;
291 
292  bool LoopIdMDFound = false;
293  for (const MDOperand &MDOp : LoopIdMD->operands()) {
294  if (MDOp == DesiredLoopIdMetadata) {
295  LoopIdMDFound = true;
296  break;
297  }
298  }
299 
300  if (!LoopIdMDFound)
301  return false;
302  }
303  }
304  return true;
305 }
306 
308  return getLocRange().getStart();
309 }
310 
312  // If we have a debug location in the loop ID, then use it.
313  if (MDNode *LoopID = getLoopID()) {
314  DebugLoc Start;
315  // We use the first DebugLoc in the header as the start location of the loop
316  // and if there is a second DebugLoc in the header we use it as end location
317  // of the loop.
318  for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
319  if (DILocation *L = dyn_cast<DILocation>(LoopID->getOperand(i))) {
320  if (!Start)
321  Start = DebugLoc(L);
322  else
323  return LocRange(Start, DebugLoc(L));
324  }
325  }
326 
327  if (Start)
328  return LocRange(Start);
329  }
330 
331  // Try the pre-header first.
332  if (BasicBlock *PHeadBB = getLoopPreheader())
333  if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
334  return LocRange(DL);
335 
336  // If we have no pre-header or there are no instructions with debug
337  // info in it, try the header.
338  if (BasicBlock *HeadBB = getHeader())
339  return LocRange(HeadBB->getTerminator()->getDebugLoc());
340 
341  return LocRange();
342 }
343 
345  // Each predecessor of each exit block of a normal loop is contained
346  // within the loop.
347  SmallVector<BasicBlock *, 4> ExitBlocks;
348  getExitBlocks(ExitBlocks);
349  for (BasicBlock *BB : ExitBlocks)
350  for (BasicBlock *Predecessor : predecessors(BB))
351  if (!contains(Predecessor))
352  return false;
353  // All the requirements are met.
354  return true;
355 }
356 
357 void
360  "getUniqueExitBlocks assumes the loop has canonical form exits!");
361 
362  SmallVector<BasicBlock *, 32> SwitchExitBlocks;
363  for (BasicBlock *BB : this->blocks()) {
364  SwitchExitBlocks.clear();
365  for (BasicBlock *Successor : successors(BB)) {
366  // If block is inside the loop then it is not an exit block.
367  if (contains(Successor))
368  continue;
369 
371  BasicBlock *FirstPred = *PI;
372 
373  // If current basic block is this exit block's first predecessor
374  // then only insert exit block in to the output ExitBlocks vector.
375  // This ensures that same exit block is not inserted twice into
376  // ExitBlocks vector.
377  if (BB != FirstPred)
378  continue;
379 
380  // If a terminator has more then two successors, for example SwitchInst,
381  // then it is possible that there are multiple edges from current block
382  // to one exit block.
383  if (std::distance(succ_begin(BB), succ_end(BB)) <= 2) {
384  ExitBlocks.push_back(Successor);
385  continue;
386  }
387 
388  // In case of multiple edges from current block to exit block, collect
389  // only one edge in ExitBlocks. Use switchExitBlocks to keep track of
390  // duplicate edges.
391  if (!is_contained(SwitchExitBlocks, Successor)) {
392  SwitchExitBlocks.push_back(Successor);
393  ExitBlocks.push_back(Successor);
394  }
395  }
396  }
397 }
398 
400  SmallVector<BasicBlock *, 8> UniqueExitBlocks;
401  getUniqueExitBlocks(UniqueExitBlocks);
402  if (UniqueExitBlocks.size() == 1)
403  return UniqueExitBlocks[0];
404  return nullptr;
405 }
406 
407 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
409  print(dbgs());
410 }
411 
413  print(dbgs(), /*Depth=*/ 0, /*Verbose=*/ true);
414 }
415 #endif
416 
417 //===----------------------------------------------------------------------===//
418 // UnloopUpdater implementation
419 //
420 
421 namespace {
422 /// Find the new parent loop for all blocks within the "unloop" whose last
423 /// backedges has just been removed.
424 class UnloopUpdater {
425  Loop &Unloop;
426  LoopInfo *LI;
427 
429 
430  // Map unloop's immediate subloops to their nearest reachable parents. Nested
431  // loops within these subloops will not change parents. However, an immediate
432  // subloop's new parent will be the nearest loop reachable from either its own
433  // exits *or* any of its nested loop's exits.
434  DenseMap<Loop*, Loop*> SubloopParents;
435 
436  // Flag the presence of an irreducible backedge whose destination is a block
437  // directly contained by the original unloop.
438  bool FoundIB;
439 
440 public:
441  UnloopUpdater(Loop *UL, LoopInfo *LInfo) :
442  Unloop(*UL), LI(LInfo), DFS(UL), FoundIB(false) {}
443 
444  void updateBlockParents();
445 
446  void removeBlocksFromAncestors();
447 
448  void updateSubloopParents();
449 
450 protected:
451  Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop);
452 };
453 } // end anonymous namespace
454 
455 /// Update the parent loop for all blocks that are directly contained within the
456 /// original "unloop".
457 void UnloopUpdater::updateBlockParents() {
458  if (Unloop.getNumBlocks()) {
459  // Perform a post order CFG traversal of all blocks within this loop,
460  // propagating the nearest loop from sucessors to predecessors.
461  LoopBlocksTraversal Traversal(DFS, LI);
462  for (BasicBlock *POI : Traversal) {
463 
464  Loop *L = LI->getLoopFor(POI);
465  Loop *NL = getNearestLoop(POI, L);
466 
467  if (NL != L) {
468  // For reducible loops, NL is now an ancestor of Unloop.
469  assert((NL != &Unloop && (!NL || NL->contains(&Unloop))) &&
470  "uninitialized successor");
471  LI->changeLoopFor(POI, NL);
472  }
473  else {
474  // Or the current block is part of a subloop, in which case its parent
475  // is unchanged.
476  assert((FoundIB || Unloop.contains(L)) && "uninitialized successor");
477  }
478  }
479  }
480  // Each irreducible loop within the unloop induces a round of iteration using
481  // the DFS result cached by Traversal.
482  bool Changed = FoundIB;
483  for (unsigned NIters = 0; Changed; ++NIters) {
484  assert(NIters < Unloop.getNumBlocks() && "runaway iterative algorithm");
485 
486  // Iterate over the postorder list of blocks, propagating the nearest loop
487  // from successors to predecessors as before.
488  Changed = false;
489  for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(),
490  POE = DFS.endPostorder(); POI != POE; ++POI) {
491 
492  Loop *L = LI->getLoopFor(*POI);
493  Loop *NL = getNearestLoop(*POI, L);
494  if (NL != L) {
495  assert(NL != &Unloop && (!NL || NL->contains(&Unloop)) &&
496  "uninitialized successor");
497  LI->changeLoopFor(*POI, NL);
498  Changed = true;
499  }
500  }
501  }
502 }
503 
504 /// Remove unloop's blocks from all ancestors below their new parents.
505 void UnloopUpdater::removeBlocksFromAncestors() {
506  // Remove all unloop's blocks (including those in nested subloops) from
507  // ancestors below the new parent loop.
508  for (Loop::block_iterator BI = Unloop.block_begin(),
509  BE = Unloop.block_end(); BI != BE; ++BI) {
510  Loop *OuterParent = LI->getLoopFor(*BI);
511  if (Unloop.contains(OuterParent)) {
512  while (OuterParent->getParentLoop() != &Unloop)
513  OuterParent = OuterParent->getParentLoop();
514  OuterParent = SubloopParents[OuterParent];
515  }
516  // Remove blocks from former Ancestors except Unloop itself which will be
517  // deleted.
518  for (Loop *OldParent = Unloop.getParentLoop(); OldParent != OuterParent;
519  OldParent = OldParent->getParentLoop()) {
520  assert(OldParent && "new loop is not an ancestor of the original");
521  OldParent->removeBlockFromLoop(*BI);
522  }
523  }
524 }
525 
526 /// Update the parent loop for all subloops directly nested within unloop.
527 void UnloopUpdater::updateSubloopParents() {
528  while (!Unloop.empty()) {
529  Loop *Subloop = *std::prev(Unloop.end());
530  Unloop.removeChildLoop(std::prev(Unloop.end()));
531 
532  assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop");
533  if (Loop *Parent = SubloopParents[Subloop])
534  Parent->addChildLoop(Subloop);
535  else
536  LI->addTopLevelLoop(Subloop);
537  }
538 }
539 
540 /// Return the nearest parent loop among this block's successors. If a successor
541 /// is a subloop header, consider its parent to be the nearest parent of the
542 /// subloop's exits.
543 ///
544 /// For subloop blocks, simply update SubloopParents and return NULL.
545 Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) {
546 
547  // Initially for blocks directly contained by Unloop, NearLoop == Unloop and
548  // is considered uninitialized.
549  Loop *NearLoop = BBLoop;
550 
551  Loop *Subloop = nullptr;
552  if (NearLoop != &Unloop && Unloop.contains(NearLoop)) {
553  Subloop = NearLoop;
554  // Find the subloop ancestor that is directly contained within Unloop.
555  while (Subloop->getParentLoop() != &Unloop) {
556  Subloop = Subloop->getParentLoop();
557  assert(Subloop && "subloop is not an ancestor of the original loop");
558  }
559  // Get the current nearest parent of the Subloop exits, initially Unloop.
560  NearLoop = SubloopParents.insert({Subloop, &Unloop}).first->second;
561  }
562 
563  succ_iterator I = succ_begin(BB), E = succ_end(BB);
564  if (I == E) {
565  assert(!Subloop && "subloop blocks must have a successor");
566  NearLoop = nullptr; // unloop blocks may now exit the function.
567  }
568  for (; I != E; ++I) {
569  if (*I == BB)
570  continue; // self loops are uninteresting
571 
572  Loop *L = LI->getLoopFor(*I);
573  if (L == &Unloop) {
574  // This successor has not been processed. This path must lead to an
575  // irreducible backedge.
576  assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB");
577  FoundIB = true;
578  }
579  if (L != &Unloop && Unloop.contains(L)) {
580  // Successor is in a subloop.
581  if (Subloop)
582  continue; // Branching within subloops. Ignore it.
583 
584  // BB branches from the original into a subloop header.
585  assert(L->getParentLoop() == &Unloop && "cannot skip into nested loops");
586 
587  // Get the current nearest parent of the Subloop's exits.
588  L = SubloopParents[L];
589  // L could be Unloop if the only exit was an irreducible backedge.
590  }
591  if (L == &Unloop) {
592  continue;
593  }
594  // Handle critical edges from Unloop into a sibling loop.
595  if (L && !L->contains(&Unloop)) {
596  L = L->getParentLoop();
597  }
598  // Remember the nearest parent loop among successors or subloop exits.
599  if (NearLoop == &Unloop || !NearLoop || NearLoop->contains(L))
600  NearLoop = L;
601  }
602  if (Subloop) {
603  SubloopParents[Subloop] = NearLoop;
604  return BBLoop;
605  }
606  return NearLoop;
607 }
608 
610  analyze(DomTree);
611 }
612 
614  assert(!Unloop->isInvalid() && "Loop has already been removed");
615  Unloop->invalidate();
616  RemovedLoops.push_back(Unloop);
617 
618  // First handle the special case of no parent loop to simplify the algorithm.
619  if (!Unloop->getParentLoop()) {
620  // Since BBLoop had no parent, Unloop blocks are no longer in a loop.
621  for (Loop::block_iterator I = Unloop->block_begin(),
622  E = Unloop->block_end();
623  I != E; ++I) {
624 
625  // Don't reparent blocks in subloops.
626  if (getLoopFor(*I) != Unloop)
627  continue;
628 
629  // Blocks no longer have a parent but are still referenced by Unloop until
630  // the Unloop object is deleted.
631  changeLoopFor(*I, nullptr);
632  }
633 
634  // Remove the loop from the top-level LoopInfo object.
635  for (iterator I = begin();; ++I) {
636  assert(I != end() && "Couldn't find loop");
637  if (*I == Unloop) {
638  removeLoop(I);
639  break;
640  }
641  }
642 
643  // Move all of the subloops to the top-level.
644  while (!Unloop->empty())
645  addTopLevelLoop(Unloop->removeChildLoop(std::prev(Unloop->end())));
646 
647  return;
648  }
649 
650  // Update the parent loop for all blocks within the loop. Blocks within
651  // subloops will not change parents.
652  UnloopUpdater Updater(Unloop, this);
653  Updater.updateBlockParents();
654 
655  // Remove blocks from former ancestor loops.
656  Updater.removeBlocksFromAncestors();
657 
658  // Add direct subloops as children in their new parent loop.
659  Updater.updateSubloopParents();
660 
661  // Remove unloop from its parent loop.
662  Loop *ParentLoop = Unloop->getParentLoop();
663  for (Loop::iterator I = ParentLoop->begin();; ++I) {
664  assert(I != ParentLoop->end() && "Couldn't find loop");
665  if (*I == Unloop) {
666  ParentLoop->removeChildLoop(I);
667  break;
668  }
669  }
670 }
671 
672 AnalysisKey LoopAnalysis::Key;
673 
675  // FIXME: Currently we create a LoopInfo from scratch for every function.
676  // This may prove to be too wasteful due to deallocating and re-allocating
677  // memory each time for the underlying map and vector datastructures. At some
678  // point it may prove worthwhile to use a freelist and recycle LoopInfo
679  // objects. I don't want to add that kind of complexity until the scope of
680  // the problem is better understood.
681  LoopInfo LI;
683  return LI;
684 }
685 
688  AM.getResult<LoopAnalysis>(F).print(OS);
689  return PreservedAnalyses::all();
690 }
691 
692 void llvm::printLoop(Loop &L, raw_ostream &OS, const std::string &Banner) {
693  OS << Banner;
694  for (auto *Block : L.blocks())
695  if (Block)
696  Block->print(OS);
697  else
698  OS << "Printing <null> block";
699 }
700 
701 //===----------------------------------------------------------------------===//
702 // LoopInfo implementation
703 //
704 
705 char LoopInfoWrapperPass::ID = 0;
706 INITIALIZE_PASS_BEGIN(LoopInfoWrapperPass, "loops", "Natural Loop Information",
707  true, true)
710  true, true)
711 
712 bool LoopInfoWrapperPass::runOnFunction(Function &) {
713  releaseMemory();
714  LI.analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree());
715  return false;
716 }
717 
719  // LoopInfoWrapperPass is a FunctionPass, but verifying every loop in the
720  // function each time verifyAnalysis is called is very expensive. The
721  // -verify-loop-info option can enable this. In order to perform some
722  // checking by default, LoopPass has been taught to call verifyLoop manually
723  // during loop pass sequences.
724  if (VerifyLoopInfo) {
725  auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
726  LI.verify(DT);
727  }
728 }
729 
731  AU.setPreservesAll();
733 }
734 
736  LI.print(OS);
737 }
738 
741  LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
742  auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
743  LI.verify(DT);
744  return PreservedAnalyses::all();
745 }
746 
747 //===----------------------------------------------------------------------===//
748 // LoopBlocksDFS implementation
749 //
750 
751 /// Traverse the loop blocks and store the DFS result.
752 /// Useful for clients that just want the final DFS result and don't need to
753 /// visit blocks during the initial traversal.
755  LoopBlocksTraversal Traversal(*this, LI);
756  for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(),
757  POE = Traversal.end(); POI != POE; ++POI) ;
758 }
MachineLoop * L
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:679
LoopInfo run(Function &F, FunctionAnalysisManager &AM)
Definition: LoopInfo.cpp:674
BasicBlock * getUniqueExitBlock() const
If getUniqueExitBlocks would return exactly one block, return that block.
Definition: LoopInfo.cpp:399
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: LoopInfo.cpp:686
LoopT * removeLoop(iterator I)
This removes the specified top-level loop from this loop info object.
Definition: LoopInfo.h:598
size_t i
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:450
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
void dropUnknownNonDebugMetadata(ArrayRef< unsigned > KnownIDs)
Drop all unknown metadata except for debug locations.
Definition: Metadata.cpp:1156
bool isAnnotatedParallel() const
Returns true if the loop is annotated parallel.
Definition: LoopInfo.cpp:266
DebugLoc getStartLoc() const
Return the debug location of the start of this loop.
Definition: LoopInfo.cpp:307
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1040
This file contains the declarations for metadata subclasses.
void invalidate()
Invalidate the loop, indicating that it is no longer a loop.
Definition: LoopInfo.h:153
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:736
void changeLoopFor(BlockT *BB, LoopT *L)
Change the top-level loop that contains BB to the specified loop.
Definition: LoopInfo.h:609
LoopT * getParentLoop() const
Definition: LoopInfo.h:103
A debug info location.
Definition: DebugLoc.h:34
Metadata node.
Definition: Metadata.h:830
bool hasLoopInvariantOperands(const Instruction *I) const
Return true if all the operands of the specified instruction are loop invariant.
Definition: LoopInfo.cpp:61
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:189
void print(raw_ostream &OS) const
Definition: LoopInfoImpl.h:512
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Definition: LoopInfo.h:575
BlockT * getHeader() const
Definition: LoopInfo.h:102
LoopT * removeChildLoop(iterator I)
This removes the specified child from being a subloop of this loop.
Definition: LoopInfo.h:287
std::vector< LoopT * >::const_iterator iterator
iterator/begin/end - The interface to the top-level loops in the current function.
Definition: LoopInfo.h:564
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
Definition: LoopInfoImpl.h:157
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:228
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:53
Traverse the blocks in a loop using a depth-first search.
Definition: LoopIterator.h:182
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
POTIterator begin()
Postorder traversal over the graph.
Definition: LoopIterator.h:198
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:32
bool isLoopInvariant(const Value *V) const
Return true if the specified value is loop invariant.
Definition: LoopInfo.cpp:55
Analysis pass that exposes the LoopInfo for a function.
Definition: LoopInfo.h:806
void print(raw_ostream &O, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
Definition: LoopInfo.cpp:735
bool isLoopSimplifyForm() const
Return true if the Loop is in the form that the LoopSimplify form transforms loops to...
Definition: LoopInfo.cpp:190
void getExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
Return all of the successor blocks of this loop.
Definition: LoopInfoImpl.h:65
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:106
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: LoopInfo.cpp:730
#define F(x, y, z)
Definition: MD5.cpp:51
bool mayReadFromMemory() const
Return true if this instruction may read memory.
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:269
Function Alias Analysis false
static cl::opt< bool, true > VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo), cl::desc("Verify loop info (time consuming)"))
Debug location.
void perform(LoopInfo *LI)
Traverse the loop blocks and store the DFS result.
Definition: LoopInfo.cpp:754
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:96
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:109
iterator_range< block_iterator > blocks() const
Definition: LoopInfo.h:143
Natural Loop true
Definition: LoopInfo.cpp:709
succ_range successors()
Definition: InstrTypes.h:280
#define P(N)
iterator begin() const
Definition: LoopInfo.h:132
Subclasses of this class are all able to terminate a basic block.
Definition: InstrTypes.h:52
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:107
void dump() const
Definition: LoopInfo.cpp:408
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
Definition: LoopInfoImpl.h:109
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:653
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
static bool VerifyLoopInfo
Definition: LoopInfo.cpp:45
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const DebugLoc & getStart() const
Definition: LoopInfo.h:381
#define H(x, y, z)
Definition: MD5.cpp:53
void analyze(const DominatorTreeBase< BlockT > &DomTree)
Create the loop forest using a stable algorithm.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:116
bool isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const
Return true if this Loop and all inner subloops are in LCSSA form.
Definition: LoopInfo.cpp:181
iterator end() const
Definition: LoopInfo.h:133
MDNode * getLoopID() const
Return the llvm.loop loop id metadata node for this loop if it is present.
Definition: LoopInfo.cpp:212
Represent the analysis usage information of a pass.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:109
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
bool isSafeToClone() const
Return true if the loop body is safe to clone in practice.
Definition: LoopInfo.cpp:197
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:119
op_range operands()
Definition: User.h:213
void markAsRemoved(Loop *L)
Update LoopInfo after removing the last backedge from a loop.
Definition: LoopInfo.cpp:613
bool hasDedicatedExits() const
Return true if no exit block for the loop has a predecessor that is outside the loop.
Definition: LoopInfo.cpp:344
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:113
void getUniqueExitBlocks(SmallVectorImpl< BasicBlock * > &ExitBlocks) const
Return all unique successor blocks of this loop.
Definition: LoopInfo.cpp:358
A range representing the start and end location of a loop.
Definition: LoopInfo.h:371
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1183
static void DFS(BasicBlock *Root, SetVector< BasicBlock * > &Set)
void setLoopID(MDNode *LoopID) const
Set the llvm.loop loop id metadata for this loop.
Definition: LoopInfo.cpp:246
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1034
Iterator for intrusive lists based on ilist_node.
INITIALIZE_PASS_BEGIN(LoopInfoWrapperPass,"loops","Natural Loop Information", true, true) INITIALIZE_PASS_END(LoopInfoWrapperPass
This is the shared class of boolean and integer constants.
Definition: Constants.h:88
bool makeLoopInvariant(Value *V, bool &Changed, Instruction *InsertPt=nullptr) const
If the given value is an instruction inside of the loop and it can be hoisted, do so to make it trivi...
Definition: LoopInfo.cpp:65
bool isLCSSAForm(DominatorTree &DT) const
Return true if the Loop is in LCSSA form.
Definition: LoopInfo.cpp:174
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:175
PHINode * getCanonicalInductionVariable() const
Check to see if the loop has a canonical induction variable: an integer recurrence that starts at 0 a...
Definition: LoopInfo.cpp:110
Natural Loop Information
Definition: LoopInfo.cpp:709
pred_range predecessors(BasicBlock *BB)
Definition: IR/CFG.h:110
void addTopLevelLoop(LoopT *New)
This adds the specified loop to the collection of top-level loops.
Definition: LoopInfo.h:629
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:586
Store the result of a depth first search within basic blocks contained by a single loop...
Definition: LoopIterator.h:98
Value * getIncomingValueForBlock(const BasicBlock *BB) const
void setPreservesAll()
Set by analyses that do not transform their input at all.
std::vector< BlockT * >::const_iterator block_iterator
Definition: LoopInfo.h:140
block_iterator block_end() const
Definition: LoopInfo.h:142
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
Definition: Instruction.h:453
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:368
#define I(x, y, z)
Definition: MD5.cpp:54
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
std::vector< BasicBlock * >::const_iterator POIterator
Postorder list iterators.
Definition: LoopIterator.h:101
LocRange getLocRange() const
Return the source code span of the loop.
Definition: LoopInfo.cpp:311
op_range operands() const
Definition: Metadata.h:1032
void verify(const DominatorTreeBase< BlockT > &DomTree) const
Definition: LoopInfoImpl.h:541
void dumpVerbose() const
Definition: LoopInfo.cpp:412
bool isInvalid()
Return true if this loop is no longer valid.
Definition: LoopInfo.h:156
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM Value Representation.
Definition: Value.h:71
succ_range successors(BasicBlock *BB)
Definition: IR/CFG.h:143
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: LoopInfo.cpp:739
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
Definition: Instruction.cpp:95
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
bool empty() const
Definition: LoopInfo.h:136
block_iterator block_begin() const
Definition: LoopInfo.h:141
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:831
A container for analyses that lazily runs them and caches their results.
void print(raw_ostream &OS, unsigned Depth=0, bool Verbose=false) const
Print loop with all the BBs inside it.
Definition: LoopInfoImpl.h:316
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:217
std::vector< LoopT * >::const_iterator iterator
Definition: LoopInfo.h:129
This header defines various interfaces for pass management in LLVM.
void printLoop(Loop &L, raw_ostream &OS, const std::string &Banner="")
Function to print a loop's contents as LLVM's text IR assembly.
Definition: LoopInfo.cpp:692
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:64
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:411
const BasicBlock * getParent() const
Definition: Instruction.h:62
loops
Definition: LoopInfo.cpp:709
static bool isBlockInLCSSAForm(const Loop &L, const BasicBlock &BB, DominatorTree &DT)
Definition: LoopInfo.cpp:147
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition: LoopInfo.cpp:718
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:783