74e0d33d4127e43fd888c2355f9faf8d2bd6d603
[oota-llvm.git] / lib / Transforms / IPO / PruneEH.cpp
1 //===- PruneEH.cpp - Pass which deletes unused exception handlers ---------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements a simple interprocedural pass which walks the
11 // call-graph, turning invoke instructions into calls, iff the callee cannot
12 // throw an exception.  It implements this as a bottom-up traversal of the
13 // call-graph.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Transforms/IPO.h"
18 #include "llvm/CallGraphSCCPass.h"
19 #include "llvm/Function.h"
20 #include "llvm/Intrinsics.h"
21 #include "llvm/iTerminators.h"
22 #include "llvm/iOther.h"
23 #include "llvm/Analysis/CallGraph.h"
24 #include "Support/Statistic.h"
25 #include <set>
26 using namespace llvm;
27
28 namespace {
29   Statistic<> NumRemoved("prune-eh", "Number of invokes removed");
30
31   struct PruneEH : public CallGraphSCCPass {
32     /// DoesNotThrow - This set contains all of the functions which we have
33     /// determined cannot throw exceptions.
34     std::set<CallGraphNode*> DoesNotThrow;
35
36     // runOnSCC - Analyze the SCC, performing the transformation if possible.
37     bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
38   };
39   RegisterOpt<PruneEH> X("prune-eh", "Remove unused exception handling info");
40 }
41
42 Pass *llvm::createPruneEHPass() { return new PruneEH(); }
43
44
45 bool PruneEH::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
46   CallGraph &CG = getAnalysis<CallGraph>();
47
48   // First, check to see if any callees might throw or if there are any external
49   // functions in this SCC: if so, we cannot prune any functions in this SCC.
50   // If this SCC includes the unwind instruction, we KNOW it throws, so
51   // obviously the SCC might throw.
52   //
53   bool SCCMightThrow = false;
54   for (unsigned i = 0, e = SCC.size(); i != e; ++i)
55     if (!DoesNotThrow.count(SCC[i]) &&          // Calls maybe throwing fn
56         // Make sure this is not one of the fn's in the SCC.
57         std::find(SCC.begin(), SCC.end(), SCC[i]) == SCC.end()) {
58       SCCMightThrow = true; break;
59     } else if (Function *F = SCC[i]->getFunction())
60       if (F->isExternal()) {
61         SCCMightThrow = true; break;
62       } else {
63         for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
64           if (isa<UnwindInst>(I->getTerminator())) {  // Uses unwind!
65             SCCMightThrow = true; break;
66           }
67       }
68
69   bool MadeChange = false;
70
71   for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
72     // If the SCC can't throw, remember this for callers...
73     if (!SCCMightThrow)
74       DoesNotThrow.insert(SCC[i]);
75
76     // Convert any invoke instructions to non-throwing functions in this node
77     // into call instructions with a branch.  This makes the exception blocks
78     // dead.
79     if (Function *F = SCC[i]->getFunction())
80       for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
81         if (InvokeInst *II = dyn_cast<InvokeInst>(I->getTerminator()))
82           if (Function *F = II->getCalledFunction())
83             if (DoesNotThrow.count(CG[F])) {
84               // Insert a call instruction before the invoke...
85               std::string Name = II->getName();  II->setName("");
86               Value *Call = new CallInst(II->getCalledValue(),
87                                          std::vector<Value*>(II->op_begin()+3,
88                                                              II->op_end()),
89                                          Name, II);
90               
91               // Anything that used the value produced by the invoke instruction
92               // now uses the value produced by the call instruction.
93               II->replaceAllUsesWith(Call);
94           
95               // Insert a branch to the normal destination right before the
96               // invoke.
97               new BranchInst(II->getNormalDest(), II);
98               
99               // Finally, delete the invoke instruction!
100               I->getInstList().pop_back();
101               
102               ++NumRemoved;
103               MadeChange = true;
104             }
105   }
106
107   return MadeChange; 
108 }
109