18 #define DEBUG_TYPE "break-crit-edges"
33 STATISTIC(NumBroken,
"Number of blocks inserted");
56 "Break critical edges in CFG",
false,
false)
61 return new BreakCriticalEdges();
67 bool BreakCriticalEdges::runOnFunction(
Function &
F) {
96 SplitBB->
isLandingPad()) &&
"SplitBB has non-PHI nodes!");
106 if (
const PHINode *VP = dyn_cast<PHINode>(V))
107 if (VP->getParent() == SplitBB)
115 for (
unsigned i = 0, e = Preds.
size(); i != e; ++i)
141 Pass *
P,
bool MergeIdenticalEdges,
142 bool DontDeleteUselessPhis,
143 bool SplitLandingPads) {
146 assert(!isa<IndirectBrInst>(TI) &&
147 "Cannot split critical edge from IndirectBrInst");
195 if (MergeIdenticalEdges) {
210 if (P == 0)
return NewBB;
216 if (DT == 0 && LI == 0)
229 for (
unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
230 if (PN->getIncomingBlock(i) != NewBB)
231 OtherPreds.
push_back(PN->getIncomingBlock(i));
241 bool NewBBDominatesDestBB =
true;
255 if (!OtherPreds.
empty()) {
256 DestBBNode = DT->
getNode(DestBB);
257 while (!OtherPreds.
empty() && NewBBDominatesDestBB) {
259 NewBBDominatesDestBB = DT->
dominates(DestBBNode, OPNode);
267 if (NewBBDominatesDestBB) {
268 if (!DestBBNode) DestBBNode = DT->
getNode(DestBB);
276 if (
Loop *TIL = LI->getLoopFor(TIBB)) {
279 if (
Loop *DestLoop = LI->getLoopFor(DestBB)) {
280 if (TIL == DestLoop) {
282 DestLoop->addBasicBlockToLoop(NewBB, LI->getBase());
283 }
else if (TIL->contains(DestLoop)) {
285 TIL->addBasicBlockToLoop(NewBB, LI->getBase());
286 }
else if (DestLoop->contains(TIL)) {
288 DestLoop->addBasicBlockToLoop(NewBB, LI->getBase());
294 assert(DestLoop->getHeader() == DestBB &&
295 "Should not create irreducible loops!");
296 if (
Loop *P = DestLoop->getParentLoop())
297 P->addBasicBlockToLoop(NewBB, LI->getBase());
303 if (!TIL->contains(DestBB) &&
305 assert(!TIL->contains(NewBB) &&
306 "Split point for loop exit is contained in loop!");
316 TIL->getExitBlocks(ExitBlocks);
317 for (
unsigned i = 0, e = ExitBlocks.
size(); i != e; ++i) {
321 bool HasPredOutsideOfLoop =
false;
326 if (TIL->contains(P)) {
333 HasPredOutsideOfLoop =
true;
341 if (!Preds.
empty() && HasPredOutsideOfLoop) {
347 }
else if (SplitLandingPads) {
350 ".split1",
".split2",
364 "SplitCriticalEdge doesn't know how to update LCCSA form "
365 "without LoopSimplify!");
void push_back(const T &Elt)
DomTreeNode * getNode(BasicBlock *BB) const
AnalysisUsage & addPreserved()
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)
static PassRegistry * getPassRegistry()
BasicBlock * SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P=0, bool MergeIdenticalEdges=false, bool DontDeleteUselessPHIs=false, bool SplitLandingPads=false)
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
const Function * getParent() const
Return the enclosing method, or null if none.
void initializeBreakCriticalEdgesPass(PassRegistry &)
LoopInfoBase< BlockT, LoopT > * LI
StringRef getName() const
AnalysisType * getAnalysisIfAvailable() const
void changeImmediateDominator(BasicBlock *N, BasicBlock *NewIDom)
Instruction * getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=0)
bool mustPreserveAnalysisID(char &AID) const
ID
LLVM Calling Convention Representation.
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, Pass *P=0)
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
AnalysisUsage & addPreservedID(const void *ID)
void setSuccessor(unsigned idx, BasicBlock *B)
size_t size() const
size - Get the array size.
unsigned getNumSuccessors() const
LLVM Basic Block Representation.
BasicBlock * getSuccessor(unsigned idx) const
char & BreakCriticalEdgesID
STATISTIC(NumBroken,"Number of blocks inserted")
FunctionPass * createBreakCriticalEdgesPass()
Interval::pred_iterator pred_begin(Interval *I)
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=0)
BasicBlock * getIncomingBlock(unsigned i) const
iterator insert(iterator where, NodeTy *New)
Interval::pred_iterator pred_end(Interval *I)
bool dominates(const DomTreeNode *A, const DomTreeNode *B) const
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
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.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=0, BasicBlock *InsertBefore=0)
Creates a new BasicBlock.
DomTreeNode * addNewBlock(BasicBlock *BB, BasicBlock *DomBB)
LLVM Value Representation.
void SplitLandingPadPredecessors(BasicBlock *OrigBB, ArrayRef< BasicBlock * > Preds, const char *Suffix, const char *Suffix2, Pass *P, SmallVectorImpl< BasicBlock * > &NewBBs)
static void createPHIsForSplitLoopExit(ArrayRef< BasicBlock * > Preds, BasicBlock *SplitBB, BasicBlock *DestBB)
void setIncomingValue(unsigned i, Value *V)
int getBasicBlockIndex(const BasicBlock *BB) const
const BasicBlock * getParent() const
bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum, bool AllowIdenticalEdges=false)