]>
Commit | Line | Data |
---|---|---|
1a4d82fc | 1 | //===- LowerInvoke.cpp - Eliminate Invoke instructions --------------------===// |
223e47cc LB |
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 | |
1a4d82fc JJ |
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). | |
223e47cc LB |
14 | // |
15 | //===----------------------------------------------------------------------===// | |
16 | ||
223e47cc | 17 | #include "llvm/Transforms/Scalar.h" |
223e47cc LB |
18 | #include "llvm/ADT/SmallVector.h" |
19 | #include "llvm/ADT/Statistic.h" | |
970d7e83 | 20 | #include "llvm/IR/Instructions.h" |
970d7e83 LB |
21 | #include "llvm/IR/LLVMContext.h" |
22 | #include "llvm/IR/Module.h" | |
23 | #include "llvm/Pass.h" | |
223e47cc | 24 | #include "llvm/Support/CommandLine.h" |
223e47cc LB |
25 | using namespace llvm; |
26 | ||
1a4d82fc | 27 | #define DEBUG_TYPE "lowerinvoke" |
223e47cc | 28 | |
1a4d82fc | 29 | STATISTIC(NumInvokes, "Number of invokes replaced"); |
223e47cc LB |
30 | |
31 | namespace { | |
32 | class LowerInvoke : public FunctionPass { | |
223e47cc LB |
33 | public: |
34 | static char ID; // Pass identification, replacement for typeid | |
1a4d82fc | 35 | explicit LowerInvoke() : FunctionPass(ID) { |
223e47cc LB |
36 | initializeLowerInvokePass(*PassRegistry::getPassRegistry()); |
37 | } | |
1a4d82fc | 38 | bool runOnFunction(Function &F) override; |
223e47cc LB |
39 | }; |
40 | } | |
41 | ||
42 | char LowerInvoke::ID = 0; | |
43 | INITIALIZE_PASS(LowerInvoke, "lowerinvoke", | |
44 | "Lower invoke and unwind, for unwindless code generators", | |
45 | false, false) | |
46 | ||
47 | char &llvm::LowerInvokePassID = LowerInvoke::ID; | |
48 | ||
49 | // Public Interface To the LowerInvoke pass. | |
1a4d82fc JJ |
50 | FunctionPass *llvm::createLowerInvokePass() { |
51 | return new LowerInvoke(); | |
223e47cc LB |
52 | } |
53 | ||
1a4d82fc | 54 | bool LowerInvoke::runOnFunction(Function &F) { |
223e47cc LB |
55 | bool Changed = false; |
56 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) | |
57 | if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) { | |
58 | SmallVector<Value*,16> CallArgs(II->op_begin(), II->op_end() - 3); | |
59 | // Insert a normal call instruction... | |
60 | CallInst *NewCall = CallInst::Create(II->getCalledValue(), | |
61 | CallArgs, "", II); | |
62 | NewCall->takeName(II); | |
63 | NewCall->setCallingConv(II->getCallingConv()); | |
64 | NewCall->setAttributes(II->getAttributes()); | |
65 | NewCall->setDebugLoc(II->getDebugLoc()); | |
66 | II->replaceAllUsesWith(NewCall); | |
67 | ||
68 | // Insert an unconditional branch to the normal destination. | |
69 | BranchInst::Create(II->getNormalDest(), II); | |
70 | ||
71 | // Remove any PHI node entries from the exception destination. | |
72 | II->getUnwindDest()->removePredecessor(BB); | |
73 | ||
74 | // Remove the invoke instruction now. | |
75 | BB->getInstList().erase(II); | |
76 | ||
77 | ++NumInvokes; Changed = true; | |
78 | } | |
79 | return Changed; | |
80 | } |