27#define DEBUG_TYPE "wasm-late-eh-prepare"
32 return "WebAssembly Late Prepare Exception";
53char WebAssemblyLateEHPrepare::ID = 0;
55 "WebAssembly Late Exception Preparation",
false,
false)
58 return new WebAssemblyLateEHPrepare();
79 if (EHPad && EHPad !=
MBB)
87 if (!CatchRetBBs.count(Pred))
95template <
typename Container>
105 for (
auto *Succ : Succs)
114bool WebAssemblyLateEHPrepare::runOnMachineFunction(
MachineFunction &MF) {
116 "********** Function: "
120 ExceptionHandling::Wasm)
123 bool Changed =
false;
125 Changed |= removeUnreachableEHPads(MF);
126 recordCatchRetBBs(MF);
127 Changed |= hoistCatches(MF);
128 Changed |= addCatchAlls(MF);
129 Changed |= replaceFuncletReturns(MF);
131 Changed |= removeUnnecessaryUnreachables(MF);
133 Changed |= restoreStackPointer(MF);
139bool WebAssemblyLateEHPrepare::removeUnreachableEHPads(
MachineFunction &MF) {
145 return !ToDelete.
empty();
153 for (
auto &
MBB : MF) {
158 if (TI->
getOpcode() == WebAssembly::CATCHRET)
176 bool Changed =
false;
183 for (
auto *
Catch : Catches) {
185 assert(EHPad &&
"No matching EH pad for catch");
186 auto InsertPos = EHPad->
begin();
190 while (InsertPos != EHPad->
end() && InsertPos->isEHLabel())
192 if (InsertPos ==
Catch)
195 EHPad->
insert(InsertPos,
Catch->removeFromParent());
202 bool Changed =
false;
205 for (
auto &
MBB : MF) {
210 while (InsertPos !=
MBB.
end() && InsertPos->isEHLabel())
214 if (InsertPos ==
MBB.
end() ||
219 TII.get(WebAssembly::CATCH_ALL));
227bool WebAssemblyLateEHPrepare::replaceFuncletReturns(
MachineFunction &MF) {
228 bool Changed =
false;
231 for (
auto &
MBB : MF) {
238 case WebAssembly::CATCHRET: {
248 case WebAssembly::CLEANUPRET: {
263bool WebAssemblyLateEHPrepare::removeUnnecessaryUnreachables(
265 bool Changed =
false;
266 for (
auto &
MBB : MF) {
267 for (
auto &
MI :
MBB) {
268 if (
MI.getOpcode() != WebAssembly::THROW &&
269 MI.getOpcode() != WebAssembly::RETHROW)
279 for (
auto *Succ : Succs)
280 if (!Succ->isEHPad())
292bool WebAssemblyLateEHPrepare::restoreStackPointer(
MachineFunction &MF) {
295 if (!FrameLowering->needsPrologForEH(MF))
297 bool Changed =
false;
299 for (
auto &
MBB : MF) {
312 while (InsertPos !=
MBB.
end() && InsertPos->isEHLabel())
316 "catch/catch_all should be present in every EH pad at this point");
318 FrameLowering->writeSPToGlobal(FrameLowering->getSPReg(MF), MF,
MBB,
319 InsertPos,
MBB.
begin()->getDebugLoc());
const HexagonInstrInfo * TII
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallPtrSet class.
static void eraseDeadBBsAndChildren(const Container &MBBs)
This file provides WebAssembly-specific target descriptions.
This file declares the WebAssembly-specific subclass of TargetSubtarget.
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.
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
succ_iterator succ_begin()
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
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...
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
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.
Function & getFunction()
Return the LLVM function that this machine code represents.
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineBasicBlock & front() const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
void insert(mop_iterator InsertBefore, ArrayRef< MachineOperand > Ops)
Inserts Ops BEFORE It. Can untie/retie tied operands.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
const MachineOperand & getOperand(unsigned i) const
MachineBasicBlock * getMBB() const
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
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.
virtual const TargetFrameLowering * getFrameLowering() const
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
bool isCatch(unsigned Opc)
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()
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.