blob: 6f497e24e75e230fb96ce80ba4e0f43a6f4119f0 [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//
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 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
12// throw an exception. It implements this as a bottom-up traversal of the
13// call-graph.
14//
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"
22#include "llvm/Intrinsics.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000023#include "llvm/Instructions.h"
Chris Lattner3e64b2e2003-08-31 02:47:32 +000024#include "llvm/Analysis/CallGraph.h"
Chris Lattner93e985f2007-02-13 02:10:56 +000025#include "llvm/ADT/SmallVector.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/ADT/Statistic.h"
Chris Lattnerd651f442005-04-27 04:52:23 +000027#include "llvm/Support/CFG.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000028#include "llvm/Support/Compiler.h"
Chris Lattner3e64b2e2003-08-31 02:47:32 +000029#include <set>
Chris Lattnerf26801b2004-10-18 15:43:46 +000030#include <algorithm>
Chris Lattner1e2385b2003-11-21 21:54:22 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattner86453c52006-12-19 22:09:18 +000033STATISTIC(NumRemoved, "Number of invokes removed");
34STATISTIC(NumUnreach, "Number of noreturn calls optimized");
Chris Lattner3e64b2e2003-08-31 02:47:32 +000035
Chris Lattner86453c52006-12-19 22:09:18 +000036namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000037 struct VISIBILITY_HIDDEN PruneEH : public CallGraphSCCPass {
Devang Patel19974732007-05-03 01:11:54 +000038 static char ID; // Pass identifcation, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000039 PruneEH() : CallGraphSCCPass((intptr_t)&ID) {}
40
Chris Lattnerbc9fa582005-04-26 23:53:25 +000041 /// DoesNotUnwind - This set contains all of the functions which we have
Chris Lattnerd651f442005-04-27 04:52:23 +000042 /// determined cannot unwind.
Chris Lattnerbc9fa582005-04-26 23:53:25 +000043 std::set<CallGraphNode*> DoesNotUnwind;
Chris Lattner3e64b2e2003-08-31 02:47:32 +000044
Chris Lattnerd651f442005-04-27 04:52:23 +000045 /// DoesNotReturn - This set contains all of the functions which we have
46 /// determined cannot return normally (but might unwind).
47 std::set<CallGraphNode*> DoesNotReturn;
48
Chris Lattner3e64b2e2003-08-31 02:47:32 +000049 // runOnSCC - Analyze the SCC, performing the transformation if possible.
50 bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
Chris Lattnerd651f442005-04-27 04:52:23 +000051
52 bool SimplifyFunction(Function *F);
53 void DeleteBasicBlock(BasicBlock *BB);
Chris Lattner3e64b2e2003-08-31 02:47:32 +000054 };
Devang Patel794fd752007-05-01 21:15:47 +000055
Devang Patel19974732007-05-03 01:11:54 +000056 char PruneEH::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000057 RegisterPass<PruneEH> X("prune-eh", "Remove unused exception handling info");
Chris Lattner3e64b2e2003-08-31 02:47:32 +000058}
59
Devang Patelc71ca3c2007-01-26 00:47:38 +000060Pass *llvm::createPruneEHPass() { return new PruneEH(); }
Chris Lattnerf6fb96f2003-08-31 16:30:07 +000061
Chris Lattner3e64b2e2003-08-31 02:47:32 +000062
63bool PruneEH::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
64 CallGraph &CG = getAnalysis<CallGraph>();
Chris Lattnerd651f442005-04-27 04:52:23 +000065 bool MadeChange = false;
Chris Lattner3e64b2e2003-08-31 02:47:32 +000066
Chris Lattnerd651f442005-04-27 04:52:23 +000067 // First pass, scan all of the functions in the SCC, simplifying them
68 // according to what we know.
69 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
70 if (Function *F = SCC[i]->getFunction())
71 MadeChange |= SimplifyFunction(F);
72
73 // Next, check to see if any callees might throw or if there are any external
Chris Lattner3e64b2e2003-08-31 02:47:32 +000074 // functions in this SCC: if so, we cannot prune any functions in this SCC.
Chris Lattneree5457c2003-09-08 19:44:26 +000075 // If this SCC includes the unwind instruction, we KNOW it throws, so
Chris Lattner3e64b2e2003-08-31 02:47:32 +000076 // obviously the SCC might throw.
77 //
Chris Lattnerd651f442005-04-27 04:52:23 +000078 bool SCCMightUnwind = false, SCCMightReturn = false;
79 for (unsigned i = 0, e = SCC.size();
80 (!SCCMightUnwind || !SCCMightReturn) && i != e; ++i) {
Chris Lattnerd08ddd32004-04-12 04:06:38 +000081 Function *F = SCC[i]->getFunction();
Reid Spencer5cbf9852007-01-30 20:08:39 +000082 if (F == 0 || (F->isDeclaration() && !F->getIntrinsicID())) {
Chris Lattnerbc9fa582005-04-26 23:53:25 +000083 SCCMightUnwind = true;
Chris Lattnerd651f442005-04-27 04:52:23 +000084 SCCMightReturn = true;
Chris Lattnerd08ddd32004-04-12 04:06:38 +000085 } else {
Reid Spencer5cbf9852007-01-30 20:08:39 +000086 if (F->isDeclaration())
Chris Lattnerd651f442005-04-27 04:52:23 +000087 SCCMightReturn = true;
88
Chris Lattnerd08ddd32004-04-12 04:06:38 +000089 // Check to see if this function performs an unwind or calls an
90 // unwinding function.
91 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
92 if (isa<UnwindInst>(BB->getTerminator())) { // Uses unwind!
Chris Lattnerbc9fa582005-04-26 23:53:25 +000093 SCCMightUnwind = true;
Chris Lattnerd651f442005-04-27 04:52:23 +000094 } else if (isa<ReturnInst>(BB->getTerminator())) {
95 SCCMightReturn = true;
Chris Lattnerd08ddd32004-04-12 04:06:38 +000096 }
Chris Lattnere0def042004-02-08 21:15:59 +000097
Chris Lattnerd08ddd32004-04-12 04:06:38 +000098 // Invoke instructions don't allow unwinding to continue, so we are
99 // only interested in call instructions.
Chris Lattnerd651f442005-04-27 04:52:23 +0000100 if (!SCCMightUnwind)
101 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
102 if (CallInst *CI = dyn_cast<CallInst>(I)) {
103 if (Function *Callee = CI->getCalledFunction()) {
104 CallGraphNode *CalleeNode = CG[Callee];
105 // If the callee is outside our current SCC, or if it is not
106 // known to throw, then we might throw also.
107 if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end()&&
108 !DoesNotUnwind.count(CalleeNode)) {
109 SCCMightUnwind = true;
110 break;
111 }
112 } else {
113 // Indirect call, it might throw.
Chris Lattnerbc9fa582005-04-26 23:53:25 +0000114 SCCMightUnwind = true;
Chris Lattnere0def042004-02-08 21:15:59 +0000115 break;
116 }
117 }
Chris Lattnerd651f442005-04-27 04:52:23 +0000118 if (SCCMightUnwind && SCCMightReturn) break;
Chris Lattnerad9b5f32003-09-15 02:22:50 +0000119 }
Chris Lattnerd08ddd32004-04-12 04:06:38 +0000120 }
121 }
Chris Lattnerd651f442005-04-27 04:52:23 +0000122
123 // If the SCC doesn't unwind or doesn't throw, note this fact.
124 if (!SCCMightUnwind)
125 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
126 DoesNotUnwind.insert(SCC[i]);
127 if (!SCCMightReturn)
128 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
129 DoesNotReturn.insert(SCC[i]);
Chris Lattner3e64b2e2003-08-31 02:47:32 +0000130
131 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
Chris Lattner3e64b2e2003-08-31 02:47:32 +0000132 // Convert any invoke instructions to non-throwing functions in this node
133 // into call instructions with a branch. This makes the exception blocks
134 // dead.
135 if (Function *F = SCC[i]->getFunction())
Chris Lattnerd651f442005-04-27 04:52:23 +0000136 MadeChange |= SimplifyFunction(F);
Chris Lattner3e64b2e2003-08-31 02:47:32 +0000137 }
138
Misha Brukmanfd939082005-04-21 23:48:37 +0000139 return MadeChange;
Chris Lattner3e64b2e2003-08-31 02:47:32 +0000140}
Brian Gaeked0fde302003-11-11 22:41:34 +0000141
Chris Lattnerd651f442005-04-27 04:52:23 +0000142
143// SimplifyFunction - Given information about callees, simplify the specified
144// function if we have invokes to non-unwinding functions or code after calls to
145// no-return functions.
146bool PruneEH::SimplifyFunction(Function *F) {
147 CallGraph &CG = getAnalysis<CallGraph>();
148 bool MadeChange = false;
149 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
150 if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
151 if (Function *F = II->getCalledFunction())
152 if (DoesNotUnwind.count(CG[F])) {
Chris Lattner93e985f2007-02-13 02:10:56 +0000153 SmallVector<Value*, 8> Args(II->op_begin()+3, II->op_end());
Chris Lattner046800a2007-02-11 01:08:35 +0000154 // Insert a call instruction before the invoke.
Chris Lattnerf201dbc2005-05-09 01:05:50 +0000155 CallInst *Call = new CallInst(II->getCalledValue(),
Chris Lattner93e985f2007-02-13 02:10:56 +0000156 &Args[0], Args.size(), "", II);
Chris Lattner046800a2007-02-11 01:08:35 +0000157 Call->takeName(II);
Chris Lattnerf201dbc2005-05-09 01:05:50 +0000158 Call->setCallingConv(II->getCallingConv());
Jeff Cohen00b168892005-07-27 06:12:32 +0000159
Chris Lattnerd651f442005-04-27 04:52:23 +0000160 // Anything that used the value produced by the invoke instruction
161 // now uses the value produced by the call instruction.
162 II->replaceAllUsesWith(Call);
163 BasicBlock *UnwindBlock = II->getUnwindDest();
164 UnwindBlock->removePredecessor(II->getParent());
Jeff Cohen00b168892005-07-27 06:12:32 +0000165
Chris Lattnerd651f442005-04-27 04:52:23 +0000166 // Insert a branch to the normal destination right before the
167 // invoke.
168 new BranchInst(II->getNormalDest(), II);
Jeff Cohen00b168892005-07-27 06:12:32 +0000169
Chris Lattnerd651f442005-04-27 04:52:23 +0000170 // Finally, delete the invoke instruction!
171 BB->getInstList().pop_back();
172
173 // If the unwind block is now dead, nuke it.
174 if (pred_begin(UnwindBlock) == pred_end(UnwindBlock))
175 DeleteBasicBlock(UnwindBlock); // Delete the new BB.
Jeff Cohen00b168892005-07-27 06:12:32 +0000176
Chris Lattnerd651f442005-04-27 04:52:23 +0000177 ++NumRemoved;
178 MadeChange = true;
179 }
180
Chris Lattner209a0ae2005-04-27 20:12:17 +0000181 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
Chris Lattnerd651f442005-04-27 04:52:23 +0000182 if (CallInst *CI = dyn_cast<CallInst>(I++))
183 if (Function *Callee = CI->getCalledFunction())
184 if (DoesNotReturn.count(CG[Callee]) && !isa<UnreachableInst>(I)) {
185 // This call calls a function that cannot return. Insert an
186 // unreachable instruction after it and simplify the code. Do this
187 // by splitting the BB, adding the unreachable, then deleting the
188 // new BB.
189 BasicBlock *New = BB->splitBasicBlock(I);
190
191 // Remove the uncond branch and add an unreachable.
192 BB->getInstList().pop_back();
193 new UnreachableInst(BB);
194
195 DeleteBasicBlock(New); // Delete the new BB.
196 MadeChange = true;
197 ++NumUnreach;
198 break;
199 }
200
201 }
202 return MadeChange;
203}
204
205/// DeleteBasicBlock - remove the specified basic block from the program,
206/// updating the callgraph to reflect any now-obsolete edges due to calls that
207/// exist in the BB.
208void PruneEH::DeleteBasicBlock(BasicBlock *BB) {
209 assert(pred_begin(BB) == pred_end(BB) && "BB is not dead!");
210 CallGraph &CG = getAnalysis<CallGraph>();
211
212 CallGraphNode *CGN = CG[BB->getParent()];
213 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; ) {
214 --I;
215 if (CallInst *CI = dyn_cast<CallInst>(I)) {
216 if (Function *Callee = CI->getCalledFunction())
217 CGN->removeCallEdgeTo(CG[Callee]);
218 } else if (InvokeInst *II = dyn_cast<InvokeInst>(I)) {
219 if (Function *Callee = II->getCalledFunction())
220 CGN->removeCallEdgeTo(CG[Callee]);
221 }
222 if (!I->use_empty())
223 I->replaceAllUsesWith(UndefValue::get(I->getType()));
224 }
225
226 // Get the list of successors of this block.
227 std::vector<BasicBlock*> Succs(succ_begin(BB), succ_end(BB));
228
229 for (unsigned i = 0, e = Succs.size(); i != e; ++i)
230 Succs[i]->removePredecessor(BB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000231
Chris Lattnerd651f442005-04-27 04:52:23 +0000232 BB->eraseFromParent();
233}