Skip to content

Commit 7acfc83

Browse files
committed
[SimplifyCFG][profcheck] Profile propagation for indirectbr
1 parent 24ab181 commit 7acfc83

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4895,9 +4895,8 @@ bool SimplifyCFGOpt::simplifyTerminatorOnSelect(Instruction *OldTerm,
48954895
// We found both of the successors we were looking for.
48964896
// Create a conditional branch sharing the condition of the select.
48974897
BranchInst *NewBI = Builder.CreateCondBr(Cond, TrueBB, FalseBB);
4898-
if (TrueWeight != FalseWeight)
4899-
setBranchWeights(*NewBI, {TrueWeight, FalseWeight},
4900-
/*IsExpected=*/false, /*ElideAllZero=*/true);
4898+
setBranchWeights(*NewBI, {TrueWeight, FalseWeight},
4899+
/*IsExpected=*/false, /*ElideAllZero=*/true);
49014900
}
49024901
} else if (KeepEdge1 && (KeepEdge2 || TrueBB == FalseBB)) {
49034902
// Neither of the selected blocks were successors, so this
@@ -4982,9 +4981,13 @@ bool SimplifyCFGOpt::simplifyIndirectBrOnSelect(IndirectBrInst *IBI,
49824981
BasicBlock *TrueBB = TBA->getBasicBlock();
49834982
BasicBlock *FalseBB = FBA->getBasicBlock();
49844983

4984+
SmallVector<uint32_t> SelectBranchWeights(2);
4985+
if (!ProfcheckDisableMetadataFixes)
4986+
extractBranchWeights(*SI, SelectBranchWeights);
49854987
// Perform the actual simplification.
4986-
return simplifyTerminatorOnSelect(IBI, SI->getCondition(), TrueBB, FalseBB, 0,
4987-
0);
4988+
return simplifyTerminatorOnSelect(IBI, SI->getCondition(), TrueBB, FalseBB,
4989+
SelectBranchWeights[0],
4990+
SelectBranchWeights[1]);
49884991
}
49894992

49904993
/// This is called when we find an icmp instruction
@@ -7877,20 +7880,25 @@ bool SimplifyCFGOpt::simplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) {
78777880
bool SimplifyCFGOpt::simplifyIndirectBr(IndirectBrInst *IBI) {
78787881
BasicBlock *BB = IBI->getParent();
78797882
bool Changed = false;
7880-
7883+
SmallVector<uint32_t> BranchWeights;
7884+
const bool HasBranchWeights = !ProfcheckDisableMetadataFixes &&
7885+
extractBranchWeights(*IBI, BranchWeights);
7886+
SmallVector<uint32_t> NewBranchWeights;
78817887
// Eliminate redundant destinations.
78827888
SmallPtrSet<Value *, 8> Succs;
78837889
SmallSetVector<BasicBlock *, 8> RemovedSuccs;
7884-
for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
7885-
BasicBlock *Dest = IBI->getDestination(i);
7890+
for (unsigned I = 0, E = IBI->getNumDestinations(); I != E; ++I) {
7891+
BasicBlock *Dest = IBI->getDestination(I);
78867892
if (!Dest->hasAddressTaken() || !Succs.insert(Dest).second) {
78877893
if (!Dest->hasAddressTaken())
78887894
RemovedSuccs.insert(Dest);
78897895
Dest->removePredecessor(BB);
7890-
IBI->removeDestination(i);
7891-
--i;
7892-
--e;
7896+
IBI->removeDestination(I);
7897+
--I;
7898+
--E;
78937899
Changed = true;
7900+
} else if (HasBranchWeights) {
7901+
NewBranchWeights.push_back(BranchWeights[I]);
78947902
}
78957903
}
78967904

@@ -7915,7 +7923,8 @@ bool SimplifyCFGOpt::simplifyIndirectBr(IndirectBrInst *IBI) {
79157923
eraseTerminatorAndDCECond(IBI);
79167924
return true;
79177925
}
7918-
7926+
if (HasBranchWeights)
7927+
setBranchWeights(*IBI, NewBranchWeights, /*IsExpected=*/false);
79197928
if (SelectInst *SI = dyn_cast<SelectInst>(IBI->getAddress())) {
79207929
if (simplifyIndirectBrOnSelect(IBI, SI))
79217930
return requestResimplify();

llvm/test/Transforms/SimplifyCFG/indirectbr.ll

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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 < %s | FileCheck %s
33

44
; SimplifyCFG should eliminate redundant indirectbr edges.
@@ -8,15 +8,19 @@ declare void @A()
88
declare void @B(i32)
99
declare void @C()
1010

11-
define void @indbrtest0(ptr %P, ptr %Q) {
11+
;.
12+
; CHECK: @anchor = constant [13 x ptr] [ptr blockaddress(@indbrtest3, %L1), ptr blockaddress(@indbrtest3, %L2), ptr inttoptr (i32 1 to ptr), ptr blockaddress(@indbrtest4, %L1), ptr inttoptr (i32 1 to ptr), ptr inttoptr (i32 1 to ptr), ptr inttoptr (i32 1 to ptr), ptr inttoptr (i32 1 to ptr), ptr inttoptr (i32 1 to ptr), ptr inttoptr (i32 1 to ptr), ptr inttoptr (i32 1 to ptr), ptr inttoptr (i32 1 to ptr), ptr inttoptr (i32 1 to ptr)]
13+
; CHECK: @xblkx.bbs = internal unnamed_addr constant [9 x ptr] [ptr blockaddress(@indbrtest7, %xlab4x), ptr blockaddress(@indbrtest7, %xlab4x), ptr blockaddress(@indbrtest7, %v2j), ptr blockaddress(@indbrtest7, %xlab4x), ptr blockaddress(@indbrtest7, %xlab4x), ptr blockaddress(@indbrtest7, %xlab4x), ptr blockaddress(@indbrtest7, %xlab4x), ptr blockaddress(@indbrtest7, %xlab4x), ptr blockaddress(@indbrtest7, %v2j)]
14+
;.
15+
define void @indbrtest0(ptr %P, ptr %Q) !prof !0 {
1216
; CHECK-LABEL: @indbrtest0(
1317
; CHECK-NEXT: entry:
1418
; CHECK-NEXT: store ptr blockaddress(@indbrtest0, [[BB0:%.*]]), ptr [[P:%.*]], align 8
1519
; CHECK-NEXT: store ptr blockaddress(@indbrtest0, [[BB1:%.*]]), ptr [[P]], align 8
1620
; CHECK-NEXT: store ptr blockaddress(@indbrtest0, [[BB2:%.*]]), ptr [[P]], align 8
1721
; CHECK-NEXT: call void @foo()
1822
; CHECK-NEXT: [[T:%.*]] = load ptr, ptr [[Q:%.*]], align 8
19-
; CHECK-NEXT: indirectbr ptr [[T]], [label [[BB0]], label [[BB1]], label %BB2]
23+
; CHECK-NEXT: indirectbr ptr [[T]], [label [[BB0]], label [[BB1]], label %BB2], !prof [[PROF1:![0-9]+]]
2024
; CHECK: BB0:
2125
; CHECK-NEXT: call void @A()
2226
; CHECK-NEXT: br label [[BB1]]
@@ -36,7 +40,7 @@ entry:
3640
store ptr blockaddress(@indbrtest0, %BB2), ptr %P
3741
call void @foo()
3842
%t = load ptr, ptr %Q
39-
indirectbr ptr %t, [label %BB0, label %BB1, label %BB2, label %BB0, label %BB1, label %BB2]
43+
indirectbr ptr %t, [label %BB0, label %BB1, label %BB2, label %BB0, label %BB1, label %BB2], !prof !1
4044
BB0:
4145
call void @A()
4246
br label %BB1
@@ -103,10 +107,10 @@ BB0:
103107
; SimplifyCFG should turn the indirectbr into a conditional branch on the
104108
; condition of the select.
105109

106-
define void @indbrtest3(i1 %cond, ptr %address) nounwind {
110+
define void @indbrtest3(i1 %cond, ptr %address) nounwind !prof !0 {
107111
; CHECK-LABEL: @indbrtest3(
108112
; CHECK-NEXT: entry:
109-
; CHECK-NEXT: br i1 [[COND:%.*]], label [[L1:%.*]], label [[L2:%.*]]
113+
; CHECK-NEXT: br i1 [[COND:%.*]], label [[L1:%.*]], label [[L2:%.*]], !prof [[PROF2:![0-9]+]]
110114
; CHECK: common.ret:
111115
; CHECK-NEXT: ret void
112116
; CHECK: L1:
@@ -117,8 +121,8 @@ define void @indbrtest3(i1 %cond, ptr %address) nounwind {
117121
; CHECK-NEXT: br label [[COMMON_RET]]
118122
;
119123
entry:
120-
%indirect.goto.dest = select i1 %cond, ptr blockaddress(@indbrtest3, %L1), ptr blockaddress(@indbrtest3, %L2)
121-
indirectbr ptr %indirect.goto.dest, [label %L1, label %L2, label %L3]
124+
%indirect.goto.dest = select i1 %cond, ptr blockaddress(@indbrtest3, %L1), ptr blockaddress(@indbrtest3, %L2), !prof !2
125+
indirectbr ptr %indirect.goto.dest, [label %L1, label %L2, label %L3], !prof !3
122126

123127
L1:
124128
call void @A()
@@ -385,3 +389,15 @@ declare i32 @xfunc5x()
385389
declare i8 @xfunc7x()
386390
declare i32 @xselectorx()
387391
declare i32 @xactionx()
392+
393+
!0 = !{!"function_entry_count", i32 10}
394+
!1 = !{!"branch_weights", i32 3, i32 5, i32 7, i32 11, i32 13, i32 17}
395+
!2 = !{!"branch_weights", i32 3, i32 5}
396+
!3 = !{!"branch_weights", i32 3, i32 5, i32 7}
397+
;.
398+
; CHECK: attributes #[[ATTR0:[0-9]+]] = { nounwind }
399+
;.
400+
; CHECK: [[META0:![0-9]+]] = !{!"function_entry_count", i32 10}
401+
; CHECK: [[PROF1]] = !{!"branch_weights", i32 3, i32 5, i32 7}
402+
; CHECK: [[PROF2]] = !{!"branch_weights", i32 3, i32 5}
403+
;.

0 commit comments

Comments
 (0)