86#include "llvm/IR/IntrinsicsWebAssembly.h"
92#define DEBUG_TYPE "wasm-eh-prepare"
95class WasmEHPrepareImpl {
96 friend class WasmEHPrepare;
98 Type *LPadContextTy =
nullptr;
102 Value *LPadIndexField =
nullptr;
103 Value *LSDAField =
nullptr;
104 Value *SelectorField =
nullptr;
117 void prepareEHPad(
BasicBlock *BB,
bool NeedPersonality,
unsigned Index = 0);
120 WasmEHPrepareImpl() =
default;
121 WasmEHPrepareImpl(
Type *LPadContextTy_) : LPadContextTy(LPadContextTy_) {}
136 return "WebAssembly Exception handling preparation";
147 auto *LPadContextTy =
149 WasmEHPrepareImpl
P(LPadContextTy);
150 bool Changed =
P.runOnFunction(
F);
154char WasmEHPrepare::ID = 0;
156 "Prepare WebAssembly exceptions",
false,
false)
162bool WasmEHPrepare::doInitialization(
Module &M) {
173template <
typename Container>
176 while (!WL.
empty()) {
185bool WasmEHPrepareImpl::runOnFunction(
Function &
F) {
186 bool Changed =
false;
187 Changed |= prepareThrows(
F);
188 Changed |= prepareEHPads(
F);
192bool WasmEHPrepareImpl::prepareThrows(
Function &
F) {
195 bool Changed =
false;
205 auto *ThrowI = cast<CallInst>(U);
206 if (ThrowI->getFunction() != &
F)
209 auto *BB = ThrowI->getParent();
212 IRB.SetInsertPoint(BB);
213 IRB.CreateUnreachable();
220bool WasmEHPrepareImpl::prepareEHPads(
Function &
F) {
229 auto *Pad = BB.getFirstNonPHI();
230 if (isa<CatchPadInst>(Pad))
232 else if (isa<CleanupPadInst>(Pad))
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 = cast<GlobalVariable>(
252 M.getOrInsertGlobal(
"__wasm_lpad_context", LPadContextTy));
255 LPadIndexField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 0,
258 IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 1,
"lsda_gep");
259 SelectorField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 2,
277 CallPersonalityF =
M.getOrInsertFunction(
"_Unwind_CallPersonality",
278 IRB.getInt32Ty(), IRB.getPtrTy());
279 if (
Function *
F = dyn_cast<Function>(CallPersonalityF.getCallee()))
280 F->setDoesNotThrow();
283 for (
auto *BB : CatchPads) {
284 auto *CPI = cast<CatchPadInst>(BB->getFirstNonPHI());
287 if (CPI->arg_size() == 1 &&
288 cast<Constant>(CPI->getArgOperand(0))->isNullValue())
289 prepareEHPad(BB,
false);
291 prepareEHPad(BB,
true,
Index++);
295 for (
auto *BB : CleanupPads)
296 prepareEHPad(BB,
false);
303void WasmEHPrepareImpl::prepareEHPad(
BasicBlock *BB,
bool NeedPersonality,
310 Instruction *GetExnCI =
nullptr, *GetSelectorCI =
nullptr;
311 for (
auto &U : FPI->uses()) {
312 if (
auto *CI = dyn_cast<CallInst>(
U.getUser())) {
313 if (CI->getCalledOperand() == GetExnF)
315 if (CI->getCalledOperand() == GetSelectorF)
324 "wasm.get.ehselector() cannot exist w/o wasm.get.exception()");
339 if (!NeedPersonality) {
341 assert(GetSelectorCI->use_empty() &&
342 "wasm.get.ehselector() still has uses!");
343 GetSelectorCI->eraseFromParent();
352 IRB.CreateCall(LPadIndexF, {FPI, IRB.getInt32(
Index)});
355 IRB.CreateStore(IRB.getInt32(
Index), LPadIndexField);
357 auto *CPI = cast<CatchPadInst>(FPI);
362 IRB.CreateStore(IRB.CreateCall(LSDAF), LSDAField);
365 CallInst *PersCI = IRB.CreateCall(CallPersonalityF, CatchCI,
371 IRB.CreateLoad(IRB.getInt32Ty(), SelectorField,
"selector");
375 assert(GetSelectorCI &&
"wasm.get.ehselector() call does not exist");
376 GetSelectorCI->replaceAllUsesWith(Selector);
377 GetSelectorCI->eraseFromParent();
385 for (
const auto &BB : *
F) {
390 if (
const auto *CatchPad = dyn_cast<CatchPadInst>(Pad)) {
391 const auto *UnwindBB = CatchPad->getCatchSwitch()->getUnwindDest();
394 const Instruction *UnwindPad = UnwindBB->getFirstNonPHI();
395 if (
const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(UnwindPad))
static bool runOnFunction(Function &F, bool PostInlining)
#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())
static void eraseDeadBBsAndChildren(const Container &BBs)
A container for analyses that lazily runs them and caches their results.
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...
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.
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.
static 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 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 IntegerType * getInt32Ty(LLVMContext &C)
LLVM Value Representation.
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.
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.
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch,...
OperandBundleDefT< Value * > OperandBundleDef
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
void calculateWasmEHInfo(const Function *F, WasmEHFuncInfo &EHInfo)
bool pred_empty(const BasicBlock *BB)
void setUnwindDest(const BasicBlock *BB, const BasicBlock *Dest)