Skip to content

Commit 0df0906

Browse files
authored
[JumpThreading] Use [BB->SuccIndx] to get probability when updating BB info. (#134585)
In case the same src BB targets to the same dest BB in different conditions/edges, such as switch-cases, we should use prob[SrcBB->SuccIndx] instead of prob[SrcBB->DstBB] to get probability.
1 parent e7365d3 commit 0df0906

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

llvm/lib/Transforms/Scalar/JumpThreading.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2546,17 +2546,16 @@ void JumpThreadingPass::updateBlockFreqAndEdgeWeight(BasicBlock *PredBB,
25462546
// frequency of BB.
25472547
auto BBOrigFreq = BFI->getBlockFreq(BB);
25482548
auto NewBBFreq = BFI->getBlockFreq(NewBB);
2549-
auto BB2SuccBBFreq = BBOrigFreq * BPI->getEdgeProbability(BB, SuccBB);
25502549
auto BBNewFreq = BBOrigFreq - NewBBFreq;
25512550
BFI->setBlockFreq(BB, BBNewFreq);
25522551

25532552
// Collect updated outgoing edges' frequencies from BB and use them to update
25542553
// edge probabilities.
25552554
SmallVector<uint64_t, 4> BBSuccFreq;
2556-
for (BasicBlock *Succ : successors(BB)) {
2557-
auto SuccFreq = (Succ == SuccBB)
2558-
? BB2SuccBBFreq - NewBBFreq
2559-
: BBOrigFreq * BPI->getEdgeProbability(BB, Succ);
2555+
for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
2556+
auto BB2SuccBBFreq =
2557+
BBOrigFreq * BPI->getEdgeProbability(BB, I.getSuccessorIndex());
2558+
auto SuccFreq = (*I == SuccBB) ? BB2SuccBBFreq - NewBBFreq : BB2SuccBBFreq;
25602559
BBSuccFreq.push_back(SuccFreq.getFrequency());
25612560
}
25622561

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
; RUN: opt -debug-only=branch-prob -passes=jump-threading -S %s 2>&1 | FileCheck %s
2+
; REQUIRES: asserts
3+
4+
; Make sure that edges' probabilities would not accumulate if they are
5+
; the same target BB.
6+
; Edge L0 -> 2 and L0 -> 3 's targets are both L2, but their respective
7+
; probability should not be L0 -> L2, because prob[L0->L2] equls to
8+
; prob[L0->2] + prob[L0->3]
9+
10+
; CHECK: Computing probabilities for entry
11+
; CHECK: eraseBlock L0
12+
; CHECK-NOT: set edge L0 -> 0 successor probability to 0x12492492 / 0x80000000 = 14.29%
13+
; CHECK-NOT: set edge L0 -> 1 successor probability to 0x24924925 / 0x80000000 = 28.57%
14+
; CHECK-NOT: set edge L0 -> 2 successor probability to 0x24924925 / 0x80000000 = 28.57%
15+
; CHECK-NOT: set edge L0 -> 3 successor probability to 0x24924925 / 0x80000000 = 28.57%
16+
; CHECK: set edge L0 -> 0 successor probability to 0x1999999a / 0x80000000 = 20.00%
17+
; CHECK: set edge L0 -> 1 successor probability to 0x33333333 / 0x80000000 = 40.00%
18+
; CHECK: set edge L0 -> 2 successor probability to 0x1999999a / 0x80000000 = 20.00%
19+
; CHECK: set edge L0 -> 3 successor probability to 0x1999999a / 0x80000000 = 20.00%
20+
; CHECK-NOT: !0 = !{!"branch_weights", i32 306783378, i32 613566757, i32 613566757, i32 613566757}
21+
; CHECK: !0 = !{!"branch_weights", i32 429496730, i32 858993459, i32 429496730, i32 429496730}
22+
define void @test_switch(i1 %cond, i8 %value) nounwind {
23+
entry:
24+
br i1 %cond, label %L0, label %L4
25+
L0:
26+
%expr = select i1 %cond, i8 1, i8 %value
27+
switch i8 %expr, label %L3 [
28+
i8 1, label %L1
29+
i8 2, label %L2
30+
i8 3, label %L2
31+
], !prof !0
32+
33+
L1:
34+
ret void
35+
L2:
36+
ret void
37+
L3:
38+
ret void
39+
L4:
40+
br label %L0
41+
}
42+
!0 = !{!"branch_weights", i32 1, i32 7, i32 1, i32 1}

0 commit comments

Comments
 (0)