1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 declare void @use(float)
6 define float @fneg_fneg(float %a) {
8 ; CHECK-LABEL: @fneg_fneg(
9 ; CHECK-NEXT: ret float [[A:%.*]]
16 ; -(X * C) --> X * (-C)
18 define float @fmul_fsub(float %x) {
19 ; CHECK-LABEL: @fmul_fsub(
20 ; CHECK-NEXT: [[R:%.*]] = fmul float [[X:%.*]], -4.200000e+01
21 ; CHECK-NEXT: ret float [[R]]
23 %m = fmul float %x, 42.0
24 %r = fsub float -0.0, %m
28 define float @fmul_fneg(float %x) {
29 ; CHECK-LABEL: @fmul_fneg(
30 ; CHECK-NEXT: [[R:%.*]] = fmul float [[X:%.*]], -4.200000e+01
31 ; CHECK-NEXT: ret float [[R]]
33 %m = fmul float %x, 42.0
38 ; Fast math is not required, but it should be propagated.
40 define float @fmul_fsub_fmf(float %x) {
41 ; CHECK-LABEL: @fmul_fsub_fmf(
42 ; CHECK-NEXT: [[R:%.*]] = fmul reassoc nsz float [[X:%.*]], -4.200000e+01
43 ; CHECK-NEXT: ret float [[R]]
45 %m = fmul float %x, 42.0
46 %r = fsub reassoc nsz float -0.0, %m
50 define float @fmul_fneg_fmf(float %x) {
51 ; CHECK-LABEL: @fmul_fneg_fmf(
52 ; CHECK-NEXT: [[R:%.*]] = fmul reassoc nsz float [[X:%.*]], -4.200000e+01
53 ; CHECK-NEXT: ret float [[R]]
55 %m = fmul float %x, 42.0
56 %r = fneg reassoc nsz float %m
60 ; Extra use prevents the fold. We don't want to replace the fneg with an fmul.
62 define float @fmul_fsub_extra_use(float %x) {
63 ; CHECK-LABEL: @fmul_fsub_extra_use(
64 ; CHECK-NEXT: [[M:%.*]] = fmul float [[X:%.*]], 4.200000e+01
65 ; CHECK-NEXT: [[R:%.*]] = fsub float -0.000000e+00, [[M]]
66 ; CHECK-NEXT: call void @use(float [[M]])
67 ; CHECK-NEXT: ret float [[R]]
69 %m = fmul float %x, 42.0
70 %r = fsub float -0.0, %m
71 call void @use(float %m)
75 define float @fmul_fneg_extra_use(float %x) {
76 ; CHECK-LABEL: @fmul_fneg_extra_use(
77 ; CHECK-NEXT: [[M:%.*]] = fmul float [[X:%.*]], 4.200000e+01
78 ; CHECK-NEXT: [[R:%.*]] = fneg float [[M]]
79 ; CHECK-NEXT: call void @use(float [[M]])
80 ; CHECK-NEXT: ret float [[R]]
82 %m = fmul float %x, 42.0
84 call void @use(float %m)
88 ; Try a vector. Use special constants (NaN, INF, undef) because they don't change anything.
90 define <4 x double> @fmul_fsub_vec(<4 x double> %x) {
91 ; CHECK-LABEL: @fmul_fsub_vec(
92 ; CHECK-NEXT: [[R:%.*]] = fmul <4 x double> [[X:%.*]], <double -4.200000e+01, double 0x7F80000000000000, double 0xFFF0000000000000, double undef>
93 ; CHECK-NEXT: ret <4 x double> [[R]]
95 %m = fmul <4 x double> %x, <double 42.0, double 0x7FF80000000000000, double 0x7FF0000000000000, double undef>
96 %r = fsub <4 x double> <double -0.0, double -0.0, double -0.0, double -0.0>, %m
100 define <4 x double> @fmul_fneg_vec(<4 x double> %x) {
101 ; CHECK-LABEL: @fmul_fneg_vec(
102 ; CHECK-NEXT: [[R:%.*]] = fmul <4 x double> [[X:%.*]], <double -4.200000e+01, double 0x7F80000000000000, double 0xFFF0000000000000, double undef>
103 ; CHECK-NEXT: ret <4 x double> [[R]]
105 %m = fmul <4 x double> %x, <double 42.0, double 0x7FF80000000000000, double 0x7FF0000000000000, double undef>
106 %r = fneg <4 x double> %m
110 ; -(X / C) --> X / (-C)
112 define float @fdiv_op1_constant_fsub(float %x) {
113 ; CHECK-LABEL: @fdiv_op1_constant_fsub(
114 ; CHECK-NEXT: [[R:%.*]] = fdiv float [[X:%.*]], 4.200000e+01
115 ; CHECK-NEXT: ret float [[R]]
117 %d = fdiv float %x, -42.0
118 %r = fsub float -0.0, %d
122 define float @fdiv_op1_constant_fneg(float %x) {
123 ; CHECK-LABEL: @fdiv_op1_constant_fneg(
124 ; CHECK-NEXT: [[R:%.*]] = fdiv float [[X:%.*]], 4.200000e+01
125 ; CHECK-NEXT: ret float [[R]]
127 %d = fdiv float %x, -42.0
132 ; Fast math is not required, but it should be propagated.
134 define float @fdiv_op1_constant_fsub_fmf(float %x) {
135 ; CHECK-LABEL: @fdiv_op1_constant_fsub_fmf(
136 ; CHECK-NEXT: [[R:%.*]] = fdiv nnan float [[X:%.*]], 4.200000e+01
137 ; CHECK-NEXT: ret float [[R]]
139 %d = fdiv float %x, -42.0
140 %r = fsub nnan float -0.0, %d
144 define float @fdiv_op1_constant_fneg_fmf(float %x) {
145 ; CHECK-LABEL: @fdiv_op1_constant_fneg_fmf(
146 ; CHECK-NEXT: [[R:%.*]] = fdiv nnan float [[X:%.*]], 4.200000e+01
147 ; CHECK-NEXT: ret float [[R]]
149 %d = fdiv float %x, -42.0
150 %r = fneg nnan float %d
154 ; Extra use prevents the fold. We don't want to replace the fneg with an fdiv.
156 define float @fdiv_op1_constant_fsub_extra_use(float %x) {
157 ; CHECK-LABEL: @fdiv_op1_constant_fsub_extra_use(
158 ; CHECK-NEXT: [[D:%.*]] = fdiv float [[X:%.*]], 4.200000e+01
159 ; CHECK-NEXT: [[R:%.*]] = fsub float -0.000000e+00, [[D]]
160 ; CHECK-NEXT: call void @use(float [[D]])
161 ; CHECK-NEXT: ret float [[R]]
163 %d = fdiv float %x, 42.0
164 %r = fsub float -0.0, %d
165 call void @use(float %d)
169 define float @fdiv_op1_constant_fneg_extra_use(float %x) {
170 ; CHECK-LABEL: @fdiv_op1_constant_fneg_extra_use(
171 ; CHECK-NEXT: [[D:%.*]] = fdiv float [[X:%.*]], 4.200000e+01
172 ; CHECK-NEXT: [[R:%.*]] = fneg float [[D]]
173 ; CHECK-NEXT: call void @use(float [[D]])
174 ; CHECK-NEXT: ret float [[R]]
176 %d = fdiv float %x, 42.0
178 call void @use(float %d)
182 ; Try a vector. Use special constants (NaN, INF, undef) because they don't change anything.
184 define <4 x double> @fdiv_op1_constant_fsub_vec(<4 x double> %x) {
185 ; CHECK-LABEL: @fdiv_op1_constant_fsub_vec(
186 ; CHECK-NEXT: [[R:%.*]] = fdiv <4 x double> [[X:%.*]], <double 4.200000e+01, double 0x7FF800000ABCD000, double 0x7FF0000000000000, double undef>
187 ; CHECK-NEXT: ret <4 x double> [[R]]
189 %d = fdiv <4 x double> %x, <double -42.0, double 0xFFF800000ABCD000, double 0xFFF0000000000000, double undef>
190 %r = fsub <4 x double> <double -0.0, double -0.0, double -0.0, double -0.0>, %d
194 define <4 x double> @fdiv_op1_constant_fneg_vec(<4 x double> %x) {
195 ; CHECK-LABEL: @fdiv_op1_constant_fneg_vec(
196 ; CHECK-NEXT: [[R:%.*]] = fdiv <4 x double> [[X:%.*]], <double 4.200000e+01, double 0x7FF800000ABCD000, double 0x7FF0000000000000, double undef>
197 ; CHECK-NEXT: ret <4 x double> [[R]]
199 %d = fdiv <4 x double> %x, <double -42.0, double 0xFFF800000ABCD000, double 0xFFF0000000000000, double undef>
200 %r = fneg <4 x double> %d
204 ; -(C / X) --> (-C) / X
206 define float @fdiv_op0_constant_fsub(float %x) {
207 ; CHECK-LABEL: @fdiv_op0_constant_fsub(
208 ; CHECK-NEXT: [[R:%.*]] = fdiv float -4.200000e+01, [[X:%.*]]
209 ; CHECK-NEXT: ret float [[R]]
211 %d = fdiv float 42.0, %x
212 %r = fsub float -0.0, %d
216 define float @fdiv_op0_constant_fneg(float %x) {
217 ; CHECK-LABEL: @fdiv_op0_constant_fneg(
218 ; CHECK-NEXT: [[R:%.*]] = fdiv float -4.200000e+01, [[X:%.*]]
219 ; CHECK-NEXT: ret float [[R]]
221 %d = fdiv float 42.0, %x
226 ; Fast math is not required, but it should be propagated.
228 define float @fdiv_op0_constant_fsub_fmf(float %x) {
229 ; CHECK-LABEL: @fdiv_op0_constant_fsub_fmf(
230 ; CHECK-NEXT: [[R:%.*]] = fdiv fast float -4.200000e+01, [[X:%.*]]
231 ; CHECK-NEXT: ret float [[R]]
233 %d = fdiv float 42.0, %x
234 %r = fsub fast float -0.0, %d
238 define float @fdiv_op0_constant_fneg_fmf(float %x) {
239 ; CHECK-LABEL: @fdiv_op0_constant_fneg_fmf(
240 ; CHECK-NEXT: [[R:%.*]] = fdiv fast float -4.200000e+01, [[X:%.*]]
241 ; CHECK-NEXT: ret float [[R]]
243 %d = fdiv float 42.0, %x
244 %r = fneg fast float %d
248 ; Extra use prevents the fold. We don't want to replace the fneg with an fdiv.
250 define float @fdiv_op0_constant_fsub_extra_use(float %x) {
251 ; CHECK-LABEL: @fdiv_op0_constant_fsub_extra_use(
252 ; CHECK-NEXT: [[D:%.*]] = fdiv float -4.200000e+01, [[X:%.*]]
253 ; CHECK-NEXT: [[R:%.*]] = fsub float -0.000000e+00, [[D]]
254 ; CHECK-NEXT: call void @use(float [[D]])
255 ; CHECK-NEXT: ret float [[R]]
257 %d = fdiv float -42.0, %x
258 %r = fsub float -0.0, %d
259 call void @use(float %d)
263 define float @fdiv_op0_constant_fneg_extra_use(float %x) {
264 ; CHECK-LABEL: @fdiv_op0_constant_fneg_extra_use(
265 ; CHECK-NEXT: [[D:%.*]] = fdiv float -4.200000e+01, [[X:%.*]]
266 ; CHECK-NEXT: [[R:%.*]] = fneg float [[D]]
267 ; CHECK-NEXT: call void @use(float [[D]])
268 ; CHECK-NEXT: ret float [[R]]
270 %d = fdiv float -42.0, %x
272 call void @use(float %d)
276 ; Try a vector. Use special constants (NaN, INF, undef) because they don't change anything.
278 define <4 x double> @fdiv_op0_constant_fsub_vec(<4 x double> %x) {
279 ; CHECK-LABEL: @fdiv_op0_constant_fsub_vec(
280 ; CHECK-NEXT: [[R:%.*]] = fdiv <4 x double> <double 4.200000e+01, double 0x7F80000000000000, double 0x7FF0000000000000, double undef>, [[X:%.*]]
281 ; CHECK-NEXT: ret <4 x double> [[R]]
283 %d = fdiv <4 x double> <double -42.0, double 0x7FF80000000000000, double 0xFFF0000000000000, double undef>, %x
284 %r = fsub <4 x double> <double -0.0, double -0.0, double -0.0, double -0.0>, %d
288 define <4 x double> @fdiv_op0_constant_fneg_vec(<4 x double> %x) {
289 ; CHECK-LABEL: @fdiv_op0_constant_fneg_vec(
290 ; CHECK-NEXT: [[R:%.*]] = fdiv <4 x double> <double 4.200000e+01, double 0x7F80000000000000, double 0x7FF0000000000000, double undef>, [[X:%.*]]
291 ; CHECK-NEXT: ret <4 x double> [[R]]
293 %d = fdiv <4 x double> <double -42.0, double 0x7FF80000000000000, double 0xFFF0000000000000, double undef>, %x
294 %r = fneg <4 x double> %d
298 ; Sink FP negation through a select:
299 ; c ? -x : -y --> -(c ? x : y)
301 define <2 x double> @fneg_fneg_sel(<2 x double> %x, <2 x double> %y, i1 %cond) {
302 ; CHECK-LABEL: @fneg_fneg_sel(
303 ; CHECK-NEXT: [[SEL_V:%.*]] = select i1 [[COND:%.*]], <2 x double> [[X:%.*]], <2 x double> [[Y:%.*]]
304 ; CHECK-NEXT: [[SEL:%.*]] = fneg <2 x double> [[SEL_V]]
305 ; CHECK-NEXT: ret <2 x double> [[SEL]]
307 %n1 = fneg <2 x double> %x
308 %n2 = fneg <2 x double> %y
309 %sel = select i1 %cond, <2 x double> %n1, <2 x double> %n2
310 ret <2 x double> %sel
313 ; An extra use is allowed.
315 define float @fneg_fneg_sel_extra_use1(float %x, float %y, i1 %cond) {
316 ; CHECK-LABEL: @fneg_fneg_sel_extra_use1(
317 ; CHECK-NEXT: [[N1:%.*]] = fneg float [[X:%.*]]
318 ; CHECK-NEXT: call void @use(float [[N1]])
319 ; CHECK-NEXT: [[SEL_V:%.*]] = select i1 [[COND:%.*]], float [[X]], float [[Y:%.*]]
320 ; CHECK-NEXT: [[SEL:%.*]] = fneg float [[SEL_V]]
321 ; CHECK-NEXT: ret float [[SEL]]
324 call void @use(float %n1)
326 %sel = select i1 %cond, float %n1, float %n2
330 define float @fneg_fneg_sel_extra_use2(float %x, float %y, i1 %cond) {
331 ; CHECK-LABEL: @fneg_fneg_sel_extra_use2(
332 ; CHECK-NEXT: [[N2:%.*]] = fneg float [[Y:%.*]]
333 ; CHECK-NEXT: call void @use(float [[N2]])
334 ; CHECK-NEXT: [[SEL_V:%.*]] = select i1 [[COND:%.*]], float [[X:%.*]], float [[Y]]
335 ; CHECK-NEXT: [[SEL:%.*]] = fneg float [[SEL_V]]
336 ; CHECK-NEXT: ret float [[SEL]]
340 call void @use(float %n2)
341 %sel = select i1 %cond, float %n1, float %n2
345 ; Legacy form of fneg should work too.
347 define float @fsub_fsub_sel_extra_use1(float %x, float %y, i1 %cond) {
348 ; CHECK-LABEL: @fsub_fsub_sel_extra_use1(
349 ; CHECK-NEXT: [[N1:%.*]] = fsub float -0.000000e+00, [[X:%.*]]
350 ; CHECK-NEXT: call void @use(float [[N1]])
351 ; CHECK-NEXT: [[SEL_V:%.*]] = select i1 [[COND:%.*]], float [[X]], float [[Y:%.*]]
352 ; CHECK-NEXT: [[SEL:%.*]] = fsub float -0.000000e+00, [[SEL_V]]
353 ; CHECK-NEXT: ret float [[SEL]]
355 %n1 = fsub float -0.0, %x
356 call void @use(float %n1)
357 %n2 = fsub float -0.0, %y
358 %sel = select i1 %cond, float %n1, float %n2
362 ; Negative test: but 2 extra uses would require an extra instruction.
364 define float @fneg_fneg_sel_extra_use3(float %x, float %y, i1 %cond) {
365 ; CHECK-LABEL: @fneg_fneg_sel_extra_use3(
366 ; CHECK-NEXT: [[N1:%.*]] = fneg float [[X:%.*]]
367 ; CHECK-NEXT: call void @use(float [[N1]])
368 ; CHECK-NEXT: [[N2:%.*]] = fneg float [[Y:%.*]]
369 ; CHECK-NEXT: call void @use(float [[N2]])
370 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], float [[N1]], float [[N2]]
371 ; CHECK-NEXT: ret float [[SEL]]
374 call void @use(float %n1)
376 call void @use(float %n2)
377 %sel = select i1 %cond, float %n1, float %n2