blob: 5b6fdb86c6d6e36091204f6a6bdd93a82713228c [file] [log] [blame]
Heejin Ahn4934f762018-06-25 01:07:11 +00001//=== WebAssemblyLateEHPrepare.cpp - WebAssembly Exception Preparation -===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Ahn4934f762018-06-25 01:07:11 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// \brief Does various transformations for exception handling.
11///
12//===----------------------------------------------------------------------===//
13
14#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
15#include "WebAssembly.h"
16#include "WebAssemblySubtarget.h"
17#include "WebAssemblyUtilities.h"
Heejin Ahnd6f48782019-01-30 03:21:57 +000018#include "llvm/ADT/SmallSet.h"
Heejin Ahn4934f762018-06-25 01:07:11 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/WasmEHFuncInfo.h"
21#include "llvm/MC/MCAsmInfo.h"
22using namespace llvm;
23
Heejin Ahn8b49b6b2019-03-13 00:37:31 +000024#define DEBUG_TYPE "wasm-late-eh-prepare"
Heejin Ahn4934f762018-06-25 01:07:11 +000025
26namespace {
27class WebAssemblyLateEHPrepare final : public MachineFunctionPass {
28 StringRef getPassName() const override {
Heejin Ahnd6f48782019-01-30 03:21:57 +000029 return "WebAssembly Late Prepare Exception";
Heejin Ahn4934f762018-06-25 01:07:11 +000030 }
31
32 bool runOnMachineFunction(MachineFunction &MF) override;
Heejin Ahnd6f48782019-01-30 03:21:57 +000033 bool addCatches(MachineFunction &MF);
Heejin Ahnb47a18c2019-03-16 04:46:05 +000034 bool replaceFuncletReturns(MachineFunction &MF);
35 bool removeUnnecessaryUnreachables(MachineFunction &MF);
Heejin Ahnd6f48782019-01-30 03:21:57 +000036 bool addExceptionExtraction(MachineFunction &MF);
Heejin Ahn0bb98652019-01-30 22:44:45 +000037 bool restoreStackPointer(MachineFunction &MF);
Heejin Ahn4934f762018-06-25 01:07:11 +000038
39public:
40 static char ID; // Pass identification, replacement for typeid
41 WebAssemblyLateEHPrepare() : MachineFunctionPass(ID) {}
42};
43} // end anonymous namespace
44
45char WebAssemblyLateEHPrepare::ID = 0;
46INITIALIZE_PASS(WebAssemblyLateEHPrepare, DEBUG_TYPE,
Heejin Ahna93e7262018-08-17 00:12:04 +000047 "WebAssembly Late Exception Preparation", false, false)
Heejin Ahn4934f762018-06-25 01:07:11 +000048
49FunctionPass *llvm::createWebAssemblyLateEHPrepare() {
50 return new WebAssemblyLateEHPrepare();
51}
52
53// Returns the nearest EH pad that dominates this instruction. This does not use
54// dominator analysis; it just does BFS on its predecessors until arriving at an
55// EH pad. This assumes valid EH scopes so the first EH pad it arrives in all
56// possible search paths should be the same.
57// Returns nullptr in case it does not find any EH pad in the search, or finds
58// multiple different EH pads.
Heejin Ahn095796a2018-11-16 00:47:18 +000059static MachineBasicBlock *getMatchingEHPad(MachineInstr *MI) {
Heejin Ahn4934f762018-06-25 01:07:11 +000060 MachineFunction *MF = MI->getParent()->getParent();
61 SmallVector<MachineBasicBlock *, 2> WL;
62 SmallPtrSet<MachineBasicBlock *, 2> Visited;
63 WL.push_back(MI->getParent());
64 MachineBasicBlock *EHPad = nullptr;
65 while (!WL.empty()) {
66 MachineBasicBlock *MBB = WL.pop_back_val();
67 if (Visited.count(MBB))
68 continue;
69 Visited.insert(MBB);
70 if (MBB->isEHPad()) {
71 if (EHPad && EHPad != MBB)
72 return nullptr;
73 EHPad = MBB;
74 continue;
75 }
76 if (MBB == &MF->front())
77 return nullptr;
78 WL.append(MBB->pred_begin(), MBB->pred_end());
79 }
80 return EHPad;
81}
82
Heejin Ahn095796a2018-11-16 00:47:18 +000083// Erase the specified BBs if the BB does not have any remaining predecessors,
84// and also all its dead children.
Benjamin Kramer83996e42018-08-08 10:13:19 +000085template <typename Container>
Heejin Ahn095796a2018-11-16 00:47:18 +000086static void eraseDeadBBsAndChildren(const Container &MBBs) {
Benjamin Kramer83996e42018-08-08 10:13:19 +000087 SmallVector<MachineBasicBlock *, 8> WL(MBBs.begin(), MBBs.end());
Heejin Ahn4934f762018-06-25 01:07:11 +000088 while (!WL.empty()) {
89 MachineBasicBlock *MBB = WL.pop_back_val();
Heejin Ahn095796a2018-11-16 00:47:18 +000090 if (!MBB->pred_empty())
91 continue;
Heejin Ahnb68d5912018-10-04 21:03:35 +000092 SmallVector<MachineBasicBlock *, 4> Succs(MBB->succ_begin(),
93 MBB->succ_end());
94 WL.append(MBB->succ_begin(), MBB->succ_end());
95 for (auto *Succ : Succs)
Heejin Ahn4934f762018-06-25 01:07:11 +000096 MBB->removeSuccessor(Succ);
Heejin Ahn4934f762018-06-25 01:07:11 +000097 MBB->eraseFromParent();
98 }
99}
100
101bool WebAssemblyLateEHPrepare::runOnMachineFunction(MachineFunction &MF) {
Heejin Ahn569f0902019-01-09 23:05:21 +0000102 LLVM_DEBUG(dbgs() << "********** Late EH Prepare **********\n"
103 "********** Function: "
104 << MF.getName() << '\n');
105
Heejin Ahn4934f762018-06-25 01:07:11 +0000106 if (MF.getTarget().getMCAsmInfo()->getExceptionHandlingType() !=
107 ExceptionHandling::Wasm)
108 return false;
109
110 bool Changed = false;
Heejin Ahnb47a18c2019-03-16 04:46:05 +0000111 if (MF.getFunction().hasPersonalityFn()) {
112 Changed |= addCatches(MF);
113 Changed |= replaceFuncletReturns(MF);
114 }
Heejin Ahn095796a2018-11-16 00:47:18 +0000115 Changed |= removeUnnecessaryUnreachables(MF);
Heejin Ahnb47a18c2019-03-16 04:46:05 +0000116 if (MF.getFunction().hasPersonalityFn()) {
117 Changed |= addExceptionExtraction(MF);
118 Changed |= restoreStackPointer(MF);
119 }
Heejin Ahn4934f762018-06-25 01:07:11 +0000120 return Changed;
121}
122
Heejin Ahnb47a18c2019-03-16 04:46:05 +0000123// Add catch instruction to beginning of catchpads and cleanuppads.
124bool WebAssemblyLateEHPrepare::addCatches(MachineFunction &MF) {
Heejin Ahn095796a2018-11-16 00:47:18 +0000125 bool Changed = false;
Heejin Ahnb47a18c2019-03-16 04:46:05 +0000126 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
127 MachineRegisterInfo &MRI = MF.getRegInfo();
Heejin Ahn095796a2018-11-16 00:47:18 +0000128 for (auto &MBB : MF) {
Heejin Ahnb47a18c2019-03-16 04:46:05 +0000129 if (MBB.isEHPad()) {
Heejin Ahn095796a2018-11-16 00:47:18 +0000130 Changed = true;
Heejin Ahnb47a18c2019-03-16 04:46:05 +0000131 auto InsertPos = MBB.begin();
132 if (InsertPos->isEHLabel()) // EH pad starts with an EH label
133 ++InsertPos;
134 unsigned DstReg =
135 MRI.createVirtualRegister(&WebAssembly::EXCEPT_REFRegClass);
136 BuildMI(MBB, InsertPos, MBB.begin()->getDebugLoc(),
137 TII.get(WebAssembly::CATCH), DstReg);
Heejin Ahn095796a2018-11-16 00:47:18 +0000138 }
139 }
Heejin Ahn095796a2018-11-16 00:47:18 +0000140 return Changed;
141}
142
Heejin Ahn4934f762018-06-25 01:07:11 +0000143bool WebAssemblyLateEHPrepare::replaceFuncletReturns(MachineFunction &MF) {
144 bool Changed = false;
145 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
Heejin Ahn4934f762018-06-25 01:07:11 +0000146
147 for (auto &MBB : MF) {
148 auto Pos = MBB.getFirstTerminator();
149 if (Pos == MBB.end())
150 continue;
151 MachineInstr *TI = &*Pos;
152
153 switch (TI->getOpcode()) {
154 case WebAssembly::CATCHRET: {
155 // Replace a catchret with a branch
156 MachineBasicBlock *TBB = TI->getOperand(0).getMBB();
157 if (!MBB.isLayoutSuccessor(TBB))
158 BuildMI(MBB, TI, TI->getDebugLoc(), TII.get(WebAssembly::BR))
159 .addMBB(TBB);
160 TI->eraseFromParent();
161 Changed = true;
162 break;
163 }
164 case WebAssembly::CLEANUPRET: {
165 // Replace a cleanupret with a rethrow
Heejin Ahnd6f48782019-01-30 03:21:57 +0000166 BuildMI(MBB, TI, TI->getDebugLoc(), TII.get(WebAssembly::RETHROW));
Heejin Ahn4934f762018-06-25 01:07:11 +0000167 TI->eraseFromParent();
168 Changed = true;
169 break;
170 }
171 }
172 }
173 return Changed;
174}
175
Heejin Ahnb47a18c2019-03-16 04:46:05 +0000176bool WebAssemblyLateEHPrepare::removeUnnecessaryUnreachables(
177 MachineFunction &MF) {
Heejin Ahn4934f762018-06-25 01:07:11 +0000178 bool Changed = false;
Heejin Ahn4934f762018-06-25 01:07:11 +0000179 for (auto &MBB : MF) {
Heejin Ahnb47a18c2019-03-16 04:46:05 +0000180 for (auto &MI : MBB) {
181 if (MI.getOpcode() != WebAssembly::THROW &&
182 MI.getOpcode() != WebAssembly::RETHROW)
183 continue;
Heejin Ahn4934f762018-06-25 01:07:11 +0000184 Changed = true;
Heejin Ahnb47a18c2019-03-16 04:46:05 +0000185
186 // The instruction after the throw should be an unreachable or a branch to
187 // another BB that should eventually lead to an unreachable. Delete it
188 // because throw itself is a terminator, and also delete successors if
189 // any.
190 MBB.erase(std::next(MI.getIterator()), MBB.end());
191 SmallVector<MachineBasicBlock *, 8> Succs(MBB.succ_begin(),
192 MBB.succ_end());
193 for (auto *Succ : Succs)
194 MBB.removeSuccessor(Succ);
195 eraseDeadBBsAndChildren(Succs);
Heejin Ahn4934f762018-06-25 01:07:11 +0000196 }
197 }
Heejin Ahnb47a18c2019-03-16 04:46:05 +0000198
Heejin Ahn4934f762018-06-25 01:07:11 +0000199 return Changed;
200}
201
Heejin Ahnd6f48782019-01-30 03:21:57 +0000202// Wasm uses 'br_on_exn' instruction to check the tag of an exception. It takes
203// except_ref type object returned by 'catch', and branches to the destination
204// if it matches a given tag. We currently use __cpp_exception symbol to
205// represent the tag for all C++ exceptions.
206//
207// block $l (result i32)
208// ...
209// ;; except_ref $e is on the stack at this point
210// br_on_exn $l $e ;; branch to $l with $e's arguments
211// ...
212// end
213// ;; Here we expect the extracted values are on top of the wasm value stack
214// ... Handle exception using values ...
215//
216// br_on_exn takes an except_ref object and branches if it matches the given
217// tag. There can be multiple br_on_exn instructions if we want to match for
218// another tag, but for now we only test for __cpp_exception tag, and if it does
219// not match, i.e., it is a foreign exception, we rethrow it.
220//
221// In the destination BB that's the target of br_on_exn, extracted exception
222// values (in C++'s case a single i32, which represents an exception pointer)
223// are placed on top of the wasm stack. Because we can't model wasm stack in
224// LLVM instruction, we use 'extract_exception' pseudo instruction to retrieve
225// it. The pseudo instruction will be deleted later.
226bool WebAssemblyLateEHPrepare::addExceptionExtraction(MachineFunction &MF) {
Heejin Ahn4934f762018-06-25 01:07:11 +0000227 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
228 auto *EHInfo = MF.getWasmEHFuncInfo();
Heejin Ahnd6f48782019-01-30 03:21:57 +0000229 SmallVector<MachineInstr *, 16> ExtractInstrs;
Heejin Ahnd2a56ac2019-02-26 04:08:49 +0000230 SmallVector<MachineInstr *, 8> ToDelete;
Heejin Ahnd6f48782019-01-30 03:21:57 +0000231 for (auto &MBB : MF) {
Heejin Ahn4934f762018-06-25 01:07:11 +0000232 for (auto &MI : MBB) {
Heejin Ahnd6f48782019-01-30 03:21:57 +0000233 if (MI.getOpcode() == WebAssembly::EXTRACT_EXCEPTION_I32) {
234 if (MI.getOperand(0).isDead())
Heejin Ahnd2a56ac2019-02-26 04:08:49 +0000235 ToDelete.push_back(&MI);
Heejin Ahnd6f48782019-01-30 03:21:57 +0000236 else
237 ExtractInstrs.push_back(&MI);
238 }
Heejin Ahn4934f762018-06-25 01:07:11 +0000239 }
Heejin Ahnd6f48782019-01-30 03:21:57 +0000240 }
Heejin Ahnd2a56ac2019-02-26 04:08:49 +0000241 bool Changed = !ToDelete.empty() || !ExtractInstrs.empty();
242 for (auto *MI : ToDelete)
243 MI->eraseFromParent();
Heejin Ahnd6f48782019-01-30 03:21:57 +0000244 if (ExtractInstrs.empty())
Heejin Ahnd2a56ac2019-02-26 04:08:49 +0000245 return Changed;
Heejin Ahn4934f762018-06-25 01:07:11 +0000246
Heejin Ahnd6f48782019-01-30 03:21:57 +0000247 // Find terminate pads.
248 SmallSet<MachineBasicBlock *, 8> TerminatePads;
249 for (auto &MBB : MF) {
250 for (auto &MI : MBB) {
Heejin Ahn4934f762018-06-25 01:07:11 +0000251 if (MI.isCall()) {
252 const MachineOperand &CalleeOp = MI.getOperand(0);
253 if (CalleeOp.isGlobal() && CalleeOp.getGlobal()->getName() ==
254 WebAssembly::ClangCallTerminateFn)
Heejin Ahnd6f48782019-01-30 03:21:57 +0000255 TerminatePads.insert(getMatchingEHPad(&MI));
Heejin Ahn4934f762018-06-25 01:07:11 +0000256 }
Heejin Ahnd6f48782019-01-30 03:21:57 +0000257 }
258 }
Heejin Ahn4934f762018-06-25 01:07:11 +0000259
Heejin Ahnd6f48782019-01-30 03:21:57 +0000260 for (auto *Extract : ExtractInstrs) {
261 MachineBasicBlock *EHPad = getMatchingEHPad(Extract);
262 assert(EHPad && "No matching EH pad for extract_exception");
Heejin Ahn8b49b6b2019-03-13 00:37:31 +0000263 auto CatchPos = EHPad->begin();
264 if (CatchPos->isEHLabel()) // EH pad starts with an EH label
265 ++CatchPos;
266 MachineInstr *Catch = &*CatchPos;
267
Heejin Ahnd6f48782019-01-30 03:21:57 +0000268 if (Catch->getNextNode() != Extract)
269 EHPad->insert(Catch->getNextNode(), Extract->removeFromParent());
270
271 // - Before:
272 // ehpad:
273 // %exnref:except_ref = catch
274 // %exn:i32 = extract_exception
275 // ... use exn ...
276 //
277 // - After:
278 // ehpad:
279 // %exnref:except_ref = catch
280 // br_on_exn %thenbb, $__cpp_exception, %exnref
281 // br %elsebb
282 // elsebb:
283 // rethrow
284 // thenbb:
285 // %exn:i32 = extract_exception
286 // ... use exn ...
Heejin Ahnc7397612019-03-05 11:11:34 +0000287 unsigned ExnReg = Catch->getOperand(0).getReg();
Heejin Ahnd6f48782019-01-30 03:21:57 +0000288 auto *ThenMBB = MF.CreateMachineBasicBlock();
289 auto *ElseMBB = MF.CreateMachineBasicBlock();
290 MF.insert(std::next(MachineFunction::iterator(EHPad)), ElseMBB);
291 MF.insert(std::next(MachineFunction::iterator(ElseMBB)), ThenMBB);
292 ThenMBB->splice(ThenMBB->end(), EHPad, Extract, EHPad->end());
293 ThenMBB->transferSuccessors(EHPad);
294 EHPad->addSuccessor(ThenMBB);
295 EHPad->addSuccessor(ElseMBB);
296
297 DebugLoc DL = Extract->getDebugLoc();
298 const char *CPPExnSymbol = MF.createExternalSymbolName("__cpp_exception");
299 BuildMI(EHPad, DL, TII.get(WebAssembly::BR_ON_EXN))
300 .addMBB(ThenMBB)
301 .addExternalSymbol(CPPExnSymbol, WebAssemblyII::MO_SYMBOL_EVENT)
Heejin Ahnc7397612019-03-05 11:11:34 +0000302 .addReg(ExnReg);
Heejin Ahnd6f48782019-01-30 03:21:57 +0000303 BuildMI(EHPad, DL, TII.get(WebAssembly::BR)).addMBB(ElseMBB);
304
305 // When this is a terminate pad with __clang_call_terminate() call, we don't
306 // rethrow it anymore and call __clang_call_terminate() with a nullptr
307 // argument, which will call std::terminate().
308 //
309 // - Before:
310 // ehpad:
311 // %exnref:except_ref = catch
312 // %exn:i32 = extract_exception
313 // call @__clang_call_terminate(%exn)
314 // unreachable
315 //
316 // - After:
317 // ehpad:
318 // %exnref:except_ref = catch
319 // br_on_exn %thenbb, $__cpp_exception, %exnref
320 // br %elsebb
321 // elsebb:
322 // call @__clang_call_terminate(0)
323 // unreachable
324 // thenbb:
325 // %exn:i32 = extract_exception
326 // call @__clang_call_terminate(%exn)
327 // unreachable
328 if (TerminatePads.count(EHPad)) {
329 Function *ClangCallTerminateFn =
330 MF.getFunction().getParent()->getFunction(
331 WebAssembly::ClangCallTerminateFn);
332 assert(ClangCallTerminateFn &&
333 "There is no __clang_call_terminate() function");
334 BuildMI(ElseMBB, DL, TII.get(WebAssembly::CALL_VOID))
335 .addGlobalAddress(ClangCallTerminateFn)
336 .addImm(0);
337 BuildMI(ElseMBB, DL, TII.get(WebAssembly::UNREACHABLE));
338
339 } else {
340 BuildMI(ElseMBB, DL, TII.get(WebAssembly::RETHROW));
341 if (EHInfo->hasEHPadUnwindDest(EHPad))
Heejin Ahn195a62e2019-03-03 22:35:56 +0000342 ElseMBB->addSuccessor(EHInfo->getEHPadUnwindDest(EHPad));
Heejin Ahnd6f48782019-01-30 03:21:57 +0000343 }
Heejin Ahn4934f762018-06-25 01:07:11 +0000344 }
Heejin Ahn4934f762018-06-25 01:07:11 +0000345
Heejin Ahn4934f762018-06-25 01:07:11 +0000346 return true;
347}
Heejin Ahn0bb98652019-01-30 22:44:45 +0000348
349// After the stack is unwound due to a thrown exception, the __stack_pointer
350// global can point to an invalid address. This inserts instructions that
351// restore __stack_pointer global.
352bool WebAssemblyLateEHPrepare::restoreStackPointer(MachineFunction &MF) {
353 const auto *FrameLowering = static_cast<const WebAssemblyFrameLowering *>(
354 MF.getSubtarget().getFrameLowering());
355 if (!FrameLowering->needsPrologForEH(MF))
356 return false;
357 bool Changed = false;
358
359 for (auto &MBB : MF) {
360 if (!MBB.isEHPad())
361 continue;
362 Changed = true;
363
364 // Insert __stack_pointer restoring instructions at the beginning of each EH
365 // pad, after the catch instruction. Here it is safe to assume that SP32
366 // holds the latest value of __stack_pointer, because the only exception for
367 // this case is when a function uses the red zone, but that only happens
368 // with leaf functions, and we don't restore __stack_pointer in leaf
369 // functions anyway.
370 auto InsertPos = MBB.begin();
Heejin Ahn8b49b6b2019-03-13 00:37:31 +0000371 if (InsertPos->isEHLabel()) // EH pad starts with an EH label
372 ++InsertPos;
373 if (InsertPos->getOpcode() == WebAssembly::CATCH)
374 ++InsertPos;
Heejin Ahn0bb98652019-01-30 22:44:45 +0000375 FrameLowering->writeSPToGlobal(WebAssembly::SP32, MF, MBB, InsertPos,
376 MBB.begin()->getDebugLoc());
377 }
378 return Changed;
379}