1 //===- LowerInvoke.cpp - Eliminate Invoke instructions --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This transformation is designed for use by code generators which do not yet
11 // support stack unwinding.  This pass converts 'invoke' instructions to 'call'
12 // instructions, so that any exception-handling 'landingpad' blocks become dead
13 // code (which can be removed by running the '-simplifycfg' pass afterwards).
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Transforms/Scalar.h"
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "lowerinvoke"
27 
28 STATISTIC(NumInvokes, "Number of invokes replaced");
29 
30 namespace {
31   class LowerInvoke : public FunctionPass {
32   public:
33     static char ID; // Pass identification, replacement for typeid
LowerInvoke()34     explicit LowerInvoke() : FunctionPass(ID) {
35       initializeLowerInvokePass(*PassRegistry::getPassRegistry());
36     }
37     bool runOnFunction(Function &F) override;
38   };
39 }
40 
41 char LowerInvoke::ID = 0;
42 INITIALIZE_PASS(LowerInvoke, "lowerinvoke",
43                 "Lower invoke and unwind, for unwindless code generators",
44                 false, false)
45 
46 char &llvm::LowerInvokePassID = LowerInvoke::ID;
47 
48 // Public Interface To the LowerInvoke pass.
createLowerInvokePass()49 FunctionPass *llvm::createLowerInvokePass() {
50   return new LowerInvoke();
51 }
52 
runOnFunction(Function & F)53 bool LowerInvoke::runOnFunction(Function &F) {
54   bool Changed = false;
55   for (BasicBlock &BB : F)
56     if (InvokeInst *II = dyn_cast<InvokeInst>(BB.getTerminator())) {
57       SmallVector<Value*,16> CallArgs(II->op_begin(), II->op_end() - 3);
58       // Insert a normal call instruction...
59       CallInst *NewCall = CallInst::Create(II->getCalledValue(),
60                                            CallArgs, "", II);
61       NewCall->takeName(II);
62       NewCall->setCallingConv(II->getCallingConv());
63       NewCall->setAttributes(II->getAttributes());
64       NewCall->setDebugLoc(II->getDebugLoc());
65       II->replaceAllUsesWith(NewCall);
66 
67       // Insert an unconditional branch to the normal destination.
68       BranchInst::Create(II->getNormalDest(), II);
69 
70       // Remove any PHI node entries from the exception destination.
71       II->getUnwindDest()->removePredecessor(&BB);
72 
73       // Remove the invoke instruction now.
74       BB.getInstList().erase(II);
75 
76       ++NumInvokes; Changed = true;
77     }
78   return Changed;
79 }
80