#define DEBUG_TYPE "prune-eh"
#include "llvm/Transforms/IPO.h"
#include "llvm/CallGraphSCCPass.h"
#include "llvm/Constants.h"
#include "llvm/Function.h"
#include "llvm/Instructions.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/Compiler.h"
#include <set>
#include <algorithm>
using namespace llvm;
STATISTIC(NumRemoved, "Number of invokes removed");
STATISTIC(NumUnreach, "Number of noreturn calls optimized");
namespace {
struct VISIBILITY_HIDDEN PruneEH : public CallGraphSCCPass {
static char ID; PruneEH() : CallGraphSCCPass(&ID) {}
bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
bool SimplifyFunction(Function *F);
void DeleteBasicBlock(BasicBlock *BB);
};
}
char PruneEH::ID = 0;
static RegisterPass<PruneEH>
X("prune-eh", "Remove unused exception handling info");
Pass *llvm::createPruneEHPass() { return new PruneEH(); }
bool PruneEH::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
SmallPtrSet<CallGraphNode *, 8> SCCNodes;
CallGraph &CG = getAnalysis<CallGraph>();
bool MadeChange = false;
for (unsigned i = 0, e = SCC.size(); i != e; ++i)
SCCNodes.insert(SCC[i]);
for (unsigned i = 0, e = SCC.size(); i != e; ++i)
if (Function *F = SCC[i]->getFunction())
MadeChange |= SimplifyFunction(F);
bool SCCMightUnwind = false, SCCMightReturn = false;
for (unsigned i = 0, e = SCC.size();
(!SCCMightUnwind || !SCCMightReturn) && i != e; ++i) {
Function *F = SCC[i]->getFunction();
if (F == 0) {
SCCMightUnwind = true;
SCCMightReturn = true;
} else if (F->isDeclaration() || F->mayBeOverridden()) {
SCCMightUnwind |= !F->doesNotThrow();
SCCMightReturn |= !F->doesNotReturn();
} else {
bool CheckUnwind = !SCCMightUnwind && !F->doesNotThrow();
bool CheckReturn = !SCCMightReturn && !F->doesNotReturn();
if (!CheckUnwind && !CheckReturn)
continue;
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
if (CheckUnwind && isa<UnwindInst>(BB->getTerminator())) {
SCCMightUnwind = true;
} else if (CheckReturn && isa<ReturnInst>(BB->getTerminator())) {
SCCMightReturn = true;
}
if (CheckUnwind && !SCCMightUnwind)
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
if (CallInst *CI = dyn_cast<CallInst>(I)) {
if (CI->doesNotThrow()) {
} else if (Function *Callee = CI->getCalledFunction()) {
CallGraphNode *CalleeNode = CG[Callee];
if (!SCCNodes.count(CalleeNode)) {
SCCMightUnwind = true;
break;
}
} else {
SCCMightUnwind = true;
break;
}
}
if (SCCMightUnwind && SCCMightReturn) break;
}
}
}
if (!SCCMightUnwind || !SCCMightReturn)
for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
Attributes NewAttributes = Attribute::None;
if (!SCCMightUnwind)
NewAttributes |= Attribute::NoUnwind;
if (!SCCMightReturn)
NewAttributes |= Attribute::NoReturn;
const AttrListPtr &PAL = SCC[i]->getFunction()->getAttributes();
const AttrListPtr &NPAL = PAL.addAttr(~0, NewAttributes);
if (PAL != NPAL) {
MadeChange = true;
SCC[i]->getFunction()->setAttributes(NPAL);
}
}
for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
if (Function *F = SCC[i]->getFunction())
MadeChange |= SimplifyFunction(F);
}
return MadeChange;
}
bool PruneEH::SimplifyFunction(Function *F) {
CallGraph &CG = getAnalysis<CallGraph>();
CallGraphNode *CGN = CG[F];
bool MadeChange = false;
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
if (II->doesNotThrow()) {
SmallVector<Value*, 8> Args(II->op_begin()+3, II->op_end());
CallInst *Call = CallInst::Create(II->getCalledValue(),
Args.begin(), Args.end(), "", II);
Call->takeName(II);
Call->setCallingConv(II->getCallingConv());
Call->setAttributes(II->getAttributes());
II->replaceAllUsesWith(Call);
BasicBlock *UnwindBlock = II->getUnwindDest();
UnwindBlock->removePredecessor(II->getParent());
CGN->replaceCallSite(II, Call);
BranchInst::Create(II->getNormalDest(), II);
BB->getInstList().pop_back();
if (pred_begin(UnwindBlock) == pred_end(UnwindBlock))
DeleteBasicBlock(UnwindBlock);
++NumRemoved;
MadeChange = true;
}
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
if (CallInst *CI = dyn_cast<CallInst>(I++))
if (CI->doesNotReturn() && !isa<UnreachableInst>(I)) {
BasicBlock *New = BB->splitBasicBlock(I);
BB->getInstList().pop_back();
new UnreachableInst(BB);
DeleteBasicBlock(New); MadeChange = true;
++NumUnreach;
break;
}
}
return MadeChange;
}
void PruneEH::DeleteBasicBlock(BasicBlock *BB) {
assert(pred_begin(BB) == pred_end(BB) && "BB is not dead!");
CallGraph &CG = getAnalysis<CallGraph>();
CallGraphNode *CGN = CG[BB->getParent()];
for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; ) {
--I;
if (CallInst *CI = dyn_cast<CallInst>(I)) {
if (!isa<DbgInfoIntrinsic>(I))
CGN->removeCallEdgeFor(CI);
} else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
CGN->removeCallEdgeFor(II);
if (!I->use_empty())
I->replaceAllUsesWith(UndefValue::get(I->getType()));
}
std::vector<BasicBlock*> Succs(succ_begin(BB), succ_end(BB));
for (unsigned i = 0, e = Succs.size(); i != e; ++i)
Succs[i]->removePredecessor(BB);
BB->eraseFromParent();
}