LLVM  9.0.0svn
LowerInvoke.cpp
Go to the documentation of this file.
1 //===- LowerInvoke.cpp - Eliminate Invoke instructions --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This transformation is designed for use by code generators which do not yet
10 // support stack unwinding. This pass converts 'invoke' instructions to 'call'
11 // instructions, so that any exception-handling 'landingpad' blocks become dead
12 // code (which can be removed by running the '-simplifycfg' pass afterwards).
13 //
14 //===----------------------------------------------------------------------===//
15 
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/Utils.h"
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "lowerinvoke"
27 
28 STATISTIC(NumInvokes, "Number of invokes replaced");
29 
30 namespace {
31  class LowerInvokeLegacyPass : public FunctionPass {
32  public:
33  static char ID; // Pass identification, replacement for typeid
34  explicit LowerInvokeLegacyPass() : FunctionPass(ID) {
36  }
37  bool runOnFunction(Function &F) override;
38  };
39 }
40 
42 INITIALIZE_PASS(LowerInvokeLegacyPass, "lowerinvoke",
43  "Lower invoke and unwind, for unwindless code generators",
44  false, false)
45 
46 static bool runImpl(Function &F) {
47  bool Changed = false;
48  for (BasicBlock &BB : F)
49  if (InvokeInst *II = dyn_cast<InvokeInst>(BB.getTerminator())) {
50  SmallVector<Value *, 16> CallArgs(II->arg_begin(), II->arg_end());
52  II->getOperandBundlesAsDefs(OpBundles);
53  // Insert a normal call instruction...
54  CallInst *NewCall =
55  CallInst::Create(II->getFunctionType(), II->getCalledValue(),
56  CallArgs, OpBundles, "", II);
57  NewCall->takeName(II);
58  NewCall->setCallingConv(II->getCallingConv());
59  NewCall->setAttributes(II->getAttributes());
60  NewCall->setDebugLoc(II->getDebugLoc());
61  II->replaceAllUsesWith(NewCall);
62 
63  // Insert an unconditional branch to the normal destination.
64  BranchInst::Create(II->getNormalDest(), II);
65 
66  // Remove any PHI node entries from the exception destination.
67  II->getUnwindDest()->removePredecessor(&BB);
68 
69  // Remove the invoke instruction now.
70  BB.getInstList().erase(II);
71 
72  ++NumInvokes;
73  Changed = true;
74  }
75  return Changed;
76 }
77 
79  return runImpl(F);
80 }
81 
82 namespace llvm {
84 
85 // Public Interface To the LowerInvoke pass.
86 FunctionPass *createLowerInvokePass() { return new LowerInvokeLegacyPass(); }
87 
90  bool Changed = runImpl(F);
91  if (!Changed)
92  return PreservedAnalyses::all();
93 
94  return PreservedAnalyses::none();
95 }
96 }
static bool runImpl(Function &F, TargetLibraryInfo &TLI, DominatorTree &DT)
This is the entry point for all transforms.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
INITIALIZE_PASS(LowerInvokeLegacyPass, "lowerinvoke", "Lower invoke and unwind, for unwindless code generators", false, false) static bool runImpl(Function &F)
Definition: LowerInvoke.cpp:42
This class represents a function call, abstracting a target machine&#39;s calling convention.
char & LowerInvokePassID
Definition: LowerInvoke.cpp:83
STATISTIC(NumFunctions, "Total number of functions")
F(f)
FunctionPass * createLowerInvokePass()
Definition: LowerInvoke.cpp:86
void initializeLowerInvokeLegacyPassPass(PassRegistry &)
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:291
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:156
static bool runOnFunction(Function &F, bool PostInlining)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:321
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
void setCallingConv(CallingConv::ID CC)
Definition: InstrTypes.h:1262
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: LowerInvoke.cpp:88
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:841
Module.h This file contains the declarations for the Module class.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
void setAttributes(AttributeList A)
Set the parameter attributes for this call.
Definition: InstrTypes.h:1284
Invoke instruction.
A container for analyses that lazily runs them and caches their results.