diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -6613,8 +6613,24 @@ if (DTU) DTU->applyUpdates({{DominatorTree::Delete, Predecessor, BB}}); return true; + } else if (SwitchInst *SI = dyn_cast(T)) { + // Create the new block in which I will redirect the edges that + // lead to undefine behavior + BasicBlock *Unreachable = BasicBlock::Create( + Predecessor->getContext(), "unreachable", SI->getFunction()); + Builder.SetInsertPoint(Unreachable); + // The new block contains only one instruction: Unreachable + Builder.CreateUnreachable(); + for (auto &Case : SI->cases()) + if (Case.getCaseSuccessor() == BB) + Case.setSuccessor(Unreachable); + + if (DTU) + DTU->applyUpdates( + { { DominatorTree::Delete, Predecessor, BB }, + { DominatorTree::Insert, Predecessor, Unreachable } }); + return true; } - // TODO: SwitchInst. } return false;