LLVM 23.0.0git
WebAssemblyLateEHPrepare.cpp
Go to the documentation of this file.
1//=== WebAssemblyLateEHPrepare.cpp - WebAssembly Exception Preparation -===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// \brief Does various transformations for exception handling.
11///
12//===----------------------------------------------------------------------===//
13
14#include "WebAssembly.h"
21#include "llvm/MC/MCAsmInfo.h"
22#include "llvm/Support/Debug.h"
24using namespace llvm;
25
26#define DEBUG_TYPE "wasm-late-eh-prepare"
27
28namespace {
29class WebAssemblyLateEHPrepare final : public MachineFunctionPass {
30 StringRef getPassName() const override {
31 return "WebAssembly Late Prepare Exception";
32 }
33
34 bool runOnMachineFunction(MachineFunction &MF) override;
35 bool removeUnreachableEHPads(MachineFunction &MF);
36 void recordCatchRetBBs(MachineFunction &MF);
37 bool hoistCatches(MachineFunction &MF);
38 bool addCatchAlls(MachineFunction &MF);
39 bool addCatchRefsAndThrowRefs(MachineFunction &MF);
40 bool replaceFuncletReturns(MachineFunction &MF);
41 bool removeUnnecessaryUnreachables(MachineFunction &MF);
42 bool restoreStackPointer(MachineFunction &MF);
43
44 MachineBasicBlock *getMatchingEHPad(MachineInstr *MI);
46
47public:
48 static char ID; // Pass identification, replacement for typeid
49 WebAssemblyLateEHPrepare() : MachineFunctionPass(ID) {}
50};
51} // end anonymous namespace
52
53char WebAssemblyLateEHPrepare::ID = 0;
54INITIALIZE_PASS(WebAssemblyLateEHPrepare, DEBUG_TYPE,
55 "WebAssembly Late Exception Preparation", false, false)
56
58 return new WebAssemblyLateEHPrepare();
59}
60
61// Returns the nearest EH pad that dominates this instruction. This does not use
62// dominator analysis; it just does BFS on its predecessors until arriving at an
63// EH pad. This assumes valid EH scopes so the first EH pad it arrives in all
64// possible search paths should be the same.
65// Returns nullptr in case it does not find any EH pad in the search, or finds
66// multiple different EH pads.
68WebAssemblyLateEHPrepare::getMatchingEHPad(MachineInstr *MI) {
69 MachineFunction *MF = MI->getParent()->getParent();
71 SmallPtrSet<MachineBasicBlock *, 2> Visited;
72 WL.push_back(MI->getParent());
73 MachineBasicBlock *EHPad = nullptr;
74 while (!WL.empty()) {
75 MachineBasicBlock *MBB = WL.pop_back_val();
76 if (!Visited.insert(MBB).second)
77 continue;
78 if (MBB->isEHPad()) {
79 if (EHPad && EHPad != MBB)
80 return nullptr;
81 EHPad = MBB;
82 continue;
83 }
84 if (MBB == &MF->front())
85 return nullptr;
86 for (auto *Pred : MBB->predecessors())
87 if (!CatchRetBBs.count(Pred)) // We don't go into child scopes
88 WL.push_back(Pred);
89 }
90 return EHPad;
91}
92
93// Erase the specified BBs if the BB does not have any remaining predecessors,
94// and also all its dead children.
95template <typename Container>
96static void eraseDeadBBsAndChildren(const Container &MBBs) {
97 SmallVector<MachineBasicBlock *, 8> WL(MBBs.begin(), MBBs.end());
99 while (!WL.empty()) {
101 if (Deleted.count(MBB) || !MBB->pred_empty())
102 continue;
103 SmallVector<MachineBasicBlock *, 4> Succs(MBB->successors());
104 WL.append(MBB->succ_begin(), MBB->succ_end());
105 for (auto *Succ : Succs)
106 MBB->removeSuccessor(Succ);
107 // To prevent deleting the same BB multiple times, which can happen when
108 // 'MBBs' contain both a parent and a child
109 Deleted.insert(MBB);
110 MBB->eraseFromParent();
111 }
112}
113
114bool WebAssemblyLateEHPrepare::runOnMachineFunction(MachineFunction &MF) {
115 LLVM_DEBUG(dbgs() << "********** Late EH Prepare **********\n"
116 "********** Function: "
117 << MF.getName() << '\n');
118
120 ExceptionHandling::Wasm)
121 return false;
122
123 bool Changed = false;
124 if (MF.getFunction().hasPersonalityFn()) {
125 Changed |= removeUnreachableEHPads(MF);
126 recordCatchRetBBs(MF);
127 Changed |= hoistCatches(MF);
128 Changed |= addCatchAlls(MF);
129 Changed |= replaceFuncletReturns(MF);
131 Changed |= addCatchRefsAndThrowRefs(MF);
132 }
133 Changed |= removeUnnecessaryUnreachables(MF);
134 if (MF.getFunction().hasPersonalityFn())
135 Changed |= restoreStackPointer(MF);
136 return Changed;
137}
138
139// Remove unreachable EH pads and its children. If they remain, CFG
140// stackification can be tricky.
141bool WebAssemblyLateEHPrepare::removeUnreachableEHPads(MachineFunction &MF) {
142 SmallVector<MachineBasicBlock *, 4> ToDelete;
143 for (auto &MBB : MF)
144 if (MBB.isEHPad() && MBB.pred_empty())
145 ToDelete.push_back(&MBB);
146 eraseDeadBBsAndChildren(ToDelete);
147 return !ToDelete.empty();
148}
149
150// Record which BB ends with catchret instruction, because this will be replaced
151// with 'br's later. This set of catchret BBs is necessary in 'getMatchingEHPad'
152// function.
153void WebAssemblyLateEHPrepare::recordCatchRetBBs(MachineFunction &MF) {
154 CatchRetBBs.clear();
155 for (auto &MBB : MF) {
156 auto Pos = MBB.getFirstTerminator();
157 if (Pos == MBB.end())
158 continue;
159 MachineInstr *TI = &*Pos;
160 if (TI->getOpcode() == WebAssembly::CATCHRET)
161 CatchRetBBs.insert(&MBB);
162 }
163}
164
165// Hoist catch instructions to the beginning of their matching EH pad BBs in
166// case,
167// (1) catch instruction is not the first instruction in EH pad.
168// ehpad:
169// some_other_instruction
170// ...
171// %exn = catch 0
172// (2) catch instruction is in a non-EH pad BB. For example,
173// ehpad:
174// br bb0
175// bb0:
176// %exn = catch 0
177bool WebAssemblyLateEHPrepare::hoistCatches(MachineFunction &MF) {
178 bool Changed = false;
180 for (auto &MBB : MF)
181 for (auto &MI : MBB)
182 if (WebAssembly::isCatch(MI.getOpcode()))
183 Catches.push_back(&MI);
184
185 for (auto *Catch : Catches) {
186 MachineBasicBlock *EHPad = getMatchingEHPad(Catch);
187 assert(EHPad && "No matching EH pad for catch");
188 auto InsertPos = EHPad->begin();
189 // Skip EH_LABELs in the beginning of an EH pad if present. We don't use
190 // these labels at the moment, but other targets also seem to have an
191 // EH_LABEL instruction in the beginning of an EH pad.
192 while (InsertPos != EHPad->end() && InsertPos->isEHLabel())
193 InsertPos++;
194 if (InsertPos == Catch)
195 continue;
196 Changed = true;
197 EHPad->insert(InsertPos, Catch->removeFromParent());
198 }
199 return Changed;
200}
201
202// Add catch_all to beginning of cleanup pads.
203bool WebAssemblyLateEHPrepare::addCatchAlls(MachineFunction &MF) {
204 bool Changed = false;
205 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
206
207 for (auto &MBB : MF) {
208 if (!MBB.isEHPad())
209 continue;
210 auto InsertPos = MBB.begin();
211 // Skip EH_LABELs in the beginning of an EH pad if present.
212 while (InsertPos != MBB.end() && InsertPos->isEHLabel())
213 InsertPos++;
214 // This runs after hoistCatches(), so we assume that if there is a catch,
215 // that should be the first non-EH-label instruction in an EH pad.
216 if (InsertPos == MBB.end() ||
217 !WebAssembly::isCatch(InsertPos->getOpcode())) {
218 Changed = true;
219 unsigned CatchAllOpcode = WebAssembly::WasmUseLegacyEH
220 ? WebAssembly::CATCH_ALL_LEGACY
221 : WebAssembly::CATCH_ALL;
222 BuildMI(MBB, InsertPos,
223 InsertPos == MBB.end() ? DebugLoc() : InsertPos->getDebugLoc(),
224 TII.get(CatchAllOpcode));
225 }
226 }
227 return Changed;
228}
229
230// Replace pseudo-instructions catchret and cleanupret with br and rethrow
231// respectively.
232bool WebAssemblyLateEHPrepare::replaceFuncletReturns(MachineFunction &MF) {
233 bool Changed = false;
234 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
235
236 for (auto &MBB : MF) {
237 auto Pos = MBB.getFirstTerminator();
238 if (Pos == MBB.end())
239 continue;
240 MachineInstr *TI = &*Pos;
241
242 switch (TI->getOpcode()) {
243 case WebAssembly::CATCHRET: {
244 // Replace a catchret with a branch
245 MachineBasicBlock *TBB = TI->getOperand(0).getMBB();
247 BuildMI(MBB, TI, TI->getDebugLoc(), TII.get(WebAssembly::BR))
248 .addMBB(TBB);
249 TI->eraseFromParent();
250 Changed = true;
251 break;
252 }
253 case WebAssembly::RETHROW:
254 // These RETHROWs here were lowered from llvm.wasm.rethrow() intrinsics,
255 // generated in Clang for when an exception is not caught by the given
256 // type (e.g. catch (int)).
257 //
258 // RETHROW's BB argument is the EH pad where the exception to rethrow has
259 // been caught. (Until this point, RETHROW has just a '0' as a placeholder
260 // argument.) For these llvm.wasm.rethrow()s, we can safely assume the
261 // exception comes from the nearest dominating EH pad, because catch.start
262 // EH pad is structured like this:
263 //
264 // catch.start:
265 // catchpad ...
266 // %matches = compare ehselector with typeid
267 // br i1 %matches, label %catch, label %rethrow
268 //
269 // rethrow:
270 // ;; rethrows the exception caught in 'catch.start'
271 // call @llvm.wasm.rethrow()
272 TI->removeOperand(0);
273 TI->addOperand(MachineOperand::CreateMBB(getMatchingEHPad(TI)));
274 Changed = true;
275 break;
276 case WebAssembly::CLEANUPRET: {
277 // CLEANUPRETs have the EH pad BB the exception to rethrow has been caught
278 // as an argument. Use it and change the instruction opcode to 'RETHROW'
279 // to make rethrowing instructions consistent.
280 //
281 // This is because we cannot safely assume that it is always the nearest
282 // dominating EH pad, in case there are code transformations such as
283 // inlining.
284 BuildMI(MBB, TI, TI->getDebugLoc(), TII.get(WebAssembly::RETHROW))
285 .addMBB(TI->getOperand(0).getMBB());
286 TI->eraseFromParent();
287 Changed = true;
288 break;
289 }
290 }
291 }
292 return Changed;
293}
294
295// Add CATCH_REF and CATCH_ALL_REF pseudo instructions to EH pads, and convert
296// RETHROWs to THROW_REFs.
297bool WebAssemblyLateEHPrepare::addCatchRefsAndThrowRefs(MachineFunction &MF) {
298 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
299 auto &MRI = MF.getRegInfo();
300 DenseMap<MachineBasicBlock *, SmallVector<MachineInstr *, 2>> EHPadToRethrows;
301
302 // Create a map of <EH pad, a vector of RETHROWs rethrowing its exception>
303 for (auto &MBB : MF)
304 for (auto &MI : MBB)
305 if (MI.getOpcode() == WebAssembly::RETHROW)
306 EHPadToRethrows[MI.getOperand(0).getMBB()].push_back(&MI);
307 if (EHPadToRethrows.empty())
308 return false;
309
310 // Convert CATCH into CATCH_REF and CATCH_ALL into CATCH_ALL_REF, when the
311 // caught exception is rethrown. And convert RETHROWs to THROW_REFs.
312 for (auto &[EHPad, Rethrows] : EHPadToRethrows) {
313 auto *Catch = WebAssembly::findCatch(EHPad);
314 auto *InsertPos = Catch->getIterator()->getNextNode();
315 auto ExnReg = MRI.createVirtualRegister(&WebAssembly::EXNREFRegClass);
316 if (Catch->getOpcode() == WebAssembly::CATCH) {
317 MachineInstrBuilder MIB = BuildMI(*EHPad, InsertPos, Catch->getDebugLoc(),
318 TII.get(WebAssembly::CATCH_REF));
319 // Copy defs (= extracted values) from the old CATCH to the new CATCH_REF
320 for (const auto &Def : Catch->defs())
321 MIB.addDef(Def.getReg());
322 MIB.addDef(ExnReg); // Attach the exnref def after extracted values
323 // Copy the tag symbol (The only use operand a CATCH can have is the tag
324 // symbol)
325 for (const auto &Use : Catch->uses()) {
326 MIB.addExternalSymbol(Use.getSymbolName());
327 break;
328 }
329 } else if (Catch->getOpcode() == WebAssembly::CATCH_ALL) {
330 BuildMI(*EHPad, InsertPos, Catch->getDebugLoc(),
331 TII.get(WebAssembly::CATCH_ALL_REF))
332 .addDef(ExnReg);
333 } else {
334 assert(false);
335 }
336 Catch->eraseFromParent();
337
338 for (auto *Rethrow : Rethrows) {
339 auto InsertPos = std::next(Rethrow->getIterator());
340 BuildMI(*Rethrow->getParent(), InsertPos, Rethrow->getDebugLoc(),
341 TII.get(WebAssembly::THROW_REF))
342 .addReg(ExnReg);
343 Rethrow->eraseFromParent();
344 }
345 }
346
347 return true;
348}
349
350// Remove unnecessary unreachables after a throw/rethrow/throw_ref.
351bool WebAssemblyLateEHPrepare::removeUnnecessaryUnreachables(
352 MachineFunction &MF) {
353 bool Changed = false;
354 for (auto &MBB : MF) {
355 for (auto &MI : MBB) {
356 if (MI.getOpcode() != WebAssembly::THROW &&
357 MI.getOpcode() != WebAssembly::RETHROW &&
358 MI.getOpcode() != WebAssembly::THROW_REF)
359 continue;
360 Changed = true;
361
362 // The instruction after the throw should be an unreachable or a branch to
363 // another BB that should eventually lead to an unreachable. Delete it
364 // because throw itself is a terminator, and also delete successors if
365 // any.
366 MBB.erase(std::next(MI.getIterator()), MBB.end());
368 for (auto *Succ : Succs)
369 if (!Succ->isEHPad())
370 MBB.removeSuccessor(Succ);
372 }
373 }
374
375 return Changed;
376}
377
378// After the stack is unwound due to a thrown exception, the __stack_pointer
379// global can point to an invalid address. This inserts instructions that
380// restore __stack_pointer global.
381bool WebAssemblyLateEHPrepare::restoreStackPointer(MachineFunction &MF) {
382 const auto *FrameLowering = static_cast<const WebAssemblyFrameLowering *>(
383 MF.getSubtarget().getFrameLowering());
384 if (!FrameLowering->needsPrologForEH(MF))
385 return false;
386 bool Changed = false;
387
388 for (auto &MBB : MF) {
389 if (!MBB.isEHPad())
390 continue;
391 Changed = true;
392
393 // Insert __stack_pointer restoring instructions at the beginning of each EH
394 // pad, after the catch instruction. Here it is safe to assume that SP32
395 // holds the latest value of __stack_pointer, because the only exception for
396 // this case is when a function uses the red zone, but that only happens
397 // with leaf functions, and we don't restore __stack_pointer in leaf
398 // functions anyway.
399 auto InsertPos = MBB.begin();
400 // Skip EH_LABELs in the beginning of an EH pad if present.
401 while (InsertPos != MBB.end() && InsertPos->isEHLabel())
402 InsertPos++;
403 assert(InsertPos != MBB.end() &&
404 WebAssembly::isCatch(InsertPos->getOpcode()) &&
405 "catch/catch_all should be present in every EH pad at this point");
406 ++InsertPos; // Skip the catch instruction
407 FrameLowering->writeSPToGlobal(FrameLowering->getSPReg(MF), MF, MBB,
408 InsertPos, MBB.begin()->getDebugLoc());
409 }
410 return Changed;
411}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
This file defines the SmallPtrSet class.
#define LLVM_DEBUG(...)
Definition Debug.h:114
static void eraseDeadBBsAndChildren(const Container &BBs)
This file declares the WebAssembly-specific subclass of TargetSubtarget.
This file declares the WebAssembly-specific subclass of TargetMachine.
This file contains the declaration of the WebAssembly-specific utility functions.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
bool empty() const
Definition DenseMap.h:109
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition Function.h:905
ExceptionHandling getExceptionHandlingType() const
Definition MCAsmInfo.h:645
bool isEHPad() const
Returns true if the block is a landing pad.
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
LLVM_ABI void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
LLVM_ABI bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
iterator_range< succ_iterator > successors()
iterator_range< pred_iterator > predecessors()
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineBasicBlock & front() const
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
LLVM_ABI void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
LLVM_ABI void removeOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
MachineBasicBlock * getMBB() const
static MachineOperand CreateMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:55
const MCAsmInfo & getMCAsmInfo() const
Return target specific asm information.
Changed
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
cl::opt< bool > WasmUseLegacyEH
MachineInstr * findCatch(MachineBasicBlock *EHPad)
Find a catch instruction from an EH pad.
bool isCatch(unsigned Opc)
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionPass * createWebAssemblyLateEHPrepare()
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...