73 #define DEBUG_TYPE "loop-simplify"
75 STATISTIC(NumInserted,
"Number of pre-header or exit blocks inserted");
76 STATISTIC(NumNested ,
"Number of nested loops split out");
86 for (
unsigned i = 0, e = SplitPreds.
size();
i != e; ++
i) {
87 if (&*BBI == SplitPreds[
i])
98 for (
unsigned i = 0, e = SplitPreds.
size();
i != e; ++
i) {
101 FoundBB = SplitPreds[
i];
110 FoundBB = SplitPreds[0];
145 DEBUG(
dbgs() <<
"LoopSimplify: Creating pre-header "
146 << PreheaderBB->
getName() <<
"\n");
161 bool PreserveLCSSA) {
173 assert(!LoopBlocks.
empty() &&
"No edges coming in from outside the loop?");
181 DEBUG(
dbgs() <<
"LoopSimplify: Creating dedicated exit block "
182 << NewExitBB->getName() <<
"\n");
190 std::set<BasicBlock*> &Blocks) {
195 if (Blocks.insert(BB).second && BB != StopBlock)
202 }
while (!Worklist.
empty());
259 assert(!Header->
isEHPad() &&
"Can't insert backedge to EH pad");
262 if (!PN)
return nullptr;
268 for (
unsigned i = 0, e = PN->getNumIncomingValues();
i != e; ++
i) {
269 if (PN->getIncomingValue(
i) != PN ||
272 if (isa<IndirectBrInst>(PN->getIncomingBlock(
i)->getTerminator()))
274 OuterLoopPreds.
push_back(PN->getIncomingBlock(
i));
277 DEBUG(
dbgs() <<
"LoopSimplify: Splitting out a new outer loop\n");
286 DT, LI, PreserveLCSSA);
297 Parent->replaceChildLoopWith(L, NewOuter);
314 std::set<BasicBlock*> BlocksInL;
323 const std::vector<Loop*> &SubLoops = L->
getSubLoops();
324 for (
size_t I = 0;
I != SubLoops.size(); )
325 if (BlocksInL.count(SubLoops[
I]->getHeader()))
336 if (!BlocksInL.count(BB)) {
339 if ((*LI)[BB] == L) {
370 "LCSSA is broken after separating nested loops!");
395 assert(!Header->isEHPad() &&
"Can't insert backedge to EH pad");
398 std::vector<BasicBlock*> BackedgeBlocks;
406 if (P != Preheader) BackedgeBlocks.push_back(P);
411 Header->getName() +
".backedge",
F);
413 BETerminator->setDebugLoc(Header->getFirstNonPHI()->getDebugLoc());
415 DEBUG(
dbgs() <<
"LoopSimplify: Inserting unique backedge block "
416 << BEBlock->
getName() <<
"\n");
427 PN->
getName()+
".be", BETerminator);
431 unsigned PreheaderIdx = ~0U;
432 bool HasUniqueIncomingValue =
true;
433 Value *UniqueValue =
nullptr;
437 if (IBB == Preheader) {
440 NewPN->addIncoming(IV, IBB);
441 if (HasUniqueIncomingValue) {
444 else if (UniqueValue != IV)
445 HasUniqueIncomingValue =
false;
451 assert(PreheaderIdx != ~0U &&
"PHI has no preheader entry??");
452 if (PreheaderIdx != 0) {
466 if (HasUniqueIncomingValue) {
467 NewPN->replaceAllUsesWith(UniqueValue);
478 for (
unsigned i = 0, e = BackedgeBlocks.size();
i != e; ++
i) {
505 bool PreserveLCSSA) {
506 bool Changed =
false;
519 PE =
pred_end(*BB); PI != PE; ++PI) {
528 DEBUG(
dbgs() <<
"LoopSimplify: Deleting edge from dead predecessor "
529 <<
P->getName() <<
"\n");
543 for (
BasicBlock *ExitingBlock : ExitingBlocks)
544 if (
BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator()))
545 if (BI->isConditional()) {
546 if (
UndefValue *Cond = dyn_cast<UndefValue>(BI->getCondition())) {
548 DEBUG(
dbgs() <<
"LoopSimplify: Resolving \"br i1 undef\" to exit in "
549 << ExitingBlock->getName() <<
"\n");
552 !L->
contains(BI->getSuccessor(0))));
648 bool UniqueExit =
true;
649 if (!ExitBlocks.
empty())
650 for (
unsigned i = 1, e = ExitBlocks.
size();
i != e; ++
i)
651 if (ExitBlocks[
i] != ExitBlocks[0]) {
656 for (
unsigned i = 0, e = ExitingBlocks.size();
i != e; ++
i) {
662 if (!CI || CI->
getParent() != ExitingBlock)
continue;
666 bool AllInvariant =
true;
667 bool AnyInvariant =
false;
671 if (isa<DbgInfoIntrinsic>(Inst))
678 AllInvariant =
false;
689 if (!AllInvariant)
continue;
699 DEBUG(
dbgs() <<
"LoopSimplify: Eliminating exiting block "
700 << ExitingBlock->
getName() <<
"\n");
714 const std::vector<DomTreeNodeBase<BasicBlock> *> &Children =
716 while (!Children.empty()) {
723 ExitingBlock, PreserveLCSSA);
725 ExitingBlock, PreserveLCSSA);
735 bool PreserveLCSSA) {
736 bool Changed =
false;
745 for (
unsigned Idx = 0; Idx != Worklist.
size(); ++Idx) {
746 Loop *L2 = Worklist[Idx];
750 while (!Worklist.
empty())
764 bool runOnFunction(
Function &
F)
override;
787 void verifyAnalysis()
const override;
793 "Canonicalize natural loops",
false,
false)
807 bool LoopSimplify::runOnFunction(
Function &
F) {
808 bool Changed =
false;
809 LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
810 DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
811 auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
814 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
816 bool PreserveLCSSA = mustPreserveAnalysisID(
LCSSAID);
819 assert(DT &&
"DT not available.");
820 assert(LI &&
"LI not available.");
823 assert(InLCSSA &&
"Requested to preserve LCSSA, but it's already broken.");
835 assert(InLCSSA &&
"LCSSA is broken after loop-simplify.");
843 bool Changed =
false;
874 static void verifyLoop(
Loop *L) {
886 bool HasIndBrPred =
false;
889 if (isa<IndirectBrInst>((*PI)->getTerminator())) {
894 "LoopSimplify has no excuse for missing loop header info!");
900 bool HasIndBrExiting =
false;
903 for (
unsigned i = 0, e = ExitingBlocks.
size();
i != e; ++
i) {
904 if (isa<IndirectBrInst>((ExitingBlocks[
i])->getTerminator())) {
905 HasIndBrExiting =
true;
911 "LoopSimplify has no excuse for missing exit block info!");
912 (void)HasIndBrExiting;
917 void LoopSimplify::verifyAnalysis()
const {
Legacy wrapper pass to provide the GlobalsAAResult object.
unsigned getNumBackEdges() const
Calculate the number of back edges to the loop header.
Pass interface - Implemented by all 'passes'.
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void push_back(const T &Elt)
loop Canonicalize natural loops
A parsed version of the target data layout string in and methods for querying it. ...
Pass * createLoopSimplifyPass()
This class is the base class for the comparison instructions.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs=false)
Notify the BasicBlock that the predecessor Pred is no longer able to reach it.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void removeBlock(BlockT *BB)
This method completely removes BB from all data structures, including all of the Loop objects it is n...
const std::vector< DomTreeNodeBase< NodeT > * > & getChildren() const
STATISTIC(NumFunctions,"Total number of functions")
iterator erase(iterator where)
AnalysisPass to compute dependence information in a function.
This is the interface for a simple mod/ref and alias analysis over globals.
Legacy pass manager pass to access dependence information.
The main scalar evolution driver.
An immutable pass that tracks lazily created AssumptionCache objects.
BasicBlock * InsertPreheaderForLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, bool PreserveLCSSA)
InsertPreheaderForLoop - Once we discover that a loop doesn't have a preheader, this method is called...
A cache of .assume calls within a function.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
bool isEHPad() const
Return true if this basic block is an exception handling block.
void changeLoopFor(BlockT *BB, LoopT *L)
Change the top-level loop that contains BB to the specified loop.
LoopT * getParentLoop() const
const Function * getParent() const
Return the enclosing method, or null if none.
Analysis pass which computes a DominatorTree.
static Loop * separateNestedLoop(Loop *L, BasicBlock *Preheader, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE, bool PreserveLCSSA, AssumptionCache *AC)
If this loop has multiple backedges, try to pull one of them out into a nested loop.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
unsigned changeToUnreachable(Instruction *I, bool UseLLVMTrap, bool PreserveLCSSA=false)
Insert an unreachable instruction before the specified instruction, making it and the rest of the cod...
const std::vector< BlockT * > & getBlocks() const
Get a list of the basic blocks which make up this loop.
static BasicBlock * rewriteLoopExitBlock(Loop *L, BasicBlock *Exit, DominatorTree *DT, LoopInfo *LI, bool PreserveLCSSA)
Ensure that the loop preheader dominates all exit blocks.
BlockT * getHeader() const
LoopT * removeChildLoop(iterator I)
This removes the specified child from being a subloop of this loop.
StringRef getName() const
Return a constant reference to the value's name.
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.
DomTreeNodeBase< NodeT > * getIDom() const
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
static PHINode * findPHIToPartitionLoops(Loop *L, DominatorTree *DT, AssumptionCache *AC)
The first part of loop-nestification is to find a PHI node that tells us how to partition the loops...
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
static bool simplifyOneLoop(Loop *L, SmallVectorImpl< Loop * > &Worklist, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE, AssumptionCache *AC, bool PreserveLCSSA)
Simplify one loop and queue further loops for simplification.
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
Remove an incoming value.
This is the interface for a SCEV-based alias analysis.
bool simplifyLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE, AssumptionCache *AC, bool PreserveLCSSA)
Simplify each loop in a loop nest recursively.
'undef' values are things that do not have specified contents.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void getExitingBlocks(SmallVectorImpl< BlockT * > &ExitingBlocks) const
Return all blocks inside the loop that have successors outside of the loop.
Analysis pass that exposes the LoopInfo for a function.
LLVM_NODISCARD bool empty() const
void getExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
Return all of the successor blocks of this loop.
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
This method is used by other analyses to update loop information.
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
BasicBlock * getSuccessor(unsigned i) const
Base class for the actual dominator tree node.
AnalysisUsage & addPreservedID(const void *ID)
void setSuccessor(unsigned idx, BasicBlock *B)
Update the specified successor to point at the provided block.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
void forgetLoopDispositions(const Loop *L)
Called when the client has changed the disposition of values in this loop.
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static BasicBlock * insertUniqueBackedgeBlock(Loop *L, BasicBlock *Preheader, DominatorTree *DT, LoopInfo *LI)
This method is called when the specified loop has more than one backedge in it.
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
bool FoldBranchToCommonDest(BranchInst *BI, unsigned BonusInstThreshold=1)
If this basic block is ONLY a setcc and a branch, and if a predecessor branches to us and one of our ...
static void placeSplitBlockCarefully(BasicBlock *NewBB, SmallVectorImpl< BasicBlock * > &SplitPreds, Loop *L)
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.
void eraseNode(NodeT *BB)
eraseNode - Removes a node from the dominator tree.
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
Conditional or Unconditional Branch instruction.
char & BreakCriticalEdgesID
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
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.
Represent the analysis usage information of a pass.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
void splice(iterator where, iplist_impl &L2)
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
const InstListType & getInstList() const
Return the underlying instruction list container.
Analysis pass providing a never-invalidated alias analysis result.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
FunctionPass class - This class is used to implement most global optimizations.
unsigned getMDKindID(StringRef Name) const
getMDKindID - Return a unique non-zero ID for the specified metadata kind.
Interval::pred_iterator pred_end(Interval *I)
Analysis pass providing a never-invalidated alias analysis result.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
self_iterator getIterator()
void forgetValue(Value *V)
This method should be called by the client when it has changed a value in a way that may effect its v...
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
void addBlockEntry(BlockT *BB)
This adds a basic block directly to the basic block list.
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 setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
void removeBlockFromLoop(BlockT *BB)
This removes the specified basic block from the current loop, updating the Blocks as appropriate...
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
bool isConditional() const
bool formLCSSA(Loop &L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution *SE)
Put loop into LCSSA form.
A function analysis which provides an AssumptionCache.
A SetVector that performs no allocations if smaller than a certain size.
Iterator for intrusive lists based on ilist_node.
void moveAfter(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it right after MovePos in the function M...
const BasicBlockListType & getBasicBlockList() const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Legacy wrapper pass to provide the SCEVAAResult object.
void setIncomingBlock(unsigned i, BasicBlock *BB)
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
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...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
LLVM_NODISCARD T pop_back_val()
void invalidate(IRUnitT &IR)
Invalidate a specific analysis pass for an IR module.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
pred_range predecessors(BasicBlock *BB)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
BasicBlock * getSinglePredecessor()
Return the predecessor of this block if it has a single predecessor block.
std::vector< BlockT * >::const_iterator block_iterator
loop Canonicalize natural false
Analysis pass that exposes the ScalarEvolution for a function.
block_iterator block_end() const
Analysis pass providing a never-invalidated alias analysis result.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Value * getCondition() const
INITIALIZE_PASS_BEGIN(LoopSimplify,"loop-simplify","Canonicalize natural loops", false, false) INITIALIZE_PASS_END(LoopSimplify
void forgetLoop(const Loop *L)
This method should be called by the client when it has changed a loop in a way that may effect Scalar...
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.
SymbolTableList< BasicBlock >::iterator eraseFromParent()
Unlink 'this' from the containing function and delete it.
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
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
void preserve()
Mark an analysis as preserved.
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
void initializeLoopSimplifyPass(PassRegistry &)
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, bool PreserveLCSSA=false)
This method introduces at least one new basic block into the function and moves some of the predecess...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVMContext & getContext() const
Get the context in which this basic block lives.
LLVM Value Representation.
void changeTopLevelLoop(LoopT *OldLoop, LoopT *NewLoop)
Replace the specified loop in the top-level loops list with the indicated loop.
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.
This is the interface for LLVM's primary stateless and local alias analysis.
A container for analyses that lazily runs them and caches their results.
Value * SimplifyInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
See if we can compute a simplified version of this instruction.
Legacy analysis pass which computes a DominatorTree.
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
DomTreeNodeBase< NodeT > * getNode(NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
void setIncomingValue(unsigned i, Value *V)
static void addBlockAndPredsToSet(BasicBlock *InputBB, BasicBlock *StopBlock, std::set< BasicBlock * > &Blocks)
Add the specified block, and all of its predecessors, to the specified set, if it's not already in th...
const BasicBlock * getParent() const
void splitBlock(NodeT *NewBB)
splitBlock - BB is split and now it has one successor.
Legacy wrapper pass to provide the BasicAAResult object.