48 cl::desc(
"Verify loop info (time consuming)"));
106 if (isa<LandingPadInst>(I))
138 BasicBlock *Incoming =
nullptr, *Backedge =
nullptr;
141 "Loop must have at least one backedge!");
143 if (PI ==
pred_end(H))
return nullptr;
145 if (PI !=
pred_end(H))
return nullptr;
159 if (CI->isNullValue())
162 if (Inc->getOpcode() == Instruction::Add &&
163 Inc->getOperand(0) == PN)
164 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
165 if (CI->equalsInt(1))
176 for (
Use &U :
I->uses()) {
179 if (
PHINode *
P = dyn_cast<PHINode>(UI))
180 UserBB =
P->getIncomingBlock(U);
211 if (isa<IndirectBrInst>((*I)->getTerminator()))
214 if (
const InvokeInst *II = dyn_cast<InvokeInst>((*I)->getTerminator()))
215 if (II->cannotDuplicate())
219 if (
const CallInst *CI = dyn_cast<CallInst>(BI)) {
220 if (CI->cannotDuplicate())
252 else if (MD != LoopID)
263 assert(LoopID &&
"Loop ID should not be null");
264 assert(LoopID->
getNumOperands() > 0 &&
"Loop ID needs at least one operand");
265 assert(LoopID->
getOperand(0) == LoopID &&
"Loop ID should refer to itself");
285 if (!desiredLoopIdMetadata)
297 if (!II->mayReadOrWriteMemory())
310 bool loopIdMDFound =
false;
311 for (
unsigned i = 0, e = loopIdMD->
getNumOperands(); i < e; ++i) {
312 if (loopIdMD->
getOperand(i) == desiredLoopIdMetadata) {
313 loopIdMDFound =
true;
333 for (
unsigned i = 0, e = ExitBlocks.
size(); i != e; ++i)
335 PE =
pred_end(ExitBlocks[i]); PI != PE; ++PI)
349 "getUniqueExitBlocks assumes the loop has canonical form exits!");
356 switchExitBlocks.
clear();
370 if (current != firstPred)
384 if (std::find(switchExitBlocks.
begin(), switchExitBlocks.
end(), *
I)
385 == switchExitBlocks.
end()) {
398 if (UniqueExitBlocks.
size() == 1)
399 return UniqueExitBlocks[0];
403 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
416 class UnloopUpdater {
434 Unloop(UL), LI(LInfo), DFS(UL), FoundIB(
false) {}
436 void updateBlockParents();
438 void removeBlocksFromAncestors();
440 void updateSubloopParents();
449 void UnloopUpdater::updateBlockParents() {
450 if (Unloop->getNumBlocks()) {
455 POE = Traversal.
end(); POI != POE; ++POI) {
457 Loop *L = LI->getLoopFor(*POI);
458 Loop *NL = getNearestLoop(*POI, L);
462 assert((NL != Unloop && (!NL || NL->
contains(Unloop))) &&
463 "uninitialized successor");
464 LI->changeLoopFor(*POI, NL);
469 assert((FoundIB || Unloop->contains(L)) &&
"uninitialized successor");
475 bool Changed = FoundIB;
476 for (
unsigned NIters = 0; Changed; ++NIters) {
477 assert(NIters < Unloop->getNumBlocks() &&
"runaway iterative algorithm");
483 POE = DFS.endPostorder(); POI != POE; ++POI) {
485 Loop *L = LI->getLoopFor(*POI);
486 Loop *NL = getNearestLoop(*POI, L);
488 assert(NL != Unloop && (!NL || NL->
contains(Unloop)) &&
489 "uninitialized successor");
490 LI->changeLoopFor(*POI, NL);
499 void UnloopUpdater::removeBlocksFromAncestors() {
503 BE = Unloop->block_end(); BI != BE; ++BI) {
504 Loop *OuterParent = LI->getLoopFor(*BI);
505 if (Unloop->contains(OuterParent)) {
508 OuterParent = SubloopParents[OuterParent];
514 assert(OldParent &&
"new loop is not an ancestor of the original");
515 OldParent->removeBlockFromLoop(*BI);
522 void UnloopUpdater::updateSubloopParents() {
523 while (!Unloop->empty()) {
524 Loop *Subloop = *std::prev(Unloop->end());
527 assert(SubloopParents.count(Subloop) &&
"DFS failed to visit subloop");
528 if (
Loop *Parent = SubloopParents[Subloop])
529 Parent->addChildLoop(Subloop);
531 LI->addTopLevelLoop(Subloop);
544 Loop *NearLoop = BBLoop;
546 Loop *Subloop =
nullptr;
547 if (NearLoop != Unloop && Unloop->
contains(NearLoop)) {
552 assert(Subloop &&
"subloop is not an ancestor of the original loop");
556 SubloopParents.insert(std::make_pair(Subloop, Unloop)).first->second;
561 assert(!Subloop &&
"subloop blocks must have a successor");
564 for (; I != E; ++
I) {
568 Loop *L = LI->getLoopFor(*I);
572 assert((FoundIB || !DFS.hasPostorder(*I)) &&
"should have seen IB");
575 if (L != Unloop && Unloop->
contains(L)) {
581 assert(L->
getParentLoop() == Unloop &&
"cannot skip into nested loops");
584 L = SubloopParents[L];
595 if (NearLoop == Unloop || !NearLoop || NearLoop->
contains(L))
599 SubloopParents[Subloop] = NearLoop;
632 assert(I !=
end() &&
"Couldn't find loop");
640 while (!Unloop->
empty())
648 UnloopUpdater Updater(Unloop,
this);
649 Updater.updateBlockParents();
652 Updater.removeBlocksFromAncestors();
655 Updater.updateSubloopParents();
660 assert(I != ParentLoop->
end() &&
"Couldn't find loop");
668 char LoopAnalysis::PassID;
699 bool LoopInfoWrapperPass::runOnFunction(
Function &) {
701 LI.Analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree());
734 POE = Traversal.
end(); POI != POE; ++POI) ;
static const char *const LoopMDName
void push_back(const T &Elt)
BasicBlock * getUniqueExitBlock() const
getUniqueExitBlock - If getUniqueExitBlocks would return exactly one block, return that block...
LoopT * removeLoop(iterator I)
removeLoop - This removes the specified top-level loop from this loop info object.
A Module instance is used to store all the information related to an LLVM module. ...
bool isAnnotatedParallel() const
Returns true if the loop is annotated parallel.
unsigned getNumOperands() const
unsigned getNumOperands() const
Return number of MDNode operands.
PreservedAnalyses run(Function &F, AnalysisManager< Function > *AM)
static po_iterator begin(GraphT G)
CallInst - This class represents a function call, abstracting a target machine's calling convention...
LoopInfo run(Function &F, AnalysisManager< Function > *AM)
void changeLoopFor(BlockT *BB, LoopT *L)
changeLoopFor - Change the top-level loop that contains BB to the specified loop. ...
LoopT * getParentLoop() const
void updateUnloop(Loop *Unloop)
updateUnloop - Update LoopInfo after removing the last backedge from a loop–now the "unloop"...
bool hasLoopInvariantOperands(const Instruction *I) const
hasLoopInvariantOperands - Return true if all the operands of the specified instruction are loop inva...
Analysis pass which computes a DominatorTree.
void print(raw_ostream &OS) const
LoopT * getLoopFor(const BlockT *BB) const
getLoopFor - Return the inner most loop that BB lives in.
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.
iterator begin()
Instruction iterator methods.
void print(raw_ostream &OS, unsigned Depth=0) const
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Traverse the blocks in a loop using a depth-first search.
A Use represents the edge between a Value definition and its users.
POTIterator begin()
Postorder traversal over the graph.
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
bool isLoopInvariant(const Value *V) const
isLoopInvariant - Return true if the specified value is loop invariant
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.
bool isLoopSimplifyForm() const
isLoopSimplifyForm - Return true if the Loop is in the form that the LoopSimplify form transforms loo...
void getExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
getExitBlocks - Return all of the successor blocks of this loop.
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool mayReadFromMemory() const
mayReadFromMemory - Return true if this instruction may read memory.
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
static cl::opt< bool, true > VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo), cl::desc("Verify loop info (time consuming)"))
void perform(LoopInfo *LI)
Traverse the loop blocks and store the DFS result.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Interval::succ_iterator succ_end(Interval *I)
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
Subclasses of this class are all able to terminate a basic block.
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.
LLVM Basic Block Representation.
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
static bool VerifyLoopInfo
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static po_iterator end(GraphT G)
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
MDNode * getLoopID() const
Return the llvm.loop loop id metadata node for this loop if it is present.
Represent the analysis usage information of a pass.
bool contains(const LoopT *L) const
contains - Return true if the specified loop is contained within in this loop.
bool isSafeToClone() const
isSafeToClone - Return true if the loop body is safe to clone in practice.
Value * getOperand(unsigned i) const
Interval::pred_iterator pred_end(Interval *I)
bool hasDedicatedExits() const
hasDedicatedExits - Return true if no exit block for the loop has a predecessor that is outside the l...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void getUniqueExitBlocks(SmallVectorImpl< BasicBlock * > &ExitBlocks) const
getUniqueExitBlocks - Return all unique successor blocks of this loop.
void setMetadata(unsigned KindID, MDNode *Node)
setMetadata - Set the metadata of the specified kind to the specified node.
void setLoopID(MDNode *LoopID) const
Set the llvm.loop loop id metadata for this loop.
const MDOperand & getOperand(unsigned I) const
INITIALIZE_PASS_BEGIN(LoopInfoWrapperPass,"loops","Natural Loop Information", true, true) INITIALIZE_PASS_END(LoopInfoWrapperPass
This is the shared class of boolean and integer constants.
bool makeLoopInvariant(Value *V, bool &Changed, Instruction *InsertPt=nullptr) const
makeLoopInvariant - If the given value is an instruction inside of the loop and it can be hoisted...
bool isLCSSAForm(DominatorTree &DT) const
isLCSSAForm - Return true if the Loop is in LCSSA form
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
MDNode * getMetadata(unsigned KindID) const
getMetadata - Get the metadata of given kind attached to this Instruction.
PassT::Result & getResult(IRUnitT &IR)
Get the result of an analysis pass for this module.
PHINode * getCanonicalInductionVariable() const
getCanonicalInductionVariable - Check to see if the loop has a canonical induction variable: an integ...
void addTopLevelLoop(LoopT *New)
addTopLevelLoop - This adds the specified loop to the collection of top-level loops.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Store the result of a depth first search within basic blocks contained by a single loop...
Value * getIncomingValueForBlock(const BasicBlock *BB) const
void setPreservesAll()
Set by analyses that do not transform their input at all.
std::vector< BlockT * >::const_iterator block_iterator
block_iterator block_end() const
void Analyze(DominatorTreeBase< BlockT > &DomTree)
Create the loop forest using a stable algorithm.
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
std::vector< BasicBlock * >::const_iterator POIterator
Postorder list iterators.
bool all_of(R &&Range, UnaryPredicate &&P)
Provide wrappers to std::all_of which take ranges instead of having to pass being/end explicitly...
LLVM Value Representation.
void moveBefore(Instruction *MovePos)
moveBefore - Unlink this instruction from its current basic block and insert it into the basic block ...
This class implements an extremely fast bulk output stream that can only output to a stream...
InvokeInst - Invoke instruction.
block_iterator block_begin() const
The legacy pass manager's analysis pass to compute loop information.
A generic analysis pass manager with lazy running and caching of results.
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
isSafeToSpeculativelyExecute - Return true if the instruction does not have any effects besides calcu...
Legacy analysis pass which computes a DominatorTree.
std::vector< LoopT * >::const_iterator iterator
This header defines various interfaces for pass management in LLVM.
LocationClass< Ty > location(Ty &L)
const BasicBlock * getParent() const
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...