1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -instcombine < %s | FileCheck %s
4 declare float @llvm.maximum.f32(float, float)
5 declare <2 x float> @llvm.maximum.v2f32(<2 x float>, <2 x float>)
6 declare <4 x float> @llvm.maximum.v4f32(<4 x float>, <4 x float>)
8 declare double @llvm.maximum.f64(double, double)
9 declare <2 x double> @llvm.maximum.v2f64(<2 x double>, <2 x double>)
11 define float @constant_fold_maximum_f32() {
12 ; CHECK-LABEL: @constant_fold_maximum_f32(
13 ; CHECK-NEXT: ret float 2.000000e+00
15 %x = call float @llvm.maximum.f32(float 1.0, float 2.0)
19 define float @constant_fold_maximum_f32_inv() {
20 ; CHECK-LABEL: @constant_fold_maximum_f32_inv(
21 ; CHECK-NEXT: ret float 2.000000e+00
23 %x = call float @llvm.maximum.f32(float 2.0, float 1.0)
27 define float @constant_fold_maximum_f32_nan0() {
28 ; CHECK-LABEL: @constant_fold_maximum_f32_nan0(
29 ; CHECK-NEXT: ret float 0x7FF8000000000000
31 %x = call float @llvm.maximum.f32(float 0x7FF8000000000000, float 2.0)
35 define float @constant_fold_maximum_f32_nan1() {
36 ; CHECK-LABEL: @constant_fold_maximum_f32_nan1(
37 ; CHECK-NEXT: ret float 0x7FF8000000000000
39 %x = call float @llvm.maximum.f32(float 2.0, float 0x7FF8000000000000)
43 define float @constant_fold_maximum_f32_nan_nan() {
44 ; CHECK-LABEL: @constant_fold_maximum_f32_nan_nan(
45 ; CHECK-NEXT: ret float 0x7FF8000000000000
47 %x = call float @llvm.maximum.f32(float 0x7FF8000000000000, float 0x7FF8000000000000)
51 define float @constant_fold_maximum_f32_p0_p0() {
52 ; CHECK-LABEL: @constant_fold_maximum_f32_p0_p0(
53 ; CHECK-NEXT: ret float 0.000000e+00
55 %x = call float @llvm.maximum.f32(float 0.0, float 0.0)
59 define float @constant_fold_maximum_f32_p0_n0() {
60 ; CHECK-LABEL: @constant_fold_maximum_f32_p0_n0(
61 ; CHECK-NEXT: ret float 0.000000e+00
63 %x = call float @llvm.maximum.f32(float 0.0, float -0.0)
67 define float @constant_fold_maximum_f32_n0_p0() {
68 ; CHECK-LABEL: @constant_fold_maximum_f32_n0_p0(
69 ; CHECK-NEXT: ret float 0.000000e+00
71 %x = call float @llvm.maximum.f32(float -0.0, float 0.0)
75 define float @constant_fold_maximum_f32_n0_n0() {
76 ; CHECK-LABEL: @constant_fold_maximum_f32_n0_n0(
77 ; CHECK-NEXT: ret float -0.000000e+00
79 %x = call float @llvm.maximum.f32(float -0.0, float -0.0)
83 define <4 x float> @constant_fold_maximum_v4f32() {
84 ; CHECK-LABEL: @constant_fold_maximum_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.maximum.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_maximum_f64() {
92 ; CHECK-LABEL: @constant_fold_maximum_f64(
93 ; CHECK-NEXT: ret double 2.000000e+00
95 %x = call double @llvm.maximum.f64(double 1.0, double 2.0)
99 define double @constant_fold_maximum_f64_nan0() {
100 ; CHECK-LABEL: @constant_fold_maximum_f64_nan0(
101 ; CHECK-NEXT: ret double 0x7FF8000000000000
103 %x = call double @llvm.maximum.f64(double 0x7FF8000000000000, double 2.0)
107 define double @constant_fold_maximum_f64_nan1() {
108 ; CHECK-LABEL: @constant_fold_maximum_f64_nan1(
109 ; CHECK-NEXT: ret double 0x7FF8000000000000
111 %x = call double @llvm.maximum.f64(double 2.0, double 0x7FF8000000000000)
115 define double @constant_fold_maximum_f64_nan_nan() {
116 ; CHECK-LABEL: @constant_fold_maximum_f64_nan_nan(
117 ; CHECK-NEXT: ret double 0x7FF8000000000000
119 %x = call double @llvm.maximum.f64(double 0x7FF8000000000000, double 0x7FF8000000000000)
123 define float @canonicalize_constant_maximum_f32(float %x) {
124 ; CHECK-LABEL: @canonicalize_constant_maximum_f32(
125 ; CHECK-NEXT: [[Y:%.*]] = call float @llvm.maximum.f32(float [[X:%.*]], float 1.000000e+00)
126 ; CHECK-NEXT: ret float [[Y]]
128 %y = call float @llvm.maximum.f32(float 1.0, float %x)
132 define float @maximum_f32_nan_val(float %x) {
133 ; CHECK-LABEL: @maximum_f32_nan_val(
134 ; CHECK-NEXT: ret float 0x7FF8000000000000
136 %y = call float @llvm.maximum.f32(float 0x7FF8000000000000, float %x)
140 define float @maximum_f32_val_nan(float %x) {
141 ; CHECK-LABEL: @maximum_f32_val_nan(
142 ; CHECK-NEXT: ret float 0x7FF8000000000000
144 %y = call float @llvm.maximum.f32(float %x, float 0x7FF8000000000000)
148 define float @maximum_f32_1_maximum_val_p0(float %x) {
149 ; CHECK-LABEL: @maximum_f32_1_maximum_val_p0(
150 ; CHECK-NEXT: [[RES:%.*]] = call float @llvm.maximum.f32(float %x, float 1.000000e+00)
151 ; CHECK-NEXT: ret float [[RES]]
152 %y = call float @llvm.maximum.f32(float %x, float 0.0)
153 %z = call float @llvm.maximum.f32(float %y, float 1.0)
157 define float @maximum_f32_1_maximum_p0_val_fast(float %x) {
158 ; CHECK-LABEL: @maximum_f32_1_maximum_p0_val_fast(
159 ; CHECK-NEXT: [[RES:%.*]] = call fast float @llvm.maximum.f32(float %x, float 1.000000e+00)
160 ; CHECK-NEXT: ret float [[RES]]
161 %y = call float @llvm.maximum.f32(float 0.0, float %x)
162 %z = call fast float @llvm.maximum.f32(float %y, float 1.0)
166 define float @maximum_f32_1_maximum_p0_val_nnan_ninf(float %x) {
167 ; CHECK-LABEL: @maximum_f32_1_maximum_p0_val_nnan_ninf(
168 ; CHECK-NEXT: [[RES:%.*]] = call nnan ninf float @llvm.maximum.f32(float %x, float 1.000000e+00)
169 ; CHECK-NEXT: ret float [[RES]]
170 %y = call float @llvm.maximum.f32(float 0.0, float %x)
171 %z = call nnan ninf float @llvm.maximum.f32(float %y, float 1.0)
175 define float @maximum_f32_p0_maximum_val_n0(float %x) {
176 ; CHECK-LABEL: @maximum_f32_p0_maximum_val_n0(
177 ; CHECK-NEXT: [[RES:%.*]] = call float @llvm.maximum.f32(float %x, float 0.000000e+00)
178 ; CHECK-NEXT: ret float [[RES]]
179 %y = call float @llvm.maximum.f32(float %x, float -0.0)
180 %z = call float @llvm.maximum.f32(float %y, float 0.0)
184 define float @maximum_f32_1_maximum_p0_val(float %x) {
185 ; CHECK-LABEL: @maximum_f32_1_maximum_p0_val(
186 ; CHECK-NEXT: [[RES:%.*]] = call float @llvm.maximum.f32(float %x, float 1.000000e+00)
187 ; CHECK-NEXT: ret float [[RES]]
188 %y = call float @llvm.maximum.f32(float 0.0, float %x)
189 %z = call float @llvm.maximum.f32(float %y, float 1.0)
193 define <2 x float> @maximum_f32_1_maximum_val_p0_val_v2f32(<2 x float> %x) {
194 ; CHECK-LABEL: @maximum_f32_1_maximum_val_p0_val_v2f32(
195 ; CHECK-NEXT: [[RES:%.*]] = call <2 x float> @llvm.maximum.v2f32(<2 x float> %x, <2 x float> <float 1.000000e+00, float 1.000000e+00>)
196 ; CHECK-NEXT: ret <2 x float> [[RES]]
197 %y = call <2 x float> @llvm.maximum.v2f32(<2 x float> %x, <2 x float> zeroinitializer)
198 %z = call <2 x float> @llvm.maximum.v2f32(<2 x float> %y, <2 x float><float 1.0, float 1.0>)
202 define float @maximum4(float %x, float %y, float %z, float %w) {
203 ; CHECK-LABEL: @maximum4(
204 ; CHECK-NEXT: [[A:%.*]] = call float @llvm.maximum.f32(float [[X:%.*]], float [[Y:%.*]])
205 ; CHECK-NEXT: [[B:%.*]] = call float @llvm.maximum.f32(float [[Z:%.*]], float [[W:%.*]])
206 ; CHECK-NEXT: [[C:%.*]] = call float @llvm.maximum.f32(float [[A]], float [[B]])
207 ; CHECK-NEXT: ret float [[C]]
209 %a = call float @llvm.maximum.f32(float %x, float %y)
210 %b = call float @llvm.maximum.f32(float %z, float %w)
211 %c = call float @llvm.maximum.f32(float %a, float %b)
215 ; PR37404 - https://bugs.llvm.org/show_bug.cgi?id=37404
217 define <2 x float> @neg_neg(<2 x float> %x, <2 x float> %y) {
218 ; CHECK-LABEL: @neg_neg(
219 ; CHECK-NEXT: [[TMP1:%.*]] = call <2 x float> @llvm.minimum.v2f32(<2 x float> [[X:%.*]], <2 x float> [[Y:%.*]])
220 ; CHECK-NEXT: [[R:%.*]] = fsub <2 x float> <float -0.000000e+00, float -0.000000e+00>, [[TMP1]]
221 ; CHECK-NEXT: ret <2 x float> [[R]]
223 %negx = fsub <2 x float> <float -0.0, float -0.0>, %x
224 %negy = fsub <2 x float> <float -0.0, float -0.0>, %y
225 %r = call <2 x float> @llvm.maximum.v2f32(<2 x float> %negx, <2 x float> %negy)
229 define <2 x float> @unary_neg_neg(<2 x float> %x, <2 x float> %y) {
230 ; CHECK-LABEL: @unary_neg_neg(
231 ; CHECK-NEXT: [[TMP1:%.*]] = call <2 x float> @llvm.minimum.v2f32(<2 x float> [[X:%.*]], <2 x float> [[Y:%.*]])
232 ; CHECK-NEXT: [[R:%.*]] = fsub <2 x float> <float -0.000000e+00, float -0.000000e+00>, [[TMP1]]
233 ; CHECK-NEXT: ret <2 x float> [[R]]
235 %negx = fneg <2 x float> %x
236 %negy = fneg <2 x float> %y
237 %r = call <2 x float> @llvm.maximum.v2f32(<2 x float> %negx, <2 x float> %negy)
241 ; FMF is not required, but it should be propagated from the intrinsic (not the fnegs).
243 define float @neg_neg_vec_fmf(float %x, float %y) {
244 ; CHECK-LABEL: @neg_neg_vec_fmf(
245 ; CHECK-NEXT: [[TMP1:%.*]] = call fast float @llvm.minimum.f32(float [[X:%.*]], float [[Y:%.*]])
246 ; CHECK-NEXT: [[R:%.*]] = fsub fast float -0.000000e+00, [[TMP1]]
247 ; CHECK-NEXT: ret float [[R]]
249 %negx = fsub arcp float -0.0, %x
250 %negy = fsub afn float -0.0, %y
251 %r = call fast float @llvm.maximum.f32(float %negx, float %negy)
255 define float @unary_neg_neg_vec_fmf(float %x, float %y) {
256 ; CHECK-LABEL: @unary_neg_neg_vec_fmf(
257 ; CHECK-NEXT: [[TMP1:%.*]] = call fast float @llvm.minimum.f32(float [[X:%.*]], float [[Y:%.*]])
258 ; CHECK-NEXT: [[R:%.*]] = fsub fast float -0.000000e+00, [[TMP1]]
259 ; CHECK-NEXT: ret float [[R]]
261 %negx = fneg arcp float %x
262 %negy = fneg afn float %y
263 %r = call fast float @llvm.maximum.f32(float %negx, float %negy)
267 ; 1 extra use of an intermediate value should still allow the fold,
268 ; but 2 would require more instructions than we started with.
270 declare void @use(float)
271 define float @neg_neg_extra_use_x(float %x, float %y) {
272 ; CHECK-LABEL: @neg_neg_extra_use_x(
273 ; CHECK-NEXT: [[NEGX:%.*]] = fsub float -0.000000e+00, [[X:%.*]]
274 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.minimum.f32(float [[X]], float [[Y:%.*]])
275 ; CHECK-NEXT: [[R:%.*]] = fsub float -0.000000e+00, [[TMP1]]
276 ; CHECK-NEXT: call void @use(float [[NEGX]])
277 ; CHECK-NEXT: ret float [[R]]
279 %negx = fsub float -0.0, %x
280 %negy = fsub float -0.0, %y
281 %r = call float @llvm.maximum.f32(float %negx, float %negy)
282 call void @use(float %negx)
286 define float @unary_neg_neg_extra_use_x(float %x, float %y) {
287 ; CHECK-LABEL: @unary_neg_neg_extra_use_x(
288 ; CHECK-NEXT: [[NEGX:%.*]] = fneg float [[X:%.*]]
289 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.minimum.f32(float [[X]], float [[Y:%.*]])
290 ; CHECK-NEXT: [[R:%.*]] = fsub float -0.000000e+00, [[TMP1]]
291 ; CHECK-NEXT: call void @use(float [[NEGX]])
292 ; CHECK-NEXT: ret float [[R]]
294 %negx = fneg float %x
295 %negy = fneg float %y
296 %r = call float @llvm.maximum.f32(float %negx, float %negy)
297 call void @use(float %negx)
301 define float @neg_neg_extra_use_y(float %x, float %y) {
302 ; CHECK-LABEL: @neg_neg_extra_use_y(
303 ; CHECK-NEXT: [[NEGY:%.*]] = fsub float -0.000000e+00, [[Y:%.*]]
304 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.minimum.f32(float [[X:%.*]], float [[Y]])
305 ; CHECK-NEXT: [[R:%.*]] = fsub float -0.000000e+00, [[TMP1]]
306 ; CHECK-NEXT: call void @use(float [[NEGY]])
307 ; CHECK-NEXT: ret float [[R]]
309 %negx = fsub float -0.0, %x
310 %negy = fsub float -0.0, %y
311 %r = call float @llvm.maximum.f32(float %negx, float %negy)
312 call void @use(float %negy)
316 define float @unary_neg_neg_extra_use_y(float %x, float %y) {
317 ; CHECK-LABEL: @unary_neg_neg_extra_use_y(
318 ; CHECK-NEXT: [[NEGY:%.*]] = fneg float [[Y:%.*]]
319 ; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.minimum.f32(float [[X:%.*]], float [[Y]])
320 ; CHECK-NEXT: [[R:%.*]] = fsub float -0.000000e+00, [[TMP1]]
321 ; CHECK-NEXT: call void @use(float [[NEGY]])
322 ; CHECK-NEXT: ret float [[R]]
324 %negx = fneg float %x
325 %negy = fneg float %y
326 %r = call float @llvm.maximum.f32(float %negx, float %negy)
327 call void @use(float %negy)
331 define float @neg_neg_extra_use_x_and_y(float %x, float %y) {
332 ; CHECK-LABEL: @neg_neg_extra_use_x_and_y(
333 ; CHECK-NEXT: [[NEGX:%.*]] = fsub float -0.000000e+00, [[X:%.*]]
334 ; CHECK-NEXT: [[NEGY:%.*]] = fsub float -0.000000e+00, [[Y:%.*]]
335 ; CHECK-NEXT: [[R:%.*]] = call float @llvm.maximum.f32(float [[NEGX]], float [[NEGY]])
336 ; CHECK-NEXT: call void @use(float [[NEGX]])
337 ; CHECK-NEXT: call void @use(float [[NEGY]])
338 ; CHECK-NEXT: ret float [[R]]
340 %negx = fsub float -0.0, %x
341 %negy = fsub float -0.0, %y
342 %r = call float @llvm.maximum.f32(float %negx, float %negy)
343 call void @use(float %negx)
344 call void @use(float %negy)
348 define float @unary_neg_neg_extra_use_x_and_y(float %x, float %y) {
349 ; CHECK-LABEL: @unary_neg_neg_extra_use_x_and_y(
350 ; CHECK-NEXT: [[NEGX:%.*]] = fneg float [[X:%.*]]
351 ; CHECK-NEXT: [[NEGY:%.*]] = fneg float [[Y:%.*]]
352 ; CHECK-NEXT: [[R:%.*]] = call float @llvm.maximum.f32(float [[NEGX]], float [[NEGY]])
353 ; CHECK-NEXT: call void @use(float [[NEGX]])
354 ; CHECK-NEXT: call void @use(float [[NEGY]])
355 ; CHECK-NEXT: ret float [[R]]
357 %negx = fneg float %x
358 %negy = fneg float %y
359 %r = call float @llvm.maximum.f32(float %negx, float %negy)
360 call void @use(float %negx)
361 call void @use(float %negy)