From: Heejin Ahn Date: Sat, 16 Mar 2019 04:46:05 +0000 (+0000) Subject: [WebAssembly] Method order change in LateEHPrepare (NFC) X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c1447a19117481bd41b1ca27f7a1c511d000a532;p=llvm [WebAssembly] Method order change in LateEHPrepare (NFC) Summary: Currently the order of these methods does not matter, but the following CL needs to have this order changed. Merging the order change and the semantics change within a CL complicates the diff, so submitting the order change first. Reviewers: dschuff Subscribers: sbc100, jgravelle-google, sunfish, jdoerfert, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D59342 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@356315 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp b/lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp index 603b717812e..5b6fdb86c6d 100644 --- a/lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp +++ b/lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp @@ -30,9 +30,9 @@ class WebAssemblyLateEHPrepare final : public MachineFunctionPass { } bool runOnMachineFunction(MachineFunction &MF) override; - bool removeUnnecessaryUnreachables(MachineFunction &MF); - bool replaceFuncletReturns(MachineFunction &MF); bool addCatches(MachineFunction &MF); + bool replaceFuncletReturns(MachineFunction &MF); + bool removeUnnecessaryUnreachables(MachineFunction &MF); bool addExceptionExtraction(MachineFunction &MF); bool restoreStackPointer(MachineFunction &MF); @@ -108,39 +108,35 @@ bool WebAssemblyLateEHPrepare::runOnMachineFunction(MachineFunction &MF) { return false; bool Changed = false; + if (MF.getFunction().hasPersonalityFn()) { + Changed |= addCatches(MF); + Changed |= replaceFuncletReturns(MF); + } Changed |= removeUnnecessaryUnreachables(MF); - if (!MF.getFunction().hasPersonalityFn()) - return Changed; - Changed |= replaceFuncletReturns(MF); - Changed |= addCatches(MF); - Changed |= addExceptionExtraction(MF); - Changed |= restoreStackPointer(MF); + if (MF.getFunction().hasPersonalityFn()) { + Changed |= addExceptionExtraction(MF); + Changed |= restoreStackPointer(MF); + } return Changed; } -bool WebAssemblyLateEHPrepare::removeUnnecessaryUnreachables( - MachineFunction &MF) { +// 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) { - for (auto &MI : MBB) { - if (MI.getOpcode() != WebAssembly::THROW && - MI.getOpcode() != WebAssembly::RETHROW) - continue; + if (MBB.isEHPad()) { Changed = true; - - // The instruction after the throw should be an unreachable or a branch to - // another BB that should eventually lead to an unreachable. Delete it - // because throw itself is a terminator, and also delete successors if - // any. - MBB.erase(std::next(MI.getIterator()), MBB.end()); - SmallVector Succs(MBB.succ_begin(), - MBB.succ_end()); - for (auto *Succ : Succs) - MBB.removeSuccessor(Succ); - eraseDeadBBsAndChildren(Succs); + 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; } @@ -177,23 +173,29 @@ bool WebAssemblyLateEHPrepare::replaceFuncletReturns(MachineFunction &MF) { return Changed; } -// Add catch instruction to beginning of catchpads and cleanuppads. -bool WebAssemblyLateEHPrepare::addCatches(MachineFunction &MF) { +bool WebAssemblyLateEHPrepare::removeUnnecessaryUnreachables( + MachineFunction &MF) { bool Changed = false; - const auto &TII = *MF.getSubtarget().getInstrInfo(); - MachineRegisterInfo &MRI = MF.getRegInfo(); for (auto &MBB : MF) { - if (MBB.isEHPad()) { + for (auto &MI : MBB) { + if (MI.getOpcode() != WebAssembly::THROW && + MI.getOpcode() != WebAssembly::RETHROW) + continue; 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); + + // The instruction after the throw should be an unreachable or a branch to + // another BB that should eventually lead to an unreachable. Delete it + // because throw itself is a terminator, and also delete successors if + // any. + MBB.erase(std::next(MI.getIterator()), MBB.end()); + SmallVector Succs(MBB.succ_begin(), + MBB.succ_end()); + for (auto *Succ : Succs) + MBB.removeSuccessor(Succ); + eraseDeadBBsAndChildren(Succs); } } + return Changed; }