26#define DEBUG_TYPE "wasm-late-eh-prepare"
31 return "WebAssembly Late Prepare Exception";
53char WebAssemblyLateEHPrepare::ID = 0;
55 "WebAssembly Late Exception Preparation",
false,
false)
58 return new WebAssemblyLateEHPrepare();
69 MachineFunction *MF =
MI->getParent()->getParent();
71 SmallPtrSet<MachineBasicBlock *, 2> Visited;
73 MachineBasicBlock *EHPad =
nullptr;
79 if (EHPad && EHPad !=
MBB)
87 if (!CatchRetBBs.count(Pred))
95template <
typename Container>
105 for (
auto *Succ : Succs)
106 MBB->removeSuccessor(Succ);
110 MBB->eraseFromParent();
114bool WebAssemblyLateEHPrepare::runOnMachineFunction(MachineFunction &MF) {
116 "********** Function: "
120 ExceptionHandling::Wasm)
125 Changed |= removeUnreachableEHPads(MF);
126 recordCatchRetBBs(MF);
129 Changed |= replaceFuncletReturns(MF);
131 Changed |= addCatchRefsAndThrowRefs(MF);
133 Changed |= removeUnnecessaryUnreachables(MF);
135 Changed |= restoreStackPointer(MF);
141bool WebAssemblyLateEHPrepare::removeUnreachableEHPads(MachineFunction &MF) {
142 SmallVector<MachineBasicBlock *, 4> ToDelete;
147 return !ToDelete.
empty();
153void WebAssemblyLateEHPrepare::recordCatchRetBBs(MachineFunction &MF) {
155 for (
auto &
MBB : MF) {
159 MachineInstr *TI = &*Pos;
160 if (TI->
getOpcode() == WebAssembly::CATCHRET)
161 CatchRetBBs.insert(&
MBB);
177bool WebAssemblyLateEHPrepare::hoistCatches(MachineFunction &MF) {
185 for (
auto *
Catch : Catches) {
186 MachineBasicBlock *EHPad = getMatchingEHPad(
Catch);
187 assert(EHPad &&
"No matching EH pad for catch");
188 auto InsertPos = EHPad->
begin();
192 while (InsertPos != EHPad->
end() && InsertPos->isEHLabel())
194 if (InsertPos ==
Catch)
197 EHPad->
insert(InsertPos,
Catch->removeFromParent());
203bool WebAssemblyLateEHPrepare::addCatchAlls(MachineFunction &MF) {
205 const auto &
TII = *MF.
getSubtarget<WebAssemblySubtarget>().getInstrInfo();
207 for (
auto &
MBB : MF) {
212 while (InsertPos !=
MBB.
end() && InsertPos->isEHLabel())
216 if (InsertPos ==
MBB.
end() ||
220 ? WebAssembly::CATCH_ALL_LEGACY
221 : WebAssembly::CATCH_ALL;
224 TII.get(CatchAllOpcode));
232bool WebAssemblyLateEHPrepare::replaceFuncletReturns(MachineFunction &MF) {
234 const auto &
TII = *MF.
getSubtarget<WebAssemblySubtarget>().getInstrInfo();
236 for (
auto &
MBB : MF) {
240 MachineInstr *TI = &*Pos;
243 case WebAssembly::CATCHRET: {
253 case WebAssembly::RETHROW:
276 case WebAssembly::CLEANUPRET: {
297bool WebAssemblyLateEHPrepare::addCatchRefsAndThrowRefs(MachineFunction &MF) {
298 const auto &
TII = *MF.
getSubtarget<WebAssemblySubtarget>().getInstrInfo();
300 DenseMap<MachineBasicBlock *, SmallVector<MachineInstr *, 2>> EHPadToRethrows;
305 if (
MI.getOpcode() == WebAssembly::RETHROW)
306 EHPadToRethrows[
MI.getOperand(0).getMBB()].push_back(&
MI);
307 if (EHPadToRethrows.
empty())
312 for (
auto &[EHPad, Rethrows] : EHPadToRethrows) {
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));
320 for (
const auto &Def :
Catch->defs())
325 for (
const auto &Use :
Catch->uses()) {
329 }
else if (
Catch->getOpcode() == WebAssembly::CATCH_ALL) {
331 TII.get(WebAssembly::CATCH_ALL_REF))
336 Catch->eraseFromParent();
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))
351bool WebAssemblyLateEHPrepare::removeUnnecessaryUnreachables(
352 MachineFunction &MF) {
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)
368 for (
auto *Succ : Succs)
369 if (!Succ->isEHPad())
381bool WebAssemblyLateEHPrepare::restoreStackPointer(MachineFunction &MF) {
382 const auto *FrameLowering =
static_cast<const WebAssemblyFrameLowering *
>(
384 if (!FrameLowering->needsPrologForEH(MF))
388 for (
auto &
MBB : MF) {
401 while (InsertPos !=
MBB.
end() && InsertPos->isEHLabel())
405 "catch/catch_all should be present in every EH pad at this point");
407 FrameLowering->writeSPToGlobal(FrameLowering->getSPReg(MF), MF,
MBB,
408 InsertPos,
MBB.
begin()->getDebugLoc());
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, 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.
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...