1 //===- LowerInvoke.cpp - Eliminate Invoke instructions --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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).
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Transforms/Utils/LowerInvoke.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Transforms/Scalar.h"
27 #define DEBUG_TYPE "lowerinvoke"
29 STATISTIC(NumInvokes
, "Number of invokes replaced");
32 class LowerInvokeLegacyPass
: public FunctionPass
{
34 static char ID
; // Pass identification, replacement for typeid
35 explicit LowerInvokeLegacyPass() : FunctionPass(ID
) {
36 initializeLowerInvokeLegacyPassPass(*PassRegistry::getPassRegistry());
38 bool runOnFunction(Function
&F
) override
;
42 char LowerInvokeLegacyPass::ID
= 0;
43 INITIALIZE_PASS(LowerInvokeLegacyPass
, "lowerinvoke",
44 "Lower invoke and unwind, for unwindless code generators",
47 static bool runImpl(Function
&F
) {
49 for (BasicBlock
&BB
: F
)
50 if (InvokeInst
*II
= dyn_cast
<InvokeInst
>(BB
.getTerminator())) {
51 SmallVector
<Value
*, 16> CallArgs(II
->op_begin(), II
->op_end() - 3);
52 // Insert a normal call instruction...
54 CallInst::Create(II
->getCalledValue(), CallArgs
, "", II
);
55 NewCall
->takeName(II
);
56 NewCall
->setCallingConv(II
->getCallingConv());
57 NewCall
->setAttributes(II
->getAttributes());
58 NewCall
->setDebugLoc(II
->getDebugLoc());
59 II
->replaceAllUsesWith(NewCall
);
61 // Insert an unconditional branch to the normal destination.
62 BranchInst::Create(II
->getNormalDest(), II
);
64 // Remove any PHI node entries from the exception destination.
65 II
->getUnwindDest()->removePredecessor(&BB
);
67 // Remove the invoke instruction now.
68 BB
.getInstList().erase(II
);
76 bool LowerInvokeLegacyPass::runOnFunction(Function
&F
) {
81 char &LowerInvokePassID
= LowerInvokeLegacyPass::ID
;
83 // Public Interface To the LowerInvoke pass.
84 FunctionPass
*createLowerInvokePass() { return new LowerInvokeLegacyPass(); }
86 PreservedAnalyses
LowerInvokePass::run(Function
&F
,
87 FunctionAnalysisManager
&AM
) {
88 bool Changed
= runImpl(F
);
90 return PreservedAnalyses::all();
92 return PreservedAnalyses::none();