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