Skip to content

Commit fe28635

Browse files
authored
[JT] tryToUnfoldSelectInCurrBB should update BFI & BPI if present (#188097)
Issue #187545
1 parent e3c6244 commit fe28635

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

llvm/lib/Transforms/Scalar/JumpThreading.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ static cl::opt<bool> ThreadAcrossLoopHeaders(
106106
cl::desc("Allow JumpThreading to thread across loop headers, for testing"),
107107
cl::init(false), cl::Hidden);
108108

109+
namespace llvm {
110+
extern cl::opt<bool> ProfcheckDisableMetadataFixes;
111+
}
112+
109113
JumpThreadingPass::JumpThreadingPass(int T) {
110114
DefaultBBDupThreshold = (T == -1) ? BBDuplicateThreshold : unsigned(T);
111115
}
@@ -2993,6 +2997,33 @@ bool JumpThreadingPass::tryToUnfoldSelectInCurrBB(BasicBlock *BB) {
29932997
NewPN->addIncoming(SI->getFalseValue(), BB);
29942998
NewPN->setDebugLoc(SI->getDebugLoc());
29952999
SI->replaceAllUsesWith(NewPN);
3000+
3001+
auto *BPI = getBPI();
3002+
auto *BFI = getBFI();
3003+
if (!ProfcheckDisableMetadataFixes && BranchWeights) {
3004+
SmallVector<uint32_t, 2> BW;
3005+
[[maybe_unused]] bool Extracted = extractBranchWeights(BranchWeights, BW);
3006+
assert(Extracted);
3007+
uint64_t Denominator =
3008+
sum_of(llvm::map_range(BW, StaticCastTo<uint64_t>));
3009+
assert(Denominator > 0 &&
3010+
"At least one of the branch probabilities should be non-zero");
3011+
BranchProbability TrueProb =
3012+
BranchProbability::getBranchProbability(BW[0], Denominator);
3013+
BranchProbability FalseProb =
3014+
BranchProbability::getBranchProbability(BW[1], Denominator);
3015+
SmallVector<BranchProbability, 2> BP = {TrueProb, FalseProb};
3016+
3017+
if (BPI)
3018+
BPI->setEdgeProbability(BB, BP);
3019+
3020+
if (BFI) {
3021+
auto BBOrigFreq = BFI->getBlockFreq(BB);
3022+
auto NewBBFreq = BBOrigFreq * TrueProb;
3023+
BFI->setBlockFreq(NewBB, NewBBFreq);
3024+
BFI->setBlockFreq(SplitBB, BBOrigFreq);
3025+
}
3026+
}
29963027
SI->eraseFromParent();
29973028
// NewBB and SplitBB are newly created blocks which require insertion.
29983029
std::vector<DominatorTree::UpdateType> Updates;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
; RUN: opt -passes="require<branch-prob>,require<block-freq>,jump-threading" -S < %s | FileCheck %s
2+
3+
@d = global i8 0, align 1
4+
@c = global i32 0, align 4
5+
@b = global i1 false, align 2
6+
@f = global i32 0, align 4
7+
@e = global i32 0, align 4
8+
@a = global i32 0, align 4
9+
10+
; CHECK-LABEL: @test
11+
define i32 @test() !prof !0 {
12+
bb:
13+
%i = load i8, ptr @d, align 1
14+
%i1 = add i8 %i, 1
15+
store i8 %i1, ptr @d, align 1
16+
%i2 = icmp eq i8 %i1, 0
17+
br i1 %i2, label %bb4, label %bb15, !prof !1
18+
19+
bb4:
20+
%i5 = load i1, ptr @b, align 2
21+
%i6 = select i1 %i5, i32 0, i32 2
22+
%i7 = load i32, ptr @c, align 4
23+
%i8 = xor i32 %i7, %i6
24+
store i32 %i8, ptr @c, align 4
25+
%i9 = icmp eq i32 %i7, %i6
26+
br i1 %i9, label %bb13, label %bb10, !prof !1
27+
28+
bb10:
29+
%i11 = load i32, ptr @f, align 4
30+
%i12 = or i32 %i11, 9
31+
store i32 %i12, ptr @f, align 4
32+
br label %bb18
33+
34+
bb13:
35+
store i1 true, ptr @b, align 2
36+
br label %bb18
37+
38+
bb15:
39+
store i32 0, ptr @c, align 4
40+
%i16 = load i32, ptr @e, align 4
41+
%i17 = or i32 %i16, 9
42+
store i32 %i17, ptr @e, align 4
43+
%i19.pre = load i1, ptr @b, align 2
44+
br label %bb18
45+
46+
bb18:
47+
%i19 = phi i1 [ %i5, %bb10 ], [ true, %bb13 ], [ %i19.pre, %bb15 ]
48+
%i20 = select i1 %i19, i32 0, i32 2, !prof !2
49+
store i32 %i20, ptr @a, align 4
50+
ret i32 0
51+
}
52+
53+
!0 = !{!"function_entry_count", i64 10}
54+
!1 = !{!"branch_weights", i32 5, i32 5}
55+
!2 = !{!"branch_weights", i32 0, i32 10}
56+
57+
; CHECK: br i1 %cond.fr{{.*}}, label %{{.*}}, label %{{.*}}, !prof ![[MD:[0-9]+]]
58+
; CHECK: ![[MD]] = !{!"branch_weights", i32 0, i32 -2147483648}

0 commit comments

Comments
 (0)