1//=== WebAssemblyLateEHPrepare.cpp - WebAssembly Exception Preparation -===//
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
7//===----------------------------------------------------------------------===//
10/// \brief Does various transformations for exception handling.
12//===----------------------------------------------------------------------===//
27 #define DEBUG_TYPE "wasm-late-eh-prepare"
32 return "WebAssembly Late Prepare Exception";
49 static char ID;
// Pass identification, replacement for typeid
52}
// end anonymous namespace
54char WebAssemblyLateEHPrepare::ID = 0;
56 "WebAssembly Late Exception Preparation",
false,
false)
59 return new WebAssemblyLateEHPrepare();
62// Returns the nearest EH pad that dominates this instruction. This does not use
63// dominator analysis; it just does BFS on its predecessors until arriving at an
64// EH pad. This assumes valid EH scopes so the first EH pad it arrives in all
65// possible search paths should be the same.
66// Returns nullptr in case it does not find any EH pad in the search, or finds
67// multiple different EH pads.
70 MachineFunction *MF =
MI->getParent()->getParent();
72 SmallPtrSet<MachineBasicBlock *, 2> Visited;
74 MachineBasicBlock *EHPad =
nullptr;
80 if (EHPad && EHPad !=
MBB)
88 if (!CatchRetBBs.count(Pred))
// We don't go into child scopes
94// Erase the specified BBs if the BB does not have any remaining predecessors,
95// and also all its dead children.
96template <
typename Container>
100 while (!WL.
empty()) {
106 for (
auto *Succ : Succs)
107 MBB->removeSuccessor(Succ);
108 // To prevent deleting the same BB multiple times, which can happen when
109 // 'MBBs' contain both a parent and a child
111 MBB->eraseFromParent();
115bool WebAssemblyLateEHPrepare::runOnMachineFunction(MachineFunction &MF) {
117 "********** Function: "
121 ExceptionHandling::Wasm)
126 Changed |= removeUnreachableEHPads(MF);
127 recordCatchRetBBs(MF);
130 Changed |= replaceFuncletReturns(MF);
132 Changed |= addCatchRefsAndThrowRefs(MF);
134 Changed |= removeUnnecessaryUnreachables(MF);
136 Changed |= restoreStackPointer(MF);
140// Remove unreachable EH pads and its children. If they remain, CFG
141// stackification can be tricky.
142bool WebAssemblyLateEHPrepare::removeUnreachableEHPads(MachineFunction &MF) {
143 SmallVector<MachineBasicBlock *, 4> ToDelete;
148 return !ToDelete.
empty();
151// Record which BB ends with catchret instruction, because this will be replaced
152// with 'br's later. This set of catchret BBs is necessary in 'getMatchingEHPad'
154void WebAssemblyLateEHPrepare::recordCatchRetBBs(MachineFunction &MF) {
156 for (
auto &
MBB : MF) {
160 MachineInstr *TI = &*Pos;
161 if (TI->
getOpcode() == WebAssembly::CATCHRET)
162 CatchRetBBs.insert(&
MBB);
166// Hoist catch instructions to the beginning of their matching EH pad BBs in
168// (1) catch instruction is not the first instruction in EH pad.
170// some_other_instruction
173// (2) catch instruction is in a non-EH pad BB. For example,
178bool WebAssemblyLateEHPrepare::hoistCatches(MachineFunction &MF) {
186 for (
auto *
Catch : Catches) {
187 MachineBasicBlock *EHPad = getMatchingEHPad(
Catch);
188 assert(EHPad &&
"No matching EH pad for catch");
189 auto InsertPos = EHPad->
begin();
190 // Skip EH_LABELs in the beginning of an EH pad if present. We don't use
191 // these labels at the moment, but other targets also seem to have an
192 // EH_LABEL instruction in the beginning of an EH pad.
193 while (InsertPos != EHPad->
end() && InsertPos->isEHLabel())
195 if (InsertPos ==
Catch)
198 EHPad->
insert(InsertPos,
Catch->removeFromParent());
203// Add catch_all to beginning of cleanup pads.
204bool WebAssemblyLateEHPrepare::addCatchAlls(MachineFunction &MF) {
206 const auto &
TII = *MF.
getSubtarget<WebAssemblySubtarget>().getInstrInfo();
208 for (
auto &
MBB : MF) {
212 // Skip EH_LABELs in the beginning of an EH pad if present.
213 while (InsertPos !=
MBB.
end() && InsertPos->isEHLabel())
215 // This runs after hoistCatches(), so we assume that if there is a catch,
216 // that should be the first non-EH-label instruction in an EH pad.
217 if (InsertPos ==
MBB.
end() ||
221 ? WebAssembly::CATCH_ALL_LEGACY
222 : WebAssembly::CATCH_ALL;
225 TII.get(CatchAllOpcode));
231// Replace pseudo-instructions catchret and cleanupret with br and rethrow
233bool WebAssemblyLateEHPrepare::replaceFuncletReturns(MachineFunction &MF) {
235 const auto &
TII = *MF.
getSubtarget<WebAssemblySubtarget>().getInstrInfo();
237 for (
auto &
MBB : MF) {
241 MachineInstr *TI = &*Pos;
244 case WebAssembly::CATCHRET: {
245 // Replace a catchret with a branch
254 case WebAssembly::RETHROW:
255 // These RETHROWs here were lowered from llvm.wasm.rethrow() intrinsics,
256 // generated in Clang for when an exception is not caught by the given
257 // type (e.g. catch (int)).
259 // RETHROW's BB argument is the EH pad where the exception to rethrow has
260 // been caught. (Until this point, RETHROW has just a '0' as a placeholder
261 // argument.) For these llvm.wasm.rethrow()s, we can safely assume the
262 // exception comes from the nearest dominating EH pad, because catch.start
263 // EH pad is structured like this:
267 // %matches = compare ehselector with typeid
268 // br i1 %matches, label %catch, label %rethrow
271 // ;; rethrows the exception caught in 'catch.start'
272 // call @llvm.wasm.rethrow()
277 case WebAssembly::CLEANUPRET: {
278 // CLEANUPRETs have the EH pad BB the exception to rethrow has been caught
279 // as an argument. Use it and change the instruction opcode to 'RETHROW'
280 // to make rethrowing instructions consistent.
282 // This is because we cannot safely assume that it is always the nearest
283 // dominating EH pad, in case there are code transformations such as
296// Add CATCH_REF and CATCH_ALL_REF pseudo instructions to EH pads, and convert
297// RETHROWs to THROW_REFs.
298bool WebAssemblyLateEHPrepare::addCatchRefsAndThrowRefs(MachineFunction &MF) {
299 const auto &
TII = *MF.
getSubtarget<WebAssemblySubtarget>().getInstrInfo();
301 DenseMap<MachineBasicBlock *, SmallVector<MachineInstr *, 2>> EHPadToRethrows;
303 // Create a map of <EH pad, a vector of RETHROWs rethrowing its exception>
306 if (
MI.getOpcode() == WebAssembly::RETHROW)
307 EHPadToRethrows[
MI.getOperand(0).getMBB()].push_back(&
MI);
308 if (EHPadToRethrows.
empty())
311 // Convert CATCH into CATCH_REF and CATCH_ALL into CATCH_ALL_REF, when the
312 // caught exception is rethrown. And convert RETHROWs to THROW_REFs.
313 for (
auto &[EHPad, Rethrows] : EHPadToRethrows) {
315 auto *InsertPos =
Catch->getIterator()->getNextNode();
316 auto ExnReg =
MRI.createVirtualRegister(&WebAssembly::EXNREFRegClass);
317 if (
Catch->getOpcode() == WebAssembly::CATCH) {
318 MachineInstrBuilder MIB =
BuildMI(*EHPad, InsertPos,
Catch->getDebugLoc(),
319 TII.get(WebAssembly::CATCH_REF));
320 // Copy defs (= extracted values) from the old CATCH to the new CATCH_REF
321 for (
const auto &Def :
Catch->defs())
323 MIB.
addDef(ExnReg);
// Attach the exnref def after extracted values
324 // Copy the tag symbol (The only use operand a CATCH can have is the tag
326 for (
const auto &Use :
Catch->uses()) {
330 }
else if (
Catch->getOpcode() == WebAssembly::CATCH_ALL) {
332 TII.get(WebAssembly::CATCH_ALL_REF))
337 Catch->eraseFromParent();
339 for (
auto *Rethrow : Rethrows) {
340 auto InsertPos = std::next(Rethrow->getIterator());
341 BuildMI(*Rethrow->getParent(), InsertPos, Rethrow->getDebugLoc(),
342 TII.get(WebAssembly::THROW_REF))
351// Remove unnecessary unreachables after a throw/rethrow/throw_ref.
352bool WebAssemblyLateEHPrepare::removeUnnecessaryUnreachables(
353 MachineFunction &MF) {
355 for (
auto &
MBB : MF) {
356 for (
auto &
MI :
MBB) {
357 if (
MI.getOpcode() != WebAssembly::THROW &&
358 MI.getOpcode() != WebAssembly::RETHROW &&
359 MI.getOpcode() != WebAssembly::THROW_REF)
363 // The instruction after the throw should be an unreachable or a branch to
364 // another BB that should eventually lead to an unreachable. Delete it
365 // because throw itself is a terminator, and also delete successors if
369 for (
auto *Succ : Succs)
370 if (!Succ->isEHPad())
379// After the stack is unwound due to a thrown exception, the __stack_pointer
380// global can point to an invalid address. This inserts instructions that
381// restore __stack_pointer global.
382bool WebAssemblyLateEHPrepare::restoreStackPointer(MachineFunction &MF) {
383 const auto *FrameLowering =
static_cast<const WebAssemblyFrameLowering *
>(
385 if (!FrameLowering->needsPrologForEH(MF))
389 for (
auto &
MBB : MF) {
394 // Insert __stack_pointer restoring instructions at the beginning of each EH
395 // pad, after the catch instruction. Here it is safe to assume that SP32
396 // holds the latest value of __stack_pointer, because the only exception for
397 // this case is when a function uses the red zone, but that only happens
398 // with leaf functions, and we don't restore __stack_pointer in leaf
401 // Skip EH_LABELs in the beginning of an EH pad if present.
402 while (InsertPos !=
MBB.
end() && InsertPos->isEHLabel())
406 "catch/catch_all should be present in every EH pad at this point");
407 ++InsertPos;
// Skip the catch instruction
408 FrameLowering->writeSPToGlobal(FrameLowering->getSPReg(MF), MF,
MBB,
409 InsertPos,
MBB.
begin()->getDebugLoc());
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const HexagonInstrInfo * TII
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
This file defines the SmallPtrSet class.
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.
FunctionPass class - This class is used to implement most global optimizations.
bool hasPersonalityFn() const
Check whether this function has a personality function.
ExceptionHandling getExceptionHandlingType() const
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, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, 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 eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
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
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.
StringRef - Represent a constant reference to a string, i.e.
const MCAsmInfo * getMCAsmInfo() const
Return target specific asm information.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
cl::opt< bool > WasmUseLegacyEH
MachineInstr * findCatch(MachineBasicBlock *EHPad)
Find a catch instruction from an EH pad.
bool isCatch(unsigned Opc)
NodeAddr< DefNode * > Def
NodeAddr< UseNode * > Use
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.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...