38 #define DEBUG_TYPE "break-crit-edges" 40 STATISTIC(NumBroken,
"Number of blocks inserted");
50 auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
51 auto *DT = DTWP ? &DTWP->getDomTree() :
nullptr;
52 auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
53 auto *LI = LIWP ? &LIWP->getLoopInfo() :
nullptr;
72 "Break critical edges in CFG",
false,
false)
77 return new BreakCriticalEdges();
107 SplitBB->
isLandingPad()) &&
"SplitBB has non-PHI nodes!");
111 unsigned Idx = PN.getBasicBlockIndex(SplitBB);
112 Value *V = PN.getIncomingValue(Idx);
116 if (
const PHINode *VP = dyn_cast<PHINode>(V))
117 if (VP->getParent() == SplitBB)
122 PN.getType(), Preds.
size(),
"split",
124 for (
unsigned i = 0, e = Preds.
size(); i != e; ++i)
128 PN.setIncomingValue(Idx, NewPN);
138 assert(!isa<IndirectBrInst>(TI) &&
139 "Cannot split critical edge from IndirectBrInst");
146 if (DestBB->
isEHPad())
return nullptr;
200 auto *DT = Options.
DT;
201 auto *LI = Options.
LI;
202 auto *MSSAU = Options.
MSSAU;
226 DT->applyUpdates(Updates);
231 if (
Loop *TIL = LI->getLoopFor(TIBB)) {
234 if (
Loop *DestLoop = LI->getLoopFor(DestBB)) {
235 if (TIL == DestLoop) {
237 DestLoop->addBasicBlockToLoop(NewBB, *LI);
238 }
else if (TIL->contains(DestLoop)) {
240 TIL->addBasicBlockToLoop(NewBB, *LI);
241 }
else if (DestLoop->contains(TIL)) {
243 DestLoop->addBasicBlockToLoop(NewBB, *LI);
249 assert(DestLoop->getHeader() == DestBB &&
250 "Should not create irreducible loops!");
251 if (
Loop *
P = DestLoop->getParentLoop())
252 P->addBasicBlockToLoop(NewBB, *LI);
258 if (!TIL->contains(DestBB)) {
259 assert(!TIL->contains(NewBB) &&
260 "Split point for loop exit is contained in loop!");
282 if (LI->getLoopFor(P) != TIL) {
289 if (!LoopPreds.
empty()) {
290 assert(!DestBB->
isEHPad() &&
"We don't split edges to EH pads!");
292 DestBB, LoopPreds,
"split", DT, LI, MSSAU, Options.
PreserveLCSSA);
323 case Instruction::IndirectBr:
328 case Instruction::Br:
329 case Instruction::Switch:
352 for (
unsigned Succ = 0,
E = IBI->getNumSuccessors(); Succ !=
E; ++Succ)
353 Targets.
insert(IBI->getSuccessor(Succ));
359 bool ShouldUpdateAnalysis = BPI &&
BFI;
360 bool Changed =
false;
366 if (!IBRPred || OtherPreds.
empty())
375 if (ShouldUpdateAnalysis) {
381 BFI->setBlockFreq(BodyBlock, BFI->getBlockFreq(
Target).getFrequency());
400 if (ShouldUpdateAnalysis)
401 BlockFreqForDirectSucc += BFI->getBlockFreq(Src) *
404 if (ShouldUpdateAnalysis) {
405 BFI->setBlockFreq(DirectSucc, BlockFreqForDirectSucc.
getFrequency());
407 BFI->getBlockFreq(
Target) - BlockFreqForDirectSucc;
418 End =
Target->getFirstNonPHI()->getIterator();
423 "Block was expected to only contain PHIs");
425 while (Indirect != End) {
426 PHINode *DirPHI = cast<PHINode>(Direct);
427 PHINode *IndPHI = cast<PHINode>(Indirect);
439 NewIndPHI->
addIncoming(IndPHI->getIncomingValueForBlock(IBRPred),
449 IndPHI->replaceAllUsesWith(MergePHI);
450 IndPHI->eraseFromParent();
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs=false)
Notify the BasicBlock that the predecessor Pred is no longer able to reach it.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
BasicBlock * getSuccessor(unsigned Idx) const
Return the specified successor. This instruction must be a terminator.
void push_back(const T &Elt)
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
LLVMContext & getContext() const
All values hold a context through their type.
STATISTIC(NumFunctions, "Total number of functions")
Analysis pass which computes a DominatorTree.
unsigned SplitAllCriticalEdges(Function &F, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
Loop over all of the edges in the CFG, breaking critical edges as they are found. ...
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 setSuccessor(unsigned Idx, BasicBlock *BB)
Update the specified successor to point at the provided block.
void initializeBreakCriticalEdgesPass(PassRegistry &)
static BasicBlock * findIBRPredecessor(BasicBlock *BB, SmallVectorImpl< BasicBlock *> &OtherPreds)
iterator begin()
Instruction iterator methods.
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
Remove an incoming value.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Option class for critical edge splitting.
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
static constexpr UpdateKind Delete
Analysis pass that exposes the LoopInfo for a function.
bool insert(const value_type &X)
Insert a new element into the SetVector.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
BasicBlock * SplitCriticalEdge(Instruction *TI, unsigned SuccNum, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
If this edge is a critical edge, insert a new node to split the critical edge.
AnalysisUsage & addPreservedID(const void *ID)
unsigned getNumSuccessors() const
Return the number of successors that this instruction has.
Interval::succ_iterator succ_end(Interval *I)
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
static bool runOnFunction(Function &F, bool PostInlining)
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock *> Preds, const char *Suffix, DominatorTree *DT=nullptr, 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...
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
A set of analyses that are preserved following a run of a transformation pass.
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
static constexpr UpdateKind Insert
LLVM Basic Block Representation.
Conditional or Unconditional Branch instruction.
char & BreakCriticalEdgesID
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const Instruction & front() const
Indirect Branch Instruction.
FunctionPass * createBreakCriticalEdgesPass()
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
void eraseBlock(const BasicBlock *BB)
Forget analysis results for the given basic block.
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Interval::pred_iterator pred_end(Interval *I)
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
self_iterator getIterator()
bool isCriticalEdge(const Instruction *TI, unsigned SuccNum, bool AllowIdenticalEdges=false)
Return true if the specified edge is a critical edge.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
bool isLandingPad() const
Return true if this basic block is a landing pad.
A SetVector that performs no allocations if smaller than a certain size.
Iterator for intrusive lists based on ilist_node.
void setIncomingBlock(unsigned i, BasicBlock *BB)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
void wireOldPredecessorsToNewImmediatePredecessor(BasicBlock *Old, BasicBlock *New, ArrayRef< BasicBlock *> Preds, bool IdenticalEdgesWereMerged=true)
A new empty BasicBlock (New) now branches directly to Old.
void setEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors, BranchProbability Prob)
Set the raw edge probability for the given edge.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
unsigned getNumIncomingValues() const
Return the number of incoming edges.
Target - Wrapper for Target specific information.
BasicBlock * CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap, const Twine &NameSuffix="", Function *F=nullptr, ClonedCodeInfo *CodeInfo=nullptr, DebugInfoFinder *DIFinder=nullptr)
Return a copy of the specified basic block, but without embedding the block into a particular functio...
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Analysis providing branch probability information.
iterator insert(iterator where, pointer New)
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
static void createPHIsForSplitLoopExit(ArrayRef< BasicBlock *> Preds, BasicBlock *SplitBB, BasicBlock *DestBB)
When a loop exit edge is split, LCSSA form may require new PHIs in the new exit block.
LLVM_NODISCARD bool empty() const
bool SplitIndirectBrCriticalEdges(Function &F, BranchProbabilityInfo *BPI=nullptr, BlockFrequencyInfo *BFI=nullptr)
Represents a single loop in the control flow graph.
StringRef getName() const
Return a constant reference to the value's name.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
const Function * getParent() const
Return the enclosing method, or null if none.
bool empty() const
Determine if the SetVector is empty or not.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
const BasicBlockListType & getBasicBlockList() const
Get the underlying elements of the Function...
void preserve()
Mark an analysis as preserved.
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isEHPad() const
Return true if this basic block is an exception handling block.
LLVM Value Representation.
succ_range successors(Instruction *I)
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
The legacy pass manager's analysis pass to compute loop information.
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
bool DontDeleteUselessPHIs
const BasicBlock * getParent() const