42 #ifdef EXPENSIVE_CHECKS
49 cl::desc(
"Verify loop info (time consuming)"));
113 BasicBlock *Incoming =
nullptr, *Backedge =
nullptr;
116 "Loop must have at least one backedge!");
118 if (PI ==
pred_end(H))
return nullptr;
120 if (PI !=
pred_end(H))
return nullptr;
134 if (CI->isNullValue())
138 Inc->getOperand(0) == PN)
139 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
140 if (CI->equalsInt(1))
153 if (
I.getType()->isTokenTy())
156 for (
const Use &U :
I.uses()) {
157 const Instruction *UI = cast<Instruction>(U.getUser());
159 if (
const PHINode *
P = dyn_cast<PHINode>(UI))
160 UserBB =
P->getIncomingBlock(U);
166 if (UserBB != &BB && !L.
contains(UserBB) &&
201 if (isa<IndirectBrInst>(BB->getTerminator()))
206 if (CS.cannotDuplicate())
236 else if (MD != LoopID)
247 assert(LoopID &&
"Loop ID should not be null");
249 assert(LoopID->
getOperand(0) == LoopID &&
"Loop ID should refer to itself");
269 if (!DesiredLoopIdMetadata)
279 if (!
I.mayReadOrWriteMemory())
292 bool LoopIdMDFound =
false;
294 if (MDOp == DesiredLoopIdMetadata) {
295 LoopIdMDFound =
true;
318 for (
unsigned i = 1, ie = LoopID->getNumOperands();
i < ie; ++
i) {
319 if (
DILocation *
L = dyn_cast<DILocation>(LoopID->getOperand(
i))) {
333 if (
DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
339 return LocRange(HeadBB->getTerminator()->getDebugLoc());
360 "getUniqueExitBlocks assumes the loop has canonical form exits!");
364 SwitchExitBlocks.
clear();
402 if (UniqueExitBlocks.
size() == 1)
403 return UniqueExitBlocks[0];
407 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
424 class UnloopUpdater {
442 Unloop(*UL), LI(LInfo),
DFS(UL), FoundIB(
false) {}
444 void updateBlockParents();
446 void removeBlocksFromAncestors();
448 void updateSubloopParents();
457 void UnloopUpdater::updateBlockParents() {
458 if (Unloop.getNumBlocks()) {
464 Loop *
L = LI->getLoopFor(POI);
465 Loop *NL = getNearestLoop(POI, L);
470 "uninitialized successor");
471 LI->changeLoopFor(POI, NL);
476 assert((FoundIB || Unloop.contains(L)) &&
"uninitialized successor");
482 bool Changed = FoundIB;
483 for (
unsigned NIters = 0; Changed; ++NIters) {
484 assert(NIters < Unloop.getNumBlocks() &&
"runaway iterative algorithm");
490 POE =
DFS.endPostorder(); POI != POE; ++POI) {
492 Loop *L = LI->getLoopFor(*POI);
493 Loop *NL = getNearestLoop(*POI, L);
496 "uninitialized successor");
497 LI->changeLoopFor(*POI, NL);
505 void UnloopUpdater::removeBlocksFromAncestors() {
509 BE = Unloop.block_end(); BI != BE; ++BI) {
510 Loop *OuterParent = LI->getLoopFor(*BI);
511 if (Unloop.contains(OuterParent)) {
514 OuterParent = SubloopParents[OuterParent];
520 assert(OldParent &&
"new loop is not an ancestor of the original");
521 OldParent->removeBlockFromLoop(*BI);
527 void UnloopUpdater::updateSubloopParents() {
528 while (!Unloop.empty()) {
529 Loop *Subloop = *std::prev(Unloop.end());
532 assert(SubloopParents.count(Subloop) &&
"DFS failed to visit subloop");
533 if (
Loop *Parent = SubloopParents[Subloop])
534 Parent->addChildLoop(Subloop);
536 LI->addTopLevelLoop(Subloop);
549 Loop *NearLoop = BBLoop;
551 Loop *Subloop =
nullptr;
552 if (NearLoop != &Unloop && Unloop.
contains(NearLoop)) {
557 assert(Subloop &&
"subloop is not an ancestor of the original loop");
560 NearLoop = SubloopParents.insert({Subloop, &Unloop}).first->second;
565 assert(!Subloop &&
"subloop blocks must have a successor");
568 for (; I !=
E; ++
I) {
572 Loop *L = LI->getLoopFor(*I);
576 assert((FoundIB || !
DFS.hasPostorder(*I)) &&
"should have seen IB");
579 if (L != &Unloop && Unloop.
contains(L)) {
588 L = SubloopParents[
L];
599 if (NearLoop == &Unloop || !NearLoop || NearLoop->
contains(L))
603 SubloopParents[Subloop] = NearLoop;
616 RemovedLoops.push_back(Unloop);
636 assert(I !=
end() &&
"Couldn't find loop");
644 while (!Unloop->
empty())
652 UnloopUpdater Updater(Unloop,
this);
653 Updater.updateBlockParents();
656 Updater.removeBlocksFromAncestors();
659 Updater.updateSubloopParents();
664 assert(I != ParentLoop->
end() &&
"Couldn't find loop");
694 for (
auto *Block : L.
blocks())
698 OS <<
"Printing <null> block";
712 bool LoopInfoWrapperPass::runOnFunction(
Function &) {
714 LI.analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree());
725 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
757 POE = Traversal.
end(); POI != POE; ++POI) ;
void push_back(const T &Elt)
Tracking metadata reference owned by Metadata.
LoopInfo run(Function &F, FunctionAnalysisManager &AM)
BasicBlock * getUniqueExitBlock() const
If getUniqueExitBlocks would return exactly one block, return that block.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LoopT * removeLoop(iterator I)
This removes the specified top-level loop from this loop info object.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
A Module instance is used to store all the information related to an LLVM module. ...
void dropUnknownNonDebugMetadata(ArrayRef< unsigned > KnownIDs)
Drop all unknown metadata except for debug locations.
bool isAnnotatedParallel() const
Returns true if the loop is annotated parallel.
DebugLoc getStartLoc() const
Return the debug location of the start of this loop.
unsigned getNumOperands() const
Return number of MDNode operands.
void invalidate()
Invalidate the loop, indicating that it is no longer a loop.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
void changeLoopFor(BlockT *BB, LoopT *L)
Change the top-level loop that contains BB to the specified loop.
LoopT * getParentLoop() const
bool hasLoopInvariantOperands(const Instruction *I) const
Return true if all the operands of the specified instruction are loop invariant.
Analysis pass which computes a DominatorTree.
void print(raw_ostream &OS) const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
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.
iterator begin()
Instruction iterator methods.
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.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
bool isLoopInvariant(const Value *V) const
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
Return true if the Loop is in the form that the LoopSimplify form transforms loops to...
void getExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
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
Return true if this instruction may read memory.
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Function Alias Analysis false
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...
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
Interval::succ_iterator succ_end(Interval *I)
iterator_range< block_iterator > blocks() const
Subclasses of this class are all able to terminate a basic block.
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.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
LLVM Basic Block Representation.
static bool VerifyLoopInfo
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const DebugLoc & getStart() const
void analyze(const DominatorTreeBase< BlockT > &DomTree)
Create the loop forest using a stable algorithm.
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...
bool isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const
Return true if this Loop and all inner subloops are in LCSSA form.
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
Return true if the specified loop is contained within in this loop.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
bool isSafeToClone() const
Return true if the loop body is safe to clone in practice.
Interval::pred_iterator pred_end(Interval *I)
void markAsRemoved(Loop *L)
Update LoopInfo after removing the last backedge from a loop.
bool hasDedicatedExits() const
Return true if no exit block for the loop has a predecessor that is outside the loop.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void getUniqueExitBlocks(SmallVectorImpl< BasicBlock * > &ExitBlocks) const
Return all unique successor blocks of this loop.
A range representing the start and end location of a loop.
void setMetadata(unsigned KindID, MDNode *Node)
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
Iterator for intrusive lists based on ilist_node.
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
If the given value is an instruction inside of the loop and it can be hoisted, do so to make it trivi...
bool isLCSSAForm(DominatorTree &DT) const
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
Get the metadata of given kind attached to this Instruction.
PHINode * getCanonicalInductionVariable() const
Check to see if the loop has a canonical induction variable: an integer recurrence that starts at 0 a...
pred_range predecessors(BasicBlock *BB)
void addTopLevelLoop(LoopT *New)
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
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
Represents a single loop in the control flow graph.
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
std::vector< BasicBlock * >::const_iterator POIterator
Postorder list iterators.
LocRange getLocRange() const
Return the source code span of the loop.
op_range operands() const
void verify(const DominatorTreeBase< BlockT > &DomTree) const
bool isInvalid()
Return true if this loop is no longer valid.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM Value Representation.
succ_range successors(BasicBlock *BB)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
This class implements an extremely fast bulk output stream that can only output to a stream...
block_iterator block_begin() const
The legacy pass manager's analysis pass to compute loop information.
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.
Legacy analysis pass which computes a DominatorTree.
std::vector< LoopT * >::const_iterator iterator
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.
A special type used by analysis passes to provide an address that identifies that particular analysis...
LocationClass< Ty > location(Ty &L)
const BasicBlock * getParent() const
static bool isBlockInLCSSAForm(const Loop &L, const BasicBlock &BB, DominatorTree &DT)
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.