1 //=== WebAssemblyLowerEmscriptenEHSjLj.cpp - Lower exceptions for Emscripten =//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file lowers exception-related instructions and setjmp/longjmp
11 /// function calls in order to use Emscripten's JavaScript try and catch
12 /// mechanism.
13 ///
14 /// To handle exceptions and setjmp/longjmps, this scheme relies on JavaScript's
15 /// try and catch syntax and relevant exception-related libraries implemented
16 /// in JavaScript glue code that will be produced by Emscripten. This is similar
17 /// to the current Emscripten asm.js exception handling in fastcomp. For
18 /// fastcomp's EH / SjLj scheme, see these files in fastcomp LLVM branch:
19 /// (Location: https://github.com/kripken/emscripten-fastcomp)
20 /// lib/Target/JSBackend/NaCl/LowerEmExceptionsPass.cpp
21 /// lib/Target/JSBackend/NaCl/LowerEmSetjmp.cpp
22 /// lib/Target/JSBackend/JSBackend.cpp
23 /// lib/Target/JSBackend/CallHandlers.h
24 ///
25 /// * Exception handling
26 /// This pass lowers invokes and landingpads into library functions in JS glue
27 /// code. Invokes are lowered into function wrappers called invoke wrappers that
28 /// exist in JS side, which wraps the original function call with JS try-catch.
29 /// If an exception occurred, cxa_throw() function in JS side sets some
30 /// variables (see below) so we can check whether an exception occurred from
31 /// wasm code and handle it appropriately.
32 ///
33 /// * Setjmp-longjmp handling
34 /// This pass lowers setjmp to a reasonably-performant approach for emscripten.
35 /// The idea is that each block with a setjmp is broken up into two parts: the
36 /// part containing setjmp and the part right after the setjmp. The latter part
37 /// is either reached from the setjmp, or later from a longjmp. To handle the
38 /// longjmp, all calls that might longjmp are also called using invoke wrappers
39 /// and thus JS / try-catch. JS longjmp() function also sets some variables so
40 /// we can check / whether a longjmp occurred from wasm code. Each block with a
41 /// function call that might longjmp is also split up after the longjmp call.
42 /// After the longjmp call, we check whether a longjmp occurred, and if it did,
43 /// which setjmp it corresponds to, and jump to the right post-setjmp block.
44 /// We assume setjmp-longjmp handling always run after EH handling, which means
45 /// we don't expect any exception-related instructions when SjLj runs.
46 /// FIXME Currently this scheme does not support indirect call of setjmp,
47 /// because of the limitation of the scheme itself. fastcomp does not support it
48 /// either.
49 ///
50 /// In detail, this pass does following things:
51 ///
52 /// 1) Assumes the existence of global variables: __THREW__, __threwValue
53 /// __THREW__ and __threwValue will be set in invoke wrappers
54 /// in JS glue code. For what invoke wrappers are, refer to 3). These
55 /// variables are used for both exceptions and setjmp/longjmps.
56 /// __THREW__ indicates whether an exception or a longjmp occurred or not. 0
57 /// means nothing occurred, 1 means an exception occurred, and other numbers
58 /// mean a longjmp occurred. In the case of longjmp, __threwValue variable
59 /// indicates the corresponding setjmp buffer the longjmp corresponds to.
60 ///
61 /// * Exception handling
62 ///
63 /// 2) We assume the existence of setThrew and setTempRet0/getTempRet0 functions
64 /// at link time.
65 /// The global variables in 1) will exist in wasm address space,
66 /// but their values should be set in JS code, so these functions
67 /// as interfaces to JS glue code. These functions are equivalent to the
68 /// following JS functions, which actually exist in asm.js version of JS
69 /// library.
70 ///
71 /// function setThrew(threw, value) {
72 /// if (__THREW__ == 0) {
73 /// __THREW__ = threw;
74 /// __threwValue = value;
75 /// }
76 /// }
77 //
78 /// setTempRet0 is called from __cxa_find_matching_catch() in JS glue code.
79 ///
80 /// In exception handling, getTempRet0 indicates the type of an exception
81 /// caught, and in setjmp/longjmp, it means the second argument to longjmp
82 /// function.
83 ///
84 /// 3) Lower
85 /// invoke @func(arg1, arg2) to label %invoke.cont unwind label %lpad
86 /// into
87 /// __THREW__ = 0;
88 /// call @__invoke_SIG(func, arg1, arg2)
89 /// %__THREW__.val = __THREW__;
90 /// __THREW__ = 0;
91 /// if (%__THREW__.val == 1)
92 /// goto %lpad
93 /// else
94 /// goto %invoke.cont
95 /// SIG is a mangled string generated based on the LLVM IR-level function
96 /// signature. After LLVM IR types are lowered to the target wasm types,
97 /// the names for these wrappers will change based on wasm types as well,
98 /// as in invoke_vi (function takes an int and returns void). The bodies of
99 /// these wrappers will be generated in JS glue code, and inside those
100 /// wrappers we use JS try-catch to generate actual exception effects. It
101 /// also calls the original callee function. An example wrapper in JS code
102 /// would look like this:
103 /// function invoke_vi(index,a1) {
104 /// try {
105 /// Module["dynCall_vi"](index,a1); // This calls original callee
106 /// } catch(e) {
107 /// if (typeof e !== 'number' && e !== 'longjmp') throw e;
108 /// asm["setThrew"](1, 0); // setThrew is called here
109 /// }
110 /// }
111 /// If an exception is thrown, __THREW__ will be set to true in a wrapper,
112 /// so we can jump to the right BB based on this value.
113 ///
114 /// 4) Lower
115 /// %val = landingpad catch c1 catch c2 catch c3 ...
116 /// ... use %val ...
117 /// into
118 /// %fmc = call @__cxa_find_matching_catch_N(c1, c2, c3, ...)
119 /// %val = {%fmc, getTempRet0()}
120 /// ... use %val ...
121 /// Here N is a number calculated based on the number of clauses.
122 /// setTempRet0 is called from __cxa_find_matching_catch() in JS glue code.
123 ///
124 /// 5) Lower
125 /// resume {%a, %b}
126 /// into
127 /// call @__resumeException(%a)
128 /// where __resumeException() is a function in JS glue code.
129 ///
130 /// 6) Lower
131 /// call @llvm.eh.typeid.for(type) (intrinsic)
132 /// into
133 /// call @llvm_eh_typeid_for(type)
134 /// llvm_eh_typeid_for function will be generated in JS glue code.
135 ///
136 /// * Setjmp / Longjmp handling
137 ///
138 /// In case calls to longjmp() exists
139 ///
140 /// 1) Lower
141 /// longjmp(buf, value)
142 /// into
143 /// emscripten_longjmp_jmpbuf(buf, value)
144 /// emscripten_longjmp_jmpbuf will be lowered to emscripten_longjmp later.
145 ///
146 /// In case calls to setjmp() exists
147 ///
148 /// 2) In the function entry that calls setjmp, initialize setjmpTable and
149 /// sejmpTableSize as follows:
150 /// setjmpTableSize = 4;
151 /// setjmpTable = (int *) malloc(40);
152 /// setjmpTable[0] = 0;
153 /// setjmpTable and setjmpTableSize are used in saveSetjmp() function in JS
154 /// code.
155 ///
156 /// 3) Lower
157 /// setjmp(buf)
158 /// into
159 /// setjmpTable = saveSetjmp(buf, label, setjmpTable, setjmpTableSize);
160 /// setjmpTableSize = getTempRet0();
161 /// For each dynamic setjmp call, setjmpTable stores its ID (a number which
162 /// is incrementally assigned from 0) and its label (a unique number that
163 /// represents each callsite of setjmp). When we need more entries in
164 /// setjmpTable, it is reallocated in saveSetjmp() in JS code and it will
165 /// return the new table address, and assign the new table size in
166 /// setTempRet0(). saveSetjmp also stores the setjmp's ID into the buffer
167 /// buf. A BB with setjmp is split into two after setjmp call in order to
168 /// make the post-setjmp BB the possible destination of longjmp BB.
169 ///
170 ///
171 /// 4) Lower every call that might longjmp into
172 /// __THREW__ = 0;
173 /// call @__invoke_SIG(func, arg1, arg2)
174 /// %__THREW__.val = __THREW__;
175 /// __THREW__ = 0;
176 /// if (%__THREW__.val != 0 & __threwValue != 0) {
177 /// %label = testSetjmp(mem[%__THREW__.val], setjmpTable,
178 /// setjmpTableSize);
179 /// if (%label == 0)
180 /// emscripten_longjmp(%__THREW__.val, __threwValue);
181 /// setTempRet0(__threwValue);
182 /// } else {
183 /// %label = -1;
184 /// }
185 /// longjmp_result = getTempRet0();
186 /// switch label {
187 /// label 1: goto post-setjmp BB 1
188 /// label 2: goto post-setjmp BB 2
189 /// ...
190 /// default: goto splitted next BB
191 /// }
192 /// testSetjmp examines setjmpTable to see if there is a matching setjmp
193 /// call. After calling an invoke wrapper, if a longjmp occurred, __THREW__
194 /// will be the address of matching jmp_buf buffer and __threwValue be the
195 /// second argument to longjmp. mem[__THREW__.val] is a setjmp ID that is
196 /// stored in saveSetjmp. testSetjmp returns a setjmp label, a unique ID to
197 /// each setjmp callsite. Label 0 means this longjmp buffer does not
198 /// correspond to one of the setjmp callsites in this function, so in this
199 /// case we just chain the longjmp to the caller. (Here we call
200 /// emscripten_longjmp, which is different from emscripten_longjmp_jmpbuf.
201 /// emscripten_longjmp_jmpbuf takes jmp_buf as its first argument, while
202 /// emscripten_longjmp takes an int. Both of them will eventually be lowered
203 /// to emscripten_longjmp in s2wasm, but here we need two signatures - we
204 /// can't translate an int value to a jmp_buf.)
205 /// Label -1 means no longjmp occurred. Otherwise we jump to the right
206 /// post-setjmp BB based on the label.
207 ///
208 ///===----------------------------------------------------------------------===//
209
210 #include "WebAssembly.h"
211 #include "llvm/IR/CallSite.h"
212 #include "llvm/IR/Dominators.h"
213 #include "llvm/IR/IRBuilder.h"
214 #include "llvm/Support/CommandLine.h"
215 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
216 #include "llvm/Transforms/Utils/SSAUpdater.h"
217
218 using namespace llvm;
219
220 #define DEBUG_TYPE "wasm-lower-em-ehsjlj"
221
222 static cl::list<std::string>
223 EHWhitelist("emscripten-cxx-exceptions-whitelist",
224 cl::desc("The list of function names in which Emscripten-style "
225 "exception handling is enabled (see emscripten "
226 "EMSCRIPTEN_CATCHING_WHITELIST options)"),
227 cl::CommaSeparated);
228
229 namespace {
230 class WebAssemblyLowerEmscriptenEHSjLj final : public ModulePass {
231 bool EnableEH; // Enable exception handling
232 bool EnableSjLj; // Enable setjmp/longjmp handling
233
234 GlobalVariable *ThrewGV = nullptr;
235 GlobalVariable *ThrewValueGV = nullptr;
236 Function *GetTempRet0Func = nullptr;
237 Function *SetTempRet0Func = nullptr;
238 Function *ResumeF = nullptr;
239 Function *EHTypeIDF = nullptr;
240 Function *EmLongjmpF = nullptr;
241 Function *EmLongjmpJmpbufF = nullptr;
242 Function *SaveSetjmpF = nullptr;
243 Function *TestSetjmpF = nullptr;
244
245 // __cxa_find_matching_catch_N functions.
246 // Indexed by the number of clauses in an original landingpad instruction.
247 DenseMap<int, Function *> FindMatchingCatches;
248 // Map of <function signature string, invoke_ wrappers>
249 StringMap<Function *> InvokeWrappers;
250 // Set of whitelisted function names for exception handling
251 std::set<std::string> EHWhitelistSet;
252
getPassName() const253 StringRef getPassName() const override {
254 return "WebAssembly Lower Emscripten Exceptions";
255 }
256
257 bool runEHOnFunction(Function &F);
258 bool runSjLjOnFunction(Function &F);
259 Function *getFindMatchingCatch(Module &M, unsigned NumClauses);
260
261 template <typename CallOrInvoke> Value *wrapInvoke(CallOrInvoke *CI);
262 void wrapTestSetjmp(BasicBlock *BB, Instruction *InsertPt, Value *Threw,
263 Value *SetjmpTable, Value *SetjmpTableSize, Value *&Label,
264 Value *&LongjmpResult, BasicBlock *&EndBB);
265 template <typename CallOrInvoke> Function *getInvokeWrapper(CallOrInvoke *CI);
266
areAllExceptionsAllowed() const267 bool areAllExceptionsAllowed() const { return EHWhitelistSet.empty(); }
268 bool canLongjmp(Module &M, const Value *Callee) const;
269 bool isEmAsmCall(Module &M, const Value *Callee) const;
270
271 void rebuildSSA(Function &F);
272
273 public:
274 static char ID;
275
WebAssemblyLowerEmscriptenEHSjLj(bool EnableEH=true,bool EnableSjLj=true)276 WebAssemblyLowerEmscriptenEHSjLj(bool EnableEH = true, bool EnableSjLj = true)
277 : ModulePass(ID), EnableEH(EnableEH), EnableSjLj(EnableSjLj) {
278 EHWhitelistSet.insert(EHWhitelist.begin(), EHWhitelist.end());
279 }
280 bool runOnModule(Module &M) override;
281
getAnalysisUsage(AnalysisUsage & AU) const282 void getAnalysisUsage(AnalysisUsage &AU) const override {
283 AU.addRequired<DominatorTreeWrapperPass>();
284 }
285 };
286 } // End anonymous namespace
287
288 char WebAssemblyLowerEmscriptenEHSjLj::ID = 0;
289 INITIALIZE_PASS(WebAssemblyLowerEmscriptenEHSjLj, DEBUG_TYPE,
290 "WebAssembly Lower Emscripten Exceptions / Setjmp / Longjmp",
291 false, false)
292
createWebAssemblyLowerEmscriptenEHSjLj(bool EnableEH,bool EnableSjLj)293 ModulePass *llvm::createWebAssemblyLowerEmscriptenEHSjLj(bool EnableEH,
294 bool EnableSjLj) {
295 return new WebAssemblyLowerEmscriptenEHSjLj(EnableEH, EnableSjLj);
296 }
297
canThrow(const Value * V)298 static bool canThrow(const Value *V) {
299 if (const auto *F = dyn_cast<const Function>(V)) {
300 // Intrinsics cannot throw
301 if (F->isIntrinsic())
302 return false;
303 StringRef Name = F->getName();
304 // leave setjmp and longjmp (mostly) alone, we process them properly later
305 if (Name == "setjmp" || Name == "longjmp")
306 return false;
307 return !F->doesNotThrow();
308 }
309 // not a function, so an indirect call - can throw, we can't tell
310 return true;
311 }
312
313 // Get a global variable with the given name. If it doesn't exist declare it,
314 // which will generate an import and asssumes that it will exist at link time.
getGlobalVariableI32(Module & M,IRBuilder<> & IRB,const char * Name)315 static GlobalVariable *getGlobalVariableI32(Module &M, IRBuilder<> &IRB,
316 const char *Name) {
317
318 auto *GV =
319 dyn_cast<GlobalVariable>(M.getOrInsertGlobal(Name, IRB.getInt32Ty()));
320 if (!GV)
321 report_fatal_error(Twine("unable to create global: ") + Name);
322
323 return GV;
324 }
325
326 // Simple function name mangler.
327 // This function simply takes LLVM's string representation of parameter types
328 // and concatenate them with '_'. There are non-alphanumeric characters but llc
329 // is ok with it, and we need to postprocess these names after the lowering
330 // phase anyway.
getSignature(FunctionType * FTy)331 static std::string getSignature(FunctionType *FTy) {
332 std::string Sig;
333 raw_string_ostream OS(Sig);
334 OS << *FTy->getReturnType();
335 for (Type *ParamTy : FTy->params())
336 OS << "_" << *ParamTy;
337 if (FTy->isVarArg())
338 OS << "_...";
339 Sig = OS.str();
340 Sig.erase(remove_if(Sig, isspace), Sig.end());
341 // When s2wasm parses .s file, a comma means the end of an argument. So a
342 // mangled function name can contain any character but a comma.
343 std::replace(Sig.begin(), Sig.end(), ',', '.');
344 return Sig;
345 }
346
347 // Returns __cxa_find_matching_catch_N function, where N = NumClauses + 2.
348 // This is because a landingpad instruction contains two more arguments, a
349 // personality function and a cleanup bit, and __cxa_find_matching_catch_N
350 // functions are named after the number of arguments in the original landingpad
351 // instruction.
352 Function *
getFindMatchingCatch(Module & M,unsigned NumClauses)353 WebAssemblyLowerEmscriptenEHSjLj::getFindMatchingCatch(Module &M,
354 unsigned NumClauses) {
355 if (FindMatchingCatches.count(NumClauses))
356 return FindMatchingCatches[NumClauses];
357 PointerType *Int8PtrTy = Type::getInt8PtrTy(M.getContext());
358 SmallVector<Type *, 16> Args(NumClauses, Int8PtrTy);
359 FunctionType *FTy = FunctionType::get(Int8PtrTy, Args, false);
360 Function *F = Function::Create(
361 FTy, GlobalValue::ExternalLinkage,
362 "__cxa_find_matching_catch_" + Twine(NumClauses + 2), &M);
363 FindMatchingCatches[NumClauses] = F;
364 return F;
365 }
366
367 // Generate invoke wrapper seqence with preamble and postamble
368 // Preamble:
369 // __THREW__ = 0;
370 // Postamble:
371 // %__THREW__.val = __THREW__; __THREW__ = 0;
372 // Returns %__THREW__.val, which indicates whether an exception is thrown (or
373 // whether longjmp occurred), for future use.
374 template <typename CallOrInvoke>
wrapInvoke(CallOrInvoke * CI)375 Value *WebAssemblyLowerEmscriptenEHSjLj::wrapInvoke(CallOrInvoke *CI) {
376 LLVMContext &C = CI->getModule()->getContext();
377
378 // If we are calling a function that is noreturn, we must remove that
379 // attribute. The code we insert here does expect it to return, after we
380 // catch the exception.
381 if (CI->doesNotReturn()) {
382 if (auto *F = dyn_cast<Function>(CI->getCalledValue()))
383 F->removeFnAttr(Attribute::NoReturn);
384 CI->removeAttribute(AttributeList::FunctionIndex, Attribute::NoReturn);
385 }
386
387 IRBuilder<> IRB(C);
388 IRB.SetInsertPoint(CI);
389
390 // Pre-invoke
391 // __THREW__ = 0;
392 IRB.CreateStore(IRB.getInt32(0), ThrewGV);
393
394 // Invoke function wrapper in JavaScript
395 SmallVector<Value *, 16> Args;
396 // Put the pointer to the callee as first argument, so it can be called
397 // within the invoke wrapper later
398 Args.push_back(CI->getCalledValue());
399 Args.append(CI->arg_begin(), CI->arg_end());
400 CallInst *NewCall = IRB.CreateCall(getInvokeWrapper(CI), Args);
401 NewCall->takeName(CI);
402 NewCall->setCallingConv(CallingConv::WASM_EmscriptenInvoke);
403 NewCall->setDebugLoc(CI->getDebugLoc());
404
405 // Because we added the pointer to the callee as first argument, all
406 // argument attribute indices have to be incremented by one.
407 SmallVector<AttributeSet, 8> ArgAttributes;
408 const AttributeList &InvokeAL = CI->getAttributes();
409
410 // No attributes for the callee pointer.
411 ArgAttributes.push_back(AttributeSet());
412 // Copy the argument attributes from the original
413 for (unsigned I = 0, E = CI->getNumArgOperands(); I < E; ++I)
414 ArgAttributes.push_back(InvokeAL.getParamAttributes(I));
415
416 AttrBuilder FnAttrs(InvokeAL.getFnAttributes());
417 if (FnAttrs.contains(Attribute::AllocSize)) {
418 // The allocsize attribute (if any) referes to parameters by index and needs
419 // to be adjusted.
420 unsigned SizeArg;
421 Optional<unsigned> NEltArg;
422 std::tie(SizeArg, NEltArg) = FnAttrs.getAllocSizeArgs();
423 SizeArg += 1;
424 if (NEltArg.hasValue())
425 NEltArg = NEltArg.getValue() + 1;
426 FnAttrs.addAllocSizeAttr(SizeArg, NEltArg);
427 }
428
429 // Reconstruct the AttributesList based on the vector we constructed.
430 AttributeList NewCallAL =
431 AttributeList::get(C, AttributeSet::get(C, FnAttrs),
432 InvokeAL.getRetAttributes(), ArgAttributes);
433 NewCall->setAttributes(NewCallAL);
434
435 CI->replaceAllUsesWith(NewCall);
436
437 // Post-invoke
438 // %__THREW__.val = __THREW__; __THREW__ = 0;
439 Value *Threw =
440 IRB.CreateLoad(IRB.getInt32Ty(), ThrewGV, ThrewGV->getName() + ".val");
441 IRB.CreateStore(IRB.getInt32(0), ThrewGV);
442 return Threw;
443 }
444
445 // Get matching invoke wrapper based on callee signature
446 template <typename CallOrInvoke>
getInvokeWrapper(CallOrInvoke * CI)447 Function *WebAssemblyLowerEmscriptenEHSjLj::getInvokeWrapper(CallOrInvoke *CI) {
448 Module *M = CI->getModule();
449 SmallVector<Type *, 16> ArgTys;
450 Value *Callee = CI->getCalledValue();
451 FunctionType *CalleeFTy;
452 if (auto *F = dyn_cast<Function>(Callee))
453 CalleeFTy = F->getFunctionType();
454 else {
455 auto *CalleeTy = cast<PointerType>(Callee->getType())->getElementType();
456 CalleeFTy = cast<FunctionType>(CalleeTy);
457 }
458
459 std::string Sig = getSignature(CalleeFTy);
460 if (InvokeWrappers.find(Sig) != InvokeWrappers.end())
461 return InvokeWrappers[Sig];
462
463 // Put the pointer to the callee as first argument
464 ArgTys.push_back(PointerType::getUnqual(CalleeFTy));
465 // Add argument types
466 ArgTys.append(CalleeFTy->param_begin(), CalleeFTy->param_end());
467
468 FunctionType *FTy = FunctionType::get(CalleeFTy->getReturnType(), ArgTys,
469 CalleeFTy->isVarArg());
470 Function *F =
471 Function::Create(FTy, GlobalValue::ExternalLinkage, "__invoke_" + Sig, M);
472 InvokeWrappers[Sig] = F;
473 return F;
474 }
475
canLongjmp(Module & M,const Value * Callee) const476 bool WebAssemblyLowerEmscriptenEHSjLj::canLongjmp(Module &M,
477 const Value *Callee) const {
478 if (auto *CalleeF = dyn_cast<Function>(Callee))
479 if (CalleeF->isIntrinsic())
480 return false;
481
482 // Attempting to transform inline assembly will result in something like:
483 // call void @__invoke_void(void ()* asm ...)
484 // which is invalid because inline assembly blocks do not have addresses
485 // and can't be passed by pointer. The result is a crash with illegal IR.
486 if (isa<InlineAsm>(Callee))
487 return false;
488 StringRef CalleeName = Callee->getName();
489
490 // The reason we include malloc/free here is to exclude the malloc/free
491 // calls generated in setjmp prep / cleanup routines.
492 if (CalleeName == "setjmp" || CalleeName == "malloc" || CalleeName == "free")
493 return false;
494
495 // There are functions in JS glue code
496 if (CalleeName == "__resumeException" || CalleeName == "llvm_eh_typeid_for" ||
497 CalleeName == "saveSetjmp" || CalleeName == "testSetjmp" ||
498 CalleeName == "getTempRet0" || CalleeName == "setTempRet0")
499 return false;
500
501 // __cxa_find_matching_catch_N functions cannot longjmp
502 if (Callee->getName().startswith("__cxa_find_matching_catch_"))
503 return false;
504
505 // Exception-catching related functions
506 if (CalleeName == "__cxa_begin_catch" || CalleeName == "__cxa_end_catch" ||
507 CalleeName == "__cxa_allocate_exception" || CalleeName == "__cxa_throw" ||
508 CalleeName == "__clang_call_terminate")
509 return false;
510
511 // Otherwise we don't know
512 return true;
513 }
514
isEmAsmCall(Module & M,const Value * Callee) const515 bool WebAssemblyLowerEmscriptenEHSjLj::isEmAsmCall(Module &M,
516 const Value *Callee) const {
517 StringRef CalleeName = Callee->getName();
518 // This is an exhaustive list from Emscripten's <emscripten/em_asm.h>.
519 return CalleeName == "emscripten_asm_const_int" ||
520 CalleeName == "emscripten_asm_const_double" ||
521 CalleeName == "emscripten_asm_const_int_sync_on_main_thread" ||
522 CalleeName == "emscripten_asm_const_double_sync_on_main_thread" ||
523 CalleeName == "emscripten_asm_const_async_on_main_thread";
524 }
525
526 // Generate testSetjmp function call seqence with preamble and postamble.
527 // The code this generates is equivalent to the following JavaScript code:
528 // if (%__THREW__.val != 0 & threwValue != 0) {
529 // %label = _testSetjmp(mem[%__THREW__.val], setjmpTable, setjmpTableSize);
530 // if (%label == 0)
531 // emscripten_longjmp(%__THREW__.val, threwValue);
532 // setTempRet0(threwValue);
533 // } else {
534 // %label = -1;
535 // }
536 // %longjmp_result = getTempRet0();
537 //
538 // As output parameters. returns %label, %longjmp_result, and the BB the last
539 // instruction (%longjmp_result = ...) is in.
wrapTestSetjmp(BasicBlock * BB,Instruction * InsertPt,Value * Threw,Value * SetjmpTable,Value * SetjmpTableSize,Value * & Label,Value * & LongjmpResult,BasicBlock * & EndBB)540 void WebAssemblyLowerEmscriptenEHSjLj::wrapTestSetjmp(
541 BasicBlock *BB, Instruction *InsertPt, Value *Threw, Value *SetjmpTable,
542 Value *SetjmpTableSize, Value *&Label, Value *&LongjmpResult,
543 BasicBlock *&EndBB) {
544 Function *F = BB->getParent();
545 LLVMContext &C = BB->getModule()->getContext();
546 IRBuilder<> IRB(C);
547 IRB.SetInsertPoint(InsertPt);
548
549 // if (%__THREW__.val != 0 & threwValue != 0)
550 IRB.SetInsertPoint(BB);
551 BasicBlock *ThenBB1 = BasicBlock::Create(C, "if.then1", F);
552 BasicBlock *ElseBB1 = BasicBlock::Create(C, "if.else1", F);
553 BasicBlock *EndBB1 = BasicBlock::Create(C, "if.end", F);
554 Value *ThrewCmp = IRB.CreateICmpNE(Threw, IRB.getInt32(0));
555 Value *ThrewValue = IRB.CreateLoad(IRB.getInt32Ty(), ThrewValueGV,
556 ThrewValueGV->getName() + ".val");
557 Value *ThrewValueCmp = IRB.CreateICmpNE(ThrewValue, IRB.getInt32(0));
558 Value *Cmp1 = IRB.CreateAnd(ThrewCmp, ThrewValueCmp, "cmp1");
559 IRB.CreateCondBr(Cmp1, ThenBB1, ElseBB1);
560
561 // %label = _testSetjmp(mem[%__THREW__.val], _setjmpTable, _setjmpTableSize);
562 // if (%label == 0)
563 IRB.SetInsertPoint(ThenBB1);
564 BasicBlock *ThenBB2 = BasicBlock::Create(C, "if.then2", F);
565 BasicBlock *EndBB2 = BasicBlock::Create(C, "if.end2", F);
566 Value *ThrewInt = IRB.CreateIntToPtr(Threw, Type::getInt32PtrTy(C),
567 Threw->getName() + ".i32p");
568 Value *LoadedThrew = IRB.CreateLoad(IRB.getInt32Ty(), ThrewInt,
569 ThrewInt->getName() + ".loaded");
570 Value *ThenLabel = IRB.CreateCall(
571 TestSetjmpF, {LoadedThrew, SetjmpTable, SetjmpTableSize}, "label");
572 Value *Cmp2 = IRB.CreateICmpEQ(ThenLabel, IRB.getInt32(0));
573 IRB.CreateCondBr(Cmp2, ThenBB2, EndBB2);
574
575 // emscripten_longjmp(%__THREW__.val, threwValue);
576 IRB.SetInsertPoint(ThenBB2);
577 IRB.CreateCall(EmLongjmpF, {Threw, ThrewValue});
578 IRB.CreateUnreachable();
579
580 // setTempRet0(threwValue);
581 IRB.SetInsertPoint(EndBB2);
582 IRB.CreateCall(SetTempRet0Func, ThrewValue);
583 IRB.CreateBr(EndBB1);
584
585 IRB.SetInsertPoint(ElseBB1);
586 IRB.CreateBr(EndBB1);
587
588 // longjmp_result = getTempRet0();
589 IRB.SetInsertPoint(EndBB1);
590 PHINode *LabelPHI = IRB.CreatePHI(IRB.getInt32Ty(), 2, "label");
591 LabelPHI->addIncoming(ThenLabel, EndBB2);
592
593 LabelPHI->addIncoming(IRB.getInt32(-1), ElseBB1);
594
595 // Output parameter assignment
596 Label = LabelPHI;
597 EndBB = EndBB1;
598 LongjmpResult = IRB.CreateCall(GetTempRet0Func, None, "longjmp_result");
599 }
600
rebuildSSA(Function & F)601 void WebAssemblyLowerEmscriptenEHSjLj::rebuildSSA(Function &F) {
602 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
603 DT.recalculate(F); // CFG has been changed
604 SSAUpdater SSA;
605 for (BasicBlock &BB : F) {
606 for (Instruction &I : BB) {
607 SSA.Initialize(I.getType(), I.getName());
608 SSA.AddAvailableValue(&BB, &I);
609 for (auto UI = I.use_begin(), UE = I.use_end(); UI != UE;) {
610 Use &U = *UI;
611 ++UI;
612 auto *User = cast<Instruction>(U.getUser());
613 if (auto *UserPN = dyn_cast<PHINode>(User))
614 if (UserPN->getIncomingBlock(U) == &BB)
615 continue;
616
617 if (DT.dominates(&I, User))
618 continue;
619 SSA.RewriteUseAfterInsertions(U);
620 }
621 }
622 }
623 }
624
runOnModule(Module & M)625 bool WebAssemblyLowerEmscriptenEHSjLj::runOnModule(Module &M) {
626 LLVM_DEBUG(dbgs() << "********** Lower Emscripten EH & SjLj **********\n");
627
628 LLVMContext &C = M.getContext();
629 IRBuilder<> IRB(C);
630
631 Function *SetjmpF = M.getFunction("setjmp");
632 Function *LongjmpF = M.getFunction("longjmp");
633 bool SetjmpUsed = SetjmpF && !SetjmpF->use_empty();
634 bool LongjmpUsed = LongjmpF && !LongjmpF->use_empty();
635 bool DoSjLj = EnableSjLj && (SetjmpUsed || LongjmpUsed);
636
637 // Declare (or get) global variables __THREW__, __threwValue, and
638 // getTempRet0/setTempRet0 function which are used in common for both
639 // exception handling and setjmp/longjmp handling
640 ThrewGV = getGlobalVariableI32(M, IRB, "__THREW__");
641 ThrewValueGV = getGlobalVariableI32(M, IRB, "__threwValue");
642 GetTempRet0Func =
643 Function::Create(FunctionType::get(IRB.getInt32Ty(), false),
644 GlobalValue::ExternalLinkage, "getTempRet0", &M);
645 SetTempRet0Func = Function::Create(
646 FunctionType::get(IRB.getVoidTy(), IRB.getInt32Ty(), false),
647 GlobalValue::ExternalLinkage, "setTempRet0", &M);
648 GetTempRet0Func->setDoesNotThrow();
649 SetTempRet0Func->setDoesNotThrow();
650
651 bool Changed = false;
652
653 // Exception handling
654 if (EnableEH) {
655 // Register __resumeException function
656 FunctionType *ResumeFTy =
657 FunctionType::get(IRB.getVoidTy(), IRB.getInt8PtrTy(), false);
658 ResumeF = Function::Create(ResumeFTy, GlobalValue::ExternalLinkage,
659 "__resumeException", &M);
660
661 // Register llvm_eh_typeid_for function
662 FunctionType *EHTypeIDTy =
663 FunctionType::get(IRB.getInt32Ty(), IRB.getInt8PtrTy(), false);
664 EHTypeIDF = Function::Create(EHTypeIDTy, GlobalValue::ExternalLinkage,
665 "llvm_eh_typeid_for", &M);
666
667 for (Function &F : M) {
668 if (F.isDeclaration())
669 continue;
670 Changed |= runEHOnFunction(F);
671 }
672 }
673
674 // Setjmp/longjmp handling
675 if (DoSjLj) {
676 Changed = true; // We have setjmp or longjmp somewhere
677
678 if (LongjmpF) {
679 // Replace all uses of longjmp with emscripten_longjmp_jmpbuf, which is
680 // defined in JS code
681 EmLongjmpJmpbufF = Function::Create(LongjmpF->getFunctionType(),
682 GlobalValue::ExternalLinkage,
683 "emscripten_longjmp_jmpbuf", &M);
684
685 LongjmpF->replaceAllUsesWith(EmLongjmpJmpbufF);
686 }
687
688 if (SetjmpF) {
689 // Register saveSetjmp function
690 FunctionType *SetjmpFTy = SetjmpF->getFunctionType();
691 SmallVector<Type *, 4> Params = {SetjmpFTy->getParamType(0),
692 IRB.getInt32Ty(), Type::getInt32PtrTy(C),
693 IRB.getInt32Ty()};
694 FunctionType *FTy =
695 FunctionType::get(Type::getInt32PtrTy(C), Params, false);
696 SaveSetjmpF =
697 Function::Create(FTy, GlobalValue::ExternalLinkage, "saveSetjmp", &M);
698
699 // Register testSetjmp function
700 Params = {IRB.getInt32Ty(), Type::getInt32PtrTy(C), IRB.getInt32Ty()};
701 FTy = FunctionType::get(IRB.getInt32Ty(), Params, false);
702 TestSetjmpF =
703 Function::Create(FTy, GlobalValue::ExternalLinkage, "testSetjmp", &M);
704
705 FTy = FunctionType::get(IRB.getVoidTy(),
706 {IRB.getInt32Ty(), IRB.getInt32Ty()}, false);
707 EmLongjmpF = Function::Create(FTy, GlobalValue::ExternalLinkage,
708 "emscripten_longjmp", &M);
709
710 // Only traverse functions that uses setjmp in order not to insert
711 // unnecessary prep / cleanup code in every function
712 SmallPtrSet<Function *, 8> SetjmpUsers;
713 for (User *U : SetjmpF->users()) {
714 auto *UI = cast<Instruction>(U);
715 SetjmpUsers.insert(UI->getFunction());
716 }
717 for (Function *F : SetjmpUsers)
718 runSjLjOnFunction(*F);
719 }
720 }
721
722 if (!Changed) {
723 // Delete unused global variables and functions
724 if (ResumeF)
725 ResumeF->eraseFromParent();
726 if (EHTypeIDF)
727 EHTypeIDF->eraseFromParent();
728 if (EmLongjmpF)
729 EmLongjmpF->eraseFromParent();
730 if (SaveSetjmpF)
731 SaveSetjmpF->eraseFromParent();
732 if (TestSetjmpF)
733 TestSetjmpF->eraseFromParent();
734 return false;
735 }
736
737 return true;
738 }
739
runEHOnFunction(Function & F)740 bool WebAssemblyLowerEmscriptenEHSjLj::runEHOnFunction(Function &F) {
741 Module &M = *F.getParent();
742 LLVMContext &C = F.getContext();
743 IRBuilder<> IRB(C);
744 bool Changed = false;
745 SmallVector<Instruction *, 64> ToErase;
746 SmallPtrSet<LandingPadInst *, 32> LandingPads;
747 bool AllowExceptions =
748 areAllExceptionsAllowed() || EHWhitelistSet.count(F.getName());
749
750 for (BasicBlock &BB : F) {
751 auto *II = dyn_cast<InvokeInst>(BB.getTerminator());
752 if (!II)
753 continue;
754 Changed = true;
755 LandingPads.insert(II->getLandingPadInst());
756 IRB.SetInsertPoint(II);
757
758 bool NeedInvoke = AllowExceptions && canThrow(II->getCalledValue());
759 if (NeedInvoke) {
760 // Wrap invoke with invoke wrapper and generate preamble/postamble
761 Value *Threw = wrapInvoke(II);
762 ToErase.push_back(II);
763
764 // Insert a branch based on __THREW__ variable
765 Value *Cmp = IRB.CreateICmpEQ(Threw, IRB.getInt32(1), "cmp");
766 IRB.CreateCondBr(Cmp, II->getUnwindDest(), II->getNormalDest());
767
768 } else {
769 // This can't throw, and we don't need this invoke, just replace it with a
770 // call+branch
771 SmallVector<Value *, 16> Args(II->arg_begin(), II->arg_end());
772 CallInst *NewCall =
773 IRB.CreateCall(II->getFunctionType(), II->getCalledValue(), Args);
774 NewCall->takeName(II);
775 NewCall->setCallingConv(II->getCallingConv());
776 NewCall->setDebugLoc(II->getDebugLoc());
777 NewCall->setAttributes(II->getAttributes());
778 II->replaceAllUsesWith(NewCall);
779 ToErase.push_back(II);
780
781 IRB.CreateBr(II->getNormalDest());
782
783 // Remove any PHI node entries from the exception destination
784 II->getUnwindDest()->removePredecessor(&BB);
785 }
786 }
787
788 // Process resume instructions
789 for (BasicBlock &BB : F) {
790 // Scan the body of the basic block for resumes
791 for (Instruction &I : BB) {
792 auto *RI = dyn_cast<ResumeInst>(&I);
793 if (!RI)
794 continue;
795 Changed = true;
796
797 // Split the input into legal values
798 Value *Input = RI->getValue();
799 IRB.SetInsertPoint(RI);
800 Value *Low = IRB.CreateExtractValue(Input, 0, "low");
801 // Create a call to __resumeException function
802 IRB.CreateCall(ResumeF, {Low});
803 // Add a terminator to the block
804 IRB.CreateUnreachable();
805 ToErase.push_back(RI);
806 }
807 }
808
809 // Process llvm.eh.typeid.for intrinsics
810 for (BasicBlock &BB : F) {
811 for (Instruction &I : BB) {
812 auto *CI = dyn_cast<CallInst>(&I);
813 if (!CI)
814 continue;
815 const Function *Callee = CI->getCalledFunction();
816 if (!Callee)
817 continue;
818 if (Callee->getIntrinsicID() != Intrinsic::eh_typeid_for)
819 continue;
820 Changed = true;
821
822 IRB.SetInsertPoint(CI);
823 CallInst *NewCI =
824 IRB.CreateCall(EHTypeIDF, CI->getArgOperand(0), "typeid");
825 CI->replaceAllUsesWith(NewCI);
826 ToErase.push_back(CI);
827 }
828 }
829
830 // Look for orphan landingpads, can occur in blocks with no predecessors
831 for (BasicBlock &BB : F) {
832 Instruction *I = BB.getFirstNonPHI();
833 if (auto *LPI = dyn_cast<LandingPadInst>(I))
834 LandingPads.insert(LPI);
835 }
836 Changed |= !LandingPads.empty();
837
838 // Handle all the landingpad for this function together, as multiple invokes
839 // may share a single lp
840 for (LandingPadInst *LPI : LandingPads) {
841 IRB.SetInsertPoint(LPI);
842 SmallVector<Value *, 16> FMCArgs;
843 for (unsigned I = 0, E = LPI->getNumClauses(); I < E; ++I) {
844 Constant *Clause = LPI->getClause(I);
845 // As a temporary workaround for the lack of aggregate varargs support
846 // in the interface between JS and wasm, break out filter operands into
847 // their component elements.
848 if (LPI->isFilter(I)) {
849 auto *ATy = cast<ArrayType>(Clause->getType());
850 for (unsigned J = 0, E = ATy->getNumElements(); J < E; ++J) {
851 Value *EV = IRB.CreateExtractValue(Clause, makeArrayRef(J), "filter");
852 FMCArgs.push_back(EV);
853 }
854 } else
855 FMCArgs.push_back(Clause);
856 }
857
858 // Create a call to __cxa_find_matching_catch_N function
859 Function *FMCF = getFindMatchingCatch(M, FMCArgs.size());
860 CallInst *FMCI = IRB.CreateCall(FMCF, FMCArgs, "fmc");
861 Value *Undef = UndefValue::get(LPI->getType());
862 Value *Pair0 = IRB.CreateInsertValue(Undef, FMCI, 0, "pair0");
863 Value *TempRet0 = IRB.CreateCall(GetTempRet0Func, None, "tempret0");
864 Value *Pair1 = IRB.CreateInsertValue(Pair0, TempRet0, 1, "pair1");
865
866 LPI->replaceAllUsesWith(Pair1);
867 ToErase.push_back(LPI);
868 }
869
870 // Erase everything we no longer need in this function
871 for (Instruction *I : ToErase)
872 I->eraseFromParent();
873
874 return Changed;
875 }
876
runSjLjOnFunction(Function & F)877 bool WebAssemblyLowerEmscriptenEHSjLj::runSjLjOnFunction(Function &F) {
878 Module &M = *F.getParent();
879 LLVMContext &C = F.getContext();
880 IRBuilder<> IRB(C);
881 SmallVector<Instruction *, 64> ToErase;
882 // Vector of %setjmpTable values
883 std::vector<Instruction *> SetjmpTableInsts;
884 // Vector of %setjmpTableSize values
885 std::vector<Instruction *> SetjmpTableSizeInsts;
886
887 // Setjmp preparation
888
889 // This instruction effectively means %setjmpTableSize = 4.
890 // We create this as an instruction intentionally, and we don't want to fold
891 // this instruction to a constant 4, because this value will be used in
892 // SSAUpdater.AddAvailableValue(...) later.
893 BasicBlock &EntryBB = F.getEntryBlock();
894 BinaryOperator *SetjmpTableSize = BinaryOperator::Create(
895 Instruction::Add, IRB.getInt32(4), IRB.getInt32(0), "setjmpTableSize",
896 &*EntryBB.getFirstInsertionPt());
897 // setjmpTable = (int *) malloc(40);
898 Instruction *SetjmpTable = CallInst::CreateMalloc(
899 SetjmpTableSize, IRB.getInt32Ty(), IRB.getInt32Ty(), IRB.getInt32(40),
900 nullptr, nullptr, "setjmpTable");
901 // setjmpTable[0] = 0;
902 IRB.SetInsertPoint(SetjmpTableSize);
903 IRB.CreateStore(IRB.getInt32(0), SetjmpTable);
904 SetjmpTableInsts.push_back(SetjmpTable);
905 SetjmpTableSizeInsts.push_back(SetjmpTableSize);
906
907 // Setjmp transformation
908 std::vector<PHINode *> SetjmpRetPHIs;
909 Function *SetjmpF = M.getFunction("setjmp");
910 for (User *U : SetjmpF->users()) {
911 auto *CI = dyn_cast<CallInst>(U);
912 if (!CI)
913 report_fatal_error("Does not support indirect calls to setjmp");
914
915 BasicBlock *BB = CI->getParent();
916 if (BB->getParent() != &F) // in other function
917 continue;
918
919 // The tail is everything right after the call, and will be reached once
920 // when setjmp is called, and later when longjmp returns to the setjmp
921 BasicBlock *Tail = SplitBlock(BB, CI->getNextNode());
922 // Add a phi to the tail, which will be the output of setjmp, which
923 // indicates if this is the first call or a longjmp back. The phi directly
924 // uses the right value based on where we arrive from
925 IRB.SetInsertPoint(Tail->getFirstNonPHI());
926 PHINode *SetjmpRet = IRB.CreatePHI(IRB.getInt32Ty(), 2, "setjmp.ret");
927
928 // setjmp initial call returns 0
929 SetjmpRet->addIncoming(IRB.getInt32(0), BB);
930 // The proper output is now this, not the setjmp call itself
931 CI->replaceAllUsesWith(SetjmpRet);
932 // longjmp returns to the setjmp will add themselves to this phi
933 SetjmpRetPHIs.push_back(SetjmpRet);
934
935 // Fix call target
936 // Our index in the function is our place in the array + 1 to avoid index
937 // 0, because index 0 means the longjmp is not ours to handle.
938 IRB.SetInsertPoint(CI);
939 Value *Args[] = {CI->getArgOperand(0), IRB.getInt32(SetjmpRetPHIs.size()),
940 SetjmpTable, SetjmpTableSize};
941 Instruction *NewSetjmpTable =
942 IRB.CreateCall(SaveSetjmpF, Args, "setjmpTable");
943 Instruction *NewSetjmpTableSize =
944 IRB.CreateCall(GetTempRet0Func, None, "setjmpTableSize");
945 SetjmpTableInsts.push_back(NewSetjmpTable);
946 SetjmpTableSizeInsts.push_back(NewSetjmpTableSize);
947 ToErase.push_back(CI);
948 }
949
950 // Update each call that can longjmp so it can return to a setjmp where
951 // relevant.
952
953 // Because we are creating new BBs while processing and don't want to make
954 // all these newly created BBs candidates again for longjmp processing, we
955 // first make the vector of candidate BBs.
956 std::vector<BasicBlock *> BBs;
957 for (BasicBlock &BB : F)
958 BBs.push_back(&BB);
959
960 // BBs.size() will change within the loop, so we query it every time
961 for (unsigned I = 0; I < BBs.size(); I++) {
962 BasicBlock *BB = BBs[I];
963 for (Instruction &I : *BB) {
964 assert(!isa<InvokeInst>(&I));
965 auto *CI = dyn_cast<CallInst>(&I);
966 if (!CI)
967 continue;
968
969 const Value *Callee = CI->getCalledValue();
970 if (!canLongjmp(M, Callee))
971 continue;
972 if (isEmAsmCall(M, Callee))
973 report_fatal_error("Cannot use EM_ASM* alongside setjmp/longjmp in " +
974 F.getName() +
975 ". Please consider using EM_JS, or move the "
976 "EM_ASM into another function.",
977 false);
978
979 Value *Threw = nullptr;
980 BasicBlock *Tail;
981 if (Callee->getName().startswith("__invoke_")) {
982 // If invoke wrapper has already been generated for this call in
983 // previous EH phase, search for the load instruction
984 // %__THREW__.val = __THREW__;
985 // in postamble after the invoke wrapper call
986 LoadInst *ThrewLI = nullptr;
987 StoreInst *ThrewResetSI = nullptr;
988 for (auto I = std::next(BasicBlock::iterator(CI)), IE = BB->end();
989 I != IE; ++I) {
990 if (auto *LI = dyn_cast<LoadInst>(I))
991 if (auto *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand()))
992 if (GV == ThrewGV) {
993 Threw = ThrewLI = LI;
994 break;
995 }
996 }
997 // Search for the store instruction after the load above
998 // __THREW__ = 0;
999 for (auto I = std::next(BasicBlock::iterator(ThrewLI)), IE = BB->end();
1000 I != IE; ++I) {
1001 if (auto *SI = dyn_cast<StoreInst>(I))
1002 if (auto *GV = dyn_cast<GlobalVariable>(SI->getPointerOperand()))
1003 if (GV == ThrewGV && SI->getValueOperand() == IRB.getInt32(0)) {
1004 ThrewResetSI = SI;
1005 break;
1006 }
1007 }
1008 assert(Threw && ThrewLI && "Cannot find __THREW__ load after invoke");
1009 assert(ThrewResetSI && "Cannot find __THREW__ store after invoke");
1010 Tail = SplitBlock(BB, ThrewResetSI->getNextNode());
1011
1012 } else {
1013 // Wrap call with invoke wrapper and generate preamble/postamble
1014 Threw = wrapInvoke(CI);
1015 ToErase.push_back(CI);
1016 Tail = SplitBlock(BB, CI->getNextNode());
1017 }
1018
1019 // We need to replace the terminator in Tail - SplitBlock makes BB go
1020 // straight to Tail, we need to check if a longjmp occurred, and go to the
1021 // right setjmp-tail if so
1022 ToErase.push_back(BB->getTerminator());
1023
1024 // Generate a function call to testSetjmp function and preamble/postamble
1025 // code to figure out (1) whether longjmp occurred (2) if longjmp
1026 // occurred, which setjmp it corresponds to
1027 Value *Label = nullptr;
1028 Value *LongjmpResult = nullptr;
1029 BasicBlock *EndBB = nullptr;
1030 wrapTestSetjmp(BB, CI, Threw, SetjmpTable, SetjmpTableSize, Label,
1031 LongjmpResult, EndBB);
1032 assert(Label && LongjmpResult && EndBB);
1033
1034 // Create switch instruction
1035 IRB.SetInsertPoint(EndBB);
1036 SwitchInst *SI = IRB.CreateSwitch(Label, Tail, SetjmpRetPHIs.size());
1037 // -1 means no longjmp happened, continue normally (will hit the default
1038 // switch case). 0 means a longjmp that is not ours to handle, needs a
1039 // rethrow. Otherwise the index is the same as the index in P+1 (to avoid
1040 // 0).
1041 for (unsigned I = 0; I < SetjmpRetPHIs.size(); I++) {
1042 SI->addCase(IRB.getInt32(I + 1), SetjmpRetPHIs[I]->getParent());
1043 SetjmpRetPHIs[I]->addIncoming(LongjmpResult, EndBB);
1044 }
1045
1046 // We are splitting the block here, and must continue to find other calls
1047 // in the block - which is now split. so continue to traverse in the Tail
1048 BBs.push_back(Tail);
1049 }
1050 }
1051
1052 // Erase everything we no longer need in this function
1053 for (Instruction *I : ToErase)
1054 I->eraseFromParent();
1055
1056 // Free setjmpTable buffer before each return instruction
1057 for (BasicBlock &BB : F) {
1058 Instruction *TI = BB.getTerminator();
1059 if (isa<ReturnInst>(TI))
1060 CallInst::CreateFree(SetjmpTable, TI);
1061 }
1062
1063 // Every call to saveSetjmp can change setjmpTable and setjmpTableSize
1064 // (when buffer reallocation occurs)
1065 // entry:
1066 // setjmpTableSize = 4;
1067 // setjmpTable = (int *) malloc(40);
1068 // setjmpTable[0] = 0;
1069 // ...
1070 // somebb:
1071 // setjmpTable = saveSetjmp(buf, label, setjmpTable, setjmpTableSize);
1072 // setjmpTableSize = getTempRet0();
1073 // So we need to make sure the SSA for these variables is valid so that every
1074 // saveSetjmp and testSetjmp calls have the correct arguments.
1075 SSAUpdater SetjmpTableSSA;
1076 SSAUpdater SetjmpTableSizeSSA;
1077 SetjmpTableSSA.Initialize(Type::getInt32PtrTy(C), "setjmpTable");
1078 SetjmpTableSizeSSA.Initialize(Type::getInt32Ty(C), "setjmpTableSize");
1079 for (Instruction *I : SetjmpTableInsts)
1080 SetjmpTableSSA.AddAvailableValue(I->getParent(), I);
1081 for (Instruction *I : SetjmpTableSizeInsts)
1082 SetjmpTableSizeSSA.AddAvailableValue(I->getParent(), I);
1083
1084 for (auto UI = SetjmpTable->use_begin(), UE = SetjmpTable->use_end();
1085 UI != UE;) {
1086 // Grab the use before incrementing the iterator.
1087 Use &U = *UI;
1088 // Increment the iterator before removing the use from the list.
1089 ++UI;
1090 if (auto *I = dyn_cast<Instruction>(U.getUser()))
1091 if (I->getParent() != &EntryBB)
1092 SetjmpTableSSA.RewriteUse(U);
1093 }
1094 for (auto UI = SetjmpTableSize->use_begin(), UE = SetjmpTableSize->use_end();
1095 UI != UE;) {
1096 Use &U = *UI;
1097 ++UI;
1098 if (auto *I = dyn_cast<Instruction>(U.getUser()))
1099 if (I->getParent() != &EntryBB)
1100 SetjmpTableSizeSSA.RewriteUse(U);
1101 }
1102
1103 // Finally, our modifications to the cfg can break dominance of SSA variables.
1104 // For example, in this code,
1105 // if (x()) { .. setjmp() .. }
1106 // if (y()) { .. longjmp() .. }
1107 // We must split the longjmp block, and it can jump into the block splitted
1108 // from setjmp one. But that means that when we split the setjmp block, it's
1109 // first part no longer dominates its second part - there is a theoretically
1110 // possible control flow path where x() is false, then y() is true and we
1111 // reach the second part of the setjmp block, without ever reaching the first
1112 // part. So, we rebuild SSA form here.
1113 rebuildSSA(F);
1114 return true;
1115 }
1116