LLVM 19.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
18#include "llvm/ADT/Statistic.h"
21#include "llvm/Pass.h"
23using namespace llvm;
24
25#define DEBUG_TYPE "lower-invoke"
26
27STATISTIC(NumInvokes, "Number of invokes replaced");
28
29namespace {
30 class LowerInvokeLegacyPass : public FunctionPass {
31 public:
32 static char ID; // Pass identification, replacement for typeid
33 explicit LowerInvokeLegacyPass() : FunctionPass(ID) {
35 }
36 bool runOnFunction(Function &F) override;
37 };
38}
39
40char LowerInvokeLegacyPass::ID = 0;
41INITIALIZE_PASS(LowerInvokeLegacyPass, "lowerinvoke",
42 "Lower invoke and unwind, for unwindless code generators",
43 false, false)
44
45static bool runImpl(Function &F) {
46 bool Changed = false;
47 for (BasicBlock &BB : F)
48 if (InvokeInst *II = dyn_cast<InvokeInst>(BB.getTerminator())) {
49 SmallVector<Value *, 16> CallArgs(II->args());
51 II->getOperandBundlesAsDefs(OpBundles);
52 // Insert a normal call instruction...
53 CallInst *NewCall =
54 CallInst::Create(II->getFunctionType(), II->getCalledOperand(),
55 CallArgs, OpBundles, "", II->getIterator());
56 NewCall->takeName(II);
57 NewCall->setCallingConv(II->getCallingConv());
58 NewCall->setAttributes(II->getAttributes());
59 NewCall->setDebugLoc(II->getDebugLoc());
60 II->replaceAllUsesWith(NewCall);
61
62 // Insert an unconditional branch to the normal destination.
63 BranchInst::Create(II->getNormalDest(), II->getIterator());
64
65 // Remove any PHI node entries from the exception destination.
66 II->getUnwindDest()->removePredecessor(&BB);
67
68 // Remove the invoke instruction now.
69 II->eraseFromParent();
70
71 ++NumInvokes;
72 Changed = true;
73 }
74 return Changed;
75}
76
77bool LowerInvokeLegacyPass::runOnFunction(Function &F) {
78 return runImpl(F);
79}
80
81namespace llvm {
82char &LowerInvokePassID = LowerInvokeLegacyPass::ID;
83
84// Public Interface To the LowerInvoke pass.
85FunctionPass *createLowerInvokePass() { return new LowerInvokeLegacyPass(); }
86
89 bool Changed = runImpl(F);
90 if (!Changed)
92
94}
95}
static bool runImpl(Function &F, const TargetLowering &TLI)
#define F(x, y, z)
Definition: MD5.cpp:55
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:348
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
static BranchInst * Create(BasicBlock *IfTrue, BasicBlock::iterator InsertBefore)
void setCallingConv(CallingConv::ID CC)
Definition: InstrTypes.h:1771
void setAttributes(AttributeList A)
Set the parameter attributes for this call.
Definition: InstrTypes.h:1790
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr, BasicBlock::iterator InsertBefore)
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:451
Invoke instruction.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: LowerInvoke.cpp:87
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:115
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:383
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeLowerInvokeLegacyPassPass(PassRegistry &)
FunctionPass * createLowerInvokePass()
Definition: LowerInvoke.cpp:85
char & LowerInvokePassID
Definition: LowerInvoke.cpp:82