blob: d9acb9b1a743b11c0165f0fe3f05bfe1d52f54f6 [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"
Reid Spencer7c16caa2004-09-01 22:55:40 +000019#include "llvm/ADT/Statistic.h"
Reid Kleckner96d01132015-02-11 01:23:16 +000020#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Analysis/CallGraph.h"
Chandler Carruth839a98e2013-01-07 15:26:48 +000022#include "llvm/Analysis/CallGraphSCCPass.h"
David Majnemer70497c62015-12-02 23:06:39 +000023#include "llvm/Analysis/EHPersonalities.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000024#include "llvm/IR/CFG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Constants.h"
26#include "llvm/IR/Function.h"
David Majnemer5185c3c2015-06-27 07:52:53 +000027#include "llvm/IR/InlineAsm.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Instructions.h"
29#include "llvm/IR/IntrinsicInst.h"
30#include "llvm/IR/LLVMContext.h"
David Majnemer146d7812016-01-23 05:41:22 +000031#include "llvm/Transforms/Utils/Local.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
Chris Lattner0a3f8d52003-08-31 02:47:32 +000050 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000051}
Sean Silva0fb77742016-07-02 19:12:56 +000052static bool SimplifyFunction(Function *F, CallGraph &CG);
53static void DeleteBasicBlock(BasicBlock *BB, CallGraph &CG);
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
Sean Silva0fb77742016-07-02 19:12:56 +000064static bool runImpl(CallGraphSCC &SCC, CallGraph &CG) {
Duncan Sands57512a12008-09-29 14:59:04 +000065 SmallPtrSet<CallGraphNode *, 8> SCCNodes;
Chris Lattner93f4e9d2005-04-27 04:52:23 +000066 bool MadeChange = false;
Chris Lattner0a3f8d52003-08-31 02:47:32 +000067
Duncan Sands57512a12008-09-29 14:59:04 +000068 // Fill SCCNodes with the elements of the SCC. Used for quickly
69 // looking up whether a given CallGraphNode is in this SCC.
Benjamin Kramer135f7352016-06-26 12:28:59 +000070 for (CallGraphNode *I : SCC)
71 SCCNodes.insert(I);
Duncan Sands57512a12008-09-29 14:59:04 +000072
Chris Lattner93f4e9d2005-04-27 04:52:23 +000073 // First pass, scan all of the functions in the SCC, simplifying them
74 // according to what we know.
Benjamin Kramer135f7352016-06-26 12:28:59 +000075 for (CallGraphNode *I : SCC)
76 if (Function *F = I->getFunction())
Sean Silva0fb77742016-07-02 19:12:56 +000077 MadeChange |= SimplifyFunction(F, CG);
Chris Lattner93f4e9d2005-04-27 04:52:23 +000078
79 // Next, check to see if any callees might throw or if there are any external
Chris Lattner0a3f8d52003-08-31 02:47:32 +000080 // functions in this SCC: if so, we cannot prune any functions in this SCC.
Dale Johannesene7f5bc22008-05-16 21:31:48 +000081 // Definitions that are weak and not declared non-throwing might be
82 // overridden at linktime with something that throws, so assume that.
Chris Lattner04ecefe2003-09-08 19:44:26 +000083 // If this SCC includes the unwind instruction, we KNOW it throws, so
Chris Lattner0a3f8d52003-08-31 02:47:32 +000084 // obviously the SCC might throw.
85 //
Chris Lattner93f4e9d2005-04-27 04:52:23 +000086 bool SCCMightUnwind = false, SCCMightReturn = false;
Chris Lattner4422d312010-04-16 22:42:17 +000087 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end();
88 (!SCCMightUnwind || !SCCMightReturn) && I != E; ++I) {
89 Function *F = (*I)->getFunction();
Craig Topperf40110f2014-04-25 05:29:35 +000090 if (!F) {
Chris Lattner7f4f7732005-04-26 23:53:25 +000091 SCCMightUnwind = true;
Chris Lattner93f4e9d2005-04-27 04:52:23 +000092 SCCMightReturn = true;
Sanjoy Das0359a192016-10-03 19:35:30 +000093 } else if (!F->hasExactDefinition()) {
Duncan Sands3353ed02007-12-18 09:59:50 +000094 SCCMightUnwind |= !F->doesNotThrow();
95 SCCMightReturn |= !F->doesNotReturn();
Chris Lattnerd041dcd2004-04-12 04:06:38 +000096 } else {
Duncan Sands3353ed02007-12-18 09:59:50 +000097 bool CheckUnwind = !SCCMightUnwind && !F->doesNotThrow();
98 bool CheckReturn = !SCCMightReturn && !F->doesNotReturn();
David Majnemer5185c3c2015-06-27 07:52:53 +000099 // Determine if we should scan for InlineAsm in a naked function as it
100 // is the only way to return without a ReturnInst. Only do this for
101 // no-inline functions as functions which may be inlined cannot
102 // meaningfully return via assembly.
103 bool CheckReturnViaAsm = CheckReturn &&
104 F->hasFnAttribute(Attribute::Naked) &&
105 F->hasFnAttribute(Attribute::NoInline);
Duncan Sands9f76be62007-12-10 19:09:40 +0000106
107 if (!CheckUnwind && !CheckReturn)
108 continue;
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000109
David Majnemer5185c3c2015-06-27 07:52:53 +0000110 for (const BasicBlock &BB : *F) {
111 const TerminatorInst *TI = BB.getTerminator();
112 if (CheckUnwind && TI->mayThrow()) {
Chris Lattner7f4f7732005-04-26 23:53:25 +0000113 SCCMightUnwind = true;
David Majnemer5185c3c2015-06-27 07:52:53 +0000114 } else if (CheckReturn && isa<ReturnInst>(TI)) {
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000115 SCCMightReturn = true;
Chris Lattnerd041dcd2004-04-12 04:06:38 +0000116 }
Chris Lattner56997dd2004-02-08 21:15:59 +0000117
David Majnemer5185c3c2015-06-27 07:52:53 +0000118 for (const Instruction &I : BB) {
119 if ((!CheckUnwind || SCCMightUnwind) &&
120 (!CheckReturnViaAsm || SCCMightReturn))
121 break;
122
123 // Check to see if this function performs an unwind or calls an
124 // unwinding function.
125 if (CheckUnwind && !SCCMightUnwind && I.mayThrow()) {
126 bool InstMightUnwind = true;
127 if (const auto *CI = dyn_cast<CallInst>(&I)) {
128 if (Function *Callee = CI->getCalledFunction()) {
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000129 CallGraphNode *CalleeNode = CG[Callee];
David Majnemer5185c3c2015-06-27 07:52:53 +0000130 // If the callee is outside our current SCC then we may throw
131 // because it might. If it is inside, do nothing.
132 if (SCCNodes.count(CalleeNode) > 0)
133 InstMightUnwind = false;
Chris Lattner56997dd2004-02-08 21:15:59 +0000134 }
135 }
David Majnemer5185c3c2015-06-27 07:52:53 +0000136 SCCMightUnwind |= InstMightUnwind;
137 }
138 if (CheckReturnViaAsm && !SCCMightReturn)
139 if (auto ICS = ImmutableCallSite(&I))
140 if (const auto *IA = dyn_cast<InlineAsm>(ICS.getCalledValue()))
141 if (IA->hasSideEffects())
142 SCCMightReturn = true;
143 }
144
145 if (SCCMightUnwind && SCCMightReturn)
146 break;
Chris Lattner5a6fa292003-09-15 02:22:50 +0000147 }
Chris Lattnerd041dcd2004-04-12 04:06:38 +0000148 }
149 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000150
151 // If the SCC doesn't unwind or doesn't throw, note this fact.
Duncan Sands9f76be62007-12-10 19:09:40 +0000152 if (!SCCMightUnwind || !SCCMightReturn)
Benjamin Kramer135f7352016-06-26 12:28:59 +0000153 for (CallGraphNode *I : SCC) {
154 Function *F = I->getFunction();
Benjamin Kramer9ddfaf22013-06-15 10:55:39 +0000155
Artur Pilipenko7ae49ac2015-12-11 16:30:26 +0000156 if (!SCCMightUnwind && !F->hasFnAttribute(Attribute::NoUnwind)) {
157 F->addFnAttr(Attribute::NoUnwind);
Duncan Sands6dd02b52008-09-05 09:08:37 +0000158 MadeChange = true;
Artur Pilipenko7ae49ac2015-12-11 16:30:26 +0000159 }
160
161 if (!SCCMightReturn && !F->hasFnAttribute(Attribute::NoReturn)) {
162 F->addFnAttr(Attribute::NoReturn);
163 MadeChange = true;
Duncan Sands6dd02b52008-09-05 09:08:37 +0000164 }
Duncan Sands9f76be62007-12-10 19:09:40 +0000165 }
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000166
Benjamin Kramer135f7352016-06-26 12:28:59 +0000167 for (CallGraphNode *I : SCC) {
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000168 // Convert any invoke instructions to non-throwing functions in this node
169 // into call instructions with a branch. This makes the exception blocks
170 // dead.
Benjamin Kramer135f7352016-06-26 12:28:59 +0000171 if (Function *F = I->getFunction())
Sean Silva0fb77742016-07-02 19:12:56 +0000172 MadeChange |= SimplifyFunction(F, CG);
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000173 }
174
Misha Brukmanb1c93172005-04-21 23:48:37 +0000175 return MadeChange;
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000176}
Brian Gaeke960707c2003-11-11 22:41:34 +0000177
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000178
Sean Silva0fb77742016-07-02 19:12:56 +0000179bool PruneEH::runOnSCC(CallGraphSCC &SCC) {
180 if (skipSCC(SCC))
181 return false;
182 CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
183 return runImpl(SCC, CG);
184}
185
186
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000187// SimplifyFunction - Given information about callees, simplify the specified
188// function if we have invokes to non-unwinding functions or code after calls to
189// no-return functions.
Sean Silva0fb77742016-07-02 19:12:56 +0000190static bool SimplifyFunction(Function *F, CallGraph &CG) {
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000191 bool MadeChange = false;
192 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
193 if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
David Majnemer7fddecc2015-06-17 20:52:32 +0000194 if (II->doesNotThrow() && canSimplifyInvokeNoUnwind(F)) {
Duncan Sands9f76be62007-12-10 19:09:40 +0000195 BasicBlock *UnwindBlock = II->getUnwindDest();
David Majnemer146d7812016-01-23 05:41:22 +0000196 removeUnwindEdge(&*BB);
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000197
Duncan Sands9f76be62007-12-10 19:09:40 +0000198 // If the unwind block is now dead, nuke it.
Ramkumar Ramachandra40c3e032015-01-13 03:46:47 +0000199 if (pred_empty(UnwindBlock))
Sean Silva0fb77742016-07-02 19:12:56 +0000200 DeleteBasicBlock(UnwindBlock, CG); // Delete the new BB.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000201
Duncan Sands9f76be62007-12-10 19:09:40 +0000202 ++NumRemoved;
203 MadeChange = true;
Nick Lewycky929703b2008-03-09 17:11:18 +0000204 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000205
Chris Lattner36ffb1f2005-04-27 20:12:17 +0000206 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
Nick Lewycky4d43d3c2008-04-25 16:53:59 +0000207 if (CallInst *CI = dyn_cast<CallInst>(I++))
Duncan Sands3353ed02007-12-18 09:59:50 +0000208 if (CI->doesNotReturn() && !isa<UnreachableInst>(I)) {
Duncan Sands9f76be62007-12-10 19:09:40 +0000209 // This call calls a function that cannot return. Insert an
210 // unreachable instruction after it and simplify the code. Do this
211 // by splitting the BB, adding the unreachable, then deleting the
212 // new BB.
213 BasicBlock *New = BB->splitBasicBlock(I);
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000214
Duncan Sands9f76be62007-12-10 19:09:40 +0000215 // Remove the uncond branch and add an unreachable.
216 BB->getInstList().pop_back();
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000217 new UnreachableInst(BB->getContext(), &*BB);
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000218
Sean Silva0fb77742016-07-02 19:12:56 +0000219 DeleteBasicBlock(New, CG); // Delete the new BB.
Duncan Sands9f76be62007-12-10 19:09:40 +0000220 MadeChange = true;
221 ++NumUnreach;
222 break;
Nick Lewycky0ac65c32008-03-09 17:13:05 +0000223 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000224 }
Nick Lewycky4d43d3c2008-04-25 16:53:59 +0000225
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000226 return MadeChange;
227}
228
229/// DeleteBasicBlock - remove the specified basic block from the program,
230/// updating the callgraph to reflect any now-obsolete edges due to calls that
231/// exist in the BB.
Sean Silva0fb77742016-07-02 19:12:56 +0000232static void DeleteBasicBlock(BasicBlock *BB, CallGraph &CG) {
Ramkumar Ramachandra40c3e032015-01-13 03:46:47 +0000233 assert(pred_empty(BB) && "BB is not dead!");
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000234
David Majnemer4bf0b6b2016-01-23 05:41:29 +0000235 Instruction *TokenInst = nullptr;
236
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000237 CallGraphNode *CGN = CG[BB->getParent()];
238 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; ) {
239 --I;
David Majnemer2d728ec2016-01-23 05:41:27 +0000240
David Majnemer4bf0b6b2016-01-23 05:41:29 +0000241 if (I->getType()->isTokenTy()) {
242 TokenInst = &*I;
243 break;
244 }
245
David Majnemer2d728ec2016-01-23 05:41:27 +0000246 if (auto CS = CallSite (&*I)) {
247 const Function *Callee = CS.getCalledFunction();
248 if (!Callee || !Intrinsic::isLeaf(Callee->getIntrinsicID()))
249 CGN->removeCallEdgeFor(CS);
250 else if (!Callee->isIntrinsic())
251 CGN->removeCallEdgeFor(CS);
252 }
253
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000254 if (!I->use_empty())
Owen Andersonb292b8c2009-07-30 23:03:37 +0000255 I->replaceAllUsesWith(UndefValue::get(I->getType()));
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000256 }
257
David Majnemer4bf0b6b2016-01-23 05:41:29 +0000258 if (TokenInst) {
David Majnemer6e510702016-01-23 06:00:44 +0000259 if (!isa<TerminatorInst>(TokenInst))
David Majnemere14e7bc2016-06-25 08:19:55 +0000260 changeToUnreachable(TokenInst->getNextNode(), /*UseLLVMTrap=*/false);
David Majnemer4bf0b6b2016-01-23 05:41:29 +0000261 } else {
262 // Get the list of successors of this block.
263 std::vector<BasicBlock *> Succs(succ_begin(BB), succ_end(BB));
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000264
David Majnemer4bf0b6b2016-01-23 05:41:29 +0000265 for (unsigned i = 0, e = Succs.size(); i != e; ++i)
266 Succs[i]->removePredecessor(BB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000267
David Majnemer4bf0b6b2016-01-23 05:41:29 +0000268 BB->eraseFromParent();
269 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000270}