Initial checkin of the -prune-eh pass, a very simple exception handling removal pass
[oota-llvm.git] / lib / Transforms / IPO / PruneEH.cpp
1 //===- 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
19 namespace {
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
33
34 bool PruneEH::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
35   CallGraph &CG = getAnalysis<CallGraph>();
36
37   // First, check to see if any callees might throw or if there are any external
38   // functions in this SCC: if so, we cannot prune any functions in this SCC.
39   // If this SCC includes the llvm.unwind intrinsic, we KNOW it throws, so
40   // obviously the SCC might throw.
41   //
42   bool SCCMightThrow = false;
43   for (unsigned i = 0, e = SCC.size(); i != e; ++i)
44     if (!DoesNotThrow.count(SCC[i]) &&          // Calls maybe throwing fn
45         // Make sure this is not one of the fn's in the SCC.
46         std::find(SCC.begin(), SCC.end(), SCC[i]) == SCC.end()) {
47       SCCMightThrow = true; break;
48     } else if (Function *F = SCC[i]->getFunction())
49       if (F->isExternal() ||                             // Is external function
50           F->getIntrinsicID() == LLVMIntrinsic::unwind) {// Is unwind function!
51         SCCMightThrow = true; break;
52       }
53
54   bool MadeChange = false;
55
56   for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
57     // If the SCC can't throw, remember this for callers...
58     if (!SCCMightThrow)
59       DoesNotThrow.insert(SCC[i]);
60
61     // Convert any invoke instructions to non-throwing functions in this node
62     // into call instructions with a branch.  This makes the exception blocks
63     // dead.
64     if (Function *F = SCC[i]->getFunction())
65       for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
66         if (InvokeInst *II = dyn_cast<InvokeInst>(I->getTerminator()))
67           if (Function *F = II->getCalledFunction())
68             if (DoesNotThrow.count(CG[F])) {
69               // Insert a call instruction before the invoke...
70               std::string Name = II->getName();  II->setName("");
71               Value *Call = new CallInst(II->getCalledValue(),
72                                          std::vector<Value*>(II->op_begin()+3,
73                                                              II->op_end()),
74                                          Name, II);
75               
76               // Anything that used the value produced by the invoke instruction
77               // now uses the value produced by the call instruction.
78               II->replaceAllUsesWith(Call);
79           
80               // Insert a branch to the normal destination right before the
81               // invoke.
82               new BranchInst(II->getNormalDest(), II);
83               
84               // Finally, delete the invoke instruction!
85               I->getInstList().pop_back();
86               
87               ++NumRemoved;
88               MadeChange = true;
89             }
90   }
91
92   return MadeChange; 
93 }