blob: 07f72addc814e7ca5421185dada3adc143ed8b71 [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"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/Instructions.h"
Philip Reames5a3f5f72014-10-21 00:13:20 +000030#include "llvm/IR/LLVMContext.h"
Pekka Jaaskelainen0d237252013-02-13 18:08:57 +000031#include "llvm/IR/Metadata.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000032#include "llvm/IR/PassManager.h"
Dan Gohman4dbb3012009-09-28 00:27:48 +000033#include "llvm/Support/CommandLine.h"
Dan Gohmanc3f21372010-01-05 21:08:02 +000034#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000035#include "llvm/Support/raw_ostream.h"
Chris Lattner6de99422001-11-26 18:41:20 +000036#include <algorithm>
Chris Lattner55b7ef52004-04-12 20:26:17 +000037using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000038
Andrew Trickcda51d42012-06-20 03:42:09 +000039// Explicitly instantiate methods in LoopInfoImpl.h for IR-level Loops.
40template class llvm::LoopBase<BasicBlock, Loop>;
41template class llvm::LoopInfoBase<BasicBlock, Loop>;
42
Dan Gohman4dbb3012009-09-28 00:27:48 +000043// Always verify loopinfo if expensive checking is enabled.
Filipe Cabecinhas0da99372016-04-29 15:22:48 +000044#ifdef EXPENSIVE_CHECKS
Serge Pavlov69b3ff92017-01-24 05:52:07 +000045bool llvm::VerifyLoopInfo = true;
Dan Gohman4dbb3012009-09-28 00:27:48 +000046#else
Serge Pavlov69b3ff92017-01-24 05:52:07 +000047bool llvm::VerifyLoopInfo = false;
Dan Gohman4dbb3012009-09-28 00:27:48 +000048#endif
Sanjoy Das66a004a2017-09-20 01:12:09 +000049static cl::opt<bool, true>
50 VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo),
Zachary Turner8065f0b2017-12-01 00:53:10 +000051 cl::Hidden, cl::desc("Verify loop info (time consuming)"));
Dan Gohman4dbb3012009-09-28 00:27:48 +000052
Chris Lattnerccf571a2002-01-31 00:42:27 +000053//===----------------------------------------------------------------------===//
Chris Lattner78dd56f2002-04-28 16:21:30 +000054// Loop implementation
Chris Lattnerccf571a2002-01-31 00:42:27 +000055//
Misha Brukman3845be22002-10-11 05:31:10 +000056
Pete Cooper016daa62015-05-13 01:12:06 +000057bool Loop::isLoopInvariant(const Value *V) const {
58 if (const Instruction *I = dyn_cast<Instruction>(V))
Chris Lattnerda24b9a2010-09-06 01:05:37 +000059 return !contains(I);
Sanjoy Das66a004a2017-09-20 01:12:09 +000060 return true; // All non-instructions are loop invariant
Dan Gohman80a99422009-07-13 22:02:44 +000061}
62
Pete Cooper016daa62015-05-13 01:12:06 +000063bool Loop::hasLoopInvariantOperands(const Instruction *I) const {
Pete Coopera264dc02015-05-13 22:19:13 +000064 return all_of(I->operands(), [this](Value *V) { return isLoopInvariant(V); });
Dan Gohman6f6d8642009-07-14 01:06:29 +000065}
66
Dan Gohmanc43e4792009-07-15 01:25:43 +000067bool Loop::makeLoopInvariant(Value *V, bool &Changed,
68 Instruction *InsertPt) const {
Dan Gohman6f6d8642009-07-14 01:06:29 +000069 if (Instruction *I = dyn_cast<Instruction>(V))
Dan Gohmanc43e4792009-07-15 01:25:43 +000070 return makeLoopInvariant(I, Changed, InsertPt);
Sanjoy Das66a004a2017-09-20 01:12:09 +000071 return true; // All non-instructions are loop-invariant.
Dan Gohman6f6d8642009-07-14 01:06:29 +000072}
73
Dan Gohmanc43e4792009-07-15 01:25:43 +000074bool Loop::makeLoopInvariant(Instruction *I, bool &Changed,
75 Instruction *InsertPt) const {
Dan Gohman6f6d8642009-07-14 01:06:29 +000076 // Test if the value is already loop-invariant.
77 if (isLoopInvariant(I))
78 return true;
Dan Gohman75d7d5e2011-12-14 23:49:11 +000079 if (!isSafeToSpeculativelyExecute(I))
Dan Gohman6f6d8642009-07-14 01:06:29 +000080 return false;
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +000081 if (I->mayReadFromMemory())
Dan Gohman6f6d8642009-07-14 01:06:29 +000082 return false;
David Majnemer654e1302015-07-31 17:58:14 +000083 // EH block instructions are immobile.
84 if (I->isEHPad())
Bill Wendlinga9ee09f2011-08-17 20:36:44 +000085 return false;
Dan Gohman6f6d8642009-07-14 01:06:29 +000086 // 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.
Sanjay Patel960e5342016-01-15 00:08:10 +000095 for (Value *Operand : I->operands())
96 if (!makeLoopInvariant(Operand, Changed, InsertPt))
Dan Gohman6f6d8642009-07-14 01:06:29 +000097 return false;
Andrew Trickf898cbd2011-08-03 23:45:50 +000098
Dan Gohman6f6d8642009-07-14 01:06:29 +000099 // Hoist.
100 I->moveBefore(InsertPt);
Igor Laevsky7310c682015-11-18 14:50:18 +0000101
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
Dan Gohmanc43e4792009-07-15 01:25:43 +0000108 Changed = true;
Dan Gohman6f6d8642009-07-14 01:06:29 +0000109 return true;
110}
111
Dan Gohman80a99422009-07-13 22:02:44 +0000112PHINode *Loop::getCanonicalInductionVariable() const {
113 BasicBlock *H = getHeader();
114
Craig Topper9f008862014-04-15 04:59:12 +0000115 BasicBlock *Incoming = nullptr, *Backedge = nullptr;
Dan Gohmanacafc612010-07-23 21:25:16 +0000116 pred_iterator PI = pred_begin(H);
Sanjoy Das66a004a2017-09-20 01:12:09 +0000117 assert(PI != pred_end(H) && "Loop must have at least one backedge!");
Dan Gohman80a99422009-07-13 22:02:44 +0000118 Backedge = *PI++;
Sanjoy Das66a004a2017-09-20 01:12:09 +0000119 if (PI == pred_end(H))
120 return nullptr; // dead loop
Dan Gohman80a99422009-07-13 22:02:44 +0000121 Incoming = *PI++;
Sanjoy Das66a004a2017-09-20 01:12:09 +0000122 if (PI != pred_end(H))
123 return nullptr; // multiple backedges?
Dan Gohman80a99422009-07-13 22:02:44 +0000124
125 if (contains(Incoming)) {
126 if (contains(Backedge))
Craig Topper9f008862014-04-15 04:59:12 +0000127 return nullptr;
Dan Gohman80a99422009-07-13 22:02:44 +0000128 std::swap(Incoming, Backedge);
129 } else if (!contains(Backedge))
Craig Topper9f008862014-04-15 04:59:12 +0000130 return nullptr;
Dan Gohman80a99422009-07-13 22:02:44 +0000131
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 =
Sanjoy Das66a004a2017-09-20 01:12:09 +0000136 dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming)))
Craig Topper79ab6432017-07-06 18:39:47 +0000137 if (CI->isZero())
Dan Gohman80a99422009-07-13 22:02:44 +0000138 if (Instruction *Inc =
Sanjoy Das66a004a2017-09-20 01:12:09 +0000139 dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
140 if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN)
Dan Gohman80a99422009-07-13 22:02:44 +0000141 if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
Craig Topperca2c8762017-07-06 18:39:49 +0000142 if (CI->isOne())
Dan Gohman80a99422009-07-13 22:02:44 +0000143 return PN;
144 }
Craig Topper9f008862014-04-15 04:59:12 +0000145 return nullptr;
Dan Gohman80a99422009-07-13 22:02:44 +0000146}
147
Igor Laevsky04423cf2016-10-11 13:37:22 +0000148// Check that 'BB' doesn't have any uses outside of the 'L'
149static 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;
Andrew Kaylor123048d2015-12-18 18:12:35 +0000157
Igor Laevsky04423cf2016-10-11 13:37:22 +0000158 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);
Dan Gohman80a99422009-07-13 22:02:44 +0000163
Igor Laevsky04423cf2016-10-11 13:37:22 +0000164 // 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;
Andrew Kaylor123048d2015-12-18 18:12:35 +0000171 }
Dan Gohman80a99422009-07-13 22:02:44 +0000172 }
Dan Gohman80a99422009-07-13 22:02:44 +0000173 return true;
174}
Dan Gohman1511f702009-07-16 16:16:23 +0000175
Igor Laevsky04423cf2016-10-11 13:37:22 +0000176bool 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}
Sanjoy Das683bf072015-12-08 00:13:21 +0000182
Igor Laevsky04423cf2016-10-11 13:37:22 +0000183bool Loop::isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const {
Davide Italiano362cc7b2017-01-08 22:22:09 +0000184 // 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.
Igor Laevsky04423cf2016-10-11 13:37:22 +0000187 return all_of(this->blocks(), [&](const BasicBlock *BB) {
188 return isBlockInLCSSAForm(*LI.getLoopFor(BB), *BB, DT);
189 });
Sanjoy Das683bf072015-12-08 00:13:21 +0000190}
191
Dan Gohman1511f702009-07-16 16:16:23 +0000192bool Loop::isLoopSimplifyForm() const {
Dan Gohmane3a17062009-11-05 19:21:41 +0000193 // 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
Sanjay Patel784b5e32016-01-14 23:23:04 +0000198// Routines that reform the loop CFG and split edges often fail on indirectbr.
Andrew Trick4442bfe2012-04-10 05:14:42 +0000199bool Loop::isSafeToClone() const {
James Molloy4f6fb952012-12-20 16:04:27 +0000200 // Return false if any loop blocks contain indirectbrs, or there are any calls
201 // to noduplicate functions.
Sanjay Patel960e5342016-01-15 00:08:10 +0000202 for (BasicBlock *BB : this->blocks()) {
203 if (isa<IndirectBrInst>(BB->getTerminator()))
Andrew Trick4442bfe2012-04-10 05:14:42 +0000204 return false;
Jakub Staszak9dca4b32013-11-13 20:18:38 +0000205
David Majnemer3d90bb72016-05-03 03:57:40 +0000206 for (Instruction &I : *BB)
207 if (auto CS = CallSite(&I))
208 if (CS.cannotDuplicate())
James Molloy4f6fb952012-12-20 16:04:27 +0000209 return false;
Andrew Trick4442bfe2012-04-10 05:14:42 +0000210 }
211 return true;
212}
213
Paul Redmond5fdf8362013-05-28 20:00:34 +0000214MDNode *Loop::getLoopID() const {
Craig Topper9f008862014-04-15 04:59:12 +0000215 MDNode *LoopID = nullptr;
Paul Redmond5fdf8362013-05-28 20:00:34 +0000216
Michael Kruse534c87d2018-09-17 18:40:29 +0000217 // 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 TerminatorInst *TI = BB->getTerminator();
222 MDNode *MD = TI->getMetadata(LLVMContext::MD_loop);
Paul Redmond5fdf8362013-05-28 20:00:34 +0000223
Michael Kruse534c87d2018-09-17 18:40:29 +0000224 if (!MD)
225 return nullptr;
226
227 if (!LoopID)
228 LoopID = MD;
229 else if (MD != LoopID)
230 return nullptr;
Paul Redmond5fdf8362013-05-28 20:00:34 +0000231 }
232 if (!LoopID || LoopID->getNumOperands() == 0 ||
233 LoopID->getOperand(0) != LoopID)
Craig Topper9f008862014-04-15 04:59:12 +0000234 return nullptr;
Paul Redmond5fdf8362013-05-28 20:00:34 +0000235 return LoopID;
236}
237
238void Loop::setLoopID(MDNode *LoopID) const {
239 assert(LoopID && "Loop ID should not be null");
240 assert(LoopID->getNumOperands() > 0 && "Loop ID needs at least one operand");
241 assert(LoopID->getOperand(0) == LoopID && "Loop ID should refer to itself");
242
Xin Tongca023602017-01-15 21:17:52 +0000243 if (BasicBlock *Latch = getLoopLatch()) {
244 Latch->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID);
Paul Redmond5fdf8362013-05-28 20:00:34 +0000245 return;
246 }
247
Sanjoy Das66a004a2017-09-20 01:12:09 +0000248 assert(!getLoopLatch() &&
249 "The loop should have no single latch at this point");
Paul Redmond5fdf8362013-05-28 20:00:34 +0000250 BasicBlock *H = getHeader();
Sanjay Patel960e5342016-01-15 00:08:10 +0000251 for (BasicBlock *BB : this->blocks()) {
252 TerminatorInst *TI = BB->getTerminator();
Chandler Carruth96fc1de2018-08-26 08:41:15 +0000253 for (BasicBlock *Successor : successors(TI)) {
Sanjay Patel960e5342016-01-15 00:08:10 +0000254 if (Successor == H)
Duncan P. N. Exon Smith1d15a9f2016-03-25 00:35:38 +0000255 TI->setMetadata(LLVMContext::MD_loop, LoopID);
Paul Redmond5fdf8362013-05-28 20:00:34 +0000256 }
257 }
258}
259
Hongbin Zheng73f65042017-10-15 07:31:02 +0000260void Loop::setLoopAlreadyUnrolled() {
261 MDNode *LoopID = getLoopID();
262 // First remove any existing loop unrolling metadata.
263 SmallVector<Metadata *, 4> MDs;
264 // Reserve first location for self reference to the LoopID metadata node.
265 MDs.push_back(nullptr);
266
267 if (LoopID) {
268 for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
269 bool IsUnrollMetadata = false;
270 MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
271 if (MD) {
272 const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
273 IsUnrollMetadata = S && S->getString().startswith("llvm.loop.unroll.");
274 }
275 if (!IsUnrollMetadata)
276 MDs.push_back(LoopID->getOperand(i));
277 }
278 }
279
280 // Add unroll(disable) metadata to disable future unrolling.
281 LLVMContext &Context = getHeader()->getContext();
282 SmallVector<Metadata *, 1> DisableOperands;
283 DisableOperands.push_back(MDString::get(Context, "llvm.loop.unroll.disable"));
284 MDNode *DisableNode = MDNode::get(Context, DisableOperands);
285 MDs.push_back(DisableNode);
286
287 MDNode *NewLoopID = MDNode::get(Context, MDs);
288 // Set operand 0 to refer to the loop id itself.
289 NewLoopID->replaceOperandWith(0, NewLoopID);
290 setLoopID(NewLoopID);
291}
292
Pekka Jaaskelainen0d237252013-02-13 18:08:57 +0000293bool Loop::isAnnotatedParallel() const {
Sanjay Patel6435c6e2016-01-17 23:18:05 +0000294 MDNode *DesiredLoopIdMetadata = getLoopID();
Pekka Jaaskelainen0d237252013-02-13 18:08:57 +0000295
Sanjay Patel6435c6e2016-01-17 23:18:05 +0000296 if (!DesiredLoopIdMetadata)
Sanjoy Das66a004a2017-09-20 01:12:09 +0000297 return false;
Pekka Jaaskelainen0d237252013-02-13 18:08:57 +0000298
299 // The loop branch contains the parallel loop metadata. In order to ensure
300 // that any parallel-loop-unaware optimization pass hasn't added loop-carried
301 // dependencies (thus converted the loop back to a sequential loop), check
302 // that all the memory instructions in the loop contain parallelism metadata
303 // that point to the same unique "loop id metadata" the loop branch does.
Sanjay Patel960e5342016-01-15 00:08:10 +0000304 for (BasicBlock *BB : this->blocks()) {
305 for (Instruction &I : *BB) {
306 if (!I.mayReadOrWriteMemory())
Pekka Jaaskelainen0d237252013-02-13 18:08:57 +0000307 continue;
308
Pekka Jaaskelainen0d237252013-02-13 18:08:57 +0000309 // The memory instruction can refer to the loop identifier metadata
310 // directly or indirectly through another list metadata (in case of
311 // nested parallel loops). The loop identifier metadata refers to
312 // itself so we can check both cases with the same routine.
Sanjay Patel6435c6e2016-01-17 23:18:05 +0000313 MDNode *LoopIdMD =
Sanjay Patel960e5342016-01-15 00:08:10 +0000314 I.getMetadata(LLVMContext::MD_mem_parallel_loop_access);
Jakub Staszak9dca4b32013-11-13 20:18:38 +0000315
Sanjay Patel6435c6e2016-01-17 23:18:05 +0000316 if (!LoopIdMD)
Jakub Staszak9dca4b32013-11-13 20:18:38 +0000317 return false;
318
Sanjay Patel6435c6e2016-01-17 23:18:05 +0000319 bool LoopIdMDFound = false;
320 for (const MDOperand &MDOp : LoopIdMD->operands()) {
321 if (MDOp == DesiredLoopIdMetadata) {
322 LoopIdMDFound = true;
Pekka Jaaskelainen0d237252013-02-13 18:08:57 +0000323 break;
324 }
325 }
326
Sanjay Patel6435c6e2016-01-17 23:18:05 +0000327 if (!LoopIdMDFound)
Pekka Jaaskelainen0d237252013-02-13 18:08:57 +0000328 return false;
329 }
330 }
331 return true;
332}
333
Sanjoy Das66a004a2017-09-20 01:12:09 +0000334DebugLoc Loop::getStartLoc() const { return getLocRange().getStart(); }
Amara Emerson0b402012016-11-08 11:18:59 +0000335
336Loop::LocRange Loop::getLocRange() const {
Hal Finkel2f688682016-05-25 21:42:37 +0000337 // If we have a debug location in the loop ID, then use it.
Amara Emerson0b402012016-11-08 11:18:59 +0000338 if (MDNode *LoopID = getLoopID()) {
339 DebugLoc Start;
340 // We use the first DebugLoc in the header as the start location of the loop
341 // and if there is a second DebugLoc in the header we use it as end location
342 // of the loop.
343 for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
344 if (DILocation *L = dyn_cast<DILocation>(LoopID->getOperand(i))) {
345 if (!Start)
346 Start = DebugLoc(L);
347 else
348 return LocRange(Start, DebugLoc(L));
349 }
350 }
351
352 if (Start)
353 return LocRange(Start);
354 }
Hal Finkel2f688682016-05-25 21:42:37 +0000355
356 // Try the pre-header first.
357 if (BasicBlock *PHeadBB = getLoopPreheader())
358 if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
Amara Emerson0b402012016-11-08 11:18:59 +0000359 return LocRange(DL);
Hal Finkel2f688682016-05-25 21:42:37 +0000360
361 // If we have no pre-header or there are no instructions with debug
362 // info in it, try the header.
363 if (BasicBlock *HeadBB = getHeader())
Amara Emerson0b402012016-11-08 11:18:59 +0000364 return LocRange(HeadBB->getTerminator()->getDebugLoc());
Hal Finkel2f688682016-05-25 21:42:37 +0000365
Amara Emerson0b402012016-11-08 11:18:59 +0000366 return LocRange();
Hal Finkel2f688682016-05-25 21:42:37 +0000367}
368
Aaron Ballman615eb472017-10-15 14:32:27 +0000369#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sanjoy Das66a004a2017-09-20 01:12:09 +0000370LLVM_DUMP_METHOD void Loop::dump() const { print(dbgs()); }
Sebastian Pop18c964d2016-07-27 05:02:17 +0000371
372LLVM_DUMP_METHOD void Loop::dumpVerbose() const {
Sanjoy Das66a004a2017-09-20 01:12:09 +0000373 print(dbgs(), /*Depth=*/0, /*Verbose=*/true);
Sebastian Pop18c964d2016-07-27 05:02:17 +0000374}
Manman Renc3366cc2012-09-06 19:55:56 +0000375#endif
Dan Gohmanc3f21372010-01-05 21:08:02 +0000376
Chris Lattner26750072002-07-27 01:12:17 +0000377//===----------------------------------------------------------------------===//
Andrew Trickd3530b92011-08-10 23:22:57 +0000378// UnloopUpdater implementation
379//
380
Benjamin Kramer4938edb2011-08-19 01:42:18 +0000381namespace {
Andrew Trickd3530b92011-08-10 23:22:57 +0000382/// Find the new parent loop for all blocks within the "unloop" whose last
383/// backedges has just been removed.
384class UnloopUpdater {
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000385 Loop &Unloop;
Andrew Trickd3530b92011-08-10 23:22:57 +0000386 LoopInfo *LI;
387
388 LoopBlocksDFS DFS;
389
390 // Map unloop's immediate subloops to their nearest reachable parents. Nested
391 // loops within these subloops will not change parents. However, an immediate
392 // subloop's new parent will be the nearest loop reachable from either its own
393 // exits *or* any of its nested loop's exits.
Sanjoy Das66a004a2017-09-20 01:12:09 +0000394 DenseMap<Loop *, Loop *> SubloopParents;
Andrew Trickd3530b92011-08-10 23:22:57 +0000395
396 // Flag the presence of an irreducible backedge whose destination is a block
397 // directly contained by the original unloop.
398 bool FoundIB;
399
400public:
Sanjoy Das66a004a2017-09-20 01:12:09 +0000401 UnloopUpdater(Loop *UL, LoopInfo *LInfo)
402 : Unloop(*UL), LI(LInfo), DFS(UL), FoundIB(false) {}
Andrew Trickd3530b92011-08-10 23:22:57 +0000403
404 void updateBlockParents();
405
Andrew Trickc12c30a2011-08-11 20:27:32 +0000406 void removeBlocksFromAncestors();
407
Andrew Trickd3530b92011-08-10 23:22:57 +0000408 void updateSubloopParents();
409
410protected:
411 Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop);
412};
Benjamin Kramer4938edb2011-08-19 01:42:18 +0000413} // end anonymous namespace
Andrew Trickd3530b92011-08-10 23:22:57 +0000414
Sanjay Patel784b5e32016-01-14 23:23:04 +0000415/// Update the parent loop for all blocks that are directly contained within the
416/// original "unloop".
Andrew Trickd3530b92011-08-10 23:22:57 +0000417void UnloopUpdater::updateBlockParents() {
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000418 if (Unloop.getNumBlocks()) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000419 // Perform a post order CFG traversal of all blocks within this loop,
Hiroshi Inoue713b5ba2017-07-09 05:54:44 +0000420 // propagating the nearest loop from successors to predecessors.
Andrew Trickd3530b92011-08-10 23:22:57 +0000421 LoopBlocksTraversal Traversal(DFS, LI);
Benjamin Krameraa209152016-06-26 17:27:42 +0000422 for (BasicBlock *POI : Traversal) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000423
Benjamin Krameraa209152016-06-26 17:27:42 +0000424 Loop *L = LI->getLoopFor(POI);
425 Loop *NL = getNearestLoop(POI, L);
Andrew Trickd3530b92011-08-10 23:22:57 +0000426
427 if (NL != L) {
428 // For reducible loops, NL is now an ancestor of Unloop.
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000429 assert((NL != &Unloop && (!NL || NL->contains(&Unloop))) &&
Andrew Trickd3530b92011-08-10 23:22:57 +0000430 "uninitialized successor");
Benjamin Krameraa209152016-06-26 17:27:42 +0000431 LI->changeLoopFor(POI, NL);
Sanjoy Das66a004a2017-09-20 01:12:09 +0000432 } else {
Andrew Trickd3530b92011-08-10 23:22:57 +0000433 // Or the current block is part of a subloop, in which case its parent
434 // is unchanged.
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000435 assert((FoundIB || Unloop.contains(L)) && "uninitialized successor");
Andrew Trickd3530b92011-08-10 23:22:57 +0000436 }
437 }
438 }
439 // Each irreducible loop within the unloop induces a round of iteration using
440 // the DFS result cached by Traversal.
441 bool Changed = FoundIB;
442 for (unsigned NIters = 0; Changed; ++NIters) {
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000443 assert(NIters < Unloop.getNumBlocks() && "runaway iterative algorithm");
Andrew Trickd3530b92011-08-10 23:22:57 +0000444
445 // Iterate over the postorder list of blocks, propagating the nearest loop
446 // from successors to predecessors as before.
447 Changed = false;
448 for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(),
Sanjoy Das66a004a2017-09-20 01:12:09 +0000449 POE = DFS.endPostorder();
450 POI != POE; ++POI) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000451
452 Loop *L = LI->getLoopFor(*POI);
453 Loop *NL = getNearestLoop(*POI, L);
454 if (NL != L) {
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000455 assert(NL != &Unloop && (!NL || NL->contains(&Unloop)) &&
Andrew Trickd3530b92011-08-10 23:22:57 +0000456 "uninitialized successor");
457 LI->changeLoopFor(*POI, NL);
458 Changed = true;
459 }
460 }
461 }
462}
463
Sanjay Patel784b5e32016-01-14 23:23:04 +0000464/// Remove unloop's blocks from all ancestors below their new parents.
Andrew Trickc12c30a2011-08-11 20:27:32 +0000465void UnloopUpdater::removeBlocksFromAncestors() {
Andrew Trick6b4d5782011-11-18 03:42:41 +0000466 // Remove all unloop's blocks (including those in nested subloops) from
467 // ancestors below the new parent loop.
Sanjoy Das66a004a2017-09-20 01:12:09 +0000468 for (Loop::block_iterator BI = Unloop.block_begin(), BE = Unloop.block_end();
469 BI != BE; ++BI) {
Andrew Trick6b4d5782011-11-18 03:42:41 +0000470 Loop *OuterParent = LI->getLoopFor(*BI);
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000471 if (Unloop.contains(OuterParent)) {
472 while (OuterParent->getParentLoop() != &Unloop)
Andrew Trick6b4d5782011-11-18 03:42:41 +0000473 OuterParent = OuterParent->getParentLoop();
474 OuterParent = SubloopParents[OuterParent];
475 }
Andrew Trickc12c30a2011-08-11 20:27:32 +0000476 // Remove blocks from former Ancestors except Unloop itself which will be
477 // deleted.
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000478 for (Loop *OldParent = Unloop.getParentLoop(); OldParent != OuterParent;
Andrew Trickc12c30a2011-08-11 20:27:32 +0000479 OldParent = OldParent->getParentLoop()) {
480 assert(OldParent && "new loop is not an ancestor of the original");
481 OldParent->removeBlockFromLoop(*BI);
482 }
483 }
484}
485
Sanjay Patel784b5e32016-01-14 23:23:04 +0000486/// Update the parent loop for all subloops directly nested within unloop.
Andrew Trickd3530b92011-08-10 23:22:57 +0000487void UnloopUpdater::updateSubloopParents() {
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000488 while (!Unloop.empty()) {
489 Loop *Subloop = *std::prev(Unloop.end());
490 Unloop.removeChildLoop(std::prev(Unloop.end()));
Andrew Trickd3530b92011-08-10 23:22:57 +0000491
492 assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop");
Benjamin Kramerf29db272012-08-22 15:37:57 +0000493 if (Loop *Parent = SubloopParents[Subloop])
494 Parent->addChildLoop(Subloop);
Andrew Trick147d9cd2011-08-26 03:06:34 +0000495 else
496 LI->addTopLevelLoop(Subloop);
Andrew Trickd3530b92011-08-10 23:22:57 +0000497 }
498}
499
Sanjay Patel784b5e32016-01-14 23:23:04 +0000500/// Return the nearest parent loop among this block's successors. If a successor
501/// is a subloop header, consider its parent to be the nearest parent of the
502/// subloop's exits.
Andrew Trickd3530b92011-08-10 23:22:57 +0000503///
504/// For subloop blocks, simply update SubloopParents and return NULL.
505Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) {
506
Andrew Trick266ab102011-08-11 17:54:58 +0000507 // Initially for blocks directly contained by Unloop, NearLoop == Unloop and
508 // is considered uninitialized.
Andrew Trickd3530b92011-08-10 23:22:57 +0000509 Loop *NearLoop = BBLoop;
510
Craig Topper9f008862014-04-15 04:59:12 +0000511 Loop *Subloop = nullptr;
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000512 if (NearLoop != &Unloop && Unloop.contains(NearLoop)) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000513 Subloop = NearLoop;
514 // Find the subloop ancestor that is directly contained within Unloop.
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000515 while (Subloop->getParentLoop() != &Unloop) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000516 Subloop = Subloop->getParentLoop();
517 assert(Subloop && "subloop is not an ancestor of the original loop");
518 }
519 // Get the current nearest parent of the Subloop exits, initially Unloop.
David Majnemer0a16c222016-08-11 21:15:00 +0000520 NearLoop = SubloopParents.insert({Subloop, &Unloop}).first->second;
Andrew Trickd3530b92011-08-10 23:22:57 +0000521 }
522
523 succ_iterator I = succ_begin(BB), E = succ_end(BB);
524 if (I == E) {
525 assert(!Subloop && "subloop blocks must have a successor");
Craig Topper9f008862014-04-15 04:59:12 +0000526 NearLoop = nullptr; // unloop blocks may now exit the function.
Andrew Trickd3530b92011-08-10 23:22:57 +0000527 }
528 for (; I != E; ++I) {
529 if (*I == BB)
530 continue; // self loops are uninteresting
531
532 Loop *L = LI->getLoopFor(*I);
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000533 if (L == &Unloop) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000534 // This successor has not been processed. This path must lead to an
535 // irreducible backedge.
536 assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB");
537 FoundIB = true;
538 }
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000539 if (L != &Unloop && Unloop.contains(L)) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000540 // Successor is in a subloop.
541 if (Subloop)
542 continue; // Branching within subloops. Ignore it.
543
544 // BB branches from the original into a subloop header.
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000545 assert(L->getParentLoop() == &Unloop && "cannot skip into nested loops");
Andrew Trickd3530b92011-08-10 23:22:57 +0000546
547 // Get the current nearest parent of the Subloop's exits.
548 L = SubloopParents[L];
549 // L could be Unloop if the only exit was an irreducible backedge.
550 }
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000551 if (L == &Unloop) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000552 continue;
553 }
554 // Handle critical edges from Unloop into a sibling loop.
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000555 if (L && !L->contains(&Unloop)) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000556 L = L->getParentLoop();
557 }
558 // Remember the nearest parent loop among successors or subloop exits.
Silviu Baranga24dbd2e2016-05-13 14:54:50 +0000559 if (NearLoop == &Unloop || !NearLoop || NearLoop->contains(L))
Andrew Trickd3530b92011-08-10 23:22:57 +0000560 NearLoop = L;
561 }
562 if (Subloop) {
563 SubloopParents[Subloop] = NearLoop;
564 return BBLoop;
565 }
566 return NearLoop;
567}
568
Sanjoy Das66a004a2017-09-20 01:12:09 +0000569LoopInfo::LoopInfo(const DomTreeBase<BasicBlock> &DomTree) { analyze(DomTree); }
Cong Hou9b4f6b22015-07-16 23:23:35 +0000570
Chandler Carruthca68a3e2017-01-15 06:32:49 +0000571bool LoopInfo::invalidate(Function &F, const PreservedAnalyses &PA,
572 FunctionAnalysisManager::Invalidator &) {
573 // Check whether the analysis, all analyses on functions, or the function's
574 // CFG have been preserved.
575 auto PAC = PA.getChecker<LoopAnalysis>();
576 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
577 PAC.preservedSet<CFGAnalyses>());
578}
579
Sanjoy Das388b0122017-09-22 01:47:41 +0000580void LoopInfo::erase(Loop *Unloop) {
Sanjoy Das76ab2322017-09-19 23:19:00 +0000581 assert(!Unloop->isInvalid() && "Loop has already been erased!");
Andrew Trickd3530b92011-08-10 23:22:57 +0000582
Sanjoy Dasdef17292017-09-28 02:45:42 +0000583 auto InvalidateOnExit = make_scope_exit([&]() { destroy(Unloop); });
Sanjoy Das09613b12017-09-20 02:31:57 +0000584
Andrew Trickd3530b92011-08-10 23:22:57 +0000585 // First handle the special case of no parent loop to simplify the algorithm.
586 if (!Unloop->getParentLoop()) {
587 // Since BBLoop had no parent, Unloop blocks are no longer in a loop.
588 for (Loop::block_iterator I = Unloop->block_begin(),
Chandler Carruth691addc2015-01-18 01:25:51 +0000589 E = Unloop->block_end();
590 I != E; ++I) {
Andrew Trickd3530b92011-08-10 23:22:57 +0000591
592 // Don't reparent blocks in subloops.
593 if (getLoopFor(*I) != Unloop)
594 continue;
595
596 // Blocks no longer have a parent but are still referenced by Unloop until
597 // the Unloop object is deleted.
Chandler Carruth691addc2015-01-18 01:25:51 +0000598 changeLoopFor(*I, nullptr);
Andrew Trickd3530b92011-08-10 23:22:57 +0000599 }
600
601 // Remove the loop from the top-level LoopInfo object.
Chandler Carruth691addc2015-01-18 01:25:51 +0000602 for (iterator I = begin();; ++I) {
603 assert(I != end() && "Couldn't find loop");
Andrew Trickd3530b92011-08-10 23:22:57 +0000604 if (*I == Unloop) {
Chandler Carruth691addc2015-01-18 01:25:51 +0000605 removeLoop(I);
Andrew Trickd3530b92011-08-10 23:22:57 +0000606 break;
607 }
608 }
609
610 // Move all of the subloops to the top-level.
611 while (!Unloop->empty())
Chandler Carruth691addc2015-01-18 01:25:51 +0000612 addTopLevelLoop(Unloop->removeChildLoop(std::prev(Unloop->end())));
Andrew Trickd3530b92011-08-10 23:22:57 +0000613
614 return;
615 }
616
617 // Update the parent loop for all blocks within the loop. Blocks within
618 // subloops will not change parents.
619 UnloopUpdater Updater(Unloop, this);
620 Updater.updateBlockParents();
621
Andrew Trickc12c30a2011-08-11 20:27:32 +0000622 // Remove blocks from former ancestor loops.
623 Updater.removeBlocksFromAncestors();
Andrew Trickd3530b92011-08-10 23:22:57 +0000624
625 // Add direct subloops as children in their new parent loop.
626 Updater.updateSubloopParents();
627
628 // Remove unloop from its parent loop.
629 Loop *ParentLoop = Unloop->getParentLoop();
Duncan Sandsa41634e2011-08-12 14:54:45 +0000630 for (Loop::iterator I = ParentLoop->begin();; ++I) {
631 assert(I != ParentLoop->end() && "Couldn't find loop");
Andrew Trickd3530b92011-08-10 23:22:57 +0000632 if (*I == Unloop) {
633 ParentLoop->removeChildLoop(I);
634 break;
635 }
636 }
637}
638
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000639AnalysisKey LoopAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000640
Sean Silva36e0d012016-08-09 00:28:15 +0000641LoopInfo LoopAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +0000642 // FIXME: Currently we create a LoopInfo from scratch for every function.
643 // This may prove to be too wasteful due to deallocating and re-allocating
644 // memory each time for the underlying map and vector datastructures. At some
645 // point it may prove worthwhile to use a freelist and recycle LoopInfo
646 // objects. I don't want to add that kind of complexity until the scope of
647 // the problem is better understood.
648 LoopInfo LI;
Chandler Carruthb47f8012016-03-11 11:05:24 +0000649 LI.analyze(AM.getResult<DominatorTreeAnalysis>(F));
Richard Trieu6ae37962015-04-30 23:07:00 +0000650 return LI;
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +0000651}
652
653PreservedAnalyses LoopPrinterPass::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000654 FunctionAnalysisManager &AM) {
Chandler Carruthb47f8012016-03-11 11:05:24 +0000655 AM.getResult<LoopAnalysis>(F).print(OS);
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +0000656 return PreservedAnalyses::all();
657}
658
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000659void llvm::printLoop(Loop &L, raw_ostream &OS, const std::string &Banner) {
Fedor Sergeev3b459c32017-12-01 18:33:58 +0000660
661 if (forcePrintModuleIR()) {
662 // handling -print-module-scope
663 OS << Banner << " (loop: ";
664 L.getHeader()->printAsOperand(OS, false);
665 OS << ")\n";
666
667 // printing whole module
668 OS << *L.getHeader()->getModule();
669 return;
670 }
671
Justin Bognerc2b98f02015-11-04 22:24:08 +0000672 OS << Banner;
Fedor Sergeev61975b42017-11-22 20:59:53 +0000673
674 auto *PreHeader = L.getLoopPreheader();
675 if (PreHeader) {
676 OS << "\n; Preheader:";
677 PreHeader->print(OS);
678 OS << "\n; Loop:";
679 }
680
Justin Bognerc2b98f02015-11-04 22:24:08 +0000681 for (auto *Block : L.blocks())
682 if (Block)
683 Block->print(OS);
684 else
685 OS << "Printing <null> block";
Fedor Sergeev61975b42017-11-22 20:59:53 +0000686
687 SmallVector<BasicBlock *, 8> ExitBlocks;
688 L.getExitBlocks(ExitBlocks);
689 if (!ExitBlocks.empty()) {
690 OS << "\n; Exit blocks";
691 for (auto *Block : ExitBlocks)
692 if (Block)
693 Block->print(OS);
694 else
695 OS << "Printing <null> block";
696 }
Justin Bognerc2b98f02015-11-04 22:24:08 +0000697}
698
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000699//===----------------------------------------------------------------------===//
700// LoopInfo implementation
701//
702
703char LoopInfoWrapperPass::ID = 0;
704INITIALIZE_PASS_BEGIN(LoopInfoWrapperPass, "loops", "Natural Loop Information",
705 true, true)
706INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
707INITIALIZE_PASS_END(LoopInfoWrapperPass, "loops", "Natural Loop Information",
708 true, true)
709
710bool LoopInfoWrapperPass::runOnFunction(Function &) {
711 releaseMemory();
Cong Houd2c1d912015-07-16 18:23:57 +0000712 LI.analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree());
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000713 return false;
714}
715
716void LoopInfoWrapperPass::verifyAnalysis() const {
717 // LoopInfoWrapperPass is a FunctionPass, but verifying every loop in the
718 // function each time verifyAnalysis is called is very expensive. The
Dan Gohman4dbb3012009-09-28 00:27:48 +0000719 // -verify-loop-info option can enable this. In order to perform some
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000720 // checking by default, LoopPass has been taught to call verifyLoop manually
721 // during loop pass sequences.
Michael Zolotukhine0b2d972016-08-31 19:26:19 +0000722 if (VerifyLoopInfo) {
Serge Pavloved5eb932017-01-15 10:23:18 +0000723 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
724 LI.verify(DT);
Michael Zolotukhine0b2d972016-08-31 19:26:19 +0000725 }
Dan Gohman3ddbc242009-09-08 15:45:00 +0000726}
727
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000728void LoopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerc8e66542002-04-27 06:56:12 +0000729 AU.setPreservesAll();
Chandler Carruth73523022014-01-13 13:07:17 +0000730 AU.addRequired<DominatorTreeWrapperPass>();
Chris Lattnerccf571a2002-01-31 00:42:27 +0000731}
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000732
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000733void LoopInfoWrapperPass::print(raw_ostream &OS, const Module *) const {
Chandler Carruth691addc2015-01-18 01:25:51 +0000734 LI.print(OS);
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000735}
736
Sean Silvae3c18a52016-07-19 23:54:23 +0000737PreservedAnalyses LoopVerifierPass::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000738 FunctionAnalysisManager &AM) {
Sean Silvae3c18a52016-07-19 23:54:23 +0000739 LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
Michael Zolotukhine0b2d972016-08-31 19:26:19 +0000740 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
741 LI.verify(DT);
Sean Silvae3c18a52016-07-19 23:54:23 +0000742 return PreservedAnalyses::all();
743}
744
Andrew Trick78b40c32011-08-10 01:59:05 +0000745//===----------------------------------------------------------------------===//
746// LoopBlocksDFS implementation
747//
748
749/// Traverse the loop blocks and store the DFS result.
750/// Useful for clients that just want the final DFS result and don't need to
751/// visit blocks during the initial traversal.
752void LoopBlocksDFS::perform(LoopInfo *LI) {
753 LoopBlocksTraversal Traversal(*this, LI);
754 for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(),
Sanjoy Das66a004a2017-09-20 01:12:09 +0000755 POE = Traversal.end();
756 POI != POE; ++POI)
757 ;
Andrew Trick78b40c32011-08-10 01:59:05 +0000758}