@@ -276,7 +276,7 @@ class SimplifyCFGOpt {
276276 bool simplifyCleanupReturn (CleanupReturnInst *RI);
277277 bool simplifyUnreachable (UnreachableInst *UI);
278278 bool simplifySwitch (SwitchInst *SI, IRBuilder<> &Builder);
279- bool simplifyDuplicateSwitchArms (SwitchInst *SI);
279+ bool simplifyDuplicateSwitchArms (SwitchInst *SI, DomTreeUpdater *DTU );
280280 bool simplifyIndirectBr (IndirectBrInst *IBI);
281281 bool simplifyBranch (BranchInst *Branch, IRBuilder<> &Builder);
282282 bool simplifyUncondBranch (BranchInst *BI, IRBuilder<> &Builder);
@@ -7525,7 +7525,8 @@ template <> struct DenseMapInfo<const CaseHandleWrapper *> {
75257525};
75267526} // namespace llvm
75277527
7528- bool SimplifyCFGOpt::simplifyDuplicateSwitchArms (SwitchInst *SI) {
7528+ bool SimplifyCFGOpt::simplifyDuplicateSwitchArms (SwitchInst *SI,
7529+ DomTreeUpdater *DTU) {
75297530 // Build Cases. Skip BBs that are not candidates for simplification. Mark
75307531 // PHINodes which need to be processed into PhiPredIVs. We decide to process
75317532 // an entire PHI at once after the loop, opposed to calling
@@ -7592,17 +7593,25 @@ bool SimplifyCFGOpt::simplifyDuplicateSwitchArms(SwitchInst *SI) {
75927593 ReplaceWith.reserve (Cases.size ());
75937594
75947595 bool MadeChange = false ;
7596+ SmallVector<DominatorTree::UpdateType> Updates;
7597+ Updates.reserve (ReplaceWith.size ());
75957598 for (auto &CHW : Cases) {
75967599 // CHW is a candidate for simplification. If we find a duplicate BB,
75977600 // replace it.
75987601 const auto [It, Inserted] = ReplaceWith.insert (&CHW);
75997602 if (!Inserted) {
7603+ // We know that SI's parent BB no longer dominates the old case successor
7604+ // since we are making it dead.
7605+ Updates.push_back ({DominatorTree::Delete, SI->getParent (),
7606+ CHW.Case .getCaseSuccessor ()});
76007607 CHW.Case .setSuccessor ((*It)->Case .getCaseSuccessor ());
76017608 MadeChange = true ;
76027609 } else {
76037610 ReplaceWith.insert (&CHW);
76047611 }
76057612 }
7613+ if (DTU)
7614+ DTU->applyUpdates (Updates);
76067615
76077616 return MadeChange;
76087617}
@@ -7667,7 +7676,7 @@ bool SimplifyCFGOpt::simplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) {
76677676 hoistCommonCodeFromSuccessors (SI, !Options.HoistCommonInsts ))
76687677 return requestResimplify ();
76697678
7670- if (simplifyDuplicateSwitchArms (SI))
7679+ if (simplifyDuplicateSwitchArms (SI, DTU ))
76717680 return requestResimplify ();
76727681
76737682 return false ;
0 commit comments