73#define DEBUG_TYPE "loop-simplify"
75STATISTIC(NumNested ,
"Number of nested loops split out");
97 if (++BBI != NewBB->
getParent()->
end() && L->contains(&*BBI)) {
107 FoundBB = SplitPreds[0];
117 bool PreserveLCSSA) {
123 if (!L->contains(
P)) {
127 if (isa<IndirectBrInst>(
P->getTerminator()))
138 LI, MSSAU, PreserveLCSSA);
143 << PreheaderBB->
getName() <<
"\n");
161 if (
Blocks.insert(BB).second && BB != StopBlock)
165 }
while (!Worklist.
empty());
231 for (
auto *BB : L->blocks()) {
232 for (
auto &
II : *BB) {
233 if (
auto CI = dyn_cast<CallBase>(&
II)) {
234 if (CI->isConvergent()) {
243 assert(!Header->isEHPad() &&
"Can't insert backedge to EH pad");
246 if (!PN)
return nullptr;
261 LLVM_DEBUG(
dbgs() <<
"LoopSimplify: Splitting out a new outer loop\n");
270 DT, LI, MSSAU, PreserveLCSSA);
280 if (
Loop *Parent = L->getParentLoop())
293 L->moveToHeader(Header);
305 const std::vector<Loop*> &SubLoops = L->getSubLoops();
306 for (
size_t I = 0;
I != SubLoops.size(); )
307 if (BlocksInL.
count(SubLoops[
I]->getHeader()))
310 NewOuter->
addChildLoop(L->removeChildLoop(SubLoops.begin() +
I));
316 for (
unsigned i = 0; i != L->getBlocks().
size(); ++i) {
318 if (!BlocksInL.
count(BB)) {
320 L->removeBlockFromLoop(BB);
321 if ((*LI)[BB] == L) {
343 "LCSSA is broken after separating nested loops!");
358 assert(L->getNumBackEdges() > 1 &&
"Must have > 1 backedge!");
369 assert(!Header->isEHPad() &&
"Can't insert backedge to EH pad");
372 std::vector<BasicBlock*> BackedgeBlocks;
375 if (isa<IndirectBrInst>(
P->getTerminator()))
378 if (
P != Preheader) BackedgeBlocks.push_back(
P);
383 Header->getName() +
".backedge",
F);
385 BETerminator->
setDebugLoc(Header->getFirstNonPHI()->getDebugLoc());
387 LLVM_DEBUG(
dbgs() <<
"LoopSimplify: Inserting unique backedge block "
388 << BEBlock->
getName() <<
"\n");
403 unsigned PreheaderIdx = ~0U;
404 bool HasUniqueIncomingValue =
true;
405 Value *UniqueValue =
nullptr;
409 if (IBB == Preheader) {
413 if (HasUniqueIncomingValue) {
416 else if (UniqueValue !=
IV)
417 HasUniqueIncomingValue =
false;
423 assert(PreheaderIdx != ~0U &&
"PHI has no preheader entry??");
424 if (PreheaderIdx != 0) {
438 if (HasUniqueIncomingValue) {
462 L->addBasicBlockToLoop(BEBlock, *LI);
479 bool Changed =
false;
490 if (BB == L->getHeader())
501 LLVM_DEBUG(
dbgs() <<
"LoopSimplify: Deleting edge from dead predecessor "
502 <<
P->getName() <<
"\n");
519 L->getExitingBlocks(ExitingBlocks);
520 for (
BasicBlock *ExitingBlock : ExitingBlocks)
521 if (
BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator()))
522 if (BI->isConditional()) {
523 if (
UndefValue *
Cond = dyn_cast<UndefValue>(BI->getCondition())) {
526 <<
"LoopSimplify: Resolving \"br i1 undef\" to exit in "
527 << ExitingBlock->getName() <<
"\n");
529 BI->setCondition(ConstantInt::get(
Cond->getType(),
530 !L->contains(BI->getSuccessor(0))));
537 BasicBlock *Preheader = L->getLoopPreheader();
561 if (L->getNumBackEdges() < 8) {
563 PreserveLCSSA, AC, MSSAU)) {
595 (PN = dyn_cast<PHINode>(
I++)); )
614 auto HasUniqueExitBlock = [&]() {
616 for (
auto *ExitingBB : ExitingBlocks)
618 if (L->contains(SuccBB))
623 else if (UniqueExit != SuccBB)
629 if (HasUniqueExitBlock()) {
630 for (
BasicBlock *ExitingBlock : ExitingBlocks) {
631 if (!ExitingBlock->getSinglePredecessor())
continue;
632 BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
635 if (!CI || CI->
getParent() != ExitingBlock)
continue;
639 bool AllInvariant =
true;
640 bool AnyInvariant =
false;
641 for (
auto I = ExitingBlock->instructionsWithoutDebug().begin(); &*
I != BI; ) {
645 if (!L->makeLoopInvariant(
647 Preheader ? Preheader->
getTerminator() :
nullptr, MSSAU, SE)) {
648 AllInvariant =
false;
654 if (!AllInvariant)
continue;
665 << ExitingBlock->getName() <<
"\n");
672 while (!
Node->isLeaf()) {
679 ExitBlockSet.
insert(ExitingBlock);
684 ExitingBlock, PreserveLCSSA);
686 ExitingBlock, PreserveLCSSA);
687 ExitingBlock->eraseFromParent();
700 bool Changed =
false;
706 assert(DT &&
"DT not available.");
707 assert(LI &&
"LI not available.");
708 assert(L->isRecursivelyLCSSAForm(*DT, *LI) &&
709 "Requested to preserve LCSSA, but it's already broken.");
725 while (!Worklist.
empty())
727 AC, MSSAU, PreserveLCSSA);
774char LoopSimplify::ID = 0;
776 "Canonicalize natural loops",
false,
false)
790bool LoopSimplify::runOnFunction(
Function &
F) {
791 bool Changed =
false;
792 LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
793 DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
794 auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
797 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
F);
799 std::unique_ptr<MemorySSAUpdater> MSSAU;
800 auto *MSSAAnalysis = getAnalysisIfAvailable<MemorySSAWrapperPass>();
802 MSSA = &MSSAAnalysis->getMSSA();
803 MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
806 bool PreserveLCSSA = mustPreserveAnalysisID(
LCSSAID);
810 Changed |=
simplifyLoop(L, DT, LI, SE, AC, MSSAU.get(), PreserveLCSSA);
815 *LI, [&](
Loop *L) {
return L->isRecursivelyLCSSAForm(*DT, *LI); });
816 assert(InLCSSA &&
"LCSSA is broken after loop-simplify.");
824 bool Changed =
false;
830 std::unique_ptr<MemorySSAUpdater> MSSAU;
832 auto *MSSA = &MSSAAnalysis->getMSSA();
833 MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
864static void verifyLoop(
Loop *L) {
875 if (!L->getLoopPreheader() || !L->getLoopLatch()) {
876 bool HasIndBrPred =
false;
878 if (isa<IndirectBrInst>(Pred->getTerminator())) {
883 "LoopSimplify has no excuse for missing loop header info!");
888 if (!
L->hasDedicatedExits()) {
889 bool HasIndBrExiting =
false;
891 L->getExitingBlocks(ExitingBlocks);
892 for (
unsigned i = 0, e = ExitingBlocks.
size(); i !=
e; ++i) {
893 if (isa<IndirectBrInst>((ExitingBlocks[i])->getTerminator())) {
894 HasIndBrExiting =
true;
900 "LoopSimplify has no excuse for missing exit block info!");
901 (void)HasIndBrExiting;
906void 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...
Module.h This file contains the declarations for the Module class.
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...
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.
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()