blob: c9c0b197eae666377a3eca5d9745bf9e83ea95b9 [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
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +000011// 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 Lattnerf52e03c2003-11-21 21:54:22 +000017#include "llvm/Transforms/IPO.h"
Duncan Sands57512a12008-09-29 14:59:04 +000018#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnera06a8fd2007-02-13 02:10:56 +000019#include "llvm/ADT/SmallVector.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/ADT/Statistic.h"
Reid Kleckner96d01132015-02-11 01:23:16 +000021#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Analysis/CallGraph.h"
Chandler Carruth839a98e2013-01-07 15:26:48 +000023#include "llvm/Analysis/CallGraphSCCPass.h"
David Majnemer70497c62015-12-02 23:06:39 +000024#include "llvm/Analysis/EHPersonalities.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000025#include "llvm/IR/CFG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Constants.h"
27#include "llvm/IR/Function.h"
David Majnemer5185c3c2015-06-27 07:52:53 +000028#include "llvm/IR/InlineAsm.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/Instructions.h"
30#include "llvm/IR/IntrinsicInst.h"
31#include "llvm/IR/LLVMContext.h"
Chris Lattner88a8a322004-10-18 15:43:46 +000032#include <algorithm>
Chris Lattnerf52e03c2003-11-21 21:54:22 +000033using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000034
Chandler Carruth964daaa2014-04-22 02:55:47 +000035#define DEBUG_TYPE "prune-eh"
36
Chris Lattner1631bcb2006-12-19 22:09:18 +000037STATISTIC(NumRemoved, "Number of invokes removed");
38STATISTIC(NumUnreach, "Number of noreturn calls optimized");
Chris Lattner0a3f8d52003-08-31 02:47:32 +000039
Chris Lattner1631bcb2006-12-19 22:09:18 +000040namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000041 struct PruneEH : public CallGraphSCCPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000042 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000043 PruneEH() : CallGraphSCCPass(ID) {
44 initializePruneEHPass(*PassRegistry::getPassRegistry());
45 }
Devang Patel09f162c2007-05-01 21:15:47 +000046
Chris Lattner0a3f8d52003-08-31 02:47:32 +000047 // runOnSCC - Analyze the SCC, performing the transformation if possible.
Craig Topper3e4c6972014-03-05 09:10:37 +000048 bool runOnSCC(CallGraphSCC &SCC) override;
Chris Lattner93f4e9d2005-04-27 04:52:23 +000049
50 bool SimplifyFunction(Function *F);
51 void DeleteBasicBlock(BasicBlock *BB);
Chris Lattner0a3f8d52003-08-31 02:47:32 +000052 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000053}
Chris Lattner0a3f8d52003-08-31 02:47:32 +000054
Dan Gohmand78c4002008-05-13 00:00:25 +000055char PruneEH::ID = 0;
Owen Anderson071cee02010-10-13 22:00:45 +000056INITIALIZE_PASS_BEGIN(PruneEH, "prune-eh",
57 "Remove unused exception handling info", false, false)
Chandler Carruth6378cf52013-11-26 04:19:30 +000058INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Owen Anderson071cee02010-10-13 22:00:45 +000059INITIALIZE_PASS_END(PruneEH, "prune-eh",
Owen Andersondf7a4f22010-10-07 22:25:06 +000060 "Remove unused exception handling info", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000061
Devang Patel13058a52007-01-26 00:47:38 +000062Pass *llvm::createPruneEHPass() { return new PruneEH(); }
Chris Lattner75444c72003-08-31 16:30:07 +000063
Chris Lattner0a3f8d52003-08-31 02:47:32 +000064
Chris Lattner4422d312010-04-16 22:42:17 +000065bool PruneEH::runOnSCC(CallGraphSCC &SCC) {
Duncan Sands57512a12008-09-29 14:59:04 +000066 SmallPtrSet<CallGraphNode *, 8> SCCNodes;
Chandler Carruth6378cf52013-11-26 04:19:30 +000067 CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
Chris Lattner93f4e9d2005-04-27 04:52:23 +000068 bool MadeChange = false;
Chris Lattner0a3f8d52003-08-31 02:47:32 +000069
Duncan Sands57512a12008-09-29 14:59:04 +000070 // Fill SCCNodes with the elements of the SCC. Used for quickly
71 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner4422d312010-04-16 22:42:17 +000072 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
73 SCCNodes.insert(*I);
Duncan Sands57512a12008-09-29 14:59:04 +000074
Chris Lattner93f4e9d2005-04-27 04:52:23 +000075 // First pass, scan all of the functions in the SCC, simplifying them
76 // according to what we know.
Chris Lattner4422d312010-04-16 22:42:17 +000077 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
78 if (Function *F = (*I)->getFunction())
Chris Lattner93f4e9d2005-04-27 04:52:23 +000079 MadeChange |= SimplifyFunction(F);
80
81 // Next, check to see if any callees might throw or if there are any external
Chris Lattner0a3f8d52003-08-31 02:47:32 +000082 // functions in this SCC: if so, we cannot prune any functions in this SCC.
Dale Johannesene7f5bc22008-05-16 21:31:48 +000083 // Definitions that are weak and not declared non-throwing might be
84 // overridden at linktime with something that throws, so assume that.
Chris Lattner04ecefe2003-09-08 19:44:26 +000085 // If this SCC includes the unwind instruction, we KNOW it throws, so
Chris Lattner0a3f8d52003-08-31 02:47:32 +000086 // obviously the SCC might throw.
87 //
Chris Lattner93f4e9d2005-04-27 04:52:23 +000088 bool SCCMightUnwind = false, SCCMightReturn = false;
Chris Lattner4422d312010-04-16 22:42:17 +000089 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end();
90 (!SCCMightUnwind || !SCCMightReturn) && I != E; ++I) {
91 Function *F = (*I)->getFunction();
Craig Topperf40110f2014-04-25 05:29:35 +000092 if (!F) {
Chris Lattner7f4f7732005-04-26 23:53:25 +000093 SCCMightUnwind = true;
Chris Lattner93f4e9d2005-04-27 04:52:23 +000094 SCCMightReturn = true;
Duncan Sands08d91172008-09-29 11:25:42 +000095 } else if (F->isDeclaration() || F->mayBeOverridden()) {
Duncan Sands3353ed02007-12-18 09:59:50 +000096 SCCMightUnwind |= !F->doesNotThrow();
97 SCCMightReturn |= !F->doesNotReturn();
Chris Lattnerd041dcd2004-04-12 04:06:38 +000098 } else {
Duncan Sands3353ed02007-12-18 09:59:50 +000099 bool CheckUnwind = !SCCMightUnwind && !F->doesNotThrow();
100 bool CheckReturn = !SCCMightReturn && !F->doesNotReturn();
David Majnemer5185c3c2015-06-27 07:52:53 +0000101 // Determine if we should scan for InlineAsm in a naked function as it
102 // is the only way to return without a ReturnInst. Only do this for
103 // no-inline functions as functions which may be inlined cannot
104 // meaningfully return via assembly.
105 bool CheckReturnViaAsm = CheckReturn &&
106 F->hasFnAttribute(Attribute::Naked) &&
107 F->hasFnAttribute(Attribute::NoInline);
Duncan Sands9f76be62007-12-10 19:09:40 +0000108
109 if (!CheckUnwind && !CheckReturn)
110 continue;
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000111
David Majnemer5185c3c2015-06-27 07:52:53 +0000112 for (const BasicBlock &BB : *F) {
113 const TerminatorInst *TI = BB.getTerminator();
114 if (CheckUnwind && TI->mayThrow()) {
Chris Lattner7f4f7732005-04-26 23:53:25 +0000115 SCCMightUnwind = true;
David Majnemer5185c3c2015-06-27 07:52:53 +0000116 } else if (CheckReturn && isa<ReturnInst>(TI)) {
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000117 SCCMightReturn = true;
Chris Lattnerd041dcd2004-04-12 04:06:38 +0000118 }
Chris Lattner56997dd2004-02-08 21:15:59 +0000119
David Majnemer5185c3c2015-06-27 07:52:53 +0000120 for (const Instruction &I : BB) {
121 if ((!CheckUnwind || SCCMightUnwind) &&
122 (!CheckReturnViaAsm || SCCMightReturn))
123 break;
124
125 // Check to see if this function performs an unwind or calls an
126 // unwinding function.
127 if (CheckUnwind && !SCCMightUnwind && I.mayThrow()) {
128 bool InstMightUnwind = true;
129 if (const auto *CI = dyn_cast<CallInst>(&I)) {
130 if (Function *Callee = CI->getCalledFunction()) {
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000131 CallGraphNode *CalleeNode = CG[Callee];
David Majnemer5185c3c2015-06-27 07:52:53 +0000132 // If the callee is outside our current SCC then we may throw
133 // because it might. If it is inside, do nothing.
134 if (SCCNodes.count(CalleeNode) > 0)
135 InstMightUnwind = false;
Chris Lattner56997dd2004-02-08 21:15:59 +0000136 }
137 }
David Majnemer5185c3c2015-06-27 07:52:53 +0000138 SCCMightUnwind |= InstMightUnwind;
139 }
140 if (CheckReturnViaAsm && !SCCMightReturn)
141 if (auto ICS = ImmutableCallSite(&I))
142 if (const auto *IA = dyn_cast<InlineAsm>(ICS.getCalledValue()))
143 if (IA->hasSideEffects())
144 SCCMightReturn = true;
145 }
146
147 if (SCCMightUnwind && SCCMightReturn)
148 break;
Chris Lattner5a6fa292003-09-15 02:22:50 +0000149 }
Chris Lattnerd041dcd2004-04-12 04:06:38 +0000150 }
151 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000152
153 // If the SCC doesn't unwind or doesn't throw, note this fact.
Duncan Sands9f76be62007-12-10 19:09:40 +0000154 if (!SCCMightUnwind || !SCCMightReturn)
Chris Lattner4422d312010-04-16 22:42:17 +0000155 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
Bill Wendling50d27842012-10-15 20:35:56 +0000156 AttrBuilder NewAttributes;
Duncan Sands9f76be62007-12-10 19:09:40 +0000157
158 if (!SCCMightUnwind)
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000159 NewAttributes.addAttribute(Attribute::NoUnwind);
Duncan Sands9f76be62007-12-10 19:09:40 +0000160 if (!SCCMightReturn)
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000161 NewAttributes.addAttribute(Attribute::NoReturn);
Duncan Sands9f76be62007-12-10 19:09:40 +0000162
Chris Lattner4422d312010-04-16 22:42:17 +0000163 Function *F = (*I)->getFunction();
Benjamin Kramer9ddfaf22013-06-15 10:55:39 +0000164 const AttributeSet &PAL = F->getAttributes().getFnAttributes();
165 const AttributeSet &NPAL = AttributeSet::get(
166 F->getContext(), AttributeSet::FunctionIndex, NewAttributes);
167
Duncan Sands6dd02b52008-09-05 09:08:37 +0000168 if (PAL != NPAL) {
169 MadeChange = true;
Benjamin Kramer9ddfaf22013-06-15 10:55:39 +0000170 F->addAttributes(AttributeSet::FunctionIndex, NPAL);
Duncan Sands6dd02b52008-09-05 09:08:37 +0000171 }
Duncan Sands9f76be62007-12-10 19:09:40 +0000172 }
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000173
Chris Lattner4422d312010-04-16 22:42:17 +0000174 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000175 // Convert any invoke instructions to non-throwing functions in this node
176 // into call instructions with a branch. This makes the exception blocks
177 // dead.
Chris Lattner4422d312010-04-16 22:42:17 +0000178 if (Function *F = (*I)->getFunction())
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000179 MadeChange |= SimplifyFunction(F);
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000180 }
181
Misha Brukmanb1c93172005-04-21 23:48:37 +0000182 return MadeChange;
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000183}
Brian Gaeke960707c2003-11-11 22:41:34 +0000184
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000185
186// SimplifyFunction - Given information about callees, simplify the specified
187// function if we have invokes to non-unwinding functions or code after calls to
188// no-return functions.
189bool PruneEH::SimplifyFunction(Function *F) {
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000190 bool MadeChange = false;
191 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
192 if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
David Majnemer7fddecc2015-06-17 20:52:32 +0000193 if (II->doesNotThrow() && canSimplifyInvokeNoUnwind(F)) {
Sanjoy Das48945cd2015-12-08 23:16:52 +0000194 CallSite CS(II);
195 SmallVector<Value*, 8> Args(CS.arg_begin(), CS.arg_end());
196 SmallVector<OperandBundleDef, 1> OpBundles;
197 II->getOperandBundlesAsDefs(OpBundles);
198
Duncan Sands9f76be62007-12-10 19:09:40 +0000199 // Insert a call instruction before the invoke.
Sanjoy Das48945cd2015-12-08 23:16:52 +0000200 CallInst *Call = CallInst::Create(II->getCalledValue(), Args, OpBundles,
201 "", II);
Duncan Sands9f76be62007-12-10 19:09:40 +0000202 Call->takeName(II);
203 Call->setCallingConv(II->getCallingConv());
Devang Patel4c758ea2008-09-25 21:00:45 +0000204 Call->setAttributes(II->getAttributes());
Devang Patel3fd06f72011-05-10 00:03:11 +0000205 Call->setDebugLoc(II->getDebugLoc());
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000206
Duncan Sands9f76be62007-12-10 19:09:40 +0000207 // Anything that used the value produced by the invoke instruction
Chris Lattner063d0652009-09-01 06:31:31 +0000208 // now uses the value produced by the call instruction. Note that we
209 // do this even for void functions and calls with no uses so that the
210 // callgraph edge is updated.
Duncan Sands9f76be62007-12-10 19:09:40 +0000211 II->replaceAllUsesWith(Call);
212 BasicBlock *UnwindBlock = II->getUnwindDest();
213 UnwindBlock->removePredecessor(II->getParent());
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000214
Duncan Sands9f76be62007-12-10 19:09:40 +0000215 // Insert a branch to the normal destination right before the
216 // invoke.
Gabor Greife9ecc682008-04-06 20:25:17 +0000217 BranchInst::Create(II->getNormalDest(), II);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000218
Duncan Sands9f76be62007-12-10 19:09:40 +0000219 // Finally, delete the invoke instruction!
220 BB->getInstList().pop_back();
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000221
Duncan Sands9f76be62007-12-10 19:09:40 +0000222 // If the unwind block is now dead, nuke it.
Ramkumar Ramachandra40c3e032015-01-13 03:46:47 +0000223 if (pred_empty(UnwindBlock))
Duncan Sands9f76be62007-12-10 19:09:40 +0000224 DeleteBasicBlock(UnwindBlock); // Delete the new BB.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000225
Duncan Sands9f76be62007-12-10 19:09:40 +0000226 ++NumRemoved;
227 MadeChange = true;
Nick Lewycky929703b2008-03-09 17:11:18 +0000228 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000229
Chris Lattner36ffb1f2005-04-27 20:12:17 +0000230 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
Nick Lewycky4d43d3c2008-04-25 16:53:59 +0000231 if (CallInst *CI = dyn_cast<CallInst>(I++))
Duncan Sands3353ed02007-12-18 09:59:50 +0000232 if (CI->doesNotReturn() && !isa<UnreachableInst>(I)) {
Duncan Sands9f76be62007-12-10 19:09:40 +0000233 // This call calls a function that cannot return. Insert an
234 // unreachable instruction after it and simplify the code. Do this
235 // by splitting the BB, adding the unreachable, then deleting the
236 // new BB.
237 BasicBlock *New = BB->splitBasicBlock(I);
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000238
Duncan Sands9f76be62007-12-10 19:09:40 +0000239 // Remove the uncond branch and add an unreachable.
240 BB->getInstList().pop_back();
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000241 new UnreachableInst(BB->getContext(), &*BB);
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000242
Duncan Sands9f76be62007-12-10 19:09:40 +0000243 DeleteBasicBlock(New); // Delete the new BB.
244 MadeChange = true;
245 ++NumUnreach;
246 break;
Nick Lewycky0ac65c32008-03-09 17:13:05 +0000247 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000248 }
Nick Lewycky4d43d3c2008-04-25 16:53:59 +0000249
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000250 return MadeChange;
251}
252
253/// DeleteBasicBlock - remove the specified basic block from the program,
254/// updating the callgraph to reflect any now-obsolete edges due to calls that
255/// exist in the BB.
256void PruneEH::DeleteBasicBlock(BasicBlock *BB) {
Ramkumar Ramachandra40c3e032015-01-13 03:46:47 +0000257 assert(pred_empty(BB) && "BB is not dead!");
Chandler Carruth6378cf52013-11-26 04:19:30 +0000258 CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000259
260 CallGraphNode *CGN = CG[BB->getParent()];
261 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; ) {
262 --I;
Dale Johannesen20509682009-03-19 18:03:56 +0000263 if (CallInst *CI = dyn_cast<CallInst>(I)) {
John McCall58fb52c2011-06-09 20:31:09 +0000264 if (!isa<IntrinsicInst>(I))
Dale Johannesen20509682009-03-19 18:03:56 +0000265 CGN->removeCallEdgeFor(CI);
266 } else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Duncan Sands46911f12008-09-08 11:05:51 +0000267 CGN->removeCallEdgeFor(II);
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000268 if (!I->use_empty())
Owen Andersonb292b8c2009-07-30 23:03:37 +0000269 I->replaceAllUsesWith(UndefValue::get(I->getType()));
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000270 }
271
272 // Get the list of successors of this block.
273 std::vector<BasicBlock*> Succs(succ_begin(BB), succ_end(BB));
274
275 for (unsigned i = 0, e = Succs.size(); i != e; ++i)
276 Succs[i]->removePredecessor(BB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000277
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000278 BB->eraseFromParent();
279}