Index: include/llvm/CodeGen/WasmEHFuncInfo.h =================================================================== --- include/llvm/CodeGen/WasmEHFuncInfo.h +++ include/llvm/CodeGen/WasmEHFuncInfo.h @@ -52,6 +52,12 @@ bool hasThrowUnwindDest(const BasicBlock *BB) const { return ThrowUnwindMap.count(BB); } + void eraseEHPadUnwindDest(const BasicBlock *BB) { + EHPadUnwindMap.erase(BB); + } + void eraseThrowUnwindDest(const BasicBlock *BB) { + ThrowUnwindMap.erase(BB); + } MachineBasicBlock *getEHPadUnwindDest(MachineBasicBlock *MBB) const { return EHPadUnwindMap.lookup(MBB).get(); @@ -71,6 +77,12 @@ bool hasThrowUnwindDest(MachineBasicBlock *MBB) const { return ThrowUnwindMap.count(MBB); } + void eraseEHPadUnwindDest(MachineBasicBlock *MBB) { + EHPadUnwindMap.erase(MBB); + } + void eraseThrowUnwindDest(MachineBasicBlock *MBB) { + ThrowUnwindMap.erase(MBB); + } }; // Analyze the IR in the given function to build WasmEHFuncInfo. Index: lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp =================================================================== --- lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp +++ lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp @@ -289,6 +289,11 @@ .addReg(ExnRefReg); BuildMI(EHPad, DL, TII.get(WebAssembly::BR)).addMBB(ElseMBB); + if (EHInfo->hasThrowUnwindDest(EHPad)) { + EHInfo->setThrowUnwindDest(ThenMBB, EHInfo->getThrowUnwindDest(EHPad)); + EHInfo->eraseThrowUnwindDest(EHPad); + } + // When this is a terminate pad with __clang_call_terminate() call, we don't // rethrow it anymore and call __clang_call_terminate() with a nullptr // argument, which will call std::terminate(). @@ -325,8 +330,11 @@ } else { BuildMI(ElseMBB, DL, TII.get(WebAssembly::RETHROW)); - if (EHInfo->hasEHPadUnwindDest(EHPad)) - EHInfo->setThrowUnwindDest(ElseMBB, EHInfo->getEHPadUnwindDest(EHPad)); + if (EHInfo->hasEHPadUnwindDest(EHPad)) { + auto *DestMBB = EHInfo->getEHPadUnwindDest(EHPad); + EHInfo->setThrowUnwindDest(ElseMBB, DestMBB); + ElseMBB->addSuccessor(DestMBB); + } } }