2003-08-31 02:47:32 +00:00
|
|
|
//===- PruneEH.cpp - Pass which deletes unused exception handlers ---------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-08-31 02:47:32 +00:00
|
|
|
//
|
|
|
|
// This file implements a simple interprocedural pass which walks the
|
|
|
|
// call-graph, turning invoke instructions into calls, iff the callee cannot
|
2007-12-10 19:09:40 +00: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 02:47:32 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-19 22:09:18 +00:00
|
|
|
#define DEBUG_TYPE "prune-eh"
|
2003-11-21 21:54:22 +00:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2003-08-31 02:47:32 +00:00
|
|
|
#include "llvm/CallGraphSCCPass.h"
|
2005-04-27 04:52:23 +00:00
|
|
|
#include "llvm/Constants.h"
|
2003-08-31 02:47:32 +00:00
|
|
|
#include "llvm/Function.h"
|
2004-07-29 17:30:56 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2003-08-31 02:47:32 +00:00
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2007-02-13 02:10:56 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2005-04-27 04:52:23 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2007-02-05 23:32:05 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-01-02 23:41:05 +00:00
|
|
|
#include "llvm/ParameterAttributes.h"
|
2003-08-31 02:47:32 +00:00
|
|
|
#include <set>
|
2004-10-18 15:43:46 +00:00
|
|
|
#include <algorithm>
|
2003-11-21 21:54:22 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2006-12-19 22:09:18 +00:00
|
|
|
STATISTIC(NumRemoved, "Number of invokes removed");
|
|
|
|
STATISTIC(NumUnreach, "Number of noreturn calls optimized");
|
2003-08-31 02:47:32 +00:00
|
|
|
|
2006-12-19 22:09:18 +00:00
|
|
|
namespace {
|
2007-02-05 23:32:05 +00:00
|
|
|
struct VISIBILITY_HIDDEN PruneEH : public CallGraphSCCPass {
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2007-05-01 21:15:47 +00:00
|
|
|
PruneEH() : CallGraphSCCPass((intptr_t)&ID) {}
|
|
|
|
|
2003-08-31 02:47:32 +00:00
|
|
|
// runOnSCC - Analyze the SCC, performing the transformation if possible.
|
|
|
|
bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
|
2005-04-27 04:52:23 +00:00
|
|
|
|
|
|
|
bool SimplifyFunction(Function *F);
|
|
|
|
void DeleteBasicBlock(BasicBlock *BB);
|
2003-08-31 02:47:32 +00:00
|
|
|
};
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2007-05-03 01:11:54 +00:00
|
|
|
char PruneEH::ID = 0;
|
2006-08-27 22:42:52 +00:00
|
|
|
RegisterPass<PruneEH> X("prune-eh", "Remove unused exception handling info");
|
2003-08-31 02:47:32 +00:00
|
|
|
}
|
|
|
|
|
2007-01-26 00:47:38 +00:00
|
|
|
Pass *llvm::createPruneEHPass() { return new PruneEH(); }
|
2003-08-31 16:30:07 +00:00
|
|
|
|
2003-08-31 02:47:32 +00:00
|
|
|
|
|
|
|
bool PruneEH::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
|
|
|
|
CallGraph &CG = getAnalysis<CallGraph>();
|
2005-04-27 04:52:23 +00: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 02:47:32 +00:00
|
|
|
|
2005-04-27 04:52:23 +00:00
|
|
|
// Next, check to see if any callees might throw or if there are any external
|
2003-08-31 02:47:32 +00:00
|
|
|
// functions in this SCC: if so, we cannot prune any functions in this SCC.
|
2003-09-08 19:44:26 +00:00
|
|
|
// If this SCC includes the unwind instruction, we KNOW it throws, so
|
2003-08-31 02:47:32 +00:00
|
|
|
// obviously the SCC might throw.
|
|
|
|
//
|
2005-04-27 04:52:23 +00:00
|
|
|
bool SCCMightUnwind = false, SCCMightReturn = false;
|
|
|
|
for (unsigned i = 0, e = SCC.size();
|
|
|
|
(!SCCMightUnwind || !SCCMightReturn) && i != e; ++i) {
|
2004-04-12 04:06:38 +00:00
|
|
|
Function *F = SCC[i]->getFunction();
|
2007-12-10 19:09:40 +00:00
|
|
|
if (F == 0) {
|
2005-04-26 23:53:25 +00:00
|
|
|
SCCMightUnwind = true;
|
2005-04-27 04:52:23 +00:00
|
|
|
SCCMightReturn = true;
|
2007-12-10 19:09:40 +00:00
|
|
|
} else if (F->isDeclaration()) {
|
2007-12-18 09:59:50 +00:00
|
|
|
SCCMightUnwind |= !F->doesNotThrow();
|
|
|
|
SCCMightReturn |= !F->doesNotReturn();
|
2004-04-12 04:06:38 +00:00
|
|
|
} else {
|
2007-12-18 09:59:50 +00:00
|
|
|
bool CheckUnwind = !SCCMightUnwind && !F->doesNotThrow();
|
|
|
|
bool CheckReturn = !SCCMightReturn && !F->doesNotReturn();
|
2007-12-10 19:09:40 +00:00
|
|
|
|
|
|
|
if (!CheckUnwind && !CheckReturn)
|
|
|
|
continue;
|
2005-04-27 04:52:23 +00:00
|
|
|
|
2004-04-12 04:06:38 +00: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-10 19:09:40 +00:00
|
|
|
if (CheckUnwind && isa<UnwindInst>(BB->getTerminator())) {
|
|
|
|
// Uses unwind!
|
2005-04-26 23:53:25 +00:00
|
|
|
SCCMightUnwind = true;
|
2007-12-10 19:09:40 +00:00
|
|
|
} else if (CheckReturn && isa<ReturnInst>(BB->getTerminator())) {
|
2005-04-27 04:52:23 +00:00
|
|
|
SCCMightReturn = true;
|
2004-04-12 04:06:38 +00:00
|
|
|
}
|
2004-02-08 21:15:59 +00:00
|
|
|
|
2004-04-12 04:06:38 +00:00
|
|
|
// Invoke instructions don't allow unwinding to continue, so we are
|
|
|
|
// only interested in call instructions.
|
2007-12-10 19:09:40 +00:00
|
|
|
if (CheckUnwind && !SCCMightUnwind)
|
2005-04-27 04:52:23 +00:00
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(I)) {
|
2007-12-18 09:59:50 +00:00
|
|
|
if (CI->doesNotThrow()) {
|
2007-12-10 19:09:40 +00:00
|
|
|
// This call cannot throw.
|
|
|
|
} else if (Function *Callee = CI->getCalledFunction()) {
|
2005-04-27 04:52:23 +00:00
|
|
|
CallGraphNode *CalleeNode = CG[Callee];
|
2007-12-10 19:09:40 +00: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 04:52:23 +00:00
|
|
|
SCCMightUnwind = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Indirect call, it might throw.
|
2005-04-26 23:53:25 +00:00
|
|
|
SCCMightUnwind = true;
|
2004-02-08 21:15:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-04-27 04:52:23 +00:00
|
|
|
if (SCCMightUnwind && SCCMightReturn) break;
|
2003-09-15 02:22:50 +00:00
|
|
|
}
|
2004-04-12 04:06:38 +00:00
|
|
|
}
|
|
|
|
}
|
2003-08-31 02:47:32 +00:00
|
|
|
|
2005-04-27 04:52:23 +00:00
|
|
|
// If the SCC doesn't unwind or doesn't throw, note this fact.
|
2007-12-10 19:09:40 +00:00
|
|
|
if (!SCCMightUnwind || !SCCMightReturn)
|
|
|
|
for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
|
2007-12-19 21:13:37 +00:00
|
|
|
uint16_t NewAttributes = ParamAttr::None;
|
2007-12-10 19:09:40 +00:00
|
|
|
|
|
|
|
if (!SCCMightUnwind)
|
2007-12-19 21:13:37 +00:00
|
|
|
NewAttributes |= ParamAttr::NoUnwind;
|
2007-12-10 19:09:40 +00:00
|
|
|
if (!SCCMightReturn)
|
2007-12-19 21:13:37 +00:00
|
|
|
NewAttributes |= ParamAttr::NoReturn;
|
2007-12-10 19:09:40 +00:00
|
|
|
|
2007-12-19 21:13:37 +00:00
|
|
|
const ParamAttrsList *PAL = SCC[i]->getFunction()->getParamAttrs();
|
|
|
|
PAL = ParamAttrsList::includeAttrs(PAL, 0, NewAttributes);
|
2007-12-10 19:09:40 +00:00
|
|
|
SCC[i]->getFunction()->setParamAttrs(PAL);
|
|
|
|
}
|
2003-08-31 02:47:32 +00:00
|
|
|
|
2005-04-27 04:52:23 +00:00
|
|
|
for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
|
2003-08-31 02:47:32 +00: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 04:52:23 +00:00
|
|
|
MadeChange |= SimplifyFunction(F);
|
2003-08-31 02:47:32 +00:00
|
|
|
}
|
|
|
|
|
2005-04-21 23:48:37 +00:00
|
|
|
return MadeChange;
|
2003-08-31 02:47:32 +00:00
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2005-04-27 04:52:23 +00: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 09:59:50 +00:00
|
|
|
if (II->doesNotThrow()) {
|
2007-12-10 19:09:40 +00: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 04:52:23 +00:00
|
|
|
|
2007-12-10 19:09:40 +00:00
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(I++))
|
2007-12-18 09:59:50 +00:00
|
|
|
if (CI->doesNotReturn() && !isa<UnreachableInst>(I)) {
|
2007-12-10 19:09:40 +00: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 06:12:32 +00:00
|
|
|
|
2007-12-10 19:09:40 +00:00
|
|
|
DeleteBasicBlock(New); // Delete the new BB.
|
2005-04-27 04:52:23 +00:00
|
|
|
MadeChange = true;
|
2007-12-10 19:09:40 +00:00
|
|
|
++NumUnreach;
|
|
|
|
break;
|
2005-04-27 04:52:23 +00: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 06:12:32 +00:00
|
|
|
|
2005-04-27 04:52:23 +00:00
|
|
|
BB->eraseFromParent();
|
|
|
|
}
|