blob: 41f7f73845c30fd07b20b3e9fbe336a87e4c3c3f [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.
41 // If this SCC includes the llvm.unwind intrinsic, we KNOW it throws, so
42 // 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())
51 if (F->isExternal() || // Is external function
52 F->getIntrinsicID() == LLVMIntrinsic::unwind) {// Is unwind function!
53 SCCMightThrow = true; break;
54 }
55
56 bool MadeChange = false;
57
58 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
59 // If the SCC can't throw, remember this for callers...
60 if (!SCCMightThrow)
61 DoesNotThrow.insert(SCC[i]);
62
63 // Convert any invoke instructions to non-throwing functions in this node
64 // into call instructions with a branch. This makes the exception blocks
65 // dead.
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,
75 II->op_end()),
76 Name, II);
77
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);
81
82 // Insert a branch to the normal destination right before the
83 // invoke.
84 new BranchInst(II->getNormalDest(), II);
85
86 // Finally, delete the invoke instruction!
87 I->getInstList().pop_back();
88
89 ++NumRemoved;
90 MadeChange = true;
91 }
92 }
93
94 return MadeChange;
95}