126 using namespace llvm;
128 #define DEBUG_TYPE "wasmehprepare" 132 Type *LPadContextTy =
nullptr;
136 Value *LPadIndexField =
nullptr;
137 Value *LSDAField =
nullptr;
138 Value *SelectorField =
nullptr;
145 Function *CallPersonalityF =
nullptr;
149 void prepareTerminateCleanupPad(
BasicBlock *BB);
156 bool doInitialization(
Module &M)
override;
160 return "WebAssembly Exception handling preparation";
171 bool WasmEHPrepare::doInitialization(
Module &M) {
186 auto *Pad = BB.getFirstNonPHI();
187 if (isa<CatchPadInst>(Pad))
189 else if (isa<CleanupPadInst>(Pad))
195 assert(F.hasPersonalityFn() &&
"Personality function not found");
197 Module &M = *F.getParent();
201 LPadContextGV = cast<GlobalVariable>(
202 M.getOrInsertGlobal(
"__wasm_lpad_context", LPadContextTy));
206 IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 1,
"lsda_gep");
207 SelectorField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 2,
223 CallPersonalityF = cast<Function>(M.getOrInsertFunction(
224 "_Unwind_CallPersonality", IRB.getInt32Ty(), IRB.getInt8PtrTy()));
225 CallPersonalityF->setDoesNotThrow();
229 ClangCallTermF = M.getFunction(
"__clang_call_terminate");
232 for (
auto *BB : CatchPads) {
233 auto *CPI = cast<CatchPadInst>(BB->getFirstNonPHI());
235 if (CPI->getNumArgOperands() == 1 &&
236 cast<Constant>(CPI->getArgOperand(0))->isNullValue())
237 prepareEHPad(BB, -1);
239 prepareEHPad(BB, Index++);
243 return !CatchPads.empty();
251 for (
auto *BB : CleanupPads)
253 if (
auto *CI = dyn_cast<CallInst>(&
I))
254 if (CI->getCalledValue() == ClangCallTermF)
255 prepareEHPad(BB, -1);
268 Instruction *Exn = IRB.CreateCall(CatchF, IRB.getInt32(0),
"exn");
272 Instruction *GetExnCI =
nullptr, *GetSelectorCI =
nullptr;
273 for (
auto &U : FPI->uses()) {
274 if (
auto *CI = dyn_cast<CallInst>(U.getUser())) {
275 if (CI->getCalledValue() == GetExnF)
277 else if (CI->getCalledValue() == GetSelectorF)
282 assert(GetExnCI &&
"wasm.get.exception() call does not exist");
283 GetExnCI->replaceAllUsesWith(Exn);
284 GetExnCI->eraseFromParent();
288 if (FPI->getNumArgOperands() == 0 ||
289 (FPI->getNumArgOperands() == 1 &&
290 cast<Constant>(FPI->getArgOperand(0))->isNullValue())) {
292 assert(GetSelectorCI->use_empty() &&
293 "wasm.get.ehselector() still has uses!");
294 GetSelectorCI->eraseFromParent();
303 IRB.CreateCall(LPadIndexF, IRB.getInt32(Index));
306 IRB.CreateStore(IRB.getInt32(Index), LPadIndexField);
314 auto *CPI = cast<CatchPadInst>(FPI);
315 if (isa<ConstantTokenNone>(CPI->getCatchSwitch()->getParentPad()))
317 IRB.CreateStore(IRB.CreateCall(LSDAF), LSDAField);
325 Instruction *Selector = IRB.CreateLoad(SelectorField,
"selector");
329 assert(GetSelectorCI &&
"wasm.get.ehselector() call does not exist");
330 GetSelectorCI->replaceAllUsesWith(Selector);
331 GetSelectorCI->eraseFromParent();
335 for (
const auto &BB : *F) {
344 if (
const auto *CatchPad = dyn_cast<CatchPadInst>(Pad)) {
345 const auto *UnwindBB = CatchPad->getCatchSwitch()->getUnwindDest();
348 const Instruction *UnwindPad = UnwindBB->getFirstNonPHI();
349 if (
const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(UnwindPad))
358 for (
const auto &BB : *F) {
361 if (
const auto *Invoke = dyn_cast<InvokeInst>(TI))
362 UnwindBB = Invoke->getUnwindDest();
363 else if (
const auto *CleanupRet = dyn_cast<CleanupReturnInst>(TI))
364 UnwindBB = CleanupRet->getUnwindDest();
368 if (
const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(UnwindPad))
void push_back(const T &Elt)
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Compute iterated dominance frontiers using a linear time algorithm.
A Module instance is used to store all the information related to an LLVM module. ...
This class represents a function call, abstracting a target machine's calling convention.
Value * CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1, const Twine &Name="")
LLVMContext & getContext() const
Get the context in which this basic block lives.
iterator begin()
Instruction iterator methods.
LLVMContext & getContext() const
Get the global data context.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
static StructType * get(LLVMContext &Context, ArrayRef< Type *> Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
FunctionPass * createWasmEHPass()
createWasmEHPass - This pass adapts exception handling code to use WebAssembly's exception handling s...
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
static bool runOnFunction(Function &F, bool PostInlining)
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
LLVM Basic Block Representation.
void setEHPadUnwindDest(const BasicBlock *BB, const BasicBlock *Dest)
The instances of the Type class are immutable: once they are created, they are never changed...
FunctionPass class - This class is used to implement most global optimizations.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
OperandBundleDefT< Value * > OperandBundleDef
INITIALIZE_PASS(WasmEHPrepare, DEBUG_TYPE, "Prepare WebAssembly exceptions", false, false) FunctionPass *llvm
LLVM_NODISCARD bool empty() const
void calculateWasmEHInfo(const Function *F, WasmEHFuncInfo &EHInfo)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isEHPad() const
Return true if this basic block is an exception handling block.
LLVM Value Representation.
void setThrowUnwindDest(const BasicBlock *BB, const BasicBlock *Dest)
StringRef - Represent a constant reference to a string, i.e.
const TerminatorInst * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
This file describes how to lower LLVM code to machine code.