LLVM  14.0.0git
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/InitializePasses.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Transforms/Utils.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->args());
53  II->getOperandBundlesAsDefs(OpBundles);
54  // Insert a normal call instruction...
55  CallInst *NewCall =
56  CallInst::Create(II->getFunctionType(), II->getCalledOperand(),
57  CallArgs, OpBundles, "", II);
58  NewCall->takeName(II);
59  NewCall->setCallingConv(II->getCallingConv());
60  NewCall->setAttributes(II->getAttributes());
61  NewCall->setDebugLoc(II->getDebugLoc());
62  II->replaceAllUsesWith(NewCall);
63 
64  // Insert an unconditional branch to the normal destination.
65  BranchInst::Create(II->getNormalDest(), II);
66 
67  // Remove any PHI node entries from the exception destination.
68  II->getUnwindDest()->removePredecessor(&BB);
69 
70  // Remove the invoke instruction now.
71  BB.getInstList().erase(II);
72 
73  ++NumInvokes;
74  Changed = true;
75  }
76  return Changed;
77 }
78 
80  return runImpl(F);
81 }
82 
83 namespace llvm {
85 
86 // Public Interface To the LowerInvoke pass.
87 FunctionPass *createLowerInvokePass() { return new LowerInvokeLegacyPass(); }
88 
91  bool Changed = runImpl(F);
92  if (!Changed)
93  return PreservedAnalyses::all();
94 
95  return PreservedAnalyses::none();
96 }
97 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm::createLowerInvokePass
FunctionPass * createLowerInvokePass()
Definition: LowerInvoke.cpp:87
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::Function
Definition: Function.h:61
Pass.h
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
Statistic.h
llvm::PreservedAnalyses::none
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:158
Module.h
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
llvm::LowerInvokePass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: LowerInvoke.cpp:89
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::CallBase::setAttributes
void setAttributes(AttributeList A)
Set the parameter attributes for this call.
Definition: InstrTypes.h:1472
LowerInvoke.h
llvm::CallInst::Create
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:1518
runImpl
static bool runImpl(const TargetLibraryInfo &TLI, Function &F)
Definition: ReplaceWithVeclib.cpp:177
llvm::STATISTIC
STATISTIC(NumFunctions, "Total number of functions")
Utils.h
llvm::InvokeInst
Invoke instruction.
Definition: Instructions.h:3749
llvm::BranchInst::Create
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
Definition: Instructions.h:3124
llvm::LowerInvokePassID
char & LowerInvokePassID
Definition: LowerInvoke.cpp:84
llvm::Instruction::setDebugLoc
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:367
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
Instructions.h
SmallVector.h
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition: Instructions.h:1475
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
LLVMContext.h
llvm::Value::takeName
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:382
INITIALIZE_PASS
INITIALIZE_PASS(LowerInvokeLegacyPass, "lowerinvoke", "Lower invoke and unwind, for unwindless code generators", false, false) static bool runImpl(Function &F)
Definition: LowerInvoke.cpp:43
InitializePasses.h
llvm::initializeLowerInvokeLegacyPassPass
void initializeLowerInvokeLegacyPassPass(PassRegistry &)
llvm::CallBase::setCallingConv
void setCallingConv(CallingConv::ID CC)
Definition: InstrTypes.h:1453
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37