blob: 7704c34a952b09e81fd3e06ce7927e59996078ce [file] [log] [blame]
Chris Lattner3e64b2e2003-08-31 02:47:32 +00001//===- PruneEH.cpp - Pass which deletes unused exception handlers ---------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner3e64b2e2003-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 Sandscbb8bad2007-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 Lattner3e64b2e2003-08-31 02:47:32 +000014//
15//===----------------------------------------------------------------------===//
16
Chris Lattner86453c52006-12-19 22:09:18 +000017#define DEBUG_TYPE "prune-eh"
Chris Lattner1e2385b2003-11-21 21:54:22 +000018#include "llvm/Transforms/IPO.h"
Chris Lattner3e64b2e2003-08-31 02:47:32 +000019#include "llvm/CallGraphSCCPass.h"
Chris Lattnerd651f442005-04-27 04:52:23 +000020#include "llvm/Constants.h"
Chris Lattner3e64b2e2003-08-31 02:47:32 +000021#include "llvm/Function.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000022#include "llvm/Instructions.h"
Chris Lattner3e64b2e2003-08-31 02:47:32 +000023#include "llvm/Analysis/CallGraph.h"
Chris Lattner93e985f2007-02-13 02:10:56 +000024#include "llvm/ADT/SmallVector.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/ADT/Statistic.h"
Chris Lattnerd651f442005-04-27 04:52:23 +000026#include "llvm/Support/CFG.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000027#include "llvm/Support/Compiler.h"
Chris Lattner3e64b2e2003-08-31 02:47:32 +000028#include <set>
Chris Lattnerf26801b2004-10-18 15:43:46 +000029#include <algorithm>
Chris Lattner1e2385b2003-11-21 21:54:22 +000030using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000031
Chris Lattner86453c52006-12-19 22:09:18 +000032STATISTIC(NumRemoved, "Number of invokes removed");
33STATISTIC(NumUnreach, "Number of noreturn calls optimized");
Chris Lattner3e64b2e2003-08-31 02:47:32 +000034
Chris Lattner86453c52006-12-19 22:09:18 +000035namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000036 struct VISIBILITY_HIDDEN PruneEH : public CallGraphSCCPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000037 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000038 PruneEH() : CallGraphSCCPass((intptr_t)&ID) {}
39
Chris Lattner3e64b2e2003-08-31 02:47:32 +000040 // runOnSCC - Analyze the SCC, performing the transformation if possible.
41 bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
Chris Lattnerd651f442005-04-27 04:52:23 +000042
43 bool SimplifyFunction(Function *F);
44 void DeleteBasicBlock(BasicBlock *BB);
Chris Lattner3e64b2e2003-08-31 02:47:32 +000045 };
Devang Patel794fd752007-05-01 21:15:47 +000046
Devang Patel19974732007-05-03 01:11:54 +000047 char PruneEH::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000048 RegisterPass<PruneEH> X("prune-eh", "Remove unused exception handling info");
Chris Lattner3e64b2e2003-08-31 02:47:32 +000049}
50
Devang Patelc71ca3c2007-01-26 00:47:38 +000051Pass *llvm::createPruneEHPass() { return new PruneEH(); }
Chris Lattnerf6fb96f2003-08-31 16:30:07 +000052
Chris Lattner3e64b2e2003-08-31 02:47:32 +000053
54bool PruneEH::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
55 CallGraph &CG = getAnalysis<CallGraph>();
Chris Lattnerd651f442005-04-27 04:52:23 +000056 bool MadeChange = false;
Chris Lattner3e64b2e2003-08-31 02:47:32 +000057
Chris Lattnerd651f442005-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 Lattner3e64b2e2003-08-31 02:47:32 +000065 // functions in this SCC: if so, we cannot prune any functions in this SCC.
Chris Lattneree5457c2003-09-08 19:44:26 +000066 // If this SCC includes the unwind instruction, we KNOW it throws, so
Chris Lattner3e64b2e2003-08-31 02:47:32 +000067 // obviously the SCC might throw.
68 //
Chris Lattnerd651f442005-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 Lattnerd08ddd32004-04-12 04:06:38 +000072 Function *F = SCC[i]->getFunction();
Duncan Sandscbb8bad2007-12-10 19:09:40 +000073 if (F == 0) {
Chris Lattnerbc9fa582005-04-26 23:53:25 +000074 SCCMightUnwind = true;
Chris Lattnerd651f442005-04-27 04:52:23 +000075 SCCMightReturn = true;
Duncan Sandscbb8bad2007-12-10 19:09:40 +000076 } else if (F->isDeclaration()) {
Duncan Sands2b0e8992007-12-18 09:59:50 +000077 SCCMightUnwind |= !F->doesNotThrow();
78 SCCMightReturn |= !F->doesNotReturn();
Chris Lattnerd08ddd32004-04-12 04:06:38 +000079 } else {
Duncan Sands2b0e8992007-12-18 09:59:50 +000080 bool CheckUnwind = !SCCMightUnwind && !F->doesNotThrow();
81 bool CheckReturn = !SCCMightReturn && !F->doesNotReturn();
Duncan Sandscbb8bad2007-12-10 19:09:40 +000082
83 if (!CheckUnwind && !CheckReturn)
84 continue;
Chris Lattnerd651f442005-04-27 04:52:23 +000085
Chris Lattnerd08ddd32004-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 Sandscbb8bad2007-12-10 19:09:40 +000089 if (CheckUnwind && isa<UnwindInst>(BB->getTerminator())) {
90 // Uses unwind!
Chris Lattnerbc9fa582005-04-26 23:53:25 +000091 SCCMightUnwind = true;
Duncan Sandscbb8bad2007-12-10 19:09:40 +000092 } else if (CheckReturn && isa<ReturnInst>(BB->getTerminator())) {
Chris Lattnerd651f442005-04-27 04:52:23 +000093 SCCMightReturn = true;
Chris Lattnerd08ddd32004-04-12 04:06:38 +000094 }
Chris Lattnere0def042004-02-08 21:15:59 +000095
Chris Lattnerd08ddd32004-04-12 04:06:38 +000096 // Invoke instructions don't allow unwinding to continue, so we are
97 // only interested in call instructions.
Duncan Sandscbb8bad2007-12-10 19:09:40 +000098 if (CheckUnwind && !SCCMightUnwind)
Chris Lattnerd651f442005-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 Sands2b0e8992007-12-18 09:59:50 +0000101 if (CI->doesNotThrow()) {
Duncan Sandscbb8bad2007-12-10 19:09:40 +0000102 // This call cannot throw.
103 } else if (Function *Callee = CI->getCalledFunction()) {
Chris Lattnerd651f442005-04-27 04:52:23 +0000104 CallGraphNode *CalleeNode = CG[Callee];
Duncan Sandscbb8bad2007-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 Lattnerd651f442005-04-27 04:52:23 +0000108 SCCMightUnwind = true;
109 break;
110 }
111 } else {
112 // Indirect call, it might throw.
Chris Lattnerbc9fa582005-04-26 23:53:25 +0000113 SCCMightUnwind = true;
Chris Lattnere0def042004-02-08 21:15:59 +0000114 break;
115 }
116 }
Chris Lattnerd651f442005-04-27 04:52:23 +0000117 if (SCCMightUnwind && SCCMightReturn) break;
Chris Lattnerad9b5f32003-09-15 02:22:50 +0000118 }
Chris Lattnerd08ddd32004-04-12 04:06:38 +0000119 }
120 }
Chris Lattnerd651f442005-04-27 04:52:23 +0000121
122 // If the SCC doesn't unwind or doesn't throw, note this fact.
Duncan Sandscbb8bad2007-12-10 19:09:40 +0000123 if (!SCCMightUnwind || !SCCMightReturn)
124 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
Duncan Sandsf0c33542007-12-19 21:13:37 +0000125 uint16_t NewAttributes = ParamAttr::None;
Duncan Sandscbb8bad2007-12-10 19:09:40 +0000126
127 if (!SCCMightUnwind)
Duncan Sandsf0c33542007-12-19 21:13:37 +0000128 NewAttributes |= ParamAttr::NoUnwind;
Duncan Sandscbb8bad2007-12-10 19:09:40 +0000129 if (!SCCMightReturn)
Duncan Sandsf0c33542007-12-19 21:13:37 +0000130 NewAttributes |= ParamAttr::NoReturn;
Duncan Sandscbb8bad2007-12-10 19:09:40 +0000131
Duncan Sandsf0c33542007-12-19 21:13:37 +0000132 const ParamAttrsList *PAL = SCC[i]->getFunction()->getParamAttrs();
133 PAL = ParamAttrsList::includeAttrs(PAL, 0, NewAttributes);
Duncan Sandscbb8bad2007-12-10 19:09:40 +0000134 SCC[i]->getFunction()->setParamAttrs(PAL);
135 }
Chris Lattner3e64b2e2003-08-31 02:47:32 +0000136
137 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
Chris Lattner3e64b2e2003-08-31 02:47:32 +0000138 // Convert any invoke instructions to non-throwing functions in this node
139 // into call instructions with a branch. This makes the exception blocks
140 // dead.
141 if (Function *F = SCC[i]->getFunction())
Chris Lattnerd651f442005-04-27 04:52:23 +0000142 MadeChange |= SimplifyFunction(F);
Chris Lattner3e64b2e2003-08-31 02:47:32 +0000143 }
144
Misha Brukmanfd939082005-04-21 23:48:37 +0000145 return MadeChange;
Chris Lattner3e64b2e2003-08-31 02:47:32 +0000146}
Brian Gaeked0fde302003-11-11 22:41:34 +0000147
Chris Lattnerd651f442005-04-27 04:52:23 +0000148
149// SimplifyFunction - Given information about callees, simplify the specified
150// function if we have invokes to non-unwinding functions or code after calls to
151// no-return functions.
152bool PruneEH::SimplifyFunction(Function *F) {
Chris Lattnerd651f442005-04-27 04:52:23 +0000153 bool MadeChange = false;
154 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
155 if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
Duncan Sands2b0e8992007-12-18 09:59:50 +0000156 if (II->doesNotThrow()) {
Duncan Sandscbb8bad2007-12-10 19:09:40 +0000157 SmallVector<Value*, 8> Args(II->op_begin()+3, II->op_end());
158 // Insert a call instruction before the invoke.
159 CallInst *Call = new CallInst(II->getCalledValue(),
160 Args.begin(), Args.end(), "", II);
161 Call->takeName(II);
162 Call->setCallingConv(II->getCallingConv());
163 Call->setParamAttrs(II->getParamAttrs());
Jeff Cohen00b168892005-07-27 06:12:32 +0000164
Duncan Sandscbb8bad2007-12-10 19:09:40 +0000165 // Anything that used the value produced by the invoke instruction
166 // now uses the value produced by the call instruction.
167 II->replaceAllUsesWith(Call);
168 BasicBlock *UnwindBlock = II->getUnwindDest();
169 UnwindBlock->removePredecessor(II->getParent());
Jeff Cohen00b168892005-07-27 06:12:32 +0000170
Duncan Sandscbb8bad2007-12-10 19:09:40 +0000171 // Insert a branch to the normal destination right before the
172 // invoke.
173 new BranchInst(II->getNormalDest(), II);
Jeff Cohen00b168892005-07-27 06:12:32 +0000174
Duncan Sandscbb8bad2007-12-10 19:09:40 +0000175 // Finally, delete the invoke instruction!
176 BB->getInstList().pop_back();
Chris Lattnerd651f442005-04-27 04:52:23 +0000177
Duncan Sandscbb8bad2007-12-10 19:09:40 +0000178 // If the unwind block is now dead, nuke it.
179 if (pred_begin(UnwindBlock) == pred_end(UnwindBlock))
180 DeleteBasicBlock(UnwindBlock); // Delete the new BB.
Jeff Cohen00b168892005-07-27 06:12:32 +0000181
Duncan Sandscbb8bad2007-12-10 19:09:40 +0000182 ++NumRemoved;
183 MadeChange = true;
184 }
Chris Lattnerd651f442005-04-27 04:52:23 +0000185
Chris Lattner209a0ae2005-04-27 20:12:17 +0000186 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
Chris Lattnerd651f442005-04-27 04:52:23 +0000187 if (CallInst *CI = dyn_cast<CallInst>(I++))
Duncan Sands2b0e8992007-12-18 09:59:50 +0000188 if (CI->doesNotReturn() && !isa<UnreachableInst>(I)) {
Duncan Sandscbb8bad2007-12-10 19:09:40 +0000189 // This call calls a function that cannot return. Insert an
190 // unreachable instruction after it and simplify the code. Do this
191 // by splitting the BB, adding the unreachable, then deleting the
192 // new BB.
193 BasicBlock *New = BB->splitBasicBlock(I);
Chris Lattnerd651f442005-04-27 04:52:23 +0000194
Duncan Sandscbb8bad2007-12-10 19:09:40 +0000195 // Remove the uncond branch and add an unreachable.
196 BB->getInstList().pop_back();
197 new UnreachableInst(BB);
Chris Lattnerd651f442005-04-27 04:52:23 +0000198
Duncan Sandscbb8bad2007-12-10 19:09:40 +0000199 DeleteBasicBlock(New); // Delete the new BB.
200 MadeChange = true;
201 ++NumUnreach;
202 break;
203 }
Chris Lattnerd651f442005-04-27 04:52:23 +0000204
205 }
206 return MadeChange;
207}
208
209/// DeleteBasicBlock - remove the specified basic block from the program,
210/// updating the callgraph to reflect any now-obsolete edges due to calls that
211/// exist in the BB.
212void PruneEH::DeleteBasicBlock(BasicBlock *BB) {
213 assert(pred_begin(BB) == pred_end(BB) && "BB is not dead!");
214 CallGraph &CG = getAnalysis<CallGraph>();
215
216 CallGraphNode *CGN = CG[BB->getParent()];
217 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; ) {
218 --I;
219 if (CallInst *CI = dyn_cast<CallInst>(I)) {
220 if (Function *Callee = CI->getCalledFunction())
221 CGN->removeCallEdgeTo(CG[Callee]);
222 } else if (InvokeInst *II = dyn_cast<InvokeInst>(I)) {
223 if (Function *Callee = II->getCalledFunction())
224 CGN->removeCallEdgeTo(CG[Callee]);
225 }
226 if (!I->use_empty())
227 I->replaceAllUsesWith(UndefValue::get(I->getType()));
228 }
229
230 // Get the list of successors of this block.
231 std::vector<BasicBlock*> Succs(succ_begin(BB), succ_end(BB));
232
233 for (unsigned i = 0, e = Succs.size(); i != e; ++i)
234 Succs[i]->removePredecessor(BB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000235
Chris Lattnerd651f442005-04-27 04:52:23 +0000236 BB->eraseFromParent();
237}