Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 1 | //===-- WasmEHPrepare - Prepare excepton handling for WebAssembly --------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This transformation is designed for use by code generators which use |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 10 | // WebAssembly exception handling scheme. This currently supports C++ |
| 11 | // exceptions. |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 12 | // |
| 13 | // WebAssembly exception handling uses Windows exception IR for the middle level |
| 14 | // representation. This pass does the following transformation for every |
| 15 | // catchpad block: |
| 16 | // (In C-style pseudocode) |
| 17 | // |
| 18 | // - Before: |
| 19 | // catchpad ... |
| 20 | // exn = wasm.get.exception(); |
| 21 | // selector = wasm.get.selector(); |
| 22 | // ... |
| 23 | // |
| 24 | // - After: |
| 25 | // catchpad ... |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 26 | // exn = wasm.extract.exception(); |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 27 | // // Only add below in case it's not a single catch (...) |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 28 | // wasm.landingpad.index(index); |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 29 | // __wasm_lpad_context.lpad_index = index; |
| 30 | // __wasm_lpad_context.lsda = wasm.lsda(); |
| 31 | // _Unwind_CallPersonality(exn); |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 32 | // selector = __wasm.landingpad_context.selector; |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 33 | // ... |
| 34 | // |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 35 | // |
| 36 | // * Background: Direct personality function call |
| 37 | // In WebAssembly EH, the VM is responsible for unwinding the stack once an |
| 38 | // exception is thrown. After the stack is unwound, the control flow is |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 39 | // transfered to WebAssembly 'catch' instruction. |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 40 | // |
| 41 | // Unwinding the stack is not done by libunwind but the VM, so the personality |
| 42 | // function in libcxxabi cannot be called from libunwind during the unwinding |
| 43 | // process. So after a catch instruction, we insert a call to a wrapper function |
| 44 | // in libunwind that in turn calls the real personality function. |
| 45 | // |
| 46 | // In Itanium EH, if the personality function decides there is no matching catch |
| 47 | // clause in a call frame and no cleanup action to perform, the unwinder doesn't |
| 48 | // stop there and continues unwinding. But in Wasm EH, the unwinder stops at |
| 49 | // every call frame with a catch intruction, after which the personality |
| 50 | // function is called from the compiler-generated user code here. |
| 51 | // |
| 52 | // In libunwind, we have this struct that serves as a communincation channel |
| 53 | // between the compiler-generated user code and the personality function in |
| 54 | // libcxxabi. |
| 55 | // |
| 56 | // struct _Unwind_LandingPadContext { |
| 57 | // uintptr_t lpad_index; |
| 58 | // uintptr_t lsda; |
| 59 | // uintptr_t selector; |
| 60 | // }; |
| 61 | // struct _Unwind_LandingPadContext __wasm_lpad_context = ...; |
| 62 | // |
| 63 | // And this wrapper in libunwind calls the personality function. |
| 64 | // |
| 65 | // _Unwind_Reason_Code _Unwind_CallPersonality(void *exception_ptr) { |
| 66 | // struct _Unwind_Exception *exception_obj = |
| 67 | // (struct _Unwind_Exception *)exception_ptr; |
| 68 | // _Unwind_Reason_Code ret = __gxx_personality_v0( |
| 69 | // 1, _UA_CLEANUP_PHASE, exception_obj->exception_class, exception_obj, |
| 70 | // (struct _Unwind_Context *)__wasm_lpad_context); |
| 71 | // return ret; |
| 72 | // } |
| 73 | // |
| 74 | // We pass a landing pad index, and the address of LSDA for the current function |
| 75 | // to the wrapper function _Unwind_CallPersonality in libunwind, and we retrieve |
| 76 | // the selector after it returns. |
| 77 | // |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | |
| 80 | #include "llvm/ADT/SetVector.h" |
| 81 | #include "llvm/ADT/Statistic.h" |
| 82 | #include "llvm/ADT/Triple.h" |
| 83 | #include "llvm/CodeGen/Passes.h" |
| 84 | #include "llvm/CodeGen/TargetLowering.h" |
| 85 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Heejin Ahn | 33c3fce | 2018-06-19 00:26:39 +0000 | [diff] [blame] | 86 | #include "llvm/CodeGen/WasmEHFuncInfo.h" |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 87 | #include "llvm/IR/Dominators.h" |
| 88 | #include "llvm/IR/IRBuilder.h" |
| 89 | #include "llvm/IR/Intrinsics.h" |
| 90 | #include "llvm/Pass.h" |
| 91 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 92 | |
| 93 | using namespace llvm; |
| 94 | |
| 95 | #define DEBUG_TYPE "wasmehprepare" |
| 96 | |
| 97 | namespace { |
| 98 | class WasmEHPrepare : public FunctionPass { |
| 99 | Type *LPadContextTy = nullptr; // type of 'struct _Unwind_LandingPadContext' |
| 100 | GlobalVariable *LPadContextGV = nullptr; // __wasm_lpad_context |
| 101 | |
| 102 | // Field addresses of struct _Unwind_LandingPadContext |
| 103 | Value *LPadIndexField = nullptr; // lpad_index field |
| 104 | Value *LSDAField = nullptr; // lsda field |
| 105 | Value *SelectorField = nullptr; // selector |
| 106 | |
Heejin Ahn | 095796a | 2018-11-16 00:47:18 +0000 | [diff] [blame] | 107 | Function *ThrowF = nullptr; // wasm.throw() intrinsic |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 108 | Function *RethrowF = nullptr; // wasm.rethrow() intrinsic |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 109 | Function *LPadIndexF = nullptr; // wasm.landingpad.index() intrinsic |
| 110 | Function *LSDAF = nullptr; // wasm.lsda() intrinsic |
| 111 | Function *GetExnF = nullptr; // wasm.get.exception() intrinsic |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 112 | Function *ExtractExnF = nullptr; // wasm.extract.exception() intrinsic |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 113 | Function *GetSelectorF = nullptr; // wasm.get.ehselector() intrinsic |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 114 | FunctionCallee CallPersonalityF = |
| 115 | nullptr; // _Unwind_CallPersonality() wrapper |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 116 | |
Heejin Ahn | 095796a | 2018-11-16 00:47:18 +0000 | [diff] [blame] | 117 | bool prepareEHPads(Function &F); |
| 118 | bool prepareThrows(Function &F); |
| 119 | |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 120 | void prepareEHPad(BasicBlock *BB, bool NeedLSDA, unsigned Index = 0); |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 121 | void prepareTerminateCleanupPad(BasicBlock *BB); |
| 122 | |
| 123 | public: |
| 124 | static char ID; // Pass identification, replacement for typeid |
| 125 | |
| 126 | WasmEHPrepare() : FunctionPass(ID) {} |
| 127 | |
| 128 | bool doInitialization(Module &M) override; |
| 129 | bool runOnFunction(Function &F) override; |
| 130 | |
| 131 | StringRef getPassName() const override { |
| 132 | return "WebAssembly Exception handling preparation"; |
| 133 | } |
| 134 | }; |
| 135 | } // end anonymous namespace |
| 136 | |
| 137 | char WasmEHPrepare::ID = 0; |
| 138 | INITIALIZE_PASS(WasmEHPrepare, DEBUG_TYPE, "Prepare WebAssembly exceptions", |
Gabor Buella | 27c96d3 | 2018-06-01 07:47:46 +0000 | [diff] [blame] | 139 | false, false) |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 140 | |
| 141 | FunctionPass *llvm::createWasmEHPass() { return new WasmEHPrepare(); } |
| 142 | |
| 143 | bool WasmEHPrepare::doInitialization(Module &M) { |
| 144 | IRBuilder<> IRB(M.getContext()); |
| 145 | LPadContextTy = StructType::get(IRB.getInt32Ty(), // lpad_index |
| 146 | IRB.getInt8PtrTy(), // lsda |
| 147 | IRB.getInt32Ty() // selector |
| 148 | ); |
| 149 | return false; |
| 150 | } |
| 151 | |
Heejin Ahn | 095796a | 2018-11-16 00:47:18 +0000 | [diff] [blame] | 152 | // Erase the specified BBs if the BB does not have any remaining predecessors, |
| 153 | // and also all its dead children. |
| 154 | template <typename Container> |
| 155 | static void eraseDeadBBsAndChildren(const Container &BBs) { |
| 156 | SmallVector<BasicBlock *, 8> WL(BBs.begin(), BBs.end()); |
| 157 | while (!WL.empty()) { |
| 158 | auto *BB = WL.pop_back_val(); |
| 159 | if (pred_begin(BB) != pred_end(BB)) |
| 160 | continue; |
| 161 | WL.append(succ_begin(BB), succ_end(BB)); |
| 162 | DeleteDeadBlock(BB); |
| 163 | } |
| 164 | } |
| 165 | |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 166 | bool WasmEHPrepare::runOnFunction(Function &F) { |
Heejin Ahn | 095796a | 2018-11-16 00:47:18 +0000 | [diff] [blame] | 167 | bool Changed = false; |
| 168 | Changed |= prepareThrows(F); |
| 169 | Changed |= prepareEHPads(F); |
| 170 | return Changed; |
| 171 | } |
| 172 | |
| 173 | bool WasmEHPrepare::prepareThrows(Function &F) { |
| 174 | Module &M = *F.getParent(); |
| 175 | IRBuilder<> IRB(F.getContext()); |
| 176 | bool Changed = false; |
| 177 | |
| 178 | // wasm.throw() intinsic, which will be lowered to wasm 'throw' instruction. |
| 179 | ThrowF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_throw); |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 180 | // wasm.rethrow() intinsic, which will be lowered to wasm 'rethrow' |
| 181 | // instruction. |
| 182 | RethrowF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_rethrow); |
Heejin Ahn | 095796a | 2018-11-16 00:47:18 +0000 | [diff] [blame] | 183 | |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 184 | // Insert an unreachable instruction after a call to @llvm.wasm.throw / |
| 185 | // @llvm.wasm.rethrow and delete all following instructions within the BB, and |
| 186 | // delete all the dead children of the BB as well. |
| 187 | for (auto L : {ThrowF->users(), RethrowF->users()}) { |
| 188 | for (User *U : L) { |
| 189 | // A call to @llvm.wasm.throw() is only generated from __cxa_throw() |
| 190 | // builtin call within libcxxabi, and cannot be an InvokeInst. |
| 191 | auto *ThrowI = cast<CallInst>(U); |
| 192 | if (ThrowI->getFunction() != &F) |
| 193 | continue; |
| 194 | Changed = true; |
| 195 | auto *BB = ThrowI->getParent(); |
| 196 | SmallVector<BasicBlock *, 4> Succs(succ_begin(BB), succ_end(BB)); |
| 197 | auto &InstList = BB->getInstList(); |
| 198 | InstList.erase(std::next(BasicBlock::iterator(ThrowI)), InstList.end()); |
| 199 | IRB.SetInsertPoint(BB); |
| 200 | IRB.CreateUnreachable(); |
| 201 | eraseDeadBBsAndChildren(Succs); |
| 202 | } |
Heejin Ahn | 095796a | 2018-11-16 00:47:18 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | return Changed; |
| 206 | } |
| 207 | |
| 208 | bool WasmEHPrepare::prepareEHPads(Function &F) { |
| 209 | Module &M = *F.getParent(); |
| 210 | IRBuilder<> IRB(F.getContext()); |
| 211 | |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 212 | SmallVector<BasicBlock *, 16> CatchPads; |
| 213 | SmallVector<BasicBlock *, 16> CleanupPads; |
| 214 | for (BasicBlock &BB : F) { |
| 215 | if (!BB.isEHPad()) |
| 216 | continue; |
| 217 | auto *Pad = BB.getFirstNonPHI(); |
| 218 | if (isa<CatchPadInst>(Pad)) |
| 219 | CatchPads.push_back(&BB); |
| 220 | else if (isa<CleanupPadInst>(Pad)) |
| 221 | CleanupPads.push_back(&BB); |
| 222 | } |
| 223 | |
| 224 | if (CatchPads.empty() && CleanupPads.empty()) |
| 225 | return false; |
| 226 | assert(F.hasPersonalityFn() && "Personality function not found"); |
| 227 | |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 228 | // __wasm_lpad_context global variable |
| 229 | LPadContextGV = cast<GlobalVariable>( |
| 230 | M.getOrInsertGlobal("__wasm_lpad_context", LPadContextTy)); |
| 231 | LPadIndexField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 0, |
| 232 | "lpad_index_gep"); |
| 233 | LSDAField = |
| 234 | IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 1, "lsda_gep"); |
| 235 | SelectorField = IRB.CreateConstGEP2_32(LPadContextTy, LPadContextGV, 0, 2, |
| 236 | "selector_gep"); |
| 237 | |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 238 | // wasm.landingpad.index() intrinsic, which is to specify landingpad index |
| 239 | LPadIndexF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_landingpad_index); |
| 240 | // wasm.lsda() intrinsic. Returns the address of LSDA table for the current |
| 241 | // function. |
| 242 | LSDAF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_lsda); |
| 243 | // wasm.get.exception() and wasm.get.ehselector() intrinsics. Calls to these |
| 244 | // are generated in clang. |
| 245 | GetExnF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_get_exception); |
| 246 | GetSelectorF = Intrinsic::getDeclaration(&M, Intrinsic::wasm_get_ehselector); |
| 247 | |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 248 | // wasm.extract.exception() is the same as wasm.get.exception() but it does |
| 249 | // not take a token argument. This will be lowered down to EXTRACT_EXCEPTION |
| 250 | // pseudo instruction in instruction selection, which will be expanded using |
| 251 | // 'br_on_exn' instruction later. |
| 252 | ExtractExnF = |
| 253 | Intrinsic::getDeclaration(&M, Intrinsic::wasm_extract_exception); |
| 254 | |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 255 | // _Unwind_CallPersonality() wrapper function, which calls the personality |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 256 | CallPersonalityF = M.getOrInsertFunction( |
| 257 | "_Unwind_CallPersonality", IRB.getInt32Ty(), IRB.getInt8PtrTy()); |
| 258 | if (Function *F = dyn_cast<Function>(CallPersonalityF.getCallee())) |
| 259 | F->setDoesNotThrow(); |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 260 | |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 261 | unsigned Index = 0; |
| 262 | for (auto *BB : CatchPads) { |
| 263 | auto *CPI = cast<CatchPadInst>(BB->getFirstNonPHI()); |
| 264 | // In case of a single catch (...), we don't need to emit LSDA |
| 265 | if (CPI->getNumArgOperands() == 1 && |
| 266 | cast<Constant>(CPI->getArgOperand(0))->isNullValue()) |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 267 | prepareEHPad(BB, false); |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 268 | else |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 269 | prepareEHPad(BB, true, Index++); |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 272 | // Cleanup pads don't need LSDA. |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 273 | for (auto *BB : CleanupPads) |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 274 | prepareEHPad(BB, false); |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 275 | |
| 276 | return true; |
| 277 | } |
| 278 | |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 279 | // Prepare an EH pad for Wasm EH handling. If NeedLSDA is false, Index is |
| 280 | // ignored. |
| 281 | void WasmEHPrepare::prepareEHPad(BasicBlock *BB, bool NeedLSDA, |
| 282 | unsigned Index) { |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 283 | assert(BB->isEHPad() && "BB is not an EHPad!"); |
| 284 | IRBuilder<> IRB(BB->getContext()); |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 285 | IRB.SetInsertPoint(&*BB->getFirstInsertionPt()); |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 286 | |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 287 | auto *FPI = cast<FuncletPadInst>(BB->getFirstNonPHI()); |
| 288 | Instruction *GetExnCI = nullptr, *GetSelectorCI = nullptr; |
| 289 | for (auto &U : FPI->uses()) { |
| 290 | if (auto *CI = dyn_cast<CallInst>(U.getUser())) { |
| 291 | if (CI->getCalledValue() == GetExnF) |
| 292 | GetExnCI = CI; |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 293 | if (CI->getCalledValue() == GetSelectorF) |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 294 | GetSelectorCI = CI; |
| 295 | } |
| 296 | } |
| 297 | |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 298 | // Cleanup pads w/o __clang_call_terminate call do not have any of |
| 299 | // wasm.get.exception() or wasm.get.ehselector() calls. We need to do nothing. |
| 300 | if (!GetExnCI) { |
| 301 | assert(!GetSelectorCI && |
| 302 | "wasm.get.ehselector() cannot exist w/o wasm.get.exception()"); |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | Instruction *ExtractExnCI = IRB.CreateCall(ExtractExnF, {}, "exn"); |
| 307 | GetExnCI->replaceAllUsesWith(ExtractExnCI); |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 308 | GetExnCI->eraseFromParent(); |
| 309 | |
| 310 | // In case it is a catchpad with single catch (...) or a cleanuppad, we don't |
| 311 | // need to call personality function because we don't need a selector. |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 312 | if (!NeedLSDA) { |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 313 | if (GetSelectorCI) { |
| 314 | assert(GetSelectorCI->use_empty() && |
| 315 | "wasm.get.ehselector() still has uses!"); |
| 316 | GetSelectorCI->eraseFromParent(); |
| 317 | } |
| 318 | return; |
| 319 | } |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 320 | IRB.SetInsertPoint(ExtractExnCI->getNextNode()); |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 321 | |
| 322 | // This is to create a map of <landingpad EH label, landingpad index> in |
| 323 | // SelectionDAGISel, which is to be used in EHStreamer to emit LSDA tables. |
| 324 | // Pseudocode: wasm.landingpad.index(Index); |
Heejin Ahn | 24faf85 | 2018-10-25 23:55:10 +0000 | [diff] [blame] | 325 | IRB.CreateCall(LPadIndexF, {FPI, IRB.getInt32(Index)}); |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 326 | |
| 327 | // Pseudocode: __wasm_lpad_context.lpad_index = index; |
| 328 | IRB.CreateStore(IRB.getInt32(Index), LPadIndexField); |
| 329 | |
| 330 | // Store LSDA address only if this catchpad belongs to a top-level |
| 331 | // catchswitch. If there is another catchpad that dominates this pad, we don't |
| 332 | // need to store LSDA address again, because they are the same throughout the |
| 333 | // function and have been already stored before. |
| 334 | // TODO Can we not store LSDA address in user function but make libcxxabi |
| 335 | // compute it? |
| 336 | auto *CPI = cast<CatchPadInst>(FPI); |
| 337 | if (isa<ConstantTokenNone>(CPI->getCatchSwitch()->getParentPad())) |
| 338 | // Pseudocode: __wasm_lpad_context.lsda = wasm.lsda(); |
| 339 | IRB.CreateStore(IRB.CreateCall(LSDAF), LSDAField); |
| 340 | |
| 341 | // Pseudocode: _Unwind_CallPersonality(exn); |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 342 | CallInst *PersCI = IRB.CreateCall(CallPersonalityF, ExtractExnCI, |
| 343 | OperandBundleDef("funclet", CPI)); |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 344 | PersCI->setDoesNotThrow(); |
| 345 | |
| 346 | // Pseudocode: int selector = __wasm.landingpad_context.selector; |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 347 | Instruction *Selector = |
| 348 | IRB.CreateLoad(IRB.getInt32Ty(), SelectorField, "selector"); |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 349 | |
| 350 | // Replace the return value from wasm.get.ehselector() with the selector value |
| 351 | // loaded from __wasm_lpad_context.selector. |
| 352 | assert(GetSelectorCI && "wasm.get.ehselector() call does not exist"); |
| 353 | GetSelectorCI->replaceAllUsesWith(Selector); |
| 354 | GetSelectorCI->eraseFromParent(); |
| 355 | } |
Heejin Ahn | 33c3fce | 2018-06-19 00:26:39 +0000 | [diff] [blame] | 356 | |
| 357 | void llvm::calculateWasmEHInfo(const Function *F, WasmEHFuncInfo &EHInfo) { |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 358 | // If an exception is not caught by a catchpad (i.e., it is a foreign |
| 359 | // exception), it will unwind to its parent catchswitch's unwind destination. |
| 360 | // We don't record an unwind destination for cleanuppads because every |
| 361 | // exception should be caught by it. |
Heejin Ahn | 33c3fce | 2018-06-19 00:26:39 +0000 | [diff] [blame] | 362 | for (const auto &BB : *F) { |
| 363 | if (!BB.isEHPad()) |
| 364 | continue; |
| 365 | const Instruction *Pad = BB.getFirstNonPHI(); |
| 366 | |
Heejin Ahn | 33c3fce | 2018-06-19 00:26:39 +0000 | [diff] [blame] | 367 | if (const auto *CatchPad = dyn_cast<CatchPadInst>(Pad)) { |
| 368 | const auto *UnwindBB = CatchPad->getCatchSwitch()->getUnwindDest(); |
| 369 | if (!UnwindBB) |
| 370 | continue; |
| 371 | const Instruction *UnwindPad = UnwindBB->getFirstNonPHI(); |
| 372 | if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(UnwindPad)) |
| 373 | // Currently there should be only one handler per a catchswitch. |
| 374 | EHInfo.setEHPadUnwindDest(&BB, *CatchSwitch->handlers().begin()); |
| 375 | else // cleanuppad |
| 376 | EHInfo.setEHPadUnwindDest(&BB, UnwindBB); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | // Record the unwind destination for invoke and cleanupret instructions. |
| 381 | for (const auto &BB : *F) { |
| 382 | const Instruction *TI = BB.getTerminator(); |
| 383 | BasicBlock *UnwindBB = nullptr; |
| 384 | if (const auto *Invoke = dyn_cast<InvokeInst>(TI)) |
| 385 | UnwindBB = Invoke->getUnwindDest(); |
| 386 | else if (const auto *CleanupRet = dyn_cast<CleanupReturnInst>(TI)) |
| 387 | UnwindBB = CleanupRet->getUnwindDest(); |
| 388 | if (!UnwindBB) |
| 389 | continue; |
| 390 | const Instruction *UnwindPad = UnwindBB->getFirstNonPHI(); |
| 391 | if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(UnwindPad)) |
| 392 | // Currently there should be only one handler per a catchswitch. |
| 393 | EHInfo.setThrowUnwindDest(&BB, *CatchSwitch->handlers().begin()); |
| 394 | else // cleanuppad |
| 395 | EHInfo.setThrowUnwindDest(&BB, UnwindBB); |
| 396 | } |
| 397 | } |