Skip to content

Commit 4becda1

Browse files
committed
[SimplifyCFG] Prevent merging cbranch to cbranch if the branch probabililty from the first to second is too low.
1 parent fc1457d commit 4becda1

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ static cl::opt<unsigned> MaxSwitchCasesPerResult(
180180
"max-switch-cases-per-result", cl::Hidden, cl::init(16),
181181
cl::desc("Limit cases to analyze when converting a switch to select"));
182182

183+
static cl::opt<unsigned> CondBranchToCondBranchWeightRatio(
184+
"simplifycfg-cbranch-to-cbranch-weight-ratio", cl::Hidden, cl::init(10000),
185+
cl::desc("Don't merge conditional branches if the branch probability from "
186+
"the first to second is below of the reciprocal of this value"));
187+
183188
STATISTIC(NumBitMaps, "Number of switch instructions turned into bitmaps");
184189
STATISTIC(NumLinearMaps,
185190
"Number of switch instructions turned into linear mapping");
@@ -4347,6 +4352,16 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
43474352
if (PBI->getSuccessor(PBIOp) == BB)
43484353
return false;
43494354

4355+
// If predecessor's branch probability to BB is too low don't merge branches.
4356+
SmallVector<uint32_t, 2> PredWeights;
4357+
if (extractBranchWeights(*PBI, PredWeights)) {
4358+
auto BIWeight = PredWeights[PBIOp ^ 1];
4359+
auto CommonWeight = PredWeights[PBIOp];
4360+
if (BIWeight &&
4361+
(CommonWeight / BIWeight > CondBranchToCondBranchWeightRatio))
4362+
return false;
4363+
}
4364+
43504365
// Do not perform this transformation if it would require
43514366
// insertion of a large number of select instructions. For targets
43524367
// without predication/cmovs, this is a big pessimization.

llvm/test/Transforms/SimplifyCFG/branch-cond-dont-merge.ll

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ define void @dont_merge_cbranches1(i32 %V) {
88
; CHECK-LABEL: @dont_merge_cbranches1(
99
; CHECK-NEXT: [[DIVERGENT_COND:%.*]] = icmp ne i32 [[V:%.*]], 0
1010
; CHECK-NEXT: [[UNIFORM_COND:%.*]] = call i1 @uniform_result(i1 [[DIVERGENT_COND]])
11-
; CHECK-NEXT: [[UNIFORM_COND_NOT:%.*]] = xor i1 [[UNIFORM_COND]], true
12-
; CHECK-NEXT: [[DIVERGENT_COND_NOT:%.*]] = xor i1 [[DIVERGENT_COND]], true
13-
; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[UNIFORM_COND_NOT]], i1 true, i1 [[DIVERGENT_COND_NOT]]
14-
; CHECK-NEXT: br i1 [[BRMERGE]], label [[EXIT:%.*]], label [[BB3:%.*]], !prof [[PROF0:![0-9]+]]
11+
; CHECK-NEXT: br i1 [[UNIFORM_COND]], label [[BB2:%.*]], label [[EXIT:%.*]], !prof [[PROF0:![0-9]+]]
12+
; CHECK: bb2:
13+
; CHECK-NEXT: br i1 [[DIVERGENT_COND]], label [[BB3:%.*]], label [[EXIT]]
1514
; CHECK: bb3:
1615
; CHECK-NEXT: call void @bar()
1716
; CHECK-NEXT: br label [[EXIT]]
@@ -34,9 +33,9 @@ define void @dont_merge_cbranches2(i32 %V) {
3433
; CHECK-LABEL: @dont_merge_cbranches2(
3534
; CHECK-NEXT: [[DIVERGENT_COND:%.*]] = icmp ne i32 [[V:%.*]], 0
3635
; CHECK-NEXT: [[UNIFORM_COND:%.*]] = call i1 @uniform_result(i1 [[DIVERGENT_COND]])
37-
; CHECK-NEXT: [[DIVERGENT_COND_NOT:%.*]] = xor i1 [[DIVERGENT_COND]], true
38-
; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[UNIFORM_COND]], i1 true, i1 [[DIVERGENT_COND_NOT]]
39-
; CHECK-NEXT: br i1 [[BRMERGE]], label [[EXIT:%.*]], label [[BB3:%.*]], !prof [[PROF0]]
36+
; CHECK-NEXT: br i1 [[UNIFORM_COND]], label [[EXIT:%.*]], label [[BB2:%.*]], !prof [[PROF1:![0-9]+]]
37+
; CHECK: bb2:
38+
; CHECK-NEXT: br i1 [[DIVERGENT_COND]], label [[BB3:%.*]], label [[EXIT]]
4039
; CHECK: bb3:
4140
; CHECK-NEXT: call void @bar()
4241
; CHECK-NEXT: br label [[EXIT]]

0 commit comments

Comments
 (0)