74#include "llvm/IR/IntrinsicsWebAssembly.h"
82#define DEBUG_TYPE "wasm-eh-prepare"
85class WasmEHPrepareImpl {
86 friend class WasmEHPrepare;
88 Type *LPadContextTy =
nullptr;
92 Value *LPadIndexField =
nullptr;
93 Value *LSDAField =
nullptr;
94 Value *SelectorField =
nullptr;
106 void prepareEHPad(
BasicBlock *BB,
bool NeedPersonality,
unsigned Index = 0);
109 WasmEHPrepareImpl() =
default;
110 WasmEHPrepareImpl(
Type *LPadContextTy_) : LPadContextTy(LPadContextTy_) {}
121 bool doInitialization(
Module &M)
override;
125 return "WebAssembly Exception handling preparation";
133 auto &Context =
F.getContext();
136 auto *LPadContextTy =
138 WasmEHPrepareImpl
P(LPadContextTy);
143char WasmEHPrepare::ID = 0;
145 "Prepare WebAssembly exceptions",
false,
false)
151bool WasmEHPrepare::doInitialization(
Module &M) {
162template <
typename Container>
165 while (!WL.
empty()) {
174bool WasmEHPrepareImpl::runOnFunction(
Function &
F) {
181bool WasmEHPrepareImpl::prepareThrows(
Function &
F) {
193 if (!ThrowI || ThrowI->getFunction() != &
F)
196 auto *BB = ThrowI->getParent();
199 IRB.SetInsertPoint(BB);
200 IRB.CreateUnreachable();
207bool WasmEHPrepareImpl::prepareEHPads(
Function &
F) {
225 if (!
F.hasPersonalityFn())
232 "' does not have a supported Wasm personality function");
234 assert(
F.hasPersonalityFn() &&
"Personality function not found");
241 LPadContextGV =
M.getOrInsertGlobal(
"__wasm_lpad_context", LPadContextTy);
244 LPadIndexField = LPadContextGV;
245 LSDAField = IRB.CreateConstInBoundsGEP2_32(LPadContextTy, LPadContextGV, 0, 1,
247 SelectorField = IRB.CreateConstInBoundsGEP2_32(LPadContextTy, LPadContextGV,
248 0, 2,
"selector_gep");
267 auto *PersPrototype =
273 F->setDoesNotThrow();
276 for (
auto *BB : CatchPads) {
280 if (CPI->arg_size() == 1 &&
282 prepareEHPad(BB,
false);
284 prepareEHPad(BB,
true, Index++);
288 for (
auto *BB : CleanupPads)
289 prepareEHPad(BB,
false);
296void WasmEHPrepareImpl::prepareEHPad(
BasicBlock *BB,
bool NeedPersonality,
303 Instruction *GetExnCI =
nullptr, *GetSelectorCI =
nullptr;
304 for (
auto &U : FPI->uses()) {
306 if (CI->getCalledOperand() == GetExnF)
308 if (CI->getCalledOperand() == GetSelectorF)
317 "wasm.get.ehselector() cannot exist w/o wasm.get.exception()");
332 if (!NeedPersonality) {
334 assert(GetSelectorCI->use_empty() &&
335 "wasm.get.ehselector() still has uses!");
336 GetSelectorCI->eraseFromParent();
345 IRB.CreateCall(LPadIndexF, {FPI, IRB.getInt32(Index)});
348 IRB.CreateStore(IRB.getInt32(Index), LPadIndexField);
355 IRB.CreateStore(IRB.CreateCall(LSDAF), LSDAField);
364 IRB.CreateLoad(IRB.getInt32Ty(), SelectorField,
"selector");
368 assert(GetSelectorCI &&
"wasm.get.ehselector() call does not exist");
369 GetSelectorCI->replaceAllUsesWith(Selector);
370 GetSelectorCI->eraseFromParent();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool runOnFunction(Function &F, bool PostInlining)
Module.h This file contains the declarations for the Module class.
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
static void eraseDeadBBsAndChildren(const Container &BBs)
LLVM Basic Block Representation.
LLVM_ABI const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::iterator iterator
Instruction iterators...
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
bool isEHPad() const
Return true if this basic block is an exception handling block.
This class represents a function call, abstracting a target machine's calling convention.
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
FunctionPass class - This class is used to implement most global optimizations.
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
A Module instance is used to store all the information related to an LLVM module.
static LLVM_ABI PointerType * get(LLVMContext &C, unsigned AddressSpace)
This constructs an opaque pointer to an object in a numbered address space.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
static LLVM_ABI StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
The instances of the Type class are immutable: once they are created, they are never changed.
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
LLVM Value Representation.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
iterator_range< user_iterator > users()
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > OverloadTys={})
Look up the Function declaration of the intrinsic id in the Module M.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI StringRef getEHPersonalityName(EHPersonality Pers)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
auto successors(const MachineBasicBlock *BB)
LLVM_ABI FunctionPass * createWasmEHPass()
createWasmEHPass - This pass adapts exception handling code to use WebAssembly's exception handling s...
LLVM_ABI void DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, bool KeepOneInputPHIs=false)
Delete the specified block, which must have no predecessors.
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch,...
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)
OperandBundleDefT< Value * > OperandBundleDef
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
bool pred_empty(const BasicBlock *BB)
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.