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