33 #define DEBUG_TYPE "break-crit-edges"
35 STATISTIC(NumBroken,
"Number of blocks inserted");
44 bool runOnFunction(
Function &
F)
override {
45 auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
46 auto *DT = DTWP ? &DTWP->getDomTree() :
nullptr;
47 auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
48 auto *LI = LIWP ? &LIWP->getLoopInfo() :
nullptr;
67 "Break critical edges in CFG",
false,
false)
72 return new BreakCriticalEdges();
102 SplitBB->
isLandingPad()) &&
"SplitBB has non-PHI nodes!");
112 if (
const PHINode *VP = dyn_cast<PHINode>(V))
113 if (VP->getParent() == SplitBB)
120 for (
unsigned i = 0, e = Preds.
size();
i != e; ++
i)
134 assert(!isa<IndirectBrInst>(TI) &&
135 "Cannot split critical edge from IndirectBrInst");
142 if (DestBB->
isEHPad())
return nullptr;
196 auto *DT = Options.
DT;
197 auto *LI = Options.
LI;
211 for (
unsigned i = 0, e = PN->getNumIncomingValues();
i != e; ++
i)
212 if (PN->getIncomingBlock(
i) != NewBB)
213 OtherPreds.
push_back(PN->getIncomingBlock(
i));
223 bool NewBBDominatesDestBB =
true;
233 DomTreeNode *NewBBNode = DT->addNewBlock(NewBB, TIBB);
237 if (!OtherPreds.
empty()) {
238 DestBBNode = DT->getNode(DestBB);
239 while (!OtherPreds.
empty() && NewBBDominatesDestBB) {
241 NewBBDominatesDestBB = DT->dominates(DestBBNode, OPNode);
249 if (NewBBDominatesDestBB) {
250 if (!DestBBNode) DestBBNode = DT->getNode(DestBB);
251 DT->changeImmediateDominator(DestBBNode, NewBBNode);
258 if (
Loop *TIL = LI->getLoopFor(TIBB)) {
261 if (
Loop *DestLoop = LI->getLoopFor(DestBB)) {
262 if (TIL == DestLoop) {
264 DestLoop->addBasicBlockToLoop(NewBB, *LI);
265 }
else if (TIL->contains(DestLoop)) {
267 TIL->addBasicBlockToLoop(NewBB, *LI);
268 }
else if (DestLoop->contains(TIL)) {
270 DestLoop->addBasicBlockToLoop(NewBB, *LI);
276 assert(DestLoop->getHeader() == DestBB &&
277 "Should not create irreducible loops!");
278 if (
Loop *
P = DestLoop->getParentLoop())
279 P->addBasicBlockToLoop(NewBB, *LI);
285 if (!TIL->contains(DestBB)) {
286 assert(!TIL->contains(NewBB) &&
287 "Split point for loop exit is contained in loop!");
309 if (LI->getLoopFor(P) != TIL) {
316 if (!LoopPreds.
empty()) {
317 assert(!DestBB->
isEHPad() &&
"We don't split edges to EH pads!");
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...
STATISTIC(NumFunctions,"Total number of functions")
bool isEHPad() const
Return true if this basic block is an exception handling block.
const Function * getParent() const
Return the enclosing method, or null if none.
const Instruction & front() const
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 &)
StringRef getName() const
Return a constant reference to the value's name.
iterator begin()
Instruction iterator methods.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Option class for critical edge splitting.
Instruction * getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Analysis pass that exposes the LoopInfo for a function.
LLVM_NODISCARD bool empty() const
Base class for the actual dominator tree node.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
AnalysisUsage & addPreservedID(const void *ID)
void setSuccessor(unsigned idx, BasicBlock *B)
Update the specified successor to point at the provided block.
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
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.
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.
LLVM Basic Block Representation.
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
Conditional or Unconditional Branch instruction.
char & BreakCriticalEdgesID
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...
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Represent the analysis usage information of a pass.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
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()
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
LLVMContext & getContext() const
All values hold a context through their type.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Iterator for intrusive lists based on ilist_node.
const BasicBlockListType & getBasicBlockList() const
void setIncomingBlock(unsigned i, BasicBlock *BB)
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Type * getType() const
All values are typed, get the type of this value.
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...
iterator insert(iterator where, pointer New)
Represents a single loop in the control flow graph.
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
bool isLandingPad() const
Return true if this basic block is a landing pad.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
void preserve()
Mark an analysis as preserved.
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...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
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.
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.
void setIncomingValue(unsigned i, Value *V)
bool DontDeleteUselessPHIs
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
const BasicBlock * getParent() const
bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum, bool AllowIdenticalEdges=false)
Return true if the specified edge is a critical edge.