pow-sqrt.ll
14.3 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -instcombine -S | FileCheck %s
; Check the libcall and the intrinsic for each case with differing FMF.
; The transform to sqrt is not allowed if we risk setting errno due to -INF.
define double @pow_libcall_half_no_FMF(double %x) {
; CHECK-LABEL: @pow_libcall_half_no_FMF(
; CHECK-NEXT: [[POW:%.*]] = call double @pow(double [[X:%.*]], double 5.000000e-01)
; CHECK-NEXT: ret double [[POW]]
;
%pow = call double @pow(double %x, double 5.0e-01)
ret double %pow
}
; The transform to (non-errno setting) sqrt is allowed as long as we deal with -0.0 and -INF.
define double @pow_intrinsic_half_no_FMF(double %x) {
; CHECK-LABEL: @pow_intrinsic_half_no_FMF(
; CHECK-NEXT: [[SQRT:%.*]] = call double @llvm.sqrt.f64(double [[X:%.*]])
; CHECK-NEXT: [[ABS:%.*]] = call double @llvm.fabs.f64(double [[SQRT]])
; CHECK-NEXT: [[ISINF:%.*]] = fcmp oeq double [[X]], 0xFFF0000000000000
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[ISINF]], double 0x7FF0000000000000, double [[ABS]]
; CHECK-NEXT: ret double [[TMP1]]
;
%pow = call double @llvm.pow.f64(double %x, double 5.0e-01)
ret double %pow
}
; `afn` makes no difference, but FMF are propagated/retained.
; (As above) the transform to sqrt may generate EDOM due to -INF. Generally, EDOM implies
; formation of a NaN (which then propagates). `afn` may justify returning NaN (along with
; setting EDOM); however, the conservatively correct approach is to avoid both the NaN and
; the EDOM.
define double @pow_libcall_half_approx(double %x) {
; CHECK-LABEL: @pow_libcall_half_approx(
; CHECK-NEXT: [[POW:%.*]] = call afn double @pow(double [[X:%.*]], double 5.000000e-01)
; CHECK-NEXT: ret double [[POW]]
;
%pow = call afn double @pow(double %x, double 5.0e-01)
ret double %pow
}
; (As above) the transform to (non-errno setting) sqrt is allowed as long as we deal with -0.0
; and -INF.
define <2 x double> @pow_intrinsic_half_approx(<2 x double> %x) {
; CHECK-LABEL: @pow_intrinsic_half_approx(
; CHECK-NEXT: [[SQRT:%.*]] = call afn <2 x double> @llvm.sqrt.v2f64(<2 x double> [[X:%.*]])
; CHECK-NEXT: [[ABS:%.*]] = call afn <2 x double> @llvm.fabs.v2f64(<2 x double> [[SQRT]])
; CHECK-NEXT: [[ISINF:%.*]] = fcmp afn oeq <2 x double> [[X]], <double 0xFFF0000000000000, double 0xFFF0000000000000>
; CHECK-NEXT: [[TMP1:%.*]] = select afn <2 x i1> [[ISINF]], <2 x double> <double 0x7FF0000000000000, double 0x7FF0000000000000>, <2 x double> [[ABS]]
; CHECK-NEXT: ret <2 x double> [[TMP1]]
;
%pow = call afn <2 x double> @llvm.pow.v2f64(<2 x double> %x, <2 x double> <double 5.0e-01, double 5.0e-01>)
ret <2 x double> %pow
}
define float @powf_intrinsic_half_fast(float %x) {
; CHECK-LABEL: @powf_intrinsic_half_fast(
; CHECK-NEXT: [[SQRT:%.*]] = call fast float @llvm.sqrt.f32(float [[X:%.*]])
; CHECK-NEXT: ret float [[SQRT]]
;
%pow = call fast float @llvm.pow.f32(float %x, float 5.0e-01)
ret float %pow
}
; If we can disregard INFs, no need for a select.
define double @pow_libcall_half_no_FMF_base_ninf(i32 %x) {
; CHECK-LABEL: @pow_libcall_half_no_FMF_base_ninf(
; CHECK-NEXT: [[CONV:%.*]] = uitofp i32 [[X:%.*]] to double
; CHECK-NEXT: [[SQRT:%.*]] = call double @sqrt(double [[CONV]])
; CHECK-NEXT: [[ABS:%.*]] = call double @llvm.fabs.f64(double [[SQRT]])
; CHECK-NEXT: ret double [[ABS]]
;
%conv = uitofp i32 %x to double
%pow = call double @pow(double %conv, double 5.0e-01)
ret double %pow
}
define double @pow_libcall_half_ninf(double %x) {
; CHECK-LABEL: @pow_libcall_half_ninf(
; CHECK-NEXT: [[SQRT:%.*]] = call ninf double @sqrt(double [[X:%.*]])
; CHECK-NEXT: [[ABS:%.*]] = call ninf double @llvm.fabs.f64(double [[SQRT]])
; CHECK-NEXT: ret double [[ABS]]
;
%pow = call ninf double @pow(double %x, double 5.0e-01)
ret double %pow
}
define <2 x double> @pow_intrinsic_half_ninf(<2 x double> %x) {
; CHECK-LABEL: @pow_intrinsic_half_ninf(
; CHECK-NEXT: [[SQRT:%.*]] = call ninf <2 x double> @llvm.sqrt.v2f64(<2 x double> [[X:%.*]])
; CHECK-NEXT: [[ABS:%.*]] = call ninf <2 x double> @llvm.fabs.v2f64(<2 x double> [[SQRT]])
; CHECK-NEXT: ret <2 x double> [[ABS]]
;
%pow = call ninf <2 x double> @llvm.pow.v2f64(<2 x double> %x, <2 x double> <double 5.0e-01, double 5.0e-01>)
ret <2 x double> %pow
}
; If we can disregard -0.0, no need for fabs, but still (because of -INF) cannot use library sqrt.
define double @pow_libcall_half_nsz(double %x) {
; CHECK-LABEL: @pow_libcall_half_nsz(
; CHECK-NEXT: [[POW:%.*]] = call nsz double @pow(double [[X:%.*]], double 5.000000e-01)
; CHECK-NEXT: ret double [[POW]]
;
%pow = call nsz double @pow(double %x, double 5.0e-01)
ret double %pow
}
define double @pow_intrinsic_half_nsz(double %x) {
; CHECK-LABEL: @pow_intrinsic_half_nsz(
; CHECK-NEXT: [[SQRT:%.*]] = call nsz double @llvm.sqrt.f64(double [[X:%.*]])
; CHECK-NEXT: [[ISINF:%.*]] = fcmp nsz oeq double [[X]], 0xFFF0000000000000
; CHECK-NEXT: [[TMP1:%.*]] = select nsz i1 [[ISINF]], double 0x7FF0000000000000, double [[SQRT]]
; CHECK-NEXT: ret double [[TMP1]]
;
%pow = call nsz double @llvm.pow.f64(double %x, double 5.0e-01)
ret double %pow
}
; This is just sqrt.
define float @pow_libcall_half_ninf_nsz(float %x) {
; CHECK-LABEL: @pow_libcall_half_ninf_nsz(
; CHECK-NEXT: [[SQRTF:%.*]] = call ninf nsz float @sqrtf(float [[X:%.*]])
; CHECK-NEXT: ret float [[SQRTF]]
;
%pow = call ninf nsz float @powf(float %x, float 5.0e-01)
ret float %pow
}
define double @pow_intrinsic_half_ninf_nsz(double %x) {
; CHECK-LABEL: @pow_intrinsic_half_ninf_nsz(
; CHECK-NEXT: [[SQRT:%.*]] = call ninf nsz double @llvm.sqrt.f64(double [[X:%.*]])
; CHECK-NEXT: ret double [[SQRT]]
;
%pow = call ninf nsz double @llvm.pow.f64(double %x, double 5.0e-01)
ret double %pow
}
; Overspecified FMF to test propagation to the new op(s).
define float @pow_libcall_half_fast(float %x) {
; CHECK-LABEL: @pow_libcall_half_fast(
; CHECK-NEXT: [[SQRTF:%.*]] = call fast float @sqrtf(float [[X:%.*]])
; CHECK-NEXT: ret float [[SQRTF]]
;
%pow = call fast float @powf(float %x, float 5.0e-01)
ret float %pow
}
define double @pow_intrinsic_half_fast(double %x) {
; CHECK-LABEL: @pow_intrinsic_half_fast(
; CHECK-NEXT: [[SQRT:%.*]] = call fast double @llvm.sqrt.f64(double [[X:%.*]])
; CHECK-NEXT: ret double [[SQRT]]
;
%pow = call fast double @llvm.pow.f64(double %x, double 5.0e-01)
ret double %pow
}
; This should not be transformed without some kind of FMF.
; -0.5 means take the reciprocal.
define float @pow_libcall_neghalf_no_FMF(float %x) {
; CHECK-LABEL: @pow_libcall_neghalf_no_FMF(
; CHECK-NEXT: [[POW:%.*]] = call float @powf(float [[X:%.*]], float -5.000000e-01)
; CHECK-NEXT: ret float [[POW]]
;
%pow = call float @powf(float %x, float -5.0e-01)
ret float %pow
}
; If we can disregard INFs, a call to a library sqrt is okay.
; Transform to sqrt+fdiv because 'reassoc' allows an extra rounding step.
; Use 'fabs' to handle -0.0 correctly.
define float @pow_libcall_neghalf_reassoc_ninf(float %x) {
; CHECK-LABEL: @pow_libcall_neghalf_reassoc_ninf(
; CHECK-NEXT: [[SQRTF:%.*]] = call reassoc ninf float @sqrtf(float [[X:%.*]])
; CHECK-NEXT: [[ABS:%.*]] = call reassoc ninf float @llvm.fabs.f32(float [[SQRTF]])
; CHECK-NEXT: [[RECIPROCAL:%.*]] = fdiv reassoc ninf float 1.000000e+00, [[ABS]]
; CHECK-NEXT: ret float [[RECIPROCAL]]
;
%pow = call reassoc ninf float @powf(float %x, float -5.0e-01)
ret float %pow
}
; If we cannot disregard INFs, a call to a library sqrt is not okay.
define float @pow_libcall_neghalf_afn(float %x) {
; CHECK-LABEL: @pow_libcall_neghalf_afn(
; CHECK-NEXT: [[POW:%.*]] = call afn float @powf(float [[X:%.*]], float -5.000000e-01)
; CHECK-NEXT: ret float [[POW]]
;
%pow = call afn float @powf(float %x, float -5.0e-01)
ret float %pow
}
; This should not be transformed without some kind of FMF.
define <2 x double> @pow_intrinsic_neghalf_no_FMF(<2 x double> %x) {
; CHECK-LABEL: @pow_intrinsic_neghalf_no_FMF(
; CHECK-NEXT: [[POW:%.*]] = call <2 x double> @llvm.pow.v2f64(<2 x double> [[X:%.*]], <2 x double> <double -5.000000e-01, double -5.000000e-01>)
; CHECK-NEXT: ret <2 x double> [[POW]]
;
%pow = call <2 x double> @llvm.pow.v2f64(<2 x double> %x, <2 x double> <double -5.0e-01, double -5.0e-01>)
ret <2 x double> %pow
}
; Transform to sqrt+fdiv because 'reassoc' allows an extra rounding step.
; Use 'fabs' to handle -0.0 correctly.
; Use 'select' to handle -INF correctly.
define <2 x double> @pow_intrinsic_neghalf_reassoc(<2 x double> %x) {
; CHECK-LABEL: @pow_intrinsic_neghalf_reassoc(
; CHECK-NEXT: [[SQRT:%.*]] = call reassoc <2 x double> @llvm.sqrt.v2f64(<2 x double> [[X:%.*]])
; CHECK-NEXT: [[ABS:%.*]] = call reassoc <2 x double> @llvm.fabs.v2f64(<2 x double> [[SQRT]])
; CHECK-NEXT: [[ISINF:%.*]] = fcmp reassoc oeq <2 x double> [[X]], <double 0xFFF0000000000000, double 0xFFF0000000000000>
; CHECK-NEXT: [[ABS_OP:%.*]] = fdiv reassoc <2 x double> <double 1.000000e+00, double 1.000000e+00>, [[ABS]]
; CHECK-NEXT: [[RECIPROCAL:%.*]] = select <2 x i1> [[ISINF]], <2 x double> zeroinitializer, <2 x double> [[ABS_OP]]
; CHECK-NEXT: ret <2 x double> [[RECIPROCAL]]
;
%pow = call reassoc <2 x double> @llvm.pow.v2f64(<2 x double> %x, <2 x double> <double -5.0e-01, double -5.0e-01>)
ret <2 x double> %pow
}
; Transform to sqrt+fdiv because 'afn' allows an extra rounding step.
; Use 'fabs' to handle -0.0 correctly.
; Use 'select' to handle -INF correctly.
define <2 x double> @pow_intrinsic_neghalf_afn(<2 x double> %x) {
; CHECK-LABEL: @pow_intrinsic_neghalf_afn(
; CHECK-NEXT: [[SQRT:%.*]] = call afn <2 x double> @llvm.sqrt.v2f64(<2 x double> [[X:%.*]])
; CHECK-NEXT: [[ABS:%.*]] = call afn <2 x double> @llvm.fabs.v2f64(<2 x double> [[SQRT]])
; CHECK-NEXT: [[ISINF:%.*]] = fcmp afn oeq <2 x double> [[X]], <double 0xFFF0000000000000, double 0xFFF0000000000000>
; CHECK-NEXT: [[ABS_OP:%.*]] = fdiv afn <2 x double> <double 1.000000e+00, double 1.000000e+00>, [[ABS]]
; CHECK-NEXT: [[RECIPROCAL:%.*]] = select <2 x i1> [[ISINF]], <2 x double> zeroinitializer, <2 x double> [[ABS_OP]]
; CHECK-NEXT: ret <2 x double> [[RECIPROCAL]]
;
%pow = call afn <2 x double> @llvm.pow.v2f64(<2 x double> %x, <2 x double> <double -5.0e-01, double -5.0e-01>)
ret <2 x double> %pow
}
; If we can disregard INFs, no need for a select.
define double @pow_libcall_neghalf_ninf(double %x) {
; CHECK-LABEL: @pow_libcall_neghalf_ninf(
; CHECK-NEXT: [[SQRT:%.*]] = call ninf afn double @sqrt(double [[X:%.*]])
; CHECK-NEXT: [[ABS:%.*]] = call ninf afn double @llvm.fabs.f64(double [[SQRT]])
; CHECK-NEXT: [[RECIPROCAL:%.*]] = fdiv ninf afn double 1.000000e+00, [[ABS]]
; CHECK-NEXT: ret double [[RECIPROCAL]]
;
%pow = call afn ninf double @pow(double %x, double -5.0e-01)
ret double %pow
}
define <2 x double> @pow_intrinsic_neghalf_ninf(<2 x double> %x) {
; CHECK-LABEL: @pow_intrinsic_neghalf_ninf(
; CHECK-NEXT: [[SQRT:%.*]] = call ninf afn <2 x double> @llvm.sqrt.v2f64(<2 x double> [[X:%.*]])
; CHECK-NEXT: [[ABS:%.*]] = call ninf afn <2 x double> @llvm.fabs.v2f64(<2 x double> [[SQRT]])
; CHECK-NEXT: [[RECIPROCAL:%.*]] = fdiv ninf afn <2 x double> <double 1.000000e+00, double 1.000000e+00>, [[ABS]]
; CHECK-NEXT: ret <2 x double> [[RECIPROCAL]]
;
%pow = call afn ninf <2 x double> @llvm.pow.v2f64(<2 x double> %x, <2 x double> <double -5.0e-01, double -5.0e-01>)
ret <2 x double> %pow
}
; If we can disregard -0.0, no need for fabs, but still (because of -INF) cannot use library sqrt.
define double @pow_libcall_neghalf_nsz(double %x) {
; CHECK-LABEL: @pow_libcall_neghalf_nsz(
; CHECK-NEXT: [[POW:%.*]] = call nsz afn double @pow(double [[X:%.*]], double -5.000000e-01)
; CHECK-NEXT: ret double [[POW]]
;
%pow = call afn nsz double @pow(double %x, double -5.0e-01)
ret double %pow
}
define double @pow_intrinsic_neghalf_nsz(double %x) {
; CHECK-LABEL: @pow_intrinsic_neghalf_nsz(
; CHECK-NEXT: [[SQRT:%.*]] = call nsz afn double @llvm.sqrt.f64(double [[X:%.*]])
; CHECK-NEXT: [[ISINF:%.*]] = fcmp nsz afn oeq double [[X]], 0xFFF0000000000000
; CHECK-NEXT: [[SQRT_OP:%.*]] = fdiv nsz afn double 1.000000e+00, [[SQRT]]
; CHECK-NEXT: [[RECIPROCAL:%.*]] = select i1 [[ISINF]], double 0.000000e+00, double [[SQRT_OP]]
; CHECK-NEXT: ret double [[RECIPROCAL]]
;
%pow = call afn nsz double @llvm.pow.f64(double %x, double -5.0e-01)
ret double %pow
}
; This is just recip-sqrt.
define double @pow_intrinsic_neghalf_ninf_nsz(double %x) {
; CHECK-LABEL: @pow_intrinsic_neghalf_ninf_nsz(
; CHECK-NEXT: [[SQRT:%.*]] = call ninf nsz afn double @llvm.sqrt.f64(double [[X:%.*]])
; CHECK-NEXT: [[RECIPROCAL:%.*]] = fdiv ninf nsz afn double 1.000000e+00, [[SQRT]]
; CHECK-NEXT: ret double [[RECIPROCAL]]
;
%pow = call afn ninf nsz double @llvm.pow.f64(double %x, double -5.0e-01)
ret double %pow
}
define float @pow_libcall_neghalf_ninf_nsz(float %x) {
; CHECK-LABEL: @pow_libcall_neghalf_ninf_nsz(
; CHECK-NEXT: [[SQRTF:%.*]] = call ninf nsz afn float @sqrtf(float [[X:%.*]])
; CHECK-NEXT: [[RECIPROCAL:%.*]] = fdiv ninf nsz afn float 1.000000e+00, [[SQRTF]]
; CHECK-NEXT: ret float [[RECIPROCAL]]
;
%pow = call afn ninf nsz float @powf(float %x, float -5.0e-01)
ret float %pow
}
; Overspecified FMF to test propagation to the new op(s).
define float @pow_libcall_neghalf_fast(float %x) {
; CHECK-LABEL: @pow_libcall_neghalf_fast(
; CHECK-NEXT: [[SQRTF:%.*]] = call fast float @sqrtf(float [[X:%.*]])
; CHECK-NEXT: [[RECIPROCAL:%.*]] = fdiv fast float 1.000000e+00, [[SQRTF]]
; CHECK-NEXT: ret float [[RECIPROCAL]]
;
%pow = call fast float @powf(float %x, float -5.0e-01)
ret float %pow
}
define double @pow_intrinsic_neghalf_fast(double %x) {
; CHECK-LABEL: @pow_intrinsic_neghalf_fast(
; CHECK-NEXT: [[SQRT:%.*]] = call fast double @llvm.sqrt.f64(double [[X:%.*]])
; CHECK-NEXT: [[RECIPROCAL:%.*]] = fdiv fast double 1.000000e+00, [[SQRT]]
; CHECK-NEXT: ret double [[RECIPROCAL]]
;
%pow = call fast double @llvm.pow.f64(double %x, double -5.0e-01)
ret double %pow
}
declare double @llvm.pow.f64(double, double) #0
declare float @llvm.pow.f32(float, float) #0
declare <2 x double> @llvm.pow.v2f64(<2 x double>, <2 x double>) #0
declare <2 x float> @llvm.pow.v2f32(<2 x float>, <2 x float>) #0
declare <4 x float> @llvm.pow.v4f32(<4 x float>, <4 x float>) #0
declare double @pow(double, double)
declare float @powf(float, float)
attributes #0 = { nounwind readnone speculatable }
attributes #1 = { nounwind readnone }