blob: 4fe139a38002d934bab0d2ec79e5f55b5d960a5a [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//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Duncan Sands9f76be62007-12-10 19:09:40 +000012// throw an exception, and marking functions 'nounwind' if they cannot throw.
13// It implements this as a bottom-up traversal of the call-graph.
Chris Lattner0a3f8d52003-08-31 02:47:32 +000014//
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"
Misha Brukman63b38bd2004-07-29 17:30:56 +000022#include "llvm/Instructions.h"
Chris Lattner0a3f8d52003-08-31 02:47:32 +000023#include "llvm/Analysis/CallGraph.h"
Chris Lattnera06a8fd2007-02-13 02:10:56 +000024#include "llvm/ADT/SmallVector.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"
Reid Spencer557ab152007-02-05 23:32:05 +000027#include "llvm/Support/Compiler.h"
Chris Lattnerd2b0e152008-01-02 23:41:05 +000028#include "llvm/ParameterAttributes.h"
Chris Lattner0a3f8d52003-08-31 02:47:32 +000029#include <set>
Chris Lattner88a8a322004-10-18 15:43:46 +000030#include <algorithm>
Chris Lattnerf52e03c2003-11-21 21:54:22 +000031using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000032
Chris Lattner1631bcb2006-12-19 22:09:18 +000033STATISTIC(NumRemoved, "Number of invokes removed");
34STATISTIC(NumUnreach, "Number of noreturn calls optimized");
Chris Lattner0a3f8d52003-08-31 02:47:32 +000035
Chris Lattner1631bcb2006-12-19 22:09:18 +000036namespace {
Reid Spencer557ab152007-02-05 23:32:05 +000037 struct VISIBILITY_HIDDEN PruneEH : public CallGraphSCCPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000038 static char ID; // Pass identification, replacement for typeid
Devang Patel09f162c2007-05-01 21:15:47 +000039 PruneEH() : CallGraphSCCPass((intptr_t)&ID) {}
40
Chris Lattner0a3f8d52003-08-31 02:47:32 +000041 // runOnSCC - Analyze the SCC, performing the transformation if possible.
42 bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
Chris Lattner93f4e9d2005-04-27 04:52:23 +000043
44 bool SimplifyFunction(Function *F);
45 void DeleteBasicBlock(BasicBlock *BB);
Chris Lattner0a3f8d52003-08-31 02:47:32 +000046 };
Devang Patel09f162c2007-05-01 21:15:47 +000047
Devang Patel8c78a0b2007-05-03 01:11:54 +000048 char PruneEH::ID = 0;
Chris Lattnerc2d3d312006-08-27 22:42:52 +000049 RegisterPass<PruneEH> X("prune-eh", "Remove unused exception handling info");
Chris Lattner0a3f8d52003-08-31 02:47:32 +000050}
51
Devang Patel13058a52007-01-26 00:47:38 +000052Pass *llvm::createPruneEHPass() { return new PruneEH(); }
Chris Lattner75444c72003-08-31 16:30:07 +000053
Chris Lattner0a3f8d52003-08-31 02:47:32 +000054
55bool PruneEH::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
56 CallGraph &CG = getAnalysis<CallGraph>();
Chris Lattner93f4e9d2005-04-27 04:52:23 +000057 bool MadeChange = false;
Chris Lattner0a3f8d52003-08-31 02:47:32 +000058
Chris Lattner93f4e9d2005-04-27 04:52:23 +000059 // First pass, scan all of the functions in the SCC, simplifying them
60 // according to what we know.
61 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
62 if (Function *F = SCC[i]->getFunction())
63 MadeChange |= SimplifyFunction(F);
64
65 // Next, check to see if any callees might throw or if there are any external
Chris Lattner0a3f8d52003-08-31 02:47:32 +000066 // functions in this SCC: if so, we cannot prune any functions in this SCC.
Chris Lattner04ecefe2003-09-08 19:44:26 +000067 // If this SCC includes the unwind instruction, we KNOW it throws, so
Chris Lattner0a3f8d52003-08-31 02:47:32 +000068 // obviously the SCC might throw.
69 //
Chris Lattner93f4e9d2005-04-27 04:52:23 +000070 bool SCCMightUnwind = false, SCCMightReturn = false;
71 for (unsigned i = 0, e = SCC.size();
72 (!SCCMightUnwind || !SCCMightReturn) && i != e; ++i) {
Chris Lattnerd041dcd2004-04-12 04:06:38 +000073 Function *F = SCC[i]->getFunction();
Duncan Sands9f76be62007-12-10 19:09:40 +000074 if (F == 0) {
Chris Lattner7f4f7732005-04-26 23:53:25 +000075 SCCMightUnwind = true;
Chris Lattner93f4e9d2005-04-27 04:52:23 +000076 SCCMightReturn = true;
Duncan Sands9f76be62007-12-10 19:09:40 +000077 } else if (F->isDeclaration()) {
Duncan Sands3353ed02007-12-18 09:59:50 +000078 SCCMightUnwind |= !F->doesNotThrow();
79 SCCMightReturn |= !F->doesNotReturn();
Chris Lattnerd041dcd2004-04-12 04:06:38 +000080 } else {
Duncan Sands3353ed02007-12-18 09:59:50 +000081 bool CheckUnwind = !SCCMightUnwind && !F->doesNotThrow();
82 bool CheckReturn = !SCCMightReturn && !F->doesNotReturn();
Duncan Sands9f76be62007-12-10 19:09:40 +000083
84 if (!CheckUnwind && !CheckReturn)
85 continue;
Chris Lattner93f4e9d2005-04-27 04:52:23 +000086
Chris Lattnerd041dcd2004-04-12 04:06:38 +000087 // Check to see if this function performs an unwind or calls an
88 // unwinding function.
89 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Duncan Sands9f76be62007-12-10 19:09:40 +000090 if (CheckUnwind && isa<UnwindInst>(BB->getTerminator())) {
91 // Uses unwind!
Chris Lattner7f4f7732005-04-26 23:53:25 +000092 SCCMightUnwind = true;
Duncan Sands9f76be62007-12-10 19:09:40 +000093 } else if (CheckReturn && isa<ReturnInst>(BB->getTerminator())) {
Chris Lattner93f4e9d2005-04-27 04:52:23 +000094 SCCMightReturn = true;
Chris Lattnerd041dcd2004-04-12 04:06:38 +000095 }
Chris Lattner56997dd2004-02-08 21:15:59 +000096
Chris Lattnerd041dcd2004-04-12 04:06:38 +000097 // Invoke instructions don't allow unwinding to continue, so we are
98 // only interested in call instructions.
Duncan Sands9f76be62007-12-10 19:09:40 +000099 if (CheckUnwind && !SCCMightUnwind)
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000100 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
101 if (CallInst *CI = dyn_cast<CallInst>(I)) {
Duncan Sands3353ed02007-12-18 09:59:50 +0000102 if (CI->doesNotThrow()) {
Duncan Sands9f76be62007-12-10 19:09:40 +0000103 // This call cannot throw.
104 } else if (Function *Callee = CI->getCalledFunction()) {
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000105 CallGraphNode *CalleeNode = CG[Callee];
Duncan Sands9f76be62007-12-10 19:09:40 +0000106 // If the callee is outside our current SCC then we may
107 // throw because it might.
108 if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end()){
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000109 SCCMightUnwind = true;
110 break;
111 }
112 } else {
113 // Indirect call, it might throw.
Chris Lattner7f4f7732005-04-26 23:53:25 +0000114 SCCMightUnwind = true;
Chris Lattner56997dd2004-02-08 21:15:59 +0000115 break;
116 }
117 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000118 if (SCCMightUnwind && SCCMightReturn) break;
Chris Lattner5a6fa292003-09-15 02:22:50 +0000119 }
Chris Lattnerd041dcd2004-04-12 04:06:38 +0000120 }
121 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000122
123 // If the SCC doesn't unwind or doesn't throw, note this fact.
Duncan Sands9f76be62007-12-10 19:09:40 +0000124 if (!SCCMightUnwind || !SCCMightReturn)
125 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
Duncan Sandsaa31b922007-12-19 21:13:37 +0000126 uint16_t NewAttributes = ParamAttr::None;
Duncan Sands9f76be62007-12-10 19:09:40 +0000127
128 if (!SCCMightUnwind)
Duncan Sandsaa31b922007-12-19 21:13:37 +0000129 NewAttributes |= ParamAttr::NoUnwind;
Duncan Sands9f76be62007-12-10 19:09:40 +0000130 if (!SCCMightReturn)
Duncan Sandsaa31b922007-12-19 21:13:37 +0000131 NewAttributes |= ParamAttr::NoReturn;
Duncan Sands9f76be62007-12-10 19:09:40 +0000132
Duncan Sandsaa31b922007-12-19 21:13:37 +0000133 const ParamAttrsList *PAL = SCC[i]->getFunction()->getParamAttrs();
134 PAL = ParamAttrsList::includeAttrs(PAL, 0, NewAttributes);
Duncan Sands9f76be62007-12-10 19:09:40 +0000135 SCC[i]->getFunction()->setParamAttrs(PAL);
136 }
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000137
138 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000139 // Convert any invoke instructions to non-throwing functions in this node
140 // into call instructions with a branch. This makes the exception blocks
141 // dead.
142 if (Function *F = SCC[i]->getFunction())
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000143 MadeChange |= SimplifyFunction(F);
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000144 }
145
Misha Brukmanb1c93172005-04-21 23:48:37 +0000146 return MadeChange;
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000147}
Brian Gaeke960707c2003-11-11 22:41:34 +0000148
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000149
150// SimplifyFunction - Given information about callees, simplify the specified
151// function if we have invokes to non-unwinding functions or code after calls to
152// no-return functions.
153bool PruneEH::SimplifyFunction(Function *F) {
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000154 bool MadeChange = false;
155 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
156 if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
Duncan Sands3353ed02007-12-18 09:59:50 +0000157 if (II->doesNotThrow()) {
Duncan Sands9f76be62007-12-10 19:09:40 +0000158 SmallVector<Value*, 8> Args(II->op_begin()+3, II->op_end());
159 // Insert a call instruction before the invoke.
160 CallInst *Call = new CallInst(II->getCalledValue(),
161 Args.begin(), Args.end(), "", II);
162 Call->takeName(II);
163 Call->setCallingConv(II->getCallingConv());
164 Call->setParamAttrs(II->getParamAttrs());
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000165
Duncan Sands9f76be62007-12-10 19:09:40 +0000166 // Anything that used the value produced by the invoke instruction
167 // now uses the value produced by the call instruction.
168 II->replaceAllUsesWith(Call);
169 BasicBlock *UnwindBlock = II->getUnwindDest();
170 UnwindBlock->removePredecessor(II->getParent());
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000171
Duncan Sands9f76be62007-12-10 19:09:40 +0000172 // Insert a branch to the normal destination right before the
173 // invoke.
174 new BranchInst(II->getNormalDest(), II);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000175
Duncan Sands9f76be62007-12-10 19:09:40 +0000176 // Finally, delete the invoke instruction!
177 BB->getInstList().pop_back();
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000178
Duncan Sands9f76be62007-12-10 19:09:40 +0000179 // If the unwind block is now dead, nuke it.
180 if (pred_begin(UnwindBlock) == pred_end(UnwindBlock))
181 DeleteBasicBlock(UnwindBlock); // Delete the new BB.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000182
Duncan Sands9f76be62007-12-10 19:09:40 +0000183 ++NumRemoved;
184 MadeChange = true;
185 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000186
Chris Lattner36ffb1f2005-04-27 20:12:17 +0000187 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000188 if (CallInst *CI = dyn_cast<CallInst>(I++))
Duncan Sands3353ed02007-12-18 09:59:50 +0000189 if (CI->doesNotReturn() && !isa<UnreachableInst>(I)) {
Duncan Sands9f76be62007-12-10 19:09:40 +0000190 // This call calls a function that cannot return. Insert an
191 // unreachable instruction after it and simplify the code. Do this
192 // by splitting the BB, adding the unreachable, then deleting the
193 // new BB.
194 BasicBlock *New = BB->splitBasicBlock(I);
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000195
Duncan Sands9f76be62007-12-10 19:09:40 +0000196 // Remove the uncond branch and add an unreachable.
197 BB->getInstList().pop_back();
198 new UnreachableInst(BB);
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000199
Duncan Sands9f76be62007-12-10 19:09:40 +0000200 DeleteBasicBlock(New); // Delete the new BB.
201 MadeChange = true;
202 ++NumUnreach;
203 break;
204 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000205
206 }
207 return MadeChange;
208}
209
210/// DeleteBasicBlock - remove the specified basic block from the program,
211/// updating the callgraph to reflect any now-obsolete edges due to calls that
212/// exist in the BB.
213void PruneEH::DeleteBasicBlock(BasicBlock *BB) {
214 assert(pred_begin(BB) == pred_end(BB) && "BB is not dead!");
215 CallGraph &CG = getAnalysis<CallGraph>();
216
217 CallGraphNode *CGN = CG[BB->getParent()];
218 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; ) {
219 --I;
220 if (CallInst *CI = dyn_cast<CallInst>(I)) {
221 if (Function *Callee = CI->getCalledFunction())
222 CGN->removeCallEdgeTo(CG[Callee]);
223 } else if (InvokeInst *II = dyn_cast<InvokeInst>(I)) {
224 if (Function *Callee = II->getCalledFunction())
225 CGN->removeCallEdgeTo(CG[Callee]);
226 }
227 if (!I->use_empty())
228 I->replaceAllUsesWith(UndefValue::get(I->getType()));
229 }
230
231 // Get the list of successors of this block.
232 std::vector<BasicBlock*> Succs(succ_begin(BB), succ_end(BB));
233
234 for (unsigned i = 0, e = Succs.size(); i != e; ++i)
235 Succs[i]->removePredecessor(BB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000236
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000237 BB->eraseFromParent();
238}