[Fast-math] Disable "(C1/X)*C2 => (C1*C2)/X" if C1/X has multiple uses.
[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 ; =========================================================================
144 ;
145 ;   Testing-cases about fmul begin
146 ;
147 ; =========================================================================
148
149 ; ((X*C1) + C2) * C3 => (X * (C1*C3)) + (C2*C3) (i.e. distribution)
150 define float @fmul_distribute1(float %f1) {
151   %t1 = fmul float %f1, 6.0e+3
152   %t2 = fadd float %t1, 2.0e+3
153   %t3 = fmul fast float %t2, 5.0e+3
154   ret float %t3
155 ; CHECK-LABEL: @fmul_distribute1(
156 ; CHECK: %1 = fmul fast float %f1, 3.000000e+07
157 ; CHECK: %t3 = fadd fast float %1, 1.000000e+07
158 }
159
160 ; (X/C1 + C2) * C3 => X/(C1/C3) + C2*C3
161 define double @fmul_distribute2(double %f1, double %f2) {
162   %t1 = fdiv double %f1, 3.0e+0
163   %t2 = fadd double %t1, 5.0e+1
164   ; 0x10000000000000 = DBL_MIN
165   %t3 = fmul fast double %t2, 0x10000000000000
166   ret double %t3
167
168 ; CHECK-LABEL: @fmul_distribute2(
169 ; CHECK: %1 = fdiv fast double %f1, 0x7FE8000000000000
170 ; CHECK: fadd fast double %1, 0x69000000000000
171 }
172
173 ; 5.0e-1 * DBL_MIN yields denormal, so "(f1*3.0 + 5.0e-1) * DBL_MIN" cannot
174 ; be simplified into f1 * (3.0*DBL_MIN) + (5.0e-1*DBL_MIN)
175 define double @fmul_distribute3(double %f1) {
176   %t1 = fdiv double %f1, 3.0e+0
177   %t2 = fadd double %t1, 5.0e-1
178   %t3 = fmul fast double %t2, 0x10000000000000
179   ret double %t3
180
181 ; CHECK-LABEL: @fmul_distribute3(
182 ; CHECK: fmul fast double %t2, 0x10000000000000
183 }
184
185 ; ((X*C1) + C2) * C3 => (X * (C1*C3)) + (C2*C3) (i.e. distribution)
186 define float @fmul_distribute4(float %f1) {
187   %t1 = fmul float %f1, 6.0e+3
188   %t2 = fsub float 2.0e+3, %t1
189   %t3 = fmul fast float %t2, 5.0e+3
190   ret float %t3
191 ; CHECK-LABEL: @fmul_distribute4(
192 ; CHECK: %1 = fmul fast float %f1, 3.000000e+07
193 ; CHECK: %t3 = fsub fast float 1.000000e+07, %1
194 }
195
196 ; C1/X * C2 => (C1*C2) / X
197 define float @fmul2(float %f1) {
198   %t1 = fdiv float 2.0e+3, %f1
199   %t3 = fmul fast float %t1, 6.0e+3
200   ret float %t3
201 ; CHECK-LABEL: @fmul2(
202 ; CHECK: fdiv fast float 1.200000e+07, %f1
203 }
204
205 ; X/C1 * C2 => X * (C2/C1) is disabled if X/C1 has multiple uses
206 @fmul2_external = external global float
207 define float @fmul2_disable(float %f1) {
208   %div = fdiv fast float 1.000000e+00, %f1 
209   store float %div, float* @fmul2_external
210   %mul = fmul fast float %div, 2.000000e+00
211   ret float %mul
212 ; CHECK-LABEL: @fmul2_disable
213 ; CHECK: store
214 ; CHECK: fmul fast
215 }
216
217 ; X/C1 * C2 => X * (C2/C1) (if C2/C1 is normal Fp)
218 define float @fmul3(float %f1, float %f2) {
219   %t1 = fdiv float %f1, 2.0e+3
220   %t3 = fmul fast float %t1, 6.0e+3
221   ret float %t3
222 ; CHECK-LABEL: @fmul3(
223 ; CHECK: fmul fast float %f1, 3.000000e+00
224 }
225
226 ; Rule "X/C1 * C2 => X * (C2/C1) is not applicable if C2/C1 is either a special
227 ; value of a denormal. The 0x3810000000000000 here take value FLT_MIN
228 ;
229 define float @fmul4(float %f1, float %f2) {
230   %t1 = fdiv float %f1, 2.0e+3
231   %t3 = fmul fast float %t1, 0x3810000000000000
232   ret float %t3
233 ; CHECK-LABEL: @fmul4(
234 ; CHECK: fmul fast float %t1, 0x3810000000000000
235 }
236
237 ; X / C1 * C2 => X / (C2/C1) if  C1/C2 is either a special value of a denormal,
238 ;  and C2/C1 is a normal value.
239 ;
240 define float @fmul5(float %f1, float %f2) {
241   %t1 = fdiv float %f1, 3.0e+0
242   %t3 = fmul fast float %t1, 0x3810000000000000
243   ret float %t3
244 ; CHECK-LABEL: @fmul5(
245 ; CHECK: fdiv fast float %f1, 0x47E8000000000000
246 }
247
248 ; (X*Y) * X => (X*X) * Y
249 define float @fmul6(float %f1, float %f2) {
250   %mul = fmul float %f1, %f2
251   %mul1 = fmul fast float %mul, %f1
252   ret float %mul1
253 ; CHECK-LABEL: @fmul6(
254 ; CHECK: fmul fast float %f1, %f1
255 }
256
257 ; "(X*Y) * X => (X*X) * Y" is disabled if "X*Y" has multiple uses
258 define float @fmul7(float %f1, float %f2) {
259   %mul = fmul float %f1, %f2
260   %mul1 = fmul fast float %mul, %f1
261   %add = fadd float %mul1, %mul
262   ret float %add
263 ; CHECK-LABEL: @fmul7(
264 ; CHECK: fmul fast float %mul, %f1
265 }
266
267 ; =========================================================================
268 ;
269 ;   Testing-cases about negation
270 ;
271 ; =========================================================================
272 define float @fneg1(float %f1, float %f2) {
273   %sub = fsub float -0.000000e+00, %f1
274   %sub1 = fsub nsz float 0.000000e+00, %f2
275   %mul = fmul float %sub, %sub1
276   ret float %mul
277 ; CHECK-LABEL: @fneg1(
278 ; CHECK: fmul float %f1, %f2
279 }
280
281 ; =========================================================================
282 ;
283 ;   Testing-cases about div
284 ;
285 ; =========================================================================
286
287 ; X/C1 / C2 => X * (1/(C2*C1))
288 define float @fdiv1(float %x) {
289   %div = fdiv float %x, 0x3FF3333340000000
290   %div1 = fdiv fast float %div, 0x4002666660000000
291   ret float %div1
292 ; 0x3FF3333340000000 = 1.2f
293 ; 0x4002666660000000 = 2.3f
294 ; 0x3FD7303B60000000 = 0.36231884057971014492
295 ; CHECK-LABEL: @fdiv1(
296 ; CHECK: fmul fast float %x, 0x3FD7303B60000000
297 }
298
299 ; X*C1 / C2 => X * (C1/C2)
300 define float @fdiv2(float %x) {
301   %mul = fmul float %x, 0x3FF3333340000000
302   %div1 = fdiv fast float %mul, 0x4002666660000000
303   ret float %div1
304
305 ; 0x3FF3333340000000 = 1.2f
306 ; 0x4002666660000000 = 2.3f
307 ; 0x3FE0B21660000000 = 0.52173918485641479492
308 ; CHECK-LABEL: @fdiv2(
309 ; CHECK: fmul fast float %x, 0x3FE0B21660000000
310 }
311
312 ; "X/C1 / C2 => X * (1/(C2*C1))" is disabled (for now) is C2/C1 is a denormal
313 ;
314 define float @fdiv3(float %x) {
315   %div = fdiv float %x, 0x47EFFFFFE0000000
316   %div1 = fdiv fast float %div, 0x4002666660000000
317   ret float %div1
318 ; CHECK-LABEL: @fdiv3(
319 ; CHECK: fdiv float %x, 0x47EFFFFFE0000000
320 }
321
322 ; "X*C1 / C2 => X * (C1/C2)" is disabled if C1/C2 is a denormal
323 define float @fdiv4(float %x) {
324   %mul = fmul float %x, 0x47EFFFFFE0000000
325   %div = fdiv float %mul, 0x3FC99999A0000000
326   ret float %div
327 ; CHECK-LABEL: @fdiv4(
328 ; CHECK: fmul float %x, 0x47EFFFFFE0000000
329 }
330
331 ; (X/Y)/Z = > X/(Y*Z)
332 define float @fdiv5(float %f1, float %f2, float %f3) {
333   %t1 = fdiv float %f1, %f2
334   %t2 = fdiv fast float %t1, %f3
335   ret float %t2
336 ; CHECK-LABEL: @fdiv5(
337 ; CHECK: fmul float %f2, %f3
338 }
339
340 ; Z/(X/Y) = > (Z*Y)/X
341 define float @fdiv6(float %f1, float %f2, float %f3) {
342   %t1 = fdiv float %f1, %f2
343   %t2 = fdiv fast float %f3, %t1
344   ret float %t2
345 ; CHECK-LABEL: @fdiv6(
346 ; CHECK: fmul float %f3, %f2
347 }
348
349 ; C1/(X*C2) => (C1/C2) / X
350 define float @fdiv7(float %x) {
351   %t1 = fmul float %x, 3.0e0
352   %t2 = fdiv fast float 15.0e0, %t1
353   ret float %t2
354 ; CHECK-LABEL: @fdiv7(
355 ; CHECK: fdiv fast float 5.000000e+00, %x
356 }
357
358 ; C1/(X/C2) => (C1*C2) / X
359 define float @fdiv8(float %x) {
360   %t1 = fdiv float %x, 3.0e0
361   %t2 = fdiv fast float 15.0e0, %t1
362   ret float %t2
363 ; CHECK-LABEL: @fdiv8(
364 ; CHECK: fdiv fast float 4.500000e+01, %x
365 }
366
367 ; C1/(C2/X) => (C1/C2) * X
368 define float @fdiv9(float %x) {
369   %t1 = fdiv float 3.0e0, %x
370   %t2 = fdiv fast float 15.0e0, %t1
371   ret float %t2
372 ; CHECK-LABEL: @fdiv9(
373 ; CHECK: fmul fast float %x, 5.000000e+00
374 }
375
376 ; =========================================================================
377 ;
378 ;   Testing-cases about factorization
379 ;
380 ; =========================================================================
381 ; x*z + y*z => (x+y) * z
382 define float @fact_mul1(float %x, float %y, float %z) {
383   %t1 = fmul fast float %x, %z
384   %t2 = fmul fast float %y, %z
385   %t3 = fadd fast float %t1, %t2
386   ret float %t3
387 ; CHECK-LABEL: @fact_mul1(
388 ; CHECK: fmul fast float %1, %z
389 }
390
391 ; z*x + y*z => (x+y) * z
392 define float @fact_mul2(float %x, float %y, float %z) {
393   %t1 = fmul fast float %z, %x
394   %t2 = fmul fast float %y, %z
395   %t3 = fsub fast float %t1, %t2
396   ret float %t3
397 ; CHECK-LABEL: @fact_mul2(
398 ; CHECK: fmul fast float %1, %z
399 }
400
401 ; z*x - z*y => (x-y) * z
402 define float @fact_mul3(float %x, float %y, float %z) {
403   %t2 = fmul fast float %z, %y
404   %t1 = fmul fast float %z, %x
405   %t3 = fsub fast float %t1, %t2
406   ret float %t3
407 ; CHECK-LABEL: @fact_mul3(
408 ; CHECK: fmul fast float %1, %z
409 }
410
411 ; x*z - z*y => (x-y) * z
412 define float @fact_mul4(float %x, float %y, float %z) {
413   %t1 = fmul fast float %x, %z
414   %t2 = fmul fast float %z, %y
415   %t3 = fsub fast float %t1, %t2
416   ret float %t3
417 ; CHECK-LABEL: @fact_mul4(
418 ; CHECK: fmul fast float %1, %z
419 }
420
421 ; x/y + x/z, no xform
422 define float @fact_div1(float %x, float %y, float %z) {
423   %t1 = fdiv fast float %x, %y
424   %t2 = fdiv fast float %x, %z
425   %t3 = fadd fast float %t1, %t2
426   ret float %t3
427 ; CHECK: fact_div1
428 ; CHECK: fadd fast float %t1, %t2
429 }
430
431 ; x/y + z/x; no xform
432 define float @fact_div2(float %x, float %y, float %z) {
433   %t1 = fdiv fast float %x, %y
434   %t2 = fdiv fast float %z, %x
435   %t3 = fadd fast float %t1, %t2
436   ret float %t3
437 ; CHECK: fact_div2
438 ; CHECK: fadd fast float %t1, %t2
439 }
440
441 ; y/x + z/x => (y+z)/x
442 define float @fact_div3(float %x, float %y, float %z) {
443   %t1 = fdiv fast float %y, %x
444   %t2 = fdiv fast float %z, %x
445   %t3 = fadd fast float %t1, %t2
446   ret float %t3
447 ; CHECK: fact_div3
448 ; CHECK: fdiv fast float %1, %x
449 }
450
451 ; y/x - z/x => (y-z)/x
452 define float @fact_div4(float %x, float %y, float %z) {
453   %t1 = fdiv fast float %y, %x
454   %t2 = fdiv fast float %z, %x
455   %t3 = fsub fast float %t1, %t2
456   ret float %t3
457 ; CHECK: fact_div4
458 ; CHECK: fdiv fast float %1, %x
459 }
460
461 ; y/x - z/x => (y-z)/x is disabled if y-z is denormal.
462 define float @fact_div5(float %x) {
463   %t1 = fdiv fast float 0x3810000000000000, %x
464   %t2 = fdiv fast float 0x3800000000000000, %x
465   %t3 = fadd fast float %t1, %t2
466   ret float %t3
467 ; CHECK: fact_div5
468 ; CHECK: fdiv fast float 0x3818000000000000, %x
469 }
470
471 ; y/x - z/x => (y-z)/x is disabled if y-z is denormal.
472 define float @fact_div6(float %x) {
473   %t1 = fdiv fast float 0x3810000000000000, %x
474   %t2 = fdiv fast float 0x3800000000000000, %x
475   %t3 = fsub fast float %t1, %t2
476   ret float %t3
477 ; CHECK: fact_div6
478 ; CHECK: %t3 = fsub fast float %t1, %t2
479 }