1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -reassociate -gvn -instcombine -S | FileCheck %s
4 ; With reassociation, constant folding can eliminate the 12 and -12 constants.
5 define float @test1(float %arg) {
7 ; CHECK-NEXT: [[ARG_NEG:%.*]] = fsub fast float -0.000000e+00, [[ARG:%.*]]
8 ; CHECK-NEXT: ret float [[ARG_NEG]]
10 %t1 = fsub fast float -1.200000e+01, %arg
11 %t2 = fadd fast float %t1, 1.200000e+01
15 ; Check again using the minimal subset of FMF.
16 ; Both 'reassoc' and 'nsz' are required.
17 define float @test1_minimal(float %arg) {
18 ; CHECK-LABEL: @test1_minimal(
19 ; CHECK-NEXT: [[TMP1:%.*]] = fsub reassoc nsz float -0.000000e+00, [[ARG:%.*]]
20 ; CHECK-NEXT: ret float [[TMP1]]
22 %t1 = fsub reassoc nsz float -1.200000e+01, %arg
23 %t2 = fadd reassoc nsz float %t1, 1.200000e+01
27 ; Verify the fold is not done with only 'reassoc' ('nsz' is required).
28 define float @test1_reassoc(float %arg) {
29 ; CHECK-LABEL: @test1_reassoc(
30 ; CHECK-NEXT: [[T1:%.*]] = fsub reassoc float -1.200000e+01, [[ARG:%.*]]
31 ; CHECK-NEXT: [[T2:%.*]] = fadd reassoc float [[T1]], 1.200000e+01
32 ; CHECK-NEXT: ret float [[T2]]
34 %t1 = fsub reassoc float -1.200000e+01, %arg
35 %t2 = fadd reassoc float %t1, 1.200000e+01
39 define float @test2(float %reg109, float %reg1111) {
40 ; CHECK-LABEL: @test2(
41 ; CHECK-NEXT: [[REG115:%.*]] = fadd float [[REG109:%.*]], -3.000000e+01
42 ; CHECK-NEXT: [[REG116:%.*]] = fadd float [[REG115]], [[REG1111:%.*]]
43 ; CHECK-NEXT: [[REG117:%.*]] = fadd float [[REG116]], 3.000000e+01
44 ; CHECK-NEXT: ret float [[REG117]]
46 %reg115 = fadd float %reg109, -3.000000e+01
47 %reg116 = fadd float %reg115, %reg1111
48 %reg117 = fadd float %reg116, 3.000000e+01
52 define float @test3(float %reg109, float %reg1111) {
53 ; CHECK-LABEL: @test3(
54 ; CHECK-NEXT: [[REG117:%.*]] = fadd fast float [[REG109:%.*]], [[REG1111:%.*]]
55 ; CHECK-NEXT: ret float [[REG117]]
57 %reg115 = fadd fast float %reg109, -3.000000e+01
58 %reg116 = fadd fast float %reg115, %reg1111
59 %reg117 = fadd fast float %reg116, 3.000000e+01
63 define float @test3_reassoc(float %reg109, float %reg1111) {
64 ; CHECK-LABEL: @test3_reassoc(
65 ; CHECK-NEXT: [[REG115:%.*]] = fadd reassoc float [[REG109:%.*]], -3.000000e+01
66 ; CHECK-NEXT: [[REG116:%.*]] = fadd reassoc float [[REG115]], [[REG1111:%.*]]
67 ; CHECK-NEXT: [[REG117:%.*]] = fadd reassoc float [[REG116]], 3.000000e+01
68 ; CHECK-NEXT: ret float [[REG117]]
70 %reg115 = fadd reassoc float %reg109, -3.000000e+01
71 %reg116 = fadd reassoc float %reg115, %reg1111
72 %reg117 = fadd reassoc float %reg116, 3.000000e+01
76 @fe = external global float
77 @fa = external global float
78 @fb = external global float
79 @fc = external global float
80 @ff = external global float
82 define void @test4() {
83 ; CHECK-LABEL: @test4(
84 ; CHECK-NEXT: [[A:%.*]] = load float, float* @fa, align 4
85 ; CHECK-NEXT: [[B:%.*]] = load float, float* @fb, align 4
86 ; CHECK-NEXT: [[C:%.*]] = load float, float* @fc, align 4
87 ; CHECK-NEXT: [[T1:%.*]] = fadd fast float [[B]], [[A]]
88 ; CHECK-NEXT: [[T2:%.*]] = fadd fast float [[T1]], [[C]]
89 ; CHECK-NEXT: store float [[T2]], float* @fe, align 4
90 ; CHECK-NEXT: store float [[T2]], float* @ff, align 4
91 ; CHECK-NEXT: ret void
93 %A = load float, float* @fa
94 %B = load float, float* @fb
95 %C = load float, float* @fc
96 %t1 = fadd fast float %A, %B
97 %t2 = fadd fast float %t1, %C
98 %t3 = fadd fast float %C, %A
99 %t4 = fadd fast float %t3, %B
101 store float %t2, float* @fe
103 store float %t4, float* @ff
107 define void @test5() {
108 ; CHECK-LABEL: @test5(
109 ; CHECK-NEXT: [[A:%.*]] = load float, float* @fa, align 4
110 ; CHECK-NEXT: [[B:%.*]] = load float, float* @fb, align 4
111 ; CHECK-NEXT: [[C:%.*]] = load float, float* @fc, align 4
112 ; CHECK-NEXT: [[T1:%.*]] = fadd fast float [[B]], [[A]]
113 ; CHECK-NEXT: [[T2:%.*]] = fadd fast float [[T1]], [[C]]
114 ; CHECK-NEXT: store float [[T2]], float* @fe, align 4
115 ; CHECK-NEXT: store float [[T2]], float* @ff, align 4
116 ; CHECK-NEXT: ret void
118 %A = load float, float* @fa
119 %B = load float, float* @fb
120 %C = load float, float* @fc
121 %t1 = fadd fast float %A, %B
122 %t2 = fadd fast float %t1, %C
123 %t3 = fadd fast float %C, %A
124 %t4 = fadd fast float %t3, %B
126 store float %t2, float* @fe
128 store float %t4, float* @ff
132 define void @test6() {
133 ; CHECK-LABEL: @test6(
134 ; CHECK-NEXT: [[A:%.*]] = load float, float* @fa, align 4
135 ; CHECK-NEXT: [[B:%.*]] = load float, float* @fb, align 4
136 ; CHECK-NEXT: [[C:%.*]] = load float, float* @fc, align 4
137 ; CHECK-NEXT: [[T1:%.*]] = fadd fast float [[B]], [[A]]
138 ; CHECK-NEXT: [[T2:%.*]] = fadd fast float [[T1]], [[C]]
139 ; CHECK-NEXT: store float [[T2]], float* @fe, align 4
140 ; CHECK-NEXT: store float [[T2]], float* @ff, align 4
141 ; CHECK-NEXT: ret void
143 %A = load float, float* @fa
144 %B = load float, float* @fb
145 %C = load float, float* @fc
146 %t1 = fadd fast float %B, %A
147 %t2 = fadd fast float %t1, %C
148 %t3 = fadd fast float %C, %A
149 %t4 = fadd fast float %t3, %B
151 store float %t2, float* @fe
153 store float %t4, float* @ff
157 define float @test7(float %A, float %B, float %C) {
158 ; CHECK-LABEL: @test7(
159 ; CHECK-NEXT: [[REASS_ADD1:%.*]] = fadd fast float [[C:%.*]], [[B:%.*]]
160 ; CHECK-NEXT: [[REASS_MUL2:%.*]] = fmul fast float [[A:%.*]], [[A]]
161 ; CHECK-NEXT: [[REASS_MUL:%.*]] = fmul fast float [[REASS_MUL2]], [[REASS_ADD1]]
162 ; CHECK-NEXT: ret float [[REASS_MUL]]
164 %aa = fmul fast float %A, %A
165 %aab = fmul fast float %aa, %B
166 %ac = fmul fast float %A, %C
167 %aac = fmul fast float %ac, %A
168 %r = fadd fast float %aab, %aac
172 define float @test7_reassoc(float %A, float %B, float %C) {
173 ; CHECK-LABEL: @test7_reassoc(
174 ; CHECK-NEXT: [[AA:%.*]] = fmul reassoc float [[A:%.*]], [[A]]
175 ; CHECK-NEXT: [[AAB:%.*]] = fmul reassoc float [[AA]], [[B:%.*]]
176 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc float [[A]], [[A]]
177 ; CHECK-NEXT: [[AAC:%.*]] = fmul reassoc float [[TMP1]], [[C:%.*]]
178 ; CHECK-NEXT: [[R:%.*]] = fadd reassoc float [[AAB]], [[AAC]]
179 ; CHECK-NEXT: ret float [[R]]
181 %aa = fmul reassoc float %A, %A
182 %aab = fmul reassoc float %aa, %B
183 %ac = fmul reassoc float %A, %C
184 %aac = fmul reassoc float %ac, %A
185 %r = fadd reassoc float %aab, %aac
189 ; (-X)*Y + Z -> Z-X*Y
191 define float @test8(float %X, float %Y, float %Z) {
192 ; CHECK-LABEL: @test8(
193 ; CHECK-NEXT: [[A:%.*]] = fmul fast float [[Y:%.*]], [[X:%.*]]
194 ; CHECK-NEXT: [[C:%.*]] = fsub fast float [[Z:%.*]], [[A]]
195 ; CHECK-NEXT: ret float [[C]]
197 %A = fsub fast float 0.0, %X
198 %B = fmul fast float %A, %Y
199 %C = fadd fast float %B, %Z
203 define float @test8_unary_fneg(float %X, float %Y, float %Z) {
204 ; CHECK-LABEL: @test8_unary_fneg(
205 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[X:%.*]], [[Y:%.*]]
206 ; CHECK-NEXT: [[C:%.*]] = fsub fast float [[Z:%.*]], [[TMP1]]
207 ; CHECK-NEXT: ret float [[C]]
209 %A = fneg fast float %X
210 %B = fmul fast float %A, %Y
211 %C = fadd fast float %B, %Z
215 define float @test8_reassoc(float %X, float %Y, float %Z) {
216 ; CHECK-LABEL: @test8_reassoc(
217 ; CHECK-NEXT: [[A:%.*]] = fsub reassoc float 0.000000e+00, [[X:%.*]]
218 ; CHECK-NEXT: [[B:%.*]] = fmul reassoc float [[A]], [[Y:%.*]]
219 ; CHECK-NEXT: [[C:%.*]] = fadd reassoc float [[B]], [[Z:%.*]]
220 ; CHECK-NEXT: ret float [[C]]
222 %A = fsub reassoc float 0.0, %X
223 %B = fmul reassoc float %A, %Y
224 %C = fadd reassoc float %B, %Z
228 define float @test9(float %X) {
229 ; CHECK-LABEL: @test9(
230 ; CHECK-NEXT: [[FACTOR:%.*]] = fmul fast float [[X:%.*]], 9.400000e+01
231 ; CHECK-NEXT: ret float [[FACTOR]]
233 %Y = fmul fast float %X, 4.700000e+01
234 %Z = fadd fast float %Y, %Y
238 ; Check again with 'reassoc' and 'nsz' ('nsz' not technically required).
239 define float @test9_reassoc_nsz(float %X) {
240 ; CHECK-LABEL: @test9_reassoc_nsz(
241 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc nsz float [[X:%.*]], 9.400000e+01
242 ; CHECK-NEXT: ret float [[TMP1]]
244 %Y = fmul reassoc nsz float %X, 4.700000e+01
245 %Z = fadd reassoc nsz float %Y, %Y
249 ; TODO: This doesn't require 'nsz'. It should fold to X * 94.0
250 define float @test9_reassoc(float %X) {
251 ; CHECK-LABEL: @test9_reassoc(
252 ; CHECK-NEXT: [[Y:%.*]] = fmul reassoc float [[X:%.*]], 4.700000e+01
253 ; CHECK-NEXT: [[Z:%.*]] = fadd reassoc float [[Y]], [[Y]]
254 ; CHECK-NEXT: ret float [[Z]]
256 %Y = fmul reassoc float %X, 4.700000e+01
257 %Z = fadd reassoc float %Y, %Y
261 ; Side note: (x + x + x) and (3*x) each have only a single rounding. So
262 ; transforming x+x+x to 3*x is always safe, even without any FMF.
263 ; To avoid that special-case, we have the addition of 'x' four times, here.
264 define float @test10(float %X) {
265 ; CHECK-LABEL: @test10(
266 ; CHECK-NEXT: [[FACTOR:%.*]] = fmul fast float [[X:%.*]], 4.000000e+00
267 ; CHECK-NEXT: ret float [[FACTOR]]
269 %Y = fadd fast float %X ,%X
270 %Z = fadd fast float %Y, %X
271 %W = fadd fast float %Z, %X
275 ; Check again with 'reassoc' and 'nsz' ('nsz' not technically required).
276 define float @test10_reassoc_nsz(float %X) {
277 ; CHECK-LABEL: @test10_reassoc_nsz(
278 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc nsz float [[X:%.*]], 4.000000e+00
279 ; CHECK-NEXT: ret float [[TMP1]]
281 %Y = fadd reassoc nsz float %X ,%X
282 %Z = fadd reassoc nsz float %Y, %X
283 %W = fadd reassoc nsz float %Z, %X
287 ; TODO: This doesn't require 'nsz'. It should fold to 4 * x
288 define float @test10_reassoc(float %X) {
289 ; CHECK-LABEL: @test10_reassoc(
290 ; CHECK-NEXT: [[Y:%.*]] = fadd reassoc float [[X:%.*]], [[X]]
291 ; CHECK-NEXT: [[Z:%.*]] = fadd reassoc float [[Y]], [[X]]
292 ; CHECK-NEXT: [[W:%.*]] = fadd reassoc float [[Z]], [[X]]
293 ; CHECK-NEXT: ret float [[W]]
295 %Y = fadd reassoc float %X ,%X
296 %Z = fadd reassoc float %Y, %X
297 %W = fadd reassoc float %Z, %X
301 define float @test11(float %W) {
302 ; CHECK-LABEL: @test11(
303 ; CHECK-NEXT: [[FACTOR:%.*]] = fmul fast float [[W:%.*]], 3.810000e+02
304 ; CHECK-NEXT: ret float [[FACTOR]]
306 %X = fmul fast float %W, 127.0
307 %Y = fadd fast float %X ,%X
308 %Z = fadd fast float %Y, %X
312 ; Check again using the minimal subset of FMF.
313 ; Check again with 'reassoc' and 'nsz' ('nsz' not technically required).
314 define float @test11_reassoc_nsz(float %W) {
315 ; CHECK-LABEL: @test11_reassoc_nsz(
316 ; CHECK-NEXT: [[Z:%.*]] = fmul reassoc nsz float [[W:%.*]], 3.810000e+02
317 ; CHECK-NEXT: ret float [[Z]]
319 %X = fmul reassoc nsz float %W, 127.0
320 %Y = fadd reassoc nsz float %X ,%X
321 %Z = fadd reassoc nsz float %Y, %X
325 ; TODO: This doesn't require 'nsz'. It should fold to W*381.0.
326 define float @test11_reassoc(float %W) {
327 ; CHECK-LABEL: @test11_reassoc(
328 ; CHECK-NEXT: [[X:%.*]] = fmul reassoc float [[W:%.*]], 1.270000e+02
329 ; CHECK-NEXT: [[Y:%.*]] = fadd reassoc float [[X]], [[X]]
330 ; CHECK-NEXT: [[Z:%.*]] = fadd reassoc float [[X]], [[Y]]
331 ; CHECK-NEXT: ret float [[Z]]
333 %X = fmul reassoc float %W, 127.0
334 %Y = fadd reassoc float %X ,%X
335 %Z = fadd reassoc float %Y, %X
339 define float @test12(float %X) {
340 ; CHECK-LABEL: @test12(
341 ; CHECK-NEXT: [[FACTOR:%.*]] = fmul fast float [[X:%.*]], -3.000000e+00
342 ; CHECK-NEXT: [[Z:%.*]] = fadd fast float [[FACTOR]], 6.000000e+00
343 ; CHECK-NEXT: ret float [[Z]]
345 %A = fsub fast float 1.000000e+00, %X
346 %B = fsub fast float 2.000000e+00, %X
347 %C = fsub fast float 3.000000e+00, %X
348 %Y = fadd fast float %A ,%B
349 %Z = fadd fast float %Y, %C
353 ; Check again with 'reassoc' and 'nsz' ('nsz' not technically required).
354 define float @test12_reassoc_nsz(float %X) {
355 ; CHECK-LABEL: @test12_reassoc_nsz(
356 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc nsz float [[X:%.*]], 3.000000e+00
357 ; CHECK-NEXT: [[TMP2:%.*]] = fsub reassoc nsz float 6.000000e+00, [[TMP1]]
358 ; CHECK-NEXT: ret float [[TMP2]]
360 %A = fsub reassoc nsz float 1.000000e+00, %X
361 %B = fsub reassoc nsz float 2.000000e+00, %X
362 %C = fsub reassoc nsz float 3.000000e+00, %X
363 %Y = fadd reassoc nsz float %A ,%B
364 %Z = fadd reassoc nsz float %Y, %C
368 ; TODO: This doesn't require 'nsz'. It should fold to (6.0 - 3.0*x)
369 define float @test12_reassoc(float %X) {
370 ; CHECK-LABEL: @test12_reassoc(
371 ; CHECK-NEXT: [[A:%.*]] = fsub reassoc float 1.000000e+00, [[X:%.*]]
372 ; CHECK-NEXT: [[B:%.*]] = fsub reassoc float 2.000000e+00, [[X]]
373 ; CHECK-NEXT: [[C:%.*]] = fsub reassoc float 3.000000e+00, [[X]]
374 ; CHECK-NEXT: [[Y:%.*]] = fadd reassoc float [[A]], [[B]]
375 ; CHECK-NEXT: [[Z:%.*]] = fadd reassoc float [[C]], [[Y]]
376 ; CHECK-NEXT: ret float [[Z]]
378 %A = fsub reassoc float 1.000000e+00, %X
379 %B = fsub reassoc float 2.000000e+00, %X
380 %C = fsub reassoc float 3.000000e+00, %X
381 %Y = fadd reassoc float %A ,%B
382 %Z = fadd reassoc float %Y, %C
386 define float @test13(float %X1, float %X2, float %X3) {
387 ; CHECK-LABEL: @test13(
388 ; CHECK-NEXT: [[REASS_ADD:%.*]] = fsub fast float [[X3:%.*]], [[X2:%.*]]
389 ; CHECK-NEXT: [[REASS_MUL:%.*]] = fmul fast float [[REASS_ADD]], [[X1:%.*]]
390 ; CHECK-NEXT: ret float [[REASS_MUL]]
392 %A = fsub fast float 0.000000e+00, %X1
393 %B = fmul fast float %A, %X2 ; -X1*X2
394 %C = fmul fast float %X1, %X3 ; X1*X3
395 %D = fadd fast float %B, %C ; -X1*X2 + X1*X3 -> X1*(X3-X2)
399 define float @test13_unary_fneg(float %X1, float %X2, float %X3) {
400 ; CHECK-LABEL: @test13_unary_fneg(
401 ; CHECK-NEXT: [[TMP1:%.*]] = fsub fast float [[X3:%.*]], [[X2:%.*]]
402 ; CHECK-NEXT: [[D:%.*]] = fmul fast float [[TMP1]], [[X1:%.*]]
403 ; CHECK-NEXT: ret float [[D]]
405 %A = fneg fast float %X1
406 %B = fmul fast float %A, %X2 ; -X1*X2
407 %C = fmul fast float %X1, %X3 ; X1*X3
408 %D = fadd fast float %B, %C ; -X1*X2 + X1*X3 -> X1*(X3-X2)
412 define float @test13_reassoc(float %X1, float %X2, float %X3) {
413 ; CHECK-LABEL: @test13_reassoc(
414 ; CHECK-NEXT: [[A:%.*]] = fsub reassoc float 0.000000e+00, [[X1:%.*]]
415 ; CHECK-NEXT: [[B:%.*]] = fmul reassoc float [[A]], [[X2:%.*]]
416 ; CHECK-NEXT: [[C:%.*]] = fmul reassoc float [[X1]], [[X3:%.*]]
417 ; CHECK-NEXT: [[D:%.*]] = fadd reassoc float [[B]], [[C]]
418 ; CHECK-NEXT: ret float [[D]]
420 %A = fsub reassoc float 0.000000e+00, %X1
421 %B = fmul reassoc float %A, %X2 ; -X1*X2
422 %C = fmul reassoc float %X1, %X3 ; X1*X3
423 %D = fadd reassoc float %B, %C ; -X1*X2 + X1*X3 -> X1*(X3-X2)
427 define float @test14(float %X1, float %X2) {
428 ; CHECK-LABEL: @test14(
429 ; CHECK-NEXT: [[TMP1:%.*]] = fsub fast float [[X1:%.*]], [[X2:%.*]]
430 ; CHECK-NEXT: [[D1:%.*]] = fmul fast float [[TMP1]], 4.700000e+01
431 ; CHECK-NEXT: ret float [[D1]]
433 %B = fmul fast float %X1, 47. ; X1*47
434 %C = fmul fast float %X2, -47. ; X2*-47
435 %D = fadd fast float %B, %C ; X1*47 + X2*-47 -> 47*(X1-X2)
439 ; (x1 * 47) + (x2 * -47) => (x1 - x2) * 47
440 ; Check again with 'reassoc' and 'nsz' ('nsz' not technically required).
441 define float @test14_reassoc_nsz(float %X1, float %X2) {
442 ; CHECK-LABEL: @test14_reassoc_nsz(
443 ; CHECK-NEXT: [[TMP1:%.*]] = fsub reassoc nsz float [[X1:%.*]], [[X2:%.*]]
444 ; CHECK-NEXT: [[D1:%.*]] = fmul reassoc nsz float [[TMP1]], 4.700000e+01
445 ; CHECK-NEXT: ret float [[D1]]
447 %B = fmul reassoc nsz float %X1, 47. ; X1*47
448 %C = fmul reassoc nsz float %X2, -47. ; X2*-47
449 %D = fadd reassoc nsz float %B, %C ; X1*47 + X2*-47 -> 47*(X1-X2)
453 ; TODO: This doesn't require 'nsz'. It should fold to ((x1 - x2) * 47.0)
454 define float @test14_reassoc(float %X1, float %X2) {
455 ; CHECK-LABEL: @test14_reassoc(
456 ; CHECK-NEXT: [[B:%.*]] = fmul reassoc float [[X1:%.*]], 4.700000e+01
457 ; CHECK-NEXT: [[C:%.*]] = fmul reassoc float [[X2:%.*]], 4.700000e+01
458 ; CHECK-NEXT: [[D1:%.*]] = fsub reassoc float [[B]], [[C]]
459 ; CHECK-NEXT: ret float [[D1]]
461 %B = fmul reassoc float %X1, 47. ; X1*47
462 %C = fmul reassoc float %X2, -47. ; X2*-47
463 %D = fadd reassoc float %B, %C ; X1*47 + X2*-47 -> 47*(X1-X2)
467 define float @test15(float %arg) {
468 ; CHECK-LABEL: @test15(
469 ; CHECK-NEXT: [[T2:%.*]] = fmul fast float [[ARG:%.*]], 1.440000e+02
470 ; CHECK-NEXT: ret float [[T2]]
472 %t1 = fmul fast float 1.200000e+01, %arg
473 %t2 = fmul fast float %t1, 1.200000e+01
477 define float @test15_reassoc(float %arg) {
478 ; CHECK-LABEL: @test15_reassoc(
479 ; CHECK-NEXT: [[T2:%.*]] = fmul reassoc float [[ARG:%.*]], 1.440000e+02
480 ; CHECK-NEXT: ret float [[T2]]
482 %t1 = fmul reassoc float 1.200000e+01, %arg
483 %t2 = fmul reassoc float %t1, 1.200000e+01
487 ; (b+(a+1234))+-a -> b+1234
488 define float @test16(float %b, float %a) {
489 ; CHECK-LABEL: @test16(
490 ; CHECK-NEXT: [[TMP1:%.*]] = fadd fast float [[B:%.*]], 1.234000e+03
491 ; CHECK-NEXT: ret float [[TMP1]]
493 %1 = fadd fast float %a, 1234.0
494 %2 = fadd fast float %b, %1
495 %3 = fsub fast float 0.0, %a
496 %4 = fadd fast float %2, %3
500 define float @test16_unary_fneg(float %b, float %a) {
501 ; CHECK-LABEL: @test16_unary_fneg(
502 ; CHECK-NEXT: [[TMP1:%.*]] = fadd fast float [[B:%.*]], 1.234000e+03
503 ; CHECK-NEXT: ret float [[TMP1]]
505 %1 = fadd fast float %a, 1234.0
506 %2 = fadd fast float %b, %1
507 %3 = fneg fast float %a
508 %4 = fadd fast float %2, %3
512 define float @test16_reassoc(float %b, float %a) {
513 ; CHECK-LABEL: @test16_reassoc(
514 ; CHECK-NEXT: [[TMP1:%.*]] = fadd reassoc float [[A:%.*]], 1.234000e+03
515 ; CHECK-NEXT: [[TMP2:%.*]] = fadd reassoc float [[TMP1]], [[B:%.*]]
516 ; CHECK-NEXT: [[TMP3:%.*]] = fsub reassoc float 0.000000e+00, [[A]]
517 ; CHECK-NEXT: [[TMP4:%.*]] = fadd reassoc float [[TMP3]], [[TMP2]]
518 ; CHECK-NEXT: ret float [[TMP4]]
520 %1 = fadd reassoc float %a, 1234.0
521 %2 = fadd reassoc float %b, %1
522 %3 = fsub reassoc float 0.0, %a
523 %4 = fadd reassoc float %2, %3
527 ; Test that we can turn things like X*-(Y*Z) -> X*-1*Y*Z.
529 define float @test17(float %a, float %b, float %z) {
530 ; CHECK-LABEL: @test17(
531 ; CHECK-NEXT: [[E:%.*]] = fmul fast float [[A:%.*]], 1.234500e+04
532 ; CHECK-NEXT: [[F:%.*]] = fmul fast float [[E]], [[B:%.*]]
533 ; CHECK-NEXT: [[G:%.*]] = fmul fast float [[F]], [[Z:%.*]]
534 ; CHECK-NEXT: ret float [[G]]
536 %c = fsub fast float 0.000000e+00, %z
537 %d = fmul fast float %a, %b
538 %e = fmul fast float %c, %d
539 %f = fmul fast float %e, 1.234500e+04
540 %g = fsub fast float 0.000000e+00, %f
544 define float @test17_unary_fneg(float %a, float %b, float %z) {
545 ; CHECK-LABEL: @test17_unary_fneg(
546 ; CHECK-NEXT: [[D:%.*]] = fmul fast float [[A:%.*]], 1.234500e+04
547 ; CHECK-NEXT: [[E:%.*]] = fmul fast float [[D]], [[B:%.*]]
548 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[E]], [[Z:%.*]]
549 ; CHECK-NEXT: ret float [[TMP1]]
551 %c = fneg fast float %z
552 %d = fmul fast float %a, %b
553 %e = fmul fast float %c, %d
554 %f = fmul fast float %e, 1.234500e+04
555 %g = fneg fast float %f
559 define float @test17_reassoc(float %a, float %b, float %z) {
560 ; CHECK-LABEL: @test17_reassoc(
561 ; CHECK-NEXT: [[C:%.*]] = fsub reassoc float 0.000000e+00, [[Z:%.*]]
562 ; CHECK-NEXT: [[D:%.*]] = fmul reassoc float [[A:%.*]], [[B:%.*]]
563 ; CHECK-NEXT: [[E:%.*]] = fmul reassoc float [[D]], [[C]]
564 ; CHECK-NEXT: [[F:%.*]] = fmul reassoc float [[E]], 1.234500e+04
565 ; CHECK-NEXT: [[G:%.*]] = fsub reassoc float 0.000000e+00, [[F]]
566 ; CHECK-NEXT: ret float [[G]]
568 %c = fsub reassoc float 0.000000e+00, %z
569 %d = fmul reassoc float %a, %b
570 %e = fmul reassoc float %c, %d
571 %f = fmul reassoc float %e, 1.234500e+04
572 %g = fsub reassoc float 0.000000e+00, %f
576 define float @test18(float %a, float %b, float %z) {
577 ; CHECK-LABEL: @test18(
578 ; CHECK-NEXT: [[E:%.*]] = fmul fast float [[A:%.*]], 4.000000e+01
579 ; CHECK-NEXT: [[F:%.*]] = fmul fast float [[E]], [[Z:%.*]]
580 ; CHECK-NEXT: ret float [[F]]
582 %d = fmul fast float %z, 4.000000e+01
583 %c = fsub fast float 0.000000e+00, %d
584 %e = fmul fast float %a, %c
585 %f = fsub fast float 0.000000e+00, %e
589 define float @test18_unary_fneg(float %a, float %b, float %z) {
590 ; CHECK-LABEL: @test18_unary_fneg(
591 ; CHECK-NEXT: [[E:%.*]] = fmul fast float [[A:%.*]], 4.000000e+01
592 ; CHECK-NEXT: [[F:%.*]] = fmul fast float [[E]], [[Z:%.*]]
593 ; CHECK-NEXT: ret float [[F]]
595 %d = fmul fast float %z, 4.000000e+01
596 %c = fneg fast float %d
597 %e = fmul fast float %a, %c
598 %f = fneg fast float %e
602 define float @test18_reassoc(float %a, float %b, float %z) {
603 ; CHECK-LABEL: @test18_reassoc(
604 ; CHECK-NEXT: [[D:%.*]] = fmul reassoc float [[Z:%.*]], 4.000000e+01
605 ; CHECK-NEXT: [[C:%.*]] = fsub reassoc float 0.000000e+00, [[D]]
606 ; CHECK-NEXT: [[E:%.*]] = fmul reassoc float [[C]], [[A:%.*]]
607 ; CHECK-NEXT: [[F:%.*]] = fsub reassoc float 0.000000e+00, [[E]]
608 ; CHECK-NEXT: ret float [[F]]
610 %d = fmul reassoc float %z, 4.000000e+01
611 %c = fsub reassoc float 0.000000e+00, %d
612 %e = fmul reassoc float %a, %c
613 %f = fsub reassoc float 0.000000e+00, %e
617 ; It is not safe to reassociate unary fneg without nnan.
618 define float @test18_reassoc_unary_fneg(float %a, float %b, float %z) {
619 ; CHECK-LABEL: @test18_reassoc_unary_fneg(
620 ; CHECK-NEXT: [[C:%.*]] = fmul reassoc float [[Z:%.*]], -4.000000e+01
621 ; CHECK-NEXT: [[E:%.*]] = fmul reassoc float [[C]], [[A:%.*]]
622 ; CHECK-NEXT: [[F:%.*]] = fneg reassoc float [[E]]
623 ; CHECK-NEXT: ret float [[F]]
625 %d = fmul reassoc float %z, 4.000000e+01
626 %c = fneg reassoc float %d
627 %e = fmul reassoc float %a, %c
628 %f = fneg reassoc float %e
632 ; With sub reassociation, constant folding can eliminate the 12 and -12 constants.
633 define float @test19(float %A, float %B) {
634 ; CHECK-LABEL: @test19(
635 ; CHECK-NEXT: [[Z:%.*]] = fsub fast float [[A:%.*]], [[B:%.*]]
636 ; CHECK-NEXT: ret float [[Z]]
638 %X = fadd fast float -1.200000e+01, %A
639 %Y = fsub fast float %X, %B
640 %Z = fadd fast float %Y, 1.200000e+01
644 define float @test19_reassoc(float %A, float %B) {
645 ; CHECK-LABEL: @test19_reassoc(
646 ; CHECK-NEXT: [[X:%.*]] = fadd reassoc float [[A:%.*]], -1.200000e+01
647 ; CHECK-NEXT: [[Y:%.*]] = fsub reassoc float [[X]], [[B:%.*]]
648 ; CHECK-NEXT: [[Z:%.*]] = fadd reassoc float [[Y]], 1.200000e+01
649 ; CHECK-NEXT: ret float [[Z]]
651 %X = fadd reassoc float -1.200000e+01, %A
652 %Y = fsub reassoc float %X, %B
653 %Z = fadd reassoc float %Y, 1.200000e+01
657 ; With sub reassociation, constant folding can eliminate the uses of %a.
658 define float @test20(float %a, float %b, float %c) nounwind {
659 ; FIXME: Should be able to generate the below, which may expose more
660 ; opportunites for FAdd reassociation.
661 ; %sum = fadd fast float %c, %b
662 ; %t7 = fsub fast float 0, %sum
663 ; CHECK-LABEL: @test20(
664 ; CHECK-NEXT: [[B_NEG:%.*]] = fsub fast float -0.000000e+00, [[B:%.*]]
665 ; CHECK-NEXT: [[T7:%.*]] = fsub fast float [[B_NEG]], [[C:%.*]]
666 ; CHECK-NEXT: ret float [[T7]]
668 %t3 = fsub fast float %a, %b
669 %t5 = fsub fast float %t3, %c
670 %t7 = fsub fast float %t5, %a
674 define float @test20_reassoc(float %a, float %b, float %c) nounwind {
675 ; CHECK-LABEL: @test20_reassoc(
676 ; CHECK-NEXT: [[T3:%.*]] = fsub reassoc float [[A:%.*]], [[B:%.*]]
677 ; CHECK-NEXT: [[T5:%.*]] = fsub reassoc float [[T3]], [[C:%.*]]
678 ; CHECK-NEXT: [[T7:%.*]] = fsub reassoc float [[T5]], [[A]]
679 ; CHECK-NEXT: ret float [[T7]]
681 %t3 = fsub reassoc float %a, %b
682 %t5 = fsub reassoc float %t3, %c
683 %t7 = fsub reassoc float %t5, %a