blob: ef7b43e8b5572f7a34b3a83381395cdd8f4b37f1 [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"
David Blaikie31b98d22018-06-04 21:23:21 +000021#include "llvm/Transforms/Utils/Local.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000022#include "llvm/IR/CFG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Constants.h"
24#include "llvm/IR/Function.h"
David Majnemer5185c3c2015-06-27 07:52:53 +000025#include "llvm/IR/InlineAsm.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/LLVMContext.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000028#include "llvm/Support/raw_ostream.h"
29#include "llvm/Transforms/IPO.h"
Chris Lattner88a8a322004-10-18 15:43:46 +000030#include <algorithm>
Chris Lattnerf52e03c2003-11-21 21:54:22 +000031using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000032
Chandler Carruth964daaa2014-04-22 02:55:47 +000033#define DEBUG_TYPE "prune-eh"
34
Chris Lattner1631bcb2006-12-19 22:09:18 +000035STATISTIC(NumRemoved, "Number of invokes removed");
36STATISTIC(NumUnreach, "Number of noreturn calls optimized");
Chris Lattner0a3f8d52003-08-31 02:47:32 +000037
Chris Lattner1631bcb2006-12-19 22:09:18 +000038namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000039 struct PruneEH : public CallGraphSCCPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000040 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000041 PruneEH() : CallGraphSCCPass(ID) {
42 initializePruneEHPass(*PassRegistry::getPassRegistry());
43 }
Devang Patel09f162c2007-05-01 21:15:47 +000044
Chris Lattner0a3f8d52003-08-31 02:47:32 +000045 // runOnSCC - Analyze the SCC, performing the transformation if possible.
Craig Topper3e4c6972014-03-05 09:10:37 +000046 bool runOnSCC(CallGraphSCC &SCC) override;
Chris Lattner93f4e9d2005-04-27 04:52:23 +000047
Chris Lattner0a3f8d52003-08-31 02:47:32 +000048 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000049}
Sean Silva0fb77742016-07-02 19:12:56 +000050static bool SimplifyFunction(Function *F, CallGraph &CG);
51static void DeleteBasicBlock(BasicBlock *BB, CallGraph &CG);
Chris Lattner0a3f8d52003-08-31 02:47:32 +000052
Dan Gohmand78c4002008-05-13 00:00:25 +000053char PruneEH::ID = 0;
Owen Anderson071cee02010-10-13 22:00:45 +000054INITIALIZE_PASS_BEGIN(PruneEH, "prune-eh",
55 "Remove unused exception handling info", false, false)
Chandler Carruth6378cf52013-11-26 04:19:30 +000056INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Owen Anderson071cee02010-10-13 22:00:45 +000057INITIALIZE_PASS_END(PruneEH, "prune-eh",
Owen Andersondf7a4f22010-10-07 22:25:06 +000058 "Remove unused exception handling info", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000059
Devang Patel13058a52007-01-26 00:47:38 +000060Pass *llvm::createPruneEHPass() { return new PruneEH(); }
Chris Lattner75444c72003-08-31 16:30:07 +000061
Sean Silva0fb77742016-07-02 19:12:56 +000062static bool runImpl(CallGraphSCC &SCC, CallGraph &CG) {
Duncan Sands57512a12008-09-29 14:59:04 +000063 SmallPtrSet<CallGraphNode *, 8> SCCNodes;
Chris Lattner93f4e9d2005-04-27 04:52:23 +000064 bool MadeChange = false;
Chris Lattner0a3f8d52003-08-31 02:47:32 +000065
Duncan Sands57512a12008-09-29 14:59:04 +000066 // Fill SCCNodes with the elements of the SCC. Used for quickly
67 // looking up whether a given CallGraphNode is in this SCC.
Benjamin Kramer135f7352016-06-26 12:28:59 +000068 for (CallGraphNode *I : SCC)
69 SCCNodes.insert(I);
Duncan Sands57512a12008-09-29 14:59:04 +000070
Chris Lattner93f4e9d2005-04-27 04:52:23 +000071 // First pass, scan all of the functions in the SCC, simplifying them
72 // according to what we know.
Benjamin Kramer135f7352016-06-26 12:28:59 +000073 for (CallGraphNode *I : SCC)
74 if (Function *F = I->getFunction())
Sean Silva0fb77742016-07-02 19:12:56 +000075 MadeChange |= SimplifyFunction(F, CG);
Chris Lattner93f4e9d2005-04-27 04:52:23 +000076
77 // Next, check to see if any callees might throw or if there are any external
Chris Lattner0a3f8d52003-08-31 02:47:32 +000078 // functions in this SCC: if so, we cannot prune any functions in this SCC.
Fangrui Songf78650a2018-07-30 19:41:25 +000079 // Definitions that are weak and not declared non-throwing might be
Dale Johannesene7f5bc22008-05-16 21:31:48 +000080 // overridden at linktime with something that throws, so assume that.
Chris Lattner04ecefe2003-09-08 19:44:26 +000081 // If this SCC includes the unwind instruction, we KNOW it throws, so
Chris Lattner0a3f8d52003-08-31 02:47:32 +000082 // obviously the SCC might throw.
83 //
Chris Lattner93f4e9d2005-04-27 04:52:23 +000084 bool SCCMightUnwind = false, SCCMightReturn = false;
Fangrui Songf78650a2018-07-30 19:41:25 +000085 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end();
Chris Lattner4422d312010-04-16 22:42:17 +000086 (!SCCMightUnwind || !SCCMightReturn) && I != E; ++I) {
87 Function *F = (*I)->getFunction();
Craig Topperf40110f2014-04-25 05:29:35 +000088 if (!F) {
Chris Lattner7f4f7732005-04-26 23:53:25 +000089 SCCMightUnwind = true;
Chris Lattner93f4e9d2005-04-27 04:52:23 +000090 SCCMightReturn = true;
Sanjoy Das0359a192016-10-03 19:35:30 +000091 } else if (!F->hasExactDefinition()) {
Duncan Sands3353ed02007-12-18 09:59:50 +000092 SCCMightUnwind |= !F->doesNotThrow();
93 SCCMightReturn |= !F->doesNotReturn();
Chris Lattnerd041dcd2004-04-12 04:06:38 +000094 } else {
Duncan Sands3353ed02007-12-18 09:59:50 +000095 bool CheckUnwind = !SCCMightUnwind && !F->doesNotThrow();
96 bool CheckReturn = !SCCMightReturn && !F->doesNotReturn();
David Majnemer5185c3c2015-06-27 07:52:53 +000097 // Determine if we should scan for InlineAsm in a naked function as it
98 // is the only way to return without a ReturnInst. Only do this for
99 // no-inline functions as functions which may be inlined cannot
100 // meaningfully return via assembly.
101 bool CheckReturnViaAsm = CheckReturn &&
102 F->hasFnAttribute(Attribute::Naked) &&
103 F->hasFnAttribute(Attribute::NoInline);
Duncan Sands9f76be62007-12-10 19:09:40 +0000104
105 if (!CheckUnwind && !CheckReturn)
106 continue;
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000107
David Majnemer5185c3c2015-06-27 07:52:53 +0000108 for (const BasicBlock &BB : *F) {
Chandler Carruthedb12a82018-10-15 10:04:59 +0000109 const Instruction *TI = BB.getTerminator();
David Majnemer5185c3c2015-06-27 07:52:53 +0000110 if (CheckUnwind && TI->mayThrow()) {
Chris Lattner7f4f7732005-04-26 23:53:25 +0000111 SCCMightUnwind = true;
David Majnemer5185c3c2015-06-27 07:52:53 +0000112 } else if (CheckReturn && isa<ReturnInst>(TI)) {
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000113 SCCMightReturn = true;
Chris Lattnerd041dcd2004-04-12 04:06:38 +0000114 }
Chris Lattner56997dd2004-02-08 21:15:59 +0000115
David Majnemer5185c3c2015-06-27 07:52:53 +0000116 for (const Instruction &I : BB) {
117 if ((!CheckUnwind || SCCMightUnwind) &&
118 (!CheckReturnViaAsm || SCCMightReturn))
119 break;
120
121 // Check to see if this function performs an unwind or calls an
122 // unwinding function.
123 if (CheckUnwind && !SCCMightUnwind && I.mayThrow()) {
124 bool InstMightUnwind = true;
125 if (const auto *CI = dyn_cast<CallInst>(&I)) {
126 if (Function *Callee = CI->getCalledFunction()) {
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000127 CallGraphNode *CalleeNode = CG[Callee];
David Majnemer5185c3c2015-06-27 07:52:53 +0000128 // If the callee is outside our current SCC then we may throw
129 // because it might. If it is inside, do nothing.
130 if (SCCNodes.count(CalleeNode) > 0)
131 InstMightUnwind = false;
Chris Lattner56997dd2004-02-08 21:15:59 +0000132 }
133 }
David Majnemer5185c3c2015-06-27 07:52:53 +0000134 SCCMightUnwind |= InstMightUnwind;
135 }
136 if (CheckReturnViaAsm && !SCCMightReturn)
137 if (auto ICS = ImmutableCallSite(&I))
138 if (const auto *IA = dyn_cast<InlineAsm>(ICS.getCalledValue()))
139 if (IA->hasSideEffects())
140 SCCMightReturn = true;
141 }
142
143 if (SCCMightUnwind && SCCMightReturn)
144 break;
Chris Lattner5a6fa292003-09-15 02:22:50 +0000145 }
Chris Lattnerd041dcd2004-04-12 04:06:38 +0000146 }
147 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000148
149 // If the SCC doesn't unwind or doesn't throw, note this fact.
Duncan Sands9f76be62007-12-10 19:09:40 +0000150 if (!SCCMightUnwind || !SCCMightReturn)
Benjamin Kramer135f7352016-06-26 12:28:59 +0000151 for (CallGraphNode *I : SCC) {
152 Function *F = I->getFunction();
Benjamin Kramer9ddfaf22013-06-15 10:55:39 +0000153
Artur Pilipenko7ae49ac2015-12-11 16:30:26 +0000154 if (!SCCMightUnwind && !F->hasFnAttribute(Attribute::NoUnwind)) {
155 F->addFnAttr(Attribute::NoUnwind);
Duncan Sands6dd02b52008-09-05 09:08:37 +0000156 MadeChange = true;
Artur Pilipenko7ae49ac2015-12-11 16:30:26 +0000157 }
158
159 if (!SCCMightReturn && !F->hasFnAttribute(Attribute::NoReturn)) {
160 F->addFnAttr(Attribute::NoReturn);
161 MadeChange = true;
Duncan Sands6dd02b52008-09-05 09:08:37 +0000162 }
Duncan Sands9f76be62007-12-10 19:09:40 +0000163 }
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000164
Benjamin Kramer135f7352016-06-26 12:28:59 +0000165 for (CallGraphNode *I : SCC) {
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000166 // Convert any invoke instructions to non-throwing functions in this node
167 // into call instructions with a branch. This makes the exception blocks
168 // dead.
Benjamin Kramer135f7352016-06-26 12:28:59 +0000169 if (Function *F = I->getFunction())
Sean Silva0fb77742016-07-02 19:12:56 +0000170 MadeChange |= SimplifyFunction(F, CG);
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000171 }
172
Misha Brukmanb1c93172005-04-21 23:48:37 +0000173 return MadeChange;
Chris Lattner0a3f8d52003-08-31 02:47:32 +0000174}
Brian Gaeke960707c2003-11-11 22:41:34 +0000175
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000176
Sean Silva0fb77742016-07-02 19:12:56 +0000177bool PruneEH::runOnSCC(CallGraphSCC &SCC) {
178 if (skipSCC(SCC))
179 return false;
180 CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
181 return runImpl(SCC, CG);
182}
183
184
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000185// SimplifyFunction - Given information about callees, simplify the specified
186// function if we have invokes to non-unwinding functions or code after calls to
187// no-return functions.
Sean Silva0fb77742016-07-02 19:12:56 +0000188static bool SimplifyFunction(Function *F, CallGraph &CG) {
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000189 bool MadeChange = false;
190 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
191 if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
David Majnemer7fddecc2015-06-17 20:52:32 +0000192 if (II->doesNotThrow() && canSimplifyInvokeNoUnwind(F)) {
Duncan Sands9f76be62007-12-10 19:09:40 +0000193 BasicBlock *UnwindBlock = II->getUnwindDest();
David Majnemer146d7812016-01-23 05:41:22 +0000194 removeUnwindEdge(&*BB);
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000195
Duncan Sands9f76be62007-12-10 19:09:40 +0000196 // If the unwind block is now dead, nuke it.
Ramkumar Ramachandra40c3e032015-01-13 03:46:47 +0000197 if (pred_empty(UnwindBlock))
Sean Silva0fb77742016-07-02 19:12:56 +0000198 DeleteBasicBlock(UnwindBlock, CG); // Delete the new BB.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000199
Duncan Sands9f76be62007-12-10 19:09:40 +0000200 ++NumRemoved;
201 MadeChange = true;
Nick Lewycky929703b2008-03-09 17:11:18 +0000202 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000203
Chris Lattner36ffb1f2005-04-27 20:12:17 +0000204 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
Nick Lewycky4d43d3c2008-04-25 16:53:59 +0000205 if (CallInst *CI = dyn_cast<CallInst>(I++))
Joseph Tremouletb69afa82019-04-02 15:47:11 +0000206 if (CI->doesNotReturn() && !CI->isMustTailCall() &&
207 !isa<UnreachableInst>(I)) {
Duncan Sands9f76be62007-12-10 19:09:40 +0000208 // This call calls a function that cannot return. Insert an
209 // unreachable instruction after it and simplify the code. Do this
210 // by splitting the BB, adding the unreachable, then deleting the
211 // new BB.
212 BasicBlock *New = BB->splitBasicBlock(I);
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000213
Duncan Sands9f76be62007-12-10 19:09:40 +0000214 // Remove the uncond branch and add an unreachable.
215 BB->getInstList().pop_back();
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000216 new UnreachableInst(BB->getContext(), &*BB);
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000217
Sean Silva0fb77742016-07-02 19:12:56 +0000218 DeleteBasicBlock(New, CG); // Delete the new BB.
Duncan Sands9f76be62007-12-10 19:09:40 +0000219 MadeChange = true;
220 ++NumUnreach;
221 break;
Nick Lewycky0ac65c32008-03-09 17:13:05 +0000222 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000223 }
Nick Lewycky4d43d3c2008-04-25 16:53:59 +0000224
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000225 return MadeChange;
226}
227
228/// DeleteBasicBlock - remove the specified basic block from the program,
229/// updating the callgraph to reflect any now-obsolete edges due to calls that
230/// exist in the BB.
Sean Silva0fb77742016-07-02 19:12:56 +0000231static void DeleteBasicBlock(BasicBlock *BB, CallGraph &CG) {
Ramkumar Ramachandra40c3e032015-01-13 03:46:47 +0000232 assert(pred_empty(BB) && "BB is not dead!");
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000233
David Majnemer4bf0b6b2016-01-23 05:41:29 +0000234 Instruction *TokenInst = nullptr;
235
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000236 CallGraphNode *CGN = CG[BB->getParent()];
237 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; ) {
238 --I;
David Majnemer2d728ec2016-01-23 05:41:27 +0000239
David Majnemer4bf0b6b2016-01-23 05:41:29 +0000240 if (I->getType()->isTokenTy()) {
241 TokenInst = &*I;
242 break;
243 }
244
David Majnemer2d728ec2016-01-23 05:41:27 +0000245 if (auto CS = CallSite (&*I)) {
246 const Function *Callee = CS.getCalledFunction();
247 if (!Callee || !Intrinsic::isLeaf(Callee->getIntrinsicID()))
248 CGN->removeCallEdgeFor(CS);
249 else if (!Callee->isIntrinsic())
250 CGN->removeCallEdgeFor(CS);
251 }
252
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000253 if (!I->use_empty())
Owen Andersonb292b8c2009-07-30 23:03:37 +0000254 I->replaceAllUsesWith(UndefValue::get(I->getType()));
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000255 }
256
David Majnemer4bf0b6b2016-01-23 05:41:29 +0000257 if (TokenInst) {
Chandler Carruth9ae926b2018-08-26 09:51:22 +0000258 if (!TokenInst->isTerminator())
David Majnemere14e7bc2016-06-25 08:19:55 +0000259 changeToUnreachable(TokenInst->getNextNode(), /*UseLLVMTrap=*/false);
David Majnemer4bf0b6b2016-01-23 05:41:29 +0000260 } else {
261 // Get the list of successors of this block.
262 std::vector<BasicBlock *> Succs(succ_begin(BB), succ_end(BB));
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000263
David Majnemer4bf0b6b2016-01-23 05:41:29 +0000264 for (unsigned i = 0, e = Succs.size(); i != e; ++i)
265 Succs[i]->removePredecessor(BB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000266
David Majnemer4bf0b6b2016-01-23 05:41:29 +0000267 BB->eraseFromParent();
268 }
Chris Lattner93f4e9d2005-04-27 04:52:23 +0000269}