1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -S -instcombine < %s | FileCheck %s
3
4declare float @llvm.minimum.f32(float, float)
5declare <2 x float> @llvm.minimum.v2f32(<2 x float>, <2 x float>)
6declare <4 x float> @llvm.minimum.v4f32(<4 x float>, <4 x float>)
7
8declare double @llvm.minimum.f64(double, double)
9declare <2 x double> @llvm.minimum.v2f64(<2 x double>, <2 x double>)
10
11declare float @llvm.maximum.f32(float, float)
12
13define float @constant_fold_minimum_f32() {
14; CHECK-LABEL: @constant_fold_minimum_f32(
15; CHECK-NEXT:    ret float 1.000000e+00
16;
17  %x = call float @llvm.minimum.f32(float 1.0, float 2.0)
18  ret float %x
19}
20
21define float @constant_fold_minimum_f32_inv() {
22; CHECK-LABEL: @constant_fold_minimum_f32_inv(
23; CHECK-NEXT:    ret float 1.000000e+00
24;
25  %x = call float @llvm.minimum.f32(float 2.0, float 1.0)
26  ret float %x
27}
28
29define float @constant_fold_minimum_f32_nan0() {
30; CHECK-LABEL: @constant_fold_minimum_f32_nan0(
31; CHECK-NEXT:    ret float 0x7FF8000000000000
32;
33  %x = call float @llvm.minimum.f32(float 0x7FF8000000000000, float 2.0)
34  ret float %x
35}
36
37define float @constant_fold_minimum_f32_nan1() {
38; CHECK-LABEL: @constant_fold_minimum_f32_nan1(
39; CHECK-NEXT:    ret float 0x7FF8000000000000
40;
41  %x = call float @llvm.minimum.f32(float 2.0, float 0x7FF8000000000000)
42  ret float %x
43}
44
45define float @constant_fold_minimum_f32_nan_nan() {
46; CHECK-LABEL: @constant_fold_minimum_f32_nan_nan(
47; CHECK-NEXT:    ret float 0x7FF8000000000000
48;
49  %x = call float @llvm.minimum.f32(float 0x7FF8000000000000, float 0x7FF8000000000000)
50  ret float %x
51}
52
53define float @constant_fold_minimum_f32_p0_p0() {
54; CHECK-LABEL: @constant_fold_minimum_f32_p0_p0(
55; CHECK-NEXT:    ret float 0.000000e+00
56;
57  %x = call float @llvm.minimum.f32(float 0.0, float 0.0)
58  ret float %x
59}
60
61define float @constant_fold_minimum_f32_p0_n0() {
62; CHECK-LABEL: @constant_fold_minimum_f32_p0_n0(
63; CHECK-NEXT:    ret float -0.000000e+00
64;
65  %x = call float @llvm.minimum.f32(float 0.0, float -0.0)
66  ret float %x
67}
68
69define float @constant_fold_minimum_f32_n0_p0() {
70; CHECK-LABEL: @constant_fold_minimum_f32_n0_p0(
71; CHECK-NEXT:    ret float -0.000000e+00
72;
73  %x = call float @llvm.minimum.f32(float -0.0, float 0.0)
74  ret float %x
75}
76
77define float @constant_fold_minimum_f32_n0_n0() {
78; CHECK-LABEL: @constant_fold_minimum_f32_n0_n0(
79; CHECK-NEXT:    ret float -0.000000e+00
80;
81  %x = call float @llvm.minimum.f32(float -0.0, float -0.0)
82  ret float %x
83}
84
85define <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>
88;
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>)
90  ret <4 x float> %x
91}
92
93define double @constant_fold_minimum_f64() {
94; CHECK-LABEL: @constant_fold_minimum_f64(
95; CHECK-NEXT:    ret double 1.000000e+00
96;
97  %x = call double @llvm.minimum.f64(double 1.0, double 2.0)
98  ret double %x
99}
100
101define double @constant_fold_minimum_f64_nan0() {
102; CHECK-LABEL: @constant_fold_minimum_f64_nan0(
103; CHECK-NEXT:    ret double 0x7FF8000000000000
104;
105  %x = call double @llvm.minimum.f64(double 0x7FF8000000000000, double 2.0)
106  ret double %x
107}
108
109define double @constant_fold_minimum_f64_nan1() {
110; CHECK-LABEL: @constant_fold_minimum_f64_nan1(
111; CHECK-NEXT:    ret double 0x7FF8000000000000
112;
113  %x = call double @llvm.minimum.f64(double 2.0, double 0x7FF8000000000000)
114  ret double %x
115}
116
117define double @constant_fold_minimum_f64_nan_nan() {
118; CHECK-LABEL: @constant_fold_minimum_f64_nan_nan(
119; CHECK-NEXT:    ret double 0x7FF8000000000000
120;
121  %x = call double @llvm.minimum.f64(double 0x7FF8000000000000, double 0x7FF8000000000000)
122  ret double %x
123}
124
125define 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]]
129;
130  %y = call float @llvm.minimum.f32(float 1.0, float %x)
131  ret float %y
132}
133
134define float @minimum_f32_nan_val(float %x) {
135; CHECK-LABEL: @minimum_f32_nan_val(
136; CHECK-NEXT:    ret float 0x7FF8000000000000
137;
138  %y = call float @llvm.minimum.f32(float 0x7FF8000000000000, float %x)
139  ret float %y
140}
141
142define float @minimum_f32_val_nan(float %x) {
143; CHECK-LABEL: @minimum_f32_val_nan(
144; CHECK-NEXT:    ret float 0x7FF8000000000000
145;
146  %y = call float @llvm.minimum.f32(float %x, float 0x7FF8000000000000)
147  ret float %y
148}
149
150define 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]]
154;
155  %y = call float @llvm.minimum.f32(float %x, float 0.0)
156  %z = call float @llvm.minimum.f32(float %y, float 1.0)
157  ret float %z
158}
159
160define 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]]
164;
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)
167  ret float %z
168}
169
170define 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]]
174;
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)
177  ret float %z
178}
179
180define 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]]
184;
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)
187  ret float %z
188}
189
190define 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]]
194;
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)
197  ret float %z
198}
199
200define 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]]
204;
205  %y = call float @llvm.minimum.f32(float %x, float -0.0)
206  %z = call float @llvm.minimum.f32(float %y, float 0.0)
207  ret float %z
208}
209
210define 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]]
214;
215  %y = call float @llvm.minimum.f32(float 0.0, float %x)
216  %z = call float @llvm.minimum.f32(float %y, float 1.0)
217  ret float %z
218}
219
220define <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]]
224;
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>)
227  ret <2 x float> %z
228}
229
230define 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]]
236;
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)
240  ret float %c
241}
242
243define 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]]
248;
249  %a = call float @llvm.maximum.f32(float %x, float %y)
250  %b = call float @llvm.minimum.f32(float %x, float %a)
251  ret float %b
252}
253
254define 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]]
259;
260  %a = call float @llvm.minimum.f32(float %x, float %y)
261  %b = call float @llvm.maximum.f32(float %x, float %a)
262  ret float %b
263}
264
265; PR37405 - https://bugs.llvm.org/show_bug.cgi?id=37405
266
267define 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]]
272;
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)
276  ret double %r
277}
278
279define 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]]
284;
285  %negx = fneg double %x
286  %negy = fneg double %y
287  %r = call double @llvm.minimum.f64(double %negx, double %negy)
288  ret double %r
289}
290
291; FMF is not required, but it should be propagated from the intrinsic (not the fnegs).
292; Also, make sure this works with vectors.
293
294define <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]]
299;
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)
303  ret <2 x double> %r
304}
305
306define <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]]
311;
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)
315  ret <2 x double> %r
316}
317
318; 1 extra use of an intermediate value should still allow the fold,
319; but 2 would require more instructions than we started with.
320
321declare void @use(double)
322define 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]]
329;
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)
334  ret double %r
335}
336
337define 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]]
344;
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)
349  ret double %r
350}
351
352define 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]]
359;
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)
364  ret double %r
365}
366
367define 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]]
374;
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)
379  ret double %r
380}
381
382define 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]]
390;
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)
396  ret double %r
397}
398
399define 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]]
407;
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)
413  ret double %r
414}
415
416define 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]]
420;
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
425  ret float %trunc
426}
427
428define 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]]
432;
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
437  ret float %trunc
438}
439