35#define DEBUG_TYPE "loop-simplifycfg"
41 "Number of terminators folded to unconditional branches");
43 "Number of loop blocks deleted");
45 "Number of loop exiting edges deleted");
52 if (
BranchInst *BI = dyn_cast<BranchInst>(TI)) {
53 if (BI->isUnconditional())
55 if (BI->getSuccessor(0) == BI->getSuccessor(1))
60 return Cond->isZero() ? BI->getSuccessor(1) : BI->getSuccessor(0);
63 if (
SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
64 auto *CI = dyn_cast<ConstantInt>(SI->getCondition());
67 for (
auto Case : SI->cases())
68 if (Case.getCaseValue() == CI)
69 return Case.getCaseSuccessor();
70 return SI->getDefaultDest();
78 Loop *LastLoop =
nullptr) {
80 "First loop is supposed to be inside of last loop!");
82 for (
Loop *Current = FirstLoop; Current != LastLoop;
84 Current->removeBlockFromLoop(BB);
91 Loop *Innermost =
nullptr;
94 while (BBL && !BBL->
contains(L.getHeader()))
109class ConstantTerminatorFoldingImpl {
121 bool HasIrreducibleCFG =
false;
130 bool DeleteCurrentLoop =
false;
132 bool HasIndirectEntry =
false;
154 dbgs() <<
"Constant terminator folding for loop " <<
L <<
"\n";
155 dbgs() <<
"After terminator constant-folding, the loop will";
156 if (!DeleteCurrentLoop)
158 dbgs() <<
" be destroyed\n";
159 auto PrintOutVector = [&](
const char *Message,
161 dbgs() << Message <<
"\n";
163 dbgs() <<
"\t" << BB->getName() <<
"\n";
165 auto PrintOutSet = [&](
const char *Message,
167 dbgs() << Message <<
"\n";
169 dbgs() <<
"\t" << BB->getName() <<
"\n";
171 PrintOutVector(
"Blocks in which we can constant-fold terminator:",
173 PrintOutSet(
"Live blocks from the original loop:", LiveLoopBlocks);
174 PrintOutVector(
"Dead blocks from the original loop:", DeadLoopBlocks);
175 PrintOutSet(
"Live exit blocks:", LiveExitBlocks);
176 PrintOutVector(
"Dead exit blocks:", DeadExitBlocks);
177 if (!DeleteCurrentLoop)
178 PrintOutSet(
"The following blocks will still be part of the loop:",
179 BlocksInLoopAfterFolding);
187 unsigned Current = 0;
194 if (
L.contains(Succ) && !LI.
isLoopHeader(Succ) && RPO[BB] > RPO[Succ])
216 if (hasIrreducibleCFG(DFS)) {
217 HasIrreducibleCFG =
true;
224 if (!
L.getLoopPreheader()) {
227 return isa<IndirectBrInst>(Pred->getTerminator());
229 "Loop should have preheader if it is not entered indirectly");
230 HasIndirectEntry =
true;
235 LiveLoopBlocks.
insert(
L.getHeader());
240 if (!LiveLoopBlocks.
count(BB)) {
251 bool TakeFoldCandidate = TheOnlySucc && LI.
getLoopFor(BB) == &
L;
252 if (TakeFoldCandidate)
257 if (!TakeFoldCandidate || TheOnlySucc == Succ) {
258 if (
L.contains(Succ))
259 LiveLoopBlocks.
insert(Succ);
261 LiveExitBlocks.
insert(Succ);
267 assert(
L.getNumBlocks() == LiveLoopBlocks.
size() + DeadLoopBlocks.
size() &&
268 "Malformed block sets?");
274 L.getExitBlocks(ExitBlocks);
276 for (
auto *ExitBlock : ExitBlocks)
277 if (!LiveExitBlocks.
count(ExitBlock) &&
278 UniqueDeadExits.
insert(ExitBlock).second &&
280 [
this](
BasicBlock *Pred) {
return L.contains(Pred); }))
293 DeleteCurrentLoop = !IsEdgeLive(
L.getLoopLatch(),
L.getHeader());
297 if (DeleteCurrentLoop)
302 BlocksInLoopAfterFolding.
insert(
L.getLoopLatch());
309 return BlocksInLoopAfterFolding.
count(Succ) && IsEdgeLive(BB, Succ);
314 if (BlockIsInLoop(BB))
315 BlocksInLoopAfterFolding.
insert(BB);
319 "Header not in loop?");
320 assert(BlocksInLoopAfterFolding.
size() <= LiveLoopBlocks.
size() &&
321 "All blocks that stay in loop should be live!");
359 void handleDeadExits() {
361 if (DeadExitBlocks.
empty())
372 Builder.CreateSwitch(Builder.getInt32(0), NewPreheader);
375 unsigned DummyIdx = 1;
383 DeadInstructions.emplace_back(LandingPad);
388 I->eraseFromParent();
391 assert(DummyIdx != 0 &&
"Too many dead exits!");
392 DummySwitch->
addCase(Builder.getInt32(DummyIdx++), BB);
393 DTUpdates.
push_back({DominatorTree::Insert, Preheader, BB});
394 ++NumLoopExitsDeleted;
397 assert(
L.getLoopPreheader() == NewPreheader &&
"Malformed CFG?");
407 if (StillReachable != OuterLoop) {
410 for (
auto *BB :
L.blocks())
412 OuterLoop->removeChildLoop(&L);
421 Loop *FixLCSSALoop = OuterLoop;
424 assert(FixLCSSALoop &&
"Should be a loop!");
447 void deleteDeadLoopBlocks() {
450 DeadLoopBlocks.
end());
460 for (
auto *BB : DeadLoopBlocks)
464 if (!
DL->isOutermost()) {
465 for (
auto *PL =
DL->getParentLoop(); PL; PL =
PL->getParentLoop())
466 for (
auto *BB :
DL->getBlocks())
467 PL->removeBlockFromLoop(BB);
468 DL->getParentLoop()->removeChildLoop(
DL);
474 for (
auto *BB : DeadLoopBlocks) {
476 "Header of the current loop cannot be dead!");
485 for (
auto *BB : DeadLoopBlocks)
488 NumLoopBlocksDeleted += DeadLoopBlocks.size();
493 void foldTerminators() {
497 assert(TheOnlySucc &&
"Should have one live successor!");
500 <<
" with an unconditional branch to the block "
501 << TheOnlySucc->
getName() <<
"\n");
505 unsigned TheOnlySuccDuplicates = 0;
507 if (Succ != TheOnlySucc) {
508 DeadSuccessors.
insert(Succ);
511 bool PreserveLCSSAPhi = !
L.contains(Succ);
516 ++TheOnlySuccDuplicates;
518 assert(TheOnlySuccDuplicates > 0 &&
"Should be!");
522 bool PreserveLCSSAPhi = !
L.contains(TheOnlySucc);
523 for (
unsigned Dup = 1; Dup < TheOnlySuccDuplicates; ++Dup)
525 if (MSSAU && TheOnlySuccDuplicates > 1)
530 Builder.SetInsertPoint(Term);
531 Builder.CreateBr(TheOnlySucc);
532 Term->eraseFromParent();
534 for (
auto *DeadSucc : DeadSuccessors)
535 DTUpdates.
push_back({DominatorTree::Delete, BB, DeadSucc});
537 ++NumTerminatorsFolded;
545 :
L(
L), LI(LI), DT(DT), SE(SE), MSSAU(MSSAU), DFS(&
L),
548 assert(
L.getLoopLatch() &&
"Should be single latch!");
556 LLVM_DEBUG(
dbgs() <<
"In function " << Header->getParent()->getName()
559 if (HasIrreducibleCFG) {
560 LLVM_DEBUG(
dbgs() <<
"Loops with irreducible CFG are not supported!\n");
564 if (HasIndirectEntry) {
565 LLVM_DEBUG(
dbgs() <<
"Loops which can be entered indirectly are not"
571 if (FoldCandidates.empty()) {
573 dbgs() <<
"No constant terminator folding candidates found in loop "
574 << Header->getName() <<
"\n");
579 if (DeleteCurrentLoop) {
582 <<
"Give up constant terminator folding in loop " << Header->getName()
583 <<
": we don't currently support deletion of the current loop.\n");
589 if (BlocksInLoopAfterFolding.
size() + DeadLoopBlocks.size() !=
592 dbgs() <<
"Give up constant terminator folding in loop "
593 << Header->getName() <<
": we don't currently"
594 " support blocks that are not dead, but will stop "
595 "being a part of the loop after constant-folding.\n");
602 if (!DeadExitBlocks.empty() && !
L.isLCSSAForm(DT,
false)) {
603 assert(
L.isLCSSAForm(DT,
true) &&
604 "LCSSA broken not by tokens?");
605 LLVM_DEBUG(
dbgs() <<
"Give up constant terminator folding in loop "
607 <<
": tokens uses potentially break LCSSA form.\n");
616 <<
" terminators in loop " << Header->getName() <<
"\n");
618 if (!DeadLoopBlocks.empty())
625 if (!DeadLoopBlocks.empty()) {
627 <<
" dead blocks in loop " << Header->getName() <<
"\n");
628 deleteDeadLoopBlocks();
640#if defined(EXPENSIVE_CHECKS)
641 assert(DT.
verify(DominatorTree::VerificationLevel::Full) &&
642 "DT broken after transform!");
644 assert(DT.
verify(DominatorTree::VerificationLevel::Fast) &&
645 "DT broken after transform!");
654 bool foldingBreaksCurrentLoop()
const {
655 return DeleteCurrentLoop;
665 bool &IsLoopDeleted) {
671 if (!L.getLoopLatch())
674 ConstantTerminatorFoldingImpl
BranchFolder(L, LI, DT, SE, MSSAU);
676 IsLoopDeleted = Changed &&
BranchFolder.foldingBreaksCurrentLoop();
683 bool Changed =
false;
717 bool &IsLoopDeleted) {
718 bool Changed =
false;
738 std::optional<MemorySSAUpdater> MSSAU;
741 bool DeleteCurrentLoop =
false;
746 if (DeleteCurrentLoop)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
BlockVerifier::State From
DenseMap< Block *, BlockRelaxAux > Blocks
This header provides classes for managing a pipeline of passes over loops in LLVM IR.
static BasicBlock * getOnlyLiveSuccessor(BasicBlock *BB)
If BB is a switch or a conditional branch, but only one of its successors can be reached from this bl...
static bool constantFoldTerminators(Loop &L, DominatorTree &DT, LoopInfo &LI, ScalarEvolution &SE, MemorySSAUpdater *MSSAU, bool &IsLoopDeleted)
Turn branches and switches with known constant conditions into unconditional branches.
static Loop * getInnermostLoopFor(SmallPtrSetImpl< BasicBlock * > &BBs, Loop &L, LoopInfo &LI)
Find innermost loop that contains at least one block from BBs and contains the header of loop L.
static bool mergeBlocksIntoPredecessors(Loop &L, DominatorTree &DT, LoopInfo &LI, MemorySSAUpdater *MSSAU, ScalarEvolution &SE)
static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI, ScalarEvolution &SE, MemorySSAUpdater *MSSAU, bool &IsLoopDeleted)
static cl::opt< bool > EnableTermFolding("enable-loop-simplifycfg-term-folding", cl::init(true))
static void removeBlockFromLoops(BasicBlock *BB, Loop *FirstLoop, Loop *LastLoop=nullptr)
Removes BB from all loops from [FirstLoop, LastLoop) in parent chain.
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
const SmallVectorImpl< MachineOperand > & Cond
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)
A container for analyses that lazily runs them and caches their results.
LLVM Basic Block Representation.
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
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...
LLVM_ABI void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)
Update PHI nodes in this BasicBlock before removal of predecessor Pred.
Conditional or Unconditional Branch instruction.
This is the shared class of boolean and integer constants.
LLVM_ABI void deleteBB(BasicBlock *DelBB)
Delete DelBB.
bool verify(VerificationLevel VL=VerificationLevel::Full) const
verify - checks if the tree is correct.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
LLVM_ABI bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
void applyUpdates(ArrayRef< UpdateT > Updates)
Submit updates to all available trees.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
void markLoopAsDeleted(Loop &L, llvm::StringRef Name)
Loop passes should use this method to indicate they have deleted a loop from the nest.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
BlockT * getHeader() const
unsigned getLoopDepth() const
Return the nesting level of this loop.
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
Store the result of a depth first search within basic blocks contained by a single loop.
RPOIterator beginRPO() const
Reverse iterate over the cached postorder blocks.
bool isComplete() const
Return true if postorder numbers are assigned to all loop blocks.
POIterator beginPostorder() const
Iterate over the cached postorder blocks.
POIterator endPostorder() const
void perform(const LoopInfo *LI)
Traverse the loop blocks and store the DFS result.
RPOIterator endRPO() const
void verify(const DominatorTreeBase< BlockT, false > &DomTree) const
void addTopLevelLoop(LoopT *New)
This adds the specified loop to the collection of top-level loops.
void removeBlock(BlockT *BB)
This method completely removes BB from all data structures, including all of the Loop objects it is n...
bool isLoopHeader(const BlockT *BB) const
void changeLoopFor(BlockT *BB, LoopT *L)
Change the top-level loop that contains BB to the specified loop.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
LLVM_ABI void erase(Loop *L)
Update LoopInfo after removing the last backedge from a loop.
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
Represents a single loop in the control flow graph.
An analysis that produces MemorySSA for a function.
MemorySSA * getMemorySSA() const
Get handle on MemorySSA.
LLVM_ABI void removeEdge(BasicBlock *From, BasicBlock *To)
Update the MemoryPhi in To following an edge deletion between From and To.
LLVM_ABI void removeDuplicatePhiEdgesBetween(const BasicBlock *From, const BasicBlock *To)
Update the MemoryPhi in To to have a single incoming edge from From, following a CFG change that repl...
LLVM_ABI void removeBlocks(const SmallSetVector< BasicBlock *, 8 > &DeadBlocks)
Remove all MemoryAcceses in a set of BasicBlocks about to be deleted.
LLVM_ABI void applyUpdates(ArrayRef< CFGUpdate > Updates, DominatorTree &DT, bool UpdateDTFirst=false)
Apply CFG updates, analogous with the DT edge updates.
LLVM_ABI void verifyMemorySSA(VerificationLevel=VerificationLevel::Fast) const
Verify that MemorySSA is self consistent (IE definitions dominate all uses, uses appear in the right ...
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
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.
The main scalar evolution driver.
LLVM_ABI void forgetTopmostLoop(const Loop *L)
LLVM_ABI 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...
LLVM_ABI void forgetBlockAndLoopDispositions(Value *V=nullptr)
Called when the client has changed the disposition of values in a loop or block.
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 push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
LLVM_ABI void addCase(ConstantInt *OnVal, BasicBlock *Dest)
Add an entry to the switch instruction.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
initializer< Ty > init(const Ty &Val)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI void detachDeadBlocks(ArrayRef< BasicBlock * > BBs, SmallVectorImpl< DominatorTree::UpdateType > *Updates, bool KeepOneInputPHIs=false)
Replace contents of every block in BBs with single unreachable instruction.
auto successors(const MachineBasicBlock *BB)
LLVM_ABI bool formLCSSARecursively(Loop &L, const DominatorTree &DT, const LoopInfo *LI, ScalarEvolution *SE)
Put a loop nest into LCSSA form.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI bool VerifyMemorySSA
Enables verification of MemorySSA.
LLVM_ABI bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, MemoryDependenceResults *MemDep=nullptr, bool PredecessorWithTwoSuccessors=false, DominatorTree *DT=nullptr)
Attempts to merge a block into its predecessor, if possible.
LLVM_ABI BasicBlock * SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="", bool Before=false)
Split the specified block at the specified instruction.
LLVM_ABI PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
auto predecessors(const MachineBasicBlock *BB)
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...