29#define DEBUG_TYPE "wasm-late-eh-prepare"
32class WebAssemblyLateEHPrepareImpl {
51 return "WebAssembly Late Prepare Exception";
62char WebAssemblyLateEHPrepareLegacy::ID = 0;
64 "WebAssembly Late Exception Preparation",
false,
false)
67 return new WebAssemblyLateEHPrepareLegacy();
80 SmallPtrSet<MachineBasicBlock *, 2> Visited;
82 MachineBasicBlock *EHPad =
nullptr;
88 if (EHPad && EHPad !=
MBB)
96 if (!CatchRetBBs.count(Pred))
104template <
typename Container>
108 while (!WL.
empty()) {
114 for (
auto *Succ : Succs)
115 MBB->removeSuccessor(Succ);
119 MBB->eraseFromParent();
123bool WebAssemblyLateEHPrepareImpl::runOnMachineFunction(
MachineFunction &MF) {
125 "********** Function: "
129 ExceptionHandling::Wasm)
134 Changed |= removeUnreachableEHPads(MF);
135 recordCatchRetBBs(MF);
138 Changed |= replaceFuncletReturns(MF);
139 if (!WebAssembly::WasmUseLegacyEH)
140 Changed |= addCatchRefsAndThrowRefs(MF);
142 Changed |= removeUnnecessaryUnreachables(MF);
144 Changed |= restoreStackPointer(MF);
150bool WebAssemblyLateEHPrepareImpl::removeUnreachableEHPads(
152 SmallVector<MachineBasicBlock *, 4> ToDelete;
157 return !ToDelete.
empty();
163void WebAssemblyLateEHPrepareImpl::recordCatchRetBBs(
MachineFunction &MF) {
165 for (
auto &
MBB : MF) {
169 MachineInstr *TI = &*Pos;
170 if (TI->
getOpcode() == WebAssembly::CATCHRET)
171 CatchRetBBs.insert(&
MBB);
192 if (WebAssembly::isCatch(
MI.getOpcode()))
195 for (
auto *
Catch : Catches) {
196 MachineBasicBlock *EHPad = getMatchingEHPad(
Catch);
197 assert(EHPad &&
"No matching EH pad for catch");
198 auto InsertPos = EHPad->
begin();
202 while (InsertPos != EHPad->
end() && InsertPos->isEHLabel())
204 if (InsertPos ==
Catch)
207 EHPad->
insert(InsertPos,
Catch->removeFromParent());
215 const auto &
TII = *MF.
getSubtarget<WebAssemblySubtarget>().getInstrInfo();
217 for (
auto &
MBB : MF) {
222 while (InsertPos !=
MBB.
end() && InsertPos->isEHLabel())
226 if (InsertPos ==
MBB.
end() ||
227 !WebAssembly::isCatch(InsertPos->getOpcode())) {
229 unsigned CatchAllOpcode = WebAssembly::WasmUseLegacyEH
230 ? WebAssembly::CATCH_ALL_LEGACY
231 : WebAssembly::CATCH_ALL;
234 TII.get(CatchAllOpcode));
242bool WebAssemblyLateEHPrepareImpl::replaceFuncletReturns(
MachineFunction &MF) {
244 const auto &
TII = *MF.
getSubtarget<WebAssemblySubtarget>().getInstrInfo();
246 for (
auto &
MBB : MF) {
250 MachineInstr *TI = &*Pos;
253 case WebAssembly::CATCHRET: {
263 case WebAssembly::RETHROW:
286 case WebAssembly::CLEANUPRET: {
307bool WebAssemblyLateEHPrepareImpl::addCatchRefsAndThrowRefs(
309 const auto &
TII = *MF.
getSubtarget<WebAssemblySubtarget>().getInstrInfo();
311 MapVector<MachineBasicBlock *, SmallVector<MachineInstr *, 2>>
317 if (
MI.getOpcode() == WebAssembly::RETHROW)
318 EHPadToRethrows[
MI.getOperand(0).getMBB()].push_back(&
MI);
319 if (EHPadToRethrows.
empty())
324 for (
auto &[EHPad, Rethrows] : EHPadToRethrows) {
325 auto *
Catch = WebAssembly::findCatch(EHPad);
327 auto InsertPos = std::next(
Catch->getIterator());
328 auto ExnReg = MRI.createVirtualRegister(&WebAssembly::EXNREFRegClass);
329 if (
Catch->getOpcode() == WebAssembly::CATCH) {
330 MachineInstrBuilder MIB =
BuildMI(*EHPad, InsertPos,
Catch->getDebugLoc(),
331 TII.get(WebAssembly::CATCH_REF));
333 for (
const auto &Def :
Catch->defs())
338 for (
const auto &Use :
Catch->uses()) {
342 }
else if (
Catch->getOpcode() == WebAssembly::CATCH_ALL) {
344 TII.get(WebAssembly::CATCH_ALL_REF))
349 Catch->eraseFromParent();
351 for (
auto *Rethrow : Rethrows) {
352 auto InsertPos = std::next(Rethrow->getIterator());
353 BuildMI(*Rethrow->getParent(), InsertPos, Rethrow->getDebugLoc(),
354 TII.get(WebAssembly::THROW_REF))
364bool WebAssemblyLateEHPrepareImpl::removeUnnecessaryUnreachables(
367 for (
auto &
MBB : MF) {
368 for (
auto &
MI :
MBB) {
369 if (
MI.getOpcode() != WebAssembly::THROW &&
370 MI.getOpcode() != WebAssembly::RETHROW &&
371 MI.getOpcode() != WebAssembly::THROW_REF)
381 for (
auto *Succ : Succs)
382 if (!Succ->isEHPad())
394bool WebAssemblyLateEHPrepareImpl::restoreStackPointer(
MachineFunction &MF) {
395 const auto *FrameLowering =
static_cast<const WebAssemblyFrameLowering *
>(
397 if (!FrameLowering->needsPrologForEH(MF))
401 for (
auto &
MBB : MF) {
414 while (InsertPos !=
MBB.
end() && InsertPos->isEHLabel())
417 WebAssembly::isCatch(InsertPos->getOpcode()) &&
418 "catch/catch_all should be present in every EH pad at this point");
420 FrameLowering->writeBackSP(FrameLowering->getSPReg(MF), MF,
MBB, InsertPos,
426bool WebAssemblyLateEHPrepareLegacy::runOnMachineFunction(
MachineFunction &MF) {
427 WebAssemblyLateEHPrepareImpl Impl;
428 return Impl.runOnMachineFunction(MF);
434 WebAssemblyLateEHPrepareImpl Impl;
435 return Impl.runOnMachineFunction(MF)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const HexagonInstrInfo * 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)
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.
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)
ExceptionHandling getExceptionModel() const
Returns the exception model recorded by the "exception-model" module flag, or ExceptionHandling::Defa...
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)
Pass manager infrastructure for declaring and invalidating analyses.
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()