74#define DEBUG_TYPE "loop-simplify"
76STATISTIC(NumNested ,
"Number of nested loops split out");
98 if (++BBI != NewBB->
getParent()->
end() && L->contains(&*BBI)) {
108 FoundBB = SplitPreds[0];
118 bool PreserveLCSSA) {
124 if (!L->contains(
P)) {
128 if (isa<IndirectBrInst>(
P->getTerminator()))
139 LI, MSSAU, PreserveLCSSA);
144 << PreheaderBB->
getName() <<
"\n");
162 if (
Blocks.insert(BB).second && BB != StopBlock)
166 }
while (!Worklist.
empty());
232 for (
auto *BB : L->blocks()) {
233 for (
auto &
II : *BB) {
234 if (
auto CI = dyn_cast<CallBase>(&
II)) {
235 if (CI->isConvergent()) {
244 assert(!Header->isEHPad() &&
"Can't insert backedge to EH pad");
247 if (!PN)
return nullptr;
262 LLVM_DEBUG(
dbgs() <<
"LoopSimplify: Splitting out a new outer loop\n");
271 DT, LI, MSSAU, PreserveLCSSA);
281 if (
Loop *Parent = L->getParentLoop())
294 L->moveToHeader(Header);
306 const std::vector<Loop*> &SubLoops = L->getSubLoops();
307 for (
size_t I = 0;
I != SubLoops.size(); )
308 if (BlocksInL.
count(SubLoops[
I]->getHeader()))
311 NewOuter->
addChildLoop(L->removeChildLoop(SubLoops.begin() +
I));
317 for (
unsigned i = 0; i != L->getBlocks().
size(); ++i) {
319 if (!BlocksInL.
count(BB)) {
321 L->removeBlockFromLoop(BB);
322 if ((*LI)[BB] == L) {
344 "LCSSA is broken after separating nested loops!");
359 assert(L->getNumBackEdges() > 1 &&
"Must have > 1 backedge!");
370 assert(!Header->isEHPad() &&
"Can't insert backedge to EH pad");
373 std::vector<BasicBlock*> BackedgeBlocks;
376 if (isa<IndirectBrInst>(
P->getTerminator()))
379 if (
P != Preheader) BackedgeBlocks.push_back(
P);
384 Header->getName() +
".backedge",
F);
386 BETerminator->
setDebugLoc(Header->getFirstNonPHI()->getDebugLoc());
388 LLVM_DEBUG(
dbgs() <<
"LoopSimplify: Inserting unique backedge block "
389 << BEBlock->
getName() <<
"\n");
404 unsigned PreheaderIdx = ~0U;
405 bool HasUniqueIncomingValue =
true;
406 Value *UniqueValue =
nullptr;
410 if (IBB == Preheader) {
414 if (HasUniqueIncomingValue) {
417 else if (UniqueValue !=
IV)
418 HasUniqueIncomingValue =
false;
424 assert(PreheaderIdx != ~0U &&
"PHI has no preheader entry??");
425 if (PreheaderIdx != 0) {
439 if (HasUniqueIncomingValue) {
463 L->addBasicBlockToLoop(BEBlock, *LI);
480 bool Changed =
false;
491 if (BB == L->getHeader())
502 LLVM_DEBUG(
dbgs() <<
"LoopSimplify: Deleting edge from dead predecessor "
503 <<
P->getName() <<
"\n");
520 L->getExitingBlocks(ExitingBlocks);
521 for (
BasicBlock *ExitingBlock : ExitingBlocks)
522 if (
BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator()))
523 if (BI->isConditional()) {
524 if (
UndefValue *
Cond = dyn_cast<UndefValue>(BI->getCondition())) {
527 <<
"LoopSimplify: Resolving \"br i1 undef\" to exit in "
528 << ExitingBlock->getName() <<
"\n");
530 BI->setCondition(ConstantInt::get(
Cond->getType(),
531 !L->contains(BI->getSuccessor(0))));
538 BasicBlock *Preheader = L->getLoopPreheader();
562 if (L->getNumBackEdges() < 8) {
564 PreserveLCSSA, AC, MSSAU)) {
596 (PN = dyn_cast<PHINode>(
I++)); )
615 auto HasUniqueExitBlock = [&]() {
617 for (
auto *ExitingBB : ExitingBlocks)
619 if (L->contains(SuccBB))
624 else if (UniqueExit != SuccBB)
630 if (HasUniqueExitBlock()) {
631 for (
BasicBlock *ExitingBlock : ExitingBlocks) {
632 if (!ExitingBlock->getSinglePredecessor())
continue;
633 BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
636 if (!CI || CI->
getParent() != ExitingBlock)
continue;
640 bool AllInvariant =
true;
641 bool AnyInvariant =
false;
642 for (
auto I = ExitingBlock->instructionsWithoutDebug().begin(); &*
I != BI; ) {
646 if (!L->makeLoopInvariant(
648 Preheader ? Preheader->
getTerminator() :
nullptr, MSSAU, SE)) {
649 AllInvariant =
false;
655 if (!AllInvariant)
continue;
666 << ExitingBlock->getName() <<
"\n");
673 while (!
Node->isLeaf()) {
680 ExitBlockSet.
insert(ExitingBlock);
685 ExitingBlock, PreserveLCSSA);
687 ExitingBlock, PreserveLCSSA);
688 ExitingBlock->eraseFromParent();
701 bool Changed =
false;
707 assert(DT &&
"DT not available.");
708 assert(LI &&
"LI not available.");
709 assert(L->isRecursivelyLCSSAForm(*DT, *LI) &&
710 "Requested to preserve LCSSA, but it's already broken.");
726 while (!Worklist.
empty())
728 AC, MSSAU, PreserveLCSSA);
776char LoopSimplify::ID = 0;
778 "Canonicalize natural loops",
false,
false)
792bool LoopSimplify::runOnFunction(
Function &
F) {
793 bool Changed =
false;
794 LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
795 DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
796 auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
799 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
F);
801 std::unique_ptr<MemorySSAUpdater> MSSAU;
802 auto *MSSAAnalysis = getAnalysisIfAvailable<MemorySSAWrapperPass>();
804 MSSA = &MSSAAnalysis->getMSSA();
805 MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
808 bool PreserveLCSSA = mustPreserveAnalysisID(
LCSSAID);
812 Changed |=
simplifyLoop(L, DT, LI, SE, AC, MSSAU.get(), PreserveLCSSA);
817 *LI, [&](
Loop *L) {
return L->isRecursivelyLCSSAForm(*DT, *LI); });
818 assert(InLCSSA &&
"LCSSA is broken after loop-simplify.");
826 bool Changed =
false;
832 std::unique_ptr<MemorySSAUpdater> MSSAU;
834 auto *MSSA = &MSSAAnalysis->getMSSA();
835 MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
867static void verifyLoop(
Loop *L) {
878 if (!L->getLoopPreheader() || !L->getLoopLatch()) {
879 bool HasIndBrPred =
false;
881 if (isa<IndirectBrInst>(Pred->getTerminator())) {
886 "LoopSimplify has no excuse for missing loop header info!");
891 if (!
L->hasDedicatedExits()) {
892 bool HasIndBrExiting =
false;
894 L->getExitingBlocks(ExitingBlocks);
895 for (
unsigned i = 0, e = ExitingBlocks.
size(); i !=
e; ++i) {
896 if (isa<IndirectBrInst>((ExitingBlocks[i])->getTerminator())) {
897 HasIndBrExiting =
true;
903 "LoopSimplify has no excuse for missing exit block info!");
904 (void)HasIndBrExiting;
909void LoopSimplify::verifyAnalysis()
const {
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This is the interface for LLVM's primary stateless and local alias analysis.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
DenseMap< Block *, BlockRelaxAux > Blocks
This is the interface for a simple mod/ref and alias analysis over globals.
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
static void placeSplitBlockCarefully(BasicBlock *NewBB, SmallVectorImpl< BasicBlock * > &SplitPreds, Loop *L)
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.
static void addBlockAndPredsToSet(BasicBlock *InputBB, BasicBlock *StopBlock, SmallPtrSetImpl< BasicBlock * > &Blocks)
Add the specified block, and all of its predecessors, to the specified set, if it's not already in th...
loop Canonicalize natural loops
static bool simplifyOneLoop(Loop *L, SmallVectorImpl< Loop * > &Worklist, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE, AssumptionCache *AC, MemorySSAUpdater *MSSAU, bool PreserveLCSSA)
Simplify one loop and queue further loops for simplification.
static Loop * separateNestedLoop(Loop *L, BasicBlock *Preheader, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE, bool PreserveLCSSA, AssumptionCache *AC, MemorySSAUpdater *MSSAU)
If this loop has multiple backedges, try to pull one of them out into a nested loop.
static BasicBlock * insertUniqueBackedgeBlock(Loop *L, BasicBlock *Preheader, DominatorTree *DT, LoopInfo *LI, MemorySSAUpdater *MSSAU)
This method is called when the specified loop has more than one backedge in it.
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
Module.h This file contains the declarations for the Module class.
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This is the interface for a SCEV-based alias analysis.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static const uint32_t IV[8]
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
A container for analyses that lazily runs them and caches their results.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addPreservedID(const void *ID)
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
A function analysis which provides an AssumptionCache.
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of @llvm.assume calls within a function.
Legacy wrapper pass to provide the BasicAAResult object.
LLVM Basic Block Representation.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
void moveAfter(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it right after MovePos in the function M...
const Function * getParent() const
Return the enclosing method, or null if none.
InstListType::iterator iterator
Instruction iterators...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)
Update PHI nodes in this BasicBlock before removal of predecessor Pred.
Conditional or Unconditional Branch instruction.
bool isConditional() const
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
BasicBlock * getSuccessor(unsigned i) const
Value * getCondition() const
Analysis pass which computes BranchProbabilityInfo.
Legacy analysis pass which computes BranchProbabilityInfo.
This class is the base class for the comparison instructions.
A parsed version of the target data layout string in and methods for querying it.
Legacy pass manager pass to access dependence information.
AnalysisPass to compute dependence information in a function.
Analysis pass which computes a DominatorTree.
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
void splitBlock(NodeT *NewBB)
splitBlock - BB is split and now it has one successor.
void eraseNode(NodeT *BB)
eraseNode - Removes a node from the dominator tree.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Legacy analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
FunctionPass class - This class is used to implement most global optimizations.
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
BasicBlockListType::iterator iterator
Legacy wrapper pass to provide the GlobalsAAResult object.
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void replaceSuccessorWith(BasicBlock *OldBB, BasicBlock *NewBB)
Replace specified successor OldBB to point at the provided block.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Analysis pass that exposes the LoopInfo for a function.
std::vector< Loop * >::const_iterator iterator
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
void addBlockEntry(BlockT *BB)
This adds a basic block directly to the basic block list.
void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild)
This is used when splitting loops up.
void changeTopLevelLoop(LoopT *OldLoop, LoopT *NewLoop)
Replace the specified loop in the top-level loops list with the indicated loop.
void removeBlock(BlockT *BB)
This method completely removes BB from all data structures, including all of the Loop objects it is n...
LoopT * AllocateLoop(ArgsTy &&...Args)
void changeLoopFor(BlockT *BB, LoopT *L)
Change the top-level loop that contains BB to the specified loop.
std::vector< Loop * >::const_iterator iterator
iterator/begin/end - The interface to the top-level loops in the current function.
The legacy pass manager's analysis pass to compute loop information.
bool replacementPreservesLCSSAForm(Instruction *From, Value *To)
Returns true if replacing From with To everywhere is guaranteed to preserve LCSSA form.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Represents a single loop in the control flow graph.
bool isRecursivelyLCSSAForm(const DominatorTree &DT, const LoopInfo &LI, bool IgnoreTokens=true) const
Return true if this Loop and all inner subloops are in LCSSA form.
An analysis that produces MemorySSA for a function.
MemorySSA * getMemorySSA() const
Get handle on MemorySSA.
void updatePhisWhenInsertingUniqueBackedgeBlock(BasicBlock *LoopHeader, BasicBlock *LoopPreheader, BasicBlock *BackedgeBlock)
Update MemorySSA when inserting a unique backedge block for a loop.
void removeBlocks(const SmallSetVector< BasicBlock *, 8 > &DeadBlocks)
Remove all MemoryAcceses in a set of BasicBlocks about to be deleted.
Legacy analysis pass which computes MemorySSA.
Encapsulates MemorySSA, including all data associated with memory accesses.
void verifyMemorySSA(VerificationLevel=VerificationLevel::Fast) const
Verify that MemorySSA is self consistent (IE definitions dominate all uses, uses appear in the right ...
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
void removeIncomingValueIf(function_ref< bool(unsigned)> Predicate, bool DeletePHIIfEmpty=true)
Remove all incoming values for which the predicate returns true.
void setIncomingBlock(unsigned i, BasicBlock *BB)
void setIncomingValue(unsigned i, Value *V)
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Pass interface - Implemented by all 'passes'.
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
virtual void verifyAnalysis() const
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void preserve()
Mark an analysis as preserved.
Legacy wrapper pass to provide the SCEVAAResult object.
Analysis pass that exposes the ScalarEvolution for a function.
The main scalar evolution driver.
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 forgetTopmostLoop(const Loop *L)
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...
bool insert(const value_type &X)
Insert a new element into the SetVector.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
'undef' values are things that do not have specified contents.
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
StringRef getName() const
Return a constant reference to the value's name.
const ParentTy * getParent() const
self_iterator getIterator()
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
This is an optimization pass for GlobalISel generic memory operations.
bool simplifyLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE, AssumptionCache *AC, MemorySSAUpdater *MSSAU, bool PreserveLCSSA)
Simplify each loop in a loop nest recursively.
BasicBlock * InsertPreheaderForLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, MemorySSAUpdater *MSSAU, bool PreserveLCSSA)
InsertPreheaderForLoop - Once we discover that a loop doesn't have a preheader, this method is called...
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
auto successors(const MachineBasicBlock *BB)
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
void initializeLoopSimplifyPass(PassRegistry &)
Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
unsigned changeToUnreachable(Instruction *I, bool PreserveLCSSA=false, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Insert an unreachable instruction before the specified instruction, making it and the rest of the cod...
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, bool PreserveLCSSA=false)
This method introduces at least one new basic block into the function and moves some of the predecess...
bool VerifyMemorySSA
Enables verification of MemorySSA.
bool formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI, MemorySSAUpdater *MSSAU, bool PreserveLCSSA)
Ensure that all exit blocks of the loop are dedicated exits.
char & BreakCriticalEdgesID
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
bool foldBranchToCommonDest(BranchInst *BI, llvm::DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr, const TargetTransformInfo *TTI=nullptr, 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 ...
bool pred_empty(const BasicBlock *BB)
bool formLCSSA(Loop &L, const DominatorTree &DT, const LoopInfo *LI, ScalarEvolution *SE)
Put loop into LCSSA form.
Pass * createLoopSimplifyPass()