1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -reassociate -gvn -instcombine -S | FileCheck %s
3
4; With reassociation, constant folding can eliminate the 12 and -12 constants.
5define float @test1(float %arg) {
6; CHECK-LABEL: @test1(
7; CHECK-NEXT:    [[ARG_NEG:%.*]] = fsub fast float -0.000000e+00, [[ARG:%.*]]
8; CHECK-NEXT:    ret float [[ARG_NEG]]
9;
10  %t1 = fsub fast float -1.200000e+01, %arg
11  %t2 = fadd fast float %t1, 1.200000e+01
12  ret float %t2
13}
14
15; Check again using the minimal subset of FMF.
16; Both 'reassoc' and 'nsz' are required.
17define float @test1_minimal(float %arg) {
18; CHECK-LABEL: @test1_minimal(
19; CHECK-NEXT:    [[ARG_NEG:%.*]] = fsub reassoc nsz float -0.000000e+00, [[ARG:%.*]]
20; CHECK-NEXT:    ret float [[ARG_NEG]]
21;
22  %t1 = fsub reassoc nsz float -1.200000e+01, %arg
23  %t2 = fadd reassoc nsz float %t1, 1.200000e+01
24  ret float %t2
25}
26
27; Verify the fold is not done with only 'reassoc' ('nsz' is required).
28define 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]]
33;
34  %t1 = fsub reassoc float -1.200000e+01, %arg
35  %t2 = fadd reassoc float %t1, 1.200000e+01
36  ret float %t2
37}
38
39define 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]]
45;
46  %reg115 = fadd float %reg109, -3.000000e+01
47  %reg116 = fadd float %reg115, %reg1111
48  %reg117 = fadd float %reg116, 3.000000e+01
49  ret float %reg117
50}
51
52define 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]]
56;
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
60  ret float %reg117
61}
62
63define 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]]
69;
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
73  ret float %reg117
74}
75
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
81
82define 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
92;
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
100  ; e = (a+b)+c;
101  store float %t2, float* @fe
102  ; f = (a+c)+b
103  store float %t4, float* @ff
104  ret void
105}
106
107define 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
117;
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
125  ; e = c+(a+b)
126  store float %t2, float* @fe
127  ; f = (c+a)+b
128  store float %t4, float* @ff
129  ret void
130}
131
132define 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
142;
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
150  ; e = c+(b+a)
151  store float %t2, float* @fe
152  ; f = (c+a)+b
153  store float %t4, float* @ff
154  ret void
155}
156
157define 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]]
163;
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
169  ret float %r
170}
171
172define 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]]
180;
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
186  ret float %r
187}
188
189; (-X)*Y + Z -> Z-X*Y
190
191define 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]]
196;
197  %A = fsub fast float 0.0, %X
198  %B = fmul fast float %A, %Y
199  %C = fadd fast float %B, %Z
200  ret float %C
201}
202
203define float @test8_reassoc(float %X, float %Y, float %Z) {
204; CHECK-LABEL: @test8_reassoc(
205; CHECK-NEXT:    [[A:%.*]] = fsub reassoc float 0.000000e+00, [[X:%.*]]
206; CHECK-NEXT:    [[B:%.*]] = fmul reassoc float [[A]], [[Y:%.*]]
207; CHECK-NEXT:    [[C:%.*]] = fadd reassoc float [[B]], [[Z:%.*]]
208; CHECK-NEXT:    ret float [[C]]
209;
210  %A = fsub reassoc float 0.0, %X
211  %B = fmul reassoc float %A, %Y
212  %C = fadd reassoc float %B, %Z
213  ret float %C
214}
215
216define float @test9(float %X) {
217; CHECK-LABEL: @test9(
218; CHECK-NEXT:    [[FACTOR:%.*]] = fmul fast float [[X:%.*]], 9.400000e+01
219; CHECK-NEXT:    ret float [[FACTOR]]
220;
221  %Y = fmul fast float %X, 4.700000e+01
222  %Z = fadd fast float %Y, %Y
223  ret float %Z
224}
225
226; Check again with 'reassoc' and 'nsz' ('nsz' not technically required).
227define float @test9_reassoc_nsz(float %X) {
228; CHECK-LABEL: @test9_reassoc_nsz(
229; CHECK-NEXT:    [[FACTOR:%.*]] = fmul reassoc nsz float [[X:%.*]], 9.400000e+01
230; CHECK-NEXT:    ret float [[FACTOR]]
231;
232  %Y = fmul reassoc nsz float %X, 4.700000e+01
233  %Z = fadd reassoc nsz float %Y, %Y
234  ret float %Z
235}
236
237; TODO: This doesn't require 'nsz'.  It should fold to X * 94.0
238define float @test9_reassoc(float %X) {
239; CHECK-LABEL: @test9_reassoc(
240; CHECK-NEXT:    [[Y:%.*]] = fmul reassoc float [[X:%.*]], 4.700000e+01
241; CHECK-NEXT:    [[Z:%.*]] = fadd reassoc float [[Y]], [[Y]]
242; CHECK-NEXT:    ret float [[Z]]
243;
244  %Y = fmul reassoc float %X, 4.700000e+01
245  %Z = fadd reassoc float %Y, %Y
246  ret float %Z
247}
248
249; Side note: (x + x + x) and (3*x) each have only a single rounding.  So
250; transforming x+x+x to 3*x is always safe, even without any FMF.
251; To avoid that special-case, we have the addition of 'x' four times, here.
252define float @test10(float %X) {
253; CHECK-LABEL: @test10(
254; CHECK-NEXT:    [[FACTOR:%.*]] = fmul fast float [[X:%.*]], 4.000000e+00
255; CHECK-NEXT:    ret float [[FACTOR]]
256;
257  %Y = fadd fast float %X ,%X
258  %Z = fadd fast float %Y, %X
259  %W = fadd fast float %Z, %X
260  ret float %W
261}
262
263; Check again with 'reassoc' and 'nsz' ('nsz' not technically required).
264define float @test10_reassoc_nsz(float %X) {
265; CHECK-LABEL: @test10_reassoc_nsz(
266; CHECK-NEXT:    [[FACTOR:%.*]] = fmul reassoc nsz float [[X:%.*]], 4.000000e+00
267; CHECK-NEXT:    ret float [[FACTOR]]
268;
269  %Y = fadd reassoc nsz float %X ,%X
270  %Z = fadd reassoc nsz float %Y, %X
271  %W = fadd reassoc nsz float %Z, %X
272  ret float %W
273}
274
275; TODO: This doesn't require 'nsz'.  It should fold to 4 * x
276define float @test10_reassoc(float %X) {
277; CHECK-LABEL: @test10_reassoc(
278; CHECK-NEXT:    [[Y:%.*]] = fadd reassoc float [[X:%.*]], [[X]]
279; CHECK-NEXT:    [[Z:%.*]] = fadd reassoc float [[Y]], [[X]]
280; CHECK-NEXT:    [[W:%.*]] = fadd reassoc float [[Z]], [[X]]
281; CHECK-NEXT:    ret float [[W]]
282;
283  %Y = fadd reassoc float %X ,%X
284  %Z = fadd reassoc float %Y, %X
285  %W = fadd reassoc float %Z, %X
286  ret float %W
287}
288
289define float @test11(float %W) {
290; CHECK-LABEL: @test11(
291; CHECK-NEXT:    [[FACTOR:%.*]] = fmul fast float [[W:%.*]], 3.810000e+02
292; CHECK-NEXT:    ret float [[FACTOR]]
293;
294  %X = fmul fast float %W, 127.0
295  %Y = fadd fast float %X ,%X
296  %Z = fadd fast float %Y, %X
297  ret float %Z
298}
299
300; Check again using the minimal subset of FMF.
301; Check again with 'reassoc' and 'nsz' ('nsz' not technically required).
302define float @test11_reassoc_nsz(float %W) {
303; CHECK-LABEL: @test11_reassoc_nsz(
304; CHECK-NEXT:    [[FACTOR:%.*]] = fmul reassoc nsz float [[W:%.*]], 3.810000e+02
305; CHECK-NEXT:    ret float [[FACTOR]]
306;
307  %X = fmul reassoc nsz float %W, 127.0
308  %Y = fadd reassoc nsz float %X ,%X
309  %Z = fadd reassoc nsz float %Y, %X
310  ret float %Z
311}
312
313; TODO: This doesn't require 'nsz'.  It should fold to W*381.0.
314define float @test11_reassoc(float %W) {
315; CHECK-LABEL: @test11_reassoc(
316; CHECK-NEXT:    [[X:%.*]] = fmul reassoc float [[W:%.*]], 1.270000e+02
317; CHECK-NEXT:    [[Y:%.*]] = fadd reassoc float [[X]], [[X]]
318; CHECK-NEXT:    [[Z:%.*]] = fadd reassoc float [[X]], [[Y]]
319; CHECK-NEXT:    ret float [[Z]]
320;
321  %X = fmul reassoc float %W, 127.0
322  %Y = fadd reassoc float %X ,%X
323  %Z = fadd reassoc float %Y, %X
324  ret float %Z
325}
326
327define float @test12(float %X) {
328; CHECK-LABEL: @test12(
329; CHECK-NEXT:    [[FACTOR:%.*]] = fmul fast float [[X:%.*]], -3.000000e+00
330; CHECK-NEXT:    [[Z:%.*]] = fadd fast float [[FACTOR]], 6.000000e+00
331; CHECK-NEXT:    ret float [[Z]]
332;
333  %A = fsub fast float 1.000000e+00, %X
334  %B = fsub fast float 2.000000e+00, %X
335  %C = fsub fast float 3.000000e+00, %X
336  %Y = fadd fast float %A ,%B
337  %Z = fadd fast float %Y, %C
338  ret float %Z
339}
340
341; Check again with 'reassoc' and 'nsz' ('nsz' not technically required).
342define float @test12_reassoc_nsz(float %X) {
343; CHECK-LABEL: @test12_reassoc_nsz(
344; CHECK-NEXT:    [[FACTOR:%.*]] = fmul reassoc nsz float [[X:%.*]], 3.000000e+00
345; CHECK-NEXT:    [[Z:%.*]] = fsub reassoc nsz float 6.000000e+00, [[FACTOR]]
346; CHECK-NEXT:    ret float [[Z]]
347;
348  %A = fsub reassoc nsz float 1.000000e+00, %X
349  %B = fsub reassoc nsz float 2.000000e+00, %X
350  %C = fsub reassoc nsz float 3.000000e+00, %X
351  %Y = fadd reassoc nsz float %A ,%B
352  %Z = fadd reassoc nsz float %Y, %C
353  ret float %Z
354}
355
356; TODO: This doesn't require 'nsz'.  It should fold to (6.0 - 3.0*x)
357define float @test12_reassoc(float %X) {
358; CHECK-LABEL: @test12_reassoc(
359; CHECK-NEXT:    [[A:%.*]] = fsub reassoc float 1.000000e+00, [[X:%.*]]
360; CHECK-NEXT:    [[B:%.*]] = fsub reassoc float 2.000000e+00, [[X]]
361; CHECK-NEXT:    [[C:%.*]] = fsub reassoc float 3.000000e+00, [[X]]
362; CHECK-NEXT:    [[Y:%.*]] = fadd reassoc float [[A]], [[B]]
363; CHECK-NEXT:    [[Z:%.*]] = fadd reassoc float [[C]], [[Y]]
364; CHECK-NEXT:    ret float [[Z]]
365;
366  %A = fsub reassoc float 1.000000e+00, %X
367  %B = fsub reassoc float 2.000000e+00, %X
368  %C = fsub reassoc float 3.000000e+00, %X
369  %Y = fadd reassoc float %A ,%B
370  %Z = fadd reassoc float %Y, %C
371  ret float %Z
372}
373
374define float @test13(float %X1, float %X2, float %X3) {
375; CHECK-LABEL: @test13(
376; CHECK-NEXT:    [[REASS_ADD:%.*]] = fsub fast float [[X3:%.*]], [[X2:%.*]]
377; CHECK-NEXT:    [[REASS_MUL:%.*]] = fmul fast float [[REASS_ADD]], [[X1:%.*]]
378; CHECK-NEXT:    ret float [[REASS_MUL]]
379;
380  %A = fsub fast float 0.000000e+00, %X1
381  %B = fmul fast float %A, %X2   ; -X1*X2
382  %C = fmul fast float %X1, %X3  ; X1*X3
383  %D = fadd fast float %B, %C    ; -X1*X2 + X1*X3 -> X1*(X3-X2)
384  ret float %D
385}
386
387define float @test13_reassoc(float %X1, float %X2, float %X3) {
388; CHECK-LABEL: @test13_reassoc(
389; CHECK-NEXT:    [[A:%.*]] = fsub reassoc float 0.000000e+00, [[X1:%.*]]
390; CHECK-NEXT:    [[B:%.*]] = fmul reassoc float [[A]], [[X2:%.*]]
391; CHECK-NEXT:    [[C:%.*]] = fmul reassoc float [[X1]], [[X3:%.*]]
392; CHECK-NEXT:    [[D:%.*]] = fadd reassoc float [[B]], [[C]]
393; CHECK-NEXT:    ret float [[D]]
394;
395  %A = fsub reassoc float 0.000000e+00, %X1
396  %B = fmul reassoc float %A, %X2   ; -X1*X2
397  %C = fmul reassoc float %X1, %X3  ; X1*X3
398  %D = fadd reassoc float %B, %C    ; -X1*X2 + X1*X3 -> X1*(X3-X2)
399  ret float %D
400}
401
402define float @test14(float %X1, float %X2) {
403; CHECK-LABEL: @test14(
404; CHECK-NEXT:    [[TMP1:%.*]] = fsub fast float [[X1:%.*]], [[X2:%.*]]
405; CHECK-NEXT:    [[TMP2:%.*]] = fmul fast float [[TMP1]], 4.700000e+01
406; CHECK-NEXT:    ret float [[TMP2]]
407;
408  %B = fmul fast float %X1, 47.   ; X1*47
409  %C = fmul fast float %X2, -47.  ; X2*-47
410  %D = fadd fast float %B, %C    ; X1*47 + X2*-47 -> 47*(X1-X2)
411  ret float %D
412}
413
414; (x1 * 47) + (x2 * -47) => (x1 - x2) * 47
415; Check again with 'reassoc' and 'nsz' ('nsz' not technically required).
416define float @test14_reassoc_nsz(float %X1, float %X2) {
417; CHECK-LABEL: @test14_reassoc_nsz(
418; CHECK-NEXT:    [[TMP1:%.*]] = fsub reassoc nsz float [[X1:%.*]], [[X2:%.*]]
419; CHECK-NEXT:    [[TMP2:%.*]] = fmul reassoc nsz float [[TMP1]], 4.700000e+01
420; CHECK-NEXT:    ret float [[TMP2]]
421;
422  %B = fmul reassoc nsz float %X1, 47.   ; X1*47
423  %C = fmul reassoc nsz float %X2, -47.  ; X2*-47
424  %D = fadd reassoc nsz float %B, %C    ; X1*47 + X2*-47 -> 47*(X1-X2)
425  ret float %D
426}
427
428; TODO: This doesn't require 'nsz'.  It should fold to ((x1 - x2) * 47.0)
429define float @test14_reassoc(float %X1, float %X2) {
430; CHECK-LABEL: @test14_reassoc(
431; CHECK-NEXT:    [[B:%.*]] = fmul reassoc float [[X1:%.*]], 4.700000e+01
432; CHECK-NEXT:    [[C:%.*]] = fmul reassoc float [[X2:%.*]], 4.700000e+01
433; CHECK-NEXT:    [[D1:%.*]] = fsub reassoc float [[B]], [[C]]
434; CHECK-NEXT:    ret float [[D1]]
435;
436  %B = fmul reassoc float %X1, 47.   ; X1*47
437  %C = fmul reassoc float %X2, -47.  ; X2*-47
438  %D = fadd reassoc float %B, %C    ; X1*47 + X2*-47 -> 47*(X1-X2)
439  ret float %D
440}
441
442define float @test15(float %arg) {
443; CHECK-LABEL: @test15(
444; CHECK-NEXT:    [[T2:%.*]] = fmul fast float [[ARG:%.*]], 1.440000e+02
445; CHECK-NEXT:    ret float [[T2]]
446;
447  %t1 = fmul fast float 1.200000e+01, %arg
448  %t2 = fmul fast float %t1, 1.200000e+01
449  ret float %t2
450}
451
452define float @test15_reassoc(float %arg) {
453; CHECK-LABEL: @test15_reassoc(
454; CHECK-NEXT:    [[T2:%.*]] = fmul reassoc float [[ARG:%.*]], 1.440000e+02
455; CHECK-NEXT:    ret float [[T2]]
456;
457  %t1 = fmul reassoc float 1.200000e+01, %arg
458  %t2 = fmul reassoc float %t1, 1.200000e+01
459  ret float %t2
460}
461
462; (b+(a+1234))+-a -> b+1234
463define float @test16(float %b, float %a) {
464; CHECK-LABEL: @test16(
465; CHECK-NEXT:    [[TMP1:%.*]] = fadd fast float [[B:%.*]], 1.234000e+03
466; CHECK-NEXT:    ret float [[TMP1]]
467;
468  %1 = fadd fast float %a, 1234.0
469  %2 = fadd fast float %b, %1
470  %3 = fsub fast float 0.0, %a
471  %4 = fadd fast float %2, %3
472  ret float %4
473}
474
475define float @test16_reassoc(float %b, float %a) {
476; CHECK-LABEL: @test16_reassoc(
477; CHECK-NEXT:    [[TMP1:%.*]] = fadd reassoc float [[A:%.*]], 1.234000e+03
478; CHECK-NEXT:    [[TMP2:%.*]] = fadd reassoc float [[TMP1]], [[B:%.*]]
479; CHECK-NEXT:    [[TMP3:%.*]] = fsub reassoc float 0.000000e+00, [[A]]
480; CHECK-NEXT:    [[TMP4:%.*]] = fadd reassoc float [[TMP3]], [[TMP2]]
481; CHECK-NEXT:    ret float [[TMP4]]
482;
483  %1 = fadd reassoc float %a, 1234.0
484  %2 = fadd reassoc float %b, %1
485  %3 = fsub reassoc float 0.0, %a
486  %4 = fadd reassoc float %2, %3
487  ret float %4
488}
489
490; Test that we can turn things like X*-(Y*Z) -> X*-1*Y*Z.
491
492define float @test17(float %a, float %b, float %z) {
493; CHECK-LABEL: @test17(
494; CHECK-NEXT:    [[E:%.*]] = fmul fast float [[A:%.*]], 1.234500e+04
495; CHECK-NEXT:    [[F:%.*]] = fmul fast float [[E]], [[B:%.*]]
496; CHECK-NEXT:    [[G:%.*]] = fmul fast float [[F]], [[Z:%.*]]
497; CHECK-NEXT:    ret float [[G]]
498;
499  %c = fsub fast float 0.000000e+00, %z
500  %d = fmul fast float %a, %b
501  %e = fmul fast float %c, %d
502  %f = fmul fast float %e, 1.234500e+04
503  %g = fsub fast float 0.000000e+00, %f
504  ret float %g
505}
506
507define float @test17_reassoc(float %a, float %b, float %z) {
508; CHECK-LABEL: @test17_reassoc(
509; CHECK-NEXT:    [[C:%.*]] = fsub reassoc float 0.000000e+00, [[Z:%.*]]
510; CHECK-NEXT:    [[D:%.*]] = fmul reassoc float [[A:%.*]], [[B:%.*]]
511; CHECK-NEXT:    [[E:%.*]] = fmul reassoc float [[D]], [[C]]
512; CHECK-NEXT:    [[F:%.*]] = fmul reassoc float [[E]], 1.234500e+04
513; CHECK-NEXT:    [[G:%.*]] = fsub reassoc float 0.000000e+00, [[F]]
514; CHECK-NEXT:    ret float [[G]]
515;
516  %c = fsub reassoc float 0.000000e+00, %z
517  %d = fmul reassoc float %a, %b
518  %e = fmul reassoc float %c, %d
519  %f = fmul reassoc float %e, 1.234500e+04
520  %g = fsub reassoc float 0.000000e+00, %f
521  ret float %g
522}
523
524define float @test18(float %a, float %b, float %z) {
525; CHECK-LABEL: @test18(
526; CHECK-NEXT:    [[E:%.*]] = fmul fast float [[A:%.*]], 4.000000e+01
527; CHECK-NEXT:    [[F:%.*]] = fmul fast float [[E]], [[Z:%.*]]
528; CHECK-NEXT:    ret float [[F]]
529;
530  %d = fmul fast float %z, 4.000000e+01
531  %c = fsub fast float 0.000000e+00, %d
532  %e = fmul fast float %a, %c
533  %f = fsub fast float 0.000000e+00, %e
534  ret float %f
535}
536
537define float @test18_reassoc(float %a, float %b, float %z) {
538; CHECK-LABEL: @test18_reassoc(
539; CHECK-NEXT:    [[D:%.*]] = fmul reassoc float [[Z:%.*]], 4.000000e+01
540; CHECK-NEXT:    [[C:%.*]] = fsub reassoc float 0.000000e+00, [[D]]
541; CHECK-NEXT:    [[E:%.*]] = fmul reassoc float [[C]], [[A:%.*]]
542; CHECK-NEXT:    [[F:%.*]] = fsub reassoc float 0.000000e+00, [[E]]
543; CHECK-NEXT:    ret float [[F]]
544;
545  %d = fmul reassoc float %z, 4.000000e+01
546  %c = fsub reassoc float 0.000000e+00, %d
547  %e = fmul reassoc float %a, %c
548  %f = fsub reassoc float 0.000000e+00, %e
549  ret float %f
550}
551
552; With sub reassociation, constant folding can eliminate the 12 and -12 constants.
553define float @test19(float %A, float %B) {
554; CHECK-LABEL: @test19(
555; CHECK-NEXT:    [[Z:%.*]] = fsub fast float [[A:%.*]], [[B:%.*]]
556; CHECK-NEXT:    ret float [[Z]]
557;
558  %X = fadd fast float -1.200000e+01, %A
559  %Y = fsub fast float %X, %B
560  %Z = fadd fast float %Y, 1.200000e+01
561  ret float %Z
562}
563
564define float @test19_reassoc(float %A, float %B) {
565; CHECK-LABEL: @test19_reassoc(
566; CHECK-NEXT:    [[X:%.*]] = fadd reassoc float [[A:%.*]], -1.200000e+01
567; CHECK-NEXT:    [[Y:%.*]] = fsub reassoc float [[X]], [[B:%.*]]
568; CHECK-NEXT:    [[Z:%.*]] = fadd reassoc float [[Y]], 1.200000e+01
569; CHECK-NEXT:    ret float [[Z]]
570;
571  %X = fadd reassoc float -1.200000e+01, %A
572  %Y = fsub reassoc float %X, %B
573  %Z = fadd reassoc float %Y, 1.200000e+01
574  ret float %Z
575}
576
577; With sub reassociation, constant folding can eliminate the uses of %a.
578define float @test20(float %a, float %b, float %c) nounwind  {
579; FIXME: Should be able to generate the below, which may expose more
580;        opportunites for FAdd reassociation.
581; %sum = fadd fast float %c, %b
582; %t7 = fsub fast float 0, %sum
583; CHECK-LABEL: @test20(
584; CHECK-NEXT:    [[B_NEG:%.*]] = fsub fast float -0.000000e+00, [[B:%.*]]
585; CHECK-NEXT:    [[T7:%.*]] = fsub fast float [[B_NEG]], [[C:%.*]]
586; CHECK-NEXT:    ret float [[T7]]
587;
588  %t3 = fsub fast float %a, %b
589  %t5 = fsub fast float %t3, %c
590  %t7 = fsub fast float %t5, %a
591  ret float %t7
592}
593
594define float @test20_reassoc(float %a, float %b, float %c) nounwind  {
595; CHECK-LABEL: @test20_reassoc(
596; CHECK-NEXT:    [[T3:%.*]] = fsub reassoc float [[A:%.*]], [[B:%.*]]
597; CHECK-NEXT:    [[T5:%.*]] = fsub reassoc float [[T3]], [[C:%.*]]
598; CHECK-NEXT:    [[T7:%.*]] = fsub reassoc float [[T5]], [[A]]
599; CHECK-NEXT:    ret float [[T7]]
600;
601  %t3 = fsub reassoc float %a, %b
602  %t5 = fsub reassoc float %t3, %c
603  %t7 = fsub reassoc float %t5, %a
604  ret float %t7
605}
606
607