1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -instcombine < %s | FileCheck %s
4 declare float @llvm.minnum.f32(float, float)
5 declare <2 x float> @llvm.minnum.v2f32(<2 x float>, <2 x float>)
6 declare <4 x float> @llvm.minnum.v4f32(<4 x float>, <4 x float>)
8 declare double @llvm.minnum.f64(double, double)
9 declare <2 x double> @llvm.minnum.v2f64(<2 x double>, <2 x double>)
11 declare float @llvm.maxnum.f32(float, float)
13 define float @constant_fold_minnum_f32() {
14 ; CHECK-LABEL: @constant_fold_minnum_f32(
15 ; CHECK-NEXT: ret float 1.000000e+00
17 %x = call float @llvm.minnum.f32(float 1.0, float 2.0)
21 define float @constant_fold_minnum_f32_inv() {
22 ; CHECK-LABEL: @constant_fold_minnum_f32_inv(
23 ; CHECK-NEXT: ret float 1.000000e+00
25 %x = call float @llvm.minnum.f32(float 2.0, float 1.0)
29 define float @constant_fold_minnum_f32_nan0() {
30 ; CHECK-LABEL: @constant_fold_minnum_f32_nan0(
31 ; CHECK-NEXT: ret float 2.000000e+00
33 %x = call float @llvm.minnum.f32(float 0x7FF8000000000000, float 2.0)
37 define float @constant_fold_minnum_f32_nan1() {
38 ; CHECK-LABEL: @constant_fold_minnum_f32_nan1(
39 ; CHECK-NEXT: ret float 2.000000e+00
41 %x = call float @llvm.minnum.f32(float 2.0, float 0x7FF8000000000000)
45 define float @constant_fold_minnum_f32_nan_nan() {
46 ; CHECK-LABEL: @constant_fold_minnum_f32_nan_nan(
47 ; CHECK-NEXT: ret float 0x7FF8000000000000
49 %x = call float @llvm.minnum.f32(float 0x7FF8000000000000, float 0x7FF8000000000000)
53 define float @constant_fold_minnum_f32_p0_p0() {
54 ; CHECK-LABEL: @constant_fold_minnum_f32_p0_p0(
55 ; CHECK-NEXT: ret float 0.000000e+00
57 %x = call float @llvm.minnum.f32(float 0.0, float 0.0)
61 define float @constant_fold_minnum_f32_p0_n0() {
62 ; CHECK-LABEL: @constant_fold_minnum_f32_p0_n0(
63 ; CHECK-NEXT: ret float 0.000000e+00
65 %x = call float @llvm.minnum.f32(float 0.0, float -0.0)
69 define float @constant_fold_minnum_f32_n0_p0() {
70 ; CHECK-LABEL: @constant_fold_minnum_f32_n0_p0(
71 ; CHECK-NEXT: ret float -0.000000e+00
73 %x = call float @llvm.minnum.f32(float -0.0, float 0.0)
77 define float @constant_fold_minnum_f32_n0_n0() {
78 ; CHECK-LABEL: @constant_fold_minnum_f32_n0_n0(
79 ; CHECK-NEXT: ret float -0.000000e+00
81 %x = call float @llvm.minnum.f32(float -0.0, float -0.0)
85 define <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>
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>)
93 define double @constant_fold_minnum_f64() {
94 ; CHECK-LABEL: @constant_fold_minnum_f64(
95 ; CHECK-NEXT: ret double 1.000000e+00
97 %x = call double @llvm.minnum.f64(double 1.0, double 2.0)
101 define double @constant_fold_minnum_f64_nan0() {
102 ; CHECK-LABEL: @constant_fold_minnum_f64_nan0(
103 ; CHECK-NEXT: ret double 2.000000e+00
105 %x = call double @llvm.minnum.f64(double 0x7FF8000000000000, double 2.0)
109 define double @constant_fold_minnum_f64_nan1() {
110 ; CHECK-LABEL: @constant_fold_minnum_f64_nan1(
111 ; CHECK-NEXT: ret double 2.000000e+00
113 %x = call double @llvm.minnum.f64(double 2.0, double 0x7FF8000000000000)
117 define double @constant_fold_minnum_f64_nan_nan() {
118 ; CHECK-LABEL: @constant_fold_minnum_f64_nan_nan(
119 ; CHECK-NEXT: ret double 0x7FF8000000000000
121 %x = call double @llvm.minnum.f64(double 0x7FF8000000000000, double 0x7FF8000000000000)
125 define 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]]
130 %y = call float @llvm.minnum.f32(float 1.0, float %x)
134 define float @minnum_f32_nan_val(float %x) {
135 ; CHECK-LABEL: @minnum_f32_nan_val(
136 ; CHECK-NEXT: ret float [[X:%.*]]
138 %y = call float @llvm.minnum.f32(float 0x7FF8000000000000, float %x)
142 define float @minnum_f32_val_nan(float %x) {
143 ; CHECK-LABEL: @minnum_f32_val_nan(
144 ; CHECK-NEXT: ret float [[X:%.*]]
146 %y = call float @llvm.minnum.f32(float %x, float 0x7FF8000000000000)
150 define float @minnum_f32_1_minnum_val_p0(float %x) {
151 ; CHECK-LABEL: @minnum_f32_1_minnum_val_p0(
152 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 0.000000e+00)
153 ; CHECK-NEXT: ret float [[TMP1]]
155 %y = call float @llvm.minnum.f32(float %x, float 0.0)
156 %z = call float @llvm.minnum.f32(float %y, float 1.0)
160 define float @minnum_f32_1_minnum_p0_val_fast(float %x) {
161 ; CHECK-LABEL: @minnum_f32_1_minnum_p0_val_fast(
162 ; CHECK-NEXT: [[TMP1:%.*]] = call fast float @llvm.minnum.f32(float [[X:%.*]], float 0.000000e+00)
163 ; CHECK-NEXT: ret float [[TMP1]]
165 %y = call float @llvm.minnum.f32(float 0.0, float %x)
166 %z = call fast float @llvm.minnum.f32(float %y, float 1.0)
170 define float @minnum_f32_1_minnum_p0_val_nnan_ninf(float %x) {
171 ; CHECK-LABEL: @minnum_f32_1_minnum_p0_val_nnan_ninf(
172 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf float @llvm.minnum.f32(float [[X:%.*]], float 0.000000e+00)
173 ; CHECK-NEXT: ret float [[TMP1]]
175 %y = call float @llvm.minnum.f32(float 0.0, float %x)
176 %z = call nnan ninf float @llvm.minnum.f32(float %y, float 1.0)
180 define float @minnum_f32_p0_minnum_val_n0(float %x) {
181 ; CHECK-LABEL: @minnum_f32_p0_minnum_val_n0(
182 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 0.000000e+00)
183 ; CHECK-NEXT: ret float [[TMP1]]
185 %y = call float @llvm.minnum.f32(float %x, float -0.0)
186 %z = call float @llvm.minnum.f32(float %y, float 0.0)
190 define float @minnum_f32_1_minnum_p0_val(float %x) {
191 ; CHECK-LABEL: @minnum_f32_1_minnum_p0_val(
192 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 0.000000e+00)
193 ; CHECK-NEXT: ret float [[TMP1]]
195 %y = call float @llvm.minnum.f32(float 0.0, float %x)
196 %z = call float @llvm.minnum.f32(float %y, float 1.0)
200 define <2 x float> @minnum_f32_1_minnum_val_p0_val_v2f32(<2 x float> %x) {
201 ; CHECK-LABEL: @minnum_f32_1_minnum_val_p0_val_v2f32(
202 ; CHECK-NEXT: [[TMP1:%.*]] = call <2 x float> @llvm.minnum.v2f32(<2 x float> [[X:%.*]], <2 x float> zeroinitializer)
203 ; CHECK-NEXT: ret <2 x float> [[TMP1]]
205 %y = call <2 x float> @llvm.minnum.v2f32(<2 x float> %x, <2 x float> zeroinitializer)
206 %z = call <2 x float> @llvm.minnum.v2f32(<2 x float> %y, <2 x float><float 1.0, float 1.0>)
210 define float @minnum4(float %x, float %y, float %z, float %w) {
211 ; CHECK-LABEL: @minnum4(
212 ; CHECK-NEXT: [[A:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float [[Y:%.*]])
213 ; CHECK-NEXT: [[B:%.*]] = call float @llvm.minnum.f32(float [[Z:%.*]], float [[W:%.*]])
214 ; CHECK-NEXT: [[C:%.*]] = call float @llvm.minnum.f32(float [[A]], float [[B]])
215 ; CHECK-NEXT: ret float [[C]]
217 %a = call float @llvm.minnum.f32(float %x, float %y)
218 %b = call float @llvm.minnum.f32(float %z, float %w)
219 %c = call float @llvm.minnum.f32(float %a, float %b)
223 define float @minnum_x_maxnum_x_y(float %x, float %y) {
224 ; CHECK-LABEL: @minnum_x_maxnum_x_y(
225 ; CHECK-NEXT: [[A:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float [[Y:%.*]])
226 ; CHECK-NEXT: [[B:%.*]] = call float @llvm.minnum.f32(float [[X]], float [[A]])
227 ; CHECK-NEXT: ret float [[B]]
229 %a = call float @llvm.maxnum.f32(float %x, float %y)
230 %b = call float @llvm.minnum.f32(float %x, float %a)
234 define float @maxnum_x_minnum_x_y(float %x, float %y) {
235 ; CHECK-LABEL: @maxnum_x_minnum_x_y(
236 ; CHECK-NEXT: [[A:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float [[Y:%.*]])
237 ; CHECK-NEXT: [[B:%.*]] = call float @llvm.maxnum.f32(float [[X]], float [[A]])
238 ; CHECK-NEXT: ret float [[B]]
240 %a = call float @llvm.minnum.f32(float %x, float %y)
241 %b = call float @llvm.maxnum.f32(float %x, float %a)
245 ; PR37405 - https://bugs.llvm.org/show_bug.cgi?id=37405
247 define double @neg_neg(double %x, double %y) {
248 ; CHECK-LABEL: @neg_neg(
249 ; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.maxnum.f64(double [[X:%.*]], double [[Y:%.*]])
250 ; CHECK-NEXT: [[R:%.*]] = fsub double -0.000000e+00, [[TMP1]]
251 ; CHECK-NEXT: ret double [[R]]
253 %negx = fsub double -0.0, %x
254 %negy = fsub double -0.0, %y
255 %r = call double @llvm.minnum.f64(double %negx, double %negy)
259 define double @unary_neg_neg(double %x, double %y) {
260 ; CHECK-LABEL: @unary_neg_neg(
261 ; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.maxnum.f64(double [[X:%.*]], double [[Y:%.*]])
262 ; CHECK-NEXT: [[R:%.*]] = fsub double -0.000000e+00, [[TMP1]]
263 ; CHECK-NEXT: ret double [[R]]
265 %negx = fneg double %x
266 %negy = fneg double %y
267 %r = call double @llvm.minnum.f64(double %negx, double %negy)
271 ; FMF is not required, but it should be propagated from the intrinsic (not the fnegs).
272 ; Also, make sure this works with vectors.
274 define <2 x double> @neg_neg_vec_fmf(<2 x double> %x, <2 x double> %y) {
275 ; CHECK-LABEL: @neg_neg_vec_fmf(
276 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf <2 x double> @llvm.maxnum.v2f64(<2 x double> [[X:%.*]], <2 x double> [[Y:%.*]])
277 ; CHECK-NEXT: [[R:%.*]] = fsub nnan ninf <2 x double> <double -0.000000e+00, double -0.000000e+00>, [[TMP1]]
278 ; CHECK-NEXT: ret <2 x double> [[R]]
280 %negx = fsub reassoc <2 x double> <double -0.0, double -0.0>, %x
281 %negy = fsub fast <2 x double> <double -0.0, double -0.0>, %y
282 %r = call nnan ninf <2 x double> @llvm.minnum.v2f64(<2 x double> %negx, <2 x double> %negy)
286 define <2 x double> @unary_neg_neg_vec_fmf(<2 x double> %x, <2 x double> %y) {
287 ; CHECK-LABEL: @unary_neg_neg_vec_fmf(
288 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf <2 x double> @llvm.maxnum.v2f64(<2 x double> [[X:%.*]], <2 x double> [[Y:%.*]])
289 ; CHECK-NEXT: [[R:%.*]] = fsub nnan ninf <2 x double> <double -0.000000e+00, double -0.000000e+00>, [[TMP1]]
290 ; CHECK-NEXT: ret <2 x double> [[R]]
292 %negx = fneg reassoc <2 x double> %x
293 %negy = fneg fast <2 x double> %y
294 %r = call nnan ninf <2 x double> @llvm.minnum.v2f64(<2 x double> %negx, <2 x double> %negy)
298 ; 1 extra use of an intermediate value should still allow the fold,
299 ; but 2 would require more instructions than we started with.
301 declare void @use(double)
302 define double @neg_neg_extra_use_x(double %x, double %y) {
303 ; CHECK-LABEL: @neg_neg_extra_use_x(
304 ; CHECK-NEXT: [[NEGX:%.*]] = fsub double -0.000000e+00, [[X:%.*]]
305 ; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.maxnum.f64(double [[X]], double [[Y:%.*]])
306 ; CHECK-NEXT: [[R:%.*]] = fsub double -0.000000e+00, [[TMP1]]
307 ; CHECK-NEXT: call void @use(double [[NEGX]])
308 ; CHECK-NEXT: ret double [[R]]
310 %negx = fsub double -0.0, %x
311 %negy = fsub double -0.0, %y
312 %r = call double @llvm.minnum.f64(double %negx, double %negy)
313 call void @use(double %negx)
317 define double @unary_neg_neg_extra_use_x(double %x, double %y) {
318 ; CHECK-LABEL: @unary_neg_neg_extra_use_x(
319 ; CHECK-NEXT: [[NEGX:%.*]] = fneg double [[X:%.*]]
320 ; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.maxnum.f64(double [[X]], double [[Y:%.*]])
321 ; CHECK-NEXT: [[R:%.*]] = fsub double -0.000000e+00, [[TMP1]]
322 ; CHECK-NEXT: call void @use(double [[NEGX]])
323 ; CHECK-NEXT: ret double [[R]]
325 %negx = fneg double %x
326 %negy = fneg double %y
327 %r = call double @llvm.minnum.f64(double %negx, double %negy)
328 call void @use(double %negx)
332 define double @neg_neg_extra_use_y(double %x, double %y) {
333 ; CHECK-LABEL: @neg_neg_extra_use_y(
334 ; CHECK-NEXT: [[NEGY:%.*]] = fsub double -0.000000e+00, [[Y:%.*]]
335 ; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.maxnum.f64(double [[X:%.*]], double [[Y]])
336 ; CHECK-NEXT: [[R:%.*]] = fsub double -0.000000e+00, [[TMP1]]
337 ; CHECK-NEXT: call void @use(double [[NEGY]])
338 ; CHECK-NEXT: ret double [[R]]
340 %negx = fsub double -0.0, %x
341 %negy = fsub double -0.0, %y
342 %r = call double @llvm.minnum.f64(double %negx, double %negy)
343 call void @use(double %negy)
347 define double @unary_neg_neg_extra_use_y(double %x, double %y) {
348 ; CHECK-LABEL: @unary_neg_neg_extra_use_y(
349 ; CHECK-NEXT: [[NEGY:%.*]] = fneg double [[Y:%.*]]
350 ; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.maxnum.f64(double [[X:%.*]], double [[Y]])
351 ; CHECK-NEXT: [[R:%.*]] = fsub double -0.000000e+00, [[TMP1]]
352 ; CHECK-NEXT: call void @use(double [[NEGY]])
353 ; CHECK-NEXT: ret double [[R]]
355 %negx = fneg double %x
356 %negy = fneg double %y
357 %r = call double @llvm.minnum.f64(double %negx, double %negy)
358 call void @use(double %negy)
362 define double @neg_neg_extra_use_x_and_y(double %x, double %y) {
363 ; CHECK-LABEL: @neg_neg_extra_use_x_and_y(
364 ; CHECK-NEXT: [[NEGX:%.*]] = fsub double -0.000000e+00, [[X:%.*]]
365 ; CHECK-NEXT: [[NEGY:%.*]] = fsub double -0.000000e+00, [[Y:%.*]]
366 ; CHECK-NEXT: [[R:%.*]] = call double @llvm.minnum.f64(double [[NEGX]], double [[NEGY]])
367 ; CHECK-NEXT: call void @use(double [[NEGX]])
368 ; CHECK-NEXT: call void @use(double [[NEGY]])
369 ; CHECK-NEXT: ret double [[R]]
371 %negx = fsub double -0.0, %x
372 %negy = fsub double -0.0, %y
373 %r = call double @llvm.minnum.f64(double %negx, double %negy)
374 call void @use(double %negx)
375 call void @use(double %negy)
379 define double @unary_neg_neg_extra_use_x_and_y(double %x, double %y) {
380 ; CHECK-LABEL: @unary_neg_neg_extra_use_x_and_y(
381 ; CHECK-NEXT: [[NEGX:%.*]] = fneg double [[X:%.*]]
382 ; CHECK-NEXT: [[NEGY:%.*]] = fneg double [[Y:%.*]]
383 ; CHECK-NEXT: [[R:%.*]] = call double @llvm.minnum.f64(double [[NEGX]], double [[NEGY]])
384 ; CHECK-NEXT: call void @use(double [[NEGX]])
385 ; CHECK-NEXT: call void @use(double [[NEGY]])
386 ; CHECK-NEXT: ret double [[R]]
388 %negx = fneg double %x
389 %negy = fneg double %y
390 %r = call double @llvm.minnum.f64(double %negx, double %negy)
391 call void @use(double %negx)
392 call void @use(double %negy)