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
125define float @test15_reassoc_nsz(float %b, float %a) {
126; CHECK-LABEL: @test15_reassoc_nsz(
127; CHECK-NEXT:    [[TMP1:%.*]] = fadd reassoc nsz float [[B:%.*]], 1.234000e+03
128; CHECK-NEXT:    ret float [[TMP1]]
129;
130  %1 = fadd reassoc nsz float %a, 1234.0
131  %2 = fadd reassoc nsz float %b, %1
132  %3 = fsub reassoc nsz float 0.0, %a
133  %4 = fadd reassoc nsz float %2, %3
134  ret float %4
135}
136
137define float @test15_reassoc(float %b, float %a) {
138; CHECK-LABEL: @test15_reassoc(
139; CHECK-NEXT:    [[TMP1:%.*]] = fadd reassoc float [[A:%.*]], 1.234000e+03
140; CHECK-NEXT:    [[TMP2:%.*]] = fadd reassoc float [[TMP1]], [[B:%.*]]
141; CHECK-NEXT:    [[TMP3:%.*]] = fsub reassoc float 0.000000e+00, [[A]]
142; CHECK-NEXT:    [[TMP4:%.*]] = fadd reassoc float [[TMP3]], [[TMP2]]
143; CHECK-NEXT:    ret float [[TMP4]]
144;
145  %1 = fadd reassoc float %a, 1234.0
146  %2 = fadd reassoc float %b, %1
147  %3 = fsub reassoc float 0.0, %a
148  %4 = fadd reassoc float %2, %3
149  ret float %4
150}
151
152; Test that we can turn things like X*-(Y*Z) -> X*-1*Y*Z.
153
154define float @test16(float %a, float %b, float %z) {
155; REASSOC_AND_IC-LABEL: @test16(
156; REASSOC_AND_IC-NEXT:    [[C:%.*]] = fmul fast float [[A:%.*]], 1.234500e+04
157; REASSOC_AND_IC-NEXT:    [[E:%.*]] = fmul fast float [[C]], [[B:%.*]]
158; REASSOC_AND_IC-NEXT:    [[F:%.*]] = fmul fast float [[E]], [[Z:%.*]]
159; REASSOC_AND_IC-NEXT:    ret float [[F]]
160;
161; O2-LABEL: @test16(
162; O2-NEXT:    [[D:%.*]] = fmul fast float [[A:%.*]], 1.234500e+04
163; O2-NEXT:    [[E:%.*]] = fmul fast float [[D]], [[B:%.*]]
164; O2-NEXT:    [[G:%.*]] = fmul fast float [[E]], [[Z:%.*]]
165; O2-NEXT:    ret float [[G]]
166;
167  %c = fsub fast float 0.000000e+00, %z
168  %d = fmul fast float %a, %b
169  %e = fmul fast float %c, %d
170  %f = fmul fast float %e, 1.234500e+04
171  %g = fsub fast float 0.000000e+00, %f
172  ret float %g
173}
174
175define float @test16_unary_fneg(float %a, float %b, float %z) {
176; REASSOC_AND_IC-LABEL: @test16_unary_fneg(
177; REASSOC_AND_IC-NEXT:    [[E:%.*]] = fmul fast float [[A:%.*]], 1.234500e+04
178; REASSOC_AND_IC-NEXT:    [[F:%.*]] = fmul fast float [[E]], [[B:%.*]]
179; REASSOC_AND_IC-NEXT:    [[G:%.*]] = fmul fast float [[F]], [[Z:%.*]]
180; REASSOC_AND_IC-NEXT:    ret float [[G]]
181;
182; O2-LABEL: @test16_unary_fneg(
183; O2-NEXT:    [[D:%.*]] = fmul fast float [[A:%.*]], 1.234500e+04
184; O2-NEXT:    [[E:%.*]] = fmul fast float [[D]], [[B:%.*]]
185; O2-NEXT:    [[G:%.*]] = fmul fast float [[E]], [[Z:%.*]]
186; O2-NEXT:    ret float [[G]]
187;
188  %c = fneg fast float %z
189  %d = fmul fast float %a, %b
190  %e = fmul fast float %c, %d
191  %f = fmul fast float %e, 1.234500e+04
192  %g = fneg fast float %f
193  ret float %g
194}
195
196define float @test16_reassoc_nsz(float %a, float %b, float %z) {
197; REASSOC_AND_IC-LABEL: @test16_reassoc_nsz(
198; REASSOC_AND_IC-NEXT:    [[C:%.*]] = fmul reassoc nsz float [[A:%.*]], 1.234500e+04
199; REASSOC_AND_IC-NEXT:    [[E:%.*]] = fmul reassoc nsz float [[C]], [[B:%.*]]
200; REASSOC_AND_IC-NEXT:    [[F:%.*]] = fmul reassoc nsz float [[E]], [[Z:%.*]]
201; REASSOC_AND_IC-NEXT:    ret float [[F]]
202;
203; O2-LABEL: @test16_reassoc_nsz(
204; O2-NEXT:    [[D:%.*]] = fmul reassoc nsz float [[A:%.*]], 1.234500e+04
205; O2-NEXT:    [[E:%.*]] = fmul reassoc nsz float [[D]], [[B:%.*]]
206; O2-NEXT:    [[G:%.*]] = fmul reassoc nsz float [[E]], [[Z:%.*]]
207; O2-NEXT:    ret float [[G]]
208;
209  %c = fsub reassoc nsz float 0.000000e+00, %z
210  %d = fmul reassoc nsz float %a, %b
211  %e = fmul reassoc nsz float %c, %d
212  %f = fmul reassoc nsz float %e, 1.234500e+04
213  %g = fsub reassoc nsz float 0.000000e+00, %f
214  ret float %g
215}
216
217define float @test16_reassoc(float %a, float %b, float %z) {
218; CHECK-LABEL: @test16_reassoc(
219; CHECK-NEXT:    [[C:%.*]] = fsub reassoc float 0.000000e+00, [[Z:%.*]]
220; CHECK-NEXT:    [[D:%.*]] = fmul reassoc float [[A:%.*]], [[B:%.*]]
221; CHECK-NEXT:    [[E:%.*]] = fmul reassoc float [[D]], [[C]]
222; CHECK-NEXT:    [[F:%.*]] = fmul reassoc float [[E]], 1.234500e+04
223; CHECK-NEXT:    [[G:%.*]] = fsub reassoc float 0.000000e+00, [[F]]
224; CHECK-NEXT:    ret float [[G]]
225;
226  %c = fsub reassoc float 0.000000e+00, %z
227  %d = fmul reassoc float %a, %b
228  %e = fmul reassoc float %c, %d
229  %f = fmul reassoc float %e, 1.234500e+04
230  %g = fsub reassoc float 0.000000e+00, %f
231  ret float %g
232}
233
234; With sub reassociation, constant folding can eliminate the 12 and -12 constants.
235
236define float @test18(float %A, float %B) {
237; CHECK-LABEL: @test18(
238; CHECK-NEXT:    [[Z:%.*]] = fsub fast float [[A:%.*]], [[B:%.*]]
239; CHECK-NEXT:    ret float [[Z]]
240;
241  %X = fadd fast float -1.200000e+01, %A
242  %Y = fsub fast float %X, %B
243  %Z = fadd fast float %Y, 1.200000e+01
244  ret float %Z
245}
246
247define float @test18_reassoc(float %A, float %B) {
248; CHECK-LABEL: @test18_reassoc(
249; CHECK-NEXT:    [[X:%.*]] = fadd reassoc float [[A:%.*]], -1.200000e+01
250; CHECK-NEXT:    [[Y:%.*]] = fsub reassoc float [[X]], [[B:%.*]]
251; CHECK-NEXT:    [[Z:%.*]] = fadd reassoc float [[Y]], 1.200000e+01
252; CHECK-NEXT:    ret float [[Z]]
253;
254  %X = fadd reassoc float -1.200000e+01, %A
255  %Y = fsub reassoc float %X, %B
256  %Z = fadd reassoc float %Y, 1.200000e+01
257  ret float %Z
258}
259
260; test18 - check that the bug described in the revision does not appear:
261; https://reviews.llvm.org/D72521
262
263; With sub reassociation, constant folding can eliminate the uses of %a.
264
265define float @test19(float %a, float %b, float %c) nounwind  {
266; REASSOC_AND_IC-LABEL: @test19(
267; REASSOC_AND_IC-NEXT:    [[TMP1:%.*]] = fadd fast float [[B:%.*]], [[C:%.*]]
268; REASSOC_AND_IC-NEXT:    [[T7:%.*]] = fneg fast float [[TMP1]]
269; REASSOC_AND_IC-NEXT:    ret float [[T7]]
270;
271; O2-LABEL: @test19(
272; O2-NEXT:    [[TMP1:%.*]] = fadd fast float [[C:%.*]], [[B:%.*]]
273; O2-NEXT:    [[T7:%.*]] = fneg fast float [[TMP1]]
274; O2-NEXT:    ret float [[T7]]
275;
276  %t3 = fsub fast float %a, %b
277  %t5 = fsub fast float %t3, %c
278  %t7 = fsub fast float %t5, %a
279  ret float %t7
280}
281
282define float @test19_reassoc_nsz(float %a, float %b, float %c) nounwind  {
283; CHECK-LABEL: @test19_reassoc_nsz(
284; CHECK-NEXT:    [[TMP1:%.*]] = fadd reassoc nsz float [[C:%.*]], [[B:%.*]]
285; CHECK-NEXT:    [[T7:%.*]] = fneg reassoc nsz float [[TMP1]]
286; CHECK-NEXT:    ret float [[T7]]
287;
288  %t3 = fsub reassoc nsz float %a, %b
289  %t5 = fsub reassoc nsz float %t3, %c
290  %t7 = fsub reassoc nsz float %t5, %a
291  ret float %t7
292}
293
294; Verify the fold is not done with only 'reassoc' ('nsz' is required).
295define float @test19_reassoc(float %a, float %b, float %c) nounwind  {
296; CHECK-LABEL: @test19_reassoc(
297; CHECK-NEXT:    [[T3:%.*]] = fsub reassoc float [[A:%.*]], [[B:%.*]]
298; CHECK-NEXT:    [[T5:%.*]] = fsub reassoc float [[T3]], [[C:%.*]]
299; CHECK-NEXT:    [[T7:%.*]] = fsub reassoc float [[T5]], [[A]]
300; CHECK-NEXT:    ret float [[T7]]
301;
302  %t3 = fsub reassoc float %a, %b
303  %t5 = fsub reassoc float %t3, %c
304  %t7 = fsub reassoc float %t5, %a
305  ret float %t7
306}
307