blob: 7c0a73e19becc44c4baa705870c78404cde03cd8 [file] [log] [blame]
Chris Lattner0a3f8d52003-08-31 02:47:32 +00001//===- PruneEH.cpp - Pass which deletes unused exception handlers ---------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattner0a3f8d52003-08-31 02:47:32 +00009//
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
Chris Lattnerf52e03c2003-11-21 21:54:22 +000017#include "llvm/Transforms/IPO.h"
Chris Lattner0a3f8d52003-08-31 02:47:32 +000018#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>
Chris Lattnerf52e03c2003-11-21 21:54:22 +000026using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000027
Chris Lattner0a3f8d52003-08-31 02:47:32 +000028namespace {
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
Chris Lattnerf52e03c2003-11-21 21:54:22 +000042Pass *llvm::createPruneEHPass() { return new PruneEH(); }
Chris Lattner75444c72003-08-31 16:30:07 +000043
Chris Lattner0a3f8d52003-08-31 02:47:32 +000044
45bool 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.
Chris Lattner04ecefe2003-09-08 19:44:26 +000050 // If this SCC includes the unwind instruction, we KNOW it throws, so
Chris Lattner0a3f8d52003-08-31 02:47:32 +000051 // obviously the SCC might throw.
52 //
53 bool SCCMightThrow = false;
Chris Lattner56997dd2004-02-08 21:15:59 +000054 for (unsigned i = 0, e = SCC.size(); !SCCMightThrow && i != e; ++i)
55 if (Function *F = SCC[i]->getFunction())
Chris Lattner5a6fa292003-09-15 02:22:50 +000056 if (F->isExternal()) {
Chris Lattner56997dd2004-02-08 21:15:59 +000057 SCCMightThrow = true;
Chris Lattner5a6fa292003-09-15 02:22:50 +000058 } else {
Chris Lattner56997dd2004-02-08 21:15:59 +000059 // Check to see if this function performs an unwind or calls an
60 // unwinding function.
61 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
62 if (isa<UnwindInst>(BB->getTerminator())) { // Uses unwind!
63 SCCMightThrow = true;
64 break;
Chris Lattner04ecefe2003-09-08 19:44:26 +000065 }
Chris Lattner56997dd2004-02-08 21:15:59 +000066
67 // Invoke instructions don't allow unwinding to continue, so we are
68 // only interested in call instructions.
69 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
70 if (CallInst *CI = dyn_cast<CallInst>(I)) {
71 if (Function *Callee = CI->getCalledFunction()) {
72 CallGraphNode *CalleeNode = CG[Callee];
73 // If the callee is outside our current SCC, or if it is not
74 // known to throw, then we might throw also.
75 if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end()&&
76 !DoesNotThrow.count(CalleeNode)) {
77 SCCMightThrow = true;
78 break;
79 }
80
81 } else {
82 // Indirect call, it might throw.
83 SCCMightThrow = true;
84 break;
85 }
86 }
87 if (SCCMightThrow) break;
88 }
Chris Lattner5a6fa292003-09-15 02:22:50 +000089 }
Chris Lattner0a3f8d52003-08-31 02:47:32 +000090
91 bool MadeChange = false;
92
93 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
94 // If the SCC can't throw, remember this for callers...
95 if (!SCCMightThrow)
96 DoesNotThrow.insert(SCC[i]);
97
98 // Convert any invoke instructions to non-throwing functions in this node
99 // into call instructions with a branch. This makes the exception blocks
100 // dead.
101 if (Function *F = SCC[i]->getFunction())
102 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
103 if (InvokeInst *II = dyn_cast<InvokeInst>(I->getTerminator()))
104 if (Function *F = II->getCalledFunction())
105 if (DoesNotThrow.count(CG[F])) {
106 // Insert a call instruction before the invoke...
107 std::string Name = II->getName(); II->setName("");
108 Value *Call = new CallInst(II->getCalledValue(),
109 std::vector<Value*>(II->op_begin()+3,
110 II->op_end()),
111 Name, II);
112
113 // Anything that used the value produced by the invoke instruction
114 // now uses the value produced by the call instruction.
115 II->replaceAllUsesWith(Call);
Chris Lattnerf40cdbe2003-11-22 02:20:36 +0000116 II->getExceptionalDest()->removePredecessor(II->getParent());
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000117
118 // Insert a branch to the normal destination right before the
119 // invoke.
120 new BranchInst(II->getNormalDest(), II);
121
122 // Finally, delete the invoke instruction!
123 I->getInstList().pop_back();
124
125 ++NumRemoved;
126 MadeChange = true;
127 }
128 }
129
130 return MadeChange;
131}
Brian Gaeke960707c2003-11-11 22:41:34 +0000132