blob: caedd140a1c90361f402a4e0ab6866f2a0a12580 [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"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/WasmEHFuncInfo.h"
20#include "llvm/MC/MCAsmInfo.h"
21using namespace llvm;
22
23#define DEBUG_TYPE "wasm-exception-prepare"
24
25namespace {
26class WebAssemblyLateEHPrepare final : public MachineFunctionPass {
27 StringRef getPassName() const override {
28 return "WebAssembly Prepare Exception";
29 }
30
31 bool runOnMachineFunction(MachineFunction &MF) override;
32
Heejin Ahn095796a2018-11-16 00:47:18 +000033 bool removeUnnecessaryUnreachables(MachineFunction &MF);
Heejin Ahn4934f762018-06-25 01:07:11 +000034 bool replaceFuncletReturns(MachineFunction &MF);
35 bool hoistCatches(MachineFunction &MF);
36 bool addCatchAlls(MachineFunction &MF);
37 bool addRethrows(MachineFunction &MF);
38 bool ensureSingleBBTermPads(MachineFunction &MF);
39 bool mergeTerminatePads(MachineFunction &MF);
40 bool addCatchAllTerminatePads(MachineFunction &MF);
41
42public:
43 static char ID; // Pass identification, replacement for typeid
44 WebAssemblyLateEHPrepare() : MachineFunctionPass(ID) {}
45};
46} // end anonymous namespace
47
48char WebAssemblyLateEHPrepare::ID = 0;
49INITIALIZE_PASS(WebAssemblyLateEHPrepare, DEBUG_TYPE,
Heejin Ahna93e7262018-08-17 00:12:04 +000050 "WebAssembly Late Exception Preparation", false, false)
Heejin Ahn4934f762018-06-25 01:07:11 +000051
52FunctionPass *llvm::createWebAssemblyLateEHPrepare() {
53 return new WebAssemblyLateEHPrepare();
54}
55
56// Returns the nearest EH pad that dominates this instruction. This does not use
57// dominator analysis; it just does BFS on its predecessors until arriving at an
58// EH pad. This assumes valid EH scopes so the first EH pad it arrives in all
59// possible search paths should be the same.
60// Returns nullptr in case it does not find any EH pad in the search, or finds
61// multiple different EH pads.
Heejin Ahn095796a2018-11-16 00:47:18 +000062static MachineBasicBlock *getMatchingEHPad(MachineInstr *MI) {
Heejin Ahn4934f762018-06-25 01:07:11 +000063 MachineFunction *MF = MI->getParent()->getParent();
64 SmallVector<MachineBasicBlock *, 2> WL;
65 SmallPtrSet<MachineBasicBlock *, 2> Visited;
66 WL.push_back(MI->getParent());
67 MachineBasicBlock *EHPad = nullptr;
68 while (!WL.empty()) {
69 MachineBasicBlock *MBB = WL.pop_back_val();
70 if (Visited.count(MBB))
71 continue;
72 Visited.insert(MBB);
73 if (MBB->isEHPad()) {
74 if (EHPad && EHPad != MBB)
75 return nullptr;
76 EHPad = MBB;
77 continue;
78 }
79 if (MBB == &MF->front())
80 return nullptr;
81 WL.append(MBB->pred_begin(), MBB->pred_end());
82 }
83 return EHPad;
84}
85
Heejin Ahn095796a2018-11-16 00:47:18 +000086// Erase the specified BBs if the BB does not have any remaining predecessors,
87// and also all its dead children.
Benjamin Kramer83996e42018-08-08 10:13:19 +000088template <typename Container>
Heejin Ahn095796a2018-11-16 00:47:18 +000089static void eraseDeadBBsAndChildren(const Container &MBBs) {
Benjamin Kramer83996e42018-08-08 10:13:19 +000090 SmallVector<MachineBasicBlock *, 8> WL(MBBs.begin(), MBBs.end());
Heejin Ahn4934f762018-06-25 01:07:11 +000091 while (!WL.empty()) {
92 MachineBasicBlock *MBB = WL.pop_back_val();
Heejin Ahn095796a2018-11-16 00:47:18 +000093 if (!MBB->pred_empty())
94 continue;
Heejin Ahnb68d5912018-10-04 21:03:35 +000095 SmallVector<MachineBasicBlock *, 4> Succs(MBB->succ_begin(),
96 MBB->succ_end());
97 WL.append(MBB->succ_begin(), MBB->succ_end());
98 for (auto *Succ : Succs)
Heejin Ahn4934f762018-06-25 01:07:11 +000099 MBB->removeSuccessor(Succ);
Heejin Ahn4934f762018-06-25 01:07:11 +0000100 MBB->eraseFromParent();
101 }
102}
103
104bool WebAssemblyLateEHPrepare::runOnMachineFunction(MachineFunction &MF) {
Heejin Ahn569f0902019-01-09 23:05:21 +0000105 LLVM_DEBUG(dbgs() << "********** Late EH Prepare **********\n"
106 "********** Function: "
107 << MF.getName() << '\n');
108
Heejin Ahn4934f762018-06-25 01:07:11 +0000109 if (MF.getTarget().getMCAsmInfo()->getExceptionHandlingType() !=
110 ExceptionHandling::Wasm)
111 return false;
112
113 bool Changed = false;
Heejin Ahn095796a2018-11-16 00:47:18 +0000114 Changed |= removeUnnecessaryUnreachables(MF);
Heejin Ahn4934f762018-06-25 01:07:11 +0000115 Changed |= addRethrows(MF);
116 if (!MF.getFunction().hasPersonalityFn())
117 return Changed;
118 Changed |= replaceFuncletReturns(MF);
119 Changed |= hoistCatches(MF);
120 Changed |= addCatchAlls(MF);
121 Changed |= ensureSingleBBTermPads(MF);
122 Changed |= mergeTerminatePads(MF);
123 Changed |= addCatchAllTerminatePads(MF);
124 return Changed;
125}
126
Heejin Ahn095796a2018-11-16 00:47:18 +0000127bool WebAssemblyLateEHPrepare::removeUnnecessaryUnreachables(
128 MachineFunction &MF) {
129 bool Changed = false;
130 for (auto &MBB : MF) {
131 for (auto &MI : MBB) {
132 if (!WebAssembly::isThrow(MI))
133 continue;
134 Changed = true;
135
136 // The instruction after the throw should be an unreachable or a branch to
137 // another BB that should eventually lead to an unreachable. Delete it
138 // because throw itself is a terminator, and also delete successors if
139 // any.
140 MBB.erase(std::next(MachineBasicBlock::iterator(MI)), MBB.end());
141 SmallVector<MachineBasicBlock *, 8> Succs(MBB.succ_begin(),
142 MBB.succ_end());
143 for (auto *Succ : Succs)
144 MBB.removeSuccessor(Succ);
145 eraseDeadBBsAndChildren(Succs);
146 }
147 }
148
149 return Changed;
150}
151
Heejin Ahn4934f762018-06-25 01:07:11 +0000152bool WebAssemblyLateEHPrepare::replaceFuncletReturns(MachineFunction &MF) {
153 bool Changed = false;
154 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
155 auto *EHInfo = MF.getWasmEHFuncInfo();
156
157 for (auto &MBB : MF) {
158 auto Pos = MBB.getFirstTerminator();
159 if (Pos == MBB.end())
160 continue;
161 MachineInstr *TI = &*Pos;
162
163 switch (TI->getOpcode()) {
164 case WebAssembly::CATCHRET: {
165 // Replace a catchret with a branch
166 MachineBasicBlock *TBB = TI->getOperand(0).getMBB();
167 if (!MBB.isLayoutSuccessor(TBB))
168 BuildMI(MBB, TI, TI->getDebugLoc(), TII.get(WebAssembly::BR))
169 .addMBB(TBB);
170 TI->eraseFromParent();
171 Changed = true;
172 break;
173 }
174 case WebAssembly::CLEANUPRET: {
175 // Replace a cleanupret with a rethrow
176 if (EHInfo->hasThrowUnwindDest(&MBB))
177 BuildMI(MBB, TI, TI->getDebugLoc(), TII.get(WebAssembly::RETHROW))
178 .addMBB(EHInfo->getThrowUnwindDest(&MBB));
179 else
180 BuildMI(MBB, TI, TI->getDebugLoc(),
181 TII.get(WebAssembly::RETHROW_TO_CALLER));
182
183 TI->eraseFromParent();
184 Changed = true;
185 break;
186 }
187 }
188 }
189 return Changed;
190}
191
192// Hoist catch instructions to the beginning of their matching EH pad BBs in
193// case,
194// (1) catch instruction is not the first instruction in EH pad.
195// ehpad:
196// some_other_instruction
197// ...
198// %exn = catch 0
199// (2) catch instruction is in a non-EH pad BB. For example,
200// ehpad:
201// br bb0
202// bb0:
203// %exn = catch 0
204bool WebAssemblyLateEHPrepare::hoistCatches(MachineFunction &MF) {
205 bool Changed = false;
206 SmallVector<MachineInstr *, 16> Catches;
207 for (auto &MBB : MF)
208 for (auto &MI : MBB)
209 if (WebAssembly::isCatch(MI))
210 Catches.push_back(&MI);
211
212 for (auto *Catch : Catches) {
Heejin Ahn095796a2018-11-16 00:47:18 +0000213 MachineBasicBlock *EHPad = getMatchingEHPad(Catch);
Heejin Ahn4934f762018-06-25 01:07:11 +0000214 assert(EHPad && "No matching EH pad for catch");
215 if (EHPad->begin() == Catch)
216 continue;
217 Changed = true;
218 EHPad->insert(EHPad->begin(), Catch->removeFromParent());
219 }
220 return Changed;
221}
222
223// Add catch_all to beginning of cleanup pads.
224bool WebAssemblyLateEHPrepare::addCatchAlls(MachineFunction &MF) {
225 bool Changed = false;
226 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
227
228 for (auto &MBB : MF) {
229 if (!MBB.isEHPad())
230 continue;
231 // This runs after hoistCatches(), so we assume that if there is a catch,
232 // that should be the first instruction in an EH pad.
233 if (!WebAssembly::isCatch(*MBB.begin())) {
234 Changed = true;
235 BuildMI(MBB, MBB.begin(), MBB.begin()->getDebugLoc(),
236 TII.get(WebAssembly::CATCH_ALL));
237 }
238 }
239 return Changed;
240}
241
242// Add a 'rethrow' instruction after __cxa_rethrow() call
243bool WebAssemblyLateEHPrepare::addRethrows(MachineFunction &MF) {
244 bool Changed = false;
245 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
246 auto *EHInfo = MF.getWasmEHFuncInfo();
247
248 for (auto &MBB : MF)
249 for (auto &MI : MBB) {
250 // Check if it is a call to __cxa_rethrow()
251 if (!MI.isCall())
252 continue;
253 MachineOperand &CalleeOp = MI.getOperand(0);
254 if (!CalleeOp.isGlobal() ||
255 CalleeOp.getGlobal()->getName() != WebAssembly::CxaRethrowFn)
256 continue;
257
258 // Now we have __cxa_rethrow() call
259 Changed = true;
260 auto InsertPt = std::next(MachineBasicBlock::iterator(MI));
261 while (InsertPt != MBB.end() && InsertPt->isLabel()) // Skip EH_LABELs
262 ++InsertPt;
263 MachineInstr *Rethrow = nullptr;
264 if (EHInfo->hasThrowUnwindDest(&MBB))
265 Rethrow = BuildMI(MBB, InsertPt, MI.getDebugLoc(),
266 TII.get(WebAssembly::RETHROW))
267 .addMBB(EHInfo->getThrowUnwindDest(&MBB));
268 else
269 Rethrow = BuildMI(MBB, InsertPt, MI.getDebugLoc(),
270 TII.get(WebAssembly::RETHROW_TO_CALLER));
271
Heejin Ahn095796a2018-11-16 00:47:18 +0000272 // Because __cxa_rethrow does not return, the instruction after the
Heejin Ahn4934f762018-06-25 01:07:11 +0000273 // rethrow should be an unreachable or a branch to another BB that should
274 // eventually lead to an unreachable. Delete it because rethrow itself is
275 // a terminator, and also delete non-EH pad successors if any.
276 MBB.erase(std::next(MachineBasicBlock::iterator(Rethrow)), MBB.end());
Benjamin Kramer83996e42018-08-08 10:13:19 +0000277 SmallVector<MachineBasicBlock *, 8> NonPadSuccessors;
Heejin Ahn4934f762018-06-25 01:07:11 +0000278 for (auto *Succ : MBB.successors())
279 if (!Succ->isEHPad())
Benjamin Kramer83996e42018-08-08 10:13:19 +0000280 NonPadSuccessors.push_back(Succ);
Heejin Ahn095796a2018-11-16 00:47:18 +0000281 for (auto *Succ : NonPadSuccessors)
282 MBB.removeSuccessor(Succ);
283 eraseDeadBBsAndChildren(NonPadSuccessors);
Heejin Ahn4934f762018-06-25 01:07:11 +0000284 }
285 return Changed;
286}
287
288// Terminate pads are an single-BB EH pad in the form of
289// termpad:
290// %exn = catch 0
291// call @__clang_call_terminate(%exn)
292// unreachable
Thomas Lively6a87dda2019-01-08 06:25:55 +0000293// (There can be local.set and local.gets before the call if we didn't run
Heejin Ahn4934f762018-06-25 01:07:11 +0000294// RegStackify)
295// But code transformations can change or add more control flow, so the call to
296// __clang_call_terminate() function may not be in the original EH pad anymore.
297// This ensures every terminate pad is a single BB in the form illustrated
298// above.
299bool WebAssemblyLateEHPrepare::ensureSingleBBTermPads(MachineFunction &MF) {
300 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
301
302 // Find calls to __clang_call_terminate()
303 SmallVector<MachineInstr *, 8> ClangCallTerminateCalls;
304 for (auto &MBB : MF)
305 for (auto &MI : MBB)
306 if (MI.isCall()) {
307 const MachineOperand &CalleeOp = MI.getOperand(0);
308 if (CalleeOp.isGlobal() && CalleeOp.getGlobal()->getName() ==
309 WebAssembly::ClangCallTerminateFn)
310 ClangCallTerminateCalls.push_back(&MI);
311 }
312
313 bool Changed = false;
314 for (auto *Call : ClangCallTerminateCalls) {
Heejin Ahn095796a2018-11-16 00:47:18 +0000315 MachineBasicBlock *EHPad = getMatchingEHPad(Call);
Heejin Ahn4934f762018-06-25 01:07:11 +0000316 assert(EHPad && "No matching EH pad for catch");
317
318 // If it is already the form we want, skip it
319 if (Call->getParent() == EHPad &&
320 Call->getNextNode()->getOpcode() == WebAssembly::UNREACHABLE)
321 continue;
322
323 // In case the __clang_call_terminate() call is not in its matching EH pad,
324 // move the call to the end of EH pad and add an unreachable instruction
325 // after that. Delete all successors and their children if any, because here
326 // the program terminates.
327 Changed = true;
328 MachineInstr *Catch = &*EHPad->begin();
329 // This runs after hoistCatches(), so catch instruction should be at the top
330 assert(WebAssembly::isCatch(*Catch));
331 // Takes the result register of the catch instruction as argument. There may
Thomas Lively6a87dda2019-01-08 06:25:55 +0000332 // have been some other local.set/local.gets in between, but at this point
Heejin Ahn4934f762018-06-25 01:07:11 +0000333 // we don't care.
334 Call->getOperand(1).setReg(Catch->getOperand(0).getReg());
335 auto InsertPos = std::next(MachineBasicBlock::iterator(Catch));
336 EHPad->insert(InsertPos, Call->removeFromParent());
337 BuildMI(*EHPad, InsertPos, Call->getDebugLoc(),
338 TII.get(WebAssembly::UNREACHABLE));
339 EHPad->erase(InsertPos, EHPad->end());
Heejin Ahn095796a2018-11-16 00:47:18 +0000340 SmallVector<MachineBasicBlock *, 8> Succs(EHPad->succ_begin(),
341 EHPad->succ_end());
342 for (auto *Succ : Succs)
343 EHPad->removeSuccessor(Succ);
344 eraseDeadBBsAndChildren(Succs);
Heejin Ahn4934f762018-06-25 01:07:11 +0000345 }
346 return Changed;
347}
348
349// In case there are multiple terminate pads, merge them into one for code size.
350// This runs after ensureSingleBBTermPads() and assumes every terminate pad is a
351// single BB.
352// In principle this violates EH scope relationship because it can merge
353// multiple inner EH scopes, each of which is in different outer EH scope. But
354// getEHScopeMembership() function will not be called after this, so it is fine.
355bool WebAssemblyLateEHPrepare::mergeTerminatePads(MachineFunction &MF) {
356 SmallVector<MachineBasicBlock *, 8> TermPads;
357 for (auto &MBB : MF)
358 if (WebAssembly::isCatchTerminatePad(MBB))
359 TermPads.push_back(&MBB);
360 if (TermPads.empty())
361 return false;
362
363 MachineBasicBlock *UniqueTermPad = TermPads.front();
364 for (auto *TermPad :
365 llvm::make_range(std::next(TermPads.begin()), TermPads.end())) {
366 SmallVector<MachineBasicBlock *, 2> Preds(TermPad->pred_begin(),
367 TermPad->pred_end());
368 for (auto *Pred : Preds)
369 Pred->replaceSuccessor(TermPad, UniqueTermPad);
370 TermPad->eraseFromParent();
371 }
372 return true;
373}
374
375// Terminate pads are cleanup pads, so they should start with a 'catch_all'
376// instruction. But in the Itanium model, when we have a C++ exception object,
377// we pass them to __clang_call_terminate function, which calls __cxa_end_catch
378// with the passed exception pointer and then std::terminate. This is the reason
379// that terminate pads are generated with not a catch_all but a catch
380// instruction in clang and earlier llvm passes. Here we append a terminate pad
381// with a catch_all after each existing terminate pad so we can also catch
382// foreign exceptions. For every terminate pad:
383// %exn = catch 0
384// call @__clang_call_terminate(%exn)
385// unreachable
386// We append this BB right after that:
387// catch_all
388// call @std::terminate()
389// unreachable
390bool WebAssemblyLateEHPrepare::addCatchAllTerminatePads(MachineFunction &MF) {
391 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
392 SmallVector<MachineBasicBlock *, 8> TermPads;
393 for (auto &MBB : MF)
394 if (WebAssembly::isCatchTerminatePad(MBB))
395 TermPads.push_back(&MBB);
396 if (TermPads.empty())
397 return false;
398
399 Function *StdTerminateFn =
400 MF.getFunction().getParent()->getFunction(WebAssembly::StdTerminateFn);
401 assert(StdTerminateFn && "There is no std::terminate() function");
402 for (auto *CatchTermPad : TermPads) {
403 DebugLoc DL = CatchTermPad->findDebugLoc(CatchTermPad->begin());
404 auto *CatchAllTermPad = MF.CreateMachineBasicBlock();
405 MF.insert(std::next(MachineFunction::iterator(CatchTermPad)),
406 CatchAllTermPad);
407 CatchAllTermPad->setIsEHPad();
408 BuildMI(CatchAllTermPad, DL, TII.get(WebAssembly::CATCH_ALL));
409 BuildMI(CatchAllTermPad, DL, TII.get(WebAssembly::CALL_VOID))
410 .addGlobalAddress(StdTerminateFn);
411 BuildMI(CatchAllTermPad, DL, TII.get(WebAssembly::UNREACHABLE));
412
413 // Actually this CatchAllTermPad (new terminate pad with a catch_all) is not
414 // a successor of an existing terminate pad. CatchAllTermPad should have all
415 // predecessors CatchTermPad has instead. This is a hack to force
416 // CatchAllTermPad be always sorted right after CatchTermPad; the correct
417 // predecessor-successor relationships will be restored in CFGStackify pass.
418 CatchTermPad->addSuccessor(CatchAllTermPad);
419 }
420 return true;
421}