28#define DEBUG_TYPE "wasm-late-eh-prepare"
31class WebAssemblyLateEHPrepareImpl {
32 const WebAssemblyOptions &CLOpts;
47 explicit WebAssemblyLateEHPrepareImpl(
const WebAssemblyOptions &CLOpts)
54 return "WebAssembly Late Prepare Exception";
65char WebAssemblyLateEHPrepareLegacy::ID = 0;
67 "WebAssembly Late Exception Preparation",
false,
false)
70 return new WebAssemblyLateEHPrepareLegacy();
82 SmallVector<MachineBasicBlock *, 2> WL;
83 SmallPtrSet<MachineBasicBlock *, 2> Visited;
85 MachineBasicBlock *EHPad =
nullptr;
91 if (EHPad && EHPad !=
MBB)
99 if (!CatchRetBBs.count(
Pred))
107template <
typename Container>
111 while (!WL.
empty()) {
117 for (
auto *Succ : Succs)
118 MBB->removeSuccessor(Succ);
122 MBB->eraseFromParent();
126bool WebAssemblyLateEHPrepareImpl::runOnMachineFunction(
MachineFunction &MF) {
128 "********** Function: "
132 ExceptionHandling::Wasm)
137 Changed |= removeUnreachableEHPads(MF);
138 recordCatchRetBBs(MF);
141 Changed |= replaceFuncletReturns(MF);
142 if (!CLOpts.use_legacy_eh)
143 Changed |= addCatchRefsAndThrowRefs(MF);
145 Changed |= removeUnnecessaryUnreachables(MF);
147 Changed |= restoreStackPointer(MF);
153bool WebAssemblyLateEHPrepareImpl::removeUnreachableEHPads(
155 SmallVector<MachineBasicBlock *, 4> ToDelete;
160 return !ToDelete.
empty();
166void WebAssemblyLateEHPrepareImpl::recordCatchRetBBs(
MachineFunction &MF) {
168 for (
auto &
MBB : MF) {
172 MachineInstr *TI = &*Pos;
173 if (TI->
getOpcode() == WebAssembly::CATCHRET)
174 CatchRetBBs.insert(&
MBB);
198 for (
auto *
Catch : Catches) {
199 MachineBasicBlock *EHPad = getMatchingEHPad(
Catch);
200 assert(EHPad &&
"No matching EH pad for catch");
201 auto InsertPos = EHPad->
begin();
205 while (InsertPos != EHPad->
end() && InsertPos->isEHLabel())
207 if (InsertPos ==
Catch)
210 EHPad->
insert(InsertPos,
Catch->removeFromParent());
218 const auto &
TII = *MF.
getSubtarget<WebAssemblySubtarget>().getInstrInfo();
220 for (
auto &
MBB : MF) {
225 while (InsertPos !=
MBB.
end() && InsertPos->isEHLabel())
229 if (InsertPos ==
MBB.
end() ||
232 unsigned CatchAllOpcode = CLOpts.use_legacy_eh
233 ? WebAssembly::CATCH_ALL_LEGACY
234 : WebAssembly::CATCH_ALL;
237 TII.get(CatchAllOpcode));
245bool WebAssemblyLateEHPrepareImpl::replaceFuncletReturns(
MachineFunction &MF) {
247 const auto &
TII = *MF.
getSubtarget<WebAssemblySubtarget>().getInstrInfo();
249 for (
auto &
MBB : MF) {
253 MachineInstr *TI = &*Pos;
256 case WebAssembly::CATCHRET: {
266 case WebAssembly::RETHROW:
289 case WebAssembly::CLEANUPRET: {
310bool WebAssemblyLateEHPrepareImpl::addCatchRefsAndThrowRefs(
312 const auto &
TII = *MF.
getSubtarget<WebAssemblySubtarget>().getInstrInfo();
314 MapVector<MachineBasicBlock *, SmallVector<MachineInstr *, 2>>
320 if (
MI.getOpcode() == WebAssembly::RETHROW)
321 EHPadToRethrows[
MI.getOperand(0).getMBB()].push_back(&
MI);
322 if (EHPadToRethrows.
empty())
327 for (
auto &[EHPad, Rethrows] : EHPadToRethrows) {
330 auto InsertPos = std::next(
Catch->getIterator());
332 if (
Catch->getOpcode() == WebAssembly::CATCH) {
333 MachineInstrBuilder MIB =
BuildMI(*EHPad, InsertPos,
Catch->getDebugLoc(),
334 TII.get(WebAssembly::CATCH_REF));
336 for (
const auto &Def :
Catch->defs())
341 for (
const auto &Use :
Catch->uses()) {
345 }
else if (
Catch->getOpcode() == WebAssembly::CATCH_ALL) {
347 TII.get(WebAssembly::CATCH_ALL_REF))
352 Catch->eraseFromParent();
354 for (
auto *Rethrow : Rethrows) {
355 auto InsertPos = std::next(Rethrow->getIterator());
356 BuildMI(*Rethrow->getParent(), InsertPos, Rethrow->getDebugLoc(),
357 TII.get(WebAssembly::THROW_REF))
367bool WebAssemblyLateEHPrepareImpl::removeUnnecessaryUnreachables(
370 for (
auto &
MBB : MF) {
371 for (
auto &
MI :
MBB) {
372 if (
MI.getOpcode() != WebAssembly::THROW &&
373 MI.getOpcode() != WebAssembly::RETHROW &&
374 MI.getOpcode() != WebAssembly::THROW_REF)
384 for (
auto *Succ : Succs)
385 if (!Succ->isEHPad())
397bool WebAssemblyLateEHPrepareImpl::restoreStackPointer(
MachineFunction &MF) {
398 const auto *FrameLowering =
static_cast<const WebAssemblyFrameLowering *
>(
400 if (!FrameLowering->needsPrologForEH(MF))
404 for (
auto &
MBB : MF) {
417 while (InsertPos !=
MBB.
end() && InsertPos->isEHLabel())
421 "catch/catch_all should be present in every EH pad at this point");
423 FrameLowering->writeBackSP(FrameLowering->getSPReg(MF), MF,
MBB, InsertPos,
429bool WebAssemblyLateEHPrepareLegacy::runOnMachineFunction(
MachineFunction &MF) {
430 WebAssemblyLateEHPrepareImpl Impl(
432 return Impl.runOnMachineFunction(MF);
438 WebAssemblyLateEHPrepareImpl Impl(
440 return Impl.runOnMachineFunction(MF)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AArch64InstrInfo & TII
Module.h This file contains the declarations for the Module class.
This file implements a map that provides insertion order iteration.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
MachineRegisterInfo & MRI
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 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.
Module * getParent()
Get the module that this global value is contained inside of...
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...
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
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 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)
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
ExceptionHandling getExceptionModel() const
Returns the exception model recorded by the "exception-model" module flag, or the target's default ex...
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.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
const WebAssemblyOptions & getCLOpts() const
Pass manager infrastructure for declaring and invalidating analyses.
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.
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()