[llvm] [SimplifyCFG] Fix DomTree update in `simplifySwitchDefaultBranch` (PR #212030)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 25 09:37:53 PDT 2026


https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/212030

>From b34648098edbefef4ec7f99abc8d3972ec05534a Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sat, 25 Jul 2026 23:43:51 +0800
Subject: [PATCH 1/2] [SimplifyCFG] Fix DomTree update in
 `simplifySwitchDefaultBranch`

---
 llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 14efc3c4f5c00..e03573abe0ba5 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -7885,14 +7885,9 @@ static bool simplifySwitchDefaultBranch(SwitchInst *SI, DomTreeUpdater *DTU,
     SwitchInstProfUpdateWrapper SIW(*SI);
     SIW.addCase(CaseVal, Default, SIW.getSuccessorWeight(0));
     SIW.setSuccessorWeight(0, 0);
-  } else {
-    // On the other hand, if there is a pre-existing case for the
-    // constant, the default branch will be removed rather than being
-    // moved. Thus, we are removing an edge in the CFG, and need to
-    // update any PHIs in the default block.
-    Default->removePredecessor(SI->getParent());
   }
-  createUnreachableSwitchDefault(SI, DTU, /*RemoveOrigDefaultBlock*/ false);
+  createUnreachableSwitchDefault(SI, DTU, /*RemoveOrigDefaultBlock=*/CaseIt !=
+                                              SI->case_default());
 
   assert(SI->getNumCases() > 0 && "Switch should have at least one case");
   assert(SI->findCaseValue(CaseVal) != SI->case_default() &&

>From a0cd326aa60fe94e65e342df76f8600a271c51d6 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sun, 26 Jul 2026 00:37:31 +0800
Subject: [PATCH 2/2] [SimplifyCFG] Restore comments.

---
 llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index e03573abe0ba5..e79dccebedf85 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -7886,6 +7886,9 @@ static bool simplifySwitchDefaultBranch(SwitchInst *SI, DomTreeUpdater *DTU,
     SIW.addCase(CaseVal, Default, SIW.getSuccessorWeight(0));
     SIW.setSuccessorWeight(0, 0);
   }
+  // If there is a pre-existing case for the constant, the default branch
+  // will be removed rather than being moved. Thus, we are removing an edge
+  // in the CFG, and need to update any PHIs in the default block.
   createUnreachableSwitchDefault(SI, DTU, /*RemoveOrigDefaultBlock=*/CaseIt !=
                                               SI->case_default());
 



More information about the llvm-commits mailing list