• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -S -instcombine < %s | FileCheck %s
3
4declare float @llvm.minnum.f32(float, float)
5declare float @llvm.minnum.v2f32(<2 x float>, <2 x float>)
6declare <4 x float> @llvm.minnum.v4f32(<4 x float>, <4 x float>)
7
8declare double @llvm.minnum.f64(double, double)
9declare <2 x double> @llvm.minnum.v2f64(<2 x double>, <2 x double>)
10
11declare float @llvm.maxnum.f32(float, float)
12
13define float @constant_fold_minnum_f32() {
14; CHECK-LABEL: @constant_fold_minnum_f32(
15; CHECK-NEXT:    ret float 1.000000e+00
16;
17  %x = call float @llvm.minnum.f32(float 1.0, float 2.0)
18  ret float %x
19}
20
21define float @constant_fold_minnum_f32_inv() {
22; CHECK-LABEL: @constant_fold_minnum_f32_inv(
23; CHECK-NEXT:    ret float 1.000000e+00
24;
25  %x = call float @llvm.minnum.f32(float 2.0, float 1.0)
26  ret float %x
27}
28
29define float @constant_fold_minnum_f32_nan0() {
30; CHECK-LABEL: @constant_fold_minnum_f32_nan0(
31; CHECK-NEXT:    ret float 2.000000e+00
32;
33  %x = call float @llvm.minnum.f32(float 0x7FF8000000000000, float 2.0)
34  ret float %x
35}
36
37define float @constant_fold_minnum_f32_nan1() {
38; CHECK-LABEL: @constant_fold_minnum_f32_nan1(
39; CHECK-NEXT:    ret float 2.000000e+00
40;
41  %x = call float @llvm.minnum.f32(float 2.0, float 0x7FF8000000000000)
42  ret float %x
43}
44
45define float @constant_fold_minnum_f32_nan_nan() {
46; CHECK-LABEL: @constant_fold_minnum_f32_nan_nan(
47; CHECK-NEXT:    ret float 0x7FF8000000000000
48;
49  %x = call float @llvm.minnum.f32(float 0x7FF8000000000000, float 0x7FF8000000000000)
50  ret float %x
51}
52
53define float @constant_fold_minnum_f32_p0_p0() {
54; CHECK-LABEL: @constant_fold_minnum_f32_p0_p0(
55; CHECK-NEXT:    ret float 0.000000e+00
56;
57  %x = call float @llvm.minnum.f32(float 0.0, float 0.0)
58  ret float %x
59}
60
61define float @constant_fold_minnum_f32_p0_n0() {
62; CHECK-LABEL: @constant_fold_minnum_f32_p0_n0(
63; CHECK-NEXT:    ret float 0.000000e+00
64;
65  %x = call float @llvm.minnum.f32(float 0.0, float -0.0)
66  ret float %x
67}
68
69define float @constant_fold_minnum_f32_n0_p0() {
70; CHECK-LABEL: @constant_fold_minnum_f32_n0_p0(
71; CHECK-NEXT:    ret float -0.000000e+00
72;
73  %x = call float @llvm.minnum.f32(float -0.0, float 0.0)
74  ret float %x
75}
76
77define float @constant_fold_minnum_f32_n0_n0() {
78; CHECK-LABEL: @constant_fold_minnum_f32_n0_n0(
79; CHECK-NEXT:    ret float -0.000000e+00
80;
81  %x = call float @llvm.minnum.f32(float -0.0, float -0.0)
82  ret float %x
83}
84
85define <4 x float> @constant_fold_minnum_v4f32() {
86; CHECK-LABEL: @constant_fold_minnum_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.minnum.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_minnum_f64() {
94; CHECK-LABEL: @constant_fold_minnum_f64(
95; CHECK-NEXT:    ret double 1.000000e+00
96;
97  %x = call double @llvm.minnum.f64(double 1.0, double 2.0)
98  ret double %x
99}
100
101define double @constant_fold_minnum_f64_nan0() {
102; CHECK-LABEL: @constant_fold_minnum_f64_nan0(
103; CHECK-NEXT:    ret double 2.000000e+00
104;
105  %x = call double @llvm.minnum.f64(double 0x7FF8000000000000, double 2.0)
106  ret double %x
107}
108
109define double @constant_fold_minnum_f64_nan1() {
110; CHECK-LABEL: @constant_fold_minnum_f64_nan1(
111; CHECK-NEXT:    ret double 2.000000e+00
112;
113  %x = call double @llvm.minnum.f64(double 2.0, double 0x7FF8000000000000)
114  ret double %x
115}
116
117define double @constant_fold_minnum_f64_nan_nan() {
118; CHECK-LABEL: @constant_fold_minnum_f64_nan_nan(
119; CHECK-NEXT:    ret double 0x7FF8000000000000
120;
121  %x = call double @llvm.minnum.f64(double 0x7FF8000000000000, double 0x7FF8000000000000)
122  ret double %x
123}
124
125define float @canonicalize_constant_minnum_f32(float %x) {
126; CHECK-LABEL: @canonicalize_constant_minnum_f32(
127; CHECK-NEXT:    [[Y:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 1.000000e+00)
128; CHECK-NEXT:    ret float [[Y]]
129;
130  %y = call float @llvm.minnum.f32(float 1.0, float %x)
131  ret float %y
132}
133
134define float @noop_minnum_f32(float %x) {
135; CHECK-LABEL: @noop_minnum_f32(
136; CHECK-NEXT:    ret float [[X:%.*]]
137;
138  %y = call float @llvm.minnum.f32(float %x, float %x)
139  ret float %y
140}
141
142define float @minnum_f32_nan_val(float %x) {
143; CHECK-LABEL: @minnum_f32_nan_val(
144; CHECK-NEXT:    ret float [[X:%.*]]
145;
146  %y = call float @llvm.minnum.f32(float 0x7FF8000000000000, float %x)
147  ret float %y
148}
149
150define float @minnum_f32_val_nan(float %x) {
151; CHECK-LABEL: @minnum_f32_val_nan(
152; CHECK-NEXT:    ret float [[X:%.*]]
153;
154  %y = call float @llvm.minnum.f32(float %x, float 0x7FF8000000000000)
155  ret float %y
156}
157
158define float @fold_minnum_f32_undef_undef(float %x) {
159; CHECK-LABEL: @fold_minnum_f32_undef_undef(
160; CHECK-NEXT:    ret float undef
161;
162  %val = call float @llvm.minnum.f32(float undef, float undef)
163  ret float %val
164}
165
166define float @fold_minnum_f32_val_undef(float %x) {
167; CHECK-LABEL: @fold_minnum_f32_val_undef(
168; CHECK-NEXT:    ret float [[X:%.*]]
169;
170  %val = call float @llvm.minnum.f32(float %x, float undef)
171  ret float %val
172}
173
174define float @fold_minnum_f32_undef_val(float %x) {
175; CHECK-LABEL: @fold_minnum_f32_undef_val(
176; CHECK-NEXT:    ret float [[X:%.*]]
177;
178  %val = call float @llvm.minnum.f32(float undef, float %x)
179  ret float %val
180}
181
182define float @minnum_x_minnum_x_y(float %x, float %y) {
183; CHECK-LABEL: @minnum_x_minnum_x_y(
184; CHECK-NEXT:    [[A:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float [[Y:%.*]])
185; CHECK-NEXT:    ret float [[A]]
186;
187  %a = call float @llvm.minnum.f32(float %x, float %y)
188  %b = call float @llvm.minnum.f32(float %x, float %a)
189  ret float %b
190}
191
192define float @minnum_y_minnum_x_y(float %x, float %y) {
193; CHECK-LABEL: @minnum_y_minnum_x_y(
194; CHECK-NEXT:    [[A:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float [[Y:%.*]])
195; CHECK-NEXT:    ret float [[A]]
196;
197  %a = call float @llvm.minnum.f32(float %x, float %y)
198  %b = call float @llvm.minnum.f32(float %y, float %a)
199  ret float %b
200}
201
202define float @minnum_z_minnum_x_y(float %x, float %y, float %z) {
203; CHECK-LABEL: @minnum_z_minnum_x_y(
204; CHECK-NEXT:    [[A:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float [[Y:%.*]])
205; CHECK-NEXT:    [[B:%.*]] = call float @llvm.minnum.f32(float [[Z:%.*]], float [[A]])
206; CHECK-NEXT:    ret float [[B]]
207;
208  %a = call float @llvm.minnum.f32(float %x, float %y)
209  %b = call float @llvm.minnum.f32(float %z, float %a)
210  ret float %b
211}
212
213define float @minnum_minnum_x_y_z(float %x, float %y, float %z) {
214; CHECK-LABEL: @minnum_minnum_x_y_z(
215; CHECK-NEXT:    [[A:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float [[Y:%.*]])
216; CHECK-NEXT:    [[B:%.*]] = call float @llvm.minnum.f32(float [[A]], float [[Z:%.*]])
217; CHECK-NEXT:    ret float [[B]]
218;
219  %a = call float @llvm.minnum.f32(float %x, float %y)
220  %b = call float @llvm.minnum.f32(float %a, float %z)
221  ret float %b
222}
223
224define float @minnum4(float %x, float %y, float %z, float %w) {
225; CHECK-LABEL: @minnum4(
226; CHECK-NEXT:    [[A:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float [[Y:%.*]])
227; CHECK-NEXT:    [[B:%.*]] = call float @llvm.minnum.f32(float [[Z:%.*]], float [[W:%.*]])
228; CHECK-NEXT:    [[C:%.*]] = call float @llvm.minnum.f32(float [[A]], float [[B]])
229; CHECK-NEXT:    ret float [[C]]
230;
231  %a = call float @llvm.minnum.f32(float %x, float %y)
232  %b = call float @llvm.minnum.f32(float %z, float %w)
233  %c = call float @llvm.minnum.f32(float %a, float %b)
234  ret float %c
235}
236
237define float @minnum_x_maxnum_x_y(float %x, float %y) {
238; CHECK-LABEL: @minnum_x_maxnum_x_y(
239; CHECK-NEXT:    [[A:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float [[Y:%.*]])
240; CHECK-NEXT:    [[B:%.*]] = call float @llvm.minnum.f32(float [[X]], float [[A]])
241; CHECK-NEXT:    ret float [[B]]
242;
243  %a = call float @llvm.maxnum.f32(float %x, float %y)
244  %b = call float @llvm.minnum.f32(float %x, float %a)
245  ret float %b
246}
247
248define float @maxnum_x_minnum_x_y(float %x, float %y) {
249; CHECK-LABEL: @maxnum_x_minnum_x_y(
250; CHECK-NEXT:    [[A:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float [[Y:%.*]])
251; CHECK-NEXT:    [[B:%.*]] = call float @llvm.maxnum.f32(float [[X]], float [[A]])
252; CHECK-NEXT:    ret float [[B]]
253;
254  %a = call float @llvm.minnum.f32(float %x, float %y)
255  %b = call float @llvm.maxnum.f32(float %x, float %a)
256  ret float %b
257}
258
259define float @fold_minnum_f32_inf_val(float %x) {
260; CHECK-LABEL: @fold_minnum_f32_inf_val(
261; CHECK-NEXT:    [[VAL:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 0x7FF0000000000000)
262; CHECK-NEXT:    ret float [[VAL]]
263;
264  %val = call float @llvm.minnum.f32(float 0x7FF0000000000000, float %x)
265  ret float %val
266}
267
268define float @fold_minnum_f32_minf_val(float %x) {
269; CHECK-LABEL: @fold_minnum_f32_minf_val(
270; CHECK-NEXT:    ret float 0xFFF0000000000000
271;
272  %val = call float @llvm.minnum.f32(float 0xFFF0000000000000, float %x)
273  ret float %val
274}
275
276; PR37405 - https://bugs.llvm.org/show_bug.cgi?id=37405
277
278define double @neg_neg(double %x, double %y) {
279; CHECK-LABEL: @neg_neg(
280; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.maxnum.f64(double [[X:%.*]], double [[Y:%.*]])
281; CHECK-NEXT:    [[R:%.*]] = fsub double -0.000000e+00, [[TMP1]]
282; CHECK-NEXT:    ret double [[R]]
283;
284  %negx = fsub double -0.0, %x
285  %negy = fsub double -0.0, %y
286  %r = call double @llvm.minnum.f64(double %negx, double %negy)
287  ret double %r
288}
289
290; FMF is not required, but it should be propagated from the intrinsic (not the fnegs).
291; Also, make sure this works with vectors.
292
293define <2 x double> @neg_neg_vec_fmf(<2 x double> %x, <2 x double> %y) {
294; CHECK-LABEL: @neg_neg_vec_fmf(
295; CHECK-NEXT:    [[TMP1:%.*]] = call nnan ninf <2 x double> @llvm.maxnum.v2f64(<2 x double> [[X:%.*]], <2 x double> [[Y:%.*]])
296; CHECK-NEXT:    [[R:%.*]] = fsub nnan ninf <2 x double> <double -0.000000e+00, double -0.000000e+00>, [[TMP1]]
297; CHECK-NEXT:    ret <2 x double> [[R]]
298;
299  %negx = fsub reassoc <2 x double> <double -0.0, double -0.0>, %x
300  %negy = fsub fast <2 x double> <double -0.0, double -0.0>, %y
301  %r = call nnan ninf <2 x double> @llvm.minnum.v2f64(<2 x double> %negx, <2 x double> %negy)
302  ret <2 x double> %r
303}
304
305; 1 extra use of an intermediate value should still allow the fold,
306; but 2 would require more instructions than we started with.
307
308declare void @use(double)
309define double @neg_neg_extra_use_x(double %x, double %y) {
310; CHECK-LABEL: @neg_neg_extra_use_x(
311; CHECK-NEXT:    [[NEGX:%.*]] = fsub double -0.000000e+00, [[X:%.*]]
312; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.maxnum.f64(double [[X]], double [[Y:%.*]])
313; CHECK-NEXT:    [[R:%.*]] = fsub double -0.000000e+00, [[TMP1]]
314; CHECK-NEXT:    call void @use(double [[NEGX]])
315; CHECK-NEXT:    ret double [[R]]
316;
317  %negx = fsub double -0.0, %x
318  %negy = fsub double -0.0, %y
319  %r = call double @llvm.minnum.f64(double %negx, double %negy)
320  call void @use(double %negx)
321  ret double %r
322}
323
324define double @neg_neg_extra_use_y(double %x, double %y) {
325; CHECK-LABEL: @neg_neg_extra_use_y(
326; CHECK-NEXT:    [[NEGY:%.*]] = fsub double -0.000000e+00, [[Y:%.*]]
327; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.maxnum.f64(double [[X:%.*]], double [[Y]])
328; CHECK-NEXT:    [[R:%.*]] = fsub double -0.000000e+00, [[TMP1]]
329; CHECK-NEXT:    call void @use(double [[NEGY]])
330; CHECK-NEXT:    ret double [[R]]
331;
332  %negx = fsub double -0.0, %x
333  %negy = fsub double -0.0, %y
334  %r = call double @llvm.minnum.f64(double %negx, double %negy)
335  call void @use(double %negy)
336  ret double %r
337}
338
339define double @neg_neg_extra_use_x_and_y(double %x, double %y) {
340; CHECK-LABEL: @neg_neg_extra_use_x_and_y(
341; CHECK-NEXT:    [[NEGX:%.*]] = fsub double -0.000000e+00, [[X:%.*]]
342; CHECK-NEXT:    [[NEGY:%.*]] = fsub double -0.000000e+00, [[Y:%.*]]
343; CHECK-NEXT:    [[R:%.*]] = call double @llvm.minnum.f64(double [[NEGX]], double [[NEGY]])
344; CHECK-NEXT:    call void @use(double [[NEGX]])
345; CHECK-NEXT:    call void @use(double [[NEGY]])
346; CHECK-NEXT:    ret double [[R]]
347;
348  %negx = fsub double -0.0, %x
349  %negy = fsub double -0.0, %y
350  %r = call double @llvm.minnum.f64(double %negx, double %negy)
351  call void @use(double %negx)
352  call void @use(double %negy)
353  ret double %r
354}
355
356