24 #define DEBUG_TYPE "wasm-exception-prepare" 29 return "WebAssembly Prepare Exception";
50 "WebAssembly Exception Preparation",
false,
false)
53 return new WebAssemblyLateEHPrepare();
70 if (Visited.
count(MBB))
74 if (EHPad && EHPad != MBB)
79 if (MBB == &MF->
front())
103 bool WebAssemblyLateEHPrepare::runOnMachineFunction(
MachineFunction &MF) {
108 bool Changed =
false;
109 Changed |= addRethrows(MF);
112 Changed |= replaceFuncletReturns(MF);
113 Changed |= hoistCatches(MF);
114 Changed |= addCatchAlls(MF);
115 Changed |= ensureSingleBBTermPads(MF);
116 Changed |= mergeTerminatePads(MF);
117 Changed |= addCatchAllTerminatePads(MF);
121 bool WebAssemblyLateEHPrepare::replaceFuncletReturns(
MachineFunction &MF) {
122 bool Changed =
false;
126 for (
auto &MBB : MF) {
127 auto Pos = MBB.getFirstTerminator();
128 if (Pos == MBB.end())
136 if (!MBB.isLayoutSuccessor(TBB))
145 if (EHInfo->hasThrowUnwindDest(&MBB))
147 .addMBB(EHInfo->getThrowUnwindDest(&MBB));
150 TII.get(WebAssembly::RETHROW_TO_CALLER));
174 bool Changed =
false;
181 for (
auto *
Catch : Catches) {
183 assert(EHPad &&
"No matching EH pad for catch");
194 bool Changed =
false;
197 for (
auto &MBB : MF) {
204 BuildMI(MBB, MBB.begin(), MBB.begin()->getDebugLoc(),
205 TII.get(WebAssembly::CATCH_ALL));
213 bool Changed =
false;
218 for (
auto &
MI : MBB) {
230 while (InsertPt != MBB.end() && InsertPt->isLabel())
233 if (EHInfo->hasThrowUnwindDest(&MBB))
234 Rethrow =
BuildMI(MBB, InsertPt,
MI.getDebugLoc(),
235 TII.get(WebAssembly::RETHROW))
236 .addMBB(EHInfo->getThrowUnwindDest(&MBB));
238 Rethrow =
BuildMI(MBB, InsertPt,
MI.getDebugLoc(),
239 TII.get(WebAssembly::RETHROW_TO_CALLER));
246 for (
auto *Succ : MBB.successors())
247 if (!Succ->isEHPad())
264 bool WebAssemblyLateEHPrepare::ensureSingleBBTermPads(
MachineFunction &MF) {
278 bool Changed =
false;
279 for (
auto *Call : ClangCallTerminateCalls) {
281 assert(EHPad &&
"No matching EH pad for catch");
284 if (Call->getParent() == EHPad &&
285 Call->getNextNode()->getOpcode() == WebAssembly::UNREACHABLE)
301 EHPad->
insert(InsertPos, Call->removeFromParent());
302 BuildMI(*EHPad, InsertPos, Call->getDebugLoc(),
303 TII.get(WebAssembly::UNREACHABLE));
304 EHPad->
erase(InsertPos, EHPad->
end());
317 bool WebAssemblyLateEHPrepare::mergeTerminatePads(
MachineFunction &MF) {
322 if (TermPads.
empty())
329 TermPad->pred_end());
330 for (
auto *Pred : Preds)
331 Pred->replaceSuccessor(TermPad, UniqueTermPad);
332 TermPad->eraseFromParent();
352 bool WebAssemblyLateEHPrepare::addCatchAllTerminatePads(
MachineFunction &MF) {
358 if (TermPads.
empty())
363 assert(StdTerminateFn &&
"There is no std::terminate() function");
364 for (
auto *CatchTermPad : TermPads) {
365 DebugLoc DL = CatchTermPad->findDebugLoc(CatchTermPad->begin());
366 auto *CatchAllTermPad = MF.CreateMachineBasicBlock();
369 CatchAllTermPad->setIsEHPad();
370 BuildMI(CatchAllTermPad, DL,
TII.get(WebAssembly::CATCH_ALL));
371 BuildMI(CatchAllTermPad, DL,
TII.get(WebAssembly::CALL_VOID))
372 .addGlobalAddress(StdTerminateFn);
373 BuildMI(CatchAllTermPad, DL,
TII.get(WebAssembly::UNREACHABLE));
380 CatchTermPad->addSuccessor(CatchAllTermPad);
void push_back(const T &Elt)
MachineBasicBlock * getMBB() const
Compute iterated dominance frontiers using a linear time algorithm.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
unsigned getReg() const
getReg - Returns the register number.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
iterator_range< succ_iterator > successors()
CLEANUPRET - Represents a return from a cleanup block funclet.
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.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
MachineBasicBlock * GetMatchingEHPad(MachineInstr *MI)
const char *const StdTerminateFn
bool hasPersonalityFn() const
Check whether this function has a personality function.
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
Windows Exception Handling.
INITIALIZE_PASS(WebAssemblyLateEHPrepare, DEBUG_TYPE, "WebAssembly Exception Preparation", false, false) FunctionPass *llvm
This file contains the declaration of the WebAssembly-specific utility functions. ...
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Control flow instructions. These all have token chains.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
const GlobalValue * getGlobal() const
const MCAsmInfo * getMCAsmInfo() const
Return target specific asm information.
This file provides WebAssembly-specific target descriptions.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
const char *const CxaRethrowFn
FunctionPass class - This class is used to implement most global optimizations.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
iterator_range< pred_iterator > predecessors()
static void EraseBBAndChildren(MachineBasicBlock *MBB)
const MachineBasicBlock & front() const
pred_iterator pred_begin()
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
This file declares the WebAssembly-specific subclass of TargetSubtarget.
Iterator for intrusive lists based on ilist_node.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
const char *const ClangCallTerminateFn
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool isCatchTerminatePad(const MachineBasicBlock &MBB)
Returns if the given BB is a single BB terminate pad which starts with a 'catch' instruction.
LLVM_NODISCARD T pop_back_val()
const Function & getFunction() const
Return the LLVM function that this machine code represents.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
CATCHRET - Represents a return from a catch block funclet.
const MachineBasicBlock * getParent() const
const WasmEHFuncInfo * getWasmEHFuncInfo() const
getWasmEHFuncInfo - Return information about how the current function uses Wasm exception handling...
Representation of each machine instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
bool isEHPad() const
Returns true if the block is a landing pad.
LLVM_NODISCARD bool empty() const
FunctionPass * createWebAssemblyLateEHPrepare()
StringRef getName() const
Return a constant reference to the value's name.
bool isCatch(const MachineInstr &MI)
void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ExceptionHandling getExceptionHandlingType() const
StringRef - Represent a constant reference to a string, i.e.
const MachineOperand & getOperand(unsigned i) const