1 //===- PruneEH.cpp - Pass which deletes unused exception handlers ---------===//
3 // This file implements a simple interprocedural pass which walks the
4 // call-graph, turning invoke instructions into calls, iff the callee cannot
5 // throw an exception. It implements this as a bottom-up traversal of the
8 //===----------------------------------------------------------------------===//
10 #include "llvm/CallGraphSCCPass.h"
11 #include "llvm/Function.h"
12 #include "llvm/Intrinsics.h"
13 #include "llvm/iTerminators.h"
14 #include "llvm/iOther.h"
15 #include "llvm/Analysis/CallGraph.h"
16 #include "Support/Statistic.h"
20 Statistic<> NumRemoved("prune-eh", "Number of invokes removed");
22 struct PruneEH : public CallGraphSCCPass {
23 /// DoesNotThrow - This set contains all of the functions which we have
24 /// determined cannot throw exceptions.
25 std::set<CallGraphNode*> DoesNotThrow;
27 // runOnSCC - Analyze the SCC, performing the transformation if possible.
28 bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
30 RegisterOpt<PruneEH> X("prune-eh", "Remove unused exception handling info");
33 Pass *createPruneEHPass() { return new PruneEH(); }
36 bool PruneEH::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
37 CallGraph &CG = getAnalysis<CallGraph>();
39 // First, check to see if any callees might throw or if there are any external
40 // functions in this SCC: if so, we cannot prune any functions in this SCC.
41 // If this SCC includes the llvm.unwind intrinsic, we KNOW it throws, so
42 // obviously the SCC might throw.
44 bool SCCMightThrow = false;
45 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
46 if (!DoesNotThrow.count(SCC[i]) && // Calls maybe throwing fn
47 // Make sure this is not one of the fn's in the SCC.
48 std::find(SCC.begin(), SCC.end(), SCC[i]) == SCC.end()) {
49 SCCMightThrow = true; break;
50 } else if (Function *F = SCC[i]->getFunction())
51 if (F->isExternal() || // Is external function
52 F->getIntrinsicID() == LLVMIntrinsic::unwind) {// Is unwind function!
53 SCCMightThrow = true; break;
56 bool MadeChange = false;
58 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
59 // If the SCC can't throw, remember this for callers...
61 DoesNotThrow.insert(SCC[i]);
63 // Convert any invoke instructions to non-throwing functions in this node
64 // into call instructions with a branch. This makes the exception blocks
66 if (Function *F = SCC[i]->getFunction())
67 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
68 if (InvokeInst *II = dyn_cast<InvokeInst>(I->getTerminator()))
69 if (Function *F = II->getCalledFunction())
70 if (DoesNotThrow.count(CG[F])) {
71 // Insert a call instruction before the invoke...
72 std::string Name = II->getName(); II->setName("");
73 Value *Call = new CallInst(II->getCalledValue(),
74 std::vector<Value*>(II->op_begin()+3,
78 // Anything that used the value produced by the invoke instruction
79 // now uses the value produced by the call instruction.
80 II->replaceAllUsesWith(Call);
82 // Insert a branch to the normal destination right before the
84 new BranchInst(II->getNormalDest(), II);
86 // Finally, delete the invoke instruction!
87 I->getInstList().pop_back();