Consider expression "0.0 - X" as the negation of X if
[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: @fold
10 ; CHECK: fmul 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: @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: @fold2
25 ; CHECK: fmul 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: @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: @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: @fold5
57 ; CHECK: fadd 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: @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: @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: @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: @fold10
110 ; CHECK: %t3 = fadd 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: @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: @fail2
130 ; CHECK: ret
131 }
132
133 ; rdar://12753946:  x * cond ? 1.0 : 0.0 => cond ? x : 0.0
134 define double @select1(i32 %cond, double %x, double %y) {
135   %tobool = icmp ne i32 %cond, 0
136   %cond1 = select i1 %tobool, double 1.000000e+00, double 0.000000e+00
137   %mul = fmul nnan nsz double %cond1, %x
138   %add = fadd double %mul, %y
139   ret double %add
140 ; CHECK: @select1
141 ; CHECK: select i1 %tobool, double %x, double 0.000000e+00
142 }
143
144 define double @select2(i32 %cond, double %x, double %y) {
145   %tobool = icmp ne i32 %cond, 0
146   %cond1 = select i1 %tobool, double 0.000000e+00, double 1.000000e+00
147   %mul = fmul nnan nsz double %cond1, %x
148   %add = fadd double %mul, %y
149   ret double %add
150 ; CHECK: @select2
151 ; CHECK: select i1 %tobool, double 0.000000e+00, double %x
152 }
153
154 define double @select3(i32 %cond, double %x, double %y) {
155   %tobool = icmp ne i32 %cond, 0
156   %cond1 = select i1 %tobool, double 0.000000e+00, double 2.000000e+00
157   %mul = fmul nnan nsz double %cond1, %x
158   %add = fadd double %mul, %y
159   ret double %add
160 ; CHECK: @select3
161 ; CHECK: fmul nnan nsz double %cond1, %x
162 }
163
164 ; =========================================================================
165 ;
166 ;   Testing-cases about fmul begin
167 ;
168 ; =========================================================================
169
170 ; ((X*C1) + C2) * C3 => (X * (C1*C3)) + (C2*C3) (i.e. distribution)
171 define float @fmul_distribute1(float %f1) {
172   %t1 = fmul float %f1, 6.0e+3
173   %t2 = fadd float %t1, 2.0e+3
174   %t3 = fmul fast float %t2, 5.0e+3
175   ret float %t3 
176 ; CHECK: @fmul_distribute1
177 ; CHECK: %1 = fmul fast float %f1, 3.000000e+07
178 ; CHECK: %t3 = fadd fast float %1, 1.000000e+07
179 }
180
181 ; (X/C1 + C2) * C3 => X/(C1/C3) + C2*C3
182 define double @fmul_distribute2(double %f1, double %f2) {
183   %t1 = fdiv double %f1, 3.0e+0
184   %t2 = fadd double %t1, 5.0e+1
185   ; 0x10000000000000 = DBL_MIN
186   %t3 = fmul fast double %t2, 0x10000000000000
187   ret double %t3
188
189 ; CHECK: @fmul_distribute2
190 ; CHECK: %1 = fdiv fast double %f1, 0x7FE8000000000000
191 ; CHECK: fadd fast double %1, 0x69000000000000
192 }
193
194 ; 5.0e-1 * DBL_MIN yields denormal, so "(f1*3.0 + 5.0e-1) * DBL_MIN" cannot
195 ; be simplified into f1 * (3.0*DBL_MIN) + (5.0e-1*DBL_MIN)
196 define double @fmul_distribute3(double %f1) {
197   %t1 = fdiv double %f1, 3.0e+0
198   %t2 = fadd double %t1, 5.0e-1
199   %t3 = fmul fast double %t2, 0x10000000000000
200   ret double %t3
201
202 ; CHECK: @fmul_distribute3
203 ; CHECK: fmul fast double %t2, 0x10000000000000
204 }
205
206 ; C1/X * C2 => (C1*C2) / X
207 define float @fmul2(float %f1) {
208   %t1 = fdiv float 2.0e+3, %f1 
209   %t3 = fmul fast float %t1, 6.0e+3
210   ret float %t3 
211 ; CHECK: @fmul2
212 ; CHECK: fdiv fast float 1.200000e+07, %f1
213 }
214
215 ; X/C1 * C2 => X * (C2/C1) (if C2/C1 is normal Fp)
216 define float @fmul3(float %f1, float %f2) {
217   %t1 = fdiv float %f1, 2.0e+3
218   %t3 = fmul fast float %t1, 6.0e+3
219   ret float %t3 
220 ; CHECK: @fmul3
221 ; CHECK: fmul fast float %f1, 3.000000e+00
222 }
223
224 ; Rule "X/C1 * C2 => X * (C2/C1) is not applicable if C2/C1 is either a special
225 ; value of a denormal. The 0x3810000000000000 here take value FLT_MIN
226 ;
227 define float @fmul4(float %f1, float %f2) {
228   %t1 = fdiv float %f1, 2.0e+3
229   %t3 = fmul fast float %t1, 0x3810000000000000
230   ret float %t3 
231 ; CHECK: @fmul4
232 ; CHECK: fmul fast float %t1, 0x3810000000000000
233 }
234
235 ; X / C1 * C2 => X / (C2/C1) if  C1/C2 is either a special value of a denormal, 
236 ;  and C2/C1 is a normal value.
237
238 define float @fmul5(float %f1, float %f2) {
239   %t1 = fdiv float %f1, 3.0e+0
240   %t3 = fmul fast float %t1, 0x3810000000000000
241   ret float %t3 
242 ; CHECK: @fmul5
243 ; CHECK: fdiv fast float %f1, 0x47E8000000000000
244 }
245
246 ; =========================================================================
247 ;
248 ;   Testing-cases about negation
249 ;
250 ; =========================================================================
251 define float @fneg1(float %f1, float %f2) {
252   %sub = fsub float -0.000000e+00, %f1
253   %sub1 = fsub nsz float 0.000000e+00, %f2
254   %mul = fmul float %sub, %sub1
255   ret float %mul
256 ; CHECK: @fneg1
257 ; CHECK: fmul float %f1, %f2
258 }