30 #ifndef LLVM_ANALYSIS_LOOPINFO_H
31 #define LLVM_ANALYSIS_LOOPINFO_H
47 template <
typename IRUnitT>
class AnalysisManager;
48 class PreservedAnalyses;
64 template<
class BlockT,
class LoopT>
68 std::vector<LoopT *> SubLoops;
71 std::vector<BlockT*> Blocks;
82 for (
size_t i = 0, e = SubLoops.size(); i != e; ++i)
91 for (
const LoopT *CurLoop = ParentLoop; CurLoop;
92 CurLoop = CurLoop->ParentLoop)
96 BlockT *
getHeader()
const {
return Blocks.front(); }
106 if (L ==
this)
return true;
107 if (!L)
return false;
108 return contains(L->getParentLoop());
114 return DenseBlockSet.
count(BB);
119 template<
class InstT>
126 const std::vector<LoopT *> &
getSubLoops()
const {
return SubLoops; }
128 typedef typename std::vector<LoopT *>::const_iterator
iterator;
129 typedef typename std::vector<LoopT *>::const_reverse_iterator
135 bool empty()
const {
return SubLoops.empty(); }
139 const std::vector<BlockT*> &
getBlocks()
const {
return Blocks; }
146 return Blocks.size();
154 for (
typename BlockTraits::ChildIteratorType
SI =
155 BlockTraits::child_begin(BB),
156 SE = BlockTraits::child_end(BB);
SI != SE; ++
SI) {
166 unsigned NumBackEdges = 0;
170 for (
typename InvBlockTraits::ChildIteratorType
I =
171 InvBlockTraits::child_begin(H),
172 E = InvBlockTraits::child_end(H);
I != E; ++
I)
207 typedef std::pair<const BlockT*, const BlockT*>
Edge;
237 for (
typename InvBlockTraits::ChildIteratorType
I =
238 InvBlockTraits::child_begin(H),
239 E = InvBlockTraits::child_end(H);
I != E; ++
I)
266 assert(!NewChild->ParentLoop &&
"NewChild already has a parent!");
267 NewChild->ParentLoop =
static_cast<LoopT *
>(
this);
268 SubLoops.push_back(NewChild);
275 assert(I != SubLoops.end() &&
"Cannot remove end iterator!");
277 assert(Child->ParentLoop ==
this &&
"Child is not a child of this loop!");
278 SubLoops.erase(SubLoops.begin()+(I-
begin()));
279 Child->ParentLoop =
nullptr;
287 Blocks.push_back(BB);
293 std::reverse(Blocks.begin() + from, Blocks.end());
298 Blocks.reserve(size);
305 if (Blocks[0] == BB)
return;
306 for (
unsigned i = 0; ; ++i) {
307 assert(i != Blocks.size() &&
"Loop does not contain BB!");
308 if (Blocks[i] == BB) {
309 Blocks[i] = Blocks[0];
320 auto I = std::find(Blocks.begin(), Blocks.end(), BB);
321 assert(
I != Blocks.end() &&
"N is not in this list!");
324 DenseBlockSet.
erase(BB);
337 explicit LoopBase(BlockT *BB) : ParentLoop(nullptr) {
338 Blocks.push_back(BB);
343 template<
class BlockT,
class LoopT>
350 extern template class LoopBase<BasicBlock, Loop>;
362 bool hasLoopInvariantOperands(
const Instruction *
I)
const;
373 bool makeLoopInvariant(
Value *V,
bool &Changed,
396 PHINode *getCanonicalInductionVariable()
const;
404 bool isLoopSimplifyForm()
const;
407 bool isSafeToClone()
const;
421 bool isAnnotatedParallel()
const;
429 MDNode *getLoopID()
const;
437 void setLoopID(
MDNode *LoopID)
const;
441 bool hasDedicatedExits()
const;
464 if ((HeadBB = getLoopPreheader()) !=
nullptr)
470 HeadBB = getHeader();
487 template<
class BlockT,
class LoopT>
490 DenseMap<const BlockT *, LoopT *> BBMap;
491 std::vector<LoopT *> TopLevelLoops;
502 : BBMap(std::move(Arg.BBMap)),
503 TopLevelLoops(std::move(Arg.TopLevelLoops)) {
505 Arg.TopLevelLoops.clear();
508 BBMap = std::move(RHS.BBMap);
510 for (
auto *L : TopLevelLoops)
512 TopLevelLoops = std::move(RHS.TopLevelLoops);
513 RHS.TopLevelLoops.clear();
520 for (
auto *L : TopLevelLoops)
522 TopLevelLoops.clear();
528 typedef typename std::vector<LoopT *>::const_iterator
iterator;
529 typedef typename std::vector<LoopT *>::const_reverse_iterator
531 iterator
begin()
const {
return TopLevelLoops.begin(); }
532 iterator
end()
const {
return TopLevelLoops.end(); }
535 bool empty()
const {
return TopLevelLoops.empty(); }
540 LoopT *
getLoopFor(
const BlockT *BB)
const {
return BBMap.lookup(BB); }
545 return getLoopFor(BB);
552 const LoopT *L = getLoopFor(BB);
553 return L ? L->getLoopDepth() : 0;
558 const LoopT *L = getLoopFor(BB);
559 return L && L->getHeader() == BB;
566 assert(I !=
end() &&
"Cannot remove end iterator!");
568 assert(!L->getParentLoop() &&
"Not a top-level loop!");
569 TopLevelLoops.erase(TopLevelLoops.begin() + (I-
begin()));
588 auto I = std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
589 assert(
I != TopLevelLoops.end() &&
"Old loop not at top level!");
591 assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
592 "Loops already embedded into a subloop!");
598 assert(!New->getParentLoop() &&
"Loop already in subloop!");
599 TopLevelLoops.push_back(New);
606 auto I = BBMap.find(BB);
607 if (
I != BBMap.end()) {
608 for (LoopT *L =
I->second; L; L = L->getParentLoop())
609 L->removeBlockFromLoop(BB);
618 const LoopT *ParentLoop) {
619 if (!SubLoop)
return true;
620 if (SubLoop == ParentLoop)
return false;
621 return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
634 extern template class LoopInfoBase<BasicBlock, Loop>;
641 void operator=(
const LoopInfo &) =
delete;
648 BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
658 void updateUnloop(
Loop *Unloop);
674 if (!ToLoop)
return true;
717 static void *
ID() {
return (
void *)&PassID; }
unsigned getNumBackEdges() const
getNumBackEdges - 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.
unsigned getLoopDepth(const BlockT *BB) const
getLoopDepth - Return the loop nesting level of the specified block.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
static bool isLoopInvariant(Value *V, const Loop *L, const DominatorTree *DT)
isLoopInvariant - Perform a quick domtree based check for loop invariance assuming that V is used wit...
void removeBlock(BlockT *BB)
removeBlock - This method completely removes BB from all data structures, including all of the Loop o...
LoopT * removeLoop(iterator I)
removeLoop - This removes the specified top-level loop from this loop info object.
void getLoopLatches(SmallVectorImpl< BlockT * > &LoopLatches) const
getLoopLatches - Return all loop latch blocks of this loop.
A Module instance is used to store all the information related to an LLVM module. ...
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
bool isLoopHeader(const BlockT *BB) const
DenseSet - This implements a dense probed hash-table based set.
const LoopT * operator[](const BlockT *BB) const
operator[] - same as getLoopFor...
PreservedAnalyses run(Function &F, AnalysisManager< Function > *AM)
static void * ID()
Opaque, unique identifier for this analysis pass.
size_type count(PtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
LoopInfo run(Function &F, AnalysisManager< Function > *AM)
std::vector< LoopT * >::const_reverse_iterator reverse_iterator
static bool isNotAlreadyContainedIn(const LoopT *SubLoop, const LoopT *ParentLoop)
static StringRef name()
Provide a name for the analysis for debugging and logging.
bool isLoopExiting(const BlockT *BB) const
isLoopExiting - True if terminator in the block can branch to another block that is outside of the cu...
void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild)
replaceChildLoopWith - This is used when splitting loops up.
const_iterator begin(StringRef path)
Get begin iterator over path.
void changeLoopFor(BlockT *BB, LoopT *L)
changeLoopFor - Change the top-level loop that contains BB to the specified loop. ...
LoopT * getParentLoop() const
LoopT * getLoopFor(const BlockT *BB) const
getLoopFor - Return the inner most loop that BB lives in.
BlockT * getExitBlock() const
getExitBlock - If getExitBlocks would return exactly one block, return that block.
const std::vector< BlockT * > & getBlocks() const
getBlocks - Get a list of the basic blocks which make up this loop.
LoopBase class - Instances of this class are used to represent loops that are detected in the flow gr...
BlockT * getHeader() const
LoopT * removeChildLoop(iterator I)
removeChildLoop - 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
getLoopLatch - If there is a single latch block for this loop, return it.
void getExitEdges(SmallVectorImpl< Edge > &ExitEdges) const
getExitEdges - Return all pairs of (inside_block,outside_block).
void print(raw_ostream &OS, unsigned Depth=0) const
void initializeLoopInfoWrapperPassPass(PassRegistry &)
static ChildIteratorType child_begin(NodeType *N)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void getExitingBlocks(SmallVectorImpl< BlockT * > &ExitingBlocks) const
getExitingBlocks - 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
getExitBlocks - Return all of the successor blocks of this loop.
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)
addBasicBlockToLoop - This method is used by other analyses to update loop information.
bool contains(const InstT *Inst) const
contains - Return true if the specified instruction is in this loop.
void addChildLoop(LoopT *NewChild)
addChildLoop - Add the specified loop to be a child of this loop.
LoopInfo & operator=(LoopInfo &&RHS)
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...
void verifyLoop() const
verifyLoop - Verify loop structure
std::vector< LoopT * >::const_reverse_iterator reverse_iterator
Core dominator tree base class.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
An abstract set of preserved analyses following a transformation pass run.
BlockT * getLoopPreheader() const
getLoopPreheader - If there is a preheader for this loop, return it.
void setParentLoop(LoopT *L)
setParentLoop is a raw interface for bypassing addChildLoop.
LLVM Basic Block Representation.
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.
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
Represent the analysis usage information of a pass.
static ChildIteratorType child_end(NodeType *N)
bool contains(const LoopT *L) const
contains - Return true if the specified loop is contained within in this loop.
FunctionPass class - This class is used to implement most global optimizations.
BlockT * getExitingBlock() const
getExitingBlock - If getExitingBlocks would return exactly one block, return that block...
void addBlockEntry(BlockT *BB)
addBlockEntry - This adds a basic block directly to the basic block list.
const LoopInfo & getLoopInfo() const
DebugLoc getStartLoc() const
Return the debug location of the start of this loop.
reverse_iterator rend() const
void removeBlockFromLoop(BlockT *BB)
removeBlockFromLoop - This removes the specified basic block from the current loop, updating the Blocks as appropriate.
std::pair< const BlockT *, const BlockT * > Edge
Edge type.
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false...
reverse_iterator rend() const
bool contains(const BlockT *BB) const
contains - Return true if the specified basic block is in this loop.
static NodeType * getEntryNode(const Loop *L)
LoopInfoBase & operator=(LoopInfoBase &&RHS)
void addTopLevelLoop(LoopT *New)
addTopLevelLoop - This adds the specified loop to the collection of top-level loops.
static ChildIteratorType child_begin(NodeType *N)
void reserveBlocks(unsigned size)
reserveBlocks- interface to do reserve() for Blocks
LoopInfo::iterator ChildIteratorType
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
std::vector< BlockT * >::const_iterator block_iterator
void verifyLoopNest(DenseSet< const LoopT * > *Loops) const
verifyLoop - Verify loop structure of this loop and all nested loops.
block_iterator block_end() const
static NodeType * getEntryNode(Loop *L)
unsigned getNumBlocks() const
getNumBlocks - Get the number of blocks in this loop in constant time.
void moveToHeader(BlockT *BB)
moveToHeader - This method is used to move BB (which must be part of this loop) to be the loop header...
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
BlockT * getLoopPredecessor() const
getLoopPredecessor - If the given loop's header has exactly one unique predecessor outside the loop...
bool runOnFunction(Function &F) override
Calculate the natural loop information for a given function.
std::vector< LoopT * > & getSubLoopsVector()
static ChildIteratorType child_end(NodeType *N)
LLVM Value Representation.
LoopBase()
Loop ctor - This creates an empty loop.
void changeTopLevelLoop(LoopT *OldLoop, LoopT *NewLoop)
changeTopLevelLoop - Replace the specified loop in the top-level loops list with the indicated loop...
This class implements an extremely fast bulk output stream that can only output to a stream...
const std::vector< LoopT * > & getSubLoops() const
iterator/begin/end - 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 generic analysis pass manager with lazy running and caching of results.
bool replacementPreservesLCSSAForm(Instruction *From, Value *To)
replacementPreservesLCSSAForm - Returns true if replacing From with To everywhere is guaranteed to pr...
std::vector< LoopT * >::const_iterator iterator
reverse_iterator rbegin() const
unsigned getLoopDepth() const
getLoopDepth - Return the nesting level of this loop.
void reverseBlock(unsigned from)
reverseBlocks - interface to reverse Blocks[from, end of loop] in this loop
const BasicBlock * getParent() const
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
LoopInfo - This class builds and contains all of the top level loop structures in the specified funct...