BreakCriticalEdges.cpp [plain text]
#define DEBUG_TYPE "break-crit-edges"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Analysis/Dominators.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Function.h"
#include "llvm/Instructions.h"
#include "llvm/Type.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/Compiler.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
using namespace llvm;
STATISTIC(NumBroken, "Number of blocks inserted");
namespace {
struct VISIBILITY_HIDDEN BreakCriticalEdges : public FunctionPass {
static char ID; BreakCriticalEdges() : FunctionPass(&ID) {}
virtual bool runOnFunction(Function &F);
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.addPreserved<DominatorTree>();
AU.addPreserved<DominanceFrontier>();
AU.addPreserved<LoopInfo>();
AU.addPreservedID(LoopSimplifyID);
}
};
}
char BreakCriticalEdges::ID = 0;
static RegisterPass<BreakCriticalEdges>
X("break-crit-edges", "Break critical edges in CFG");
const PassInfo *const llvm::BreakCriticalEdgesID = &X;
FunctionPass *llvm::createBreakCriticalEdgesPass() {
return new BreakCriticalEdges();
}
bool BreakCriticalEdges::runOnFunction(Function &F) {
bool Changed = false;
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
TerminatorInst *TI = I->getTerminator();
if (TI->getNumSuccessors() > 1)
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
if (SplitCriticalEdge(TI, i, this)) {
++NumBroken;
Changed = true;
}
}
return Changed;
}
bool llvm::isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum,
bool AllowIdenticalEdges) {
assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
if (TI->getNumSuccessors() == 1) return false;
const BasicBlock *Dest = TI->getSuccessor(SuccNum);
pred_const_iterator I = pred_begin(Dest), E = pred_end(Dest);
assert(I != E && "No preds, but we have an edge to the block?");
const BasicBlock *FirstPred = *I;
++I; if (!AllowIdenticalEdges)
return I != E;
while (I != E) {
if (*I != FirstPred)
return true;
E = pred_end(*I);
++I;
}
return false;
}
bool llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P,
bool MergeIdenticalEdges) {
if (!isCriticalEdge(TI, SuccNum, MergeIdenticalEdges)) return false;
BasicBlock *TIBB = TI->getParent();
BasicBlock *DestBB = TI->getSuccessor(SuccNum);
BasicBlock *NewBB = BasicBlock::Create(TIBB->getName() + "." +
DestBB->getName() + "_crit_edge");
BranchInst::Create(DestBB, NewBB);
TI->setSuccessor(SuccNum, NewBB);
Function &F = *TIBB->getParent();
Function::iterator FBBI = TIBB;
F.getBasicBlockList().insert(++FBBI, NewBB);
for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) {
PHINode *PN = cast<PHINode>(I);
int BBIdx = PN->getBasicBlockIndex(TIBB);
PN->setIncomingBlock(BBIdx, NewBB);
}
if (MergeIdenticalEdges) {
for (unsigned i = SuccNum+1, e = TI->getNumSuccessors(); i != e; ++i) {
if (TI->getSuccessor(i) != DestBB) continue;
DestBB->removePredecessor(TIBB);
TI->setSuccessor(i, NewBB);
}
}
if (P == 0) return true;
SmallVector<BasicBlock*, 8> OtherPreds;
for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB); I != E; ++I)
if (*I != NewBB)
OtherPreds.push_back(*I);
bool NewBBDominatesDestBB = true;
if (DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>()) {
DomTreeNode *TINode = DT->getNode(TIBB);
if (TINode) { DomTreeNode *NewBBNode = DT->addNewBlock(NewBB, TIBB);
DomTreeNode *DestBBNode = 0;
if (!OtherPreds.empty()) {
DestBBNode = DT->getNode(DestBB);
while (!OtherPreds.empty() && NewBBDominatesDestBB) {
if (DomTreeNode *OPNode = DT->getNode(OtherPreds.back()))
NewBBDominatesDestBB = DT->dominates(DestBBNode, OPNode);
OtherPreds.pop_back();
}
OtherPreds.clear();
}
if (NewBBDominatesDestBB) {
if (!DestBBNode) DestBBNode = DT->getNode(DestBB);
DT->changeImmediateDominator(DestBBNode, NewBBNode);
}
}
}
if (DominanceFrontier *DF = P->getAnalysisIfAvailable<DominanceFrontier>()) {
if (!OtherPreds.empty()) {
assert(0 && "Requiring domfrontiers but not idom/domtree/domset."
" not implemented yet!");
}
DominanceFrontier::DomSetType NewDFSet;
if (NewBBDominatesDestBB) {
DominanceFrontier::iterator I = DF->find(DestBB);
if (I != DF->end()) {
DF->addBasicBlock(NewBB, I->second);
if (I->second.count(DestBB)) {
DominanceFrontier::iterator NF = DF->find(NewBB);
DF->removeFromFrontier(NF, DestBB);
}
}
else
DF->addBasicBlock(NewBB, DominanceFrontier::DomSetType());
} else {
DominanceFrontier::DomSetType NewDFSet;
NewDFSet.insert(DestBB);
DF->addBasicBlock(NewBB, NewDFSet);
}
}
if (LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>()) {
if (Loop *TIL = LI->getLoopFor(TIBB))
if (Loop *DestLoop = LI->getLoopFor(DestBB)) {
if (TIL == DestLoop) {
DestLoop->addBasicBlockToLoop(NewBB, LI->getBase());
} else if (TIL->contains(DestLoop->getHeader())) {
TIL->addBasicBlockToLoop(NewBB, LI->getBase());
} else if (DestLoop->contains(TIL->getHeader())) {
DestLoop->addBasicBlockToLoop(NewBB, LI->getBase());
} else {
assert(DestLoop->getHeader() == DestBB &&
"Should not create irreducible loops!");
if (Loop *P = DestLoop->getParentLoop())
P->addBasicBlockToLoop(NewBB, LI->getBase());
}
}
}
return true;
}