blob: 4008ea6d63832af7365ddef4a5cd739dd97197f5 [file] [log] [blame]
Chris Lattner0a3f8d52003-08-31 02:47:32 +00001//===- PruneEH.cpp - Pass which deletes unused exception handlers ---------===//
2//
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
6// call-graph.
7//
8//===----------------------------------------------------------------------===//
9
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"
17#include <set>
18
19namespace {
20 Statistic<> NumRemoved("prune-eh", "Number of invokes removed");
21
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;
26
27 // runOnSCC - Analyze the SCC, performing the transformation if possible.
28 bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
29 };
30 RegisterOpt<PruneEH> X("prune-eh", "Remove unused exception handling info");
31}
32
Chris Lattner75444c72003-08-31 16:30:07 +000033Pass *createPruneEHPass() { return new PruneEH(); }
34
Chris Lattner0a3f8d52003-08-31 02:47:32 +000035
36bool PruneEH::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
37 CallGraph &CG = getAnalysis<CallGraph>();
38
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.
Chris Lattner04ecefe2003-09-08 19:44:26 +000041 // If this SCC includes the unwind instruction, we KNOW it throws, so
Chris Lattner0a3f8d52003-08-31 02:47:32 +000042 // obviously the SCC might throw.
43 //
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())
Chris Lattner5a6fa292003-09-15 02:22:50 +000051 if (F->isExternal()) {
52 SCCMightThrow = true; break;
53 } else {
Chris Lattner04ecefe2003-09-08 19:44:26 +000054 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
55 if (isa<UnwindInst>(I->getTerminator())) { // Uses unwind!
56 SCCMightThrow = true; break;
57 }
Chris Lattner5a6fa292003-09-15 02:22:50 +000058 }
Chris Lattner0a3f8d52003-08-31 02:47:32 +000059
60 bool MadeChange = false;
61
62 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
63 // If the SCC can't throw, remember this for callers...
64 if (!SCCMightThrow)
65 DoesNotThrow.insert(SCC[i]);
66
67 // Convert any invoke instructions to non-throwing functions in this node
68 // into call instructions with a branch. This makes the exception blocks
69 // dead.
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,
79 II->op_end()),
80 Name, II);
81
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);
85
86 // Insert a branch to the normal destination right before the
87 // invoke.
88 new BranchInst(II->getNormalDest(), II);
89
90 // Finally, delete the invoke instruction!
91 I->getInstList().pop_back();
92
93 ++NumRemoved;
94 MadeChange = true;
95 }
96 }
97
98 return MadeChange;
99}