1 ; RUN: opt < %s -instcombine -S | FileCheck %s
3 ; testing-case "float fold(float a) { return 1.2f * a * 2.3f; }"
4 ; 1.2f and 2.3f is supposed to be fold.
5 define float @fold(float %a) {
6 %mul = fmul fast float %a, 0x3FF3333340000000
7 %mul1 = fmul fast float %mul, 0x4002666660000000
10 ; CHECK: fmul fast float %a, 0x4006147AE0000000
13 ; Same testing-case as the one used in fold() except that the operators have
15 define float @notfold(float %a) {
16 ; CHECK-LABEL: @notfold(
17 ; CHECK: %mul = fmul fast float %a, 0x3FF3333340000000
18 %mul = fmul fast float %a, 0x3FF3333340000000
19 %mul1 = fmul float %mul, 0x4002666660000000
23 define float @fold2(float %a) {
24 ; CHECK-LABEL: @fold2(
25 ; CHECK: fmul fast float %a, 0x4006147AE0000000
26 %mul = fmul float %a, 0x3FF3333340000000
27 %mul1 = fmul fast float %mul, 0x4002666660000000
31 ; C * f1 + f1 = (C+1) * f1
32 define double @fold3(double %f1) {
33 %t1 = fmul fast double 2.000000e+00, %f1
34 %t2 = fadd fast double %f1, %t1
36 ; CHECK-LABEL: @fold3(
37 ; CHECK: fmul fast double %f1, 3.000000e+00
40 ; (C1 - X) + (C2 - Y) => (C1+C2) - (X + Y)
41 define float @fold4(float %f1, float %f2) {
42 %sub = fsub float 4.000000e+00, %f1
43 %sub1 = fsub float 5.000000e+00, %f2
44 %add = fadd fast float %sub, %sub1
46 ; CHECK-LABEL: @fold4(
47 ; CHECK: %1 = fadd fast float %f1, %f2
48 ; CHECK: fsub fast float 9.000000e+00, %1
51 ; (X + C1) + C2 => X + (C1 + C2)
52 define float @fold5(float %f1, float %f2) {
53 %add = fadd float %f1, 4.000000e+00
54 %add1 = fadd fast float %add, 5.000000e+00
56 ; CHECK-LABEL: @fold5(
57 ; CHECK: fadd fast float %f1, 9.000000e+00
60 ; (X + X) + X => 3.0 * X
61 define float @fold6(float %f1) {
62 %t1 = fadd fast float %f1, %f1
63 %t2 = fadd fast float %f1, %t1
65 ; CHECK-LABEL: @fold6(
66 ; CHECK: fmul fast float %f1, 3.000000e+00
69 ; C1 * X + (X + X) = (C1 + 2) * X
70 define float @fold7(float %f1) {
71 %t1 = fmul fast float %f1, 5.000000e+00
72 %t2 = fadd fast float %f1, %f1
73 %t3 = fadd fast float %t1, %t2
75 ; CHECK-LABEL: @fold7(
76 ; CHECK: fmul fast float %f1, 7.000000e+00
79 ; (X + X) + (X + X) => 4.0 * X
80 define float @fold8(float %f1) {
81 %t1 = fadd fast float %f1, %f1
82 %t2 = fadd fast float %f1, %f1
83 %t3 = fadd fast float %t1, %t2
86 ; CHECK: fmul fast float %f1, 4.000000e+00
89 ; X - (X + Y) => 0 - Y
90 define float @fold9(float %f1, float %f2) {
91 %t1 = fadd float %f1, %f2
92 %t3 = fsub fast float %f1, %t1
95 ; CHECK-LABEL: @fold9(
96 ; CHECK: fsub fast float 0.000000e+00, %f2
99 ; Let C3 = C1 + C2. (f1 + C1) + (f2 + C2) => (f1 + f2) + C3 instead of
100 ; "(f1 + C3) + f2" or "(f2 + C3) + f1". Placing constant-addend at the
101 ; top of resulting simplified expression tree may potentially reveal some
102 ; optimization opportunities in the super-expression trees.
104 define float @fold10(float %f1, float %f2) {
105 %t1 = fadd fast float 2.000000e+00, %f1
106 %t2 = fsub fast float %f2, 3.000000e+00
107 %t3 = fadd fast float %t1, %t2
109 ; CHECK-LABEL: @fold10(
110 ; CHECK: %t3 = fadd fast float %t2, -1.000000e+00
111 ; CHECK: ret float %t3
114 ; once cause Crash/miscompilation
115 define float @fail1(float %f1, float %f2) {
116 %conv3 = fadd fast float %f1, -1.000000e+00
117 %add = fadd fast float %conv3, %conv3
118 %add2 = fadd fast float %add, %conv3
120 ; CHECK-LABEL: @fail1(
124 define double @fail2(double %f1, double %f2) {
125 %t1 = fsub fast double %f1, %f2
126 %t2 = fadd fast double %f1, %f2
127 %t3 = fsub fast double %t1, %t2
129 ; CHECK-LABEL: @fail2(
133 ; c1 * x - x => (c1 - 1.0) * x
134 define float @fold13(float %x) {
135 %mul = fmul fast float %x, 7.000000e+00
136 %sub = fsub fast float %mul, %x
139 ; CHECK: fmul fast float %x, 6.000000e+00
144 define float @fold14(float %x, float %y) {
145 %neg = fsub fast float -0.0, %x
146 %add = fadd fast float %neg, %y
149 ; CHECK: fsub fast float %y, %x
154 define float @fold15(float %x, float %y) {
155 %neg = fsub fast float -0.0, %y
156 %add = fadd fast float %x, %neg
159 ; CHECK: fsub fast float %x, %y
163 ; =========================================================================
165 ; Testing-cases about fmul begin
167 ; =========================================================================
169 ; ((X*C1) + C2) * C3 => (X * (C1*C3)) + (C2*C3) (i.e. distribution)
170 define float @fmul_distribute1(float %f1) {
171 %t1 = fmul float %f1, 6.0e+3
172 %t2 = fadd float %t1, 2.0e+3
173 %t3 = fmul fast float %t2, 5.0e+3
175 ; CHECK-LABEL: @fmul_distribute1(
176 ; CHECK: %1 = fmul fast float %f1, 3.000000e+07
177 ; CHECK: %t3 = fadd fast float %1, 1.000000e+07
180 ; (X/C1 + C2) * C3 => X/(C1/C3) + C2*C3
181 define double @fmul_distribute2(double %f1, double %f2) {
182 %t1 = fdiv double %f1, 3.0e+0
183 %t2 = fadd double %t1, 5.0e+1
184 ; 0x10000000000000 = DBL_MIN
185 %t3 = fmul fast double %t2, 0x10000000000000
188 ; CHECK-LABEL: @fmul_distribute2(
189 ; CHECK: %1 = fdiv fast double %f1, 0x7FE8000000000000
190 ; CHECK: fadd fast double %1, 0x69000000000000
193 ; 5.0e-1 * DBL_MIN yields denormal, so "(f1*3.0 + 5.0e-1) * DBL_MIN" cannot
194 ; be simplified into f1 * (3.0*DBL_MIN) + (5.0e-1*DBL_MIN)
195 define double @fmul_distribute3(double %f1) {
196 %t1 = fdiv double %f1, 3.0e+0
197 %t2 = fadd double %t1, 5.0e-1
198 %t3 = fmul fast double %t2, 0x10000000000000
201 ; CHECK-LABEL: @fmul_distribute3(
202 ; CHECK: fmul fast double %t2, 0x10000000000000
205 ; ((X*C1) + C2) * C3 => (X * (C1*C3)) + (C2*C3) (i.e. distribution)
206 define float @fmul_distribute4(float %f1) {
207 %t1 = fmul float %f1, 6.0e+3
208 %t2 = fsub float 2.0e+3, %t1
209 %t3 = fmul fast float %t2, 5.0e+3
211 ; CHECK-LABEL: @fmul_distribute4(
212 ; CHECK: %1 = fmul fast float %f1, 3.000000e+07
213 ; CHECK: %t3 = fsub fast float 1.000000e+07, %1
216 ; C1/X * C2 => (C1*C2) / X
217 define float @fmul2(float %f1) {
218 %t1 = fdiv float 2.0e+3, %f1
219 %t3 = fmul fast float %t1, 6.0e+3
221 ; CHECK-LABEL: @fmul2(
222 ; CHECK: fdiv fast float 1.200000e+07, %f1
225 ; X/C1 * C2 => X * (C2/C1) is disabled if X/C1 has multiple uses
226 @fmul2_external = external global float
227 define float @fmul2_disable(float %f1) {
228 %div = fdiv fast float 1.000000e+00, %f1
229 store float %div, float* @fmul2_external
230 %mul = fmul fast float %div, 2.000000e+00
232 ; CHECK-LABEL: @fmul2_disable
237 ; X/C1 * C2 => X * (C2/C1) (if C2/C1 is normal Fp)
238 define float @fmul3(float %f1, float %f2) {
239 %t1 = fdiv float %f1, 2.0e+3
240 %t3 = fmul fast float %t1, 6.0e+3
242 ; CHECK-LABEL: @fmul3(
243 ; CHECK: fmul fast float %f1, 3.000000e+00
246 ; Rule "X/C1 * C2 => X * (C2/C1) is not applicable if C2/C1 is either a special
247 ; value of a denormal. The 0x3810000000000000 here take value FLT_MIN
249 define float @fmul4(float %f1, float %f2) {
250 %t1 = fdiv float %f1, 2.0e+3
251 %t3 = fmul fast float %t1, 0x3810000000000000
253 ; CHECK-LABEL: @fmul4(
254 ; CHECK: fmul fast float %t1, 0x3810000000000000
257 ; X / C1 * C2 => X / (C2/C1) if C1/C2 is either a special value of a denormal,
258 ; and C2/C1 is a normal value.
260 define float @fmul5(float %f1, float %f2) {
261 %t1 = fdiv float %f1, 3.0e+0
262 %t3 = fmul fast float %t1, 0x3810000000000000
264 ; CHECK-LABEL: @fmul5(
265 ; CHECK: fdiv fast float %f1, 0x47E8000000000000
268 ; (X*Y) * X => (X*X) * Y
269 define float @fmul6(float %f1, float %f2) {
270 %mul = fmul float %f1, %f2
271 %mul1 = fmul fast float %mul, %f1
273 ; CHECK-LABEL: @fmul6(
274 ; CHECK: fmul fast float %f1, %f1
277 ; "(X*Y) * X => (X*X) * Y" is disabled if "X*Y" has multiple uses
278 define float @fmul7(float %f1, float %f2) {
279 %mul = fmul float %f1, %f2
280 %mul1 = fmul fast float %mul, %f1
281 %add = fadd float %mul1, %mul
283 ; CHECK-LABEL: @fmul7(
284 ; CHECK: fmul fast float %mul, %f1
287 ; =========================================================================
289 ; Testing-cases about negation
291 ; =========================================================================
292 define float @fneg1(float %f1, float %f2) {
293 %sub = fsub float -0.000000e+00, %f1
294 %sub1 = fsub nsz float 0.000000e+00, %f2
295 %mul = fmul float %sub, %sub1
297 ; CHECK-LABEL: @fneg1(
298 ; CHECK: fmul float %f1, %f2
301 ; =========================================================================
303 ; Testing-cases about div
305 ; =========================================================================
307 ; X/C1 / C2 => X * (1/(C2*C1))
308 define float @fdiv1(float %x) {
309 %div = fdiv float %x, 0x3FF3333340000000
310 %div1 = fdiv fast float %div, 0x4002666660000000
312 ; 0x3FF3333340000000 = 1.2f
313 ; 0x4002666660000000 = 2.3f
314 ; 0x3FD7303B60000000 = 0.36231884057971014492
315 ; CHECK-LABEL: @fdiv1(
316 ; CHECK: fmul fast float %x, 0x3FD7303B60000000
319 ; X*C1 / C2 => X * (C1/C2)
320 define float @fdiv2(float %x) {
321 %mul = fmul float %x, 0x3FF3333340000000
322 %div1 = fdiv fast float %mul, 0x4002666660000000
325 ; 0x3FF3333340000000 = 1.2f
326 ; 0x4002666660000000 = 2.3f
327 ; 0x3FE0B21660000000 = 0.52173918485641479492
328 ; CHECK-LABEL: @fdiv2(
329 ; CHECK: fmul fast float %x, 0x3FE0B21660000000
332 ; "X/C1 / C2 => X * (1/(C2*C1))" is disabled (for now) is C2/C1 is a denormal
334 define float @fdiv3(float %x) {
335 %div = fdiv float %x, 0x47EFFFFFE0000000
336 %div1 = fdiv fast float %div, 0x4002666660000000
338 ; CHECK-LABEL: @fdiv3(
339 ; CHECK: fdiv float %x, 0x47EFFFFFE0000000
342 ; "X*C1 / C2 => X * (C1/C2)" is disabled if C1/C2 is a denormal
343 define float @fdiv4(float %x) {
344 %mul = fmul float %x, 0x47EFFFFFE0000000
345 %div = fdiv float %mul, 0x3FC99999A0000000
347 ; CHECK-LABEL: @fdiv4(
348 ; CHECK: fmul float %x, 0x47EFFFFFE0000000
351 ; (X/Y)/Z = > X/(Y*Z)
352 define float @fdiv5(float %f1, float %f2, float %f3) {
353 %t1 = fdiv float %f1, %f2
354 %t2 = fdiv fast float %t1, %f3
356 ; CHECK-LABEL: @fdiv5(
357 ; CHECK: fmul float %f2, %f3
360 ; Z/(X/Y) = > (Z*Y)/X
361 define float @fdiv6(float %f1, float %f2, float %f3) {
362 %t1 = fdiv float %f1, %f2
363 %t2 = fdiv fast float %f3, %t1
365 ; CHECK-LABEL: @fdiv6(
366 ; CHECK: fmul float %f3, %f2
369 ; C1/(X*C2) => (C1/C2) / X
370 define float @fdiv7(float %x) {
371 %t1 = fmul float %x, 3.0e0
372 %t2 = fdiv fast float 15.0e0, %t1
374 ; CHECK-LABEL: @fdiv7(
375 ; CHECK: fdiv fast float 5.000000e+00, %x
378 ; C1/(X/C2) => (C1*C2) / X
379 define float @fdiv8(float %x) {
380 %t1 = fdiv float %x, 3.0e0
381 %t2 = fdiv fast float 15.0e0, %t1
383 ; CHECK-LABEL: @fdiv8(
384 ; CHECK: fdiv fast float 4.500000e+01, %x
387 ; C1/(C2/X) => (C1/C2) * X
388 define float @fdiv9(float %x) {
389 %t1 = fdiv float 3.0e0, %x
390 %t2 = fdiv fast float 15.0e0, %t1
392 ; CHECK-LABEL: @fdiv9(
393 ; CHECK: fmul fast float %x, 5.000000e+00
396 ; =========================================================================
398 ; Testing-cases about factorization
400 ; =========================================================================
401 ; x*z + y*z => (x+y) * z
402 define float @fact_mul1(float %x, float %y, float %z) {
403 %t1 = fmul fast float %x, %z
404 %t2 = fmul fast float %y, %z
405 %t3 = fadd fast float %t1, %t2
407 ; CHECK-LABEL: @fact_mul1(
408 ; CHECK: fmul fast float %1, %z
411 ; z*x + y*z => (x+y) * z
412 define float @fact_mul2(float %x, float %y, float %z) {
413 %t1 = fmul fast float %z, %x
414 %t2 = fmul fast float %y, %z
415 %t3 = fsub fast float %t1, %t2
417 ; CHECK-LABEL: @fact_mul2(
418 ; CHECK: fmul fast float %1, %z
421 ; z*x - z*y => (x-y) * z
422 define float @fact_mul3(float %x, float %y, float %z) {
423 %t2 = fmul fast float %z, %y
424 %t1 = fmul fast float %z, %x
425 %t3 = fsub fast float %t1, %t2
427 ; CHECK-LABEL: @fact_mul3(
428 ; CHECK: fmul fast float %1, %z
431 ; x*z - z*y => (x-y) * z
432 define float @fact_mul4(float %x, float %y, float %z) {
433 %t1 = fmul fast float %x, %z
434 %t2 = fmul fast float %z, %y
435 %t3 = fsub fast float %t1, %t2
437 ; CHECK-LABEL: @fact_mul4(
438 ; CHECK: fmul fast float %1, %z
441 ; x/y + x/z, no xform
442 define float @fact_div1(float %x, float %y, float %z) {
443 %t1 = fdiv fast float %x, %y
444 %t2 = fdiv fast float %x, %z
445 %t3 = fadd fast float %t1, %t2
448 ; CHECK: fadd fast float %t1, %t2
451 ; x/y + z/x; no xform
452 define float @fact_div2(float %x, float %y, float %z) {
453 %t1 = fdiv fast float %x, %y
454 %t2 = fdiv fast float %z, %x
455 %t3 = fadd fast float %t1, %t2
458 ; CHECK: fadd fast float %t1, %t2
461 ; y/x + z/x => (y+z)/x
462 define float @fact_div3(float %x, float %y, float %z) {
463 %t1 = fdiv fast float %y, %x
464 %t2 = fdiv fast float %z, %x
465 %t3 = fadd fast float %t1, %t2
468 ; CHECK: fdiv fast float %1, %x
471 ; y/x - z/x => (y-z)/x
472 define float @fact_div4(float %x, float %y, float %z) {
473 %t1 = fdiv fast float %y, %x
474 %t2 = fdiv fast float %z, %x
475 %t3 = fsub fast float %t1, %t2
478 ; CHECK: fdiv fast float %1, %x
481 ; y/x - z/x => (y-z)/x is disabled if y-z is denormal.
482 define float @fact_div5(float %x) {
483 %t1 = fdiv fast float 0x3810000000000000, %x
484 %t2 = fdiv fast float 0x3800000000000000, %x
485 %t3 = fadd fast float %t1, %t2
488 ; CHECK: fdiv fast float 0x3818000000000000, %x
491 ; y/x - z/x => (y-z)/x is disabled if y-z is denormal.
492 define float @fact_div6(float %x) {
493 %t1 = fdiv fast float 0x3810000000000000, %x
494 %t2 = fdiv fast float 0x3800000000000000, %x
495 %t3 = fsub fast float %t1, %t2
498 ; CHECK: %t3 = fsub fast float %t1, %t2