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/InitializePasses.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include <algorithm>
42 using namespace llvm;
43 
44 // Explicitly instantiate methods in LoopInfoImpl.h for IR-level Loops.
45 template class llvm::LoopBase<BasicBlock, Loop>;
46 template class llvm::LoopInfoBase<BasicBlock, Loop>;
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
54 static cl::opt<bool, true>
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 
68 bool Loop::hasLoopInvariantOperands(const Instruction *I) const {
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;
85   if (!isSafeToSpeculativelyExecute(I))
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(),
110                          MemorySSA::BeforeTerminator);
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.
116   I->dropUnknownNonDebugMetadata();
117 
118   Changed = true;
119   return true;
120 }
121 
122 bool Loop::getIncomingAndBackEdge(BasicBlock *&Incoming,
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 
148 PHINode *Loop::getCanonicalInductionVariable() const {
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 
199 Optional<Loop::LoopBounds> Loop::LoopBounds::getBounds(const Loop &L,
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 
228 using Direction = Loop::LoopBounds::Direction;
229 
230 ICmpInst::Predicate Loop::LoopBounds::getCanonicalPredicate() const {
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)
258     return ICmpInst::getFlippedStrictnessPredicate(Pred);
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
269   return ICmpInst::BAD_ICMP_PREDICATE;
270 }
271 
272 Direction Loop::LoopBounds::getDirection() const {
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 
285 Optional<Loop::LoopBounds> Loop::getBounds(ScalarEvolution &SE) const {
286   if (PHINode *IndVar = getInductionVariable(SE))
287     return LoopBounds::getBounds(*this, *IndVar, SE);
288 
289   return None;
290 }
291 
292 PHINode *Loop::getInductionVariable(ScalarEvolution &SE) const {
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 
330 bool Loop::getInductionDescriptor(ScalarEvolution &SE,
331                                   InductionDescriptor &IndDesc) const {
332   if (PHINode *IndVar = getInductionVariable(SE))
333     return InductionDescriptor::isInductionPHI(IndVar, this, &SE, IndDesc);
334 
335   return false;
336 }
337 
338 bool Loop::isAuxiliaryInductionVariable(PHINode &AuxIndVar,
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 
364 BranchInst *Loop::getLoopGuardBranch() const {
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 
402 bool Loop::isCanonical(ScalarEvolution &SE) const {
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                                const 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 
435       // For practical purposes, we consider that the use in a PHI
436       // occurs in the respective predecessor block. For more info,
437       // see the `phi` doc in LangRef and the LCSSA doc.
438       if (const PHINode *P = dyn_cast<PHINode>(UI))
439         UserBB = P->getIncomingBlock(U);
440 
441       // Check the current block, as a fast-path, before checking whether
442       // the use is anywhere in the loop.  Most values are used in the same
443       // block they are defined in.  Also, blocks not reachable from the
444       // entry are special; uses in them don't need to go through PHIs.
445       if (UserBB != &BB && !L.contains(UserBB) &&
446           DT.isReachableFromEntry(UserBB))
447         return false;
448     }
449   }
450   return true;
451 }
452 
453 bool Loop::isLCSSAForm(const DominatorTree &DT) const {
454   // For each block we check that it doesn't have any uses outside of this loop.
455   return all_of(this->blocks(), [&](const BasicBlock *BB) {
456     return isBlockInLCSSAForm(*this, *BB, DT);
457   });
458 }
459 
460 bool Loop::isRecursivelyLCSSAForm(const DominatorTree &DT,
461                                   const LoopInfo &LI) const {
462   // For each block we check that it doesn't have any uses outside of its
463   // innermost loop. This process will transitively guarantee that the current
464   // loop and all of the nested loops are in LCSSA form.
465   return all_of(this->blocks(), [&](const BasicBlock *BB) {
466     return isBlockInLCSSAForm(*LI.getLoopFor(BB), *BB, DT);
467   });
468 }
469 
470 bool Loop::isLoopSimplifyForm() const {
471   // Normal-form loops have a preheader, a single backedge, and all of their
472   // exits have all their predecessors inside the loop.
473   return getLoopPreheader() && getLoopLatch() && hasDedicatedExits();
474 }
475 
476 // Routines that reform the loop CFG and split edges often fail on indirectbr.
477 bool Loop::isSafeToClone() const {
478   // Return false if any loop blocks contain indirectbrs, or there are any calls
479   // to noduplicate functions.
480   // FIXME: it should be ok to clone CallBrInst's if we correctly update the
481   // operand list to reflect the newly cloned labels.
482   for (BasicBlock *BB : this->blocks()) {
483     if (isa<IndirectBrInst>(BB->getTerminator()) ||
484         isa<CallBrInst>(BB->getTerminator()))
485       return false;
486 
487     for (Instruction &I : *BB)
488       if (auto *CB = dyn_cast<CallBase>(&I))
489         if (CB->cannotDuplicate())
490           return false;
491   }
492   return true;
493 }
494 
495 MDNode *Loop::getLoopID() const {
496   MDNode *LoopID = nullptr;
497 
498   // Go through the latch blocks and check the terminator for the metadata.
499   SmallVector<BasicBlock *, 4> LatchesBlocks;
500   getLoopLatches(LatchesBlocks);
501   for (BasicBlock *BB : LatchesBlocks) {
502     Instruction *TI = BB->getTerminator();
503     MDNode *MD = TI->getMetadata(LLVMContext::MD_loop);
504 
505     if (!MD)
506       return nullptr;
507 
508     if (!LoopID)
509       LoopID = MD;
510     else if (MD != LoopID)
511       return nullptr;
512   }
513   if (!LoopID || LoopID->getNumOperands() == 0 ||
514       LoopID->getOperand(0) != LoopID)
515     return nullptr;
516   return LoopID;
517 }
518 
519 void Loop::setLoopID(MDNode *LoopID) const {
520   assert((!LoopID || LoopID->getNumOperands() > 0) &&
521          "Loop ID needs at least one operand");
522   assert((!LoopID || LoopID->getOperand(0) == LoopID) &&
523          "Loop ID should refer to itself");
524 
525   SmallVector<BasicBlock *, 4> LoopLatches;
526   getLoopLatches(LoopLatches);
527   for (BasicBlock *BB : LoopLatches)
528     BB->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID);
529 }
530 
531 void Loop::setLoopAlreadyUnrolled() {
532   LLVMContext &Context = getHeader()->getContext();
533 
534   MDNode *DisableUnrollMD =
535       MDNode::get(Context, MDString::get(Context, "llvm.loop.unroll.disable"));
536   MDNode *LoopID = getLoopID();
537   MDNode *NewLoopID = makePostTransformationMetadata(
538       Context, LoopID, {"llvm.loop.unroll."}, {DisableUnrollMD});
539   setLoopID(NewLoopID);
540 }
541 
542 void Loop::setLoopMustProgress() {
543   LLVMContext &Context = getHeader()->getContext();
544 
545   MDNode *MustProgress = findOptionMDForLoop(this, "llvm.loop.mustprogress");
546 
547   if (MustProgress)
548     return;
549 
550   MDNode *MustProgressMD =
551       MDNode::get(Context, MDString::get(Context, "llvm.loop.mustprogress"));
552   MDNode *LoopID = getLoopID();
553   MDNode *NewLoopID =
554       makePostTransformationMetadata(Context, LoopID, {}, {MustProgressMD});
555   setLoopID(NewLoopID);
556 }
557 
558 bool Loop::isAnnotatedParallel() const {
559   MDNode *DesiredLoopIdMetadata = getLoopID();
560 
561   if (!DesiredLoopIdMetadata)
562     return false;
563 
564   MDNode *ParallelAccesses =
565       findOptionMDForLoop(this, "llvm.loop.parallel_accesses");
566   SmallPtrSet<MDNode *, 4>
567       ParallelAccessGroups; // For scalable 'contains' check.
568   if (ParallelAccesses) {
569     for (const MDOperand &MD : drop_begin(ParallelAccesses->operands(), 1)) {
570       MDNode *AccGroup = cast<MDNode>(MD.get());
571       assert(isValidAsAccessGroup(AccGroup) &&
572              "List item must be an access group");
573       ParallelAccessGroups.insert(AccGroup);
574     }
575   }
576 
577   // The loop branch contains the parallel loop metadata. In order to ensure
578   // that any parallel-loop-unaware optimization pass hasn't added loop-carried
579   // dependencies (thus converted the loop back to a sequential loop), check
580   // that all the memory instructions in the loop belong to an access group that
581   // is parallel to this loop.
582   for (BasicBlock *BB : this->blocks()) {
583     for (Instruction &I : *BB) {
584       if (!I.mayReadOrWriteMemory())
585         continue;
586 
587       if (MDNode *AccessGroup = I.getMetadata(LLVMContext::MD_access_group)) {
588         auto ContainsAccessGroup = [&ParallelAccessGroups](MDNode *AG) -> bool {
589           if (AG->getNumOperands() == 0) {
590             assert(isValidAsAccessGroup(AG) && "Item must be an access group");
591             return ParallelAccessGroups.count(AG);
592           }
593 
594           for (const MDOperand &AccessListItem : AG->operands()) {
595             MDNode *AccGroup = cast<MDNode>(AccessListItem.get());
596             assert(isValidAsAccessGroup(AccGroup) &&
597                    "List item must be an access group");
598             if (ParallelAccessGroups.count(AccGroup))
599               return true;
600           }
601           return false;
602         };
603 
604         if (ContainsAccessGroup(AccessGroup))
605           continue;
606       }
607 
608       // The memory instruction can refer to the loop identifier metadata
609       // directly or indirectly through another list metadata (in case of
610       // nested parallel loops). The loop identifier metadata refers to
611       // itself so we can check both cases with the same routine.
612       MDNode *LoopIdMD =
613           I.getMetadata(LLVMContext::MD_mem_parallel_loop_access);
614 
615       if (!LoopIdMD)
616         return false;
617 
618       bool LoopIdMDFound = false;
619       for (const MDOperand &MDOp : LoopIdMD->operands()) {
620         if (MDOp == DesiredLoopIdMetadata) {
621           LoopIdMDFound = true;
622           break;
623         }
624       }
625 
626       if (!LoopIdMDFound)
627         return false;
628     }
629   }
630   return true;
631 }
632 
633 DebugLoc Loop::getStartLoc() const { return getLocRange().getStart(); }
634 
635 Loop::LocRange Loop::getLocRange() const {
636   // If we have a debug location in the loop ID, then use it.
637   if (MDNode *LoopID = getLoopID()) {
638     DebugLoc Start;
639     // We use the first DebugLoc in the header as the start location of the loop
640     // and if there is a second DebugLoc in the header we use it as end location
641     // of the loop.
642     for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
643       if (DILocation *L = dyn_cast<DILocation>(LoopID->getOperand(i))) {
644         if (!Start)
645           Start = DebugLoc(L);
646         else
647           return LocRange(Start, DebugLoc(L));
648       }
649     }
650 
651     if (Start)
652       return LocRange(Start);
653   }
654 
655   // Try the pre-header first.
656   if (BasicBlock *PHeadBB = getLoopPreheader())
657     if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
658       return LocRange(DL);
659 
660   // If we have no pre-header or there are no instructions with debug
661   // info in it, try the header.
662   if (BasicBlock *HeadBB = getHeader())
663     return LocRange(HeadBB->getTerminator()->getDebugLoc());
664 
665   return LocRange();
666 }
667 
668 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
669 LLVM_DUMP_METHOD void Loop::dump() const { print(dbgs()); }
670 
671 LLVM_DUMP_METHOD void Loop::dumpVerbose() const {
672   print(dbgs(), /*Depth=*/0, /*Verbose=*/true);
673 }
674 #endif
675 
676 //===----------------------------------------------------------------------===//
677 // UnloopUpdater implementation
678 //
679 
680 namespace {
681 /// Find the new parent loop for all blocks within the "unloop" whose last
682 /// backedges has just been removed.
683 class UnloopUpdater {
684   Loop &Unloop;
685   LoopInfo *LI;
686 
687   LoopBlocksDFS DFS;
688 
689   // Map unloop's immediate subloops to their nearest reachable parents. Nested
690   // loops within these subloops will not change parents. However, an immediate
691   // subloop's new parent will be the nearest loop reachable from either its own
692   // exits *or* any of its nested loop's exits.
693   DenseMap<Loop *, Loop *> SubloopParents;
694 
695   // Flag the presence of an irreducible backedge whose destination is a block
696   // directly contained by the original unloop.
697   bool FoundIB;
698 
699 public:
700   UnloopUpdater(Loop *UL, LoopInfo *LInfo)
701       : Unloop(*UL), LI(LInfo), DFS(UL), FoundIB(false) {}
702 
703   void updateBlockParents();
704 
705   void removeBlocksFromAncestors();
706 
707   void updateSubloopParents();
708 
709 protected:
710   Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop);
711 };
712 } // end anonymous namespace
713 
714 /// Update the parent loop for all blocks that are directly contained within the
715 /// original "unloop".
716 void UnloopUpdater::updateBlockParents() {
717   if (Unloop.getNumBlocks()) {
718     // Perform a post order CFG traversal of all blocks within this loop,
719     // propagating the nearest loop from successors to predecessors.
720     LoopBlocksTraversal Traversal(DFS, LI);
721     for (BasicBlock *POI : Traversal) {
722 
723       Loop *L = LI->getLoopFor(POI);
724       Loop *NL = getNearestLoop(POI, L);
725 
726       if (NL != L) {
727         // For reducible loops, NL is now an ancestor of Unloop.
728         assert((NL != &Unloop && (!NL || NL->contains(&Unloop))) &&
729                "uninitialized successor");
730         LI->changeLoopFor(POI, NL);
731       } else {
732         // Or the current block is part of a subloop, in which case its parent
733         // is unchanged.
734         assert((FoundIB || Unloop.contains(L)) && "uninitialized successor");
735       }
736     }
737   }
738   // Each irreducible loop within the unloop induces a round of iteration using
739   // the DFS result cached by Traversal.
740   bool Changed = FoundIB;
741   for (unsigned NIters = 0; Changed; ++NIters) {
742     assert(NIters < Unloop.getNumBlocks() && "runaway iterative algorithm");
743 
744     // Iterate over the postorder list of blocks, propagating the nearest loop
745     // from successors to predecessors as before.
746     Changed = false;
747     for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(),
748                                    POE = DFS.endPostorder();
749          POI != POE; ++POI) {
750 
751       Loop *L = LI->getLoopFor(*POI);
752       Loop *NL = getNearestLoop(*POI, L);
753       if (NL != L) {
754         assert(NL != &Unloop && (!NL || NL->contains(&Unloop)) &&
755                "uninitialized successor");
756         LI->changeLoopFor(*POI, NL);
757         Changed = true;
758       }
759     }
760   }
761 }
762 
763 /// Remove unloop's blocks from all ancestors below their new parents.
764 void UnloopUpdater::removeBlocksFromAncestors() {
765   // Remove all unloop's blocks (including those in nested subloops) from
766   // ancestors below the new parent loop.
767   for (Loop::block_iterator BI = Unloop.block_begin(), BE = Unloop.block_end();
768        BI != BE; ++BI) {
769     Loop *OuterParent = LI->getLoopFor(*BI);
770     if (Unloop.contains(OuterParent)) {
771       while (OuterParent->getParentLoop() != &Unloop)
772         OuterParent = OuterParent->getParentLoop();
773       OuterParent = SubloopParents[OuterParent];
774     }
775     // Remove blocks from former Ancestors except Unloop itself which will be
776     // deleted.
777     for (Loop *OldParent = Unloop.getParentLoop(); OldParent != OuterParent;
778          OldParent = OldParent->getParentLoop()) {
779       assert(OldParent && "new loop is not an ancestor of the original");
780       OldParent->removeBlockFromLoop(*BI);
781     }
782   }
783 }
784 
785 /// Update the parent loop for all subloops directly nested within unloop.
786 void UnloopUpdater::updateSubloopParents() {
787   while (!Unloop.isInnermost()) {
788     Loop *Subloop = *std::prev(Unloop.end());
789     Unloop.removeChildLoop(std::prev(Unloop.end()));
790 
791     assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop");
792     if (Loop *Parent = SubloopParents[Subloop])
793       Parent->addChildLoop(Subloop);
794     else
795       LI->addTopLevelLoop(Subloop);
796   }
797 }
798 
799 /// Return the nearest parent loop among this block's successors. If a successor
800 /// is a subloop header, consider its parent to be the nearest parent of the
801 /// subloop's exits.
802 ///
803 /// For subloop blocks, simply update SubloopParents and return NULL.
804 Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) {
805 
806   // Initially for blocks directly contained by Unloop, NearLoop == Unloop and
807   // is considered uninitialized.
808   Loop *NearLoop = BBLoop;
809 
810   Loop *Subloop = nullptr;
811   if (NearLoop != &Unloop && Unloop.contains(NearLoop)) {
812     Subloop = NearLoop;
813     // Find the subloop ancestor that is directly contained within Unloop.
814     while (Subloop->getParentLoop() != &Unloop) {
815       Subloop = Subloop->getParentLoop();
816       assert(Subloop && "subloop is not an ancestor of the original loop");
817     }
818     // Get the current nearest parent of the Subloop exits, initially Unloop.
819     NearLoop = SubloopParents.insert({Subloop, &Unloop}).first->second;
820   }
821 
822   succ_iterator I = succ_begin(BB), E = succ_end(BB);
823   if (I == E) {
824     assert(!Subloop && "subloop blocks must have a successor");
825     NearLoop = nullptr; // unloop blocks may now exit the function.
826   }
827   for (; I != E; ++I) {
828     if (*I == BB)
829       continue; // self loops are uninteresting
830 
831     Loop *L = LI->getLoopFor(*I);
832     if (L == &Unloop) {
833       // This successor has not been processed. This path must lead to an
834       // irreducible backedge.
835       assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB");
836       FoundIB = true;
837     }
838     if (L != &Unloop && Unloop.contains(L)) {
839       // Successor is in a subloop.
840       if (Subloop)
841         continue; // Branching within subloops. Ignore it.
842 
843       // BB branches from the original into a subloop header.
844       assert(L->getParentLoop() == &Unloop && "cannot skip into nested loops");
845 
846       // Get the current nearest parent of the Subloop's exits.
847       L = SubloopParents[L];
848       // L could be Unloop if the only exit was an irreducible backedge.
849     }
850     if (L == &Unloop) {
851       continue;
852     }
853     // Handle critical edges from Unloop into a sibling loop.
854     if (L && !L->contains(&Unloop)) {
855       L = L->getParentLoop();
856     }
857     // Remember the nearest parent loop among successors or subloop exits.
858     if (NearLoop == &Unloop || !NearLoop || NearLoop->contains(L))
859       NearLoop = L;
860   }
861   if (Subloop) {
862     SubloopParents[Subloop] = NearLoop;
863     return BBLoop;
864   }
865   return NearLoop;
866 }
867 
868 LoopInfo::LoopInfo(const DomTreeBase<BasicBlock> &DomTree) { analyze(DomTree); }
869 
870 bool LoopInfo::invalidate(Function &F, const PreservedAnalyses &PA,
871                           FunctionAnalysisManager::Invalidator &) {
872   // Check whether the analysis, all analyses on functions, or the function's
873   // CFG have been preserved.
874   auto PAC = PA.getChecker<LoopAnalysis>();
875   return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
876            PAC.preservedSet<CFGAnalyses>());
877 }
878 
879 void LoopInfo::erase(Loop *Unloop) {
880   assert(!Unloop->isInvalid() && "Loop has already been erased!");
881 
882   auto InvalidateOnExit = make_scope_exit([&]() { destroy(Unloop); });
883 
884   // First handle the special case of no parent loop to simplify the algorithm.
885   if (Unloop->isOutermost()) {
886     // Since BBLoop had no parent, Unloop blocks are no longer in a loop.
887     for (Loop::block_iterator I = Unloop->block_begin(),
888                               E = Unloop->block_end();
889          I != E; ++I) {
890 
891       // Don't reparent blocks in subloops.
892       if (getLoopFor(*I) != Unloop)
893         continue;
894 
895       // Blocks no longer have a parent but are still referenced by Unloop until
896       // the Unloop object is deleted.
897       changeLoopFor(*I, nullptr);
898     }
899 
900     // Remove the loop from the top-level LoopInfo object.
901     for (iterator I = begin();; ++I) {
902       assert(I != end() && "Couldn't find loop");
903       if (*I == Unloop) {
904         removeLoop(I);
905         break;
906       }
907     }
908 
909     // Move all of the subloops to the top-level.
910     while (!Unloop->isInnermost())
911       addTopLevelLoop(Unloop->removeChildLoop(std::prev(Unloop->end())));
912 
913     return;
914   }
915 
916   // Update the parent loop for all blocks within the loop. Blocks within
917   // subloops will not change parents.
918   UnloopUpdater Updater(Unloop, this);
919   Updater.updateBlockParents();
920 
921   // Remove blocks from former ancestor loops.
922   Updater.removeBlocksFromAncestors();
923 
924   // Add direct subloops as children in their new parent loop.
925   Updater.updateSubloopParents();
926 
927   // Remove unloop from its parent loop.
928   Loop *ParentLoop = Unloop->getParentLoop();
929   for (Loop::iterator I = ParentLoop->begin();; ++I) {
930     assert(I != ParentLoop->end() && "Couldn't find loop");
931     if (*I == Unloop) {
932       ParentLoop->removeChildLoop(I);
933       break;
934     }
935   }
936 }
937 
938 AnalysisKey LoopAnalysis::Key;
939 
940 LoopInfo LoopAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
941   // FIXME: Currently we create a LoopInfo from scratch for every function.
942   // This may prove to be too wasteful due to deallocating and re-allocating
943   // memory each time for the underlying map and vector datastructures. At some
944   // point it may prove worthwhile to use a freelist and recycle LoopInfo
945   // objects. I don't want to add that kind of complexity until the scope of
946   // the problem is better understood.
947   LoopInfo LI;
948   LI.analyze(AM.getResult<DominatorTreeAnalysis>(F));
949   return LI;
950 }
951 
952 PreservedAnalyses LoopPrinterPass::run(Function &F,
953                                        FunctionAnalysisManager &AM) {
954   AM.getResult<LoopAnalysis>(F).print(OS);
955   return PreservedAnalyses::all();
956 }
957 
958 void llvm::printLoop(Loop &L, raw_ostream &OS, const std::string &Banner) {
959 
960   if (forcePrintModuleIR()) {
961     // handling -print-module-scope
962     OS << Banner << " (loop: ";
963     L.getHeader()->printAsOperand(OS, false);
964     OS << ")\n";
965 
966     // printing whole module
967     OS << *L.getHeader()->getModule();
968     return;
969   }
970 
971   OS << Banner;
972 
973   auto *PreHeader = L.getLoopPreheader();
974   if (PreHeader) {
975     OS << "\n; Preheader:";
976     PreHeader->print(OS);
977     OS << "\n; Loop:";
978   }
979 
980   for (auto *Block : L.blocks())
981     if (Block)
982       Block->print(OS);
983     else
984       OS << "Printing <null> block";
985 
986   SmallVector<BasicBlock *, 8> ExitBlocks;
987   L.getExitBlocks(ExitBlocks);
988   if (!ExitBlocks.empty()) {
989     OS << "\n; Exit blocks";
990     for (auto *Block : ExitBlocks)
991       if (Block)
992         Block->print(OS);
993       else
994         OS << "Printing <null> block";
995   }
996 }
997 
998 MDNode *llvm::findOptionMDForLoopID(MDNode *LoopID, StringRef Name) {
999   // No loop metadata node, no loop properties.
1000   if (!LoopID)
1001     return nullptr;
1002 
1003   // First operand should refer to the metadata node itself, for legacy reasons.
1004   assert(LoopID->getNumOperands() > 0 && "requires at least one operand");
1005   assert(LoopID->getOperand(0) == LoopID && "invalid loop id");
1006 
1007   // Iterate over the metdata node operands and look for MDString metadata.
1008   for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) {
1009     MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
1010     if (!MD || MD->getNumOperands() < 1)
1011       continue;
1012     MDString *S = dyn_cast<MDString>(MD->getOperand(0));
1013     if (!S)
1014       continue;
1015     // Return the operand node if MDString holds expected metadata.
1016     if (Name.equals(S->getString()))
1017       return MD;
1018   }
1019 
1020   // Loop property not found.
1021   return nullptr;
1022 }
1023 
1024 MDNode *llvm::findOptionMDForLoop(const Loop *TheLoop, StringRef Name) {
1025   return findOptionMDForLoopID(TheLoop->getLoopID(), Name);
1026 }
1027 
1028 bool llvm::isValidAsAccessGroup(MDNode *Node) {
1029   return Node->getNumOperands() == 0 && Node->isDistinct();
1030 }
1031 
1032 MDNode *llvm::makePostTransformationMetadata(LLVMContext &Context,
1033                                              MDNode *OrigLoopID,
1034                                              ArrayRef<StringRef> RemovePrefixes,
1035                                              ArrayRef<MDNode *> AddAttrs) {
1036   // First remove any existing loop metadata related to this transformation.
1037   SmallVector<Metadata *, 4> MDs;
1038 
1039   // Reserve first location for self reference to the LoopID metadata node.
1040   MDs.push_back(nullptr);
1041 
1042   // Remove metadata for the transformation that has been applied or that became
1043   // outdated.
1044   if (OrigLoopID) {
1045     for (unsigned i = 1, ie = OrigLoopID->getNumOperands(); i < ie; ++i) {
1046       bool IsVectorMetadata = false;
1047       Metadata *Op = OrigLoopID->getOperand(i);
1048       if (MDNode *MD = dyn_cast<MDNode>(Op)) {
1049         const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
1050         if (S)
1051           IsVectorMetadata =
1052               llvm::any_of(RemovePrefixes, [S](StringRef Prefix) -> bool {
1053                 return S->getString().startswith(Prefix);
1054               });
1055       }
1056       if (!IsVectorMetadata)
1057         MDs.push_back(Op);
1058     }
1059   }
1060 
1061   // Add metadata to avoid reapplying a transformation, such as
1062   // llvm.loop.unroll.disable and llvm.loop.isvectorized.
1063   MDs.append(AddAttrs.begin(), AddAttrs.end());
1064 
1065   MDNode *NewLoopID = MDNode::getDistinct(Context, MDs);
1066   // Replace the temporary node with a self-reference.
1067   NewLoopID->replaceOperandWith(0, NewLoopID);
1068   return NewLoopID;
1069 }
1070 
1071 //===----------------------------------------------------------------------===//
1072 // LoopInfo implementation
1073 //
1074 
1075 LoopInfoWrapperPass::LoopInfoWrapperPass() : FunctionPass(ID) {
1076   initializeLoopInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1077 }
1078 
1079 char LoopInfoWrapperPass::ID = 0;
1080 INITIALIZE_PASS_BEGIN(LoopInfoWrapperPass, "loops", "Natural Loop Information",
1081                       true, true)
1082 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
1083 INITIALIZE_PASS_END(LoopInfoWrapperPass, "loops", "Natural Loop Information",
1084                     true, true)
1085 
1086 bool LoopInfoWrapperPass::runOnFunction(Function &) {
1087   releaseMemory();
1088   LI.analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree());
1089   return false;
1090 }
1091 
1092 void LoopInfoWrapperPass::verifyAnalysis() const {
1093   // LoopInfoWrapperPass is a FunctionPass, but verifying every loop in the
1094   // function each time verifyAnalysis is called is very expensive. The
1095   // -verify-loop-info option can enable this. In order to perform some
1096   // checking by default, LoopPass has been taught to call verifyLoop manually
1097   // during loop pass sequences.
1098   if (VerifyLoopInfo) {
1099     auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1100     LI.verify(DT);
1101   }
1102 }
1103 
1104 void LoopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1105   AU.setPreservesAll();
1106   AU.addRequiredTransitive<DominatorTreeWrapperPass>();
1107 }
1108 
1109 void LoopInfoWrapperPass::print(raw_ostream &OS, const Module *) const {
1110   LI.print(OS);
1111 }
1112 
1113 PreservedAnalyses LoopVerifierPass::run(Function &F,
1114                                         FunctionAnalysisManager &AM) {
1115   LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
1116   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
1117   LI.verify(DT);
1118   return PreservedAnalyses::all();
1119 }
1120 
1121 //===----------------------------------------------------------------------===//
1122 // LoopBlocksDFS implementation
1123 //
1124 
1125 /// Traverse the loop blocks and store the DFS result.
1126 /// Useful for clients that just want the final DFS result and don't need to
1127 /// visit blocks during the initial traversal.
1128 void LoopBlocksDFS::perform(LoopInfo *LI) {
1129   LoopBlocksTraversal Traversal(*this, LI);
1130   for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(),
1131                                         POE = Traversal.end();
1132        POI != POE; ++POI)
1133     ;
1134 }
1135