blob: 69bb1f67c74cf69d263b5cd9747c496ac62d26bb [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
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 Lattner0a3f8d52003-08-31 02:47:32 +000028#include <set>
Chris Lattner88a8a322004-10-18 15:43:46 +000029#include <algorithm>
Chris Lattnerf52e03c2003-11-21 21:54:22 +000030using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000031
Chris Lattner1631bcb2006-12-19 22:09:18 +000032STATISTIC(NumRemoved, "Number of invokes removed");
33STATISTIC(NumUnreach, "Number of noreturn calls optimized");
Chris Lattner0a3f8d52003-08-31 02:47:32 +000034
Chris Lattner1631bcb2006-12-19 22:09:18 +000035namespace {
Reid Spencer557ab152007-02-05 23:32:05 +000036 struct VISIBILITY_HIDDEN PruneEH : public CallGraphSCCPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000037 static char ID; // Pass identification, replacement for typeid
Devang Patel09f162c2007-05-01 21:15:47 +000038 PruneEH() : CallGraphSCCPass((intptr_t)&ID) {}
39
Chris Lattner0a3f8d52003-08-31 02:47:32 +000040 // runOnSCC - Analyze the SCC, performing the transformation if possible.
41 bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
Chris Lattner93f4e9d2005-04-27 04:52:23 +000042
43 bool SimplifyFunction(Function *F);
44 void DeleteBasicBlock(BasicBlock *BB);
Chris Lattner0a3f8d52003-08-31 02:47:32 +000045 };
Devang Patel09f162c2007-05-01 21:15:47 +000046
Devang Patel8c78a0b2007-05-03 01:11:54 +000047 char PruneEH::ID = 0;
Chris Lattnerc2d3d312006-08-27 22:42:52 +000048 RegisterPass<PruneEH> X("prune-eh", "Remove unused exception handling info");
Chris Lattner0a3f8d52003-08-31 02:47:32 +000049}
50
Devang Patel13058a52007-01-26 00:47:38 +000051Pass *llvm::createPruneEHPass() { return new PruneEH(); }
Chris Lattner75444c72003-08-31 16:30:07 +000052
Chris Lattner0a3f8d52003-08-31 02:47:32 +000053
54bool PruneEH::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
55 CallGraph &CG = getAnalysis<CallGraph>();
Chris Lattner93f4e9d2005-04-27 04:52:23 +000056 bool MadeChange = false;
Chris Lattner0a3f8d52003-08-31 02:47:32 +000057
Chris Lattner93f4e9d2005-04-27 04:52:23 +000058 // First pass, scan all of the functions in the SCC, simplifying them
59 // according to what we know.
60 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
61 if (Function *F = SCC[i]->getFunction())
62 MadeChange |= SimplifyFunction(F);
63
64 // Next, check to see if any callees might throw or if there are any external
Chris Lattner0a3f8d52003-08-31 02:47:32 +000065 // functions in this SCC: if so, we cannot prune any functions in this SCC.
Chris Lattner04ecefe2003-09-08 19:44:26 +000066 // If this SCC includes the unwind instruction, we KNOW it throws, so
Chris Lattner0a3f8d52003-08-31 02:47:32 +000067 // obviously the SCC might throw.
68 //
Chris Lattner93f4e9d2005-04-27 04:52:23 +000069 bool SCCMightUnwind = false, SCCMightReturn = false;
70 for (unsigned i = 0, e = SCC.size();
71 (!SCCMightUnwind || !SCCMightReturn) && i != e; ++i) {
Chris Lattnerd041dcd2004-04-12 04:06:38 +000072 Function *F = SCC[i]->getFunction();
Duncan Sands9f76be62007-12-10 19:09:40 +000073 if (F == 0) {
Chris Lattner7f4f7732005-04-26 23:53:25 +000074 SCCMightUnwind = true;
Chris Lattner93f4e9d2005-04-27 04:52:23 +000075 SCCMightReturn = true;
Duncan Sands9f76be62007-12-10 19:09:40 +000076 } else if (F->isDeclaration()) {
Duncan Sands3353ed02007-12-18 09:59:50 +000077 SCCMightUnwind |= !F->doesNotThrow();
78 SCCMightReturn |= !F->doesNotReturn();
Chris Lattnerd041dcd2004-04-12 04:06:38 +000079 } else {
Duncan Sands3353ed02007-12-18 09:59:50 +000080 bool CheckUnwind = !SCCMightUnwind && !F->doesNotThrow();
81 bool CheckReturn = !SCCMightReturn && !F->doesNotReturn();
Duncan Sands9f76be62007-12-10 19:09:40 +000082
83 if (!CheckUnwind && !CheckReturn)
84 continue;
Chris Lattner93f4e9d2005-04-27 04:52:23 +000085
Chris Lattnerd041dcd2004-04-12 04:06:38 +000086 // Check to see if this function performs an unwind or calls an
87 // unwinding function.
88 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Duncan Sands9f76be62007-12-10 19:09:40 +000089 if (CheckUnwind && isa<UnwindInst>(BB->getTerminator())) {
90 // Uses unwind!
Chris Lattner7f4f7732005-04-26 23:53:25 +000091 SCCMightUnwind = true;
Duncan Sands9f76be62007-12-10 19:09:40 +000092 } else if (CheckReturn && isa<ReturnInst>(BB->getTerminator())) {
Chris Lattner93f4e9d2005-04-27 04:52:23 +000093 SCCMightReturn = true;
Chris Lattnerd041dcd2004-04-12 04:06:38 +000094 }
Chris Lattner56997dd2004-02-08 21:15:59 +000095
Chris Lattnerd041dcd2004-04-12 04:06:38 +000096 // Invoke instructions don't allow unwinding to continue, so we are
97 // only interested in call instructions.
Duncan Sands9f76be62007-12-10 19:09:40 +000098 if (CheckUnwind && !SCCMightUnwind)
Chris Lattner93f4e9d2005-04-27 04:52:23 +000099 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
100 if (CallInst *CI = dyn_cast<CallInst>(I)) {
Duncan Sands3353ed02007-12-18 09:59:50 +0000101 if (CI->doesNotThrow()) {
Duncan Sands9f76be62007-12-10 19:09:40 +0000102 // This call cannot throw.
103 } else if (Function *Callee = CI->getCalledFunction()) {
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000104 CallGraphNode *CalleeNode = CG[Callee];
Duncan Sands9f76be62007-12-10 19:09:40 +0000105 // If the callee is outside our current SCC then we may
106 // throw because it might.
107 if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end()){
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000108 SCCMightUnwind = true;
109 break;
110 }
111 } else {
112 // Indirect call, it might throw.
Chris Lattner7f4f7732005-04-26 23:53:25 +0000113 SCCMightUnwind = true;
Chris Lattner56997dd2004-02-08 21:15:59 +0000114 break;
115 }
116 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000117 if (SCCMightUnwind && SCCMightReturn) break;
Chris Lattner5a6fa292003-09-15 02:22:50 +0000118 }
Chris Lattnerd041dcd2004-04-12 04:06:38 +0000119 }
120 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000121
122 // If the SCC doesn't unwind or doesn't throw, note this fact.
Duncan Sands9f76be62007-12-10 19:09:40 +0000123 if (!SCCMightUnwind || !SCCMightReturn)
124 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
125 const ParamAttrsList *PAL = SCC[i]->getFunction()->getParamAttrs();
126 uint16_t RAttributes = PAL ? PAL->getParamAttrs(0) : 0;
127
128 if (!SCCMightUnwind)
129 RAttributes |= ParamAttr::NoUnwind;
130 if (!SCCMightReturn)
131 RAttributes |= ParamAttr::NoReturn;
132
133 ParamAttrsVector modVec;
134 modVec.push_back(ParamAttrsWithIndex::get(0, RAttributes));
135 PAL = ParamAttrsList::getModified(PAL, modVec);
136 SCC[i]->getFunction()->setParamAttrs(PAL);
137 }
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000138
139 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000140 // Convert any invoke instructions to non-throwing functions in this node
141 // into call instructions with a branch. This makes the exception blocks
142 // dead.
143 if (Function *F = SCC[i]->getFunction())
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000144 MadeChange |= SimplifyFunction(F);
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000145 }
146
Misha Brukmanb1c93172005-04-21 23:48:37 +0000147 return MadeChange;
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000148}
Brian Gaeke960707c2003-11-11 22:41:34 +0000149
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000150
151// SimplifyFunction - Given information about callees, simplify the specified
152// function if we have invokes to non-unwinding functions or code after calls to
153// no-return functions.
154bool PruneEH::SimplifyFunction(Function *F) {
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000155 bool MadeChange = false;
156 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
157 if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
Duncan Sands3353ed02007-12-18 09:59:50 +0000158 if (II->doesNotThrow()) {
Duncan Sands9f76be62007-12-10 19:09:40 +0000159 SmallVector<Value*, 8> Args(II->op_begin()+3, II->op_end());
160 // Insert a call instruction before the invoke.
161 CallInst *Call = new CallInst(II->getCalledValue(),
162 Args.begin(), Args.end(), "", II);
163 Call->takeName(II);
164 Call->setCallingConv(II->getCallingConv());
165 Call->setParamAttrs(II->getParamAttrs());
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000166
Duncan Sands9f76be62007-12-10 19:09:40 +0000167 // Anything that used the value produced by the invoke instruction
168 // now uses the value produced by the call instruction.
169 II->replaceAllUsesWith(Call);
170 BasicBlock *UnwindBlock = II->getUnwindDest();
171 UnwindBlock->removePredecessor(II->getParent());
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000172
Duncan Sands9f76be62007-12-10 19:09:40 +0000173 // Insert a branch to the normal destination right before the
174 // invoke.
175 new BranchInst(II->getNormalDest(), II);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000176
Duncan Sands9f76be62007-12-10 19:09:40 +0000177 // Finally, delete the invoke instruction!
178 BB->getInstList().pop_back();
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000179
Duncan Sands9f76be62007-12-10 19:09:40 +0000180 // If the unwind block is now dead, nuke it.
181 if (pred_begin(UnwindBlock) == pred_end(UnwindBlock))
182 DeleteBasicBlock(UnwindBlock); // Delete the new BB.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000183
Duncan Sands9f76be62007-12-10 19:09:40 +0000184 ++NumRemoved;
185 MadeChange = true;
186 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000187
Chris Lattner36ffb1f2005-04-27 20:12:17 +0000188 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000189 if (CallInst *CI = dyn_cast<CallInst>(I++))
Duncan Sands3353ed02007-12-18 09:59:50 +0000190 if (CI->doesNotReturn() && !isa<UnreachableInst>(I)) {
Duncan Sands9f76be62007-12-10 19:09:40 +0000191 // This call calls a function that cannot return. Insert an
192 // unreachable instruction after it and simplify the code. Do this
193 // by splitting the BB, adding the unreachable, then deleting the
194 // new BB.
195 BasicBlock *New = BB->splitBasicBlock(I);
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000196
Duncan Sands9f76be62007-12-10 19:09:40 +0000197 // Remove the uncond branch and add an unreachable.
198 BB->getInstList().pop_back();
199 new UnreachableInst(BB);
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000200
Duncan Sands9f76be62007-12-10 19:09:40 +0000201 DeleteBasicBlock(New); // Delete the new BB.
202 MadeChange = true;
203 ++NumUnreach;
204 break;
205 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000206
207 }
208 return MadeChange;
209}
210
211/// DeleteBasicBlock - remove the specified basic block from the program,
212/// updating the callgraph to reflect any now-obsolete edges due to calls that
213/// exist in the BB.
214void PruneEH::DeleteBasicBlock(BasicBlock *BB) {
215 assert(pred_begin(BB) == pred_end(BB) && "BB is not dead!");
216 CallGraph &CG = getAnalysis<CallGraph>();
217
218 CallGraphNode *CGN = CG[BB->getParent()];
219 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; ) {
220 --I;
221 if (CallInst *CI = dyn_cast<CallInst>(I)) {
222 if (Function *Callee = CI->getCalledFunction())
223 CGN->removeCallEdgeTo(CG[Callee]);
224 } else if (InvokeInst *II = dyn_cast<InvokeInst>(I)) {
225 if (Function *Callee = II->getCalledFunction())
226 CGN->removeCallEdgeTo(CG[Callee]);
227 }
228 if (!I->use_empty())
229 I->replaceAllUsesWith(UndefValue::get(I->getType()));
230 }
231
232 // Get the list of successors of this block.
233 std::vector<BasicBlock*> Succs(succ_begin(BB), succ_end(BB));
234
235 for (unsigned i = 0, e = Succs.size(); i != e; ++i)
236 Succs[i]->removePredecessor(BB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000237
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000238 BB->eraseFromParent();
239}