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
|
2012-09-27 10:14:43 +00:00
|
|
|
// 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"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2013-01-07 15:26:48 +00:00
|
|
|
#include "llvm/Analysis/CallGraphSCCPass.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2005-04-27 04:52:23 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
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 {
|
2009-10-25 06:33:48 +00:00
|
|
|
struct PruneEH : public CallGraphSCCPass {
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-19 17:21:58 +00:00
|
|
|
PruneEH() : CallGraphSCCPass(ID) {
|
|
|
|
initializePruneEHPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2003-08-31 02:47:32 +00:00
|
|
|
// runOnSCC - Analyze the SCC, performing the transformation if possible.
|
2010-04-16 22:42:17 +00:00
|
|
|
bool runOnSCC(CallGraphSCC &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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char PruneEH::ID = 0;
|
2010-10-13 22:00:45 +00: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-07 22:25:06 +00:00
|
|
|
"Remove unused exception handling info", false, false)
|
2008-05-13 00:00:25 +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
|
|
|
|
2010-04-16 22:42:17 +00:00
|
|
|
bool PruneEH::runOnSCC(CallGraphSCC &SCC) {
|
2008-09-29 14:59:04 +00:00
|
|
|
SmallPtrSet<CallGraphNode *, 8> SCCNodes;
|
2003-08-31 02:47:32 +00:00
|
|
|
CallGraph &CG = getAnalysis<CallGraph>();
|
2005-04-27 04:52:23 +00:00
|
|
|
bool MadeChange = false;
|
|
|
|
|
2008-09-29 14:59:04 +00:00
|
|
|
// Fill SCCNodes with the elements of the SCC. Used for quickly
|
|
|
|
// looking up whether a given CallGraphNode is in this SCC.
|
2010-04-16 22:42:17 +00:00
|
|
|
for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
|
|
|
|
SCCNodes.insert(*I);
|
2008-09-29 14:59:04 +00:00
|
|
|
|
2005-04-27 04:52:23 +00:00
|
|
|
// First pass, scan all of the functions in the SCC, simplifying them
|
|
|
|
// according to what we know.
|
2010-04-16 22:42:17 +00:00
|
|
|
for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
|
|
|
|
if (Function *F = (*I)->getFunction())
|
2005-04-27 04:52:23 +00:00
|
|
|
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.
|
2008-05-16 21:31:48 +00: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 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;
|
2010-04-16 22:42:17 +00:00
|
|
|
for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end();
|
|
|
|
(!SCCMightUnwind || !SCCMightReturn) && I != E; ++I) {
|
|
|
|
Function *F = (*I)->getFunction();
|
2008-05-16 23:18:52 +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;
|
2008-09-29 11:25:42 +00:00
|
|
|
} else if (F->isDeclaration() || F->mayBeOverridden()) {
|
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) {
|
2012-02-06 21:16:41 +00:00
|
|
|
if (CheckUnwind && isa<ResumeInst>(BB->getTerminator())) {
|
2011-08-15 18:22:00 +00:00
|
|
|
// Uses unwind / resume!
|
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.
|
2008-09-29 14:59:04 +00:00
|
|
|
if (!SCCNodes.count(CalleeNode)) {
|
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)
|
2010-04-16 22:42:17 +00:00
|
|
|
for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
|
2012-10-15 20:35:56 +00:00
|
|
|
AttrBuilder NewAttributes;
|
2007-12-10 19:09:40 +00:00
|
|
|
|
|
|
|
if (!SCCMightUnwind)
|
2012-12-19 07:18:57 +00:00
|
|
|
NewAttributes.addAttribute(Attribute::NoUnwind);
|
2007-12-10 19:09:40 +00:00
|
|
|
if (!SCCMightReturn)
|
2012-12-19 07:18:57 +00:00
|
|
|
NewAttributes.addAttribute(Attribute::NoReturn);
|
2007-12-10 19:09:40 +00:00
|
|
|
|
2010-04-16 22:42:17 +00:00
|
|
|
Function *F = (*I)->getFunction();
|
2012-12-07 23:16:57 +00:00
|
|
|
const AttributeSet &PAL = F->getAttributes();
|
2013-01-22 21:15:51 +00:00
|
|
|
const AttributeSet &NPAL =
|
|
|
|
PAL.addFnAttributes(F->getContext(),
|
|
|
|
AttributeSet::get(F->getContext(),
|
|
|
|
AttributeSet::FunctionIndex,
|
|
|
|
NewAttributes));
|
2008-09-05 09:08:37 +00:00
|
|
|
if (PAL != NPAL) {
|
|
|
|
MadeChange = true;
|
2010-04-16 22:42:17 +00:00
|
|
|
F->setAttributes(NPAL);
|
2008-09-05 09:08:37 +00:00
|
|
|
}
|
2007-12-10 19:09:40 +00:00
|
|
|
}
|
2003-08-31 02:47:32 +00:00
|
|
|
|
2010-04-16 22:42:17 +00:00
|
|
|
for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); 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.
|
2010-04-16 22:42:17 +00:00
|
|
|
if (Function *F = (*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()) {
|
2010-03-24 13:21:49 +00:00
|
|
|
SmallVector<Value*, 8> Args(II->op_begin(), II->op_end() - 3);
|
2007-12-10 19:09:40 +00:00
|
|
|
// Insert a call instruction before the invoke.
|
2011-07-15 08:37:34 +00:00
|
|
|
CallInst *Call = CallInst::Create(II->getCalledValue(), Args, "", II);
|
2007-12-10 19:09:40 +00:00
|
|
|
Call->takeName(II);
|
|
|
|
Call->setCallingConv(II->getCallingConv());
|
2008-09-25 21:00:45 +00:00
|
|
|
Call->setAttributes(II->getAttributes());
|
2011-05-10 00:03:11 +00:00
|
|
|
Call->setDebugLoc(II->getDebugLoc());
|
2007-12-10 19:09:40 +00:00
|
|
|
|
|
|
|
// Anything that used the value produced by the invoke instruction
|
2009-09-01 06:31:31 +00: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 19:09:40 +00: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 20:25:17 +00:00
|
|
|
BranchInst::Create(II->getNormalDest(), II);
|
2007-12-10 19:09:40 +00: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 17:11:18 +00:00
|
|
|
}
|
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; )
|
2008-04-25 16:53:59 +00:00
|
|
|
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();
|
2009-08-13 21:58:54 +00:00
|
|
|
new UnreachableInst(BB->getContext(), 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;
|
2008-03-09 17:13:05 +00:00
|
|
|
}
|
2005-04-27 04:52:23 +00:00
|
|
|
}
|
2008-04-25 16:53:59 +00:00
|
|
|
|
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;
|
2009-03-19 18:03:56 +00:00
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(I)) {
|
2011-06-09 20:31:09 +00:00
|
|
|
if (!isa<IntrinsicInst>(I))
|
2009-03-19 18:03:56 +00:00
|
|
|
CGN->removeCallEdgeFor(CI);
|
|
|
|
} else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
|
2008-09-08 11:05:51 +00:00
|
|
|
CGN->removeCallEdgeFor(II);
|
2005-04-27 04:52:23 +00:00
|
|
|
if (!I->use_empty())
|
2009-07-30 23:03:37 +00:00
|
|
|
I->replaceAllUsesWith(UndefValue::get(I->getType()));
|
2005-04-27 04:52:23 +00: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 06:12:32 +00:00
|
|
|
|
2005-04-27 04:52:23 +00:00
|
|
|
BB->eraseFromParent();
|
|
|
|
}
|