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/LoopInfoImpl.h"
21 #include "llvm/Analysis/LoopIterator.h"
22 #include "llvm/Analysis/ValueTracking.h"
23 #include "llvm/Config/llvm-config.h"
24 #include "llvm/IR/CFG.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DebugLoc.h"
27 #include "llvm/IR/Dominators.h"
28 #include "llvm/IR/IRPrintingPasses.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/IR/Metadata.h"
32 #include "llvm/IR/PassManager.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include <algorithm>
37 using namespace llvm;
38 
39 // Explicitly instantiate methods in LoopInfoImpl.h for IR-level Loops.
40 template class llvm::LoopBase<BasicBlock, Loop>;
41 template class llvm::LoopInfoBase<BasicBlock, Loop>;
42 
43 // Always verify loopinfo if expensive checking is enabled.
44 #ifdef EXPENSIVE_CHECKS
45 bool llvm::VerifyLoopInfo = true;
46 #else
47 bool llvm::VerifyLoopInfo = false;
48 #endif
49 static cl::opt<bool, true>
50     VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo),
51                     cl::Hidden, cl::desc("Verify loop info (time consuming)"));
52 
53 //===----------------------------------------------------------------------===//
54 // Loop implementation
55 //
56 
57 bool Loop::isLoopInvariant(const Value *V) const {
58   if (const Instruction *I = dyn_cast<Instruction>(V))
59     return !contains(I);
60   return true; // All non-instructions are loop invariant
61 }
62 
63 bool Loop::hasLoopInvariantOperands(const Instruction *I) const {
64   return all_of(I->operands(), [this](Value *V) { return isLoopInvariant(V); });
65 }
66 
67 bool Loop::makeLoopInvariant(Value *V, bool &Changed,
68                              Instruction *InsertPt) const {
69   if (Instruction *I = dyn_cast<Instruction>(V))
70     return makeLoopInvariant(I, Changed, InsertPt);
71   return true; // All non-instructions are loop-invariant.
72 }
73 
74 bool Loop::makeLoopInvariant(Instruction *I, bool &Changed,
75                              Instruction *InsertPt) const {
76   // Test if the value is already loop-invariant.
77   if (isLoopInvariant(I))
78     return true;
79   if (!isSafeToSpeculativelyExecute(I))
80     return false;
81   if (I->mayReadFromMemory())
82     return false;
83   // EH block instructions are immobile.
84   if (I->isEHPad())
85     return false;
86   // Determine the insertion point, unless one was given.
87   if (!InsertPt) {
88     BasicBlock *Preheader = getLoopPreheader();
89     // Without a preheader, hoisting is not feasible.
90     if (!Preheader)
91       return false;
92     InsertPt = Preheader->getTerminator();
93   }
94   // Don't hoist instructions with loop-variant operands.
95   for (Value *Operand : I->operands())
96     if (!makeLoopInvariant(Operand, Changed, InsertPt))
97       return false;
98 
99   // Hoist.
100   I->moveBefore(InsertPt);
101 
102   // There is possibility of hoisting this instruction above some arbitrary
103   // condition. Any metadata defined on it can be control dependent on this
104   // condition. Conservatively strip it here so that we don't give any wrong
105   // information to the optimizer.
106   I->dropUnknownNonDebugMetadata();
107 
108   Changed = true;
109   return true;
110 }
111 
112 PHINode *Loop::getCanonicalInductionVariable() const {
113   BasicBlock *H = getHeader();
114 
115   BasicBlock *Incoming = nullptr, *Backedge = nullptr;
116   pred_iterator PI = pred_begin(H);
117   assert(PI != pred_end(H) && "Loop must have at least one backedge!");
118   Backedge = *PI++;
119   if (PI == pred_end(H))
120     return nullptr; // dead loop
121   Incoming = *PI++;
122   if (PI != pred_end(H))
123     return nullptr; // multiple backedges?
124 
125   if (contains(Incoming)) {
126     if (contains(Backedge))
127       return nullptr;
128     std::swap(Incoming, Backedge);
129   } else if (!contains(Backedge))
130     return nullptr;
131 
132   // Loop over all of the PHI nodes, looking for a canonical indvar.
133   for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) {
134     PHINode *PN = cast<PHINode>(I);
135     if (ConstantInt *CI =
136             dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming)))
137       if (CI->isZero())
138         if (Instruction *Inc =
139                 dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
140           if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN)
141             if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
142               if (CI->isOne())
143                 return PN;
144   }
145   return nullptr;
146 }
147 
148 // Check that 'BB' doesn't have any uses outside of the 'L'
149 static bool isBlockInLCSSAForm(const Loop &L, const BasicBlock &BB,
150                                DominatorTree &DT) {
151   for (const Instruction &I : BB) {
152     // Tokens can't be used in PHI nodes and live-out tokens prevent loop
153     // optimizations, so for the purposes of considered LCSSA form, we
154     // can ignore them.
155     if (I.getType()->isTokenTy())
156       continue;
157 
158     for (const Use &U : I.uses()) {
159       const Instruction *UI = cast<Instruction>(U.getUser());
160       const BasicBlock *UserBB = UI->getParent();
161       if (const PHINode *P = dyn_cast<PHINode>(UI))
162         UserBB = P->getIncomingBlock(U);
163 
164       // Check the current block, as a fast-path, before checking whether
165       // the use is anywhere in the loop.  Most values are used in the same
166       // block they are defined in.  Also, blocks not reachable from the
167       // entry are special; uses in them don't need to go through PHIs.
168       if (UserBB != &BB && !L.contains(UserBB) &&
169           DT.isReachableFromEntry(UserBB))
170         return false;
171     }
172   }
173   return true;
174 }
175 
176 bool Loop::isLCSSAForm(DominatorTree &DT) const {
177   // For each block we check that it doesn't have any uses outside of this loop.
178   return all_of(this->blocks(), [&](const BasicBlock *BB) {
179     return isBlockInLCSSAForm(*this, *BB, DT);
180   });
181 }
182 
183 bool Loop::isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const {
184   // For each block we check that it doesn't have any uses outside of its
185   // innermost loop. This process will transitively guarantee that the current
186   // loop and all of the nested loops are in LCSSA form.
187   return all_of(this->blocks(), [&](const BasicBlock *BB) {
188     return isBlockInLCSSAForm(*LI.getLoopFor(BB), *BB, DT);
189   });
190 }
191 
192 bool Loop::isLoopSimplifyForm() const {
193   // Normal-form loops have a preheader, a single backedge, and all of their
194   // exits have all their predecessors inside the loop.
195   return getLoopPreheader() && getLoopLatch() && hasDedicatedExits();
196 }
197 
198 // Routines that reform the loop CFG and split edges often fail on indirectbr.
199 bool Loop::isSafeToClone() const {
200   // Return false if any loop blocks contain indirectbrs, or there are any calls
201   // to noduplicate functions.
202   for (BasicBlock *BB : this->blocks()) {
203     if (isa<IndirectBrInst>(BB->getTerminator()))
204       return false;
205 
206     for (Instruction &I : *BB)
207       if (auto CS = CallSite(&I))
208         if (CS.cannotDuplicate())
209           return false;
210   }
211   return true;
212 }
213 
214 MDNode *Loop::getLoopID() const {
215   MDNode *LoopID = nullptr;
216 
217   // Go through the latch blocks and check the terminator for the metadata.
218   SmallVector<BasicBlock *, 4> LatchesBlocks;
219   getLoopLatches(LatchesBlocks);
220   for (BasicBlock *BB : LatchesBlocks) {
221     Instruction *TI = BB->getTerminator();
222     MDNode *MD = TI->getMetadata(LLVMContext::MD_loop);
223 
224     if (!MD)
225       return nullptr;
226 
227     if (!LoopID)
228       LoopID = MD;
229     else if (MD != LoopID)
230       return nullptr;
231   }
232   if (!LoopID || LoopID->getNumOperands() == 0 ||
233       LoopID->getOperand(0) != LoopID)
234     return nullptr;
235   return LoopID;
236 }
237 
238 void Loop::setLoopID(MDNode *LoopID) const {
239   assert((!LoopID || LoopID->getNumOperands() > 0) &&
240          "Loop ID needs at least one operand");
241   assert((!LoopID || LoopID->getOperand(0) == LoopID) &&
242          "Loop ID should refer to itself");
243 
244   BasicBlock *H = getHeader();
245   for (BasicBlock *BB : this->blocks()) {
246     Instruction *TI = BB->getTerminator();
247     for (BasicBlock *Successor : successors(TI)) {
248       if (Successor == H) {
249         TI->setMetadata(LLVMContext::MD_loop, LoopID);
250         break;
251       }
252     }
253   }
254 }
255 
256 void Loop::setLoopAlreadyUnrolled() {
257   MDNode *LoopID = getLoopID();
258   // First remove any existing loop unrolling metadata.
259   SmallVector<Metadata *, 4> MDs;
260   // Reserve first location for self reference to the LoopID metadata node.
261   MDs.push_back(nullptr);
262 
263   if (LoopID) {
264     for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
265       bool IsUnrollMetadata = false;
266       MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
267       if (MD) {
268         const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
269         IsUnrollMetadata = S && S->getString().startswith("llvm.loop.unroll.");
270       }
271       if (!IsUnrollMetadata)
272         MDs.push_back(LoopID->getOperand(i));
273     }
274   }
275 
276   // Add unroll(disable) metadata to disable future unrolling.
277   LLVMContext &Context = getHeader()->getContext();
278   SmallVector<Metadata *, 1> DisableOperands;
279   DisableOperands.push_back(MDString::get(Context, "llvm.loop.unroll.disable"));
280   MDNode *DisableNode = MDNode::get(Context, DisableOperands);
281   MDs.push_back(DisableNode);
282 
283   MDNode *NewLoopID = MDNode::get(Context, MDs);
284   // Set operand 0 to refer to the loop id itself.
285   NewLoopID->replaceOperandWith(0, NewLoopID);
286   setLoopID(NewLoopID);
287 }
288 
289 bool Loop::isAnnotatedParallel() const {
290   MDNode *DesiredLoopIdMetadata = getLoopID();
291 
292   if (!DesiredLoopIdMetadata)
293     return false;
294 
295   MDNode *ParallelAccesses =
296       findOptionMDForLoop(this, "llvm.loop.parallel_accesses");
297   SmallPtrSet<MDNode *, 4>
298       ParallelAccessGroups; // For scalable 'contains' check.
299   if (ParallelAccesses) {
300     for (const MDOperand &MD : drop_begin(ParallelAccesses->operands(), 1)) {
301       MDNode *AccGroup = cast<MDNode>(MD.get());
302       assert(isValidAsAccessGroup(AccGroup) &&
303              "List item must be an access group");
304       ParallelAccessGroups.insert(AccGroup);
305     }
306   }
307 
308   // The loop branch contains the parallel loop metadata. In order to ensure
309   // that any parallel-loop-unaware optimization pass hasn't added loop-carried
310   // dependencies (thus converted the loop back to a sequential loop), check
311   // that all the memory instructions in the loop belong to an access group that
312   // is parallel to this loop.
313   for (BasicBlock *BB : this->blocks()) {
314     for (Instruction &I : *BB) {
315       if (!I.mayReadOrWriteMemory())
316         continue;
317 
318       if (MDNode *AccessGroup = I.getMetadata(LLVMContext::MD_access_group)) {
319         auto ContainsAccessGroup = [&ParallelAccessGroups](MDNode *AG) -> bool {
320           if (AG->getNumOperands() == 0) {
321             assert(isValidAsAccessGroup(AG) && "Item must be an access group");
322             return ParallelAccessGroups.count(AG);
323           }
324 
325           for (const MDOperand &AccessListItem : AG->operands()) {
326             MDNode *AccGroup = cast<MDNode>(AccessListItem.get());
327             assert(isValidAsAccessGroup(AccGroup) &&
328                    "List item must be an access group");
329             if (ParallelAccessGroups.count(AccGroup))
330               return true;
331           }
332           return false;
333         };
334 
335         if (ContainsAccessGroup(AccessGroup))
336           continue;
337       }
338 
339       // The memory instruction can refer to the loop identifier metadata
340       // directly or indirectly through another list metadata (in case of
341       // nested parallel loops). The loop identifier metadata refers to
342       // itself so we can check both cases with the same routine.
343       MDNode *LoopIdMD =
344           I.getMetadata(LLVMContext::MD_mem_parallel_loop_access);
345 
346       if (!LoopIdMD)
347         return false;
348 
349       bool LoopIdMDFound = false;
350       for (const MDOperand &MDOp : LoopIdMD->operands()) {
351         if (MDOp == DesiredLoopIdMetadata) {
352           LoopIdMDFound = true;
353           break;
354         }
355       }
356 
357       if (!LoopIdMDFound)
358         return false;
359     }
360   }
361   return true;
362 }
363 
364 DebugLoc Loop::getStartLoc() const { return getLocRange().getStart(); }
365 
366 Loop::LocRange Loop::getLocRange() const {
367   // If we have a debug location in the loop ID, then use it.
368   if (MDNode *LoopID = getLoopID()) {
369     DebugLoc Start;
370     // We use the first DebugLoc in the header as the start location of the loop
371     // and if there is a second DebugLoc in the header we use it as end location
372     // of the loop.
373     for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
374       if (DILocation *L = dyn_cast<DILocation>(LoopID->getOperand(i))) {
375         if (!Start)
376           Start = DebugLoc(L);
377         else
378           return LocRange(Start, DebugLoc(L));
379       }
380     }
381 
382     if (Start)
383       return LocRange(Start);
384   }
385 
386   // Try the pre-header first.
387   if (BasicBlock *PHeadBB = getLoopPreheader())
388     if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
389       return LocRange(DL);
390 
391   // If we have no pre-header or there are no instructions with debug
392   // info in it, try the header.
393   if (BasicBlock *HeadBB = getHeader())
394     return LocRange(HeadBB->getTerminator()->getDebugLoc());
395 
396   return LocRange();
397 }
398 
399 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
400 LLVM_DUMP_METHOD void Loop::dump() const { print(dbgs()); }
401 
402 LLVM_DUMP_METHOD void Loop::dumpVerbose() const {
403   print(dbgs(), /*Depth=*/0, /*Verbose=*/true);
404 }
405 #endif
406 
407 //===----------------------------------------------------------------------===//
408 // UnloopUpdater implementation
409 //
410 
411 namespace {
412 /// Find the new parent loop for all blocks within the "unloop" whose last
413 /// backedges has just been removed.
414 class UnloopUpdater {
415   Loop &Unloop;
416   LoopInfo *LI;
417 
418   LoopBlocksDFS DFS;
419 
420   // Map unloop's immediate subloops to their nearest reachable parents. Nested
421   // loops within these subloops will not change parents. However, an immediate
422   // subloop's new parent will be the nearest loop reachable from either its own
423   // exits *or* any of its nested loop's exits.
424   DenseMap<Loop *, Loop *> SubloopParents;
425 
426   // Flag the presence of an irreducible backedge whose destination is a block
427   // directly contained by the original unloop.
428   bool FoundIB;
429 
430 public:
431   UnloopUpdater(Loop *UL, LoopInfo *LInfo)
432       : Unloop(*UL), LI(LInfo), DFS(UL), FoundIB(false) {}
433 
434   void updateBlockParents();
435 
436   void removeBlocksFromAncestors();
437 
438   void updateSubloopParents();
439 
440 protected:
441   Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop);
442 };
443 } // end anonymous namespace
444 
445 /// Update the parent loop for all blocks that are directly contained within the
446 /// original "unloop".
447 void UnloopUpdater::updateBlockParents() {
448   if (Unloop.getNumBlocks()) {
449     // Perform a post order CFG traversal of all blocks within this loop,
450     // propagating the nearest loop from successors to predecessors.
451     LoopBlocksTraversal Traversal(DFS, LI);
452     for (BasicBlock *POI : Traversal) {
453 
454       Loop *L = LI->getLoopFor(POI);
455       Loop *NL = getNearestLoop(POI, L);
456 
457       if (NL != L) {
458         // For reducible loops, NL is now an ancestor of Unloop.
459         assert((NL != &Unloop && (!NL || NL->contains(&Unloop))) &&
460                "uninitialized successor");
461         LI->changeLoopFor(POI, NL);
462       } else {
463         // Or the current block is part of a subloop, in which case its parent
464         // is unchanged.
465         assert((FoundIB || Unloop.contains(L)) && "uninitialized successor");
466       }
467     }
468   }
469   // Each irreducible loop within the unloop induces a round of iteration using
470   // the DFS result cached by Traversal.
471   bool Changed = FoundIB;
472   for (unsigned NIters = 0; Changed; ++NIters) {
473     assert(NIters < Unloop.getNumBlocks() && "runaway iterative algorithm");
474 
475     // Iterate over the postorder list of blocks, propagating the nearest loop
476     // from successors to predecessors as before.
477     Changed = false;
478     for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(),
479                                    POE = DFS.endPostorder();
480          POI != POE; ++POI) {
481 
482       Loop *L = LI->getLoopFor(*POI);
483       Loop *NL = getNearestLoop(*POI, L);
484       if (NL != L) {
485         assert(NL != &Unloop && (!NL || NL->contains(&Unloop)) &&
486                "uninitialized successor");
487         LI->changeLoopFor(*POI, NL);
488         Changed = true;
489       }
490     }
491   }
492 }
493 
494 /// Remove unloop's blocks from all ancestors below their new parents.
495 void UnloopUpdater::removeBlocksFromAncestors() {
496   // Remove all unloop's blocks (including those in nested subloops) from
497   // ancestors below the new parent loop.
498   for (Loop::block_iterator BI = Unloop.block_begin(), BE = Unloop.block_end();
499        BI != BE; ++BI) {
500     Loop *OuterParent = LI->getLoopFor(*BI);
501     if (Unloop.contains(OuterParent)) {
502       while (OuterParent->getParentLoop() != &Unloop)
503         OuterParent = OuterParent->getParentLoop();
504       OuterParent = SubloopParents[OuterParent];
505     }
506     // Remove blocks from former Ancestors except Unloop itself which will be
507     // deleted.
508     for (Loop *OldParent = Unloop.getParentLoop(); OldParent != OuterParent;
509          OldParent = OldParent->getParentLoop()) {
510       assert(OldParent && "new loop is not an ancestor of the original");
511       OldParent->removeBlockFromLoop(*BI);
512     }
513   }
514 }
515 
516 /// Update the parent loop for all subloops directly nested within unloop.
517 void UnloopUpdater::updateSubloopParents() {
518   while (!Unloop.empty()) {
519     Loop *Subloop = *std::prev(Unloop.end());
520     Unloop.removeChildLoop(std::prev(Unloop.end()));
521 
522     assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop");
523     if (Loop *Parent = SubloopParents[Subloop])
524       Parent->addChildLoop(Subloop);
525     else
526       LI->addTopLevelLoop(Subloop);
527   }
528 }
529 
530 /// Return the nearest parent loop among this block's successors. If a successor
531 /// is a subloop header, consider its parent to be the nearest parent of the
532 /// subloop's exits.
533 ///
534 /// For subloop blocks, simply update SubloopParents and return NULL.
535 Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) {
536 
537   // Initially for blocks directly contained by Unloop, NearLoop == Unloop and
538   // is considered uninitialized.
539   Loop *NearLoop = BBLoop;
540 
541   Loop *Subloop = nullptr;
542   if (NearLoop != &Unloop && Unloop.contains(NearLoop)) {
543     Subloop = NearLoop;
544     // Find the subloop ancestor that is directly contained within Unloop.
545     while (Subloop->getParentLoop() != &Unloop) {
546       Subloop = Subloop->getParentLoop();
547       assert(Subloop && "subloop is not an ancestor of the original loop");
548     }
549     // Get the current nearest parent of the Subloop exits, initially Unloop.
550     NearLoop = SubloopParents.insert({Subloop, &Unloop}).first->second;
551   }
552 
553   succ_iterator I = succ_begin(BB), E = succ_end(BB);
554   if (I == E) {
555     assert(!Subloop && "subloop blocks must have a successor");
556     NearLoop = nullptr; // unloop blocks may now exit the function.
557   }
558   for (; I != E; ++I) {
559     if (*I == BB)
560       continue; // self loops are uninteresting
561 
562     Loop *L = LI->getLoopFor(*I);
563     if (L == &Unloop) {
564       // This successor has not been processed. This path must lead to an
565       // irreducible backedge.
566       assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB");
567       FoundIB = true;
568     }
569     if (L != &Unloop && Unloop.contains(L)) {
570       // Successor is in a subloop.
571       if (Subloop)
572         continue; // Branching within subloops. Ignore it.
573 
574       // BB branches from the original into a subloop header.
575       assert(L->getParentLoop() == &Unloop && "cannot skip into nested loops");
576 
577       // Get the current nearest parent of the Subloop's exits.
578       L = SubloopParents[L];
579       // L could be Unloop if the only exit was an irreducible backedge.
580     }
581     if (L == &Unloop) {
582       continue;
583     }
584     // Handle critical edges from Unloop into a sibling loop.
585     if (L && !L->contains(&Unloop)) {
586       L = L->getParentLoop();
587     }
588     // Remember the nearest parent loop among successors or subloop exits.
589     if (NearLoop == &Unloop || !NearLoop || NearLoop->contains(L))
590       NearLoop = L;
591   }
592   if (Subloop) {
593     SubloopParents[Subloop] = NearLoop;
594     return BBLoop;
595   }
596   return NearLoop;
597 }
598 
599 LoopInfo::LoopInfo(const DomTreeBase<BasicBlock> &DomTree) { analyze(DomTree); }
600 
601 bool LoopInfo::invalidate(Function &F, const PreservedAnalyses &PA,
602                           FunctionAnalysisManager::Invalidator &) {
603   // Check whether the analysis, all analyses on functions, or the function's
604   // CFG have been preserved.
605   auto PAC = PA.getChecker<LoopAnalysis>();
606   return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
607            PAC.preservedSet<CFGAnalyses>());
608 }
609 
610 void LoopInfo::erase(Loop *Unloop) {
611   assert(!Unloop->isInvalid() && "Loop has already been erased!");
612 
613   auto InvalidateOnExit = make_scope_exit([&]() { destroy(Unloop); });
614 
615   // First handle the special case of no parent loop to simplify the algorithm.
616   if (!Unloop->getParentLoop()) {
617     // Since BBLoop had no parent, Unloop blocks are no longer in a loop.
618     for (Loop::block_iterator I = Unloop->block_begin(),
619                               E = Unloop->block_end();
620          I != E; ++I) {
621 
622       // Don't reparent blocks in subloops.
623       if (getLoopFor(*I) != Unloop)
624         continue;
625 
626       // Blocks no longer have a parent but are still referenced by Unloop until
627       // the Unloop object is deleted.
628       changeLoopFor(*I, nullptr);
629     }
630 
631     // Remove the loop from the top-level LoopInfo object.
632     for (iterator I = begin();; ++I) {
633       assert(I != end() && "Couldn't find loop");
634       if (*I == Unloop) {
635         removeLoop(I);
636         break;
637       }
638     }
639 
640     // Move all of the subloops to the top-level.
641     while (!Unloop->empty())
642       addTopLevelLoop(Unloop->removeChildLoop(std::prev(Unloop->end())));
643 
644     return;
645   }
646 
647   // Update the parent loop for all blocks within the loop. Blocks within
648   // subloops will not change parents.
649   UnloopUpdater Updater(Unloop, this);
650   Updater.updateBlockParents();
651 
652   // Remove blocks from former ancestor loops.
653   Updater.removeBlocksFromAncestors();
654 
655   // Add direct subloops as children in their new parent loop.
656   Updater.updateSubloopParents();
657 
658   // Remove unloop from its parent loop.
659   Loop *ParentLoop = Unloop->getParentLoop();
660   for (Loop::iterator I = ParentLoop->begin();; ++I) {
661     assert(I != ParentLoop->end() && "Couldn't find loop");
662     if (*I == Unloop) {
663       ParentLoop->removeChildLoop(I);
664       break;
665     }
666   }
667 }
668 
669 AnalysisKey LoopAnalysis::Key;
670 
671 LoopInfo LoopAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
672   // FIXME: Currently we create a LoopInfo from scratch for every function.
673   // This may prove to be too wasteful due to deallocating and re-allocating
674   // memory each time for the underlying map and vector datastructures. At some
675   // point it may prove worthwhile to use a freelist and recycle LoopInfo
676   // objects. I don't want to add that kind of complexity until the scope of
677   // the problem is better understood.
678   LoopInfo LI;
679   LI.analyze(AM.getResult<DominatorTreeAnalysis>(F));
680   return LI;
681 }
682 
683 PreservedAnalyses LoopPrinterPass::run(Function &F,
684                                        FunctionAnalysisManager &AM) {
685   AM.getResult<LoopAnalysis>(F).print(OS);
686   return PreservedAnalyses::all();
687 }
688 
689 void llvm::printLoop(Loop &L, raw_ostream &OS, const std::string &Banner) {
690 
691   if (forcePrintModuleIR()) {
692     // handling -print-module-scope
693     OS << Banner << " (loop: ";
694     L.getHeader()->printAsOperand(OS, false);
695     OS << ")\n";
696 
697     // printing whole module
698     OS << *L.getHeader()->getModule();
699     return;
700   }
701 
702   OS << Banner;
703 
704   auto *PreHeader = L.getLoopPreheader();
705   if (PreHeader) {
706     OS << "\n; Preheader:";
707     PreHeader->print(OS);
708     OS << "\n; Loop:";
709   }
710 
711   for (auto *Block : L.blocks())
712     if (Block)
713       Block->print(OS);
714     else
715       OS << "Printing <null> block";
716 
717   SmallVector<BasicBlock *, 8> ExitBlocks;
718   L.getExitBlocks(ExitBlocks);
719   if (!ExitBlocks.empty()) {
720     OS << "\n; Exit blocks";
721     for (auto *Block : ExitBlocks)
722       if (Block)
723         Block->print(OS);
724       else
725         OS << "Printing <null> block";
726   }
727 }
728 
729 MDNode *llvm::findOptionMDForLoopID(MDNode *LoopID, StringRef Name) {
730   // No loop metadata node, no loop properties.
731   if (!LoopID)
732     return nullptr;
733 
734   // First operand should refer to the metadata node itself, for legacy reasons.
735   assert(LoopID->getNumOperands() > 0 && "requires at least one operand");
736   assert(LoopID->getOperand(0) == LoopID && "invalid loop id");
737 
738   // Iterate over the metdata node operands and look for MDString metadata.
739   for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) {
740     MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
741     if (!MD || MD->getNumOperands() < 1)
742       continue;
743     MDString *S = dyn_cast<MDString>(MD->getOperand(0));
744     if (!S)
745       continue;
746     // Return the operand node if MDString holds expected metadata.
747     if (Name.equals(S->getString()))
748       return MD;
749   }
750 
751   // Loop property not found.
752   return nullptr;
753 }
754 
755 MDNode *llvm::findOptionMDForLoop(const Loop *TheLoop, StringRef Name) {
756   return findOptionMDForLoopID(TheLoop->getLoopID(), Name);
757 }
758 
759 bool llvm::isValidAsAccessGroup(MDNode *Node) {
760   return Node->getNumOperands() == 0 && Node->isDistinct();
761 }
762 
763 //===----------------------------------------------------------------------===//
764 // LoopInfo implementation
765 //
766 
767 char LoopInfoWrapperPass::ID = 0;
768 INITIALIZE_PASS_BEGIN(LoopInfoWrapperPass, "loops", "Natural Loop Information",
769                       true, true)
770 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
771 INITIALIZE_PASS_END(LoopInfoWrapperPass, "loops", "Natural Loop Information",
772                     true, true)
773 
774 bool LoopInfoWrapperPass::runOnFunction(Function &) {
775   releaseMemory();
776   LI.analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree());
777   return false;
778 }
779 
780 void LoopInfoWrapperPass::verifyAnalysis() const {
781   // LoopInfoWrapperPass is a FunctionPass, but verifying every loop in the
782   // function each time verifyAnalysis is called is very expensive. The
783   // -verify-loop-info option can enable this. In order to perform some
784   // checking by default, LoopPass has been taught to call verifyLoop manually
785   // during loop pass sequences.
786   if (VerifyLoopInfo) {
787     auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
788     LI.verify(DT);
789   }
790 }
791 
792 void LoopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
793   AU.setPreservesAll();
794   AU.addRequired<DominatorTreeWrapperPass>();
795 }
796 
797 void LoopInfoWrapperPass::print(raw_ostream &OS, const Module *) const {
798   LI.print(OS);
799 }
800 
801 PreservedAnalyses LoopVerifierPass::run(Function &F,
802                                         FunctionAnalysisManager &AM) {
803   LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
804   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
805   LI.verify(DT);
806   return PreservedAnalyses::all();
807 }
808 
809 //===----------------------------------------------------------------------===//
810 // LoopBlocksDFS implementation
811 //
812 
813 /// Traverse the loop blocks and store the DFS result.
814 /// Useful for clients that just want the final DFS result and don't need to
815 /// visit blocks during the initial traversal.
816 void LoopBlocksDFS::perform(LoopInfo *LI) {
817   LoopBlocksTraversal Traversal(*this, LI);
818   for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(),
819                                         POE = Traversal.end();
820        POI != POE; ++POI)
821     ;
822 }
823