blob: c9e3455c94b7f235727d168f7ff43cca3023cefc [file] [log] [blame]
Chris Lattner0a3f8d52003-08-31 02:47:32 +00001//===- PruneEH.cpp - Pass which deletes unused exception handlers ---------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// 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.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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 Lattner1631bcb2006-12-19 22:09:18 +000017#define DEBUG_TYPE "prune-eh"
Chris Lattnerf52e03c2003-11-21 21:54:22 +000018#include "llvm/Transforms/IPO.h"
Chris Lattner0a3f8d52003-08-31 02:47:32 +000019#include "llvm/CallGraphSCCPass.h"
Chris Lattner93f4e9d2005-04-27 04:52:23 +000020#include "llvm/Constants.h"
Chris Lattner0a3f8d52003-08-31 02:47:32 +000021#include "llvm/Function.h"
22#include "llvm/Intrinsics.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000023#include "llvm/Instructions.h"
Chris Lattner0a3f8d52003-08-31 02:47:32 +000024#include "llvm/Analysis/CallGraph.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000025#include "llvm/ADT/Statistic.h"
Chris Lattner93f4e9d2005-04-27 04:52:23 +000026#include "llvm/Support/CFG.h"
Chris Lattner0a3f8d52003-08-31 02:47:32 +000027#include <set>
Chris Lattner88a8a322004-10-18 15:43:46 +000028#include <algorithm>
Chris Lattnerf52e03c2003-11-21 21:54:22 +000029using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000030
Chris Lattner1631bcb2006-12-19 22:09:18 +000031STATISTIC(NumRemoved, "Number of invokes removed");
32STATISTIC(NumUnreach, "Number of noreturn calls optimized");
Chris Lattner0a3f8d52003-08-31 02:47:32 +000033
Chris Lattner1631bcb2006-12-19 22:09:18 +000034namespace {
Chris Lattner0a3f8d52003-08-31 02:47:32 +000035 struct PruneEH : public CallGraphSCCPass {
Chris Lattner7f4f7732005-04-26 23:53:25 +000036 /// DoesNotUnwind - This set contains all of the functions which we have
Chris Lattner93f4e9d2005-04-27 04:52:23 +000037 /// determined cannot unwind.
Chris Lattner7f4f7732005-04-26 23:53:25 +000038 std::set<CallGraphNode*> DoesNotUnwind;
Chris Lattner0a3f8d52003-08-31 02:47:32 +000039
Chris Lattner93f4e9d2005-04-27 04:52:23 +000040 /// DoesNotReturn - This set contains all of the functions which we have
41 /// determined cannot return normally (but might unwind).
42 std::set<CallGraphNode*> DoesNotReturn;
43
Chris Lattner0a3f8d52003-08-31 02:47:32 +000044 // runOnSCC - Analyze the SCC, performing the transformation if possible.
45 bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
Chris Lattner93f4e9d2005-04-27 04:52:23 +000046
47 bool SimplifyFunction(Function *F);
48 void DeleteBasicBlock(BasicBlock *BB);
Chris Lattner0a3f8d52003-08-31 02:47:32 +000049 };
Chris Lattnerc2d3d312006-08-27 22:42:52 +000050 RegisterPass<PruneEH> X("prune-eh", "Remove unused exception handling info");
Chris Lattner0a3f8d52003-08-31 02:47:32 +000051}
52
Devang Patel13058a52007-01-26 00:47:38 +000053Pass *llvm::createPruneEHPass() { return new PruneEH(); }
Chris Lattner75444c72003-08-31 16:30:07 +000054
Chris Lattner0a3f8d52003-08-31 02:47:32 +000055
56bool PruneEH::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
57 CallGraph &CG = getAnalysis<CallGraph>();
Chris Lattner93f4e9d2005-04-27 04:52:23 +000058 bool MadeChange = false;
Chris Lattner0a3f8d52003-08-31 02:47:32 +000059
Chris Lattner93f4e9d2005-04-27 04:52:23 +000060 // First pass, scan all of the functions in the SCC, simplifying them
61 // according to what we know.
62 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
63 if (Function *F = SCC[i]->getFunction())
64 MadeChange |= SimplifyFunction(F);
65
66 // Next, check to see if any callees might throw or if there are any external
Chris Lattner0a3f8d52003-08-31 02:47:32 +000067 // functions in this SCC: if so, we cannot prune any functions in this SCC.
Chris Lattner04ecefe2003-09-08 19:44:26 +000068 // If this SCC includes the unwind instruction, we KNOW it throws, so
Chris Lattner0a3f8d52003-08-31 02:47:32 +000069 // obviously the SCC might throw.
70 //
Chris Lattner93f4e9d2005-04-27 04:52:23 +000071 bool SCCMightUnwind = false, SCCMightReturn = false;
72 for (unsigned i = 0, e = SCC.size();
73 (!SCCMightUnwind || !SCCMightReturn) && i != e; ++i) {
Chris Lattnerd041dcd2004-04-12 04:06:38 +000074 Function *F = SCC[i]->getFunction();
75 if (F == 0 || (F->isExternal() && !F->getIntrinsicID())) {
Chris Lattner7f4f7732005-04-26 23:53:25 +000076 SCCMightUnwind = true;
Chris Lattner93f4e9d2005-04-27 04:52:23 +000077 SCCMightReturn = true;
Chris Lattnerd041dcd2004-04-12 04:06:38 +000078 } else {
Chris Lattner93f4e9d2005-04-27 04:52:23 +000079 if (F->isExternal())
80 SCCMightReturn = true;
81
Chris Lattnerd041dcd2004-04-12 04:06:38 +000082 // Check to see if this function performs an unwind or calls an
83 // unwinding function.
84 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
85 if (isa<UnwindInst>(BB->getTerminator())) { // Uses unwind!
Chris Lattner7f4f7732005-04-26 23:53:25 +000086 SCCMightUnwind = true;
Chris Lattner93f4e9d2005-04-27 04:52:23 +000087 } else if (isa<ReturnInst>(BB->getTerminator())) {
88 SCCMightReturn = true;
Chris Lattnerd041dcd2004-04-12 04:06:38 +000089 }
Chris Lattner56997dd2004-02-08 21:15:59 +000090
Chris Lattnerd041dcd2004-04-12 04:06:38 +000091 // Invoke instructions don't allow unwinding to continue, so we are
92 // only interested in call instructions.
Chris Lattner93f4e9d2005-04-27 04:52:23 +000093 if (!SCCMightUnwind)
94 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
95 if (CallInst *CI = dyn_cast<CallInst>(I)) {
96 if (Function *Callee = CI->getCalledFunction()) {
97 CallGraphNode *CalleeNode = CG[Callee];
98 // If the callee is outside our current SCC, or if it is not
99 // known to throw, then we might throw also.
100 if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end()&&
101 !DoesNotUnwind.count(CalleeNode)) {
102 SCCMightUnwind = true;
103 break;
104 }
105 } else {
106 // Indirect call, it might throw.
Chris Lattner7f4f7732005-04-26 23:53:25 +0000107 SCCMightUnwind = true;
Chris Lattner56997dd2004-02-08 21:15:59 +0000108 break;
109 }
110 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000111 if (SCCMightUnwind && SCCMightReturn) break;
Chris Lattner5a6fa292003-09-15 02:22:50 +0000112 }
Chris Lattnerd041dcd2004-04-12 04:06:38 +0000113 }
114 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000115
116 // If the SCC doesn't unwind or doesn't throw, note this fact.
117 if (!SCCMightUnwind)
118 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
119 DoesNotUnwind.insert(SCC[i]);
120 if (!SCCMightReturn)
121 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
122 DoesNotReturn.insert(SCC[i]);
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000123
124 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000125 // Convert any invoke instructions to non-throwing functions in this node
126 // into call instructions with a branch. This makes the exception blocks
127 // dead.
128 if (Function *F = SCC[i]->getFunction())
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000129 MadeChange |= SimplifyFunction(F);
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000130 }
131
Misha Brukmanb1c93172005-04-21 23:48:37 +0000132 return MadeChange;
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000133}
Brian Gaeke960707c2003-11-11 22:41:34 +0000134
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000135
136// SimplifyFunction - Given information about callees, simplify the specified
137// function if we have invokes to non-unwinding functions or code after calls to
138// no-return functions.
139bool PruneEH::SimplifyFunction(Function *F) {
140 CallGraph &CG = getAnalysis<CallGraph>();
141 bool MadeChange = false;
142 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
143 if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
144 if (Function *F = II->getCalledFunction())
145 if (DoesNotUnwind.count(CG[F])) {
146 // Insert a call instruction before the invoke...
147 std::string Name = II->getName(); II->setName("");
Chris Lattnerd0525a22005-05-09 01:05:50 +0000148 CallInst *Call = new CallInst(II->getCalledValue(),
149 std::vector<Value*>(II->op_begin()+3,
150 II->op_end()),
151 Name, II);
152 Call->setCallingConv(II->getCallingConv());
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000153
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000154 // Anything that used the value produced by the invoke instruction
155 // now uses the value produced by the call instruction.
156 II->replaceAllUsesWith(Call);
157 BasicBlock *UnwindBlock = II->getUnwindDest();
158 UnwindBlock->removePredecessor(II->getParent());
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000159
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000160 // Insert a branch to the normal destination right before the
161 // invoke.
162 new BranchInst(II->getNormalDest(), II);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000163
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000164 // Finally, delete the invoke instruction!
165 BB->getInstList().pop_back();
166
167 // If the unwind block is now dead, nuke it.
168 if (pred_begin(UnwindBlock) == pred_end(UnwindBlock))
169 DeleteBasicBlock(UnwindBlock); // Delete the new BB.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000170
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000171 ++NumRemoved;
172 MadeChange = true;
173 }
174
Chris Lattner36ffb1f2005-04-27 20:12:17 +0000175 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000176 if (CallInst *CI = dyn_cast<CallInst>(I++))
177 if (Function *Callee = CI->getCalledFunction())
178 if (DoesNotReturn.count(CG[Callee]) && !isa<UnreachableInst>(I)) {
179 // This call calls a function that cannot return. Insert an
180 // unreachable instruction after it and simplify the code. Do this
181 // by splitting the BB, adding the unreachable, then deleting the
182 // new BB.
183 BasicBlock *New = BB->splitBasicBlock(I);
184
185 // Remove the uncond branch and add an unreachable.
186 BB->getInstList().pop_back();
187 new UnreachableInst(BB);
188
189 DeleteBasicBlock(New); // Delete the new BB.
190 MadeChange = true;
191 ++NumUnreach;
192 break;
193 }
194
195 }
196 return MadeChange;
197}
198
199/// DeleteBasicBlock - remove the specified basic block from the program,
200/// updating the callgraph to reflect any now-obsolete edges due to calls that
201/// exist in the BB.
202void PruneEH::DeleteBasicBlock(BasicBlock *BB) {
203 assert(pred_begin(BB) == pred_end(BB) && "BB is not dead!");
204 CallGraph &CG = getAnalysis<CallGraph>();
205
206 CallGraphNode *CGN = CG[BB->getParent()];
207 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; ) {
208 --I;
209 if (CallInst *CI = dyn_cast<CallInst>(I)) {
210 if (Function *Callee = CI->getCalledFunction())
211 CGN->removeCallEdgeTo(CG[Callee]);
212 } else if (InvokeInst *II = dyn_cast<InvokeInst>(I)) {
213 if (Function *Callee = II->getCalledFunction())
214 CGN->removeCallEdgeTo(CG[Callee]);
215 }
216 if (!I->use_empty())
217 I->replaceAllUsesWith(UndefValue::get(I->getType()));
218 }
219
220 // Get the list of successors of this block.
221 std::vector<BasicBlock*> Succs(succ_begin(BB), succ_end(BB));
222
223 for (unsigned i = 0, e = Succs.size(); i != e; ++i)
224 Succs[i]->removePredecessor(BB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000225
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000226 BB->eraseFromParent();
227}