From f463bef09be73ae9a415fcd3fd49689bd95b0f0a Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Fri, 20 Feb 2026 07:03:27 +0800 Subject: [PATCH] [SimplifyCFG] process prof data when remove case in umin (#182261) In #164097, we introduce a optimization for umin. But it does not handle profile data correctly. This PR remove profile data when remove cases. Fixed: #181837 (cherry picked from commit 31e5f86a3cdc960ef7b2f0a533c4a37cf526cacd) --- llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 2 +- .../Transforms/SimplifyCFG/switch-umin.ll | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index 5f4807242581d..a16f274a4ed5a 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -7724,7 +7724,7 @@ 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); + I = SIW.removeCase(I); E = SIW->case_end(); } diff --git a/llvm/test/Transforms/SimplifyCFG/switch-umin.ll b/llvm/test/Transforms/SimplifyCFG/switch-umin.ll index 44665365dc222..ff958e4d04147 100644 --- a/llvm/test/Transforms/SimplifyCFG/switch-umin.ll +++ b/llvm/test/Transforms/SimplifyCFG/switch-umin.ll @@ -239,8 +239,51 @@ case4: } +define void @switch_remove_dead_cases(i32 %x) { +; CHECK-LABEL: define void @switch_remove_dead_cases( +; CHECK-SAME: i32 [[X:%.*]]) { +; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.umin.i32(i32 [[X]], i32 4) +; CHECK-NEXT: switch i32 [[X]], label %[[COMMON_RET:.*]] [ +; CHECK-NEXT: i32 2, label %[[CASE_A:.*]] +; CHECK-NEXT: i32 3, label %[[CASE_B:.*]] +; CHECK-NEXT: ], !prof [[PROF1:![0-9]+]] +; CHECK: [[COMMON_RET]]: +; CHECK-NEXT: ret void +; CHECK: [[CASE_A]]: +; CHECK-NEXT: call void @a() +; CHECK-NEXT: br label %[[COMMON_RET]] +; CHECK: [[CASE_B]]: +; CHECK-NEXT: call void @b() +; CHECK-NEXT: br label %[[COMMON_RET]] +; + %min = call i32 @llvm.umin.i32(i32 %x, i32 4) + switch i32 %min, label %unreachable [ + i32 2, label %case_a + i32 3, label %case_b + i32 4, label %case_ret + i32 5, label %case_ret + ], !prof !1 + +case_a: + call void @a() + ret void + +case_b: + call void @b() + ret void + +case_ret: + ret void + +unreachable: + unreachable +} !0 = !{!"branch_weights", i32 1, i32 2, i32 3, i32 99, i32 5} ;. ; CHECK: [[PROF0]] = !{!"branch_weights", i32 5, i32 2, i32 3, i32 99} ;. +!1 = !{!"branch_weights", i32 11, i32 12, i32 13, i32 14, i32 15} +;. +; CHECK: [[PROF1]] = !{!"branch_weights", i32 14, i32 12, i32 13} +;.