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 float %a, 0x4006147AE0000000
13 ; Same testing-case as the one used in fold() except that the operators have
15 define float @notfold(float %a) {
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) {
25 ; CHECK: fmul 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
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
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
57 ; CHECK: fadd 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
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
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
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
110 ; CHECK: %t3 = fadd 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
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
133 ; =========================================================================
135 ; Testing-cases about fmul begin
137 ; =========================================================================
139 ; ((X*C1) + C2) * C3 => (X * (C1*C3)) + (C2*C3) (i.e. distribution)
140 define float @fmul_distribute1(float %f1) {
141 %t1 = fmul float %f1, 6.0e+3
142 %t2 = fadd float %t1, 2.0e+3
143 %t3 = fmul fast float %t2, 5.0e+3
145 ; CHECK: @fmul_distribute1
146 ; CHECK: %1 = fmul fast float %f1, 3.000000e+07
147 ; CHECK: %t3 = fadd fast float %1, 1.000000e+07
150 ; (X/C1 + C2) * C3 => X/(C1/C3) + C2*C3
151 define double @fmul_distribute2(double %f1, double %f2) {
152 %t1 = fdiv double %f1, 3.0e+0
153 %t2 = fadd double %t1, 5.0e+1
154 ; 0x10000000000000 = DBL_MIN
155 %t3 = fmul fast double %t2, 0x10000000000000
158 ; CHECK: @fmul_distribute2
159 ; CHECK: %1 = fdiv fast double %f1, 0x7FE8000000000000
160 ; CHECK: fadd fast double %1, 0x69000000000000
163 ; 5.0e-1 * DBL_MIN yields denormal, so "(f1*3.0 + 5.0e-1) * DBL_MIN" cannot
164 ; be simplified into f1 * (3.0*DBL_MIN) + (5.0e-1*DBL_MIN)
165 define double @fmul_distribute3(double %f1) {
166 %t1 = fdiv double %f1, 3.0e+0
167 %t2 = fadd double %t1, 5.0e-1
168 %t3 = fmul fast double %t2, 0x10000000000000
171 ; CHECK: @fmul_distribute3
172 ; CHECK: fmul fast double %t2, 0x10000000000000
175 ; C1/X * C2 => (C1*C2) / X
176 define float @fmul2(float %f1) {
177 %t1 = fdiv float 2.0e+3, %f1
178 %t3 = fmul fast float %t1, 6.0e+3
181 ; CHECK: fdiv fast float 1.200000e+07, %f1
184 ; X/C1 * C2 => X * (C2/C1) (if C2/C1 is normal Fp)
185 define float @fmul3(float %f1, float %f2) {
186 %t1 = fdiv float %f1, 2.0e+3
187 %t3 = fmul fast float %t1, 6.0e+3
190 ; CHECK: fmul fast float %f1, 3.000000e+00
193 ; Rule "X/C1 * C2 => X * (C2/C1) is not applicable if C2/C1 is either a special
194 ; value of a denormal. The 0x3810000000000000 here take value FLT_MIN
196 define float @fmul4(float %f1, float %f2) {
197 %t1 = fdiv float %f1, 2.0e+3
198 %t3 = fmul fast float %t1, 0x3810000000000000
201 ; CHECK: fmul fast float %t1, 0x3810000000000000
204 ; X / C1 * C2 => X / (C2/C1) if C1/C2 is either a special value of a denormal,
205 ; and C2/C1 is a normal value.
207 define float @fmul5(float %f1, float %f2) {
208 %t1 = fdiv float %f1, 3.0e+0
209 %t3 = fmul fast float %t1, 0x3810000000000000
212 ; CHECK: fdiv fast float %f1, 0x47E8000000000000
215 ; (X*Y) * X => (X*X) * Y
216 define float @fmul6(float %f1, float %f2) {
217 %mul = fmul float %f1, %f2
218 %mul1 = fmul fast float %mul, %f1
221 ; CHECK: fmul fast float %f1, %f1
224 ; "(X*Y) * X => (X*X) * Y" is disabled if "X*Y" has multiple uses
225 define float @fmul7(float %f1, float %f2) {
226 %mul = fmul float %f1, %f2
227 %mul1 = fmul fast float %mul, %f1
228 %add = fadd float %mul1, %mul
231 ; CHECK: fmul fast float %mul, %f1
234 ; =========================================================================
236 ; Testing-cases about negation
238 ; =========================================================================
239 define float @fneg1(float %f1, float %f2) {
240 %sub = fsub float -0.000000e+00, %f1
241 %sub1 = fsub nsz float 0.000000e+00, %f2
242 %mul = fmul float %sub, %sub1
245 ; CHECK: fmul float %f1, %f2
248 ; =========================================================================
250 ; Testing-cases about div
252 ; =========================================================================
254 ; X/C1 / C2 => X * (1/(C2*C1))
255 define float @fdiv1(float %x) {
256 %div = fdiv float %x, 0x3FF3333340000000
257 %div1 = fdiv fast float %div, 0x4002666660000000
259 ; 0x3FF3333340000000 = 1.2f
260 ; 0x4002666660000000 = 2.3f
261 ; 0x3FD7303B60000000 = 0.36231884057971014492
263 ; CHECK: fmul fast float %x, 0x3FD7303B60000000
266 ; X*C1 / C2 => X * (C1/C2)
267 define float @fdiv2(float %x) {
268 %mul = fmul float %x, 0x3FF3333340000000
269 %div1 = fdiv fast float %mul, 0x4002666660000000
272 ; 0x3FF3333340000000 = 1.2f
273 ; 0x4002666660000000 = 2.3f
274 ; 0x3FE0B21660000000 = 0.52173918485641479492
276 ; CHECK: fmul fast float %x, 0x3FE0B21660000000
279 ; "X/C1 / C2 => X * (1/(C2*C1))" is disabled (for now) is C2/C1 is a denormal
281 define float @fdiv3(float %x) {
282 %div = fdiv float %x, 0x47EFFFFFE0000000
283 %div1 = fdiv fast float %div, 0x4002666660000000
286 ; CHECK: fdiv float %x, 0x47EFFFFFE0000000
289 ; "X*C1 / C2 => X * (C1/C2)" is disabled if C1/C2 is a denormal
290 define float @fdiv4(float %x) {
291 %mul = fmul float %x, 0x47EFFFFFE0000000
292 %div = fdiv float %mul, 0x3FC99999A0000000
295 ; CHECK: fmul float %x, 0x47EFFFFFE0000000
298 ; (X/Y)/Z = > X/(Y*Z)
299 define float @fdiv5(float %f1, float %f2, float %f3) {
300 %t1 = fdiv float %f1, %f2
301 %t2 = fdiv fast float %t1, %f3
304 ; CHECK: fmul float %f2, %f3
307 ; Z/(X/Y) = > (Z*Y)/X
308 define float @fdiv6(float %f1, float %f2, float %f3) {
309 %t1 = fdiv float %f1, %f2
310 %t2 = fdiv fast float %f3, %t1
313 ; CHECK: fmul float %f3, %f2
316 ; C1/(X*C2) => (C1/C2) / X
317 define float @fdiv7(float %x) {
318 %t1 = fmul float %x, 3.0e0
319 %t2 = fdiv fast float 15.0e0, %t1
322 ; CHECK: fdiv fast float 5.000000e+00, %x
325 ; C1/(X/C2) => (C1*C2) / X
326 define float @fdiv8(float %x) {
327 %t1 = fdiv float %x, 3.0e0
328 %t2 = fdiv fast float 15.0e0, %t1
331 ; CHECK: fdiv fast float 4.500000e+01, %x
334 ; C1/(C2/X) => (C1/C2) * X
335 define float @fdiv9(float %x) {
336 %t1 = fdiv float 3.0e0, %x
337 %t2 = fdiv fast float 15.0e0, %t1
340 ; CHECK: fmul fast float %x, 5.000000e+00