blob: 45a0ce20eb175c8097fff0b8c7f92c83ba53e96f [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukmanb1c93172005-04-21 23:48:37 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner0a3f8d52003-08-31 02:47:32 +00008//
9// This file implements a simple interprocedural pass which walks the
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +000010// call-graph, turning invoke instructions into calls, iff the callee cannot
Duncan Sands9f76be62007-12-10 19:09:40 +000011// throw an exception, and marking functions 'nounwind' if they cannot throw.
12// It implements this as a bottom-up traversal of the call-graph.
Chris Lattner0a3f8d52003-08-31 02:47:32 +000013//
14//===----------------------------------------------------------------------===//
15
Duncan Sands57512a12008-09-29 14:59:04 +000016#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000017#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/Analysis/CallGraph.h"
Chandler Carruth839a98e2013-01-07 15:26:48 +000019#include "llvm/Analysis/CallGraphSCCPass.h"
David Majnemer70497c62015-12-02 23:06:39 +000020#include "llvm/Analysis/EHPersonalities.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000021#include "llvm/IR/CFG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Constants.h"
23#include "llvm/IR/Function.h"
David Majnemer5185c3c2015-06-27 07:52:53 +000024#include "llvm/IR/InlineAsm.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/LLVMContext.h"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080027#include "llvm/InitializePasses.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000028#include "llvm/Support/raw_ostream.h"
29#include "llvm/Transforms/IPO.h"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080030#include "llvm/Transforms/Utils/Local.h"
Chris Lattner88a8a322004-10-18 15:43:46 +000031#include <algorithm>
Chris Lattnerf52e03c2003-11-21 21:54:22 +000032using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000033
Chandler Carruth964daaa2014-04-22 02:55:47 +000034#define DEBUG_TYPE "prune-eh"
35
Chris Lattner1631bcb2006-12-19 22:09:18 +000036STATISTIC(NumRemoved, "Number of invokes removed");
37STATISTIC(NumUnreach, "Number of noreturn calls optimized");
Chris Lattner0a3f8d52003-08-31 02:47:32 +000038
Chris Lattner1631bcb2006-12-19 22:09:18 +000039namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000040 struct PruneEH : public CallGraphSCCPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000041 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000042 PruneEH() : CallGraphSCCPass(ID) {
43 initializePruneEHPass(*PassRegistry::getPassRegistry());
44 }
Devang Patel09f162c2007-05-01 21:15:47 +000045
Chris Lattner0a3f8d52003-08-31 02:47:32 +000046 // runOnSCC - Analyze the SCC, performing the transformation if possible.
Craig Topper3e4c6972014-03-05 09:10:37 +000047 bool runOnSCC(CallGraphSCC &SCC) override;
Chris Lattner93f4e9d2005-04-27 04:52:23 +000048
Chris Lattner0a3f8d52003-08-31 02:47:32 +000049 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000050}
Sean Silva0fb77742016-07-02 19:12:56 +000051static bool SimplifyFunction(Function *F, CallGraph &CG);
52static void DeleteBasicBlock(BasicBlock *BB, CallGraph &CG);
Chris Lattner0a3f8d52003-08-31 02:47:32 +000053
Dan Gohmand78c4002008-05-13 00:00:25 +000054char PruneEH::ID = 0;
Owen Anderson071cee02010-10-13 22:00:45 +000055INITIALIZE_PASS_BEGIN(PruneEH, "prune-eh",
56 "Remove unused exception handling info", false, false)
Chandler Carruth6378cf52013-11-26 04:19:30 +000057INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Owen Anderson071cee02010-10-13 22:00:45 +000058INITIALIZE_PASS_END(PruneEH, "prune-eh",
Owen Andersondf7a4f22010-10-07 22:25:06 +000059 "Remove unused exception handling info", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000060
Devang Patel13058a52007-01-26 00:47:38 +000061Pass *llvm::createPruneEHPass() { return new PruneEH(); }
Chris Lattner75444c72003-08-31 16:30:07 +000062
Sean Silva0fb77742016-07-02 19:12:56 +000063static bool runImpl(CallGraphSCC &SCC, CallGraph &CG) {
Duncan Sands57512a12008-09-29 14:59:04 +000064 SmallPtrSet<CallGraphNode *, 8> SCCNodes;
Chris Lattner93f4e9d2005-04-27 04:52:23 +000065 bool MadeChange = false;
Chris Lattner0a3f8d52003-08-31 02:47:32 +000066
Duncan Sands57512a12008-09-29 14:59:04 +000067 // Fill SCCNodes with the elements of the SCC. Used for quickly
68 // looking up whether a given CallGraphNode is in this SCC.
Benjamin Kramer135f7352016-06-26 12:28:59 +000069 for (CallGraphNode *I : SCC)
70 SCCNodes.insert(I);
Duncan Sands57512a12008-09-29 14:59:04 +000071
Chris Lattner93f4e9d2005-04-27 04:52:23 +000072 // First pass, scan all of the functions in the SCC, simplifying them
73 // according to what we know.
Benjamin Kramer135f7352016-06-26 12:28:59 +000074 for (CallGraphNode *I : SCC)
75 if (Function *F = I->getFunction())
Sean Silva0fb77742016-07-02 19:12:56 +000076 MadeChange |= SimplifyFunction(F, CG);
Chris Lattner93f4e9d2005-04-27 04:52:23 +000077
78 // Next, check to see if any callees might throw or if there are any external
Chris Lattner0a3f8d52003-08-31 02:47:32 +000079 // functions in this SCC: if so, we cannot prune any functions in this SCC.
Fangrui Songf78650a2018-07-30 19:41:25 +000080 // Definitions that are weak and not declared non-throwing might be
Dale Johannesene7f5bc22008-05-16 21:31:48 +000081 // overridden at linktime with something that throws, so assume that.
Chris Lattner04ecefe2003-09-08 19:44:26 +000082 // If this SCC includes the unwind instruction, we KNOW it throws, so
Chris Lattner0a3f8d52003-08-31 02:47:32 +000083 // obviously the SCC might throw.
84 //
Chris Lattner93f4e9d2005-04-27 04:52:23 +000085 bool SCCMightUnwind = false, SCCMightReturn = false;
Fangrui Songf78650a2018-07-30 19:41:25 +000086 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end();
Chris Lattner4422d312010-04-16 22:42:17 +000087 (!SCCMightUnwind || !SCCMightReturn) && I != E; ++I) {
88 Function *F = (*I)->getFunction();
Craig Topperf40110f2014-04-25 05:29:35 +000089 if (!F) {
Chris Lattner7f4f7732005-04-26 23:53:25 +000090 SCCMightUnwind = true;
Chris Lattner93f4e9d2005-04-27 04:52:23 +000091 SCCMightReturn = true;
Sanjoy Das0359a192016-10-03 19:35:30 +000092 } else if (!F->hasExactDefinition()) {
Duncan Sands3353ed02007-12-18 09:59:50 +000093 SCCMightUnwind |= !F->doesNotThrow();
94 SCCMightReturn |= !F->doesNotReturn();
Chris Lattnerd041dcd2004-04-12 04:06:38 +000095 } else {
Duncan Sands3353ed02007-12-18 09:59:50 +000096 bool CheckUnwind = !SCCMightUnwind && !F->doesNotThrow();
97 bool CheckReturn = !SCCMightReturn && !F->doesNotReturn();
David Majnemer5185c3c2015-06-27 07:52:53 +000098 // Determine if we should scan for InlineAsm in a naked function as it
99 // is the only way to return without a ReturnInst. Only do this for
100 // no-inline functions as functions which may be inlined cannot
101 // meaningfully return via assembly.
102 bool CheckReturnViaAsm = CheckReturn &&
103 F->hasFnAttribute(Attribute::Naked) &&
104 F->hasFnAttribute(Attribute::NoInline);
Duncan Sands9f76be62007-12-10 19:09:40 +0000105
106 if (!CheckUnwind && !CheckReturn)
107 continue;
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000108
David Majnemer5185c3c2015-06-27 07:52:53 +0000109 for (const BasicBlock &BB : *F) {
Chandler Carruthedb12a82018-10-15 10:04:59 +0000110 const Instruction *TI = BB.getTerminator();
David Majnemer5185c3c2015-06-27 07:52:53 +0000111 if (CheckUnwind && TI->mayThrow()) {
Chris Lattner7f4f7732005-04-26 23:53:25 +0000112 SCCMightUnwind = true;
David Majnemer5185c3c2015-06-27 07:52:53 +0000113 } else if (CheckReturn && isa<ReturnInst>(TI)) {
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000114 SCCMightReturn = true;
Chris Lattnerd041dcd2004-04-12 04:06:38 +0000115 }
Chris Lattner56997dd2004-02-08 21:15:59 +0000116
David Majnemer5185c3c2015-06-27 07:52:53 +0000117 for (const Instruction &I : BB) {
118 if ((!CheckUnwind || SCCMightUnwind) &&
119 (!CheckReturnViaAsm || SCCMightReturn))
120 break;
121
122 // Check to see if this function performs an unwind or calls an
123 // unwinding function.
124 if (CheckUnwind && !SCCMightUnwind && I.mayThrow()) {
125 bool InstMightUnwind = true;
126 if (const auto *CI = dyn_cast<CallInst>(&I)) {
127 if (Function *Callee = CI->getCalledFunction()) {
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000128 CallGraphNode *CalleeNode = CG[Callee];
David Majnemer5185c3c2015-06-27 07:52:53 +0000129 // If the callee is outside our current SCC then we may throw
130 // because it might. If it is inside, do nothing.
131 if (SCCNodes.count(CalleeNode) > 0)
132 InstMightUnwind = false;
Chris Lattner56997dd2004-02-08 21:15:59 +0000133 }
134 }
David Majnemer5185c3c2015-06-27 07:52:53 +0000135 SCCMightUnwind |= InstMightUnwind;
136 }
137 if (CheckReturnViaAsm && !SCCMightReturn)
138 if (auto ICS = ImmutableCallSite(&I))
139 if (const auto *IA = dyn_cast<InlineAsm>(ICS.getCalledValue()))
140 if (IA->hasSideEffects())
141 SCCMightReturn = true;
142 }
143
144 if (SCCMightUnwind && SCCMightReturn)
145 break;
Chris Lattner5a6fa292003-09-15 02:22:50 +0000146 }
Chris Lattnerd041dcd2004-04-12 04:06:38 +0000147 }
148 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000149
150 // If the SCC doesn't unwind or doesn't throw, note this fact.
Duncan Sands9f76be62007-12-10 19:09:40 +0000151 if (!SCCMightUnwind || !SCCMightReturn)
Benjamin Kramer135f7352016-06-26 12:28:59 +0000152 for (CallGraphNode *I : SCC) {
153 Function *F = I->getFunction();
Benjamin Kramer9ddfaf22013-06-15 10:55:39 +0000154
Artur Pilipenko7ae49ac2015-12-11 16:30:26 +0000155 if (!SCCMightUnwind && !F->hasFnAttribute(Attribute::NoUnwind)) {
156 F->addFnAttr(Attribute::NoUnwind);
Duncan Sands6dd02b52008-09-05 09:08:37 +0000157 MadeChange = true;
Artur Pilipenko7ae49ac2015-12-11 16:30:26 +0000158 }
159
160 if (!SCCMightReturn && !F->hasFnAttribute(Attribute::NoReturn)) {
161 F->addFnAttr(Attribute::NoReturn);
162 MadeChange = true;
Duncan Sands6dd02b52008-09-05 09:08:37 +0000163 }
Duncan Sands9f76be62007-12-10 19:09:40 +0000164 }
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000165
Benjamin Kramer135f7352016-06-26 12:28:59 +0000166 for (CallGraphNode *I : SCC) {
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000167 // Convert any invoke instructions to non-throwing functions in this node
168 // into call instructions with a branch. This makes the exception blocks
169 // dead.
Benjamin Kramer135f7352016-06-26 12:28:59 +0000170 if (Function *F = I->getFunction())
Sean Silva0fb77742016-07-02 19:12:56 +0000171 MadeChange |= SimplifyFunction(F, CG);
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000172 }
173
Misha Brukmanb1c93172005-04-21 23:48:37 +0000174 return MadeChange;
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000175}
Brian Gaeke960707c2003-11-11 22:41:34 +0000176
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000177
Sean Silva0fb77742016-07-02 19:12:56 +0000178bool PruneEH::runOnSCC(CallGraphSCC &SCC) {
179 if (skipSCC(SCC))
180 return false;
181 CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
182 return runImpl(SCC, CG);
183}
184
185
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000186// 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.
Sean Silva0fb77742016-07-02 19:12:56 +0000189static bool SimplifyFunction(Function *F, CallGraph &CG) {
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)) {
Duncan Sands9f76be62007-12-10 19:09:40 +0000194 BasicBlock *UnwindBlock = II->getUnwindDest();
David Majnemer146d7812016-01-23 05:41:22 +0000195 removeUnwindEdge(&*BB);
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000196
Duncan Sands9f76be62007-12-10 19:09:40 +0000197 // If the unwind block is now dead, nuke it.
Ramkumar Ramachandra40c3e032015-01-13 03:46:47 +0000198 if (pred_empty(UnwindBlock))
Sean Silva0fb77742016-07-02 19:12:56 +0000199 DeleteBasicBlock(UnwindBlock, CG); // Delete the new BB.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000200
Duncan Sands9f76be62007-12-10 19:09:40 +0000201 ++NumRemoved;
202 MadeChange = true;
Nick Lewycky929703b2008-03-09 17:11:18 +0000203 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000204
Chris Lattner36ffb1f2005-04-27 20:12:17 +0000205 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
Nick Lewycky4d43d3c2008-04-25 16:53:59 +0000206 if (CallInst *CI = dyn_cast<CallInst>(I++))
Joseph Tremouletb69afa82019-04-02 15:47:11 +0000207 if (CI->doesNotReturn() && !CI->isMustTailCall() &&
208 !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
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000246 if (auto *Call = dyn_cast<CallBase>(&*I)) {
247 const Function *Callee = Call->getCalledFunction();
David Majnemer2d728ec2016-01-23 05:41:27 +0000248 if (!Callee || !Intrinsic::isLeaf(Callee->getIntrinsicID()))
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000249 CGN->removeCallEdgeFor(*Call);
David Majnemer2d728ec2016-01-23 05:41:27 +0000250 else if (!Callee->isIntrinsic())
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000251 CGN->removeCallEdgeFor(*Call);
David Majnemer2d728ec2016-01-23 05:41:27 +0000252 }
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) {
Chandler Carruth9ae926b2018-08-26 09:51:22 +0000259 if (!TokenInst->isTerminator())
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}