36#define DEBUG_TYPE "loop-simplifycfg"
42 "Number of terminators folded to unconditional branches");
44 "Number of loop blocks deleted");
46 "Number of loop exiting edges deleted");
54 if (BI->getSuccessor(0) == BI->getSuccessor(1))
55 return BI->getSuccessor(0);
59 return Cond->isZero() ? BI->getSuccessor(1) : BI->getSuccessor(0);
66 for (
auto Case :
SI->cases())
67 if (Case.getCaseValue() == CI)
68 return Case.getCaseSuccessor();
69 return SI->getDefaultDest();
77 Loop *LastLoop =
nullptr) {
79 "First loop is supposed to be inside of last loop!");
81 for (
Loop *Current = FirstLoop; Current != LastLoop;
83 Current->removeBlockFromLoop(BB);
90 Loop *Innermost =
nullptr;
93 while (BBL && !BBL->
contains(L.getHeader()))
108class ConstantTerminatorFoldingImpl {
114 MemorySSAUpdater *MSSAU;
120 bool HasIrreducibleCFG =
false;
129 bool DeleteCurrentLoop =
false;
131 bool HasIndirectEntry =
false;
135 SmallPtrSet<BasicBlock *, 8> LiveLoopBlocks;
138 SmallVector<BasicBlock *, 8> DeadLoopBlocks;
141 SmallPtrSet<BasicBlock *, 8> LiveExitBlocks;
144 SmallVector<BasicBlock *, 8> DeadExitBlocks;
146 SmallPtrSet<BasicBlock *, 8> BlocksInLoopAfterFolding;
150 SmallVector<BasicBlock *, 8> FoldCandidates;
153 dbgs() <<
"Constant terminator folding for loop " << L <<
"\n";
154 dbgs() <<
"After terminator constant-folding, the loop will";
155 if (!DeleteCurrentLoop)
157 dbgs() <<
" be destroyed\n";
158 auto PrintOutVector = [&](
const char *Message,
159 const SmallVectorImpl<BasicBlock *> &S) {
160 dbgs() << Message <<
"\n";
161 for (
const BasicBlock *BB : S)
162 dbgs() <<
"\t" << BB->getName() <<
"\n";
164 auto PrintOutSet = [&](
const char *Message,
165 const SmallPtrSetImpl<BasicBlock *> &S) {
166 dbgs() << Message <<
"\n";
167 for (
const BasicBlock *BB : S)
168 dbgs() <<
"\t" << BB->getName() <<
"\n";
170 PrintOutVector(
"Blocks in which we can constant-fold terminator:",
172 PrintOutSet(
"Live blocks from the original loop:", LiveLoopBlocks);
173 PrintOutVector(
"Dead blocks from the original loop:", DeadLoopBlocks);
174 PrintOutSet(
"Live exit blocks:", LiveExitBlocks);
175 PrintOutVector(
"Dead exit blocks:", DeadExitBlocks);
176 if (!DeleteCurrentLoop)
177 PrintOutSet(
"The following blocks will still be part of the loop:",
178 BlocksInLoopAfterFolding);
182 bool hasIrreducibleCFG(LoopBlocksDFS &DFS) {
183 assert(DFS.isComplete() &&
"DFS is expected to be finished");
185 DenseMap<const BasicBlock *, unsigned> RPO;
186 unsigned Current = 0;
187 for (
auto I = DFS.beginRPO(),
E = DFS.endRPO();
I !=
E; ++
I)
190 for (
auto I = DFS.beginRPO(),
E = DFS.endRPO();
I !=
E; ++
I) {
193 if (L.contains(Succ) && !LI.isLoopHeader(Succ) && RPO[BB] > RPO[Succ])
206 assert(DFS.isComplete() &&
"DFS is expected to be finished");
215 if (hasIrreducibleCFG(DFS)) {
216 HasIrreducibleCFG =
true;
223 if (!L.getLoopPreheader()) {
225 [&](BasicBlock *Pred) {
226 return isa<IndirectBrInst>(Pred->getTerminator());
228 "Loop should have preheader if it is not entered indirectly");
229 HasIndirectEntry =
true;
234 LiveLoopBlocks.insert(L.getHeader());
235 for (
auto I = DFS.beginRPO(),
E = DFS.endRPO();
I !=
E; ++
I) {
239 if (!LiveLoopBlocks.count(BB)) {
240 DeadLoopBlocks.push_back(BB);
250 bool TakeFoldCandidate = TheOnlySucc && LI.getLoopFor(BB) == &L;
251 if (TakeFoldCandidate)
252 FoldCandidates.push_back(BB);
256 if (!TakeFoldCandidate || TheOnlySucc == Succ) {
257 if (L.contains(Succ))
258 LiveLoopBlocks.insert(Succ);
260 LiveExitBlocks.insert(Succ);
266 assert(L.getNumBlocks() == LiveLoopBlocks.size() + DeadLoopBlocks.size() &&
267 "Malformed block sets?");
272 SmallVector<BasicBlock *, 8> ExitBlocks;
273 L.getExitBlocks(ExitBlocks);
274 SmallPtrSet<BasicBlock *, 8> UniqueDeadExits;
275 for (
auto *ExitBlock : ExitBlocks)
276 if (!LiveExitBlocks.count(ExitBlock) &&
277 UniqueDeadExits.
insert(ExitBlock).second &&
279 [
this](BasicBlock *Pred) {
return L.contains(Pred); }))
280 DeadExitBlocks.push_back(ExitBlock);
285 if (!LiveLoopBlocks.count(From))
288 return !TheOnlySucc || TheOnlySucc == To || LI.getLoopFor(From) != &L;
292 DeleteCurrentLoop = !IsEdgeLive(L.getLoopLatch(), L.getHeader());
296 if (DeleteCurrentLoop)
301 BlocksInLoopAfterFolding.insert(L.getLoopLatch());
308 return BlocksInLoopAfterFolding.count(Succ) && IsEdgeLive(BB, Succ);
311 for (
auto I = DFS.beginPostorder(),
E = DFS.endPostorder();
I !=
E; ++
I) {
313 if (BlockIsInLoop(BB))
314 BlocksInLoopAfterFolding.insert(BB);
317 assert(BlocksInLoopAfterFolding.count(L.getHeader()) &&
318 "Header not in loop?");
319 assert(BlocksInLoopAfterFolding.size() <= LiveLoopBlocks.size() &&
320 "All blocks that stay in loop should be live!");
358 void handleDeadExits() {
360 if (DeadExitBlocks.empty())
370 SwitchInst *DummySwitch =
371 Builder.CreateSwitch(Builder.getInt32(0), NewPreheader);
374 unsigned DummyIdx = 1;
375 for (BasicBlock *BB : DeadExitBlocks) {
378 SmallVector<Instruction *, 4> DeadInstructions(
382 DeadInstructions.emplace_back(LandingPad);
384 for (Instruction *
I : DeadInstructions) {
387 I->eraseFromParent();
390 assert(DummyIdx != 0 &&
"Too many dead exits!");
391 DummySwitch->
addCase(Builder.getInt32(DummyIdx++), BB);
392 DTUpdates.push_back({DominatorTree::Insert, Preheader, BB});
393 ++NumLoopExitsDeleted;
400 if (DummySwitch->
getParent()->getParent()->hasProfileData()) {
401 SmallVector<uint32_t> DummyBranchWeights(1 + DummySwitch->
getNumCases());
403 DummyBranchWeights[0] = 1;
407 assert(L.getLoopPreheader() == NewPreheader &&
"Malformed CFG?");
408 if (Loop *OuterLoop = LI.getLoopFor(Preheader)) {
417 if (StillReachable != OuterLoop) {
418 LI.changeLoopFor(NewPreheader, StillReachable);
420 for (
auto *BB : L.blocks())
422 OuterLoop->removeChildLoop(&L);
426 LI.addTopLevelLoop(&L);
431 Loop *FixLCSSALoop = OuterLoop;
434 assert(FixLCSSALoop &&
"Should be a loop!");
437 MSSAU->applyUpdates(DTUpdates, DT,
true);
439 DTU.applyUpdates(DTUpdates);
442 SE.forgetBlockAndLoopDispositions();
448 MSSAU->applyUpdates(DTUpdates, DT,
true);
451 MSSAU->getMemorySSA()->verifyMemorySSA();
457 void deleteDeadLoopBlocks() {
459 SmallSetVector<BasicBlock *, 8> DeadLoopBlocksSet(DeadLoopBlocks.begin(),
460 DeadLoopBlocks.end());
461 MSSAU->removeBlocks(DeadLoopBlocksSet);
470 for (
auto *BB : DeadLoopBlocks)
471 if (LI.isLoopHeader(BB)) {
472 assert(LI.getLoopFor(BB) != &L &&
"Attempt to remove current loop!");
473 Loop *
DL = LI.getLoopFor(BB);
474 if (!
DL->isOutermost()) {
475 for (
auto *PL =
DL->getParentLoop(); PL; PL =
PL->getParentLoop())
476 for (
auto *BB :
DL->getBlocks())
477 PL->removeBlockFromLoop(BB);
478 DL->getParentLoop()->removeChildLoop(
DL);
479 LI.addTopLevelLoop(
DL);
484 for (
auto *BB : DeadLoopBlocks) {
485 assert(BB != L.getHeader() &&
486 "Header of the current loop cannot be dead!");
493 DTU.applyUpdates(DTUpdates);
495 for (
auto *BB : DeadLoopBlocks)
498 NumLoopBlocksDeleted += DeadLoopBlocks.size();
503 void foldTerminators() {
504 for (BasicBlock *BB : FoldCandidates) {
505 assert(LI.getLoopFor(BB) == &L &&
"Should be a loop block!");
507 assert(TheOnlySucc &&
"Should have one live successor!");
510 <<
" with an unconditional branch to the block "
511 << TheOnlySucc->
getName() <<
"\n");
513 SmallPtrSet<BasicBlock *, 2> DeadSuccessors;
515 unsigned TheOnlySuccDuplicates = 0;
517 if (Succ != TheOnlySucc) {
518 DeadSuccessors.
insert(Succ);
521 bool PreserveLCSSAPhi = !L.contains(Succ);
524 MSSAU->removeEdge(BB, Succ);
526 ++TheOnlySuccDuplicates;
528 assert(TheOnlySuccDuplicates > 0 &&
"Should be!");
532 bool PreserveLCSSAPhi = !L.contains(TheOnlySucc);
533 for (
unsigned Dup = 1; Dup < TheOnlySuccDuplicates; ++Dup)
535 if (MSSAU && TheOnlySuccDuplicates > 1)
536 MSSAU->removeDuplicatePhiEdgesBetween(BB, TheOnlySucc);
540 Builder.SetInsertPoint(Term);
541 Builder.CreateBr(TheOnlySucc);
542 Term->eraseFromParent();
544 for (
auto *DeadSucc : DeadSuccessors)
545 DTUpdates.push_back({DominatorTree::Delete, BB, DeadSucc});
547 ++NumTerminatorsFolded;
552 ConstantTerminatorFoldingImpl(Loop &L, LoopInfo &LI, DominatorTree &DT,
554 MemorySSAUpdater *MSSAU)
555 : L(L), LI(LI), DT(DT), SE(SE), MSSAU(MSSAU), DFS(&L),
556 DTU(DT, DomTreeUpdater::UpdateStrategy::Eager) {}
558 assert(L.getLoopLatch() &&
"Should be single latch!");
566 LLVM_DEBUG(
dbgs() <<
"In function " << Header->getParent()->getName()
569 if (HasIrreducibleCFG) {
570 LLVM_DEBUG(
dbgs() <<
"Loops with irreducible CFG are not supported!\n");
574 if (HasIndirectEntry) {
575 LLVM_DEBUG(
dbgs() <<
"Loops which can be entered indirectly are not"
581 if (FoldCandidates.empty()) {
583 dbgs() <<
"No constant terminator folding candidates found in loop "
584 << Header->getName() <<
"\n");
589 if (DeleteCurrentLoop) {
592 <<
"Give up constant terminator folding in loop " << Header->getName()
593 <<
": we don't currently support deletion of the current loop.\n");
599 if (BlocksInLoopAfterFolding.size() + DeadLoopBlocks.size() !=
602 dbgs() <<
"Give up constant terminator folding in loop "
603 << Header->getName() <<
": we don't currently"
604 " support blocks that are not dead, but will stop "
605 "being a part of the loop after constant-folding.\n");
612 if (!DeadExitBlocks.empty() && !L.isLCSSAForm(DT,
false)) {
613 assert(L.isLCSSAForm(DT,
true) &&
614 "LCSSA broken not by tokens?");
615 LLVM_DEBUG(
dbgs() <<
"Give up constant terminator folding in loop "
617 <<
": tokens uses potentially break LCSSA form.\n");
621 SE.forgetTopmostLoop(&L);
626 <<
" terminators in loop " << Header->getName() <<
"\n");
628 if (!DeadLoopBlocks.empty())
629 SE.forgetBlockAndLoopDispositions();
635 if (!DeadLoopBlocks.empty()) {
637 <<
" dead blocks in loop " << Header->getName() <<
"\n");
638 deleteDeadLoopBlocks();
641 DTU.applyUpdates(DTUpdates);
646 MSSAU->getMemorySSA()->verifyMemorySSA();
650#if defined(EXPENSIVE_CHECKS)
651 assert(DT.verify(DominatorTree::VerificationLevel::Full) &&
652 "DT broken after transform!");
654 assert(DT.verify(DominatorTree::VerificationLevel::Fast) &&
655 "DT broken after transform!");
657 assert(DT.isReachableFromEntry(Header));
664 bool foldingBreaksCurrentLoop()
const {
665 return DeleteCurrentLoop;
675 bool &IsLoopDeleted) {
681 if (!L.getLoopLatch())
684 ConstantTerminatorFoldingImpl
BranchFolder(L, LI, DT, SE, MSSAU);
699 for (
auto &
Block : Blocks) {
707 if (!Pred || !Pred->getSingleSuccessor() || LI.
getLoopFor(Pred) != &L)
727 bool &IsLoopDeleted) {
748 std::optional<MemorySSAUpdater> MSSAU;
751 bool DeleteCurrentLoop =
false;
756 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...
This file contains the declarations for profiling metadata utility functions.
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 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.
unsigned getNumCases() const
Return the number of 'cases' in this switch instruction, excluding the default case.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
const ParentTy * getParent() const
@ 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)
LLVM_ABI void setBranchWeights(Instruction &I, ArrayRef< uint32_t > Weights, bool IsExpected, bool ElideAllZero=false)
Create a new branch_weights metadata node and add or overwrite a prof metadata reference to instructi...
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="")
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...