90 #include "llvm/IR/IntrinsicsWebAssembly.h" 97 #define DEBUG_TYPE "wasmehprepare" 101 Type *LPadContextTy =
nullptr;
105 Value *LPadIndexField =
nullptr;
106 Value *LSDAField =
nullptr;
107 Value *SelectorField =
nullptr;
122 void prepareTerminateCleanupPad(
BasicBlock *BB);
129 bool doInitialization(
Module &M)
override;
133 return "WebAssembly Exception handling preparation";
144 bool WasmEHPrepare::doInitialization(
Module &M) {
155 template <
typename Container>
158 while (!WL.empty()) {
168 bool Changed =
false;
169 Changed |= prepareThrows(F);
170 Changed |= prepareEHPads(F);
174 bool WasmEHPrepare::prepareThrows(
Function &F) {
177 bool Changed =
false;
187 auto *ThrowI = cast<CallInst>(U);
188 if (ThrowI->getFunction() != &
F)
191 auto *BB = ThrowI->getParent();
193 auto &InstList = BB->getInstList();
195 IRB.SetInsertPoint(BB);
196 IRB.CreateUnreachable();
203 bool WasmEHPrepare::prepareEHPads(
Function &F) {
212 auto *Pad = BB.getFirstNonPHI();
213 if (isa<CatchPadInst>(Pad))
214 CatchPads.push_back(&BB);
215 else if (isa<CleanupPadInst>(Pad))
219 if (CatchPads.empty() && CleanupPads.
empty())
221 assert(F.hasPersonalityFn() &&
"Personality function not found");
224 LPadContextGV = cast<GlobalVariable>(
226 LPadIndexField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 0,
229 IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 1,
"lsda_gep");
230 SelectorField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 2,
252 "_Unwind_CallPersonality", IRB.getInt32Ty(), IRB.getInt8PtrTy());
253 if (
Function *F = dyn_cast<Function>(CallPersonalityF.getCallee()))
254 F->setDoesNotThrow();
257 for (
auto *BB : CatchPads) {
258 auto *CPI = cast<CatchPadInst>(BB->getFirstNonPHI());
260 if (CPI->getNumArgOperands() == 1 &&
261 cast<Constant>(CPI->getArgOperand(0))->isNullValue())
262 prepareEHPad(BB,
false);
264 prepareEHPad(BB,
true, Index++);
268 for (
auto *BB : CleanupPads)
269 prepareEHPad(BB,
false);
276 void WasmEHPrepare::prepareEHPad(
BasicBlock *BB,
bool NeedLSDA,
283 Instruction *GetExnCI =
nullptr, *GetSelectorCI =
nullptr;
284 for (
auto &U : FPI->uses()) {
285 if (
auto *CI = dyn_cast<CallInst>(U.getUser())) {
286 if (CI->getCalledValue() == GetExnF)
288 if (CI->getCalledValue() == GetSelectorF)
297 "wasm.get.ehselector() cannot exist w/o wasm.get.exception()");
301 Instruction *ExtractExnCI = IRB.CreateCall(ExtractExnF, {},
"exn");
303 GetExnCI->eraseFromParent();
309 assert(GetSelectorCI->use_empty() &&
310 "wasm.get.ehselector() still has uses!");
311 GetSelectorCI->eraseFromParent();
320 IRB.CreateCall(LPadIndexF, {FPI, IRB.getInt32(Index)});
323 IRB.CreateStore(IRB.getInt32(Index), LPadIndexField);
331 auto *CPI = cast<CatchPadInst>(FPI);
332 if (isa<ConstantTokenNone>(CPI->getCatchSwitch()->getParentPad()))
334 IRB.CreateStore(IRB.CreateCall(LSDAF), LSDAField);
337 CallInst *PersCI = IRB.CreateCall(CallPersonalityF, ExtractExnCI,
343 IRB.CreateLoad(IRB.getInt32Ty(), SelectorField,
"selector");
347 assert(GetSelectorCI &&
"wasm.get.ehselector() call does not exist");
348 GetSelectorCI->replaceAllUsesWith(Selector);
349 GetSelectorCI->eraseFromParent();
357 for (
const auto &BB : *F) {
362 if (
const auto *CatchPad = dyn_cast<CatchPadInst>(Pad)) {
363 const auto *UnwindBB = CatchPad->getCatchSwitch()->getUnwindDest();
366 const Instruction *UnwindPad = UnwindBB->getFirstNonPHI();
367 if (
const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(UnwindPad))
void DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, bool KeepOneInputPHIs=false)
Delete the specified block, which must have no predecessors.
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
This class represents lattice values for constants.
A Module instance is used to store all the information related to an LLVM module. ...
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
void push_back(const T &Elt)
This class represents a function call, abstracting a target machine's calling convention.
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.
static StructType * get(LLVMContext &Context, ArrayRef< Type *> Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
static void eraseDeadBBsAndChildren(const Container &BBs)
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
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...
Interval::succ_iterator succ_end(Interval *I)
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...
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
FunctionPass class - This class is used to implement most global optimizations.
Interval::pred_iterator pred_end(Interval *I)
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Iterator for intrusive lists based on ilist_node.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
LLVM_NODISCARD T pop_back_val()
OperandBundleDefT< Value * > OperandBundleDef
FunctionCallee getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
INITIALIZE_PASS(WasmEHPrepare, DEBUG_TYPE, "Prepare WebAssembly exceptions", false, false) FunctionPass *llvm
iterator_range< user_iterator > users()
Constant * getOrInsertGlobal(StringRef Name, Type *Ty, function_ref< GlobalVariable *()> CreateGlobalCallback)
Look up the specified global in the module symbol table.
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.
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
StringRef - Represent a constant reference to a string, i.e.
This file describes how to lower LLVM code to machine code.