LLVM  10.0.0
LoopInfo.cpp
Go to the documentation of this file.
1 //===- LoopInfo.cpp - Natural Loop Calculator -----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the LoopInfo class that is used to identify natural loops
10 // and determine the loop depth of various nodes of the CFG. Note that the
11 // loops identified may actually be several natural loops that share the same
12 // header node... not just a single natural loop.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/ADT/ScopeExit.h"
19 #include "llvm/ADT/SmallPtrSet.h"
27 #include "llvm/Config/llvm-config.h"
28 #include "llvm/IR/CFG.h"
29 #include "llvm/IR/Constants.h"
30 #include "llvm/IR/DebugLoc.h"
31 #include "llvm/IR/Dominators.h"
33 #include "llvm/IR/Instructions.h"
34 #include "llvm/IR/LLVMContext.h"
35 #include "llvm/IR/Metadata.h"
36 #include "llvm/IR/PassManager.h"
37 #include "llvm/InitializePasses.h"
39 #include "llvm/Support/Debug.h"
41 #include <algorithm>
42 using namespace llvm;
43 
44 // Explicitly instantiate methods in LoopInfoImpl.h for IR-level Loops.
47 
48 // Always verify loopinfo if expensive checking is enabled.
49 #ifdef EXPENSIVE_CHECKS
50 bool llvm::VerifyLoopInfo = true;
51 #else
52 bool llvm::VerifyLoopInfo = false;
53 #endif
55  VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo),
56  cl::Hidden, cl::desc("Verify loop info (time consuming)"));
57 
58 //===----------------------------------------------------------------------===//
59 // Loop implementation
60 //
61 
62 bool Loop::isLoopInvariant(const Value *V) const {
63  if (const Instruction *I = dyn_cast<Instruction>(V))
64  return !contains(I);
65  return true; // All non-instructions are loop invariant
66 }
67 
69  return all_of(I->operands(), [this](Value *V) { return isLoopInvariant(V); });
70 }
71 
72 bool Loop::makeLoopInvariant(Value *V, bool &Changed, Instruction *InsertPt,
73  MemorySSAUpdater *MSSAU) const {
74  if (Instruction *I = dyn_cast<Instruction>(V))
75  return makeLoopInvariant(I, Changed, InsertPt, MSSAU);
76  return true; // All non-instructions are loop-invariant.
77 }
78 
79 bool Loop::makeLoopInvariant(Instruction *I, bool &Changed,
80  Instruction *InsertPt,
81  MemorySSAUpdater *MSSAU) const {
82  // Test if the value is already loop-invariant.
83  if (isLoopInvariant(I))
84  return true;
86  return false;
87  if (I->mayReadFromMemory())
88  return false;
89  // EH block instructions are immobile.
90  if (I->isEHPad())
91  return false;
92  // Determine the insertion point, unless one was given.
93  if (!InsertPt) {
94  BasicBlock *Preheader = getLoopPreheader();
95  // Without a preheader, hoisting is not feasible.
96  if (!Preheader)
97  return false;
98  InsertPt = Preheader->getTerminator();
99  }
100  // Don't hoist instructions with loop-variant operands.
101  for (Value *Operand : I->operands())
102  if (!makeLoopInvariant(Operand, Changed, InsertPt, MSSAU))
103  return false;
104 
105  // Hoist.
106  I->moveBefore(InsertPt);
107  if (MSSAU)
108  if (auto *MUD = MSSAU->getMemorySSA()->getMemoryAccess(I))
109  MSSAU->moveToPlace(MUD, InsertPt->getParent(),
111 
112  // There is possibility of hoisting this instruction above some arbitrary
113  // condition. Any metadata defined on it can be control dependent on this
114  // condition. Conservatively strip it here so that we don't give any wrong
115  // information to the optimizer.
117 
118  Changed = true;
119  return true;
120 }
121 
123  BasicBlock *&Backedge) const {
124  BasicBlock *H = getHeader();
125 
126  Incoming = nullptr;
127  Backedge = nullptr;
128  pred_iterator PI = pred_begin(H);
129  assert(PI != pred_end(H) && "Loop must have at least one backedge!");
130  Backedge = *PI++;
131  if (PI == pred_end(H))
132  return false; // dead loop
133  Incoming = *PI++;
134  if (PI != pred_end(H))
135  return false; // multiple backedges?
136 
137  if (contains(Incoming)) {
138  if (contains(Backedge))
139  return false;
140  std::swap(Incoming, Backedge);
141  } else if (!contains(Backedge))
142  return false;
143 
144  assert(Incoming && Backedge && "expected non-null incoming and backedges");
145  return true;
146 }
147 
149  BasicBlock *H = getHeader();
150 
151  BasicBlock *Incoming = nullptr, *Backedge = nullptr;
152  if (!getIncomingAndBackEdge(Incoming, Backedge))
153  return nullptr;
154 
155  // Loop over all of the PHI nodes, looking for a canonical indvar.
156  for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) {
157  PHINode *PN = cast<PHINode>(I);
158  if (ConstantInt *CI =
159  dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming)))
160  if (CI->isZero())
161  if (Instruction *Inc =
162  dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
163  if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN)
164  if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
165  if (CI->isOne())
166  return PN;
167  }
168  return nullptr;
169 }
170 
171 /// Get the latch condition instruction.
172 static ICmpInst *getLatchCmpInst(const Loop &L) {
173  if (BasicBlock *Latch = L.getLoopLatch())
174  if (BranchInst *BI = dyn_cast_or_null<BranchInst>(Latch->getTerminator()))
175  if (BI->isConditional())
176  return dyn_cast<ICmpInst>(BI->getCondition());
177 
178  return nullptr;
179 }
180 
181 /// Return the final value of the loop induction variable if found.
182 static Value *findFinalIVValue(const Loop &L, const PHINode &IndVar,
183  const Instruction &StepInst) {
184  ICmpInst *LatchCmpInst = getLatchCmpInst(L);
185  if (!LatchCmpInst)
186  return nullptr;
187 
188  Value *Op0 = LatchCmpInst->getOperand(0);
189  Value *Op1 = LatchCmpInst->getOperand(1);
190  if (Op0 == &IndVar || Op0 == &StepInst)
191  return Op1;
192 
193  if (Op1 == &IndVar || Op1 == &StepInst)
194  return Op0;
195 
196  return nullptr;
197 }
198 
200  PHINode &IndVar,
201  ScalarEvolution &SE) {
202  InductionDescriptor IndDesc;
203  if (!InductionDescriptor::isInductionPHI(&IndVar, &L, &SE, IndDesc))
204  return None;
205 
206  Value *InitialIVValue = IndDesc.getStartValue();
207  Instruction *StepInst = IndDesc.getInductionBinOp();
208  if (!InitialIVValue || !StepInst)
209  return None;
210 
211  const SCEV *Step = IndDesc.getStep();
212  Value *StepInstOp1 = StepInst->getOperand(1);
213  Value *StepInstOp0 = StepInst->getOperand(0);
214  Value *StepValue = nullptr;
215  if (SE.getSCEV(StepInstOp1) == Step)
216  StepValue = StepInstOp1;
217  else if (SE.getSCEV(StepInstOp0) == Step)
218  StepValue = StepInstOp0;
219 
220  Value *FinalIVValue = findFinalIVValue(L, IndVar, *StepInst);
221  if (!FinalIVValue)
222  return None;
223 
224  return LoopBounds(L, *InitialIVValue, *StepInst, StepValue, *FinalIVValue,
225  SE);
226 }
227 
229 
231  BasicBlock *Latch = L.getLoopLatch();
232  assert(Latch && "Expecting valid latch");
233 
234  BranchInst *BI = dyn_cast_or_null<BranchInst>(Latch->getTerminator());
235  assert(BI && BI->isConditional() && "Expecting conditional latch branch");
236 
237  ICmpInst *LatchCmpInst = dyn_cast<ICmpInst>(BI->getCondition());
238  assert(LatchCmpInst &&
239  "Expecting the latch compare instruction to be a CmpInst");
240 
241  // Need to inverse the predicate when first successor is not the loop
242  // header
243  ICmpInst::Predicate Pred = (BI->getSuccessor(0) == L.getHeader())
244  ? LatchCmpInst->getPredicate()
245  : LatchCmpInst->getInversePredicate();
246 
247  if (LatchCmpInst->getOperand(0) == &getFinalIVValue())
248  Pred = ICmpInst::getSwappedPredicate(Pred);
249 
250  // Need to flip strictness of the predicate when the latch compare instruction
251  // is not using StepInst
252  if (LatchCmpInst->getOperand(0) == &getStepInst() ||
253  LatchCmpInst->getOperand(1) == &getStepInst())
254  return Pred;
255 
256  // Cannot flip strictness of NE and EQ
257  if (Pred != ICmpInst::ICMP_NE && Pred != ICmpInst::ICMP_EQ)
259 
260  Direction D = getDirection();
261  if (D == Direction::Increasing)
262  return ICmpInst::ICMP_SLT;
263 
264  if (D == Direction::Decreasing)
265  return ICmpInst::ICMP_SGT;
266 
267  // If cannot determine the direction, then unable to find the canonical
268  // predicate
270 }
271 
273  if (const SCEVAddRecExpr *StepAddRecExpr =
274  dyn_cast<SCEVAddRecExpr>(SE.getSCEV(&getStepInst())))
275  if (const SCEV *StepRecur = StepAddRecExpr->getStepRecurrence(SE)) {
276  if (SE.isKnownPositive(StepRecur))
277  return Direction::Increasing;
278  if (SE.isKnownNegative(StepRecur))
279  return Direction::Decreasing;
280  }
281 
282  return Direction::Unknown;
283 }
284 
286  if (PHINode *IndVar = getInductionVariable(SE))
287  return LoopBounds::getBounds(*this, *IndVar, SE);
288 
289  return None;
290 }
291 
293  if (!isLoopSimplifyForm())
294  return nullptr;
295 
296  BasicBlock *Header = getHeader();
297  assert(Header && "Expected a valid loop header");
298  ICmpInst *CmpInst = getLatchCmpInst(*this);
299  if (!CmpInst)
300  return nullptr;
301 
302  Instruction *LatchCmpOp0 = dyn_cast<Instruction>(CmpInst->getOperand(0));
303  Instruction *LatchCmpOp1 = dyn_cast<Instruction>(CmpInst->getOperand(1));
304 
305  for (PHINode &IndVar : Header->phis()) {
306  InductionDescriptor IndDesc;
307  if (!InductionDescriptor::isInductionPHI(&IndVar, this, &SE, IndDesc))
308  continue;
309 
310  Instruction *StepInst = IndDesc.getInductionBinOp();
311 
312  // case 1:
313  // IndVar = phi[{InitialValue, preheader}, {StepInst, latch}]
314  // StepInst = IndVar + step
315  // cmp = StepInst < FinalValue
316  if (StepInst == LatchCmpOp0 || StepInst == LatchCmpOp1)
317  return &IndVar;
318 
319  // case 2:
320  // IndVar = phi[{InitialValue, preheader}, {StepInst, latch}]
321  // StepInst = IndVar + step
322  // cmp = IndVar < FinalValue
323  if (&IndVar == LatchCmpOp0 || &IndVar == LatchCmpOp1)
324  return &IndVar;
325  }
326 
327  return nullptr;
328 }
329 
331  InductionDescriptor &IndDesc) const {
332  if (PHINode *IndVar = getInductionVariable(SE))
333  return InductionDescriptor::isInductionPHI(IndVar, this, &SE, IndDesc);
334 
335  return false;
336 }
337 
339  ScalarEvolution &SE) const {
340  // Located in the loop header
341  BasicBlock *Header = getHeader();
342  if (AuxIndVar.getParent() != Header)
343  return false;
344 
345  // No uses outside of the loop
346  for (User *U : AuxIndVar.users())
347  if (const Instruction *I = dyn_cast<Instruction>(U))
348  if (!contains(I))
349  return false;
350 
351  InductionDescriptor IndDesc;
352  if (!InductionDescriptor::isInductionPHI(&AuxIndVar, this, &SE, IndDesc))
353  return false;
354 
355  // The step instruction opcode should be add or sub.
356  if (IndDesc.getInductionOpcode() != Instruction::Add &&
357  IndDesc.getInductionOpcode() != Instruction::Sub)
358  return false;
359 
360  // Incremented by a loop invariant step for each loop iteration
361  return SE.isLoopInvariant(IndDesc.getStep(), this);
362 }
363 
365  if (!isLoopSimplifyForm())
366  return nullptr;
367 
368  BasicBlock *Preheader = getLoopPreheader();
369  assert(Preheader && getLoopLatch() &&
370  "Expecting a loop with valid preheader and latch");
371 
372  // Loop should be in rotate form.
373  if (!isRotatedForm())
374  return nullptr;
375 
376  // Disallow loops with more than one unique exit block, as we do not verify
377  // that GuardOtherSucc post dominates all exit blocks.
378  BasicBlock *ExitFromLatch = getUniqueExitBlock();
379  if (!ExitFromLatch)
380  return nullptr;
381 
382  BasicBlock *ExitFromLatchSucc = ExitFromLatch->getUniqueSuccessor();
383  if (!ExitFromLatchSucc)
384  return nullptr;
385 
386  BasicBlock *GuardBB = Preheader->getUniquePredecessor();
387  if (!GuardBB)
388  return nullptr;
389 
390  assert(GuardBB->getTerminator() && "Expecting valid guard terminator");
391 
392  BranchInst *GuardBI = dyn_cast<BranchInst>(GuardBB->getTerminator());
393  if (!GuardBI || GuardBI->isUnconditional())
394  return nullptr;
395 
396  BasicBlock *GuardOtherSucc = (GuardBI->getSuccessor(0) == Preheader)
397  ? GuardBI->getSuccessor(1)
398  : GuardBI->getSuccessor(0);
399  return (GuardOtherSucc == ExitFromLatchSucc) ? GuardBI : nullptr;
400 }
401 
403  InductionDescriptor IndDesc;
404  if (!getInductionDescriptor(SE, IndDesc))
405  return false;
406 
407  ConstantInt *Init = dyn_cast_or_null<ConstantInt>(IndDesc.getStartValue());
408  if (!Init || !Init->isZero())
409  return false;
410 
411  if (IndDesc.getInductionOpcode() != Instruction::Add)
412  return false;
413 
414  ConstantInt *Step = IndDesc.getConstIntStepValue();
415  if (!Step || !Step->isOne())
416  return false;
417 
418  return true;
419 }
420 
421 // Check that 'BB' doesn't have any uses outside of the 'L'
422 static bool isBlockInLCSSAForm(const Loop &L, const BasicBlock &BB,
423  DominatorTree &DT) {
424  for (const Instruction &I : BB) {
425  // Tokens can't be used in PHI nodes and live-out tokens prevent loop
426  // optimizations, so for the purposes of considered LCSSA form, we
427  // can ignore them.
428  if (I.getType()->isTokenTy())
429  continue;
430 
431  for (const Use &U : I.uses()) {
432  const Instruction *UI = cast<Instruction>(U.getUser());
433  const BasicBlock *UserBB = UI->getParent();
434  if (const PHINode *P = dyn_cast<PHINode>(UI))
435  UserBB = P->getIncomingBlock(U);
436 
437  // Check the current block, as a fast-path, before checking whether
438  // the use is anywhere in the loop. Most values are used in the same
439  // block they are defined in. Also, blocks not reachable from the
440  // entry are special; uses in them don't need to go through PHIs.
441  if (UserBB != &BB && !L.contains(UserBB) &&
442  DT.isReachableFromEntry(UserBB))
443  return false;
444  }
445  }
446  return true;
447 }
448 
450  // For each block we check that it doesn't have any uses outside of this loop.
451  return all_of(this->blocks(), [&](const BasicBlock *BB) {
452  return isBlockInLCSSAForm(*this, *BB, DT);
453  });
454 }
455 
457  // For each block we check that it doesn't have any uses outside of its
458  // innermost loop. This process will transitively guarantee that the current
459  // loop and all of the nested loops are in LCSSA form.
460  return all_of(this->blocks(), [&](const BasicBlock *BB) {
461  return isBlockInLCSSAForm(*LI.getLoopFor(BB), *BB, DT);
462  });
463 }
464 
466  // Normal-form loops have a preheader, a single backedge, and all of their
467  // exits have all their predecessors inside the loop.
469 }
470 
471 // Routines that reform the loop CFG and split edges often fail on indirectbr.
472 bool Loop::isSafeToClone() const {
473  // Return false if any loop blocks contain indirectbrs, or there are any calls
474  // to noduplicate functions.
475  // FIXME: it should be ok to clone CallBrInst's if we correctly update the
476  // operand list to reflect the newly cloned labels.
477  for (BasicBlock *BB : this->blocks()) {
478  if (isa<IndirectBrInst>(BB->getTerminator()) ||
479  isa<CallBrInst>(BB->getTerminator()))
480  return false;
481 
482  for (Instruction &I : *BB)
483  if (auto CS = CallSite(&I))
484  if (CS.cannotDuplicate())
485  return false;
486  }
487  return true;
488 }
489 
491  MDNode *LoopID = nullptr;
492 
493  // Go through the latch blocks and check the terminator for the metadata.
494  SmallVector<BasicBlock *, 4> LatchesBlocks;
495  getLoopLatches(LatchesBlocks);
496  for (BasicBlock *BB : LatchesBlocks) {
497  Instruction *TI = BB->getTerminator();
498  MDNode *MD = TI->getMetadata(LLVMContext::MD_loop);
499 
500  if (!MD)
501  return nullptr;
502 
503  if (!LoopID)
504  LoopID = MD;
505  else if (MD != LoopID)
506  return nullptr;
507  }
508  if (!LoopID || LoopID->getNumOperands() == 0 ||
509  LoopID->getOperand(0) != LoopID)
510  return nullptr;
511  return LoopID;
512 }
513 
514 void Loop::setLoopID(MDNode *LoopID) const {
515  assert((!LoopID || LoopID->getNumOperands() > 0) &&
516  "Loop ID needs at least one operand");
517  assert((!LoopID || LoopID->getOperand(0) == LoopID) &&
518  "Loop ID should refer to itself");
519 
520  SmallVector<BasicBlock *, 4> LoopLatches;
521  getLoopLatches(LoopLatches);
522  for (BasicBlock *BB : LoopLatches)
523  BB->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID);
524 }
525 
528 
529  MDNode *DisableUnrollMD =
530  MDNode::get(Context, MDString::get(Context, "llvm.loop.unroll.disable"));
531  MDNode *LoopID = getLoopID();
533  Context, LoopID, {"llvm.loop.unroll."}, {DisableUnrollMD});
534  setLoopID(NewLoopID);
535 }
536 
538  MDNode *DesiredLoopIdMetadata = getLoopID();
539 
540  if (!DesiredLoopIdMetadata)
541  return false;
542 
543  MDNode *ParallelAccesses =
544  findOptionMDForLoop(this, "llvm.loop.parallel_accesses");
546  ParallelAccessGroups; // For scalable 'contains' check.
547  if (ParallelAccesses) {
548  for (const MDOperand &MD : drop_begin(ParallelAccesses->operands(), 1)) {
549  MDNode *AccGroup = cast<MDNode>(MD.get());
550  assert(isValidAsAccessGroup(AccGroup) &&
551  "List item must be an access group");
552  ParallelAccessGroups.insert(AccGroup);
553  }
554  }
555 
556  // The loop branch contains the parallel loop metadata. In order to ensure
557  // that any parallel-loop-unaware optimization pass hasn't added loop-carried
558  // dependencies (thus converted the loop back to a sequential loop), check
559  // that all the memory instructions in the loop belong to an access group that
560  // is parallel to this loop.
561  for (BasicBlock *BB : this->blocks()) {
562  for (Instruction &I : *BB) {
563  if (!I.mayReadOrWriteMemory())
564  continue;
565 
566  if (MDNode *AccessGroup = I.getMetadata(LLVMContext::MD_access_group)) {
567  auto ContainsAccessGroup = [&ParallelAccessGroups](MDNode *AG) -> bool {
568  if (AG->getNumOperands() == 0) {
569  assert(isValidAsAccessGroup(AG) && "Item must be an access group");
570  return ParallelAccessGroups.count(AG);
571  }
572 
573  for (const MDOperand &AccessListItem : AG->operands()) {
574  MDNode *AccGroup = cast<MDNode>(AccessListItem.get());
575  assert(isValidAsAccessGroup(AccGroup) &&
576  "List item must be an access group");
577  if (ParallelAccessGroups.count(AccGroup))
578  return true;
579  }
580  return false;
581  };
582 
583  if (ContainsAccessGroup(AccessGroup))
584  continue;
585  }
586 
587  // The memory instruction can refer to the loop identifier metadata
588  // directly or indirectly through another list metadata (in case of
589  // nested parallel loops). The loop identifier metadata refers to
590  // itself so we can check both cases with the same routine.
591  MDNode *LoopIdMD =
592  I.getMetadata(LLVMContext::MD_mem_parallel_loop_access);
593 
594  if (!LoopIdMD)
595  return false;
596 
597  bool LoopIdMDFound = false;
598  for (const MDOperand &MDOp : LoopIdMD->operands()) {
599  if (MDOp == DesiredLoopIdMetadata) {
600  LoopIdMDFound = true;
601  break;
602  }
603  }
604 
605  if (!LoopIdMDFound)
606  return false;
607  }
608  }
609  return true;
610 }
611 
613 
615  // If we have a debug location in the loop ID, then use it.
616  if (MDNode *LoopID = getLoopID()) {
617  DebugLoc Start;
618  // We use the first DebugLoc in the header as the start location of the loop
619  // and if there is a second DebugLoc in the header we use it as end location
620  // of the loop.
621  for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
622  if (DILocation *L = dyn_cast<DILocation>(LoopID->getOperand(i))) {
623  if (!Start)
624  Start = DebugLoc(L);
625  else
626  return LocRange(Start, DebugLoc(L));
627  }
628  }
629 
630  if (Start)
631  return LocRange(Start);
632  }
633 
634  // Try the pre-header first.
635  if (BasicBlock *PHeadBB = getLoopPreheader())
636  if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
637  return LocRange(DL);
638 
639  // If we have no pre-header or there are no instructions with debug
640  // info in it, try the header.
641  if (BasicBlock *HeadBB = getHeader())
642  return LocRange(HeadBB->getTerminator()->getDebugLoc());
643 
644  return LocRange();
645 }
646 
647 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
648 LLVM_DUMP_METHOD void Loop::dump() const { print(dbgs()); }
649 
651  print(dbgs(), /*Depth=*/0, /*Verbose=*/true);
652 }
653 #endif
654 
655 //===----------------------------------------------------------------------===//
656 // UnloopUpdater implementation
657 //
658 
659 namespace {
660 /// Find the new parent loop for all blocks within the "unloop" whose last
661 /// backedges has just been removed.
662 class UnloopUpdater {
663  Loop &Unloop;
664  LoopInfo *LI;
665 
667 
668  // Map unloop's immediate subloops to their nearest reachable parents. Nested
669  // loops within these subloops will not change parents. However, an immediate
670  // subloop's new parent will be the nearest loop reachable from either its own
671  // exits *or* any of its nested loop's exits.
672  DenseMap<Loop *, Loop *> SubloopParents;
673 
674  // Flag the presence of an irreducible backedge whose destination is a block
675  // directly contained by the original unloop.
676  bool FoundIB;
677 
678 public:
679  UnloopUpdater(Loop *UL, LoopInfo *LInfo)
680  : Unloop(*UL), LI(LInfo), DFS(UL), FoundIB(false) {}
681 
682  void updateBlockParents();
683 
684  void removeBlocksFromAncestors();
685 
686  void updateSubloopParents();
687 
688 protected:
689  Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop);
690 };
691 } // end anonymous namespace
692 
693 /// Update the parent loop for all blocks that are directly contained within the
694 /// original "unloop".
695 void UnloopUpdater::updateBlockParents() {
696  if (Unloop.getNumBlocks()) {
697  // Perform a post order CFG traversal of all blocks within this loop,
698  // propagating the nearest loop from successors to predecessors.
699  LoopBlocksTraversal Traversal(DFS, LI);
700  for (BasicBlock *POI : Traversal) {
701 
702  Loop *L = LI->getLoopFor(POI);
703  Loop *NL = getNearestLoop(POI, L);
704 
705  if (NL != L) {
706  // For reducible loops, NL is now an ancestor of Unloop.
707  assert((NL != &Unloop && (!NL || NL->contains(&Unloop))) &&
708  "uninitialized successor");
709  LI->changeLoopFor(POI, NL);
710  } else {
711  // Or the current block is part of a subloop, in which case its parent
712  // is unchanged.
713  assert((FoundIB || Unloop.contains(L)) && "uninitialized successor");
714  }
715  }
716  }
717  // Each irreducible loop within the unloop induces a round of iteration using
718  // the DFS result cached by Traversal.
719  bool Changed = FoundIB;
720  for (unsigned NIters = 0; Changed; ++NIters) {
721  assert(NIters < Unloop.getNumBlocks() && "runaway iterative algorithm");
722 
723  // Iterate over the postorder list of blocks, propagating the nearest loop
724  // from successors to predecessors as before.
725  Changed = false;
726  for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(),
727  POE = DFS.endPostorder();
728  POI != POE; ++POI) {
729 
730  Loop *L = LI->getLoopFor(*POI);
731  Loop *NL = getNearestLoop(*POI, L);
732  if (NL != L) {
733  assert(NL != &Unloop && (!NL || NL->contains(&Unloop)) &&
734  "uninitialized successor");
735  LI->changeLoopFor(*POI, NL);
736  Changed = true;
737  }
738  }
739  }
740 }
741 
742 /// Remove unloop's blocks from all ancestors below their new parents.
743 void UnloopUpdater::removeBlocksFromAncestors() {
744  // Remove all unloop's blocks (including those in nested subloops) from
745  // ancestors below the new parent loop.
746  for (Loop::block_iterator BI = Unloop.block_begin(), BE = Unloop.block_end();
747  BI != BE; ++BI) {
748  Loop *OuterParent = LI->getLoopFor(*BI);
749  if (Unloop.contains(OuterParent)) {
750  while (OuterParent->getParentLoop() != &Unloop)
751  OuterParent = OuterParent->getParentLoop();
752  OuterParent = SubloopParents[OuterParent];
753  }
754  // Remove blocks from former Ancestors except Unloop itself which will be
755  // deleted.
756  for (Loop *OldParent = Unloop.getParentLoop(); OldParent != OuterParent;
757  OldParent = OldParent->getParentLoop()) {
758  assert(OldParent && "new loop is not an ancestor of the original");
759  OldParent->removeBlockFromLoop(*BI);
760  }
761  }
762 }
763 
764 /// Update the parent loop for all subloops directly nested within unloop.
765 void UnloopUpdater::updateSubloopParents() {
766  while (!Unloop.empty()) {
767  Loop *Subloop = *std::prev(Unloop.end());
768  Unloop.removeChildLoop(std::prev(Unloop.end()));
769 
770  assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop");
771  if (Loop *Parent = SubloopParents[Subloop])
772  Parent->addChildLoop(Subloop);
773  else
774  LI->addTopLevelLoop(Subloop);
775  }
776 }
777 
778 /// Return the nearest parent loop among this block's successors. If a successor
779 /// is a subloop header, consider its parent to be the nearest parent of the
780 /// subloop's exits.
781 ///
782 /// For subloop blocks, simply update SubloopParents and return NULL.
783 Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) {
784 
785  // Initially for blocks directly contained by Unloop, NearLoop == Unloop and
786  // is considered uninitialized.
787  Loop *NearLoop = BBLoop;
788 
789  Loop *Subloop = nullptr;
790  if (NearLoop != &Unloop && Unloop.contains(NearLoop)) {
791  Subloop = NearLoop;
792  // Find the subloop ancestor that is directly contained within Unloop.
793  while (Subloop->getParentLoop() != &Unloop) {
794  Subloop = Subloop->getParentLoop();
795  assert(Subloop && "subloop is not an ancestor of the original loop");
796  }
797  // Get the current nearest parent of the Subloop exits, initially Unloop.
798  NearLoop = SubloopParents.insert({Subloop, &Unloop}).first->second;
799  }
800 
801  succ_iterator I = succ_begin(BB), E = succ_end(BB);
802  if (I == E) {
803  assert(!Subloop && "subloop blocks must have a successor");
804  NearLoop = nullptr; // unloop blocks may now exit the function.
805  }
806  for (; I != E; ++I) {
807  if (*I == BB)
808  continue; // self loops are uninteresting
809 
810  Loop *L = LI->getLoopFor(*I);
811  if (L == &Unloop) {
812  // This successor has not been processed. This path must lead to an
813  // irreducible backedge.
814  assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB");
815  FoundIB = true;
816  }
817  if (L != &Unloop && Unloop.contains(L)) {
818  // Successor is in a subloop.
819  if (Subloop)
820  continue; // Branching within subloops. Ignore it.
821 
822  // BB branches from the original into a subloop header.
823  assert(L->getParentLoop() == &Unloop && "cannot skip into nested loops");
824 
825  // Get the current nearest parent of the Subloop's exits.
826  L = SubloopParents[L];
827  // L could be Unloop if the only exit was an irreducible backedge.
828  }
829  if (L == &Unloop) {
830  continue;
831  }
832  // Handle critical edges from Unloop into a sibling loop.
833  if (L && !L->contains(&Unloop)) {
834  L = L->getParentLoop();
835  }
836  // Remember the nearest parent loop among successors or subloop exits.
837  if (NearLoop == &Unloop || !NearLoop || NearLoop->contains(L))
838  NearLoop = L;
839  }
840  if (Subloop) {
841  SubloopParents[Subloop] = NearLoop;
842  return BBLoop;
843  }
844  return NearLoop;
845 }
846 
847 LoopInfo::LoopInfo(const DomTreeBase<BasicBlock> &DomTree) { analyze(DomTree); }
848 
851  // Check whether the analysis, all analyses on functions, or the function's
852  // CFG have been preserved.
853  auto PAC = PA.getChecker<LoopAnalysis>();
854  return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
855  PAC.preservedSet<CFGAnalyses>());
856 }
857 
858 void LoopInfo::erase(Loop *Unloop) {
859  assert(!Unloop->isInvalid() && "Loop has already been erased!");
860 
861  auto InvalidateOnExit = make_scope_exit([&]() { destroy(Unloop); });
862 
863  // First handle the special case of no parent loop to simplify the algorithm.
864  if (!Unloop->getParentLoop()) {
865  // Since BBLoop had no parent, Unloop blocks are no longer in a loop.
866  for (Loop::block_iterator I = Unloop->block_begin(),
867  E = Unloop->block_end();
868  I != E; ++I) {
869 
870  // Don't reparent blocks in subloops.
871  if (getLoopFor(*I) != Unloop)
872  continue;
873 
874  // Blocks no longer have a parent but are still referenced by Unloop until
875  // the Unloop object is deleted.
876  changeLoopFor(*I, nullptr);
877  }
878 
879  // Remove the loop from the top-level LoopInfo object.
880  for (iterator I = begin();; ++I) {
881  assert(I != end() && "Couldn't find loop");
882  if (*I == Unloop) {
883  removeLoop(I);
884  break;
885  }
886  }
887 
888  // Move all of the subloops to the top-level.
889  while (!Unloop->empty())
890  addTopLevelLoop(Unloop->removeChildLoop(std::prev(Unloop->end())));
891 
892  return;
893  }
894 
895  // Update the parent loop for all blocks within the loop. Blocks within
896  // subloops will not change parents.
897  UnloopUpdater Updater(Unloop, this);
898  Updater.updateBlockParents();
899 
900  // Remove blocks from former ancestor loops.
901  Updater.removeBlocksFromAncestors();
902 
903  // Add direct subloops as children in their new parent loop.
904  Updater.updateSubloopParents();
905 
906  // Remove unloop from its parent loop.
907  Loop *ParentLoop = Unloop->getParentLoop();
908  for (Loop::iterator I = ParentLoop->begin();; ++I) {
909  assert(I != ParentLoop->end() && "Couldn't find loop");
910  if (*I == Unloop) {
911  ParentLoop->removeChildLoop(I);
912  break;
913  }
914  }
915 }
916 
917 AnalysisKey LoopAnalysis::Key;
918 
920  // FIXME: Currently we create a LoopInfo from scratch for every function.
921  // This may prove to be too wasteful due to deallocating and re-allocating
922  // memory each time for the underlying map and vector datastructures. At some
923  // point it may prove worthwhile to use a freelist and recycle LoopInfo
924  // objects. I don't want to add that kind of complexity until the scope of
925  // the problem is better understood.
926  LoopInfo LI;
928  return LI;
929 }
930 
933  AM.getResult<LoopAnalysis>(F).print(OS);
934  return PreservedAnalyses::all();
935 }
936 
937 void llvm::printLoop(Loop &L, raw_ostream &OS, const std::string &Banner) {
938 
939  if (forcePrintModuleIR()) {
940  // handling -print-module-scope
941  OS << Banner << " (loop: ";
942  L.getHeader()->printAsOperand(OS, false);
943  OS << ")\n";
944 
945  // printing whole module
946  OS << *L.getHeader()->getModule();
947  return;
948  }
949 
950  OS << Banner;
951 
952  auto *PreHeader = L.getLoopPreheader();
953  if (PreHeader) {
954  OS << "\n; Preheader:";
955  PreHeader->print(OS);
956  OS << "\n; Loop:";
957  }
958 
959  for (auto *Block : L.blocks())
960  if (Block)
961  Block->print(OS);
962  else
963  OS << "Printing <null> block";
964 
965  SmallVector<BasicBlock *, 8> ExitBlocks;
966  L.getExitBlocks(ExitBlocks);
967  if (!ExitBlocks.empty()) {
968  OS << "\n; Exit blocks";
969  for (auto *Block : ExitBlocks)
970  if (Block)
971  Block->print(OS);
972  else
973  OS << "Printing <null> block";
974  }
975 }
976 
978  // No loop metadata node, no loop properties.
979  if (!LoopID)
980  return nullptr;
981 
982  // First operand should refer to the metadata node itself, for legacy reasons.
983  assert(LoopID->getNumOperands() > 0 && "requires at least one operand");
984  assert(LoopID->getOperand(0) == LoopID && "invalid loop id");
985 
986  // Iterate over the metdata node operands and look for MDString metadata.
987  for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) {
988  MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
989  if (!MD || MD->getNumOperands() < 1)
990  continue;
991  MDString *S = dyn_cast<MDString>(MD->getOperand(0));
992  if (!S)
993  continue;
994  // Return the operand node if MDString holds expected metadata.
995  if (Name.equals(S->getString()))
996  return MD;
997  }
998 
999  // Loop property not found.
1000  return nullptr;
1001 }
1002 
1004  return findOptionMDForLoopID(TheLoop->getLoopID(), Name);
1005 }
1006 
1008  return Node->getNumOperands() == 0 && Node->isDistinct();
1009 }
1010 
1012  MDNode *OrigLoopID,
1013  ArrayRef<StringRef> RemovePrefixes,
1014  ArrayRef<MDNode *> AddAttrs) {
1015  // First remove any existing loop metadata related to this transformation.
1017 
1018  // Reserve first location for self reference to the LoopID metadata node.
1019  TempMDTuple TempNode = MDNode::getTemporary(Context, None);
1020  MDs.push_back(TempNode.get());
1021 
1022  // Remove metadata for the transformation that has been applied or that became
1023  // outdated.
1024  if (OrigLoopID) {
1025  for (unsigned i = 1, ie = OrigLoopID->getNumOperands(); i < ie; ++i) {
1026  bool IsVectorMetadata = false;
1027  Metadata *Op = OrigLoopID->getOperand(i);
1028  if (MDNode *MD = dyn_cast<MDNode>(Op)) {
1029  const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
1030  if (S)
1031  IsVectorMetadata =
1032  llvm::any_of(RemovePrefixes, [S](StringRef Prefix) -> bool {
1033  return S->getString().startswith(Prefix);
1034  });
1035  }
1036  if (!IsVectorMetadata)
1037  MDs.push_back(Op);
1038  }
1039  }
1040 
1041  // Add metadata to avoid reapplying a transformation, such as
1042  // llvm.loop.unroll.disable and llvm.loop.isvectorized.
1043  MDs.append(AddAttrs.begin(), AddAttrs.end());
1044 
1045  MDNode *NewLoopID = MDNode::getDistinct(Context, MDs);
1046  // Replace the temporary node with a self-reference.
1047  NewLoopID->replaceOperandWith(0, NewLoopID);
1048  return NewLoopID;
1049 }
1050 
1051 //===----------------------------------------------------------------------===//
1052 // LoopInfo implementation
1053 //
1054 
1057 }
1058 
1059 char LoopInfoWrapperPass::ID = 0;
1060 INITIALIZE_PASS_BEGIN(LoopInfoWrapperPass, "loops", "Natural Loop Information",
1061  true, true)
1064  true, true)
1065 
1066 bool LoopInfoWrapperPass::runOnFunction(Function &) {
1067  releaseMemory();
1068  LI.analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree());
1069  return false;
1070 }
1071 
1073  // LoopInfoWrapperPass is a FunctionPass, but verifying every loop in the
1074  // function each time verifyAnalysis is called is very expensive. The
1075  // -verify-loop-info option can enable this. In order to perform some
1076  // checking by default, LoopPass has been taught to call verifyLoop manually
1077  // during loop pass sequences.
1078  if (VerifyLoopInfo) {
1079  auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1080  LI.verify(DT);
1081  }
1082 }
1083 
1085  AU.setPreservesAll();
1087 }
1088 
1090  LI.print(OS);
1091 }
1092 
1095  LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
1096  auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
1097  LI.verify(DT);
1098  return PreservedAnalyses::all();
1099 }
1100 
1101 //===----------------------------------------------------------------------===//
1102 // LoopBlocksDFS implementation
1103 //
1104 
1105 /// Traverse the loop blocks and store the DFS result.
1106 /// Useful for clients that just want the final DFS result and don't need to
1107 /// visit blocks during the initial traversal.
1109  LoopBlocksTraversal Traversal(*this, LI);
1110  for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(),
1111  POE = Traversal.end();
1112  POI != POE; ++POI)
1113  ;
1114 }
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:719
LoopInfo run(Function &F, FunctionAnalysisManager &AM)
Definition: LoopInfo.cpp:919
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:722
bool forcePrintModuleIR()
forcePrintModuleIR - returns true if IR printing passes should
static Value * findFinalIVValue(const Loop &L, const PHINode &IndVar, const Instruction &StepInst)
Return the final value of the loop induction variable if found.
Definition: LoopInfo.cpp:182
auto drop_begin(T &&RangeOrContainer, size_t N) -> iterator_range< decltype(adl_begin(RangeOrContainer))>
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h:199
BinaryOperator * getInductionBinOp() const
bool isDistinct() const
Definition: Metadata.h:951
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
Definition: LoopInfoImpl.h:209
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: LoopInfo.cpp:931
LLVMContext & Context
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:777
This class represents lattice values for constants.
Definition: AllocatorList.h:23
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:492
static bool isInductionPHI(PHINode *Phi, const Loop *L, ScalarEvolution *SE, InductionDescriptor &D, const SCEV *Expr=nullptr, SmallVectorImpl< Instruction *> *CastsToIgnore=nullptr)
Returns true if Phi is an induction in the loop L.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
void dropUnknownNonDebugMetadata(ArrayRef< unsigned > KnownIDs)
Drop all unknown metadata except for debug locations.
Definition: Metadata.cpp:1197
iterator begin() const
Definition: ArrayRef.h:146
void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
Definition: Metadata.cpp:860
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:453
bool hasDedicatedExits() const
Return true if no exit block for the loop has a predecessor that is outside the loop.
Definition: LoopInfoImpl.h:85
bool isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const
Return true if this Loop and all inner subloops are in LCSSA form.
Definition: LoopInfo.cpp:456
ConstantInt * getConstIntStepValue() const
bool isLCSSAForm(DominatorTree &DT) const
Return true if the Loop is in LCSSA form.
Definition: LoopInfo.cpp:449
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:277
Instruction::BinaryOps getInductionOpcode() const
Returns binary opcode of the induction operator.
llvm::MDNode * makePostTransformationMetadata(llvm::LLVMContext &Context, MDNode *OrigLoopID, llvm::ArrayRef< llvm::StringRef > RemovePrefixes, llvm::ArrayRef< llvm::MDNode *> AddAttrs)
Create a new LoopID after the loop has been transformed.
Definition: LoopInfo.cpp:1011
LoopT * removeChildLoop(iterator I)
This removes the specified child from being a subloop of this loop.
Definition: LoopInfo.h:384
The main scalar evolution driver.
bool isRotatedForm() const
Return true if the loop is in rotated form.
Definition: LoopInfo.h:764
This file contains the declarations for metadata subclasses.
BasicBlock * getLoopPreheader() const
If there is a preheader for this loop, return it.
Definition: LoopInfoImpl.h:160
Direction getDirection() const
Get the direction of the loop.
Definition: LoopInfo.cpp:272
LLVM_NODISCARD detail::scope_exit< typename std::decay< Callable >::type > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:58
bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
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:1182
BasicBlock * getSuccessor(unsigned i) const
A debug info location.
Definition: DebugLoc.h:33
Metadata node.
Definition: Metadata.h:872
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:230
F(f)
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1077
Value * getCondition() const
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:144
Value * getStartValue() const
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:32
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:300
bool hasLoopInvariantOperands(const Instruction *I) const
Return true if all the operands of the specified instruction are loop invariant.
Definition: LoopInfo.cpp:68
bool isInvalid() const
Return true if this loop is no longer valid.
Definition: LoopInfo.h:198
void verify(const DominatorTreeBase< BlockT, false > &DomTree) const
Definition: LoopInfoImpl.h:681
void print(raw_ostream &OS, unsigned Depth=0, bool Verbose=false) const
Print loop with all the BBs inside it.
Definition: LoopInfoImpl.h:377
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:273
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition: PassManager.h:312
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
Definition: BasicBlock.cpp:140
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
Traverse the blocks in a loop using a depth-first search.
Definition: LoopIterator.h:200
MemorySSA * getMemorySSA() const
Get handle on MemorySSA.
static cl::opt< bool, true > VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo), cl::Hidden, cl::desc("Verify loop info (time consuming)"))
void initializeLoopInfoWrapperPassPass(PassRegistry &)
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Definition: LoopInfo.h:939
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
PHINode * getInductionVariable(ScalarEvolution &SE) const
Return the loop induction variable if found, else return nullptr.
Definition: LoopInfo.cpp:292
bool isAuxiliaryInductionVariable(PHINode &AuxIndVar, ScalarEvolution &SE) const
Return true if the given PHINode AuxIndVar is.
Definition: LoopInfo.cpp:338
POTIterator begin()
Postorder traversal over the graph.
Definition: LoopIterator.h:216
std::vector< Loop *>::const_iterator iterator
iterator/begin/end - The interface to the top-level loops in the current function.
Definition: LoopInfo.h:911
void getLoopLatches(SmallVectorImpl< BasicBlock * > &LoopLatches) const
Return all loop latch blocks of this loop.
Definition: LoopInfo.h:314
void erase(Loop *L)
Update LoopInfo after removing the last backedge from a loop.
Definition: LoopInfo.cpp:858
Direction
An enum for the direction of the loop.
Definition: LoopInfo.h:677
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1186
Analysis pass that exposes the LoopInfo for a function.
Definition: LoopInfo.h:1194
void print(raw_ostream &O, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
Definition: LoopInfo.cpp:1089
BranchInst * getLoopGuardBranch() const
Return the loop guard branch, if it exists.
Definition: LoopInfo.cpp:364
BasicBlock * getHeader() const
Definition: LoopInfo.h:105
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:102
void getExitBlocks(SmallVectorImpl< BlockT *> &ExitBlocks) const
Return all of the successor blocks of this loop.
Definition: LoopInfoImpl.h:62
std::vector< Loop *>::const_iterator iterator
Definition: LoopInfo.h:144
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
Definition: Constants.h:200
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: LoopInfo.cpp:1084
MemoryUseOrDef * getMemoryAccess(const Instruction *I) const
Given a memory Mod/Ref&#39;ing instruction, get the MemorySSA access associated with it.
Definition: MemorySSA.h:721
This node represents a polynomial recurrence on the trip count of the specified loop.
op_range operands() const
Definition: Metadata.h:1075
void setLoopID(MDNode *LoopID) const
Set the llvm.loop loop id metadata for this loop.
Definition: LoopInfo.cpp:514
const BasicBlock * getUniquePredecessor() const
Return the predecessor of this block if it has a unique predecessor block.
Definition: BasicBlock.cpp:253
MDNode * findOptionMDForLoop(const Loop *TheLoop, StringRef Name)
Find string metadata for a loop.
Definition: LoopInfo.cpp:1003
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:244
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
ICmpInst::Predicate getCanonicalPredicate() const
Return the canonical predicate for the latch compare instruction, if able to be calcuated.
Definition: LoopInfo.cpp:230
Debug location.
void perform(LoopInfo *LI)
Traverse the loop blocks and store the DFS result.
Definition: LoopInfo.cpp:1108
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
Definition: LoopInfo.h:1235
Value * getOperand(unsigned i) const
Definition: User.h:169
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:105
Natural Loop true
Definition: LoopInfo.cpp:1063
StringRef getString() const
Definition: Metadata.cpp:463
Core dominator tree base class.
Definition: LoopInfo.h:66
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1174
#define P(N)
Optional< LoopBounds > getBounds(ScalarEvolution &SE) const
Return the struct LoopBounds collected if all struct members are found, else None.
Definition: LoopInfo.cpp:285
INITIALIZE_PASS_BEGIN(LoopInfoWrapperPass, "loops", "Natural Loop Information", true, true) INITIALIZE_PASS_END(LoopInfoWrapperPass
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
void dump() const
Definition: LoopInfo.cpp:648
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:63
Conditional or Unconditional Branch instruction.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1182
const SCEV * getStep() const
Value * getIncomingValueForBlock(const BasicBlock *BB) const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define H(x, y, z)
Definition: MD5.cpp:57
void setLoopAlreadyUnrolled()
Add llvm.loop.unroll.disable to this loop&#39;s loop id metadata.
Definition: LoopInfo.cpp:526
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:370
void dumpVerbose() const
Definition: LoopInfo.cpp:650
void analyze(const DominatorTreeBase< BlockT, false > &DomTree)
Create the loop forest using a stable algorithm.
Definition: LoopInfoImpl.h:538
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:112
Represent the analysis usage information of a pass.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1189
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:732
constexpr double e
Definition: MathExtras.h:57
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:282
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:115
op_range operands()
Definition: User.h:237
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:381
BasicBlock * getUniqueExitBlock() const
If getUniqueExitBlocks would return exactly one block, return that block.
Definition: LoopInfoImpl.h:131
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
DebugLoc getStartLoc() const
Return the debug location of the start of this loop.
Definition: LoopInfo.cpp:612
A range representing the start and end location of a loop.
Definition: LoopInfo.h:512
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
Definition: AsmWriter.cpp:4393
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
void moveToPlace(MemoryUseOrDef *What, BasicBlock *BB, MemorySSA::InsertionPlace Where)
bool isLoopInvariant(const Value *V) const
Return true if the specified value is loop invariant.
Definition: LoopInfo.cpp:62
signed greater than
Definition: InstrTypes.h:759
unsigned first
bool contains(const Loop *L) const
Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:115
Iterator for intrusive lists based on ilist_node.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:443
void print(raw_ostream &OS) const
Definition: LoopInfoImpl.h:610
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
A struct for saving information about induction variables.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
iterator end() const
Definition: ArrayRef.h:147
MDNode * findOptionMDForLoopID(MDNode *LoopID, StringRef Name)
Find and return the loop attribute node for the attribute Name in LoopID.
Definition: LoopInfo.cpp:977
signed less than
Definition: InstrTypes.h:761
static Optional< Loop::LoopBounds > getBounds(const Loop &L, PHINode &IndVar, ScalarEvolution &SE)
Return the LoopBounds object if.
Definition: LoopInfo.cpp:199
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
bool isConditional() const
Natural Loop Information
Definition: LoopInfo.cpp:1063
bool VerifyLoopInfo
Enables verification of loop info.
Definition: LoopInfo.cpp:52
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition: InstrTypes.h:862
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:940
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:148
Store the result of a depth first search within basic blocks contained by a single loop...
Definition: LoopIterator.h:97
LocRange getLocRange() const
Return the source code span of the loop.
Definition: LoopInfo.cpp:614
void setPreservesAll()
Set by analyses that do not transform their input at all.
iterator_range< user_iterator > users()
Definition: Value.h:420
static void DFS(BasicBlock *Root, SetVector< BasicBlock *> &Set)
Represents analyses that only rely on functions&#39; control flow.
Definition: PassManager.h:115
bool getIncomingAndBackEdge(BasicBlock *&Incoming, BasicBlock *&Backedge) const
Obtain the unique incoming and back edge.
Definition: LoopInfo.cpp:122
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:387
LLVM_NODISCARD bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:181
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &)
Handle invalidation explicitly.
Definition: LoopInfo.cpp:849
LoopT * getParentLoop() const
Definition: LoopInfo.h:106
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
bool isLoopSimplifyForm() const
Return true if the Loop is in the form that the LoopSimplify form transforms loops to...
Definition: LoopInfo.cpp:465
MDNode * getLoopID() const
Return the llvm.loop loop id metadata node for this loop if it is present.
Definition: LoopInfo.cpp:490
const DebugLoc & getStart() const
Definition: LoopInfo.h:522
This class represents an analyzed expression in the program.
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:509
#define I(x, y, z)
Definition: MD5.cpp:58
Below are some utilities to get the loop guard, loop bounds and induction variable, and to check if a given phinode is an auxiliary induction variable, if the loop is guarded, and if the loop is canonical.
Definition: LoopInfo.h:614
bool mayReadFromMemory() const
Return true if this instruction may read memory.
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:192
std::vector< BasicBlock * >::const_iterator POIterator
Postorder list iterators.
Definition: LoopIterator.h:100
bool runOnFunction(Function &F) override
Calculate the natural loop information for a given function.
Definition: LoopInfo.cpp:1066
static ICmpInst * getLatchCmpInst(const Loop &L)
Get the latch condition instruction.
Definition: LoopInfo.cpp:172
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
block_iterator block_end() const
Definition: LoopInfo.h:160
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition: BasicBlock.h:329
bool isSafeToClone() const
Return true if the loop body is safe to clone in practice.
Definition: LoopInfo.cpp:472
AnalysisUsage & addRequiredTransitive()
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:649
This file defines passes to print out IR in various granularities.
bool empty() const
Definition: LoopInfo.h:151
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This templated class represents "all analyses that operate over <a particular IR unit>" (e...
Definition: PassManager.h:92
bool isCanonical(ScalarEvolution &SE) const
Return true if the loop induction variable starts at zero and increments by one each time through the...
Definition: LoopInfo.cpp:402
bool isAnnotatedParallel() const
Returns true if the loop is annotated parallel.
Definition: LoopInfo.cpp:537
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:74
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: LoopInfo.cpp:1093
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
Definition: Instruction.cpp:86
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
Definition: Instruction.h:593
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
The legacy pass manager&#39;s analysis pass to compute loop information.
Definition: LoopInfo.h:1219
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition: InstrTypes.h:847
A single uniqued string.
Definition: Metadata.h:604
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:259
This header defines various interfaces for pass management in LLVM.
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1083
bool getInductionDescriptor(ScalarEvolution &SE, InductionDescriptor &IndDesc) const
Get the loop induction descriptor for the loop induction variable.
Definition: LoopInfo.cpp:330
iterator_range< block_iterator > blocks() const
Definition: LoopInfo.h:161
void printLoop(Loop &L, raw_ostream &OS, const std::string &Banner="")
Function to print a loop&#39;s contents as LLVM&#39;s text IR assembly.
Definition: LoopInfo.cpp:937
block_iterator block_begin() const
Definition: LoopInfo.h:159
Root of the metadata hierarchy.
Definition: Metadata.h:58
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:71
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:451
bool isValidAsAccessGroup(MDNode *AccGroup)
Return whether an MDNode might represent an access group.
Definition: LoopInfo.cpp:1007
loops
Definition: LoopInfo.cpp:1063
const BasicBlock * getParent() const
Definition: Instruction.h:66
static bool isBlockInLCSSAForm(const Loop &L, const BasicBlock &BB, DominatorTree &DT)
Definition: LoopInfo.cpp:422
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition: LoopInfo.cpp:1072
const BasicBlock * getUniqueSuccessor() const
Return the successor of this block if it has a unique successor.
Definition: BasicBlock.cpp:283
bool makeLoopInvariant(Value *V, bool &Changed, Instruction *InsertPt=nullptr, MemorySSAUpdater *MSSAU=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:72