40#ifndef LLVM_SUPPORT_GENERICLOOPINFO_H
41#define LLVM_SUPPORT_GENERICLOOPINFO_H
52template <
class N,
class M>
class LoopInfoBase;
53template <
class N,
class M>
class LoopBase;
59template <
class BlockT,
class LoopT>
class LoopBase {
62 std::vector<LoopT *> SubLoops;
65 std::vector<BlockT *> Blocks;
69#if LLVM_ENABLE_ABI_BREAKING_CHECKS
71 bool IsInvalid =
false;
85 for (
const LoopT *CurLoop = ParentLoop; CurLoop;
86 CurLoop = CurLoop->ParentLoop)
104 const LoopT *L =
static_cast<const LoopT *
>(
this);
105 while (L->ParentLoop)
111 LoopT *L =
static_cast<LoopT *
>(
this);
112 while (L->ParentLoop)
130 return contains(L->getParentLoop());
136 return DenseBlockSet.
count(BB);
140 template <
class InstT>
bool contains(
const InstT *Inst)
const {
153 typedef typename std::vector<LoopT *>::const_iterator
iterator;
202 return DenseBlockSet;
208 return DenseBlockSet;
218#if LLVM_ENABLE_ABI_BREAKING_CHECKS
230 for (
const auto *Succ : children<const BlockT *>(BB)) {
251 [&](BlockT *Pred) {
return contains(Pred); });
301 typedef std::pair<BlockT *, BlockT *>
Edge;
329 for (
const auto Pred : inverse_children<BlockT *>(
H))
336 template <
class Type>
340 PreOrderWorklist.
append(L.rbegin(), L.rend());
342 while (!PreOrderWorklist.
empty()) {
346 PreOrderWorklist.
append(L->rbegin(), L->rend());
355 const LoopT *CurLoop =
static_cast<const LoopT *
>(
this);
358 return PreOrderLoops;
362 LoopT *CurLoop =
static_cast<LoopT *
>(
this);
365 return PreOrderLoops;
389 assert(!NewChild->ParentLoop &&
"NewChild already has a parent!");
390 NewChild->ParentLoop =
static_cast<LoopT *
>(
this);
391 SubLoops.push_back(NewChild);
398 assert(
I != SubLoops.end() &&
"Cannot remove end iterator!");
400 assert(Child->ParentLoop ==
this &&
"Child is not a child of this loop!");
401 SubLoops.erase(SubLoops.begin() + (
I -
begin()));
402 Child->ParentLoop =
nullptr;
439 for (
unsigned i = 0;; ++i) {
440 assert(i !=
Blocks.size() &&
"Loop does not contain BB!");
458 DenseBlockSet.
erase(BB);
475 unsigned Depth = 0)
const;
483 explicit LoopBase(BlockT *BB) : ParentLoop(nullptr) {
498 for (
auto *SubLoop : SubLoops)
501#if LLVM_ENABLE_ABI_BREAKING_CHECKS
506 DenseBlockSet.
clear();
507 ParentLoop =
nullptr;
511template <
class BlockT,
class LoopT>
525 std::vector<LoopT *> TopLevelLoops;
528 friend class LoopBase<BlockT, LoopT>;
540 TopLevelLoops(
std::
move(Arg.TopLevelLoops)),
541 LoopAllocator(
std::
move(Arg.LoopAllocator)) {
543 Arg.TopLevelLoops.clear();
546 BBMap = std::move(
RHS.BBMap);
548 for (
auto *L : TopLevelLoops)
551 TopLevelLoops = std::move(
RHS.TopLevelLoops);
552 LoopAllocator = std::move(
RHS.LoopAllocator);
553 RHS.TopLevelLoops.clear();
560 for (
auto *L : TopLevelLoops)
562 TopLevelLoops.clear();
563 LoopAllocator.
Reset();
567 LoopT *Storage = LoopAllocator.
Allocate<LoopT>();
568 return new (Storage) LoopT(std::forward<ArgsTy>(Args)...);
574 typedef typename std::vector<LoopT *>::const_iterator
iterator;
581 bool empty()
const {
return TopLevelLoops.empty(); }
611 return L ? L->getLoopDepth() : 0;
617 return L && L->getHeader() == BB;
630 assert(
I !=
end() &&
"Cannot remove end iterator!");
632 assert(L->isOutermost() &&
"Not a top-level loop!");
633 TopLevelLoops.erase(TopLevelLoops.begin() + (
I -
begin()));
651 auto I =
find(TopLevelLoops, OldLoop);
652 assert(
I != TopLevelLoops.end() &&
"Old loop not at top level!");
654 assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
655 "Loops already embedded into a subloop!");
660 assert(New->isOutermost() &&
"Loop already in subloop!");
661 TopLevelLoops.push_back(New);
668 auto I = BBMap.
find(BB);
669 if (
I != BBMap.
end()) {
670 for (LoopT *L =
I->second; L; L = L->getParentLoop())
671 L->removeBlockFromLoop(BB);
680 const LoopT *ParentLoop) {
683 if (SubLoop == ParentLoop)
This file defines the BumpPtrAllocator interface.
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
This file defines the DenseSet and SmallDenseSet classes.
DenseMap< Block *, BlockRelaxAux > Blocks
This file defines a set of templates that efficiently compute a dominator tree over a generic graph.
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines generic set operations that may be used on set's of different types,...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Allocate memory in an ever growing pool, as if by bump-pointer.
LLVM_ATTRIBUTE_RETURNS_NONNULL void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it,...
void Deallocate(const void *Ptr, size_t Size, size_t)
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
iterator find(const_arg_type_t< KeyT > Val)
bool erase(const KeyT &Val)
Implements a dense probed hash-table based set.
Core dominator tree base class.
Instances of this class are used to represent loops that are detected in the flow graph.
bool isAnnotatedParallel() const
Returns true if the loop is annotated parallel.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
SmallPtrSetImpl< const BlockT * > & getBlocksSet()
Return a direct, mutable handle to the blocks set so that we can mutate it efficiently.
static void getInnerLoopsInPreorder(const LoopT &L, SmallVectorImpl< Type > &PreOrderLoops)
Return all inner loops in the loop nest rooted by the loop in preorder, with siblings in forward prog...
bool isOutermost() const
Return true if the loop does not have a parent (natural) loop.
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
bool isInnermost() const
Return true if the loop does not contain any (natural) loops.
void removeBlockFromLoop(BlockT *BB)
This removes the specified basic block from the current loop, updating the Blocks as appropriate.
void getExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
Return all of the successor blocks of this loop.
bool contains(const InstT *Inst) const
Return true if the specified instruction is in this loop.
unsigned getNumBlocks() const
Get the number of blocks in this loop in constant time.
void verifyLoop() const
Verify loop structure.
void verifyLoopNest(DenseSet< const LoopT * > *Loops) const
Verify loop structure of this loop and all nested loops.
SmallVector< LoopT *, 4 > getLoopsInPreorder()
unsigned getNumBackEdges() const
Calculate the number of back edges to the loop header.
void reverseBlock(unsigned from)
interface to reverse Blocks[from, end of loop] in this loop
SmallVector< const LoopT *, 4 > getLoopsInPreorder() const
Return all loops in the loop nest rooted by the loop in preorder, with siblings in forward program or...
void getExitingBlocks(SmallVectorImpl< BlockT * > &ExitingBlocks) const
Return all blocks inside the loop that have successors outside of the loop.
const std::vector< LoopT * > & getSubLoops() const
Return the loops contained entirely within this loop.
BlockT * getHeader() const
const LoopT * getOutermostLoop() const
Get the outermost loop in which this loop is contained.
void getLoopLatches(SmallVectorImpl< BlockT * > &LoopLatches) const
Return all loop latch blocks of this loop.
unsigned getLoopDepth() const
Return the nesting level of this loop.
LoopBase()
This creates an empty loop.
void print(raw_ostream &OS, bool Verbose=false, bool PrintNested=true, unsigned Depth=0) const
Print loop with all the BBs inside it.
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
This method is used by other analyses to update loop information.
std::vector< BlockT * > & getBlocksVector()
Return a direct, mutable handle to the blocks vector so that we can mutate it efficiently with techni...
std::vector< LoopT * >::const_reverse_iterator reverse_iterator
LoopT * removeChildLoop(LoopT *Child)
This removes the specified child from being a subloop of this loop.
std::vector< LoopT * >::const_iterator iterator
void reserveBlocks(unsigned size)
interface to do reserve() for Blocks
iterator_range< block_iterator > blocks() const
block_iterator block_end() const
std::pair< BlockT *, BlockT * > Edge
Edge type.
bool isInvalid() const
Return true if this loop is no longer valid.
BlockT * getLoopPredecessor() const
If the given loop's header has exactly one unique predecessor outside the loop, return it.
bool contains(const BlockT *BB) const
Return true if the specified basic block is in this loop.
bool isLoopLatch(const BlockT *BB) const
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
void getExitEdges(SmallVectorImpl< Edge > &ExitEdges) const
Return all pairs of (inside_block,outside_block).
void addBlockEntry(BlockT *BB)
This adds a basic block directly to the basic block list.
ArrayRef< BlockT * >::const_iterator block_iterator
std::vector< LoopT * > & getSubLoopsVector()
reverse_iterator rbegin() const
BlockT * getExitBlock() const
If getExitBlocks would return exactly one block, return that block.
bool hasNoExitBlocks() const
Return true if this loop does not have any exit blocks.
void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild)
This is used when splitting loops up.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
ArrayRef< BlockT * > getBlocks() const
Get a list of the basic blocks which make up this loop.
reverse_iterator rend() const
BlockT * getExitingBlock() const
If getExitingBlocks would return exactly one block, return that block.
LoopT * getOutermostLoop()
void getUniqueExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
Return all unique successor blocks of this loop.
void setParentLoop(LoopT *L)
This is a raw interface for bypassing addChildLoop.
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
bool hasDedicatedExits() const
Return true if no exit block for the loop has a predecessor that is outside the loop.
void getUniqueNonLatchExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
Return all unique successor blocks of this loop except successors from Latch block are not considered...
const SmallPtrSetImpl< const BlockT * > & getBlocksSet() const
Return a direct, immutable handle to the blocks set.
bool isLoopExiting(const BlockT *BB) const
True if terminator in the block can branch to another block that is outside of the current loop.
block_iterator block_begin() const
void moveToHeader(BlockT *BB)
This method is used to move BB (which must be part of this loop) to be the loop header of the loop (t...
BlockT * getUniqueExitBlock() const
If getUniqueExitBlocks would return exactly one block, return that block.
LoopT * removeChildLoop(iterator I)
This removes the specified child from being a subloop of this loop.
This class builds and contains all of the top-level loop structures in the specified function.
const std::vector< LoopT * > & getTopLevelLoops() const
Return the top-level loops.
void addTopLevelLoop(LoopT *New)
This adds the specified loop to the collection of top-level loops.
void analyze(const DominatorTreeBase< BlockT, false > &DomTree)
Create the loop forest using a stable algorithm.
SmallVector< LoopT *, 4 > getLoopsInReverseSiblingPreorder() const
Return all of the loops in the function in preorder across the loop nests, with siblings in reverse p...
void print(raw_ostream &OS) const
reverse_iterator rend() const
void changeTopLevelLoop(LoopT *OldLoop, LoopT *NewLoop)
Replace the specified loop in the top-level loops list with the indicated loop.
void removeBlock(BlockT *BB)
This method completely removes BB from all data structures, including all of the Loop objects it is n...
LoopInfoBase(LoopInfoBase &&Arg)
LoopT * AllocateLoop(ArgsTy &&...Args)
const LoopT * operator[](const BlockT *BB) const
Same as getLoopFor.
bool isLoopHeader(const BlockT *BB) const
LoopT * removeLoop(iterator I)
This removes the specified top-level loop from this loop info object.
SmallVector< LoopT *, 4 > getLoopsInPreorder() const
Return all of the loops in the function in preorder across the loop nests, with siblings in forward p...
void changeLoopFor(BlockT *BB, LoopT *L)
Change the top-level loop that contains BB to the specified loop.
unsigned getLoopDepth(const BlockT *BB) const
Return the loop nesting level of the specified block.
std::vector< LoopT * > & getTopLevelLoopsVector()
Return the top-level loops.
static bool isNotAlreadyContainedIn(const LoopT *SubLoop, const LoopT *ParentLoop)
std::vector< LoopT * >::const_reverse_iterator reverse_iterator
reverse_iterator rbegin() const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
LoopInfoBase & operator=(LoopInfoBase &&RHS)
void destroy(LoopT *L)
Destroy a loop that has been removed from the LoopInfo nest.
std::vector< LoopT * >::const_iterator iterator
iterator/begin/end - The interface to the top-level loops in the current function.
Represents a single loop in the control flow graph.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
This is an optimization pass for GlobalISel generic memory operations.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Implement std::hash so that hash_code can be used in STL containers.