2003-08-31 10:47:32 +08:00
|
|
|
//===- PruneEH.cpp - Pass which deletes unused exception handlers ---------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-08-31 10:47:32 +08:00
|
|
|
//
|
|
|
|
// This file implements a simple interprocedural pass which walks the
|
|
|
|
// call-graph, turning invoke instructions into calls, iff the callee cannot
|
2007-12-11 03:09:40 +08:00
|
|
|
// throw an exception, and marking functions 'nounwind' if they cannot throw.
|
|
|
|
// It implements this as a bottom-up traversal of the call-graph.
|
2003-08-31 10:47:32 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-20 06:09:18 +08:00
|
|
|
#define DEBUG_TYPE "prune-eh"
|
2003-11-22 05:54:22 +08:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2003-08-31 10:47:32 +08:00
|
|
|
#include "llvm/CallGraphSCCPass.h"
|
2005-04-27 12:52:23 +08:00
|
|
|
#include "llvm/Constants.h"
|
2003-08-31 10:47:32 +08:00
|
|
|
#include "llvm/Function.h"
|
2004-07-30 01:30:56 +08:00
|
|
|
#include "llvm/Instructions.h"
|
2003-08-31 10:47:32 +08:00
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2007-02-13 10:10:56 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2005-04-27 12:52:23 +08:00
|
|
|
#include "llvm/Support/CFG.h"
|
2007-02-06 07:32:05 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-02-23 06:17:59 +08:00
|
|
|
#include "llvm/ParamAttrsList.h"
|
2003-08-31 10:47:32 +08:00
|
|
|
#include <set>
|
2004-10-18 23:43:46 +08:00
|
|
|
#include <algorithm>
|
2003-11-22 05:54:22 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2006-12-20 06:09:18 +08:00
|
|
|
STATISTIC(NumRemoved, "Number of invokes removed");
|
|
|
|
STATISTIC(NumUnreach, "Number of noreturn calls optimized");
|
2003-08-31 10:47:32 +08:00
|
|
|
|
2006-12-20 06:09:18 +08:00
|
|
|
namespace {
|
2007-02-06 07:32:05 +08:00
|
|
|
struct VISIBILITY_HIDDEN PruneEH : public CallGraphSCCPass {
|
2007-05-06 21:37:16 +08:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2007-05-02 05:15:47 +08:00
|
|
|
PruneEH() : CallGraphSCCPass((intptr_t)&ID) {}
|
|
|
|
|
2003-08-31 10:47:32 +08:00
|
|
|
// runOnSCC - Analyze the SCC, performing the transformation if possible.
|
|
|
|
bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
|
2005-04-27 12:52:23 +08:00
|
|
|
|
|
|
|
bool SimplifyFunction(Function *F);
|
|
|
|
void DeleteBasicBlock(BasicBlock *BB);
|
2003-08-31 10:47:32 +08:00
|
|
|
};
|
2007-05-02 05:15:47 +08:00
|
|
|
|
2007-05-03 09:11:54 +08:00
|
|
|
char PruneEH::ID = 0;
|
2006-08-28 06:42:52 +08:00
|
|
|
RegisterPass<PruneEH> X("prune-eh", "Remove unused exception handling info");
|
2003-08-31 10:47:32 +08:00
|
|
|
}
|
|
|
|
|
2007-01-26 08:47:38 +08:00
|
|
|
Pass *llvm::createPruneEHPass() { return new PruneEH(); }
|
2003-09-01 00:30:07 +08:00
|
|
|
|
2003-08-31 10:47:32 +08:00
|
|
|
|
|
|
|
bool PruneEH::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
|
|
|
|
CallGraph &CG = getAnalysis<CallGraph>();
|
2005-04-27 12:52:23 +08:00
|
|
|
bool MadeChange = false;
|
|
|
|
|
|
|
|
// First pass, scan all of the functions in the SCC, simplifying them
|
|
|
|
// according to what we know.
|
|
|
|
for (unsigned i = 0, e = SCC.size(); i != e; ++i)
|
|
|
|
if (Function *F = SCC[i]->getFunction())
|
|
|
|
MadeChange |= SimplifyFunction(F);
|
2003-08-31 10:47:32 +08:00
|
|
|
|
2005-04-27 12:52:23 +08:00
|
|
|
// Next, check to see if any callees might throw or if there are any external
|
2003-08-31 10:47:32 +08:00
|
|
|
// functions in this SCC: if so, we cannot prune any functions in this SCC.
|
2003-09-09 03:44:26 +08:00
|
|
|
// If this SCC includes the unwind instruction, we KNOW it throws, so
|
2003-08-31 10:47:32 +08:00
|
|
|
// obviously the SCC might throw.
|
|
|
|
//
|
2005-04-27 12:52:23 +08:00
|
|
|
bool SCCMightUnwind = false, SCCMightReturn = false;
|
|
|
|
for (unsigned i = 0, e = SCC.size();
|
|
|
|
(!SCCMightUnwind || !SCCMightReturn) && i != e; ++i) {
|
2004-04-12 12:06:38 +08:00
|
|
|
Function *F = SCC[i]->getFunction();
|
2007-12-11 03:09:40 +08:00
|
|
|
if (F == 0) {
|
2005-04-27 07:53:25 +08:00
|
|
|
SCCMightUnwind = true;
|
2005-04-27 12:52:23 +08:00
|
|
|
SCCMightReturn = true;
|
2007-12-11 03:09:40 +08:00
|
|
|
} else if (F->isDeclaration()) {
|
2007-12-18 17:59:50 +08:00
|
|
|
SCCMightUnwind |= !F->doesNotThrow();
|
|
|
|
SCCMightReturn |= !F->doesNotReturn();
|
2004-04-12 12:06:38 +08:00
|
|
|
} else {
|
2007-12-18 17:59:50 +08:00
|
|
|
bool CheckUnwind = !SCCMightUnwind && !F->doesNotThrow();
|
|
|
|
bool CheckReturn = !SCCMightReturn && !F->doesNotReturn();
|
2007-12-11 03:09:40 +08:00
|
|
|
|
|
|
|
if (!CheckUnwind && !CheckReturn)
|
|
|
|
continue;
|
2005-04-27 12:52:23 +08:00
|
|
|
|
2004-04-12 12:06:38 +08:00
|
|
|
// Check to see if this function performs an unwind or calls an
|
|
|
|
// unwinding function.
|
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
|
2007-12-11 03:09:40 +08:00
|
|
|
if (CheckUnwind && isa<UnwindInst>(BB->getTerminator())) {
|
|
|
|
// Uses unwind!
|
2005-04-27 07:53:25 +08:00
|
|
|
SCCMightUnwind = true;
|
2007-12-11 03:09:40 +08:00
|
|
|
} else if (CheckReturn && isa<ReturnInst>(BB->getTerminator())) {
|
2005-04-27 12:52:23 +08:00
|
|
|
SCCMightReturn = true;
|
2004-04-12 12:06:38 +08:00
|
|
|
}
|
2004-02-09 05:15:59 +08:00
|
|
|
|
2004-04-12 12:06:38 +08:00
|
|
|
// Invoke instructions don't allow unwinding to continue, so we are
|
|
|
|
// only interested in call instructions.
|
2007-12-11 03:09:40 +08:00
|
|
|
if (CheckUnwind && !SCCMightUnwind)
|
2005-04-27 12:52:23 +08:00
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(I)) {
|
2007-12-18 17:59:50 +08:00
|
|
|
if (CI->doesNotThrow()) {
|
2007-12-11 03:09:40 +08:00
|
|
|
// This call cannot throw.
|
|
|
|
} else if (Function *Callee = CI->getCalledFunction()) {
|
2005-04-27 12:52:23 +08:00
|
|
|
CallGraphNode *CalleeNode = CG[Callee];
|
2007-12-11 03:09:40 +08:00
|
|
|
// If the callee is outside our current SCC then we may
|
|
|
|
// throw because it might.
|
|
|
|
if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end()){
|
2005-04-27 12:52:23 +08:00
|
|
|
SCCMightUnwind = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Indirect call, it might throw.
|
2005-04-27 07:53:25 +08:00
|
|
|
SCCMightUnwind = true;
|
2004-02-09 05:15:59 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-04-27 12:52:23 +08:00
|
|
|
if (SCCMightUnwind && SCCMightReturn) break;
|
2003-09-15 10:22:50 +08:00
|
|
|
}
|
2004-04-12 12:06:38 +08:00
|
|
|
}
|
|
|
|
}
|
2003-08-31 10:47:32 +08:00
|
|
|
|
2005-04-27 12:52:23 +08:00
|
|
|
// If the SCC doesn't unwind or doesn't throw, note this fact.
|
2007-12-11 03:09:40 +08:00
|
|
|
if (!SCCMightUnwind || !SCCMightReturn)
|
|
|
|
for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
|
2008-02-20 05:38:47 +08:00
|
|
|
ParameterAttributes NewAttributes = ParamAttr::None;
|
2007-12-11 03:09:40 +08:00
|
|
|
|
|
|
|
if (!SCCMightUnwind)
|
2007-12-20 05:13:37 +08:00
|
|
|
NewAttributes |= ParamAttr::NoUnwind;
|
2007-12-11 03:09:40 +08:00
|
|
|
if (!SCCMightReturn)
|
2007-12-20 05:13:37 +08:00
|
|
|
NewAttributes |= ParamAttr::NoReturn;
|
2007-12-11 03:09:40 +08:00
|
|
|
|
2007-12-20 05:13:37 +08:00
|
|
|
const ParamAttrsList *PAL = SCC[i]->getFunction()->getParamAttrs();
|
|
|
|
PAL = ParamAttrsList::includeAttrs(PAL, 0, NewAttributes);
|
2007-12-11 03:09:40 +08:00
|
|
|
SCC[i]->getFunction()->setParamAttrs(PAL);
|
|
|
|
}
|
2003-08-31 10:47:32 +08:00
|
|
|
|
2005-04-27 12:52:23 +08:00
|
|
|
for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
|
2003-08-31 10:47:32 +08:00
|
|
|
// Convert any invoke instructions to non-throwing functions in this node
|
|
|
|
// into call instructions with a branch. This makes the exception blocks
|
|
|
|
// dead.
|
|
|
|
if (Function *F = SCC[i]->getFunction())
|
2005-04-27 12:52:23 +08:00
|
|
|
MadeChange |= SimplifyFunction(F);
|
2003-08-31 10:47:32 +08:00
|
|
|
}
|
|
|
|
|
2005-04-22 07:48:37 +08:00
|
|
|
return MadeChange;
|
2003-08-31 10:47:32 +08:00
|
|
|
}
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2005-04-27 12:52:23 +08:00
|
|
|
|
|
|
|
// SimplifyFunction - Given information about callees, simplify the specified
|
|
|
|
// function if we have invokes to non-unwinding functions or code after calls to
|
|
|
|
// no-return functions.
|
|
|
|
bool PruneEH::SimplifyFunction(Function *F) {
|
|
|
|
bool MadeChange = false;
|
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
|
2007-12-18 17:59:50 +08:00
|
|
|
if (II->doesNotThrow()) {
|
2007-12-11 03:09:40 +08:00
|
|
|
SmallVector<Value*, 8> Args(II->op_begin()+3, II->op_end());
|
|
|
|
// Insert a call instruction before the invoke.
|
|
|
|
CallInst *Call = new CallInst(II->getCalledValue(),
|
|
|
|
Args.begin(), Args.end(), "", II);
|
|
|
|
Call->takeName(II);
|
|
|
|
Call->setCallingConv(II->getCallingConv());
|
|
|
|
Call->setParamAttrs(II->getParamAttrs());
|
|
|
|
|
|
|
|
// Anything that used the value produced by the invoke instruction
|
|
|
|
// now uses the value produced by the call instruction.
|
|
|
|
II->replaceAllUsesWith(Call);
|
|
|
|
BasicBlock *UnwindBlock = II->getUnwindDest();
|
|
|
|
UnwindBlock->removePredecessor(II->getParent());
|
|
|
|
|
|
|
|
// Insert a branch to the normal destination right before the
|
|
|
|
// invoke.
|
|
|
|
new BranchInst(II->getNormalDest(), II);
|
|
|
|
|
|
|
|
// Finally, delete the invoke instruction!
|
|
|
|
BB->getInstList().pop_back();
|
|
|
|
|
|
|
|
// If the unwind block is now dead, nuke it.
|
|
|
|
if (pred_begin(UnwindBlock) == pred_end(UnwindBlock))
|
|
|
|
DeleteBasicBlock(UnwindBlock); // Delete the new BB.
|
|
|
|
|
|
|
|
++NumRemoved;
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
2005-04-27 12:52:23 +08:00
|
|
|
|
2007-12-11 03:09:40 +08:00
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(I++))
|
2007-12-18 17:59:50 +08:00
|
|
|
if (CI->doesNotReturn() && !isa<UnreachableInst>(I)) {
|
2007-12-11 03:09:40 +08:00
|
|
|
// This call calls a function that cannot return. Insert an
|
|
|
|
// unreachable instruction after it and simplify the code. Do this
|
|
|
|
// by splitting the BB, adding the unreachable, then deleting the
|
|
|
|
// new BB.
|
|
|
|
BasicBlock *New = BB->splitBasicBlock(I);
|
|
|
|
|
|
|
|
// Remove the uncond branch and add an unreachable.
|
|
|
|
BB->getInstList().pop_back();
|
|
|
|
new UnreachableInst(BB);
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2007-12-11 03:09:40 +08:00
|
|
|
DeleteBasicBlock(New); // Delete the new BB.
|
2005-04-27 12:52:23 +08:00
|
|
|
MadeChange = true;
|
2007-12-11 03:09:40 +08:00
|
|
|
++NumUnreach;
|
|
|
|
break;
|
2005-04-27 12:52:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// DeleteBasicBlock - remove the specified basic block from the program,
|
|
|
|
/// updating the callgraph to reflect any now-obsolete edges due to calls that
|
|
|
|
/// exist in the BB.
|
|
|
|
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 (Function *Callee = CI->getCalledFunction())
|
|
|
|
CGN->removeCallEdgeTo(CG[Callee]);
|
|
|
|
} else if (InvokeInst *II = dyn_cast<InvokeInst>(I)) {
|
|
|
|
if (Function *Callee = II->getCalledFunction())
|
|
|
|
CGN->removeCallEdgeTo(CG[Callee]);
|
|
|
|
}
|
|
|
|
if (!I->use_empty())
|
|
|
|
I->replaceAllUsesWith(UndefValue::get(I->getType()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the list of successors of this block.
|
|
|
|
std::vector<BasicBlock*> Succs(succ_begin(BB), succ_end(BB));
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Succs.size(); i != e; ++i)
|
|
|
|
Succs[i]->removePredecessor(BB);
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2005-04-27 12:52:23 +08:00
|
|
|
BB->eraseFromParent();
|
|
|
|
}
|