Go to the documentation of this file.
27 #define DEBUG_TYPE "wasm-late-eh-prepare"
32 return "WebAssembly Late Prepare Exception";
55 "WebAssembly Late Exception Preparation",
false,
false)
58 return new WebAssemblyLateEHPrepare();
72 WL.push_back(
MI->getParent());
80 if (EHPad && EHPad !=
MBB)
88 if (!CatchRetBBs.count(Pred))
96 template <
typename Container>
100 while (!WL.empty()) {
106 for (
auto *Succ : Succs)
115 bool WebAssemblyLateEHPrepare::runOnMachineFunction(
MachineFunction &MF) {
117 "********** Function: "
124 bool Changed =
false;
126 Changed |= removeUnreachableEHPads(MF);
127 recordCatchRetBBs(MF);
128 Changed |= hoistCatches(MF);
129 Changed |= addCatchAlls(MF);
130 Changed |= replaceFuncletReturns(MF);
132 Changed |= removeUnnecessaryUnreachables(MF);
134 Changed |= restoreStackPointer(MF);
140 bool WebAssemblyLateEHPrepare::removeUnreachableEHPads(
MachineFunction &MF) {
144 ToDelete.push_back(&
MBB);
146 return !ToDelete.empty();
152 void WebAssemblyLateEHPrepare::recordCatchRetBBs(
MachineFunction &MF) {
154 for (
auto &
MBB : MF) {
160 CatchRetBBs.insert(&
MBB);
177 bool Changed =
false;
182 Catches.push_back(&
MI);
184 for (
auto *Catch : Catches) {
186 assert(EHPad &&
"No matching EH pad for catch");
187 auto InsertPos = EHPad->
begin();
191 while (InsertPos != EHPad->
end() && InsertPos->isEHLabel())
193 if (InsertPos == Catch)
196 EHPad->
insert(InsertPos,
Catch->removeFromParent());
203 bool Changed =
false;
206 for (
auto &
MBB : MF) {
211 while (InsertPos !=
MBB.
end() && InsertPos->isEHLabel())
215 if (InsertPos ==
MBB.
end() ||
220 TII.get(WebAssembly::CATCH_ALL));
228 bool WebAssemblyLateEHPrepare::replaceFuncletReturns(
MachineFunction &MF) {
229 bool Changed =
false;
232 for (
auto &
MBB : MF) {
264 bool WebAssemblyLateEHPrepare::removeUnnecessaryUnreachables(
266 bool Changed =
false;
267 for (
auto &
MBB : MF) {
268 for (
auto &
MI :
MBB) {
269 if (
MI.getOpcode() != WebAssembly::THROW &&
270 MI.getOpcode() != WebAssembly::RETHROW)
280 for (
auto *Succ : Succs)
281 if (!Succ->isEHPad())
293 bool WebAssemblyLateEHPrepare::restoreStackPointer(
MachineFunction &MF) {
296 if (!FrameLowering->needsPrologForEH(MF))
298 bool Changed =
false;
300 for (
auto &
MBB : MF) {
313 while (InsertPos !=
MBB.
end() && InsertPos->isEHLabel())
317 "catch/catch_all should be present in every EH pad at this point");
319 FrameLowering->writeSPToGlobal(FrameLowering->getSPReg(MF), MF,
MBB,
320 InsertPos,
MBB.
begin()->getDebugLoc());
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
This is an optimization pass for GlobalISel generic memory operations.
bool isCatch(unsigned Opc)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
INITIALIZE_PASS(WebAssemblyLateEHPrepare, DEBUG_TYPE, "WebAssembly Late Exception Preparation", false, false) FunctionPass *llvm
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
LLVM_NODISCARD T pop_back_val()
static void eraseDeadBBsAndChildren(const Container &MBBs)
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
const MachineBasicBlock & front() const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
@ CATCHRET
CATCHRET - Represents a return from a catch block funclet.
const MachineOperand & getOperand(unsigned i) const
void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
const HexagonInstrInfo * TII
@ Wasm
WebAssembly Exception Handling.
bool hasPersonalityFn() const
Check whether this function has a personality function.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Representation of each machine instruction.
@ CLEANUPRET
CLEANUPRET - Represents a return from a cleanup block funclet.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
succ_iterator succ_begin()
iterator_range< pred_iterator > predecessors()
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
const MCAsmInfo * getMCAsmInfo() const
Return target specific asm information.
MachineBasicBlock * getMBB() const
iterator_range< succ_iterator > successors()
bool isEHPad() const
Returns true if the block is a landing pad.
StringRef - Represent a constant reference to a string, i.e.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Function & getFunction()
Return the LLVM function that this machine code represents.
virtual const TargetFrameLowering * getFrameLowering() const
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
@ BR
Control flow instructions. These all have token chains.
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
ExceptionHandling getExceptionHandlingType() const
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionPass class - This class is used to implement most global optimizations.
FunctionPass * createWebAssemblyLateEHPrepare()
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.