1 //===- LoopInfo.cpp - Natural Loop Calculator -----------------------------===//
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
7 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
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/Support/CommandLine.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Support/raw_ostream.h"
43 // Explicitly instantiate methods in LoopInfoImpl.h for IR-level Loops.
44 template class llvm::LoopBase
<BasicBlock
, Loop
>;
45 template class llvm::LoopInfoBase
<BasicBlock
, Loop
>;
47 // Always verify loopinfo if expensive checking is enabled.
48 #ifdef EXPENSIVE_CHECKS
49 bool llvm::VerifyLoopInfo
= true;
51 bool llvm::VerifyLoopInfo
= false;
53 static cl::opt
<bool, true>
54 VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo
),
55 cl::Hidden
, cl::desc("Verify loop info (time consuming)"));
57 //===----------------------------------------------------------------------===//
58 // Loop implementation
61 bool Loop::isLoopInvariant(const Value
*V
) const {
62 if (const Instruction
*I
= dyn_cast
<Instruction
>(V
))
64 return true; // All non-instructions are loop invariant
67 bool Loop::hasLoopInvariantOperands(const Instruction
*I
) const {
68 return all_of(I
->operands(), [this](Value
*V
) { return isLoopInvariant(V
); });
71 bool Loop::makeLoopInvariant(Value
*V
, bool &Changed
, Instruction
*InsertPt
,
72 MemorySSAUpdater
*MSSAU
) const {
73 if (Instruction
*I
= dyn_cast
<Instruction
>(V
))
74 return makeLoopInvariant(I
, Changed
, InsertPt
, MSSAU
);
75 return true; // All non-instructions are loop-invariant.
78 bool Loop::makeLoopInvariant(Instruction
*I
, bool &Changed
,
79 Instruction
*InsertPt
,
80 MemorySSAUpdater
*MSSAU
) const {
81 // Test if the value is already loop-invariant.
82 if (isLoopInvariant(I
))
84 if (!isSafeToSpeculativelyExecute(I
))
86 if (I
->mayReadFromMemory())
88 // EH block instructions are immobile.
91 // Determine the insertion point, unless one was given.
93 BasicBlock
*Preheader
= getLoopPreheader();
94 // Without a preheader, hoisting is not feasible.
97 InsertPt
= Preheader
->getTerminator();
99 // Don't hoist instructions with loop-variant operands.
100 for (Value
*Operand
: I
->operands())
101 if (!makeLoopInvariant(Operand
, Changed
, InsertPt
, MSSAU
))
105 I
->moveBefore(InsertPt
);
107 if (auto *MUD
= MSSAU
->getMemorySSA()->getMemoryAccess(I
))
108 MSSAU
->moveToPlace(MUD
, InsertPt
->getParent(), MemorySSA::End
);
110 // There is possibility of hoisting this instruction above some arbitrary
111 // condition. Any metadata defined on it can be control dependent on this
112 // condition. Conservatively strip it here so that we don't give any wrong
113 // information to the optimizer.
114 I
->dropUnknownNonDebugMetadata();
120 bool Loop::getIncomingAndBackEdge(BasicBlock
*&Incoming
,
121 BasicBlock
*&Backedge
) const {
122 BasicBlock
*H
= getHeader();
126 pred_iterator PI
= pred_begin(H
);
127 assert(PI
!= pred_end(H
) && "Loop must have at least one backedge!");
129 if (PI
== pred_end(H
))
130 return false; // dead loop
132 if (PI
!= pred_end(H
))
133 return false; // multiple backedges?
135 if (contains(Incoming
)) {
136 if (contains(Backedge
))
138 std::swap(Incoming
, Backedge
);
139 } else if (!contains(Backedge
))
142 assert(Incoming
&& Backedge
&& "expected non-null incoming and backedges");
146 PHINode
*Loop::getCanonicalInductionVariable() const {
147 BasicBlock
*H
= getHeader();
149 BasicBlock
*Incoming
= nullptr, *Backedge
= nullptr;
150 if (!getIncomingAndBackEdge(Incoming
, Backedge
))
153 // Loop over all of the PHI nodes, looking for a canonical indvar.
154 for (BasicBlock::iterator I
= H
->begin(); isa
<PHINode
>(I
); ++I
) {
155 PHINode
*PN
= cast
<PHINode
>(I
);
156 if (ConstantInt
*CI
=
157 dyn_cast
<ConstantInt
>(PN
->getIncomingValueForBlock(Incoming
)))
159 if (Instruction
*Inc
=
160 dyn_cast
<Instruction
>(PN
->getIncomingValueForBlock(Backedge
)))
161 if (Inc
->getOpcode() == Instruction::Add
&& Inc
->getOperand(0) == PN
)
162 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(Inc
->getOperand(1)))
169 /// Get the latch condition instruction.
170 static ICmpInst
*getLatchCmpInst(const Loop
&L
) {
171 if (BasicBlock
*Latch
= L
.getLoopLatch())
172 if (BranchInst
*BI
= dyn_cast_or_null
<BranchInst
>(Latch
->getTerminator()))
173 if (BI
->isConditional())
174 return dyn_cast
<ICmpInst
>(BI
->getCondition());
179 /// Return the final value of the loop induction variable if found.
180 static Value
*findFinalIVValue(const Loop
&L
, const PHINode
&IndVar
,
181 const Instruction
&StepInst
) {
182 ICmpInst
*LatchCmpInst
= getLatchCmpInst(L
);
186 Value
*Op0
= LatchCmpInst
->getOperand(0);
187 Value
*Op1
= LatchCmpInst
->getOperand(1);
188 if (Op0
== &IndVar
|| Op0
== &StepInst
)
191 if (Op1
== &IndVar
|| Op1
== &StepInst
)
197 Optional
<Loop::LoopBounds
> Loop::LoopBounds::getBounds(const Loop
&L
,
199 ScalarEvolution
&SE
) {
200 InductionDescriptor IndDesc
;
201 if (!InductionDescriptor::isInductionPHI(&IndVar
, &L
, &SE
, IndDesc
))
204 Value
*InitialIVValue
= IndDesc
.getStartValue();
205 Instruction
*StepInst
= IndDesc
.getInductionBinOp();
206 if (!InitialIVValue
|| !StepInst
)
209 const SCEV
*Step
= IndDesc
.getStep();
210 Value
*StepInstOp1
= StepInst
->getOperand(1);
211 Value
*StepInstOp0
= StepInst
->getOperand(0);
212 Value
*StepValue
= nullptr;
213 if (SE
.getSCEV(StepInstOp1
) == Step
)
214 StepValue
= StepInstOp1
;
215 else if (SE
.getSCEV(StepInstOp0
) == Step
)
216 StepValue
= StepInstOp0
;
218 Value
*FinalIVValue
= findFinalIVValue(L
, IndVar
, *StepInst
);
222 return LoopBounds(L
, *InitialIVValue
, *StepInst
, StepValue
, *FinalIVValue
,
226 using Direction
= Loop::LoopBounds::Direction
;
228 ICmpInst::Predicate
Loop::LoopBounds::getCanonicalPredicate() const {
229 BasicBlock
*Latch
= L
.getLoopLatch();
230 assert(Latch
&& "Expecting valid latch");
232 BranchInst
*BI
= dyn_cast_or_null
<BranchInst
>(Latch
->getTerminator());
233 assert(BI
&& BI
->isConditional() && "Expecting conditional latch branch");
235 ICmpInst
*LatchCmpInst
= dyn_cast
<ICmpInst
>(BI
->getCondition());
236 assert(LatchCmpInst
&&
237 "Expecting the latch compare instruction to be a CmpInst");
239 // Need to inverse the predicate when first successor is not the loop
241 ICmpInst::Predicate Pred
= (BI
->getSuccessor(0) == L
.getHeader())
242 ? LatchCmpInst
->getPredicate()
243 : LatchCmpInst
->getInversePredicate();
245 if (LatchCmpInst
->getOperand(0) == &getFinalIVValue())
246 Pred
= ICmpInst::getSwappedPredicate(Pred
);
248 // Need to flip strictness of the predicate when the latch compare instruction
249 // is not using StepInst
250 if (LatchCmpInst
->getOperand(0) == &getStepInst() ||
251 LatchCmpInst
->getOperand(1) == &getStepInst())
254 // Cannot flip strictness of NE and EQ
255 if (Pred
!= ICmpInst::ICMP_NE
&& Pred
!= ICmpInst::ICMP_EQ
)
256 return ICmpInst::getFlippedStrictnessPredicate(Pred
);
258 Direction D
= getDirection();
259 if (D
== Direction::Increasing
)
260 return ICmpInst::ICMP_SLT
;
262 if (D
== Direction::Decreasing
)
263 return ICmpInst::ICMP_SGT
;
265 // If cannot determine the direction, then unable to find the canonical
267 return ICmpInst::BAD_ICMP_PREDICATE
;
270 Direction
Loop::LoopBounds::getDirection() const {
271 if (const SCEVAddRecExpr
*StepAddRecExpr
=
272 dyn_cast
<SCEVAddRecExpr
>(SE
.getSCEV(&getStepInst())))
273 if (const SCEV
*StepRecur
= StepAddRecExpr
->getStepRecurrence(SE
)) {
274 if (SE
.isKnownPositive(StepRecur
))
275 return Direction::Increasing
;
276 if (SE
.isKnownNegative(StepRecur
))
277 return Direction::Decreasing
;
280 return Direction::Unknown
;
283 Optional
<Loop::LoopBounds
> Loop::getBounds(ScalarEvolution
&SE
) const {
284 if (PHINode
*IndVar
= getInductionVariable(SE
))
285 return LoopBounds::getBounds(*this, *IndVar
, SE
);
290 PHINode
*Loop::getInductionVariable(ScalarEvolution
&SE
) const {
291 if (!isLoopSimplifyForm())
294 BasicBlock
*Header
= getHeader();
295 assert(Header
&& "Expected a valid loop header");
296 ICmpInst
*CmpInst
= getLatchCmpInst(*this);
300 Instruction
*LatchCmpOp0
= dyn_cast
<Instruction
>(CmpInst
->getOperand(0));
301 Instruction
*LatchCmpOp1
= dyn_cast
<Instruction
>(CmpInst
->getOperand(1));
303 for (PHINode
&IndVar
: Header
->phis()) {
304 InductionDescriptor IndDesc
;
305 if (!InductionDescriptor::isInductionPHI(&IndVar
, this, &SE
, IndDesc
))
308 Instruction
*StepInst
= IndDesc
.getInductionBinOp();
311 // IndVar = phi[{InitialValue, preheader}, {StepInst, latch}]
312 // StepInst = IndVar + step
313 // cmp = StepInst < FinalValue
314 if (StepInst
== LatchCmpOp0
|| StepInst
== LatchCmpOp1
)
318 // IndVar = phi[{InitialValue, preheader}, {StepInst, latch}]
319 // StepInst = IndVar + step
320 // cmp = IndVar < FinalValue
321 if (&IndVar
== LatchCmpOp0
|| &IndVar
== LatchCmpOp1
)
328 bool Loop::getInductionDescriptor(ScalarEvolution
&SE
,
329 InductionDescriptor
&IndDesc
) const {
330 if (PHINode
*IndVar
= getInductionVariable(SE
))
331 return InductionDescriptor::isInductionPHI(IndVar
, this, &SE
, IndDesc
);
336 bool Loop::isAuxiliaryInductionVariable(PHINode
&AuxIndVar
,
337 ScalarEvolution
&SE
) const {
338 // Located in the loop header
339 BasicBlock
*Header
= getHeader();
340 if (AuxIndVar
.getParent() != Header
)
343 // No uses outside of the loop
344 for (User
*U
: AuxIndVar
.users())
345 if (const Instruction
*I
= dyn_cast
<Instruction
>(U
))
349 InductionDescriptor IndDesc
;
350 if (!InductionDescriptor::isInductionPHI(&AuxIndVar
, this, &SE
, IndDesc
))
353 // The step instruction opcode should be add or sub.
354 if (IndDesc
.getInductionOpcode() != Instruction::Add
&&
355 IndDesc
.getInductionOpcode() != Instruction::Sub
)
358 // Incremented by a loop invariant step for each loop iteration
359 return SE
.isLoopInvariant(IndDesc
.getStep(), this);
362 BranchInst
*Loop::getLoopGuardBranch() const {
363 if (!isLoopSimplifyForm())
366 BasicBlock
*Preheader
= getLoopPreheader();
367 BasicBlock
*Latch
= getLoopLatch();
368 assert(Preheader
&& Latch
&&
369 "Expecting a loop with valid preheader and latch");
371 // Loop should be in rotate form.
372 if (!isLoopExiting(Latch
))
375 // Disallow loops with more than one unique exit block, as we do not verify
376 // that GuardOtherSucc post dominates all exit blocks.
377 BasicBlock
*ExitFromLatch
= getUniqueExitBlock();
381 BasicBlock
*ExitFromLatchSucc
= ExitFromLatch
->getUniqueSuccessor();
382 if (!ExitFromLatchSucc
)
385 BasicBlock
*GuardBB
= Preheader
->getUniquePredecessor();
389 assert(GuardBB
->getTerminator() && "Expecting valid guard terminator");
391 BranchInst
*GuardBI
= dyn_cast
<BranchInst
>(GuardBB
->getTerminator());
392 if (!GuardBI
|| GuardBI
->isUnconditional())
395 BasicBlock
*GuardOtherSucc
= (GuardBI
->getSuccessor(0) == Preheader
)
396 ? GuardBI
->getSuccessor(1)
397 : GuardBI
->getSuccessor(0);
398 return (GuardOtherSucc
== ExitFromLatchSucc
) ? GuardBI
: nullptr;
401 bool Loop::isCanonical(ScalarEvolution
&SE
) const {
402 InductionDescriptor IndDesc
;
403 if (!getInductionDescriptor(SE
, IndDesc
))
406 ConstantInt
*Init
= dyn_cast_or_null
<ConstantInt
>(IndDesc
.getStartValue());
407 if (!Init
|| !Init
->isZero())
410 if (IndDesc
.getInductionOpcode() != Instruction::Add
)
413 ConstantInt
*Step
= IndDesc
.getConstIntStepValue();
414 if (!Step
|| !Step
->isOne())
420 // Check that 'BB' doesn't have any uses outside of the 'L'
421 static bool isBlockInLCSSAForm(const Loop
&L
, const BasicBlock
&BB
,
423 for (const Instruction
&I
: BB
) {
424 // Tokens can't be used in PHI nodes and live-out tokens prevent loop
425 // optimizations, so for the purposes of considered LCSSA form, we
427 if (I
.getType()->isTokenTy())
430 for (const Use
&U
: I
.uses()) {
431 const Instruction
*UI
= cast
<Instruction
>(U
.getUser());
432 const BasicBlock
*UserBB
= UI
->getParent();
433 if (const PHINode
*P
= dyn_cast
<PHINode
>(UI
))
434 UserBB
= P
->getIncomingBlock(U
);
436 // Check the current block, as a fast-path, before checking whether
437 // the use is anywhere in the loop. Most values are used in the same
438 // block they are defined in. Also, blocks not reachable from the
439 // entry are special; uses in them don't need to go through PHIs.
440 if (UserBB
!= &BB
&& !L
.contains(UserBB
) &&
441 DT
.isReachableFromEntry(UserBB
))
448 bool Loop::isLCSSAForm(DominatorTree
&DT
) const {
449 // For each block we check that it doesn't have any uses outside of this loop.
450 return all_of(this->blocks(), [&](const BasicBlock
*BB
) {
451 return isBlockInLCSSAForm(*this, *BB
, DT
);
455 bool Loop::isRecursivelyLCSSAForm(DominatorTree
&DT
, const LoopInfo
&LI
) const {
456 // For each block we check that it doesn't have any uses outside of its
457 // innermost loop. This process will transitively guarantee that the current
458 // loop and all of the nested loops are in LCSSA form.
459 return all_of(this->blocks(), [&](const BasicBlock
*BB
) {
460 return isBlockInLCSSAForm(*LI
.getLoopFor(BB
), *BB
, DT
);
464 bool Loop::isLoopSimplifyForm() const {
465 // Normal-form loops have a preheader, a single backedge, and all of their
466 // exits have all their predecessors inside the loop.
467 return getLoopPreheader() && getLoopLatch() && hasDedicatedExits();
470 // Routines that reform the loop CFG and split edges often fail on indirectbr.
471 bool Loop::isSafeToClone() const {
472 // Return false if any loop blocks contain indirectbrs, or there are any calls
473 // to noduplicate functions.
474 // FIXME: it should be ok to clone CallBrInst's if we correctly update the
475 // operand list to reflect the newly cloned labels.
476 for (BasicBlock
*BB
: this->blocks()) {
477 if (isa
<IndirectBrInst
>(BB
->getTerminator()) ||
478 isa
<CallBrInst
>(BB
->getTerminator()))
481 for (Instruction
&I
: *BB
)
482 if (auto CS
= CallSite(&I
))
483 if (CS
.cannotDuplicate())
489 MDNode
*Loop::getLoopID() const {
490 MDNode
*LoopID
= nullptr;
492 // Go through the latch blocks and check the terminator for the metadata.
493 SmallVector
<BasicBlock
*, 4> LatchesBlocks
;
494 getLoopLatches(LatchesBlocks
);
495 for (BasicBlock
*BB
: LatchesBlocks
) {
496 Instruction
*TI
= BB
->getTerminator();
497 MDNode
*MD
= TI
->getMetadata(LLVMContext::MD_loop
);
504 else if (MD
!= LoopID
)
507 if (!LoopID
|| LoopID
->getNumOperands() == 0 ||
508 LoopID
->getOperand(0) != LoopID
)
513 void Loop::setLoopID(MDNode
*LoopID
) const {
514 assert((!LoopID
|| LoopID
->getNumOperands() > 0) &&
515 "Loop ID needs at least one operand");
516 assert((!LoopID
|| LoopID
->getOperand(0) == LoopID
) &&
517 "Loop ID should refer to itself");
519 SmallVector
<BasicBlock
*, 4> LoopLatches
;
520 getLoopLatches(LoopLatches
);
521 for (BasicBlock
*BB
: LoopLatches
)
522 BB
->getTerminator()->setMetadata(LLVMContext::MD_loop
, LoopID
);
525 void Loop::setLoopAlreadyUnrolled() {
526 LLVMContext
&Context
= getHeader()->getContext();
528 MDNode
*DisableUnrollMD
=
529 MDNode::get(Context
, MDString::get(Context
, "llvm.loop.unroll.disable"));
530 MDNode
*LoopID
= getLoopID();
531 MDNode
*NewLoopID
= makePostTransformationMetadata(
532 Context
, LoopID
, {"llvm.loop.unroll."}, {DisableUnrollMD
});
533 setLoopID(NewLoopID
);
536 bool Loop::isAnnotatedParallel() const {
537 MDNode
*DesiredLoopIdMetadata
= getLoopID();
539 if (!DesiredLoopIdMetadata
)
542 MDNode
*ParallelAccesses
=
543 findOptionMDForLoop(this, "llvm.loop.parallel_accesses");
544 SmallPtrSet
<MDNode
*, 4>
545 ParallelAccessGroups
; // For scalable 'contains' check.
546 if (ParallelAccesses
) {
547 for (const MDOperand
&MD
: drop_begin(ParallelAccesses
->operands(), 1)) {
548 MDNode
*AccGroup
= cast
<MDNode
>(MD
.get());
549 assert(isValidAsAccessGroup(AccGroup
) &&
550 "List item must be an access group");
551 ParallelAccessGroups
.insert(AccGroup
);
555 // The loop branch contains the parallel loop metadata. In order to ensure
556 // that any parallel-loop-unaware optimization pass hasn't added loop-carried
557 // dependencies (thus converted the loop back to a sequential loop), check
558 // that all the memory instructions in the loop belong to an access group that
559 // is parallel to this loop.
560 for (BasicBlock
*BB
: this->blocks()) {
561 for (Instruction
&I
: *BB
) {
562 if (!I
.mayReadOrWriteMemory())
565 if (MDNode
*AccessGroup
= I
.getMetadata(LLVMContext::MD_access_group
)) {
566 auto ContainsAccessGroup
= [&ParallelAccessGroups
](MDNode
*AG
) -> bool {
567 if (AG
->getNumOperands() == 0) {
568 assert(isValidAsAccessGroup(AG
) && "Item must be an access group");
569 return ParallelAccessGroups
.count(AG
);
572 for (const MDOperand
&AccessListItem
: AG
->operands()) {
573 MDNode
*AccGroup
= cast
<MDNode
>(AccessListItem
.get());
574 assert(isValidAsAccessGroup(AccGroup
) &&
575 "List item must be an access group");
576 if (ParallelAccessGroups
.count(AccGroup
))
582 if (ContainsAccessGroup(AccessGroup
))
586 // The memory instruction can refer to the loop identifier metadata
587 // directly or indirectly through another list metadata (in case of
588 // nested parallel loops). The loop identifier metadata refers to
589 // itself so we can check both cases with the same routine.
591 I
.getMetadata(LLVMContext::MD_mem_parallel_loop_access
);
596 bool LoopIdMDFound
= false;
597 for (const MDOperand
&MDOp
: LoopIdMD
->operands()) {
598 if (MDOp
== DesiredLoopIdMetadata
) {
599 LoopIdMDFound
= true;
611 DebugLoc
Loop::getStartLoc() const { return getLocRange().getStart(); }
613 Loop::LocRange
Loop::getLocRange() const {
614 // If we have a debug location in the loop ID, then use it.
615 if (MDNode
*LoopID
= getLoopID()) {
617 // We use the first DebugLoc in the header as the start location of the loop
618 // and if there is a second DebugLoc in the header we use it as end location
620 for (unsigned i
= 1, ie
= LoopID
->getNumOperands(); i
< ie
; ++i
) {
621 if (DILocation
*L
= dyn_cast
<DILocation
>(LoopID
->getOperand(i
))) {
625 return LocRange(Start
, DebugLoc(L
));
630 return LocRange(Start
);
633 // Try the pre-header first.
634 if (BasicBlock
*PHeadBB
= getLoopPreheader())
635 if (DebugLoc DL
= PHeadBB
->getTerminator()->getDebugLoc())
638 // If we have no pre-header or there are no instructions with debug
639 // info in it, try the header.
640 if (BasicBlock
*HeadBB
= getHeader())
641 return LocRange(HeadBB
->getTerminator()->getDebugLoc());
646 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
647 LLVM_DUMP_METHOD
void Loop::dump() const { print(dbgs()); }
649 LLVM_DUMP_METHOD
void Loop::dumpVerbose() const {
650 print(dbgs(), /*Depth=*/0, /*Verbose=*/true);
654 //===----------------------------------------------------------------------===//
655 // UnloopUpdater implementation
659 /// Find the new parent loop for all blocks within the "unloop" whose last
660 /// backedges has just been removed.
661 class UnloopUpdater
{
667 // Map unloop's immediate subloops to their nearest reachable parents. Nested
668 // loops within these subloops will not change parents. However, an immediate
669 // subloop's new parent will be the nearest loop reachable from either its own
670 // exits *or* any of its nested loop's exits.
671 DenseMap
<Loop
*, Loop
*> SubloopParents
;
673 // Flag the presence of an irreducible backedge whose destination is a block
674 // directly contained by the original unloop.
678 UnloopUpdater(Loop
*UL
, LoopInfo
*LInfo
)
679 : Unloop(*UL
), LI(LInfo
), DFS(UL
), FoundIB(false) {}
681 void updateBlockParents();
683 void removeBlocksFromAncestors();
685 void updateSubloopParents();
688 Loop
*getNearestLoop(BasicBlock
*BB
, Loop
*BBLoop
);
690 } // end anonymous namespace
692 /// Update the parent loop for all blocks that are directly contained within the
693 /// original "unloop".
694 void UnloopUpdater::updateBlockParents() {
695 if (Unloop
.getNumBlocks()) {
696 // Perform a post order CFG traversal of all blocks within this loop,
697 // propagating the nearest loop from successors to predecessors.
698 LoopBlocksTraversal
Traversal(DFS
, LI
);
699 for (BasicBlock
*POI
: Traversal
) {
701 Loop
*L
= LI
->getLoopFor(POI
);
702 Loop
*NL
= getNearestLoop(POI
, L
);
705 // For reducible loops, NL is now an ancestor of Unloop.
706 assert((NL
!= &Unloop
&& (!NL
|| NL
->contains(&Unloop
))) &&
707 "uninitialized successor");
708 LI
->changeLoopFor(POI
, NL
);
710 // Or the current block is part of a subloop, in which case its parent
712 assert((FoundIB
|| Unloop
.contains(L
)) && "uninitialized successor");
716 // Each irreducible loop within the unloop induces a round of iteration using
717 // the DFS result cached by Traversal.
718 bool Changed
= FoundIB
;
719 for (unsigned NIters
= 0; Changed
; ++NIters
) {
720 assert(NIters
< Unloop
.getNumBlocks() && "runaway iterative algorithm");
722 // Iterate over the postorder list of blocks, propagating the nearest loop
723 // from successors to predecessors as before.
725 for (LoopBlocksDFS::POIterator POI
= DFS
.beginPostorder(),
726 POE
= DFS
.endPostorder();
729 Loop
*L
= LI
->getLoopFor(*POI
);
730 Loop
*NL
= getNearestLoop(*POI
, L
);
732 assert(NL
!= &Unloop
&& (!NL
|| NL
->contains(&Unloop
)) &&
733 "uninitialized successor");
734 LI
->changeLoopFor(*POI
, NL
);
741 /// Remove unloop's blocks from all ancestors below their new parents.
742 void UnloopUpdater::removeBlocksFromAncestors() {
743 // Remove all unloop's blocks (including those in nested subloops) from
744 // ancestors below the new parent loop.
745 for (Loop::block_iterator BI
= Unloop
.block_begin(), BE
= Unloop
.block_end();
747 Loop
*OuterParent
= LI
->getLoopFor(*BI
);
748 if (Unloop
.contains(OuterParent
)) {
749 while (OuterParent
->getParentLoop() != &Unloop
)
750 OuterParent
= OuterParent
->getParentLoop();
751 OuterParent
= SubloopParents
[OuterParent
];
753 // Remove blocks from former Ancestors except Unloop itself which will be
755 for (Loop
*OldParent
= Unloop
.getParentLoop(); OldParent
!= OuterParent
;
756 OldParent
= OldParent
->getParentLoop()) {
757 assert(OldParent
&& "new loop is not an ancestor of the original");
758 OldParent
->removeBlockFromLoop(*BI
);
763 /// Update the parent loop for all subloops directly nested within unloop.
764 void UnloopUpdater::updateSubloopParents() {
765 while (!Unloop
.empty()) {
766 Loop
*Subloop
= *std::prev(Unloop
.end());
767 Unloop
.removeChildLoop(std::prev(Unloop
.end()));
769 assert(SubloopParents
.count(Subloop
) && "DFS failed to visit subloop");
770 if (Loop
*Parent
= SubloopParents
[Subloop
])
771 Parent
->addChildLoop(Subloop
);
773 LI
->addTopLevelLoop(Subloop
);
777 /// Return the nearest parent loop among this block's successors. If a successor
778 /// is a subloop header, consider its parent to be the nearest parent of the
781 /// For subloop blocks, simply update SubloopParents and return NULL.
782 Loop
*UnloopUpdater::getNearestLoop(BasicBlock
*BB
, Loop
*BBLoop
) {
784 // Initially for blocks directly contained by Unloop, NearLoop == Unloop and
785 // is considered uninitialized.
786 Loop
*NearLoop
= BBLoop
;
788 Loop
*Subloop
= nullptr;
789 if (NearLoop
!= &Unloop
&& Unloop
.contains(NearLoop
)) {
791 // Find the subloop ancestor that is directly contained within Unloop.
792 while (Subloop
->getParentLoop() != &Unloop
) {
793 Subloop
= Subloop
->getParentLoop();
794 assert(Subloop
&& "subloop is not an ancestor of the original loop");
796 // Get the current nearest parent of the Subloop exits, initially Unloop.
797 NearLoop
= SubloopParents
.insert({Subloop
, &Unloop
}).first
->second
;
800 succ_iterator I
= succ_begin(BB
), E
= succ_end(BB
);
802 assert(!Subloop
&& "subloop blocks must have a successor");
803 NearLoop
= nullptr; // unloop blocks may now exit the function.
805 for (; I
!= E
; ++I
) {
807 continue; // self loops are uninteresting
809 Loop
*L
= LI
->getLoopFor(*I
);
811 // This successor has not been processed. This path must lead to an
812 // irreducible backedge.
813 assert((FoundIB
|| !DFS
.hasPostorder(*I
)) && "should have seen IB");
816 if (L
!= &Unloop
&& Unloop
.contains(L
)) {
817 // Successor is in a subloop.
819 continue; // Branching within subloops. Ignore it.
821 // BB branches from the original into a subloop header.
822 assert(L
->getParentLoop() == &Unloop
&& "cannot skip into nested loops");
824 // Get the current nearest parent of the Subloop's exits.
825 L
= SubloopParents
[L
];
826 // L could be Unloop if the only exit was an irreducible backedge.
831 // Handle critical edges from Unloop into a sibling loop.
832 if (L
&& !L
->contains(&Unloop
)) {
833 L
= L
->getParentLoop();
835 // Remember the nearest parent loop among successors or subloop exits.
836 if (NearLoop
== &Unloop
|| !NearLoop
|| NearLoop
->contains(L
))
840 SubloopParents
[Subloop
] = NearLoop
;
846 LoopInfo::LoopInfo(const DomTreeBase
<BasicBlock
> &DomTree
) { analyze(DomTree
); }
848 bool LoopInfo::invalidate(Function
&F
, const PreservedAnalyses
&PA
,
849 FunctionAnalysisManager::Invalidator
&) {
850 // Check whether the analysis, all analyses on functions, or the function's
851 // CFG have been preserved.
852 auto PAC
= PA
.getChecker
<LoopAnalysis
>();
853 return !(PAC
.preserved() || PAC
.preservedSet
<AllAnalysesOn
<Function
>>() ||
854 PAC
.preservedSet
<CFGAnalyses
>());
857 void LoopInfo::erase(Loop
*Unloop
) {
858 assert(!Unloop
->isInvalid() && "Loop has already been erased!");
860 auto InvalidateOnExit
= make_scope_exit([&]() { destroy(Unloop
); });
862 // First handle the special case of no parent loop to simplify the algorithm.
863 if (!Unloop
->getParentLoop()) {
864 // Since BBLoop had no parent, Unloop blocks are no longer in a loop.
865 for (Loop::block_iterator I
= Unloop
->block_begin(),
866 E
= Unloop
->block_end();
869 // Don't reparent blocks in subloops.
870 if (getLoopFor(*I
) != Unloop
)
873 // Blocks no longer have a parent but are still referenced by Unloop until
874 // the Unloop object is deleted.
875 changeLoopFor(*I
, nullptr);
878 // Remove the loop from the top-level LoopInfo object.
879 for (iterator I
= begin();; ++I
) {
880 assert(I
!= end() && "Couldn't find loop");
887 // Move all of the subloops to the top-level.
888 while (!Unloop
->empty())
889 addTopLevelLoop(Unloop
->removeChildLoop(std::prev(Unloop
->end())));
894 // Update the parent loop for all blocks within the loop. Blocks within
895 // subloops will not change parents.
896 UnloopUpdater
Updater(Unloop
, this);
897 Updater
.updateBlockParents();
899 // Remove blocks from former ancestor loops.
900 Updater
.removeBlocksFromAncestors();
902 // Add direct subloops as children in their new parent loop.
903 Updater
.updateSubloopParents();
905 // Remove unloop from its parent loop.
906 Loop
*ParentLoop
= Unloop
->getParentLoop();
907 for (Loop::iterator I
= ParentLoop
->begin();; ++I
) {
908 assert(I
!= ParentLoop
->end() && "Couldn't find loop");
910 ParentLoop
->removeChildLoop(I
);
916 AnalysisKey
LoopAnalysis::Key
;
918 LoopInfo
LoopAnalysis::run(Function
&F
, FunctionAnalysisManager
&AM
) {
919 // FIXME: Currently we create a LoopInfo from scratch for every function.
920 // This may prove to be too wasteful due to deallocating and re-allocating
921 // memory each time for the underlying map and vector datastructures. At some
922 // point it may prove worthwhile to use a freelist and recycle LoopInfo
923 // objects. I don't want to add that kind of complexity until the scope of
924 // the problem is better understood.
926 LI
.analyze(AM
.getResult
<DominatorTreeAnalysis
>(F
));
930 PreservedAnalyses
LoopPrinterPass::run(Function
&F
,
931 FunctionAnalysisManager
&AM
) {
932 AM
.getResult
<LoopAnalysis
>(F
).print(OS
);
933 return PreservedAnalyses::all();
936 void llvm::printLoop(Loop
&L
, raw_ostream
&OS
, const std::string
&Banner
) {
938 if (forcePrintModuleIR()) {
939 // handling -print-module-scope
940 OS
<< Banner
<< " (loop: ";
941 L
.getHeader()->printAsOperand(OS
, false);
944 // printing whole module
945 OS
<< *L
.getHeader()->getModule();
951 auto *PreHeader
= L
.getLoopPreheader();
953 OS
<< "\n; Preheader:";
954 PreHeader
->print(OS
);
958 for (auto *Block
: L
.blocks())
962 OS
<< "Printing <null> block";
964 SmallVector
<BasicBlock
*, 8> ExitBlocks
;
965 L
.getExitBlocks(ExitBlocks
);
966 if (!ExitBlocks
.empty()) {
967 OS
<< "\n; Exit blocks";
968 for (auto *Block
: ExitBlocks
)
972 OS
<< "Printing <null> block";
976 MDNode
*llvm::findOptionMDForLoopID(MDNode
*LoopID
, StringRef Name
) {
977 // No loop metadata node, no loop properties.
981 // First operand should refer to the metadata node itself, for legacy reasons.
982 assert(LoopID
->getNumOperands() > 0 && "requires at least one operand");
983 assert(LoopID
->getOperand(0) == LoopID
&& "invalid loop id");
985 // Iterate over the metdata node operands and look for MDString metadata.
986 for (unsigned i
= 1, e
= LoopID
->getNumOperands(); i
< e
; ++i
) {
987 MDNode
*MD
= dyn_cast
<MDNode
>(LoopID
->getOperand(i
));
988 if (!MD
|| MD
->getNumOperands() < 1)
990 MDString
*S
= dyn_cast
<MDString
>(MD
->getOperand(0));
993 // Return the operand node if MDString holds expected metadata.
994 if (Name
.equals(S
->getString()))
998 // Loop property not found.
1002 MDNode
*llvm::findOptionMDForLoop(const Loop
*TheLoop
, StringRef Name
) {
1003 return findOptionMDForLoopID(TheLoop
->getLoopID(), Name
);
1006 bool llvm::isValidAsAccessGroup(MDNode
*Node
) {
1007 return Node
->getNumOperands() == 0 && Node
->isDistinct();
1010 MDNode
*llvm::makePostTransformationMetadata(LLVMContext
&Context
,
1012 ArrayRef
<StringRef
> RemovePrefixes
,
1013 ArrayRef
<MDNode
*> AddAttrs
) {
1014 // First remove any existing loop metadata related to this transformation.
1015 SmallVector
<Metadata
*, 4> MDs
;
1017 // Reserve first location for self reference to the LoopID metadata node.
1018 TempMDTuple TempNode
= MDNode::getTemporary(Context
, None
);
1019 MDs
.push_back(TempNode
.get());
1021 // Remove metadata for the transformation that has been applied or that became
1024 for (unsigned i
= 1, ie
= OrigLoopID
->getNumOperands(); i
< ie
; ++i
) {
1025 bool IsVectorMetadata
= false;
1026 Metadata
*Op
= OrigLoopID
->getOperand(i
);
1027 if (MDNode
*MD
= dyn_cast
<MDNode
>(Op
)) {
1028 const MDString
*S
= dyn_cast
<MDString
>(MD
->getOperand(0));
1031 llvm::any_of(RemovePrefixes
, [S
](StringRef Prefix
) -> bool {
1032 return S
->getString().startswith(Prefix
);
1035 if (!IsVectorMetadata
)
1040 // Add metadata to avoid reapplying a transformation, such as
1041 // llvm.loop.unroll.disable and llvm.loop.isvectorized.
1042 MDs
.append(AddAttrs
.begin(), AddAttrs
.end());
1044 MDNode
*NewLoopID
= MDNode::getDistinct(Context
, MDs
);
1045 // Replace the temporary node with a self-reference.
1046 NewLoopID
->replaceOperandWith(0, NewLoopID
);
1050 //===----------------------------------------------------------------------===//
1051 // LoopInfo implementation
1054 char LoopInfoWrapperPass::ID
= 0;
1055 INITIALIZE_PASS_BEGIN(LoopInfoWrapperPass
, "loops", "Natural Loop Information",
1057 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass
)
1058 INITIALIZE_PASS_END(LoopInfoWrapperPass
, "loops", "Natural Loop Information",
1061 bool LoopInfoWrapperPass::runOnFunction(Function
&) {
1063 LI
.analyze(getAnalysis
<DominatorTreeWrapperPass
>().getDomTree());
1067 void LoopInfoWrapperPass::verifyAnalysis() const {
1068 // LoopInfoWrapperPass is a FunctionPass, but verifying every loop in the
1069 // function each time verifyAnalysis is called is very expensive. The
1070 // -verify-loop-info option can enable this. In order to perform some
1071 // checking by default, LoopPass has been taught to call verifyLoop manually
1072 // during loop pass sequences.
1073 if (VerifyLoopInfo
) {
1074 auto &DT
= getAnalysis
<DominatorTreeWrapperPass
>().getDomTree();
1079 void LoopInfoWrapperPass::getAnalysisUsage(AnalysisUsage
&AU
) const {
1080 AU
.setPreservesAll();
1081 AU
.addRequiredTransitive
<DominatorTreeWrapperPass
>();
1084 void LoopInfoWrapperPass::print(raw_ostream
&OS
, const Module
*) const {
1088 PreservedAnalyses
LoopVerifierPass::run(Function
&F
,
1089 FunctionAnalysisManager
&AM
) {
1090 LoopInfo
&LI
= AM
.getResult
<LoopAnalysis
>(F
);
1091 auto &DT
= AM
.getResult
<DominatorTreeAnalysis
>(F
);
1093 return PreservedAnalyses::all();
1096 //===----------------------------------------------------------------------===//
1097 // LoopBlocksDFS implementation
1100 /// Traverse the loop blocks and store the DFS result.
1101 /// Useful for clients that just want the final DFS result and don't need to
1102 /// visit blocks during the initial traversal.
1103 void LoopBlocksDFS::perform(LoopInfo
*LI
) {
1104 LoopBlocksTraversal
Traversal(*this, LI
);
1105 for (LoopBlocksTraversal::POTIterator POI
= Traversal
.begin(),
1106 POE
= Traversal
.end();