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");
53 if (BI->isUnconditional())
55 if (BI->getSuccessor(0) == BI->getSuccessor(1))
56 return BI->getSuccessor(0);
60 return Cond->isZero() ? BI->getSuccessor(1) : BI->getSuccessor(0);
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 {
115 MemorySSAUpdater *MSSAU;
121 bool HasIrreducibleCFG =
false;
130 bool DeleteCurrentLoop =
false;
132 bool HasIndirectEntry =
false;
136 SmallPtrSet<BasicBlock *, 8> LiveLoopBlocks;
139 SmallVector<BasicBlock *, 8> DeadLoopBlocks;
142 SmallPtrSet<BasicBlock *, 8> LiveExitBlocks;
145 SmallVector<BasicBlock *, 8> DeadExitBlocks;
147 SmallPtrSet<BasicBlock *, 8> BlocksInLoopAfterFolding;
151 SmallVector<BasicBlock *, 8> FoldCandidates;
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,
160 const SmallVectorImpl<BasicBlock *> &S) {
161 dbgs() << Message <<
"\n";
162 for (
const BasicBlock *BB : S)
163 dbgs() <<
"\t" << BB->getName() <<
"\n";
165 auto PrintOutSet = [&](
const char *Message,
166 const SmallPtrSetImpl<BasicBlock *> &S) {
167 dbgs() << Message <<
"\n";
168 for (
const BasicBlock *BB : S)
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);
183 bool hasIrreducibleCFG(LoopBlocksDFS &DFS) {
184 assert(DFS.isComplete() &&
"DFS is expected to be finished");
186 DenseMap<const BasicBlock *, unsigned> RPO;
187 unsigned Current = 0;
188 for (
auto I = DFS.beginRPO(),
E = DFS.endRPO();
I !=
E; ++
I)
191 for (
auto I = DFS.beginRPO(),
E = DFS.endRPO();
I !=
E; ++
I) {
194 if (L.contains(Succ) && !LI.isLoopHeader(Succ) && RPO[BB] > RPO[Succ])
207 assert(DFS.isComplete() &&
"DFS is expected to be finished");
216 if (hasIrreducibleCFG(DFS)) {
217 HasIrreducibleCFG =
true;
224 if (!L.getLoopPreheader()) {
226 [&](BasicBlock *Pred) {
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());
236 for (
auto I = DFS.beginRPO(),
E = DFS.endRPO();
I !=
E; ++
I) {
240 if (!LiveLoopBlocks.count(BB)) {
241 DeadLoopBlocks.push_back(BB);
251 bool TakeFoldCandidate = TheOnlySucc && LI.getLoopFor(BB) == &L;
252 if (TakeFoldCandidate)
253 FoldCandidates.push_back(BB);
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?");
273 SmallVector<BasicBlock *, 8> ExitBlocks;
274 L.getExitBlocks(ExitBlocks);
275 SmallPtrSet<BasicBlock *, 8> UniqueDeadExits;
276 for (
auto *ExitBlock : ExitBlocks)
277 if (!LiveExitBlocks.count(ExitBlock) &&
278 UniqueDeadExits.
insert(ExitBlock).second &&
280 [
this](BasicBlock *Pred) {
return L.contains(Pred); }))
281 DeadExitBlocks.push_back(ExitBlock);
286 if (!LiveLoopBlocks.count(From))
289 return !TheOnlySucc || TheOnlySucc == To || LI.getLoopFor(From) != &L;
293 DeleteCurrentLoop = !IsEdgeLive(L.getLoopLatch(), L.getHeader());
297 if (DeleteCurrentLoop)
302 BlocksInLoopAfterFolding.insert(L.getLoopLatch());
309 return BlocksInLoopAfterFolding.count(Succ) && IsEdgeLive(BB, Succ);
312 for (
auto I = DFS.beginPostorder(),
E = DFS.endPostorder();
I !=
E; ++
I) {
314 if (BlockIsInLoop(BB))
315 BlocksInLoopAfterFolding.insert(BB);
318 assert(BlocksInLoopAfterFolding.count(L.getHeader()) &&
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())
371 SwitchInst *DummySwitch =
372 Builder.CreateSwitch(Builder.getInt32(0), NewPreheader);
375 unsigned DummyIdx = 1;
376 for (BasicBlock *BB : DeadExitBlocks) {
379 SmallVector<Instruction *, 4> DeadInstructions(
383 DeadInstructions.emplace_back(LandingPad);
385 for (Instruction *
I : DeadInstructions) {
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?");
398 if (Loop *OuterLoop = LI.getLoopFor(Preheader)) {
407 if (StillReachable != OuterLoop) {
408 LI.changeLoopFor(NewPreheader, StillReachable);
410 for (
auto *BB : L.blocks())
412 OuterLoop->removeChildLoop(&L);
416 LI.addTopLevelLoop(&L);
421 Loop *FixLCSSALoop = OuterLoop;
424 assert(FixLCSSALoop &&
"Should be a loop!");
427 MSSAU->applyUpdates(DTUpdates, DT,
true);
429 DTU.applyUpdates(DTUpdates);
432 SE.forgetBlockAndLoopDispositions();
438 MSSAU->applyUpdates(DTUpdates, DT,
true);
441 MSSAU->getMemorySSA()->verifyMemorySSA();
447 void deleteDeadLoopBlocks() {
449 SmallSetVector<BasicBlock *, 8> DeadLoopBlocksSet(DeadLoopBlocks.begin(),
450 DeadLoopBlocks.end());
451 MSSAU->removeBlocks(DeadLoopBlocksSet);
460 for (
auto *BB : DeadLoopBlocks)
461 if (LI.isLoopHeader(BB)) {
462 assert(LI.getLoopFor(BB) != &L &&
"Attempt to remove current loop!");
463 Loop *
DL = LI.getLoopFor(BB);
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);
469 LI.addTopLevelLoop(
DL);
474 for (
auto *BB : DeadLoopBlocks) {
475 assert(BB != L.getHeader() &&
476 "Header of the current loop cannot be dead!");
483 DTU.applyUpdates(DTUpdates);
485 for (
auto *BB : DeadLoopBlocks)
488 NumLoopBlocksDeleted += DeadLoopBlocks.size();
493 void foldTerminators() {
494 for (BasicBlock *BB : FoldCandidates) {
495 assert(LI.getLoopFor(BB) == &L &&
"Should be a loop block!");
497 assert(TheOnlySucc &&
"Should have one live successor!");
500 <<
" with an unconditional branch to the block "
501 << TheOnlySucc->
getName() <<
"\n");
503 SmallPtrSet<BasicBlock *, 2> DeadSuccessors;
505 unsigned TheOnlySuccDuplicates = 0;
507 if (Succ != TheOnlySucc) {
508 DeadSuccessors.
insert(Succ);
511 bool PreserveLCSSAPhi = !L.contains(Succ);
514 MSSAU->removeEdge(BB, 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)
526 MSSAU->removeDuplicatePhiEdgesBetween(BB, TheOnlySucc);
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;
542 ConstantTerminatorFoldingImpl(Loop &L, LoopInfo &LI, DominatorTree &DT,
544 MemorySSAUpdater *MSSAU)
545 : L(L), LI(LI), DT(DT), SE(SE), MSSAU(MSSAU), DFS(&L),
546 DTU(DT, DomTreeUpdater::UpdateStrategy::Eager) {}
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");
611 SE.forgetTopmostLoop(&L);
616 <<
" terminators in loop " << Header->getName() <<
"\n");
618 if (!DeadLoopBlocks.empty())
619 SE.forgetBlockAndLoopDispositions();
625 if (!DeadLoopBlocks.empty()) {
627 <<
" dead blocks in loop " << Header->getName() <<
"\n");
628 deleteDeadLoopBlocks();
631 DTU.applyUpdates(DTUpdates);
636 MSSAU->getMemorySSA()->verifyMemorySSA();
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!");
647 assert(DT.isReachableFromEntry(Header));
654 bool foldingBreaksCurrentLoop()
const {
655 return DeleteCurrentLoop;
665 bool &IsLoopDeleted) {
671 if (!L.getLoopLatch())
674 ConstantTerminatorFoldingImpl
BranchFolder(L, LI, DT, SE, MSSAU);
689 for (
auto &
Block : Blocks) {
697 if (!Pred || !Pred->getSingleSuccessor() || LI.
getLoopFor(Pred) != &L)
717 bool &IsLoopDeleted) {
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
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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)
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 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.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
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.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
Represents a single loop in the control flow graph.
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
An analysis that produces MemorySSA for a function.
MemorySSA * getMemorySSA() const
Get handle on MemorySSA.
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 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...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
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.
@ BasicBlock
Various leaf nodes.
initializer< Ty > init(const Ty &Val)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
friend class Instruction
Iterator for Instructions in a `BasicBlock.
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.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
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.
auto cast_or_null(const Y &Val)
AnalysisManager< Loop, LoopStandardAnalysisResults & > LoopAnalysisManager
The loop analysis manager.
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.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
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...