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"
17 #include "llvm/ADT/DepthFirstIterator.h"
18 #include "llvm/ADT/ScopeExit.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/Analysis/IVDescriptors.h"
21 #include "llvm/Analysis/LoopInfoImpl.h"
22 #include "llvm/Analysis/LoopIterator.h"
23 #include "llvm/Analysis/MemorySSA.h"
24 #include "llvm/Analysis/MemorySSAUpdater.h"
25 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
26 #include "llvm/Analysis/ValueTracking.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"
32 #include "llvm/IR/IRPrintingPasses.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/IR/PrintPasses.h"
38 #include "llvm/InitializePasses.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include <algorithm>
43 using namespace llvm;
44 
45 // Explicitly instantiate methods in LoopInfoImpl.h for IR-level Loops.
46 template class llvm::LoopBase<BasicBlock, Loop>;
47 template class llvm::LoopInfoBase<BasicBlock, Loop>;
48 
49 // Always verify loopinfo if expensive checking is enabled.
50 #ifdef EXPENSIVE_CHECKS
51 bool llvm::VerifyLoopInfo = true;
52 #else
53 bool llvm::VerifyLoopInfo = false;
54 #endif
55 static cl::opt<bool, true>
56     VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo),
57                     cl::Hidden, cl::desc("Verify loop info (time consuming)"));
58 
59 //===----------------------------------------------------------------------===//
60 // Loop implementation
61 //
62 
63 bool Loop::isLoopInvariant(const Value *V) const {
64   if (const Instruction *I = dyn_cast<Instruction>(V))
65     return !contains(I);
66   return true; // All non-instructions are loop invariant
67 }
68 
69 bool Loop::hasLoopInvariantOperands(const Instruction *I) const {
70   return all_of(I->operands(), [this](Value *V) { return isLoopInvariant(V); });
71 }
72 
73 bool Loop::makeLoopInvariant(Value *V, bool &Changed, Instruction *InsertPt,
74                              MemorySSAUpdater *MSSAU) const {
75   if (Instruction *I = dyn_cast<Instruction>(V))
76     return makeLoopInvariant(I, Changed, InsertPt, MSSAU);
77   return true; // All non-instructions are loop-invariant.
78 }
79 
80 bool Loop::makeLoopInvariant(Instruction *I, bool &Changed,
81                              Instruction *InsertPt,
82                              MemorySSAUpdater *MSSAU) const {
83   // Test if the value is already loop-invariant.
84   if (isLoopInvariant(I))
85     return true;
86   if (!isSafeToSpeculativelyExecute(I))
87     return false;
88   if (I->mayReadFromMemory())
89     return false;
90   // EH block instructions are immobile.
91   if (I->isEHPad())
92     return false;
93   // Determine the insertion point, unless one was given.
94   if (!InsertPt) {
95     BasicBlock *Preheader = getLoopPreheader();
96     // Without a preheader, hoisting is not feasible.
97     if (!Preheader)
98       return false;
99     InsertPt = Preheader->getTerminator();
100   }
101   // Don't hoist instructions with loop-variant operands.
102   for (Value *Operand : I->operands())
103     if (!makeLoopInvariant(Operand, Changed, InsertPt, MSSAU))
104       return false;
105 
106   // Hoist.
107   I->moveBefore(InsertPt);
108   if (MSSAU)
109     if (auto *MUD = MSSAU->getMemorySSA()->getMemoryAccess(I))
110       MSSAU->moveToPlace(MUD, InsertPt->getParent(),
111                          MemorySSA::BeforeTerminator);
112 
113   // There is possibility of hoisting this instruction above some arbitrary
114   // condition. Any metadata defined on it can be control dependent on this
115   // condition. Conservatively strip it here so that we don't give any wrong
116   // information to the optimizer.
117   I->dropUnknownNonDebugMetadata();
118 
119   Changed = true;
120   return true;
121 }
122 
123 bool Loop::getIncomingAndBackEdge(BasicBlock *&Incoming,
124                                   BasicBlock *&Backedge) const {
125   BasicBlock *H = getHeader();
126 
127   Incoming = nullptr;
128   Backedge = nullptr;
129   pred_iterator PI = pred_begin(H);
130   assert(PI != pred_end(H) && "Loop must have at least one backedge!");
131   Backedge = *PI++;
132   if (PI == pred_end(H))
133     return false; // dead loop
134   Incoming = *PI++;
135   if (PI != pred_end(H))
136     return false; // multiple backedges?
137 
138   if (contains(Incoming)) {
139     if (contains(Backedge))
140       return false;
141     std::swap(Incoming, Backedge);
142   } else if (!contains(Backedge))
143     return false;
144 
145   assert(Incoming && Backedge && "expected non-null incoming and backedges");
146   return true;
147 }
148 
149 PHINode *Loop::getCanonicalInductionVariable() const {
150   BasicBlock *H = getHeader();
151 
152   BasicBlock *Incoming = nullptr, *Backedge = nullptr;
153   if (!getIncomingAndBackEdge(Incoming, Backedge))
154     return nullptr;
155 
156   // Loop over all of the PHI nodes, looking for a canonical indvar.
157   for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) {
158     PHINode *PN = cast<PHINode>(I);
159     if (ConstantInt *CI =
160             dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming)))
161       if (CI->isZero())
162         if (Instruction *Inc =
163                 dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
164           if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN)
165             if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
166               if (CI->isOne())
167                 return PN;
168   }
169   return nullptr;
170 }
171 
172 /// Get the latch condition instruction.
173 static ICmpInst *getLatchCmpInst(const Loop &L) {
174   if (BasicBlock *Latch = L.getLoopLatch())
175     if (BranchInst *BI = dyn_cast_or_null<BranchInst>(Latch->getTerminator()))
176       if (BI->isConditional())
177         return dyn_cast<ICmpInst>(BI->getCondition());
178 
179   return nullptr;
180 }
181 
182 /// Return the final value of the loop induction variable if found.
183 static Value *findFinalIVValue(const Loop &L, const PHINode &IndVar,
184                                const Instruction &StepInst) {
185   ICmpInst *LatchCmpInst = getLatchCmpInst(L);
186   if (!LatchCmpInst)
187     return nullptr;
188 
189   Value *Op0 = LatchCmpInst->getOperand(0);
190   Value *Op1 = LatchCmpInst->getOperand(1);
191   if (Op0 == &IndVar || Op0 == &StepInst)
192     return Op1;
193 
194   if (Op1 == &IndVar || Op1 == &StepInst)
195     return Op0;
196 
197   return nullptr;
198 }
199 
200 Optional<Loop::LoopBounds> Loop::LoopBounds::getBounds(const Loop &L,
201                                                        PHINode &IndVar,
202                                                        ScalarEvolution &SE) {
203   InductionDescriptor IndDesc;
204   if (!InductionDescriptor::isInductionPHI(&IndVar, &L, &SE, IndDesc))
205     return None;
206 
207   Value *InitialIVValue = IndDesc.getStartValue();
208   Instruction *StepInst = IndDesc.getInductionBinOp();
209   if (!InitialIVValue || !StepInst)
210     return None;
211 
212   const SCEV *Step = IndDesc.getStep();
213   Value *StepInstOp1 = StepInst->getOperand(1);
214   Value *StepInstOp0 = StepInst->getOperand(0);
215   Value *StepValue = nullptr;
216   if (SE.getSCEV(StepInstOp1) == Step)
217     StepValue = StepInstOp1;
218   else if (SE.getSCEV(StepInstOp0) == Step)
219     StepValue = StepInstOp0;
220 
221   Value *FinalIVValue = findFinalIVValue(L, IndVar, *StepInst);
222   if (!FinalIVValue)
223     return None;
224 
225   return LoopBounds(L, *InitialIVValue, *StepInst, StepValue, *FinalIVValue,
226                     SE);
227 }
228 
229 using Direction = Loop::LoopBounds::Direction;
230 
231 ICmpInst::Predicate Loop::LoopBounds::getCanonicalPredicate() const {
232   BasicBlock *Latch = L.getLoopLatch();
233   assert(Latch && "Expecting valid latch");
234 
235   BranchInst *BI = dyn_cast_or_null<BranchInst>(Latch->getTerminator());
236   assert(BI && BI->isConditional() && "Expecting conditional latch branch");
237 
238   ICmpInst *LatchCmpInst = dyn_cast<ICmpInst>(BI->getCondition());
239   assert(LatchCmpInst &&
240          "Expecting the latch compare instruction to be a CmpInst");
241 
242   // Need to inverse the predicate when first successor is not the loop
243   // header
244   ICmpInst::Predicate Pred = (BI->getSuccessor(0) == L.getHeader())
245                                  ? LatchCmpInst->getPredicate()
246                                  : LatchCmpInst->getInversePredicate();
247 
248   if (LatchCmpInst->getOperand(0) == &getFinalIVValue())
249     Pred = ICmpInst::getSwappedPredicate(Pred);
250 
251   // Need to flip strictness of the predicate when the latch compare instruction
252   // is not using StepInst
253   if (LatchCmpInst->getOperand(0) == &getStepInst() ||
254       LatchCmpInst->getOperand(1) == &getStepInst())
255     return Pred;
256 
257   // Cannot flip strictness of NE and EQ
258   if (Pred != ICmpInst::ICMP_NE && Pred != ICmpInst::ICMP_EQ)
259     return ICmpInst::getFlippedStrictnessPredicate(Pred);
260 
261   Direction D = getDirection();
262   if (D == Direction::Increasing)
263     return ICmpInst::ICMP_SLT;
264 
265   if (D == Direction::Decreasing)
266     return ICmpInst::ICMP_SGT;
267 
268   // If cannot determine the direction, then unable to find the canonical
269   // predicate
270   return ICmpInst::BAD_ICMP_PREDICATE;
271 }
272 
273 Direction Loop::LoopBounds::getDirection() const {
274   if (const SCEVAddRecExpr *StepAddRecExpr =
275           dyn_cast<SCEVAddRecExpr>(SE.getSCEV(&getStepInst())))
276     if (const SCEV *StepRecur = StepAddRecExpr->getStepRecurrence(SE)) {
277       if (SE.isKnownPositive(StepRecur))
278         return Direction::Increasing;
279       if (SE.isKnownNegative(StepRecur))
280         return Direction::Decreasing;
281     }
282 
283   return Direction::Unknown;
284 }
285 
286 Optional<Loop::LoopBounds> Loop::getBounds(ScalarEvolution &SE) const {
287   if (PHINode *IndVar = getInductionVariable(SE))
288     return LoopBounds::getBounds(*this, *IndVar, SE);
289 
290   return None;
291 }
292 
293 PHINode *Loop::getInductionVariable(ScalarEvolution &SE) const {
294   if (!isLoopSimplifyForm())
295     return nullptr;
296 
297   BasicBlock *Header = getHeader();
298   assert(Header && "Expected a valid loop header");
299   ICmpInst *CmpInst = getLatchCmpInst(*this);
300   if (!CmpInst)
301     return nullptr;
302 
303   Instruction *LatchCmpOp0 = dyn_cast<Instruction>(CmpInst->getOperand(0));
304   Instruction *LatchCmpOp1 = dyn_cast<Instruction>(CmpInst->getOperand(1));
305 
306   for (PHINode &IndVar : Header->phis()) {
307     InductionDescriptor IndDesc;
308     if (!InductionDescriptor::isInductionPHI(&IndVar, this, &SE, IndDesc))
309       continue;
310 
311     Instruction *StepInst = IndDesc.getInductionBinOp();
312 
313     // case 1:
314     // IndVar = phi[{InitialValue, preheader}, {StepInst, latch}]
315     // StepInst = IndVar + step
316     // cmp = StepInst < FinalValue
317     if (StepInst == LatchCmpOp0 || StepInst == LatchCmpOp1)
318       return &IndVar;
319 
320     // case 2:
321     // IndVar = phi[{InitialValue, preheader}, {StepInst, latch}]
322     // StepInst = IndVar + step
323     // cmp = IndVar < FinalValue
324     if (&IndVar == LatchCmpOp0 || &IndVar == LatchCmpOp1)
325       return &IndVar;
326   }
327 
328   return nullptr;
329 }
330 
331 bool Loop::getInductionDescriptor(ScalarEvolution &SE,
332                                   InductionDescriptor &IndDesc) const {
333   if (PHINode *IndVar = getInductionVariable(SE))
334     return InductionDescriptor::isInductionPHI(IndVar, this, &SE, IndDesc);
335 
336   return false;
337 }
338 
339 bool Loop::isAuxiliaryInductionVariable(PHINode &AuxIndVar,
340                                         ScalarEvolution &SE) const {
341   // Located in the loop header
342   BasicBlock *Header = getHeader();
343   if (AuxIndVar.getParent() != Header)
344     return false;
345 
346   // No uses outside of the loop
347   for (User *U : AuxIndVar.users())
348     if (const Instruction *I = dyn_cast<Instruction>(U))
349       if (!contains(I))
350         return false;
351 
352   InductionDescriptor IndDesc;
353   if (!InductionDescriptor::isInductionPHI(&AuxIndVar, this, &SE, IndDesc))
354     return false;
355 
356   // The step instruction opcode should be add or sub.
357   if (IndDesc.getInductionOpcode() != Instruction::Add &&
358       IndDesc.getInductionOpcode() != Instruction::Sub)
359     return false;
360 
361   // Incremented by a loop invariant step for each loop iteration
362   return SE.isLoopInvariant(IndDesc.getStep(), this);
363 }
364 
365 BranchInst *Loop::getLoopGuardBranch() const {
366   if (!isLoopSimplifyForm())
367     return nullptr;
368 
369   BasicBlock *Preheader = getLoopPreheader();
370   assert(Preheader && getLoopLatch() &&
371          "Expecting a loop with valid preheader and latch");
372 
373   // Loop should be in rotate form.
374   if (!isRotatedForm())
375     return nullptr;
376 
377   // Disallow loops with more than one unique exit block, as we do not verify
378   // that GuardOtherSucc post dominates all exit blocks.
379   BasicBlock *ExitFromLatch = getUniqueExitBlock();
380   if (!ExitFromLatch)
381     return nullptr;
382 
383   BasicBlock *ExitFromLatchSucc = ExitFromLatch->getUniqueSuccessor();
384   if (!ExitFromLatchSucc)
385     return nullptr;
386 
387   BasicBlock *GuardBB = Preheader->getUniquePredecessor();
388   if (!GuardBB)
389     return nullptr;
390 
391   assert(GuardBB->getTerminator() && "Expecting valid guard terminator");
392 
393   BranchInst *GuardBI = dyn_cast<BranchInst>(GuardBB->getTerminator());
394   if (!GuardBI || GuardBI->isUnconditional())
395     return nullptr;
396 
397   BasicBlock *GuardOtherSucc = (GuardBI->getSuccessor(0) == Preheader)
398                                    ? GuardBI->getSuccessor(1)
399                                    : GuardBI->getSuccessor(0);
400   return (GuardOtherSucc == ExitFromLatchSucc) ? GuardBI : nullptr;
401 }
402 
403 bool Loop::isCanonical(ScalarEvolution &SE) const {
404   InductionDescriptor IndDesc;
405   if (!getInductionDescriptor(SE, IndDesc))
406     return false;
407 
408   ConstantInt *Init = dyn_cast_or_null<ConstantInt>(IndDesc.getStartValue());
409   if (!Init || !Init->isZero())
410     return false;
411 
412   if (IndDesc.getInductionOpcode() != Instruction::Add)
413     return false;
414 
415   ConstantInt *Step = IndDesc.getConstIntStepValue();
416   if (!Step || !Step->isOne())
417     return false;
418 
419   return true;
420 }
421 
422 // Check that 'BB' doesn't have any uses outside of the 'L'
423 static bool isBlockInLCSSAForm(const Loop &L, const BasicBlock &BB,
424                                const DominatorTree &DT) {
425   for (const Instruction &I : BB) {
426     // Tokens can't be used in PHI nodes and live-out tokens prevent loop
427     // optimizations, so for the purposes of considered LCSSA form, we
428     // can ignore them.
429     if (I.getType()->isTokenTy())
430       continue;
431 
432     for (const Use &U : I.uses()) {
433       const Instruction *UI = cast<Instruction>(U.getUser());
434       const BasicBlock *UserBB = UI->getParent();
435 
436       // For practical purposes, we consider that the use in a PHI
437       // occurs in the respective predecessor block. For more info,
438       // see the `phi` doc in LangRef and the LCSSA doc.
439       if (const PHINode *P = dyn_cast<PHINode>(UI))
440         UserBB = P->getIncomingBlock(U);
441 
442       // Check the current block, as a fast-path, before checking whether
443       // the use is anywhere in the loop.  Most values are used in the same
444       // block they are defined in.  Also, blocks not reachable from the
445       // entry are special; uses in them don't need to go through PHIs.
446       if (UserBB != &BB && !L.contains(UserBB) &&
447           DT.isReachableFromEntry(UserBB))
448         return false;
449     }
450   }
451   return true;
452 }
453 
454 bool Loop::isLCSSAForm(const DominatorTree &DT) const {
455   // For each block we check that it doesn't have any uses outside of this loop.
456   return all_of(this->blocks(), [&](const BasicBlock *BB) {
457     return isBlockInLCSSAForm(*this, *BB, DT);
458   });
459 }
460 
461 bool Loop::isRecursivelyLCSSAForm(const DominatorTree &DT,
462                                   const LoopInfo &LI) const {
463   // For each block we check that it doesn't have any uses outside of its
464   // innermost loop. This process will transitively guarantee that the current
465   // loop and all of the nested loops are in LCSSA form.
466   return all_of(this->blocks(), [&](const BasicBlock *BB) {
467     return isBlockInLCSSAForm(*LI.getLoopFor(BB), *BB, DT);
468   });
469 }
470 
471 bool Loop::isLoopSimplifyForm() const {
472   // Normal-form loops have a preheader, a single backedge, and all of their
473   // exits have all their predecessors inside the loop.
474   return getLoopPreheader() && getLoopLatch() && hasDedicatedExits();
475 }
476 
477 // Routines that reform the loop CFG and split edges often fail on indirectbr.
478 bool Loop::isSafeToClone() const {
479   // Return false if any loop blocks contain indirectbrs, or there are any calls
480   // to noduplicate functions.
481   // FIXME: it should be ok to clone CallBrInst's if we correctly update the
482   // operand list to reflect the newly cloned labels.
483   for (BasicBlock *BB : this->blocks()) {
484     if (isa<IndirectBrInst>(BB->getTerminator()) ||
485         isa<CallBrInst>(BB->getTerminator()))
486       return false;
487 
488     for (Instruction &I : *BB)
489       if (auto *CB = dyn_cast<CallBase>(&I))
490         if (CB->cannotDuplicate())
491           return false;
492   }
493   return true;
494 }
495 
496 MDNode *Loop::getLoopID() const {
497   MDNode *LoopID = nullptr;
498 
499   // Go through the latch blocks and check the terminator for the metadata.
500   SmallVector<BasicBlock *, 4> LatchesBlocks;
501   getLoopLatches(LatchesBlocks);
502   for (BasicBlock *BB : LatchesBlocks) {
503     Instruction *TI = BB->getTerminator();
504     MDNode *MD = TI->getMetadata(LLVMContext::MD_loop);
505 
506     if (!MD)
507       return nullptr;
508 
509     if (!LoopID)
510       LoopID = MD;
511     else if (MD != LoopID)
512       return nullptr;
513   }
514   if (!LoopID || LoopID->getNumOperands() == 0 ||
515       LoopID->getOperand(0) != LoopID)
516     return nullptr;
517   return LoopID;
518 }
519 
520 void Loop::setLoopID(MDNode *LoopID) const {
521   assert((!LoopID || LoopID->getNumOperands() > 0) &&
522          "Loop ID needs at least one operand");
523   assert((!LoopID || LoopID->getOperand(0) == LoopID) &&
524          "Loop ID should refer to itself");
525 
526   SmallVector<BasicBlock *, 4> LoopLatches;
527   getLoopLatches(LoopLatches);
528   for (BasicBlock *BB : LoopLatches)
529     BB->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID);
530 }
531 
532 void Loop::setLoopAlreadyUnrolled() {
533   LLVMContext &Context = getHeader()->getContext();
534 
535   MDNode *DisableUnrollMD =
536       MDNode::get(Context, MDString::get(Context, "llvm.loop.unroll.disable"));
537   MDNode *LoopID = getLoopID();
538   MDNode *NewLoopID = makePostTransformationMetadata(
539       Context, LoopID, {"llvm.loop.unroll."}, {DisableUnrollMD});
540   setLoopID(NewLoopID);
541 }
542 
543 void Loop::setLoopMustProgress() {
544   LLVMContext &Context = getHeader()->getContext();
545 
546   MDNode *MustProgress = findOptionMDForLoop(this, "llvm.loop.mustprogress");
547 
548   if (MustProgress)
549     return;
550 
551   MDNode *MustProgressMD =
552       MDNode::get(Context, MDString::get(Context, "llvm.loop.mustprogress"));
553   MDNode *LoopID = getLoopID();
554   MDNode *NewLoopID =
555       makePostTransformationMetadata(Context, LoopID, {}, {MustProgressMD});
556   setLoopID(NewLoopID);
557 }
558 
559 bool Loop::isAnnotatedParallel() const {
560   MDNode *DesiredLoopIdMetadata = getLoopID();
561 
562   if (!DesiredLoopIdMetadata)
563     return false;
564 
565   MDNode *ParallelAccesses =
566       findOptionMDForLoop(this, "llvm.loop.parallel_accesses");
567   SmallPtrSet<MDNode *, 4>
568       ParallelAccessGroups; // For scalable 'contains' check.
569   if (ParallelAccesses) {
570     for (const MDOperand &MD : drop_begin(ParallelAccesses->operands())) {
571       MDNode *AccGroup = cast<MDNode>(MD.get());
572       assert(isValidAsAccessGroup(AccGroup) &&
573              "List item must be an access group");
574       ParallelAccessGroups.insert(AccGroup);
575     }
576   }
577 
578   // The loop branch contains the parallel loop metadata. In order to ensure
579   // that any parallel-loop-unaware optimization pass hasn't added loop-carried
580   // dependencies (thus converted the loop back to a sequential loop), check
581   // that all the memory instructions in the loop belong to an access group that
582   // is parallel to this loop.
583   for (BasicBlock *BB : this->blocks()) {
584     for (Instruction &I : *BB) {
585       if (!I.mayReadOrWriteMemory())
586         continue;
587 
588       if (MDNode *AccessGroup = I.getMetadata(LLVMContext::MD_access_group)) {
589         auto ContainsAccessGroup = [&ParallelAccessGroups](MDNode *AG) -> bool {
590           if (AG->getNumOperands() == 0) {
591             assert(isValidAsAccessGroup(AG) && "Item must be an access group");
592             return ParallelAccessGroups.count(AG);
593           }
594 
595           for (const MDOperand &AccessListItem : AG->operands()) {
596             MDNode *AccGroup = cast<MDNode>(AccessListItem.get());
597             assert(isValidAsAccessGroup(AccGroup) &&
598                    "List item must be an access group");
599             if (ParallelAccessGroups.count(AccGroup))
600               return true;
601           }
602           return false;
603         };
604 
605         if (ContainsAccessGroup(AccessGroup))
606           continue;
607       }
608 
609       // The memory instruction can refer to the loop identifier metadata
610       // directly or indirectly through another list metadata (in case of
611       // nested parallel loops). The loop identifier metadata refers to
612       // itself so we can check both cases with the same routine.
613       MDNode *LoopIdMD =
614           I.getMetadata(LLVMContext::MD_mem_parallel_loop_access);
615 
616       if (!LoopIdMD)
617         return false;
618 
619       if (!llvm::is_contained(LoopIdMD->operands(), DesiredLoopIdMetadata))
620         return false;
621     }
622   }
623   return true;
624 }
625 
626 DebugLoc Loop::getStartLoc() const { return getLocRange().getStart(); }
627 
628 Loop::LocRange Loop::getLocRange() const {
629   // If we have a debug location in the loop ID, then use it.
630   if (MDNode *LoopID = getLoopID()) {
631     DebugLoc Start;
632     // We use the first DebugLoc in the header as the start location of the loop
633     // and if there is a second DebugLoc in the header we use it as end location
634     // of the loop.
635     for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
636       if (DILocation *L = dyn_cast<DILocation>(LoopID->getOperand(i))) {
637         if (!Start)
638           Start = DebugLoc(L);
639         else
640           return LocRange(Start, DebugLoc(L));
641       }
642     }
643 
644     if (Start)
645       return LocRange(Start);
646   }
647 
648   // Try the pre-header first.
649   if (BasicBlock *PHeadBB = getLoopPreheader())
650     if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
651       return LocRange(DL);
652 
653   // If we have no pre-header or there are no instructions with debug
654   // info in it, try the header.
655   if (BasicBlock *HeadBB = getHeader())
656     return LocRange(HeadBB->getTerminator()->getDebugLoc());
657 
658   return LocRange();
659 }
660 
661 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
662 LLVM_DUMP_METHOD void Loop::dump() const { print(dbgs()); }
663 
664 LLVM_DUMP_METHOD void Loop::dumpVerbose() const {
665   print(dbgs(), /*Depth=*/0, /*Verbose=*/true);
666 }
667 #endif
668 
669 //===----------------------------------------------------------------------===//
670 // UnloopUpdater implementation
671 //
672 
673 namespace {
674 /// Find the new parent loop for all blocks within the "unloop" whose last
675 /// backedges has just been removed.
676 class UnloopUpdater {
677   Loop &Unloop;
678   LoopInfo *LI;
679 
680   LoopBlocksDFS DFS;
681 
682   // Map unloop's immediate subloops to their nearest reachable parents. Nested
683   // loops within these subloops will not change parents. However, an immediate
684   // subloop's new parent will be the nearest loop reachable from either its own
685   // exits *or* any of its nested loop's exits.
686   DenseMap<Loop *, Loop *> SubloopParents;
687 
688   // Flag the presence of an irreducible backedge whose destination is a block
689   // directly contained by the original unloop.
690   bool FoundIB;
691 
692 public:
693   UnloopUpdater(Loop *UL, LoopInfo *LInfo)
694       : Unloop(*UL), LI(LInfo), DFS(UL), FoundIB(false) {}
695 
696   void updateBlockParents();
697 
698   void removeBlocksFromAncestors();
699 
700   void updateSubloopParents();
701 
702 protected:
703   Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop);
704 };
705 } // end anonymous namespace
706 
707 /// Update the parent loop for all blocks that are directly contained within the
708 /// original "unloop".
709 void UnloopUpdater::updateBlockParents() {
710   if (Unloop.getNumBlocks()) {
711     // Perform a post order CFG traversal of all blocks within this loop,
712     // propagating the nearest loop from successors to predecessors.
713     LoopBlocksTraversal Traversal(DFS, LI);
714     for (BasicBlock *POI : Traversal) {
715 
716       Loop *L = LI->getLoopFor(POI);
717       Loop *NL = getNearestLoop(POI, L);
718 
719       if (NL != L) {
720         // For reducible loops, NL is now an ancestor of Unloop.
721         assert((NL != &Unloop && (!NL || NL->contains(&Unloop))) &&
722                "uninitialized successor");
723         LI->changeLoopFor(POI, NL);
724       } else {
725         // Or the current block is part of a subloop, in which case its parent
726         // is unchanged.
727         assert((FoundIB || Unloop.contains(L)) && "uninitialized successor");
728       }
729     }
730   }
731   // Each irreducible loop within the unloop induces a round of iteration using
732   // the DFS result cached by Traversal.
733   bool Changed = FoundIB;
734   for (unsigned NIters = 0; Changed; ++NIters) {
735     assert(NIters < Unloop.getNumBlocks() && "runaway iterative algorithm");
736 
737     // Iterate over the postorder list of blocks, propagating the nearest loop
738     // from successors to predecessors as before.
739     Changed = false;
740     for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(),
741                                    POE = DFS.endPostorder();
742          POI != POE; ++POI) {
743 
744       Loop *L = LI->getLoopFor(*POI);
745       Loop *NL = getNearestLoop(*POI, L);
746       if (NL != L) {
747         assert(NL != &Unloop && (!NL || NL->contains(&Unloop)) &&
748                "uninitialized successor");
749         LI->changeLoopFor(*POI, NL);
750         Changed = true;
751       }
752     }
753   }
754 }
755 
756 /// Remove unloop's blocks from all ancestors below their new parents.
757 void UnloopUpdater::removeBlocksFromAncestors() {
758   // Remove all unloop's blocks (including those in nested subloops) from
759   // ancestors below the new parent loop.
760   for (BasicBlock *BB : Unloop.blocks()) {
761     Loop *OuterParent = LI->getLoopFor(BB);
762     if (Unloop.contains(OuterParent)) {
763       while (OuterParent->getParentLoop() != &Unloop)
764         OuterParent = OuterParent->getParentLoop();
765       OuterParent = SubloopParents[OuterParent];
766     }
767     // Remove blocks from former Ancestors except Unloop itself which will be
768     // deleted.
769     for (Loop *OldParent = Unloop.getParentLoop(); OldParent != OuterParent;
770          OldParent = OldParent->getParentLoop()) {
771       assert(OldParent && "new loop is not an ancestor of the original");
772       OldParent->removeBlockFromLoop(BB);
773     }
774   }
775 }
776 
777 /// Update the parent loop for all subloops directly nested within unloop.
778 void UnloopUpdater::updateSubloopParents() {
779   while (!Unloop.isInnermost()) {
780     Loop *Subloop = *std::prev(Unloop.end());
781     Unloop.removeChildLoop(std::prev(Unloop.end()));
782 
783     assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop");
784     if (Loop *Parent = SubloopParents[Subloop])
785       Parent->addChildLoop(Subloop);
786     else
787       LI->addTopLevelLoop(Subloop);
788   }
789 }
790 
791 /// Return the nearest parent loop among this block's successors. If a successor
792 /// is a subloop header, consider its parent to be the nearest parent of the
793 /// subloop's exits.
794 ///
795 /// For subloop blocks, simply update SubloopParents and return NULL.
796 Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) {
797 
798   // Initially for blocks directly contained by Unloop, NearLoop == Unloop and
799   // is considered uninitialized.
800   Loop *NearLoop = BBLoop;
801 
802   Loop *Subloop = nullptr;
803   if (NearLoop != &Unloop && Unloop.contains(NearLoop)) {
804     Subloop = NearLoop;
805     // Find the subloop ancestor that is directly contained within Unloop.
806     while (Subloop->getParentLoop() != &Unloop) {
807       Subloop = Subloop->getParentLoop();
808       assert(Subloop && "subloop is not an ancestor of the original loop");
809     }
810     // Get the current nearest parent of the Subloop exits, initially Unloop.
811     NearLoop = SubloopParents.insert({Subloop, &Unloop}).first->second;
812   }
813 
814   succ_iterator I = succ_begin(BB), E = succ_end(BB);
815   if (I == E) {
816     assert(!Subloop && "subloop blocks must have a successor");
817     NearLoop = nullptr; // unloop blocks may now exit the function.
818   }
819   for (; I != E; ++I) {
820     if (*I == BB)
821       continue; // self loops are uninteresting
822 
823     Loop *L = LI->getLoopFor(*I);
824     if (L == &Unloop) {
825       // This successor has not been processed. This path must lead to an
826       // irreducible backedge.
827       assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB");
828       FoundIB = true;
829     }
830     if (L != &Unloop && Unloop.contains(L)) {
831       // Successor is in a subloop.
832       if (Subloop)
833         continue; // Branching within subloops. Ignore it.
834 
835       // BB branches from the original into a subloop header.
836       assert(L->getParentLoop() == &Unloop && "cannot skip into nested loops");
837 
838       // Get the current nearest parent of the Subloop's exits.
839       L = SubloopParents[L];
840       // L could be Unloop if the only exit was an irreducible backedge.
841     }
842     if (L == &Unloop) {
843       continue;
844     }
845     // Handle critical edges from Unloop into a sibling loop.
846     if (L && !L->contains(&Unloop)) {
847       L = L->getParentLoop();
848     }
849     // Remember the nearest parent loop among successors or subloop exits.
850     if (NearLoop == &Unloop || !NearLoop || NearLoop->contains(L))
851       NearLoop = L;
852   }
853   if (Subloop) {
854     SubloopParents[Subloop] = NearLoop;
855     return BBLoop;
856   }
857   return NearLoop;
858 }
859 
860 LoopInfo::LoopInfo(const DomTreeBase<BasicBlock> &DomTree) { analyze(DomTree); }
861 
862 bool LoopInfo::invalidate(Function &F, const PreservedAnalyses &PA,
863                           FunctionAnalysisManager::Invalidator &) {
864   // Check whether the analysis, all analyses on functions, or the function's
865   // CFG have been preserved.
866   auto PAC = PA.getChecker<LoopAnalysis>();
867   return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
868            PAC.preservedSet<CFGAnalyses>());
869 }
870 
871 void LoopInfo::erase(Loop *Unloop) {
872   assert(!Unloop->isInvalid() && "Loop has already been erased!");
873 
874   auto InvalidateOnExit = make_scope_exit([&]() { destroy(Unloop); });
875 
876   // First handle the special case of no parent loop to simplify the algorithm.
877   if (Unloop->isOutermost()) {
878     // Since BBLoop had no parent, Unloop blocks are no longer in a loop.
879     for (BasicBlock *BB : Unloop->blocks()) {
880       // Don't reparent blocks in subloops.
881       if (getLoopFor(BB) != Unloop)
882         continue;
883 
884       // Blocks no longer have a parent but are still referenced by Unloop until
885       // the Unloop object is deleted.
886       changeLoopFor(BB, nullptr);
887     }
888 
889     // Remove the loop from the top-level LoopInfo object.
890     for (iterator I = begin();; ++I) {
891       assert(I != end() && "Couldn't find loop");
892       if (*I == Unloop) {
893         removeLoop(I);
894         break;
895       }
896     }
897 
898     // Move all of the subloops to the top-level.
899     while (!Unloop->isInnermost())
900       addTopLevelLoop(Unloop->removeChildLoop(std::prev(Unloop->end())));
901 
902     return;
903   }
904 
905   // Update the parent loop for all blocks within the loop. Blocks within
906   // subloops will not change parents.
907   UnloopUpdater Updater(Unloop, this);
908   Updater.updateBlockParents();
909 
910   // Remove blocks from former ancestor loops.
911   Updater.removeBlocksFromAncestors();
912 
913   // Add direct subloops as children in their new parent loop.
914   Updater.updateSubloopParents();
915 
916   // Remove unloop from its parent loop.
917   Loop *ParentLoop = Unloop->getParentLoop();
918   for (Loop::iterator I = ParentLoop->begin();; ++I) {
919     assert(I != ParentLoop->end() && "Couldn't find loop");
920     if (*I == Unloop) {
921       ParentLoop->removeChildLoop(I);
922       break;
923     }
924   }
925 }
926 
927 bool
928 LoopInfo::wouldBeOutOfLoopUseRequiringLCSSA(const Value *V,
929                                             const BasicBlock *ExitBB) const {
930   if (V->getType()->isTokenTy())
931     // We can't form PHIs of token type, so the definition of LCSSA excludes
932     // values of that type.
933     return false;
934 
935   const Instruction *I = dyn_cast<Instruction>(V);
936   if (!I)
937     return false;
938   const Loop *L = getLoopFor(I->getParent());
939   if (!L)
940     return false;
941   if (L->contains(ExitBB))
942     // Could be an exit bb of a subloop and contained in defining loop
943     return false;
944 
945   // We found a (new) out-of-loop use location, for a value defined in-loop.
946   // (Note that because of LCSSA, we don't have to account for values defined
947   // in sibling loops.  Such values will have LCSSA phis of their own in the
948   // common parent loop.)
949   return true;
950 }
951 
952 AnalysisKey LoopAnalysis::Key;
953 
954 LoopInfo LoopAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
955   // FIXME: Currently we create a LoopInfo from scratch for every function.
956   // This may prove to be too wasteful due to deallocating and re-allocating
957   // memory each time for the underlying map and vector datastructures. At some
958   // point it may prove worthwhile to use a freelist and recycle LoopInfo
959   // objects. I don't want to add that kind of complexity until the scope of
960   // the problem is better understood.
961   LoopInfo LI;
962   LI.analyze(AM.getResult<DominatorTreeAnalysis>(F));
963   return LI;
964 }
965 
966 PreservedAnalyses LoopPrinterPass::run(Function &F,
967                                        FunctionAnalysisManager &AM) {
968   AM.getResult<LoopAnalysis>(F).print(OS);
969   return PreservedAnalyses::all();
970 }
971 
972 void llvm::printLoop(Loop &L, raw_ostream &OS, const std::string &Banner) {
973 
974   if (forcePrintModuleIR()) {
975     // handling -print-module-scope
976     OS << Banner << " (loop: ";
977     L.getHeader()->printAsOperand(OS, false);
978     OS << ")\n";
979 
980     // printing whole module
981     OS << *L.getHeader()->getModule();
982     return;
983   }
984 
985   OS << Banner;
986 
987   auto *PreHeader = L.getLoopPreheader();
988   if (PreHeader) {
989     OS << "\n; Preheader:";
990     PreHeader->print(OS);
991     OS << "\n; Loop:";
992   }
993 
994   for (auto *Block : L.blocks())
995     if (Block)
996       Block->print(OS);
997     else
998       OS << "Printing <null> block";
999 
1000   SmallVector<BasicBlock *, 8> ExitBlocks;
1001   L.getExitBlocks(ExitBlocks);
1002   if (!ExitBlocks.empty()) {
1003     OS << "\n; Exit blocks";
1004     for (auto *Block : ExitBlocks)
1005       if (Block)
1006         Block->print(OS);
1007       else
1008         OS << "Printing <null> block";
1009   }
1010 }
1011 
1012 MDNode *llvm::findOptionMDForLoopID(MDNode *LoopID, StringRef Name) {
1013   // No loop metadata node, no loop properties.
1014   if (!LoopID)
1015     return nullptr;
1016 
1017   // First operand should refer to the metadata node itself, for legacy reasons.
1018   assert(LoopID->getNumOperands() > 0 && "requires at least one operand");
1019   assert(LoopID->getOperand(0) == LoopID && "invalid loop id");
1020 
1021   // Iterate over the metdata node operands and look for MDString metadata.
1022   for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) {
1023     MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
1024     if (!MD || MD->getNumOperands() < 1)
1025       continue;
1026     MDString *S = dyn_cast<MDString>(MD->getOperand(0));
1027     if (!S)
1028       continue;
1029     // Return the operand node if MDString holds expected metadata.
1030     if (Name.equals(S->getString()))
1031       return MD;
1032   }
1033 
1034   // Loop property not found.
1035   return nullptr;
1036 }
1037 
1038 MDNode *llvm::findOptionMDForLoop(const Loop *TheLoop, StringRef Name) {
1039   return findOptionMDForLoopID(TheLoop->getLoopID(), Name);
1040 }
1041 
1042 bool llvm::isValidAsAccessGroup(MDNode *Node) {
1043   return Node->getNumOperands() == 0 && Node->isDistinct();
1044 }
1045 
1046 MDNode *llvm::makePostTransformationMetadata(LLVMContext &Context,
1047                                              MDNode *OrigLoopID,
1048                                              ArrayRef<StringRef> RemovePrefixes,
1049                                              ArrayRef<MDNode *> AddAttrs) {
1050   // First remove any existing loop metadata related to this transformation.
1051   SmallVector<Metadata *, 4> MDs;
1052 
1053   // Reserve first location for self reference to the LoopID metadata node.
1054   MDs.push_back(nullptr);
1055 
1056   // Remove metadata for the transformation that has been applied or that became
1057   // outdated.
1058   if (OrigLoopID) {
1059     for (unsigned i = 1, ie = OrigLoopID->getNumOperands(); i < ie; ++i) {
1060       bool IsVectorMetadata = false;
1061       Metadata *Op = OrigLoopID->getOperand(i);
1062       if (MDNode *MD = dyn_cast<MDNode>(Op)) {
1063         const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
1064         if (S)
1065           IsVectorMetadata =
1066               llvm::any_of(RemovePrefixes, [S](StringRef Prefix) -> bool {
1067                 return S->getString().startswith(Prefix);
1068               });
1069       }
1070       if (!IsVectorMetadata)
1071         MDs.push_back(Op);
1072     }
1073   }
1074 
1075   // Add metadata to avoid reapplying a transformation, such as
1076   // llvm.loop.unroll.disable and llvm.loop.isvectorized.
1077   MDs.append(AddAttrs.begin(), AddAttrs.end());
1078 
1079   MDNode *NewLoopID = MDNode::getDistinct(Context, MDs);
1080   // Replace the temporary node with a self-reference.
1081   NewLoopID->replaceOperandWith(0, NewLoopID);
1082   return NewLoopID;
1083 }
1084 
1085 //===----------------------------------------------------------------------===//
1086 // LoopInfo implementation
1087 //
1088 
1089 LoopInfoWrapperPass::LoopInfoWrapperPass() : FunctionPass(ID) {
1090   initializeLoopInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1091 }
1092 
1093 char LoopInfoWrapperPass::ID = 0;
1094 INITIALIZE_PASS_BEGIN(LoopInfoWrapperPass, "loops", "Natural Loop Information",
1095                       true, true)
1096 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
1097 INITIALIZE_PASS_END(LoopInfoWrapperPass, "loops", "Natural Loop Information",
1098                     true, true)
1099 
1100 bool LoopInfoWrapperPass::runOnFunction(Function &) {
1101   releaseMemory();
1102   LI.analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree());
1103   return false;
1104 }
1105 
1106 void LoopInfoWrapperPass::verifyAnalysis() const {
1107   // LoopInfoWrapperPass is a FunctionPass, but verifying every loop in the
1108   // function each time verifyAnalysis is called is very expensive. The
1109   // -verify-loop-info option can enable this. In order to perform some
1110   // checking by default, LoopPass has been taught to call verifyLoop manually
1111   // during loop pass sequences.
1112   if (VerifyLoopInfo) {
1113     auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1114     LI.verify(DT);
1115   }
1116 }
1117 
1118 void LoopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1119   AU.setPreservesAll();
1120   AU.addRequiredTransitive<DominatorTreeWrapperPass>();
1121 }
1122 
1123 void LoopInfoWrapperPass::print(raw_ostream &OS, const Module *) const {
1124   LI.print(OS);
1125 }
1126 
1127 PreservedAnalyses LoopVerifierPass::run(Function &F,
1128                                         FunctionAnalysisManager &AM) {
1129   LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
1130   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
1131   LI.verify(DT);
1132   return PreservedAnalyses::all();
1133 }
1134 
1135 //===----------------------------------------------------------------------===//
1136 // LoopBlocksDFS implementation
1137 //
1138 
1139 /// Traverse the loop blocks and store the DFS result.
1140 /// Useful for clients that just want the final DFS result and don't need to
1141 /// visit blocks during the initial traversal.
1142 void LoopBlocksDFS::perform(LoopInfo *LI) {
1143   LoopBlocksTraversal Traversal(*this, LI);
1144   for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(),
1145                                         POE = Traversal.end();
1146        POI != POE; ++POI)
1147     ;
1148 }
1149