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 ; The test above shows that no FMF are needed, but show that we are not dropping FMF.
203 define float @fneg_fneg_fast(float %x, float %y) {
204 ; CHECK-LABEL: @fneg_fneg_fast(
205 ; CHECK-NEXT: [[DIV:%.*]] = fdiv fast float [[X:%.*]], [[Y:%.*]]
206 ; CHECK-NEXT: ret float [[DIV]]
208 %x.fneg = fsub float -0.0, %x
209 %y.fneg = fsub float -0.0, %y
210 %div = fdiv fast float %x.fneg, %y.fneg
214 define <2 x float> @fneg_fneg_vec(<2 x float> %x, <2 x float> %y) {
215 ; CHECK-LABEL: @fneg_fneg_vec(
216 ; CHECK-NEXT: [[DIV:%.*]] = fdiv <2 x float> [[X:%.*]], [[Y:%.*]]
217 ; CHECK-NEXT: ret <2 x float> [[DIV]]
219 %xneg = fsub <2 x float> <float -0.0, float -0.0>, %x
220 %yneg = fsub <2 x float> <float -0.0, float -0.0>, %y
221 %div = fdiv <2 x float> %xneg, %yneg
225 define <2 x float> @fneg_fneg_vec_undef_elts(<2 x float> %x, <2 x float> %y) {
226 ; CHECK-LABEL: @fneg_fneg_vec_undef_elts(
227 ; CHECK-NEXT: [[DIV:%.*]] = fdiv <2 x float> [[X:%.*]], [[Y:%.*]]
228 ; CHECK-NEXT: ret <2 x float> [[DIV]]
230 %xneg = fsub <2 x float> <float undef, float -0.0>, %x
231 %yneg = fsub <2 x float> <float -0.0, float undef>, %y
232 %div = fdiv <2 x float> %xneg, %yneg
236 define float @fneg_dividend_constant_divisor(float %x) {
237 ; CHECK-LABEL: @fneg_dividend_constant_divisor(
238 ; CHECK-NEXT: [[DIV:%.*]] = fdiv nsz float [[X:%.*]], -3.000000e+00
239 ; CHECK-NEXT: ret float [[DIV]]
241 %neg = fsub float -0.0, %x
242 %div = fdiv nsz float %neg, 3.0
246 define float @fneg_divisor_constant_dividend(float %x) {
247 ; CHECK-LABEL: @fneg_divisor_constant_dividend(
248 ; CHECK-NEXT: [[DIV:%.*]] = fdiv nnan float 3.000000e+00, [[X:%.*]]
249 ; CHECK-NEXT: ret float [[DIV]]
251 %neg = fsub float -0.0, %x
252 %div = fdiv nnan float -3.0, %neg
256 define <2 x float> @fneg_dividend_constant_divisor_vec(<2 x float> %x) {
257 ; CHECK-LABEL: @fneg_dividend_constant_divisor_vec(
258 ; CHECK-NEXT: [[DIV:%.*]] = fdiv ninf <2 x float> [[X:%.*]], <float -3.000000e+00, float 8.000000e+00>
259 ; CHECK-NEXT: ret <2 x float> [[DIV]]
261 %neg = fsub <2 x float> <float -0.0, float -0.0>, %x
262 %div = fdiv ninf <2 x float> %neg, <float 3.0, float -8.0>
266 define <2 x float> @fneg_dividend_constant_divisor_vec_undef_elt(<2 x float> %x) {
267 ; CHECK-LABEL: @fneg_dividend_constant_divisor_vec_undef_elt(
268 ; CHECK-NEXT: [[DIV:%.*]] = fdiv ninf <2 x float> [[X:%.*]], <float -3.000000e+00, float 8.000000e+00>
269 ; CHECK-NEXT: ret <2 x float> [[DIV]]
271 %neg = fsub <2 x float> <float undef, float -0.0>, %x
272 %div = fdiv ninf <2 x float> %neg, <float 3.0, float -8.0>
276 define <2 x float> @fneg_divisor_constant_dividend_vec(<2 x float> %x) {
277 ; CHECK-LABEL: @fneg_divisor_constant_dividend_vec(
278 ; CHECK-NEXT: [[DIV:%.*]] = fdiv afn <2 x float> <float 3.000000e+00, float -5.000000e+00>, [[X:%.*]]
279 ; CHECK-NEXT: ret <2 x float> [[DIV]]
281 %neg = fsub <2 x float> <float -0.0, float -0.0>, %x
282 %div = fdiv afn <2 x float> <float -3.0, float 5.0>, %neg
286 ; X / (X * Y) --> 1.0 / Y
288 define float @div_factor(float %x, float %y) {
289 ; CHECK-LABEL: @div_factor(
290 ; CHECK-NEXT: [[D:%.*]] = fdiv reassoc nnan float 1.000000e+00, [[Y:%.*]]
291 ; CHECK-NEXT: ret float [[D]]
293 %m = fmul float %x, %y
294 %d = fdiv nnan reassoc float %x, %m
298 ; We can't do the transform without 'nnan' because if x is NAN and y is a number, this should return NAN.
300 define float @div_factor_too_strict(float %x, float %y) {
301 ; CHECK-LABEL: @div_factor_too_strict(
302 ; CHECK-NEXT: [[M:%.*]] = fmul float [[X:%.*]], [[Y:%.*]]
303 ; CHECK-NEXT: [[D:%.*]] = fdiv reassoc float [[X]], [[M]]
304 ; CHECK-NEXT: ret float [[D]]
306 %m = fmul float %x, %y
307 %d = fdiv reassoc float %x, %m
311 ; Commute, verify vector types, and show that we are not dropping extra FMF.
312 ; X / (Y * X) --> 1.0 / Y
314 define <2 x float> @div_factor_commute(<2 x float> %x, <2 x float> %y) {
315 ; CHECK-LABEL: @div_factor_commute(
316 ; CHECK-NEXT: [[D:%.*]] = fdiv reassoc nnan ninf nsz <2 x float> <float 1.000000e+00, float 1.000000e+00>, [[Y:%.*]]
317 ; CHECK-NEXT: ret <2 x float> [[D]]
319 %m = fmul <2 x float> %y, %x
320 %d = fdiv nnan ninf nsz reassoc <2 x float> %x, %m
324 ; C1/(X*C2) => (C1/C2) / X
326 define <2 x float> @div_constant_dividend1(<2 x float> %x) {
327 ; CHECK-LABEL: @div_constant_dividend1(
328 ; CHECK-NEXT: [[T2:%.*]] = fdiv reassoc arcp <2 x float> <float 5.000000e+00, float 1.000000e+00>, [[X:%.*]]
329 ; CHECK-NEXT: ret <2 x float> [[T2]]
331 %t1 = fmul <2 x float> %x, <float 3.0e0, float 7.0e0>
332 %t2 = fdiv arcp reassoc <2 x float> <float 15.0e0, float 7.0e0>, %t1
336 define <2 x float> @div_constant_dividend1_arcp_only(<2 x float> %x) {
337 ; CHECK-LABEL: @div_constant_dividend1_arcp_only(
338 ; CHECK-NEXT: [[T1:%.*]] = fmul <2 x float> [[X:%.*]], <float 3.000000e+00, float 7.000000e+00>
339 ; CHECK-NEXT: [[T2:%.*]] = fdiv arcp <2 x float> <float 1.500000e+01, float 7.000000e+00>, [[T1]]
340 ; CHECK-NEXT: ret <2 x float> [[T2]]
342 %t1 = fmul <2 x float> %x, <float 3.0e0, float 7.0e0>
343 %t2 = fdiv arcp <2 x float> <float 15.0e0, float 7.0e0>, %t1
347 ; C1/(X/C2) => (C1*C2) / X
349 define <2 x float> @div_constant_dividend2(<2 x float> %x) {
350 ; CHECK-LABEL: @div_constant_dividend2(
351 ; CHECK-NEXT: [[T2:%.*]] = fdiv reassoc arcp <2 x float> <float 4.500000e+01, float 4.900000e+01>, [[X:%.*]]
352 ; CHECK-NEXT: ret <2 x float> [[T2]]
354 %t1 = fdiv <2 x float> %x, <float 3.0e0, float -7.0e0>
355 %t2 = fdiv arcp reassoc <2 x float> <float 15.0e0, float -7.0e0>, %t1
359 define <2 x float> @div_constant_dividend2_reassoc_only(<2 x float> %x) {
360 ; CHECK-LABEL: @div_constant_dividend2_reassoc_only(
361 ; CHECK-NEXT: [[T1:%.*]] = fdiv <2 x float> [[X:%.*]], <float 3.000000e+00, float -7.000000e+00>
362 ; CHECK-NEXT: [[T2:%.*]] = fdiv reassoc <2 x float> <float 1.500000e+01, float -7.000000e+00>, [[T1]]
363 ; CHECK-NEXT: ret <2 x float> [[T2]]
365 %t1 = fdiv <2 x float> %x, <float 3.0e0, float -7.0e0>
366 %t2 = fdiv reassoc <2 x float> <float 15.0e0, float -7.0e0>, %t1
370 ; C1/(C2/X) => (C1/C2) * X
371 ; This tests the combination of 2 folds: (C1 * X) / C2 --> (C1 / C2) * X
373 define <2 x float> @div_constant_dividend3(<2 x float> %x) {
374 ; CHECK-LABEL: @div_constant_dividend3(
375 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc arcp <2 x float> [[X:%.*]], <float 1.500000e+01, float -7.000000e+00>
376 ; CHECK-NEXT: [[T2:%.*]] = fmul reassoc arcp <2 x float> [[TMP1]], <float 0x3FD5555560000000, float 0x3FC24924A0000000>
377 ; CHECK-NEXT: ret <2 x float> [[T2]]
379 %t1 = fdiv <2 x float> <float 3.0e0, float 7.0e0>, %x
380 %t2 = fdiv arcp reassoc <2 x float> <float 15.0e0, float -7.0e0>, %t1