1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -instcombine < %s | FileCheck %s
4 declare float @llvm.maxnum.f32(float, float)
5 declare <2 x float> @llvm.maxnum.v2f32(<2 x float>, <2 x float>)
6 declare <4 x float> @llvm.maxnum.v4f32(<4 x float>, <4 x float>)
8 declare double @llvm.maxnum.f64(double, double)
9 declare <2 x double> @llvm.maxnum.v2f64(<2 x double>, <2 x double>)
11 define float @constant_fold_maxnum_f32() {
12 ; CHECK-LABEL: @constant_fold_maxnum_f32(
13 ; CHECK-NEXT: ret float 2.000000e+00
15 %x = call float @llvm.maxnum.f32(float 1.0, float 2.0)
19 define float @constant_fold_maxnum_f32_inv() {
20 ; CHECK-LABEL: @constant_fold_maxnum_f32_inv(
21 ; CHECK-NEXT: ret float 2.000000e+00
23 %x = call float @llvm.maxnum.f32(float 2.0, float 1.0)
27 define float @constant_fold_maxnum_f32_nan0() {
28 ; CHECK-LABEL: @constant_fold_maxnum_f32_nan0(
29 ; CHECK-NEXT: ret float 2.000000e+00
31 %x = call float @llvm.maxnum.f32(float 0x7FF8000000000000, float 2.0)
35 define float @constant_fold_maxnum_f32_nan1() {
36 ; CHECK-LABEL: @constant_fold_maxnum_f32_nan1(
37 ; CHECK-NEXT: ret float 2.000000e+00
39 %x = call float @llvm.maxnum.f32(float 2.0, float 0x7FF8000000000000)
43 define float @constant_fold_maxnum_f32_nan_nan() {
44 ; CHECK-LABEL: @constant_fold_maxnum_f32_nan_nan(
45 ; CHECK-NEXT: ret float 0x7FF8000000000000
47 %x = call float @llvm.maxnum.f32(float 0x7FF8000000000000, float 0x7FF8000000000000)
51 define float @constant_fold_maxnum_f32_p0_p0() {
52 ; CHECK-LABEL: @constant_fold_maxnum_f32_p0_p0(
53 ; CHECK-NEXT: ret float 0.000000e+00
55 %x = call float @llvm.maxnum.f32(float 0.0, float 0.0)
59 define float @constant_fold_maxnum_f32_p0_n0() {
60 ; CHECK-LABEL: @constant_fold_maxnum_f32_p0_n0(
61 ; CHECK-NEXT: ret float 0.000000e+00
63 %x = call float @llvm.maxnum.f32(float 0.0, float -0.0)
67 define float @constant_fold_maxnum_f32_n0_p0() {
68 ; CHECK-LABEL: @constant_fold_maxnum_f32_n0_p0(
69 ; CHECK-NEXT: ret float -0.000000e+00
71 %x = call float @llvm.maxnum.f32(float -0.0, float 0.0)
75 define float @constant_fold_maxnum_f32_n0_n0() {
76 ; CHECK-LABEL: @constant_fold_maxnum_f32_n0_n0(
77 ; CHECK-NEXT: ret float -0.000000e+00
79 %x = call float @llvm.maxnum.f32(float -0.0, float -0.0)
83 define <4 x float> @constant_fold_maxnum_v4f32() {
84 ; CHECK-LABEL: @constant_fold_maxnum_v4f32(
85 ; CHECK-NEXT: ret <4 x float> <float 2.000000e+00, float 8.000000e+00, float 1.000000e+01, float 9.000000e+00>
87 %x = call <4 x float> @llvm.maxnum.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>)
91 define double @constant_fold_maxnum_f64() {
92 ; CHECK-LABEL: @constant_fold_maxnum_f64(
93 ; CHECK-NEXT: ret double 2.000000e+00
95 %x = call double @llvm.maxnum.f64(double 1.0, double 2.0)
99 define double @constant_fold_maxnum_f64_nan0() {
100 ; CHECK-LABEL: @constant_fold_maxnum_f64_nan0(
101 ; CHECK-NEXT: ret double 2.000000e+00
103 %x = call double @llvm.maxnum.f64(double 0x7FF8000000000000, double 2.0)
107 define double @constant_fold_maxnum_f64_nan1() {
108 ; CHECK-LABEL: @constant_fold_maxnum_f64_nan1(
109 ; CHECK-NEXT: ret double 2.000000e+00
111 %x = call double @llvm.maxnum.f64(double 2.0, double 0x7FF8000000000000)
115 define double @constant_fold_maxnum_f64_nan_nan() {
116 ; CHECK-LABEL: @constant_fold_maxnum_f64_nan_nan(
117 ; CHECK-NEXT: ret double 0x7FF8000000000000
119 %x = call double @llvm.maxnum.f64(double 0x7FF8000000000000, double 0x7FF8000000000000)
123 define float @canonicalize_constant_maxnum_f32(float %x) {
124 ; CHECK-LABEL: @canonicalize_constant_maxnum_f32(
125 ; CHECK-NEXT: [[Y:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float 1.000000e+00)
126 ; CHECK-NEXT: ret float [[Y]]
128 %y = call float @llvm.maxnum.f32(float 1.0, float %x)
132 define float @maxnum_f32_nan_val(float %x) {
133 ; CHECK-LABEL: @maxnum_f32_nan_val(
134 ; CHECK-NEXT: ret float [[X:%.*]]
136 %y = call float @llvm.maxnum.f32(float 0x7FF8000000000000, float %x)
140 define float @maxnum_f32_val_nan(float %x) {
141 ; CHECK-LABEL: @maxnum_f32_val_nan(
142 ; CHECK-NEXT: ret float [[X:%.*]]
144 %y = call float @llvm.maxnum.f32(float %x, float 0x7FF8000000000000)
148 define float @maxnum_f32_1_maxnum_val_p0(float %x) {
149 ; CHECK-LABEL: @maxnum_f32_1_maxnum_val_p0(
150 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float 1.000000e+00)
151 ; CHECK-NEXT: ret float [[TMP1]]
153 %y = call float @llvm.maxnum.f32(float %x, float 0.0)
154 %z = call float @llvm.maxnum.f32(float %y, float 1.0)
158 define float @maxnum_f32_1_maxnum_p0_val_fast(float %x) {
159 ; CHECK-LABEL: @maxnum_f32_1_maxnum_p0_val_fast(
160 ; CHECK-NEXT: [[TMP1:%.*]] = call fast float @llvm.maxnum.f32(float [[X:%.*]], float 1.000000e+00)
161 ; CHECK-NEXT: ret float [[TMP1]]
163 %y = call float @llvm.maxnum.f32(float 0.0, float %x)
164 %z = call fast float @llvm.maxnum.f32(float %y, float 1.0)
168 define float @maxnum_f32_1_maxnum_p0_val_nnan_ninf(float %x) {
169 ; CHECK-LABEL: @maxnum_f32_1_maxnum_p0_val_nnan_ninf(
170 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf float @llvm.maxnum.f32(float [[X:%.*]], float 1.000000e+00)
171 ; CHECK-NEXT: ret float [[TMP1]]
173 %y = call float @llvm.maxnum.f32(float 0.0, float %x)
174 %z = call nnan ninf float @llvm.maxnum.f32(float %y, float 1.0)
178 define float @maxnum_f32_p0_maxnum_val_n0(float %x) {
179 ; CHECK-LABEL: @maxnum_f32_p0_maxnum_val_n0(
180 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float 0.000000e+00)
181 ; CHECK-NEXT: ret float [[TMP1]]
183 %y = call float @llvm.maxnum.f32(float %x, float -0.0)
184 %z = call float @llvm.maxnum.f32(float %y, float 0.0)
188 define float @maxnum_f32_1_maxnum_p0_val(float %x) {
189 ; CHECK-LABEL: @maxnum_f32_1_maxnum_p0_val(
190 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float 1.000000e+00)
191 ; CHECK-NEXT: ret float [[TMP1]]
193 %y = call float @llvm.maxnum.f32(float 0.0, float %x)
194 %z = call float @llvm.maxnum.f32(float %y, float 1.0)
198 define <2 x float> @maxnum_f32_1_maxnum_val_p0_val_v2f32(<2 x float> %x) {
199 ; CHECK-LABEL: @maxnum_f32_1_maxnum_val_p0_val_v2f32(
200 ; CHECK-NEXT: [[TMP1:%.*]] = call <2 x float> @llvm.maxnum.v2f32(<2 x float> [[X:%.*]], <2 x float> <float 1.000000e+00, float 1.000000e+00>)
201 ; CHECK-NEXT: ret <2 x float> [[TMP1]]
203 %y = call <2 x float> @llvm.maxnum.v2f32(<2 x float> %x, <2 x float> zeroinitializer)
204 %z = call <2 x float> @llvm.maxnum.v2f32(<2 x float> %y, <2 x float><float 1.0, float 1.0>)
208 define float @maxnum4(float %x, float %y, float %z, float %w) {
209 ; CHECK-LABEL: @maxnum4(
210 ; CHECK-NEXT: [[A:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float [[Y:%.*]])
211 ; CHECK-NEXT: [[B:%.*]] = call float @llvm.maxnum.f32(float [[Z:%.*]], float [[W:%.*]])
212 ; CHECK-NEXT: [[C:%.*]] = call float @llvm.maxnum.f32(float [[A]], float [[B]])
213 ; CHECK-NEXT: ret float [[C]]
215 %a = call float @llvm.maxnum.f32(float %x, float %y)
216 %b = call float @llvm.maxnum.f32(float %z, float %w)
217 %c = call float @llvm.maxnum.f32(float %a, float %b)
221 ; PR37404 - https://bugs.llvm.org/show_bug.cgi?id=37404
223 define <2 x float> @neg_neg(<2 x float> %x, <2 x float> %y) {
224 ; CHECK-LABEL: @neg_neg(
225 ; CHECK-NEXT: [[TMP1:%.*]] = call <2 x float> @llvm.minnum.v2f32(<2 x float> [[X:%.*]], <2 x float> [[Y:%.*]])
226 ; CHECK-NEXT: [[R:%.*]] = fsub <2 x float> <float -0.000000e+00, float -0.000000e+00>, [[TMP1]]
227 ; CHECK-NEXT: ret <2 x float> [[R]]
229 %negx = fsub <2 x float> <float -0.0, float -0.0>, %x
230 %negy = fsub <2 x float> <float -0.0, float -0.0>, %y
231 %r = call <2 x float> @llvm.maxnum.v2f32(<2 x float> %negx, <2 x float> %negy)
235 ; FMF is not required, but it should be propagated from the intrinsic (not the fnegs).
237 define float @neg_neg_vec_fmf(float %x, float %y) {
238 ; CHECK-LABEL: @neg_neg_vec_fmf(
239 ; CHECK-NEXT: [[TMP1:%.*]] = call fast float @llvm.minnum.f32(float [[X:%.*]], float [[Y:%.*]])
240 ; CHECK-NEXT: [[R:%.*]] = fsub fast float -0.000000e+00, [[TMP1]]
241 ; CHECK-NEXT: ret float [[R]]
243 %negx = fsub arcp float -0.0, %x
244 %negy = fsub afn float -0.0, %y
245 %r = call fast float @llvm.maxnum.f32(float %negx, float %negy)
249 define float @unary_neg_neg_vec_fmf(float %x, float %y) {
250 ; CHECK-LABEL: @unary_neg_neg_vec_fmf(
251 ; CHECK-NEXT: [[TMP1:%.*]] = call fast float @llvm.minnum.f32(float [[X:%.*]], float [[Y:%.*]])
252 ; CHECK-NEXT: [[R:%.*]] = fsub fast float -0.000000e+00, [[TMP1]]
253 ; CHECK-NEXT: ret float [[R]]
255 %negx = fneg arcp float %x
256 %negy = fneg afn float %y
257 %r = call fast float @llvm.maxnum.f32(float %negx, float %negy)
261 ; 1 extra use of an intermediate value should still allow the fold,
262 ; but 2 would require more instructions than we started with.
264 declare void @use(float)
265 define float @neg_neg_extra_use_x(float %x, float %y) {
266 ; CHECK-LABEL: @neg_neg_extra_use_x(
267 ; CHECK-NEXT: [[NEGX:%.*]] = fsub float -0.000000e+00, [[X:%.*]]
268 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.minnum.f32(float [[X]], float [[Y:%.*]])
269 ; CHECK-NEXT: [[R:%.*]] = fsub float -0.000000e+00, [[TMP1]]
270 ; CHECK-NEXT: call void @use(float [[NEGX]])
271 ; CHECK-NEXT: ret float [[R]]
273 %negx = fsub float -0.0, %x
274 %negy = fsub float -0.0, %y
275 %r = call float @llvm.maxnum.f32(float %negx, float %negy)
276 call void @use(float %negx)
280 define float @unary_neg_neg_extra_use_x(float %x, float %y) {
281 ; CHECK-LABEL: @unary_neg_neg_extra_use_x(
282 ; CHECK-NEXT: [[NEGX:%.*]] = fneg float [[X:%.*]]
283 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.minnum.f32(float [[X]], float [[Y:%.*]])
284 ; CHECK-NEXT: [[R:%.*]] = fsub float -0.000000e+00, [[TMP1]]
285 ; CHECK-NEXT: call void @use(float [[NEGX]])
286 ; CHECK-NEXT: ret float [[R]]
288 %negx = fneg float %x
289 %negy = fneg float %y
290 %r = call float @llvm.maxnum.f32(float %negx, float %negy)
291 call void @use(float %negx)
295 define float @neg_neg_extra_use_y(float %x, float %y) {
296 ; CHECK-LABEL: @neg_neg_extra_use_y(
297 ; CHECK-NEXT: [[NEGY:%.*]] = fsub float -0.000000e+00, [[Y:%.*]]
298 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float [[Y]])
299 ; CHECK-NEXT: [[R:%.*]] = fsub float -0.000000e+00, [[TMP1]]
300 ; CHECK-NEXT: call void @use(float [[NEGY]])
301 ; CHECK-NEXT: ret float [[R]]
303 %negx = fsub float -0.0, %x
304 %negy = fsub float -0.0, %y
305 %r = call float @llvm.maxnum.f32(float %negx, float %negy)
306 call void @use(float %negy)
310 define float @unary_neg_neg_extra_use_y(float %x, float %y) {
311 ; CHECK-LABEL: @unary_neg_neg_extra_use_y(
312 ; CHECK-NEXT: [[NEGY:%.*]] = fneg float [[Y:%.*]]
313 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float [[Y]])
314 ; CHECK-NEXT: [[R:%.*]] = fsub float -0.000000e+00, [[TMP1]]
315 ; CHECK-NEXT: call void @use(float [[NEGY]])
316 ; CHECK-NEXT: ret float [[R]]
318 %negx = fneg float %x
319 %negy = fneg float %y
320 %r = call float @llvm.maxnum.f32(float %negx, float %negy)
321 call void @use(float %negy)
325 define float @neg_neg_extra_use_x_and_y(float %x, float %y) {
326 ; CHECK-LABEL: @neg_neg_extra_use_x_and_y(
327 ; CHECK-NEXT: [[NEGX:%.*]] = fsub float -0.000000e+00, [[X:%.*]]
328 ; CHECK-NEXT: [[NEGY:%.*]] = fsub float -0.000000e+00, [[Y:%.*]]
329 ; CHECK-NEXT: [[R:%.*]] = call float @llvm.maxnum.f32(float [[NEGX]], float [[NEGY]])
330 ; CHECK-NEXT: call void @use(float [[NEGX]])
331 ; CHECK-NEXT: call void @use(float [[NEGY]])
332 ; CHECK-NEXT: ret float [[R]]
334 %negx = fsub float -0.0, %x
335 %negy = fsub float -0.0, %y
336 %r = call float @llvm.maxnum.f32(float %negx, float %negy)
337 call void @use(float %negx)
338 call void @use(float %negy)
342 define float @unary_neg_neg_extra_use_x_and_y(float %x, float %y) {
343 ; CHECK-LABEL: @unary_neg_neg_extra_use_x_and_y(
344 ; CHECK-NEXT: [[NEGX:%.*]] = fneg float [[X:%.*]]
345 ; CHECK-NEXT: [[NEGY:%.*]] = fneg float [[Y:%.*]]
346 ; CHECK-NEXT: [[R:%.*]] = call float @llvm.maxnum.f32(float [[NEGX]], float [[NEGY]])
347 ; CHECK-NEXT: call void @use(float [[NEGX]])
348 ; CHECK-NEXT: call void @use(float [[NEGY]])
349 ; CHECK-NEXT: ret float [[R]]
351 %negx = fneg float %x
352 %negy = fneg float %y
353 %r = call float @llvm.maxnum.f32(float %negx, float %negy)
354 call void @use(float %negx)
355 call void @use(float %negy)