Skip to content

Commit 5ed3f46

Browse files
authored
[FuncSpec] Improve handling of Comparison Instructions (#114073)
When visiting comparison instructions during computation of a specializations's bonus, make use of information from the lattice value of the other operand in the case where we have not found this to have a specific constant value.
1 parent 5e75880 commit 5ed3f46

File tree

3 files changed

+144
-9
lines changed

3 files changed

+144
-9
lines changed

llvm/include/llvm/Transforms/IPO/FunctionSpecialization.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class InstCostVisitor : public InstVisitor<InstCostVisitor, Constant *> {
155155
Function *F;
156156
const DataLayout &DL;
157157
TargetTransformInfo &TTI;
158-
SCCPSolver &Solver;
158+
const SCCPSolver &Solver;
159159

160160
ConstMap KnownConstants;
161161
// Basic blocks known to be unreachable after constant propagation.

llvm/lib/Transforms/IPO/FunctionSpecialization.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -472,16 +472,24 @@ Constant *InstCostVisitor::visitCastInst(CastInst &I) {
472472
Constant *InstCostVisitor::visitCmpInst(CmpInst &I) {
473473
assert(LastVisited != KnownConstants.end() && "Invalid iterator!");
474474

475-
bool Swap = I.getOperand(1) == LastVisited->first;
476-
Value *V = Swap ? I.getOperand(0) : I.getOperand(1);
475+
Constant *Const = LastVisited->second;
476+
bool ConstOnRHS = I.getOperand(1) == LastVisited->first;
477+
Value *V = ConstOnRHS ? I.getOperand(0) : I.getOperand(1);
477478
Constant *Other = findConstantFor(V, KnownConstants);
478-
if (!Other)
479-
return nullptr;
480479

481-
Constant *Const = LastVisited->second;
482-
return Swap ?
483-
ConstantFoldCompareInstOperands(I.getPredicate(), Other, Const, DL)
484-
: ConstantFoldCompareInstOperands(I.getPredicate(), Const, Other, DL);
480+
if (Other) {
481+
if (ConstOnRHS)
482+
std::swap(Const, Other);
483+
return ConstantFoldCompareInstOperands(I.getPredicate(), Const, Other, DL);
484+
}
485+
486+
// If we haven't found Other to be a specific constant value, we may still be
487+
// able to constant fold using information from the lattice value.
488+
const ValueLatticeElement &ConstLV = ValueLatticeElement::get(Const);
489+
const ValueLatticeElement &OtherLV = Solver.getLatticeValueFor(V);
490+
auto &V1State = ConstOnRHS ? OtherLV : ConstLV;
491+
auto &V2State = ConstOnRHS ? ConstLV : OtherLV;
492+
return V1State.getCompare(I.getPredicate(), I.getType(), V2State, DL);
485493
}
486494

487495
Constant *InstCostVisitor::visitUnaryOperator(UnaryOperator &I) {
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --include-generated-funcs --version 5
2+
; RUN: opt -passes="ipsccp<func-spec>" -funcspec-min-function-size=1 \
3+
; RUN: -funcspec-for-literal-constant=true \
4+
; RUN: -funcspec-min-codesize-savings=50 \
5+
; RUN: -funcspec-min-latency-savings=0 \
6+
; RUN: -S < %s | FileCheck %s
7+
8+
; Verify that we are able to estimate the codesize savings arising from a branch
9+
; based on a comparison with a value found to have a constant range by IPSCCP.
10+
define i32 @main() {
11+
%notspec = call i32 @test_use_on_lhs(i32 8)
12+
%spec1 = call i32 @test_use_on_lhs(i32 0)
13+
%spec2 = call i32 @test_use_on_rhs(i32 1)
14+
%sum1 = add i32 %notspec, %spec1
15+
%sum2 = add i32 %sum1, %spec2
16+
ret i32 %sum2
17+
}
18+
19+
define i32 @test_use_on_lhs(i32 %x) {
20+
entry:
21+
%range = call i32 @foo(), !range !{ i32 1, i32 0 }
22+
%bound = shl nsw nuw i32 %range, 3
23+
%cmp = icmp uge i32 %x, %bound
24+
br i1 %cmp, label %if.then, label %if.end
25+
26+
if.then:
27+
call void @do_something()
28+
call void @do_something()
29+
call void @do_something()
30+
call void @do_something()
31+
br label %if.end
32+
33+
if.end:
34+
%res = phi i32 [ 0, %entry ], [ 1, %if.then]
35+
ret i32 %res
36+
}
37+
38+
define i32 @test_use_on_rhs(i32 %x) {
39+
entry:
40+
%range = call i32 @foo(), !range !{ i32 1, i32 0 }
41+
%bound = shl nsw nuw i32 %range, 3
42+
%x.sub = sub nsw nuw i32 %x, 1
43+
%cmp = icmp ult i32 %bound, %x.sub
44+
br i1 %cmp, label %if.then, label %if.end
45+
46+
if.then:
47+
call void @do_something()
48+
call void @do_something()
49+
call void @do_something()
50+
call void @do_something()
51+
br label %if.end
52+
53+
if.end:
54+
%res = phi i32 [ 0, %entry ], [ 1, %if.then]
55+
ret i32 %res
56+
}
57+
58+
declare i32 @foo()
59+
declare void @do_something()
60+
; CHECK-LABEL: define range(i32 0, 2) i32 @main() {
61+
; CHECK-NEXT: [[NOTSPEC:%.*]] = call i32 @test_use_on_lhs(i32 8)
62+
; CHECK-NEXT: [[SPEC1:%.*]] = call i32 @test_use_on_lhs.specialized.1(i32 0)
63+
; CHECK-NEXT: [[SPEC2:%.*]] = call i32 @test_use_on_rhs.specialized.2(i32 1)
64+
; CHECK-NEXT: [[SUM:%.*]] = add nuw nsw i32 [[NOTSPEC]], 0
65+
; CHECK-NEXT: [[RES:%.*]] = add nuw nsw i32 [[SUM]], 0
66+
; CHECK-NEXT: ret i32 [[RES]]
67+
;
68+
;
69+
; CHECK-LABEL: define range(i32 0, 2) i32 @test_use_on_lhs(
70+
; CHECK-SAME: i32 [[X:%.*]]) {
71+
; CHECK-NEXT: [[ENTRY:.*]]:
72+
; CHECK-NEXT: [[RANGE:%.*]] = call i32 @foo(), !range [[RNG0:![0-9]+]]
73+
; CHECK-NEXT: [[BOUND:%.*]] = shl nuw nsw i32 [[RANGE]], 3
74+
; CHECK-NEXT: [[CMP:%.*]] = icmp uge i32 [[X]], [[BOUND]]
75+
; CHECK-NEXT: br i1 [[CMP]], label %[[IF_THEN:.*]], label %[[IF_END:.*]]
76+
; CHECK: [[IF_THEN]]:
77+
; CHECK-NEXT: call void @do_something()
78+
; CHECK-NEXT: call void @do_something()
79+
; CHECK-NEXT: call void @do_something()
80+
; CHECK-NEXT: call void @do_something()
81+
; CHECK-NEXT: br label %[[IF_END]]
82+
; CHECK: [[IF_END]]:
83+
; CHECK-NEXT: [[RES:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ 1, %[[IF_THEN]] ]
84+
; CHECK-NEXT: ret i32 [[RES]]
85+
;
86+
;
87+
; CHECK-LABEL: define range(i32 0, 2) i32 @test_use_on_rhs(
88+
; CHECK-SAME: i32 [[X:%.*]]) {
89+
; CHECK-NEXT: [[ENTRY:.*]]:
90+
; CHECK-NEXT: [[RANGE:%.*]] = call i32 @foo(), !range [[RNG0]]
91+
; CHECK-NEXT: [[BOUND:%.*]] = shl nuw nsw i32 [[RANGE]], 3
92+
; CHECK-NEXT: [[X_SUB:%.*]] = sub nuw nsw i32 [[X]], 1
93+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[BOUND]], [[X_SUB]]
94+
; CHECK-NEXT: br i1 [[CMP]], label %[[IF_THEN:.*]], label %[[IF_END:.*]]
95+
; CHECK: [[IF_THEN]]:
96+
; CHECK-NEXT: call void @do_something()
97+
; CHECK-NEXT: call void @do_something()
98+
; CHECK-NEXT: call void @do_something()
99+
; CHECK-NEXT: call void @do_something()
100+
; CHECK-NEXT: br label %[[IF_END]]
101+
; CHECK: [[IF_END]]:
102+
; CHECK-NEXT: [[RES:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ 1, %[[IF_THEN]] ]
103+
; CHECK-NEXT: ret i32 [[RES]]
104+
;
105+
;
106+
; CHECK-LABEL: define internal i32 @test_use_on_lhs.specialized.1(
107+
; CHECK-SAME: i32 [[X:%.*]]) {
108+
; CHECK-NEXT: [[ENTRY:.*:]]
109+
; CHECK-NEXT: [[RANGE:%.*]] = call i32 @foo(), !range [[RNG0]]
110+
; CHECK-NEXT: [[BOUND:%.*]] = shl nuw nsw i32 [[RANGE]], 3
111+
; CHECK-NEXT: br label %[[IF_END:.*]]
112+
; CHECK: [[IF_END]]:
113+
; CHECK-NEXT: ret i32 poison
114+
;
115+
;
116+
; CHECK-LABEL: define internal i32 @test_use_on_rhs.specialized.2(
117+
; CHECK-SAME: i32 [[X:%.*]]) {
118+
; CHECK-NEXT: [[ENTRY:.*:]]
119+
; CHECK-NEXT: [[RANGE:%.*]] = call i32 @foo(), !range [[RNG0]]
120+
; CHECK-NEXT: [[BOUND:%.*]] = shl nuw nsw i32 [[RANGE]], 3
121+
; CHECK-NEXT: br label %[[IF_END:.*]]
122+
; CHECK: [[IF_END]]:
123+
; CHECK-NEXT: ret i32 poison
124+
;
125+
;.
126+
; CHECK: [[RNG0]] = !{i32 1, i32 0}
127+
;.

0 commit comments

Comments
 (0)