1 //===- LoopInfo.cpp - Natural Loop Calculator -----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the LoopInfo class that is used to identify natural loops
10 // and determine the loop depth of various nodes of the CFG. Note that the
11 // loops identified may actually be several natural loops that share the same
12 // header node... not just a single natural loop.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Analysis/LoopInfo.h"
17 #include "llvm/ADT/DepthFirstIterator.h"
18 #include "llvm/ADT/ScopeExit.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/Analysis/IVDescriptors.h"
21 #include "llvm/Analysis/LoopInfoImpl.h"
22 #include "llvm/Analysis/LoopIterator.h"
23 #include "llvm/Analysis/MemorySSA.h"
24 #include "llvm/Analysis/MemorySSAUpdater.h"
25 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
26 #include "llvm/Analysis/ValueTracking.h"
27 #include "llvm/Config/llvm-config.h"
28 #include "llvm/IR/CFG.h"
29 #include "llvm/IR/Constants.h"
30 #include "llvm/IR/DebugLoc.h"
31 #include "llvm/IR/Dominators.h"
32 #include "llvm/IR/IRPrintingPasses.h"
33 #include "llvm/IR/Instructions.h"
34 #include "llvm/IR/LLVMContext.h"
35 #include "llvm/IR/Metadata.h"
36 #include "llvm/IR/PassManager.h"
37 #include "llvm/IR/PrintPasses.h"
38 #include "llvm/InitializePasses.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include <algorithm>
43 using namespace llvm;
44
45 // Explicitly instantiate methods in LoopInfoImpl.h for IR-level Loops.
46 template class llvm::LoopBase<BasicBlock, Loop>;
47 template class llvm::LoopInfoBase<BasicBlock, Loop>;
48
49 // Always verify loopinfo if expensive checking is enabled.
50 #ifdef EXPENSIVE_CHECKS
51 bool llvm::VerifyLoopInfo = true;
52 #else
53 bool llvm::VerifyLoopInfo = false;
54 #endif
55 static cl::opt<bool, true>
56 VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo),
57 cl::Hidden, cl::desc("Verify loop info (time consuming)"));
58
59 //===----------------------------------------------------------------------===//
60 // Loop implementation
61 //
62
isLoopInvariant(const Value * V) const63 bool Loop::isLoopInvariant(const Value *V) const {
64 if (const Instruction *I = dyn_cast<Instruction>(V))
65 return !contains(I);
66 return true; // All non-instructions are loop invariant
67 }
68
hasLoopInvariantOperands(const Instruction * I) const69 bool Loop::hasLoopInvariantOperands(const Instruction *I) const {
70 return all_of(I->operands(), [this](Value *V) { return isLoopInvariant(V); });
71 }
72
makeLoopInvariant(Value * V,bool & Changed,Instruction * InsertPt,MemorySSAUpdater * MSSAU) const73 bool Loop::makeLoopInvariant(Value *V, bool &Changed, Instruction *InsertPt,
74 MemorySSAUpdater *MSSAU) const {
75 if (Instruction *I = dyn_cast<Instruction>(V))
76 return makeLoopInvariant(I, Changed, InsertPt, MSSAU);
77 return true; // All non-instructions are loop-invariant.
78 }
79
makeLoopInvariant(Instruction * I,bool & Changed,Instruction * InsertPt,MemorySSAUpdater * MSSAU) const80 bool Loop::makeLoopInvariant(Instruction *I, bool &Changed,
81 Instruction *InsertPt,
82 MemorySSAUpdater *MSSAU) const {
83 // Test if the value is already loop-invariant.
84 if (isLoopInvariant(I))
85 return true;
86 if (!isSafeToSpeculativelyExecute(I))
87 return false;
88 if (I->mayReadFromMemory())
89 return false;
90 // EH block instructions are immobile.
91 if (I->isEHPad())
92 return false;
93 // Determine the insertion point, unless one was given.
94 if (!InsertPt) {
95 BasicBlock *Preheader = getLoopPreheader();
96 // Without a preheader, hoisting is not feasible.
97 if (!Preheader)
98 return false;
99 InsertPt = Preheader->getTerminator();
100 }
101 // Don't hoist instructions with loop-variant operands.
102 for (Value *Operand : I->operands())
103 if (!makeLoopInvariant(Operand, Changed, InsertPt, MSSAU))
104 return false;
105
106 // Hoist.
107 I->moveBefore(InsertPt);
108 if (MSSAU)
109 if (auto *MUD = MSSAU->getMemorySSA()->getMemoryAccess(I))
110 MSSAU->moveToPlace(MUD, InsertPt->getParent(),
111 MemorySSA::BeforeTerminator);
112
113 // There is possibility of hoisting this instruction above some arbitrary
114 // condition. Any metadata defined on it can be control dependent on this
115 // condition. Conservatively strip it here so that we don't give any wrong
116 // information to the optimizer.
117 I->dropUnknownNonDebugMetadata();
118
119 Changed = true;
120 return true;
121 }
122
getIncomingAndBackEdge(BasicBlock * & Incoming,BasicBlock * & Backedge) const123 bool Loop::getIncomingAndBackEdge(BasicBlock *&Incoming,
124 BasicBlock *&Backedge) const {
125 BasicBlock *H = getHeader();
126
127 Incoming = nullptr;
128 Backedge = nullptr;
129 pred_iterator PI = pred_begin(H);
130 assert(PI != pred_end(H) && "Loop must have at least one backedge!");
131 Backedge = *PI++;
132 if (PI == pred_end(H))
133 return false; // dead loop
134 Incoming = *PI++;
135 if (PI != pred_end(H))
136 return false; // multiple backedges?
137
138 if (contains(Incoming)) {
139 if (contains(Backedge))
140 return false;
141 std::swap(Incoming, Backedge);
142 } else if (!contains(Backedge))
143 return false;
144
145 assert(Incoming && Backedge && "expected non-null incoming and backedges");
146 return true;
147 }
148
getCanonicalInductionVariable() const149 PHINode *Loop::getCanonicalInductionVariable() const {
150 BasicBlock *H = getHeader();
151
152 BasicBlock *Incoming = nullptr, *Backedge = nullptr;
153 if (!getIncomingAndBackEdge(Incoming, Backedge))
154 return nullptr;
155
156 // Loop over all of the PHI nodes, looking for a canonical indvar.
157 for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) {
158 PHINode *PN = cast<PHINode>(I);
159 if (ConstantInt *CI =
160 dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming)))
161 if (CI->isZero())
162 if (Instruction *Inc =
163 dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
164 if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN)
165 if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
166 if (CI->isOne())
167 return PN;
168 }
169 return nullptr;
170 }
171
172 /// Get the latch condition instruction.
getLatchCmpInst(const Loop & L)173 static ICmpInst *getLatchCmpInst(const Loop &L) {
174 if (BasicBlock *Latch = L.getLoopLatch())
175 if (BranchInst *BI = dyn_cast_or_null<BranchInst>(Latch->getTerminator()))
176 if (BI->isConditional())
177 return dyn_cast<ICmpInst>(BI->getCondition());
178
179 return nullptr;
180 }
181
182 /// Return the final value of the loop induction variable if found.
findFinalIVValue(const Loop & L,const PHINode & IndVar,const Instruction & StepInst)183 static Value *findFinalIVValue(const Loop &L, const PHINode &IndVar,
184 const Instruction &StepInst) {
185 ICmpInst *LatchCmpInst = getLatchCmpInst(L);
186 if (!LatchCmpInst)
187 return nullptr;
188
189 Value *Op0 = LatchCmpInst->getOperand(0);
190 Value *Op1 = LatchCmpInst->getOperand(1);
191 if (Op0 == &IndVar || Op0 == &StepInst)
192 return Op1;
193
194 if (Op1 == &IndVar || Op1 == &StepInst)
195 return Op0;
196
197 return nullptr;
198 }
199
getBounds(const Loop & L,PHINode & IndVar,ScalarEvolution & SE)200 Optional<Loop::LoopBounds> Loop::LoopBounds::getBounds(const Loop &L,
201 PHINode &IndVar,
202 ScalarEvolution &SE) {
203 InductionDescriptor IndDesc;
204 if (!InductionDescriptor::isInductionPHI(&IndVar, &L, &SE, IndDesc))
205 return None;
206
207 Value *InitialIVValue = IndDesc.getStartValue();
208 Instruction *StepInst = IndDesc.getInductionBinOp();
209 if (!InitialIVValue || !StepInst)
210 return None;
211
212 const SCEV *Step = IndDesc.getStep();
213 Value *StepInstOp1 = StepInst->getOperand(1);
214 Value *StepInstOp0 = StepInst->getOperand(0);
215 Value *StepValue = nullptr;
216 if (SE.getSCEV(StepInstOp1) == Step)
217 StepValue = StepInstOp1;
218 else if (SE.getSCEV(StepInstOp0) == Step)
219 StepValue = StepInstOp0;
220
221 Value *FinalIVValue = findFinalIVValue(L, IndVar, *StepInst);
222 if (!FinalIVValue)
223 return None;
224
225 return LoopBounds(L, *InitialIVValue, *StepInst, StepValue, *FinalIVValue,
226 SE);
227 }
228
229 using Direction = Loop::LoopBounds::Direction;
230
getCanonicalPredicate() const231 ICmpInst::Predicate Loop::LoopBounds::getCanonicalPredicate() const {
232 BasicBlock *Latch = L.getLoopLatch();
233 assert(Latch && "Expecting valid latch");
234
235 BranchInst *BI = dyn_cast_or_null<BranchInst>(Latch->getTerminator());
236 assert(BI && BI->isConditional() && "Expecting conditional latch branch");
237
238 ICmpInst *LatchCmpInst = dyn_cast<ICmpInst>(BI->getCondition());
239 assert(LatchCmpInst &&
240 "Expecting the latch compare instruction to be a CmpInst");
241
242 // Need to inverse the predicate when first successor is not the loop
243 // header
244 ICmpInst::Predicate Pred = (BI->getSuccessor(0) == L.getHeader())
245 ? LatchCmpInst->getPredicate()
246 : LatchCmpInst->getInversePredicate();
247
248 if (LatchCmpInst->getOperand(0) == &getFinalIVValue())
249 Pred = ICmpInst::getSwappedPredicate(Pred);
250
251 // Need to flip strictness of the predicate when the latch compare instruction
252 // is not using StepInst
253 if (LatchCmpInst->getOperand(0) == &getStepInst() ||
254 LatchCmpInst->getOperand(1) == &getStepInst())
255 return Pred;
256
257 // Cannot flip strictness of NE and EQ
258 if (Pred != ICmpInst::ICMP_NE && Pred != ICmpInst::ICMP_EQ)
259 return ICmpInst::getFlippedStrictnessPredicate(Pred);
260
261 Direction D = getDirection();
262 if (D == Direction::Increasing)
263 return ICmpInst::ICMP_SLT;
264
265 if (D == Direction::Decreasing)
266 return ICmpInst::ICMP_SGT;
267
268 // If cannot determine the direction, then unable to find the canonical
269 // predicate
270 return ICmpInst::BAD_ICMP_PREDICATE;
271 }
272
getDirection() const273 Direction Loop::LoopBounds::getDirection() const {
274 if (const SCEVAddRecExpr *StepAddRecExpr =
275 dyn_cast<SCEVAddRecExpr>(SE.getSCEV(&getStepInst())))
276 if (const SCEV *StepRecur = StepAddRecExpr->getStepRecurrence(SE)) {
277 if (SE.isKnownPositive(StepRecur))
278 return Direction::Increasing;
279 if (SE.isKnownNegative(StepRecur))
280 return Direction::Decreasing;
281 }
282
283 return Direction::Unknown;
284 }
285
getBounds(ScalarEvolution & SE) const286 Optional<Loop::LoopBounds> Loop::getBounds(ScalarEvolution &SE) const {
287 if (PHINode *IndVar = getInductionVariable(SE))
288 return LoopBounds::getBounds(*this, *IndVar, SE);
289
290 return None;
291 }
292
getInductionVariable(ScalarEvolution & SE) const293 PHINode *Loop::getInductionVariable(ScalarEvolution &SE) const {
294 if (!isLoopSimplifyForm())
295 return nullptr;
296
297 BasicBlock *Header = getHeader();
298 assert(Header && "Expected a valid loop header");
299 ICmpInst *CmpInst = getLatchCmpInst(*this);
300 if (!CmpInst)
301 return nullptr;
302
303 Instruction *LatchCmpOp0 = dyn_cast<Instruction>(CmpInst->getOperand(0));
304 Instruction *LatchCmpOp1 = dyn_cast<Instruction>(CmpInst->getOperand(1));
305
306 for (PHINode &IndVar : Header->phis()) {
307 InductionDescriptor IndDesc;
308 if (!InductionDescriptor::isInductionPHI(&IndVar, this, &SE, IndDesc))
309 continue;
310
311 Instruction *StepInst = IndDesc.getInductionBinOp();
312
313 // case 1:
314 // IndVar = phi[{InitialValue, preheader}, {StepInst, latch}]
315 // StepInst = IndVar + step
316 // cmp = StepInst < FinalValue
317 if (StepInst == LatchCmpOp0 || StepInst == LatchCmpOp1)
318 return &IndVar;
319
320 // case 2:
321 // IndVar = phi[{InitialValue, preheader}, {StepInst, latch}]
322 // StepInst = IndVar + step
323 // cmp = IndVar < FinalValue
324 if (&IndVar == LatchCmpOp0 || &IndVar == LatchCmpOp1)
325 return &IndVar;
326 }
327
328 return nullptr;
329 }
330
getInductionDescriptor(ScalarEvolution & SE,InductionDescriptor & IndDesc) const331 bool Loop::getInductionDescriptor(ScalarEvolution &SE,
332 InductionDescriptor &IndDesc) const {
333 if (PHINode *IndVar = getInductionVariable(SE))
334 return InductionDescriptor::isInductionPHI(IndVar, this, &SE, IndDesc);
335
336 return false;
337 }
338
isAuxiliaryInductionVariable(PHINode & AuxIndVar,ScalarEvolution & SE) const339 bool Loop::isAuxiliaryInductionVariable(PHINode &AuxIndVar,
340 ScalarEvolution &SE) const {
341 // Located in the loop header
342 BasicBlock *Header = getHeader();
343 if (AuxIndVar.getParent() != Header)
344 return false;
345
346 // No uses outside of the loop
347 for (User *U : AuxIndVar.users())
348 if (const Instruction *I = dyn_cast<Instruction>(U))
349 if (!contains(I))
350 return false;
351
352 InductionDescriptor IndDesc;
353 if (!InductionDescriptor::isInductionPHI(&AuxIndVar, this, &SE, IndDesc))
354 return false;
355
356 // The step instruction opcode should be add or sub.
357 if (IndDesc.getInductionOpcode() != Instruction::Add &&
358 IndDesc.getInductionOpcode() != Instruction::Sub)
359 return false;
360
361 // Incremented by a loop invariant step for each loop iteration
362 return SE.isLoopInvariant(IndDesc.getStep(), this);
363 }
364
getLoopGuardBranch() const365 BranchInst *Loop::getLoopGuardBranch() const {
366 if (!isLoopSimplifyForm())
367 return nullptr;
368
369 BasicBlock *Preheader = getLoopPreheader();
370 assert(Preheader && getLoopLatch() &&
371 "Expecting a loop with valid preheader and latch");
372
373 // Loop should be in rotate form.
374 if (!isRotatedForm())
375 return nullptr;
376
377 // Disallow loops with more than one unique exit block, as we do not verify
378 // that GuardOtherSucc post dominates all exit blocks.
379 BasicBlock *ExitFromLatch = getUniqueExitBlock();
380 if (!ExitFromLatch)
381 return nullptr;
382
383 BasicBlock *ExitFromLatchSucc = ExitFromLatch->getUniqueSuccessor();
384 if (!ExitFromLatchSucc)
385 return nullptr;
386
387 BasicBlock *GuardBB = Preheader->getUniquePredecessor();
388 if (!GuardBB)
389 return nullptr;
390
391 assert(GuardBB->getTerminator() && "Expecting valid guard terminator");
392
393 BranchInst *GuardBI = dyn_cast<BranchInst>(GuardBB->getTerminator());
394 if (!GuardBI || GuardBI->isUnconditional())
395 return nullptr;
396
397 BasicBlock *GuardOtherSucc = (GuardBI->getSuccessor(0) == Preheader)
398 ? GuardBI->getSuccessor(1)
399 : GuardBI->getSuccessor(0);
400 return (GuardOtherSucc == ExitFromLatchSucc) ? GuardBI : nullptr;
401 }
402
isCanonical(ScalarEvolution & SE) const403 bool Loop::isCanonical(ScalarEvolution &SE) const {
404 InductionDescriptor IndDesc;
405 if (!getInductionDescriptor(SE, IndDesc))
406 return false;
407
408 ConstantInt *Init = dyn_cast_or_null<ConstantInt>(IndDesc.getStartValue());
409 if (!Init || !Init->isZero())
410 return false;
411
412 if (IndDesc.getInductionOpcode() != Instruction::Add)
413 return false;
414
415 ConstantInt *Step = IndDesc.getConstIntStepValue();
416 if (!Step || !Step->isOne())
417 return false;
418
419 return true;
420 }
421
422 // Check that 'BB' doesn't have any uses outside of the 'L'
isBlockInLCSSAForm(const Loop & L,const BasicBlock & BB,const DominatorTree & DT)423 static bool isBlockInLCSSAForm(const Loop &L, const BasicBlock &BB,
424 const DominatorTree &DT) {
425 for (const Instruction &I : BB) {
426 // Tokens can't be used in PHI nodes and live-out tokens prevent loop
427 // optimizations, so for the purposes of considered LCSSA form, we
428 // can ignore them.
429 if (I.getType()->isTokenTy())
430 continue;
431
432 for (const Use &U : I.uses()) {
433 const Instruction *UI = cast<Instruction>(U.getUser());
434 const BasicBlock *UserBB = UI->getParent();
435
436 // For practical purposes, we consider that the use in a PHI
437 // occurs in the respective predecessor block. For more info,
438 // see the `phi` doc in LangRef and the LCSSA doc.
439 if (const PHINode *P = dyn_cast<PHINode>(UI))
440 UserBB = P->getIncomingBlock(U);
441
442 // Check the current block, as a fast-path, before checking whether
443 // the use is anywhere in the loop. Most values are used in the same
444 // block they are defined in. Also, blocks not reachable from the
445 // entry are special; uses in them don't need to go through PHIs.
446 if (UserBB != &BB && !L.contains(UserBB) &&
447 DT.isReachableFromEntry(UserBB))
448 return false;
449 }
450 }
451 return true;
452 }
453
isLCSSAForm(const DominatorTree & DT) const454 bool Loop::isLCSSAForm(const DominatorTree &DT) const {
455 // For each block we check that it doesn't have any uses outside of this loop.
456 return all_of(this->blocks(), [&](const BasicBlock *BB) {
457 return isBlockInLCSSAForm(*this, *BB, DT);
458 });
459 }
460
isRecursivelyLCSSAForm(const DominatorTree & DT,const LoopInfo & LI) const461 bool Loop::isRecursivelyLCSSAForm(const DominatorTree &DT,
462 const LoopInfo &LI) const {
463 // For each block we check that it doesn't have any uses outside of its
464 // innermost loop. This process will transitively guarantee that the current
465 // loop and all of the nested loops are in LCSSA form.
466 return all_of(this->blocks(), [&](const BasicBlock *BB) {
467 return isBlockInLCSSAForm(*LI.getLoopFor(BB), *BB, DT);
468 });
469 }
470
isLoopSimplifyForm() const471 bool Loop::isLoopSimplifyForm() const {
472 // Normal-form loops have a preheader, a single backedge, and all of their
473 // exits have all their predecessors inside the loop.
474 return getLoopPreheader() && getLoopLatch() && hasDedicatedExits();
475 }
476
477 // Routines that reform the loop CFG and split edges often fail on indirectbr.
isSafeToClone() const478 bool Loop::isSafeToClone() const {
479 // Return false if any loop blocks contain indirectbrs, or there are any calls
480 // to noduplicate functions.
481 // FIXME: it should be ok to clone CallBrInst's if we correctly update the
482 // operand list to reflect the newly cloned labels.
483 for (BasicBlock *BB : this->blocks()) {
484 if (isa<IndirectBrInst>(BB->getTerminator()) ||
485 isa<CallBrInst>(BB->getTerminator()))
486 return false;
487
488 for (Instruction &I : *BB)
489 if (auto *CB = dyn_cast<CallBase>(&I))
490 if (CB->cannotDuplicate())
491 return false;
492 }
493 return true;
494 }
495
getLoopID() const496 MDNode *Loop::getLoopID() const {
497 MDNode *LoopID = nullptr;
498
499 // Go through the latch blocks and check the terminator for the metadata.
500 SmallVector<BasicBlock *, 4> LatchesBlocks;
501 getLoopLatches(LatchesBlocks);
502 for (BasicBlock *BB : LatchesBlocks) {
503 Instruction *TI = BB->getTerminator();
504 MDNode *MD = TI->getMetadata(LLVMContext::MD_loop);
505
506 if (!MD)
507 return nullptr;
508
509 if (!LoopID)
510 LoopID = MD;
511 else if (MD != LoopID)
512 return nullptr;
513 }
514 if (!LoopID || LoopID->getNumOperands() == 0 ||
515 LoopID->getOperand(0) != LoopID)
516 return nullptr;
517 return LoopID;
518 }
519
setLoopID(MDNode * LoopID) const520 void Loop::setLoopID(MDNode *LoopID) const {
521 assert((!LoopID || LoopID->getNumOperands() > 0) &&
522 "Loop ID needs at least one operand");
523 assert((!LoopID || LoopID->getOperand(0) == LoopID) &&
524 "Loop ID should refer to itself");
525
526 SmallVector<BasicBlock *, 4> LoopLatches;
527 getLoopLatches(LoopLatches);
528 for (BasicBlock *BB : LoopLatches)
529 BB->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID);
530 }
531
setLoopAlreadyUnrolled()532 void Loop::setLoopAlreadyUnrolled() {
533 LLVMContext &Context = getHeader()->getContext();
534
535 MDNode *DisableUnrollMD =
536 MDNode::get(Context, MDString::get(Context, "llvm.loop.unroll.disable"));
537 MDNode *LoopID = getLoopID();
538 MDNode *NewLoopID = makePostTransformationMetadata(
539 Context, LoopID, {"llvm.loop.unroll."}, {DisableUnrollMD});
540 setLoopID(NewLoopID);
541 }
542
setLoopMustProgress()543 void Loop::setLoopMustProgress() {
544 LLVMContext &Context = getHeader()->getContext();
545
546 MDNode *MustProgress = findOptionMDForLoop(this, "llvm.loop.mustprogress");
547
548 if (MustProgress)
549 return;
550
551 MDNode *MustProgressMD =
552 MDNode::get(Context, MDString::get(Context, "llvm.loop.mustprogress"));
553 MDNode *LoopID = getLoopID();
554 MDNode *NewLoopID =
555 makePostTransformationMetadata(Context, LoopID, {}, {MustProgressMD});
556 setLoopID(NewLoopID);
557 }
558
isAnnotatedParallel() const559 bool Loop::isAnnotatedParallel() const {
560 MDNode *DesiredLoopIdMetadata = getLoopID();
561
562 if (!DesiredLoopIdMetadata)
563 return false;
564
565 MDNode *ParallelAccesses =
566 findOptionMDForLoop(this, "llvm.loop.parallel_accesses");
567 SmallPtrSet<MDNode *, 4>
568 ParallelAccessGroups; // For scalable 'contains' check.
569 if (ParallelAccesses) {
570 for (const MDOperand &MD : drop_begin(ParallelAccesses->operands(), 1)) {
571 MDNode *AccGroup = cast<MDNode>(MD.get());
572 assert(isValidAsAccessGroup(AccGroup) &&
573 "List item must be an access group");
574 ParallelAccessGroups.insert(AccGroup);
575 }
576 }
577
578 // The loop branch contains the parallel loop metadata. In order to ensure
579 // that any parallel-loop-unaware optimization pass hasn't added loop-carried
580 // dependencies (thus converted the loop back to a sequential loop), check
581 // that all the memory instructions in the loop belong to an access group that
582 // is parallel to this loop.
583 for (BasicBlock *BB : this->blocks()) {
584 for (Instruction &I : *BB) {
585 if (!I.mayReadOrWriteMemory())
586 continue;
587
588 if (MDNode *AccessGroup = I.getMetadata(LLVMContext::MD_access_group)) {
589 auto ContainsAccessGroup = [&ParallelAccessGroups](MDNode *AG) -> bool {
590 if (AG->getNumOperands() == 0) {
591 assert(isValidAsAccessGroup(AG) && "Item must be an access group");
592 return ParallelAccessGroups.count(AG);
593 }
594
595 for (const MDOperand &AccessListItem : AG->operands()) {
596 MDNode *AccGroup = cast<MDNode>(AccessListItem.get());
597 assert(isValidAsAccessGroup(AccGroup) &&
598 "List item must be an access group");
599 if (ParallelAccessGroups.count(AccGroup))
600 return true;
601 }
602 return false;
603 };
604
605 if (ContainsAccessGroup(AccessGroup))
606 continue;
607 }
608
609 // The memory instruction can refer to the loop identifier metadata
610 // directly or indirectly through another list metadata (in case of
611 // nested parallel loops). The loop identifier metadata refers to
612 // itself so we can check both cases with the same routine.
613 MDNode *LoopIdMD =
614 I.getMetadata(LLVMContext::MD_mem_parallel_loop_access);
615
616 if (!LoopIdMD)
617 return false;
618
619 bool LoopIdMDFound = false;
620 for (const MDOperand &MDOp : LoopIdMD->operands()) {
621 if (MDOp == DesiredLoopIdMetadata) {
622 LoopIdMDFound = true;
623 break;
624 }
625 }
626
627 if (!LoopIdMDFound)
628 return false;
629 }
630 }
631 return true;
632 }
633
getStartLoc() const634 DebugLoc Loop::getStartLoc() const { return getLocRange().getStart(); }
635
getLocRange() const636 Loop::LocRange Loop::getLocRange() const {
637 // If we have a debug location in the loop ID, then use it.
638 if (MDNode *LoopID = getLoopID()) {
639 DebugLoc Start;
640 // We use the first DebugLoc in the header as the start location of the loop
641 // and if there is a second DebugLoc in the header we use it as end location
642 // of the loop.
643 for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
644 if (DILocation *L = dyn_cast<DILocation>(LoopID->getOperand(i))) {
645 if (!Start)
646 Start = DebugLoc(L);
647 else
648 return LocRange(Start, DebugLoc(L));
649 }
650 }
651
652 if (Start)
653 return LocRange(Start);
654 }
655
656 // Try the pre-header first.
657 if (BasicBlock *PHeadBB = getLoopPreheader())
658 if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
659 return LocRange(DL);
660
661 // If we have no pre-header or there are no instructions with debug
662 // info in it, try the header.
663 if (BasicBlock *HeadBB = getHeader())
664 return LocRange(HeadBB->getTerminator()->getDebugLoc());
665
666 return LocRange();
667 }
668
669 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const670 LLVM_DUMP_METHOD void Loop::dump() const { print(dbgs()); }
671
dumpVerbose() const672 LLVM_DUMP_METHOD void Loop::dumpVerbose() const {
673 print(dbgs(), /*Depth=*/0, /*Verbose=*/true);
674 }
675 #endif
676
677 //===----------------------------------------------------------------------===//
678 // UnloopUpdater implementation
679 //
680
681 namespace {
682 /// Find the new parent loop for all blocks within the "unloop" whose last
683 /// backedges has just been removed.
684 class UnloopUpdater {
685 Loop &Unloop;
686 LoopInfo *LI;
687
688 LoopBlocksDFS DFS;
689
690 // Map unloop's immediate subloops to their nearest reachable parents. Nested
691 // loops within these subloops will not change parents. However, an immediate
692 // subloop's new parent will be the nearest loop reachable from either its own
693 // exits *or* any of its nested loop's exits.
694 DenseMap<Loop *, Loop *> SubloopParents;
695
696 // Flag the presence of an irreducible backedge whose destination is a block
697 // directly contained by the original unloop.
698 bool FoundIB;
699
700 public:
UnloopUpdater(Loop * UL,LoopInfo * LInfo)701 UnloopUpdater(Loop *UL, LoopInfo *LInfo)
702 : Unloop(*UL), LI(LInfo), DFS(UL), FoundIB(false) {}
703
704 void updateBlockParents();
705
706 void removeBlocksFromAncestors();
707
708 void updateSubloopParents();
709
710 protected:
711 Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop);
712 };
713 } // end anonymous namespace
714
715 /// Update the parent loop for all blocks that are directly contained within the
716 /// original "unloop".
updateBlockParents()717 void UnloopUpdater::updateBlockParents() {
718 if (Unloop.getNumBlocks()) {
719 // Perform a post order CFG traversal of all blocks within this loop,
720 // propagating the nearest loop from successors to predecessors.
721 LoopBlocksTraversal Traversal(DFS, LI);
722 for (BasicBlock *POI : Traversal) {
723
724 Loop *L = LI->getLoopFor(POI);
725 Loop *NL = getNearestLoop(POI, L);
726
727 if (NL != L) {
728 // For reducible loops, NL is now an ancestor of Unloop.
729 assert((NL != &Unloop && (!NL || NL->contains(&Unloop))) &&
730 "uninitialized successor");
731 LI->changeLoopFor(POI, NL);
732 } else {
733 // Or the current block is part of a subloop, in which case its parent
734 // is unchanged.
735 assert((FoundIB || Unloop.contains(L)) && "uninitialized successor");
736 }
737 }
738 }
739 // Each irreducible loop within the unloop induces a round of iteration using
740 // the DFS result cached by Traversal.
741 bool Changed = FoundIB;
742 for (unsigned NIters = 0; Changed; ++NIters) {
743 assert(NIters < Unloop.getNumBlocks() && "runaway iterative algorithm");
744
745 // Iterate over the postorder list of blocks, propagating the nearest loop
746 // from successors to predecessors as before.
747 Changed = false;
748 for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(),
749 POE = DFS.endPostorder();
750 POI != POE; ++POI) {
751
752 Loop *L = LI->getLoopFor(*POI);
753 Loop *NL = getNearestLoop(*POI, L);
754 if (NL != L) {
755 assert(NL != &Unloop && (!NL || NL->contains(&Unloop)) &&
756 "uninitialized successor");
757 LI->changeLoopFor(*POI, NL);
758 Changed = true;
759 }
760 }
761 }
762 }
763
764 /// Remove unloop's blocks from all ancestors below their new parents.
removeBlocksFromAncestors()765 void UnloopUpdater::removeBlocksFromAncestors() {
766 // Remove all unloop's blocks (including those in nested subloops) from
767 // ancestors below the new parent loop.
768 for (Loop::block_iterator BI = Unloop.block_begin(), BE = Unloop.block_end();
769 BI != BE; ++BI) {
770 Loop *OuterParent = LI->getLoopFor(*BI);
771 if (Unloop.contains(OuterParent)) {
772 while (OuterParent->getParentLoop() != &Unloop)
773 OuterParent = OuterParent->getParentLoop();
774 OuterParent = SubloopParents[OuterParent];
775 }
776 // Remove blocks from former Ancestors except Unloop itself which will be
777 // deleted.
778 for (Loop *OldParent = Unloop.getParentLoop(); OldParent != OuterParent;
779 OldParent = OldParent->getParentLoop()) {
780 assert(OldParent && "new loop is not an ancestor of the original");
781 OldParent->removeBlockFromLoop(*BI);
782 }
783 }
784 }
785
786 /// Update the parent loop for all subloops directly nested within unloop.
updateSubloopParents()787 void UnloopUpdater::updateSubloopParents() {
788 while (!Unloop.isInnermost()) {
789 Loop *Subloop = *std::prev(Unloop.end());
790 Unloop.removeChildLoop(std::prev(Unloop.end()));
791
792 assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop");
793 if (Loop *Parent = SubloopParents[Subloop])
794 Parent->addChildLoop(Subloop);
795 else
796 LI->addTopLevelLoop(Subloop);
797 }
798 }
799
800 /// Return the nearest parent loop among this block's successors. If a successor
801 /// is a subloop header, consider its parent to be the nearest parent of the
802 /// subloop's exits.
803 ///
804 /// For subloop blocks, simply update SubloopParents and return NULL.
getNearestLoop(BasicBlock * BB,Loop * BBLoop)805 Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) {
806
807 // Initially for blocks directly contained by Unloop, NearLoop == Unloop and
808 // is considered uninitialized.
809 Loop *NearLoop = BBLoop;
810
811 Loop *Subloop = nullptr;
812 if (NearLoop != &Unloop && Unloop.contains(NearLoop)) {
813 Subloop = NearLoop;
814 // Find the subloop ancestor that is directly contained within Unloop.
815 while (Subloop->getParentLoop() != &Unloop) {
816 Subloop = Subloop->getParentLoop();
817 assert(Subloop && "subloop is not an ancestor of the original loop");
818 }
819 // Get the current nearest parent of the Subloop exits, initially Unloop.
820 NearLoop = SubloopParents.insert({Subloop, &Unloop}).first->second;
821 }
822
823 succ_iterator I = succ_begin(BB), E = succ_end(BB);
824 if (I == E) {
825 assert(!Subloop && "subloop blocks must have a successor");
826 NearLoop = nullptr; // unloop blocks may now exit the function.
827 }
828 for (; I != E; ++I) {
829 if (*I == BB)
830 continue; // self loops are uninteresting
831
832 Loop *L = LI->getLoopFor(*I);
833 if (L == &Unloop) {
834 // This successor has not been processed. This path must lead to an
835 // irreducible backedge.
836 assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB");
837 FoundIB = true;
838 }
839 if (L != &Unloop && Unloop.contains(L)) {
840 // Successor is in a subloop.
841 if (Subloop)
842 continue; // Branching within subloops. Ignore it.
843
844 // BB branches from the original into a subloop header.
845 assert(L->getParentLoop() == &Unloop && "cannot skip into nested loops");
846
847 // Get the current nearest parent of the Subloop's exits.
848 L = SubloopParents[L];
849 // L could be Unloop if the only exit was an irreducible backedge.
850 }
851 if (L == &Unloop) {
852 continue;
853 }
854 // Handle critical edges from Unloop into a sibling loop.
855 if (L && !L->contains(&Unloop)) {
856 L = L->getParentLoop();
857 }
858 // Remember the nearest parent loop among successors or subloop exits.
859 if (NearLoop == &Unloop || !NearLoop || NearLoop->contains(L))
860 NearLoop = L;
861 }
862 if (Subloop) {
863 SubloopParents[Subloop] = NearLoop;
864 return BBLoop;
865 }
866 return NearLoop;
867 }
868
LoopInfo(const DomTreeBase<BasicBlock> & DomTree)869 LoopInfo::LoopInfo(const DomTreeBase<BasicBlock> &DomTree) { analyze(DomTree); }
870
invalidate(Function & F,const PreservedAnalyses & PA,FunctionAnalysisManager::Invalidator &)871 bool LoopInfo::invalidate(Function &F, const PreservedAnalyses &PA,
872 FunctionAnalysisManager::Invalidator &) {
873 // Check whether the analysis, all analyses on functions, or the function's
874 // CFG have been preserved.
875 auto PAC = PA.getChecker<LoopAnalysis>();
876 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
877 PAC.preservedSet<CFGAnalyses>());
878 }
879
erase(Loop * Unloop)880 void LoopInfo::erase(Loop *Unloop) {
881 assert(!Unloop->isInvalid() && "Loop has already been erased!");
882
883 auto InvalidateOnExit = make_scope_exit([&]() { destroy(Unloop); });
884
885 // First handle the special case of no parent loop to simplify the algorithm.
886 if (Unloop->isOutermost()) {
887 // Since BBLoop had no parent, Unloop blocks are no longer in a loop.
888 for (Loop::block_iterator I = Unloop->block_begin(),
889 E = Unloop->block_end();
890 I != E; ++I) {
891
892 // Don't reparent blocks in subloops.
893 if (getLoopFor(*I) != Unloop)
894 continue;
895
896 // Blocks no longer have a parent but are still referenced by Unloop until
897 // the Unloop object is deleted.
898 changeLoopFor(*I, nullptr);
899 }
900
901 // Remove the loop from the top-level LoopInfo object.
902 for (iterator I = begin();; ++I) {
903 assert(I != end() && "Couldn't find loop");
904 if (*I == Unloop) {
905 removeLoop(I);
906 break;
907 }
908 }
909
910 // Move all of the subloops to the top-level.
911 while (!Unloop->isInnermost())
912 addTopLevelLoop(Unloop->removeChildLoop(std::prev(Unloop->end())));
913
914 return;
915 }
916
917 // Update the parent loop for all blocks within the loop. Blocks within
918 // subloops will not change parents.
919 UnloopUpdater Updater(Unloop, this);
920 Updater.updateBlockParents();
921
922 // Remove blocks from former ancestor loops.
923 Updater.removeBlocksFromAncestors();
924
925 // Add direct subloops as children in their new parent loop.
926 Updater.updateSubloopParents();
927
928 // Remove unloop from its parent loop.
929 Loop *ParentLoop = Unloop->getParentLoop();
930 for (Loop::iterator I = ParentLoop->begin();; ++I) {
931 assert(I != ParentLoop->end() && "Couldn't find loop");
932 if (*I == Unloop) {
933 ParentLoop->removeChildLoop(I);
934 break;
935 }
936 }
937 }
938
939 AnalysisKey LoopAnalysis::Key;
940
run(Function & F,FunctionAnalysisManager & AM)941 LoopInfo LoopAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
942 // FIXME: Currently we create a LoopInfo from scratch for every function.
943 // This may prove to be too wasteful due to deallocating and re-allocating
944 // memory each time for the underlying map and vector datastructures. At some
945 // point it may prove worthwhile to use a freelist and recycle LoopInfo
946 // objects. I don't want to add that kind of complexity until the scope of
947 // the problem is better understood.
948 LoopInfo LI;
949 LI.analyze(AM.getResult<DominatorTreeAnalysis>(F));
950 return LI;
951 }
952
run(Function & F,FunctionAnalysisManager & AM)953 PreservedAnalyses LoopPrinterPass::run(Function &F,
954 FunctionAnalysisManager &AM) {
955 AM.getResult<LoopAnalysis>(F).print(OS);
956 return PreservedAnalyses::all();
957 }
958
printLoop(Loop & L,raw_ostream & OS,const std::string & Banner)959 void llvm::printLoop(Loop &L, raw_ostream &OS, const std::string &Banner) {
960
961 if (forcePrintModuleIR()) {
962 // handling -print-module-scope
963 OS << Banner << " (loop: ";
964 L.getHeader()->printAsOperand(OS, false);
965 OS << ")\n";
966
967 // printing whole module
968 OS << *L.getHeader()->getModule();
969 return;
970 }
971
972 OS << Banner;
973
974 auto *PreHeader = L.getLoopPreheader();
975 if (PreHeader) {
976 OS << "\n; Preheader:";
977 PreHeader->print(OS);
978 OS << "\n; Loop:";
979 }
980
981 for (auto *Block : L.blocks())
982 if (Block)
983 Block->print(OS);
984 else
985 OS << "Printing <null> block";
986
987 SmallVector<BasicBlock *, 8> ExitBlocks;
988 L.getExitBlocks(ExitBlocks);
989 if (!ExitBlocks.empty()) {
990 OS << "\n; Exit blocks";
991 for (auto *Block : ExitBlocks)
992 if (Block)
993 Block->print(OS);
994 else
995 OS << "Printing <null> block";
996 }
997 }
998
findOptionMDForLoopID(MDNode * LoopID,StringRef Name)999 MDNode *llvm::findOptionMDForLoopID(MDNode *LoopID, StringRef Name) {
1000 // No loop metadata node, no loop properties.
1001 if (!LoopID)
1002 return nullptr;
1003
1004 // First operand should refer to the metadata node itself, for legacy reasons.
1005 assert(LoopID->getNumOperands() > 0 && "requires at least one operand");
1006 assert(LoopID->getOperand(0) == LoopID && "invalid loop id");
1007
1008 // Iterate over the metdata node operands and look for MDString metadata.
1009 for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) {
1010 MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
1011 if (!MD || MD->getNumOperands() < 1)
1012 continue;
1013 MDString *S = dyn_cast<MDString>(MD->getOperand(0));
1014 if (!S)
1015 continue;
1016 // Return the operand node if MDString holds expected metadata.
1017 if (Name.equals(S->getString()))
1018 return MD;
1019 }
1020
1021 // Loop property not found.
1022 return nullptr;
1023 }
1024
findOptionMDForLoop(const Loop * TheLoop,StringRef Name)1025 MDNode *llvm::findOptionMDForLoop(const Loop *TheLoop, StringRef Name) {
1026 return findOptionMDForLoopID(TheLoop->getLoopID(), Name);
1027 }
1028
isValidAsAccessGroup(MDNode * Node)1029 bool llvm::isValidAsAccessGroup(MDNode *Node) {
1030 return Node->getNumOperands() == 0 && Node->isDistinct();
1031 }
1032
makePostTransformationMetadata(LLVMContext & Context,MDNode * OrigLoopID,ArrayRef<StringRef> RemovePrefixes,ArrayRef<MDNode * > AddAttrs)1033 MDNode *llvm::makePostTransformationMetadata(LLVMContext &Context,
1034 MDNode *OrigLoopID,
1035 ArrayRef<StringRef> RemovePrefixes,
1036 ArrayRef<MDNode *> AddAttrs) {
1037 // First remove any existing loop metadata related to this transformation.
1038 SmallVector<Metadata *, 4> MDs;
1039
1040 // Reserve first location for self reference to the LoopID metadata node.
1041 MDs.push_back(nullptr);
1042
1043 // Remove metadata for the transformation that has been applied or that became
1044 // outdated.
1045 if (OrigLoopID) {
1046 for (unsigned i = 1, ie = OrigLoopID->getNumOperands(); i < ie; ++i) {
1047 bool IsVectorMetadata = false;
1048 Metadata *Op = OrigLoopID->getOperand(i);
1049 if (MDNode *MD = dyn_cast<MDNode>(Op)) {
1050 const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
1051 if (S)
1052 IsVectorMetadata =
1053 llvm::any_of(RemovePrefixes, [S](StringRef Prefix) -> bool {
1054 return S->getString().startswith(Prefix);
1055 });
1056 }
1057 if (!IsVectorMetadata)
1058 MDs.push_back(Op);
1059 }
1060 }
1061
1062 // Add metadata to avoid reapplying a transformation, such as
1063 // llvm.loop.unroll.disable and llvm.loop.isvectorized.
1064 MDs.append(AddAttrs.begin(), AddAttrs.end());
1065
1066 MDNode *NewLoopID = MDNode::getDistinct(Context, MDs);
1067 // Replace the temporary node with a self-reference.
1068 NewLoopID->replaceOperandWith(0, NewLoopID);
1069 return NewLoopID;
1070 }
1071
1072 //===----------------------------------------------------------------------===//
1073 // LoopInfo implementation
1074 //
1075
LoopInfoWrapperPass()1076 LoopInfoWrapperPass::LoopInfoWrapperPass() : FunctionPass(ID) {
1077 initializeLoopInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1078 }
1079
1080 char LoopInfoWrapperPass::ID = 0;
1081 INITIALIZE_PASS_BEGIN(LoopInfoWrapperPass, "loops", "Natural Loop Information",
1082 true, true)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)1083 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
1084 INITIALIZE_PASS_END(LoopInfoWrapperPass, "loops", "Natural Loop Information",
1085 true, true)
1086
1087 bool LoopInfoWrapperPass::runOnFunction(Function &) {
1088 releaseMemory();
1089 LI.analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree());
1090 return false;
1091 }
1092
verifyAnalysis() const1093 void LoopInfoWrapperPass::verifyAnalysis() const {
1094 // LoopInfoWrapperPass is a FunctionPass, but verifying every loop in the
1095 // function each time verifyAnalysis is called is very expensive. The
1096 // -verify-loop-info option can enable this. In order to perform some
1097 // checking by default, LoopPass has been taught to call verifyLoop manually
1098 // during loop pass sequences.
1099 if (VerifyLoopInfo) {
1100 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1101 LI.verify(DT);
1102 }
1103 }
1104
getAnalysisUsage(AnalysisUsage & AU) const1105 void LoopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1106 AU.setPreservesAll();
1107 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
1108 }
1109
print(raw_ostream & OS,const Module *) const1110 void LoopInfoWrapperPass::print(raw_ostream &OS, const Module *) const {
1111 LI.print(OS);
1112 }
1113
run(Function & F,FunctionAnalysisManager & AM)1114 PreservedAnalyses LoopVerifierPass::run(Function &F,
1115 FunctionAnalysisManager &AM) {
1116 LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
1117 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
1118 LI.verify(DT);
1119 return PreservedAnalyses::all();
1120 }
1121
1122 //===----------------------------------------------------------------------===//
1123 // LoopBlocksDFS implementation
1124 //
1125
1126 /// Traverse the loop blocks and store the DFS result.
1127 /// Useful for clients that just want the final DFS result and don't need to
1128 /// visit blocks during the initial traversal.
perform(LoopInfo * LI)1129 void LoopBlocksDFS::perform(LoopInfo *LI) {
1130 LoopBlocksTraversal Traversal(*this, LI);
1131 for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(),
1132 POE = Traversal.end();
1133 POI != POE; ++POI)
1134 ;
1135 }
1136