Skip to content

Commit d1ddd89

Browse files
committed
[SimplifyCFG][profcheck] Handle branch weights in simplifySwitchLookup
1 parent e49a6f0 commit d1ddd89

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7227,6 +7227,7 @@ static bool simplifySwitchLookup(SwitchInst *SI, IRBuilder<> &Builder,
72277227
Mod.getContext(), "switch.lookup", CommonDest->getParent(), CommonDest);
72287228

72297229
BranchInst *RangeCheckBranch = nullptr;
7230+
BranchInst *CondBranch = nullptr;
72307231

72317232
Builder.SetInsertPoint(SI);
72327233
const bool GeneratingCoveredLookupTable = (MaxTableSize == TableSize);
@@ -7241,6 +7242,7 @@ static bool simplifySwitchLookup(SwitchInst *SI, IRBuilder<> &Builder,
72417242
TableIndex, ConstantInt::get(MinCaseVal->getType(), TableSize));
72427243
RangeCheckBranch =
72437244
Builder.CreateCondBr(Cmp, LookupBB, SI->getDefaultDest());
7245+
CondBranch = RangeCheckBranch;
72447246
if (DTU)
72457247
Updates.push_back({DominatorTree::Insert, BB, LookupBB});
72467248
}
@@ -7279,7 +7281,7 @@ static bool simplifySwitchLookup(SwitchInst *SI, IRBuilder<> &Builder,
72797281
Value *Shifted = Builder.CreateLShr(TableMask, MaskIndex, "switch.shifted");
72807282
Value *LoBit = Builder.CreateTrunc(
72817283
Shifted, Type::getInt1Ty(Mod.getContext()), "switch.lobit");
7282-
Builder.CreateCondBr(LoBit, LookupBB, SI->getDefaultDest());
7284+
CondBranch = Builder.CreateCondBr(LoBit, LookupBB, SI->getDefaultDest());
72837285
if (DTU) {
72847286
Updates.push_back({DominatorTree::Insert, MaskBB, LookupBB});
72857287
Updates.push_back({DominatorTree::Insert, MaskBB, SI->getDefaultDest()});
@@ -7319,19 +7321,32 @@ static bool simplifySwitchLookup(SwitchInst *SI, IRBuilder<> &Builder,
73197321
if (DTU)
73207322
Updates.push_back({DominatorTree::Insert, LookupBB, CommonDest});
73217323

7324+
SmallVector<uint32_t> BranchWeights;
7325+
const bool HasBranchWeights = CondBranch && !ProfcheckDisableMetadataFixes &&
7326+
extractBranchWeights(*SI, BranchWeights);
7327+
uint64_t ToLookupWeight = 0;
7328+
uint64_t ToDefaultWeight = 0;
7329+
73227330
// Remove the switch.
73237331
SmallPtrSet<BasicBlock *, 8> RemovedSuccessors;
7324-
for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) {
7325-
BasicBlock *Succ = SI->getSuccessor(i);
7332+
for (unsigned I = 0, E = SI->getNumSuccessors(); I < E; ++I) {
7333+
BasicBlock *Succ = SI->getSuccessor(I);
73267334

7327-
if (Succ == SI->getDefaultDest())
7335+
if (Succ == SI->getDefaultDest()) {
7336+
if (HasBranchWeights)
7337+
ToDefaultWeight += BranchWeights[I];
73287338
continue;
7339+
}
73297340
Succ->removePredecessor(BB);
73307341
if (DTU && RemovedSuccessors.insert(Succ).second)
73317342
Updates.push_back({DominatorTree::Delete, BB, Succ});
7343+
if (HasBranchWeights)
7344+
ToLookupWeight += BranchWeights[I];
73327345
}
73337346
SI->eraseFromParent();
7334-
7347+
if (HasBranchWeights)
7348+
setFittedBranchWeights(*CondBranch, {ToLookupWeight, ToDefaultWeight},
7349+
/*IsExpected=*/false);
73357350
if (DTU)
73367351
DTU->applyUpdates(Updates);
73377352

llvm/test/Transforms/SimplifyCFG/X86/switch_to_lookup_table.ll

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,14 +1565,14 @@ end:
15651565
; lookup (since i3 can only hold values in the range of explicit
15661566
; values) and simultaneously trying to generate a branch to deal with
15671567
; the fact that we have holes in the range.
1568-
define i32 @covered_switch_with_bit_tests(i3) {
1568+
define i32 @covered_switch_with_bit_tests(i3) !prof !0 {
15691569
; CHECK-LABEL: @covered_switch_with_bit_tests(
15701570
; CHECK-NEXT: entry:
15711571
; CHECK-NEXT: [[SWITCH_TABLEIDX:%.*]] = sub i3 [[TMP0:%.*]], -4
15721572
; CHECK-NEXT: [[SWITCH_MASKINDEX:%.*]] = zext i3 [[SWITCH_TABLEIDX]] to i8
15731573
; CHECK-NEXT: [[SWITCH_SHIFTED:%.*]] = lshr i8 -61, [[SWITCH_MASKINDEX]]
15741574
; CHECK-NEXT: [[SWITCH_LOBIT:%.*]] = trunc i8 [[SWITCH_SHIFTED]] to i1
1575-
; CHECK-NEXT: br i1 [[SWITCH_LOBIT]], label [[SWITCH_LOOKUP:%.*]], label [[L6:%.*]]
1575+
; CHECK-NEXT: br i1 [[SWITCH_LOBIT]], label [[SWITCH_LOOKUP:%.*]], label [[L6:%.*]], !prof [[PROF1:![0-9]+]]
15761576
; CHECK: switch.lookup:
15771577
; CHECK-NEXT: [[TMP1:%.*]] = zext i3 [[SWITCH_TABLEIDX]] to i64
15781578
; CHECK-NEXT: [[SWITCH_GEP:%.*]] = getelementptr inbounds [8 x i32], ptr @switch.table.covered_switch_with_bit_tests, i64 0, i64 [[TMP1]]
@@ -1588,7 +1588,7 @@ entry:
15881588
i3 -4, label %l5
15891589
i3 3, label %l1
15901590
i3 2, label %l1
1591-
]
1591+
], !prof !1
15921592

15931593
l1: br label %l2
15941594

@@ -2425,3 +2425,10 @@ return:
24252425
%res = phi i1 [ 0, %bb0 ], [ 1, %bb1 ]
24262426
ret i1 %res
24272427
}
2428+
2429+
!0 = !{!"function_entry_count", i32 10}
2430+
!1 = !{!"branch_weights", i32 3, i32 5, i32 7, i32 11, i32 13}
2431+
;.
2432+
; CHECK: [[META0:![0-9]+]] = !{!"function_entry_count", i32 10}
2433+
; CHECK: [[PROF1]] = !{!"branch_weights", i32 36, i32 3}
2434+
;.

llvm/test/Transforms/SimplifyCFG/rangereduce.ll

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
22
; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -switch-to-lookup -S | FileCheck %s
33
; RUN: opt < %s -passes='simplifycfg<switch-to-lookup>' -S | FileCheck %s
44

55
target datalayout = "e-n32"
66

7-
define i32 @test1(i32 %a) {
7+
;.
8+
; CHECK: @switch.table.test1 = private unnamed_addr constant [4 x i32] [i32 11984, i32 1143, i32 99783, i32 99783], align 4
9+
; CHECK: @switch.table.test3 = private unnamed_addr constant [3 x i32] [i32 11984, i32 1143, i32 99783], align 4
10+
; CHECK: @switch.table.test6 = private unnamed_addr constant [4 x i32] [i32 99783, i32 99783, i32 1143, i32 11984], align 4
11+
; CHECK: @switch.table.test8 = private unnamed_addr constant [5 x i32] [i32 11984, i32 1143, i32 99783, i32 8867, i32 99783], align 4
12+
; CHECK: @switch.table.test9 = private unnamed_addr constant [8 x i32] [i32 99783, i32 8867, i32 99783, i32 8867, i32 8867, i32 8867, i32 11984, i32 1143], align 4
13+
;.
14+
define i32 @test1(i32 %a) !prof !0 {
815
; CHECK-LABEL: @test1(
916
; CHECK-NEXT: [[TMP1:%.*]] = sub i32 [[A:%.*]], 97
1017
; CHECK-NEXT: [[TMP2:%.*]] = call i32 @llvm.fshl.i32(i32 [[TMP1]], i32 [[TMP1]], i32 30)
1118
; CHECK-NEXT: [[TMP3:%.*]] = icmp ult i32 [[TMP2]], 4
12-
; CHECK-NEXT: br i1 [[TMP3]], label [[SWITCH_LOOKUP:%.*]], label [[COMMON_RET:%.*]]
19+
; CHECK-NEXT: br i1 [[TMP3]], label [[SWITCH_LOOKUP:%.*]], label [[COMMON_RET:%.*]], !prof [[PROF1:![0-9]+]]
1320
; CHECK: switch.lookup:
1421
; CHECK-NEXT: [[TMP4:%.*]] = zext nneg i32 [[TMP2]] to i64
1522
; CHECK-NEXT: [[SWITCH_GEP:%.*]] = getelementptr inbounds [4 x i32], ptr @switch.table.test1, i64 0, i64 [[TMP4]]
@@ -24,7 +31,7 @@ define i32 @test1(i32 %a) {
2431
i32 101, label %two
2532
i32 105, label %three
2633
i32 109, label %three
27-
]
34+
], !prof !1
2835

2936
def:
3037
ret i32 8867
@@ -310,3 +317,12 @@ three:
310317
ret i32 99783
311318
}
312319

320+
!0 = !{!"function_entry_count", i32 100}
321+
!1 = !{!"branch_weights", i32 5, i32 7, i32 11, i32 13, i32 17}
322+
;.
323+
; CHECK: attributes #[[ATTR0:[0-9]+]] = { optsize }
324+
; CHECK: attributes #[[ATTR1:[0-9]+]] = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
325+
;.
326+
; CHECK: [[META0:![0-9]+]] = !{!"function_entry_count", i32 100}
327+
; CHECK: [[PROF1]] = !{!"branch_weights", i32 48, i32 5}
328+
;.

0 commit comments

Comments
 (0)