1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2;
3; Test cases in this file are intended to be run with both reassociate and
4; instcombine passes enabled.
5;
6; Test numbering remains continuous across:
7; - InstCombine/fast-basictest.ll
8; - PhaseOrdering/fast-basictest.ll
9; - PhaseOrdering/fast-reassociate-gvn.ll
10; - Reassociate/fast-basictest.ll
11;
12; RUN: opt < %s -reassociate -instcombine -S | FileCheck %s --check-prefixes=CHECK,REASSOC_AND_IC --allow-unused-prefixes
13; RUN: opt < %s -O2 -S | FileCheck %s --check-prefixes=CHECK,O2 --allow-unused-prefixes
14
15; test2 ... test18 - both reassociate and instcombine passes
16; are required to perform a transform
17
18; ((a + (-3)) + b) + 3 -> a + b
19
20define float @test2(float %reg109, float %reg1111) {
21; CHECK-LABEL: @test2(
22; CHECK-NEXT:    [[REG117:%.*]] = fadd fast float [[REG109:%.*]], [[REG1111:%.*]]
23; CHECK-NEXT:    ret float [[REG117]]
24;
25  %reg115 = fadd fast float %reg109, -3.000000e+01
26  %reg116 = fadd fast float %reg115, %reg1111
27  %reg117 = fadd fast float %reg116, 3.000000e+01
28  ret float %reg117
29}
30
31; Verify that fold is not done without 'fast'
32define float @test2_no_FMF(float %reg109, float %reg1111) {
33; CHECK-LABEL: @test2_no_FMF(
34; CHECK-NEXT:    [[REG115:%.*]] = fadd float [[REG109:%.*]], -3.000000e+01
35; CHECK-NEXT:    [[REG116:%.*]] = fadd float [[REG115]], [[REG1111:%.*]]
36; CHECK-NEXT:    [[REG117:%.*]] = fadd float [[REG116]], 3.000000e+01
37; CHECK-NEXT:    ret float [[REG117]]
38;
39  %reg115 = fadd float %reg109, -3.000000e+01
40  %reg116 = fadd float %reg115, %reg1111
41  %reg117 = fadd float %reg116, 3.000000e+01
42  ret float %reg117
43}
44
45define float @test2_reassoc(float %reg109, float %reg1111) {
46; CHECK-LABEL: @test2_reassoc(
47; CHECK-NEXT:    [[REG115:%.*]] = fadd reassoc float [[REG109:%.*]], -3.000000e+01
48; CHECK-NEXT:    [[REG116:%.*]] = fadd reassoc float [[REG115]], [[REG1111:%.*]]
49; CHECK-NEXT:    [[REG117:%.*]] = fadd reassoc float [[REG116]], 3.000000e+01
50; CHECK-NEXT:    ret float [[REG117]]
51;
52  %reg115 = fadd reassoc float %reg109, -3.000000e+01
53  %reg116 = fadd reassoc float %reg115, %reg1111
54  %reg117 = fadd reassoc float %reg116, 3.000000e+01
55  ret float %reg117
56}
57
58; (x1 * 47) + (x2 * -47) => (x1 - x2) * 47
59
60define float @test13(float %X1, float %X2) {
61; CHECK-LABEL: @test13(
62; CHECK-NEXT:    [[TMP1:%.*]] = fsub fast float [[X1:%.*]], [[X2:%.*]]
63; CHECK-NEXT:    [[TMP2:%.*]] = fmul fast float [[TMP1]], 4.700000e+01
64; CHECK-NEXT:    ret float [[TMP2]]
65;
66  %B = fmul fast float %X1, 47.   ; X1*47
67  %C = fmul fast float %X2, -47.  ; X2*-47
68  %D = fadd fast float %B, %C     ; X1*47 + X2*-47 -> 47*(X1-X2)
69  ret float %D
70}
71
72; Check again with 'reassoc' and 'nsz' ('nsz' not technically required).
73define float @test13_reassoc_nsz(float %X1, float %X2) {
74; CHECK-LABEL: @test13_reassoc_nsz(
75; CHECK-NEXT:    [[TMP1:%.*]] = fsub reassoc nsz float [[X1:%.*]], [[X2:%.*]]
76; CHECK-NEXT:    [[TMP2:%.*]] = fmul reassoc nsz float [[TMP1]], 4.700000e+01
77; CHECK-NEXT:    ret float [[TMP2]]
78;
79  %B = fmul reassoc nsz float %X1, 47.   ; X1*47
80  %C = fmul reassoc nsz float %X2, -47.  ; X2*-47
81  %D = fadd reassoc nsz float %B, %C     ; X1*47 + X2*-47 -> 47*(X1-X2)
82  ret float %D
83}
84
85; TODO: This doesn't require 'nsz'.  It should fold to ((x1 - x2) * 47.0)
86define float @test13_reassoc(float %X1, float %X2) {
87; CHECK-LABEL: @test13_reassoc(
88; CHECK-NEXT:    [[B:%.*]] = fmul reassoc float [[X1:%.*]], 4.700000e+01
89; CHECK-NEXT:    [[C:%.*]] = fmul reassoc float [[X2:%.*]], 4.700000e+01
90; CHECK-NEXT:    [[TMP1:%.*]] = fsub reassoc float [[B]], [[C]]
91; CHECK-NEXT:    ret float [[TMP1]]
92;
93  %B = fmul reassoc float %X1, 47.   ; X1*47
94  %C = fmul reassoc float %X2, -47.  ; X2*-47
95  %D = fadd reassoc float %B, %C     ; X1*47 + X2*-47 -> 47*(X1-X2)
96  ret float %D
97}
98
99; (b+(a+1234))+-a -> b+1234
100
101define float @test15(float %b, float %a) {
102; CHECK-LABEL: @test15(
103; CHECK-NEXT:    [[TMP1:%.*]] = fadd fast float [[B:%.*]], 1.234000e+03
104; CHECK-NEXT:    ret float [[TMP1]]
105;
106  %1 = fadd fast float %a, 1234.0
107  %2 = fadd fast float %b, %1
108  %3 = fsub fast float 0.0, %a
109  %4 = fadd fast float %2, %3
110  ret float %4
111}
112
113define float @test15_unary_fneg(float %b, float %a) {
114; CHECK-LABEL: @test15_unary_fneg(
115; CHECK-NEXT:    [[TMP1:%.*]] = fadd fast float [[B:%.*]], 1.234000e+03
116; CHECK-NEXT:    ret float [[TMP1]]
117;
118  %1 = fadd fast float %a, 1234.0
119  %2 = fadd fast float %b, %1
120  %3 = fneg fast float %a
121  %4 = fadd fast float %2, %3
122  ret float %4
123}
124
125; TODO: check if it is possible to perform the optimization without 'fast'
126; with 'reassoc' and 'nsz' only.
127define float @test15_reassoc_nsz(float %b, float %a) {
128; CHECK-LABEL: @test15_reassoc_nsz(
129; CHECK-NEXT:    [[TMP1:%.*]] = fadd reassoc nsz float [[A:%.*]], 1.234000e+03
130; CHECK-NEXT:    [[TMP2:%.*]] = fadd reassoc nsz float [[TMP1]], [[B:%.*]]
131; CHECK-NEXT:    [[TMP3:%.*]] = fsub reassoc nsz float [[TMP2]], [[A]]
132; CHECK-NEXT:    ret float [[TMP3]]
133;
134  %1 = fadd reassoc nsz float %a, 1234.0
135  %2 = fadd reassoc nsz float %b, %1
136  %3 = fsub reassoc nsz float 0.0, %a
137  %4 = fadd reassoc nsz float %2, %3
138  ret float %4
139}
140
141define float @test15_reassoc(float %b, float %a) {
142; CHECK-LABEL: @test15_reassoc(
143; CHECK-NEXT:    [[TMP1:%.*]] = fadd reassoc float [[A:%.*]], 1.234000e+03
144; CHECK-NEXT:    [[TMP2:%.*]] = fadd reassoc float [[TMP1]], [[B:%.*]]
145; CHECK-NEXT:    [[TMP3:%.*]] = fsub reassoc float 0.000000e+00, [[A]]
146; CHECK-NEXT:    [[TMP4:%.*]] = fadd reassoc float [[TMP3]], [[TMP2]]
147; CHECK-NEXT:    ret float [[TMP4]]
148;
149  %1 = fadd reassoc float %a, 1234.0
150  %2 = fadd reassoc float %b, %1
151  %3 = fsub reassoc float 0.0, %a
152  %4 = fadd reassoc float %2, %3
153  ret float %4
154}
155
156; Test that we can turn things like X*-(Y*Z) -> X*-1*Y*Z.
157
158define float @test16(float %a, float %b, float %z) {
159; REASSOC_AND_IC-LABEL: @test16(
160; REASSOC_AND_IC-NEXT:    [[C:%.*]] = fmul fast float [[A:%.*]], 1.234500e+04
161; REASSOC_AND_IC-NEXT:    [[E:%.*]] = fmul fast float [[C]], [[B:%.*]]
162; REASSOC_AND_IC-NEXT:    [[F:%.*]] = fmul fast float [[E]], [[Z:%.*]]
163; REASSOC_AND_IC-NEXT:    ret float [[F]]
164;
165; O2-LABEL: @test16(
166; O2-NEXT:    [[D:%.*]] = fmul fast float [[A:%.*]], 1.234500e+04
167; O2-NEXT:    [[E:%.*]] = fmul fast float [[D]], [[B:%.*]]
168; O2-NEXT:    [[G:%.*]] = fmul fast float [[E]], [[Z:%.*]]
169; O2-NEXT:    ret float [[G]]
170;
171  %c = fsub fast float 0.000000e+00, %z
172  %d = fmul fast float %a, %b
173  %e = fmul fast float %c, %d
174  %f = fmul fast float %e, 1.234500e+04
175  %g = fsub fast float 0.000000e+00, %f
176  ret float %g
177}
178
179define float @test16_unary_fneg(float %a, float %b, float %z) {
180; REASSOC_AND_IC-LABEL: @test16_unary_fneg(
181; REASSOC_AND_IC-NEXT:    [[E:%.*]] = fmul fast float [[A:%.*]], 1.234500e+04
182; REASSOC_AND_IC-NEXT:    [[F:%.*]] = fmul fast float [[E]], [[B:%.*]]
183; REASSOC_AND_IC-NEXT:    [[G:%.*]] = fmul fast float [[F]], [[Z:%.*]]
184; REASSOC_AND_IC-NEXT:    ret float [[G]]
185;
186; O2-LABEL: @test16_unary_fneg(
187; O2-NEXT:    [[D:%.*]] = fmul fast float [[A:%.*]], 1.234500e+04
188; O2-NEXT:    [[E:%.*]] = fmul fast float [[D]], [[B:%.*]]
189; O2-NEXT:    [[G:%.*]] = fmul fast float [[E]], [[Z:%.*]]
190; O2-NEXT:    ret float [[G]]
191;
192  %c = fneg fast float %z
193  %d = fmul fast float %a, %b
194  %e = fmul fast float %c, %d
195  %f = fmul fast float %e, 1.234500e+04
196  %g = fneg fast float %f
197  ret float %g
198}
199
200; TODO: check if it is possible to perform the optimization without 'fast'
201; with 'reassoc' and 'nsz' only.
202define float @test16_reassoc_nsz(float %a, float %b, float %z) {
203; CHECK-LABEL: @test16_reassoc_nsz(
204; CHECK-NEXT:    [[C:%.*]] = fneg reassoc nsz float [[Z:%.*]]
205; CHECK-NEXT:    [[D:%.*]] = fmul reassoc nsz float [[A:%.*]], [[B:%.*]]
206; CHECK-NEXT:    [[E:%.*]] = fmul reassoc nsz float [[D]], [[C]]
207; CHECK-NEXT:    [[G:%.*]] = fmul reassoc nsz float [[E]], -1.234500e+04
208; CHECK-NEXT:    ret float [[G]]
209;
210  %c = fsub reassoc nsz float 0.000000e+00, %z
211  %d = fmul reassoc nsz float %a, %b
212  %e = fmul reassoc nsz float %c, %d
213  %f = fmul reassoc nsz float %e, 1.234500e+04
214  %g = fsub reassoc nsz float 0.000000e+00, %f
215  ret float %g
216}
217
218define float @test16_reassoc(float %a, float %b, float %z) {
219; CHECK-LABEL: @test16_reassoc(
220; CHECK-NEXT:    [[C:%.*]] = fsub reassoc float 0.000000e+00, [[Z:%.*]]
221; CHECK-NEXT:    [[D:%.*]] = fmul reassoc float [[A:%.*]], [[B:%.*]]
222; CHECK-NEXT:    [[E:%.*]] = fmul reassoc float [[D]], [[C]]
223; CHECK-NEXT:    [[F:%.*]] = fmul reassoc float [[E]], 1.234500e+04
224; CHECK-NEXT:    [[G:%.*]] = fsub reassoc float 0.000000e+00, [[F]]
225; CHECK-NEXT:    ret float [[G]]
226;
227  %c = fsub reassoc float 0.000000e+00, %z
228  %d = fmul reassoc float %a, %b
229  %e = fmul reassoc float %c, %d
230  %f = fmul reassoc float %e, 1.234500e+04
231  %g = fsub reassoc float 0.000000e+00, %f
232  ret float %g
233}
234
235; With sub reassociation, constant folding can eliminate the 12 and -12 constants.
236
237define float @test18(float %A, float %B) {
238; CHECK-LABEL: @test18(
239; CHECK-NEXT:    [[Z:%.*]] = fsub fast float [[A:%.*]], [[B:%.*]]
240; CHECK-NEXT:    ret float [[Z]]
241;
242  %X = fadd fast float -1.200000e+01, %A
243  %Y = fsub fast float %X, %B
244  %Z = fadd fast float %Y, 1.200000e+01
245  ret float %Z
246}
247
248define float @test18_reassoc(float %A, float %B) {
249; CHECK-LABEL: @test18_reassoc(
250; CHECK-NEXT:    [[X:%.*]] = fadd reassoc float [[A:%.*]], -1.200000e+01
251; CHECK-NEXT:    [[Y:%.*]] = fsub reassoc float [[X]], [[B:%.*]]
252; CHECK-NEXT:    [[Z:%.*]] = fadd reassoc float [[Y]], 1.200000e+01
253; CHECK-NEXT:    ret float [[Z]]
254;
255  %X = fadd reassoc float -1.200000e+01, %A
256  %Y = fsub reassoc float %X, %B
257  %Z = fadd reassoc float %Y, 1.200000e+01
258  ret float %Z
259}
260
261; test18 - check that the bug described in the revision does not appear:
262; https://reviews.llvm.org/D72521
263
264; With sub reassociation, constant folding can eliminate the uses of %a.
265
266define float @test19(float %a, float %b, float %c) nounwind  {
267; REASSOC_AND_IC-LABEL: @test19(
268; REASSOC_AND_IC-NEXT:    [[TMP1:%.*]] = fadd fast float [[B:%.*]], [[C:%.*]]
269; REASSOC_AND_IC-NEXT:    [[T7:%.*]] = fneg fast float [[TMP1]]
270; REASSOC_AND_IC-NEXT:    ret float [[T7]]
271;
272; O2-LABEL: @test19(
273; O2-NEXT:    [[TMP1:%.*]] = fadd fast float [[C:%.*]], [[B:%.*]]
274; O2-NEXT:    [[T7:%.*]] = fneg fast float [[TMP1]]
275; O2-NEXT:    ret float [[T7]]
276;
277  %t3 = fsub fast float %a, %b
278  %t5 = fsub fast float %t3, %c
279  %t7 = fsub fast float %t5, %a
280  ret float %t7
281}
282
283define float @test19_reassoc_nsz(float %a, float %b, float %c) nounwind  {
284; CHECK-LABEL: @test19_reassoc_nsz(
285; CHECK-NEXT:    [[TMP1:%.*]] = fadd reassoc nsz float [[B:%.*]], [[C:%.*]]
286; CHECK-NEXT:    [[T7:%.*]] = fneg reassoc nsz float [[TMP1]]
287; CHECK-NEXT:    ret float [[T7]]
288;
289  %t3 = fsub reassoc nsz float %a, %b
290  %t5 = fsub reassoc nsz float %t3, %c
291  %t7 = fsub reassoc nsz float %t5, %a
292  ret float %t7
293}
294
295; Verify the fold is not done with only 'reassoc' ('nsz' is required).
296define float @test19_reassoc(float %a, float %b, float %c) nounwind  {
297; CHECK-LABEL: @test19_reassoc(
298; CHECK-NEXT:    [[T3:%.*]] = fsub reassoc float [[A:%.*]], [[B:%.*]]
299; CHECK-NEXT:    [[T5:%.*]] = fsub reassoc float [[T3]], [[C:%.*]]
300; CHECK-NEXT:    [[T7:%.*]] = fsub reassoc float [[T5]], [[A]]
301; CHECK-NEXT:    ret float [[T7]]
302;
303  %t3 = fsub reassoc float %a, %b
304  %t5 = fsub reassoc float %t3, %c
305  %t7 = fsub reassoc float %t5, %a
306  ret float %t7
307}
308