Bug Summary

File:include/llvm/Analysis/LoopInfo.h
Warning:line 473, column 57
Moved-from object 'Start' is moved

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name LoopInfo.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-9/lib/clang/9.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-9~svn359999/build-llvm/lib/Analysis -I /build/llvm-toolchain-snapshot-9~svn359999/lib/Analysis -I /build/llvm-toolchain-snapshot-9~svn359999/build-llvm/include -I /build/llvm-toolchain-snapshot-9~svn359999/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/include/clang/9.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-9/lib/clang/9.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-9~svn359999/build-llvm/lib/Analysis -fdebug-prefix-map=/build/llvm-toolchain-snapshot-9~svn359999=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2019-05-06-051613-19774-1 -x c++ /build/llvm-toolchain-snapshot-9~svn359999/lib/Analysis/LoopInfo.cpp -faddrsig

/build/llvm-toolchain-snapshot-9~svn359999/lib/Analysis/LoopInfo.cpp

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

/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h

1//===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===//
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. A natural loop
11// has exactly one entry-point, which is called the header. Note that natural
12// loops may actually be several loops that share the same header node.
13//
14// This analysis calculates the nesting structure of loops in a function. For
15// each natural loop identified, this analysis identifies natural loops
16// contained entirely within the loop and the basic blocks the make up the loop.
17//
18// It can calculate on the fly various bits of information, for example:
19//
20// * whether there is a preheader for the loop
21// * the number of back edges to the header
22// * whether or not a particular block branches out of the loop
23// * the successor blocks of the loop
24// * the loop depth
25// * etc...
26//
27// Note that this analysis specifically identifies *Loops* not cycles or SCCs
28// in the CFG. There can be strongly connected components in the CFG which
29// this analysis will not recognize and that will not be represented by a Loop
30// instance. In particular, a Loop might be inside such a non-loop SCC, or a
31// non-loop SCC might contain a sub-SCC which is a Loop.
32//
33//===----------------------------------------------------------------------===//
34
35#ifndef LLVM_ANALYSIS_LOOPINFO_H
36#define LLVM_ANALYSIS_LOOPINFO_H
37
38#include "llvm/ADT/DenseMap.h"
39#include "llvm/ADT/DenseSet.h"
40#include "llvm/ADT/GraphTraits.h"
41#include "llvm/ADT/SmallPtrSet.h"
42#include "llvm/ADT/SmallVector.h"
43#include "llvm/IR/CFG.h"
44#include "llvm/IR/Instruction.h"
45#include "llvm/IR/Instructions.h"
46#include "llvm/IR/PassManager.h"
47#include "llvm/Pass.h"
48#include "llvm/Support/Allocator.h"
49#include <algorithm>
50#include <utility>
51
52namespace llvm {
53
54class DominatorTree;
55class LoopInfo;
56class Loop;
57class MDNode;
58class PHINode;
59class raw_ostream;
60template <class N, bool IsPostDom> class DominatorTreeBase;
61template <class N, class M> class LoopInfoBase;
62template <class N, class M> class LoopBase;
63
64//===----------------------------------------------------------------------===//
65/// Instances of this class are used to represent loops that are detected in the
66/// flow graph.
67///
68template <class BlockT, class LoopT> class LoopBase {
69 LoopT *ParentLoop;
70 // Loops contained entirely within this one.
71 std::vector<LoopT *> SubLoops;
72
73 // The list of blocks in this loop. First entry is the header node.
74 std::vector<BlockT *> Blocks;
75
76 SmallPtrSet<const BlockT *, 8> DenseBlockSet;
77
78#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
79 /// Indicator that this loop is no longer a valid loop.
80 bool IsInvalid = false;
81#endif
82
83 LoopBase(const LoopBase<BlockT, LoopT> &) = delete;
84 const LoopBase<BlockT, LoopT> &
85 operator=(const LoopBase<BlockT, LoopT> &) = delete;
86
87public:
88 /// Return the nesting level of this loop. An outer-most loop has depth 1,
89 /// for consistency with loop depth values used for basic blocks, where depth
90 /// 0 is used for blocks not inside any loops.
91 unsigned getLoopDepth() const {
92 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 92, __PRETTY_FUNCTION__))
;
93 unsigned D = 1;
94 for (const LoopT *CurLoop = ParentLoop; CurLoop;
95 CurLoop = CurLoop->ParentLoop)
96 ++D;
97 return D;
98 }
99 BlockT *getHeader() const { return getBlocks().front(); }
100 LoopT *getParentLoop() const { return ParentLoop; }
101
102 /// This is a raw interface for bypassing addChildLoop.
103 void setParentLoop(LoopT *L) {
104 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 104, __PRETTY_FUNCTION__))
;
105 ParentLoop = L;
106 }
107
108 /// Return true if the specified loop is contained within in this loop.
109 bool contains(const LoopT *L) const {
110 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 110, __PRETTY_FUNCTION__))
;
111 if (L == this)
112 return true;
113 if (!L)
114 return false;
115 return contains(L->getParentLoop());
116 }
117
118 /// Return true if the specified basic block is in this loop.
119 bool contains(const BlockT *BB) const {
120 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 120, __PRETTY_FUNCTION__))
;
121 return DenseBlockSet.count(BB);
122 }
123
124 /// Return true if the specified instruction is in this loop.
125 template <class InstT> bool contains(const InstT *Inst) const {
126 return contains(Inst->getParent());
127 }
128
129 /// Return the loops contained entirely within this loop.
130 const std::vector<LoopT *> &getSubLoops() const {
131 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 131, __PRETTY_FUNCTION__))
;
132 return SubLoops;
133 }
134 std::vector<LoopT *> &getSubLoopsVector() {
135 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 135, __PRETTY_FUNCTION__))
;
136 return SubLoops;
137 }
138 typedef typename std::vector<LoopT *>::const_iterator iterator;
139 typedef
140 typename std::vector<LoopT *>::const_reverse_iterator reverse_iterator;
141 iterator begin() const { return getSubLoops().begin(); }
142 iterator end() const { return getSubLoops().end(); }
143 reverse_iterator rbegin() const { return getSubLoops().rbegin(); }
144 reverse_iterator rend() const { return getSubLoops().rend(); }
145 bool empty() const { return getSubLoops().empty(); }
146
147 /// Get a list of the basic blocks which make up this loop.
148 ArrayRef<BlockT *> getBlocks() const {
149 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 149, __PRETTY_FUNCTION__))
;
150 return Blocks;
151 }
152 typedef typename ArrayRef<BlockT *>::const_iterator block_iterator;
153 block_iterator block_begin() const { return getBlocks().begin(); }
154 block_iterator block_end() const { return getBlocks().end(); }
155 inline iterator_range<block_iterator> blocks() const {
156 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 156, __PRETTY_FUNCTION__))
;
157 return make_range(block_begin(), block_end());
158 }
159
160 /// Get the number of blocks in this loop in constant time.
161 /// Invalidate the loop, indicating that it is no longer a loop.
162 unsigned getNumBlocks() const {
163 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 163, __PRETTY_FUNCTION__))
;
164 return Blocks.size();
165 }
166
167 /// Return a direct, mutable handle to the blocks vector so that we can
168 /// mutate it efficiently with techniques like `std::remove`.
169 std::vector<BlockT *> &getBlocksVector() {
170 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 170, __PRETTY_FUNCTION__))
;
171 return Blocks;
172 }
173 /// Return a direct, mutable handle to the blocks set so that we can
174 /// mutate it efficiently.
175 SmallPtrSetImpl<const BlockT *> &getBlocksSet() {
176 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 176, __PRETTY_FUNCTION__))
;
177 return DenseBlockSet;
178 }
179
180 /// Return a direct, immutable handle to the blocks set.
181 const SmallPtrSetImpl<const BlockT *> &getBlocksSet() const {
182 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 182, __PRETTY_FUNCTION__))
;
183 return DenseBlockSet;
184 }
185
186 /// Return true if this loop is no longer valid. The only valid use of this
187 /// helper is "assert(L.isInvalid())" or equivalent, since IsInvalid is set to
188 /// true by the destructor. In other words, if this accessor returns true,
189 /// the caller has already triggered UB by calling this accessor; and so it
190 /// can only be called in a context where a return value of true indicates a
191 /// programmer error.
192 bool isInvalid() const {
193#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
194 return IsInvalid;
195#else
196 return false;
197#endif
198 }
199
200 /// True if terminator in the block can branch to another block that is
201 /// outside of the current loop.
202 bool isLoopExiting(const BlockT *BB) const {
203 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 203, __PRETTY_FUNCTION__))
;
204 for (const auto &Succ : children<const BlockT *>(BB)) {
205 if (!contains(Succ))
206 return true;
207 }
208 return false;
209 }
210
211 /// Returns true if \p BB is a loop-latch.
212 /// A latch block is a block that contains a branch back to the header.
213 /// This function is useful when there are multiple latches in a loop
214 /// because \fn getLoopLatch will return nullptr in that case.
215 bool isLoopLatch(const BlockT *BB) const {
216 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 216, __PRETTY_FUNCTION__))
;
217 assert(contains(BB) && "block does not belong to the loop")((contains(BB) && "block does not belong to the loop"
) ? static_cast<void> (0) : __assert_fail ("contains(BB) && \"block does not belong to the loop\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 217, __PRETTY_FUNCTION__))
;
218
219 BlockT *Header = getHeader();
220 auto PredBegin = GraphTraits<Inverse<BlockT *>>::child_begin(Header);
221 auto PredEnd = GraphTraits<Inverse<BlockT *>>::child_end(Header);
222 return std::find(PredBegin, PredEnd, BB) != PredEnd;
223 }
224
225 /// Calculate the number of back edges to the loop header.
226 unsigned getNumBackEdges() const {
227 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 227, __PRETTY_FUNCTION__))
;
228 unsigned NumBackEdges = 0;
229 BlockT *H = getHeader();
230
231 for (const auto Pred : children<Inverse<BlockT *>>(H))
232 if (contains(Pred))
233 ++NumBackEdges;
234
235 return NumBackEdges;
236 }
237
238 //===--------------------------------------------------------------------===//
239 // APIs for simple analysis of the loop.
240 //
241 // Note that all of these methods can fail on general loops (ie, there may not
242 // be a preheader, etc). For best success, the loop simplification and
243 // induction variable canonicalization pass should be used to normalize loops
244 // for easy analysis. These methods assume canonical loops.
245
246 /// Return all blocks inside the loop that have successors outside of the
247 /// loop. These are the blocks _inside of the current loop_ which branch out.
248 /// The returned list is always unique.
249 void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const;
250
251 /// If getExitingBlocks would return exactly one block, return that block.
252 /// Otherwise return null.
253 BlockT *getExitingBlock() const;
254
255 /// Return all of the successor blocks of this loop. These are the blocks
256 /// _outside of the current loop_ which are branched to.
257 void getExitBlocks(SmallVectorImpl<BlockT *> &ExitBlocks) const;
258
259 /// If getExitBlocks would return exactly one block, return that block.
260 /// Otherwise return null.
261 BlockT *getExitBlock() const;
262
263 /// Return true if no exit block for the loop has a predecessor that is
264 /// outside the loop.
265 bool hasDedicatedExits() const;
266
267 /// Return all unique successor blocks of this loop.
268 /// These are the blocks _outside of the current loop_ which are branched to.
269 /// This assumes that loop exits are in canonical form, i.e. all exits are
270 /// dedicated exits.
271 void getUniqueExitBlocks(SmallVectorImpl<BlockT *> &ExitBlocks) const;
272
273 /// If getUniqueExitBlocks would return exactly one block, return that block.
274 /// Otherwise return null.
275 BlockT *getUniqueExitBlock() const;
276
277 /// Edge type.
278 typedef std::pair<const BlockT *, const BlockT *> Edge;
279
280 /// Return all pairs of (_inside_block_,_outside_block_).
281 void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const;
282
283 /// If there is a preheader for this loop, return it. A loop has a preheader
284 /// if there is only one edge to the header of the loop from outside of the
285 /// loop. If this is the case, the block branching to the header of the loop
286 /// is the preheader node.
287 ///
288 /// This method returns null if there is no preheader for the loop.
289 BlockT *getLoopPreheader() const;
290
291 /// If the given loop's header has exactly one unique predecessor outside the
292 /// loop, return it. Otherwise return null.
293 /// This is less strict that the loop "preheader" concept, which requires
294 /// the predecessor to have exactly one successor.
295 BlockT *getLoopPredecessor() const;
296
297 /// If there is a single latch block for this loop, return it.
298 /// A latch block is a block that contains a branch back to the header.
299 BlockT *getLoopLatch() const;
300
301 /// Return all loop latch blocks of this loop. A latch block is a block that
302 /// contains a branch back to the header.
303 void getLoopLatches(SmallVectorImpl<BlockT *> &LoopLatches) const {
304 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 304, __PRETTY_FUNCTION__))
;
305 BlockT *H = getHeader();
306 for (const auto Pred : children<Inverse<BlockT *>>(H))
307 if (contains(Pred))
308 LoopLatches.push_back(Pred);
309 }
310
311 //===--------------------------------------------------------------------===//
312 // APIs for updating loop information after changing the CFG
313 //
314
315 /// This method is used by other analyses to update loop information.
316 /// NewBB is set to be a new member of the current loop.
317 /// Because of this, it is added as a member of all parent loops, and is added
318 /// to the specified LoopInfo object as being in the current basic block. It
319 /// is not valid to replace the loop header with this method.
320 void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
321
322 /// This is used when splitting loops up. It replaces the OldChild entry in
323 /// our children list with NewChild, and updates the parent pointer of
324 /// OldChild to be null and the NewChild to be this loop.
325 /// This updates the loop depth of the new child.
326 void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild);
327
328 /// Add the specified loop to be a child of this loop.
329 /// This updates the loop depth of the new child.
330 void addChildLoop(LoopT *NewChild) {
331 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 331, __PRETTY_FUNCTION__))
;
332 assert(!NewChild->ParentLoop && "NewChild already has a parent!")((!NewChild->ParentLoop && "NewChild already has a parent!"
) ? static_cast<void> (0) : __assert_fail ("!NewChild->ParentLoop && \"NewChild already has a parent!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 332, __PRETTY_FUNCTION__))
;
333 NewChild->ParentLoop = static_cast<LoopT *>(this);
334 SubLoops.push_back(NewChild);
335 }
336
337 /// This removes the specified child from being a subloop of this loop. The
338 /// loop is not deleted, as it will presumably be inserted into another loop.
339 LoopT *removeChildLoop(iterator I) {
340 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 340, __PRETTY_FUNCTION__))
;
341 assert(I != SubLoops.end() && "Cannot remove end iterator!")((I != SubLoops.end() && "Cannot remove end iterator!"
) ? static_cast<void> (0) : __assert_fail ("I != SubLoops.end() && \"Cannot remove end iterator!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 341, __PRETTY_FUNCTION__))
;
342 LoopT *Child = *I;
343 assert(Child->ParentLoop == this && "Child is not a child of this loop!")((Child->ParentLoop == this && "Child is not a child of this loop!"
) ? static_cast<void> (0) : __assert_fail ("Child->ParentLoop == this && \"Child is not a child of this loop!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 343, __PRETTY_FUNCTION__))
;
344 SubLoops.erase(SubLoops.begin() + (I - begin()));
345 Child->ParentLoop = nullptr;
346 return Child;
347 }
348
349 /// This removes the specified child from being a subloop of this loop. The
350 /// loop is not deleted, as it will presumably be inserted into another loop.
351 LoopT *removeChildLoop(LoopT *Child) {
352 return removeChildLoop(llvm::find(*this, Child));
353 }
354
355 /// This adds a basic block directly to the basic block list.
356 /// This should only be used by transformations that create new loops. Other
357 /// transformations should use addBasicBlockToLoop.
358 void addBlockEntry(BlockT *BB) {
359 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 359, __PRETTY_FUNCTION__))
;
360 Blocks.push_back(BB);
361 DenseBlockSet.insert(BB);
362 }
363
364 /// interface to reverse Blocks[from, end of loop] in this loop
365 void reverseBlock(unsigned from) {
366 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 366, __PRETTY_FUNCTION__))
;
367 std::reverse(Blocks.begin() + from, Blocks.end());
368 }
369
370 /// interface to do reserve() for Blocks
371 void reserveBlocks(unsigned size) {
372 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 372, __PRETTY_FUNCTION__))
;
373 Blocks.reserve(size);
374 }
375
376 /// This method is used to move BB (which must be part of this loop) to be the
377 /// loop header of the loop (the block that dominates all others).
378 void moveToHeader(BlockT *BB) {
379 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 379, __PRETTY_FUNCTION__))
;
380 if (Blocks[0] == BB)
381 return;
382 for (unsigned i = 0;; ++i) {
383 assert(i != Blocks.size() && "Loop does not contain BB!")((i != Blocks.size() && "Loop does not contain BB!") ?
static_cast<void> (0) : __assert_fail ("i != Blocks.size() && \"Loop does not contain BB!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 383, __PRETTY_FUNCTION__))
;
384 if (Blocks[i] == BB) {
385 Blocks[i] = Blocks[0];
386 Blocks[0] = BB;
387 return;
388 }
389 }
390 }
391
392 /// This removes the specified basic block from the current loop, updating the
393 /// Blocks as appropriate. This does not update the mapping in the LoopInfo
394 /// class.
395 void removeBlockFromLoop(BlockT *BB) {
396 assert(!isInvalid() && "Loop not in a valid state!")((!isInvalid() && "Loop not in a valid state!") ? static_cast
<void> (0) : __assert_fail ("!isInvalid() && \"Loop not in a valid state!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 396, __PRETTY_FUNCTION__))
;
397 auto I = find(Blocks, BB);
398 assert(I != Blocks.end() && "N is not in this list!")((I != Blocks.end() && "N is not in this list!") ? static_cast
<void> (0) : __assert_fail ("I != Blocks.end() && \"N is not in this list!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 398, __PRETTY_FUNCTION__))
;
399 Blocks.erase(I);
400
401 DenseBlockSet.erase(BB);
402 }
403
404 /// Verify loop structure
405 void verifyLoop() const;
406
407 /// Verify loop structure of this loop and all nested loops.
408 void verifyLoopNest(DenseSet<const LoopT *> *Loops) const;
409
410 /// Returns true if the loop is annotated parallel.
411 ///
412 /// Derived classes can override this method using static template
413 /// polymorphism.
414 bool isAnnotatedParallel() const { return false; }
415
416 /// Print loop with all the BBs inside it.
417 void print(raw_ostream &OS, unsigned Depth = 0, bool Verbose = false) const;
418
419protected:
420 friend class LoopInfoBase<BlockT, LoopT>;
421
422 /// This creates an empty loop.
423 LoopBase() : ParentLoop(nullptr) {}
424
425 explicit LoopBase(BlockT *BB) : ParentLoop(nullptr) {
426 Blocks.push_back(BB);
427 DenseBlockSet.insert(BB);
428 }
429
430 // Since loop passes like SCEV are allowed to key analysis results off of
431 // `Loop` pointers, we cannot re-use pointers within a loop pass manager.
432 // This means loop passes should not be `delete` ing `Loop` objects directly
433 // (and risk a later `Loop` allocation re-using the address of a previous one)
434 // but should be using LoopInfo::markAsRemoved, which keeps around the `Loop`
435 // pointer till the end of the lifetime of the `LoopInfo` object.
436 //
437 // To make it easier to follow this rule, we mark the destructor as
438 // non-public.
439 ~LoopBase() {
440 for (auto *SubLoop : SubLoops)
441 SubLoop->~LoopT();
442
443#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
444 IsInvalid = true;
445#endif
446 SubLoops.clear();
447 Blocks.clear();
448 DenseBlockSet.clear();
449 ParentLoop = nullptr;
450 }
451};
452
453template <class BlockT, class LoopT>
454raw_ostream &operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
455 Loop.print(OS);
456 return OS;
457}
458
459// Implementation in LoopInfoImpl.h
460extern template class LoopBase<BasicBlock, Loop>;
461
462/// Represents a single loop in the control flow graph. Note that not all SCCs
463/// in the CFG are necessarily loops.
464class Loop : public LoopBase<BasicBlock, Loop> {
465public:
466 /// A range representing the start and end location of a loop.
467 class LocRange {
468 DebugLoc Start;
469 DebugLoc End;
470
471 public:
472 LocRange() {}
473 LocRange(DebugLoc Start) : Start(std::move(Start)), End(std::move(Start)) {}
9
Object 'Start' is moved
10
Moved-from object 'Start' is moved
474 LocRange(DebugLoc Start, DebugLoc End)
475 : Start(std::move(Start)), End(std::move(End)) {}
476
477 const DebugLoc &getStart() const { return Start; }
478 const DebugLoc &getEnd() const { return End; }
479
480 /// Check for null.
481 ///
482 explicit operator bool() const { return Start && End; }
483 };
484
485 /// Return true if the specified value is loop invariant.
486 bool isLoopInvariant(const Value *V) const;
487
488 /// Return true if all the operands of the specified instruction are loop
489 /// invariant.
490 bool hasLoopInvariantOperands(const Instruction *I) const;
491
492 /// If the given value is an instruction inside of the loop and it can be
493 /// hoisted, do so to make it trivially loop-invariant.
494 /// Return true if the value after any hoisting is loop invariant. This
495 /// function can be used as a slightly more aggressive replacement for
496 /// isLoopInvariant.
497 ///
498 /// If InsertPt is specified, it is the point to hoist instructions to.
499 /// If null, the terminator of the loop preheader is used.
500 bool makeLoopInvariant(Value *V, bool &Changed,
501 Instruction *InsertPt = nullptr) const;
502
503 /// If the given instruction is inside of the loop and it can be hoisted, do
504 /// so to make it trivially loop-invariant.
505 /// Return true if the instruction after any hoisting is loop invariant. This
506 /// function can be used as a slightly more aggressive replacement for
507 /// isLoopInvariant.
508 ///
509 /// If InsertPt is specified, it is the point to hoist instructions to.
510 /// If null, the terminator of the loop preheader is used.
511 ///
512 bool makeLoopInvariant(Instruction *I, bool &Changed,
513 Instruction *InsertPt = nullptr) const;
514
515 /// Check to see if the loop has a canonical induction variable: an integer
516 /// recurrence that starts at 0 and increments by one each time through the
517 /// loop. If so, return the phi node that corresponds to it.
518 ///
519 /// The IndVarSimplify pass transforms loops to have a canonical induction
520 /// variable.
521 ///
522 PHINode *getCanonicalInductionVariable() const;
523
524 /// Obtain the unique incoming and back edge. Return false if they are
525 /// non-unique or the loop is dead; otherwise, return true.
526 bool getIncomingAndBackEdge(BasicBlock *&Incoming,
527 BasicBlock *&Backedge) const;
528
529 /// Return true if the Loop is in LCSSA form.
530 bool isLCSSAForm(DominatorTree &DT) const;
531
532 /// Return true if this Loop and all inner subloops are in LCSSA form.
533 bool isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const;
534
535 /// Return true if the Loop is in the form that the LoopSimplify form
536 /// transforms loops to, which is sometimes called normal form.
537 bool isLoopSimplifyForm() const;
538
539 /// Return true if the loop body is safe to clone in practice.
540 bool isSafeToClone() const;
541
542 /// Returns true if the loop is annotated parallel.
543 ///
544 /// A parallel loop can be assumed to not contain any dependencies between
545 /// iterations by the compiler. That is, any loop-carried dependency checking
546 /// can be skipped completely when parallelizing the loop on the target
547 /// machine. Thus, if the parallel loop information originates from the
548 /// programmer, e.g. via the OpenMP parallel for pragma, it is the
549 /// programmer's responsibility to ensure there are no loop-carried
550 /// dependencies. The final execution order of the instructions across
551 /// iterations is not guaranteed, thus, the end result might or might not
552 /// implement actual concurrent execution of instructions across multiple
553 /// iterations.
554 bool isAnnotatedParallel() const;
555
556 /// Return the llvm.loop loop id metadata node for this loop if it is present.
557 ///
558 /// If this loop contains the same llvm.loop metadata on each branch to the
559 /// header then the node is returned. If any latch instruction does not
560 /// contain llvm.loop or if multiple latches contain different nodes then
561 /// 0 is returned.
562 MDNode *getLoopID() const;
563 /// Set the llvm.loop loop id metadata for this loop.
564 ///
565 /// The LoopID metadata node will be added to each terminator instruction in
566 /// the loop that branches to the loop header.
567 ///
568 /// The LoopID metadata node should have one or more operands and the first
569 /// operand should be the node itself.
570 void setLoopID(MDNode *LoopID) const;
571
572 /// Add llvm.loop.unroll.disable to this loop's loop id metadata.
573 ///
574 /// Remove existing unroll metadata and add unroll disable metadata to
575 /// indicate the loop has already been unrolled. This prevents a loop
576 /// from being unrolled more than is directed by a pragma if the loop
577 /// unrolling pass is run more than once (which it generally is).
578 void setLoopAlreadyUnrolled();
579
580 void dump() const;
581 void dumpVerbose() const;
582
583 /// Return the debug location of the start of this loop.
584 /// This looks for a BB terminating instruction with a known debug
585 /// location by looking at the preheader and header blocks. If it
586 /// cannot find a terminating instruction with location information,
587 /// it returns an unknown location.
588 DebugLoc getStartLoc() const;
589
590 /// Return the source code span of the loop.
591 LocRange getLocRange() const;
592
593 StringRef getName() const {
594 if (BasicBlock *Header = getHeader())
595 if (Header->hasName())
596 return Header->getName();
597 return "<unnamed loop>";
598 }
599
600private:
601 Loop() = default;
602
603 friend class LoopInfoBase<BasicBlock, Loop>;
604 friend class LoopBase<BasicBlock, Loop>;
605 explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
606 ~Loop() = default;
607};
608
609//===----------------------------------------------------------------------===//
610/// This class builds and contains all of the top-level loop
611/// structures in the specified function.
612///
613
614template <class BlockT, class LoopT> class LoopInfoBase {
615 // BBMap - Mapping of basic blocks to the inner most loop they occur in
616 DenseMap<const BlockT *, LoopT *> BBMap;
617 std::vector<LoopT *> TopLevelLoops;
618 BumpPtrAllocator LoopAllocator;
619
620 friend class LoopBase<BlockT, LoopT>;
621 friend class LoopInfo;
622
623 void operator=(const LoopInfoBase &) = delete;
624 LoopInfoBase(const LoopInfoBase &) = delete;
625
626public:
627 LoopInfoBase() {}
628 ~LoopInfoBase() { releaseMemory(); }
629
630 LoopInfoBase(LoopInfoBase &&Arg)
631 : BBMap(std::move(Arg.BBMap)),
632 TopLevelLoops(std::move(Arg.TopLevelLoops)),
633 LoopAllocator(std::move(Arg.LoopAllocator)) {
634 // We have to clear the arguments top level loops as we've taken ownership.
635 Arg.TopLevelLoops.clear();
636 }
637 LoopInfoBase &operator=(LoopInfoBase &&RHS) {
638 BBMap = std::move(RHS.BBMap);
639
640 for (auto *L : TopLevelLoops)
641 L->~LoopT();
642
643 TopLevelLoops = std::move(RHS.TopLevelLoops);
644 LoopAllocator = std::move(RHS.LoopAllocator);
645 RHS.TopLevelLoops.clear();
646 return *this;
647 }
648
649 void releaseMemory() {
650 BBMap.clear();
651
652 for (auto *L : TopLevelLoops)
653 L->~LoopT();
654 TopLevelLoops.clear();
655 LoopAllocator.Reset();
656 }
657
658 template <typename... ArgsTy> LoopT *AllocateLoop(ArgsTy &&... Args) {
659 LoopT *Storage = LoopAllocator.Allocate<LoopT>();
660 return new (Storage) LoopT(std::forward<ArgsTy>(Args)...);
661 }
662
663 /// iterator/begin/end - The interface to the top-level loops in the current
664 /// function.
665 ///
666 typedef typename std::vector<LoopT *>::const_iterator iterator;
667 typedef
668 typename std::vector<LoopT *>::const_reverse_iterator reverse_iterator;
669 iterator begin() const { return TopLevelLoops.begin(); }
670 iterator end() const { return TopLevelLoops.end(); }
671 reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
672 reverse_iterator rend() const { return TopLevelLoops.rend(); }
673 bool empty() const { return TopLevelLoops.empty(); }
674
675 /// Return all of the loops in the function in preorder across the loop
676 /// nests, with siblings in forward program order.
677 ///
678 /// Note that because loops form a forest of trees, preorder is equivalent to
679 /// reverse postorder.
680 SmallVector<LoopT *, 4> getLoopsInPreorder();
681
682 /// Return all of the loops in the function in preorder across the loop
683 /// nests, with siblings in *reverse* program order.
684 ///
685 /// Note that because loops form a forest of trees, preorder is equivalent to
686 /// reverse postorder.
687 ///
688 /// Also note that this is *not* a reverse preorder. Only the siblings are in
689 /// reverse program order.
690 SmallVector<LoopT *, 4> getLoopsInReverseSiblingPreorder();
691
692 /// Return the inner most loop that BB lives in. If a basic block is in no
693 /// loop (for example the entry node), null is returned.
694 LoopT *getLoopFor(const BlockT *BB) const { return BBMap.lookup(BB); }
695
696 /// Same as getLoopFor.
697 const LoopT *operator[](const BlockT *BB) const { return getLoopFor(BB); }
698
699 /// Return the loop nesting level of the specified block. A depth of 0 means
700 /// the block is not inside any loop.
701 unsigned getLoopDepth(const BlockT *BB) const {
702 const LoopT *L = getLoopFor(BB);
703 return L ? L->getLoopDepth() : 0;
704 }
705
706 // True if the block is a loop header node
707 bool isLoopHeader(const BlockT *BB) const {
708 const LoopT *L = getLoopFor(BB);
709 return L && L->getHeader() == BB;
710 }
711
712 /// This removes the specified top-level loop from this loop info object.
713 /// The loop is not deleted, as it will presumably be inserted into
714 /// another loop.
715 LoopT *removeLoop(iterator I) {
716 assert(I != end() && "Cannot remove end iterator!")((I != end() && "Cannot remove end iterator!") ? static_cast
<void> (0) : __assert_fail ("I != end() && \"Cannot remove end iterator!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 716, __PRETTY_FUNCTION__))
;
717 LoopT *L = *I;
718 assert(!L->getParentLoop() && "Not a top-level loop!")((!L->getParentLoop() && "Not a top-level loop!") ?
static_cast<void> (0) : __assert_fail ("!L->getParentLoop() && \"Not a top-level loop!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 718, __PRETTY_FUNCTION__))
;
719 TopLevelLoops.erase(TopLevelLoops.begin() + (I - begin()));
720 return L;
721 }
722
723 /// Change the top-level loop that contains BB to the specified loop.
724 /// This should be used by transformations that restructure the loop hierarchy
725 /// tree.
726 void changeLoopFor(BlockT *BB, LoopT *L) {
727 if (!L) {
728 BBMap.erase(BB);
729 return;
730 }
731 BBMap[BB] = L;
732 }
733
734 /// Replace the specified loop in the top-level loops list with the indicated
735 /// loop.
736 void changeTopLevelLoop(LoopT *OldLoop, LoopT *NewLoop) {
737 auto I = find(TopLevelLoops, OldLoop);
738 assert(I != TopLevelLoops.end() && "Old loop not at top level!")((I != TopLevelLoops.end() && "Old loop not at top level!"
) ? static_cast<void> (0) : __assert_fail ("I != TopLevelLoops.end() && \"Old loop not at top level!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 738, __PRETTY_FUNCTION__))
;
739 *I = NewLoop;
740 assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&((!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
"Loops already embedded into a subloop!") ? static_cast<void
> (0) : __assert_fail ("!NewLoop->ParentLoop && !OldLoop->ParentLoop && \"Loops already embedded into a subloop!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 741, __PRETTY_FUNCTION__))
741 "Loops already embedded into a subloop!")((!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
"Loops already embedded into a subloop!") ? static_cast<void
> (0) : __assert_fail ("!NewLoop->ParentLoop && !OldLoop->ParentLoop && \"Loops already embedded into a subloop!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 741, __PRETTY_FUNCTION__))
;
742 }
743
744 /// This adds the specified loop to the collection of top-level loops.
745 void addTopLevelLoop(LoopT *New) {
746 assert(!New->getParentLoop() && "Loop already in subloop!")((!New->getParentLoop() && "Loop already in subloop!"
) ? static_cast<void> (0) : __assert_fail ("!New->getParentLoop() && \"Loop already in subloop!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 746, __PRETTY_FUNCTION__))
;
747 TopLevelLoops.push_back(New);
748 }
749
750 /// This method completely removes BB from all data structures,
751 /// including all of the Loop objects it is nested in and our mapping from
752 /// BasicBlocks to loops.
753 void removeBlock(BlockT *BB) {
754 auto I = BBMap.find(BB);
755 if (I != BBMap.end()) {
756 for (LoopT *L = I->second; L; L = L->getParentLoop())
757 L->removeBlockFromLoop(BB);
758
759 BBMap.erase(I);
760 }
761 }
762
763 // Internals
764
765 static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
766 const LoopT *ParentLoop) {
767 if (!SubLoop)
768 return true;
769 if (SubLoop == ParentLoop)
770 return false;
771 return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
772 }
773
774 /// Create the loop forest using a stable algorithm.
775 void analyze(const DominatorTreeBase<BlockT, false> &DomTree);
776
777 // Debugging
778 void print(raw_ostream &OS) const;
779
780 void verify(const DominatorTreeBase<BlockT, false> &DomTree) const;
781
782 /// Destroy a loop that has been removed from the `LoopInfo` nest.
783 ///
784 /// This runs the destructor of the loop object making it invalid to
785 /// reference afterward. The memory is retained so that the *pointer* to the
786 /// loop remains valid.
787 ///
788 /// The caller is responsible for removing this loop from the loop nest and
789 /// otherwise disconnecting it from the broader `LoopInfo` data structures.
790 /// Callers that don't naturally handle this themselves should probably call
791 /// `erase' instead.
792 void destroy(LoopT *L) {
793 L->~LoopT();
794
795 // Since LoopAllocator is a BumpPtrAllocator, this Deallocate only poisons
796 // \c L, but the pointer remains valid for non-dereferencing uses.
797 LoopAllocator.Deallocate(L);
798 }
799};
800
801// Implementation in LoopInfoImpl.h
802extern template class LoopInfoBase<BasicBlock, Loop>;
803
804class LoopInfo : public LoopInfoBase<BasicBlock, Loop> {
805 typedef LoopInfoBase<BasicBlock, Loop> BaseT;
806
807 friend class LoopBase<BasicBlock, Loop>;
808
809 void operator=(const LoopInfo &) = delete;
810 LoopInfo(const LoopInfo &) = delete;
811
812public:
813 LoopInfo() {}
814 explicit LoopInfo(const DominatorTreeBase<BasicBlock, false> &DomTree);
815
816 LoopInfo(LoopInfo &&Arg) : BaseT(std::move(static_cast<BaseT &>(Arg))) {}
817 LoopInfo &operator=(LoopInfo &&RHS) {
818 BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
819 return *this;
820 }
821
822 /// Handle invalidation explicitly.
823 bool invalidate(Function &F, const PreservedAnalyses &PA,
824 FunctionAnalysisManager::Invalidator &);
825
826 // Most of the public interface is provided via LoopInfoBase.
827
828 /// Update LoopInfo after removing the last backedge from a loop. This updates
829 /// the loop forest and parent loops for each block so that \c L is no longer
830 /// referenced, but does not actually delete \c L immediately. The pointer
831 /// will remain valid until this LoopInfo's memory is released.
832 void erase(Loop *L);
833
834 /// Returns true if replacing From with To everywhere is guaranteed to
835 /// preserve LCSSA form.
836 bool replacementPreservesLCSSAForm(Instruction *From, Value *To) {
837 // Preserving LCSSA form is only problematic if the replacing value is an
838 // instruction.
839 Instruction *I = dyn_cast<Instruction>(To);
840 if (!I)
841 return true;
842 // If both instructions are defined in the same basic block then replacement
843 // cannot break LCSSA form.
844 if (I->getParent() == From->getParent())
845 return true;
846 // If the instruction is not defined in a loop then it can safely replace
847 // anything.
848 Loop *ToLoop = getLoopFor(I->getParent());
849 if (!ToLoop)
850 return true;
851 // If the replacing instruction is defined in the same loop as the original
852 // instruction, or in a loop that contains it as an inner loop, then using
853 // it as a replacement will not break LCSSA form.
854 return ToLoop->contains(getLoopFor(From->getParent()));
855 }
856
857 /// Checks if moving a specific instruction can break LCSSA in any loop.
858 ///
859 /// Return true if moving \p Inst to before \p NewLoc will break LCSSA,
860 /// assuming that the function containing \p Inst and \p NewLoc is currently
861 /// in LCSSA form.
862 bool movementPreservesLCSSAForm(Instruction *Inst, Instruction *NewLoc) {
863 assert(Inst->getFunction() == NewLoc->getFunction() &&((Inst->getFunction() == NewLoc->getFunction() &&
"Can't reason about IPO!") ? static_cast<void> (0) : __assert_fail
("Inst->getFunction() == NewLoc->getFunction() && \"Can't reason about IPO!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 864, __PRETTY_FUNCTION__))
864 "Can't reason about IPO!")((Inst->getFunction() == NewLoc->getFunction() &&
"Can't reason about IPO!") ? static_cast<void> (0) : __assert_fail
("Inst->getFunction() == NewLoc->getFunction() && \"Can't reason about IPO!\""
, "/build/llvm-toolchain-snapshot-9~svn359999/include/llvm/Analysis/LoopInfo.h"
, 864, __PRETTY_FUNCTION__))
;
865
866 auto *OldBB = Inst->getParent();
867 auto *NewBB = NewLoc->getParent();
868
869 // Movement within the same loop does not break LCSSA (the equality check is
870 // to avoid doing a hashtable lookup in case of intra-block movement).
871 if (OldBB == NewBB)
872 return true;
873
874 auto *OldLoop = getLoopFor(OldBB);
875 auto *NewLoop = getLoopFor(NewBB);
876
877 if (OldLoop == NewLoop)
878 return true;
879
880 // Check if Outer contains Inner; with the null loop counting as the
881 // "outermost" loop.
882 auto Contains = [](const Loop *Outer, const Loop *Inner) {
883 return !Outer || Outer->contains(Inner);
884 };
885
886 // To check that the movement of Inst to before NewLoc does not break LCSSA,
887 // we need to check two sets of uses for possible LCSSA violations at
888 // NewLoc: the users of NewInst, and the operands of NewInst.
889
890 // If we know we're hoisting Inst out of an inner loop to an outer loop,
891 // then the uses *of* Inst don't need to be checked.
892
893 if (!Contains(NewLoop, OldLoop)) {
894 for (Use &U : Inst->uses()) {
895 auto *UI = cast<Instruction>(U.getUser());
896 auto *UBB = isa<PHINode>(UI) ? cast<PHINode>(UI)->getIncomingBlock(U)
897 : UI->getParent();
898 if (UBB != NewBB && getLoopFor(UBB) != NewLoop)
899 return false;
900 }
901 }
902
903 // If we know we're sinking Inst from an outer loop into an inner loop, then
904 // the *operands* of Inst don't need to be checked.
905
906 if (!Contains(OldLoop, NewLoop)) {
907 // See below on why we can't handle phi nodes here.
908 if (isa<PHINode>(Inst))
909 return false;
910
911 for (Use &U : Inst->operands()) {
912 auto *DefI = dyn_cast<Instruction>(U.get());
913 if (!DefI)
914 return false;
915
916 // This would need adjustment if we allow Inst to be a phi node -- the
917 // new use block won't simply be NewBB.
918
919 auto *DefBlock = DefI->getParent();
920 if (DefBlock != NewBB && getLoopFor(DefBlock) != NewLoop)
921 return false;
922 }
923 }
924
925 return true;
926 }
927};
928
929// Allow clients to walk the list of nested loops...
930template <> struct GraphTraits<const Loop *> {
931 typedef const Loop *NodeRef;
932 typedef LoopInfo::iterator ChildIteratorType;
933
934 static NodeRef getEntryNode(const Loop *L) { return L; }
935 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
936 static ChildIteratorType child_end(NodeRef N) { return N->end(); }
937};
938
939template <> struct GraphTraits<Loop *> {
940 typedef Loop *NodeRef;
941 typedef LoopInfo::iterator ChildIteratorType;
942
943 static NodeRef getEntryNode(Loop *L) { return L; }
944 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
945 static ChildIteratorType child_end(NodeRef N) { return N->end(); }
946};
947
948/// Analysis pass that exposes the \c LoopInfo for a function.
949class LoopAnalysis : public AnalysisInfoMixin<LoopAnalysis> {
950 friend AnalysisInfoMixin<LoopAnalysis>;
951 static AnalysisKey Key;
952
953public:
954 typedef LoopInfo Result;
955
956 LoopInfo run(Function &F, FunctionAnalysisManager &AM);
957};
958
959/// Printer pass for the \c LoopAnalysis results.
960class LoopPrinterPass : public PassInfoMixin<LoopPrinterPass> {
961 raw_ostream &OS;
962
963public:
964 explicit LoopPrinterPass(raw_ostream &OS) : OS(OS) {}
965 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
966};
967
968/// Verifier pass for the \c LoopAnalysis results.
969struct LoopVerifierPass : public PassInfoMixin<LoopVerifierPass> {
970 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
971};
972
973/// The legacy pass manager's analysis pass to compute loop information.
974class LoopInfoWrapperPass : public FunctionPass {
975 LoopInfo LI;
976
977public:
978 static char ID; // Pass identification, replacement for typeid
979
980 LoopInfoWrapperPass() : FunctionPass(ID) {
981 initializeLoopInfoWrapperPassPass(*PassRegistry::getPassRegistry());
982 }
983
984 LoopInfo &getLoopInfo() { return LI; }
985 const LoopInfo &getLoopInfo() const { return LI; }
986
987 /// Calculate the natural loop information for a given function.
988 bool runOnFunction(Function &F) override;
989
990 void verifyAnalysis() const override;
991
992 void releaseMemory() override { LI.releaseMemory(); }
993
994 void print(raw_ostream &O, const Module *M = nullptr) const override;
995
996 void getAnalysisUsage(AnalysisUsage &AU) const override;
997};
998
999/// Function to print a loop's contents as LLVM's text IR assembly.
1000void printLoop(Loop &L, raw_ostream &OS, const std::string &Banner = "");
1001
1002/// Find and return the loop attribute node for the attribute @p Name in
1003/// @p LoopID. Return nullptr if there is no such attribute.
1004MDNode *findOptionMDForLoopID(MDNode *LoopID, StringRef Name);
1005
1006/// Find string metadata for a loop.
1007///
1008/// Returns the MDNode where the first operand is the metadata's name. The
1009/// following operands are the metadata's values. If no metadata with @p Name is
1010/// found, return nullptr.
1011MDNode *findOptionMDForLoop(const Loop *TheLoop, StringRef Name);
1012
1013/// Return whether an MDNode might represent an access group.
1014///
1015/// Access group metadata nodes have to be distinct and empty. Being
1016/// always-empty ensures that it never needs to be changed (which -- because
1017/// MDNodes are designed immutable -- would require creating a new MDNode). Note
1018/// that this is not a sufficient condition: not every distinct and empty NDNode
1019/// is representing an access group.
1020bool isValidAsAccessGroup(MDNode *AccGroup);
1021
1022/// Create a new LoopID after the loop has been transformed.
1023///
1024/// This can be used when no follow-up loop attributes are defined
1025/// (llvm::makeFollowupLoopID returning None) to stop transformations to be
1026/// applied again.
1027///
1028/// @param Context The LLVMContext in which to create the new LoopID.
1029/// @param OrigLoopID The original LoopID; can be nullptr if the original
1030/// loop has no LoopID.
1031/// @param RemovePrefixes Remove all loop attributes that have these prefixes.
1032/// Use to remove metadata of the transformation that has
1033/// been applied.
1034/// @param AddAttrs Add these loop attributes to the new LoopID.
1035///
1036/// @return A new LoopID that can be applied using Loop::setLoopID().
1037llvm::MDNode *
1038makePostTransformationMetadata(llvm::LLVMContext &Context, MDNode *OrigLoopID,
1039 llvm::ArrayRef<llvm::StringRef> RemovePrefixes,
1040 llvm::ArrayRef<llvm::MDNode *> AddAttrs);
1041
1042} // End llvm namespace
1043
1044#endif