[llvm-branch-commits] [llvm] 6e90682 - Revert "[SimplifyCFG] Fix branch-weight overflow when folding switch case int…"

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Jul 28 09:47:51 PDT 2026


Author: Aiden Grossman
Date: 2026-07-28T09:47:47-07:00
New Revision: 6e906825c399fc36565e08c57848650f402690e0

URL: https://github.com/llvm/llvm-project/commit/6e906825c399fc36565e08c57848650f402690e0
DIFF: https://github.com/llvm/llvm-project/commit/6e906825c399fc36565e08c57848650f402690e0.diff

LOG: Revert "[SimplifyCFG] Fix branch-weight overflow when folding switch case int…"

This reverts commit 4674ce3fe91a0edea6cf780332891575af0884e9.

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/Local.cpp

Removed: 
    llvm/test/Transforms/SimplifyCFG/switch-default-fold-weight-overflow.ll


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index 643ad0fb24267..b17740c0bc192 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -228,10 +228,8 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions,
           unsigned Idx = It->getCaseIndex();
 
           // Check for and prevent uint64_t overflow by reducing branch weights.
-          if (Weights[0] > UINT64_MAX - Weights[Idx + 1]) {
-            SmallVector<uint32_t> Fitted = fitWeights(Weights);
-            Weights.assign(Fitted.begin(), Fitted.end());
-          }
+          if (Weights[0] > UINT64_MAX - Weights[Idx + 1])
+            fitWeights(Weights);
 
           Weights[0] += Weights[Idx + 1];
           // Remove weight for this case.

diff  --git a/llvm/test/Transforms/SimplifyCFG/switch-default-fold-weight-overflow.ll b/llvm/test/Transforms/SimplifyCFG/switch-default-fold-weight-overflow.ll
deleted file mode 100644
index 91311343e5572..0000000000000
--- a/llvm/test/Transforms/SimplifyCFG/switch-default-fold-weight-overflow.ll
+++ /dev/null
@@ -1,45 +0,0 @@
-; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
-; RUN: opt < %s -passes=simplifycfg -S | FileCheck %s
-
-; A case whose destination is the same as the default gets folded into the
-; default, adding its weight to the default's. If default + case overflows
-; uint64_t, the weights must be downscaled via fitWeights() before the
-; addition; otherwise it wraps around and the default's huge weight turns
-; into a tiny, wrong one.
-;
-; default = 2^64 - 100, folded case = 200 (default + case wraps around to
-; 100 as a uint64_t), surviving case = 1000. Correctly scaled, default stays
-; by far the biggest weight. If the downscale isn't applied, it collapses to
-; 100 -- smaller than the surviving case's 1000, which is the wrong relative
-; order.
-
-define void @switch_default_fold_overflow(i32 %x, ptr %p) {
-; CHECK-LABEL: define void @switch_default_fold_overflow(
-; CHECK-SAME: i32 [[X:%.*]], ptr [[P:%.*]]) {
-; CHECK-NEXT:  [[ENTRY:.*:]]
-; CHECK-NEXT:    [[COND:%.*]] = icmp eq i32 [[X]], 1
-; CHECK-NEXT:    br i1 [[COND]], label %[[OTHER:.*]], label %[[DEFAULT:.*]], !prof [[PROF0:![0-9]+]]
-; CHECK:       [[DEFAULT]]:
-; CHECK-NEXT:    ret void
-; CHECK:       [[OTHER]]:
-; CHECK-NEXT:    store i32 0, ptr [[P]], align 4
-; CHECK-NEXT:    br label %[[DEFAULT]]
-;
-entry:
-  switch i32 %x, label %default [
-  i32 0, label %default
-  i32 1, label %other
-  ], !prof !0
-
-default:
-  ret void
-
-other:
-  store i32 0, ptr %p
-  br label %default
-}
-
-!0 = !{!"branch_weights", i64 18446744073709551516, i64 200, i64 1000}
-;.
-; CHECK: [[PROF0]] = !{!"branch_weights", i32 0, i32 -1}
-;.


        


More information about the llvm-branch-commits mailing list