30#define DEBUG_TYPE "wasm-late-eh-prepare"
33class WebAssemblyLateEHPrepareImpl {
52 return "WebAssembly Late Prepare Exception";
63char WebAssemblyLateEHPrepareLegacy::ID = 0;
65 "WebAssembly Late Exception Preparation",
false,
false)
68 return new WebAssemblyLateEHPrepareLegacy();
79 MachineFunction *MF =
MI->getParent()->getParent();
81 SmallPtrSet<MachineBasicBlock *, 2> Visited;
83 MachineBasicBlock *EHPad =
nullptr;
89 if (EHPad && EHPad !=
MBB)
97 if (!CatchRetBBs.count(Pred))
105template <
typename Container>
109 while (!WL.
empty()) {
115 for (
auto *Succ : Succs)
116 MBB->removeSuccessor(Succ);
120 MBB->eraseFromParent();
124bool WebAssemblyLateEHPrepareImpl::runOnMachineFunction(MachineFunction &MF) {
126 "********** Function: "
130 ExceptionHandling::Wasm)
135 Changed |= removeUnreachableEHPads(MF);
136 recordCatchRetBBs(MF);
139 Changed |= replaceFuncletReturns(MF);
140 if (!WebAssembly::WasmUseLegacyEH)
141 Changed |= addCatchRefsAndThrowRefs(MF);
143 Changed |= removeUnnecessaryUnreachables(MF);
145 Changed |= restoreStackPointer(MF);
151bool WebAssemblyLateEHPrepareImpl::removeUnreachableEHPads(
152 MachineFunction &MF) {
153 SmallVector<MachineBasicBlock *, 4> ToDelete;
158 return !ToDelete.
empty();
164void WebAssemblyLateEHPrepareImpl::recordCatchRetBBs(MachineFunction &MF) {
166 for (
auto &
MBB : MF) {
170 MachineInstr *TI = &*Pos;
171 if (TI->
getOpcode() == WebAssembly::CATCHRET)
172 CatchRetBBs.insert(&
MBB);
188bool WebAssemblyLateEHPrepareImpl::hoistCatches(MachineFunction &MF) {
193 if (WebAssembly::isCatch(
MI.getOpcode()))
196 for (
auto *
Catch : Catches) {
197 MachineBasicBlock *EHPad = getMatchingEHPad(
Catch);
198 assert(EHPad &&
"No matching EH pad for catch");
199 auto InsertPos = EHPad->
begin();
203 while (InsertPos != EHPad->
end() && InsertPos->isEHLabel())
205 if (InsertPos ==
Catch)
208 EHPad->
insert(InsertPos,
Catch->removeFromParent());
214bool WebAssemblyLateEHPrepareImpl::addCatchAlls(MachineFunction &MF) {
216 const auto &
TII = *MF.
getSubtarget<WebAssemblySubtarget>().getInstrInfo();
218 for (
auto &
MBB : MF) {
223 while (InsertPos !=
MBB.
end() && InsertPos->isEHLabel())
227 if (InsertPos ==
MBB.
end() ||
228 !WebAssembly::isCatch(InsertPos->getOpcode())) {
230 unsigned CatchAllOpcode = WebAssembly::WasmUseLegacyEH
231 ? WebAssembly::CATCH_ALL_LEGACY
232 : WebAssembly::CATCH_ALL;
235 TII.get(CatchAllOpcode));
243bool WebAssemblyLateEHPrepareImpl::replaceFuncletReturns(MachineFunction &MF) {
245 const auto &
TII = *MF.
getSubtarget<WebAssemblySubtarget>().getInstrInfo();
247 for (
auto &
MBB : MF) {
251 MachineInstr *TI = &*Pos;
254 case WebAssembly::CATCHRET: {
264 case WebAssembly::RETHROW:
287 case WebAssembly::CLEANUPRET: {
308bool WebAssemblyLateEHPrepareImpl::addCatchRefsAndThrowRefs(
309 MachineFunction &MF) {
310 const auto &
TII = *MF.
getSubtarget<WebAssemblySubtarget>().getInstrInfo();
312 MapVector<MachineBasicBlock *, SmallVector<MachineInstr *, 2>>
318 if (
MI.getOpcode() == WebAssembly::RETHROW)
319 EHPadToRethrows[
MI.getOperand(0).getMBB()].push_back(&
MI);
320 if (EHPadToRethrows.
empty())
325 for (
auto &[EHPad, Rethrows] : EHPadToRethrows) {
326 auto *
Catch = WebAssembly::findCatch(EHPad);
328 auto InsertPos = std::next(
Catch->getIterator());
329 auto ExnReg = MRI.createVirtualRegister(&WebAssembly::EXNREFRegClass);
330 if (
Catch->getOpcode() == WebAssembly::CATCH) {
331 MachineInstrBuilder MIB =
BuildMI(*EHPad, InsertPos,
Catch->getDebugLoc(),
332 TII.get(WebAssembly::CATCH_REF));
334 for (
const auto &Def :
Catch->defs())
339 for (
const auto &Use :
Catch->uses()) {
343 }
else if (
Catch->getOpcode() == WebAssembly::CATCH_ALL) {
345 TII.get(WebAssembly::CATCH_ALL_REF))
350 Catch->eraseFromParent();
352 for (
auto *Rethrow : Rethrows) {
353 auto InsertPos = std::next(Rethrow->getIterator());
354 BuildMI(*Rethrow->getParent(), InsertPos, Rethrow->getDebugLoc(),
355 TII.get(WebAssembly::THROW_REF))
365bool WebAssemblyLateEHPrepareImpl::removeUnnecessaryUnreachables(
366 MachineFunction &MF) {
368 for (
auto &
MBB : MF) {
369 for (
auto &
MI :
MBB) {
370 if (
MI.getOpcode() != WebAssembly::THROW &&
371 MI.getOpcode() != WebAssembly::RETHROW &&
372 MI.getOpcode() != WebAssembly::THROW_REF)
382 for (
auto *Succ : Succs)
383 if (!Succ->isEHPad())
395bool WebAssemblyLateEHPrepareImpl::restoreStackPointer(MachineFunction &MF) {
396 const auto *FrameLowering =
static_cast<const WebAssemblyFrameLowering *
>(
398 if (!FrameLowering->needsPrologForEH(MF))
402 for (
auto &
MBB : MF) {
415 while (InsertPos !=
MBB.
end() && InsertPos->isEHLabel())
418 WebAssembly::isCatch(InsertPos->getOpcode()) &&
419 "catch/catch_all should be present in every EH pad at this point");
421 FrameLowering->writeBackSP(FrameLowering->getSPReg(MF), MF,
MBB, InsertPos,
427bool WebAssemblyLateEHPrepareLegacy::runOnMachineFunction(MachineFunction &MF) {
428 WebAssemblyLateEHPrepareImpl Impl;
429 return Impl.runOnMachineFunction(MF);
435 WebAssemblyLateEHPrepareImpl Impl;
436 return Impl.runOnMachineFunction(MF)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const HexagonInstrInfo * TII
This file implements a map that provides insertion order iteration.
#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)
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
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.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Pass manager infrastructure for declaring and invalidating analyses.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
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.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
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...
FunctionPass * createWebAssemblyLateEHPrepareLegacyPass()