1*9880d681SAndroid Build Coastguard Worker //===- PruneEH.cpp - Pass which deletes unused exception handlers ---------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements a simple interprocedural pass which walks the
11*9880d681SAndroid Build Coastguard Worker // call-graph, turning invoke instructions into calls, iff the callee cannot
12*9880d681SAndroid Build Coastguard Worker // throw an exception, and marking functions 'nounwind' if they cannot throw.
13*9880d681SAndroid Build Coastguard Worker // It implements this as a bottom-up traversal of the call-graph.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
16*9880d681SAndroid Build Coastguard Worker
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/IPO.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallPtrSet.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CallGraph.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CallGraphSCCPass.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/EHPersonalities.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CFG.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/InlineAsm.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LLVMContext.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/Local.h"
32*9880d681SAndroid Build Coastguard Worker #include <algorithm>
33*9880d681SAndroid Build Coastguard Worker using namespace llvm;
34*9880d681SAndroid Build Coastguard Worker
35*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "prune-eh"
36*9880d681SAndroid Build Coastguard Worker
37*9880d681SAndroid Build Coastguard Worker STATISTIC(NumRemoved, "Number of invokes removed");
38*9880d681SAndroid Build Coastguard Worker STATISTIC(NumUnreach, "Number of noreturn calls optimized");
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker namespace {
41*9880d681SAndroid Build Coastguard Worker struct PruneEH : public CallGraphSCCPass {
42*9880d681SAndroid Build Coastguard Worker static char ID; // Pass identification, replacement for typeid
PruneEH__anon499633b40111::PruneEH43*9880d681SAndroid Build Coastguard Worker PruneEH() : CallGraphSCCPass(ID) {
44*9880d681SAndroid Build Coastguard Worker initializePruneEHPass(*PassRegistry::getPassRegistry());
45*9880d681SAndroid Build Coastguard Worker }
46*9880d681SAndroid Build Coastguard Worker
47*9880d681SAndroid Build Coastguard Worker // runOnSCC - Analyze the SCC, performing the transformation if possible.
48*9880d681SAndroid Build Coastguard Worker bool runOnSCC(CallGraphSCC &SCC) override;
49*9880d681SAndroid Build Coastguard Worker
50*9880d681SAndroid Build Coastguard Worker };
51*9880d681SAndroid Build Coastguard Worker }
52*9880d681SAndroid Build Coastguard Worker static bool SimplifyFunction(Function *F, CallGraph &CG);
53*9880d681SAndroid Build Coastguard Worker static void DeleteBasicBlock(BasicBlock *BB, CallGraph &CG);
54*9880d681SAndroid Build Coastguard Worker
55*9880d681SAndroid Build Coastguard Worker char PruneEH::ID = 0;
56*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(PruneEH, "prune-eh",
57*9880d681SAndroid Build Coastguard Worker "Remove unused exception handling info", false, false)
INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)58*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
59*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(PruneEH, "prune-eh",
60*9880d681SAndroid Build Coastguard Worker "Remove unused exception handling info", false, false)
61*9880d681SAndroid Build Coastguard Worker
62*9880d681SAndroid Build Coastguard Worker Pass *llvm::createPruneEHPass() { return new PruneEH(); }
63*9880d681SAndroid Build Coastguard Worker
runImpl(CallGraphSCC & SCC,CallGraph & CG)64*9880d681SAndroid Build Coastguard Worker static bool runImpl(CallGraphSCC &SCC, CallGraph &CG) {
65*9880d681SAndroid Build Coastguard Worker SmallPtrSet<CallGraphNode *, 8> SCCNodes;
66*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
67*9880d681SAndroid Build Coastguard Worker
68*9880d681SAndroid Build Coastguard Worker // Fill SCCNodes with the elements of the SCC. Used for quickly
69*9880d681SAndroid Build Coastguard Worker // looking up whether a given CallGraphNode is in this SCC.
70*9880d681SAndroid Build Coastguard Worker for (CallGraphNode *I : SCC)
71*9880d681SAndroid Build Coastguard Worker SCCNodes.insert(I);
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard Worker // First pass, scan all of the functions in the SCC, simplifying them
74*9880d681SAndroid Build Coastguard Worker // according to what we know.
75*9880d681SAndroid Build Coastguard Worker for (CallGraphNode *I : SCC)
76*9880d681SAndroid Build Coastguard Worker if (Function *F = I->getFunction())
77*9880d681SAndroid Build Coastguard Worker MadeChange |= SimplifyFunction(F, CG);
78*9880d681SAndroid Build Coastguard Worker
79*9880d681SAndroid Build Coastguard Worker // Next, check to see if any callees might throw or if there are any external
80*9880d681SAndroid Build Coastguard Worker // functions in this SCC: if so, we cannot prune any functions in this SCC.
81*9880d681SAndroid Build Coastguard Worker // Definitions that are weak and not declared non-throwing might be
82*9880d681SAndroid Build Coastguard Worker // overridden at linktime with something that throws, so assume that.
83*9880d681SAndroid Build Coastguard Worker // If this SCC includes the unwind instruction, we KNOW it throws, so
84*9880d681SAndroid Build Coastguard Worker // obviously the SCC might throw.
85*9880d681SAndroid Build Coastguard Worker //
86*9880d681SAndroid Build Coastguard Worker bool SCCMightUnwind = false, SCCMightReturn = false;
87*9880d681SAndroid Build Coastguard Worker for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end();
88*9880d681SAndroid Build Coastguard Worker (!SCCMightUnwind || !SCCMightReturn) && I != E; ++I) {
89*9880d681SAndroid Build Coastguard Worker Function *F = (*I)->getFunction();
90*9880d681SAndroid Build Coastguard Worker if (!F) {
91*9880d681SAndroid Build Coastguard Worker SCCMightUnwind = true;
92*9880d681SAndroid Build Coastguard Worker SCCMightReturn = true;
93*9880d681SAndroid Build Coastguard Worker } else if (F->isDeclaration() || F->isInterposable()) {
94*9880d681SAndroid Build Coastguard Worker // Note: isInterposable (as opposed to hasExactDefinition) is fine above,
95*9880d681SAndroid Build Coastguard Worker // since we're not inferring new attributes here, but only using existing,
96*9880d681SAndroid Build Coastguard Worker // assumed to be correct, function attributes.
97*9880d681SAndroid Build Coastguard Worker SCCMightUnwind |= !F->doesNotThrow();
98*9880d681SAndroid Build Coastguard Worker SCCMightReturn |= !F->doesNotReturn();
99*9880d681SAndroid Build Coastguard Worker } else {
100*9880d681SAndroid Build Coastguard Worker bool CheckUnwind = !SCCMightUnwind && !F->doesNotThrow();
101*9880d681SAndroid Build Coastguard Worker bool CheckReturn = !SCCMightReturn && !F->doesNotReturn();
102*9880d681SAndroid Build Coastguard Worker // Determine if we should scan for InlineAsm in a naked function as it
103*9880d681SAndroid Build Coastguard Worker // is the only way to return without a ReturnInst. Only do this for
104*9880d681SAndroid Build Coastguard Worker // no-inline functions as functions which may be inlined cannot
105*9880d681SAndroid Build Coastguard Worker // meaningfully return via assembly.
106*9880d681SAndroid Build Coastguard Worker bool CheckReturnViaAsm = CheckReturn &&
107*9880d681SAndroid Build Coastguard Worker F->hasFnAttribute(Attribute::Naked) &&
108*9880d681SAndroid Build Coastguard Worker F->hasFnAttribute(Attribute::NoInline);
109*9880d681SAndroid Build Coastguard Worker
110*9880d681SAndroid Build Coastguard Worker if (!CheckUnwind && !CheckReturn)
111*9880d681SAndroid Build Coastguard Worker continue;
112*9880d681SAndroid Build Coastguard Worker
113*9880d681SAndroid Build Coastguard Worker for (const BasicBlock &BB : *F) {
114*9880d681SAndroid Build Coastguard Worker const TerminatorInst *TI = BB.getTerminator();
115*9880d681SAndroid Build Coastguard Worker if (CheckUnwind && TI->mayThrow()) {
116*9880d681SAndroid Build Coastguard Worker SCCMightUnwind = true;
117*9880d681SAndroid Build Coastguard Worker } else if (CheckReturn && isa<ReturnInst>(TI)) {
118*9880d681SAndroid Build Coastguard Worker SCCMightReturn = true;
119*9880d681SAndroid Build Coastguard Worker }
120*9880d681SAndroid Build Coastguard Worker
121*9880d681SAndroid Build Coastguard Worker for (const Instruction &I : BB) {
122*9880d681SAndroid Build Coastguard Worker if ((!CheckUnwind || SCCMightUnwind) &&
123*9880d681SAndroid Build Coastguard Worker (!CheckReturnViaAsm || SCCMightReturn))
124*9880d681SAndroid Build Coastguard Worker break;
125*9880d681SAndroid Build Coastguard Worker
126*9880d681SAndroid Build Coastguard Worker // Check to see if this function performs an unwind or calls an
127*9880d681SAndroid Build Coastguard Worker // unwinding function.
128*9880d681SAndroid Build Coastguard Worker if (CheckUnwind && !SCCMightUnwind && I.mayThrow()) {
129*9880d681SAndroid Build Coastguard Worker bool InstMightUnwind = true;
130*9880d681SAndroid Build Coastguard Worker if (const auto *CI = dyn_cast<CallInst>(&I)) {
131*9880d681SAndroid Build Coastguard Worker if (Function *Callee = CI->getCalledFunction()) {
132*9880d681SAndroid Build Coastguard Worker CallGraphNode *CalleeNode = CG[Callee];
133*9880d681SAndroid Build Coastguard Worker // If the callee is outside our current SCC then we may throw
134*9880d681SAndroid Build Coastguard Worker // because it might. If it is inside, do nothing.
135*9880d681SAndroid Build Coastguard Worker if (SCCNodes.count(CalleeNode) > 0)
136*9880d681SAndroid Build Coastguard Worker InstMightUnwind = false;
137*9880d681SAndroid Build Coastguard Worker }
138*9880d681SAndroid Build Coastguard Worker }
139*9880d681SAndroid Build Coastguard Worker SCCMightUnwind |= InstMightUnwind;
140*9880d681SAndroid Build Coastguard Worker }
141*9880d681SAndroid Build Coastguard Worker if (CheckReturnViaAsm && !SCCMightReturn)
142*9880d681SAndroid Build Coastguard Worker if (auto ICS = ImmutableCallSite(&I))
143*9880d681SAndroid Build Coastguard Worker if (const auto *IA = dyn_cast<InlineAsm>(ICS.getCalledValue()))
144*9880d681SAndroid Build Coastguard Worker if (IA->hasSideEffects())
145*9880d681SAndroid Build Coastguard Worker SCCMightReturn = true;
146*9880d681SAndroid Build Coastguard Worker }
147*9880d681SAndroid Build Coastguard Worker
148*9880d681SAndroid Build Coastguard Worker if (SCCMightUnwind && SCCMightReturn)
149*9880d681SAndroid Build Coastguard Worker break;
150*9880d681SAndroid Build Coastguard Worker }
151*9880d681SAndroid Build Coastguard Worker }
152*9880d681SAndroid Build Coastguard Worker }
153*9880d681SAndroid Build Coastguard Worker
154*9880d681SAndroid Build Coastguard Worker // If the SCC doesn't unwind or doesn't throw, note this fact.
155*9880d681SAndroid Build Coastguard Worker if (!SCCMightUnwind || !SCCMightReturn)
156*9880d681SAndroid Build Coastguard Worker for (CallGraphNode *I : SCC) {
157*9880d681SAndroid Build Coastguard Worker Function *F = I->getFunction();
158*9880d681SAndroid Build Coastguard Worker
159*9880d681SAndroid Build Coastguard Worker if (!SCCMightUnwind && !F->hasFnAttribute(Attribute::NoUnwind)) {
160*9880d681SAndroid Build Coastguard Worker F->addFnAttr(Attribute::NoUnwind);
161*9880d681SAndroid Build Coastguard Worker MadeChange = true;
162*9880d681SAndroid Build Coastguard Worker }
163*9880d681SAndroid Build Coastguard Worker
164*9880d681SAndroid Build Coastguard Worker if (!SCCMightReturn && !F->hasFnAttribute(Attribute::NoReturn)) {
165*9880d681SAndroid Build Coastguard Worker F->addFnAttr(Attribute::NoReturn);
166*9880d681SAndroid Build Coastguard Worker MadeChange = true;
167*9880d681SAndroid Build Coastguard Worker }
168*9880d681SAndroid Build Coastguard Worker }
169*9880d681SAndroid Build Coastguard Worker
170*9880d681SAndroid Build Coastguard Worker for (CallGraphNode *I : SCC) {
171*9880d681SAndroid Build Coastguard Worker // Convert any invoke instructions to non-throwing functions in this node
172*9880d681SAndroid Build Coastguard Worker // into call instructions with a branch. This makes the exception blocks
173*9880d681SAndroid Build Coastguard Worker // dead.
174*9880d681SAndroid Build Coastguard Worker if (Function *F = I->getFunction())
175*9880d681SAndroid Build Coastguard Worker MadeChange |= SimplifyFunction(F, CG);
176*9880d681SAndroid Build Coastguard Worker }
177*9880d681SAndroid Build Coastguard Worker
178*9880d681SAndroid Build Coastguard Worker return MadeChange;
179*9880d681SAndroid Build Coastguard Worker }
180*9880d681SAndroid Build Coastguard Worker
181*9880d681SAndroid Build Coastguard Worker
runOnSCC(CallGraphSCC & SCC)182*9880d681SAndroid Build Coastguard Worker bool PruneEH::runOnSCC(CallGraphSCC &SCC) {
183*9880d681SAndroid Build Coastguard Worker if (skipSCC(SCC))
184*9880d681SAndroid Build Coastguard Worker return false;
185*9880d681SAndroid Build Coastguard Worker CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
186*9880d681SAndroid Build Coastguard Worker return runImpl(SCC, CG);
187*9880d681SAndroid Build Coastguard Worker }
188*9880d681SAndroid Build Coastguard Worker
189*9880d681SAndroid Build Coastguard Worker
190*9880d681SAndroid Build Coastguard Worker // SimplifyFunction - Given information about callees, simplify the specified
191*9880d681SAndroid Build Coastguard Worker // function if we have invokes to non-unwinding functions or code after calls to
192*9880d681SAndroid Build Coastguard Worker // no-return functions.
SimplifyFunction(Function * F,CallGraph & CG)193*9880d681SAndroid Build Coastguard Worker static bool SimplifyFunction(Function *F, CallGraph &CG) {
194*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
195*9880d681SAndroid Build Coastguard Worker for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
196*9880d681SAndroid Build Coastguard Worker if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
197*9880d681SAndroid Build Coastguard Worker if (II->doesNotThrow() && canSimplifyInvokeNoUnwind(F)) {
198*9880d681SAndroid Build Coastguard Worker BasicBlock *UnwindBlock = II->getUnwindDest();
199*9880d681SAndroid Build Coastguard Worker removeUnwindEdge(&*BB);
200*9880d681SAndroid Build Coastguard Worker
201*9880d681SAndroid Build Coastguard Worker // If the unwind block is now dead, nuke it.
202*9880d681SAndroid Build Coastguard Worker if (pred_empty(UnwindBlock))
203*9880d681SAndroid Build Coastguard Worker DeleteBasicBlock(UnwindBlock, CG); // Delete the new BB.
204*9880d681SAndroid Build Coastguard Worker
205*9880d681SAndroid Build Coastguard Worker ++NumRemoved;
206*9880d681SAndroid Build Coastguard Worker MadeChange = true;
207*9880d681SAndroid Build Coastguard Worker }
208*9880d681SAndroid Build Coastguard Worker
209*9880d681SAndroid Build Coastguard Worker for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
210*9880d681SAndroid Build Coastguard Worker if (CallInst *CI = dyn_cast<CallInst>(I++))
211*9880d681SAndroid Build Coastguard Worker if (CI->doesNotReturn() && !isa<UnreachableInst>(I)) {
212*9880d681SAndroid Build Coastguard Worker // This call calls a function that cannot return. Insert an
213*9880d681SAndroid Build Coastguard Worker // unreachable instruction after it and simplify the code. Do this
214*9880d681SAndroid Build Coastguard Worker // by splitting the BB, adding the unreachable, then deleting the
215*9880d681SAndroid Build Coastguard Worker // new BB.
216*9880d681SAndroid Build Coastguard Worker BasicBlock *New = BB->splitBasicBlock(I);
217*9880d681SAndroid Build Coastguard Worker
218*9880d681SAndroid Build Coastguard Worker // Remove the uncond branch and add an unreachable.
219*9880d681SAndroid Build Coastguard Worker BB->getInstList().pop_back();
220*9880d681SAndroid Build Coastguard Worker new UnreachableInst(BB->getContext(), &*BB);
221*9880d681SAndroid Build Coastguard Worker
222*9880d681SAndroid Build Coastguard Worker DeleteBasicBlock(New, CG); // Delete the new BB.
223*9880d681SAndroid Build Coastguard Worker MadeChange = true;
224*9880d681SAndroid Build Coastguard Worker ++NumUnreach;
225*9880d681SAndroid Build Coastguard Worker break;
226*9880d681SAndroid Build Coastguard Worker }
227*9880d681SAndroid Build Coastguard Worker }
228*9880d681SAndroid Build Coastguard Worker
229*9880d681SAndroid Build Coastguard Worker return MadeChange;
230*9880d681SAndroid Build Coastguard Worker }
231*9880d681SAndroid Build Coastguard Worker
232*9880d681SAndroid Build Coastguard Worker /// DeleteBasicBlock - remove the specified basic block from the program,
233*9880d681SAndroid Build Coastguard Worker /// updating the callgraph to reflect any now-obsolete edges due to calls that
234*9880d681SAndroid Build Coastguard Worker /// exist in the BB.
DeleteBasicBlock(BasicBlock * BB,CallGraph & CG)235*9880d681SAndroid Build Coastguard Worker static void DeleteBasicBlock(BasicBlock *BB, CallGraph &CG) {
236*9880d681SAndroid Build Coastguard Worker assert(pred_empty(BB) && "BB is not dead!");
237*9880d681SAndroid Build Coastguard Worker
238*9880d681SAndroid Build Coastguard Worker Instruction *TokenInst = nullptr;
239*9880d681SAndroid Build Coastguard Worker
240*9880d681SAndroid Build Coastguard Worker CallGraphNode *CGN = CG[BB->getParent()];
241*9880d681SAndroid Build Coastguard Worker for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; ) {
242*9880d681SAndroid Build Coastguard Worker --I;
243*9880d681SAndroid Build Coastguard Worker
244*9880d681SAndroid Build Coastguard Worker if (I->getType()->isTokenTy()) {
245*9880d681SAndroid Build Coastguard Worker TokenInst = &*I;
246*9880d681SAndroid Build Coastguard Worker break;
247*9880d681SAndroid Build Coastguard Worker }
248*9880d681SAndroid Build Coastguard Worker
249*9880d681SAndroid Build Coastguard Worker if (auto CS = CallSite (&*I)) {
250*9880d681SAndroid Build Coastguard Worker const Function *Callee = CS.getCalledFunction();
251*9880d681SAndroid Build Coastguard Worker if (!Callee || !Intrinsic::isLeaf(Callee->getIntrinsicID()))
252*9880d681SAndroid Build Coastguard Worker CGN->removeCallEdgeFor(CS);
253*9880d681SAndroid Build Coastguard Worker else if (!Callee->isIntrinsic())
254*9880d681SAndroid Build Coastguard Worker CGN->removeCallEdgeFor(CS);
255*9880d681SAndroid Build Coastguard Worker }
256*9880d681SAndroid Build Coastguard Worker
257*9880d681SAndroid Build Coastguard Worker if (!I->use_empty())
258*9880d681SAndroid Build Coastguard Worker I->replaceAllUsesWith(UndefValue::get(I->getType()));
259*9880d681SAndroid Build Coastguard Worker }
260*9880d681SAndroid Build Coastguard Worker
261*9880d681SAndroid Build Coastguard Worker if (TokenInst) {
262*9880d681SAndroid Build Coastguard Worker if (!isa<TerminatorInst>(TokenInst))
263*9880d681SAndroid Build Coastguard Worker changeToUnreachable(TokenInst->getNextNode(), /*UseLLVMTrap=*/false);
264*9880d681SAndroid Build Coastguard Worker } else {
265*9880d681SAndroid Build Coastguard Worker // Get the list of successors of this block.
266*9880d681SAndroid Build Coastguard Worker std::vector<BasicBlock *> Succs(succ_begin(BB), succ_end(BB));
267*9880d681SAndroid Build Coastguard Worker
268*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Succs.size(); i != e; ++i)
269*9880d681SAndroid Build Coastguard Worker Succs[i]->removePredecessor(BB);
270*9880d681SAndroid Build Coastguard Worker
271*9880d681SAndroid Build Coastguard Worker BB->eraseFromParent();
272*9880d681SAndroid Build Coastguard Worker }
273*9880d681SAndroid Build Coastguard Worker }
274