[llvm] [SimplifyCFG] Don't propagate weights to unconditional branches in `turnSwitchRangeIntoICmp` (PR #165931)

Mircea Trofin via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 31 15:29:52 PDT 2025


https://github.com/mtrofin created https://github.com/llvm/llvm-project/pull/165931

None

>From d87a30b71f9da35c5838ebee94bdbfd07487575a Mon Sep 17 00:00:00 2001
From: Mircea Trofin <mtrofin at google.com>
Date: Fri, 31 Oct 2025 15:29:22 -0700
Subject: [PATCH] [SimplifyCFG] Don't propagate weights to unconditional
 branches in `turnSwitchRangeIntoICmp`

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

diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 6addcfab15125..cbc604e87cf1a 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -5956,7 +5956,7 @@ bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
   }
 
   // Update weight for the newly-created conditional branch.
-  if (hasBranchWeightMD(*SI)) {
+  if (hasBranchWeightMD(*SI) && NewBI->isConditional()) {
     SmallVector<uint64_t, 8> Weights;
     getBranchWeights(SI, Weights);
     if (Weights.size() == 1 + SI->getNumCases()) {
diff --git a/llvm/test/Transforms/SimplifyCFG/pr165301.ll b/llvm/test/Transforms/SimplifyCFG/pr165301.ll
index 4a539d77af3cb..74cc0c3d46c54 100644
--- a/llvm/test/Transforms/SimplifyCFG/pr165301.ll
+++ b/llvm/test/Transforms/SimplifyCFG/pr165301.ll
@@ -3,7 +3,7 @@
 
 ; Make sure there's no use after free when removing incoming values from PHI nodes
 
-define i32 @pr165301(i1 %cond) {
+define i32 @pr165301(i1 %cond) !prof !0 {
 ; CHECK-LABEL: define i32 @pr165301(
 ; CHECK-SAME: i1 [[COND:%.*]]) {
 ; CHECK-NEXT:  [[ENTRY:.*:]]
@@ -18,9 +18,11 @@ switchbb:
   switch i1 %cond, label %default [
   i1 false, label %switchbb
   i1 true, label %switchbb
-  ]
+  ], !prof !1
 
 default:
   %phi.lcssa = phi i32 [ 0, %switchbb ]
   ret i32 %phi.lcssa
 }
+!0 = !{!"function_entry_count", i32 10}
+!1 = !{!"branch_weights", i32 2, i32 3, i32 5}



More information about the llvm-commits mailing list