36 #ifndef LLVM_ANALYSIS_LOOPINFO_H
37 #define LLVM_ANALYSIS_LOOPINFO_H
59 template <
class N,
bool IsPostDom>
68 template<
class BlockT,
class LoopT>
72 std::vector<LoopT *> SubLoops;
75 std::vector<BlockT*> Blocks;
80 bool IsInvalid =
false;
89 for (
size_t i = 0, e = SubLoops.size(); i != e; ++i)
98 for (
const LoopT *CurLoop = ParentLoop; CurLoop;
99 CurLoop = CurLoop->ParentLoop)
111 if (L ==
this)
return true;
112 if (!L)
return false;
113 return contains(L->getParentLoop());
118 return DenseBlockSet.
count(BB);
122 template<
class InstT>
128 const std::vector<LoopT *> &
getSubLoops()
const {
return SubLoops; }
130 typedef typename std::vector<LoopT *>::const_iterator
iterator;
131 typedef typename std::vector<LoopT *>::const_reverse_iterator
137 bool empty()
const {
return SubLoops.empty(); }
140 const std::vector<BlockT*> &
getBlocks()
const {
return Blocks; }
150 return Blocks.size();
162 for (
const auto &Succ : children<const BlockT*>(BB)) {
179 return std::find(PredBegin, PredEnd, BB) != PredEnd;
184 unsigned NumBackEdges = 0;
220 typedef std::pair<const BlockT*, const BlockT*>
Edge;
272 assert(!NewChild->ParentLoop &&
"NewChild already has a parent!");
273 NewChild->ParentLoop =
static_cast<LoopT *
>(
this);
274 SubLoops.push_back(NewChild);
280 assert(I != SubLoops.end() &&
"Cannot remove end iterator!");
282 assert(Child->ParentLoop ==
this &&
"Child is not a child of this loop!");
283 SubLoops.erase(SubLoops.begin()+(I-
begin()));
284 Child->ParentLoop =
nullptr;
292 Blocks.push_back(BB);
303 Blocks.reserve(size);
309 if (Blocks[0] == BB)
return;
310 for (
unsigned i = 0; ; ++i) {
311 assert(i != Blocks.size() &&
"Loop does not contain BB!");
312 if (Blocks[i] == BB) {
313 Blocks[i] = Blocks[0];
324 auto I =
find(Blocks, BB);
325 assert(
I != Blocks.end() &&
"N is not in this list!");
328 DenseBlockSet.
erase(BB);
342 explicit LoopBase(BlockT *BB) : ParentLoop(nullptr) {
343 Blocks.push_back(BB);
348 template<
class BlockT,
class LoopT>
355 extern template class LoopBase<BasicBlock, Loop>;
371 End(std::move(End)) {}
378 explicit operator bool()
const {
390 bool hasLoopInvariantOperands(
const Instruction *
I)
const;
400 bool makeLoopInvariant(
Value *V,
bool &Changed,
422 PHINode *getCanonicalInductionVariable()
const;
432 bool isLoopSimplifyForm()
const;
435 bool isSafeToClone()
const;
449 bool isAnnotatedParallel()
const;
457 MDNode *getLoopID()
const;
465 void setLoopID(
MDNode *LoopID)
const;
469 bool hasDedicatedExits()
const;
482 void dumpVerbose()
const;
492 LocRange getLocRange()
const;
496 if (Header->hasName())
497 return Header->getName();
498 return "<unnamed loop>";
511 template<
class BlockT,
class LoopT>
514 DenseMap<const BlockT *, LoopT *> BBMap;
515 std::vector<LoopT *> TopLevelLoops;
516 std::vector<LoopT *> RemovedLoops;
528 : BBMap(std::move(Arg.BBMap)),
529 TopLevelLoops(std::move(Arg.TopLevelLoops)) {
531 Arg.TopLevelLoops.clear();
534 BBMap = std::move(RHS.BBMap);
536 for (
auto *
L : TopLevelLoops)
538 TopLevelLoops = std::move(RHS.TopLevelLoops);
539 RHS.TopLevelLoops.clear();
546 for (
auto *
L : TopLevelLoops)
548 TopLevelLoops.clear();
549 for (
auto *
L : RemovedLoops)
551 RemovedLoops.clear();
557 typedef typename std::vector<LoopT *>::const_iterator
iterator;
558 typedef typename std::vector<LoopT *>::const_reverse_iterator
560 iterator
begin()
const {
return TopLevelLoops.begin(); }
561 iterator
end()
const {
return TopLevelLoops.end(); }
564 bool empty()
const {
return TopLevelLoops.empty(); }
585 LoopT *
getLoopFor(
const BlockT *BB)
const {
return BBMap.lookup(BB); }
589 return getLoopFor(BB);
595 const LoopT *
L = getLoopFor(BB);
596 return L ? L->getLoopDepth() : 0;
601 const LoopT *
L = getLoopFor(BB);
602 return L && L->getHeader() == BB;
609 assert(I !=
end() &&
"Cannot remove end iterator!");
611 assert(!L->getParentLoop() &&
"Not a top-level loop!");
612 TopLevelLoops.erase(TopLevelLoops.begin() + (I-
begin()));
631 auto I =
find(TopLevelLoops, OldLoop);
632 assert(
I != TopLevelLoops.end() &&
"Old loop not at top level!");
634 assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
635 "Loops already embedded into a subloop!");
640 assert(!New->getParentLoop() &&
"Loop already in subloop!");
641 TopLevelLoops.push_back(New);
648 auto I = BBMap.find(BB);
649 if (
I != BBMap.end()) {
660 const LoopT *ParentLoop) {
661 if (!SubLoop)
return true;
662 if (SubLoop == ParentLoop)
return false;
663 return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
676 extern template class LoopInfoBase<BasicBlock, Loop>;
683 void operator=(
const LoopInfo &) =
delete;
691 BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
705 void markAsRemoved(
Loop *
L);
721 if (!ToLoop)
return true;
735 "Can't reason about IPO!");
745 auto *OldLoop = getLoopFor(OldBB);
746 auto *NewLoop = getLoopFor(NewBB);
748 if (OldLoop == NewLoop)
753 auto Contains = [](
const Loop *Outer,
const Loop *Inner) {
754 return !Outer || Outer->
contains(Inner);
764 if (!Contains(NewLoop, OldLoop)) {
766 auto *UI = cast<Instruction>(U.getUser());
767 auto *UBB = isa<PHINode>(UI) ? cast<PHINode>(UI)->getIncomingBlock(U)
769 if (UBB != NewBB && getLoopFor(UBB) != NewLoop)
777 if (!Contains(OldLoop, NewLoop)) {
779 if (isa<PHINode>(Inst))
791 if (DefBlock != NewBB && getLoopFor(DefBlock) != NewLoop)
871 void printLoop(Loop &
L, raw_ostream &OS,
const std::string &Banner =
"");
unsigned getNumBackEdges() const
Calculate the number of back edges to the loop header.
LoopInfo::iterator ChildIteratorType
void push_back(const T &Elt)
LoopInfoBase(LoopInfoBase &&Arg)
const_iterator end(StringRef path)
Get end iterator over path.
LoopInfo run(Function &F, FunctionAnalysisManager &AM)
unsigned getLoopDepth(const BlockT *BB) const
Return the loop nesting level of the specified block.
iterator_range< typename GraphTraits< GraphType >::ChildIteratorType > children(const typename GraphTraits< GraphType >::NodeRef &G)
iterator_range< use_iterator > uses()
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
static bool isLoopInvariant(Value *V, const Loop *L, const DominatorTree *DT)
Perform a quick domtree based check for loop invariance assuming that V is used within the loop...
void removeBlock(BlockT *BB)
This method completely removes BB from all data structures, including all of the Loop objects it is n...
LoopT * removeLoop(iterator I)
This removes the specified top-level loop from this loop info object.
void getLoopLatches(SmallVectorImpl< BlockT * > &LoopLatches) const
Return all loop latch blocks of this loop.
A Module instance is used to store all the information related to an LLVM module. ...
bool isLoopHeader(const BlockT *BB) const
Implements a dense probed hash-table based set.
const DebugLoc & getEnd() const
const LoopT * operator[](const BlockT *BB) const
Same as getLoopFor.
StringRef getName() const
std::vector< LoopT * >::const_reverse_iterator reverse_iterator
static bool isNotAlreadyContainedIn(const LoopT *SubLoop, const LoopT *ParentLoop)
void invalidate()
Invalidate the loop, indicating that it is no longer a loop.
bool isLoopExiting(const BlockT *BB) const
True if terminator in the block can branch to another block that is outside of the current loop...
void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild)
This is used when splitting loops up.
void changeLoopFor(BlockT *BB, LoopT *L)
Change the top-level loop that contains BB to the specified loop.
LoopT * getParentLoop() const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
BlockT * getExitBlock() const
If getExitBlocks would return exactly one block, return that block.
const std::vector< BlockT * > & getBlocks() const
Get a list of the basic blocks which make up this loop.
Instances of this class are used to represent loops that are detected in the flow graph...
BlockT * getHeader() const
LoopT * removeChildLoop(iterator I)
This removes the specified child from being a subloop of this loop.
std::vector< LoopT * >::const_iterator iterator
iterator/begin/end - The interface to the top-level loops in the current function.
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
void getExitEdges(SmallVectorImpl< Edge > &ExitEdges) const
Return all pairs of (inside_block,outside_block).
void initializeLoopInfoWrapperPassPass(PassRegistry &)
A Use represents the edge between a Value definition and its users.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void getExitingBlocks(SmallVectorImpl< BlockT * > &ExitingBlocks) const
Return all blocks inside the loop that have successors outside of the loop.
Printer pass for the LoopAnalysis results.
Analysis pass that exposes the LoopInfo for a function.
void print(raw_ostream &O, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
void getExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
Return all of the successor blocks of this loop.
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
reverse_iterator rbegin() const
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
This method is used by other analyses to update loop information.
bool contains(const InstT *Inst) const
Return true if the specified instruction is in this loop.
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
A CRTP mix-in to automatically provide informational APIs needed for passes.
LoopInfo & operator=(LoopInfo &&RHS)
const Function * getFunction() const
Return the function this instruction belongs to.
LocRange(DebugLoc Start, DebugLoc End)
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
iterator_range< block_iterator > blocks() const
void verifyLoop() const
Verify loop structure.
std::vector< LoopT * >::const_reverse_iterator reverse_iterator
Core dominator tree base class.
bool verify(const TargetRegisterInfo &TRI) const
Check that information hold by this instance make sense for the given TRI.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
A set of analyses that are preserved following a run of a transformation pass.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
void setParentLoop(LoopT *L)
This is a raw interface for bypassing addChildLoop.
LLVM Basic Block Representation.
static ChildIteratorType child_begin(NodeRef N)
const DebugLoc & getStart() const
LoopPrinterPass(raw_ostream &OS)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
A CRTP mix-in that provides informational APIs needed for analysis passes.
Represent the analysis usage information of a pass.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
static const unsigned End
FunctionPass class - This class is used to implement most global optimizations.
BlockT * getExitingBlock() const
If getExitingBlocks would return exactly one block, return that block.
void addBlockEntry(BlockT *BB)
This adds a basic block directly to the basic block list.
A range representing the start and end location of a loop.
const LoopInfo & getLoopInfo() const
reverse_iterator rend() const
void removeBlockFromLoop(BlockT *BB)
This removes the specified basic block from the current loop, updating the Blocks as appropriate...
std::pair< const BlockT *, const BlockT * > Edge
Edge type.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
auto find(R &&Range, const T &Val) -> decltype(std::begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false...
bool movementPreservesLCSSAForm(Instruction *Inst, Instruction *NewLoc)
Checks if moving a specific instruction can break LCSSA in any loop.
reverse_iterator rend() const
Verifier pass for the LoopAnalysis results.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
bool contains(const BlockT *BB) const
Return true if the specified basic block is in this loop.
static NodeRef getEntryNode(const Loop *L)
static ChildIteratorType child_end(NodeRef N)
LoopInfoBase & operator=(LoopInfoBase &&RHS)
void addTopLevelLoop(LoopT *New)
This adds the specified loop to the collection of top-level loops.
A range adaptor for a pair of iterators.
void reserveBlocks(unsigned size)
interface to do reserve() for Blocks
LoopInfo::iterator ChildIteratorType
std::vector< BlockT * >::const_iterator block_iterator
void verifyLoopNest(DenseSet< const LoopT * > *Loops) const
Verify loop structure of this loop and all nested loops.
block_iterator block_end() const
static NodeRef getEntryNode(Loop *L)
unsigned getNumBlocks() const
Get the number of blocks in this loop in constant time.
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...
Represents a single loop in the control flow graph.
static ChildIteratorType child_end(NodeRef N)
BlockT * getLoopPredecessor() const
If the given loop's header has exactly one unique predecessor outside the loop, return it...
bool runOnFunction(Function &F) override
Calculate the natural loop information for a given function.
bool isLoopLatch(const BlockT *BB) const
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
std::vector< LoopT * > & getSubLoopsVector()
API to communicate dependencies between analyses during invalidation.
bool isInvalid()
Return true if this loop is no longer valid.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
LoopBase()
This creates an empty loop.
void changeTopLevelLoop(LoopT *OldLoop, LoopT *NewLoop)
Replace the specified loop in the top-level loops list with the indicated loop.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
This class implements an extremely fast bulk output stream that can only output to a stream...
const std::vector< LoopT * > & getSubLoops() const
Return the loops contained entirely within this loop.
block_iterator block_begin() const
The legacy pass manager's analysis pass to compute loop information.
StringRef - Represent a constant reference to a string, i.e.
A container for analyses that lazily runs them and caches their results.
void print(raw_ostream &OS, unsigned Depth=0, bool Verbose=false) const
Print loop with all the BBs inside it.
bool replacementPreservesLCSSAForm(Instruction *From, Value *To)
Returns true if replacing From with To everywhere is guaranteed to preserve LCSSA form...
std::vector< LoopT * >::const_iterator iterator
reverse_iterator rbegin() const
This header defines various interfaces for pass management in LLVM.
void printLoop(Loop &L, raw_ostream &OS, const std::string &Banner="")
Function to print a loop's contents as LLVM's text IR assembly.
unsigned getLoopDepth() const
Return the nesting level of this loop.
void reverseBlock(unsigned from)
interface to reverse Blocks[from, end of loop] in this loop
A special type used by analysis passes to provide an address that identifies that particular analysis...
static ChildIteratorType child_begin(NodeRef N)
const BasicBlock * getParent() const
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
This class builds and contains all of the top-level loop structures in the specified function...