[llvm] [SimplfiyCFG] Deduplicate edges in `hoistSuccIdenticalTerminatorToSwitchOrIf` (PR #212017)

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


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

Fix domtree update by deduplicating edges to be inserted. Tested by the function `@dedup`.


>From f0fbfcc87a30f925fc127fd29b9b908e103c85ee Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sat, 25 Jul 2026 19:43:16 +0800
Subject: [PATCH] [SimplfiyCFG] Deduplicate edges in
 `hoistSuccIdenticalTerminatorToSwitchOrIf`

---
 llvm/lib/Transforms/Utils/SimplifyCFG.cpp        | 4 +++-
 llvm/test/Transforms/SimplifyCFG/switch-dedup.ll | 2 +-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 14efc3c4f5c00..994b8f32d28b7 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2178,9 +2178,11 @@ bool SimplifyCFGOpt::hoistSuccIdenticalTerminatorToSwitchOrIf(
   SmallVector<DominatorTree::UpdateType, 4> Updates;
 
   // Update any PHI nodes in our new successors.
+  SmallPtrSet<BasicBlock *, 8> VisitedSuccs;
   for (BasicBlock *Succ : successors(BB1)) {
     addPredecessorToBlock(Succ, TIParent, BB1);
-    if (DTU)
+
+    if (DTU && VisitedSuccs.insert(Succ).second)
       Updates.push_back({DominatorTree::Insert, TIParent, Succ});
   }
 
diff --git a/llvm/test/Transforms/SimplifyCFG/switch-dedup.ll b/llvm/test/Transforms/SimplifyCFG/switch-dedup.ll
index 1a8115e3ca386..f770d76d29b11 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch-dedup.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch-dedup.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
 
 define i32 @nodedup(i1 %c, i16 %v) {
 ; CHECK-LABEL: define i32 @nodedup(



More information about the llvm-commits mailing list