Skip to content

Commit b5afe41

Browse files
authored
[InstCombine] Preserve profile data with select instructions and binary operators (#158375)
Tracking issue: #147390
1 parent 7943513 commit b5afe41

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ static cl::opt<unsigned>
144144
MaxArraySize("instcombine-maxarray-size", cl::init(1024),
145145
cl::desc("Maximum array size considered when doing a combine"));
146146

147+
extern cl::opt<bool> ProfcheckDisableMetadataFixes;
148+
147149
// FIXME: Remove this flag when it is no longer necessary to convert
148150
// llvm.dbg.declare to avoid inaccurate debug info. Setting this to false
149151
// increases variable availability at the cost of accuracy. Variables that
@@ -1361,6 +1363,10 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
13611363
if (!LHSIsSelect && !RHSIsSelect)
13621364
return nullptr;
13631365

1366+
SelectInst *SI = ProfcheckDisableMetadataFixes
1367+
? nullptr
1368+
: cast<SelectInst>(LHSIsSelect ? LHS : RHS);
1369+
13641370
FastMathFlags FMF;
13651371
BuilderTy::FastMathFlagGuard Guard(Builder);
13661372
if (isa<FPMathOperator>(&I)) {
@@ -1381,15 +1387,14 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
13811387
// We need an 'add' and exactly 1 arm of the select to have been simplified.
13821388
if (Opcode != Instruction::Add || (!True && !False) || (True && False))
13831389
return nullptr;
1384-
13851390
Value *N;
13861391
if (True && match(FVal, m_Neg(m_Value(N)))) {
13871392
Value *Sub = Builder.CreateSub(Z, N);
1388-
return Builder.CreateSelect(Cond, True, Sub, I.getName());
1393+
return Builder.CreateSelect(Cond, True, Sub, I.getName(), SI);
13891394
}
13901395
if (False && match(TVal, m_Neg(m_Value(N)))) {
13911396
Value *Sub = Builder.CreateSub(Z, N);
1392-
return Builder.CreateSelect(Cond, Sub, False, I.getName());
1397+
return Builder.CreateSelect(Cond, Sub, False, I.getName(), SI);
13931398
}
13941399
return nullptr;
13951400
};
@@ -1425,9 +1430,9 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
14251430
if (!True || !False)
14261431
return nullptr;
14271432

1428-
Value *SI = Builder.CreateSelect(Cond, True, False);
1429-
SI->takeName(&I);
1430-
return SI;
1433+
Value *NewSI = Builder.CreateSelect(Cond, True, False, I.getName(), SI);
1434+
NewSI->takeName(&I);
1435+
return NewSI;
14311436
}
14321437

14331438
/// Freely adapt every user of V as-if V was changed to !V.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals all --version 6
2+
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3+
4+
define i32 @LHSBin(i1 %C) !prof !0 {
5+
; CHECK-LABEL: define i32 @LHSBin(
6+
; CHECK-SAME: i1 [[C:%.*]]) !prof [[PROF0:![0-9]+]] {
7+
; CHECK-NEXT: [[V:%.*]] = select i1 [[C]], i32 1010, i32 20, !prof [[PROF1:![0-9]+]]
8+
; CHECK-NEXT: ret i32 [[V]]
9+
;
10+
%A = select i1 %C, i32 1000, i32 10, !prof !1
11+
%V = add i32 %A, 10
12+
ret i32 %V
13+
}
14+
15+
define i32 @RHSBin(i1 %C) !prof !0 {
16+
; CHECK-LABEL: define i32 @RHSBin(
17+
; CHECK-SAME: i1 [[C:%.*]]) !prof [[PROF0]] {
18+
; CHECK-NEXT: [[V:%.*]] = select i1 [[C]], i32 1010, i32 20, !prof [[PROF1]]
19+
; CHECK-NEXT: ret i32 [[V]]
20+
;
21+
%A = select i1 %C, i32 1000, i32 10, !prof !1
22+
%V = add i32 10, %A
23+
ret i32 %V;
24+
}
25+
26+
define i32 @BothBin(i1 %C) !prof !0 {
27+
; CHECK-LABEL: define i32 @BothBin(
28+
; CHECK-SAME: i1 [[C:%.*]]) !prof [[PROF0]] {
29+
; CHECK-NEXT: [[V:%.*]] = select i1 [[C]], i32 2000, i32 20, !prof [[PROF1]]
30+
; CHECK-NEXT: ret i32 [[V]]
31+
;
32+
%A = select i1 %C, i32 1000, i32 10, !prof !1
33+
%B = select i1 %C, i32 1000, i32 10, !prof !1
34+
%V = add i32 %A, %B
35+
ret i32 %V;
36+
}
37+
38+
define i32 @NegBin(i1 %C) !prof !0 {
39+
; CHECK-LABEL: define i32 @NegBin(
40+
; CHECK-SAME: i1 [[C:%.*]]) !prof [[PROF0]] {
41+
; CHECK-NEXT: [[V:%.*]] = select i1 [[C]], i32 1010, i32 0, !prof [[PROF1]]
42+
; CHECK-NEXT: ret i32 [[V]]
43+
;
44+
%A = select i1 %C, i32 1000, i32 -10, !prof !1
45+
%V = add i32 %A, 10
46+
ret i32 %V
47+
}
48+
49+
!0 = !{!"function_entry_count", i64 1000}
50+
!1 = !{!"branch_weights", i32 2, i32 3}
51+
;.
52+
; CHECK: [[PROF0]] = !{!"function_entry_count", i64 1000}
53+
; CHECK: [[PROF1]] = !{!"branch_weights", i32 2, i32 3}
54+
;.

0 commit comments

Comments
 (0)