1 //===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===//
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. 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.
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.
18 // It can calculate on the fly various bits of information, for example:
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
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.
33 // For an overview of terminology used in this API (and thus all of our loop
34 // analyses or transforms), see docs/LoopTerminology.rst.
36 //===----------------------------------------------------------------------===//
38 #ifndef LLVM_ANALYSIS_LOOPINFO_H
39 #define LLVM_ANALYSIS_LOOPINFO_H
41 #include "llvm/ADT/DenseMap.h"
42 #include "llvm/ADT/DenseSet.h"
43 #include "llvm/ADT/GraphTraits.h"
44 #include "llvm/ADT/SmallPtrSet.h"
45 #include "llvm/ADT/SmallVector.h"
46 #include "llvm/IR/CFG.h"
47 #include "llvm/IR/Instruction.h"
48 #include "llvm/IR/Instructions.h"
49 #include "llvm/IR/PassManager.h"
50 #include "llvm/Pass.h"
51 #include "llvm/Support/Allocator.h"
60 class InductionDescriptor
;
62 class MemorySSAUpdater
;
64 class ScalarEvolution
;
66 template <class N
, bool IsPostDom
> class DominatorTreeBase
;
67 template <class N
, class M
> class LoopInfoBase
;
68 template <class N
, class M
> class LoopBase
;
70 //===----------------------------------------------------------------------===//
71 /// Instances of this class are used to represent loops that are detected in the
74 template <class BlockT
, class LoopT
> class LoopBase
{
76 // Loops contained entirely within this one.
77 std::vector
<LoopT
*> SubLoops
;
79 // The list of blocks in this loop. First entry is the header node.
80 std::vector
<BlockT
*> Blocks
;
82 SmallPtrSet
<const BlockT
*, 8> DenseBlockSet
;
84 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
85 /// Indicator that this loop is no longer a valid loop.
86 bool IsInvalid
= false;
89 LoopBase(const LoopBase
<BlockT
, LoopT
> &) = delete;
90 const LoopBase
<BlockT
, LoopT
> &
91 operator=(const LoopBase
<BlockT
, LoopT
> &) = delete;
94 /// Return the nesting level of this loop. An outer-most loop has depth 1,
95 /// for consistency with loop depth values used for basic blocks, where depth
96 /// 0 is used for blocks not inside any loops.
97 unsigned getLoopDepth() const {
98 assert(!isInvalid() && "Loop not in a valid state!");
100 for (const LoopT
*CurLoop
= ParentLoop
; CurLoop
;
101 CurLoop
= CurLoop
->ParentLoop
)
105 BlockT
*getHeader() const { return getBlocks().front(); }
106 LoopT
*getParentLoop() const { return ParentLoop
; }
108 /// This is a raw interface for bypassing addChildLoop.
109 void setParentLoop(LoopT
*L
) {
110 assert(!isInvalid() && "Loop not in a valid state!");
114 /// Return true if the specified loop is contained within in this loop.
115 bool contains(const LoopT
*L
) const {
116 assert(!isInvalid() && "Loop not in a valid state!");
121 return contains(L
->getParentLoop());
124 /// Return true if the specified basic block is in this loop.
125 bool contains(const BlockT
*BB
) const {
126 assert(!isInvalid() && "Loop not in a valid state!");
127 return DenseBlockSet
.count(BB
);
130 /// Return true if the specified instruction is in this loop.
131 template <class InstT
> bool contains(const InstT
*Inst
) const {
132 return contains(Inst
->getParent());
135 /// Return the loops contained entirely within this loop.
136 const std::vector
<LoopT
*> &getSubLoops() const {
137 assert(!isInvalid() && "Loop not in a valid state!");
140 std::vector
<LoopT
*> &getSubLoopsVector() {
141 assert(!isInvalid() && "Loop not in a valid state!");
144 typedef typename
std::vector
<LoopT
*>::const_iterator iterator
;
146 typename
std::vector
<LoopT
*>::const_reverse_iterator reverse_iterator
;
147 iterator
begin() const { return getSubLoops().begin(); }
148 iterator
end() const { return getSubLoops().end(); }
149 reverse_iterator
rbegin() const { return getSubLoops().rbegin(); }
150 reverse_iterator
rend() const { return getSubLoops().rend(); }
151 bool empty() const { return getSubLoops().empty(); }
153 /// Get a list of the basic blocks which make up this loop.
154 ArrayRef
<BlockT
*> getBlocks() const {
155 assert(!isInvalid() && "Loop not in a valid state!");
158 typedef typename ArrayRef
<BlockT
*>::const_iterator block_iterator
;
159 block_iterator
block_begin() const { return getBlocks().begin(); }
160 block_iterator
block_end() const { return getBlocks().end(); }
161 inline iterator_range
<block_iterator
> blocks() const {
162 assert(!isInvalid() && "Loop not in a valid state!");
163 return make_range(block_begin(), block_end());
166 /// Get the number of blocks in this loop in constant time.
167 /// Invalidate the loop, indicating that it is no longer a loop.
168 unsigned getNumBlocks() const {
169 assert(!isInvalid() && "Loop not in a valid state!");
170 return Blocks
.size();
173 /// Return a direct, mutable handle to the blocks vector so that we can
174 /// mutate it efficiently with techniques like `std::remove`.
175 std::vector
<BlockT
*> &getBlocksVector() {
176 assert(!isInvalid() && "Loop not in a valid state!");
179 /// Return a direct, mutable handle to the blocks set so that we can
180 /// mutate it efficiently.
181 SmallPtrSetImpl
<const BlockT
*> &getBlocksSet() {
182 assert(!isInvalid() && "Loop not in a valid state!");
183 return DenseBlockSet
;
186 /// Return a direct, immutable handle to the blocks set.
187 const SmallPtrSetImpl
<const BlockT
*> &getBlocksSet() const {
188 assert(!isInvalid() && "Loop not in a valid state!");
189 return DenseBlockSet
;
192 /// Return true if this loop is no longer valid. The only valid use of this
193 /// helper is "assert(L.isInvalid())" or equivalent, since IsInvalid is set to
194 /// true by the destructor. In other words, if this accessor returns true,
195 /// the caller has already triggered UB by calling this accessor; and so it
196 /// can only be called in a context where a return value of true indicates a
197 /// programmer error.
198 bool isInvalid() const {
199 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
206 /// True if terminator in the block can branch to another block that is
207 /// outside of the current loop. \p BB must be inside the loop.
208 bool isLoopExiting(const BlockT
*BB
) const {
209 assert(!isInvalid() && "Loop not in a valid state!");
210 assert(contains(BB
) && "Exiting block must be part of the loop");
211 for (const auto &Succ
: children
<const BlockT
*>(BB
)) {
218 /// Returns true if \p BB is a loop-latch.
219 /// A latch block is a block that contains a branch back to the header.
220 /// This function is useful when there are multiple latches in a loop
221 /// because \fn getLoopLatch will return nullptr in that case.
222 bool isLoopLatch(const BlockT
*BB
) const {
223 assert(!isInvalid() && "Loop not in a valid state!");
224 assert(contains(BB
) && "block does not belong to the loop");
226 BlockT
*Header
= getHeader();
227 auto PredBegin
= GraphTraits
<Inverse
<BlockT
*>>::child_begin(Header
);
228 auto PredEnd
= GraphTraits
<Inverse
<BlockT
*>>::child_end(Header
);
229 return std::find(PredBegin
, PredEnd
, BB
) != PredEnd
;
232 /// Calculate the number of back edges to the loop header.
233 unsigned getNumBackEdges() const {
234 assert(!isInvalid() && "Loop not in a valid state!");
235 unsigned NumBackEdges
= 0;
236 BlockT
*H
= getHeader();
238 for (const auto Pred
: children
<Inverse
<BlockT
*>>(H
))
245 //===--------------------------------------------------------------------===//
246 // APIs for simple analysis of the loop.
248 // Note that all of these methods can fail on general loops (ie, there may not
249 // be a preheader, etc). For best success, the loop simplification and
250 // induction variable canonicalization pass should be used to normalize loops
251 // for easy analysis. These methods assume canonical loops.
253 /// Return all blocks inside the loop that have successors outside of the
254 /// loop. These are the blocks _inside of the current loop_ which branch out.
255 /// The returned list is always unique.
256 void getExitingBlocks(SmallVectorImpl
<BlockT
*> &ExitingBlocks
) const;
258 /// If getExitingBlocks would return exactly one block, return that block.
259 /// Otherwise return null.
260 BlockT
*getExitingBlock() const;
262 /// Return all of the successor blocks of this loop. These are the blocks
263 /// _outside of the current loop_ which are branched to.
264 void getExitBlocks(SmallVectorImpl
<BlockT
*> &ExitBlocks
) const;
266 /// If getExitBlocks would return exactly one block, return that block.
267 /// Otherwise return null.
268 BlockT
*getExitBlock() const;
270 /// Return true if no exit block for the loop has a predecessor that is
271 /// outside the loop.
272 bool hasDedicatedExits() const;
274 /// Return all unique successor blocks of this loop.
275 /// These are the blocks _outside of the current loop_ which are branched to.
276 void getUniqueExitBlocks(SmallVectorImpl
<BlockT
*> &ExitBlocks
) const;
278 /// Return all unique successor blocks of this loop except successors from
279 /// Latch block are not considered. If the exit comes from Latch has also
280 /// non Latch predecessor in a loop it will be added to ExitBlocks.
281 /// These are the blocks _outside of the current loop_ which are branched to.
282 void getUniqueNonLatchExitBlocks(SmallVectorImpl
<BlockT
*> &ExitBlocks
) const;
284 /// If getUniqueExitBlocks would return exactly one block, return that block.
285 /// Otherwise return null.
286 BlockT
*getUniqueExitBlock() const;
289 typedef std::pair
<BlockT
*, BlockT
*> Edge
;
291 /// Return all pairs of (_inside_block_,_outside_block_).
292 void getExitEdges(SmallVectorImpl
<Edge
> &ExitEdges
) const;
294 /// If there is a preheader for this loop, return it. A loop has a preheader
295 /// if there is only one edge to the header of the loop from outside of the
296 /// loop. If this is the case, the block branching to the header of the loop
297 /// is the preheader node.
299 /// This method returns null if there is no preheader for the loop.
300 BlockT
*getLoopPreheader() const;
302 /// If the given loop's header has exactly one unique predecessor outside the
303 /// loop, return it. Otherwise return null.
304 /// This is less strict that the loop "preheader" concept, which requires
305 /// the predecessor to have exactly one successor.
306 BlockT
*getLoopPredecessor() const;
308 /// If there is a single latch block for this loop, return it.
309 /// A latch block is a block that contains a branch back to the header.
310 BlockT
*getLoopLatch() const;
312 /// Return all loop latch blocks of this loop. A latch block is a block that
313 /// contains a branch back to the header.
314 void getLoopLatches(SmallVectorImpl
<BlockT
*> &LoopLatches
) const {
315 assert(!isInvalid() && "Loop not in a valid state!");
316 BlockT
*H
= getHeader();
317 for (const auto Pred
: children
<Inverse
<BlockT
*>>(H
))
319 LoopLatches
.push_back(Pred
);
322 /// Return all inner loops in the loop nest rooted by the loop in preorder,
323 /// with siblings in forward program order.
324 template <class Type
>
325 static void getInnerLoopsInPreorder(const LoopT
&L
,
326 SmallVectorImpl
<Type
> &PreOrderLoops
) {
327 SmallVector
<LoopT
*, 4> PreOrderWorklist
;
328 PreOrderWorklist
.append(L
.rbegin(), L
.rend());
330 while (!PreOrderWorklist
.empty()) {
331 LoopT
*L
= PreOrderWorklist
.pop_back_val();
332 // Sub-loops are stored in forward program order, but will process the
333 // worklist backwards so append them in reverse order.
334 PreOrderWorklist
.append(L
->rbegin(), L
->rend());
335 PreOrderLoops
.push_back(L
);
339 /// Return all loops in the loop nest rooted by the loop in preorder, with
340 /// siblings in forward program order.
341 SmallVector
<const LoopT
*, 4> getLoopsInPreorder() const {
342 SmallVector
<const LoopT
*, 4> PreOrderLoops
;
343 const LoopT
*CurLoop
= static_cast<const LoopT
*>(this);
344 PreOrderLoops
.push_back(CurLoop
);
345 getInnerLoopsInPreorder(*CurLoop
, PreOrderLoops
);
346 return PreOrderLoops
;
348 SmallVector
<LoopT
*, 4> getLoopsInPreorder() {
349 SmallVector
<LoopT
*, 4> PreOrderLoops
;
350 LoopT
*CurLoop
= static_cast<LoopT
*>(this);
351 PreOrderLoops
.push_back(CurLoop
);
352 getInnerLoopsInPreorder(*CurLoop
, PreOrderLoops
);
353 return PreOrderLoops
;
356 //===--------------------------------------------------------------------===//
357 // APIs for updating loop information after changing the CFG
360 /// This method is used by other analyses to update loop information.
361 /// NewBB is set to be a new member of the current loop.
362 /// Because of this, it is added as a member of all parent loops, and is added
363 /// to the specified LoopInfo object as being in the current basic block. It
364 /// is not valid to replace the loop header with this method.
365 void addBasicBlockToLoop(BlockT
*NewBB
, LoopInfoBase
<BlockT
, LoopT
> &LI
);
367 /// This is used when splitting loops up. It replaces the OldChild entry in
368 /// our children list with NewChild, and updates the parent pointer of
369 /// OldChild to be null and the NewChild to be this loop.
370 /// This updates the loop depth of the new child.
371 void replaceChildLoopWith(LoopT
*OldChild
, LoopT
*NewChild
);
373 /// Add the specified loop to be a child of this loop.
374 /// This updates the loop depth of the new child.
375 void addChildLoop(LoopT
*NewChild
) {
376 assert(!isInvalid() && "Loop not in a valid state!");
377 assert(!NewChild
->ParentLoop
&& "NewChild already has a parent!");
378 NewChild
->ParentLoop
= static_cast<LoopT
*>(this);
379 SubLoops
.push_back(NewChild
);
382 /// This removes the specified child from being a subloop of this loop. The
383 /// loop is not deleted, as it will presumably be inserted into another loop.
384 LoopT
*removeChildLoop(iterator I
) {
385 assert(!isInvalid() && "Loop not in a valid state!");
386 assert(I
!= SubLoops
.end() && "Cannot remove end iterator!");
388 assert(Child
->ParentLoop
== this && "Child is not a child of this loop!");
389 SubLoops
.erase(SubLoops
.begin() + (I
- begin()));
390 Child
->ParentLoop
= nullptr;
394 /// This removes the specified child from being a subloop of this loop. The
395 /// loop is not deleted, as it will presumably be inserted into another loop.
396 LoopT
*removeChildLoop(LoopT
*Child
) {
397 return removeChildLoop(llvm::find(*this, Child
));
400 /// This adds a basic block directly to the basic block list.
401 /// This should only be used by transformations that create new loops. Other
402 /// transformations should use addBasicBlockToLoop.
403 void addBlockEntry(BlockT
*BB
) {
404 assert(!isInvalid() && "Loop not in a valid state!");
405 Blocks
.push_back(BB
);
406 DenseBlockSet
.insert(BB
);
409 /// interface to reverse Blocks[from, end of loop] in this loop
410 void reverseBlock(unsigned from
) {
411 assert(!isInvalid() && "Loop not in a valid state!");
412 std::reverse(Blocks
.begin() + from
, Blocks
.end());
415 /// interface to do reserve() for Blocks
416 void reserveBlocks(unsigned size
) {
417 assert(!isInvalid() && "Loop not in a valid state!");
418 Blocks
.reserve(size
);
421 /// This method is used to move BB (which must be part of this loop) to be the
422 /// loop header of the loop (the block that dominates all others).
423 void moveToHeader(BlockT
*BB
) {
424 assert(!isInvalid() && "Loop not in a valid state!");
427 for (unsigned i
= 0;; ++i
) {
428 assert(i
!= Blocks
.size() && "Loop does not contain BB!");
429 if (Blocks
[i
] == BB
) {
430 Blocks
[i
] = Blocks
[0];
437 /// This removes the specified basic block from the current loop, updating the
438 /// Blocks as appropriate. This does not update the mapping in the LoopInfo
440 void removeBlockFromLoop(BlockT
*BB
) {
441 assert(!isInvalid() && "Loop not in a valid state!");
442 auto I
= find(Blocks
, BB
);
443 assert(I
!= Blocks
.end() && "N is not in this list!");
446 DenseBlockSet
.erase(BB
);
449 /// Verify loop structure
450 void verifyLoop() const;
452 /// Verify loop structure of this loop and all nested loops.
453 void verifyLoopNest(DenseSet
<const LoopT
*> *Loops
) const;
455 /// Returns true if the loop is annotated parallel.
457 /// Derived classes can override this method using static template
459 bool isAnnotatedParallel() const { return false; }
461 /// Print loop with all the BBs inside it.
462 void print(raw_ostream
&OS
, unsigned Depth
= 0, bool Verbose
= false) const;
465 friend class LoopInfoBase
<BlockT
, LoopT
>;
467 /// This creates an empty loop.
468 LoopBase() : ParentLoop(nullptr) {}
470 explicit LoopBase(BlockT
*BB
) : ParentLoop(nullptr) {
471 Blocks
.push_back(BB
);
472 DenseBlockSet
.insert(BB
);
475 // Since loop passes like SCEV are allowed to key analysis results off of
476 // `Loop` pointers, we cannot re-use pointers within a loop pass manager.
477 // This means loop passes should not be `delete` ing `Loop` objects directly
478 // (and risk a later `Loop` allocation re-using the address of a previous one)
479 // but should be using LoopInfo::markAsRemoved, which keeps around the `Loop`
480 // pointer till the end of the lifetime of the `LoopInfo` object.
482 // To make it easier to follow this rule, we mark the destructor as
485 for (auto *SubLoop
: SubLoops
)
488 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
493 DenseBlockSet
.clear();
494 ParentLoop
= nullptr;
498 template <class BlockT
, class LoopT
>
499 raw_ostream
&operator<<(raw_ostream
&OS
, const LoopBase
<BlockT
, LoopT
> &Loop
) {
504 // Implementation in LoopInfoImpl.h
505 extern template class LoopBase
<BasicBlock
, Loop
>;
507 /// Represents a single loop in the control flow graph. Note that not all SCCs
508 /// in the CFG are necessarily loops.
509 class Loop
: public LoopBase
<BasicBlock
, Loop
> {
511 /// A range representing the start and end location of a loop.
518 LocRange(DebugLoc Start
) : Start(Start
), End(Start
) {}
519 LocRange(DebugLoc Start
, DebugLoc End
)
520 : Start(std::move(Start
)), End(std::move(End
)) {}
522 const DebugLoc
&getStart() const { return Start
; }
523 const DebugLoc
&getEnd() const { return End
; }
527 explicit operator bool() const { return Start
&& End
; }
530 /// Return true if the specified value is loop invariant.
531 bool isLoopInvariant(const Value
*V
) const;
533 /// Return true if all the operands of the specified instruction are loop
535 bool hasLoopInvariantOperands(const Instruction
*I
) const;
537 /// If the given value is an instruction inside of the loop and it can be
538 /// hoisted, do so to make it trivially loop-invariant.
539 /// Return true if the value after any hoisting is loop invariant. This
540 /// function can be used as a slightly more aggressive replacement for
543 /// If InsertPt is specified, it is the point to hoist instructions to.
544 /// If null, the terminator of the loop preheader is used.
545 bool makeLoopInvariant(Value
*V
, bool &Changed
,
546 Instruction
*InsertPt
= nullptr,
547 MemorySSAUpdater
*MSSAU
= nullptr) const;
549 /// If the given instruction is inside of the loop and it can be hoisted, do
550 /// so to make it trivially loop-invariant.
551 /// Return true if the instruction after any hoisting is loop invariant. This
552 /// function can be used as a slightly more aggressive replacement for
555 /// If InsertPt is specified, it is the point to hoist instructions to.
556 /// If null, the terminator of the loop preheader is used.
558 bool makeLoopInvariant(Instruction
*I
, bool &Changed
,
559 Instruction
*InsertPt
= nullptr,
560 MemorySSAUpdater
*MSSAU
= nullptr) const;
562 /// Check to see if the loop has a canonical induction variable: an integer
563 /// recurrence that starts at 0 and increments by one each time through the
564 /// loop. If so, return the phi node that corresponds to it.
566 /// The IndVarSimplify pass transforms loops to have a canonical induction
569 PHINode
*getCanonicalInductionVariable() const;
571 /// Obtain the unique incoming and back edge. Return false if they are
572 /// non-unique or the loop is dead; otherwise, return true.
573 bool getIncomingAndBackEdge(BasicBlock
*&Incoming
,
574 BasicBlock
*&Backedge
) const;
576 /// Below are some utilities to get the loop guard, loop bounds and induction
577 /// variable, and to check if a given phinode is an auxiliary induction
578 /// variable, if the loop is guarded, and if the loop is canonical.
580 /// Here is an example:
582 /// for (int i = lb; i < ub; i+=step)
584 /// --- pseudo LLVMIR ---
586 /// guardcmp = (lb < ub)
587 /// if (guardcmp) goto preheader; else goto afterloop
590 /// i_1 = phi[{lb, preheader}, {i_2, latch}]
595 /// if (cmp) goto loop
601 /// - getInitialIVValue --> lb
602 /// - getStepInst --> i_2 = i_1 + step
603 /// - getStepValue --> step
604 /// - getFinalIVValue --> ub
605 /// - getCanonicalPredicate --> '<'
606 /// - getDirection --> Increasing
608 /// - getInductionVariable --> i_1
609 /// - isAuxiliaryInductionVariable(x) --> true if x == i_1
610 /// - getLoopGuardBranch()
611 /// --> `if (guardcmp) goto preheader; else goto afterloop`
612 /// - isGuarded() --> true
613 /// - isCanonical --> false
615 /// Return the LoopBounds object if
616 /// - the given \p IndVar is an induction variable
617 /// - the initial value of the induction variable can be found
618 /// - the step instruction of the induction variable can be found
619 /// - the final value of the induction variable can be found
622 static Optional
<Loop::LoopBounds
> getBounds(const Loop
&L
, PHINode
&IndVar
,
623 ScalarEvolution
&SE
);
625 /// Get the initial value of the loop induction variable.
626 Value
&getInitialIVValue() const { return InitialIVValue
; }
628 /// Get the instruction that updates the loop induction variable.
629 Instruction
&getStepInst() const { return StepInst
; }
631 /// Get the step that the loop induction variable gets updated by in each
632 /// loop iteration. Return nullptr if not found.
633 Value
*getStepValue() const { return StepValue
; }
635 /// Get the final value of the loop induction variable.
636 Value
&getFinalIVValue() const { return FinalIVValue
; }
638 /// Return the canonical predicate for the latch compare instruction, if
639 /// able to be calcuated. Else BAD_ICMP_PREDICATE.
641 /// A predicate is considered as canonical if requirements below are all
643 /// 1. The first successor of the latch branch is the loop header
644 /// If not, inverse the predicate.
645 /// 2. One of the operands of the latch comparison is StepInst
647 /// - if the current calcuated predicate is not ne or eq, flip the
649 /// - else if the loop is increasing, return slt
650 /// (notice that it is safe to change from ne or eq to sign compare)
651 /// - else if the loop is decreasing, return sgt
652 /// (notice that it is safe to change from ne or eq to sign compare)
654 /// Here is an example when both (1) and (2) are not satisfied:
657 /// %iv = phi [%initialiv, %loop.preheader], [%inc, %loop.header]
658 /// %inc = add %iv, %step
659 /// %cmp = slt %iv, %finaliv
660 /// br %cmp, %loop.exit, %loop.header
663 /// - The second successor of the latch branch is the loop header instead
664 /// of the first successor (slt -> sge)
665 /// - The first operand of the latch comparison (%cmp) is the IndVar (%iv)
666 /// instead of the StepInst (%inc) (sge -> sgt)
668 /// The predicate would be sgt if both (1) and (2) are satisfied.
669 /// getCanonicalPredicate() returns sgt for this example.
670 /// Note: The IR is not changed.
671 ICmpInst::Predicate
getCanonicalPredicate() const;
673 /// An enum for the direction of the loop
674 /// - for (int i = 0; i < ub; ++i) --> Increasing
675 /// - for (int i = ub; i > 0; --i) --> Descresing
676 /// - for (int i = x; i != y; i+=z) --> Unknown
677 enum class Direction
{ Increasing
, Decreasing
, Unknown
};
679 /// Get the direction of the loop.
680 Direction
getDirection() const;
683 LoopBounds(const Loop
&Loop
, Value
&I
, Instruction
&SI
, Value
*SV
, Value
&F
,
685 : L(Loop
), InitialIVValue(I
), StepInst(SI
), StepValue(SV
),
686 FinalIVValue(F
), SE(SE
) {}
690 // The initial value of the loop induction variable
691 Value
&InitialIVValue
;
693 // The instruction that updates the loop induction variable
694 Instruction
&StepInst
;
696 // The value that the loop induction variable gets updated by in each loop
700 // The final value of the loop induction variable
706 /// Return the struct LoopBounds collected if all struct members are found,
708 Optional
<LoopBounds
> getBounds(ScalarEvolution
&SE
) const;
710 /// Return the loop induction variable if found, else return nullptr.
711 /// An instruction is considered as the loop induction variable if
712 /// - it is an induction variable of the loop; and
713 /// - it is used to determine the condition of the branch in the loop latch
715 /// Note: the induction variable doesn't need to be canonical, i.e. starts at
716 /// zero and increments by one each time through the loop (but it can be).
717 PHINode
*getInductionVariable(ScalarEvolution
&SE
) const;
719 /// Get the loop induction descriptor for the loop induction variable. Return
720 /// true if the loop induction variable is found.
721 bool getInductionDescriptor(ScalarEvolution
&SE
,
722 InductionDescriptor
&IndDesc
) const;
724 /// Return true if the given PHINode \p AuxIndVar is
725 /// - in the loop header
726 /// - not used outside of the loop
727 /// - incremented by a loop invariant step for each loop iteration
728 /// - step instruction opcode should be add or sub
729 /// Note: auxiliary induction variable is not required to be used in the
730 /// conditional branch in the loop latch. (but it can be)
731 bool isAuxiliaryInductionVariable(PHINode
&AuxIndVar
,
732 ScalarEvolution
&SE
) const;
734 /// Return the loop guard branch, if it exists.
736 /// This currently only works on simplified loop, as it requires a preheader
737 /// and a latch to identify the guard. It will work on loops of the form:
740 /// br cond1, Preheader, ExitSucc <== GuardBranch
747 /// br cond2, Header, ExitBlock
752 BranchInst
*getLoopGuardBranch() const;
754 /// Return true iff the loop is
755 /// - in simplify rotated form, and
756 /// - guarded by a loop guard branch.
757 bool isGuarded() const { return (getLoopGuardBranch() != nullptr); }
759 /// Return true if the loop induction variable starts at zero and increments
760 /// by one each time through the loop.
761 bool isCanonical(ScalarEvolution
&SE
) const;
763 /// Return true if the Loop is in LCSSA form.
764 bool isLCSSAForm(DominatorTree
&DT
) const;
766 /// Return true if this Loop and all inner subloops are in LCSSA form.
767 bool isRecursivelyLCSSAForm(DominatorTree
&DT
, const LoopInfo
&LI
) const;
769 /// Return true if the Loop is in the form that the LoopSimplify form
770 /// transforms loops to, which is sometimes called normal form.
771 bool isLoopSimplifyForm() const;
773 /// Return true if the loop body is safe to clone in practice.
774 bool isSafeToClone() const;
776 /// Returns true if the loop is annotated parallel.
778 /// A parallel loop can be assumed to not contain any dependencies between
779 /// iterations by the compiler. That is, any loop-carried dependency checking
780 /// can be skipped completely when parallelizing the loop on the target
781 /// machine. Thus, if the parallel loop information originates from the
782 /// programmer, e.g. via the OpenMP parallel for pragma, it is the
783 /// programmer's responsibility to ensure there are no loop-carried
784 /// dependencies. The final execution order of the instructions across
785 /// iterations is not guaranteed, thus, the end result might or might not
786 /// implement actual concurrent execution of instructions across multiple
788 bool isAnnotatedParallel() const;
790 /// Return the llvm.loop loop id metadata node for this loop if it is present.
792 /// If this loop contains the same llvm.loop metadata on each branch to the
793 /// header then the node is returned. If any latch instruction does not
794 /// contain llvm.loop or if multiple latches contain different nodes then
796 MDNode
*getLoopID() const;
797 /// Set the llvm.loop loop id metadata for this loop.
799 /// The LoopID metadata node will be added to each terminator instruction in
800 /// the loop that branches to the loop header.
802 /// The LoopID metadata node should have one or more operands and the first
803 /// operand should be the node itself.
804 void setLoopID(MDNode
*LoopID
) const;
806 /// Add llvm.loop.unroll.disable to this loop's loop id metadata.
808 /// Remove existing unroll metadata and add unroll disable metadata to
809 /// indicate the loop has already been unrolled. This prevents a loop
810 /// from being unrolled more than is directed by a pragma if the loop
811 /// unrolling pass is run more than once (which it generally is).
812 void setLoopAlreadyUnrolled();
815 void dumpVerbose() const;
817 /// Return the debug location of the start of this loop.
818 /// This looks for a BB terminating instruction with a known debug
819 /// location by looking at the preheader and header blocks. If it
820 /// cannot find a terminating instruction with location information,
821 /// it returns an unknown location.
822 DebugLoc
getStartLoc() const;
824 /// Return the source code span of the loop.
825 LocRange
getLocRange() const;
827 StringRef
getName() const {
828 if (BasicBlock
*Header
= getHeader())
829 if (Header
->hasName())
830 return Header
->getName();
831 return "<unnamed loop>";
837 friend class LoopInfoBase
<BasicBlock
, Loop
>;
838 friend class LoopBase
<BasicBlock
, Loop
>;
839 explicit Loop(BasicBlock
*BB
) : LoopBase
<BasicBlock
, Loop
>(BB
) {}
843 //===----------------------------------------------------------------------===//
844 /// This class builds and contains all of the top-level loop
845 /// structures in the specified function.
848 template <class BlockT
, class LoopT
> class LoopInfoBase
{
849 // BBMap - Mapping of basic blocks to the inner most loop they occur in
850 DenseMap
<const BlockT
*, LoopT
*> BBMap
;
851 std::vector
<LoopT
*> TopLevelLoops
;
852 BumpPtrAllocator LoopAllocator
;
854 friend class LoopBase
<BlockT
, LoopT
>;
855 friend class LoopInfo
;
857 void operator=(const LoopInfoBase
&) = delete;
858 LoopInfoBase(const LoopInfoBase
&) = delete;
862 ~LoopInfoBase() { releaseMemory(); }
864 LoopInfoBase(LoopInfoBase
&&Arg
)
865 : BBMap(std::move(Arg
.BBMap
)),
866 TopLevelLoops(std::move(Arg
.TopLevelLoops
)),
867 LoopAllocator(std::move(Arg
.LoopAllocator
)) {
868 // We have to clear the arguments top level loops as we've taken ownership.
869 Arg
.TopLevelLoops
.clear();
871 LoopInfoBase
&operator=(LoopInfoBase
&&RHS
) {
872 BBMap
= std::move(RHS
.BBMap
);
874 for (auto *L
: TopLevelLoops
)
877 TopLevelLoops
= std::move(RHS
.TopLevelLoops
);
878 LoopAllocator
= std::move(RHS
.LoopAllocator
);
879 RHS
.TopLevelLoops
.clear();
883 void releaseMemory() {
886 for (auto *L
: TopLevelLoops
)
888 TopLevelLoops
.clear();
889 LoopAllocator
.Reset();
892 template <typename
... ArgsTy
> LoopT
*AllocateLoop(ArgsTy
&&... Args
) {
893 LoopT
*Storage
= LoopAllocator
.Allocate
<LoopT
>();
894 return new (Storage
) LoopT(std::forward
<ArgsTy
>(Args
)...);
897 /// iterator/begin/end - The interface to the top-level loops in the current
900 typedef typename
std::vector
<LoopT
*>::const_iterator iterator
;
902 typename
std::vector
<LoopT
*>::const_reverse_iterator reverse_iterator
;
903 iterator
begin() const { return TopLevelLoops
.begin(); }
904 iterator
end() const { return TopLevelLoops
.end(); }
905 reverse_iterator
rbegin() const { return TopLevelLoops
.rbegin(); }
906 reverse_iterator
rend() const { return TopLevelLoops
.rend(); }
907 bool empty() const { return TopLevelLoops
.empty(); }
909 /// Return all of the loops in the function in preorder across the loop
910 /// nests, with siblings in forward program order.
912 /// Note that because loops form a forest of trees, preorder is equivalent to
913 /// reverse postorder.
914 SmallVector
<LoopT
*, 4> getLoopsInPreorder();
916 /// Return all of the loops in the function in preorder across the loop
917 /// nests, with siblings in *reverse* program order.
919 /// Note that because loops form a forest of trees, preorder is equivalent to
920 /// reverse postorder.
922 /// Also note that this is *not* a reverse preorder. Only the siblings are in
923 /// reverse program order.
924 SmallVector
<LoopT
*, 4> getLoopsInReverseSiblingPreorder();
926 /// Return the inner most loop that BB lives in. If a basic block is in no
927 /// loop (for example the entry node), null is returned.
928 LoopT
*getLoopFor(const BlockT
*BB
) const { return BBMap
.lookup(BB
); }
930 /// Same as getLoopFor.
931 const LoopT
*operator[](const BlockT
*BB
) const { return getLoopFor(BB
); }
933 /// Return the loop nesting level of the specified block. A depth of 0 means
934 /// the block is not inside any loop.
935 unsigned getLoopDepth(const BlockT
*BB
) const {
936 const LoopT
*L
= getLoopFor(BB
);
937 return L
? L
->getLoopDepth() : 0;
940 // True if the block is a loop header node
941 bool isLoopHeader(const BlockT
*BB
) const {
942 const LoopT
*L
= getLoopFor(BB
);
943 return L
&& L
->getHeader() == BB
;
946 /// This removes the specified top-level loop from this loop info object.
947 /// The loop is not deleted, as it will presumably be inserted into
949 LoopT
*removeLoop(iterator I
) {
950 assert(I
!= end() && "Cannot remove end iterator!");
952 assert(!L
->getParentLoop() && "Not a top-level loop!");
953 TopLevelLoops
.erase(TopLevelLoops
.begin() + (I
- begin()));
957 /// Change the top-level loop that contains BB to the specified loop.
958 /// This should be used by transformations that restructure the loop hierarchy
960 void changeLoopFor(BlockT
*BB
, LoopT
*L
) {
968 /// Replace the specified loop in the top-level loops list with the indicated
970 void changeTopLevelLoop(LoopT
*OldLoop
, LoopT
*NewLoop
) {
971 auto I
= find(TopLevelLoops
, OldLoop
);
972 assert(I
!= TopLevelLoops
.end() && "Old loop not at top level!");
974 assert(!NewLoop
->ParentLoop
&& !OldLoop
->ParentLoop
&&
975 "Loops already embedded into a subloop!");
978 /// This adds the specified loop to the collection of top-level loops.
979 void addTopLevelLoop(LoopT
*New
) {
980 assert(!New
->getParentLoop() && "Loop already in subloop!");
981 TopLevelLoops
.push_back(New
);
984 /// This method completely removes BB from all data structures,
985 /// including all of the Loop objects it is nested in and our mapping from
986 /// BasicBlocks to loops.
987 void removeBlock(BlockT
*BB
) {
988 auto I
= BBMap
.find(BB
);
989 if (I
!= BBMap
.end()) {
990 for (LoopT
*L
= I
->second
; L
; L
= L
->getParentLoop())
991 L
->removeBlockFromLoop(BB
);
999 static bool isNotAlreadyContainedIn(const LoopT
*SubLoop
,
1000 const LoopT
*ParentLoop
) {
1003 if (SubLoop
== ParentLoop
)
1005 return isNotAlreadyContainedIn(SubLoop
->getParentLoop(), ParentLoop
);
1008 /// Create the loop forest using a stable algorithm.
1009 void analyze(const DominatorTreeBase
<BlockT
, false> &DomTree
);
1012 void print(raw_ostream
&OS
) const;
1014 void verify(const DominatorTreeBase
<BlockT
, false> &DomTree
) const;
1016 /// Destroy a loop that has been removed from the `LoopInfo` nest.
1018 /// This runs the destructor of the loop object making it invalid to
1019 /// reference afterward. The memory is retained so that the *pointer* to the
1020 /// loop remains valid.
1022 /// The caller is responsible for removing this loop from the loop nest and
1023 /// otherwise disconnecting it from the broader `LoopInfo` data structures.
1024 /// Callers that don't naturally handle this themselves should probably call
1025 /// `erase' instead.
1026 void destroy(LoopT
*L
) {
1029 // Since LoopAllocator is a BumpPtrAllocator, this Deallocate only poisons
1030 // \c L, but the pointer remains valid for non-dereferencing uses.
1031 LoopAllocator
.Deallocate(L
);
1035 // Implementation in LoopInfoImpl.h
1036 extern template class LoopInfoBase
<BasicBlock
, Loop
>;
1038 class LoopInfo
: public LoopInfoBase
<BasicBlock
, Loop
> {
1039 typedef LoopInfoBase
<BasicBlock
, Loop
> BaseT
;
1041 friend class LoopBase
<BasicBlock
, Loop
>;
1043 void operator=(const LoopInfo
&) = delete;
1044 LoopInfo(const LoopInfo
&) = delete;
1048 explicit LoopInfo(const DominatorTreeBase
<BasicBlock
, false> &DomTree
);
1050 LoopInfo(LoopInfo
&&Arg
) : BaseT(std::move(static_cast<BaseT
&>(Arg
))) {}
1051 LoopInfo
&operator=(LoopInfo
&&RHS
) {
1052 BaseT::operator=(std::move(static_cast<BaseT
&>(RHS
)));
1056 /// Handle invalidation explicitly.
1057 bool invalidate(Function
&F
, const PreservedAnalyses
&PA
,
1058 FunctionAnalysisManager::Invalidator
&);
1060 // Most of the public interface is provided via LoopInfoBase.
1062 /// Update LoopInfo after removing the last backedge from a loop. This updates
1063 /// the loop forest and parent loops for each block so that \c L is no longer
1064 /// referenced, but does not actually delete \c L immediately. The pointer
1065 /// will remain valid until this LoopInfo's memory is released.
1066 void erase(Loop
*L
);
1068 /// Returns true if replacing From with To everywhere is guaranteed to
1069 /// preserve LCSSA form.
1070 bool replacementPreservesLCSSAForm(Instruction
*From
, Value
*To
) {
1071 // Preserving LCSSA form is only problematic if the replacing value is an
1073 Instruction
*I
= dyn_cast
<Instruction
>(To
);
1076 // If both instructions are defined in the same basic block then replacement
1077 // cannot break LCSSA form.
1078 if (I
->getParent() == From
->getParent())
1080 // If the instruction is not defined in a loop then it can safely replace
1082 Loop
*ToLoop
= getLoopFor(I
->getParent());
1085 // If the replacing instruction is defined in the same loop as the original
1086 // instruction, or in a loop that contains it as an inner loop, then using
1087 // it as a replacement will not break LCSSA form.
1088 return ToLoop
->contains(getLoopFor(From
->getParent()));
1091 /// Checks if moving a specific instruction can break LCSSA in any loop.
1093 /// Return true if moving \p Inst to before \p NewLoc will break LCSSA,
1094 /// assuming that the function containing \p Inst and \p NewLoc is currently
1096 bool movementPreservesLCSSAForm(Instruction
*Inst
, Instruction
*NewLoc
) {
1097 assert(Inst
->getFunction() == NewLoc
->getFunction() &&
1098 "Can't reason about IPO!");
1100 auto *OldBB
= Inst
->getParent();
1101 auto *NewBB
= NewLoc
->getParent();
1103 // Movement within the same loop does not break LCSSA (the equality check is
1104 // to avoid doing a hashtable lookup in case of intra-block movement).
1108 auto *OldLoop
= getLoopFor(OldBB
);
1109 auto *NewLoop
= getLoopFor(NewBB
);
1111 if (OldLoop
== NewLoop
)
1114 // Check if Outer contains Inner; with the null loop counting as the
1115 // "outermost" loop.
1116 auto Contains
= [](const Loop
*Outer
, const Loop
*Inner
) {
1117 return !Outer
|| Outer
->contains(Inner
);
1120 // To check that the movement of Inst to before NewLoc does not break LCSSA,
1121 // we need to check two sets of uses for possible LCSSA violations at
1122 // NewLoc: the users of NewInst, and the operands of NewInst.
1124 // If we know we're hoisting Inst out of an inner loop to an outer loop,
1125 // then the uses *of* Inst don't need to be checked.
1127 if (!Contains(NewLoop
, OldLoop
)) {
1128 for (Use
&U
: Inst
->uses()) {
1129 auto *UI
= cast
<Instruction
>(U
.getUser());
1130 auto *UBB
= isa
<PHINode
>(UI
) ? cast
<PHINode
>(UI
)->getIncomingBlock(U
)
1132 if (UBB
!= NewBB
&& getLoopFor(UBB
) != NewLoop
)
1137 // If we know we're sinking Inst from an outer loop into an inner loop, then
1138 // the *operands* of Inst don't need to be checked.
1140 if (!Contains(OldLoop
, NewLoop
)) {
1141 // See below on why we can't handle phi nodes here.
1142 if (isa
<PHINode
>(Inst
))
1145 for (Use
&U
: Inst
->operands()) {
1146 auto *DefI
= dyn_cast
<Instruction
>(U
.get());
1150 // This would need adjustment if we allow Inst to be a phi node -- the
1151 // new use block won't simply be NewBB.
1153 auto *DefBlock
= DefI
->getParent();
1154 if (DefBlock
!= NewBB
&& getLoopFor(DefBlock
) != NewLoop
)
1163 // Allow clients to walk the list of nested loops...
1164 template <> struct GraphTraits
<const Loop
*> {
1165 typedef const Loop
*NodeRef
;
1166 typedef LoopInfo::iterator ChildIteratorType
;
1168 static NodeRef
getEntryNode(const Loop
*L
) { return L
; }
1169 static ChildIteratorType
child_begin(NodeRef N
) { return N
->begin(); }
1170 static ChildIteratorType
child_end(NodeRef N
) { return N
->end(); }
1173 template <> struct GraphTraits
<Loop
*> {
1174 typedef Loop
*NodeRef
;
1175 typedef LoopInfo::iterator ChildIteratorType
;
1177 static NodeRef
getEntryNode(Loop
*L
) { return L
; }
1178 static ChildIteratorType
child_begin(NodeRef N
) { return N
->begin(); }
1179 static ChildIteratorType
child_end(NodeRef N
) { return N
->end(); }
1182 /// Analysis pass that exposes the \c LoopInfo for a function.
1183 class LoopAnalysis
: public AnalysisInfoMixin
<LoopAnalysis
> {
1184 friend AnalysisInfoMixin
<LoopAnalysis
>;
1185 static AnalysisKey Key
;
1188 typedef LoopInfo Result
;
1190 LoopInfo
run(Function
&F
, FunctionAnalysisManager
&AM
);
1193 /// Printer pass for the \c LoopAnalysis results.
1194 class LoopPrinterPass
: public PassInfoMixin
<LoopPrinterPass
> {
1198 explicit LoopPrinterPass(raw_ostream
&OS
) : OS(OS
) {}
1199 PreservedAnalyses
run(Function
&F
, FunctionAnalysisManager
&AM
);
1202 /// Verifier pass for the \c LoopAnalysis results.
1203 struct LoopVerifierPass
: public PassInfoMixin
<LoopVerifierPass
> {
1204 PreservedAnalyses
run(Function
&F
, FunctionAnalysisManager
&AM
);
1207 /// The legacy pass manager's analysis pass to compute loop information.
1208 class LoopInfoWrapperPass
: public FunctionPass
{
1212 static char ID
; // Pass identification, replacement for typeid
1214 LoopInfoWrapperPass() : FunctionPass(ID
) {
1215 initializeLoopInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1218 LoopInfo
&getLoopInfo() { return LI
; }
1219 const LoopInfo
&getLoopInfo() const { return LI
; }
1221 /// Calculate the natural loop information for a given function.
1222 bool runOnFunction(Function
&F
) override
;
1224 void verifyAnalysis() const override
;
1226 void releaseMemory() override
{ LI
.releaseMemory(); }
1228 void print(raw_ostream
&O
, const Module
*M
= nullptr) const override
;
1230 void getAnalysisUsage(AnalysisUsage
&AU
) const override
;
1233 /// Function to print a loop's contents as LLVM's text IR assembly.
1234 void printLoop(Loop
&L
, raw_ostream
&OS
, const std::string
&Banner
= "");
1236 /// Find and return the loop attribute node for the attribute @p Name in
1237 /// @p LoopID. Return nullptr if there is no such attribute.
1238 MDNode
*findOptionMDForLoopID(MDNode
*LoopID
, StringRef Name
);
1240 /// Find string metadata for a loop.
1242 /// Returns the MDNode where the first operand is the metadata's name. The
1243 /// following operands are the metadata's values. If no metadata with @p Name is
1244 /// found, return nullptr.
1245 MDNode
*findOptionMDForLoop(const Loop
*TheLoop
, StringRef Name
);
1247 /// Return whether an MDNode might represent an access group.
1249 /// Access group metadata nodes have to be distinct and empty. Being
1250 /// always-empty ensures that it never needs to be changed (which -- because
1251 /// MDNodes are designed immutable -- would require creating a new MDNode). Note
1252 /// that this is not a sufficient condition: not every distinct and empty NDNode
1253 /// is representing an access group.
1254 bool isValidAsAccessGroup(MDNode
*AccGroup
);
1256 /// Create a new LoopID after the loop has been transformed.
1258 /// This can be used when no follow-up loop attributes are defined
1259 /// (llvm::makeFollowupLoopID returning None) to stop transformations to be
1262 /// @param Context The LLVMContext in which to create the new LoopID.
1263 /// @param OrigLoopID The original LoopID; can be nullptr if the original
1264 /// loop has no LoopID.
1265 /// @param RemovePrefixes Remove all loop attributes that have these prefixes.
1266 /// Use to remove metadata of the transformation that has
1268 /// @param AddAttrs Add these loop attributes to the new LoopID.
1270 /// @return A new LoopID that can be applied using Loop::setLoopID().
1272 makePostTransformationMetadata(llvm::LLVMContext
&Context
, MDNode
*OrigLoopID
,
1273 llvm::ArrayRef
<llvm::StringRef
> RemovePrefixes
,
1274 llvm::ArrayRef
<llvm::MDNode
*> AddAttrs
);
1276 } // End llvm namespace