blob: 06c6dc69d0c670f74f52ca607b7c1d1a7b24eca8 [file] [log] [blame]
Derek Schuffccdceda2016-08-18 15:27:25 +00001//=== WebAssemblyLowerEmscriptenEHSjLj.cpp - Lower exceptions for Emscripten =//
Derek Schufff41f67d2016-08-01 21:34:04 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// This file lowers exception-related instructions and setjmp/longjmp
Heejin Ahnc0f18172016-09-01 21:05:15 +000012/// function calls in order to use Emscripten's JavaScript try and catch
13/// mechanism.
Derek Schufff41f67d2016-08-01 21:34:04 +000014///
Heejin Ahnc0f18172016-09-01 21:05:15 +000015/// To handle exceptions and setjmp/longjmps, this scheme relies on JavaScript's
16/// try and catch syntax and relevant exception-related libraries implemented
17/// in JavaScript glue code that will be produced by Emscripten. This is similar
18/// to the current Emscripten asm.js exception handling in fastcomp. For
19/// fastcomp's EH / SjLj scheme, see these files in fastcomp LLVM branch:
Derek Schufff41f67d2016-08-01 21:34:04 +000020/// (Location: https://github.com/kripken/emscripten-fastcomp)
21/// lib/Target/JSBackend/NaCl/LowerEmExceptionsPass.cpp
Heejin Ahnc0f18172016-09-01 21:05:15 +000022/// lib/Target/JSBackend/NaCl/LowerEmSetjmp.cpp
Derek Schufff41f67d2016-08-01 21:34:04 +000023/// lib/Target/JSBackend/JSBackend.cpp
24/// lib/Target/JSBackend/CallHandlers.h
25///
Heejin Ahnc0f18172016-09-01 21:05:15 +000026/// * Exception handling
27/// This pass lowers invokes and landingpads into library functions in JS glue
28/// code. Invokes are lowered into function wrappers called invoke wrappers that
29/// exist in JS side, which wraps the original function call with JS try-catch.
30/// If an exception occurred, cxa_throw() function in JS side sets some
31/// variables (see below) so we can check whether an exception occurred from
32/// wasm code and handle it appropriately.
Derek Schufff41f67d2016-08-01 21:34:04 +000033///
Heejin Ahnc0f18172016-09-01 21:05:15 +000034/// * Setjmp-longjmp handling
35/// This pass lowers setjmp to a reasonably-performant approach for emscripten.
36/// The idea is that each block with a setjmp is broken up into two parts: the
37/// part containing setjmp and the part right after the setjmp. The latter part
38/// is either reached from the setjmp, or later from a longjmp. To handle the
39/// longjmp, all calls that might longjmp are also called using invoke wrappers
40/// and thus JS / try-catch. JS longjmp() function also sets some variables so
41/// we can check / whether a longjmp occurred from wasm code. Each block with a
42/// function call that might longjmp is also split up after the longjmp call.
43/// After the longjmp call, we check whether a longjmp occurred, and if it did,
44/// which setjmp it corresponds to, and jump to the right post-setjmp block.
45/// We assume setjmp-longjmp handling always run after EH handling, which means
46/// we don't expect any exception-related instructions when SjLj runs.
47/// FIXME Currently this scheme does not support indirect call of setjmp,
48/// because of the limitation of the scheme itself. fastcomp does not support it
49/// either.
50///
51/// In detail, this pass does following things:
52///
53/// 1) Create three global variables: __THREW__, __threwValue, and __tempRet0.
54/// __tempRet0 will be set within __cxa_find_matching_catch() function in
Derek Schuffccdceda2016-08-18 15:27:25 +000055/// JS library, and __THREW__ and __threwValue will be set in invoke wrappers
Heejin Ahnc0f18172016-09-01 21:05:15 +000056/// in JS glue code. For what invoke wrappers are, refer to 3). These
57/// variables are used for both exceptions and setjmp/longjmps.
58/// __THREW__ indicates whether an exception or a longjmp occurred or not. 0
59/// means nothing occurred, 1 means an exception occurred, and other numbers
Heejin Ahn99bd16b2016-09-10 02:33:47 +000060/// mean a longjmp occurred. In the case of longjmp, __threwValue variable
Heejin Ahnc0f18172016-09-01 21:05:15 +000061/// indicates the corresponding setjmp buffer the longjmp corresponds to.
62/// In exception handling, __tempRet0 indicates the type of an exception
63/// caught, and in setjmp/longjmp, it means the second argument to longjmp
64/// function.
65///
66/// * Exception handling
Derek Schufff41f67d2016-08-01 21:34:04 +000067///
68/// 2) Create setThrew and setTempRet0 functions.
69/// The global variables created in 1) will exist in wasm address space,
70/// but their values should be set in JS code, so we provide these functions
71/// as interfaces to JS glue code. These functions are equivalent to the
72/// following JS functions, which actually exist in asm.js version of JS
73/// library.
74///
75/// function setThrew(threw, value) {
76/// if (__THREW__ == 0) {
77/// __THREW__ = threw;
Derek Schuffccdceda2016-08-18 15:27:25 +000078/// __threwValue = value;
Derek Schufff41f67d2016-08-01 21:34:04 +000079/// }
80/// }
81///
82/// function setTempRet0(value) {
Heejin Ahnc0f18172016-09-01 21:05:15 +000083/// __tempRet0 = value;
Derek Schufff41f67d2016-08-01 21:34:04 +000084/// }
85///
86/// 3) Lower
87/// invoke @func(arg1, arg2) to label %invoke.cont unwind label %lpad
88/// into
89/// __THREW__ = 0;
Heejin Ahn99bd16b2016-09-10 02:33:47 +000090/// call @__invoke_SIG(func, arg1, arg2)
Derek Schufff41f67d2016-08-01 21:34:04 +000091/// %__THREW__.val = __THREW__;
92/// __THREW__ = 0;
Heejin Ahn99bd16b2016-09-10 02:33:47 +000093/// if (%__THREW__.val == 1)
94/// goto %lpad
95/// else
96/// goto %invoke.cont
Derek Schufff41f67d2016-08-01 21:34:04 +000097/// SIG is a mangled string generated based on the LLVM IR-level function
98/// signature. After LLVM IR types are lowered to the target wasm types,
99/// the names for these wrappers will change based on wasm types as well,
100/// as in invoke_vi (function takes an int and returns void). The bodies of
101/// these wrappers will be generated in JS glue code, and inside those
102/// wrappers we use JS try-catch to generate actual exception effects. It
103/// also calls the original callee function. An example wrapper in JS code
104/// would look like this:
105/// function invoke_vi(index,a1) {
106/// try {
107/// Module["dynCall_vi"](index,a1); // This calls original callee
108/// } catch(e) {
109/// if (typeof e !== 'number' && e !== 'longjmp') throw e;
110/// asm["setThrew"](1, 0); // setThrew is called here
111/// }
112/// }
113/// If an exception is thrown, __THREW__ will be set to true in a wrapper,
114/// so we can jump to the right BB based on this value.
115///
116/// 4) Lower
117/// %val = landingpad catch c1 catch c2 catch c3 ...
118/// ... use %val ...
119/// into
Derek Schuff53b9af02016-08-09 00:29:55 +0000120/// %fmc = call @__cxa_find_matching_catch_N(c1, c2, c3, ...)
Heejin Ahnc0f18172016-09-01 21:05:15 +0000121/// %val = {%fmc, __tempRet0}
Derek Schufff41f67d2016-08-01 21:34:04 +0000122/// ... use %val ...
123/// Here N is a number calculated based on the number of clauses.
Heejin Ahnc0f18172016-09-01 21:05:15 +0000124/// Global variable __tempRet0 is set within __cxa_find_matching_catch() in
Derek Schufff41f67d2016-08-01 21:34:04 +0000125/// JS glue code.
126///
127/// 5) Lower
128/// resume {%a, %b}
129/// into
Derek Schuff53b9af02016-08-09 00:29:55 +0000130/// call @__resumeException(%a)
131/// where __resumeException() is a function in JS glue code.
Derek Schufff41f67d2016-08-01 21:34:04 +0000132///
Derek Schuff53b9af02016-08-09 00:29:55 +0000133/// 6) Lower
134/// call @llvm.eh.typeid.for(type) (intrinsic)
135/// into
136/// call @llvm_eh_typeid_for(type)
137/// llvm_eh_typeid_for function will be generated in JS glue code.
Derek Schufff41f67d2016-08-01 21:34:04 +0000138///
Heejin Ahnc0f18172016-09-01 21:05:15 +0000139/// * Setjmp / Longjmp handling
140///
141/// 7) In the function entry that calls setjmp, initialize setjmpTable and
142/// sejmpTableSize as follows:
143/// setjmpTableSize = 4;
144/// setjmpTable = (int *) malloc(40);
145/// setjmpTable[0] = 0;
146/// setjmpTable and setjmpTableSize are used in saveSetjmp() function in JS
147/// code.
148///
149/// 8) Lower
150/// setjmp(buf)
151/// into
152/// setjmpTable = saveSetjmp(buf, label, setjmpTable, setjmpTableSize);
153/// setjmpTableSize = __tempRet0;
154/// For each dynamic setjmp call, setjmpTable stores its ID (a number which
155/// is incrementally assigned from 0) and its label (a unique number that
156/// represents each callsite of setjmp). When we need more entries in
157/// setjmpTable, it is reallocated in saveSetjmp() in JS code and it will
158/// return the new table address, and assign the new table size in
159/// __tempRet0. saveSetjmp also stores the setjmp's ID into the buffer buf.
160/// A BB with setjmp is split into two after setjmp call in order to make the
161/// post-setjmp BB the possible destination of longjmp BB.
162///
163/// 9) Lower
164/// longjmp(buf, value)
165/// into
166/// emscripten_longjmp_jmpbuf(buf, value)
167/// emscripten_longjmp_jmpbuf will be lowered to emscripten_longjmp later.
168///
169/// 10) Lower every call that might longjmp into
170/// __THREW__ = 0;
Heejin Ahn99bd16b2016-09-10 02:33:47 +0000171/// call @__invoke_SIG(func, arg1, arg2)
Heejin Ahnc0f18172016-09-01 21:05:15 +0000172/// %__THREW__.val = __THREW__;
173/// __THREW__ = 0;
Heejin Ahn99bd16b2016-09-10 02:33:47 +0000174/// if (%__THREW__.val != 0 & __threwValue != 0) {
Heejin Ahnc0f18172016-09-01 21:05:15 +0000175/// %label = testSetjmp(mem[%__THREW__.val], setjmpTable,
176/// setjmpTableSize);
177/// if (%label == 0)
Heejin Ahn99bd16b2016-09-10 02:33:47 +0000178/// emscripten_longjmp(%__THREW__.val, __threwValue);
179/// __tempRet0 = __threwValue;
Heejin Ahnc0f18172016-09-01 21:05:15 +0000180/// } else {
181/// %label = -1;
182/// }
183/// longjmp_result = __tempRet0;
184/// switch label {
Heejin Ahn99bd16b2016-09-10 02:33:47 +0000185/// label 1: goto post-setjmp BB 1
186/// label 2: goto post-setjmp BB 2
Heejin Ahnc0f18172016-09-01 21:05:15 +0000187/// ...
Heejin Ahn99bd16b2016-09-10 02:33:47 +0000188/// default: goto splitted next BB
Heejin Ahnc0f18172016-09-01 21:05:15 +0000189/// }
190/// testSetjmp examines setjmpTable to see if there is a matching setjmp
191/// call. After calling an invoke wrapper, if a longjmp occurred, __THREW__
Heejin Ahn99bd16b2016-09-10 02:33:47 +0000192/// will be the address of matching jmp_buf buffer and __threwValue be the
Heejin Ahnc0f18172016-09-01 21:05:15 +0000193/// second argument to longjmp. mem[__THREW__.val] is a setjmp ID that is
194/// stored in saveSetjmp. testSetjmp returns a setjmp label, a unique ID to
195/// each setjmp callsite. Label 0 means this longjmp buffer does not
196/// correspond to one of the setjmp callsites in this function, so in this
197/// case we just chain the longjmp to the caller. (Here we call
198/// emscripten_longjmp, which is different from emscripten_longjmp_jmpbuf.
199/// emscripten_longjmp_jmpbuf takes jmp_buf as its first argument, while
200/// emscripten_longjmp takes an int. Both of them will eventually be lowered
201/// to emscripten_longjmp in s2wasm, but here we need two signatures - we
202/// can't translate an int value to a jmp_buf.)
203/// Label -1 means no longjmp occurred. Otherwise we jump to the right
204/// post-setjmp BB based on the label.
205///
Derek Schufff41f67d2016-08-01 21:34:04 +0000206///===----------------------------------------------------------------------===//
207
208#include "WebAssembly.h"
Derek Schuff53b9af02016-08-09 00:29:55 +0000209#include "llvm/IR/CallSite.h"
Heejin Ahnc0f18172016-09-01 21:05:15 +0000210#include "llvm/IR/Dominators.h"
Derek Schufff41f67d2016-08-01 21:34:04 +0000211#include "llvm/IR/IRBuilder.h"
Heejin Ahnc0f18172016-09-01 21:05:15 +0000212#include "llvm/Transforms/Utils/BasicBlockUtils.h"
213#include "llvm/Transforms/Utils/SSAUpdater.h"
Derek Schufff41f67d2016-08-01 21:34:04 +0000214
215using namespace llvm;
216
Derek Schuffccdceda2016-08-18 15:27:25 +0000217#define DEBUG_TYPE "wasm-lower-em-ehsjlj"
Derek Schufff41f67d2016-08-01 21:34:04 +0000218
Derek Schuff66641322016-08-09 22:37:00 +0000219static cl::list<std::string>
Derek Schuffccdceda2016-08-18 15:27:25 +0000220 EHWhitelist("emscripten-cxx-exceptions-whitelist",
221 cl::desc("The list of function names in which Emscripten-style "
222 "exception handling is enabled (see emscripten "
223 "EMSCRIPTEN_CATCHING_WHITELIST options)"),
224 cl::CommaSeparated);
Derek Schuff66641322016-08-09 22:37:00 +0000225
Derek Schufff41f67d2016-08-01 21:34:04 +0000226namespace {
Derek Schuffccdceda2016-08-18 15:27:25 +0000227class WebAssemblyLowerEmscriptenEHSjLj final : public ModulePass {
228 static const char *ThrewGVName;
229 static const char *ThrewValueGVName;
230 static const char *TempRet0GVName;
231 static const char *ResumeFName;
232 static const char *EHTypeIDFName;
233 static const char *SetThrewFName;
234 static const char *SetTempRet0FName;
Heejin Ahnc0f18172016-09-01 21:05:15 +0000235 static const char *EmLongjmpFName;
236 static const char *EmLongjmpJmpbufFName;
237 static const char *SaveSetjmpFName;
238 static const char *TestSetjmpFName;
Derek Schuffccdceda2016-08-18 15:27:25 +0000239 static const char *FindMatchingCatchPrefix;
240 static const char *InvokePrefix;
241
Heejin Ahnc0f18172016-09-01 21:05:15 +0000242 bool EnableEH; // Enable exception handling
243 bool EnableSjLj; // Enable setjmp/longjmp handling
Derek Schuffccdceda2016-08-18 15:27:25 +0000244
245 GlobalVariable *ThrewGV;
246 GlobalVariable *ThrewValueGV;
247 GlobalVariable *TempRet0GV;
248 Function *ResumeF;
249 Function *EHTypeIDF;
Heejin Ahnc0f18172016-09-01 21:05:15 +0000250 Function *EmLongjmpF;
251 Function *EmLongjmpJmpbufF;
252 Function *SaveSetjmpF;
253 Function *TestSetjmpF;
254
Derek Schuffccdceda2016-08-18 15:27:25 +0000255 // __cxa_find_matching_catch_N functions.
256 // Indexed by the number of clauses in an original landingpad instruction.
257 DenseMap<int, Function *> FindMatchingCatches;
258 // Map of <function signature string, invoke_ wrappers>
259 StringMap<Function *> InvokeWrappers;
260 // Set of whitelisted function names for exception handling
261 std::set<std::string> EHWhitelistSet;
262
Mehdi Amini117296c2016-10-01 02:56:57 +0000263 StringRef getPassName() const override {
Derek Schufff41f67d2016-08-01 21:34:04 +0000264 return "WebAssembly Lower Emscripten Exceptions";
265 }
266
Derek Schuffccdceda2016-08-18 15:27:25 +0000267 bool runEHOnFunction(Function &F);
268 bool runSjLjOnFunction(Function &F);
Derek Schufff41f67d2016-08-01 21:34:04 +0000269 Function *getFindMatchingCatch(Module &M, unsigned NumClauses);
270
Heejin Ahnc0f18172016-09-01 21:05:15 +0000271 template <typename CallOrInvoke> Value *wrapInvoke(CallOrInvoke *CI);
272 void wrapTestSetjmp(BasicBlock *BB, Instruction *InsertPt, Value *Threw,
273 Value *SetjmpTable, Value *SetjmpTableSize, Value *&Label,
274 Value *&LongjmpResult, BasicBlock *&EndBB);
275 template <typename CallOrInvoke> Function *getInvokeWrapper(CallOrInvoke *CI);
276
Derek Schuffccdceda2016-08-18 15:27:25 +0000277 bool areAllExceptionsAllowed() const { return EHWhitelistSet.empty(); }
Heejin Ahnc0f18172016-09-01 21:05:15 +0000278 bool canLongjmp(Module &M, const Value *Callee) const;
279
280 void createSetThrewFunction(Module &M);
281 void createSetTempRet0Function(Module &M);
282
283 void rebuildSSA(Function &F);
Derek Schufff41f67d2016-08-01 21:34:04 +0000284
285public:
286 static char ID;
287
Heejin Ahnc0f18172016-09-01 21:05:15 +0000288 WebAssemblyLowerEmscriptenEHSjLj(bool EnableEH = true, bool EnableSjLj = true)
289 : ModulePass(ID), EnableEH(EnableEH), EnableSjLj(EnableSjLj),
290 ThrewGV(nullptr), ThrewValueGV(nullptr), TempRet0GV(nullptr),
291 ResumeF(nullptr), EHTypeIDF(nullptr), EmLongjmpF(nullptr),
292 EmLongjmpJmpbufF(nullptr), SaveSetjmpF(nullptr), TestSetjmpF(nullptr) {
Derek Schuffccdceda2016-08-18 15:27:25 +0000293 EHWhitelistSet.insert(EHWhitelist.begin(), EHWhitelist.end());
Derek Schuff66641322016-08-09 22:37:00 +0000294 }
Derek Schufff41f67d2016-08-01 21:34:04 +0000295 bool runOnModule(Module &M) override;
Heejin Ahnc0f18172016-09-01 21:05:15 +0000296
297 void getAnalysisUsage(AnalysisUsage &AU) const override {
298 AU.addRequired<DominatorTreeWrapperPass>();
299 }
Derek Schufff41f67d2016-08-01 21:34:04 +0000300};
301} // End anonymous namespace
302
Derek Schuffccdceda2016-08-18 15:27:25 +0000303const char *WebAssemblyLowerEmscriptenEHSjLj::ThrewGVName = "__THREW__";
304const char *WebAssemblyLowerEmscriptenEHSjLj::ThrewValueGVName = "__threwValue";
305const char *WebAssemblyLowerEmscriptenEHSjLj::TempRet0GVName = "__tempRet0";
306const char *WebAssemblyLowerEmscriptenEHSjLj::ResumeFName = "__resumeException";
307const char *WebAssemblyLowerEmscriptenEHSjLj::EHTypeIDFName =
308 "llvm_eh_typeid_for";
309const char *WebAssemblyLowerEmscriptenEHSjLj::SetThrewFName = "setThrew";
310const char *WebAssemblyLowerEmscriptenEHSjLj::SetTempRet0FName = "setTempRet0";
Heejin Ahnc0f18172016-09-01 21:05:15 +0000311const char *WebAssemblyLowerEmscriptenEHSjLj::EmLongjmpFName =
312 "emscripten_longjmp";
313const char *WebAssemblyLowerEmscriptenEHSjLj::EmLongjmpJmpbufFName =
314 "emscripten_longjmp_jmpbuf";
315const char *WebAssemblyLowerEmscriptenEHSjLj::SaveSetjmpFName = "saveSetjmp";
316const char *WebAssemblyLowerEmscriptenEHSjLj::TestSetjmpFName = "testSetjmp";
Derek Schuffccdceda2016-08-18 15:27:25 +0000317const char *WebAssemblyLowerEmscriptenEHSjLj::FindMatchingCatchPrefix =
318 "__cxa_find_matching_catch_";
319const char *WebAssemblyLowerEmscriptenEHSjLj::InvokePrefix = "__invoke_";
Derek Schufff41f67d2016-08-01 21:34:04 +0000320
Derek Schuffccdceda2016-08-18 15:27:25 +0000321char WebAssemblyLowerEmscriptenEHSjLj::ID = 0;
322INITIALIZE_PASS(WebAssemblyLowerEmscriptenEHSjLj, DEBUG_TYPE,
323 "WebAssembly Lower Emscripten Exceptions / Setjmp / Longjmp",
324 false, false)
325
Heejin Ahnc0f18172016-09-01 21:05:15 +0000326ModulePass *llvm::createWebAssemblyLowerEmscriptenEHSjLj(bool EnableEH,
327 bool EnableSjLj) {
328 return new WebAssemblyLowerEmscriptenEHSjLj(EnableEH, EnableSjLj);
Derek Schufff41f67d2016-08-01 21:34:04 +0000329}
330
331static bool canThrow(const Value *V) {
332 if (const auto *F = dyn_cast<const Function>(V)) {
333 // Intrinsics cannot throw
334 if (F->isIntrinsic())
335 return false;
336 StringRef Name = F->getName();
337 // leave setjmp and longjmp (mostly) alone, we process them properly later
338 if (Name == "setjmp" || Name == "longjmp")
339 return false;
Heejin Ahnc0f18172016-09-01 21:05:15 +0000340 return !F->doesNotThrow();
Derek Schufff41f67d2016-08-01 21:34:04 +0000341 }
Heejin Ahnb6cd5122016-08-24 22:53:00 +0000342 // not a function, so an indirect call - can throw, we can't tell
343 return true;
Derek Schufff41f67d2016-08-01 21:34:04 +0000344}
345
346// Returns an available name for a global value.
347// If the proposed name already exists in the module, adds '_' at the end of
348// the name until the name is available.
349static inline std::string createGlobalValueName(const Module &M,
350 const std::string &Propose) {
351 std::string Name = Propose;
352 while (M.getNamedGlobal(Name))
353 Name += "_";
354 return Name;
355}
356
357// Simple function name mangler.
358// This function simply takes LLVM's string representation of parameter types
Derek Schuff53b9af02016-08-09 00:29:55 +0000359// and concatenate them with '_'. There are non-alphanumeric characters but llc
360// is ok with it, and we need to postprocess these names after the lowering
361// phase anyway.
Derek Schufff41f67d2016-08-01 21:34:04 +0000362static std::string getSignature(FunctionType *FTy) {
363 std::string Sig;
364 raw_string_ostream OS(Sig);
365 OS << *FTy->getReturnType();
366 for (Type *ParamTy : FTy->params())
367 OS << "_" << *ParamTy;
368 if (FTy->isVarArg())
369 OS << "_...";
370 Sig = OS.str();
David Majnemerc7004902016-08-12 04:32:37 +0000371 Sig.erase(remove_if(Sig, isspace), Sig.end());
Derek Schuff53b9af02016-08-09 00:29:55 +0000372 // When s2wasm parses .s file, a comma means the end of an argument. So a
373 // mangled function name can contain any character but a comma.
374 std::replace(Sig.begin(), Sig.end(), ',', '.');
Derek Schufff41f67d2016-08-01 21:34:04 +0000375 return Sig;
376}
377
Heejin Ahnc0f18172016-09-01 21:05:15 +0000378// Returns __cxa_find_matching_catch_N function, where N = NumClauses + 2.
379// This is because a landingpad instruction contains two more arguments, a
380// personality function and a cleanup bit, and __cxa_find_matching_catch_N
381// functions are named after the number of arguments in the original landingpad
382// instruction.
Derek Schuffccdceda2016-08-18 15:27:25 +0000383Function *
384WebAssemblyLowerEmscriptenEHSjLj::getFindMatchingCatch(Module &M,
385 unsigned NumClauses) {
Derek Schufff41f67d2016-08-01 21:34:04 +0000386 if (FindMatchingCatches.count(NumClauses))
387 return FindMatchingCatches[NumClauses];
388 PointerType *Int8PtrTy = Type::getInt8PtrTy(M.getContext());
389 SmallVector<Type *, 16> Args(NumClauses, Int8PtrTy);
390 FunctionType *FTy = FunctionType::get(Int8PtrTy, Args, false);
Derek Schuffccdceda2016-08-18 15:27:25 +0000391 Function *F =
392 Function::Create(FTy, GlobalValue::ExternalLinkage,
393 FindMatchingCatchPrefix + Twine(NumClauses + 2), &M);
Derek Schufff41f67d2016-08-01 21:34:04 +0000394 FindMatchingCatches[NumClauses] = F;
395 return F;
396}
397
Heejin Ahnc0f18172016-09-01 21:05:15 +0000398// Generate invoke wrapper seqence with preamble and postamble
399// Preamble:
400// __THREW__ = 0;
401// Postamble:
402// %__THREW__.val = __THREW__; __THREW__ = 0;
403// Returns %__THREW__.val, which indicates whether an exception is thrown (or
404// whether longjmp occurred), for future use.
405template <typename CallOrInvoke>
406Value *WebAssemblyLowerEmscriptenEHSjLj::wrapInvoke(CallOrInvoke *CI) {
407 LLVMContext &C = CI->getModule()->getContext();
408
409 // If we are calling a function that is noreturn, we must remove that
410 // attribute. The code we insert here does expect it to return, after we
411 // catch the exception.
412 if (CI->doesNotReturn()) {
413 if (auto *F = dyn_cast<Function>(CI->getCalledValue()))
414 F->removeFnAttr(Attribute::NoReturn);
Reid Klecknerb5180542017-03-21 16:57:19 +0000415 CI->removeAttribute(AttributeList::FunctionIndex, Attribute::NoReturn);
Heejin Ahnc0f18172016-09-01 21:05:15 +0000416 }
417
418 IRBuilder<> IRB(C);
419 IRB.SetInsertPoint(CI);
420
421 // Pre-invoke
422 // __THREW__ = 0;
423 IRB.CreateStore(IRB.getInt32(0), ThrewGV);
424
425 // Invoke function wrapper in JavaScript
426 SmallVector<Value *, 16> Args;
427 // Put the pointer to the callee as first argument, so it can be called
428 // within the invoke wrapper later
429 Args.push_back(CI->getCalledValue());
430 Args.append(CI->arg_begin(), CI->arg_end());
431 CallInst *NewCall = IRB.CreateCall(getInvokeWrapper(CI), Args);
432 NewCall->takeName(CI);
433 NewCall->setCallingConv(CI->getCallingConv());
434 NewCall->setDebugLoc(CI->getDebugLoc());
435
436 // Because we added the pointer to the callee as first argument, all
437 // argument attribute indices have to be incremented by one.
Reid Kleckner7f720332017-04-13 00:58:09 +0000438 SmallVector<AttributeSet, 8> ArgAttributes;
Derek Schuff0db0ca32017-04-12 16:03:00 +0000439 const AttributeList &InvokeAL = CI->getAttributes();
440
Derek Schuff0db0ca32017-04-12 16:03:00 +0000441 // No attributes for the callee pointer.
Reid Kleckner7f720332017-04-13 00:58:09 +0000442 ArgAttributes.push_back(AttributeSet());
Derek Schuff0db0ca32017-04-12 16:03:00 +0000443 // Copy the argument attributes from the original
Reid Klecknerf021fab2017-04-13 23:12:13 +0000444 for (unsigned i = 0, e = CI->getNumArgOperands(); i < e; ++i)
Reid Kleckner7f720332017-04-13 00:58:09 +0000445 ArgAttributes.push_back(InvokeAL.getParamAttributes(i));
Derek Schuff0db0ca32017-04-12 16:03:00 +0000446
Heejin Ahnc0f18172016-09-01 21:05:15 +0000447 // Reconstruct the AttributesList based on the vector we constructed.
Reid Kleckner7f720332017-04-13 00:58:09 +0000448 AttributeList NewCallAL =
449 AttributeList::get(C, InvokeAL.getFnAttributes(),
450 InvokeAL.getRetAttributes(), ArgAttributes);
Derek Schuff0db0ca32017-04-12 16:03:00 +0000451 NewCall->setAttributes(NewCallAL);
Heejin Ahnc0f18172016-09-01 21:05:15 +0000452
453 CI->replaceAllUsesWith(NewCall);
454
455 // Post-invoke
456 // %__THREW__.val = __THREW__; __THREW__ = 0;
457 Value *Threw = IRB.CreateLoad(ThrewGV, ThrewGV->getName() + ".val");
458 IRB.CreateStore(IRB.getInt32(0), ThrewGV);
459 return Threw;
460}
461
462// Get matching invoke wrapper based on callee signature
463template <typename CallOrInvoke>
464Function *WebAssemblyLowerEmscriptenEHSjLj::getInvokeWrapper(CallOrInvoke *CI) {
465 Module *M = CI->getModule();
Derek Schufff41f67d2016-08-01 21:34:04 +0000466 SmallVector<Type *, 16> ArgTys;
Heejin Ahnc0f18172016-09-01 21:05:15 +0000467 Value *Callee = CI->getCalledValue();
Derek Schufff41f67d2016-08-01 21:34:04 +0000468 FunctionType *CalleeFTy;
469 if (auto *F = dyn_cast<Function>(Callee))
470 CalleeFTy = F->getFunctionType();
471 else {
Heejin Ahnc0f18172016-09-01 21:05:15 +0000472 auto *CalleeTy = cast<PointerType>(Callee->getType())->getElementType();
Derek Schufff41f67d2016-08-01 21:34:04 +0000473 CalleeFTy = dyn_cast<FunctionType>(CalleeTy);
474 }
475
476 std::string Sig = getSignature(CalleeFTy);
477 if (InvokeWrappers.find(Sig) != InvokeWrappers.end())
478 return InvokeWrappers[Sig];
479
480 // Put the pointer to the callee as first argument
481 ArgTys.push_back(PointerType::getUnqual(CalleeFTy));
482 // Add argument types
483 ArgTys.append(CalleeFTy->param_begin(), CalleeFTy->param_end());
484
485 FunctionType *FTy = FunctionType::get(CalleeFTy->getReturnType(), ArgTys,
486 CalleeFTy->isVarArg());
487 Function *F = Function::Create(FTy, GlobalValue::ExternalLinkage,
Heejin Ahnc0f18172016-09-01 21:05:15 +0000488 InvokePrefix + Sig, M);
Derek Schufff41f67d2016-08-01 21:34:04 +0000489 InvokeWrappers[Sig] = F;
490 return F;
491}
492
Heejin Ahnc0f18172016-09-01 21:05:15 +0000493bool WebAssemblyLowerEmscriptenEHSjLj::canLongjmp(Module &M,
494 const Value *Callee) const {
495 if (auto *CalleeF = dyn_cast<Function>(Callee))
496 if (CalleeF->isIntrinsic())
497 return false;
Heejin Ahn23d57102016-08-31 22:40:34 +0000498
Heejin Ahnc0f18172016-09-01 21:05:15 +0000499 // The reason we include malloc/free here is to exclude the malloc/free
500 // calls generated in setjmp prep / cleanup routines.
501 Function *SetjmpF = M.getFunction("setjmp");
502 Function *MallocF = M.getFunction("malloc");
503 Function *FreeF = M.getFunction("free");
504 if (Callee == SetjmpF || Callee == MallocF || Callee == FreeF)
Heejin Ahn10a70862016-09-01 00:44:37 +0000505 return false;
506
Heejin Ahnc0f18172016-09-01 21:05:15 +0000507 // There are functions in JS glue code
508 if (Callee == ResumeF || Callee == EHTypeIDF || Callee == SaveSetjmpF ||
509 Callee == TestSetjmpF)
510 return false;
Heejin Ahn10a70862016-09-01 00:44:37 +0000511
Heejin Ahnc0f18172016-09-01 21:05:15 +0000512 // __cxa_find_matching_catch_N functions cannot longjmp
513 if (Callee->getName().startswith(FindMatchingCatchPrefix))
514 return false;
515
516 // Exception-catching related functions
517 Function *BeginCatchF = M.getFunction("__cxa_begin_catch");
518 Function *EndCatchF = M.getFunction("__cxa_end_catch");
519 Function *AllocExceptionF = M.getFunction("__cxa_allocate_exception");
520 Function *ThrowF = M.getFunction("__cxa_throw");
521 Function *TerminateF = M.getFunction("__clang_call_terminate");
522 if (Callee == BeginCatchF || Callee == EndCatchF ||
523 Callee == AllocExceptionF || Callee == ThrowF || Callee == TerminateF)
524 return false;
525
526 // Otherwise we don't know
527 return true;
528}
529
530// Generate testSetjmp function call seqence with preamble and postamble.
531// The code this generates is equivalent to the following JavaScript code:
532// if (%__THREW__.val != 0 & threwValue != 0) {
533// %label = _testSetjmp(mem[%__THREW__.val], setjmpTable, setjmpTableSize);
534// if (%label == 0)
535// emscripten_longjmp(%__THREW__.val, threwValue);
536// __tempRet0 = threwValue;
537// } else {
538// %label = -1;
539// }
540// %longjmp_result = __tempRet0;
541//
542// As output parameters. returns %label, %longjmp_result, and the BB the last
543// instruction (%longjmp_result = ...) is in.
544void WebAssemblyLowerEmscriptenEHSjLj::wrapTestSetjmp(
545 BasicBlock *BB, Instruction *InsertPt, Value *Threw, Value *SetjmpTable,
546 Value *SetjmpTableSize, Value *&Label, Value *&LongjmpResult,
547 BasicBlock *&EndBB) {
548 Function *F = BB->getParent();
549 LLVMContext &C = BB->getModule()->getContext();
550 IRBuilder<> IRB(C);
551 IRB.SetInsertPoint(InsertPt);
552
553 // if (%__THREW__.val != 0 & threwValue != 0)
554 IRB.SetInsertPoint(BB);
555 BasicBlock *ThenBB1 = BasicBlock::Create(C, "if.then1", F);
556 BasicBlock *ElseBB1 = BasicBlock::Create(C, "if.else1", F);
557 BasicBlock *EndBB1 = BasicBlock::Create(C, "if.end", F);
558 Value *ThrewCmp = IRB.CreateICmpNE(Threw, IRB.getInt32(0));
559 Value *ThrewValue =
560 IRB.CreateLoad(ThrewValueGV, ThrewValueGV->getName() + ".val");
561 Value *ThrewValueCmp = IRB.CreateICmpNE(ThrewValue, IRB.getInt32(0));
562 Value *Cmp1 = IRB.CreateAnd(ThrewCmp, ThrewValueCmp, "cmp1");
563 IRB.CreateCondBr(Cmp1, ThenBB1, ElseBB1);
564
565 // %label = _testSetjmp(mem[%__THREW__.val], _setjmpTable, _setjmpTableSize);
566 // if (%label == 0)
567 IRB.SetInsertPoint(ThenBB1);
568 BasicBlock *ThenBB2 = BasicBlock::Create(C, "if.then2", F);
569 BasicBlock *EndBB2 = BasicBlock::Create(C, "if.end2", F);
570 Value *ThrewInt = IRB.CreateIntToPtr(Threw, Type::getInt32PtrTy(C),
571 Threw->getName() + ".i32p");
572 Value *LoadedThrew =
573 IRB.CreateLoad(ThrewInt, ThrewInt->getName() + ".loaded");
574 Value *ThenLabel = IRB.CreateCall(
575 TestSetjmpF, {LoadedThrew, SetjmpTable, SetjmpTableSize}, "label");
576 Value *Cmp2 = IRB.CreateICmpEQ(ThenLabel, IRB.getInt32(0));
577 IRB.CreateCondBr(Cmp2, ThenBB2, EndBB2);
578
579 // emscripten_longjmp(%__THREW__.val, threwValue);
580 IRB.SetInsertPoint(ThenBB2);
581 IRB.CreateCall(EmLongjmpF, {Threw, ThrewValue});
582 IRB.CreateUnreachable();
583
584 // __tempRet0 = threwValue;
585 IRB.SetInsertPoint(EndBB2);
586 IRB.CreateStore(ThrewValue, TempRet0GV);
587 IRB.CreateBr(EndBB1);
588
589 IRB.SetInsertPoint(ElseBB1);
590 IRB.CreateBr(EndBB1);
591
592 // longjmp_result = __tempRet0;
593 IRB.SetInsertPoint(EndBB1);
594 PHINode *LabelPHI = IRB.CreatePHI(IRB.getInt32Ty(), 2, "label");
595 LabelPHI->addIncoming(ThenLabel, EndBB2);
596
597 LabelPHI->addIncoming(IRB.getInt32(-1), ElseBB1);
598
599 // Output parameter assignment
600 Label = LabelPHI;
601 EndBB = EndBB1;
602 LongjmpResult = IRB.CreateLoad(TempRet0GV, "longjmp_result");
603}
604
605// Create setThrew function
606// function setThrew(threw, value) {
607// if (__THREW__ == 0) {
608// __THREW__ = threw;
609// __threwValue = value;
610// }
611// }
612void WebAssemblyLowerEmscriptenEHSjLj::createSetThrewFunction(Module &M) {
613 LLVMContext &C = M.getContext();
614 IRBuilder<> IRB(C);
615
616 assert(!M.getNamedGlobal(SetThrewFName) && "setThrew already exists");
617 Type *Params[] = {IRB.getInt32Ty(), IRB.getInt32Ty()};
618 FunctionType *FTy = FunctionType::get(IRB.getVoidTy(), Params, false);
Derek Schufff41f67d2016-08-01 21:34:04 +0000619 Function *F =
Derek Schuffccdceda2016-08-18 15:27:25 +0000620 Function::Create(FTy, GlobalValue::ExternalLinkage, SetThrewFName, &M);
Derek Schufff41f67d2016-08-01 21:34:04 +0000621 Argument *Arg1 = &*(F->arg_begin());
Reid Kleckner98e56432017-03-17 17:24:03 +0000622 Argument *Arg2 = &*std::next(F->arg_begin());
Derek Schufff41f67d2016-08-01 21:34:04 +0000623 Arg1->setName("threw");
624 Arg2->setName("value");
Derek Schuff53b9af02016-08-09 00:29:55 +0000625 BasicBlock *EntryBB = BasicBlock::Create(C, "entry", F);
626 BasicBlock *ThenBB = BasicBlock::Create(C, "if.then", F);
627 BasicBlock *EndBB = BasicBlock::Create(C, "if.end", F);
Derek Schufff41f67d2016-08-01 21:34:04 +0000628
Derek Schuffccdceda2016-08-18 15:27:25 +0000629 IRB.SetInsertPoint(EntryBB);
630 Value *Threw = IRB.CreateLoad(ThrewGV, ThrewGV->getName() + ".val");
Heejin Ahnc0f18172016-09-01 21:05:15 +0000631 Value *Cmp = IRB.CreateICmpEQ(Threw, IRB.getInt32(0), "cmp");
Derek Schuffccdceda2016-08-18 15:27:25 +0000632 IRB.CreateCondBr(Cmp, ThenBB, EndBB);
Derek Schufff41f67d2016-08-01 21:34:04 +0000633
Derek Schuffccdceda2016-08-18 15:27:25 +0000634 IRB.SetInsertPoint(ThenBB);
635 IRB.CreateStore(Arg1, ThrewGV);
636 IRB.CreateStore(Arg2, ThrewValueGV);
637 IRB.CreateBr(EndBB);
Derek Schufff41f67d2016-08-01 21:34:04 +0000638
Derek Schuffccdceda2016-08-18 15:27:25 +0000639 IRB.SetInsertPoint(EndBB);
640 IRB.CreateRetVoid();
Heejin Ahnc0f18172016-09-01 21:05:15 +0000641}
Derek Schufff41f67d2016-08-01 21:34:04 +0000642
Heejin Ahnc0f18172016-09-01 21:05:15 +0000643// Create setTempRet0 function
644// function setTempRet0(value) {
645// __tempRet0 = value;
646// }
647void WebAssemblyLowerEmscriptenEHSjLj::createSetTempRet0Function(Module &M) {
648 LLVMContext &C = M.getContext();
649 IRBuilder<> IRB(C);
650
651 assert(!M.getNamedGlobal(SetTempRet0FName) && "setTempRet0 already exists");
652 Type *Params[] = {IRB.getInt32Ty()};
653 FunctionType *FTy = FunctionType::get(IRB.getVoidTy(), Params, false);
654 Function *F =
655 Function::Create(FTy, GlobalValue::ExternalLinkage, SetTempRet0FName, &M);
Derek Schufff41f67d2016-08-01 21:34:04 +0000656 F->arg_begin()->setName("value");
Heejin Ahnc0f18172016-09-01 21:05:15 +0000657 BasicBlock *EntryBB = BasicBlock::Create(C, "entry", F);
Derek Schuffccdceda2016-08-18 15:27:25 +0000658 IRB.SetInsertPoint(EntryBB);
659 IRB.CreateStore(&*F->arg_begin(), TempRet0GV);
660 IRB.CreateRetVoid();
Heejin Ahnc0f18172016-09-01 21:05:15 +0000661}
662
663void WebAssemblyLowerEmscriptenEHSjLj::rebuildSSA(Function &F) {
664 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
665 DT.recalculate(F); // CFG has been changed
666 SSAUpdater SSA;
667 for (BasicBlock &BB : F) {
668 for (Instruction &I : BB) {
669 for (auto UI = I.use_begin(), UE = I.use_end(); UI != UE;) {
670 Use &U = *UI;
671 ++UI;
672 SSA.Initialize(I.getType(), I.getName());
673 SSA.AddAvailableValue(&BB, &I);
674 Instruction *User = cast<Instruction>(U.getUser());
675 if (User->getParent() == &BB)
676 continue;
677
678 if (PHINode *UserPN = dyn_cast<PHINode>(User))
679 if (UserPN->getIncomingBlock(U) == &BB)
680 continue;
681
682 if (DT.dominates(&I, User))
683 continue;
684 SSA.RewriteUseAfterInsertions(U);
685 }
686 }
687 }
688}
689
690bool WebAssemblyLowerEmscriptenEHSjLj::runOnModule(Module &M) {
691 LLVMContext &C = M.getContext();
692 IRBuilder<> IRB(C);
693
694 Function *SetjmpF = M.getFunction("setjmp");
695 Function *LongjmpF = M.getFunction("longjmp");
696 bool SetjmpUsed = SetjmpF && !SetjmpF->use_empty();
697 bool LongjmpUsed = LongjmpF && !LongjmpF->use_empty();
698 bool DoSjLj = EnableSjLj && (SetjmpUsed || LongjmpUsed);
699
700 // Create global variables __THREW__, threwValue, and __tempRet0, which are
701 // used in common for both exception handling and setjmp/longjmp handling
702 ThrewGV = new GlobalVariable(M, IRB.getInt32Ty(), false,
703 GlobalValue::ExternalLinkage, IRB.getInt32(0),
704 createGlobalValueName(M, ThrewGVName));
705 ThrewValueGV = new GlobalVariable(
706 M, IRB.getInt32Ty(), false, GlobalValue::ExternalLinkage, IRB.getInt32(0),
707 createGlobalValueName(M, ThrewValueGVName));
708 TempRet0GV = new GlobalVariable(M, IRB.getInt32Ty(), false,
709 GlobalValue::ExternalLinkage, IRB.getInt32(0),
710 createGlobalValueName(M, TempRet0GVName));
711
712 bool Changed = false;
713
714 // Exception handling
715 if (EnableEH) {
716 // Register __resumeException function
717 FunctionType *ResumeFTy =
718 FunctionType::get(IRB.getVoidTy(), IRB.getInt8PtrTy(), false);
719 ResumeF = Function::Create(ResumeFTy, GlobalValue::ExternalLinkage,
720 ResumeFName, &M);
721
722 // Register llvm_eh_typeid_for function
723 FunctionType *EHTypeIDTy =
724 FunctionType::get(IRB.getInt32Ty(), IRB.getInt8PtrTy(), false);
725 EHTypeIDF = Function::Create(EHTypeIDTy, GlobalValue::ExternalLinkage,
726 EHTypeIDFName, &M);
727
728 for (Function &F : M) {
729 if (F.isDeclaration())
730 continue;
731 Changed |= runEHOnFunction(F);
732 }
733 }
734
735 // Setjmp/longjmp handling
736 if (DoSjLj) {
737 Changed = true; // We have setjmp or longjmp somewhere
738
739 Function *MallocF = M.getFunction("malloc");
740 Function *FreeF = M.getFunction("free");
741 if (!MallocF || !FreeF)
742 report_fatal_error(
743 "malloc and free must be linked into the module if setjmp is used");
744
745 // Register saveSetjmp function
746 FunctionType *SetjmpFTy = SetjmpF->getFunctionType();
747 SmallVector<Type *, 4> Params = {SetjmpFTy->getParamType(0),
748 IRB.getInt32Ty(), Type::getInt32PtrTy(C),
749 IRB.getInt32Ty()};
750 FunctionType *FTy =
751 FunctionType::get(Type::getInt32PtrTy(C), Params, false);
752 SaveSetjmpF = Function::Create(FTy, GlobalValue::ExternalLinkage,
753 SaveSetjmpFName, &M);
754
755 // Register testSetjmp function
756 Params = {IRB.getInt32Ty(), Type::getInt32PtrTy(C), IRB.getInt32Ty()};
757 FTy = FunctionType::get(IRB.getInt32Ty(), Params, false);
758 TestSetjmpF = Function::Create(FTy, GlobalValue::ExternalLinkage,
759 TestSetjmpFName, &M);
760
761 if (LongjmpF) {
762 // Replace all uses of longjmp with emscripten_longjmp_jmpbuf, which is
763 // defined in JS code
764 EmLongjmpJmpbufF = Function::Create(LongjmpF->getFunctionType(),
765 GlobalValue::ExternalLinkage,
766 EmLongjmpJmpbufFName, &M);
767
768 LongjmpF->replaceAllUsesWith(EmLongjmpJmpbufF);
769 }
770 FTy = FunctionType::get(IRB.getVoidTy(),
771 {IRB.getInt32Ty(), IRB.getInt32Ty()}, false);
772 EmLongjmpF =
773 Function::Create(FTy, GlobalValue::ExternalLinkage, EmLongjmpFName, &M);
774
775 // Only traverse functions that uses setjmp in order not to insert
776 // unnecessary prep / cleanup code in every function
777 SmallPtrSet<Function *, 8> SetjmpUsers;
778 for (User *U : SetjmpF->users()) {
779 auto *UI = cast<Instruction>(U);
780 SetjmpUsers.insert(UI->getFunction());
781 }
782 for (Function *F : SetjmpUsers)
783 runSjLjOnFunction(*F);
784 }
785
786 if (!Changed) {
787 // Delete unused global variables and functions
788 ThrewGV->eraseFromParent();
789 ThrewValueGV->eraseFromParent();
790 TempRet0GV->eraseFromParent();
791 if (ResumeF)
792 ResumeF->eraseFromParent();
793 if (EHTypeIDF)
794 EHTypeIDF->eraseFromParent();
795 if (EmLongjmpF)
796 EmLongjmpF->eraseFromParent();
797 if (SaveSetjmpF)
798 SaveSetjmpF->eraseFromParent();
799 if (TestSetjmpF)
800 TestSetjmpF->eraseFromParent();
801 return false;
802 }
803
804 // If we have made any changes while doing exception handling or
805 // setjmp/longjmp handling, we have to create these functions for JavaScript
806 // to call.
807 createSetThrewFunction(M);
808 createSetTempRet0Function(M);
Derek Schufff41f67d2016-08-01 21:34:04 +0000809
810 return true;
811}
812
Derek Schuffccdceda2016-08-18 15:27:25 +0000813bool WebAssemblyLowerEmscriptenEHSjLj::runEHOnFunction(Function &F) {
Derek Schufff41f67d2016-08-01 21:34:04 +0000814 Module &M = *F.getParent();
Derek Schuff53b9af02016-08-09 00:29:55 +0000815 LLVMContext &C = F.getContext();
Derek Schuffccdceda2016-08-18 15:27:25 +0000816 IRBuilder<> IRB(C);
Derek Schufff41f67d2016-08-01 21:34:04 +0000817 bool Changed = false;
818 SmallVector<Instruction *, 64> ToErase;
819 SmallPtrSet<LandingPadInst *, 32> LandingPads;
Derek Schuff66641322016-08-09 22:37:00 +0000820 bool AllowExceptions =
Derek Schuffccdceda2016-08-18 15:27:25 +0000821 areAllExceptionsAllowed() || EHWhitelistSet.count(F.getName());
Derek Schufff41f67d2016-08-01 21:34:04 +0000822
823 for (BasicBlock &BB : F) {
824 auto *II = dyn_cast<InvokeInst>(BB.getTerminator());
825 if (!II)
826 continue;
827 Changed = true;
828 LandingPads.insert(II->getLandingPadInst());
Derek Schuffccdceda2016-08-18 15:27:25 +0000829 IRB.SetInsertPoint(II);
Derek Schufff41f67d2016-08-01 21:34:04 +0000830
Derek Schuff53b9af02016-08-09 00:29:55 +0000831 bool NeedInvoke = AllowExceptions && canThrow(II->getCalledValue());
832 if (NeedInvoke) {
Heejin Ahnc0f18172016-09-01 21:05:15 +0000833 // Wrap invoke with invoke wrapper and generate preamble/postamble
834 Value *Threw = wrapInvoke(II);
Derek Schufff41f67d2016-08-01 21:34:04 +0000835 ToErase.push_back(II);
836
Derek Schufff41f67d2016-08-01 21:34:04 +0000837 // Insert a branch based on __THREW__ variable
Heejin Ahnc0f18172016-09-01 21:05:15 +0000838 Value *Cmp = IRB.CreateICmpEQ(Threw, IRB.getInt32(1), "cmp");
839 IRB.CreateCondBr(Cmp, II->getUnwindDest(), II->getNormalDest());
Derek Schufff41f67d2016-08-01 21:34:04 +0000840
841 } else {
842 // This can't throw, and we don't need this invoke, just replace it with a
843 // call+branch
Heejin Ahnc0f18172016-09-01 21:05:15 +0000844 SmallVector<Value *, 16> Args(II->arg_begin(), II->arg_end());
845 CallInst *NewCall = IRB.CreateCall(II->getCalledValue(), Args);
Derek Schufff41f67d2016-08-01 21:34:04 +0000846 NewCall->takeName(II);
847 NewCall->setCallingConv(II->getCallingConv());
Derek Schufff41f67d2016-08-01 21:34:04 +0000848 NewCall->setDebugLoc(II->getDebugLoc());
Derek Schuff53b9af02016-08-09 00:29:55 +0000849 NewCall->setAttributes(II->getAttributes());
Derek Schufff41f67d2016-08-01 21:34:04 +0000850 II->replaceAllUsesWith(NewCall);
851 ToErase.push_back(II);
852
Derek Schuffccdceda2016-08-18 15:27:25 +0000853 IRB.CreateBr(II->getNormalDest());
Derek Schufff41f67d2016-08-01 21:34:04 +0000854
855 // Remove any PHI node entries from the exception destination
856 II->getUnwindDest()->removePredecessor(&BB);
857 }
858 }
859
860 // Process resume instructions
861 for (BasicBlock &BB : F) {
862 // Scan the body of the basic block for resumes
863 for (Instruction &I : BB) {
864 auto *RI = dyn_cast<ResumeInst>(&I);
865 if (!RI)
866 continue;
867
868 // Split the input into legal values
869 Value *Input = RI->getValue();
Derek Schuffccdceda2016-08-18 15:27:25 +0000870 IRB.SetInsertPoint(RI);
871 Value *Low = IRB.CreateExtractValue(Input, 0, "low");
Derek Schuff53b9af02016-08-09 00:29:55 +0000872 // Create a call to __resumeException function
Heejin Ahnc0f18172016-09-01 21:05:15 +0000873 IRB.CreateCall(ResumeF, {Low});
Derek Schufff41f67d2016-08-01 21:34:04 +0000874 // Add a terminator to the block
Derek Schuffccdceda2016-08-18 15:27:25 +0000875 IRB.CreateUnreachable();
Derek Schufff41f67d2016-08-01 21:34:04 +0000876 ToErase.push_back(RI);
877 }
878 }
879
Derek Schuff53b9af02016-08-09 00:29:55 +0000880 // Process llvm.eh.typeid.for intrinsics
881 for (BasicBlock &BB : F) {
882 for (Instruction &I : BB) {
883 auto *CI = dyn_cast<CallInst>(&I);
884 if (!CI)
885 continue;
886 const Function *Callee = CI->getCalledFunction();
887 if (!Callee)
888 continue;
889 if (Callee->getIntrinsicID() != Intrinsic::eh_typeid_for)
890 continue;
891
Derek Schuffccdceda2016-08-18 15:27:25 +0000892 IRB.SetInsertPoint(CI);
Derek Schuff53b9af02016-08-09 00:29:55 +0000893 CallInst *NewCI =
Derek Schuffccdceda2016-08-18 15:27:25 +0000894 IRB.CreateCall(EHTypeIDF, CI->getArgOperand(0), "typeid");
Derek Schuff53b9af02016-08-09 00:29:55 +0000895 CI->replaceAllUsesWith(NewCI);
896 ToErase.push_back(CI);
897 }
898 }
899
Hiroshi Inouec3969642017-06-30 07:17:53 +0000900 // Look for orphan landingpads, can occur in blocks with no predecessors
Derek Schufff41f67d2016-08-01 21:34:04 +0000901 for (BasicBlock &BB : F) {
902 Instruction *I = BB.getFirstNonPHI();
903 if (auto *LPI = dyn_cast<LandingPadInst>(I))
904 LandingPads.insert(LPI);
905 }
906
907 // Handle all the landingpad for this function together, as multiple invokes
908 // may share a single lp
909 for (LandingPadInst *LPI : LandingPads) {
Derek Schuffccdceda2016-08-18 15:27:25 +0000910 IRB.SetInsertPoint(LPI);
Derek Schufff41f67d2016-08-01 21:34:04 +0000911 SmallVector<Value *, 16> FMCArgs;
912 for (unsigned i = 0, e = LPI->getNumClauses(); i < e; ++i) {
913 Constant *Clause = LPI->getClause(i);
914 // As a temporary workaround for the lack of aggregate varargs support
915 // in the interface between JS and wasm, break out filter operands into
916 // their component elements.
917 if (LPI->isFilter(i)) {
Heejin Ahnc0f18172016-09-01 21:05:15 +0000918 auto *ATy = cast<ArrayType>(Clause->getType());
Derek Schufff41f67d2016-08-01 21:34:04 +0000919 for (unsigned j = 0, e = ATy->getNumElements(); j < e; ++j) {
Derek Schuffccdceda2016-08-18 15:27:25 +0000920 Value *EV = IRB.CreateExtractValue(Clause, makeArrayRef(j), "filter");
Derek Schufff41f67d2016-08-01 21:34:04 +0000921 FMCArgs.push_back(EV);
922 }
923 } else
924 FMCArgs.push_back(Clause);
925 }
926
Derek Schuff53b9af02016-08-09 00:29:55 +0000927 // Create a call to __cxa_find_matching_catch_N function
Derek Schufff41f67d2016-08-01 21:34:04 +0000928 Function *FMCF = getFindMatchingCatch(M, FMCArgs.size());
Derek Schuffccdceda2016-08-18 15:27:25 +0000929 CallInst *FMCI = IRB.CreateCall(FMCF, FMCArgs, "fmc");
Derek Schufff41f67d2016-08-01 21:34:04 +0000930 Value *Undef = UndefValue::get(LPI->getType());
Derek Schuffccdceda2016-08-18 15:27:25 +0000931 Value *Pair0 = IRB.CreateInsertValue(Undef, FMCI, 0, "pair0");
Heejin Ahnc0f18172016-09-01 21:05:15 +0000932 Value *TempRet0 =
933 IRB.CreateLoad(TempRet0GV, TempRet0GV->getName() + ".val");
Derek Schuffccdceda2016-08-18 15:27:25 +0000934 Value *Pair1 = IRB.CreateInsertValue(Pair0, TempRet0, 1, "pair1");
Derek Schufff41f67d2016-08-01 21:34:04 +0000935
936 LPI->replaceAllUsesWith(Pair1);
937 ToErase.push_back(LPI);
938 }
939
940 // Erase everything we no longer need in this function
941 for (Instruction *I : ToErase)
942 I->eraseFromParent();
943
944 return Changed;
945}
Derek Schuffccdceda2016-08-18 15:27:25 +0000946
947bool WebAssemblyLowerEmscriptenEHSjLj::runSjLjOnFunction(Function &F) {
Heejin Ahnc0f18172016-09-01 21:05:15 +0000948 Module &M = *F.getParent();
949 LLVMContext &C = F.getContext();
950 IRBuilder<> IRB(C);
951 SmallVector<Instruction *, 64> ToErase;
952 // Vector of %setjmpTable values
953 std::vector<Instruction *> SetjmpTableInsts;
954 // Vector of %setjmpTableSize values
955 std::vector<Instruction *> SetjmpTableSizeInsts;
956
957 // Setjmp preparation
958
959 // This instruction effectively means %setjmpTableSize = 4.
960 // We create this as an instruction intentionally, and we don't want to fold
961 // this instruction to a constant 4, because this value will be used in
962 // SSAUpdater.AddAvailableValue(...) later.
963 BasicBlock &EntryBB = F.getEntryBlock();
964 BinaryOperator *SetjmpTableSize = BinaryOperator::Create(
965 Instruction::Add, IRB.getInt32(4), IRB.getInt32(0), "setjmpTableSize",
966 &*EntryBB.getFirstInsertionPt());
967 // setjmpTable = (int *) malloc(40);
968 Instruction *SetjmpTable = CallInst::CreateMalloc(
969 SetjmpTableSize, IRB.getInt32Ty(), IRB.getInt32Ty(), IRB.getInt32(40),
970 nullptr, nullptr, "setjmpTable");
971 // setjmpTable[0] = 0;
972 IRB.SetInsertPoint(SetjmpTableSize);
973 IRB.CreateStore(IRB.getInt32(0), SetjmpTable);
974 SetjmpTableInsts.push_back(SetjmpTable);
975 SetjmpTableSizeInsts.push_back(SetjmpTableSize);
976
977 // Setjmp transformation
978 std::vector<PHINode *> SetjmpRetPHIs;
979 Function *SetjmpF = M.getFunction("setjmp");
980 for (User *U : SetjmpF->users()) {
981 auto *CI = dyn_cast<CallInst>(U);
982 if (!CI)
983 report_fatal_error("Does not support indirect calls to setjmp");
984
985 BasicBlock *BB = CI->getParent();
986 if (BB->getParent() != &F) // in other function
987 continue;
988
989 // The tail is everything right after the call, and will be reached once
990 // when setjmp is called, and later when longjmp returns to the setjmp
991 BasicBlock *Tail = SplitBlock(BB, CI->getNextNode());
992 // Add a phi to the tail, which will be the output of setjmp, which
993 // indicates if this is the first call or a longjmp back. The phi directly
994 // uses the right value based on where we arrive from
995 IRB.SetInsertPoint(Tail->getFirstNonPHI());
996 PHINode *SetjmpRet = IRB.CreatePHI(IRB.getInt32Ty(), 2, "setjmp.ret");
997
998 // setjmp initial call returns 0
999 SetjmpRet->addIncoming(IRB.getInt32(0), BB);
1000 // The proper output is now this, not the setjmp call itself
1001 CI->replaceAllUsesWith(SetjmpRet);
1002 // longjmp returns to the setjmp will add themselves to this phi
1003 SetjmpRetPHIs.push_back(SetjmpRet);
1004
1005 // Fix call target
1006 // Our index in the function is our place in the array + 1 to avoid index
1007 // 0, because index 0 means the longjmp is not ours to handle.
1008 IRB.SetInsertPoint(CI);
1009 Value *Args[] = {CI->getArgOperand(0), IRB.getInt32(SetjmpRetPHIs.size()),
1010 SetjmpTable, SetjmpTableSize};
1011 Instruction *NewSetjmpTable =
1012 IRB.CreateCall(SaveSetjmpF, Args, "setjmpTable");
1013 Instruction *NewSetjmpTableSize =
1014 IRB.CreateLoad(TempRet0GV, "setjmpTableSize");
1015 SetjmpTableInsts.push_back(NewSetjmpTable);
1016 SetjmpTableSizeInsts.push_back(NewSetjmpTableSize);
1017 ToErase.push_back(CI);
1018 }
1019
1020 // Update each call that can longjmp so it can return to a setjmp where
1021 // relevant.
1022
1023 // Because we are creating new BBs while processing and don't want to make
1024 // all these newly created BBs candidates again for longjmp processing, we
1025 // first make the vector of candidate BBs.
1026 std::vector<BasicBlock *> BBs;
1027 for (BasicBlock &BB : F)
1028 BBs.push_back(&BB);
1029
1030 // BBs.size() will change within the loop, so we query it every time
1031 for (unsigned i = 0; i < BBs.size(); i++) {
1032 BasicBlock *BB = BBs[i];
1033 for (Instruction &I : *BB) {
1034 assert(!isa<InvokeInst>(&I));
1035 auto *CI = dyn_cast<CallInst>(&I);
1036 if (!CI)
1037 continue;
1038
1039 const Value *Callee = CI->getCalledValue();
1040 if (!canLongjmp(M, Callee))
1041 continue;
1042
1043 Value *Threw = nullptr;
1044 BasicBlock *Tail;
1045 if (Callee->getName().startswith(InvokePrefix)) {
1046 // If invoke wrapper has already been generated for this call in
1047 // previous EH phase, search for the load instruction
1048 // %__THREW__.val = __THREW__;
1049 // in postamble after the invoke wrapper call
1050 LoadInst *ThrewLI = nullptr;
1051 StoreInst *ThrewResetSI = nullptr;
1052 for (auto I = std::next(BasicBlock::iterator(CI)), IE = BB->end();
1053 I != IE; ++I) {
1054 if (auto *LI = dyn_cast<LoadInst>(I))
1055 if (auto *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand()))
1056 if (GV == ThrewGV) {
1057 Threw = ThrewLI = LI;
1058 break;
1059 }
1060 }
1061 // Search for the store instruction after the load above
1062 // __THREW__ = 0;
1063 for (auto I = std::next(BasicBlock::iterator(ThrewLI)), IE = BB->end();
1064 I != IE; ++I) {
1065 if (auto *SI = dyn_cast<StoreInst>(I))
1066 if (auto *GV = dyn_cast<GlobalVariable>(SI->getPointerOperand()))
1067 if (GV == ThrewGV && SI->getValueOperand() == IRB.getInt32(0)) {
1068 ThrewResetSI = SI;
1069 break;
1070 }
1071 }
1072 assert(Threw && ThrewLI && "Cannot find __THREW__ load after invoke");
1073 assert(ThrewResetSI && "Cannot find __THREW__ store after invoke");
1074 Tail = SplitBlock(BB, ThrewResetSI->getNextNode());
1075
1076 } else {
1077 // Wrap call with invoke wrapper and generate preamble/postamble
1078 Threw = wrapInvoke(CI);
1079 ToErase.push_back(CI);
1080 Tail = SplitBlock(BB, CI->getNextNode());
1081 }
1082
1083 // We need to replace the terminator in Tail - SplitBlock makes BB go
1084 // straight to Tail, we need to check if a longjmp occurred, and go to the
1085 // right setjmp-tail if so
1086 ToErase.push_back(BB->getTerminator());
1087
1088 // Generate a function call to testSetjmp function and preamble/postamble
1089 // code to figure out (1) whether longjmp occurred (2) if longjmp
1090 // occurred, which setjmp it corresponds to
1091 Value *Label = nullptr;
1092 Value *LongjmpResult = nullptr;
1093 BasicBlock *EndBB = nullptr;
1094 wrapTestSetjmp(BB, CI, Threw, SetjmpTable, SetjmpTableSize, Label,
1095 LongjmpResult, EndBB);
1096 assert(Label && LongjmpResult && EndBB);
1097
1098 // Create switch instruction
1099 IRB.SetInsertPoint(EndBB);
1100 SwitchInst *SI = IRB.CreateSwitch(Label, Tail, SetjmpRetPHIs.size());
1101 // -1 means no longjmp happened, continue normally (will hit the default
1102 // switch case). 0 means a longjmp that is not ours to handle, needs a
1103 // rethrow. Otherwise the index is the same as the index in P+1 (to avoid
1104 // 0).
1105 for (unsigned i = 0; i < SetjmpRetPHIs.size(); i++) {
1106 SI->addCase(IRB.getInt32(i + 1), SetjmpRetPHIs[i]->getParent());
1107 SetjmpRetPHIs[i]->addIncoming(LongjmpResult, EndBB);
1108 }
1109
1110 // We are splitting the block here, and must continue to find other calls
1111 // in the block - which is now split. so continue to traverse in the Tail
1112 BBs.push_back(Tail);
1113 }
1114 }
1115
1116 // Erase everything we no longer need in this function
1117 for (Instruction *I : ToErase)
1118 I->eraseFromParent();
1119
1120 // Free setjmpTable buffer before each return instruction
1121 for (BasicBlock &BB : F) {
1122 TerminatorInst *TI = BB.getTerminator();
1123 if (isa<ReturnInst>(TI))
1124 CallInst::CreateFree(SetjmpTable, TI);
1125 }
1126
1127 // Every call to saveSetjmp can change setjmpTable and setjmpTableSize
1128 // (when buffer reallocation occurs)
1129 // entry:
1130 // setjmpTableSize = 4;
1131 // setjmpTable = (int *) malloc(40);
1132 // setjmpTable[0] = 0;
1133 // ...
1134 // somebb:
1135 // setjmpTable = saveSetjmp(buf, label, setjmpTable, setjmpTableSize);
1136 // setjmpTableSize = __tempRet0;
1137 // So we need to make sure the SSA for these variables is valid so that every
1138 // saveSetjmp and testSetjmp calls have the correct arguments.
1139 SSAUpdater SetjmpTableSSA;
1140 SSAUpdater SetjmpTableSizeSSA;
1141 SetjmpTableSSA.Initialize(Type::getInt32PtrTy(C), "setjmpTable");
1142 SetjmpTableSizeSSA.Initialize(Type::getInt32Ty(C), "setjmpTableSize");
1143 for (Instruction *I : SetjmpTableInsts)
1144 SetjmpTableSSA.AddAvailableValue(I->getParent(), I);
1145 for (Instruction *I : SetjmpTableSizeInsts)
1146 SetjmpTableSizeSSA.AddAvailableValue(I->getParent(), I);
1147
1148 for (auto UI = SetjmpTable->use_begin(), UE = SetjmpTable->use_end();
1149 UI != UE;) {
1150 // Grab the use before incrementing the iterator.
1151 Use &U = *UI;
1152 // Increment the iterator before removing the use from the list.
1153 ++UI;
1154 if (Instruction *I = dyn_cast<Instruction>(U.getUser()))
1155 if (I->getParent() != &EntryBB)
1156 SetjmpTableSSA.RewriteUse(U);
1157 }
1158 for (auto UI = SetjmpTableSize->use_begin(), UE = SetjmpTableSize->use_end();
1159 UI != UE;) {
1160 Use &U = *UI;
1161 ++UI;
1162 if (Instruction *I = dyn_cast<Instruction>(U.getUser()))
1163 if (I->getParent() != &EntryBB)
1164 SetjmpTableSizeSSA.RewriteUse(U);
1165 }
1166
1167 // Finally, our modifications to the cfg can break dominance of SSA variables.
1168 // For example, in this code,
1169 // if (x()) { .. setjmp() .. }
1170 // if (y()) { .. longjmp() .. }
1171 // We must split the longjmp block, and it can jump into the block splitted
1172 // from setjmp one. But that means that when we split the setjmp block, it's
1173 // first part no longer dominates its second part - there is a theoretically
1174 // possible control flow path where x() is false, then y() is true and we
1175 // reach the second part of the setjmp block, without ever reaching the first
1176 // part. So, we rebuild SSA form here.
1177 rebuildSSA(F);
1178 return true;
Derek Schuffccdceda2016-08-18 15:27:25 +00001179}