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();
89 SplitBB->
isLandingPad()) &&
"SplitBB has non-PHI nodes!");
99 if (
const PHINode *VP = dyn_cast<PHINode>(V))
100 if (VP->getParent() == SplitBB)
108 for (
unsigned i = 0, e = Preds.
size(); i != e; ++i)
138 assert(!isa<IndirectBrInst>(TI) &&
139 "Cannot split critical edge from IndirectBrInst");
200 auto *AA = Options.
AA;
201 auto *DT = Options.
DT;
202 auto *LI = Options.
LI;
216 for (
unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
217 if (PN->getIncomingBlock(i) != NewBB)
218 OtherPreds.
push_back(PN->getIncomingBlock(i));
228 bool NewBBDominatesDestBB =
true;
238 DomTreeNode *NewBBNode = DT->addNewBlock(NewBB, TIBB);
242 if (!OtherPreds.
empty()) {
243 DestBBNode = DT->getNode(DestBB);
244 while (!OtherPreds.
empty() && NewBBDominatesDestBB) {
246 NewBBDominatesDestBB = DT->dominates(DestBBNode, OPNode);
254 if (NewBBDominatesDestBB) {
255 if (!DestBBNode) DestBBNode = DT->getNode(DestBB);
256 DT->changeImmediateDominator(DestBBNode, NewBBNode);
263 if (
Loop *TIL = LI->getLoopFor(TIBB)) {
266 if (
Loop *DestLoop = LI->getLoopFor(DestBB)) {
267 if (TIL == DestLoop) {
269 DestLoop->addBasicBlockToLoop(NewBB, *LI);
270 }
else if (TIL->contains(DestLoop)) {
272 TIL->addBasicBlockToLoop(NewBB, *LI);
273 }
else if (DestLoop->contains(TIL)) {
275 DestLoop->addBasicBlockToLoop(NewBB, *LI);
281 assert(DestLoop->getHeader() == DestBB &&
282 "Should not create irreducible loops!");
283 if (
Loop *
P = DestLoop->getParentLoop())
284 P->addBasicBlockToLoop(NewBB, *LI);
290 if (!TIL->contains(DestBB)) {
291 assert(!TIL->contains(NewBB) &&
292 "Split point for loop exit is contained in loop!");
314 if (LI->getLoopFor(P) != TIL) {
321 if (!LoopPreds.
empty()) {
323 "We don't split edges to landing pads!");
325 DestBB, LoopPreds,
"split", AA, DT, LI, Options.
PreserveLCSSA);
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)
addIncoming - 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")
const Function * getParent() const
Return the enclosing method, or null if none.
unsigned SplitAllCriticalEdges(Function &F, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
void initializeBreakCriticalEdgesPass(PassRegistry &)
StringRef getName() const
Return a constant reference to the value's name.
iterator begin()
Instruction iterator methods.
Option class for critical edge splitting.
Instruction * getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
bool LLVM_ATTRIBUTE_UNUSED_RESULT 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.
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
BasicBlock * SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
SplitCriticalEdge - 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.
LLVM Basic Block Representation.
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
BranchInst - 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
getDebugLoc - Return the debug location for this node as a DebugLoc.
Represent the analysis usage information of a pass.
BasicBlock * getIncomingBlock(unsigned i) const
getIncomingBlock - Return incoming basic block number i.
iterator insert(iterator where, NodeTy *New)
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.
LLVMContext & getContext() const
All values hold a context through their type.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
const BasicBlockListType & getBasicBlockList() const
void setIncomingBlock(unsigned i, BasicBlock *BB)
Value * getIncomingValue(unsigned i) const
getIncomingValue - 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...
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
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 Value Representation.
The legacy pass manager's analysis pass to compute loop information.
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, AliasAnalysis *AA=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, bool PreserveLCSSA=false)
SplitBlockPredecessors - This method introduces at least one new basic block into the function and mo...
Legacy analysis pass which computes a DominatorTree.
static void createPHIsForSplitLoopExit(ArrayRef< BasicBlock * > Preds, BasicBlock *SplitBB, BasicBlock *DestBB)
createPHIsForSplitLoopExit - When a loop exit edge is split, LCSSA form may require new PHIs in the n...
void setIncomingValue(unsigned i, Value *V)
bool DontDeleteUselessPHIs
int getBasicBlockIndex(const BasicBlock *BB) const
getBasicBlockIndex - Return the first index of the specified basic block in the value list for this P...
const BasicBlock * getParent() const
bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum, bool AllowIdenticalEdges=false)
Return true if the specified edge is a critical edge.