25 #define DEBUG_TYPE "wasm-late-eh-prepare" 30 return "WebAssembly Late Prepare Exception";
48 "WebAssembly Late Exception Preparation",
false,
false)
51 return new WebAssemblyLateEHPrepare();
68 if (Visited.
count(MBB))
72 if (EHPad && EHPad != MBB)
77 if (MBB == &MF->
front())
86 template <
typename Container>
96 for (
auto *Succ : Succs)
102 bool WebAssemblyLateEHPrepare::runOnMachineFunction(
MachineFunction &MF) {
104 "********** Function: " 111 bool Changed =
false;
113 Changed |= addCatches(MF);
114 Changed |= replaceFuncletReturns(MF);
116 Changed |= removeUnnecessaryUnreachables(MF);
118 Changed |= addExceptionExtraction(MF);
119 Changed |= restoreStackPointer(MF);
126 bool Changed =
false;
129 for (
auto &MBB : MF) {
132 auto InsertPos = MBB.begin();
133 if (InsertPos->isEHLabel())
136 BuildMI(MBB, InsertPos, MBB.begin()->getDebugLoc(),
137 TII.get(WebAssembly::CATCH), DstReg);
143 bool WebAssemblyLateEHPrepare::replaceFuncletReturns(
MachineFunction &MF) {
144 bool Changed =
false;
147 for (
auto &MBB : MF) {
148 auto Pos = MBB.getFirstTerminator();
149 if (Pos == MBB.end())
157 if (!MBB.isLayoutSuccessor(TBB))
165 case WebAssembly::RETHROW_IN_CATCH: {
168 auto CatchPos = EHPad->begin();
169 if (CatchPos->isEHLabel())
184 bool WebAssemblyLateEHPrepare::removeUnnecessaryUnreachables(
186 bool Changed =
false;
187 for (
auto &MBB : MF) {
188 for (
auto &
MI : MBB) {
189 if (
MI.getOpcode() != WebAssembly::THROW &&
190 MI.getOpcode() != WebAssembly::RETHROW)
198 MBB.erase(std::next(
MI.getIterator()), MBB.end());
201 for (
auto *Succ : Succs)
202 if (!Succ->isEHPad())
203 MBB.removeSuccessor(Succ);
235 bool WebAssemblyLateEHPrepare::addExceptionExtraction(
MachineFunction &MF) {
241 for (
auto &MBB : MF) {
242 for (
auto &
MI : MBB) {
243 if (
MI.getOpcode() == WebAssembly::EXTRACT_EXCEPTION_I32) {
244 if (
MI.getOperand(0).isDead())
251 bool Changed = !ToDelete.
empty() || !ExtractInstrs.
empty();
252 for (
auto *
MI : ToDelete)
253 MI->eraseFromParent();
254 if (ExtractInstrs.
empty())
259 for (
auto &MBB : MF) {
260 for (
auto &
MI : MBB) {
270 for (
auto *Extract : ExtractInstrs) {
272 assert(EHPad &&
"No matching EH pad for extract_exception");
273 auto CatchPos = EHPad->
begin();
274 if (CatchPos->isEHLabel())
298 auto *ThenMBB = MF.CreateMachineBasicBlock();
299 auto *ElseMBB = MF.CreateMachineBasicBlock();
302 ThenMBB->splice(ThenMBB->end(), EHPad, Extract, EHPad->
end());
303 ThenMBB->transferSuccessors(EHPad);
307 DebugLoc DL = Extract->getDebugLoc();
308 const char *CPPExnSymbol = MF.createExternalSymbolName(
"__cpp_exception");
309 BuildMI(EHPad, DL,
TII.get(WebAssembly::BR_ON_EXN))
338 if (TerminatePads.
count(EHPad)) {
340 MF.getFunction().getParent()->getFunction(
342 assert(ClangCallTerminateFn &&
343 "There is no __clang_call_terminate() function");
345 BuildMI(ElseMBB, DL,
TII.get(WebAssembly::CONST_I32),
Reg).addImm(0);
346 BuildMI(ElseMBB, DL,
TII.get(WebAssembly::CALL_VOID))
347 .addGlobalAddress(ClangCallTerminateFn)
349 BuildMI(ElseMBB, DL,
TII.get(WebAssembly::UNREACHABLE));
352 BuildMI(ElseMBB, DL,
TII.get(WebAssembly::RETHROW)).addReg(ExnReg);
353 if (EHInfo->hasEHPadUnwindDest(EHPad))
354 ElseMBB->addSuccessor(EHInfo->getEHPadUnwindDest(EHPad));
364 bool WebAssemblyLateEHPrepare::restoreStackPointer(
MachineFunction &MF) {
367 if (!FrameLowering->needsPrologForEH(MF))
369 bool Changed =
false;
371 for (
auto &MBB : MF) {
382 auto InsertPos = MBB.begin();
383 if (InsertPos->isEHLabel())
385 if (InsertPos->getOpcode() == WebAssembly::CATCH)
387 FrameLowering->writeSPToGlobal(WebAssembly::SP32, MF, MBB, InsertPos,
388 MBB.begin()->getDebugLoc());
MachineBasicBlock * getMBB() const
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
This class represents lattice values for constants.
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
void push_back(const T &Elt)
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
CLEANUPRET - Represents a return from a cleanup block funclet.
void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
static void eraseDeadBBsAndChildren(const Container &MBBs)
bool hasPersonalityFn() const
Check whether this function has a personality function.
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
WebAssembly Exception Handling.
This file contains the declaration of the WebAssembly-specific utility functions. ...
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Control flow instructions. These all have token chains.
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
const GlobalValue * getGlobal() const
const MCAsmInfo * getMCAsmInfo() const
Return target specific asm information.
This file provides WebAssembly-specific target descriptions.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
FunctionPass class - This class is used to implement most global optimizations.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
succ_iterator succ_begin()
const MachineBasicBlock & front() const
pred_iterator pred_begin()
INITIALIZE_PASS(WebAssemblyLateEHPrepare, DEBUG_TYPE, "WebAssembly Late Exception Preparation", false, false) FunctionPass *llvm
This file declares the WebAssembly-specific subclass of TargetSubtarget.
Iterator for intrusive lists based on ilist_node.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
const char *const ClangCallTerminateFn
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
LLVM_NODISCARD T pop_back_val()
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static MachineBasicBlock * getMatchingEHPad(MachineInstr *MI)
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
CATCHRET - Represents a return from a catch block funclet.
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
const WasmEHFuncInfo * getWasmEHFuncInfo() const
getWasmEHFuncInfo - Return information about how the current function uses Wasm exception handling...
Representation of each machine instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
bool isEHPad() const
Returns true if the block is a landing pad.
LLVM_NODISCARD bool empty() const
FunctionPass * createWebAssemblyLateEHPrepare()
StringRef getName() const
Return a constant reference to the value's name.
virtual const TargetFrameLowering * getFrameLowering() const
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
MachineInstr * removeFromParent()
Unlink 'this' from the containing basic block, and return it without deleting it. ...
void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ExceptionHandling getExceptionHandlingType() const
StringRef - Represent a constant reference to a string, i.e.
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
Wrapper class representing virtual and physical registers.
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.