de51c494685f083e679769ece0f82b4b0bf2926f
[oota-llvm.git] / test / Transforms / InstCombine / fast-math.ll
1 ; RUN: opt < %s -instcombine -S | FileCheck %s
2
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
8   ret float %mul1
9 ; CHECK-LABEL: @fold(
10 ; CHECK: fmul fast float %a, 0x4006147AE0000000
11 }
12
13 ; Same testing-case as the one used in fold() except that the operators have
14 ; fixed FP mode.
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
20   ret float %mul1
21 }
22
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
28   ret float %mul1
29 }
30
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
35   ret double %t2
36 ; CHECK-LABEL: @fold3(
37 ; CHECK: fmul fast double %f1, 3.000000e+00
38 }
39
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
45   ret float %add
46 ; CHECK-LABEL: @fold4(
47 ; CHECK: %1 = fadd fast float %f1, %f2
48 ; CHECK: fsub fast float 9.000000e+00, %1
49 }
50
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
55   ret float %add1
56 ; CHECK-LABEL: @fold5(
57 ; CHECK: fadd fast float %f1, 9.000000e+00
58 }
59
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
64   ret float %t2
65 ; CHECK-LABEL: @fold6(
66 ; CHECK: fmul fast float %f1, 3.000000e+00
67 }
68
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
74   ret float %t3
75 ; CHECK-LABEL: @fold7(
76 ; CHECK: fmul fast float %f1, 7.000000e+00
77 }
78
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
84   ret float %t3
85 ; CHECK: fold8
86 ; CHECK: fmul fast float %f1, 4.000000e+00
87 }
88
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
93   ret float %t3
94
95 ; CHECK-LABEL: @fold9(
96 ; CHECK: fsub fast float 0.000000e+00, %f2
97 }
98
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.
103 ;
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
108   ret float %t3
109 ; CHECK-LABEL: @fold10(
110 ; CHECK: %t3 = fadd fast float %t2, -1.000000e+00
111 ; CHECK: ret float %t3
112 }
113
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
119   ret float %add2
120 ; CHECK-LABEL: @fail1(
121 ; CHECK: ret
122 }
123
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
128   ret double %t3
129 ; CHECK-LABEL: @fail2(
130 ; CHECK: ret
131 }
132
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
137   ret float %sub
138 ; CHECK: fold13
139 ; CHECK: fmul fast float %x, 6.000000e+00
140 ; CHECK: ret
141 }
142
143 ; -x + y => y - x
144 define float @fold14(float %x, float %y) {
145   %neg = fsub fast float -0.0, %x
146   %add = fadd fast float %neg, %y
147   ret float %add
148 ; CHECK: fold14
149 ; CHECK: fsub fast float %y, %x
150 ; CHECK: ret
151 }
152
153 ; x + -y => x - y
154 define float @fold15(float %x, float %y) {
155   %neg = fsub fast float -0.0, %y
156   %add = fadd fast float %x, %neg
157   ret float %add
158 ; CHECK: fold15
159 ; CHECK: fsub fast float %x, %y
160 ; CHECK: ret
161 }
162
163 ; (select X+Y, X-Y) => X + (select Y, -Y)
164 define float @fold16(float %x, float %y) {
165   %cmp = fcmp ogt float %x, %y
166   %plus = fadd fast float %x, %y
167   %minus = fsub fast float %x, %y
168   %r = select i1 %cmp, float %plus, float %minus
169   ret float %r
170 ; CHECK: fold16
171 ; CHECK: fsub fast float
172 ; CHECK: select
173 ; CHECK: fadd fast float
174 ; CHECK: ret
175 }
176
177
178
179 ; =========================================================================
180 ;
181 ;   Testing-cases about fmul begin
182 ;
183 ; =========================================================================
184
185 ; ((X*C1) + C2) * C3 => (X * (C1*C3)) + (C2*C3) (i.e. distribution)
186 define float @fmul_distribute1(float %f1) {
187   %t1 = fmul float %f1, 6.0e+3
188   %t2 = fadd float %t1, 2.0e+3
189   %t3 = fmul fast float %t2, 5.0e+3
190   ret float %t3
191 ; CHECK-LABEL: @fmul_distribute1(
192 ; CHECK: %1 = fmul fast float %f1, 3.000000e+07
193 ; CHECK: %t3 = fadd fast float %1, 1.000000e+07
194 }
195
196 ; (X/C1 + C2) * C3 => X/(C1/C3) + C2*C3
197 define double @fmul_distribute2(double %f1, double %f2) {
198   %t1 = fdiv double %f1, 3.0e+0
199   %t2 = fadd double %t1, 5.0e+1
200   ; 0x10000000000000 = DBL_MIN
201   %t3 = fmul fast double %t2, 0x10000000000000
202   ret double %t3
203
204 ; CHECK-LABEL: @fmul_distribute2(
205 ; CHECK: %1 = fdiv fast double %f1, 0x7FE8000000000000
206 ; CHECK: fadd fast double %1, 0x69000000000000
207 }
208
209 ; 5.0e-1 * DBL_MIN yields denormal, so "(f1*3.0 + 5.0e-1) * DBL_MIN" cannot
210 ; be simplified into f1 * (3.0*DBL_MIN) + (5.0e-1*DBL_MIN)
211 define double @fmul_distribute3(double %f1) {
212   %t1 = fdiv double %f1, 3.0e+0
213   %t2 = fadd double %t1, 5.0e-1
214   %t3 = fmul fast double %t2, 0x10000000000000
215   ret double %t3
216
217 ; CHECK-LABEL: @fmul_distribute3(
218 ; CHECK: fmul fast double %t2, 0x10000000000000
219 }
220
221 ; ((X*C1) + C2) * C3 => (X * (C1*C3)) + (C2*C3) (i.e. distribution)
222 define float @fmul_distribute4(float %f1) {
223   %t1 = fmul float %f1, 6.0e+3
224   %t2 = fsub float 2.0e+3, %t1
225   %t3 = fmul fast float %t2, 5.0e+3
226   ret float %t3
227 ; CHECK-LABEL: @fmul_distribute4(
228 ; CHECK: %1 = fmul fast float %f1, 3.000000e+07
229 ; CHECK: %t3 = fsub fast float 1.000000e+07, %1
230 }
231
232 ; C1/X * C2 => (C1*C2) / X
233 define float @fmul2(float %f1) {
234   %t1 = fdiv float 2.0e+3, %f1
235   %t3 = fmul fast float %t1, 6.0e+3
236   ret float %t3
237 ; CHECK-LABEL: @fmul2(
238 ; CHECK: fdiv fast float 1.200000e+07, %f1
239 }
240
241 ; X/C1 * C2 => X * (C2/C1) is disabled if X/C1 has multiple uses
242 @fmul2_external = external global float
243 define float @fmul2_disable(float %f1) {
244   %div = fdiv fast float 1.000000e+00, %f1 
245   store float %div, float* @fmul2_external
246   %mul = fmul fast float %div, 2.000000e+00
247   ret float %mul
248 ; CHECK-LABEL: @fmul2_disable
249 ; CHECK: store
250 ; CHECK: fmul fast
251 }
252
253 ; X/C1 * C2 => X * (C2/C1) (if C2/C1 is normal Fp)
254 define float @fmul3(float %f1, float %f2) {
255   %t1 = fdiv float %f1, 2.0e+3
256   %t3 = fmul fast float %t1, 6.0e+3
257   ret float %t3
258 ; CHECK-LABEL: @fmul3(
259 ; CHECK: fmul fast float %f1, 3.000000e+00
260 }
261
262 ; Rule "X/C1 * C2 => X * (C2/C1) is not applicable if C2/C1 is either a special
263 ; value of a denormal. The 0x3810000000000000 here take value FLT_MIN
264 ;
265 define float @fmul4(float %f1, float %f2) {
266   %t1 = fdiv float %f1, 2.0e+3
267   %t3 = fmul fast float %t1, 0x3810000000000000
268   ret float %t3
269 ; CHECK-LABEL: @fmul4(
270 ; CHECK: fmul fast float %t1, 0x3810000000000000
271 }
272
273 ; X / C1 * C2 => X / (C2/C1) if  C1/C2 is either a special value of a denormal,
274 ;  and C2/C1 is a normal value.
275 ;
276 define float @fmul5(float %f1, float %f2) {
277   %t1 = fdiv float %f1, 3.0e+0
278   %t3 = fmul fast float %t1, 0x3810000000000000
279   ret float %t3
280 ; CHECK-LABEL: @fmul5(
281 ; CHECK: fdiv fast float %f1, 0x47E8000000000000
282 }
283
284 ; (X*Y) * X => (X*X) * Y
285 define float @fmul6(float %f1, float %f2) {
286   %mul = fmul float %f1, %f2
287   %mul1 = fmul fast float %mul, %f1
288   ret float %mul1
289 ; CHECK-LABEL: @fmul6(
290 ; CHECK: fmul fast float %f1, %f1
291 }
292
293 ; "(X*Y) * X => (X*X) * Y" is disabled if "X*Y" has multiple uses
294 define float @fmul7(float %f1, float %f2) {
295   %mul = fmul float %f1, %f2
296   %mul1 = fmul fast float %mul, %f1
297   %add = fadd float %mul1, %mul
298   ret float %add
299 ; CHECK-LABEL: @fmul7(
300 ; CHECK: fmul fast float %mul, %f1
301 }
302
303 ; =========================================================================
304 ;
305 ;   Testing-cases about negation
306 ;
307 ; =========================================================================
308 define float @fneg1(float %f1, float %f2) {
309   %sub = fsub float -0.000000e+00, %f1
310   %sub1 = fsub nsz float 0.000000e+00, %f2
311   %mul = fmul float %sub, %sub1
312   ret float %mul
313 ; CHECK-LABEL: @fneg1(
314 ; CHECK: fmul float %f1, %f2
315 }
316
317 ; =========================================================================
318 ;
319 ;   Testing-cases about div
320 ;
321 ; =========================================================================
322
323 ; X/C1 / C2 => X * (1/(C2*C1))
324 define float @fdiv1(float %x) {
325   %div = fdiv float %x, 0x3FF3333340000000
326   %div1 = fdiv fast float %div, 0x4002666660000000
327   ret float %div1
328 ; 0x3FF3333340000000 = 1.2f
329 ; 0x4002666660000000 = 2.3f
330 ; 0x3FD7303B60000000 = 0.36231884057971014492
331 ; CHECK-LABEL: @fdiv1(
332 ; CHECK: fmul fast float %x, 0x3FD7303B60000000
333 }
334
335 ; X*C1 / C2 => X * (C1/C2)
336 define float @fdiv2(float %x) {
337   %mul = fmul float %x, 0x3FF3333340000000
338   %div1 = fdiv fast float %mul, 0x4002666660000000
339   ret float %div1
340
341 ; 0x3FF3333340000000 = 1.2f
342 ; 0x4002666660000000 = 2.3f
343 ; 0x3FE0B21660000000 = 0.52173918485641479492
344 ; CHECK-LABEL: @fdiv2(
345 ; CHECK: fmul fast float %x, 0x3FE0B21660000000
346 }
347
348 ; "X/C1 / C2 => X * (1/(C2*C1))" is disabled (for now) is C2/C1 is a denormal
349 ;
350 define float @fdiv3(float %x) {
351   %div = fdiv float %x, 0x47EFFFFFE0000000
352   %div1 = fdiv fast float %div, 0x4002666660000000
353   ret float %div1
354 ; CHECK-LABEL: @fdiv3(
355 ; CHECK: fdiv float %x, 0x47EFFFFFE0000000
356 }
357
358 ; "X*C1 / C2 => X * (C1/C2)" is disabled if C1/C2 is a denormal
359 define float @fdiv4(float %x) {
360   %mul = fmul float %x, 0x47EFFFFFE0000000
361   %div = fdiv float %mul, 0x3FC99999A0000000
362   ret float %div
363 ; CHECK-LABEL: @fdiv4(
364 ; CHECK: fmul float %x, 0x47EFFFFFE0000000
365 }
366
367 ; (X/Y)/Z = > X/(Y*Z)
368 define float @fdiv5(float %f1, float %f2, float %f3) {
369   %t1 = fdiv float %f1, %f2
370   %t2 = fdiv fast float %t1, %f3
371   ret float %t2
372 ; CHECK-LABEL: @fdiv5(
373 ; CHECK: fmul float %f2, %f3
374 }
375
376 ; Z/(X/Y) = > (Z*Y)/X
377 define float @fdiv6(float %f1, float %f2, float %f3) {
378   %t1 = fdiv float %f1, %f2
379   %t2 = fdiv fast float %f3, %t1
380   ret float %t2
381 ; CHECK-LABEL: @fdiv6(
382 ; CHECK: fmul float %f3, %f2
383 }
384
385 ; C1/(X*C2) => (C1/C2) / X
386 define float @fdiv7(float %x) {
387   %t1 = fmul float %x, 3.0e0
388   %t2 = fdiv fast float 15.0e0, %t1
389   ret float %t2
390 ; CHECK-LABEL: @fdiv7(
391 ; CHECK: fdiv fast float 5.000000e+00, %x
392 }
393
394 ; C1/(X/C2) => (C1*C2) / X
395 define float @fdiv8(float %x) {
396   %t1 = fdiv float %x, 3.0e0
397   %t2 = fdiv fast float 15.0e0, %t1
398   ret float %t2
399 ; CHECK-LABEL: @fdiv8(
400 ; CHECK: fdiv fast float 4.500000e+01, %x
401 }
402
403 ; C1/(C2/X) => (C1/C2) * X
404 define float @fdiv9(float %x) {
405   %t1 = fdiv float 3.0e0, %x
406   %t2 = fdiv fast float 15.0e0, %t1
407   ret float %t2
408 ; CHECK-LABEL: @fdiv9(
409 ; CHECK: fmul fast float %x, 5.000000e+00
410 }
411
412 ; =========================================================================
413 ;
414 ;   Testing-cases about factorization
415 ;
416 ; =========================================================================
417 ; x*z + y*z => (x+y) * z
418 define float @fact_mul1(float %x, float %y, float %z) {
419   %t1 = fmul fast float %x, %z
420   %t2 = fmul fast float %y, %z
421   %t3 = fadd fast float %t1, %t2
422   ret float %t3
423 ; CHECK-LABEL: @fact_mul1(
424 ; CHECK: fmul fast float %1, %z
425 }
426
427 ; z*x + y*z => (x+y) * z
428 define float @fact_mul2(float %x, float %y, float %z) {
429   %t1 = fmul fast float %z, %x
430   %t2 = fmul fast float %y, %z
431   %t3 = fsub fast float %t1, %t2
432   ret float %t3
433 ; CHECK-LABEL: @fact_mul2(
434 ; CHECK: fmul fast float %1, %z
435 }
436
437 ; z*x - z*y => (x-y) * z
438 define float @fact_mul3(float %x, float %y, float %z) {
439   %t2 = fmul fast float %z, %y
440   %t1 = fmul fast float %z, %x
441   %t3 = fsub fast float %t1, %t2
442   ret float %t3
443 ; CHECK-LABEL: @fact_mul3(
444 ; CHECK: fmul fast float %1, %z
445 }
446
447 ; x*z - z*y => (x-y) * z
448 define float @fact_mul4(float %x, float %y, float %z) {
449   %t1 = fmul fast float %x, %z
450   %t2 = fmul fast float %z, %y
451   %t3 = fsub fast float %t1, %t2
452   ret float %t3
453 ; CHECK-LABEL: @fact_mul4(
454 ; CHECK: fmul fast float %1, %z
455 }
456
457 ; x/y + x/z, no xform
458 define float @fact_div1(float %x, float %y, float %z) {
459   %t1 = fdiv fast float %x, %y
460   %t2 = fdiv fast float %x, %z
461   %t3 = fadd fast float %t1, %t2
462   ret float %t3
463 ; CHECK: fact_div1
464 ; CHECK: fadd fast float %t1, %t2
465 }
466
467 ; x/y + z/x; no xform
468 define float @fact_div2(float %x, float %y, float %z) {
469   %t1 = fdiv fast float %x, %y
470   %t2 = fdiv fast float %z, %x
471   %t3 = fadd fast float %t1, %t2
472   ret float %t3
473 ; CHECK: fact_div2
474 ; CHECK: fadd fast float %t1, %t2
475 }
476
477 ; y/x + z/x => (y+z)/x
478 define float @fact_div3(float %x, float %y, float %z) {
479   %t1 = fdiv fast float %y, %x
480   %t2 = fdiv fast float %z, %x
481   %t3 = fadd fast float %t1, %t2
482   ret float %t3
483 ; CHECK: fact_div3
484 ; CHECK: fdiv fast float %1, %x
485 }
486
487 ; y/x - z/x => (y-z)/x
488 define float @fact_div4(float %x, float %y, float %z) {
489   %t1 = fdiv fast float %y, %x
490   %t2 = fdiv fast float %z, %x
491   %t3 = fsub fast float %t1, %t2
492   ret float %t3
493 ; CHECK: fact_div4
494 ; CHECK: fdiv fast float %1, %x
495 }
496
497 ; y/x - z/x => (y-z)/x is disabled if y-z is denormal.
498 define float @fact_div5(float %x) {
499   %t1 = fdiv fast float 0x3810000000000000, %x
500   %t2 = fdiv fast float 0x3800000000000000, %x
501   %t3 = fadd fast float %t1, %t2
502   ret float %t3
503 ; CHECK: fact_div5
504 ; CHECK: fdiv fast float 0x3818000000000000, %x
505 }
506
507 ; y/x - z/x => (y-z)/x is disabled if y-z is denormal.
508 define float @fact_div6(float %x) {
509   %t1 = fdiv fast float 0x3810000000000000, %x
510   %t2 = fdiv fast float 0x3800000000000000, %x
511   %t3 = fsub fast float %t1, %t2
512   ret float %t3
513 ; CHECK: fact_div6
514 ; CHECK: %t3 = fsub fast float %t1, %t2
515 }