1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 ; testing-case "float fold(float a) { return 1.2f * a * 2.3f; }"
5 ; 1.2f and 2.3f is supposed to be fold.
6 define float @fold(float %a) {
8 ; CHECK-NEXT: [[MUL1:%.*]] = fmul fast float [[A:%.*]], 0x4006147AE0000000
9 ; CHECK-NEXT: ret float [[MUL1]]
11 %mul = fmul fast float %a, 0x3FF3333340000000
12 %mul1 = fmul fast float %mul, 0x4002666660000000
16 ; Same testing-case as the one used in fold() except that the operators have
18 define float @notfold(float %a) {
19 ; CHECK-LABEL: @notfold(
20 ; CHECK-NEXT: [[MUL:%.*]] = fmul fast float [[A:%.*]], 0x3FF3333340000000
21 ; CHECK-NEXT: [[MUL1:%.*]] = fmul float [[MUL]], 0x4002666660000000
22 ; CHECK-NEXT: ret float [[MUL1]]
24 %mul = fmul fast float %a, 0x3FF3333340000000
25 %mul1 = fmul float %mul, 0x4002666660000000
29 define float @fold2(float %a) {
30 ; CHECK-LABEL: @fold2(
31 ; CHECK-NEXT: [[MUL1:%.*]] = fmul fast float [[A:%.*]], 0x4006147AE0000000
32 ; CHECK-NEXT: ret float [[MUL1]]
34 %mul = fmul float %a, 0x3FF3333340000000
35 %mul1 = fmul fast float %mul, 0x4002666660000000
39 ; C * f1 + f1 = (C+1) * f1
40 ; TODO: The particular case where C is 2 (so the folded result is 3.0*f1) is
41 ; always safe, and so doesn't need any FMF.
42 ; That is, (x + x + x) and (3*x) each have only a single rounding.
43 define double @fold3(double %f1) {
44 ; CHECK-LABEL: @fold3(
45 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast double [[F1:%.*]], 6.000000e+00
46 ; CHECK-NEXT: ret double [[TMP1]]
48 %t1 = fmul fast double 5.000000e+00, %f1
49 %t2 = fadd fast double %f1, %t1
53 ; Check again with 'reassoc' and 'nsz' ('nsz' not technically required).
54 define double @fold3_reassoc_nsz(double %f1) {
55 ; CHECK-LABEL: @fold3_reassoc_nsz(
56 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc nsz double [[F1:%.*]], 6.000000e+00
57 ; CHECK-NEXT: ret double [[TMP1]]
59 %t1 = fmul reassoc nsz double 5.000000e+00, %f1
60 %t2 = fadd reassoc nsz double %f1, %t1
64 ; TODO: This doesn't require 'nsz'. It should fold to f1 * 6.0.
65 define double @fold3_reassoc(double %f1) {
66 ; CHECK-LABEL: @fold3_reassoc(
67 ; CHECK-NEXT: [[T1:%.*]] = fmul reassoc double [[F1:%.*]], 5.000000e+00
68 ; CHECK-NEXT: [[T2:%.*]] = fadd reassoc double [[T1]], [[F1]]
69 ; CHECK-NEXT: ret double [[T2]]
71 %t1 = fmul reassoc double 5.000000e+00, %f1
72 %t2 = fadd reassoc double %f1, %t1
76 ; (C1 - X) + (C2 - Y) => (C1+C2) - (X + Y)
77 define float @fold4(float %f1, float %f2) {
78 ; CHECK-LABEL: @fold4(
79 ; CHECK-NEXT: [[TMP1:%.*]] = fadd fast float [[F1:%.*]], [[F2:%.*]]
80 ; CHECK-NEXT: [[TMP2:%.*]] = fsub fast float 9.000000e+00, [[TMP1]]
81 ; CHECK-NEXT: ret float [[TMP2]]
83 %sub = fsub float 4.000000e+00, %f1
84 %sub1 = fsub float 5.000000e+00, %f2
85 %add = fadd fast float %sub, %sub1
89 ; Check again with 'reassoc' and 'nsz' ('nsz' not technically required).
90 define float @fold4_reassoc_nsz(float %f1, float %f2) {
91 ; CHECK-LABEL: @fold4_reassoc_nsz(
92 ; CHECK-NEXT: [[TMP1:%.*]] = fadd reassoc nsz float [[F1:%.*]], [[F2:%.*]]
93 ; CHECK-NEXT: [[TMP2:%.*]] = fsub reassoc nsz float 9.000000e+00, [[TMP1]]
94 ; CHECK-NEXT: ret float [[TMP2]]
96 %sub = fsub float 4.000000e+00, %f1
97 %sub1 = fsub float 5.000000e+00, %f2
98 %add = fadd reassoc nsz float %sub, %sub1
102 ; TODO: This doesn't require 'nsz'. It should fold to (9.0 - (f1 + f2)).
103 define float @fold4_reassoc(float %f1, float %f2) {
104 ; CHECK-LABEL: @fold4_reassoc(
105 ; CHECK-NEXT: [[SUB:%.*]] = fsub float 4.000000e+00, [[F1:%.*]]
106 ; CHECK-NEXT: [[SUB1:%.*]] = fsub float 5.000000e+00, [[F2:%.*]]
107 ; CHECK-NEXT: [[ADD:%.*]] = fadd reassoc float [[SUB]], [[SUB1]]
108 ; CHECK-NEXT: ret float [[ADD]]
110 %sub = fsub float 4.000000e+00, %f1
111 %sub1 = fsub float 5.000000e+00, %f2
112 %add = fadd reassoc float %sub, %sub1
116 ; (X + C1) + C2 => X + (C1 + C2)
117 define float @fold5(float %f1) {
118 ; CHECK-LABEL: @fold5(
119 ; CHECK-NEXT: [[ADD1:%.*]] = fadd fast float [[F1:%.*]], 9.000000e+00
120 ; CHECK-NEXT: ret float [[ADD1]]
122 %add = fadd float %f1, 4.000000e+00
123 %add1 = fadd fast float %add, 5.000000e+00
127 ; Check again with 'reassoc' and 'nsz' ('nsz' not technically required).
128 define float @fold5_reassoc_nsz(float %f1) {
129 ; CHECK-LABEL: @fold5_reassoc_nsz(
130 ; CHECK-NEXT: [[ADD1:%.*]] = fadd reassoc nsz float [[F1:%.*]], 9.000000e+00
131 ; CHECK-NEXT: ret float [[ADD1]]
133 %add = fadd float %f1, 4.000000e+00
134 %add1 = fadd reassoc nsz float %add, 5.000000e+00
138 ; TODO: This doesn't require 'nsz'. It should fold to f1 + 9.0
139 define float @fold5_reassoc(float %f1) {
140 ; CHECK-LABEL: @fold5_reassoc(
141 ; CHECK-NEXT: [[ADD:%.*]] = fadd float [[F1:%.*]], 4.000000e+00
142 ; CHECK-NEXT: [[ADD1:%.*]] = fadd reassoc float [[ADD]], 5.000000e+00
143 ; CHECK-NEXT: ret float [[ADD1]]
145 %add = fadd float %f1, 4.000000e+00
146 %add1 = fadd reassoc float %add, 5.000000e+00
150 ; (X + X) + X + X => 4.0 * X
151 define float @fold6(float %f1) {
152 ; CHECK-LABEL: @fold6(
153 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[F1:%.*]], 4.000000e+00
154 ; CHECK-NEXT: ret float [[TMP1]]
156 %t1 = fadd fast float %f1, %f1
157 %t2 = fadd fast float %f1, %t1
158 %t3 = fadd fast float %t2, %f1
162 ; Check again with 'reassoc' and 'nsz' ('nsz' not technically required).
163 define float @fold6_reassoc_nsz(float %f1) {
164 ; CHECK-LABEL: @fold6_reassoc_nsz(
165 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc nsz float [[F1:%.*]], 4.000000e+00
166 ; CHECK-NEXT: ret float [[TMP1]]
168 %t1 = fadd reassoc nsz float %f1, %f1
169 %t2 = fadd reassoc nsz float %f1, %t1
170 %t3 = fadd reassoc nsz float %t2, %f1
174 ; TODO: This doesn't require 'nsz'. It should fold to f1 * 4.0.
175 define float @fold6_reassoc(float %f1) {
176 ; CHECK-LABEL: @fold6_reassoc(
177 ; CHECK-NEXT: [[T1:%.*]] = fadd reassoc float [[F1:%.*]], [[F1]]
178 ; CHECK-NEXT: [[T2:%.*]] = fadd reassoc float [[T1]], [[F1]]
179 ; CHECK-NEXT: [[T3:%.*]] = fadd reassoc float [[T2]], [[F1]]
180 ; CHECK-NEXT: ret float [[T3]]
182 %t1 = fadd reassoc float %f1, %f1
183 %t2 = fadd reassoc float %f1, %t1
184 %t3 = fadd reassoc float %t2, %f1
188 ; C1 * X + (X + X) = (C1 + 2) * X
189 define float @fold7(float %f1) {
190 ; CHECK-LABEL: @fold7(
191 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[F1:%.*]], 7.000000e+00
192 ; CHECK-NEXT: ret float [[TMP1]]
194 %t1 = fmul fast float %f1, 5.000000e+00
195 %t2 = fadd fast float %f1, %f1
196 %t3 = fadd fast float %t1, %t2
200 ; Check again with 'reassoc' and 'nsz' ('nsz' not technically required).
201 define float @fold7_reassoc_nsz(float %f1) {
202 ; CHECK-LABEL: @fold7_reassoc_nsz(
203 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc nsz float [[F1:%.*]], 7.000000e+00
204 ; CHECK-NEXT: ret float [[TMP1]]
206 %t1 = fmul reassoc nsz float %f1, 5.000000e+00
207 %t2 = fadd reassoc nsz float %f1, %f1
208 %t3 = fadd reassoc nsz float %t1, %t2
212 ; TODO: This doesn't require 'nsz'. It should fold to f1 * 7.0.
213 define float @fold7_reassoc(float %f1) {
214 ; CHECK-LABEL: @fold7_reassoc(
215 ; CHECK-NEXT: [[T1:%.*]] = fmul reassoc float [[F1:%.*]], 5.000000e+00
216 ; CHECK-NEXT: [[T2:%.*]] = fadd reassoc float [[F1]], [[F1]]
217 ; CHECK-NEXT: [[T3:%.*]] = fadd reassoc float [[T1]], [[T2]]
218 ; CHECK-NEXT: ret float [[T3]]
220 %t1 = fmul reassoc float %f1, 5.000000e+00
221 %t2 = fadd reassoc float %f1, %f1
222 %t3 = fadd reassoc float %t1, %t2
226 ; (X + X) + (X + X) + X => 5.0 * X
227 define float @fold8(float %f1) {
228 ; CHECK-LABEL: @fold8(
229 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[F1:%.*]], 5.000000e+00
230 ; CHECK-NEXT: ret float [[TMP1]]
232 %t1 = fadd fast float %f1, %f1
233 %t2 = fadd fast float %f1, %f1
234 %t3 = fadd fast float %t1, %t2
235 %t4 = fadd fast float %t3, %f1
239 ; Check again with 'reassoc' and 'nsz' ('nsz' not technically required).
240 define float @fold8_reassoc_nsz(float %f1) {
241 ; CHECK-LABEL: @fold8_reassoc_nsz(
242 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc nsz float [[F1:%.*]], 5.000000e+00
243 ; CHECK-NEXT: ret float [[TMP1]]
245 %t1 = fadd reassoc nsz float %f1, %f1
246 %t2 = fadd reassoc nsz float %f1, %f1
247 %t3 = fadd reassoc nsz float %t1, %t2
248 %t4 = fadd reassoc nsz float %t3, %f1
252 ; TODO: This doesn't require 'nsz'. It should fold to f1 * 5.0.
253 define float @fold8_reassoc(float %f1) {
254 ; CHECK-LABEL: @fold8_reassoc(
255 ; CHECK-NEXT: [[T1:%.*]] = fadd reassoc float [[F1:%.*]], [[F1]]
256 ; CHECK-NEXT: [[T2:%.*]] = fadd reassoc float [[F1]], [[F1]]
257 ; CHECK-NEXT: [[T3:%.*]] = fadd reassoc float [[T1]], [[T2]]
258 ; CHECK-NEXT: [[T4:%.*]] = fadd reassoc float [[T3]], [[F1]]
259 ; CHECK-NEXT: ret float [[T4]]
261 %t1 = fadd reassoc float %f1, %f1
262 %t2 = fadd reassoc float %f1, %f1
263 %t3 = fadd reassoc float %t1, %t2
264 %t4 = fadd reassoc float %t3, %f1
270 define float @fsub_fadd_common_op_fneg(float %x, float %y) {
271 ; CHECK-LABEL: @fsub_fadd_common_op_fneg(
272 ; CHECK-NEXT: [[R:%.*]] = fsub fast float -0.000000e+00, [[X:%.*]]
273 ; CHECK-NEXT: ret float [[R]]
275 %a = fadd float %x, %y
276 %r = fsub fast float %y, %a
281 ; Check again with 'reassoc' and 'nsz'.
282 ; nsz is required because: 0.0 - (0.0 + 0.0) -> 0.0, not -0.0
284 define float @fsub_fadd_common_op_fneg_reassoc_nsz(float %x, float %y) {
285 ; CHECK-LABEL: @fsub_fadd_common_op_fneg_reassoc_nsz(
286 ; CHECK-NEXT: [[R:%.*]] = fsub reassoc nsz float -0.000000e+00, [[X:%.*]]
287 ; CHECK-NEXT: ret float [[R]]
289 %a = fadd float %x, %y
290 %r = fsub reassoc nsz float %y, %a
296 define <2 x float> @fsub_fadd_common_op_fneg_vec(<2 x float> %x, <2 x float> %y) {
297 ; CHECK-LABEL: @fsub_fadd_common_op_fneg_vec(
298 ; CHECK-NEXT: [[R:%.*]] = fsub reassoc nsz <2 x float> <float -0.000000e+00, float -0.000000e+00>, [[X:%.*]]
299 ; CHECK-NEXT: ret <2 x float> [[R]]
301 %a = fadd <2 x float> %x, %y
302 %r = fsub nsz reassoc <2 x float> %y, %a
307 ; Commute operands of the 'add'.
309 define float @fsub_fadd_common_op_fneg_commute(float %x, float %y) {
310 ; CHECK-LABEL: @fsub_fadd_common_op_fneg_commute(
311 ; CHECK-NEXT: [[R:%.*]] = fsub reassoc nsz float -0.000000e+00, [[X:%.*]]
312 ; CHECK-NEXT: ret float [[R]]
314 %a = fadd float %y, %x
315 %r = fsub reassoc nsz float %y, %a
321 define <2 x float> @fsub_fadd_common_op_fneg_commute_vec(<2 x float> %x, <2 x float> %y) {
322 ; CHECK-LABEL: @fsub_fadd_common_op_fneg_commute_vec(
323 ; CHECK-NEXT: [[R:%.*]] = fsub reassoc nsz <2 x float> <float -0.000000e+00, float -0.000000e+00>, [[X:%.*]]
324 ; CHECK-NEXT: ret <2 x float> [[R]]
326 %a = fadd <2 x float> %y, %x
327 %r = fsub reassoc nsz <2 x float> %y, %a
332 ; nsz is required because: (0.0 - 0.0) - 0.0 -> 0.0, not -0.0
334 define float @fsub_fsub_common_op_fneg(float %x, float %y) {
335 ; CHECK-LABEL: @fsub_fsub_common_op_fneg(
336 ; CHECK-NEXT: [[R:%.*]] = fsub reassoc nsz float -0.000000e+00, [[X:%.*]]
337 ; CHECK-NEXT: ret float [[R]]
339 %s = fsub float %y, %x
340 %r = fsub reassoc nsz float %s, %y
346 define <2 x float> @fsub_fsub_common_op_fneg_vec(<2 x float> %x, <2 x float> %y) {
347 ; CHECK-LABEL: @fsub_fsub_common_op_fneg_vec(
348 ; CHECK-NEXT: [[R:%.*]] = fsub reassoc nsz <2 x float> <float -0.000000e+00, float -0.000000e+00>, [[X:%.*]]
349 ; CHECK-NEXT: ret <2 x float> [[R]]
351 %s = fsub <2 x float> %y, %x
352 %r = fsub reassoc nsz <2 x float> %s, %y
356 ; TODO: This doesn't require 'nsz'. It should fold to 0 - f2
357 define float @fold9_reassoc(float %f1, float %f2) {
358 ; CHECK-LABEL: @fold9_reassoc(
359 ; CHECK-NEXT: [[T1:%.*]] = fadd float [[F1:%.*]], [[F2:%.*]]
360 ; CHECK-NEXT: [[T3:%.*]] = fsub reassoc float [[F1]], [[T1]]
361 ; CHECK-NEXT: ret float [[T3]]
363 %t1 = fadd float %f1, %f2
364 %t3 = fsub reassoc float %f1, %t1
368 ; Let C3 = C1 + C2. (f1 + C1) + (f2 + C2) => (f1 + f2) + C3 instead of
369 ; "(f1 + C3) + f2" or "(f2 + C3) + f1". Placing constant-addend at the
370 ; top of resulting simplified expression tree may potentially reveal some
371 ; optimization opportunities in the super-expression trees.
373 define float @fold10(float %f1, float %f2) {
374 ; CHECK-LABEL: @fold10(
375 ; CHECK-NEXT: [[T2:%.*]] = fadd fast float [[F1:%.*]], [[F2:%.*]]
376 ; CHECK-NEXT: [[T3:%.*]] = fadd fast float [[T2]], -1.000000e+00
377 ; CHECK-NEXT: ret float [[T3]]
379 %t1 = fadd fast float 2.000000e+00, %f1
380 %t2 = fsub fast float %f2, 3.000000e+00
381 %t3 = fadd fast float %t1, %t2
385 ; Check again with 'reassoc' and 'nsz'.
386 ; TODO: We may be able to remove the 'nsz' requirement.
387 define float @fold10_reassoc_nsz(float %f1, float %f2) {
388 ; CHECK-LABEL: @fold10_reassoc_nsz(
389 ; CHECK-NEXT: [[T2:%.*]] = fadd reassoc nsz float [[F1:%.*]], [[F2:%.*]]
390 ; CHECK-NEXT: [[T3:%.*]] = fadd reassoc nsz float [[T2]], -1.000000e+00
391 ; CHECK-NEXT: ret float [[T3]]
393 %t1 = fadd reassoc nsz float 2.000000e+00, %f1
394 %t2 = fsub reassoc nsz float %f2, 3.000000e+00
395 %t3 = fadd reassoc nsz float %t1, %t2
399 ; Observe that the fold is not done with only reassoc (the instructions are
400 ; canonicalized, but not folded).
401 ; TODO: As noted above, 'nsz' may not be required for this to be fully folded.
402 define float @fold10_reassoc(float %f1, float %f2) {
403 ; CHECK-LABEL: @fold10_reassoc(
404 ; CHECK-NEXT: [[T1:%.*]] = fadd reassoc float [[F1:%.*]], 2.000000e+00
405 ; CHECK-NEXT: [[T2:%.*]] = fadd reassoc float [[F2:%.*]], -3.000000e+00
406 ; CHECK-NEXT: [[T3:%.*]] = fadd reassoc float [[T1]], [[T2]]
407 ; CHECK-NEXT: ret float [[T3]]
409 %t1 = fadd reassoc float 2.000000e+00, %f1
410 %t2 = fsub reassoc float %f2, 3.000000e+00
411 %t3 = fadd reassoc float %t1, %t2
415 ; This used to crash/miscompile.
417 define float @fail1(float %f1, float %f2) {
418 ; CHECK-LABEL: @fail1(
419 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[F1:%.*]], 3.000000e+00
420 ; CHECK-NEXT: [[TMP2:%.*]] = fadd fast float [[TMP1]], -3.000000e+00
421 ; CHECK-NEXT: ret float [[TMP2]]
423 %conv3 = fadd fast float %f1, -1.000000e+00
424 %add = fadd fast float %conv3, %conv3
425 %add2 = fadd fast float %add, %conv3
429 define double @fail2(double %f1, double %f2) {
430 ; CHECK-LABEL: @fail2(
431 ; CHECK-NEXT: [[TMP1:%.*]] = fadd fast double [[F2:%.*]], [[F2]]
432 ; CHECK-NEXT: [[TMP2:%.*]] = fsub fast double -0.000000e+00, [[TMP1]]
433 ; CHECK-NEXT: ret double [[TMP2]]
435 %t1 = fsub fast double %f1, %f2
436 %t2 = fadd fast double %f1, %f2
437 %t3 = fsub fast double %t1, %t2
441 ; (X * C) - X --> X * (C - 1.0)
443 define float @fsub_op0_fmul_const(float %x) {
444 ; CHECK-LABEL: @fsub_op0_fmul_const(
445 ; CHECK-NEXT: [[SUB:%.*]] = fmul reassoc nsz float [[X:%.*]], 6.000000e+00
446 ; CHECK-NEXT: ret float [[SUB]]
448 %mul = fmul float %x, 7.0
449 %sub = fsub reassoc nsz float %mul, %x
453 ; (X * C) - X --> X * (C - 1.0)
455 define <2 x float> @fsub_op0_fmul_const_vec(<2 x float> %x) {
456 ; CHECK-LABEL: @fsub_op0_fmul_const_vec(
457 ; CHECK-NEXT: [[SUB:%.*]] = fmul reassoc nsz <2 x float> [[X:%.*]], <float 6.000000e+00, float -4.300000e+01>
458 ; CHECK-NEXT: ret <2 x float> [[SUB]]
460 %mul = fmul <2 x float> %x, <float 7.0, float -42.0>
461 %sub = fsub reassoc nsz <2 x float> %mul, %x
465 ; X - (X * C) --> X * (1.0 - C)
467 define float @fsub_op1_fmul_const(float %x) {
468 ; CHECK-LABEL: @fsub_op1_fmul_const(
469 ; CHECK-NEXT: [[SUB:%.*]] = fmul reassoc nsz float [[X:%.*]], -6.000000e+00
470 ; CHECK-NEXT: ret float [[SUB]]
472 %mul = fmul float %x, 7.0
473 %sub = fsub reassoc nsz float %x, %mul
477 ; X - (X * C) --> X * (1.0 - C)
479 define <2 x float> @fsub_op1_fmul_const_vec(<2 x float> %x) {
480 ; CHECK-LABEL: @fsub_op1_fmul_const_vec(
481 ; CHECK-NEXT: [[SUB:%.*]] = fmul reassoc nsz <2 x float> [[X:%.*]], <float -6.000000e+00, float 1.000000e+00>
482 ; CHECK-NEXT: ret <2 x float> [[SUB]]
484 %mul = fmul <2 x float> %x, <float 7.0, float 0.0>
485 %sub = fsub reassoc nsz <2 x float> %x, %mul
489 ; Verify the fold is not done with only 'reassoc' ('nsz' is required).
491 define float @fsub_op0_fmul_const_wrong_FMF(float %x) {
492 ; CHECK-LABEL: @fsub_op0_fmul_const_wrong_FMF(
493 ; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc float [[X:%.*]], 7.000000e+00
494 ; CHECK-NEXT: [[SUB:%.*]] = fsub reassoc float [[MUL]], [[X]]
495 ; CHECK-NEXT: ret float [[SUB]]
497 %mul = fmul reassoc float %x, 7.0
498 %sub = fsub reassoc float %mul, %x
502 ; (select X+Y, X-Y) => X + (select Y, -Y)
503 ; This is always safe. No FMF required.
504 define float @fold16(float %x, float %y) {
505 ; CHECK-LABEL: @fold16(
506 ; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[X:%.*]], [[Y:%.*]]
507 ; CHECK-NEXT: [[TMP1:%.*]] = fsub float -0.000000e+00, [[Y]]
508 ; CHECK-NEXT: [[R_P:%.*]] = select i1 [[CMP]], float [[Y]], float [[TMP1]]
509 ; CHECK-NEXT: [[R:%.*]] = fadd float [[R_P]], [[X]]
510 ; CHECK-NEXT: ret float [[R]]
512 %cmp = fcmp ogt float %x, %y
513 %plus = fadd float %x, %y
514 %minus = fsub float %x, %y
515 %r = select i1 %cmp, float %plus, float %minus
519 ; =========================================================================
521 ; Testing-cases about negation
523 ; =========================================================================
524 define float @fneg1(float %f1, float %f2) {
525 ; CHECK-LABEL: @fneg1(
526 ; CHECK-NEXT: [[MUL:%.*]] = fmul float [[F1:%.*]], [[F2:%.*]]
527 ; CHECK-NEXT: ret float [[MUL]]
529 %sub = fsub float -0.000000e+00, %f1
530 %sub1 = fsub nsz float 0.000000e+00, %f2
531 %mul = fmul float %sub, %sub1
535 define float @fneg2(float %x) {
536 ; CHECK-LABEL: @fneg2(
537 ; CHECK-NEXT: [[SUB:%.*]] = fsub nsz float -0.000000e+00, [[X:%.*]]
538 ; CHECK-NEXT: ret float [[SUB]]
540 %sub = fsub nsz float 0.0, %x
544 define <2 x float> @fneg2_vec_undef(<2 x float> %x) {
545 ; CHECK-LABEL: @fneg2_vec_undef(
546 ; CHECK-NEXT: [[SUB:%.*]] = fsub nsz <2 x float> <float -0.000000e+00, float -0.000000e+00>, [[X:%.*]]
547 ; CHECK-NEXT: ret <2 x float> [[SUB]]
549 %sub = fsub nsz <2 x float> <float undef, float 0.0>, %x
553 ; =========================================================================
555 ; Testing-cases about div
557 ; =========================================================================
559 ; X/C1 / C2 => X * (1/(C2*C1))
560 define float @fdiv1(float %x) {
561 ; CHECK-LABEL: @fdiv1(
562 ; CHECK-NEXT: [[DIV1:%.*]] = fmul fast float [[X:%.*]], 0x3FD7303B60000000
563 ; CHECK-NEXT: ret float [[DIV1]]
565 %div = fdiv float %x, 0x3FF3333340000000
566 %div1 = fdiv fast float %div, 0x4002666660000000
568 ; 0x3FF3333340000000 = 1.2f
569 ; 0x4002666660000000 = 2.3f
570 ; 0x3FD7303B60000000 = 0.36231884057971014492
573 ; X*C1 / C2 => X * (C1/C2)
574 define float @fdiv2(float %x) {
575 ; CHECK-LABEL: @fdiv2(
576 ; CHECK-NEXT: [[DIV1:%.*]] = fmul fast float [[X:%.*]], 0x3FE0B21660000000
577 ; CHECK-NEXT: ret float [[DIV1]]
579 %mul = fmul float %x, 0x3FF3333340000000
580 %div1 = fdiv fast float %mul, 0x4002666660000000
583 ; 0x3FF3333340000000 = 1.2f
584 ; 0x4002666660000000 = 2.3f
585 ; 0x3FE0B21660000000 = 0.52173918485641479492
588 define <2 x float> @fdiv2_vec(<2 x float> %x) {
589 ; CHECK-LABEL: @fdiv2_vec(
590 ; CHECK-NEXT: [[DIV1:%.*]] = fmul fast <2 x float> [[X:%.*]], <float 3.000000e+00, float 3.000000e+00>
591 ; CHECK-NEXT: ret <2 x float> [[DIV1]]
593 %mul = fmul <2 x float> %x, <float 6.0, float 9.0>
594 %div1 = fdiv fast <2 x float> %mul, <float 2.0, float 3.0>
595 ret <2 x float> %div1
598 ; "X/C1 / C2 => X * (1/(C2*C1))" is disabled (for now) is C2/C1 is a denormal
600 define float @fdiv3(float %x) {
601 ; CHECK-LABEL: @fdiv3(
602 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[X:%.*]], 0x3FDBD37A80000000
603 ; CHECK-NEXT: [[DIV1:%.*]] = fdiv fast float [[TMP1]], 0x47EFFFFFE0000000
604 ; CHECK-NEXT: ret float [[DIV1]]
606 %div = fdiv float %x, 0x47EFFFFFE0000000
607 %div1 = fdiv fast float %div, 0x4002666660000000
611 ; "X*C1 / C2 => X * (C1/C2)" is disabled if C1/C2 is a denormal
612 define float @fdiv4(float %x) {
613 ; CHECK-LABEL: @fdiv4(
614 ; CHECK-NEXT: [[MUL:%.*]] = fmul float [[X:%.*]], 0x47EFFFFFE0000000
615 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[MUL]], 0x3FC99999A0000000
616 ; CHECK-NEXT: ret float [[DIV]]
618 %mul = fmul float %x, 0x47EFFFFFE0000000
619 %div = fdiv float %mul, 0x3FC99999A0000000
623 ; =========================================================================
625 ; Test-cases for square root
627 ; =========================================================================
629 ; A squared factor fed into a square root intrinsic should be hoisted out
632 declare double @llvm.sqrt.f64(double)
634 define double @sqrt_intrinsic_arg_squared(double %x) {
635 ; CHECK-LABEL: @sqrt_intrinsic_arg_squared(
636 ; CHECK-NEXT: [[FABS:%.*]] = call fast double @llvm.fabs.f64(double [[X:%.*]])
637 ; CHECK-NEXT: ret double [[FABS]]
639 %mul = fmul fast double %x, %x
640 %sqrt = call fast double @llvm.sqrt.f64(double %mul)
644 ; Check all 6 combinations of a 3-way multiplication tree where
645 ; one factor is repeated.
647 define double @sqrt_intrinsic_three_args1(double %x, double %y) {
648 ; CHECK-LABEL: @sqrt_intrinsic_three_args1(
649 ; CHECK-NEXT: [[FABS:%.*]] = call fast double @llvm.fabs.f64(double [[X:%.*]])
650 ; CHECK-NEXT: [[SQRT1:%.*]] = call fast double @llvm.sqrt.f64(double [[Y:%.*]])
651 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast double [[FABS]], [[SQRT1]]
652 ; CHECK-NEXT: ret double [[TMP1]]
654 %mul = fmul fast double %y, %x
655 %mul2 = fmul fast double %mul, %x
656 %sqrt = call fast double @llvm.sqrt.f64(double %mul2)
660 define double @sqrt_intrinsic_three_args2(double %x, double %y) {
661 ; CHECK-LABEL: @sqrt_intrinsic_three_args2(
662 ; CHECK-NEXT: [[FABS:%.*]] = call fast double @llvm.fabs.f64(double [[X:%.*]])
663 ; CHECK-NEXT: [[SQRT1:%.*]] = call fast double @llvm.sqrt.f64(double [[Y:%.*]])
664 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast double [[FABS]], [[SQRT1]]
665 ; CHECK-NEXT: ret double [[TMP1]]
667 %mul = fmul fast double %x, %y
668 %mul2 = fmul fast double %mul, %x
669 %sqrt = call fast double @llvm.sqrt.f64(double %mul2)
673 define double @sqrt_intrinsic_three_args3(double %x, double %y) {
674 ; CHECK-LABEL: @sqrt_intrinsic_three_args3(
675 ; CHECK-NEXT: [[FABS:%.*]] = call fast double @llvm.fabs.f64(double [[X:%.*]])
676 ; CHECK-NEXT: [[SQRT1:%.*]] = call fast double @llvm.sqrt.f64(double [[Y:%.*]])
677 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast double [[FABS]], [[SQRT1]]
678 ; CHECK-NEXT: ret double [[TMP1]]
680 %mul = fmul fast double %x, %x
681 %mul2 = fmul fast double %mul, %y
682 %sqrt = call fast double @llvm.sqrt.f64(double %mul2)
686 define double @sqrt_intrinsic_three_args4(double %x, double %y) {
687 ; CHECK-LABEL: @sqrt_intrinsic_three_args4(
688 ; CHECK-NEXT: [[FABS:%.*]] = call fast double @llvm.fabs.f64(double [[X:%.*]])
689 ; CHECK-NEXT: [[SQRT1:%.*]] = call fast double @llvm.sqrt.f64(double [[Y:%.*]])
690 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast double [[FABS]], [[SQRT1]]
691 ; CHECK-NEXT: ret double [[TMP1]]
693 %mul = fmul fast double %y, %x
694 %mul2 = fmul fast double %x, %mul
695 %sqrt = call fast double @llvm.sqrt.f64(double %mul2)
699 define double @sqrt_intrinsic_three_args5(double %x, double %y) {
700 ; CHECK-LABEL: @sqrt_intrinsic_three_args5(
701 ; CHECK-NEXT: [[FABS:%.*]] = call fast double @llvm.fabs.f64(double [[X:%.*]])
702 ; CHECK-NEXT: [[SQRT1:%.*]] = call fast double @llvm.sqrt.f64(double [[Y:%.*]])
703 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast double [[FABS]], [[SQRT1]]
704 ; CHECK-NEXT: ret double [[TMP1]]
706 %mul = fmul fast double %x, %y
707 %mul2 = fmul fast double %x, %mul
708 %sqrt = call fast double @llvm.sqrt.f64(double %mul2)
712 define double @sqrt_intrinsic_three_args6(double %x, double %y) {
713 ; CHECK-LABEL: @sqrt_intrinsic_three_args6(
714 ; CHECK-NEXT: [[FABS:%.*]] = call fast double @llvm.fabs.f64(double [[X:%.*]])
715 ; CHECK-NEXT: [[SQRT1:%.*]] = call fast double @llvm.sqrt.f64(double [[Y:%.*]])
716 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast double [[FABS]], [[SQRT1]]
717 ; CHECK-NEXT: ret double [[TMP1]]
719 %mul = fmul fast double %x, %x
720 %mul2 = fmul fast double %y, %mul
721 %sqrt = call fast double @llvm.sqrt.f64(double %mul2)
725 ; If any operation is not 'fast', we can't simplify.
727 define double @sqrt_intrinsic_not_so_fast(double %x, double %y) {
728 ; CHECK-LABEL: @sqrt_intrinsic_not_so_fast(
729 ; CHECK-NEXT: [[MUL:%.*]] = fmul double [[X:%.*]], [[X]]
730 ; CHECK-NEXT: [[MUL2:%.*]] = fmul fast double [[MUL]], [[Y:%.*]]
731 ; CHECK-NEXT: [[SQRT:%.*]] = call fast double @llvm.sqrt.f64(double [[MUL2]])
732 ; CHECK-NEXT: ret double [[SQRT]]
734 %mul = fmul double %x, %x
735 %mul2 = fmul fast double %mul, %y
736 %sqrt = call fast double @llvm.sqrt.f64(double %mul2)
740 define double @sqrt_intrinsic_arg_4th(double %x) {
741 ; CHECK-LABEL: @sqrt_intrinsic_arg_4th(
742 ; CHECK-NEXT: [[MUL:%.*]] = fmul fast double [[X:%.*]], [[X]]
743 ; CHECK-NEXT: ret double [[MUL]]
745 %mul = fmul fast double %x, %x
746 %mul2 = fmul fast double %mul, %mul
747 %sqrt = call fast double @llvm.sqrt.f64(double %mul2)
751 define double @sqrt_intrinsic_arg_5th(double %x) {
752 ; CHECK-LABEL: @sqrt_intrinsic_arg_5th(
753 ; CHECK-NEXT: [[MUL:%.*]] = fmul fast double [[X:%.*]], [[X]]
754 ; CHECK-NEXT: [[SQRT1:%.*]] = call fast double @llvm.sqrt.f64(double [[X]])
755 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast double [[MUL]], [[SQRT1]]
756 ; CHECK-NEXT: ret double [[TMP1]]
758 %mul = fmul fast double %x, %x
759 %mul2 = fmul fast double %mul, %x
760 %mul3 = fmul fast double %mul2, %mul
761 %sqrt = call fast double @llvm.sqrt.f64(double %mul3)
765 ; Check that square root calls have the same behavior.
767 declare float @sqrtf(float)
768 declare double @sqrt(double)
769 declare fp128 @sqrtl(fp128)
771 define float @sqrt_call_squared_f32(float %x) {
772 ; CHECK-LABEL: @sqrt_call_squared_f32(
773 ; CHECK-NEXT: [[FABS:%.*]] = call fast float @llvm.fabs.f32(float [[X:%.*]])
774 ; CHECK-NEXT: ret float [[FABS]]
776 %mul = fmul fast float %x, %x
777 %sqrt = call fast float @sqrtf(float %mul)
781 define double @sqrt_call_squared_f64(double %x) {
782 ; CHECK-LABEL: @sqrt_call_squared_f64(
783 ; CHECK-NEXT: [[FABS:%.*]] = call fast double @llvm.fabs.f64(double [[X:%.*]])
784 ; CHECK-NEXT: ret double [[FABS]]
786 %mul = fmul fast double %x, %x
787 %sqrt = call fast double @sqrt(double %mul)
791 define fp128 @sqrt_call_squared_f128(fp128 %x) {
792 ; CHECK-LABEL: @sqrt_call_squared_f128(
793 ; CHECK-NEXT: [[FABS:%.*]] = call fast fp128 @llvm.fabs.f128(fp128 [[X:%.*]])
794 ; CHECK-NEXT: ret fp128 [[FABS]]
796 %mul = fmul fast fp128 %x, %x
797 %sqrt = call fast fp128 @sqrtl(fp128 %mul)
801 ; =========================================================================
803 ; Test-cases for fmin / fmax
805 ; =========================================================================
807 declare double @fmax(double, double)
808 declare double @fmin(double, double)
809 declare float @fmaxf(float, float)
810 declare float @fminf(float, float)
811 declare fp128 @fmaxl(fp128, fp128)
812 declare fp128 @fminl(fp128, fp128)
814 ; 'nsz' is implied by the definition of fmax or fmin itself.
816 ; Shrink and replace the call.
817 define float @max1(float %a, float %b) {
818 ; CHECK-LABEL: @max1(
819 ; CHECK-NEXT: [[TMP1:%.*]] = call fast float @llvm.maxnum.f32(float [[A:%.*]], float [[B:%.*]])
820 ; CHECK-NEXT: ret float [[TMP1]]
822 %c = fpext float %a to double
823 %d = fpext float %b to double
824 %e = call fast double @fmax(double %c, double %d)
825 %f = fptrunc double %e to float
829 define float @fmax_no_fmf(float %a, float %b) {
830 ; CHECK-LABEL: @fmax_no_fmf(
831 ; CHECK-NEXT: [[TMP1:%.*]] = call nsz float @llvm.maxnum.f32(float [[A:%.*]], float [[B:%.*]])
832 ; CHECK-NEXT: ret float [[TMP1]]
834 %c = call float @fmaxf(float %a, float %b)
838 define float @max2(float %a, float %b) {
839 ; CHECK-LABEL: @max2(
840 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz float @llvm.maxnum.f32(float [[A:%.*]], float [[B:%.*]])
841 ; CHECK-NEXT: ret float [[TMP1]]
843 %c = call nnan float @fmaxf(float %a, float %b)
848 define double @max3(double %a, double %b) {
849 ; CHECK-LABEL: @max3(
850 ; CHECK-NEXT: [[TMP1:%.*]] = call fast double @llvm.maxnum.f64(double [[A:%.*]], double [[B:%.*]])
851 ; CHECK-NEXT: ret double [[TMP1]]
853 %c = call fast double @fmax(double %a, double %b)
857 define fp128 @max4(fp128 %a, fp128 %b) {
858 ; CHECK-LABEL: @max4(
859 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz fp128 @llvm.maxnum.f128(fp128 [[A:%.*]], fp128 [[B:%.*]])
860 ; CHECK-NEXT: ret fp128 [[TMP1]]
862 %c = call nnan fp128 @fmaxl(fp128 %a, fp128 %b)
866 ; Shrink and remove the call.
867 define float @min1(float %a, float %b) {
868 ; CHECK-LABEL: @min1(
869 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz float @llvm.minnum.f32(float [[A:%.*]], float [[B:%.*]])
870 ; CHECK-NEXT: ret float [[TMP1]]
872 %c = fpext float %a to double
873 %d = fpext float %b to double
874 %e = call nnan double @fmin(double %c, double %d)
875 %f = fptrunc double %e to float
879 define float @fmin_no_fmf(float %a, float %b) {
880 ; CHECK-LABEL: @fmin_no_fmf(
881 ; CHECK-NEXT: [[TMP1:%.*]] = call nsz float @llvm.minnum.f32(float [[A:%.*]], float [[B:%.*]])
882 ; CHECK-NEXT: ret float [[TMP1]]
884 %c = call float @fminf(float %a, float %b)
888 define float @min2(float %a, float %b) {
889 ; CHECK-LABEL: @min2(
890 ; CHECK-NEXT: [[TMP1:%.*]] = call fast float @llvm.minnum.f32(float [[A:%.*]], float [[B:%.*]])
891 ; CHECK-NEXT: ret float [[TMP1]]
893 %c = call fast float @fminf(float %a, float %b)
897 define double @min3(double %a, double %b) {
898 ; CHECK-LABEL: @min3(
899 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz double @llvm.minnum.f64(double [[A:%.*]], double [[B:%.*]])
900 ; CHECK-NEXT: ret double [[TMP1]]
902 %c = call nnan double @fmin(double %a, double %b)
906 define fp128 @min4(fp128 %a, fp128 %b) {
907 ; CHECK-LABEL: @min4(
908 ; CHECK-NEXT: [[TMP1:%.*]] = call fast fp128 @llvm.minnum.f128(fp128 [[A:%.*]], fp128 [[B:%.*]])
909 ; CHECK-NEXT: ret fp128 [[TMP1]]
911 %c = call fast fp128 @fminl(fp128 %a, fp128 %b)
915 ; ((which ? 2.0 : a) + 1.0) => (which ? 3.0 : (a + 1.0))
916 ; This is always safe. No FMF required.
917 define float @test55(i1 %which, float %a) {
918 ; CHECK-LABEL: @test55(
920 ; CHECK-NEXT: br i1 [[WHICH:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]]
922 ; CHECK-NEXT: [[PHITMP:%.*]] = fadd float [[A:%.*]], 1.000000e+00
923 ; CHECK-NEXT: br label [[FINAL]]
925 ; CHECK-NEXT: [[A:%.*]] = phi float [ 3.000000e+00, [[ENTRY:%.*]] ], [ [[PHITMP]], [[DELAY]] ]
926 ; CHECK-NEXT: ret float [[A]]
929 br i1 %which, label %final, label %delay
935 %A = phi float [ 2.0, %entry ], [ %a, %delay ]
936 %value = fadd float %A, 1.0