aboutsummaryrefslogtreecommitdiff
path: root/llvm/test/Transforms/SCCP/ip-add-range-to-call.ll
blob: 64c1b9020a054f31f5963b14db181c990920bd38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -passes=ipsccp -S %s | FileCheck %s

; Test 1.
; Both arguments and return value of @callee can be tracked. The inferred range
; can be added to call sites.
define internal i32 @callee(i32 %x) {
; CHECK-LABEL: @callee(
; CHECK-NEXT:    ret i32 [[X:%.*]]
;
  ret i32 %x
}

define i32 @caller1() {
; CHECK-LABEL: @caller1(
; CHECK-NEXT:    [[C1:%.*]] = call i32 @callee(i32 10), !range [[RNG0:![0-9]+]]
; CHECK-NEXT:    [[C2:%.*]] = call i32 @callee(i32 20), !range [[RNG0]]
; CHECK-NEXT:    [[A:%.*]] = add nuw nsw i32 [[C1]], [[C2]]
; CHECK-NEXT:    ret i32 [[A]]
;
  %c1 = call i32 @callee(i32 10)
  %c2 = call i32 @callee(i32 20)
  %a = add i32 %c1, %c2
  ret i32 %a
}

define i32 @caller2(i32 %x) {
; CHECK-LABEL: @caller2(
; CHECK-NEXT:    [[X_15:%.*]] = and i32 [[X:%.*]], 15
; CHECK-NEXT:    [[C:%.*]] = call i32 @callee(i32 [[X_15]]), !range [[RNG0]]
; CHECK-NEXT:    ret i32 [[C]]
;
  %x.15 = and i32 %x, 15
  %c = call i32 @callee(i32 %x.15)
  ret i32 %c
}

; Test 2.
; The return value of @callee2 can be tracked, but arguments cannot, because
; it is passed to @use_cb1. We cannot infer a range for the return value, no
; metadata should be added.

declare void @use_cb1(ptr)

define internal i32 @callee2(i32 %x) {
; CHECK-LABEL: @callee2(
; CHECK-NEXT:    ret i32 [[X:%.*]]
;
  ret i32 %x
}

define void @caller_cb1() {
; CHECK-LABEL: @caller_cb1(
; CHECK-NEXT:    [[C1:%.*]] = call i32 @callee2(i32 9)
; CHECK-NEXT:    [[C2:%.*]] = call i32 @callee2(i32 10)
; CHECK-NEXT:    call void @use_cb1(ptr @callee2)
; CHECK-NEXT:    ret void
;
  %c1 = call i32 @callee2(i32 9)
  %c2 = call i32 @callee2(i32 10)
  call void @use_cb1(ptr @callee2)
  ret void
}

; Test 3.
; The return value can be tracked and it the result range ([500, 601) does not
; depend on the arguments, which cannot be tracked because @callee3 is passed
; to @use_cb2. The result range can be added to the call sites of @callee.

declare void @use_cb2(ptr)

define internal i32 @callee3(i32 %x) {
; CHECK-LABEL: @callee3(
; CHECK-NEXT:    [[C:%.*]] = icmp eq i32 [[X:%.*]], 10
; CHECK-NEXT:    [[S:%.*]] = select i1 [[C]], i32 500, i32 600
; CHECK-NEXT:    ret i32 [[S]]
;
  %c = icmp eq i32 %x, 10
  %s = select i1 %c, i32 500, i32 600
  ret i32 %s
}

define void @caller_cb2() {
; CHECK-LABEL: @caller_cb2(
; CHECK-NEXT:    [[C1:%.*]] = call i32 @callee3(i32 9), !range [[RNG1:![0-9]+]]
; CHECK-NEXT:    [[C2:%.*]] = call i32 @callee3(i32 10), !range [[RNG1]]
; CHECK-NEXT:    call void @use_cb2(ptr @callee3)
; CHECK-NEXT:    ret void
;
  %c1 = call i32 @callee3(i32 9)
  %c2 = call i32 @callee3(i32 10)
  call void @use_cb2(ptr @callee3)
  ret void
}

; Test 4.
; The return value of @callee4 can be tracked, but depends on an argument which
; cannot be tracked. No result range can be inferred.

declare void @use_cb3(ptr)

define internal i32 @callee4(i32 %x, i32 %y) {
; CHECK-LABEL: @callee4(
; CHECK-NEXT:    [[C:%.*]] = icmp eq i32 [[X:%.*]], 10
; CHECK-NEXT:    [[S:%.*]] = select i1 [[C]], i32 500, i32 [[Y:%.*]]
; CHECK-NEXT:    ret i32 [[S]]
;
  %c = icmp eq i32 %x, 10
  %s = select i1 %c, i32 500, i32 %y
  ret i32 %s
}

define void @caller_cb3() {
; CHECK-LABEL: @caller_cb3(
; CHECK-NEXT:    [[C1:%.*]] = call i32 @callee4(i32 11, i32 30)
; CHECK-NOT:   !range
; CHECK-NEXT:    [[C2:%.*]] = call i32 @callee4(i32 12, i32 40)
; CHECK-NOT:   !range
; CHECK-NEXT:    call void @use_cb3(ptr @callee4)
; CHECK-NEXT:    ret void
;
  %c1 = call i32 @callee4(i32 11, i32 30)
  %c2 = call i32 @callee4(i32 12, i32 40)
  call void @use_cb3(ptr @callee4)
  ret void
}

; Test 5.
; Range for the return value of callee5 includes undef. No range metadata
; should be added at call sites.
define internal i32 @callee5(i32 %x, i32 %y) {
; CHECK-LABEL: @callee5(
; CHECK-NEXT:    [[C:%.*]] = icmp slt i32 [[X:%.*]], 15
; CHECK-NEXT:    br i1 [[C]], label [[BB1:%.*]], label [[BB2:%.*]]
; CHECK:       bb1:
; CHECK-NEXT:    br label [[EXIT:%.*]]
; CHECK:       bb2:
; CHECK-NEXT:    br label [[EXIT]]
; CHECK:       exit:
; CHECK-NEXT:    [[RES:%.*]] = phi i32 [ [[Y:%.*]], [[BB1]] ], [ undef, [[BB2]] ]
; CHECK-NEXT:    ret i32 [[RES]]
;
  %c = icmp slt i32 %x, 15
  br i1 %c, label %bb1, label %bb2

bb1:
  br label %exit

bb2:
  br label %exit

exit:
  %res = phi i32 [ %y, %bb1 ], [ undef, %bb2]
  ret i32 %res
}

define i32 @caller5() {
; CHECK-LABEL: @caller5(
; CHECK-NEXT:    [[C1:%.*]] = call i32 @callee5(i32 10, i32 100)
; CHECK-NOT:   !range
; CHECK-NEXT:    [[C2:%.*]] = call i32 @callee5(i32 20, i32 200)
; CHECK-NOT:   !range
; CHECK-NEXT:    [[A:%.*]] = add i32 [[C1]], [[C2]]
; CHECK-NEXT:    ret i32 [[A]]
;
  %c1 = call i32 @callee5(i32 10, i32 100)
  %c2 = call i32 @callee5(i32 20, i32 200)
  %a = add i32 %c1, %c2
  ret i32 %a
}

define internal <2 x i64> @ctlz(<2 x i64> %arg) {
; CHECK-LABEL: @ctlz(
; CHECK-NEXT:    [[RES:%.*]] = call <2 x i64> @llvm.ctlz.v2i64(<2 x i64> [[ARG:%.*]], i1 false)
; CHECK-NEXT:    ret <2 x i64> [[RES]]
;
  %res = call <2 x i64> @llvm.ctlz.v2i64(<2 x i64> %arg, i1 false)
  ret <2 x i64> %res
}

define <2 x i64> @ctlz_caller(<2 x i64> %arg) {
; CHECK-LABEL: @ctlz_caller(
; CHECK-NEXT:    [[RES:%.*]] = call <2 x i64> @ctlz(<2 x i64> [[ARG:%.*]]), !range [[RNG2:![0-9]+]]
; CHECK-NEXT:    ret <2 x i64> [[RES]]
;
  %res = call <2 x i64> @ctlz(<2 x i64> %arg)
  ret <2 x i64> %res
}

declare <2 x i64> @llvm.ctlz.v2i64(<2 x i64>, i1)

; CHECK: [[RNG0]] = !{i32 0, i32 21}
; CHECK: [[RNG1]] = !{i32 500, i32 601}
; CHECK: [[RNG2]] = !{i64 0, i64 65}