LLVM  4.0.0
LowerInvoke.cpp
Go to the documentation of this file.
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 
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"
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "lowerinvoke"
28 
29 STATISTIC(NumInvokes, "Number of invokes replaced");
30 
31 namespace {
32  class LowerInvokeLegacyPass : public FunctionPass {
33  public:
34  static char ID; // Pass identification, replacement for typeid
35  explicit LowerInvokeLegacyPass() : FunctionPass(ID) {
37  }
38  bool runOnFunction(Function &F) override;
39  };
40 }
41 
43 INITIALIZE_PASS(LowerInvokeLegacyPass, "lowerinvoke",
44  "Lower invoke and unwind, for unwindless code generators",
45  false, false)
46 
47 static bool runImpl(Function &F) {
48  bool Changed = false;
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...
53  CallInst *NewCall =
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);
60 
61  // Insert an unconditional branch to the normal destination.
62  BranchInst::Create(II->getNormalDest(), II);
63 
64  // Remove any PHI node entries from the exception destination.
65  II->getUnwindDest()->removePredecessor(&BB);
66 
67  // Remove the invoke instruction now.
68  BB.getInstList().erase(II);
69 
70  ++NumInvokes;
71  Changed = true;
72  }
73  return Changed;
74 }
75 
76 bool LowerInvokeLegacyPass::runOnFunction(Function &F) {
77  return runImpl(F);
78 }
79 
80 namespace llvm {
82 
83 // Public Interface To the LowerInvoke pass.
84 FunctionPass *createLowerInvokePass() { return new LowerInvokeLegacyPass(); }
85 
88  bool Changed = runImpl(F);
89  if (!Changed)
90  return PreservedAnalyses::all();
91 
92  return PreservedAnalyses::none();
93 }
94 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
This class represents a function call, abstracting a target machine's calling convention.
char & LowerInvokePassID
Definition: LowerInvoke.cpp:81
FunctionPass * createLowerInvokePass()
Definition: LowerInvoke.cpp:84
void initializeLowerInvokeLegacyPassPass(PassRegistry &)
#define F(x, y, z)
Definition: MD5.cpp:51
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:110
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:107
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:113
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: LowerInvoke.cpp:86
static CallInst * Create(Value *Func, ArrayRef< Value * > Args, ArrayRef< OperandBundleDef > Bundles=None, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
Module.h This file contains the declarations for the Module class.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
static bool runImpl(CallGraphSCC &SCC, CallGraph &CG, function_ref< AAResults &(Function &F)> AARGetter, unsigned MaxElements)
Invoke instruction.
A container for analyses that lazily runs them and caches their results.
INITIALIZE_PASS(LowerInvokeLegacyPass,"lowerinvoke","Lower invoke and unwind, for unwindless code generators", false, false) static bool runImpl(Function &F)
Definition: LowerInvoke.cpp:43