diff --git a/llvm/include/llvm/CodeGen/MachineJumpTableInfo.h b/llvm/include/llvm/CodeGen/MachineJumpTableInfo.h --- a/llvm/include/llvm/CodeGen/MachineJumpTableInfo.h +++ b/llvm/include/llvm/CodeGen/MachineJumpTableInfo.h @@ -106,6 +106,9 @@ JumpTables[Idx].MBBs.clear(); } + /// RemoveMBBFromJumpTables - If MBB is present in any jump tables, remove it + bool RemoveMBBFromJumpTables(MachineBasicBlock *MBB); + /// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update /// the jump tables to branch to New instead. bool ReplaceMBBInJumpTables(MachineBasicBlock *Old, MachineBasicBlock *New); diff --git a/llvm/lib/CodeGen/MachineFunction.cpp b/llvm/lib/CodeGen/MachineFunction.cpp --- a/llvm/lib/CodeGen/MachineFunction.cpp +++ b/llvm/lib/CodeGen/MachineFunction.cpp @@ -420,6 +420,9 @@ void MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) { assert(MBB->getParent() == this && "MBB parent mismatch!"); + // Clean up any references to MBB in jump tables before deleting it + if (JumpTableInfo) + JumpTableInfo->RemoveMBBFromJumpTables(MBB); MBB->~MachineBasicBlock(); BasicBlockRecycler.Deallocate(Allocator, MBB); } @@ -1047,6 +1050,17 @@ return MadeChange; } +/// If MBB is present in any jump tables, remove it +bool MachineJumpTableInfo::RemoveMBBFromJumpTables(MachineBasicBlock *MBB) { + bool MadeChange = false; + for (MachineJumpTableEntry &JTE : JumpTables) { + auto removeBeginItr = std::remove(JTE.MBBs.begin(), JTE.MBBs.end(), MBB); + MadeChange |= (removeBeginItr != JTE.MBBs.end()); + JTE.MBBs.erase(removeBeginItr, JTE.MBBs.end()); + } + return MadeChange; +} + /// If Old is a target of the jump tables, update the jump table to branch to /// New instead. bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,