[llvm] 026a34b - [SimplifyCFG] Fix DomTree update in `simplifySwitchDefaultBranch` (#212030)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 25 10:14:34 PDT 2026


Author: Yingwei Zheng
Date: 2026-07-25T17:14:28Z
New Revision: 026a34b3ff5e6a53313ae81853278d55926785d1

URL: https://github.com/llvm/llvm-project/commit/026a34b3ff5e6a53313ae81853278d55926785d1
DIFF: https://github.com/llvm/llvm-project/commit/026a34b3ff5e6a53313ae81853278d55926785d1.diff

LOG: [SimplifyCFG] Fix DomTree update in `simplifySwitchDefaultBranch` (#212030)

When there is no edge to the original default destination BB, also
remove the edge in DT.

This issue cannot be reproduced via
-simplifycfg-require-and-preserve-domtree=1. I just found it by checking
DT in requestResimplify (will be added after fixing all existing
issues). The following test covers this case:

https://github.com/llvm/llvm-project/blob/5bc304c65494702d9d4928ff6cb369e6e6496e53/llvm/test/Transforms/SimplifyCFG/switch-simplify-default.ll#L172-L214

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index a900327d3f8d6..c2ca86077b880 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -7885,14 +7885,12 @@ 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);
+  // 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());
 
   assert(SI->getNumCases() > 0 && "Switch should have at least one case");
   assert(SI->findCaseValue(CaseVal) != SI->case_default() &&


        


More information about the llvm-commits mailing list