86#include "llvm/IR/IntrinsicsWebAssembly.h"
94#define DEBUG_TYPE "wasm-eh-prepare"
97class WasmEHPrepareImpl {
98 friend class WasmEHPrepare;
100 Type *LPadContextTy =
nullptr;
104 Value *LPadIndexField =
nullptr;
105 Value *LSDAField =
nullptr;
106 Value *SelectorField =
nullptr;
119 void prepareEHPad(
BasicBlock *BB,
bool NeedPersonality,
unsigned Index = 0);
122 WasmEHPrepareImpl() =
default;
123 WasmEHPrepareImpl(
Type *LPadContextTy_) : LPadContextTy(LPadContextTy_) {}
134 bool doInitialization(
Module &M)
override;
138 return "WebAssembly Exception handling preparation";
146 auto &Context =
F.getContext();
149 auto *LPadContextTy =
151 WasmEHPrepareImpl
P(LPadContextTy);
156char WasmEHPrepare::ID = 0;
158 "Prepare WebAssembly exceptions",
false,
false)
164bool WasmEHPrepare::doInitialization(
Module &M) {
175template <
typename Container>
178 while (!WL.
empty()) {
187bool WasmEHPrepareImpl::runOnFunction(
Function &
F) {
194bool WasmEHPrepareImpl::prepareThrows(
Function &
F) {
206 if (!ThrowI || ThrowI->getFunction() != &
F)
209 auto *BB = ThrowI->getParent();
212 IRB.SetInsertPoint(BB);
213 IRB.CreateUnreachable();
220bool WasmEHPrepareImpl::prepareEHPads(
Function &
F) {
238 if (!
F.hasPersonalityFn() ||
241 "' does not have a correct Wasm personality function "
242 "'__gxx_wasm_personality_v0'");
244 assert(
F.hasPersonalityFn() &&
"Personality function not found");
251 LPadContextGV =
M.getOrInsertGlobal(
"__wasm_lpad_context", LPadContextTy);
254 LPadIndexField = LPadContextGV;
255 LSDAField = IRB.CreateConstInBoundsGEP2_32(LPadContextTy, LPadContextGV, 0, 1,
257 SelectorField = IRB.CreateConstInBoundsGEP2_32(LPadContextTy, LPadContextGV,
258 0, 2,
"selector_gep");
280 RTLIB::impl__Unwind_CallPersonality);
283 CallPersonalityF =
M.getOrInsertFunction(UnwindCallPersonalityName,
284 IRB.getInt32Ty(), IRB.getPtrTy());
286 F->setDoesNotThrow();
289 for (
auto *BB : CatchPads) {
293 if (CPI->arg_size() == 1 &&
295 prepareEHPad(BB,
false);
297 prepareEHPad(BB,
true, Index++);
301 for (
auto *BB : CleanupPads)
302 prepareEHPad(BB,
false);
309void WasmEHPrepareImpl::prepareEHPad(
BasicBlock *BB,
bool NeedPersonality,
316 Instruction *GetExnCI =
nullptr, *GetSelectorCI =
nullptr;
317 for (
auto &U : FPI->uses()) {
319 if (CI->getCalledOperand() == GetExnF)
321 if (CI->getCalledOperand() == GetSelectorF)
330 "wasm.get.ehselector() cannot exist w/o wasm.get.exception()");
345 if (!NeedPersonality) {
347 assert(GetSelectorCI->use_empty() &&
348 "wasm.get.ehselector() still has uses!");
349 GetSelectorCI->eraseFromParent();
358 IRB.CreateCall(LPadIndexF, {FPI, IRB.getInt32(Index)});
361 IRB.CreateStore(IRB.getInt32(Index), LPadIndexField);
368 IRB.CreateStore(IRB.CreateCall(LSDAF), LSDAField);
371 CallInst *PersCI = IRB.CreateCall(CallPersonalityF, CatchCI,
377 IRB.CreateLoad(IRB.getInt32Ty(), SelectorField,
"selector");
381 assert(GetSelectorCI &&
"wasm.get.ehselector() call does not exist");
382 GetSelectorCI->replaceAllUsesWith(Selector);
383 GetSelectorCI->eraseFromParent();
391 for (
const auto &BB : *
F) {
397 const auto *UnwindBB = CatchPad->getCatchSwitch()->getUnwindDest();
400 const Instruction *UnwindPad = &*UnwindBB->getFirstNonPHIIt();
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.
iterator begin()
Instruction iterator methods.
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.
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(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type 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.
StringRef - 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()
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
This is an optimization pass for GlobalISel generic memory operations.
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
void calculateWasmEHInfo(const Function *F, WasmEHFuncInfo &EHInfo)
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.
static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl)
Get the libcall routine name for the specified libcall implementation.
void setUnwindDest(const BasicBlock *BB, const BasicBlock *Dest)