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 unwind instruction, 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()) {
52 SCCMightThrow = true; break;
54 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
55 if (isa<UnwindInst>(I->getTerminator())) { // Uses unwind!
56 SCCMightThrow = true; break;
60 bool MadeChange = false;
62 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
63 // If the SCC can't throw, remember this for callers...
65 DoesNotThrow.insert(SCC[i]);
67 // Convert any invoke instructions to non-throwing functions in this node
68 // into call instructions with a branch. This makes the exception blocks
70 if (Function *F = SCC[i]->getFunction())
71 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
72 if (InvokeInst *II = dyn_cast<InvokeInst>(I->getTerminator()))
73 if (Function *F = II->getCalledFunction())
74 if (DoesNotThrow.count(CG[F])) {
75 // Insert a call instruction before the invoke...
76 std::string Name = II->getName(); II->setName("");
77 Value *Call = new CallInst(II->getCalledValue(),
78 std::vector<Value*>(II->op_begin()+3,
82 // Anything that used the value produced by the invoke instruction
83 // now uses the value produced by the call instruction.
84 II->replaceAllUsesWith(Call);
86 // Insert a branch to the normal destination right before the
88 new BranchInst(II->getNormalDest(), II);
90 // Finally, delete the invoke instruction!
91 I->getInstList().pop_back();