Skip to content

Commit af93acd

Browse files
committed
[SimplfyCFG] Set MD_prof for select used for certain conditional simplifications
1 parent 2c11a83 commit af93acd

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ static cl::opt<unsigned> MaxJumpThreadingLiveBlocks(
203203
cl::desc("Limit number of blocks a define in a threaded block is allowed "
204204
"to be live in"));
205205

206+
extern cl::opt<bool> ProfcheckDisableMetadataFixes;
207+
206208
STATISTIC(NumBitMaps, "Number of switch instructions turned into bitmaps");
207209
STATISTIC(NumLinearMaps,
208210
"Number of switch instructions turned into linear mapping");
@@ -4772,6 +4774,14 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
47724774
fitWeights(NewWeights);
47734775

47744776
setBranchWeights(PBI, NewWeights[0], NewWeights[1], /*IsExpected=*/false);
4777+
// Cond may be a select instruction with the first operand set to "true".
4778+
if (!ProfcheckDisableMetadataFixes)
4779+
if (auto *SI = dyn_cast<SelectInst>(Cond)) {
4780+
assert(isa<ConstantInt>(SI->getTrueValue()) &&
4781+
(dyn_cast<ConstantInt>(SI->getTrueValue())->isOne()));
4782+
setBranchWeights(SI, NewWeights[0], NewWeights[1],
4783+
/*IsExpected=*/false);
4784+
}
47754785
}
47764786

47774787
// OtherDest may have phi nodes. If so, add an entry from PBI's

llvm/test/Transforms/SimplifyCFG/branch-fold.ll

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
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
define void @test(ptr %P, ptr %Q, i1 %A, i1 %B) {
55
; CHECK-LABEL: @test(
66
; CHECK-NEXT: entry:
77
; CHECK-NEXT: [[A_NOT:%.*]] = xor i1 [[A:%.*]], true
8-
; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[A_NOT]], i1 true, i1 [[B:%.*]]
9-
; CHECK-NEXT: br i1 [[BRMERGE]], label [[B:%.*]], label [[COMMON_RET:%.*]]
8+
; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[A_NOT]], i1 true, i1 [[B:%.*]], !prof [[PROF0:![0-9]+]]
9+
; CHECK-NEXT: br i1 [[BRMERGE]], label [[B:%.*]], label [[COMMON_RET:%.*]], !prof [[PROF0]]
1010
; CHECK: common.ret:
1111
; CHECK-NEXT: ret void
1212
; CHECK: b:
@@ -15,9 +15,9 @@ define void @test(ptr %P, ptr %Q, i1 %A, i1 %B) {
1515
;
1616

1717
entry:
18-
br i1 %A, label %a, label %b
18+
br i1 %A, label %a, label %b, !prof !0
1919
a:
20-
br i1 %B, label %b, label %c
20+
br i1 %B, label %b, label %c, !prof !1
2121
b:
2222
store i32 123, ptr %P
2323
ret void
@@ -146,3 +146,11 @@ Succ:
146146
}
147147

148148
declare void @dummy()
149+
150+
!0 = !{!"branch_weights", i32 3, i32 7}
151+
!1 = !{!"branch_weights", i32 11, i32 4}
152+
;.
153+
; CHECK: attributes #[[ATTR0:[0-9]+]] = { nounwind ssp memory(read) uwtable }
154+
;.
155+
; CHECK: [[PROF0]] = !{!"branch_weights", i32 138, i32 12}
156+
;.

llvm/test/Transforms/SimplifyCFG/preserve-branchweights.ll

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ define void @test7(i1 %a, i1 %b) {
268268
; CHECK-LABEL: @test7(
269269
; CHECK-NEXT: entry:
270270
; CHECK-NEXT: [[C:%.*]] = or i1 [[B:%.*]], false
271-
; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[A:%.*]], i1 true, i1 [[C]]
272-
; CHECK-NEXT: br i1 [[BRMERGE]], label [[Y:%.*]], label [[Z:%.*]], !prof [[PROF6:![0-9]+]]
271+
; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[A:%.*]], i1 true, i1 [[C]], !prof [[PROF6:![0-9]+]]
272+
; CHECK-NEXT: br i1 [[BRMERGE]], label [[Y:%.*]], label [[Z:%.*]], !prof [[PROF6]]
273273
; CHECK: common.ret:
274274
; CHECK-NEXT: ret void
275275
; CHECK: Y:
@@ -557,9 +557,9 @@ return:
557557
define i32 @SimplifyCondBranchToCondBranch(i1 %cmpa, i1 %cmpb) {
558558
; CHECK-LABEL: @SimplifyCondBranchToCondBranch(
559559
; CHECK-NEXT: block1:
560-
; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[CMPA:%.*]], i1 true, i1 [[CMPB:%.*]]
561-
; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[CMPA]], i32 0, i32 2, !prof [[PROF13:![0-9]+]]
562-
; CHECK-NEXT: [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof [[PROF14:![0-9]+]]
560+
; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[CMPA:%.*]], i1 true, i1 [[CMPB:%.*]], !prof [[PROF13:![0-9]+]]
561+
; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[CMPA]], i32 0, i32 2, !prof [[PROF14:![0-9]+]]
562+
; CHECK-NEXT: [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof [[PROF13]]
563563
; CHECK-NEXT: ret i32 [[OUTVAL]]
564564
;
565565
block1:
@@ -584,9 +584,9 @@ define i32 @SimplifyCondBranchToCondBranchSwap(i1 %cmpa, i1 %cmpb) {
584584
; CHECK-NEXT: block1:
585585
; CHECK-NEXT: [[CMPA_NOT:%.*]] = xor i1 [[CMPA:%.*]], true
586586
; CHECK-NEXT: [[CMPB_NOT:%.*]] = xor i1 [[CMPB:%.*]], true
587-
; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[CMPA_NOT]], i1 true, i1 [[CMPB_NOT]]
588-
; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[CMPA_NOT]], i32 0, i32 2, !prof [[PROF15:![0-9]+]]
589-
; CHECK-NEXT: [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof [[PROF16:![0-9]+]]
587+
; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[CMPA_NOT]], i1 true, i1 [[CMPB_NOT]], !prof [[PROF15:![0-9]+]]
588+
; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[CMPA_NOT]], i32 0, i32 2, !prof [[PROF16:![0-9]+]]
589+
; CHECK-NEXT: [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof [[PROF15]]
590590
; CHECK-NEXT: ret i32 [[OUTVAL]]
591591
;
592592
block1:
@@ -609,9 +609,9 @@ define i32 @SimplifyCondBranchToCondBranchSwapMissingWeight(i1 %cmpa, i1 %cmpb)
609609
; CHECK-NEXT: block1:
610610
; CHECK-NEXT: [[CMPA_NOT:%.*]] = xor i1 [[CMPA:%.*]], true
611611
; CHECK-NEXT: [[CMPB_NOT:%.*]] = xor i1 [[CMPB:%.*]], true
612-
; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[CMPA_NOT]], i1 true, i1 [[CMPB_NOT]]
613-
; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[CMPA_NOT]], i32 0, i32 2, !prof [[PROF17:![0-9]+]]
614-
; CHECK-NEXT: [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof [[PROF18:![0-9]+]]
612+
; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[CMPA_NOT]], i1 true, i1 [[CMPB_NOT]], !prof [[PROF17:![0-9]+]]
613+
; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[CMPA_NOT]], i32 0, i32 2, !prof [[PROF18:![0-9]+]]
614+
; CHECK-NEXT: [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof [[PROF17]]
615615
; CHECK-NEXT: ret i32 [[OUTVAL]]
616616
;
617617
block1:
@@ -1114,12 +1114,12 @@ exit:
11141114
; CHECK: [[PROF10]] = !{!"branch_weights", i32 8, i32 33}
11151115
; CHECK: [[PROF11]] = !{!"branch_weights", i32 112017436, i32 -735157296}
11161116
; CHECK: [[PROF12]] = !{!"branch_weights", i32 3, i32 5}
1117-
; CHECK: [[PROF13]] = !{!"branch_weights", i32 22, i32 12}
1118-
; CHECK: [[PROF14]] = !{!"branch_weights", i32 34, i32 21}
1119-
; CHECK: [[PROF15]] = !{!"branch_weights", i32 33, i32 14}
1120-
; CHECK: [[PROF16]] = !{!"branch_weights", i32 47, i32 8}
1121-
; CHECK: [[PROF17]] = !{!"branch_weights", i32 6, i32 2}
1122-
; CHECK: [[PROF18]] = !{!"branch_weights", i32 8, i32 2}
1117+
; CHECK: [[PROF13]] = !{!"branch_weights", i32 34, i32 21}
1118+
; CHECK: [[PROF14]] = !{!"branch_weights", i32 22, i32 12}
1119+
; CHECK: [[PROF15]] = !{!"branch_weights", i32 47, i32 8}
1120+
; CHECK: [[PROF16]] = !{!"branch_weights", i32 33, i32 14}
1121+
; CHECK: [[PROF17]] = !{!"branch_weights", i32 8, i32 2}
1122+
; CHECK: [[PROF18]] = !{!"branch_weights", i32 6, i32 2}
11231123
; CHECK: [[PROF19]] = !{!"branch_weights", i32 99, i32 1}
11241124
; CHECK: [[PROF20]] = !{!"branch_weights", i32 1, i32 99}
11251125
; CHECK: [[PROF21]] = !{!"branch_weights", i32 199, i32 1}

0 commit comments

Comments
 (0)