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