2003-08-31 04:47:32 +02:00
|
|
|
//===- PruneEH.cpp - Pass which deletes unused exception handlers ---------===//
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-08-31 04:47:32 +02: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 20:09:40 +01: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 04:47:32 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-19 23:09:18 +01:00
|
|
|
#define DEBUG_TYPE "prune-eh"
|
2003-11-21 22:54:22 +01:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2003-08-31 04:47:32 +02:00
|
|
|
#include "llvm/CallGraphSCCPass.h"
|
2005-04-27 06:52:23 +02:00
|
|
|
#include "llvm/Constants.h"
|
2003-08-31 04:47:32 +02:00
|
|
|
#include "llvm/Function.h"
|
2009-07-06 03:34:54 +02:00
|
|
|
#include "llvm/LLVMContext.h"
|
2004-07-29 19:30:56 +02:00
|
|
|
#include "llvm/Instructions.h"
|
2009-03-19 19:03:56 +01:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2003-08-31 04:47:32 +02:00
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2008-09-29 16:59:04 +02:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2007-02-13 03:10:56 +01:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2005-04-27 06:52:23 +02:00
|
|
|
#include "llvm/Support/CFG.h"
|
2003-08-31 04:47:32 +02:00
|
|
|
#include <set>
|
2004-10-18 17:43:46 +02:00
|
|
|
#include <algorithm>
|
2003-11-21 22:54:22 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2006-12-19 23:09:18 +01:00
|
|
|
STATISTIC(NumRemoved, "Number of invokes removed");
|
|
|
|
STATISTIC(NumUnreach, "Number of noreturn calls optimized");
|
2003-08-31 04:47:32 +02:00
|
|
|
|
2006-12-19 23:09:18 +01:00
|
|
|
namespace {
|
2009-10-25 07:33:48 +01:00
|
|
|
struct PruneEH : public CallGraphSCCPass {
|
2007-05-06 15:37:16 +02:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-19 19:21:58 +02:00
|
|
|
PruneEH() : CallGraphSCCPass(ID) {
|
|
|
|
initializePruneEHPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2007-05-01 23:15:47 +02:00
|
|
|
|
2003-08-31 04:47:32 +02:00
|
|
|
// runOnSCC - Analyze the SCC, performing the transformation if possible.
|
2010-04-17 00:42:17 +02:00
|
|
|
bool runOnSCC(CallGraphSCC &SCC);
|
2005-04-27 06:52:23 +02:00
|
|
|
|
|
|
|
bool SimplifyFunction(Function *F);
|
|
|
|
void DeleteBasicBlock(BasicBlock *BB);
|
2003-08-31 04:47:32 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 02:00:25 +02:00
|
|
|
char PruneEH::ID = 0;
|
2010-10-14 00:00:45 +02:00
|
|
|
INITIALIZE_PASS_BEGIN(PruneEH, "prune-eh",
|
|
|
|
"Remove unused exception handling info", false, false)
|
|
|
|
INITIALIZE_AG_DEPENDENCY(CallGraph)
|
|
|
|
INITIALIZE_PASS_END(PruneEH, "prune-eh",
|
2010-10-08 00:25:06 +02:00
|
|
|
"Remove unused exception handling info", false, false)
|
2008-05-13 02:00:25 +02:00
|
|
|
|
2007-01-26 01:47:38 +01:00
|
|
|
Pass *llvm::createPruneEHPass() { return new PruneEH(); }
|
2003-08-31 18:30:07 +02:00
|
|
|
|
2003-08-31 04:47:32 +02:00
|
|
|
|
2010-04-17 00:42:17 +02:00
|
|
|
bool PruneEH::runOnSCC(CallGraphSCC &SCC) {
|
2008-09-29 16:59:04 +02:00
|
|
|
SmallPtrSet<CallGraphNode *, 8> SCCNodes;
|
2003-08-31 04:47:32 +02:00
|
|
|
CallGraph &CG = getAnalysis<CallGraph>();
|
2005-04-27 06:52:23 +02:00
|
|
|
bool MadeChange = false;
|
|
|
|
|
2008-09-29 16:59:04 +02:00
|
|
|
// Fill SCCNodes with the elements of the SCC. Used for quickly
|
|
|
|
// looking up whether a given CallGraphNode is in this SCC.
|
2010-04-17 00:42:17 +02:00
|
|
|
for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
|
|
|
|
SCCNodes.insert(*I);
|
2008-09-29 16:59:04 +02:00
|
|
|
|
2005-04-27 06:52:23 +02:00
|
|
|
// First pass, scan all of the functions in the SCC, simplifying them
|
|
|
|
// according to what we know.
|
2010-04-17 00:42:17 +02:00
|
|
|
for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
|
|
|
|
if (Function *F = (*I)->getFunction())
|
2005-04-27 06:52:23 +02:00
|
|
|
MadeChange |= SimplifyFunction(F);
|
2003-08-31 04:47:32 +02:00
|
|
|
|
2005-04-27 06:52:23 +02:00
|
|
|
// Next, check to see if any callees might throw or if there are any external
|
2003-08-31 04:47:32 +02:00
|
|
|
// functions in this SCC: if so, we cannot prune any functions in this SCC.
|
2008-05-16 23:31:48 +02:00
|
|
|
// Definitions that are weak and not declared non-throwing might be
|
|
|
|
// overridden at linktime with something that throws, so assume that.
|
2003-09-08 21:44:26 +02:00
|
|
|
// If this SCC includes the unwind instruction, we KNOW it throws, so
|
2003-08-31 04:47:32 +02:00
|
|
|
// obviously the SCC might throw.
|
|
|
|
//
|
2005-04-27 06:52:23 +02:00
|
|
|
bool SCCMightUnwind = false, SCCMightReturn = false;
|
2010-04-17 00:42:17 +02:00
|
|
|
for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end();
|
|
|
|
(!SCCMightUnwind || !SCCMightReturn) && I != E; ++I) {
|
|
|
|
Function *F = (*I)->getFunction();
|
2008-05-17 01:18:52 +02:00
|
|
|
if (F == 0) {
|
2005-04-27 01:53:25 +02:00
|
|
|
SCCMightUnwind = true;
|
2005-04-27 06:52:23 +02:00
|
|
|
SCCMightReturn = true;
|
2008-09-29 13:25:42 +02:00
|
|
|
} else if (F->isDeclaration() || F->mayBeOverridden()) {
|
2007-12-18 10:59:50 +01:00
|
|
|
SCCMightUnwind |= !F->doesNotThrow();
|
|
|
|
SCCMightReturn |= !F->doesNotReturn();
|
2004-04-12 06:06:38 +02:00
|
|
|
} else {
|
2007-12-18 10:59:50 +01:00
|
|
|
bool CheckUnwind = !SCCMightUnwind && !F->doesNotThrow();
|
|
|
|
bool CheckReturn = !SCCMightReturn && !F->doesNotReturn();
|
2007-12-10 20:09:40 +01:00
|
|
|
|
|
|
|
if (!CheckUnwind && !CheckReturn)
|
|
|
|
continue;
|
2005-04-27 06:52:23 +02:00
|
|
|
|
2004-04-12 06:06:38 +02: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 20:09:40 +01:00
|
|
|
if (CheckUnwind && isa<UnwindInst>(BB->getTerminator())) {
|
|
|
|
// Uses unwind!
|
2005-04-27 01:53:25 +02:00
|
|
|
SCCMightUnwind = true;
|
2007-12-10 20:09:40 +01:00
|
|
|
} else if (CheckReturn && isa<ReturnInst>(BB->getTerminator())) {
|
2005-04-27 06:52:23 +02:00
|
|
|
SCCMightReturn = true;
|
2004-04-12 06:06:38 +02:00
|
|
|
}
|
2004-02-08 22:15:59 +01:00
|
|
|
|
2004-04-12 06:06:38 +02:00
|
|
|
// Invoke instructions don't allow unwinding to continue, so we are
|
|
|
|
// only interested in call instructions.
|
2007-12-10 20:09:40 +01:00
|
|
|
if (CheckUnwind && !SCCMightUnwind)
|
2005-04-27 06:52:23 +02:00
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(I)) {
|
2007-12-18 10:59:50 +01:00
|
|
|
if (CI->doesNotThrow()) {
|
2007-12-10 20:09:40 +01:00
|
|
|
// This call cannot throw.
|
|
|
|
} else if (Function *Callee = CI->getCalledFunction()) {
|
2005-04-27 06:52:23 +02:00
|
|
|
CallGraphNode *CalleeNode = CG[Callee];
|
2007-12-10 20:09:40 +01:00
|
|
|
// If the callee is outside our current SCC then we may
|
|
|
|
// throw because it might.
|
2008-09-29 16:59:04 +02:00
|
|
|
if (!SCCNodes.count(CalleeNode)) {
|
2005-04-27 06:52:23 +02:00
|
|
|
SCCMightUnwind = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Indirect call, it might throw.
|
2005-04-27 01:53:25 +02:00
|
|
|
SCCMightUnwind = true;
|
2004-02-08 22:15:59 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-04-27 06:52:23 +02:00
|
|
|
if (SCCMightUnwind && SCCMightReturn) break;
|
2003-09-15 04:22:50 +02:00
|
|
|
}
|
2004-04-12 06:06:38 +02:00
|
|
|
}
|
|
|
|
}
|
2003-08-31 04:47:32 +02:00
|
|
|
|
2005-04-27 06:52:23 +02:00
|
|
|
// If the SCC doesn't unwind or doesn't throw, note this fact.
|
2007-12-10 20:09:40 +01:00
|
|
|
if (!SCCMightUnwind || !SCCMightReturn)
|
2010-04-17 00:42:17 +02:00
|
|
|
for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
|
2008-09-25 23:00:45 +02:00
|
|
|
Attributes NewAttributes = Attribute::None;
|
2007-12-10 20:09:40 +01:00
|
|
|
|
|
|
|
if (!SCCMightUnwind)
|
2008-09-25 23:00:45 +02:00
|
|
|
NewAttributes |= Attribute::NoUnwind;
|
2007-12-10 20:09:40 +01:00
|
|
|
if (!SCCMightReturn)
|
2008-09-25 23:00:45 +02:00
|
|
|
NewAttributes |= Attribute::NoReturn;
|
2007-12-10 20:09:40 +01:00
|
|
|
|
2010-04-17 00:42:17 +02:00
|
|
|
Function *F = (*I)->getFunction();
|
|
|
|
const AttrListPtr &PAL = F->getAttributes();
|
2008-09-27 00:53:05 +02:00
|
|
|
const AttrListPtr &NPAL = PAL.addAttr(~0, NewAttributes);
|
2008-09-05 11:08:37 +02:00
|
|
|
if (PAL != NPAL) {
|
|
|
|
MadeChange = true;
|
2010-04-17 00:42:17 +02:00
|
|
|
F->setAttributes(NPAL);
|
2008-09-05 11:08:37 +02:00
|
|
|
}
|
2007-12-10 20:09:40 +01:00
|
|
|
}
|
2003-08-31 04:47:32 +02:00
|
|
|
|
2010-04-17 00:42:17 +02:00
|
|
|
for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
|
2003-08-31 04:47:32 +02:00
|
|
|
// Convert any invoke instructions to non-throwing functions in this node
|
|
|
|
// into call instructions with a branch. This makes the exception blocks
|
|
|
|
// dead.
|
2010-04-17 00:42:17 +02:00
|
|
|
if (Function *F = (*I)->getFunction())
|
2005-04-27 06:52:23 +02:00
|
|
|
MadeChange |= SimplifyFunction(F);
|
2003-08-31 04:47:32 +02:00
|
|
|
}
|
|
|
|
|
2005-04-22 01:48:37 +02:00
|
|
|
return MadeChange;
|
2003-08-31 04:47:32 +02:00
|
|
|
}
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2005-04-27 06:52:23 +02: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 10:59:50 +01:00
|
|
|
if (II->doesNotThrow()) {
|
2010-03-24 14:21:49 +01:00
|
|
|
SmallVector<Value*, 8> Args(II->op_begin(), II->op_end() - 3);
|
2007-12-10 20:09:40 +01:00
|
|
|
// Insert a call instruction before the invoke.
|
2008-04-06 22:25:17 +02:00
|
|
|
CallInst *Call = CallInst::Create(II->getCalledValue(),
|
|
|
|
Args.begin(), Args.end(), "", II);
|
2007-12-10 20:09:40 +01:00
|
|
|
Call->takeName(II);
|
|
|
|
Call->setCallingConv(II->getCallingConv());
|
2008-09-25 23:00:45 +02:00
|
|
|
Call->setAttributes(II->getAttributes());
|
2007-12-10 20:09:40 +01:00
|
|
|
|
|
|
|
// Anything that used the value produced by the invoke instruction
|
2009-09-01 08:31:31 +02:00
|
|
|
// now uses the value produced by the call instruction. Note that we
|
|
|
|
// do this even for void functions and calls with no uses so that the
|
|
|
|
// callgraph edge is updated.
|
2007-12-10 20:09:40 +01:00
|
|
|
II->replaceAllUsesWith(Call);
|
|
|
|
BasicBlock *UnwindBlock = II->getUnwindDest();
|
|
|
|
UnwindBlock->removePredecessor(II->getParent());
|
|
|
|
|
|
|
|
// Insert a branch to the normal destination right before the
|
|
|
|
// invoke.
|
2008-04-06 22:25:17 +02:00
|
|
|
BranchInst::Create(II->getNormalDest(), II);
|
2007-12-10 20:09:40 +01:00
|
|
|
|
|
|
|
// 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;
|
2008-03-09 18:11:18 +01:00
|
|
|
}
|
2005-04-27 06:52:23 +02:00
|
|
|
|
2007-12-10 20:09:40 +01:00
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
|
2008-04-25 18:53:59 +02:00
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(I++))
|
2007-12-18 10:59:50 +01:00
|
|
|
if (CI->doesNotReturn() && !isa<UnreachableInst>(I)) {
|
2007-12-10 20:09:40 +01: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();
|
2009-08-13 23:58:54 +02:00
|
|
|
new UnreachableInst(BB->getContext(), BB);
|
2005-07-27 08:12:32 +02:00
|
|
|
|
2007-12-10 20:09:40 +01:00
|
|
|
DeleteBasicBlock(New); // Delete the new BB.
|
2005-04-27 06:52:23 +02:00
|
|
|
MadeChange = true;
|
2007-12-10 20:09:40 +01:00
|
|
|
++NumUnreach;
|
|
|
|
break;
|
2008-03-09 18:13:05 +01:00
|
|
|
}
|
2005-04-27 06:52:23 +02:00
|
|
|
}
|
2008-04-25 18:53:59 +02:00
|
|
|
|
2005-04-27 06:52:23 +02: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;
|
2009-03-19 19:03:56 +01:00
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(I)) {
|
|
|
|
if (!isa<DbgInfoIntrinsic>(I))
|
|
|
|
CGN->removeCallEdgeFor(CI);
|
|
|
|
} else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
|
2008-09-08 13:05:51 +02:00
|
|
|
CGN->removeCallEdgeFor(II);
|
2005-04-27 06:52:23 +02:00
|
|
|
if (!I->use_empty())
|
2009-07-31 01:03:37 +02:00
|
|
|
I->replaceAllUsesWith(UndefValue::get(I->getType()));
|
2005-04-27 06:52:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 08:12:32 +02:00
|
|
|
|
2005-04-27 06:52:23 +02:00
|
|
|
BB->eraseFromParent();
|
|
|
|
}
|