blob: 4b174b66d1e12aa55122d17bfdae7286bcb1444b [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===- LoopInfo.cpp - Natural Loop Calculator -----------------------------===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman01808ca2005-04-21 21:13:18 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner6de99422001-11-26 18:41:20 +00009//
10// This file defines the LoopInfo class that is used to identify natural loops
11// and determine the loop depth of various nodes of the CFG. Note that the
12// loops identified may actually be several natural loops that share the same
13// header node... not just a single natural loop.
14//
15//===----------------------------------------------------------------------===//
16
Misha Brukman81804b42004-01-30 17:26:24 +000017#include "llvm/Analysis/LoopInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/DepthFirstIterator.h"
Sanjoy Das09613b12017-09-20 02:31:57 +000019#include "llvm/ADT/ScopeExit.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/SmallPtrSet.h"
Andrew Trickcda51d42012-06-20 03:42:09 +000021#include "llvm/Analysis/LoopInfoImpl.h"
Andrew Trick78b40c32011-08-10 01:59:05 +000022#include "llvm/Analysis/LoopIterator.h"
Dan Gohman75d7d5e2011-12-14 23:49:11 +000023#include "llvm/Analysis/ValueTracking.h"
Nico Weber432a3882018-04-30 14:59:11 +000024#include "llvm/Config/llvm-config.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000025#include "llvm/IR/CFG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Constants.h"
Hal Finkel2f688682016-05-25 21:42:37 +000027#include "llvm/IR/DebugLoc.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000028#include "llvm/IR/Dominators.h"
Fedor Sergeev662e5682018-09-24 16:08:15 +000029#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/Instructions.h"
Philip Reames5a3f5f72014-10-21 00:13:20 +000031#include "llvm/IR/LLVMContext.h"
Pekka Jaaskelainen0d237252013-02-13 18:08:57 +000032#include "llvm/IR/Metadata.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000033#include "llvm/IR/PassManager.h"
Dan Gohman4dbb3012009-09-28 00:27:48 +000034#include "llvm/Support/CommandLine.h"
Dan Gohmanc3f21372010-01-05 21:08:02 +000035#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000036#include "llvm/Support/raw_ostream.h"
Chris Lattner6de99422001-11-26 18:41:20 +000037#include <algorithm>
Chris Lattner55b7ef52004-04-12 20:26:17 +000038using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000039
Andrew Trickcda51d42012-06-20 03:42:09 +000040// Explicitly instantiate methods in LoopInfoImpl.h for IR-level Loops.
41template class llvm::LoopBase<BasicBlock, Loop>;
42template class llvm::LoopInfoBase<BasicBlock, Loop>;
43
Dan Gohman4dbb3012009-09-28 00:27:48 +000044// Always verify loopinfo if expensive checking is enabled.
Filipe Cabecinhas0da99372016-04-29 15:22:48 +000045#ifdef EXPENSIVE_CHECKS
Serge Pavlov69b3ff92017-01-24 05:52:07 +000046bool llvm::VerifyLoopInfo = true;
Dan Gohman4dbb3012009-09-28 00:27:48 +000047#else
Serge Pavlov69b3ff92017-01-24 05:52:07 +000048bool llvm::VerifyLoopInfo = false;
Dan Gohman4dbb3012009-09-28 00:27:48 +000049#endif
Sanjoy Das66a004a2017-09-20 01:12:09 +000050static cl::opt<bool, true>
51 VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo),
Zachary Turner8065f0b2017-12-01 00:53:10 +000052 cl::Hidden, cl::desc("Verify loop info (time consuming)"));
Dan Gohman4dbb3012009-09-28 00:27:48 +000053
Chris Lattnerccf571a2002-01-31 00:42:27 +000054//===----------------------------------------------------------------------===//
Chris Lattner78dd56f2002-04-28 16:21:30 +000055// Loop implementation
Chris Lattnerccf571a2002-01-31 00:42:27 +000056//
Misha Brukman3845be22002-10-11 05:31:10 +000057
Pete Cooper016daa62015-05-13 01:12:06 +000058bool Loop::isLoopInvariant(const Value *V) const {
59 if (const Instruction *I = dyn_cast<Instruction>(V))
Chris Lattnerda24b9a2010-09-06 01:05:37 +000060 return !contains(I);
Sanjoy Das66a004a2017-09-20 01:12:09 +000061 return true; // All non-instructions are loop invariant
Dan Gohman80a99422009-07-13 22:02:44 +000062}
63
Pete Cooper016daa62015-05-13 01:12:06 +000064bool Loop::hasLoopInvariantOperands(const Instruction *I) const {
Pete Coopera264dc02015-05-13 22:19:13 +000065 return all_of(I->operands(), [this](Value *V) { return isLoopInvariant(V); });
Dan Gohman6f6d8642009-07-14 01:06:29 +000066}
67
Dan Gohmanc43e4792009-07-15 01:25:43 +000068bool Loop::makeLoopInvariant(Value *V, bool &Changed,
69 Instruction *InsertPt) const {
Dan Gohman6f6d8642009-07-14 01:06:29 +000070 if (Instruction *I = dyn_cast<Instruction>(V))
Dan Gohmanc43e4792009-07-15 01:25:43 +000071 return makeLoopInvariant(I, Changed, InsertPt);
Sanjoy Das66a004a2017-09-20 01:12:09 +000072 return true; // All non-instructions are loop-invariant.
Dan Gohman6f6d8642009-07-14 01:06:29 +000073}
74
Dan Gohmanc43e4792009-07-15 01:25:43 +000075bool Loop::makeLoopInvariant(Instruction *I, bool &Changed,
76 Instruction *InsertPt) const {
Dan Gohman6f6d8642009-07-14 01:06:29 +000077 // Test if the value is already loop-invariant.
78 if (isLoopInvariant(I))
79 return true;
Dan Gohman75d7d5e2011-12-14 23:49:11 +000080 if (!isSafeToSpeculativelyExecute(I))
Dan Gohman6f6d8642009-07-14 01:06:29 +000081 return false;
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +000082 if (I->mayReadFromMemory())
Dan Gohman6f6d8642009-07-14 01:06:29 +000083 return false;
David Majnemer654e1302015-07-31 17:58:14 +000084 // EH block instructions are immobile.
85 if (I->isEHPad())
Bill Wendlinga9ee09f2011-08-17 20:36:44 +000086 return false;
Dan Gohman6f6d8642009-07-14 01:06:29 +000087 // Determine the insertion point, unless one was given.
88 if (!InsertPt) {
89 BasicBlock *Preheader = getLoopPreheader();
90 // Without a preheader, hoisting is not feasible.
91 if (!Preheader)
92 return false;
93 InsertPt = Preheader->getTerminator();
94 }
95 // Don't hoist instructions with loop-variant operands.
Sanjay Patel960e5342016-01-15 00:08:10 +000096 for (Value *Operand : I->operands())
97 if (!makeLoopInvariant(Operand, Changed, InsertPt))
Dan Gohman6f6d8642009-07-14 01:06:29 +000098 return false;
Andrew Trickf898cbd2011-08-03 23:45:50 +000099
Dan Gohman6f6d8642009-07-14 01:06:29 +0000100 // Hoist.
101 I->moveBefore(InsertPt);
Igor Laevsky7310c682015-11-18 14:50:18 +0000102
103 // There is possibility of hoisting this instruction above some arbitrary
104 // condition. Any metadata defined on it can be control dependent on this
105 // condition. Conservatively strip it here so that we don't give any wrong
106 // information to the optimizer.
107 I->dropUnknownNonDebugMetadata();
108
Dan Gohmanc43e4792009-07-15 01:25:43 +0000109 Changed = true;
Dan Gohman6f6d8642009-07-14 01:06:29 +0000110 return true;
111}
112
Dan Gohman80a99422009-07-13 22:02:44 +0000113PHINode *Loop::getCanonicalInductionVariable() const {
114 BasicBlock *H = getHeader();
115
Craig Topper9f008862014-04-15 04:59:12 +0000116 BasicBlock *Incoming = nullptr, *Backedge = nullptr;
Dan Gohmanacafc612010-07-23 21:25:16 +0000117 pred_iterator PI = pred_begin(H);
Sanjoy Das66a004a2017-09-20 01:12:09 +0000118 assert(PI != pred_end(H) && "Loop must have at least one backedge!");
Dan Gohman80a99422009-07-13 22:02:44 +0000119 Backedge = *PI++;
Sanjoy Das66a004a2017-09-20 01:12:09 +0000120 if (PI == pred_end(H))
121 return nullptr; // dead loop
Dan Gohman80a99422009-07-13 22:02:44 +0000122 Incoming = *PI++;
Sanjoy Das66a004a2017-09-20 01:12:09 +0000123 if (PI != pred_end(H))
124 return nullptr; // multiple backedges?
Dan Gohman80a99422009-07-13 22:02:44 +0000125
126 if (contains(Incoming)) {
127 if (contains(Backedge))
Craig Topper9f008862014-04-15 04:59:12 +0000128 return nullptr;
Dan Gohman80a99422009-07-13 22:02:44 +0000129 std::swap(Incoming, Backedge);
130 } else if (!contains(Backedge))
Craig Topper9f008862014-04-15 04:59:12 +0000131 return nullptr;
Dan Gohman80a99422009-07-13 22:02:44 +0000132
133 // Loop over all of the PHI nodes, looking for a canonical indvar.
134 for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) {
135 PHINode *PN = cast<PHINode>(I);
136 if (ConstantInt *CI =
Sanjoy Das66a004a2017-09-20 01:12:09 +0000137 dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming)))
Craig Topper79ab6432017-07-06 18:39:47 +0000138 if (CI->isZero())
Dan Gohman80a99422009-07-13 22:02:44 +0000139 if (Instruction *Inc =
Sanjoy Das66a004a2017-09-20 01:12:09 +0000140 dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
141 if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN)
Dan Gohman80a99422009-07-13 22:02:44 +0000142 if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
Craig Topperca2c8762017-07-06 18:39:49 +0000143 if (CI->isOne())
Dan Gohman80a99422009-07-13 22:02:44 +0000144 return PN;
145 }
Craig Topper9f008862014-04-15 04:59:12 +0000146 return nullptr;
Dan Gohman80a99422009-07-13 22:02:44 +0000147}
148
Igor Laevsky04423cf2016-10-11 13:37:22 +0000149// Check that 'BB' doesn't have any uses outside of the 'L'
150static bool isBlockInLCSSAForm(const Loop &L, const BasicBlock &BB,
151 DominatorTree &DT) {
152 for (const Instruction &I : BB) {
153 // Tokens can't be used in PHI nodes and live-out tokens prevent loop
154 // optimizations, so for the purposes of considered LCSSA form, we
155 // can ignore them.
156 if (I.getType()->isTokenTy())
157 continue;
Andrew Kaylor123048d2015-12-18 18:12:35 +0000158
Igor Laevsky04423cf2016-10-11 13:37:22 +0000159 for (const Use &U : I.uses()) {
160 const Instruction *UI = cast<Instruction>(U.getUser());
161 const BasicBlock *UserBB = UI->getParent();
162 if (const PHINode *P = dyn_cast<PHINode>(UI))
163 UserBB = P->getIncomingBlock(U);
Dan Gohman80a99422009-07-13 22:02:44 +0000164
Igor Laevsky04423cf2016-10-11 13:37:22 +0000165 // Check the current block, as a fast-path, before checking whether
166 // the use is anywhere in the loop. Most values are used in the same
167 // block they are defined in. Also, blocks not reachable from the
168 // entry are special; uses in them don't need to go through PHIs.
169 if (UserBB != &BB && !L.contains(UserBB) &&
170 DT.isReachableFromEntry(UserBB))
171 return false;
Andrew Kaylor123048d2015-12-18 18:12:35 +0000172 }
Dan Gohman80a99422009-07-13 22:02:44 +0000173 }
Dan Gohman80a99422009-07-13 22:02:44 +0000174 return true;
175}
Dan Gohman1511f702009-07-16 16:16:23 +0000176
Igor Laevsky04423cf2016-10-11 13:37:22 +0000177bool Loop::isLCSSAForm(DominatorTree &DT) const {
178 // For each block we check that it doesn't have any uses outside of this loop.
179 return all_of(this->blocks(), [&](const BasicBlock *BB) {
180 return isBlockInLCSSAForm(*this, *BB, DT);
181 });
182}
Sanjoy Das683bf072015-12-08 00:13:21 +0000183
Igor Laevsky04423cf2016-10-11 13:37:22 +0000184bool Loop::isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const {
Davide Italiano362cc7b2017-01-08 22:22:09 +0000185 // For each block we check that it doesn't have any uses outside of its
186 // innermost loop. This process will transitively guarantee that the current
187 // loop and all of the nested loops are in LCSSA form.
Igor Laevsky04423cf2016-10-11 13:37:22 +0000188 return all_of(this->blocks(), [&](const BasicBlock *BB) {
189 return isBlockInLCSSAForm(*LI.getLoopFor(BB), *BB, DT);
190 });
Sanjoy Das683bf072015-12-08 00:13:21 +0000191}
192
Dan Gohman1511f702009-07-16 16:16:23 +0000193bool Loop::isLoopSimplifyForm() const {
Dan Gohmane3a17062009-11-05 19:21:41 +0000194 // Normal-form loops have a preheader, a single backedge, and all of their
195 // exits have all their predecessors inside the loop.
196 return getLoopPreheader() && getLoopLatch() && hasDedicatedExits();
197}
198
Sanjay Patel784b5e32016-01-14 23:23:04 +0000199// Routines that reform the loop CFG and split edges often fail on indirectbr.
Andrew Trick4442bfe2012-04-10 05:14:42 +0000200bool Loop::isSafeToClone() const {
James Molloy4f6fb952012-12-20 16:04:27 +0000201 // Return false if any loop blocks contain indirectbrs, or there are any calls
202 // to noduplicate functions.
Sanjay Patel960e5342016-01-15 00:08:10 +0000203 for (BasicBlock *BB : this->blocks()) {
204 if (isa<IndirectBrInst>(BB->getTerminator()))
Andrew Trick4442bfe2012-04-10 05:14:42 +0000205 return false;
Jakub Staszak9dca4b32013-11-13 20:18:38 +0000206
David Majnemer3d90bb72016-05-03 03:57:40 +0000207 for (Instruction &I : *BB)
208 if (auto CS = CallSite(&I))
209 if (CS.cannotDuplicate())
James Molloy4f6fb952012-12-20 16:04:27 +0000210 return false;
Andrew Trick4442bfe2012-04-10 05:14:42 +0000211 }
212 return true;
213}
214
Paul Redmond5fdf8362013-05-28 20:00:34 +0000215MDNode *Loop::getLoopID() const {
Craig Topper9f008862014-04-15 04:59:12 +0000216 MDNode *LoopID = nullptr;
Paul Redmond5fdf8362013-05-28 20:00:34 +0000217
Michael Kruse534c87d2018-09-17 18:40:29 +0000218 // Go through the latch blocks and check the terminator for the metadata.
219 SmallVector<BasicBlock *, 4> LatchesBlocks;
220 getLoopLatches(LatchesBlocks);
221 for (BasicBlock *BB : LatchesBlocks) {
Chandler Carruthedb12a82018-10-15 10:04:59 +0000222 Instruction *TI = BB->getTerminator();
Michael Kruse534c87d2018-09-17 18:40:29 +0000223 MDNode *MD = TI->getMetadata(LLVMContext::MD_loop);
Paul Redmond5fdf8362013-05-28 20:00:34 +0000224
Michael Kruse534c87d2018-09-17 18:40:29 +0000225 if (!MD)
226 return nullptr;
227
228 if (!LoopID)
229 LoopID = MD;
230 else if (MD != LoopID)
231 return nullptr;
Paul Redmond5fdf8362013-05-28 20:00:34 +0000232 }
233 if (!LoopID || LoopID->getNumOperands() == 0 ||
234 LoopID->getOperand(0) != LoopID)
Craig Topper9f008862014-04-15 04:59:12 +0000235 return nullptr;
Paul Redmond5fdf8362013-05-28 20:00:34 +0000236 return LoopID;
237}
238
239void Loop::setLoopID(MDNode *LoopID) const {
240 assert(LoopID && "Loop ID should not be null");
241 assert(LoopID->getNumOperands() > 0 && "Loop ID needs at least one operand");
242 assert(LoopID->getOperand(0) == LoopID && "Loop ID should refer to itself");
243
Xin Tongca023602017-01-15 21:17:52 +0000244 if (BasicBlock *Latch = getLoopLatch()) {
245 Latch->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID);
Paul Redmond5fdf8362013-05-28 20:00:34 +0000246 return;
247 }
248
Sanjoy Das66a004a2017-09-20 01:12:09 +0000249 assert(!getLoopLatch() &&
250 "The loop should have no single latch at this point");
Paul Redmond5fdf8362013-05-28 20:00:34 +0000251 BasicBlock *H = getHeader();
Sanjay Patel960e5342016-01-15 00:08:10 +0000252 for (BasicBlock *BB : this->blocks()) {
Chandler Carruthedb12a82018-10-15 10:04:59 +0000253 Instruction *TI = BB->getTerminator();
Chandler Carruth96fc1de2018-08-26 08:41:15 +0000254 for (BasicBlock *Successor : successors(TI)) {
Sanjay Patel960e5342016-01-15 00:08:10 +0000255 if (Successor == H)
Duncan P. N. Exon Smith1d15a9f2016-03-25 00:35:38 +0000256 TI->setMetadata(LLVMContext::MD_loop, LoopID);
Paul Redmond5fdf8362013-05-28 20:00:34 +0000257 }
258 }
259}
260
Hongbin Zheng73f65042017-10-15 07:31:02 +0000261void Loop::setLoopAlreadyUnrolled() {
262 MDNode *LoopID = getLoopID();
263 // First remove any existing loop unrolling metadata.
264 SmallVector<Metadata *, 4> MDs;
265 // Reserve first location for self reference to the LoopID metadata node.
266 MDs.push_back(nullptr);
267
268 if (LoopID) {
269 for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
270 bool IsUnrollMetadata = false;
271 MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
272 if (MD) {
273 const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
274 IsUnrollMetadata = S && S->getString().startswith("llvm.loop.unroll.");
275 }
276 if (!IsUnrollMetadata)
277 MDs.push_back(LoopID->getOperand(i));
278 }
279 }
280
281 // Add unroll(disable) metadata to disable future unrolling.
282 LLVMContext &Context = getHeader()->getContext();
283 SmallVector<Metadata *, 1> DisableOperands;
284 DisableOperands.push_back(MDString::get(Context, "llvm.loop.unroll.disable"));
285 MDNode *DisableNode = MDNode::get(Context, DisableOperands);
286 MDs.push_back(DisableNode);
287
288 MDNode *NewLoopID = MDNode::get(Context, MDs);
289 // Set operand 0 to refer to the loop id itself.
290 NewLoopID->replaceOperandWith(0, NewLoopID);
291 setLoopID(NewLoopID);
292}
293
Pekka Jaaskelainen0d237252013-02-13 18:08:57 +0000294bool Loop::isAnnotatedParallel() const {
Sanjay Patel6435c6e2016-01-17 23:18:05 +0000295 MDNode *DesiredLoopIdMetadata = getLoopID();
Pekka Jaaskelainen0d237252013-02-13 18:08:57 +0000296
Sanjay Patel6435c6e2016-01-17 23:18:05 +0000297 if (!DesiredLoopIdMetadata)
Sanjoy Das66a004a2017-09-20 01:12:09 +0000298 return false;
Pekka Jaaskelainen0d237252013-02-13 18:08:57 +0000299
300 // The loop branch contains the parallel loop metadata. In order to ensure
301 // that any parallel-loop-unaware optimization pass hasn't added loop-carried
302 // dependencies (thus converted the loop back to a sequential loop), check
303 // that all the memory instructions in the loop contain parallelism metadata
304 // that point to the same unique "loop id metadata" the loop branch does.
Sanjay Patel960e5342016-01-15 00:08:10 +0000305 for (BasicBlock *BB : this->blocks()) {
306 for (Instruction &I : *BB) {
307 if (!I.mayReadOrWriteMemory())
Pekka Jaaskelainen0d237252013-02-13 18:08:57 +0000308 continue;
309
Pekka Jaaskelainen0d237252013-02-13 18:08:57 +0000310 // The memory instruction can refer to the loop identifier metadata
311 // directly or indirectly through another list metadata (in case of
312 // nested parallel loops). The loop identifier metadata refers to
313 // itself so we can check both cases with the same routine.
Sanjay Patel6435c6e2016-01-17 23:18:05 +0000314 MDNode *LoopIdMD =
Sanjay Patel960e5342016-01-15 00:08:10 +0000315 I.getMetadata(LLVMContext::MD_mem_parallel_loop_access);
Jakub Staszak9dca4b32013-11-13 20:18:38 +0000316
Sanjay Patel6435c6e2016-01-17 23:18:05 +0000317 if (!LoopIdMD)
Jakub Staszak9dca4b32013-11-13 20:18:38 +0000318 return false;
319
Sanjay Patel6435c6e2016-01-17 23:18:05 +0000320 bool LoopIdMDFound = false;
321 for (const MDOperand &MDOp : LoopIdMD->operands()) {
322 if (MDOp == DesiredLoopIdMetadata) {
323 LoopIdMDFound = true;
Pekka Jaaskelainen0d237252013-02-13 18:08:57 +0000324 break;
325 }
326 }
327
Sanjay Patel6435c6e2016-01-17 23:18:05 +0000328 if (!LoopIdMDFound)
Pekka Jaaskelainen0d237252013-02-13 18:08:57 +0000329 return false;
330 }
331 }
332 return true;
333}
334
Sanjoy Das66a004a2017-09-20 01:12:09 +0000335DebugLoc Loop::getStartLoc() const { return getLocRange().getStart(); }
Amara Emerson0b402012016-11-08 11:18:59 +0000336
337Loop::LocRange Loop::getLocRange() const {
Hal Finkel2f688682016-05-25 21:42:37 +0000338 // If we have a debug location in the loop ID, then use it.
Amara Emerson0b402012016-11-08 11:18:59 +0000339 if (MDNode *LoopID = getLoopID()) {
340 DebugLoc Start;
341 // We use the first DebugLoc in the header as the start location of the loop
342 // and if there is a second DebugLoc in the header we use it as end location
343 // of the loop.
344 for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
345 if (DILocation *L = dyn_cast<DILocation>(LoopID->getOperand(i))) {
346 if (!Start)
347 Start = DebugLoc(L);
348 else
349 return LocRange(Start, DebugLoc(L));
350 }
351 }
352
353 if (Start)
354 return LocRange(Start);
355 }
Hal Finkel2f688682016-05-25 21:42:37 +0000356
357 // Try the pre-header first.
358 if (BasicBlock *PHeadBB = getLoopPreheader())
359 if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
Amara Emerson0b402012016-11-08 11:18:59 +0000360 return LocRange(DL);
Hal Finkel2f688682016-05-25 21:42:37 +0000361
362 // If we have no pre-header or there are no instructions with debug
363 // info in it, try the header.
364 if (BasicBlock *HeadBB = getHeader())
Amara Emerson0b402012016-11-08 11:18:59 +0000365 return LocRange(HeadBB->getTerminator()->getDebugLoc());
Hal Finkel2f688682016-05-25 21:42:37 +0000366
Amara Emerson0b402012016-11-08 11:18:59 +0000367 return LocRange();
Hal Finkel2f688682016-05-25 21:42:37 +0000368}
369
Aaron Ballman615eb472017-10-15 14:32:27 +0000370#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sanjoy Das66a004a2017-09-20 01:12:09 +0000371LLVM_DUMP_METHOD void Loop::dump() const { print(dbgs()); }
Sebastian Pop18c964d2016-07-27 05:02:17 +0000372
373LLVM_DUMP_METHOD void Loop::dumpVerbose() const {
Sanjoy Das66a004a2017-09-20 01:12:09 +0000374 print(dbgs(), /*Depth=*/0, /*Verbose=*/true);
Sebastian Pop18c964d2016-07-27 05:02:17 +0000375}
Manman Renc3366cc2012-09-06 19:55:56 +0000376#endif
Dan Gohmanc3f21372010-01-05 21:08:02 +0000377
Chris Lattner26750072002-07-27 01:12:17 +0000378//===----------------------------------------------------------------------===//
Andrew Trickd3530b92011-08-10 23:22:57 +0000379// UnloopUpdater implementation
380//
381
Benjamin Kramer4938edb2011-08-19 01:42:18 +0000382namespace {
Andrew Trickd3530b92011-08-10 23:22:57 +0000383/// Find the new parent loop for all blocks within the "unloop" whose last
384/// backedges has just been removed.
385class UnloopUpdater {
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000386 Loop &Unloop;
Andrew Trickd3530b92011-08-10 23:22:57 +0000387 LoopInfo *LI;
388
389 LoopBlocksDFS DFS;
390
391 // Map unloop's immediate subloops to their nearest reachable parents. Nested
392 // loops within these subloops will not change parents. However, an immediate
393 // subloop's new parent will be the nearest loop reachable from either its own
394 // exits *or* any of its nested loop's exits.
Sanjoy Das66a004a2017-09-20 01:12:09 +0000395 DenseMap<Loop *, Loop *> SubloopParents;
Andrew Trickd3530b92011-08-10 23:22:57 +0000396
397 // Flag the presence of an irreducible backedge whose destination is a block
398 // directly contained by the original unloop.
399 bool FoundIB;
400
401public:
Sanjoy Das66a004a2017-09-20 01:12:09 +0000402 UnloopUpdater(Loop *UL, LoopInfo *LInfo)
403 : Unloop(*UL), LI(LInfo), DFS(UL), FoundIB(false) {}
Andrew Trickd3530b92011-08-10 23:22:57 +0000404
405 void updateBlockParents();
406
Andrew Trickc12c30a2011-08-11 20:27:32 +0000407 void removeBlocksFromAncestors();
408
Andrew Trickd3530b92011-08-10 23:22:57 +0000409 void updateSubloopParents();
410
411protected:
412 Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop);
413};
Benjamin Kramer4938edb2011-08-19 01:42:18 +0000414} // end anonymous namespace
Andrew Trickd3530b92011-08-10 23:22:57 +0000415
Sanjay Patel784b5e32016-01-14 23:23:04 +0000416/// Update the parent loop for all blocks that are directly contained within the
417/// original "unloop".
Andrew Trickd3530b92011-08-10 23:22:57 +0000418void UnloopUpdater::updateBlockParents() {
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000419 if (Unloop.getNumBlocks()) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000420 // Perform a post order CFG traversal of all blocks within this loop,
Hiroshi Inoue713b5ba2017-07-09 05:54:44 +0000421 // propagating the nearest loop from successors to predecessors.
Andrew Trickd3530b92011-08-10 23:22:57 +0000422 LoopBlocksTraversal Traversal(DFS, LI);
Benjamin Krameraa209152016-06-26 17:27:42 +0000423 for (BasicBlock *POI : Traversal) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000424
Benjamin Krameraa209152016-06-26 17:27:42 +0000425 Loop *L = LI->getLoopFor(POI);
426 Loop *NL = getNearestLoop(POI, L);
Andrew Trickd3530b92011-08-10 23:22:57 +0000427
428 if (NL != L) {
429 // For reducible loops, NL is now an ancestor of Unloop.
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000430 assert((NL != &Unloop && (!NL || NL->contains(&Unloop))) &&
Andrew Trickd3530b92011-08-10 23:22:57 +0000431 "uninitialized successor");
Benjamin Krameraa209152016-06-26 17:27:42 +0000432 LI->changeLoopFor(POI, NL);
Sanjoy Das66a004a2017-09-20 01:12:09 +0000433 } else {
Andrew Trickd3530b92011-08-10 23:22:57 +0000434 // Or the current block is part of a subloop, in which case its parent
435 // is unchanged.
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000436 assert((FoundIB || Unloop.contains(L)) && "uninitialized successor");
Andrew Trickd3530b92011-08-10 23:22:57 +0000437 }
438 }
439 }
440 // Each irreducible loop within the unloop induces a round of iteration using
441 // the DFS result cached by Traversal.
442 bool Changed = FoundIB;
443 for (unsigned NIters = 0; Changed; ++NIters) {
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000444 assert(NIters < Unloop.getNumBlocks() && "runaway iterative algorithm");
Andrew Trickd3530b92011-08-10 23:22:57 +0000445
446 // Iterate over the postorder list of blocks, propagating the nearest loop
447 // from successors to predecessors as before.
448 Changed = false;
449 for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(),
Sanjoy Das66a004a2017-09-20 01:12:09 +0000450 POE = DFS.endPostorder();
451 POI != POE; ++POI) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000452
453 Loop *L = LI->getLoopFor(*POI);
454 Loop *NL = getNearestLoop(*POI, L);
455 if (NL != L) {
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000456 assert(NL != &Unloop && (!NL || NL->contains(&Unloop)) &&
Andrew Trickd3530b92011-08-10 23:22:57 +0000457 "uninitialized successor");
458 LI->changeLoopFor(*POI, NL);
459 Changed = true;
460 }
461 }
462 }
463}
464
Sanjay Patel784b5e32016-01-14 23:23:04 +0000465/// Remove unloop's blocks from all ancestors below their new parents.
Andrew Trickc12c30a2011-08-11 20:27:32 +0000466void UnloopUpdater::removeBlocksFromAncestors() {
Andrew Trick6b4d5782011-11-18 03:42:41 +0000467 // Remove all unloop's blocks (including those in nested subloops) from
468 // ancestors below the new parent loop.
Sanjoy Das66a004a2017-09-20 01:12:09 +0000469 for (Loop::block_iterator BI = Unloop.block_begin(), BE = Unloop.block_end();
470 BI != BE; ++BI) {
Andrew Trick6b4d5782011-11-18 03:42:41 +0000471 Loop *OuterParent = LI->getLoopFor(*BI);
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000472 if (Unloop.contains(OuterParent)) {
473 while (OuterParent->getParentLoop() != &Unloop)
Andrew Trick6b4d5782011-11-18 03:42:41 +0000474 OuterParent = OuterParent->getParentLoop();
475 OuterParent = SubloopParents[OuterParent];
476 }
Andrew Trickc12c30a2011-08-11 20:27:32 +0000477 // Remove blocks from former Ancestors except Unloop itself which will be
478 // deleted.
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000479 for (Loop *OldParent = Unloop.getParentLoop(); OldParent != OuterParent;
Andrew Trickc12c30a2011-08-11 20:27:32 +0000480 OldParent = OldParent->getParentLoop()) {
481 assert(OldParent && "new loop is not an ancestor of the original");
482 OldParent->removeBlockFromLoop(*BI);
483 }
484 }
485}
486
Sanjay Patel784b5e32016-01-14 23:23:04 +0000487/// Update the parent loop for all subloops directly nested within unloop.
Andrew Trickd3530b92011-08-10 23:22:57 +0000488void UnloopUpdater::updateSubloopParents() {
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000489 while (!Unloop.empty()) {
490 Loop *Subloop = *std::prev(Unloop.end());
491 Unloop.removeChildLoop(std::prev(Unloop.end()));
Andrew Trickd3530b92011-08-10 23:22:57 +0000492
493 assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop");
Benjamin Kramerf29db272012-08-22 15:37:57 +0000494 if (Loop *Parent = SubloopParents[Subloop])
495 Parent->addChildLoop(Subloop);
Andrew Trick147d9cd2011-08-26 03:06:34 +0000496 else
497 LI->addTopLevelLoop(Subloop);
Andrew Trickd3530b92011-08-10 23:22:57 +0000498 }
499}
500
Sanjay Patel784b5e32016-01-14 23:23:04 +0000501/// Return the nearest parent loop among this block's successors. If a successor
502/// is a subloop header, consider its parent to be the nearest parent of the
503/// subloop's exits.
Andrew Trickd3530b92011-08-10 23:22:57 +0000504///
505/// For subloop blocks, simply update SubloopParents and return NULL.
506Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) {
507
Andrew Trick266ab102011-08-11 17:54:58 +0000508 // Initially for blocks directly contained by Unloop, NearLoop == Unloop and
509 // is considered uninitialized.
Andrew Trickd3530b92011-08-10 23:22:57 +0000510 Loop *NearLoop = BBLoop;
511
Craig Topper9f008862014-04-15 04:59:12 +0000512 Loop *Subloop = nullptr;
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000513 if (NearLoop != &Unloop && Unloop.contains(NearLoop)) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000514 Subloop = NearLoop;
515 // Find the subloop ancestor that is directly contained within Unloop.
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000516 while (Subloop->getParentLoop() != &Unloop) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000517 Subloop = Subloop->getParentLoop();
518 assert(Subloop && "subloop is not an ancestor of the original loop");
519 }
520 // Get the current nearest parent of the Subloop exits, initially Unloop.
David Majnemer0a16c222016-08-11 21:15:00 +0000521 NearLoop = SubloopParents.insert({Subloop, &Unloop}).first->second;
Andrew Trickd3530b92011-08-10 23:22:57 +0000522 }
523
524 succ_iterator I = succ_begin(BB), E = succ_end(BB);
525 if (I == E) {
526 assert(!Subloop && "subloop blocks must have a successor");
Craig Topper9f008862014-04-15 04:59:12 +0000527 NearLoop = nullptr; // unloop blocks may now exit the function.
Andrew Trickd3530b92011-08-10 23:22:57 +0000528 }
529 for (; I != E; ++I) {
530 if (*I == BB)
531 continue; // self loops are uninteresting
532
533 Loop *L = LI->getLoopFor(*I);
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000534 if (L == &Unloop) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000535 // This successor has not been processed. This path must lead to an
536 // irreducible backedge.
537 assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB");
538 FoundIB = true;
539 }
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000540 if (L != &Unloop && Unloop.contains(L)) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000541 // Successor is in a subloop.
542 if (Subloop)
543 continue; // Branching within subloops. Ignore it.
544
545 // BB branches from the original into a subloop header.
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000546 assert(L->getParentLoop() == &Unloop && "cannot skip into nested loops");
Andrew Trickd3530b92011-08-10 23:22:57 +0000547
548 // Get the current nearest parent of the Subloop's exits.
549 L = SubloopParents[L];
550 // L could be Unloop if the only exit was an irreducible backedge.
551 }
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000552 if (L == &Unloop) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000553 continue;
554 }
555 // Handle critical edges from Unloop into a sibling loop.
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000556 if (L && !L->contains(&Unloop)) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000557 L = L->getParentLoop();
558 }
559 // Remember the nearest parent loop among successors or subloop exits.
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000560 if (NearLoop == &Unloop || !NearLoop || NearLoop->contains(L))
Andrew Trickd3530b92011-08-10 23:22:57 +0000561 NearLoop = L;
562 }
563 if (Subloop) {
564 SubloopParents[Subloop] = NearLoop;
565 return BBLoop;
566 }
567 return NearLoop;
568}
569
Sanjoy Das66a004a2017-09-20 01:12:09 +0000570LoopInfo::LoopInfo(const DomTreeBase<BasicBlock> &DomTree) { analyze(DomTree); }
Cong Hou9b4f6b22015-07-16 23:23:35 +0000571
Chandler Carruthca68a3e2017-01-15 06:32:49 +0000572bool LoopInfo::invalidate(Function &F, const PreservedAnalyses &PA,
573 FunctionAnalysisManager::Invalidator &) {
574 // Check whether the analysis, all analyses on functions, or the function's
575 // CFG have been preserved.
576 auto PAC = PA.getChecker<LoopAnalysis>();
577 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
578 PAC.preservedSet<CFGAnalyses>());
579}
580
Sanjoy Das388b0122017-09-22 01:47:41 +0000581void LoopInfo::erase(Loop *Unloop) {
Sanjoy Das76ab2322017-09-19 23:19:00 +0000582 assert(!Unloop->isInvalid() && "Loop has already been erased!");
Andrew Trickd3530b92011-08-10 23:22:57 +0000583
Sanjoy Dasdef17292017-09-28 02:45:42 +0000584 auto InvalidateOnExit = make_scope_exit([&]() { destroy(Unloop); });
Sanjoy Das09613b12017-09-20 02:31:57 +0000585
Andrew Trickd3530b92011-08-10 23:22:57 +0000586 // First handle the special case of no parent loop to simplify the algorithm.
587 if (!Unloop->getParentLoop()) {
588 // Since BBLoop had no parent, Unloop blocks are no longer in a loop.
589 for (Loop::block_iterator I = Unloop->block_begin(),
Chandler Carruth691addc2015-01-18 01:25:51 +0000590 E = Unloop->block_end();
591 I != E; ++I) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000592
593 // Don't reparent blocks in subloops.
594 if (getLoopFor(*I) != Unloop)
595 continue;
596
597 // Blocks no longer have a parent but are still referenced by Unloop until
598 // the Unloop object is deleted.
Chandler Carruth691addc2015-01-18 01:25:51 +0000599 changeLoopFor(*I, nullptr);
Andrew Trickd3530b92011-08-10 23:22:57 +0000600 }
601
602 // Remove the loop from the top-level LoopInfo object.
Chandler Carruth691addc2015-01-18 01:25:51 +0000603 for (iterator I = begin();; ++I) {
604 assert(I != end() && "Couldn't find loop");
Andrew Trickd3530b92011-08-10 23:22:57 +0000605 if (*I == Unloop) {
Chandler Carruth691addc2015-01-18 01:25:51 +0000606 removeLoop(I);
Andrew Trickd3530b92011-08-10 23:22:57 +0000607 break;
608 }
609 }
610
611 // Move all of the subloops to the top-level.
612 while (!Unloop->empty())
Chandler Carruth691addc2015-01-18 01:25:51 +0000613 addTopLevelLoop(Unloop->removeChildLoop(std::prev(Unloop->end())));
Andrew Trickd3530b92011-08-10 23:22:57 +0000614
615 return;
616 }
617
618 // Update the parent loop for all blocks within the loop. Blocks within
619 // subloops will not change parents.
620 UnloopUpdater Updater(Unloop, this);
621 Updater.updateBlockParents();
622
Andrew Trickc12c30a2011-08-11 20:27:32 +0000623 // Remove blocks from former ancestor loops.
624 Updater.removeBlocksFromAncestors();
Andrew Trickd3530b92011-08-10 23:22:57 +0000625
626 // Add direct subloops as children in their new parent loop.
627 Updater.updateSubloopParents();
628
629 // Remove unloop from its parent loop.
630 Loop *ParentLoop = Unloop->getParentLoop();
Duncan Sandsa41634e2011-08-12 14:54:45 +0000631 for (Loop::iterator I = ParentLoop->begin();; ++I) {
632 assert(I != ParentLoop->end() && "Couldn't find loop");
Andrew Trickd3530b92011-08-10 23:22:57 +0000633 if (*I == Unloop) {
634 ParentLoop->removeChildLoop(I);
635 break;
636 }
637 }
638}
639
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000640AnalysisKey LoopAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000641
Sean Silva36e0d012016-08-09 00:28:15 +0000642LoopInfo LoopAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +0000643 // FIXME: Currently we create a LoopInfo from scratch for every function.
644 // This may prove to be too wasteful due to deallocating and re-allocating
645 // memory each time for the underlying map and vector datastructures. At some
646 // point it may prove worthwhile to use a freelist and recycle LoopInfo
647 // objects. I don't want to add that kind of complexity until the scope of
648 // the problem is better understood.
649 LoopInfo LI;
Chandler Carruthb47f8012016-03-11 11:05:24 +0000650 LI.analyze(AM.getResult<DominatorTreeAnalysis>(F));
Richard Trieu6ae37962015-04-30 23:07:00 +0000651 return LI;
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +0000652}
653
654PreservedAnalyses LoopPrinterPass::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000655 FunctionAnalysisManager &AM) {
Chandler Carruthb47f8012016-03-11 11:05:24 +0000656 AM.getResult<LoopAnalysis>(F).print(OS);
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +0000657 return PreservedAnalyses::all();
658}
659
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000660void llvm::printLoop(Loop &L, raw_ostream &OS, const std::string &Banner) {
Fedor Sergeev3b459c32017-12-01 18:33:58 +0000661
662 if (forcePrintModuleIR()) {
663 // handling -print-module-scope
664 OS << Banner << " (loop: ";
665 L.getHeader()->printAsOperand(OS, false);
666 OS << ")\n";
667
668 // printing whole module
669 OS << *L.getHeader()->getModule();
670 return;
671 }
672
Justin Bognerc2b98f02015-11-04 22:24:08 +0000673 OS << Banner;
Fedor Sergeev61975b42017-11-22 20:59:53 +0000674
675 auto *PreHeader = L.getLoopPreheader();
676 if (PreHeader) {
677 OS << "\n; Preheader:";
678 PreHeader->print(OS);
679 OS << "\n; Loop:";
680 }
681
Justin Bognerc2b98f02015-11-04 22:24:08 +0000682 for (auto *Block : L.blocks())
683 if (Block)
684 Block->print(OS);
685 else
686 OS << "Printing <null> block";
Fedor Sergeev61975b42017-11-22 20:59:53 +0000687
688 SmallVector<BasicBlock *, 8> ExitBlocks;
689 L.getExitBlocks(ExitBlocks);
690 if (!ExitBlocks.empty()) {
691 OS << "\n; Exit blocks";
692 for (auto *Block : ExitBlocks)
693 if (Block)
694 Block->print(OS);
695 else
696 OS << "Printing <null> block";
697 }
Justin Bognerc2b98f02015-11-04 22:24:08 +0000698}
699
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000700//===----------------------------------------------------------------------===//
701// LoopInfo implementation
702//
703
704char LoopInfoWrapperPass::ID = 0;
705INITIALIZE_PASS_BEGIN(LoopInfoWrapperPass, "loops", "Natural Loop Information",
706 true, true)
707INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
708INITIALIZE_PASS_END(LoopInfoWrapperPass, "loops", "Natural Loop Information",
709 true, true)
710
711bool LoopInfoWrapperPass::runOnFunction(Function &) {
712 releaseMemory();
Cong Houd2c1d912015-07-16 18:23:57 +0000713 LI.analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree());
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000714 return false;
715}
716
717void LoopInfoWrapperPass::verifyAnalysis() const {
718 // LoopInfoWrapperPass is a FunctionPass, but verifying every loop in the
719 // function each time verifyAnalysis is called is very expensive. The
Dan Gohman4dbb3012009-09-28 00:27:48 +0000720 // -verify-loop-info option can enable this. In order to perform some
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000721 // checking by default, LoopPass has been taught to call verifyLoop manually
722 // during loop pass sequences.
Michael Zolotukhine0b2d972016-08-31 19:26:19 +0000723 if (VerifyLoopInfo) {
Serge Pavloved5eb932017-01-15 10:23:18 +0000724 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
725 LI.verify(DT);
Michael Zolotukhine0b2d972016-08-31 19:26:19 +0000726 }
Dan Gohman3ddbc242009-09-08 15:45:00 +0000727}
728
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000729void LoopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerc8e66542002-04-27 06:56:12 +0000730 AU.setPreservesAll();
Chandler Carruth73523022014-01-13 13:07:17 +0000731 AU.addRequired<DominatorTreeWrapperPass>();
Chris Lattnerccf571a2002-01-31 00:42:27 +0000732}
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000733
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000734void LoopInfoWrapperPass::print(raw_ostream &OS, const Module *) const {
Chandler Carruth691addc2015-01-18 01:25:51 +0000735 LI.print(OS);
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000736}
737
Sean Silvae3c18a52016-07-19 23:54:23 +0000738PreservedAnalyses LoopVerifierPass::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000739 FunctionAnalysisManager &AM) {
Sean Silvae3c18a52016-07-19 23:54:23 +0000740 LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
Michael Zolotukhine0b2d972016-08-31 19:26:19 +0000741 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
742 LI.verify(DT);
Sean Silvae3c18a52016-07-19 23:54:23 +0000743 return PreservedAnalyses::all();
744}
745
Andrew Trick78b40c32011-08-10 01:59:05 +0000746//===----------------------------------------------------------------------===//
747// LoopBlocksDFS implementation
748//
749
750/// Traverse the loop blocks and store the DFS result.
751/// Useful for clients that just want the final DFS result and don't need to
752/// visit blocks during the initial traversal.
753void LoopBlocksDFS::perform(LoopInfo *LI) {
754 LoopBlocksTraversal Traversal(*this, LI);
755 for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(),
Sanjoy Das66a004a2017-09-20 01:12:09 +0000756 POE = Traversal.end();
757 POI != POE; ++POI)
758 ;
Andrew Trick78b40c32011-08-10 01:59:05 +0000759}