1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -instcombine < %s | FileCheck %s
4 declare float @llvm.minimum.f32(float, float)
5 declare <2 x float> @llvm.minimum.v2f32(<2 x float>, <2 x float>)
6 declare <4 x float> @llvm.minimum.v4f32(<4 x float>, <4 x float>)
8 declare double @llvm.minimum.f64(double, double)
9 declare <2 x double> @llvm.minimum.v2f64(<2 x double>, <2 x double>)
11 declare float @llvm.maximum.f32(float, float)
13 define float @constant_fold_minimum_f32() {
14 ; CHECK-LABEL: @constant_fold_minimum_f32(
15 ; CHECK-NEXT: ret float 1.000000e+00
17 %x = call float @llvm.minimum.f32(float 1.0, float 2.0)
21 define float @constant_fold_minimum_f32_inv() {
22 ; CHECK-LABEL: @constant_fold_minimum_f32_inv(
23 ; CHECK-NEXT: ret float 1.000000e+00
25 %x = call float @llvm.minimum.f32(float 2.0, float 1.0)
29 define float @constant_fold_minimum_f32_nan0() {
30 ; CHECK-LABEL: @constant_fold_minimum_f32_nan0(
31 ; CHECK-NEXT: ret float 0x7FF8000000000000
33 %x = call float @llvm.minimum.f32(float 0x7FF8000000000000, float 2.0)
37 define float @constant_fold_minimum_f32_nan1() {
38 ; CHECK-LABEL: @constant_fold_minimum_f32_nan1(
39 ; CHECK-NEXT: ret float 0x7FF8000000000000
41 %x = call float @llvm.minimum.f32(float 2.0, float 0x7FF8000000000000)
45 define float @constant_fold_minimum_f32_nan_nan() {
46 ; CHECK-LABEL: @constant_fold_minimum_f32_nan_nan(
47 ; CHECK-NEXT: ret float 0x7FF8000000000000
49 %x = call float @llvm.minimum.f32(float 0x7FF8000000000000, float 0x7FF8000000000000)
53 define float @constant_fold_minimum_f32_p0_p0() {
54 ; CHECK-LABEL: @constant_fold_minimum_f32_p0_p0(
55 ; CHECK-NEXT: ret float 0.000000e+00
57 %x = call float @llvm.minimum.f32(float 0.0, float 0.0)
61 define float @constant_fold_minimum_f32_p0_n0() {
62 ; CHECK-LABEL: @constant_fold_minimum_f32_p0_n0(
63 ; CHECK-NEXT: ret float -0.000000e+00
65 %x = call float @llvm.minimum.f32(float 0.0, float -0.0)
69 define float @constant_fold_minimum_f32_n0_p0() {
70 ; CHECK-LABEL: @constant_fold_minimum_f32_n0_p0(
71 ; CHECK-NEXT: ret float -0.000000e+00
73 %x = call float @llvm.minimum.f32(float -0.0, float 0.0)
77 define float @constant_fold_minimum_f32_n0_n0() {
78 ; CHECK-LABEL: @constant_fold_minimum_f32_n0_n0(
79 ; CHECK-NEXT: ret float -0.000000e+00
81 %x = call float @llvm.minimum.f32(float -0.0, float -0.0)
85 define <4 x float> @constant_fold_minimum_v4f32() {
86 ; CHECK-LABEL: @constant_fold_minimum_v4f32(
87 ; CHECK-NEXT: ret <4 x float> <float 1.000000e+00, float 2.000000e+00, float 3.000000e+00, float 5.000000e+00>
89 %x = call <4 x float> @llvm.minimum.v4f32(<4 x float> <float 1.0, float 8.0, float 3.0, float 9.0>, <4 x float> <float 2.0, float 2.0, float 10.0, float 5.0>)
93 define double @constant_fold_minimum_f64() {
94 ; CHECK-LABEL: @constant_fold_minimum_f64(
95 ; CHECK-NEXT: ret double 1.000000e+00
97 %x = call double @llvm.minimum.f64(double 1.0, double 2.0)
101 define double @constant_fold_minimum_f64_nan0() {
102 ; CHECK-LABEL: @constant_fold_minimum_f64_nan0(
103 ; CHECK-NEXT: ret double 0x7FF8000000000000
105 %x = call double @llvm.minimum.f64(double 0x7FF8000000000000, double 2.0)
109 define double @constant_fold_minimum_f64_nan1() {
110 ; CHECK-LABEL: @constant_fold_minimum_f64_nan1(
111 ; CHECK-NEXT: ret double 0x7FF8000000000000
113 %x = call double @llvm.minimum.f64(double 2.0, double 0x7FF8000000000000)
117 define double @constant_fold_minimum_f64_nan_nan() {
118 ; CHECK-LABEL: @constant_fold_minimum_f64_nan_nan(
119 ; CHECK-NEXT: ret double 0x7FF8000000000000
121 %x = call double @llvm.minimum.f64(double 0x7FF8000000000000, double 0x7FF8000000000000)
125 define float @canonicalize_constant_minimum_f32(float %x) {
126 ; CHECK-LABEL: @canonicalize_constant_minimum_f32(
127 ; CHECK-NEXT: [[Y:%.*]] = call float @llvm.minimum.f32(float [[X:%.*]], float 1.000000e+00)
128 ; CHECK-NEXT: ret float [[Y]]
130 %y = call float @llvm.minimum.f32(float 1.0, float %x)
134 define float @minimum_f32_nan_val(float %x) {
135 ; CHECK-LABEL: @minimum_f32_nan_val(
136 ; CHECK-NEXT: ret float 0x7FF8000000000000
138 %y = call float @llvm.minimum.f32(float 0x7FF8000000000000, float %x)
142 define float @minimum_f32_val_nan(float %x) {
143 ; CHECK-LABEL: @minimum_f32_val_nan(
144 ; CHECK-NEXT: ret float 0x7FF8000000000000
146 %y = call float @llvm.minimum.f32(float %x, float 0x7FF8000000000000)
150 define float @minimum_f32_1_minimum_val_p0(float %x) {
151 ; CHECK-LABEL: @minimum_f32_1_minimum_val_p0(
152 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.minimum.f32(float [[X:%.*]], float 0.000000e+00)
153 ; CHECK-NEXT: ret float [[TMP1]]
155 %y = call float @llvm.minimum.f32(float %x, float 0.0)
156 %z = call float @llvm.minimum.f32(float %y, float 1.0)
160 define float @minimum_f32_1_minimum_p0_val_fast(float %x) {
161 ; CHECK-LABEL: @minimum_f32_1_minimum_p0_val_fast(
162 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.minimum.f32(float [[X:%.*]], float 0.000000e+00)
163 ; CHECK-NEXT: ret float [[TMP1]]
165 %y = call float @llvm.minimum.f32(float 0.0, float %x)
166 %z = call fast float @llvm.minimum.f32(float %y, float 1.0)
170 define float @minimum_f32_1_minimum_p0_val_fmf1(float %x) {
171 ; CHECK-LABEL: @minimum_f32_1_minimum_p0_val_fmf1(
172 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.minimum.f32(float [[X:%.*]], float 0.000000e+00)
173 ; CHECK-NEXT: ret float [[TMP1]]
175 %y = call float @llvm.minimum.f32(float 0.0, float %x)
176 %z = call nnan ninf float @llvm.minimum.f32(float %y, float 1.0)
180 define float @minimum_f32_1_minimum_p0_val_fmf2(float %x) {
181 ; CHECK-LABEL: @minimum_f32_1_minimum_p0_val_fmf2(
182 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.minimum.f32(float [[X:%.*]], float 0.000000e+00)
183 ; CHECK-NEXT: ret float [[TMP1]]
185 %y = call nnan ninf float @llvm.minimum.f32(float 0.0, float %x)
186 %z = call float @llvm.minimum.f32(float %y, float 1.0)
190 define float @minimum_f32_1_minimum_p0_val_fmf3(float %x) {
191 ; CHECK-LABEL: @minimum_f32_1_minimum_p0_val_fmf3(
192 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf float @llvm.minimum.f32(float [[X:%.*]], float 0.000000e+00)
193 ; CHECK-NEXT: ret float [[TMP1]]
195 %y = call nnan ninf float @llvm.minimum.f32(float 0.0, float %x)
196 %z = call nnan ninf float @llvm.minimum.f32(float %y, float 1.0)
200 define float @minimum_f32_p0_minimum_val_n0(float %x) {
201 ; CHECK-LABEL: @minimum_f32_p0_minimum_val_n0(
202 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.minimum.f32(float [[X:%.*]], float -0.000000e+00)
203 ; CHECK-NEXT: ret float [[TMP1]]
205 %y = call float @llvm.minimum.f32(float %x, float -0.0)
206 %z = call float @llvm.minimum.f32(float %y, float 0.0)
210 define float @minimum_f32_1_minimum_p0_val(float %x) {
211 ; CHECK-LABEL: @minimum_f32_1_minimum_p0_val(
212 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.minimum.f32(float [[X:%.*]], float 0.000000e+00)
213 ; CHECK-NEXT: ret float [[TMP1]]
215 %y = call float @llvm.minimum.f32(float 0.0, float %x)
216 %z = call float @llvm.minimum.f32(float %y, float 1.0)
220 define <2 x float> @minimum_f32_1_minimum_val_p0_val_v2f32(<2 x float> %x) {
221 ; CHECK-LABEL: @minimum_f32_1_minimum_val_p0_val_v2f32(
222 ; CHECK-NEXT: [[TMP1:%.*]] = call <2 x float> @llvm.minimum.v2f32(<2 x float> [[X:%.*]], <2 x float> zeroinitializer)
223 ; CHECK-NEXT: ret <2 x float> [[TMP1]]
225 %y = call <2 x float> @llvm.minimum.v2f32(<2 x float> %x, <2 x float> zeroinitializer)
226 %z = call <2 x float> @llvm.minimum.v2f32(<2 x float> %y, <2 x float><float 1.0, float 1.0>)
230 define float @minimum4(float %x, float %y, float %z, float %w) {
231 ; CHECK-LABEL: @minimum4(
232 ; CHECK-NEXT: [[A:%.*]] = call float @llvm.minimum.f32(float [[X:%.*]], float [[Y:%.*]])
233 ; CHECK-NEXT: [[B:%.*]] = call float @llvm.minimum.f32(float [[Z:%.*]], float [[W:%.*]])
234 ; CHECK-NEXT: [[C:%.*]] = call float @llvm.minimum.f32(float [[A]], float [[B]])
235 ; CHECK-NEXT: ret float [[C]]
237 %a = call float @llvm.minimum.f32(float %x, float %y)
238 %b = call float @llvm.minimum.f32(float %z, float %w)
239 %c = call float @llvm.minimum.f32(float %a, float %b)
243 define float @minimum_x_maximum_x_y(float %x, float %y) {
244 ; CHECK-LABEL: @minimum_x_maximum_x_y(
245 ; CHECK-NEXT: [[A:%.*]] = call float @llvm.maximum.f32(float [[X:%.*]], float [[Y:%.*]])
246 ; CHECK-NEXT: [[B:%.*]] = call float @llvm.minimum.f32(float [[X]], float [[A]])
247 ; CHECK-NEXT: ret float [[B]]
249 %a = call float @llvm.maximum.f32(float %x, float %y)
250 %b = call float @llvm.minimum.f32(float %x, float %a)
254 define float @maximum_x_minimum_x_y(float %x, float %y) {
255 ; CHECK-LABEL: @maximum_x_minimum_x_y(
256 ; CHECK-NEXT: [[A:%.*]] = call float @llvm.minimum.f32(float [[X:%.*]], float [[Y:%.*]])
257 ; CHECK-NEXT: [[B:%.*]] = call float @llvm.maximum.f32(float [[X]], float [[A]])
258 ; CHECK-NEXT: ret float [[B]]
260 %a = call float @llvm.minimum.f32(float %x, float %y)
261 %b = call float @llvm.maximum.f32(float %x, float %a)
265 ; PR37405 - https://bugs.llvm.org/show_bug.cgi?id=37405
267 define double @neg_neg(double %x, double %y) {
268 ; CHECK-LABEL: @neg_neg(
269 ; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.maximum.f64(double [[X:%.*]], double [[Y:%.*]])
270 ; CHECK-NEXT: [[R:%.*]] = fneg double [[TMP1]]
271 ; CHECK-NEXT: ret double [[R]]
273 %negx = fsub double -0.0, %x
274 %negy = fsub double -0.0, %y
275 %r = call double @llvm.minimum.f64(double %negx, double %negy)
279 define double @unary_neg_neg(double %x, double %y) {
280 ; CHECK-LABEL: @unary_neg_neg(
281 ; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.maximum.f64(double [[X:%.*]], double [[Y:%.*]])
282 ; CHECK-NEXT: [[R:%.*]] = fneg double [[TMP1]]
283 ; CHECK-NEXT: ret double [[R]]
285 %negx = fneg double %x
286 %negy = fneg double %y
287 %r = call double @llvm.minimum.f64(double %negx, double %negy)
291 ; FMF is not required, but it should be propagated from the intrinsic (not the fnegs).
292 ; Also, make sure this works with vectors.
294 define <2 x double> @neg_neg_vec_fmf(<2 x double> %x, <2 x double> %y) {
295 ; CHECK-LABEL: @neg_neg_vec_fmf(
296 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf <2 x double> @llvm.maximum.v2f64(<2 x double> [[X:%.*]], <2 x double> [[Y:%.*]])
297 ; CHECK-NEXT: [[R:%.*]] = fneg nnan ninf <2 x double> [[TMP1]]
298 ; CHECK-NEXT: ret <2 x double> [[R]]
300 %negx = fsub reassoc <2 x double> <double -0.0, double -0.0>, %x
301 %negy = fsub fast <2 x double> <double -0.0, double -0.0>, %y
302 %r = call nnan ninf <2 x double> @llvm.minimum.v2f64(<2 x double> %negx, <2 x double> %negy)
306 define <2 x double> @unary_neg_neg_vec_fmf(<2 x double> %x, <2 x double> %y) {
307 ; CHECK-LABEL: @unary_neg_neg_vec_fmf(
308 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf <2 x double> @llvm.maximum.v2f64(<2 x double> [[X:%.*]], <2 x double> [[Y:%.*]])
309 ; CHECK-NEXT: [[R:%.*]] = fneg nnan ninf <2 x double> [[TMP1]]
310 ; CHECK-NEXT: ret <2 x double> [[R]]
312 %negx = fneg reassoc <2 x double> %x
313 %negy = fneg fast <2 x double> %y
314 %r = call nnan ninf <2 x double> @llvm.minimum.v2f64(<2 x double> %negx, <2 x double> %negy)
318 ; 1 extra use of an intermediate value should still allow the fold,
319 ; but 2 would require more instructions than we started with.
321 declare void @use(double)
322 define double @neg_neg_extra_use_x(double %x, double %y) {
323 ; CHECK-LABEL: @neg_neg_extra_use_x(
324 ; CHECK-NEXT: [[NEGX:%.*]] = fneg double [[X:%.*]]
325 ; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.maximum.f64(double [[X]], double [[Y:%.*]])
326 ; CHECK-NEXT: [[R:%.*]] = fneg double [[TMP1]]
327 ; CHECK-NEXT: call void @use(double [[NEGX]])
328 ; CHECK-NEXT: ret double [[R]]
330 %negx = fsub double -0.0, %x
331 %negy = fsub double -0.0, %y
332 %r = call double @llvm.minimum.f64(double %negx, double %negy)
333 call void @use(double %negx)
337 define double @unary_neg_neg_extra_use_x(double %x, double %y) {
338 ; CHECK-LABEL: @unary_neg_neg_extra_use_x(
339 ; CHECK-NEXT: [[NEGX:%.*]] = fneg double [[X:%.*]]
340 ; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.maximum.f64(double [[X]], double [[Y:%.*]])
341 ; CHECK-NEXT: [[R:%.*]] = fneg double [[TMP1]]
342 ; CHECK-NEXT: call void @use(double [[NEGX]])
343 ; CHECK-NEXT: ret double [[R]]
345 %negx = fneg double %x
346 %negy = fneg double %y
347 %r = call double @llvm.minimum.f64(double %negx, double %negy)
348 call void @use(double %negx)
352 define double @neg_neg_extra_use_y(double %x, double %y) {
353 ; CHECK-LABEL: @neg_neg_extra_use_y(
354 ; CHECK-NEXT: [[NEGY:%.*]] = fneg double [[Y:%.*]]
355 ; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.maximum.f64(double [[X:%.*]], double [[Y]])
356 ; CHECK-NEXT: [[R:%.*]] = fneg double [[TMP1]]
357 ; CHECK-NEXT: call void @use(double [[NEGY]])
358 ; CHECK-NEXT: ret double [[R]]
360 %negx = fsub double -0.0, %x
361 %negy = fsub double -0.0, %y
362 %r = call double @llvm.minimum.f64(double %negx, double %negy)
363 call void @use(double %negy)
367 define double @unary_neg_neg_extra_use_y(double %x, double %y) {
368 ; CHECK-LABEL: @unary_neg_neg_extra_use_y(
369 ; CHECK-NEXT: [[NEGY:%.*]] = fneg double [[Y:%.*]]
370 ; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.maximum.f64(double [[X:%.*]], double [[Y]])
371 ; CHECK-NEXT: [[R:%.*]] = fneg double [[TMP1]]
372 ; CHECK-NEXT: call void @use(double [[NEGY]])
373 ; CHECK-NEXT: ret double [[R]]
375 %negx = fneg double %x
376 %negy = fneg double %y
377 %r = call double @llvm.minimum.f64(double %negx, double %negy)
378 call void @use(double %negy)
382 define double @neg_neg_extra_use_x_and_y(double %x, double %y) {
383 ; CHECK-LABEL: @neg_neg_extra_use_x_and_y(
384 ; CHECK-NEXT: [[NEGX:%.*]] = fneg double [[X:%.*]]
385 ; CHECK-NEXT: [[NEGY:%.*]] = fneg double [[Y:%.*]]
386 ; CHECK-NEXT: [[R:%.*]] = call double @llvm.minimum.f64(double [[NEGX]], double [[NEGY]])
387 ; CHECK-NEXT: call void @use(double [[NEGX]])
388 ; CHECK-NEXT: call void @use(double [[NEGY]])
389 ; CHECK-NEXT: ret double [[R]]
391 %negx = fsub double -0.0, %x
392 %negy = fsub double -0.0, %y
393 %r = call double @llvm.minimum.f64(double %negx, double %negy)
394 call void @use(double %negx)
395 call void @use(double %negy)
399 define double @unary_neg_neg_extra_use_x_and_y(double %x, double %y) {
400 ; CHECK-LABEL: @unary_neg_neg_extra_use_x_and_y(
401 ; CHECK-NEXT: [[NEGX:%.*]] = fneg double [[X:%.*]]
402 ; CHECK-NEXT: [[NEGY:%.*]] = fneg double [[Y:%.*]]
403 ; CHECK-NEXT: [[R:%.*]] = call double @llvm.minimum.f64(double [[NEGX]], double [[NEGY]])
404 ; CHECK-NEXT: call void @use(double [[NEGX]])
405 ; CHECK-NEXT: call void @use(double [[NEGY]])
406 ; CHECK-NEXT: ret double [[R]]
408 %negx = fneg double %x
409 %negy = fneg double %y
410 %r = call double @llvm.minimum.f64(double %negx, double %negy)
411 call void @use(double %negx)
412 call void @use(double %negy)
416 define float @reduce_precision(float %x, float %y) {
417 ; CHECK-LABEL: @reduce_precision(
418 ; CHECK-NEXT: [[MINIMUM:%.*]] = call float @llvm.minimum.f32(float [[X:%.*]], float [[Y:%.*]])
419 ; CHECK-NEXT: ret float [[MINIMUM]]
421 %x.ext = fpext float %x to double
422 %y.ext = fpext float %y to double
423 %minimum = call double @llvm.minimum.f64(double %x.ext, double %y.ext)
424 %trunc = fptrunc double %minimum to float
428 define float @reduce_precision_fmf(float %x, float %y) {
429 ; CHECK-LABEL: @reduce_precision_fmf(
430 ; CHECK-NEXT: [[MINIMUM:%.*]] = call nnan float @llvm.minimum.f32(float [[X:%.*]], float [[Y:%.*]])
431 ; CHECK-NEXT: ret float [[MINIMUM]]
433 %x.ext = fpext float %x to double
434 %y.ext = fpext float %y to double
435 %minimum = call nnan double @llvm.minimum.f64(double %x.ext, double %y.ext)
436 %trunc = fptrunc double %minimum to float
440 define float @negated_op(float %x) {
441 ; CHECK-LABEL: @negated_op(
442 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.fabs.f32(float [[X:%.*]])
443 ; CHECK-NEXT: [[TMP2:%.*]] = fneg float [[TMP1]]
444 ; CHECK-NEXT: ret float [[TMP2]]
446 %negx = fneg float %x
447 %r = call float @llvm.minimum.f32(float %x, float %negx)
451 define <2 x double> @negated_op_fmf_commute_vec(<2 x double> %x) {
452 ; CHECK-LABEL: @negated_op_fmf_commute_vec(
453 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf nsz <2 x double> @llvm.fabs.v2f64(<2 x double> [[X:%.*]])
454 ; CHECK-NEXT: [[TMP2:%.*]] = fneg nnan ninf nsz <2 x double> [[TMP1]]
455 ; CHECK-NEXT: ret <2 x double> [[TMP2]]
457 %negx = fneg <2 x double> %x
458 %r = call nsz nnan ninf <2 x double> @llvm.minimum.v2f64(<2 x double> %negx, <2 x double> %x)
462 define double @negated_op_extra_use(double %x) {
463 ; CHECK-LABEL: @negated_op_extra_use(
464 ; CHECK-NEXT: [[NEGX:%.*]] = fneg double [[X:%.*]]
465 ; CHECK-NEXT: call void @use(double [[NEGX]])
466 ; CHECK-NEXT: [[R:%.*]] = call double @llvm.minimum.f64(double [[NEGX]], double [[X]])
467 ; CHECK-NEXT: ret double [[R]]
469 %negx = fneg double %x
470 call void @use(double %negx)
471 %r = call double @llvm.minimum.f64(double %negx, double %x)