#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
#include "Utils/WebAssemblyUtilities.h"
#include "WebAssembly.h"
#include "WebAssemblySubtarget.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/WasmEHFuncInfo.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/Support/Debug.h"
#include "llvm/Target/TargetMachine.h"
using namespace llvm;
#define DEBUG_TYPE "wasm-late-eh-prepare"
namespace {
class WebAssemblyLateEHPrepare final : public MachineFunctionPass {
StringRef getPassName() const override {
return "WebAssembly Late Prepare Exception";
}
bool runOnMachineFunction(MachineFunction &MF) override;
bool removeUnreachableEHPads(MachineFunction &MF);
void recordCatchRetBBs(MachineFunction &MF);
bool hoistCatches(MachineFunction &MF);
bool addCatchAlls(MachineFunction &MF);
bool replaceFuncletReturns(MachineFunction &MF);
bool removeUnnecessaryUnreachables(MachineFunction &MF);
bool restoreStackPointer(MachineFunction &MF);
MachineBasicBlock *getMatchingEHPad(MachineInstr *MI);
SmallPtrSet<MachineBasicBlock *, 8> CatchRetBBs;
public:
static char ID; WebAssemblyLateEHPrepare() : MachineFunctionPass(ID) {}
};
}
char WebAssemblyLateEHPrepare::ID = 0;
INITIALIZE_PASS(WebAssemblyLateEHPrepare, DEBUG_TYPE,
"WebAssembly Late Exception Preparation", false, false)
FunctionPass *llvm::createWebAssemblyLateEHPrepare() {
return new WebAssemblyLateEHPrepare();
}
MachineBasicBlock *
WebAssemblyLateEHPrepare::getMatchingEHPad(MachineInstr *MI) {
MachineFunction *MF = MI->getParent()->getParent();
SmallVector<MachineBasicBlock *, 2> WL;
SmallPtrSet<MachineBasicBlock *, 2> Visited;
WL.push_back(MI->getParent());
MachineBasicBlock *EHPad = nullptr;
while (!WL.empty()) {
MachineBasicBlock *MBB = WL.pop_back_val();
if (!Visited.insert(MBB).second)
continue;
if (MBB->isEHPad()) {
if (EHPad && EHPad != MBB)
return nullptr;
EHPad = MBB;
continue;
}
if (MBB == &MF->front())
return nullptr;
for (auto *Pred : MBB->predecessors())
if (!CatchRetBBs.count(Pred)) WL.push_back(Pred);
}
return EHPad;
}
template <typename Container>
static void eraseDeadBBsAndChildren(const Container &MBBs) {
SmallVector<MachineBasicBlock *, 8> WL(MBBs.begin(), MBBs.end());
SmallPtrSet<MachineBasicBlock *, 8> Deleted;
while (!WL.empty()) {
MachineBasicBlock *MBB = WL.pop_back_val();
if (Deleted.count(MBB) || !MBB->pred_empty())
continue;
SmallVector<MachineBasicBlock *, 4> Succs(MBB->successors());
WL.append(MBB->succ_begin(), MBB->succ_end());
for (auto *Succ : Succs)
MBB->removeSuccessor(Succ);
Deleted.insert(MBB);
MBB->eraseFromParent();
}
}
bool WebAssemblyLateEHPrepare::runOnMachineFunction(MachineFunction &MF) {
LLVM_DEBUG(dbgs() << "********** Late EH Prepare **********\n"
"********** Function: "
<< MF.getName() << '\n');
if (MF.getTarget().getMCAsmInfo()->getExceptionHandlingType() !=
ExceptionHandling::Wasm)
return false;
bool Changed = false;
if (MF.getFunction().hasPersonalityFn()) {
Changed |= removeUnreachableEHPads(MF);
recordCatchRetBBs(MF);
Changed |= hoistCatches(MF);
Changed |= addCatchAlls(MF);
Changed |= replaceFuncletReturns(MF);
}
Changed |= removeUnnecessaryUnreachables(MF);
if (MF.getFunction().hasPersonalityFn())
Changed |= restoreStackPointer(MF);
return Changed;
}
bool WebAssemblyLateEHPrepare::removeUnreachableEHPads(MachineFunction &MF) {
SmallVector<MachineBasicBlock *, 4> ToDelete;
for (auto &MBB : MF)
if (MBB.isEHPad() && MBB.pred_empty())
ToDelete.push_back(&MBB);
eraseDeadBBsAndChildren(ToDelete);
return !ToDelete.empty();
}
void WebAssemblyLateEHPrepare::recordCatchRetBBs(MachineFunction &MF) {
CatchRetBBs.clear();
for (auto &MBB : MF) {
auto Pos = MBB.getFirstTerminator();
if (Pos == MBB.end())
continue;
MachineInstr *TI = &*Pos;
if (TI->getOpcode() == WebAssembly::CATCHRET)
CatchRetBBs.insert(&MBB);
}
}
bool WebAssemblyLateEHPrepare::hoistCatches(MachineFunction &MF) {
bool Changed = false;
SmallVector<MachineInstr *, 16> Catches;
for (auto &MBB : MF)
for (auto &MI : MBB)
if (WebAssembly::isCatch(MI.getOpcode()))
Catches.push_back(&MI);
for (auto *Catch : Catches) {
MachineBasicBlock *EHPad = getMatchingEHPad(Catch);
assert(EHPad && "No matching EH pad for catch");
auto InsertPos = EHPad->begin();
while (InsertPos != EHPad->end() && InsertPos->isEHLabel())
InsertPos++;
if (InsertPos == Catch)
continue;
Changed = true;
EHPad->insert(InsertPos, Catch->removeFromParent());
}
return Changed;
}
bool WebAssemblyLateEHPrepare::addCatchAlls(MachineFunction &MF) {
bool Changed = false;
const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
for (auto &MBB : MF) {
if (!MBB.isEHPad())
continue;
auto InsertPos = MBB.begin();
while (InsertPos != MBB.end() && InsertPos->isEHLabel())
InsertPos++;
if (InsertPos == MBB.end() ||
!WebAssembly::isCatch(InsertPos->getOpcode())) {
Changed = true;
BuildMI(MBB, InsertPos,
InsertPos == MBB.end() ? DebugLoc() : InsertPos->getDebugLoc(),
TII.get(WebAssembly::CATCH_ALL));
}
}
return Changed;
}
bool WebAssemblyLateEHPrepare::replaceFuncletReturns(MachineFunction &MF) {
bool Changed = false;
const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
for (auto &MBB : MF) {
auto Pos = MBB.getFirstTerminator();
if (Pos == MBB.end())
continue;
MachineInstr *TI = &*Pos;
switch (TI->getOpcode()) {
case WebAssembly::CATCHRET: {
MachineBasicBlock *TBB = TI->getOperand(0).getMBB();
if (!MBB.isLayoutSuccessor(TBB))
BuildMI(MBB, TI, TI->getDebugLoc(), TII.get(WebAssembly::BR))
.addMBB(TBB);
TI->eraseFromParent();
Changed = true;
break;
}
case WebAssembly::CLEANUPRET: {
BuildMI(MBB, TI, TI->getDebugLoc(), TII.get(WebAssembly::RETHROW))
.addImm(0);
TI->eraseFromParent();
Changed = true;
break;
}
}
}
return Changed;
}
bool WebAssemblyLateEHPrepare::removeUnnecessaryUnreachables(
MachineFunction &MF) {
bool Changed = false;
for (auto &MBB : MF) {
for (auto &MI : MBB) {
if (MI.getOpcode() != WebAssembly::THROW &&
MI.getOpcode() != WebAssembly::RETHROW)
continue;
Changed = true;
MBB.erase(std::next(MI.getIterator()), MBB.end());
SmallVector<MachineBasicBlock *, 8> Succs(MBB.successors());
for (auto *Succ : Succs)
if (!Succ->isEHPad())
MBB.removeSuccessor(Succ);
eraseDeadBBsAndChildren(Succs);
}
}
return Changed;
}
bool WebAssemblyLateEHPrepare::restoreStackPointer(MachineFunction &MF) {
const auto *FrameLowering = static_cast<const WebAssemblyFrameLowering *>(
MF.getSubtarget().getFrameLowering());
if (!FrameLowering->needsPrologForEH(MF))
return false;
bool Changed = false;
for (auto &MBB : MF) {
if (!MBB.isEHPad())
continue;
Changed = true;
auto InsertPos = MBB.begin();
while (InsertPos != MBB.end() && InsertPos->isEHLabel())
InsertPos++;
assert(InsertPos != MBB.end() &&
WebAssembly::isCatch(InsertPos->getOpcode()) &&
"catch/catch_all should be present in every EH pad at this point");
++InsertPos; FrameLowering->writeSPToGlobal(FrameLowering->getSPReg(MF), MF, MBB,
InsertPos, MBB.begin()->getDebugLoc());
}
return Changed;
}