[llvm-branch-commits] [llvm] 60ae70b - Revert "Revert "[SimplifyCFG] Mark branch weights unknown in masked switch to…"

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Oct 20 21:07:27 PDT 2026


Author: Aiden Grossman
Date: 2026-10-20T21:07:22-07:00
New Revision: 60ae70b0cdf57a23f93b396f0b97357cf11f9b87

URL: https://github.com/llvm/llvm-project/commit/60ae70b0cdf57a23f93b396f0b97357cf11f9b87
DIFF: https://github.com/llvm/llvm-project/commit/60ae70b0cdf57a23f93b396f0b97357cf11f9b87.diff

LOG: Revert "Revert "[SimplifyCFG] Mark branch weights unknown in masked switch to…"

This reverts commit 63ad0005f5eaac3e58289c05111ce5e5a20592b8.

Added: 
    llvm/test/Transforms/SimplifyCFG/X86/switch-to-lookup-masked-profile.ll

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index ff76a7f2264a8..7f2c9afaccf4c 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -7807,7 +7807,9 @@ static bool simplifySwitchLookup(SwitchInst *SI, IRBuilder<> &Builder,
 
   Builder.SetInsertPoint(SI);
   const bool GeneratingCoveredLookupTable = (MaxTableSize == TableSize);
-  if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
+  const bool IsFullyCoveredLookupTable =
+      !DefaultIsReachable || GeneratingCoveredLookupTable;
+  if (IsFullyCoveredLookupTable) {
     Builder.CreateBr(LookupBB);
     if (DTU)
       Updates.push_back({DominatorTree::Insert, BB, LookupBB});
@@ -7857,6 +7859,8 @@ static bool simplifySwitchLookup(SwitchInst *SI, IRBuilder<> &Builder,
     Value *Shifted = Builder.CreateLShr(TableMask, MaskIndex, "switch.shifted");
     Value *LoBit = Builder.CreateTrunc(
         Shifted, Type::getInt1Ty(Mod.getContext()), "switch.lobit");
+    if (!IsFullyCoveredLookupTable)
+      setExplicitlyUnknownBranchWeightsIfProfiled(*CondBranch, DEBUG_TYPE);
     CondBranch = Builder.CreateCondBr(LoBit, LookupBB, SI->getDefaultDest());
     if (DTU) {
       Updates.push_back({DominatorTree::Insert, MaskBB, LookupBB});
@@ -7920,7 +7924,14 @@ static bool simplifySwitchLookup(SwitchInst *SI, IRBuilder<> &Builder,
       ToLookupWeight += BranchWeights[I];
   }
   SI->eraseFromParent();
-  if (HasBranchWeights)
+  // In the case where we do not have a fully covered lookup table (i.e., we
+  // need a range check), and we need a mask check, we split the default case
+  // into two 
diff erent cases for which we cannot infer branch weights.
+  // Otherwise we have a single branch for which we can infer branch weights.
+  if ((NeedMask && !IsFullyCoveredLookupTable) &&
+      !ProfcheckDisableMetadataFixes)
+    setExplicitlyUnknownBranchWeightsIfProfiled(*CondBranch, DEBUG_TYPE);
+  else if (HasBranchWeights)
     setFittedBranchWeights(*CondBranch, {ToLookupWeight, ToDefaultWeight},
                            /*IsExpected=*/false);
   if (DTU)

diff  --git a/llvm/test/Transforms/SimplifyCFG/X86/switch-to-lookup-masked-profile.ll b/llvm/test/Transforms/SimplifyCFG/X86/switch-to-lookup-masked-profile.ll
new file mode 100644
index 0000000000000..93a85cbc5170f
--- /dev/null
+++ b/llvm/test/Transforms/SimplifyCFG/X86/switch-to-lookup-masked-profile.ll
@@ -0,0 +1,48 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes="simplifycfg<switch-to-lookup>" -S | FileCheck %s
+
+target triple = "x86_64-unknown-linux-gnu"
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: write)
+define i16 @four_out_of_fifteen(ptr %this, i8 %0)  !prof !0 {
+; CHECK-LABEL: define i16 @four_out_of_fifteen(
+; CHECK-SAME: ptr [[THIS:%.*]], i8 [[TMP0:%.*]]) !prof [[PROF0:![0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[SWITCH_TABLEIDX:%.*]] = sub i8 [[TMP0]], 57
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp ult i8 [[SWITCH_TABLEIDX]], 11
+; CHECK-NEXT:    br i1 [[TMP1]], label %[[SWITCH_HOLE_CHECK:.*]], label %[[SW_EPILOG66:.*]], !prof [[PROF1:![0-9]+]]
+; CHECK:       [[SWITCH_HOLE_CHECK]]:
+; CHECK-NEXT:    [[SWITCH_MASKINDEX:%.*]] = zext i8 [[SWITCH_TABLEIDX]] to i16
+; CHECK-NEXT:    [[SWITCH_SHIFTED:%.*]] = lshr i16 1793, [[SWITCH_MASKINDEX]]
+; CHECK-NEXT:    [[SWITCH_LOBIT:%.*]] = trunc i16 [[SWITCH_SHIFTED]] to i1
+; CHECK-NEXT:    br i1 [[SWITCH_LOBIT]], label %[[COMMON_RET:.*]], label %[[SW_EPILOG66]], !prof [[PROF1]]
+; CHECK:       [[COMMON_RET]]:
+; CHECK-NEXT:    [[COMMON_RET_OP:%.*]] = phi i16 [ 0, %[[SW_EPILOG66]] ], [ 1, %[[SWITCH_HOLE_CHECK]] ]
+; CHECK-NEXT:    ret i16 [[COMMON_RET_OP]]
+; CHECK:       [[SW_EPILOG66]]:
+; CHECK-NEXT:    store i8 0, ptr [[THIS]], align 8
+; CHECK-NEXT:    br label %[[COMMON_RET]]
+;
+entry:
+  switch i8 %0, label %sw.epilog66 [
+  i8 57, label %common.ret
+  i8 65, label %common.ret
+  i8 66, label %common.ret
+  i8 67, label %common.ret
+  ], !prof !1
+
+common.ret:
+  %common.ret.op = phi i16 [ 0, %sw.epilog66 ], [ 1, %entry ], [ 1, %entry ], [ 1, %entry ], [ 1, %entry ]
+  ret i16 %common.ret.op
+
+sw.epilog66:
+  store i8 0, ptr %this, align 8
+  br label %common.ret
+}
+
+!0 = !{!"function_entry_count", i64 62163}
+!1 = !{!"branch_weights", i32 19796, i32 1297434962, i32 18771, i32 4410451, i32 4}
+;.
+; CHECK: [[PROF0]] = !{!"function_entry_count", i64 62163}
+; CHECK: [[PROF1]] = !{!"unknown", !"simplifycfg"}
+;.


        


More information about the llvm-branch-commits mailing list