1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
6 define float @test1(float %x, float %y) {
8 ; CHECK-NEXT: [[T1:%.*]] = fsub float [[X:%.*]], [[Y:%.*]]
9 ; CHECK-NEXT: [[T2:%.*]] = fneg float [[T1]]
10 ; CHECK-NEXT: ret float [[T2]]
12 %t1 = fsub float %x, %y
13 %t2 = fsub float -0.0, %t1
17 define float @test1_unary(float %x, float %y) {
18 ; CHECK-LABEL: @test1_unary(
19 ; CHECK-NEXT: [[T1:%.*]] = fsub float [[X:%.*]], [[Y:%.*]]
20 ; CHECK-NEXT: [[T2:%.*]] = fneg float [[T1]]
21 ; CHECK-NEXT: ret float [[T2]]
23 %t1 = fsub float %x, %y
28 ; Can't do anything with the test above because -0.0 - 0.0 = -0.0, but if we have nsz:
31 define float @neg_sub_nsz(float %x, float %y) {
32 ; CHECK-LABEL: @neg_sub_nsz(
33 ; CHECK-NEXT: [[T2:%.*]] = fsub nsz float [[Y:%.*]], [[X:%.*]]
34 ; CHECK-NEXT: ret float [[T2]]
36 %t1 = fsub float %x, %y
37 %t2 = fsub nsz float -0.0, %t1
41 define float @unary_neg_sub_nsz(float %x, float %y) {
42 ; CHECK-LABEL: @unary_neg_sub_nsz(
43 ; CHECK-NEXT: [[T2:%.*]] = fsub nsz float [[Y:%.*]], [[X:%.*]]
44 ; CHECK-NEXT: ret float [[T2]]
46 %t1 = fsub float %x, %y
47 %t2 = fneg nsz float %t1
51 ; If the subtract has another use, we don't do the transform (even though it
52 ; doesn't increase the IR instruction count) because we assume that fneg is
53 ; easier to analyze and generally cheaper than generic fsub.
55 declare void @use(float)
56 declare void @use2(float, double)
58 define float @neg_sub_nsz_extra_use(float %x, float %y) {
59 ; CHECK-LABEL: @neg_sub_nsz_extra_use(
60 ; CHECK-NEXT: [[T1:%.*]] = fsub float [[X:%.*]], [[Y:%.*]]
61 ; CHECK-NEXT: [[T2:%.*]] = fneg nsz float [[T1]]
62 ; CHECK-NEXT: call void @use(float [[T1]])
63 ; CHECK-NEXT: ret float [[T2]]
65 %t1 = fsub float %x, %y
66 %t2 = fsub nsz float -0.0, %t1
67 call void @use(float %t1)
71 define float @unary_neg_sub_nsz_extra_use(float %x, float %y) {
72 ; CHECK-LABEL: @unary_neg_sub_nsz_extra_use(
73 ; CHECK-NEXT: [[T1:%.*]] = fsub float [[X:%.*]], [[Y:%.*]]
74 ; CHECK-NEXT: [[T2:%.*]] = fneg nsz float [[T1]]
75 ; CHECK-NEXT: call void @use(float [[T1]])
76 ; CHECK-NEXT: ret float [[T2]]
78 %t1 = fsub float %x, %y
79 %t2 = fneg nsz float %t1
80 call void @use(float %t1)
84 ; With nsz: Z - (X - Y) --> Z + (Y - X)
86 define float @sub_sub_nsz(float %x, float %y, float %z) {
87 ; CHECK-LABEL: @sub_sub_nsz(
88 ; CHECK-NEXT: [[TMP1:%.*]] = fsub nsz float [[Y:%.*]], [[X:%.*]]
89 ; CHECK-NEXT: [[T2:%.*]] = fadd nsz float [[TMP1]], [[Z:%.*]]
90 ; CHECK-NEXT: ret float [[T2]]
92 %t1 = fsub float %x, %y
93 %t2 = fsub nsz float %z, %t1
97 ; With nsz and reassoc: Y - ((X * 5) + Y) --> X * -5
99 define float @sub_add_neg_x(float %x, float %y) {
100 ; CHECK-LABEL: @sub_add_neg_x(
101 ; CHECK-NEXT: [[R:%.*]] = fmul reassoc nsz float [[X:%.*]], -5.000000e+00
102 ; CHECK-NEXT: ret float [[R]]
104 %mul = fmul float %x, 5.000000e+00
105 %add = fadd float %mul, %y
106 %r = fsub nsz reassoc float %y, %add
110 ; Same as above: if 'Z' is not -0.0, swap fsub operands and convert to fadd.
112 define float @sub_sub_known_not_negzero(float %x, float %y) {
113 ; CHECK-LABEL: @sub_sub_known_not_negzero(
114 ; CHECK-NEXT: [[TMP1:%.*]] = fsub float [[Y:%.*]], [[X:%.*]]
115 ; CHECK-NEXT: [[T2:%.*]] = fadd float [[TMP1]], 4.200000e+01
116 ; CHECK-NEXT: ret float [[T2]]
118 %t1 = fsub float %x, %y
119 %t2 = fsub float 42.0, %t1
123 ; <rdar://problem/7530098>
125 define double @test2(double %x, double %y) {
126 ; CHECK-LABEL: @test2(
127 ; CHECK-NEXT: [[T1:%.*]] = fadd double [[X:%.*]], [[Y:%.*]]
128 ; CHECK-NEXT: [[T2:%.*]] = fsub double [[X]], [[T1]]
129 ; CHECK-NEXT: ret double [[T2]]
131 %t1 = fadd double %x, %y
132 %t2 = fsub double %x, %t1
138 define float @constant_op1(float %x, float %y) {
139 ; CHECK-LABEL: @constant_op1(
140 ; CHECK-NEXT: [[R:%.*]] = fadd float [[X:%.*]], -4.200000e+01
141 ; CHECK-NEXT: ret float [[R]]
143 %r = fsub float %x, 42.0
147 define <2 x float> @constant_op1_vec(<2 x float> %x, <2 x float> %y) {
148 ; CHECK-LABEL: @constant_op1_vec(
149 ; CHECK-NEXT: [[R:%.*]] = fadd <2 x float> [[X:%.*]], <float -4.200000e+01, float 4.200000e+01>
150 ; CHECK-NEXT: ret <2 x float> [[R]]
152 %r = fsub <2 x float> %x, <float 42.0, float -42.0>
156 define <2 x float> @constant_op1_vec_poison(<2 x float> %x, <2 x float> %y) {
157 ; CHECK-LABEL: @constant_op1_vec_poison(
158 ; CHECK-NEXT: [[R:%.*]] = fadd <2 x float> [[X:%.*]], <float poison, float 4.200000e+01>
159 ; CHECK-NEXT: ret <2 x float> [[R]]
161 %r = fsub <2 x float> %x, <float poison, float -42.0>
167 define float @neg_op1(float %x, float %y) {
168 ; CHECK-LABEL: @neg_op1(
169 ; CHECK-NEXT: [[R:%.*]] = fadd float [[X:%.*]], [[Y:%.*]]
170 ; CHECK-NEXT: ret float [[R]]
172 %negy = fsub float -0.0, %y
173 %r = fsub float %x, %negy
177 define float @unary_neg_op1(float %x, float %y) {
178 ; CHECK-LABEL: @unary_neg_op1(
179 ; CHECK-NEXT: [[R:%.*]] = fadd float [[X:%.*]], [[Y:%.*]]
180 ; CHECK-NEXT: ret float [[R]]
182 %negy = fneg float %y
183 %r = fsub float %x, %negy
187 define <2 x float> @neg_op1_vec(<2 x float> %x, <2 x float> %y) {
188 ; CHECK-LABEL: @neg_op1_vec(
189 ; CHECK-NEXT: [[R:%.*]] = fadd <2 x float> [[X:%.*]], [[Y:%.*]]
190 ; CHECK-NEXT: ret <2 x float> [[R]]
192 %negy = fsub <2 x float> <float -0.0, float -0.0>, %y
193 %r = fsub <2 x float> %x, %negy
197 define <2 x float> @unary_neg_op1_vec(<2 x float> %x, <2 x float> %y) {
198 ; CHECK-LABEL: @unary_neg_op1_vec(
199 ; CHECK-NEXT: [[R:%.*]] = fadd <2 x float> [[X:%.*]], [[Y:%.*]]
200 ; CHECK-NEXT: ret <2 x float> [[R]]
202 %negy = fneg <2 x float> %y
203 %r = fsub <2 x float> %x, %negy
207 define <2 x float> @neg_op1_vec_poison(<2 x float> %x, <2 x float> %y) {
208 ; CHECK-LABEL: @neg_op1_vec_poison(
209 ; CHECK-NEXT: [[R:%.*]] = fadd <2 x float> [[X:%.*]], [[Y:%.*]]
210 ; CHECK-NEXT: ret <2 x float> [[R]]
212 %negy = fsub <2 x float> <float -0.0, float poison>, %y
213 %r = fsub <2 x float> %x, %negy
217 ; Similar to above - but look through fpext/fptrunc casts to find the fneg.
219 define double @neg_ext_op1(float %a, double %b) {
220 ; CHECK-LABEL: @neg_ext_op1(
221 ; CHECK-NEXT: [[TMP1:%.*]] = fpext float [[A:%.*]] to double
222 ; CHECK-NEXT: [[T3:%.*]] = fadd double [[TMP1]], [[B:%.*]]
223 ; CHECK-NEXT: ret double [[T3]]
225 %t1 = fsub float -0.0, %a
226 %t2 = fpext float %t1 to double
227 %t3 = fsub double %b, %t2
231 define double @unary_neg_ext_op1(float %a, double %b) {
232 ; CHECK-LABEL: @unary_neg_ext_op1(
233 ; CHECK-NEXT: [[TMP1:%.*]] = fpext float [[A:%.*]] to double
234 ; CHECK-NEXT: [[T3:%.*]] = fadd double [[TMP1]], [[B:%.*]]
235 ; CHECK-NEXT: ret double [[T3]]
238 %t2 = fpext float %t1 to double
239 %t3 = fsub double %b, %t2
243 ; Verify that vectors work too.
245 define <2 x float> @neg_trunc_op1(<2 x double> %a, <2 x float> %b) {
246 ; CHECK-LABEL: @neg_trunc_op1(
247 ; CHECK-NEXT: [[TMP1:%.*]] = fptrunc <2 x double> [[A:%.*]] to <2 x float>
248 ; CHECK-NEXT: [[T3:%.*]] = fadd <2 x float> [[TMP1]], [[B:%.*]]
249 ; CHECK-NEXT: ret <2 x float> [[T3]]
251 %t1 = fsub <2 x double> <double -0.0, double -0.0>, %a
252 %t2 = fptrunc <2 x double> %t1 to <2 x float>
253 %t3 = fsub <2 x float> %b, %t2
257 define <2 x float> @unary_neg_trunc_op1(<2 x double> %a, <2 x float> %b) {
258 ; CHECK-LABEL: @unary_neg_trunc_op1(
259 ; CHECK-NEXT: [[TMP1:%.*]] = fptrunc <2 x double> [[A:%.*]] to <2 x float>
260 ; CHECK-NEXT: [[T3:%.*]] = fadd <2 x float> [[TMP1]], [[B:%.*]]
261 ; CHECK-NEXT: ret <2 x float> [[T3]]
263 %t1 = fneg <2 x double> %a
264 %t2 = fptrunc <2 x double> %t1 to <2 x float>
265 %t3 = fsub <2 x float> %b, %t2
269 ; No FMF needed, but they should propagate to the fadd.
271 define double @neg_ext_op1_fast(float %a, double %b) {
272 ; CHECK-LABEL: @neg_ext_op1_fast(
273 ; CHECK-NEXT: [[TMP1:%.*]] = fpext float [[A:%.*]] to double
274 ; CHECK-NEXT: [[T3:%.*]] = fadd fast double [[TMP1]], [[B:%.*]]
275 ; CHECK-NEXT: ret double [[T3]]
277 %t1 = fsub float -0.0, %a
278 %t2 = fpext float %t1 to double
279 %t3 = fsub fast double %b, %t2
283 define double @unary_neg_ext_op1_fast(float %a, double %b) {
284 ; CHECK-LABEL: @unary_neg_ext_op1_fast(
285 ; CHECK-NEXT: [[TMP1:%.*]] = fpext float [[A:%.*]] to double
286 ; CHECK-NEXT: [[T3:%.*]] = fadd fast double [[TMP1]], [[B:%.*]]
287 ; CHECK-NEXT: ret double [[T3]]
290 %t2 = fpext float %t1 to double
291 %t3 = fsub fast double %b, %t2
295 ; Extra use should prevent the transform.
297 define float @neg_ext_op1_extra_use(half %a, float %b) {
298 ; CHECK-LABEL: @neg_ext_op1_extra_use(
299 ; CHECK-NEXT: [[T1:%.*]] = fneg half [[A:%.*]]
300 ; CHECK-NEXT: [[T2:%.*]] = fpext half [[T1]] to float
301 ; CHECK-NEXT: [[T3:%.*]] = fsub float [[B:%.*]], [[T2]]
302 ; CHECK-NEXT: call void @use(float [[T2]])
303 ; CHECK-NEXT: ret float [[T3]]
305 %t1 = fsub half -0.0, %a
306 %t2 = fpext half %t1 to float
307 %t3 = fsub float %b, %t2
308 call void @use(float %t2)
312 define float @unary_neg_ext_op1_extra_use(half %a, float %b) {
313 ; CHECK-LABEL: @unary_neg_ext_op1_extra_use(
314 ; CHECK-NEXT: [[T1:%.*]] = fneg half [[A:%.*]]
315 ; CHECK-NEXT: [[T2:%.*]] = fpext half [[T1]] to float
316 ; CHECK-NEXT: [[T3:%.*]] = fsub float [[B:%.*]], [[T2]]
317 ; CHECK-NEXT: call void @use(float [[T2]])
318 ; CHECK-NEXT: ret float [[T3]]
321 %t2 = fpext half %t1 to float
322 %t3 = fsub float %b, %t2
323 call void @use(float %t2)
327 ; One-use fptrunc is always hoisted above fneg, so the corresponding
328 ; multi-use bug for fptrunc isn't visible with a fold starting from
331 define float @neg_trunc_op1_extra_use(double %a, float %b) {
332 ; CHECK-LABEL: @neg_trunc_op1_extra_use(
333 ; CHECK-NEXT: [[TMP1:%.*]] = fptrunc double [[A:%.*]] to float
334 ; CHECK-NEXT: [[T2:%.*]] = fneg float [[TMP1]]
335 ; CHECK-NEXT: [[T3:%.*]] = fadd float [[TMP1]], [[B:%.*]]
336 ; CHECK-NEXT: call void @use(float [[T2]])
337 ; CHECK-NEXT: ret float [[T3]]
339 %t1 = fsub double -0.0, %a
340 %t2 = fptrunc double %t1 to float
341 %t3 = fsub float %b, %t2
342 call void @use(float %t2)
346 define float @unary_neg_trunc_op1_extra_use(double %a, float %b) {
347 ; CHECK-LABEL: @unary_neg_trunc_op1_extra_use(
348 ; CHECK-NEXT: [[TMP1:%.*]] = fptrunc double [[A:%.*]] to float
349 ; CHECK-NEXT: [[T2:%.*]] = fneg float [[TMP1]]
350 ; CHECK-NEXT: [[T3:%.*]] = fadd float [[TMP1]], [[B:%.*]]
351 ; CHECK-NEXT: call void @use(float [[T2]])
352 ; CHECK-NEXT: ret float [[T3]]
355 %t2 = fptrunc double %t1 to float
356 %t3 = fsub float %b, %t2
357 call void @use(float %t2)
361 ; Extra uses should prevent the transform.
363 define float @neg_trunc_op1_extra_uses(double %a, float %b) {
364 ; CHECK-LABEL: @neg_trunc_op1_extra_uses(
365 ; CHECK-NEXT: [[T1:%.*]] = fneg double [[A:%.*]]
366 ; CHECK-NEXT: [[T2:%.*]] = fptrunc double [[T1]] to float
367 ; CHECK-NEXT: [[T3:%.*]] = fsub float [[B:%.*]], [[T2]]
368 ; CHECK-NEXT: call void @use2(float [[T2]], double [[T1]])
369 ; CHECK-NEXT: ret float [[T3]]
371 %t1 = fsub double -0.0, %a
372 %t2 = fptrunc double %t1 to float
373 %t3 = fsub float %b, %t2
374 call void @use2(float %t2, double %t1)
378 define float @unary_neg_trunc_op1_extra_uses(double %a, float %b) {
379 ; CHECK-LABEL: @unary_neg_trunc_op1_extra_uses(
380 ; CHECK-NEXT: [[T1:%.*]] = fneg double [[A:%.*]]
381 ; CHECK-NEXT: [[T2:%.*]] = fptrunc double [[T1]] to float
382 ; CHECK-NEXT: [[T3:%.*]] = fsub float [[B:%.*]], [[T2]]
383 ; CHECK-NEXT: call void @use2(float [[T2]], double [[T1]])
384 ; CHECK-NEXT: ret float [[T3]]
387 %t2 = fptrunc double %t1 to float
388 %t3 = fsub float %b, %t2
389 call void @use2(float %t2, double %t1)
393 ; Don't negate a constant expression to form fadd and induce infinite looping:
394 ; https://bugs.llvm.org/show_bug.cgi?id=37605
396 @b = external global i16, align 1
398 define float @PR37605(float %conv) {
399 ; CHECK-LABEL: @PR37605(
400 ; CHECK-NEXT: [[SUB:%.*]] = fsub float [[CONV:%.*]], bitcast (i32 ptrtoint (ptr @b to i32) to float)
401 ; CHECK-NEXT: ret float [[SUB]]
403 %sub = fsub float %conv, bitcast (i32 ptrtoint (ptr @b to i32) to float)
407 define double @fsub_fdiv_fneg1(double %x, double %y, double %z) {
408 ; CHECK-LABEL: @fsub_fdiv_fneg1(
409 ; CHECK-NEXT: [[TMP1:%.*]] = fdiv double [[X:%.*]], [[Y:%.*]]
410 ; CHECK-NEXT: [[R:%.*]] = fadd double [[TMP1]], [[Z:%.*]]
411 ; CHECK-NEXT: ret double [[R]]
413 %neg = fsub double -0.000000e+00, %x
414 %div = fdiv double %neg, %y
415 %r = fsub double %z, %div
419 define <2 x double> @fsub_fdiv_fneg2(<2 x double> %x, <2 x double> %y, <2 x double> %z) {
420 ; CHECK-LABEL: @fsub_fdiv_fneg2(
421 ; CHECK-NEXT: [[TMP1:%.*]] = fdiv <2 x double> [[Y:%.*]], [[X:%.*]]
422 ; CHECK-NEXT: [[R:%.*]] = fadd <2 x double> [[TMP1]], [[Z:%.*]]
423 ; CHECK-NEXT: ret <2 x double> [[R]]
425 %neg = fsub <2 x double> <double -0.0, double -0.0>, %x
426 %div = fdiv <2 x double> %y, %neg
427 %r = fsub <2 x double> %z, %div
431 define double @fsub_fmul_fneg1(double %x, double %y, double %z) {
432 ; CHECK-LABEL: @fsub_fmul_fneg1(
433 ; CHECK-NEXT: [[TMP1:%.*]] = fmul double [[X:%.*]], [[Y:%.*]]
434 ; CHECK-NEXT: [[R:%.*]] = fadd double [[TMP1]], [[Z:%.*]]
435 ; CHECK-NEXT: ret double [[R]]
437 %neg = fsub double -0.000000e+00, %x
438 %mul = fmul double %neg, %y
439 %r = fsub double %z, %mul
443 define double @fsub_fmul_fneg2(double %x, double %y, double %z) {
444 ; CHECK-LABEL: @fsub_fmul_fneg2(
445 ; CHECK-NEXT: [[TMP1:%.*]] = fmul double [[X:%.*]], [[Y:%.*]]
446 ; CHECK-NEXT: [[R:%.*]] = fadd double [[TMP1]], [[Z:%.*]]
447 ; CHECK-NEXT: ret double [[R]]
449 %neg = fsub double -0.000000e+00, %x
450 %mul = fmul double %y, %neg
451 %r = fsub double %z, %mul
455 define float @fsub_fdiv_fneg1_extra_use(float %x, float %y, float %z) {
456 ; CHECK-LABEL: @fsub_fdiv_fneg1_extra_use(
457 ; CHECK-NEXT: [[NEG:%.*]] = fneg float [[X:%.*]]
458 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[NEG]], [[Y:%.*]]
459 ; CHECK-NEXT: call void @use(float [[DIV]])
460 ; CHECK-NEXT: [[R:%.*]] = fsub float [[Z:%.*]], [[DIV]]
461 ; CHECK-NEXT: ret float [[R]]
463 %neg = fsub float -0.000000e+00, %x
464 %div = fdiv float %neg, %y
465 call void @use(float %div)
466 %r = fsub float %z, %div
470 define float @fsub_fdiv_fneg2_extra_use(float %x, float %y, float %z) {
471 ; CHECK-LABEL: @fsub_fdiv_fneg2_extra_use(
472 ; CHECK-NEXT: [[NEG:%.*]] = fneg float [[X:%.*]]
473 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[Y:%.*]], [[NEG]]
474 ; CHECK-NEXT: call void @use(float [[DIV]])
475 ; CHECK-NEXT: [[R:%.*]] = fsub float [[Z:%.*]], [[DIV]]
476 ; CHECK-NEXT: ret float [[R]]
478 %neg = fsub float -0.000000e+00, %x
479 %div = fdiv float %y, %neg
480 call void @use(float %div)
481 %r = fsub float %z, %div
485 declare void @use_vec(<2 x float>)
487 define <2 x float> @fsub_fmul_fneg1_extra_use(<2 x float> %x, <2 x float> %y, <2 x float> %z) {
488 ; CHECK-LABEL: @fsub_fmul_fneg1_extra_use(
489 ; CHECK-NEXT: [[NEG:%.*]] = fneg <2 x float> [[X:%.*]]
490 ; CHECK-NEXT: [[MUL:%.*]] = fmul <2 x float> [[NEG]], [[Y:%.*]]
491 ; CHECK-NEXT: call void @use_vec(<2 x float> [[MUL]])
492 ; CHECK-NEXT: [[R:%.*]] = fsub <2 x float> [[Z:%.*]], [[MUL]]
493 ; CHECK-NEXT: ret <2 x float> [[R]]
495 %neg = fsub <2 x float> <float -0.0, float -0.0>, %x
496 %mul = fmul <2 x float> %neg, %y
497 call void @use_vec(<2 x float> %mul)
498 %r = fsub <2 x float> %z, %mul
502 define float @fsub_fmul_fneg2_extra_use(float %x, float %y, float %z) {
503 ; CHECK-LABEL: @fsub_fmul_fneg2_extra_use(
504 ; CHECK-NEXT: [[NEG:%.*]] = fneg float [[X:%.*]]
505 ; CHECK-NEXT: [[MUL:%.*]] = fmul float [[NEG]], [[Y:%.*]]
506 ; CHECK-NEXT: call void @use(float [[MUL]])
507 ; CHECK-NEXT: [[R:%.*]] = fsub float [[Z:%.*]], [[MUL]]
508 ; CHECK-NEXT: ret float [[R]]
510 %neg = fsub float -0.000000e+00, %x
511 %mul = fmul float %y, %neg
512 call void @use(float %mul)
513 %r = fsub float %z, %mul
517 define float @fsub_fdiv_fneg1_extra_use2(float %x, float %y, float %z) {
518 ; CHECK-LABEL: @fsub_fdiv_fneg1_extra_use2(
519 ; CHECK-NEXT: [[NEG:%.*]] = fneg float [[X:%.*]]
520 ; CHECK-NEXT: call void @use(float [[NEG]])
521 ; CHECK-NEXT: [[TMP1:%.*]] = fdiv float [[X]], [[Y:%.*]]
522 ; CHECK-NEXT: [[R:%.*]] = fadd float [[TMP1]], [[Z:%.*]]
523 ; CHECK-NEXT: ret float [[R]]
525 %neg = fsub float -0.000000e+00, %x
526 call void @use(float %neg)
527 %div = fdiv float %neg, %y
528 %r = fsub float %z, %div
532 define float @fsub_fdiv_fneg2_extra_use2(float %x, float %y, float %z) {
533 ; CHECK-LABEL: @fsub_fdiv_fneg2_extra_use2(
534 ; CHECK-NEXT: [[NEG:%.*]] = fneg float [[X:%.*]]
535 ; CHECK-NEXT: call void @use(float [[NEG]])
536 ; CHECK-NEXT: [[TMP1:%.*]] = fdiv float [[Y:%.*]], [[X]]
537 ; CHECK-NEXT: [[R:%.*]] = fadd float [[TMP1]], [[Z:%.*]]
538 ; CHECK-NEXT: ret float [[R]]
540 %neg = fsub float -0.000000e+00, %x
541 call void @use(float %neg)
542 %div = fdiv float %y, %neg
543 %r = fsub float %z, %div
547 define <2 x float> @fsub_fmul_fneg1_extra_use2(<2 x float> %x, <2 x float> %y, <2 x float> %z) {
548 ; CHECK-LABEL: @fsub_fmul_fneg1_extra_use2(
549 ; CHECK-NEXT: [[NEG:%.*]] = fneg <2 x float> [[X:%.*]]
550 ; CHECK-NEXT: call void @use_vec(<2 x float> [[NEG]])
551 ; CHECK-NEXT: [[TMP1:%.*]] = fmul <2 x float> [[X]], [[Y:%.*]]
552 ; CHECK-NEXT: [[R:%.*]] = fadd <2 x float> [[TMP1]], [[Z:%.*]]
553 ; CHECK-NEXT: ret <2 x float> [[R]]
555 %neg = fsub <2 x float> <float -0.0, float -0.0>, %x
556 call void @use_vec(<2 x float> %neg)
557 %mul = fmul <2 x float> %neg, %y
558 %r = fsub <2 x float> %z, %mul
562 define float @fsub_fmul_fneg2_extra_use2(float %x, float %y, float %z) {
563 ; CHECK-LABEL: @fsub_fmul_fneg2_extra_use2(
564 ; CHECK-NEXT: [[NEG:%.*]] = fneg float [[X:%.*]]
565 ; CHECK-NEXT: call void @use(float [[NEG]])
566 ; CHECK-NEXT: [[TMP1:%.*]] = fmul float [[X]], [[Y:%.*]]
567 ; CHECK-NEXT: [[R:%.*]] = fadd float [[TMP1]], [[Z:%.*]]
568 ; CHECK-NEXT: ret float [[R]]
570 %neg = fsub float -0.000000e+00, %x
571 call void @use(float %neg)
572 %mul = fmul float %y, %neg
573 %r = fsub float %z, %mul
577 define float @fsub_fdiv_fneg1_extra_use3(float %x, float %y, float %z) {
578 ; CHECK-LABEL: @fsub_fdiv_fneg1_extra_use3(
579 ; CHECK-NEXT: [[NEG:%.*]] = fneg float [[X:%.*]]
580 ; CHECK-NEXT: call void @use(float [[NEG]])
581 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[NEG]], [[Y:%.*]]
582 ; CHECK-NEXT: call void @use(float [[DIV]])
583 ; CHECK-NEXT: [[R:%.*]] = fsub float [[Z:%.*]], [[DIV]]
584 ; CHECK-NEXT: ret float [[R]]
586 %neg = fsub float -0.000000e+00, %x
587 call void @use(float %neg)
588 %div = fdiv float %neg, %y
589 call void @use(float %div)
590 %r = fsub float %z, %div
594 define float @fsub_fdiv_fneg2_extra_use3(float %x, float %y, float %z) {
595 ; CHECK-LABEL: @fsub_fdiv_fneg2_extra_use3(
596 ; CHECK-NEXT: [[NEG:%.*]] = fneg float [[X:%.*]]
597 ; CHECK-NEXT: call void @use(float [[NEG]])
598 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[Y:%.*]], [[NEG]]
599 ; CHECK-NEXT: call void @use(float [[DIV]])
600 ; CHECK-NEXT: [[R:%.*]] = fsub float [[Z:%.*]], [[DIV]]
601 ; CHECK-NEXT: ret float [[R]]
603 %neg = fsub float -0.000000e+00, %x
604 call void @use(float %neg)
605 %div = fdiv float %y, %neg
606 call void @use(float %div)
607 %r = fsub float %z, %div
611 define <2 x float> @fsub_fmul_fneg1_extra_use3(<2 x float> %x, <2 x float> %y, <2 x float> %z) {
612 ; CHECK-LABEL: @fsub_fmul_fneg1_extra_use3(
613 ; CHECK-NEXT: [[NEG:%.*]] = fneg <2 x float> [[X:%.*]]
614 ; CHECK-NEXT: call void @use_vec(<2 x float> [[NEG]])
615 ; CHECK-NEXT: [[MUL:%.*]] = fmul <2 x float> [[NEG]], [[Y:%.*]]
616 ; CHECK-NEXT: call void @use_vec(<2 x float> [[MUL]])
617 ; CHECK-NEXT: [[R:%.*]] = fsub <2 x float> [[Z:%.*]], [[MUL]]
618 ; CHECK-NEXT: ret <2 x float> [[R]]
620 %neg = fsub <2 x float> <float -0.0, float -0.0>, %x
621 call void @use_vec(<2 x float> %neg)
622 %mul = fmul <2 x float> %neg, %y
623 call void @use_vec(<2 x float> %mul)
624 %r = fsub <2 x float> %z, %mul
628 define float @fsub_fmul_fneg2_extra_use3(float %x, float %y, float %z) {
629 ; CHECK-LABEL: @fsub_fmul_fneg2_extra_use3(
630 ; CHECK-NEXT: [[NEG:%.*]] = fneg float [[X:%.*]]
631 ; CHECK-NEXT: call void @use(float [[NEG]])
632 ; CHECK-NEXT: [[MUL:%.*]] = fmul float [[NEG]], [[Y:%.*]]
633 ; CHECK-NEXT: call void @use(float [[MUL]])
634 ; CHECK-NEXT: [[R:%.*]] = fsub float [[Z:%.*]], [[MUL]]
635 ; CHECK-NEXT: ret float [[R]]
637 %neg = fsub float -0.000000e+00, %x
638 call void @use(float %neg)
639 %mul = fmul float %y, %neg
640 call void @use(float %mul)
641 %r = fsub float %z, %mul
645 ; Negative test - can't reassociate without FMF.
647 define float @fsub_fsub(float %x, float %y, float %z) {
648 ; CHECK-LABEL: @fsub_fsub(
649 ; CHECK-NEXT: [[XY:%.*]] = fsub float [[X:%.*]], [[Y:%.*]]
650 ; CHECK-NEXT: [[XYZ:%.*]] = fsub float [[XY]], [[Z:%.*]]
651 ; CHECK-NEXT: ret float [[XYZ]]
653 %xy = fsub float %x, %y
654 %xyz = fsub float %xy, %z
658 ; Negative test - can't reassociate without enough FMF.
660 define float @fsub_fsub_nsz(float %x, float %y, float %z) {
661 ; CHECK-LABEL: @fsub_fsub_nsz(
662 ; CHECK-NEXT: [[XY:%.*]] = fsub float [[X:%.*]], [[Y:%.*]]
663 ; CHECK-NEXT: [[XYZ:%.*]] = fsub nsz float [[XY]], [[Z:%.*]]
664 ; CHECK-NEXT: ret float [[XYZ]]
666 %xy = fsub float %x, %y
667 %xyz = fsub nsz float %xy, %z
671 ; Negative test - can't reassociate without enough FMF.
673 define float @fsub_fsub_reassoc(float %x, float %y, float %z) {
674 ; CHECK-LABEL: @fsub_fsub_reassoc(
675 ; CHECK-NEXT: [[XY:%.*]] = fsub float [[X:%.*]], [[Y:%.*]]
676 ; CHECK-NEXT: [[XYZ:%.*]] = fsub reassoc float [[XY]], [[Z:%.*]]
677 ; CHECK-NEXT: ret float [[XYZ]]
679 %xy = fsub float %x, %y
680 %xyz = fsub reassoc float %xy, %z
684 define float @fsub_fsub_nsz_reassoc(float %x, float %y, float %z) {
685 ; CHECK-LABEL: @fsub_fsub_nsz_reassoc(
686 ; CHECK-NEXT: [[TMP1:%.*]] = fadd reassoc nsz float [[Y:%.*]], [[Z:%.*]]
687 ; CHECK-NEXT: [[XYZ:%.*]] = fsub reassoc nsz float [[X:%.*]], [[TMP1]]
688 ; CHECK-NEXT: ret float [[XYZ]]
690 %xy = fsub float %x, %y
691 %xyz = fsub nsz reassoc float %xy, %z
695 define <2 x double> @fsub_fsub_fast_vec(<2 x double> %x, <2 x double> %y, <2 x double> %z) {
696 ; CHECK-LABEL: @fsub_fsub_fast_vec(
697 ; CHECK-NEXT: [[TMP1:%.*]] = fadd fast <2 x double> [[Y:%.*]], [[Z:%.*]]
698 ; CHECK-NEXT: [[XYZ:%.*]] = fsub fast <2 x double> [[X:%.*]], [[TMP1]]
699 ; CHECK-NEXT: ret <2 x double> [[XYZ]]
701 %xy = fsub fast <2 x double> %x, %y
702 %xyz = fsub fast reassoc <2 x double> %xy, %z
703 ret <2 x double> %xyz
706 ; Negative test - don't reassociate and increase instructions.
708 define float @fsub_fsub_nsz_reassoc_extra_use(float %x, float %y, float %z) {
709 ; CHECK-LABEL: @fsub_fsub_nsz_reassoc_extra_use(
710 ; CHECK-NEXT: [[XY:%.*]] = fsub float [[X:%.*]], [[Y:%.*]]
711 ; CHECK-NEXT: call void @use(float [[XY]])
712 ; CHECK-NEXT: [[XYZ:%.*]] = fsub reassoc nsz float [[XY]], [[Z:%.*]]
713 ; CHECK-NEXT: ret float [[XYZ]]
715 %xy = fsub float %x, %y
716 call void @use(float %xy)
717 %xyz = fsub nsz reassoc float %xy, %z
721 define float @fneg_fsub(float %x, float %y) {
722 ; CHECK-LABEL: @fneg_fsub(
723 ; CHECK-NEXT: [[NEGX:%.*]] = fneg float [[X:%.*]]
724 ; CHECK-NEXT: [[SUB:%.*]] = fsub float [[NEGX]], [[Y:%.*]]
725 ; CHECK-NEXT: ret float [[SUB]]
727 %negx = fneg float %x
728 %sub = fsub float %negx, %y
732 define float @fneg_fsub_nsz(float %x, float %y) {
733 ; CHECK-LABEL: @fneg_fsub_nsz(
734 ; CHECK-NEXT: [[TMP1:%.*]] = fadd nsz float [[X:%.*]], [[Y:%.*]]
735 ; CHECK-NEXT: [[SUB:%.*]] = fneg nsz float [[TMP1]]
736 ; CHECK-NEXT: ret float [[SUB]]
738 %negx = fneg float %x
739 %sub = fsub nsz float %negx, %y
743 define float @fake_fneg_fsub_fast(float %x, float %y) {
744 ; CHECK-LABEL: @fake_fneg_fsub_fast(
745 ; CHECK-NEXT: [[TMP1:%.*]] = fadd fast float [[X:%.*]], [[Y:%.*]]
746 ; CHECK-NEXT: [[SUB:%.*]] = fneg fast float [[TMP1]]
747 ; CHECK-NEXT: ret float [[SUB]]
749 %negx = fsub float -0.0, %x
750 %sub = fsub fast float %negx, %y
754 define float @fake_fneg_fsub_fast_extra_use(float %x, float %y) {
755 ; CHECK-LABEL: @fake_fneg_fsub_fast_extra_use(
756 ; CHECK-NEXT: [[NEGX:%.*]] = fneg float [[X:%.*]]
757 ; CHECK-NEXT: call void @use(float [[NEGX]])
758 ; CHECK-NEXT: [[SUB:%.*]] = fsub fast float [[NEGX]], [[Y:%.*]]
759 ; CHECK-NEXT: ret float [[SUB]]
761 %negx = fsub float -0.0, %x
762 call void @use(float %negx)
763 %sub = fsub fast float %negx, %y
767 define <2 x float> @fake_fneg_fsub_vec(<2 x float> %x, <2 x float> %y) {
768 ; CHECK-LABEL: @fake_fneg_fsub_vec(
769 ; CHECK-NEXT: [[TMP1:%.*]] = fadd nsz <2 x float> [[X:%.*]], [[Y:%.*]]
770 ; CHECK-NEXT: [[SUB:%.*]] = fneg nsz <2 x float> [[TMP1]]
771 ; CHECK-NEXT: ret <2 x float> [[SUB]]
773 %negx = fsub <2 x float> <float -0.0, float -0.0>, %x
774 %sub = fsub nsz <2 x float> %negx, %y
778 define float @fneg_fsub_constant(float %x) {
779 ; CHECK-LABEL: @fneg_fsub_constant(
780 ; CHECK-NEXT: [[SUB:%.*]] = fsub nsz float -4.200000e+01, [[X:%.*]]
781 ; CHECK-NEXT: ret float [[SUB]]
783 %negx = fneg float %x
784 %sub = fsub nsz float %negx, 42.0
788 ; ((w-x) + y) - z --> (w+y) - (x+z)
790 define float @fsub_fadd_fsub_reassoc(float %w, float %x, float %y, float %z) {
791 ; CHECK-LABEL: @fsub_fadd_fsub_reassoc(
792 ; CHECK-NEXT: [[TMP1:%.*]] = fadd reassoc nsz float [[W:%.*]], [[Y:%.*]]
793 ; CHECK-NEXT: [[TMP2:%.*]] = fadd reassoc nsz float [[X:%.*]], [[Z:%.*]]
794 ; CHECK-NEXT: [[S2:%.*]] = fsub reassoc nsz float [[TMP1]], [[TMP2]]
795 ; CHECK-NEXT: ret float [[S2]]
797 %s1 = fsub reassoc nsz float %w, %x
798 %a = fadd reassoc nsz float %s1, %y
799 %s2 = fsub reassoc nsz float %a, %z
803 ; FMF on the last op is enough to do the transform; vectors work too.
805 define <2 x float> @fsub_fadd_fsub_reassoc_commute(<2 x float> %w, <2 x float> %x, <2 x float> %y, <2 x float> %z) {
806 ; CHECK-LABEL: @fsub_fadd_fsub_reassoc_commute(
807 ; CHECK-NEXT: [[D:%.*]] = fdiv <2 x float> [[Y:%.*]], <float 4.200000e+01, float -4.200000e+01>
808 ; CHECK-NEXT: [[TMP1:%.*]] = fadd fast <2 x float> [[D]], [[W:%.*]]
809 ; CHECK-NEXT: [[TMP2:%.*]] = fadd fast <2 x float> [[X:%.*]], [[Z:%.*]]
810 ; CHECK-NEXT: [[S2:%.*]] = fsub fast <2 x float> [[TMP1]], [[TMP2]]
811 ; CHECK-NEXT: ret <2 x float> [[S2]]
813 %d = fdiv <2 x float> %y, <float 42.0, float -42.0> ; thwart complexity-based canonicalization
814 %s1 = fsub <2 x float> %w, %x
815 %a = fadd <2 x float> %d, %s1
816 %s2 = fsub fast <2 x float> %a, %z
820 ; (v-w) + (x-y) - z --> (v+x) - (w+y+z)
822 define float @fsub_fadd_fsub_reassoc_twice(float %v, float %w, float %x, float %y, float %z) {
823 ; CHECK-LABEL: @fsub_fadd_fsub_reassoc_twice(
824 ; CHECK-NEXT: [[TMP1:%.*]] = fadd reassoc nsz float [[W:%.*]], [[Z:%.*]]
825 ; CHECK-NEXT: [[TMP2:%.*]] = fadd reassoc nsz float [[X:%.*]], [[V:%.*]]
826 ; CHECK-NEXT: [[TMP3:%.*]] = fadd reassoc nsz float [[TMP1]], [[Y:%.*]]
827 ; CHECK-NEXT: [[S3:%.*]] = fsub reassoc nsz float [[TMP2]], [[TMP3]]
828 ; CHECK-NEXT: ret float [[S3]]
830 %s1 = fsub reassoc nsz float %v, %w
831 %s2 = fsub reassoc nsz float %x, %y
832 %a = fadd reassoc nsz float %s1, %s2
833 %s3 = fsub reassoc nsz float %a, %z
837 ; negative test - FMF
839 define float @fsub_fadd_fsub_not_reassoc(float %w, float %x, float %y, float %z) {
840 ; CHECK-LABEL: @fsub_fadd_fsub_not_reassoc(
841 ; CHECK-NEXT: [[S1:%.*]] = fsub fast float [[W:%.*]], [[X:%.*]]
842 ; CHECK-NEXT: [[A:%.*]] = fadd fast float [[S1]], [[Y:%.*]]
843 ; CHECK-NEXT: [[S2:%.*]] = fsub nsz float [[A]], [[Z:%.*]]
844 ; CHECK-NEXT: ret float [[S2]]
846 %s1 = fsub fast float %w, %x
847 %a = fadd fast float %s1, %y
848 %s2 = fsub nsz float %a, %z
852 ; negative test - uses
854 define float @fsub_fadd_fsub_reassoc_use1(float %w, float %x, float %y, float %z) {
855 ; CHECK-LABEL: @fsub_fadd_fsub_reassoc_use1(
856 ; CHECK-NEXT: [[S1:%.*]] = fsub fast float [[W:%.*]], [[X:%.*]]
857 ; CHECK-NEXT: call void @use(float [[S1]])
858 ; CHECK-NEXT: [[A:%.*]] = fadd fast float [[S1]], [[Y:%.*]]
859 ; CHECK-NEXT: [[S2:%.*]] = fsub fast float [[A]], [[Z:%.*]]
860 ; CHECK-NEXT: ret float [[S2]]
862 %s1 = fsub fast float %w, %x
863 call void @use(float %s1)
864 %a = fadd fast float %s1, %y
865 %s2 = fsub fast float %a, %z
869 ; negative test - uses
871 define float @fsub_fadd_fsub_reassoc_use2(float %w, float %x, float %y, float %z) {
872 ; CHECK-LABEL: @fsub_fadd_fsub_reassoc_use2(
873 ; CHECK-NEXT: [[S1:%.*]] = fsub fast float [[W:%.*]], [[X:%.*]]
874 ; CHECK-NEXT: [[A:%.*]] = fadd fast float [[S1]], [[Y:%.*]]
875 ; CHECK-NEXT: call void @use(float [[A]])
876 ; CHECK-NEXT: [[S2:%.*]] = fsub fast float [[A]], [[Z:%.*]]
877 ; CHECK-NEXT: ret float [[S2]]
879 %s1 = fsub fast float %w, %x
880 %a = fadd fast float %s1, %y
881 call void @use(float %a)
882 %s2 = fsub fast float %a, %z
886 define float @fmul_c1(float %x, float %y) {
887 ; CHECK-LABEL: @fmul_c1(
888 ; CHECK-NEXT: [[M:%.*]] = fmul float [[X:%.*]], 4.200000e+01
889 ; CHECK-NEXT: [[R:%.*]] = fsub float [[Y:%.*]], [[M]]
890 ; CHECK-NEXT: ret float [[R]]
892 %m = fmul float %x, 42.0
893 %r = fsub float %y, %m
897 define <2 x float> @fmul_c1_fmf(<2 x float> %x, <2 x float> %y) {
898 ; CHECK-LABEL: @fmul_c1_fmf(
899 ; CHECK-NEXT: [[M:%.*]] = fmul ninf nsz <2 x float> [[X:%.*]], <float 4.200000e+01, float 5.000000e-01>
900 ; CHECK-NEXT: [[R:%.*]] = fsub <2 x float> [[Y:%.*]], [[M]]
901 ; CHECK-NEXT: ret <2 x float> [[R]]
903 %m = fmul ninf nsz <2 x float> %x, <float 42.0, float 0.5>
904 %r = fsub <2 x float> %y, %m
908 define float @fmul_c1_use(float %x, float %y) {
909 ; CHECK-LABEL: @fmul_c1_use(
910 ; CHECK-NEXT: [[M:%.*]] = fmul float [[X:%.*]], 4.200000e+01
911 ; CHECK-NEXT: call void @use(float [[M]])
912 ; CHECK-NEXT: [[R:%.*]] = fsub float [[Y:%.*]], [[M]]
913 ; CHECK-NEXT: ret float [[R]]
915 %m = fmul float %x, 42.0
916 call void @use(float %m)
917 %r = fsub float %y, %m
921 define half @fdiv_c0(half %x, half %y) {
922 ; CHECK-LABEL: @fdiv_c0(
923 ; CHECK-NEXT: [[M:%.*]] = fdiv half 0xH4700, [[X:%.*]]
924 ; CHECK-NEXT: [[R:%.*]] = fsub half [[Y:%.*]], [[M]]
925 ; CHECK-NEXT: ret half [[R]]
927 %m = fdiv half 7.0, %x
928 %r = fsub half %y, %m
932 define double @fdiv_c0_fmf(double %x, double %y) {
933 ; CHECK-LABEL: @fdiv_c0_fmf(
934 ; CHECK-NEXT: [[M:%.*]] = fdiv double 7.000000e+00, [[X:%.*]]
935 ; CHECK-NEXT: [[R:%.*]] = fsub reassoc nnan double [[Y:%.*]], [[M]]
936 ; CHECK-NEXT: ret double [[R]]
938 %m = fdiv double 7.0, %x
939 %r = fsub reassoc nnan double %y, %m
943 define <2 x float> @fdiv_c1(<2 x float> %x, <2 x float> %y) {
944 ; CHECK-LABEL: @fdiv_c1(
945 ; CHECK-NEXT: [[M:%.*]] = fdiv <2 x float> [[X:%.*]], <float 4.270000e+02, float -0.000000e+00>
946 ; CHECK-NEXT: [[R:%.*]] = fsub <2 x float> [[Y:%.*]], [[M]]
947 ; CHECK-NEXT: ret <2 x float> [[R]]
949 %m = fdiv <2 x float> %x, <float 427.0, float -0.0>
950 %r = fsub <2 x float> %y, %m
954 define float @fdiv_c1_fmf(float %x, float %y) {
955 ; CHECK-LABEL: @fdiv_c1_fmf(
956 ; CHECK-NEXT: [[M:%.*]] = fdiv nnan float [[X:%.*]], 4.270000e+02
957 ; CHECK-NEXT: [[R:%.*]] = fsub reassoc float [[Y:%.*]], [[M]]
958 ; CHECK-NEXT: ret float [[R]]
960 %m = fdiv nnan float %x, 427.0
961 %r = fsub reassoc float %y, %m
965 define <2 x float> @fdiv_c1_use(<2 x float> %x, <2 x float> %y) {
966 ; CHECK-LABEL: @fdiv_c1_use(
967 ; CHECK-NEXT: [[M:%.*]] = fdiv <2 x float> [[X:%.*]], <float 4.270000e+02, float -0.000000e+00>
968 ; CHECK-NEXT: call void @use_vec(<2 x float> [[M]])
969 ; CHECK-NEXT: [[R:%.*]] = fsub <2 x float> [[Y:%.*]], [[M]]
970 ; CHECK-NEXT: ret <2 x float> [[R]]
972 %m = fdiv <2 x float> %x, <float 427.0, float -0.0>
973 call void @use_vec(<2 x float> %m)
974 %r = fsub <2 x float> %y, %m