36 #ifndef LLVM_ANALYSIS_LOOPINFO_H
37 #define LLVM_ANALYSIS_LOOPINFO_H
67 template<
class BlockT,
class LoopT>
71 std::vector<LoopT *> SubLoops;
74 std::vector<BlockT*> Blocks;
79 bool IsInvalid =
false;
88 for (
size_t i = 0, e = SubLoops.size();
i != e; ++
i)
97 for (
const LoopT *CurLoop = ParentLoop; CurLoop;
98 CurLoop = CurLoop->ParentLoop)
110 if (L ==
this)
return true;
111 if (!L)
return false;
112 return contains(L->getParentLoop());
117 return DenseBlockSet.
count(BB);
121 template<
class InstT>
127 const std::vector<LoopT *> &
getSubLoops()
const {
return SubLoops; }
129 typedef typename std::vector<LoopT *>::const_iterator
iterator;
130 typedef typename std::vector<LoopT *>::const_reverse_iterator
136 bool empty()
const {
return SubLoops.empty(); }
139 const std::vector<BlockT*> &
getBlocks()
const {
return Blocks; }
149 return Blocks.size();
162 for (
typename BlockTraits::ChildIteratorType
SI =
163 BlockTraits::child_begin(BB),
164 SE = BlockTraits::child_end(BB);
SI != SE; ++
SI) {
181 return std::find(PredBegin, PredEnd, BB) != PredEnd;
186 unsigned NumBackEdges = 0;
190 for (
typename InvBlockTraits::ChildIteratorType
I =
191 InvBlockTraits::child_begin(H),
192 E = InvBlockTraits::child_end(H);
I !=
E; ++
I)
225 typedef std::pair<const BlockT*, const BlockT*>
Edge;
253 for (
typename InvBlockTraits::ChildIteratorType
I =
254 InvBlockTraits::child_begin(H),
255 E = InvBlockTraits::child_end(H);
I !=
E; ++
I)
280 assert(!NewChild->ParentLoop &&
"NewChild already has a parent!");
281 NewChild->ParentLoop =
static_cast<LoopT *
>(
this);
282 SubLoops.push_back(NewChild);
288 assert(I != SubLoops.end() &&
"Cannot remove end iterator!");
290 assert(Child->ParentLoop ==
this &&
"Child is not a child of this loop!");
291 SubLoops.erase(SubLoops.begin()+(I-
begin()));
292 Child->ParentLoop =
nullptr;
300 Blocks.push_back(BB);
311 Blocks.reserve(size);
317 if (Blocks[0] == BB)
return;
318 for (
unsigned i = 0; ; ++
i) {
319 assert(
i != Blocks.size() &&
"Loop does not contain BB!");
320 if (Blocks[
i] == BB) {
321 Blocks[
i] = Blocks[0];
332 auto I =
find(Blocks, BB);
333 assert(
I != Blocks.end() &&
"N is not in this list!");
336 DenseBlockSet.
erase(BB);
350 explicit LoopBase(BlockT *BB) : ParentLoop(nullptr) {
351 Blocks.push_back(BB);
356 template<
class BlockT,
class LoopT>
363 extern template class LoopBase<BasicBlock, Loop>;
379 End(std::move(End)) {}
386 explicit operator bool()
const {
398 bool hasLoopInvariantOperands(
const Instruction *
I)
const;
408 bool makeLoopInvariant(
Value *V,
bool &Changed,
430 PHINode *getCanonicalInductionVariable()
const;
440 bool isLoopSimplifyForm()
const;
443 bool isSafeToClone()
const;
457 bool isAnnotatedParallel()
const;
465 MDNode *getLoopID()
const;
473 void setLoopID(
MDNode *LoopID)
const;
477 bool hasDedicatedExits()
const;
489 void dumpVerbose()
const;
499 LocRange getLocRange()
const;
503 if (Header->hasName())
504 return Header->getName();
505 return "<unnamed loop>";
518 template<
class BlockT,
class LoopT>
521 DenseMap<const BlockT *, LoopT *> BBMap;
522 std::vector<LoopT *> TopLevelLoops;
523 std::vector<LoopT *> RemovedLoops;
535 : BBMap(std::move(Arg.BBMap)),
536 TopLevelLoops(std::move(Arg.TopLevelLoops)) {
538 Arg.TopLevelLoops.clear();
541 BBMap = std::move(RHS.BBMap);
543 for (
auto *
L : TopLevelLoops)
545 TopLevelLoops = std::move(RHS.TopLevelLoops);
546 RHS.TopLevelLoops.clear();
553 for (
auto *
L : TopLevelLoops)
555 TopLevelLoops.clear();
556 for (
auto *
L : RemovedLoops)
558 RemovedLoops.clear();
564 typedef typename std::vector<LoopT *>::const_iterator
iterator;
565 typedef typename std::vector<LoopT *>::const_reverse_iterator
567 iterator
begin()
const {
return TopLevelLoops.begin(); }
568 iterator
end()
const {
return TopLevelLoops.end(); }
571 bool empty()
const {
return TopLevelLoops.empty(); }
575 LoopT *
getLoopFor(
const BlockT *BB)
const {
return BBMap.lookup(BB); }
579 return getLoopFor(BB);
585 const LoopT *
L = getLoopFor(BB);
586 return L ? L->getLoopDepth() : 0;
591 const LoopT *
L = getLoopFor(BB);
592 return L && L->getHeader() == BB;
599 assert(I !=
end() &&
"Cannot remove end iterator!");
601 assert(!L->getParentLoop() &&
"Not a top-level loop!");
602 TopLevelLoops.erase(TopLevelLoops.begin() + (I-
begin()));
621 auto I =
find(TopLevelLoops, OldLoop);
622 assert(
I != TopLevelLoops.end() &&
"Old loop not at top level!");
624 assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
625 "Loops already embedded into a subloop!");
630 assert(!New->getParentLoop() &&
"Loop already in subloop!");
631 TopLevelLoops.push_back(New);
638 auto I = BBMap.find(BB);
639 if (
I != BBMap.end()) {
650 const LoopT *ParentLoop) {
651 if (!SubLoop)
return true;
652 if (SubLoop == ParentLoop)
return false;
653 return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
666 extern template class LoopInfoBase<BasicBlock, Loop>;
673 void operator=(
const LoopInfo &) =
delete;
681 BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
691 void markAsRemoved(
Loop *
L);
707 if (!ToLoop)
return true;
721 "Can't reason about IPO!");
731 auto *OldLoop = getLoopFor(OldBB);
732 auto *NewLoop = getLoopFor(NewBB);
734 if (OldLoop == NewLoop)
739 auto Contains = [](
const Loop *Outer,
const Loop *Inner) {
740 return !Outer || Outer->
contains(Inner);
750 if (!Contains(NewLoop, OldLoop)) {
752 auto *UI = cast<Instruction>(U.getUser());
753 auto *UBB = isa<PHINode>(UI) ? cast<PHINode>(UI)->getIncomingBlock(U)
755 if (UBB != NewBB && getLoopFor(UBB) != NewLoop)
763 if (!Contains(OldLoop, NewLoop)) {
765 if (isa<PHINode>(Inst))
777 if (DefBlock != NewBB && getLoopFor(DefBlock) != NewLoop)
857 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< use_iterator > uses()
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
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.
size_type count(PtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
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.
const_iterator begin(StringRef path)
Get begin iterator over path.
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...
static GCRegistry::Add< StatepointGC > D("statepoint-example","an example strategy for statepoint")
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...
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
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.
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()
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...