1311116Sdim//=== WebAssemblyLowerEmscriptenEHSjLj.cpp - Lower exceptions for Emscripten =// 2311116Sdim// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6311116Sdim// 7311116Sdim//===----------------------------------------------------------------------===// 8311116Sdim/// 9311116Sdim/// \file 10341825Sdim/// This file lowers exception-related instructions and setjmp/longjmp 11311116Sdim/// function calls in order to use Emscripten's JavaScript try and catch 12311116Sdim/// mechanism. 13311116Sdim/// 14311116Sdim/// To handle exceptions and setjmp/longjmps, this scheme relies on JavaScript's 15311116Sdim/// try and catch syntax and relevant exception-related libraries implemented 16311116Sdim/// in JavaScript glue code that will be produced by Emscripten. This is similar 17311116Sdim/// to the current Emscripten asm.js exception handling in fastcomp. For 18311116Sdim/// fastcomp's EH / SjLj scheme, see these files in fastcomp LLVM branch: 19311116Sdim/// (Location: https://github.com/kripken/emscripten-fastcomp) 20311116Sdim/// lib/Target/JSBackend/NaCl/LowerEmExceptionsPass.cpp 21311116Sdim/// lib/Target/JSBackend/NaCl/LowerEmSetjmp.cpp 22311116Sdim/// lib/Target/JSBackend/JSBackend.cpp 23311116Sdim/// lib/Target/JSBackend/CallHandlers.h 24311116Sdim/// 25311116Sdim/// * Exception handling 26311116Sdim/// This pass lowers invokes and landingpads into library functions in JS glue 27311116Sdim/// code. Invokes are lowered into function wrappers called invoke wrappers that 28311116Sdim/// exist in JS side, which wraps the original function call with JS try-catch. 29311116Sdim/// If an exception occurred, cxa_throw() function in JS side sets some 30311116Sdim/// variables (see below) so we can check whether an exception occurred from 31311116Sdim/// wasm code and handle it appropriately. 32311116Sdim/// 33311116Sdim/// * Setjmp-longjmp handling 34311116Sdim/// This pass lowers setjmp to a reasonably-performant approach for emscripten. 35311116Sdim/// The idea is that each block with a setjmp is broken up into two parts: the 36311116Sdim/// part containing setjmp and the part right after the setjmp. The latter part 37311116Sdim/// is either reached from the setjmp, or later from a longjmp. To handle the 38311116Sdim/// longjmp, all calls that might longjmp are also called using invoke wrappers 39311116Sdim/// and thus JS / try-catch. JS longjmp() function also sets some variables so 40311116Sdim/// we can check / whether a longjmp occurred from wasm code. Each block with a 41311116Sdim/// function call that might longjmp is also split up after the longjmp call. 42311116Sdim/// After the longjmp call, we check whether a longjmp occurred, and if it did, 43311116Sdim/// which setjmp it corresponds to, and jump to the right post-setjmp block. 44311116Sdim/// We assume setjmp-longjmp handling always run after EH handling, which means 45311116Sdim/// we don't expect any exception-related instructions when SjLj runs. 46311116Sdim/// FIXME Currently this scheme does not support indirect call of setjmp, 47311116Sdim/// because of the limitation of the scheme itself. fastcomp does not support it 48311116Sdim/// either. 49311116Sdim/// 50311116Sdim/// In detail, this pass does following things: 51311116Sdim/// 52344779Sdim/// 1) Assumes the existence of global variables: __THREW__, __threwValue 53344779Sdim/// __THREW__ and __threwValue will be set in invoke wrappers 54311116Sdim/// in JS glue code. For what invoke wrappers are, refer to 3). These 55311116Sdim/// variables are used for both exceptions and setjmp/longjmps. 56311116Sdim/// __THREW__ indicates whether an exception or a longjmp occurred or not. 0 57311116Sdim/// means nothing occurred, 1 means an exception occurred, and other numbers 58311116Sdim/// mean a longjmp occurred. In the case of longjmp, __threwValue variable 59311116Sdim/// indicates the corresponding setjmp buffer the longjmp corresponds to. 60311116Sdim/// 61311116Sdim/// * Exception handling 62311116Sdim/// 63344779Sdim/// 2) We assume the existence of setThrew and setTempRet0/getTempRet0 functions 64344779Sdim/// at link time. 65344779Sdim/// The global variables in 1) will exist in wasm address space, 66344779Sdim/// but their values should be set in JS code, so these functions 67311116Sdim/// as interfaces to JS glue code. These functions are equivalent to the 68311116Sdim/// following JS functions, which actually exist in asm.js version of JS 69311116Sdim/// library. 70311116Sdim/// 71311116Sdim/// function setThrew(threw, value) { 72311116Sdim/// if (__THREW__ == 0) { 73311116Sdim/// __THREW__ = threw; 74311116Sdim/// __threwValue = value; 75311116Sdim/// } 76311116Sdim/// } 77344779Sdim// 78344779Sdim/// setTempRet0 is called from __cxa_find_matching_catch() in JS glue code. 79311116Sdim/// 80344779Sdim/// In exception handling, getTempRet0 indicates the type of an exception 81344779Sdim/// caught, and in setjmp/longjmp, it means the second argument to longjmp 82344779Sdim/// function. 83311116Sdim/// 84311116Sdim/// 3) Lower 85311116Sdim/// invoke @func(arg1, arg2) to label %invoke.cont unwind label %lpad 86311116Sdim/// into 87311116Sdim/// __THREW__ = 0; 88311116Sdim/// call @__invoke_SIG(func, arg1, arg2) 89311116Sdim/// %__THREW__.val = __THREW__; 90311116Sdim/// __THREW__ = 0; 91311116Sdim/// if (%__THREW__.val == 1) 92311116Sdim/// goto %lpad 93311116Sdim/// else 94311116Sdim/// goto %invoke.cont 95311116Sdim/// SIG is a mangled string generated based on the LLVM IR-level function 96311116Sdim/// signature. After LLVM IR types are lowered to the target wasm types, 97311116Sdim/// the names for these wrappers will change based on wasm types as well, 98311116Sdim/// as in invoke_vi (function takes an int and returns void). The bodies of 99311116Sdim/// these wrappers will be generated in JS glue code, and inside those 100311116Sdim/// wrappers we use JS try-catch to generate actual exception effects. It 101311116Sdim/// also calls the original callee function. An example wrapper in JS code 102311116Sdim/// would look like this: 103311116Sdim/// function invoke_vi(index,a1) { 104311116Sdim/// try { 105311116Sdim/// Module["dynCall_vi"](index,a1); // This calls original callee 106311116Sdim/// } catch(e) { 107311116Sdim/// if (typeof e !== 'number' && e !== 'longjmp') throw e; 108311116Sdim/// asm["setThrew"](1, 0); // setThrew is called here 109311116Sdim/// } 110311116Sdim/// } 111311116Sdim/// If an exception is thrown, __THREW__ will be set to true in a wrapper, 112311116Sdim/// so we can jump to the right BB based on this value. 113311116Sdim/// 114311116Sdim/// 4) Lower 115311116Sdim/// %val = landingpad catch c1 catch c2 catch c3 ... 116311116Sdim/// ... use %val ... 117311116Sdim/// into 118311116Sdim/// %fmc = call @__cxa_find_matching_catch_N(c1, c2, c3, ...) 119344779Sdim/// %val = {%fmc, getTempRet0()} 120311116Sdim/// ... use %val ... 121311116Sdim/// Here N is a number calculated based on the number of clauses. 122344779Sdim/// setTempRet0 is called from __cxa_find_matching_catch() in JS glue code. 123311116Sdim/// 124311116Sdim/// 5) Lower 125311116Sdim/// resume {%a, %b} 126311116Sdim/// into 127311116Sdim/// call @__resumeException(%a) 128311116Sdim/// where __resumeException() is a function in JS glue code. 129311116Sdim/// 130311116Sdim/// 6) Lower 131311116Sdim/// call @llvm.eh.typeid.for(type) (intrinsic) 132311116Sdim/// into 133311116Sdim/// call @llvm_eh_typeid_for(type) 134311116Sdim/// llvm_eh_typeid_for function will be generated in JS glue code. 135311116Sdim/// 136311116Sdim/// * Setjmp / Longjmp handling 137311116Sdim/// 138344779Sdim/// In case calls to longjmp() exists 139344779Sdim/// 140344779Sdim/// 1) Lower 141344779Sdim/// longjmp(buf, value) 142344779Sdim/// into 143344779Sdim/// emscripten_longjmp_jmpbuf(buf, value) 144344779Sdim/// emscripten_longjmp_jmpbuf will be lowered to emscripten_longjmp later. 145344779Sdim/// 146344779Sdim/// In case calls to setjmp() exists 147344779Sdim/// 148344779Sdim/// 2) In the function entry that calls setjmp, initialize setjmpTable and 149311116Sdim/// sejmpTableSize as follows: 150311116Sdim/// setjmpTableSize = 4; 151311116Sdim/// setjmpTable = (int *) malloc(40); 152311116Sdim/// setjmpTable[0] = 0; 153311116Sdim/// setjmpTable and setjmpTableSize are used in saveSetjmp() function in JS 154311116Sdim/// code. 155311116Sdim/// 156344779Sdim/// 3) Lower 157311116Sdim/// setjmp(buf) 158311116Sdim/// into 159311116Sdim/// setjmpTable = saveSetjmp(buf, label, setjmpTable, setjmpTableSize); 160344779Sdim/// setjmpTableSize = getTempRet0(); 161311116Sdim/// For each dynamic setjmp call, setjmpTable stores its ID (a number which 162311116Sdim/// is incrementally assigned from 0) and its label (a unique number that 163311116Sdim/// represents each callsite of setjmp). When we need more entries in 164311116Sdim/// setjmpTable, it is reallocated in saveSetjmp() in JS code and it will 165311116Sdim/// return the new table address, and assign the new table size in 166344779Sdim/// setTempRet0(). saveSetjmp also stores the setjmp's ID into the buffer 167344779Sdim/// buf. A BB with setjmp is split into two after setjmp call in order to 168344779Sdim/// make the post-setjmp BB the possible destination of longjmp BB. 169311116Sdim/// 170311116Sdim/// 171344779Sdim/// 4) Lower every call that might longjmp into 172311116Sdim/// __THREW__ = 0; 173311116Sdim/// call @__invoke_SIG(func, arg1, arg2) 174311116Sdim/// %__THREW__.val = __THREW__; 175311116Sdim/// __THREW__ = 0; 176311116Sdim/// if (%__THREW__.val != 0 & __threwValue != 0) { 177311116Sdim/// %label = testSetjmp(mem[%__THREW__.val], setjmpTable, 178311116Sdim/// setjmpTableSize); 179311116Sdim/// if (%label == 0) 180311116Sdim/// emscripten_longjmp(%__THREW__.val, __threwValue); 181344779Sdim/// setTempRet0(__threwValue); 182311116Sdim/// } else { 183311116Sdim/// %label = -1; 184311116Sdim/// } 185344779Sdim/// longjmp_result = getTempRet0(); 186311116Sdim/// switch label { 187311116Sdim/// label 1: goto post-setjmp BB 1 188311116Sdim/// label 2: goto post-setjmp BB 2 189311116Sdim/// ... 190311116Sdim/// default: goto splitted next BB 191311116Sdim/// } 192344779Sdim/// testSetjmp examines setjmpTable to see if there is a matching setjmp 193344779Sdim/// call. After calling an invoke wrapper, if a longjmp occurred, __THREW__ 194344779Sdim/// will be the address of matching jmp_buf buffer and __threwValue be the 195344779Sdim/// second argument to longjmp. mem[__THREW__.val] is a setjmp ID that is 196344779Sdim/// stored in saveSetjmp. testSetjmp returns a setjmp label, a unique ID to 197344779Sdim/// each setjmp callsite. Label 0 means this longjmp buffer does not 198344779Sdim/// correspond to one of the setjmp callsites in this function, so in this 199344779Sdim/// case we just chain the longjmp to the caller. (Here we call 200344779Sdim/// emscripten_longjmp, which is different from emscripten_longjmp_jmpbuf. 201344779Sdim/// emscripten_longjmp_jmpbuf takes jmp_buf as its first argument, while 202344779Sdim/// emscripten_longjmp takes an int. Both of them will eventually be lowered 203344779Sdim/// to emscripten_longjmp in s2wasm, but here we need two signatures - we 204344779Sdim/// can't translate an int value to a jmp_buf.) 205344779Sdim/// Label -1 means no longjmp occurred. Otherwise we jump to the right 206344779Sdim/// post-setjmp BB based on the label. 207311116Sdim/// 208311116Sdim///===----------------------------------------------------------------------===// 209311116Sdim 210311116Sdim#include "WebAssembly.h" 211311116Sdim#include "llvm/IR/CallSite.h" 212311116Sdim#include "llvm/IR/Dominators.h" 213311116Sdim#include "llvm/IR/IRBuilder.h" 214360784Sdim#include "llvm/Support/CommandLine.h" 215311116Sdim#include "llvm/Transforms/Utils/BasicBlockUtils.h" 216311116Sdim#include "llvm/Transforms/Utils/SSAUpdater.h" 217311116Sdim 218311116Sdimusing namespace llvm; 219311116Sdim 220311116Sdim#define DEBUG_TYPE "wasm-lower-em-ehsjlj" 221311116Sdim 222311116Sdimstatic cl::list<std::string> 223311116Sdim EHWhitelist("emscripten-cxx-exceptions-whitelist", 224311116Sdim cl::desc("The list of function names in which Emscripten-style " 225311116Sdim "exception handling is enabled (see emscripten " 226311116Sdim "EMSCRIPTEN_CATCHING_WHITELIST options)"), 227311116Sdim cl::CommaSeparated); 228311116Sdim 229311116Sdimnamespace { 230311116Sdimclass WebAssemblyLowerEmscriptenEHSjLj final : public ModulePass { 231311116Sdim bool EnableEH; // Enable exception handling 232311116Sdim bool EnableSjLj; // Enable setjmp/longjmp handling 233311116Sdim 234353358Sdim GlobalVariable *ThrewGV = nullptr; 235353358Sdim GlobalVariable *ThrewValueGV = nullptr; 236353358Sdim Function *GetTempRet0Func = nullptr; 237353358Sdim Function *SetTempRet0Func = nullptr; 238353358Sdim Function *ResumeF = nullptr; 239353358Sdim Function *EHTypeIDF = nullptr; 240353358Sdim Function *EmLongjmpF = nullptr; 241353358Sdim Function *EmLongjmpJmpbufF = nullptr; 242353358Sdim Function *SaveSetjmpF = nullptr; 243353358Sdim Function *TestSetjmpF = nullptr; 244311116Sdim 245311116Sdim // __cxa_find_matching_catch_N functions. 246311116Sdim // Indexed by the number of clauses in an original landingpad instruction. 247311116Sdim DenseMap<int, Function *> FindMatchingCatches; 248311116Sdim // Map of <function signature string, invoke_ wrappers> 249311116Sdim StringMap<Function *> InvokeWrappers; 250311116Sdim // Set of whitelisted function names for exception handling 251311116Sdim std::set<std::string> EHWhitelistSet; 252311116Sdim 253311116Sdim StringRef getPassName() const override { 254311116Sdim return "WebAssembly Lower Emscripten Exceptions"; 255311116Sdim } 256311116Sdim 257311116Sdim bool runEHOnFunction(Function &F); 258311116Sdim bool runSjLjOnFunction(Function &F); 259311116Sdim Function *getFindMatchingCatch(Module &M, unsigned NumClauses); 260311116Sdim 261311116Sdim template <typename CallOrInvoke> Value *wrapInvoke(CallOrInvoke *CI); 262311116Sdim void wrapTestSetjmp(BasicBlock *BB, Instruction *InsertPt, Value *Threw, 263311116Sdim Value *SetjmpTable, Value *SetjmpTableSize, Value *&Label, 264311116Sdim Value *&LongjmpResult, BasicBlock *&EndBB); 265311116Sdim template <typename CallOrInvoke> Function *getInvokeWrapper(CallOrInvoke *CI); 266311116Sdim 267311116Sdim bool areAllExceptionsAllowed() const { return EHWhitelistSet.empty(); } 268311116Sdim bool canLongjmp(Module &M, const Value *Callee) const; 269360784Sdim bool isEmAsmCall(Module &M, const Value *Callee) const; 270311116Sdim 271311116Sdim void rebuildSSA(Function &F); 272311116Sdim 273311116Sdimpublic: 274311116Sdim static char ID; 275311116Sdim 276311116Sdim WebAssemblyLowerEmscriptenEHSjLj(bool EnableEH = true, bool EnableSjLj = true) 277353358Sdim : ModulePass(ID), EnableEH(EnableEH), EnableSjLj(EnableSjLj) { 278311116Sdim EHWhitelistSet.insert(EHWhitelist.begin(), EHWhitelist.end()); 279311116Sdim } 280311116Sdim bool runOnModule(Module &M) override; 281311116Sdim 282311116Sdim void getAnalysisUsage(AnalysisUsage &AU) const override { 283311116Sdim AU.addRequired<DominatorTreeWrapperPass>(); 284311116Sdim } 285311116Sdim}; 286311116Sdim} // End anonymous namespace 287311116Sdim 288311116Sdimchar WebAssemblyLowerEmscriptenEHSjLj::ID = 0; 289311116SdimINITIALIZE_PASS(WebAssemblyLowerEmscriptenEHSjLj, DEBUG_TYPE, 290311116Sdim "WebAssembly Lower Emscripten Exceptions / Setjmp / Longjmp", 291311116Sdim false, false) 292311116Sdim 293311116SdimModulePass *llvm::createWebAssemblyLowerEmscriptenEHSjLj(bool EnableEH, 294311116Sdim bool EnableSjLj) { 295311116Sdim return new WebAssemblyLowerEmscriptenEHSjLj(EnableEH, EnableSjLj); 296311116Sdim} 297311116Sdim 298311116Sdimstatic bool canThrow(const Value *V) { 299311116Sdim if (const auto *F = dyn_cast<const Function>(V)) { 300311116Sdim // Intrinsics cannot throw 301311116Sdim if (F->isIntrinsic()) 302311116Sdim return false; 303311116Sdim StringRef Name = F->getName(); 304311116Sdim // leave setjmp and longjmp (mostly) alone, we process them properly later 305311116Sdim if (Name == "setjmp" || Name == "longjmp") 306311116Sdim return false; 307311116Sdim return !F->doesNotThrow(); 308311116Sdim } 309311116Sdim // not a function, so an indirect call - can throw, we can't tell 310311116Sdim return true; 311311116Sdim} 312311116Sdim 313344779Sdim// Get a global variable with the given name. If it doesn't exist declare it, 314344779Sdim// which will generate an import and asssumes that it will exist at link time. 315344779Sdimstatic GlobalVariable *getGlobalVariableI32(Module &M, IRBuilder<> &IRB, 316344779Sdim const char *Name) { 317341825Sdim 318360784Sdim auto *GV = 319360784Sdim dyn_cast<GlobalVariable>(M.getOrInsertGlobal(Name, IRB.getInt32Ty())); 320353358Sdim if (!GV) 321353358Sdim report_fatal_error(Twine("unable to create global: ") + Name); 322353358Sdim 323353358Sdim return GV; 324311116Sdim} 325311116Sdim 326311116Sdim// Simple function name mangler. 327311116Sdim// This function simply takes LLVM's string representation of parameter types 328311116Sdim// and concatenate them with '_'. There are non-alphanumeric characters but llc 329311116Sdim// is ok with it, and we need to postprocess these names after the lowering 330311116Sdim// phase anyway. 331311116Sdimstatic std::string getSignature(FunctionType *FTy) { 332311116Sdim std::string Sig; 333311116Sdim raw_string_ostream OS(Sig); 334311116Sdim OS << *FTy->getReturnType(); 335311116Sdim for (Type *ParamTy : FTy->params()) 336311116Sdim OS << "_" << *ParamTy; 337311116Sdim if (FTy->isVarArg()) 338311116Sdim OS << "_..."; 339311116Sdim Sig = OS.str(); 340311116Sdim Sig.erase(remove_if(Sig, isspace), Sig.end()); 341311116Sdim // When s2wasm parses .s file, a comma means the end of an argument. So a 342311116Sdim // mangled function name can contain any character but a comma. 343311116Sdim std::replace(Sig.begin(), Sig.end(), ',', '.'); 344311116Sdim return Sig; 345311116Sdim} 346311116Sdim 347311116Sdim// Returns __cxa_find_matching_catch_N function, where N = NumClauses + 2. 348311116Sdim// This is because a landingpad instruction contains two more arguments, a 349311116Sdim// personality function and a cleanup bit, and __cxa_find_matching_catch_N 350311116Sdim// functions are named after the number of arguments in the original landingpad 351311116Sdim// instruction. 352311116SdimFunction * 353311116SdimWebAssemblyLowerEmscriptenEHSjLj::getFindMatchingCatch(Module &M, 354311116Sdim unsigned NumClauses) { 355311116Sdim if (FindMatchingCatches.count(NumClauses)) 356311116Sdim return FindMatchingCatches[NumClauses]; 357311116Sdim PointerType *Int8PtrTy = Type::getInt8PtrTy(M.getContext()); 358311116Sdim SmallVector<Type *, 16> Args(NumClauses, Int8PtrTy); 359311116Sdim FunctionType *FTy = FunctionType::get(Int8PtrTy, Args, false); 360360784Sdim Function *F = Function::Create( 361360784Sdim FTy, GlobalValue::ExternalLinkage, 362360784Sdim "__cxa_find_matching_catch_" + Twine(NumClauses + 2), &M); 363311116Sdim FindMatchingCatches[NumClauses] = F; 364311116Sdim return F; 365311116Sdim} 366311116Sdim 367311116Sdim// Generate invoke wrapper seqence with preamble and postamble 368311116Sdim// Preamble: 369311116Sdim// __THREW__ = 0; 370311116Sdim// Postamble: 371311116Sdim// %__THREW__.val = __THREW__; __THREW__ = 0; 372311116Sdim// Returns %__THREW__.val, which indicates whether an exception is thrown (or 373311116Sdim// whether longjmp occurred), for future use. 374311116Sdimtemplate <typename CallOrInvoke> 375311116SdimValue *WebAssemblyLowerEmscriptenEHSjLj::wrapInvoke(CallOrInvoke *CI) { 376311116Sdim LLVMContext &C = CI->getModule()->getContext(); 377311116Sdim 378311116Sdim // If we are calling a function that is noreturn, we must remove that 379311116Sdim // attribute. The code we insert here does expect it to return, after we 380311116Sdim // catch the exception. 381311116Sdim if (CI->doesNotReturn()) { 382311116Sdim if (auto *F = dyn_cast<Function>(CI->getCalledValue())) 383311116Sdim F->removeFnAttr(Attribute::NoReturn); 384321369Sdim CI->removeAttribute(AttributeList::FunctionIndex, Attribute::NoReturn); 385311116Sdim } 386311116Sdim 387311116Sdim IRBuilder<> IRB(C); 388311116Sdim IRB.SetInsertPoint(CI); 389311116Sdim 390311116Sdim // Pre-invoke 391311116Sdim // __THREW__ = 0; 392311116Sdim IRB.CreateStore(IRB.getInt32(0), ThrewGV); 393311116Sdim 394311116Sdim // Invoke function wrapper in JavaScript 395311116Sdim SmallVector<Value *, 16> Args; 396311116Sdim // Put the pointer to the callee as first argument, so it can be called 397311116Sdim // within the invoke wrapper later 398311116Sdim Args.push_back(CI->getCalledValue()); 399311116Sdim Args.append(CI->arg_begin(), CI->arg_end()); 400311116Sdim CallInst *NewCall = IRB.CreateCall(getInvokeWrapper(CI), Args); 401311116Sdim NewCall->takeName(CI); 402360784Sdim NewCall->setCallingConv(CallingConv::WASM_EmscriptenInvoke); 403311116Sdim NewCall->setDebugLoc(CI->getDebugLoc()); 404311116Sdim 405311116Sdim // Because we added the pointer to the callee as first argument, all 406311116Sdim // argument attribute indices have to be incremented by one. 407321369Sdim SmallVector<AttributeSet, 8> ArgAttributes; 408321369Sdim const AttributeList &InvokeAL = CI->getAttributes(); 409321369Sdim 410321369Sdim // No attributes for the callee pointer. 411321369Sdim ArgAttributes.push_back(AttributeSet()); 412321369Sdim // Copy the argument attributes from the original 413353358Sdim for (unsigned I = 0, E = CI->getNumArgOperands(); I < E; ++I) 414353358Sdim ArgAttributes.push_back(InvokeAL.getParamAttributes(I)); 415321369Sdim 416360784Sdim AttrBuilder FnAttrs(InvokeAL.getFnAttributes()); 417360784Sdim if (FnAttrs.contains(Attribute::AllocSize)) { 418360784Sdim // The allocsize attribute (if any) referes to parameters by index and needs 419360784Sdim // to be adjusted. 420360784Sdim unsigned SizeArg; 421360784Sdim Optional<unsigned> NEltArg; 422360784Sdim std::tie(SizeArg, NEltArg) = FnAttrs.getAllocSizeArgs(); 423360784Sdim SizeArg += 1; 424360784Sdim if (NEltArg.hasValue()) 425360784Sdim NEltArg = NEltArg.getValue() + 1; 426360784Sdim FnAttrs.addAllocSizeAttr(SizeArg, NEltArg); 427360784Sdim } 428360784Sdim 429311116Sdim // Reconstruct the AttributesList based on the vector we constructed. 430321369Sdim AttributeList NewCallAL = 431360784Sdim AttributeList::get(C, AttributeSet::get(C, FnAttrs), 432321369Sdim InvokeAL.getRetAttributes(), ArgAttributes); 433321369Sdim NewCall->setAttributes(NewCallAL); 434311116Sdim 435311116Sdim CI->replaceAllUsesWith(NewCall); 436311116Sdim 437311116Sdim // Post-invoke 438311116Sdim // %__THREW__.val = __THREW__; __THREW__ = 0; 439353358Sdim Value *Threw = 440353358Sdim IRB.CreateLoad(IRB.getInt32Ty(), ThrewGV, ThrewGV->getName() + ".val"); 441311116Sdim IRB.CreateStore(IRB.getInt32(0), ThrewGV); 442311116Sdim return Threw; 443311116Sdim} 444311116Sdim 445311116Sdim// Get matching invoke wrapper based on callee signature 446311116Sdimtemplate <typename CallOrInvoke> 447311116SdimFunction *WebAssemblyLowerEmscriptenEHSjLj::getInvokeWrapper(CallOrInvoke *CI) { 448311116Sdim Module *M = CI->getModule(); 449311116Sdim SmallVector<Type *, 16> ArgTys; 450311116Sdim Value *Callee = CI->getCalledValue(); 451311116Sdim FunctionType *CalleeFTy; 452311116Sdim if (auto *F = dyn_cast<Function>(Callee)) 453311116Sdim CalleeFTy = F->getFunctionType(); 454311116Sdim else { 455311116Sdim auto *CalleeTy = cast<PointerType>(Callee->getType())->getElementType(); 456360784Sdim CalleeFTy = cast<FunctionType>(CalleeTy); 457311116Sdim } 458311116Sdim 459311116Sdim std::string Sig = getSignature(CalleeFTy); 460311116Sdim if (InvokeWrappers.find(Sig) != InvokeWrappers.end()) 461311116Sdim return InvokeWrappers[Sig]; 462311116Sdim 463311116Sdim // Put the pointer to the callee as first argument 464311116Sdim ArgTys.push_back(PointerType::getUnqual(CalleeFTy)); 465311116Sdim // Add argument types 466311116Sdim ArgTys.append(CalleeFTy->param_begin(), CalleeFTy->param_end()); 467311116Sdim 468311116Sdim FunctionType *FTy = FunctionType::get(CalleeFTy->getReturnType(), ArgTys, 469311116Sdim CalleeFTy->isVarArg()); 470360784Sdim Function *F = 471360784Sdim Function::Create(FTy, GlobalValue::ExternalLinkage, "__invoke_" + Sig, M); 472311116Sdim InvokeWrappers[Sig] = F; 473311116Sdim return F; 474311116Sdim} 475311116Sdim 476311116Sdimbool WebAssemblyLowerEmscriptenEHSjLj::canLongjmp(Module &M, 477311116Sdim const Value *Callee) const { 478311116Sdim if (auto *CalleeF = dyn_cast<Function>(Callee)) 479311116Sdim if (CalleeF->isIntrinsic()) 480311116Sdim return false; 481311116Sdim 482353358Sdim // Attempting to transform inline assembly will result in something like: 483353358Sdim // call void @__invoke_void(void ()* asm ...) 484353358Sdim // which is invalid because inline assembly blocks do not have addresses 485353358Sdim // and can't be passed by pointer. The result is a crash with illegal IR. 486353358Sdim if (isa<InlineAsm>(Callee)) 487353358Sdim return false; 488360784Sdim StringRef CalleeName = Callee->getName(); 489353358Sdim 490311116Sdim // The reason we include malloc/free here is to exclude the malloc/free 491311116Sdim // calls generated in setjmp prep / cleanup routines. 492360784Sdim if (CalleeName == "setjmp" || CalleeName == "malloc" || CalleeName == "free") 493311116Sdim return false; 494311116Sdim 495311116Sdim // There are functions in JS glue code 496360784Sdim if (CalleeName == "__resumeException" || CalleeName == "llvm_eh_typeid_for" || 497360784Sdim CalleeName == "saveSetjmp" || CalleeName == "testSetjmp" || 498360784Sdim CalleeName == "getTempRet0" || CalleeName == "setTempRet0") 499311116Sdim return false; 500311116Sdim 501311116Sdim // __cxa_find_matching_catch_N functions cannot longjmp 502360784Sdim if (Callee->getName().startswith("__cxa_find_matching_catch_")) 503311116Sdim return false; 504311116Sdim 505311116Sdim // Exception-catching related functions 506360784Sdim if (CalleeName == "__cxa_begin_catch" || CalleeName == "__cxa_end_catch" || 507360784Sdim CalleeName == "__cxa_allocate_exception" || CalleeName == "__cxa_throw" || 508360784Sdim CalleeName == "__clang_call_terminate") 509311116Sdim return false; 510311116Sdim 511311116Sdim // Otherwise we don't know 512311116Sdim return true; 513311116Sdim} 514311116Sdim 515360784Sdimbool WebAssemblyLowerEmscriptenEHSjLj::isEmAsmCall(Module &M, 516360784Sdim const Value *Callee) const { 517360784Sdim StringRef CalleeName = Callee->getName(); 518360784Sdim // This is an exhaustive list from Emscripten's <emscripten/em_asm.h>. 519360784Sdim return CalleeName == "emscripten_asm_const_int" || 520360784Sdim CalleeName == "emscripten_asm_const_double" || 521360784Sdim CalleeName == "emscripten_asm_const_int_sync_on_main_thread" || 522360784Sdim CalleeName == "emscripten_asm_const_double_sync_on_main_thread" || 523360784Sdim CalleeName == "emscripten_asm_const_async_on_main_thread"; 524360784Sdim} 525360784Sdim 526311116Sdim// Generate testSetjmp function call seqence with preamble and postamble. 527311116Sdim// The code this generates is equivalent to the following JavaScript code: 528311116Sdim// if (%__THREW__.val != 0 & threwValue != 0) { 529311116Sdim// %label = _testSetjmp(mem[%__THREW__.val], setjmpTable, setjmpTableSize); 530311116Sdim// if (%label == 0) 531311116Sdim// emscripten_longjmp(%__THREW__.val, threwValue); 532344779Sdim// setTempRet0(threwValue); 533311116Sdim// } else { 534311116Sdim// %label = -1; 535311116Sdim// } 536344779Sdim// %longjmp_result = getTempRet0(); 537311116Sdim// 538311116Sdim// As output parameters. returns %label, %longjmp_result, and the BB the last 539311116Sdim// instruction (%longjmp_result = ...) is in. 540311116Sdimvoid WebAssemblyLowerEmscriptenEHSjLj::wrapTestSetjmp( 541311116Sdim BasicBlock *BB, Instruction *InsertPt, Value *Threw, Value *SetjmpTable, 542311116Sdim Value *SetjmpTableSize, Value *&Label, Value *&LongjmpResult, 543311116Sdim BasicBlock *&EndBB) { 544311116Sdim Function *F = BB->getParent(); 545311116Sdim LLVMContext &C = BB->getModule()->getContext(); 546311116Sdim IRBuilder<> IRB(C); 547311116Sdim IRB.SetInsertPoint(InsertPt); 548311116Sdim 549311116Sdim // if (%__THREW__.val != 0 & threwValue != 0) 550311116Sdim IRB.SetInsertPoint(BB); 551311116Sdim BasicBlock *ThenBB1 = BasicBlock::Create(C, "if.then1", F); 552311116Sdim BasicBlock *ElseBB1 = BasicBlock::Create(C, "if.else1", F); 553311116Sdim BasicBlock *EndBB1 = BasicBlock::Create(C, "if.end", F); 554311116Sdim Value *ThrewCmp = IRB.CreateICmpNE(Threw, IRB.getInt32(0)); 555353358Sdim Value *ThrewValue = IRB.CreateLoad(IRB.getInt32Ty(), ThrewValueGV, 556353358Sdim ThrewValueGV->getName() + ".val"); 557311116Sdim Value *ThrewValueCmp = IRB.CreateICmpNE(ThrewValue, IRB.getInt32(0)); 558311116Sdim Value *Cmp1 = IRB.CreateAnd(ThrewCmp, ThrewValueCmp, "cmp1"); 559311116Sdim IRB.CreateCondBr(Cmp1, ThenBB1, ElseBB1); 560311116Sdim 561311116Sdim // %label = _testSetjmp(mem[%__THREW__.val], _setjmpTable, _setjmpTableSize); 562311116Sdim // if (%label == 0) 563311116Sdim IRB.SetInsertPoint(ThenBB1); 564311116Sdim BasicBlock *ThenBB2 = BasicBlock::Create(C, "if.then2", F); 565311116Sdim BasicBlock *EndBB2 = BasicBlock::Create(C, "if.end2", F); 566311116Sdim Value *ThrewInt = IRB.CreateIntToPtr(Threw, Type::getInt32PtrTy(C), 567311116Sdim Threw->getName() + ".i32p"); 568353358Sdim Value *LoadedThrew = IRB.CreateLoad(IRB.getInt32Ty(), ThrewInt, 569353358Sdim ThrewInt->getName() + ".loaded"); 570311116Sdim Value *ThenLabel = IRB.CreateCall( 571311116Sdim TestSetjmpF, {LoadedThrew, SetjmpTable, SetjmpTableSize}, "label"); 572311116Sdim Value *Cmp2 = IRB.CreateICmpEQ(ThenLabel, IRB.getInt32(0)); 573311116Sdim IRB.CreateCondBr(Cmp2, ThenBB2, EndBB2); 574311116Sdim 575311116Sdim // emscripten_longjmp(%__THREW__.val, threwValue); 576311116Sdim IRB.SetInsertPoint(ThenBB2); 577311116Sdim IRB.CreateCall(EmLongjmpF, {Threw, ThrewValue}); 578311116Sdim IRB.CreateUnreachable(); 579311116Sdim 580344779Sdim // setTempRet0(threwValue); 581311116Sdim IRB.SetInsertPoint(EndBB2); 582344779Sdim IRB.CreateCall(SetTempRet0Func, ThrewValue); 583311116Sdim IRB.CreateBr(EndBB1); 584311116Sdim 585311116Sdim IRB.SetInsertPoint(ElseBB1); 586311116Sdim IRB.CreateBr(EndBB1); 587311116Sdim 588344779Sdim // longjmp_result = getTempRet0(); 589311116Sdim IRB.SetInsertPoint(EndBB1); 590311116Sdim PHINode *LabelPHI = IRB.CreatePHI(IRB.getInt32Ty(), 2, "label"); 591311116Sdim LabelPHI->addIncoming(ThenLabel, EndBB2); 592311116Sdim 593311116Sdim LabelPHI->addIncoming(IRB.getInt32(-1), ElseBB1); 594311116Sdim 595311116Sdim // Output parameter assignment 596311116Sdim Label = LabelPHI; 597311116Sdim EndBB = EndBB1; 598344779Sdim LongjmpResult = IRB.CreateCall(GetTempRet0Func, None, "longjmp_result"); 599311116Sdim} 600311116Sdim 601311116Sdimvoid WebAssemblyLowerEmscriptenEHSjLj::rebuildSSA(Function &F) { 602311116Sdim DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(F).getDomTree(); 603311116Sdim DT.recalculate(F); // CFG has been changed 604311116Sdim SSAUpdater SSA; 605311116Sdim for (BasicBlock &BB : F) { 606311116Sdim for (Instruction &I : BB) { 607360784Sdim SSA.Initialize(I.getType(), I.getName()); 608360784Sdim SSA.AddAvailableValue(&BB, &I); 609311116Sdim for (auto UI = I.use_begin(), UE = I.use_end(); UI != UE;) { 610311116Sdim Use &U = *UI; 611311116Sdim ++UI; 612353358Sdim auto *User = cast<Instruction>(U.getUser()); 613353358Sdim if (auto *UserPN = dyn_cast<PHINode>(User)) 614311116Sdim if (UserPN->getIncomingBlock(U) == &BB) 615311116Sdim continue; 616311116Sdim 617311116Sdim if (DT.dominates(&I, User)) 618311116Sdim continue; 619311116Sdim SSA.RewriteUseAfterInsertions(U); 620311116Sdim } 621311116Sdim } 622311116Sdim } 623311116Sdim} 624311116Sdim 625311116Sdimbool WebAssemblyLowerEmscriptenEHSjLj::runOnModule(Module &M) { 626344779Sdim LLVM_DEBUG(dbgs() << "********** Lower Emscripten EH & SjLj **********\n"); 627344779Sdim 628311116Sdim LLVMContext &C = M.getContext(); 629311116Sdim IRBuilder<> IRB(C); 630311116Sdim 631311116Sdim Function *SetjmpF = M.getFunction("setjmp"); 632311116Sdim Function *LongjmpF = M.getFunction("longjmp"); 633311116Sdim bool SetjmpUsed = SetjmpF && !SetjmpF->use_empty(); 634311116Sdim bool LongjmpUsed = LongjmpF && !LongjmpF->use_empty(); 635311116Sdim bool DoSjLj = EnableSjLj && (SetjmpUsed || LongjmpUsed); 636311116Sdim 637344779Sdim // Declare (or get) global variables __THREW__, __threwValue, and 638344779Sdim // getTempRet0/setTempRet0 function which are used in common for both 639344779Sdim // exception handling and setjmp/longjmp handling 640344779Sdim ThrewGV = getGlobalVariableI32(M, IRB, "__THREW__"); 641344779Sdim ThrewValueGV = getGlobalVariableI32(M, IRB, "__threwValue"); 642344779Sdim GetTempRet0Func = 643344779Sdim Function::Create(FunctionType::get(IRB.getInt32Ty(), false), 644344779Sdim GlobalValue::ExternalLinkage, "getTempRet0", &M); 645344779Sdim SetTempRet0Func = Function::Create( 646344779Sdim FunctionType::get(IRB.getVoidTy(), IRB.getInt32Ty(), false), 647344779Sdim GlobalValue::ExternalLinkage, "setTempRet0", &M); 648344779Sdim GetTempRet0Func->setDoesNotThrow(); 649344779Sdim SetTempRet0Func->setDoesNotThrow(); 650311116Sdim 651311116Sdim bool Changed = false; 652311116Sdim 653311116Sdim // Exception handling 654311116Sdim if (EnableEH) { 655311116Sdim // Register __resumeException function 656311116Sdim FunctionType *ResumeFTy = 657311116Sdim FunctionType::get(IRB.getVoidTy(), IRB.getInt8PtrTy(), false); 658311116Sdim ResumeF = Function::Create(ResumeFTy, GlobalValue::ExternalLinkage, 659360784Sdim "__resumeException", &M); 660311116Sdim 661311116Sdim // Register llvm_eh_typeid_for function 662311116Sdim FunctionType *EHTypeIDTy = 663311116Sdim FunctionType::get(IRB.getInt32Ty(), IRB.getInt8PtrTy(), false); 664311116Sdim EHTypeIDF = Function::Create(EHTypeIDTy, GlobalValue::ExternalLinkage, 665360784Sdim "llvm_eh_typeid_for", &M); 666311116Sdim 667311116Sdim for (Function &F : M) { 668311116Sdim if (F.isDeclaration()) 669311116Sdim continue; 670311116Sdim Changed |= runEHOnFunction(F); 671311116Sdim } 672311116Sdim } 673311116Sdim 674311116Sdim // Setjmp/longjmp handling 675311116Sdim if (DoSjLj) { 676311116Sdim Changed = true; // We have setjmp or longjmp somewhere 677311116Sdim 678311116Sdim if (LongjmpF) { 679311116Sdim // Replace all uses of longjmp with emscripten_longjmp_jmpbuf, which is 680311116Sdim // defined in JS code 681311116Sdim EmLongjmpJmpbufF = Function::Create(LongjmpF->getFunctionType(), 682311116Sdim GlobalValue::ExternalLinkage, 683360784Sdim "emscripten_longjmp_jmpbuf", &M); 684311116Sdim 685311116Sdim LongjmpF->replaceAllUsesWith(EmLongjmpJmpbufF); 686311116Sdim } 687311116Sdim 688344779Sdim if (SetjmpF) { 689344779Sdim // Register saveSetjmp function 690344779Sdim FunctionType *SetjmpFTy = SetjmpF->getFunctionType(); 691344779Sdim SmallVector<Type *, 4> Params = {SetjmpFTy->getParamType(0), 692344779Sdim IRB.getInt32Ty(), Type::getInt32PtrTy(C), 693344779Sdim IRB.getInt32Ty()}; 694344779Sdim FunctionType *FTy = 695344779Sdim FunctionType::get(Type::getInt32PtrTy(C), Params, false); 696360784Sdim SaveSetjmpF = 697360784Sdim Function::Create(FTy, GlobalValue::ExternalLinkage, "saveSetjmp", &M); 698344779Sdim 699344779Sdim // Register testSetjmp function 700344779Sdim Params = {IRB.getInt32Ty(), Type::getInt32PtrTy(C), IRB.getInt32Ty()}; 701344779Sdim FTy = FunctionType::get(IRB.getInt32Ty(), Params, false); 702360784Sdim TestSetjmpF = 703360784Sdim Function::Create(FTy, GlobalValue::ExternalLinkage, "testSetjmp", &M); 704344779Sdim 705344779Sdim FTy = FunctionType::get(IRB.getVoidTy(), 706344779Sdim {IRB.getInt32Ty(), IRB.getInt32Ty()}, false); 707344779Sdim EmLongjmpF = Function::Create(FTy, GlobalValue::ExternalLinkage, 708360784Sdim "emscripten_longjmp", &M); 709344779Sdim 710344779Sdim // Only traverse functions that uses setjmp in order not to insert 711344779Sdim // unnecessary prep / cleanup code in every function 712344779Sdim SmallPtrSet<Function *, 8> SetjmpUsers; 713344779Sdim for (User *U : SetjmpF->users()) { 714344779Sdim auto *UI = cast<Instruction>(U); 715344779Sdim SetjmpUsers.insert(UI->getFunction()); 716344779Sdim } 717344779Sdim for (Function *F : SetjmpUsers) 718344779Sdim runSjLjOnFunction(*F); 719311116Sdim } 720311116Sdim } 721311116Sdim 722311116Sdim if (!Changed) { 723311116Sdim // Delete unused global variables and functions 724311116Sdim if (ResumeF) 725311116Sdim ResumeF->eraseFromParent(); 726311116Sdim if (EHTypeIDF) 727311116Sdim EHTypeIDF->eraseFromParent(); 728311116Sdim if (EmLongjmpF) 729311116Sdim EmLongjmpF->eraseFromParent(); 730311116Sdim if (SaveSetjmpF) 731311116Sdim SaveSetjmpF->eraseFromParent(); 732311116Sdim if (TestSetjmpF) 733311116Sdim TestSetjmpF->eraseFromParent(); 734311116Sdim return false; 735311116Sdim } 736311116Sdim 737311116Sdim return true; 738311116Sdim} 739311116Sdim 740311116Sdimbool WebAssemblyLowerEmscriptenEHSjLj::runEHOnFunction(Function &F) { 741311116Sdim Module &M = *F.getParent(); 742311116Sdim LLVMContext &C = F.getContext(); 743311116Sdim IRBuilder<> IRB(C); 744311116Sdim bool Changed = false; 745311116Sdim SmallVector<Instruction *, 64> ToErase; 746311116Sdim SmallPtrSet<LandingPadInst *, 32> LandingPads; 747311116Sdim bool AllowExceptions = 748311116Sdim areAllExceptionsAllowed() || EHWhitelistSet.count(F.getName()); 749311116Sdim 750311116Sdim for (BasicBlock &BB : F) { 751311116Sdim auto *II = dyn_cast<InvokeInst>(BB.getTerminator()); 752311116Sdim if (!II) 753311116Sdim continue; 754311116Sdim Changed = true; 755311116Sdim LandingPads.insert(II->getLandingPadInst()); 756311116Sdim IRB.SetInsertPoint(II); 757311116Sdim 758311116Sdim bool NeedInvoke = AllowExceptions && canThrow(II->getCalledValue()); 759311116Sdim if (NeedInvoke) { 760311116Sdim // Wrap invoke with invoke wrapper and generate preamble/postamble 761311116Sdim Value *Threw = wrapInvoke(II); 762311116Sdim ToErase.push_back(II); 763311116Sdim 764311116Sdim // Insert a branch based on __THREW__ variable 765311116Sdim Value *Cmp = IRB.CreateICmpEQ(Threw, IRB.getInt32(1), "cmp"); 766311116Sdim IRB.CreateCondBr(Cmp, II->getUnwindDest(), II->getNormalDest()); 767311116Sdim 768311116Sdim } else { 769311116Sdim // This can't throw, and we don't need this invoke, just replace it with a 770311116Sdim // call+branch 771311116Sdim SmallVector<Value *, 16> Args(II->arg_begin(), II->arg_end()); 772353358Sdim CallInst *NewCall = 773353358Sdim IRB.CreateCall(II->getFunctionType(), II->getCalledValue(), Args); 774311116Sdim NewCall->takeName(II); 775311116Sdim NewCall->setCallingConv(II->getCallingConv()); 776311116Sdim NewCall->setDebugLoc(II->getDebugLoc()); 777311116Sdim NewCall->setAttributes(II->getAttributes()); 778311116Sdim II->replaceAllUsesWith(NewCall); 779311116Sdim ToErase.push_back(II); 780311116Sdim 781311116Sdim IRB.CreateBr(II->getNormalDest()); 782311116Sdim 783311116Sdim // Remove any PHI node entries from the exception destination 784311116Sdim II->getUnwindDest()->removePredecessor(&BB); 785311116Sdim } 786311116Sdim } 787311116Sdim 788311116Sdim // Process resume instructions 789311116Sdim for (BasicBlock &BB : F) { 790311116Sdim // Scan the body of the basic block for resumes 791311116Sdim for (Instruction &I : BB) { 792311116Sdim auto *RI = dyn_cast<ResumeInst>(&I); 793311116Sdim if (!RI) 794311116Sdim continue; 795360784Sdim Changed = true; 796311116Sdim 797311116Sdim // Split the input into legal values 798311116Sdim Value *Input = RI->getValue(); 799311116Sdim IRB.SetInsertPoint(RI); 800311116Sdim Value *Low = IRB.CreateExtractValue(Input, 0, "low"); 801311116Sdim // Create a call to __resumeException function 802311116Sdim IRB.CreateCall(ResumeF, {Low}); 803311116Sdim // Add a terminator to the block 804311116Sdim IRB.CreateUnreachable(); 805311116Sdim ToErase.push_back(RI); 806311116Sdim } 807311116Sdim } 808311116Sdim 809311116Sdim // Process llvm.eh.typeid.for intrinsics 810311116Sdim for (BasicBlock &BB : F) { 811311116Sdim for (Instruction &I : BB) { 812311116Sdim auto *CI = dyn_cast<CallInst>(&I); 813311116Sdim if (!CI) 814311116Sdim continue; 815311116Sdim const Function *Callee = CI->getCalledFunction(); 816311116Sdim if (!Callee) 817311116Sdim continue; 818311116Sdim if (Callee->getIntrinsicID() != Intrinsic::eh_typeid_for) 819311116Sdim continue; 820360784Sdim Changed = true; 821311116Sdim 822311116Sdim IRB.SetInsertPoint(CI); 823311116Sdim CallInst *NewCI = 824311116Sdim IRB.CreateCall(EHTypeIDF, CI->getArgOperand(0), "typeid"); 825311116Sdim CI->replaceAllUsesWith(NewCI); 826311116Sdim ToErase.push_back(CI); 827311116Sdim } 828311116Sdim } 829311116Sdim 830321369Sdim // Look for orphan landingpads, can occur in blocks with no predecessors 831311116Sdim for (BasicBlock &BB : F) { 832311116Sdim Instruction *I = BB.getFirstNonPHI(); 833311116Sdim if (auto *LPI = dyn_cast<LandingPadInst>(I)) 834311116Sdim LandingPads.insert(LPI); 835311116Sdim } 836360784Sdim Changed |= !LandingPads.empty(); 837311116Sdim 838311116Sdim // Handle all the landingpad for this function together, as multiple invokes 839311116Sdim // may share a single lp 840311116Sdim for (LandingPadInst *LPI : LandingPads) { 841311116Sdim IRB.SetInsertPoint(LPI); 842311116Sdim SmallVector<Value *, 16> FMCArgs; 843353358Sdim for (unsigned I = 0, E = LPI->getNumClauses(); I < E; ++I) { 844353358Sdim Constant *Clause = LPI->getClause(I); 845311116Sdim // As a temporary workaround for the lack of aggregate varargs support 846311116Sdim // in the interface between JS and wasm, break out filter operands into 847311116Sdim // their component elements. 848353358Sdim if (LPI->isFilter(I)) { 849311116Sdim auto *ATy = cast<ArrayType>(Clause->getType()); 850353358Sdim for (unsigned J = 0, E = ATy->getNumElements(); J < E; ++J) { 851353358Sdim Value *EV = IRB.CreateExtractValue(Clause, makeArrayRef(J), "filter"); 852311116Sdim FMCArgs.push_back(EV); 853311116Sdim } 854311116Sdim } else 855311116Sdim FMCArgs.push_back(Clause); 856311116Sdim } 857311116Sdim 858311116Sdim // Create a call to __cxa_find_matching_catch_N function 859311116Sdim Function *FMCF = getFindMatchingCatch(M, FMCArgs.size()); 860311116Sdim CallInst *FMCI = IRB.CreateCall(FMCF, FMCArgs, "fmc"); 861311116Sdim Value *Undef = UndefValue::get(LPI->getType()); 862311116Sdim Value *Pair0 = IRB.CreateInsertValue(Undef, FMCI, 0, "pair0"); 863344779Sdim Value *TempRet0 = IRB.CreateCall(GetTempRet0Func, None, "tempret0"); 864311116Sdim Value *Pair1 = IRB.CreateInsertValue(Pair0, TempRet0, 1, "pair1"); 865311116Sdim 866311116Sdim LPI->replaceAllUsesWith(Pair1); 867311116Sdim ToErase.push_back(LPI); 868311116Sdim } 869311116Sdim 870311116Sdim // Erase everything we no longer need in this function 871311116Sdim for (Instruction *I : ToErase) 872311116Sdim I->eraseFromParent(); 873311116Sdim 874311116Sdim return Changed; 875311116Sdim} 876311116Sdim 877311116Sdimbool WebAssemblyLowerEmscriptenEHSjLj::runSjLjOnFunction(Function &F) { 878311116Sdim Module &M = *F.getParent(); 879311116Sdim LLVMContext &C = F.getContext(); 880311116Sdim IRBuilder<> IRB(C); 881311116Sdim SmallVector<Instruction *, 64> ToErase; 882311116Sdim // Vector of %setjmpTable values 883311116Sdim std::vector<Instruction *> SetjmpTableInsts; 884311116Sdim // Vector of %setjmpTableSize values 885311116Sdim std::vector<Instruction *> SetjmpTableSizeInsts; 886311116Sdim 887311116Sdim // Setjmp preparation 888311116Sdim 889311116Sdim // This instruction effectively means %setjmpTableSize = 4. 890311116Sdim // We create this as an instruction intentionally, and we don't want to fold 891311116Sdim // this instruction to a constant 4, because this value will be used in 892311116Sdim // SSAUpdater.AddAvailableValue(...) later. 893311116Sdim BasicBlock &EntryBB = F.getEntryBlock(); 894311116Sdim BinaryOperator *SetjmpTableSize = BinaryOperator::Create( 895311116Sdim Instruction::Add, IRB.getInt32(4), IRB.getInt32(0), "setjmpTableSize", 896311116Sdim &*EntryBB.getFirstInsertionPt()); 897311116Sdim // setjmpTable = (int *) malloc(40); 898311116Sdim Instruction *SetjmpTable = CallInst::CreateMalloc( 899311116Sdim SetjmpTableSize, IRB.getInt32Ty(), IRB.getInt32Ty(), IRB.getInt32(40), 900311116Sdim nullptr, nullptr, "setjmpTable"); 901311116Sdim // setjmpTable[0] = 0; 902311116Sdim IRB.SetInsertPoint(SetjmpTableSize); 903311116Sdim IRB.CreateStore(IRB.getInt32(0), SetjmpTable); 904311116Sdim SetjmpTableInsts.push_back(SetjmpTable); 905311116Sdim SetjmpTableSizeInsts.push_back(SetjmpTableSize); 906311116Sdim 907311116Sdim // Setjmp transformation 908311116Sdim std::vector<PHINode *> SetjmpRetPHIs; 909311116Sdim Function *SetjmpF = M.getFunction("setjmp"); 910311116Sdim for (User *U : SetjmpF->users()) { 911311116Sdim auto *CI = dyn_cast<CallInst>(U); 912311116Sdim if (!CI) 913311116Sdim report_fatal_error("Does not support indirect calls to setjmp"); 914311116Sdim 915311116Sdim BasicBlock *BB = CI->getParent(); 916311116Sdim if (BB->getParent() != &F) // in other function 917311116Sdim continue; 918311116Sdim 919311116Sdim // The tail is everything right after the call, and will be reached once 920311116Sdim // when setjmp is called, and later when longjmp returns to the setjmp 921311116Sdim BasicBlock *Tail = SplitBlock(BB, CI->getNextNode()); 922311116Sdim // Add a phi to the tail, which will be the output of setjmp, which 923311116Sdim // indicates if this is the first call or a longjmp back. The phi directly 924311116Sdim // uses the right value based on where we arrive from 925311116Sdim IRB.SetInsertPoint(Tail->getFirstNonPHI()); 926311116Sdim PHINode *SetjmpRet = IRB.CreatePHI(IRB.getInt32Ty(), 2, "setjmp.ret"); 927311116Sdim 928311116Sdim // setjmp initial call returns 0 929311116Sdim SetjmpRet->addIncoming(IRB.getInt32(0), BB); 930311116Sdim // The proper output is now this, not the setjmp call itself 931311116Sdim CI->replaceAllUsesWith(SetjmpRet); 932311116Sdim // longjmp returns to the setjmp will add themselves to this phi 933311116Sdim SetjmpRetPHIs.push_back(SetjmpRet); 934311116Sdim 935311116Sdim // Fix call target 936311116Sdim // Our index in the function is our place in the array + 1 to avoid index 937311116Sdim // 0, because index 0 means the longjmp is not ours to handle. 938311116Sdim IRB.SetInsertPoint(CI); 939311116Sdim Value *Args[] = {CI->getArgOperand(0), IRB.getInt32(SetjmpRetPHIs.size()), 940311116Sdim SetjmpTable, SetjmpTableSize}; 941311116Sdim Instruction *NewSetjmpTable = 942311116Sdim IRB.CreateCall(SaveSetjmpF, Args, "setjmpTable"); 943311116Sdim Instruction *NewSetjmpTableSize = 944344779Sdim IRB.CreateCall(GetTempRet0Func, None, "setjmpTableSize"); 945311116Sdim SetjmpTableInsts.push_back(NewSetjmpTable); 946311116Sdim SetjmpTableSizeInsts.push_back(NewSetjmpTableSize); 947311116Sdim ToErase.push_back(CI); 948311116Sdim } 949311116Sdim 950311116Sdim // Update each call that can longjmp so it can return to a setjmp where 951311116Sdim // relevant. 952311116Sdim 953311116Sdim // Because we are creating new BBs while processing and don't want to make 954311116Sdim // all these newly created BBs candidates again for longjmp processing, we 955311116Sdim // first make the vector of candidate BBs. 956311116Sdim std::vector<BasicBlock *> BBs; 957311116Sdim for (BasicBlock &BB : F) 958311116Sdim BBs.push_back(&BB); 959311116Sdim 960311116Sdim // BBs.size() will change within the loop, so we query it every time 961353358Sdim for (unsigned I = 0; I < BBs.size(); I++) { 962353358Sdim BasicBlock *BB = BBs[I]; 963311116Sdim for (Instruction &I : *BB) { 964311116Sdim assert(!isa<InvokeInst>(&I)); 965311116Sdim auto *CI = dyn_cast<CallInst>(&I); 966311116Sdim if (!CI) 967311116Sdim continue; 968311116Sdim 969311116Sdim const Value *Callee = CI->getCalledValue(); 970311116Sdim if (!canLongjmp(M, Callee)) 971311116Sdim continue; 972360784Sdim if (isEmAsmCall(M, Callee)) 973360784Sdim report_fatal_error("Cannot use EM_ASM* alongside setjmp/longjmp in " + 974360784Sdim F.getName() + 975360784Sdim ". Please consider using EM_JS, or move the " 976360784Sdim "EM_ASM into another function.", 977360784Sdim false); 978311116Sdim 979311116Sdim Value *Threw = nullptr; 980311116Sdim BasicBlock *Tail; 981360784Sdim if (Callee->getName().startswith("__invoke_")) { 982311116Sdim // If invoke wrapper has already been generated for this call in 983311116Sdim // previous EH phase, search for the load instruction 984311116Sdim // %__THREW__.val = __THREW__; 985311116Sdim // in postamble after the invoke wrapper call 986311116Sdim LoadInst *ThrewLI = nullptr; 987311116Sdim StoreInst *ThrewResetSI = nullptr; 988311116Sdim for (auto I = std::next(BasicBlock::iterator(CI)), IE = BB->end(); 989311116Sdim I != IE; ++I) { 990311116Sdim if (auto *LI = dyn_cast<LoadInst>(I)) 991311116Sdim if (auto *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand())) 992311116Sdim if (GV == ThrewGV) { 993311116Sdim Threw = ThrewLI = LI; 994311116Sdim break; 995311116Sdim } 996311116Sdim } 997311116Sdim // Search for the store instruction after the load above 998311116Sdim // __THREW__ = 0; 999311116Sdim for (auto I = std::next(BasicBlock::iterator(ThrewLI)), IE = BB->end(); 1000311116Sdim I != IE; ++I) { 1001311116Sdim if (auto *SI = dyn_cast<StoreInst>(I)) 1002311116Sdim if (auto *GV = dyn_cast<GlobalVariable>(SI->getPointerOperand())) 1003311116Sdim if (GV == ThrewGV && SI->getValueOperand() == IRB.getInt32(0)) { 1004311116Sdim ThrewResetSI = SI; 1005311116Sdim break; 1006311116Sdim } 1007311116Sdim } 1008311116Sdim assert(Threw && ThrewLI && "Cannot find __THREW__ load after invoke"); 1009311116Sdim assert(ThrewResetSI && "Cannot find __THREW__ store after invoke"); 1010311116Sdim Tail = SplitBlock(BB, ThrewResetSI->getNextNode()); 1011311116Sdim 1012311116Sdim } else { 1013311116Sdim // Wrap call with invoke wrapper and generate preamble/postamble 1014311116Sdim Threw = wrapInvoke(CI); 1015311116Sdim ToErase.push_back(CI); 1016311116Sdim Tail = SplitBlock(BB, CI->getNextNode()); 1017311116Sdim } 1018311116Sdim 1019311116Sdim // We need to replace the terminator in Tail - SplitBlock makes BB go 1020311116Sdim // straight to Tail, we need to check if a longjmp occurred, and go to the 1021311116Sdim // right setjmp-tail if so 1022311116Sdim ToErase.push_back(BB->getTerminator()); 1023311116Sdim 1024311116Sdim // Generate a function call to testSetjmp function and preamble/postamble 1025311116Sdim // code to figure out (1) whether longjmp occurred (2) if longjmp 1026311116Sdim // occurred, which setjmp it corresponds to 1027311116Sdim Value *Label = nullptr; 1028311116Sdim Value *LongjmpResult = nullptr; 1029311116Sdim BasicBlock *EndBB = nullptr; 1030311116Sdim wrapTestSetjmp(BB, CI, Threw, SetjmpTable, SetjmpTableSize, Label, 1031311116Sdim LongjmpResult, EndBB); 1032311116Sdim assert(Label && LongjmpResult && EndBB); 1033311116Sdim 1034311116Sdim // Create switch instruction 1035311116Sdim IRB.SetInsertPoint(EndBB); 1036311116Sdim SwitchInst *SI = IRB.CreateSwitch(Label, Tail, SetjmpRetPHIs.size()); 1037311116Sdim // -1 means no longjmp happened, continue normally (will hit the default 1038311116Sdim // switch case). 0 means a longjmp that is not ours to handle, needs a 1039311116Sdim // rethrow. Otherwise the index is the same as the index in P+1 (to avoid 1040311116Sdim // 0). 1041353358Sdim for (unsigned I = 0; I < SetjmpRetPHIs.size(); I++) { 1042353358Sdim SI->addCase(IRB.getInt32(I + 1), SetjmpRetPHIs[I]->getParent()); 1043353358Sdim SetjmpRetPHIs[I]->addIncoming(LongjmpResult, EndBB); 1044311116Sdim } 1045311116Sdim 1046311116Sdim // We are splitting the block here, and must continue to find other calls 1047311116Sdim // in the block - which is now split. so continue to traverse in the Tail 1048311116Sdim BBs.push_back(Tail); 1049311116Sdim } 1050311116Sdim } 1051311116Sdim 1052311116Sdim // Erase everything we no longer need in this function 1053311116Sdim for (Instruction *I : ToErase) 1054311116Sdim I->eraseFromParent(); 1055311116Sdim 1056311116Sdim // Free setjmpTable buffer before each return instruction 1057311116Sdim for (BasicBlock &BB : F) { 1058344779Sdim Instruction *TI = BB.getTerminator(); 1059311116Sdim if (isa<ReturnInst>(TI)) 1060311116Sdim CallInst::CreateFree(SetjmpTable, TI); 1061311116Sdim } 1062311116Sdim 1063311116Sdim // Every call to saveSetjmp can change setjmpTable and setjmpTableSize 1064311116Sdim // (when buffer reallocation occurs) 1065311116Sdim // entry: 1066311116Sdim // setjmpTableSize = 4; 1067311116Sdim // setjmpTable = (int *) malloc(40); 1068311116Sdim // setjmpTable[0] = 0; 1069311116Sdim // ... 1070311116Sdim // somebb: 1071311116Sdim // setjmpTable = saveSetjmp(buf, label, setjmpTable, setjmpTableSize); 1072344779Sdim // setjmpTableSize = getTempRet0(); 1073311116Sdim // So we need to make sure the SSA for these variables is valid so that every 1074311116Sdim // saveSetjmp and testSetjmp calls have the correct arguments. 1075311116Sdim SSAUpdater SetjmpTableSSA; 1076311116Sdim SSAUpdater SetjmpTableSizeSSA; 1077311116Sdim SetjmpTableSSA.Initialize(Type::getInt32PtrTy(C), "setjmpTable"); 1078311116Sdim SetjmpTableSizeSSA.Initialize(Type::getInt32Ty(C), "setjmpTableSize"); 1079311116Sdim for (Instruction *I : SetjmpTableInsts) 1080311116Sdim SetjmpTableSSA.AddAvailableValue(I->getParent(), I); 1081311116Sdim for (Instruction *I : SetjmpTableSizeInsts) 1082311116Sdim SetjmpTableSizeSSA.AddAvailableValue(I->getParent(), I); 1083311116Sdim 1084311116Sdim for (auto UI = SetjmpTable->use_begin(), UE = SetjmpTable->use_end(); 1085311116Sdim UI != UE;) { 1086311116Sdim // Grab the use before incrementing the iterator. 1087311116Sdim Use &U = *UI; 1088311116Sdim // Increment the iterator before removing the use from the list. 1089311116Sdim ++UI; 1090353358Sdim if (auto *I = dyn_cast<Instruction>(U.getUser())) 1091311116Sdim if (I->getParent() != &EntryBB) 1092311116Sdim SetjmpTableSSA.RewriteUse(U); 1093311116Sdim } 1094311116Sdim for (auto UI = SetjmpTableSize->use_begin(), UE = SetjmpTableSize->use_end(); 1095311116Sdim UI != UE;) { 1096311116Sdim Use &U = *UI; 1097311116Sdim ++UI; 1098353358Sdim if (auto *I = dyn_cast<Instruction>(U.getUser())) 1099311116Sdim if (I->getParent() != &EntryBB) 1100311116Sdim SetjmpTableSizeSSA.RewriteUse(U); 1101311116Sdim } 1102311116Sdim 1103311116Sdim // Finally, our modifications to the cfg can break dominance of SSA variables. 1104311116Sdim // For example, in this code, 1105311116Sdim // if (x()) { .. setjmp() .. } 1106311116Sdim // if (y()) { .. longjmp() .. } 1107311116Sdim // We must split the longjmp block, and it can jump into the block splitted 1108311116Sdim // from setjmp one. But that means that when we split the setjmp block, it's 1109311116Sdim // first part no longer dominates its second part - there is a theoretically 1110311116Sdim // possible control flow path where x() is false, then y() is true and we 1111311116Sdim // reach the second part of the setjmp block, without ever reaching the first 1112311116Sdim // part. So, we rebuild SSA form here. 1113311116Sdim rebuildSSA(F); 1114311116Sdim return true; 1115311116Sdim} 1116