1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -instcombine < %s | FileCheck %s
4 define float @exact_inverse(float %x) {
5 ; CHECK-LABEL: @exact_inverse(
6 ; CHECK-NEXT: [[DIV:%.*]] = fmul float [[X:%.*]], 1.250000e-01
7 ; CHECK-NEXT: ret float [[DIV]]
9 %div = fdiv float %x, 8.0
13 ; Min normal float = 1.17549435E-38
15 define float @exact_inverse2(float %x) {
16 ; CHECK-LABEL: @exact_inverse2(
17 ; CHECK-NEXT: [[DIV:%.*]] = fmul float [[X:%.*]], 0x47D0000000000000
18 ; CHECK-NEXT: ret float [[DIV]]
20 %div = fdiv float %x, 0x3810000000000000
24 ; Max exponent = 1.70141183E+38; don't transform to multiply with denormal.
26 define float @exact_inverse_but_denorm(float %x) {
27 ; CHECK-LABEL: @exact_inverse_but_denorm(
28 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[X:%.*]], 0x47E0000000000000
29 ; CHECK-NEXT: ret float [[DIV]]
31 %div = fdiv float %x, 0x47E0000000000000
35 ; Denormal = float 1.40129846E-45; inverse can't be represented.
37 define float @not_exact_inverse2(float %x) {
38 ; CHECK-LABEL: @not_exact_inverse2(
39 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[X:%.*]], 0x36A0000000000000
40 ; CHECK-NEXT: ret float [[DIV]]
42 %div = fdiv float %x, 0x36A0000000000000
46 ; Fast math allows us to replace this fdiv.
48 define float @not_exact_but_allow_recip(float %x) {
49 ; CHECK-LABEL: @not_exact_but_allow_recip(
50 ; CHECK-NEXT: [[DIV:%.*]] = fmul arcp float [[X:%.*]], 0x3FD5555560000000
51 ; CHECK-NEXT: ret float [[DIV]]
53 %div = fdiv arcp float %x, 3.0
57 ; Fast math allows us to replace this fdiv, but we don't to avoid a denormal.
58 ; TODO: What if the function attributes tell us that denormals are flushed?
60 define float @not_exact_but_allow_recip_but_denorm(float %x) {
61 ; CHECK-LABEL: @not_exact_but_allow_recip_but_denorm(
62 ; CHECK-NEXT: [[DIV:%.*]] = fdiv arcp float [[X:%.*]], 0x47E0000100000000
63 ; CHECK-NEXT: ret float [[DIV]]
65 %div = fdiv arcp float %x, 0x47E0000100000000
69 define <2 x float> @exact_inverse_splat(<2 x float> %x) {
70 ; CHECK-LABEL: @exact_inverse_splat(
71 ; CHECK-NEXT: [[DIV:%.*]] = fmul <2 x float> [[X:%.*]], <float 2.500000e-01, float 2.500000e-01>
72 ; CHECK-NEXT: ret <2 x float> [[DIV]]
74 %div = fdiv <2 x float> %x, <float 4.0, float 4.0>
78 ; Fast math allows us to replace this fdiv.
80 define <2 x float> @not_exact_but_allow_recip_splat(<2 x float> %x) {
81 ; CHECK-LABEL: @not_exact_but_allow_recip_splat(
82 ; CHECK-NEXT: [[DIV:%.*]] = fmul arcp <2 x float> [[X:%.*]], <float 0x3FD5555560000000, float 0x3FD5555560000000>
83 ; CHECK-NEXT: ret <2 x float> [[DIV]]
85 %div = fdiv arcp <2 x float> %x, <float 3.0, float 3.0>
89 define <2 x float> @exact_inverse_vec(<2 x float> %x) {
90 ; CHECK-LABEL: @exact_inverse_vec(
91 ; CHECK-NEXT: [[DIV:%.*]] = fmul <2 x float> [[X:%.*]], <float 2.500000e-01, float 1.250000e-01>
92 ; CHECK-NEXT: ret <2 x float> [[DIV]]
94 %div = fdiv <2 x float> %x, <float 4.0, float 8.0>
98 define <2 x float> @not_exact_inverse_splat(<2 x float> %x) {
99 ; CHECK-LABEL: @not_exact_inverse_splat(
100 ; CHECK-NEXT: [[DIV:%.*]] = fdiv <2 x float> [[X:%.*]], <float 3.000000e+00, float 3.000000e+00>
101 ; CHECK-NEXT: ret <2 x float> [[DIV]]
103 %div = fdiv <2 x float> %x, <float 3.0, float 3.0>
107 define <2 x float> @not_exact_inverse_vec(<2 x float> %x) {
108 ; CHECK-LABEL: @not_exact_inverse_vec(
109 ; CHECK-NEXT: [[DIV:%.*]] = fdiv <2 x float> [[X:%.*]], <float 4.000000e+00, float 3.000000e+00>
110 ; CHECK-NEXT: ret <2 x float> [[DIV]]
112 %div = fdiv <2 x float> %x, <float 4.0, float 3.0>
116 define <2 x float> @not_exact_inverse_vec_arcp(<2 x float> %x) {
117 ; CHECK-LABEL: @not_exact_inverse_vec_arcp(
118 ; CHECK-NEXT: [[DIV:%.*]] = fmul arcp <2 x float> [[X:%.*]], <float 2.500000e-01, float 0x3FD5555560000000>
119 ; CHECK-NEXT: ret <2 x float> [[DIV]]
121 %div = fdiv arcp <2 x float> %x, <float 4.0, float 3.0>
125 define <2 x float> @not_exact_inverse_vec_arcp_with_undef_elt(<2 x float> %x) {
126 ; CHECK-LABEL: @not_exact_inverse_vec_arcp_with_undef_elt(
127 ; CHECK-NEXT: [[DIV:%.*]] = fdiv arcp <2 x float> [[X:%.*]], <float undef, float 3.000000e+00>
128 ; CHECK-NEXT: ret <2 x float> [[DIV]]
130 %div = fdiv arcp <2 x float> %x, <float undef, float 3.0>
134 ; (X / Y) / Z --> X / (Y * Z)
136 define float @div_with_div_numerator(float %x, float %y, float %z) {
137 ; CHECK-LABEL: @div_with_div_numerator(
138 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc arcp float [[Y:%.*]], [[Z:%.*]]
139 ; CHECK-NEXT: [[DIV2:%.*]] = fdiv reassoc arcp float [[X:%.*]], [[TMP1]]
140 ; CHECK-NEXT: ret float [[DIV2]]
142 %div1 = fdiv ninf float %x, %y
143 %div2 = fdiv arcp reassoc float %div1, %z
147 ; Z / (X / Y) --> (Z * Y) / X
149 define <2 x float> @div_with_div_denominator(<2 x float> %x, <2 x float> %y, <2 x float> %z) {
150 ; CHECK-LABEL: @div_with_div_denominator(
151 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc arcp <2 x float> [[Y:%.*]], [[Z:%.*]]
152 ; CHECK-NEXT: [[DIV2:%.*]] = fdiv reassoc arcp <2 x float> [[TMP1]], [[X:%.*]]
153 ; CHECK-NEXT: ret <2 x float> [[DIV2]]
155 %div1 = fdiv nnan <2 x float> %x, %y
156 %div2 = fdiv arcp reassoc <2 x float> %z, %div1
157 ret <2 x float> %div2
160 ; Don't create an extra multiply if we can't eliminate the first div.
162 declare void @use_f32(float)
164 define float @div_with_div_numerator_extra_use(float %x, float %y, float %z) {
165 ; CHECK-LABEL: @div_with_div_numerator_extra_use(
166 ; CHECK-NEXT: [[DIV1:%.*]] = fdiv float [[X:%.*]], [[Y:%.*]]
167 ; CHECK-NEXT: [[DIV2:%.*]] = fdiv fast float [[DIV1]], [[Z:%.*]]
168 ; CHECK-NEXT: call void @use_f32(float [[DIV1]])
169 ; CHECK-NEXT: ret float [[DIV2]]
171 %div1 = fdiv float %x, %y
172 %div2 = fdiv fast float %div1, %z
173 call void @use_f32(float %div1)
177 define float @div_with_div_denominator_extra_use(float %x, float %y, float %z) {
178 ; CHECK-LABEL: @div_with_div_denominator_extra_use(
179 ; CHECK-NEXT: [[DIV1:%.*]] = fdiv float [[X:%.*]], [[Y:%.*]]
180 ; CHECK-NEXT: [[DIV2:%.*]] = fdiv fast float [[Z:%.*]], [[DIV1]]
181 ; CHECK-NEXT: call void @use_f32(float [[DIV1]])
182 ; CHECK-NEXT: ret float [[DIV2]]
184 %div1 = fdiv float %x, %y
185 %div2 = fdiv fast float %z, %div1
186 call void @use_f32(float %div1)
190 define float @fneg_fneg(float %x, float %y) {
191 ; CHECK-LABEL: @fneg_fneg(
192 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[X:%.*]], [[Y:%.*]]
193 ; CHECK-NEXT: ret float [[DIV]]
195 %x.fneg = fsub float -0.0, %x
196 %y.fneg = fsub float -0.0, %y
197 %div = fdiv float %x.fneg, %y.fneg
201 define float @unary_fneg_unary_fneg(float %x, float %y) {
202 ; CHECK-LABEL: @unary_fneg_unary_fneg(
203 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[X:%.*]], [[Y:%.*]]
204 ; CHECK-NEXT: ret float [[DIV]]
206 %x.fneg = fneg float %x
207 %y.fneg = fneg float %y
208 %div = fdiv float %x.fneg, %y.fneg
212 define float @unary_fneg_fneg(float %x, float %y) {
213 ; CHECK-LABEL: @unary_fneg_fneg(
214 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[X:%.*]], [[Y:%.*]]
215 ; CHECK-NEXT: ret float [[DIV]]
217 %x.fneg = fneg float %x
218 %y.fneg = fsub float -0.0, %y
219 %div = fdiv float %x.fneg, %y.fneg
223 define float @fneg_unary_fneg(float %x, float %y) {
224 ; CHECK-LABEL: @fneg_unary_fneg(
225 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[X:%.*]], [[Y:%.*]]
226 ; CHECK-NEXT: ret float [[DIV]]
228 %x.fneg = fsub float -0.0, %x
229 %y.fneg = fneg float %y
230 %div = fdiv float %x.fneg, %y.fneg
234 ; The test above shows that no FMF are needed, but show that we are not dropping FMF.
236 define float @fneg_fneg_fast(float %x, float %y) {
237 ; CHECK-LABEL: @fneg_fneg_fast(
238 ; CHECK-NEXT: [[DIV:%.*]] = fdiv fast float [[X:%.*]], [[Y:%.*]]
239 ; CHECK-NEXT: ret float [[DIV]]
241 %x.fneg = fsub float -0.0, %x
242 %y.fneg = fsub float -0.0, %y
243 %div = fdiv fast float %x.fneg, %y.fneg
247 define float @unary_fneg_unary_fneg_fast(float %x, float %y) {
248 ; CHECK-LABEL: @unary_fneg_unary_fneg_fast(
249 ; CHECK-NEXT: [[DIV:%.*]] = fdiv fast float [[X:%.*]], [[Y:%.*]]
250 ; CHECK-NEXT: ret float [[DIV]]
252 %x.fneg = fneg float %x
253 %y.fneg = fneg float %y
254 %div = fdiv fast float %x.fneg, %y.fneg
258 define <2 x float> @fneg_fneg_vec(<2 x float> %x, <2 x float> %y) {
259 ; CHECK-LABEL: @fneg_fneg_vec(
260 ; CHECK-NEXT: [[DIV:%.*]] = fdiv <2 x float> [[X:%.*]], [[Y:%.*]]
261 ; CHECK-NEXT: ret <2 x float> [[DIV]]
263 %xneg = fsub <2 x float> <float -0.0, float -0.0>, %x
264 %yneg = fsub <2 x float> <float -0.0, float -0.0>, %y
265 %div = fdiv <2 x float> %xneg, %yneg
269 define <2 x float> @unary_fneg_unary_fneg_vec(<2 x float> %x, <2 x float> %y) {
270 ; CHECK-LABEL: @unary_fneg_unary_fneg_vec(
271 ; CHECK-NEXT: [[DIV:%.*]] = fdiv <2 x float> [[X:%.*]], [[Y:%.*]]
272 ; CHECK-NEXT: ret <2 x float> [[DIV]]
274 %xneg = fneg <2 x float> %x
275 %yneg = fneg <2 x float> %y
276 %div = fdiv <2 x float> %xneg, %yneg
280 define <2 x float> @fneg_unary_fneg_vec(<2 x float> %x, <2 x float> %y) {
281 ; CHECK-LABEL: @fneg_unary_fneg_vec(
282 ; CHECK-NEXT: [[DIV:%.*]] = fdiv <2 x float> [[X:%.*]], [[Y:%.*]]
283 ; CHECK-NEXT: ret <2 x float> [[DIV]]
285 %xneg = fsub <2 x float> <float -0.0, float -0.0>, %x
286 %yneg = fneg <2 x float> %y
287 %div = fdiv <2 x float> %xneg, %yneg
291 define <2 x float> @unary_fneg_fneg_vec(<2 x float> %x, <2 x float> %y) {
292 ; CHECK-LABEL: @unary_fneg_fneg_vec(
293 ; CHECK-NEXT: [[DIV:%.*]] = fdiv <2 x float> [[X:%.*]], [[Y:%.*]]
294 ; CHECK-NEXT: ret <2 x float> [[DIV]]
296 %xneg = fneg <2 x float> %x
297 %yneg = fsub <2 x float> <float -0.0, float -0.0>, %y
298 %div = fdiv <2 x float> %xneg, %yneg
302 define <2 x float> @fneg_fneg_vec_undef_elts(<2 x float> %x, <2 x float> %y) {
303 ; CHECK-LABEL: @fneg_fneg_vec_undef_elts(
304 ; CHECK-NEXT: [[DIV:%.*]] = fdiv <2 x float> [[X:%.*]], [[Y:%.*]]
305 ; CHECK-NEXT: ret <2 x float> [[DIV]]
307 %xneg = fsub <2 x float> <float undef, float -0.0>, %x
308 %yneg = fsub <2 x float> <float -0.0, float undef>, %y
309 %div = fdiv <2 x float> %xneg, %yneg
313 define float @fneg_dividend_constant_divisor(float %x) {
314 ; CHECK-LABEL: @fneg_dividend_constant_divisor(
315 ; CHECK-NEXT: [[DIV:%.*]] = fdiv nsz float [[X:%.*]], -3.000000e+00
316 ; CHECK-NEXT: ret float [[DIV]]
318 %neg = fsub float -0.0, %x
319 %div = fdiv nsz float %neg, 3.0
323 define float @unary_fneg_dividend_constant_divisor(float %x) {
324 ; CHECK-LABEL: @unary_fneg_dividend_constant_divisor(
325 ; CHECK-NEXT: [[DIV:%.*]] = fdiv nsz float [[X:%.*]], -3.000000e+00
326 ; CHECK-NEXT: ret float [[DIV]]
329 %div = fdiv nsz float %neg, 3.0
333 define float @fneg_divisor_constant_dividend(float %x) {
334 ; CHECK-LABEL: @fneg_divisor_constant_dividend(
335 ; CHECK-NEXT: [[DIV:%.*]] = fdiv nnan float 3.000000e+00, [[X:%.*]]
336 ; CHECK-NEXT: ret float [[DIV]]
338 %neg = fsub float -0.0, %x
339 %div = fdiv nnan float -3.0, %neg
343 define float @unary_fneg_divisor_constant_dividend(float %x) {
344 ; CHECK-LABEL: @unary_fneg_divisor_constant_dividend(
345 ; CHECK-NEXT: [[DIV:%.*]] = fdiv nnan float 3.000000e+00, [[X:%.*]]
346 ; CHECK-NEXT: ret float [[DIV]]
349 %div = fdiv nnan float -3.0, %neg
353 define <2 x float> @fneg_dividend_constant_divisor_vec(<2 x float> %x) {
354 ; CHECK-LABEL: @fneg_dividend_constant_divisor_vec(
355 ; CHECK-NEXT: [[DIV:%.*]] = fdiv ninf <2 x float> [[X:%.*]], <float -3.000000e+00, float 8.000000e+00>
356 ; CHECK-NEXT: ret <2 x float> [[DIV]]
358 %neg = fsub <2 x float> <float -0.0, float -0.0>, %x
359 %div = fdiv ninf <2 x float> %neg, <float 3.0, float -8.0>
363 define <2 x float> @unary_fneg_dividend_constant_divisor_vec(<2 x float> %x) {
364 ; CHECK-LABEL: @unary_fneg_dividend_constant_divisor_vec(
365 ; CHECK-NEXT: [[DIV:%.*]] = fdiv ninf <2 x float> [[X:%.*]], <float -3.000000e+00, float 8.000000e+00>
366 ; CHECK-NEXT: ret <2 x float> [[DIV]]
368 %neg = fneg <2 x float> %x
369 %div = fdiv ninf <2 x float> %neg, <float 3.0, float -8.0>
373 define <2 x float> @fneg_dividend_constant_divisor_vec_undef_elt(<2 x float> %x) {
374 ; CHECK-LABEL: @fneg_dividend_constant_divisor_vec_undef_elt(
375 ; CHECK-NEXT: [[DIV:%.*]] = fdiv ninf <2 x float> [[X:%.*]], <float -3.000000e+00, float 8.000000e+00>
376 ; CHECK-NEXT: ret <2 x float> [[DIV]]
378 %neg = fsub <2 x float> <float undef, float -0.0>, %x
379 %div = fdiv ninf <2 x float> %neg, <float 3.0, float -8.0>
383 define <2 x float> @fneg_divisor_constant_dividend_vec(<2 x float> %x) {
384 ; CHECK-LABEL: @fneg_divisor_constant_dividend_vec(
385 ; CHECK-NEXT: [[DIV:%.*]] = fdiv afn <2 x float> <float 3.000000e+00, float -5.000000e+00>, [[X:%.*]]
386 ; CHECK-NEXT: ret <2 x float> [[DIV]]
388 %neg = fsub <2 x float> <float -0.0, float -0.0>, %x
389 %div = fdiv afn <2 x float> <float -3.0, float 5.0>, %neg
393 define <2 x float> @unary_fneg_divisor_constant_dividend_vec(<2 x float> %x) {
394 ; CHECK-LABEL: @unary_fneg_divisor_constant_dividend_vec(
395 ; CHECK-NEXT: [[DIV:%.*]] = fdiv afn <2 x float> <float 3.000000e+00, float -5.000000e+00>, [[X:%.*]]
396 ; CHECK-NEXT: ret <2 x float> [[DIV]]
398 %neg = fneg <2 x float> %x
399 %div = fdiv afn <2 x float> <float -3.0, float 5.0>, %neg
404 ; X / (X * Y) --> 1.0 / Y
406 define float @div_factor(float %x, float %y) {
407 ; CHECK-LABEL: @div_factor(
408 ; CHECK-NEXT: [[D:%.*]] = fdiv reassoc nnan float 1.000000e+00, [[Y:%.*]]
409 ; CHECK-NEXT: ret float [[D]]
411 %m = fmul float %x, %y
412 %d = fdiv nnan reassoc float %x, %m
416 ; We can't do the transform without 'nnan' because if x is NAN and y is a number, this should return NAN.
418 define float @div_factor_too_strict(float %x, float %y) {
419 ; CHECK-LABEL: @div_factor_too_strict(
420 ; CHECK-NEXT: [[M:%.*]] = fmul float [[X:%.*]], [[Y:%.*]]
421 ; CHECK-NEXT: [[D:%.*]] = fdiv reassoc float [[X]], [[M]]
422 ; CHECK-NEXT: ret float [[D]]
424 %m = fmul float %x, %y
425 %d = fdiv reassoc float %x, %m
429 ; Commute, verify vector types, and show that we are not dropping extra FMF.
430 ; X / (Y * X) --> 1.0 / Y
432 define <2 x float> @div_factor_commute(<2 x float> %x, <2 x float> %y) {
433 ; CHECK-LABEL: @div_factor_commute(
434 ; CHECK-NEXT: [[D:%.*]] = fdiv reassoc nnan ninf nsz <2 x float> <float 1.000000e+00, float 1.000000e+00>, [[Y:%.*]]
435 ; CHECK-NEXT: ret <2 x float> [[D]]
437 %m = fmul <2 x float> %y, %x
438 %d = fdiv nnan ninf nsz reassoc <2 x float> %x, %m
442 ; C1/(X*C2) => (C1/C2) / X
444 define <2 x float> @div_constant_dividend1(<2 x float> %x) {
445 ; CHECK-LABEL: @div_constant_dividend1(
446 ; CHECK-NEXT: [[T2:%.*]] = fdiv reassoc arcp <2 x float> <float 5.000000e+00, float 1.000000e+00>, [[X:%.*]]
447 ; CHECK-NEXT: ret <2 x float> [[T2]]
449 %t1 = fmul <2 x float> %x, <float 3.0e0, float 7.0e0>
450 %t2 = fdiv arcp reassoc <2 x float> <float 15.0e0, float 7.0e0>, %t1
454 define <2 x float> @div_constant_dividend1_arcp_only(<2 x float> %x) {
455 ; CHECK-LABEL: @div_constant_dividend1_arcp_only(
456 ; CHECK-NEXT: [[T1:%.*]] = fmul <2 x float> [[X:%.*]], <float 3.000000e+00, float 7.000000e+00>
457 ; CHECK-NEXT: [[T2:%.*]] = fdiv arcp <2 x float> <float 1.500000e+01, float 7.000000e+00>, [[T1]]
458 ; CHECK-NEXT: ret <2 x float> [[T2]]
460 %t1 = fmul <2 x float> %x, <float 3.0e0, float 7.0e0>
461 %t2 = fdiv arcp <2 x float> <float 15.0e0, float 7.0e0>, %t1
465 ; C1/(X/C2) => (C1*C2) / X
467 define <2 x float> @div_constant_dividend2(<2 x float> %x) {
468 ; CHECK-LABEL: @div_constant_dividend2(
469 ; CHECK-NEXT: [[T2:%.*]] = fdiv reassoc arcp <2 x float> <float 4.500000e+01, float 4.900000e+01>, [[X:%.*]]
470 ; CHECK-NEXT: ret <2 x float> [[T2]]
472 %t1 = fdiv <2 x float> %x, <float 3.0e0, float -7.0e0>
473 %t2 = fdiv arcp reassoc <2 x float> <float 15.0e0, float -7.0e0>, %t1
477 define <2 x float> @div_constant_dividend2_reassoc_only(<2 x float> %x) {
478 ; CHECK-LABEL: @div_constant_dividend2_reassoc_only(
479 ; CHECK-NEXT: [[T1:%.*]] = fdiv <2 x float> [[X:%.*]], <float 3.000000e+00, float -7.000000e+00>
480 ; CHECK-NEXT: [[T2:%.*]] = fdiv reassoc <2 x float> <float 1.500000e+01, float -7.000000e+00>, [[T1]]
481 ; CHECK-NEXT: ret <2 x float> [[T2]]
483 %t1 = fdiv <2 x float> %x, <float 3.0e0, float -7.0e0>
484 %t2 = fdiv reassoc <2 x float> <float 15.0e0, float -7.0e0>, %t1
488 ; C1/(C2/X) => (C1/C2) * X
489 ; This tests the combination of 2 folds: (C1 * X) / C2 --> (C1 / C2) * X
491 define <2 x float> @div_constant_dividend3(<2 x float> %x) {
492 ; CHECK-LABEL: @div_constant_dividend3(
493 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc arcp <2 x float> [[X:%.*]], <float 1.500000e+01, float -7.000000e+00>
494 ; CHECK-NEXT: [[T2:%.*]] = fmul reassoc arcp <2 x float> [[TMP1]], <float 0x3FD5555560000000, float 0x3FC24924A0000000>
495 ; CHECK-NEXT: ret <2 x float> [[T2]]
497 %t1 = fdiv <2 x float> <float 3.0e0, float 7.0e0>, %x
498 %t2 = fdiv arcp reassoc <2 x float> <float 15.0e0, float -7.0e0>, %t1