Index: lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp =================================================================== --- lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp +++ lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp @@ -31,8 +31,8 @@ bool runOnMachineFunction(MachineFunction &MF) override; bool removeUnnecessaryUnreachables(MachineFunction &MF); - bool replaceFuncletReturns(MachineFunction &MF); bool addCatches(MachineFunction &MF); + bool replaceFuncletReturns(MachineFunction &MF); bool addExceptionExtraction(MachineFunction &MF); bool restoreStackPointer(MachineFunction &MF); @@ -111,8 +111,8 @@ Changed |= removeUnnecessaryUnreachables(MF); if (!MF.getFunction().hasPersonalityFn()) return Changed; - Changed |= replaceFuncletReturns(MF); Changed |= addCatches(MF); + Changed |= replaceFuncletReturns(MF); Changed |= addExceptionExtraction(MF); Changed |= restoreStackPointer(MF); return Changed; @@ -144,6 +144,26 @@ return Changed; } +// Add catch instruction to beginning of catchpads and cleanuppads. +bool WebAssemblyLateEHPrepare::addCatches(MachineFunction &MF) { + bool Changed = false; + const auto &TII = *MF.getSubtarget().getInstrInfo(); + MachineRegisterInfo &MRI = MF.getRegInfo(); + for (auto &MBB : MF) { + if (MBB.isEHPad()) { + Changed = true; + auto InsertPos = MBB.begin(); + if (InsertPos->isEHLabel()) // EH pad starts with an EH label + ++InsertPos; + unsigned DstReg = + MRI.createVirtualRegister(&WebAssembly::EXCEPT_REFRegClass); + BuildMI(MBB, InsertPos, MBB.begin()->getDebugLoc(), + TII.get(WebAssembly::CATCH), DstReg); + } + } + return Changed; +} + bool WebAssemblyLateEHPrepare::replaceFuncletReturns(MachineFunction &MF) { bool Changed = false; const auto &TII = *MF.getSubtarget().getInstrInfo(); @@ -177,26 +197,6 @@ return Changed; } -// Add catch instruction to beginning of catchpads and cleanuppads. -bool WebAssemblyLateEHPrepare::addCatches(MachineFunction &MF) { - bool Changed = false; - const auto &TII = *MF.getSubtarget().getInstrInfo(); - MachineRegisterInfo &MRI = MF.getRegInfo(); - for (auto &MBB : MF) { - if (MBB.isEHPad()) { - Changed = true; - auto InsertPos = MBB.begin(); - if (InsertPos->isEHLabel()) // EH pad starts with an EH label - ++InsertPos; - unsigned DstReg = - MRI.createVirtualRegister(&WebAssembly::EXCEPT_REFRegClass); - BuildMI(MBB, InsertPos, MBB.begin()->getDebugLoc(), - TII.get(WebAssembly::CATCH), DstReg); - } - } - return Changed; -} - // Wasm uses 'br_on_exn' instruction to check the tag of an exception. It takes // except_ref type object returned by 'catch', and branches to the destination // if it matches a given tag. We currently use __cpp_exception symbol to