37 #define DEBUG_TYPE "break-crit-edges" 39 STATISTIC(NumBroken,
"Number of blocks inserted");
49 auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
50 auto *DT = DTWP ? &DTWP->getDomTree() :
nullptr;
51 auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
52 auto *LI = LIWP ? &LIWP->getLoopInfo() :
nullptr;
71 "Break critical edges in CFG",
false,
false)
76 return new BreakCriticalEdges();
106 SplitBB->
isLandingPad()) &&
"SplitBB has non-PHI nodes!");
110 unsigned Idx = PN.getBasicBlockIndex(SplitBB);
111 Value *V = PN.getIncomingValue(Idx);
115 if (
const PHINode *VP = dyn_cast<PHINode>(V))
116 if (VP->getParent() == SplitBB)
121 PN.getType(), Preds.
size(),
"split",
123 for (
unsigned i = 0, e = Preds.
size(); i != e; ++i)
127 PN.setIncomingValue(Idx, NewPN);
137 assert(!isa<IndirectBrInst>(TI) &&
138 "Cannot split critical edge from IndirectBrInst");
145 if (DestBB->
isEHPad())
return nullptr;
199 auto *DT = Options.
DT;
200 auto *LI = Options.
LI;
220 DT->applyUpdates(Updates);
225 if (
Loop *TIL = LI->getLoopFor(TIBB)) {
228 if (
Loop *DestLoop = LI->getLoopFor(DestBB)) {
229 if (TIL == DestLoop) {
231 DestLoop->addBasicBlockToLoop(NewBB, *LI);
232 }
else if (TIL->contains(DestLoop)) {
234 TIL->addBasicBlockToLoop(NewBB, *LI);
235 }
else if (DestLoop->contains(TIL)) {
237 DestLoop->addBasicBlockToLoop(NewBB, *LI);
243 assert(DestLoop->getHeader() == DestBB &&
244 "Should not create irreducible loops!");
245 if (
Loop *
P = DestLoop->getParentLoop())
246 P->addBasicBlockToLoop(NewBB, *LI);
252 if (!TIL->contains(DestBB)) {
253 assert(!TIL->contains(NewBB) &&
254 "Split point for loop exit is contained in loop!");
276 if (LI->getLoopFor(P) != TIL) {
283 if (!LoopPreds.
empty()) {
284 assert(!DestBB->
isEHPad() &&
"We don't split edges to EH pads!");
317 case Instruction::IndirectBr:
322 case Instruction::Br:
323 case Instruction::Switch:
346 for (
unsigned Succ = 0,
E = IBI->getNumSuccessors(); Succ !=
E; ++Succ)
347 Targets.
insert(IBI->getSuccessor(Succ));
353 bool ShouldUpdateAnalysis = BPI &&
BFI;
354 bool Changed =
false;
360 if (!IBRPred || OtherPreds.
empty())
369 if (ShouldUpdateAnalysis) {
375 BFI->setBlockFreq(BodyBlock, BFI->getBlockFreq(
Target).getFrequency());
394 if (ShouldUpdateAnalysis)
395 BlockFreqForDirectSucc += BFI->getBlockFreq(Src) *
398 if (ShouldUpdateAnalysis) {
399 BFI->setBlockFreq(DirectSucc, BlockFreqForDirectSucc.
getFrequency());
401 BFI->getBlockFreq(
Target) - BlockFreqForDirectSucc;
412 End =
Target->getFirstNonPHI()->getIterator();
417 "Block was expected to only contain PHIs");
419 while (Indirect != End) {
420 PHINode *DirPHI = cast<PHINode>(Direct);
421 PHINode *IndPHI = cast<PHINode>(Indirect);
433 NewIndPHI->
addIncoming(IndPHI->getIncomingValueForBlock(IBRPred),
443 IndPHI->replaceAllUsesWith(MergePHI);
444 IndPHI->eraseFromParent();
void push_back(const T &Elt)
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...
Compute iterated dominance frontiers using a linear time algorithm.
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
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. ...
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.
AnalysisUsage & addPreservedID(const void *ID)
Interval::succ_iterator succ_end(Interval *I)
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
BasicBlock * SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
If this edge is a critical edge, insert a new node to split the critical edge.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
static bool runOnFunction(Function &F, bool PostInlining)
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Subclasses of this class are all able to terminate a basic block.
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...
void setSuccessor(unsigned idx, BasicBlock *B)
Update the specified successor to point at the provided block.
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)
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock *> Preds, const char *Suffix, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, bool PreserveLCSSA=false)
This method introduces at least one new basic block into the function and moves some of the predecess...
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
self_iterator getIterator()
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 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)
CloneBasicBlock - Return a copy of the specified basic block, but without embedding the block into a ...
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())
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
bool isEHPad() const
Return true if this basic block is an exception handling block.
LLVM Value Representation.
succ_range successors(BasicBlock *BB)
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.
const TerminatorInst * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
bool DontDeleteUselessPHIs
const BasicBlock * getParent() const
bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum, bool AllowIdenticalEdges=false)
Return true if the specified edge is a critical edge.