diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp --- a/llvm/lib/CodeGen/BranchFolding.cpp +++ b/llvm/lib/CodeGen/BranchFolding.cpp @@ -159,6 +159,19 @@ void BranchFolder::RemoveDeadBlock(MachineBasicBlock *MBB) { assert(MBB->pred_empty() && "MBB must be dead!"); LLVM_DEBUG(dbgs() << "\nRemoving MBB: " << *MBB); +#ifndef NDEBUG + if (MBB->hasAddressTaken()) + for (const MachineBasicBlock &Pred : *MBB->getParent()) + for (const MachineInstr &I : Pred) + if (I.getOpcode() == TargetOpcode::INLINEASM_BR) + // Iterating I.terminators() may be unreliable in this case. + // https://reviews.llvm.org/D78166#1984487 + for (const MachineOperand &Op : I.operands()) + if (Op.isBlockAddress() && + Op.getBlockAddress()->getBasicBlock() == MBB->getBasicBlock()) + assert(false && + "Removing MBB that's an indirect target of INLINEASM_BR"); +#endif MachineFunction *MF = MBB->getParent(); // drop all successors.