84#include "llvm/IR/IntrinsicsWebAssembly.h"
90#define DEBUG_TYPE "wasmehprepare"
94 Type *LPadContextTy =
nullptr;
98 Value *LPadIndexField =
nullptr;
99 Value *LSDAField =
nullptr;
100 Value *SelectorField =
nullptr;
113 void prepareEHPad(
BasicBlock *BB,
bool NeedPersonality,
unsigned Index = 0);
123 return "WebAssembly Exception handling preparation";
128char WasmEHPrepare::ID = 0;
130 "Prepare WebAssembly exceptions",
false,
false)
136bool WasmEHPrepare::doInitialization(
Module &M) {
147template <
typename Container>
150 while (!WL.
empty()) {
159bool WasmEHPrepare::runOnFunction(
Function &
F) {
160 bool Changed =
false;
161 Changed |= prepareThrows(
F);
162 Changed |= prepareEHPads(
F);
166bool WasmEHPrepare::prepareThrows(
Function &
F) {
169 bool Changed =
false;
179 auto *ThrowI = cast<CallInst>(U);
180 if (ThrowI->getFunction() != &
F)
183 auto *BB = ThrowI->getParent();
186 IRB.SetInsertPoint(BB);
187 IRB.CreateUnreachable();
194bool WasmEHPrepare::prepareEHPads(
Function &
F) {
203 auto *Pad = BB.getFirstNonPHI();
204 if (isa<CatchPadInst>(Pad))
206 else if (isa<CleanupPadInst>(Pad))
212 assert(
F.hasPersonalityFn() &&
"Personality function not found");
219 LPadContextGV = cast<GlobalVariable>(
220 M.getOrInsertGlobal(
"__wasm_lpad_context", LPadContextTy));
223 LPadIndexField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 0,
226 IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 1,
"lsda_gep");
227 SelectorField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 2,
245 CallPersonalityF =
M.getOrInsertFunction(
246 "_Unwind_CallPersonality", IRB.getInt32Ty(), IRB.getInt8PtrTy());
247 if (
Function *
F = dyn_cast<Function>(CallPersonalityF.getCallee()))
248 F->setDoesNotThrow();
251 for (
auto *BB : CatchPads) {
252 auto *CPI = cast<CatchPadInst>(BB->getFirstNonPHI());
255 if (CPI->arg_size() == 1 &&
256 cast<Constant>(CPI->getArgOperand(0))->isNullValue())
257 prepareEHPad(BB,
false);
259 prepareEHPad(BB,
true,
Index++);
263 for (
auto *BB : CleanupPads)
264 prepareEHPad(BB,
false);
271void WasmEHPrepare::prepareEHPad(
BasicBlock *BB,
bool NeedPersonality,
278 Instruction *GetExnCI =
nullptr, *GetSelectorCI =
nullptr;
279 for (
auto &U : FPI->uses()) {
280 if (
auto *CI = dyn_cast<CallInst>(
U.getUser())) {
281 if (CI->getCalledOperand() == GetExnF)
283 if (CI->getCalledOperand() == GetSelectorF)
292 "wasm.get.ehselector() cannot exist w/o wasm.get.exception()");
307 if (!NeedPersonality) {
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);
325 auto *CPI = cast<CatchPadInst>(FPI);
330 IRB.CreateStore(IRB.CreateCall(LSDAF), LSDAField);
333 CallInst *PersCI = IRB.CreateCall(CallPersonalityF, CatchCI,
339 IRB.CreateLoad(IRB.getInt32Ty(), SelectorField,
"selector");
343 assert(GetSelectorCI &&
"wasm.get.ehselector() call does not exist");
344 GetSelectorCI->replaceAllUsesWith(Selector);
345 GetSelectorCI->eraseFromParent();
353 for (
const auto &BB : *
F) {
358 if (
const auto *CatchPad = dyn_cast<CatchPadInst>(Pad)) {
359 const auto *UnwindBB = CatchPad->getCatchSwitch()->getUnwindDest();
362 const Instruction *UnwindPad = UnwindBB->getFirstNonPHI();
363 if (
const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(UnwindPad))
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Prepare WebAssembly exceptions
static void eraseDeadBBsAndChildren(const Container &BBs)
LLVM Basic Block Representation.
iterator begin()
Instruction iterator methods.
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
InstListType::iterator iterator
Instruction iterators...
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.
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
SymbolTableList< Instruction >::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.
virtual bool doInitialization(Module &)
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
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 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.
LLVM Value Representation.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
iterator_range< user_iterator > users()
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.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
This is an optimization pass for GlobalISel generic memory operations.
Interval::succ_iterator succ_end(Interval *I)
auto successors(const MachineBasicBlock *BB)
FunctionPass * createWasmEHPass()
createWasmEHPass - This pass adapts exception handling code to use WebAssembly's exception handling s...
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...
void DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, bool KeepOneInputPHIs=false)
Delete the specified block, which must have no predecessors.
OperandBundleDefT< Value * > OperandBundleDef
void calculateWasmEHInfo(const Function *F, WasmEHFuncInfo &EHInfo)
bool pred_empty(const BasicBlock *BB)
void setUnwindDest(const BasicBlock *BB, const BasicBlock *Dest)