[llvm] [SimplifyCFG] Fix DomTree update in `turnSwitchRangeIntoICmp` (PR #212006)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 25 02:48:13 PDT 2026


https://github.com/dtcxzyw created https://github.com/llvm/llvm-project/pull/212006

The original removal of `BB->Default` has been handled by `createUnreachableSwitchDefault`.
When `OtherDest` is also unreachable, the edge should be removed.

This issue cannot be reproduced via `-simplifycfg-require-and-preserve-domtree=1`. I just found it by checking DT in `requestResimplify`.

>From f7b7a57bf1ca5595e391891259cd8a2022112c24 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sat, 25 Jul 2026 17:43:53 +0800
Subject: [PATCH] [SimplifyCFG] Fix DomTree update in `turnSwitchRangeIntoICmp`

---
 llvm/lib/Transforms/Utils/SimplifyCFG.cpp    | 6 ++----
 llvm/test/Transforms/SimplifyCFG/pr165301.ll | 2 +-
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 14efc3c4f5c00..90e17915613c6 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -6142,13 +6142,11 @@ bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
   if (!HasDefault)
     createUnreachableSwitchDefault(SI, DTU);
 
-  auto *UnreachableDefault = SI->getDefaultDest();
-
   // Drop the switch.
   SI->eraseFromParent();
 
-  if (!HasDefault && DTU)
-    DTU->applyUpdates({{DominatorTree::Delete, BB, UnreachableDefault}});
+  if (DTU && isa<UncondBrInst>(NewBI))
+    DTU->applyUpdates({{DominatorTree::Delete, BB, OtherDest}});
 
   return true;
 }
diff --git a/llvm/test/Transforms/SimplifyCFG/pr165301.ll b/llvm/test/Transforms/SimplifyCFG/pr165301.ll
index 1df655250f57e..f4a2e0296f566 100644
--- a/llvm/test/Transforms/SimplifyCFG/pr165301.ll
+++ b/llvm/test/Transforms/SimplifyCFG/pr165301.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals all --version 6
-; RUN: opt -S -passes="simplifycfg<switch-range-to-icmp>" < %s | FileCheck %s
+; RUN: opt -S -passes="simplifycfg<switch-range-to-icmp>" -simplifycfg-require-and-preserve-domtree=1 < %s | FileCheck %s
 
 ; Make sure there's no use after free when removing incoming values from PHI nodes
 



More information about the llvm-commits mailing list