[llvm] [SimplifyCFG] Fix DomTree update in `simplifySwitchWhenUMin` (PR #212008)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sat Jul 25 03:18:06 PDT 2026
https://github.com/dtcxzyw created https://github.com/llvm/llvm-project/pull/212008
In the test `switch_remove_dead_cases`, both case 4 and case 5 jump into bb `%case_ret`. Thus, the edge will be removed twice, triggering an assertion failure. This patch uses `applyUpdatesPermissive` to avoid duplicate removals.
>From b28b3500c47a8241bced537ed7461267c934777b Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sat, 25 Jul 2026 18:00:38 +0800
Subject: [PATCH 1/3] [SimplifyCFG] Fix DomTree update in
`simplifySwitchWhenUMin`
---
llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 6 ++++--
llvm/test/Transforms/SimplifyCFG/switch-umin.ll | 2 +-
2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 14efc3c4f5c00..77b1387e3c4c7 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -7828,9 +7828,10 @@ static bool simplifySwitchWhenUMin(SwitchInst *SI, DomTreeUpdater *DTU) {
}
BasicBlock *DeadCaseBB = I->getCaseSuccessor();
DeadCaseBB->removePredecessor(BB);
- Updates.push_back({DominatorTree::Delete, BB, DeadCaseBB});
I = SIW.removeCase(I);
E = SIW->case_end();
+ if (!is_contained(successors(BB), DeadCaseBB))
+ Updates.push_back({DominatorTree::Delete, BB, DeadCaseBB});
}
auto Case = SI->findCaseValue(Constant);
@@ -7849,7 +7850,8 @@ static bool simplifySwitchWhenUMin(SwitchInst *SI, DomTreeUpdater *DTU) {
SIW.removeCase(Case);
SIW->setCondition(A);
- Updates.push_back({DominatorTree::Delete, BB, Unreachable});
+ if (!is_contained(successors(BB), Unreachable))
+ Updates.push_back({DominatorTree::Delete, BB, Unreachable});
if (DTU)
DTU->applyUpdates(Updates);
diff --git a/llvm/test/Transforms/SimplifyCFG/switch-umin.ll b/llvm/test/Transforms/SimplifyCFG/switch-umin.ll
index ff958e4d04147..a6962a3c5285d 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch-umin.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch-umin.ll
@@ -1,5 +1,5 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
-; RUN: opt -S -passes=simplifycfg < %s | FileCheck %s
+; RUN: opt -S -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 < %s | FileCheck %s
declare void @a()
declare void @b()
>From 6baaf0ae8392337f99d407104a0629bcd00f3e3f Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sat, 25 Jul 2026 18:10:24 +0800
Subject: [PATCH 2/3] [SimplifyCFG] Drop non-testable changes
---
llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 77b1387e3c4c7..8d31fa92381a9 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -7850,8 +7850,7 @@ static bool simplifySwitchWhenUMin(SwitchInst *SI, DomTreeUpdater *DTU) {
SIW.removeCase(Case);
SIW->setCondition(A);
- if (!is_contained(successors(BB), Unreachable))
- Updates.push_back({DominatorTree::Delete, BB, Unreachable});
+ Updates.push_back({DominatorTree::Delete, BB, Unreachable});
if (DTU)
DTU->applyUpdates(Updates);
>From 45148439cf18e05cbab2136b51d911cf01e06457 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sat, 25 Jul 2026 18:17:21 +0800
Subject: [PATCH 3/3] [SimplifyCFG] Use `applyUpdatesPermissive`
---
llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 8d31fa92381a9..1de2d718a105a 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -7828,10 +7828,9 @@ static bool simplifySwitchWhenUMin(SwitchInst *SI, DomTreeUpdater *DTU) {
}
BasicBlock *DeadCaseBB = I->getCaseSuccessor();
DeadCaseBB->removePredecessor(BB);
+ Updates.push_back({DominatorTree::Delete, BB, DeadCaseBB});
I = SIW.removeCase(I);
E = SIW->case_end();
- if (!is_contained(successors(BB), DeadCaseBB))
- Updates.push_back({DominatorTree::Delete, BB, DeadCaseBB});
}
auto Case = SI->findCaseValue(Constant);
@@ -7841,7 +7840,7 @@ static bool simplifySwitchWhenUMin(SwitchInst *SI, DomTreeUpdater *DTU) {
// is reachable.
if (!SI->defaultDestUnreachable() || Case == SI->case_default()) {
if (DTU)
- DTU->applyUpdates(Updates);
+ DTU->applyUpdatesPermissive(Updates);
return !Updates.empty();
}
@@ -7853,7 +7852,7 @@ static bool simplifySwitchWhenUMin(SwitchInst *SI, DomTreeUpdater *DTU) {
Updates.push_back({DominatorTree::Delete, BB, Unreachable});
if (DTU)
- DTU->applyUpdates(Updates);
+ DTU->applyUpdatesPermissive(Updates);
return true;
}
More information about the llvm-commits
mailing list