15 #ifndef LLVM_ANALYSIS_LOOPINFOIMPL_H
16 #define LLVM_ANALYSIS_LOOPINFOIMPL_H
33 template<
class BlockT,
class LoopT>
37 for (
block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
38 for (
typename BlockTraits::ChildIteratorType
I =
39 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
50 template<
class BlockT,
class LoopT>
53 getExitingBlocks(ExitingBlocks);
54 if (ExitingBlocks.
size() == 1)
55 return ExitingBlocks[0];
62 template<
class BlockT,
class LoopT>
66 for (
block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
67 for (
typename BlockTraits::ChildIteratorType
I =
68 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
77 template<
class BlockT,
class LoopT>
80 getExitBlocks(ExitBlocks);
81 if (ExitBlocks.
size() == 1)
87 template<
class BlockT,
class LoopT>
91 for (
block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
92 for (
typename BlockTraits::ChildIteratorType
I =
93 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
107 template<
class BlockT,
class LoopT>
110 BlockT *Out = getLoopPredecessor();
111 if (!Out)
return nullptr;
115 typename BlockTraits::ChildIteratorType
SI = BlockTraits::child_begin(Out);
117 if (SI != BlockTraits::child_end(Out))
129 template<
class BlockT,
class LoopT>
132 BlockT *Out =
nullptr;
135 BlockT *Header = getHeader();
137 for (
typename InvBlockTraits::ChildIteratorType PI =
138 InvBlockTraits::child_begin(Header),
139 PE = InvBlockTraits::child_end(Header); PI != PE; ++PI) {
149 assert(Out &&
"Header of loop has no predecessors from outside loop?");
155 template<
class BlockT,
class LoopT>
157 BlockT *Header = getHeader();
159 typename InvBlockTraits::ChildIteratorType PI =
160 InvBlockTraits::child_begin(Header);
161 typename InvBlockTraits::ChildIteratorType PE =
162 InvBlockTraits::child_end(Header);
163 BlockT *Latch =
nullptr;
164 for (; PI != PE; ++PI) {
167 if (Latch)
return nullptr;
185 template<
class BlockT,
class LoopT>
188 assert((Blocks.empty() || LIB[getHeader()] ==
this) &&
189 "Incorrect LI specified for this loop!");
190 assert(NewBB &&
"Cannot add a null basic block to the loop!");
191 assert(!LIB[NewBB] &&
"BasicBlock already in the loop!");
193 LoopT *L =
static_cast<LoopT *
>(
this);
196 LIB.BBMap[NewBB] = L;
200 L->addBlockEntry(NewBB);
201 L = L->getParentLoop();
209 template<
class BlockT,
class LoopT>
212 assert(OldChild->ParentLoop ==
this &&
"This loop is already broken!");
213 assert(!NewChild->ParentLoop &&
"NewChild already has a parent!");
214 typename std::vector<LoopT *>::iterator
I =
215 std::find(SubLoops.begin(), SubLoops.end(), OldChild);
216 assert(I != SubLoops.end() &&
"OldChild not in loop!");
218 OldChild->ParentLoop =
nullptr;
219 NewChild->ParentLoop =
static_cast<LoopT *
>(
this);
223 template<
class BlockT,
class LoopT>
226 assert(!Blocks.empty() &&
"Loop header is missing");
230 getExitBlocks(ExitBBs);
232 VisitSet.
insert(ExitBBs.begin(), ExitBBs.end());
238 unsigned NumVisited = 0;
241 for ( ; BI != BE; ++BI) {
243 bool HasInsideLoopSuccs =
false;
244 bool HasInsideLoopPreds =
false;
248 for (
typename BlockTraits::ChildIteratorType
SI =
249 BlockTraits::child_begin(BB), SE = BlockTraits::child_end(BB);
252 HasInsideLoopSuccs =
true;
256 for (
typename InvBlockTraits::ChildIteratorType PI =
257 InvBlockTraits::child_begin(BB), PE = InvBlockTraits::child_end(BB);
261 HasInsideLoopPreds =
true;
266 if (BB == getHeader()) {
267 assert(!OutsideLoopPreds.
empty() &&
"Loop is unreachable!");
268 }
else if (!OutsideLoopPreds.
empty()) {
272 BlockT *EntryBB = BB->getParent()->begin();
274 for (
unsigned i = 0, e = OutsideLoopPreds.
size(); i != e; ++i)
275 assert(CB != OutsideLoopPreds[i] &&
276 "Loop has multiple entry points!");
278 assert(HasInsideLoopPreds &&
"Loop block has no in-loop predecessors!");
279 assert(HasInsideLoopSuccs &&
"Loop block has no in-loop successors!");
281 "Loop contains function entry block!");
286 assert(NumVisited == getNumBlocks() &&
"Unreachable block in loop");
291 for (
block_iterator BI = (*I)->block_begin(), BE = (*I)->block_end();
294 "Loop does not contain all the blocks of a subloop!");
299 assert(std::find(ParentLoop->begin(), ParentLoop->end(),
this) !=
301 "Loop is not a subloop of its parent!");
307 template<
class BlockT,
class LoopT>
310 Loops->
insert(static_cast<const LoopT *>(
this));
315 (*I)->verifyLoopNest(Loops);
318 template<
class BlockT,
class LoopT>
320 OS.
indent(Depth*2) <<
"Loop at depth " << getLoopDepth()
323 for (
unsigned i = 0; i < getBlocks().size(); ++i) {
325 BlockT *BB = getBlocks()[i];
326 BB->printAsOperand(OS,
false);
327 if (BB == getHeader()) OS <<
"<header>";
328 if (BB == getLoopLatch()) OS <<
"<latch>";
329 if (isLoopExiting(BB)) OS <<
"<exiting>";
334 (*I)->print(OS, Depth+2);
345 template<
class BlockT,
class LoopT>
351 unsigned NumBlocks = 0;
352 unsigned NumSubloops = 0;
355 std::vector<BlockT *> ReverseCFGWorklist(Backedges.
begin(), Backedges.
end());
356 while (!ReverseCFGWorklist.empty()) {
357 BlockT *PredBB = ReverseCFGWorklist.back();
358 ReverseCFGWorklist.pop_back();
368 if (PredBB == L->getHeader())
371 ReverseCFGWorklist.insert(ReverseCFGWorklist.end(),
372 InvBlockTraits::child_begin(PredBB),
373 InvBlockTraits::child_end(PredBB));
377 while (LoopT *Parent = Subloop->getParentLoop())
385 Subloop->setParentLoop(L);
387 NumBlocks += Subloop->getBlocks().capacity();
388 PredBB = Subloop->getHeader();
393 for (
typename InvBlockTraits::ChildIteratorType PI =
394 InvBlockTraits::child_begin(PredBB),
395 PE = InvBlockTraits::child_end(PredBB); PI != PE; ++PI) {
397 ReverseCFGWorklist.push_back(*PI);
401 L->getSubLoopsVector().reserve(NumSubloops);
402 L->reserveBlocks(NumBlocks);
406 template<
class BlockT,
class LoopT>
409 typedef typename BlockTraits::ChildIteratorType SuccIterTy;
423 template<
class BlockT,
class LoopT>
432 template<
class BlockT,
class LoopT>
434 LoopT *Subloop = LI->getLoopFor(Block);
435 if (Subloop && Block == Subloop->getHeader()) {
438 if (Subloop->getParentLoop())
439 Subloop->getParentLoop()->getSubLoopsVector().push_back(Subloop);
441 LI->addTopLevelLoop(Subloop);
445 Subloop->reverseBlock(1);
446 std::reverse(Subloop->getSubLoopsVector().begin(),
447 Subloop->getSubLoopsVector().end());
449 Subloop = Subloop->getParentLoop();
451 for (; Subloop; Subloop = Subloop->getParentLoop())
452 Subloop->addBlockEntry(Block);
469 template<
class BlockT,
class LoopT>
477 BlockT *Header = DomNode->
getBlock();
482 for (
typename InvBlockTraits::ChildIteratorType PI =
483 InvBlockTraits::child_begin(Header),
484 PE = InvBlockTraits::child_end(Header); PI != PE; ++PI) {
486 BlockT *Backedge = *PI;
495 if (!Backedges.
empty()) {
496 LoopT *L =
new LoopT(Header);
507 template<
class BlockT,
class LoopT>
509 for (
unsigned i = 0; i < TopLevelLoops.size(); ++i)
510 TopLevelLoops[i]->print(OS);
513 E = BBMap.end();
I != E; ++
I)
514 OS <<
"BB '" <<
I->first->getName() <<
"' level = "
515 <<
I->second->getLoopDepth() <<
"\n";
519 template<
class BlockT,
class LoopT>
523 assert(!(*I)->getParentLoop() &&
"Top-level loop has a parent!");
524 (*I)->verifyLoopNest(&Loops);
529 for (
auto &Entry : BBMap) {
530 const BlockT *BB = Entry.first;
531 LoopT *L = Entry.second;
532 assert(Loops.
count(L) &&
"orphaned loop");
533 assert(L->contains(BB) &&
"orphaned block");
void push_back(const T &Elt)
const_iterator end(StringRef path)
Get end iterator over path.
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
DenseSet - This implements a dense probed hash-table based set.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
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 insertIntoLoop(BlockT *Block)
Add a single Block to its ancestor loops in PostOrder.
void changeLoopFor(BlockT *BB, LoopT *L)
changeLoopFor - Change the top-level loop that contains BB to the specified loop. ...
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.
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
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
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...
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
void getExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
getExitBlocks - Return all of the successor blocks of this loop.
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
addBasicBlockToLoop - This method is used by other analyses to update loop information.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
void traverse(BlockT *EntryBlock)
Top-level driver for the forward DFS within the loop.
Base class for the actual dominator tree node.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
void verifyLoop() const
verifyLoop - Verify loop structure
Core dominator tree base class.
PopulateLoopsDFS(LoopInfoBase< BlockT, LoopT > *li)
BlockT * getLoopPreheader() const
getLoopPreheader - If there is a preheader for this loop, return it.
bool isReachableFromEntry(const NodeT *A) const
isReachableFromEntry - Return true if A is dominated by the entry block of the function containing it...
bool dominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
dominates - Returns true iff A dominates B.
size_type count(const ValueT &V) const
Return 1 if the specified key is in the set, 0 otherwise.
df_ext_iterator< T, SetTy > df_ext_end(const T &G, SetTy &S)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
df_ext_iterator< T, SetTy > df_ext_begin(const T &G, SetTy &S)
BlockT * getExitingBlock() const
getExitingBlock - If getExitingBlocks would return exactly one block, return that block...
iterator_range< po_iterator< T > > post_order(const T &G)
std::pair< const BlockT *, const BlockT * > Edge
Edge type.
std::pair< iterator, bool > insert(const ValueT &V)
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
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.
static void discoverAndMapSubloop(LoopT *L, ArrayRef< BlockT * > Backedges, LoopInfoBase< BlockT, LoopT > *LI, DominatorTreeBase< BlockT > &DomTree)
Stable LoopInfo Analysis - Build a loop tree using stable iterators so the result does / not depend o...
void Analyze(DominatorTreeBase< BlockT > &DomTree)
Create the loop forest using a stable algorithm.
BlockT * getLoopPredecessor() const
getLoopPredecessor - If the given loop's header has exactly one unique predecessor outside the loop...
iterator_range< df_iterator< T > > depth_first(const T &G)
static const Function * getParent(const Value *V)
This class implements an extremely fast bulk output stream that can only output to a stream...
std::vector< LoopT * >::const_iterator iterator
Populate all loop data in a stable order during a single forward DFS.