1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -passes=instcombine < %s | FileCheck %s
4 ; (-0.0 - X) * C => X * -C
5 define float @neg_constant(float %x) {
6 ; CHECK-LABEL: @neg_constant(
7 ; CHECK-NEXT: [[MUL:%.*]] = fmul ninf float [[X:%.*]], -2.000000e+01
8 ; CHECK-NEXT: ret float [[MUL]]
10 %sub = fsub float -0.0, %x
11 %mul = fmul ninf float %sub, 2.0e+1
15 define float @unary_neg_constant(float %x) {
16 ; CHECK-LABEL: @unary_neg_constant(
17 ; CHECK-NEXT: [[MUL:%.*]] = fmul ninf float [[X:%.*]], -2.000000e+01
18 ; CHECK-NEXT: ret float [[MUL]]
21 %mul = fmul ninf float %sub, 2.0e+1
25 define <2 x float> @neg_constant_vec(<2 x float> %x) {
26 ; CHECK-LABEL: @neg_constant_vec(
27 ; CHECK-NEXT: [[MUL:%.*]] = fmul ninf <2 x float> [[X:%.*]], <float -2.000000e+00, float -3.000000e+00>
28 ; CHECK-NEXT: ret <2 x float> [[MUL]]
30 %sub = fsub <2 x float> <float -0.0, float -0.0>, %x
31 %mul = fmul ninf <2 x float> %sub, <float 2.0, float 3.0>
35 define <2 x float> @unary_neg_constant_vec(<2 x float> %x) {
36 ; CHECK-LABEL: @unary_neg_constant_vec(
37 ; CHECK-NEXT: [[MUL:%.*]] = fmul ninf <2 x float> [[X:%.*]], <float -2.000000e+00, float -3.000000e+00>
38 ; CHECK-NEXT: ret <2 x float> [[MUL]]
40 %sub = fneg <2 x float> %x
41 %mul = fmul ninf <2 x float> %sub, <float 2.0, float 3.0>
45 define <2 x float> @neg_constant_vec_poison(<2 x float> %x) {
46 ; CHECK-LABEL: @neg_constant_vec_poison(
47 ; CHECK-NEXT: [[MUL:%.*]] = fmul ninf <2 x float> [[X:%.*]], <float -2.000000e+00, float -3.000000e+00>
48 ; CHECK-NEXT: ret <2 x float> [[MUL]]
50 %sub = fsub <2 x float> <float poison, float -0.0>, %x
51 %mul = fmul ninf <2 x float> %sub, <float 2.0, float 3.0>
55 ; (0.0 - X) * C => X * -C
56 define float @neg_nsz_constant(float %x) {
57 ; CHECK-LABEL: @neg_nsz_constant(
58 ; CHECK-NEXT: [[MUL:%.*]] = fmul nnan float [[X:%.*]], -2.000000e+01
59 ; CHECK-NEXT: ret float [[MUL]]
61 %sub = fsub nsz float 0.0, %x
62 %mul = fmul nnan float %sub, 2.0e+1
66 define float @unary_neg_nsz_constant(float %x) {
67 ; CHECK-LABEL: @unary_neg_nsz_constant(
68 ; CHECK-NEXT: [[MUL:%.*]] = fmul nnan float [[X:%.*]], -2.000000e+01
69 ; CHECK-NEXT: ret float [[MUL]]
71 %sub = fneg nsz float %x
72 %mul = fmul nnan float %sub, 2.0e+1
76 ; (-0.0 - X) * (-0.0 - Y) => X * Y
77 define float @neg_neg(float %x, float %y) {
78 ; CHECK-LABEL: @neg_neg(
79 ; CHECK-NEXT: [[MUL:%.*]] = fmul arcp float [[X:%.*]], [[Y:%.*]]
80 ; CHECK-NEXT: ret float [[MUL]]
82 %sub1 = fsub float -0.0, %x
83 %sub2 = fsub float -0.0, %y
84 %mul = fmul arcp float %sub1, %sub2
88 define float @unary_neg_unary_neg(float %x, float %y) {
89 ; CHECK-LABEL: @unary_neg_unary_neg(
90 ; CHECK-NEXT: [[MUL:%.*]] = fmul arcp float [[X:%.*]], [[Y:%.*]]
91 ; CHECK-NEXT: ret float [[MUL]]
95 %mul = fmul arcp float %sub1, %sub2
99 define float @unary_neg_neg(float %x, float %y) {
100 ; CHECK-LABEL: @unary_neg_neg(
101 ; CHECK-NEXT: [[MUL:%.*]] = fmul arcp float [[X:%.*]], [[Y:%.*]]
102 ; CHECK-NEXT: ret float [[MUL]]
104 %sub1 = fneg float %x
105 %sub2 = fsub float -0.0, %y
106 %mul = fmul arcp float %sub1, %sub2
110 define float @neg_unary_neg(float %x, float %y) {
111 ; CHECK-LABEL: @neg_unary_neg(
112 ; CHECK-NEXT: [[MUL:%.*]] = fmul arcp float [[X:%.*]], [[Y:%.*]]
113 ; CHECK-NEXT: ret float [[MUL]]
115 %sub1 = fsub float -0.0, %x
116 %sub2 = fneg float %y
117 %mul = fmul arcp float %sub1, %sub2
121 define <2 x float> @neg_neg_vec(<2 x float> %x, <2 x float> %y) {
122 ; CHECK-LABEL: @neg_neg_vec(
123 ; CHECK-NEXT: [[MUL:%.*]] = fmul arcp <2 x float> [[X:%.*]], [[Y:%.*]]
124 ; CHECK-NEXT: ret <2 x float> [[MUL]]
126 %sub1 = fsub <2 x float> <float -0.0, float -0.0>, %x
127 %sub2 = fsub <2 x float> <float -0.0, float -0.0>, %y
128 %mul = fmul arcp <2 x float> %sub1, %sub2
132 define <2 x float> @unary_neg_unary_neg_vec(<2 x float> %x, <2 x float> %y) {
133 ; CHECK-LABEL: @unary_neg_unary_neg_vec(
134 ; CHECK-NEXT: [[MUL:%.*]] = fmul arcp <2 x float> [[X:%.*]], [[Y:%.*]]
135 ; CHECK-NEXT: ret <2 x float> [[MUL]]
137 %sub1 = fneg <2 x float> %x
138 %sub2 = fneg <2 x float> %y
139 %mul = fmul arcp <2 x float> %sub1, %sub2
143 define <2 x float> @unary_neg_neg_vec(<2 x float> %x, <2 x float> %y) {
144 ; CHECK-LABEL: @unary_neg_neg_vec(
145 ; CHECK-NEXT: [[MUL:%.*]] = fmul arcp <2 x float> [[X:%.*]], [[Y:%.*]]
146 ; CHECK-NEXT: ret <2 x float> [[MUL]]
148 %sub1 = fneg <2 x float> %x
149 %sub2 = fsub <2 x float> <float -0.0, float -0.0>, %y
150 %mul = fmul arcp <2 x float> %sub1, %sub2
154 define <2 x float> @neg_unary_neg_vec(<2 x float> %x, <2 x float> %y) {
155 ; CHECK-LABEL: @neg_unary_neg_vec(
156 ; CHECK-NEXT: [[MUL:%.*]] = fmul arcp <2 x float> [[X:%.*]], [[Y:%.*]]
157 ; CHECK-NEXT: ret <2 x float> [[MUL]]
159 %sub1 = fsub <2 x float> <float -0.0, float -0.0>, %x
160 %sub2 = fneg <2 x float> %y
161 %mul = fmul arcp <2 x float> %sub1, %sub2
165 define <2 x float> @neg_neg_vec_poison(<2 x float> %x, <2 x float> %y) {
166 ; CHECK-LABEL: @neg_neg_vec_poison(
167 ; CHECK-NEXT: [[MUL:%.*]] = fmul arcp <2 x float> [[X:%.*]], [[Y:%.*]]
168 ; CHECK-NEXT: ret <2 x float> [[MUL]]
170 %sub1 = fsub <2 x float> <float -0.0, float poison>, %x
171 %sub2 = fsub <2 x float> <float poison, float -0.0>, %y
172 %mul = fmul arcp <2 x float> %sub1, %sub2
176 define <2 x float> @unary_neg_neg_vec_poison(<2 x float> %x, <2 x float> %y) {
177 ; CHECK-LABEL: @unary_neg_neg_vec_poison(
178 ; CHECK-NEXT: [[MUL:%.*]] = fmul arcp <2 x float> [[X:%.*]], [[Y:%.*]]
179 ; CHECK-NEXT: ret <2 x float> [[MUL]]
181 %neg = fneg <2 x float> %x
182 %sub = fsub <2 x float> <float poison, float -0.0>, %y
183 %mul = fmul arcp <2 x float> %neg, %sub
187 define <2 x float> @neg_unary_neg_vec_poison(<2 x float> %x, <2 x float> %y) {
188 ; CHECK-LABEL: @neg_unary_neg_vec_poison(
189 ; CHECK-NEXT: [[MUL:%.*]] = fmul arcp <2 x float> [[X:%.*]], [[Y:%.*]]
190 ; CHECK-NEXT: ret <2 x float> [[MUL]]
192 %sub = fsub <2 x float> <float -0.0, float poison>, %x
193 %neg = fneg <2 x float> %y
194 %mul = fmul arcp <2 x float> %sub, %neg
198 ; (0.0 - X) * (0.0 - Y) => X * Y
199 define float @neg_neg_nsz(float %x, float %y) {
200 ; CHECK-LABEL: @neg_neg_nsz(
201 ; CHECK-NEXT: [[MUL:%.*]] = fmul afn float [[X:%.*]], [[Y:%.*]]
202 ; CHECK-NEXT: ret float [[MUL]]
204 %sub1 = fsub nsz float 0.0, %x
205 %sub2 = fsub nsz float 0.0, %y
206 %mul = fmul afn float %sub1, %sub2
210 declare void @use_f32(float)
212 define float @neg_neg_multi_use(float %x, float %y) {
213 ; CHECK-LABEL: @neg_neg_multi_use(
214 ; CHECK-NEXT: [[NX:%.*]] = fneg float [[X:%.*]]
215 ; CHECK-NEXT: [[NY:%.*]] = fneg float [[Y:%.*]]
216 ; CHECK-NEXT: [[MUL:%.*]] = fmul afn float [[X]], [[Y]]
217 ; CHECK-NEXT: call void @use_f32(float [[NX]])
218 ; CHECK-NEXT: call void @use_f32(float [[NY]])
219 ; CHECK-NEXT: ret float [[MUL]]
221 %nx = fsub float -0.0, %x
222 %ny = fsub float -0.0, %y
223 %mul = fmul afn float %nx, %ny
224 call void @use_f32(float %nx)
225 call void @use_f32(float %ny)
229 define float @unary_neg_unary_neg_multi_use(float %x, float %y) {
230 ; CHECK-LABEL: @unary_neg_unary_neg_multi_use(
231 ; CHECK-NEXT: [[NX:%.*]] = fneg float [[X:%.*]]
232 ; CHECK-NEXT: [[NY:%.*]] = fneg float [[Y:%.*]]
233 ; CHECK-NEXT: [[MUL:%.*]] = fmul afn float [[X]], [[Y]]
234 ; CHECK-NEXT: call void @use_f32(float [[NX]])
235 ; CHECK-NEXT: call void @use_f32(float [[NY]])
236 ; CHECK-NEXT: ret float [[MUL]]
240 %mul = fmul afn float %nx, %ny
241 call void @use_f32(float %nx)
242 call void @use_f32(float %ny)
246 define float @unary_neg_neg_multi_use(float %x, float %y) {
247 ; CHECK-LABEL: @unary_neg_neg_multi_use(
248 ; CHECK-NEXT: [[NX:%.*]] = fneg float [[X:%.*]]
249 ; CHECK-NEXT: [[NY:%.*]] = fneg float [[Y:%.*]]
250 ; CHECK-NEXT: [[MUL:%.*]] = fmul afn float [[X]], [[Y]]
251 ; CHECK-NEXT: call void @use_f32(float [[NX]])
252 ; CHECK-NEXT: call void @use_f32(float [[NY]])
253 ; CHECK-NEXT: ret float [[MUL]]
256 %ny = fsub float -0.0, %y
257 %mul = fmul afn float %nx, %ny
258 call void @use_f32(float %nx)
259 call void @use_f32(float %ny)
263 define float @neg_unary_neg_multi_use(float %x, float %y) {
264 ; CHECK-LABEL: @neg_unary_neg_multi_use(
265 ; CHECK-NEXT: [[NX:%.*]] = fneg float [[X:%.*]]
266 ; CHECK-NEXT: [[NY:%.*]] = fneg float [[Y:%.*]]
267 ; CHECK-NEXT: [[MUL:%.*]] = fmul afn float [[X]], [[Y]]
268 ; CHECK-NEXT: call void @use_f32(float [[NX]])
269 ; CHECK-NEXT: call void @use_f32(float [[NY]])
270 ; CHECK-NEXT: ret float [[MUL]]
272 %nx = fsub float -0.0, %x
274 %mul = fmul afn float %nx, %ny
275 call void @use_f32(float %nx)
276 call void @use_f32(float %ny)
281 define float @neg_mul(float %x, float %y) {
282 ; CHECK-LABEL: @neg_mul(
283 ; CHECK-NEXT: [[SUB:%.*]] = fneg float [[X:%.*]]
284 ; CHECK-NEXT: [[MUL:%.*]] = fmul float [[Y:%.*]], [[SUB]]
285 ; CHECK-NEXT: ret float [[MUL]]
287 %sub = fsub float -0.0, %x
288 %mul = fmul float %sub, %y
292 define float @unary_neg_mul(float %x, float %y) {
293 ; CHECK-LABEL: @unary_neg_mul(
294 ; CHECK-NEXT: [[NEG:%.*]] = fneg float [[X:%.*]]
295 ; CHECK-NEXT: [[MUL:%.*]] = fmul float [[Y:%.*]], [[NEG]]
296 ; CHECK-NEXT: ret float [[MUL]]
299 %mul = fmul float %neg, %y
303 define <2 x float> @neg_mul_vec(<2 x float> %x, <2 x float> %y) {
304 ; CHECK-LABEL: @neg_mul_vec(
305 ; CHECK-NEXT: [[SUB:%.*]] = fneg <2 x float> [[X:%.*]]
306 ; CHECK-NEXT: [[MUL:%.*]] = fmul <2 x float> [[Y:%.*]], [[SUB]]
307 ; CHECK-NEXT: ret <2 x float> [[MUL]]
309 %sub = fsub <2 x float> <float -0.0, float -0.0>, %x
310 %mul = fmul <2 x float> %sub, %y
314 define <2 x float> @unary_neg_mul_vec(<2 x float> %x, <2 x float> %y) {
315 ; CHECK-LABEL: @unary_neg_mul_vec(
316 ; CHECK-NEXT: [[SUB:%.*]] = fneg <2 x float> [[X:%.*]]
317 ; CHECK-NEXT: [[MUL:%.*]] = fmul <2 x float> [[Y:%.*]], [[SUB]]
318 ; CHECK-NEXT: ret <2 x float> [[MUL]]
320 %sub = fneg <2 x float> %x
321 %mul = fmul <2 x float> %sub, %y
325 define <2 x float> @neg_mul_vec_poison(<2 x float> %x, <2 x float> %y) {
326 ; CHECK-LABEL: @neg_mul_vec_poison(
327 ; CHECK-NEXT: [[SUB:%.*]] = fneg <2 x float> [[X:%.*]]
328 ; CHECK-NEXT: [[MUL:%.*]] = fmul <2 x float> [[Y:%.*]], [[SUB]]
329 ; CHECK-NEXT: ret <2 x float> [[MUL]]
331 %sub = fsub <2 x float> <float poison, float -0.0>, %x
332 %mul = fmul <2 x float> %sub, %y
337 define float @neg_sink_nsz(float %x, float %y) {
338 ; CHECK-LABEL: @neg_sink_nsz(
339 ; CHECK-NEXT: [[SUB1:%.*]] = fneg nsz float [[X:%.*]]
340 ; CHECK-NEXT: [[MUL:%.*]] = fmul float [[Y:%.*]], [[SUB1]]
341 ; CHECK-NEXT: ret float [[MUL]]
343 %sub1 = fsub nsz float 0.0, %x
344 %mul = fmul float %sub1, %y
348 define float @neg_sink_multi_use(float %x, float %y) {
349 ; CHECK-LABEL: @neg_sink_multi_use(
350 ; CHECK-NEXT: [[SUB1:%.*]] = fneg float [[X:%.*]]
351 ; CHECK-NEXT: [[MUL:%.*]] = fmul float [[Y:%.*]], [[SUB1]]
352 ; CHECK-NEXT: [[MUL2:%.*]] = fmul float [[MUL]], [[SUB1]]
353 ; CHECK-NEXT: ret float [[MUL2]]
355 %sub1 = fsub float -0.0, %x
356 %mul = fmul float %sub1, %y
357 %mul2 = fmul float %mul, %sub1
361 define float @unary_neg_mul_multi_use(float %x, float %y) {
362 ; CHECK-LABEL: @unary_neg_mul_multi_use(
363 ; CHECK-NEXT: [[SUB1:%.*]] = fneg float [[X:%.*]]
364 ; CHECK-NEXT: [[MUL:%.*]] = fmul float [[Y:%.*]], [[SUB1]]
365 ; CHECK-NEXT: [[MUL2:%.*]] = fmul float [[MUL]], [[SUB1]]
366 ; CHECK-NEXT: ret float [[MUL2]]
368 %sub1 = fneg float %x
369 %mul = fmul float %sub1, %y
370 %mul2 = fmul float %mul, %sub1
374 ; Don't crash when attempting to cast a constant FMul to an instruction.
375 define void @test8(ptr %inout, i1 %c1) {
376 ; CHECK-LABEL: @test8(
378 ; CHECK-NEXT: br label [[FOR_COND:%.*]]
380 ; CHECK-NEXT: [[LOCAL_VAR_7_0:%.*]] = phi <4 x float> [ splat (float -0.000000e+00), [[ENTRY:%.*]] ], [ [[TMP0:%.*]], [[FOR_BODY:%.*]] ]
381 ; CHECK-NEXT: br i1 [[C1:%.*]], label [[FOR_BODY]], label [[FOR_END:%.*]]
383 ; CHECK-NEXT: [[TMP0]] = insertelement <4 x float> [[LOCAL_VAR_7_0]], float 0.000000e+00, i64 2
384 ; CHECK-NEXT: br label [[FOR_COND]]
386 ; CHECK-NEXT: ret void
389 %0 = load i32, ptr %inout, align 4
390 %conv = uitofp i32 %0 to float
391 %vecinit = insertelement <4 x float> <float 0.000000e+00, float 0.000000e+00, float 0.000000e+00, float poison>, float %conv, i32 3
392 %sub = fsub <4 x float> <float -0.000000e+00, float -0.000000e+00, float -0.000000e+00, float -0.000000e+00>, %vecinit
393 %1 = shufflevector <4 x float> %sub, <4 x float> poison, <4 x i32> <i32 1, i32 1, i32 1, i32 1>
394 %mul = fmul <4 x float> zeroinitializer, %1
397 for.cond: ; preds = %for.body, %entry
398 %local_var_7.0 = phi <4 x float> [ %mul, %entry ], [ %2, %for.body ]
399 br i1 %c1, label %for.body, label %for.end
401 for.body: ; preds = %for.cond
402 %2 = insertelement <4 x float> %local_var_7.0, float 0.000000e+00, i32 2
405 for.end: ; preds = %for.cond
409 ; X * -1.0 => -0.0 - X
410 define float @test9(float %x) {
411 ; CHECK-LABEL: @test9(
412 ; CHECK-NEXT: [[MUL:%.*]] = fneg float [[X:%.*]]
413 ; CHECK-NEXT: ret float [[MUL]]
415 %mul = fmul float %x, -1.0
420 define <4 x float> @test10(<4 x float> %x) {
421 ; CHECK-LABEL: @test10(
422 ; CHECK-NEXT: [[MUL:%.*]] = fneg arcp afn <4 x float> [[X:%.*]]
423 ; CHECK-NEXT: ret <4 x float> [[MUL]]
425 %mul = fmul arcp afn <4 x float> %x, <float -1.0, float -1.0, float -1.0, float -1.0>
429 define float @test11(float %x, float %y) {
430 ; CHECK-LABEL: @test11(
431 ; CHECK-NEXT: [[B:%.*]] = fadd fast float [[X:%.*]], [[Y:%.*]]
432 ; CHECK-NEXT: [[C:%.*]] = fadd fast float [[B]], 3.000000e+00
433 ; CHECK-NEXT: ret float [[C]]
435 %a = fadd fast float %x, 1.0
436 %b = fadd fast float %y, 2.0
437 %c = fadd fast float %a, %b
441 declare double @llvm.sqrt.f64(double)
443 ; With unsafe/fast math, sqrt(X) * sqrt(X) is just X,
444 ; but make sure another use of the sqrt is intact.
445 ; Note that the remaining fmul is altered but is not 'fast'
446 ; itself because it was not marked 'fast' originally.
447 ; Thus, we have an overall fast result, but no more indication of
448 ; 'fast'ness in the code.
449 define double @sqrt_squared2(double %f) {
450 ; CHECK-LABEL: @sqrt_squared2(
451 ; CHECK-NEXT: [[SQRT:%.*]] = call double @llvm.sqrt.f64(double [[F:%.*]])
452 ; CHECK-NEXT: [[MUL2:%.*]] = fmul double [[F]], [[SQRT]]
453 ; CHECK-NEXT: ret double [[MUL2]]
455 %sqrt = call double @llvm.sqrt.f64(double %f)
456 %mul1 = fmul fast double %sqrt, %sqrt
457 %mul2 = fmul double %mul1, %sqrt
461 declare float @llvm.fabs.f32(float) nounwind readnone
463 define float @fabs_squared(float %x) {
464 ; CHECK-LABEL: @fabs_squared(
465 ; CHECK-NEXT: [[MUL:%.*]] = fmul float [[X:%.*]], [[X]]
466 ; CHECK-NEXT: ret float [[MUL]]
468 %x.fabs = call float @llvm.fabs.f32(float %x)
469 %mul = fmul float %x.fabs, %x.fabs
473 define float @fabs_squared_fast(float %x) {
474 ; CHECK-LABEL: @fabs_squared_fast(
475 ; CHECK-NEXT: [[MUL:%.*]] = fmul fast float [[X:%.*]], [[X]]
476 ; CHECK-NEXT: ret float [[MUL]]
478 %x.fabs = call float @llvm.fabs.f32(float %x)
479 %mul = fmul fast float %x.fabs, %x.fabs
483 define float @fabs_fabs(float %x, float %y) {
484 ; CHECK-LABEL: @fabs_fabs(
485 ; CHECK-NEXT: [[TMP1:%.*]] = fmul float [[X:%.*]], [[Y:%.*]]
486 ; CHECK-NEXT: [[MUL:%.*]] = call float @llvm.fabs.f32(float [[TMP1]])
487 ; CHECK-NEXT: ret float [[MUL]]
489 %x.fabs = call float @llvm.fabs.f32(float %x)
490 %y.fabs = call float @llvm.fabs.f32(float %y)
491 %mul = fmul float %x.fabs, %y.fabs
495 define float @fabs_fabs_extra_use1(float %x, float %y) {
496 ; CHECK-LABEL: @fabs_fabs_extra_use1(
497 ; CHECK-NEXT: [[X_FABS:%.*]] = call float @llvm.fabs.f32(float [[X:%.*]])
498 ; CHECK-NEXT: call void @use_f32(float [[X_FABS]])
499 ; CHECK-NEXT: [[TMP1:%.*]] = fmul ninf float [[X]], [[Y:%.*]]
500 ; CHECK-NEXT: [[MUL:%.*]] = call ninf float @llvm.fabs.f32(float [[TMP1]])
501 ; CHECK-NEXT: ret float [[MUL]]
503 %x.fabs = call float @llvm.fabs.f32(float %x)
504 call void @use_f32(float %x.fabs)
505 %y.fabs = call float @llvm.fabs.f32(float %y)
506 %mul = fmul ninf float %x.fabs, %y.fabs
510 define float @fabs_fabs_extra_use2(float %x, float %y) {
511 ; CHECK-LABEL: @fabs_fabs_extra_use2(
512 ; CHECK-NEXT: [[Y_FABS:%.*]] = call fast float @llvm.fabs.f32(float [[Y:%.*]])
513 ; CHECK-NEXT: call void @use_f32(float [[Y_FABS]])
514 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc ninf float [[X:%.*]], [[Y]]
515 ; CHECK-NEXT: [[MUL:%.*]] = call reassoc ninf float @llvm.fabs.f32(float [[TMP1]])
516 ; CHECK-NEXT: ret float [[MUL]]
518 %x.fabs = call fast float @llvm.fabs.f32(float %x)
519 %y.fabs = call fast float @llvm.fabs.f32(float %y)
520 call void @use_f32(float %y.fabs)
521 %mul = fmul reassoc ninf float %x.fabs, %y.fabs
525 ; negative test - don't create an extra instruction
527 define float @fabs_fabs_extra_use3(float %x, float %y) {
528 ; CHECK-LABEL: @fabs_fabs_extra_use3(
529 ; CHECK-NEXT: [[X_FABS:%.*]] = call float @llvm.fabs.f32(float [[X:%.*]])
530 ; CHECK-NEXT: call void @use_f32(float [[X_FABS]])
531 ; CHECK-NEXT: [[Y_FABS:%.*]] = call float @llvm.fabs.f32(float [[Y:%.*]])
532 ; CHECK-NEXT: call void @use_f32(float [[Y_FABS]])
533 ; CHECK-NEXT: [[MUL:%.*]] = fmul float [[X_FABS]], [[Y_FABS]]
534 ; CHECK-NEXT: ret float [[MUL]]
536 %x.fabs = call float @llvm.fabs.f32(float %x)
537 call void @use_f32(float %x.fabs)
538 %y.fabs = call float @llvm.fabs.f32(float %y)
539 call void @use_f32(float %y.fabs)
540 %mul = fmul float %x.fabs, %y.fabs
544 ; (X*Y) * X => (X*X) * Y
545 ; The transform only requires 'reassoc', but test other FMF in
546 ; the commuted variants to make sure FMF propagates as expected.
548 define float @reassoc_common_operand1(float %x, float %y) {
549 ; CHECK-LABEL: @reassoc_common_operand1(
550 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc float [[X:%.*]], [[X]]
551 ; CHECK-NEXT: [[MUL2:%.*]] = fmul reassoc float [[TMP1]], [[Y:%.*]]
552 ; CHECK-NEXT: ret float [[MUL2]]
554 %mul1 = fmul float %x, %y
555 %mul2 = fmul reassoc float %mul1, %x
559 ; (Y*X) * X => (X*X) * Y
561 define float @reassoc_common_operand2(float %x, float %y) {
562 ; CHECK-LABEL: @reassoc_common_operand2(
563 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[X:%.*]], [[X]]
564 ; CHECK-NEXT: [[MUL2:%.*]] = fmul fast float [[TMP1]], [[Y:%.*]]
565 ; CHECK-NEXT: ret float [[MUL2]]
567 %mul1 = fmul float %y, %x
568 %mul2 = fmul fast float %mul1, %x
572 ; X * (X*Y) => (X*X) * Y
574 define float @reassoc_common_operand3(float %x1, float %y) {
575 ; CHECK-LABEL: @reassoc_common_operand3(
576 ; CHECK-NEXT: [[X:%.*]] = fdiv float [[X1:%.*]], 3.000000e+00
577 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc nnan float [[X]], [[X]]
578 ; CHECK-NEXT: [[MUL2:%.*]] = fmul reassoc nnan float [[TMP1]], [[Y:%.*]]
579 ; CHECK-NEXT: ret float [[MUL2]]
581 %x = fdiv float %x1, 3.0 ; thwart complexity-based canonicalization
582 %mul1 = fmul float %x, %y
583 %mul2 = fmul reassoc nnan float %x, %mul1
587 ; X * (Y*X) => (X*X) * Y
589 define float @reassoc_common_operand4(float %x1, float %y) {
590 ; CHECK-LABEL: @reassoc_common_operand4(
591 ; CHECK-NEXT: [[X:%.*]] = fdiv float [[X1:%.*]], 3.000000e+00
592 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc ninf float [[X]], [[X]]
593 ; CHECK-NEXT: [[MUL2:%.*]] = fmul reassoc ninf float [[TMP1]], [[Y:%.*]]
594 ; CHECK-NEXT: ret float [[MUL2]]
596 %x = fdiv float %x1, 3.0 ; thwart complexity-based canonicalization
597 %mul1 = fmul float %y, %x
598 %mul2 = fmul reassoc ninf float %x, %mul1
602 ; No change if the first fmul has another use.
604 define float @reassoc_common_operand_multi_use(float %x, float %y) {
605 ; CHECK-LABEL: @reassoc_common_operand_multi_use(
606 ; CHECK-NEXT: [[MUL1:%.*]] = fmul float [[X:%.*]], [[Y:%.*]]
607 ; CHECK-NEXT: [[MUL2:%.*]] = fmul fast float [[MUL1]], [[X]]
608 ; CHECK-NEXT: call void @use_f32(float [[MUL1]])
609 ; CHECK-NEXT: ret float [[MUL2]]
611 %mul1 = fmul float %x, %y
612 %mul2 = fmul fast float %mul1, %x
613 call void @use_f32(float %mul1)
617 declare float @llvm.log2.f32(float)
619 ; log2(Y * 0.5) * X = log2(Y) * X - X
621 define float @log2half(float %x, float %y) {
622 ; CHECK-LABEL: @log2half(
623 ; CHECK-NEXT: [[TMP1:%.*]] = call fast float @llvm.log2.f32(float [[Y:%.*]])
624 ; CHECK-NEXT: [[TMP2:%.*]] = fmul fast float [[TMP1]], [[X:%.*]]
625 ; CHECK-NEXT: [[MUL:%.*]] = fsub fast float [[TMP2]], [[X]]
626 ; CHECK-NEXT: ret float [[MUL]]
628 %halfy = fmul float %y, 0.5
629 %log2 = call float @llvm.log2.f32(float %halfy)
630 %mul = fmul fast float %log2, %x
634 define float @log2half_commute(float %x1, float %y) {
635 ; CHECK-LABEL: @log2half_commute(
636 ; CHECK-NEXT: [[X:%.*]] = fmul fast float [[X1:%.*]], 0x3FC24924A0000000
637 ; CHECK-NEXT: [[TMP1:%.*]] = call fast float @llvm.log2.f32(float [[Y:%.*]])
638 ; CHECK-NEXT: [[TMP2:%.*]] = fmul fast float [[TMP1]], [[X]]
639 ; CHECK-NEXT: [[MUL:%.*]] = fsub fast float [[TMP2]], [[X]]
640 ; CHECK-NEXT: ret float [[MUL]]
642 %x = fdiv fast float %x1, 7.0 ; thwart complexity-based canonicalization
643 %halfy = fmul fast float %y, 0.5
644 %log2 = call fast float @llvm.log2.f32(float %halfy)
645 %mul = fmul fast float %x, %log2
649 ; C1/X * C2 => (C1*C2) / X
651 define float @fdiv_constant_numerator_fmul(float %x) {
652 ; CHECK-LABEL: @fdiv_constant_numerator_fmul(
653 ; CHECK-NEXT: [[T3:%.*]] = fdiv reassoc float 1.200000e+07, [[X:%.*]]
654 ; CHECK-NEXT: ret float [[T3]]
656 %t1 = fdiv reassoc float 2.0e+3, %x
657 %t3 = fmul reassoc float %t1, 6.0e+3
661 ; C1/X * C2 => (C1*C2) / X with mixed fast-math flags
663 define float @fdiv_constant_numerator_fmul_mixed(float %x) {
664 ; CHECK-LABEL: @fdiv_constant_numerator_fmul_mixed(
665 ; CHECK-NEXT: [[T3:%.*]] = fdiv reassoc float 1.200000e+07, [[X:%.*]]
666 ; CHECK-NEXT: ret float [[T3]]
668 %t1 = fdiv reassoc float 2.0e+3, %x
669 %t3 = fmul fast float %t1, 6.0e+3
673 ; C1/X * C2 => (C1*C2) / X with full fast-math flags
675 define float @fdiv_constant_numerator_fmul_fast(float %x) {
676 ; CHECK-LABEL: @fdiv_constant_numerator_fmul_fast(
677 ; CHECK-NEXT: [[T3:%.*]] = fdiv fast float 1.200000e+07, [[X:%.*]]
678 ; CHECK-NEXT: ret float [[T3]]
680 %t1 = fdiv fast float 2.0e+3, %x
681 %t3 = fmul fast float %t1, 6.0e+3
685 ; C1/X * C2 => (C1*C2) / X with no fast-math flags on the fdiv
687 define float @fdiv_constant_numerator_fmul_precdiv(float %x) {
688 ; CHECK-LABEL: @fdiv_constant_numerator_fmul_precdiv(
689 ; CHECK-NEXT: [[T1:%.*]] = fdiv float 2.000000e+03, [[X:%.*]]
690 ; CHECK-NEXT: [[T3:%.*]] = fmul reassoc float [[T1]], 6.000000e+03
691 ; CHECK-NEXT: ret float [[T3]]
693 %t1 = fdiv float 2.0e+3, %x
694 %t3 = fmul reassoc float %t1, 6.0e+3
699 ; C1/X * C2 => (C1*C2) / X is disabled if C1/X has multiple uses
701 @fmul2_external = external global float
703 define float @fdiv_constant_numerator_fmul_extra_use(float %x) {
704 ; CHECK-LABEL: @fdiv_constant_numerator_fmul_extra_use(
705 ; CHECK-NEXT: [[DIV:%.*]] = fdiv fast float 1.000000e+00, [[X:%.*]]
706 ; CHECK-NEXT: store float [[DIV]], ptr @fmul2_external, align 4
707 ; CHECK-NEXT: [[MUL:%.*]] = fmul fast float [[DIV]], 2.000000e+00
708 ; CHECK-NEXT: ret float [[MUL]]
710 %div = fdiv fast float 1.0, %x
711 store float %div, ptr @fmul2_external
712 %mul = fmul fast float %div, 2.0
716 ; X/C1 * C2 => X * (C2/C1) (if C2/C1 is normal FP)
718 define float @fdiv_constant_denominator_fmul(float %x) {
719 ; CHECK-LABEL: @fdiv_constant_denominator_fmul(
720 ; CHECK-NEXT: [[T3:%.*]] = fmul reassoc float [[X:%.*]], 3.000000e+00
721 ; CHECK-NEXT: ret float [[T3]]
723 %t1 = fdiv reassoc float %x, 2.0e+3
724 %t3 = fmul reassoc float %t1, 6.0e+3
728 define <4 x float> @fdiv_constant_denominator_fmul_vec(<4 x float> %x) {
729 ; CHECK-LABEL: @fdiv_constant_denominator_fmul_vec(
730 ; CHECK-NEXT: [[T3:%.*]] = fmul reassoc <4 x float> [[X:%.*]], <float 3.000000e+00, float 2.000000e+00, float 1.000000e+00, float 1.000000e+00>
731 ; CHECK-NEXT: ret <4 x float> [[T3]]
733 %t1 = fdiv reassoc <4 x float> %x, <float 2.0e+3, float 3.0e+3, float 2.0e+3, float 1.0e+3>
734 %t3 = fmul reassoc <4 x float> %t1, <float 6.0e+3, float 6.0e+3, float 2.0e+3, float 1.0e+3>
738 ; Make sure fmul with constant expression doesn't assert.
740 define <4 x float> @fdiv_constant_denominator_fmul_vec_constexpr(<4 x float> %x) {
741 ; CHECK-LABEL: @fdiv_constant_denominator_fmul_vec_constexpr(
742 ; CHECK-NEXT: [[T3:%.*]] = fmul reassoc <4 x float> [[X:%.*]], <float 3.000000e+00, float 2.000000e+00, float 1.000000e+00, float 1.000000e+00>
743 ; CHECK-NEXT: ret <4 x float> [[T3]]
745 %constExprMul = bitcast i128 trunc (i160 bitcast (<5 x float> <float 6.0e+3, float 6.0e+3, float 2.0e+3, float 1.0e+3, float poison> to i160) to i128) to <4 x float>
746 %t1 = fdiv reassoc <4 x float> %x, <float 2.0e+3, float 3.0e+3, float 2.0e+3, float 1.0e+3>
747 %t3 = fmul reassoc <4 x float> %t1, %constExprMul
751 ; This shows that at least part of instcombine does not check constant
752 ; values to see if it is creating denorms (0x3800000000000000 is a denorm
753 ; for 32-bit float), so protecting against denorms in other parts is
754 ; probably not doing the intended job.
756 define float @fmul_constant_reassociation(float %x) {
757 ; CHECK-LABEL: @fmul_constant_reassociation(
758 ; CHECK-NEXT: [[R:%.*]] = fmul reassoc nsz float [[X:%.*]], 0x3800000000000000
759 ; CHECK-NEXT: ret float [[R]]
761 %mul_flt_min = fmul reassoc nsz float %x, 0x3810000000000000
762 %r = fmul reassoc nsz float %mul_flt_min, 0.5
766 ; Canonicalization "X/C1 * C2 => X * (C2/C1)" still applies if C2/C1 is denormal
767 ; (otherwise, we should not have allowed the reassociation in the previous test).
768 ; 0x3810000000000000 == FLT_MIN
770 define float @fdiv_constant_denominator_fmul_denorm(float %x) {
771 ; CHECK-LABEL: @fdiv_constant_denominator_fmul_denorm(
772 ; CHECK-NEXT: [[T3:%.*]] = fmul fast float [[X:%.*]], 0x3760620000000000
773 ; CHECK-NEXT: ret float [[T3]]
775 %t1 = fdiv fast float %x, 2.0e+3
776 %t3 = fmul fast float %t1, 0x3810000000000000
780 ; X / C1 * C2 => X / (C2/C1) if C1/C2 is abnormal, but C2/C1 is a normal value.
781 ; TODO: We don't convert the fast fdiv to fmul because that would be multiplication
782 ; by a denormal, but we could do better when we know that denormals are not a problem.
784 define float @fdiv_constant_denominator_fmul_denorm_try_harder(float %x) {
785 ; CHECK-LABEL: @fdiv_constant_denominator_fmul_denorm_try_harder(
786 ; CHECK-NEXT: [[T3:%.*]] = fdiv reassoc float [[X:%.*]], 0x47E8000000000000
787 ; CHECK-NEXT: ret float [[T3]]
789 %t1 = fdiv reassoc float %x, 3.0
790 %t3 = fmul reassoc float %t1, 0x3810000000000000
794 ; Negative test: we should not have 2 divisions instead of the 1 we started with.
796 define float @fdiv_constant_denominator_fmul_denorm_try_harder_extra_use(float %x) {
797 ; CHECK-LABEL: @fdiv_constant_denominator_fmul_denorm_try_harder_extra_use(
798 ; CHECK-NEXT: [[T1:%.*]] = fdiv float [[X:%.*]], 3.000000e+00
799 ; CHECK-NEXT: [[T3:%.*]] = fmul fast float [[T1]], 0x3810000000000000
800 ; CHECK-NEXT: [[R:%.*]] = fadd float [[T1]], [[T3]]
801 ; CHECK-NEXT: ret float [[R]]
803 %t1 = fdiv float %x, 3.0e+0
804 %t3 = fmul fast float %t1, 0x3810000000000000
805 %r = fadd float %t1, %t3
809 ; (X + C1) * C2 --> (X * C2) + C1*C2
811 define float @fmul_fadd_distribute(float %x) {
812 ; CHECK-LABEL: @fmul_fadd_distribute(
813 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc float [[X:%.*]], 3.000000e+00
814 ; CHECK-NEXT: [[T3:%.*]] = fadd reassoc float [[TMP1]], 6.000000e+00
815 ; CHECK-NEXT: ret float [[T3]]
817 %t2 = fadd reassoc float %x, 2.0
818 %t3 = fmul reassoc float %t2, 3.0
822 define <2 x float> @fmul_fadd_distribute_vec(<2 x float> %x) {
823 ; CHECK-LABEL: @fmul_fadd_distribute_vec(
824 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc <2 x float> [[X:%.*]], splat (float 6.000000e+03)
825 ; CHECK-NEXT: [[T3:%.*]] = fadd reassoc <2 x float> [[TMP1]], splat (float 1.200000e+07)
826 ; CHECK-NEXT: ret <2 x float> [[T3]]
828 %t1 = fadd reassoc <2 x float> <float 2.0e+3, float 2.0e+3>, %x
829 %t3 = fmul reassoc <2 x float> %t1, <float 6.0e+3, float 6.0e+3>
833 define <vscale x 2 x float> @fmul_fadd_distribute_scalablevec(<vscale x 2 x float> %x) {
834 ; CHECK-LABEL: @fmul_fadd_distribute_scalablevec(
835 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc <vscale x 2 x float> [[X:%.*]], splat (float 6.000000e+03)
836 ; CHECK-NEXT: [[T3:%.*]] = fadd reassoc <vscale x 2 x float> [[TMP1]], splat (float 1.200000e+07)
837 ; CHECK-NEXT: ret <vscale x 2 x float> [[T3]]
839 %t1 = fadd reassoc <vscale x 2 x float> splat (float 2.0e+3), %x
840 %t3 = fmul reassoc <vscale x 2 x float> %t1, splat (float 6.0e+3)
843 ret <vscale x 2 x float> %t3
846 ; (X - C1) * C2 --> (X * C2) - C1*C2
848 define float @fmul_fsub_distribute1(float %x) {
849 ; CHECK-LABEL: @fmul_fsub_distribute1(
850 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc float [[X:%.*]], 3.000000e+00
851 ; CHECK-NEXT: [[T3:%.*]] = fadd reassoc float [[TMP1]], -6.000000e+00
852 ; CHECK-NEXT: ret float [[T3]]
854 %t2 = fsub reassoc float %x, 2.0
855 %t3 = fmul reassoc float %t2, 3.0
859 ; (C1 - X) * C2 --> C1*C2 - (X * C2)
861 define float @fmul_fsub_distribute2(float %x) {
862 ; CHECK-LABEL: @fmul_fsub_distribute2(
863 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc float [[X:%.*]], 3.000000e+00
864 ; CHECK-NEXT: [[T3:%.*]] = fsub reassoc float 6.000000e+00, [[TMP1]]
865 ; CHECK-NEXT: ret float [[T3]]
867 %t2 = fsub reassoc float 2.0, %x
868 %t3 = fmul reassoc float %t2, 3.0
872 ; FIXME: This should only need 'reassoc'.
873 ; ((X*C1) + C2) * C3 => (X * (C1*C3)) + (C2*C3)
875 define float @fmul_fadd_fmul_distribute(float %x) {
876 ; CHECK-LABEL: @fmul_fadd_fmul_distribute(
877 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[X:%.*]], 3.000000e+01
878 ; CHECK-NEXT: [[T3:%.*]] = fadd fast float [[TMP1]], 1.000000e+01
879 ; CHECK-NEXT: ret float [[T3]]
881 %t1 = fmul fast float %x, 6.0
882 %t2 = fadd fast float %t1, 2.0
883 %t3 = fmul fast float %t2, 5.0
887 define float @fmul_fadd_distribute_extra_use(float %x) {
888 ; CHECK-LABEL: @fmul_fadd_distribute_extra_use(
889 ; CHECK-NEXT: [[T1:%.*]] = fmul float [[X:%.*]], 6.000000e+00
890 ; CHECK-NEXT: [[T2:%.*]] = fadd float [[T1]], 2.000000e+00
891 ; CHECK-NEXT: [[T3:%.*]] = fmul fast float [[T2]], 5.000000e+00
892 ; CHECK-NEXT: call void @use_f32(float [[T2]])
893 ; CHECK-NEXT: ret float [[T3]]
895 %t1 = fmul float %x, 6.0
896 %t2 = fadd float %t1, 2.0
897 %t3 = fmul fast float %t2, 5.0
898 call void @use_f32(float %t2)
902 ; (X/C1 + C2) * C3 => X/(C1/C3) + C2*C3
903 ; 0x10000000000000 = DBL_MIN
904 ; TODO: We don't convert the fast fdiv to fmul because that would be multiplication
905 ; by a denormal, but we could do better when we know that denormals are not a problem.
907 define double @fmul_fadd_fdiv_distribute2(double %x) {
908 ; CHECK-LABEL: @fmul_fadd_fdiv_distribute2(
909 ; CHECK-NEXT: [[TMP1:%.*]] = fdiv reassoc double [[X:%.*]], 0x7FE8000000000000
910 ; CHECK-NEXT: [[T3:%.*]] = fadd reassoc double [[TMP1]], 0x34000000000000
911 ; CHECK-NEXT: ret double [[T3]]
913 %t1 = fdiv reassoc double %x, 3.0
914 %t2 = fadd reassoc double %t1, 5.0
915 %t3 = fmul reassoc double %t2, 0x10000000000000
919 ; 5.0e-1 * DBL_MIN yields denormal, so "(f1*3.0 + 5.0e-1) * DBL_MIN" cannot
920 ; be simplified into f1 * (3.0*DBL_MIN) + (5.0e-1*DBL_MIN)
922 define double @fmul_fadd_fdiv_distribute3(double %x) {
923 ; CHECK-LABEL: @fmul_fadd_fdiv_distribute3(
924 ; CHECK-NEXT: [[TMP1:%.*]] = fdiv reassoc double [[X:%.*]], 0x7FE8000000000000
925 ; CHECK-NEXT: [[T3:%.*]] = fadd reassoc double [[TMP1]], 0x34000000000000
926 ; CHECK-NEXT: ret double [[T3]]
928 %t1 = fdiv reassoc double %x, 3.0
929 %t2 = fadd reassoc double %t1, 5.0
930 %t3 = fmul reassoc double %t2, 0x10000000000000
934 ; FIXME: This should only need 'reassoc'.
935 ; (C2 - (X*C1)) * C3 => (C2*C3) - (X * (C1*C3))
937 define float @fmul_fsub_fmul_distribute(float %x) {
938 ; CHECK-LABEL: @fmul_fsub_fmul_distribute(
939 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[X:%.*]], 3.000000e+01
940 ; CHECK-NEXT: [[T3:%.*]] = fsub fast float 1.000000e+01, [[TMP1]]
941 ; CHECK-NEXT: ret float [[T3]]
943 %t1 = fmul fast float %x, 6.0
944 %t2 = fsub fast float 2.0, %t1
945 %t3 = fmul fast float %t2, 5.0
949 define float @fmul_fsub_fmul_distribute_extra_use(float %x) {
950 ; CHECK-LABEL: @fmul_fsub_fmul_distribute_extra_use(
951 ; CHECK-NEXT: [[T1:%.*]] = fmul float [[X:%.*]], 6.000000e+00
952 ; CHECK-NEXT: [[T2:%.*]] = fsub float 2.000000e+00, [[T1]]
953 ; CHECK-NEXT: [[T3:%.*]] = fmul fast float [[T2]], 5.000000e+00
954 ; CHECK-NEXT: call void @use_f32(float [[T2]])
955 ; CHECK-NEXT: ret float [[T3]]
957 %t1 = fmul float %x, 6.0
958 %t2 = fsub float 2.0, %t1
959 %t3 = fmul fast float %t2, 5.0
960 call void @use_f32(float %t2)
964 ; FIXME: This should only need 'reassoc'.
965 ; ((X*C1) - C2) * C3 => (X * (C1*C3)) - C2*C3
967 define float @fmul_fsub_fmul_distribute2(float %x) {
968 ; CHECK-LABEL: @fmul_fsub_fmul_distribute2(
969 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[X:%.*]], 3.000000e+01
970 ; CHECK-NEXT: [[T3:%.*]] = fadd fast float [[TMP1]], -1.000000e+01
971 ; CHECK-NEXT: ret float [[T3]]
973 %t1 = fmul fast float %x, 6.0
974 %t2 = fsub fast float %t1, 2.0
975 %t3 = fmul fast float %t2, 5.0
979 define float @fmul_fsub_fmul_distribute2_extra_use(float %x) {
980 ; CHECK-LABEL: @fmul_fsub_fmul_distribute2_extra_use(
981 ; CHECK-NEXT: [[T1:%.*]] = fmul float [[X:%.*]], 6.000000e+00
982 ; CHECK-NEXT: [[T2:%.*]] = fsub float 2.000000e+00, [[T1]]
983 ; CHECK-NEXT: [[T3:%.*]] = fmul fast float [[T2]], 5.000000e+00
984 ; CHECK-NEXT: call void @use_f32(float [[T2]])
985 ; CHECK-NEXT: ret float [[T3]]
987 %t1 = fmul float %x, 6.0
988 %t2 = fsub float 2.0, %t1
989 %t3 = fmul fast float %t2, 5.0
990 call void @use_f32(float %t2)
994 ; "(X*Y) * X => (X*X) * Y" is disabled if "X*Y" has multiple uses
996 define float @common_factor(float %x, float %y) {
997 ; CHECK-LABEL: @common_factor(
998 ; CHECK-NEXT: [[MUL:%.*]] = fmul float [[X:%.*]], [[Y:%.*]]
999 ; CHECK-NEXT: [[MUL1:%.*]] = fmul fast float [[MUL]], [[X]]
1000 ; CHECK-NEXT: [[ADD:%.*]] = fadd float [[MUL1]], [[MUL]]
1001 ; CHECK-NEXT: ret float [[ADD]]
1003 %mul = fmul float %x, %y
1004 %mul1 = fmul fast float %mul, %x
1005 %add = fadd float %mul1, %mul
1009 define double @fmul_fdiv_factor_squared(double %x, double %y) {
1010 ; CHECK-LABEL: @fmul_fdiv_factor_squared(
1011 ; CHECK-NEXT: [[DIV:%.*]] = fdiv fast double [[X:%.*]], [[Y:%.*]]
1012 ; CHECK-NEXT: [[SQUARED:%.*]] = fmul fast double [[DIV]], [[DIV]]
1013 ; CHECK-NEXT: ret double [[SQUARED]]
1015 %div = fdiv fast double %x, %y
1016 %squared = fmul fast double %div, %div
1020 define double @fmul_fdivs_factor_common_denominator(double %x, double %y, double %z) {
1021 ; CHECK-LABEL: @fmul_fdivs_factor_common_denominator(
1022 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast double [[Y:%.*]], [[X:%.*]]
1023 ; CHECK-NEXT: [[TMP2:%.*]] = fmul fast double [[Z:%.*]], [[Z]]
1024 ; CHECK-NEXT: [[MUL:%.*]] = fdiv fast double [[TMP1]], [[TMP2]]
1025 ; CHECK-NEXT: ret double [[MUL]]
1027 %div1 = fdiv fast double %x, %z
1028 %div2 = fdiv fast double %y, %z
1029 %mul = fmul fast double %div1, %div2
1033 define double @fmul_fdivs_factor(double %x, double %y, double %z, double %w) {
1034 ; CHECK-LABEL: @fmul_fdivs_factor(
1035 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc double [[Z:%.*]], [[X:%.*]]
1036 ; CHECK-NEXT: [[TMP2:%.*]] = fdiv reassoc double [[TMP1]], [[W:%.*]]
1037 ; CHECK-NEXT: [[MUL:%.*]] = fdiv reassoc double [[TMP2]], [[Y:%.*]]
1038 ; CHECK-NEXT: ret double [[MUL]]
1040 %div1 = fdiv reassoc double %x, %y
1041 %div2 = fdiv reassoc double %z, %w
1042 %mul = fmul reassoc double %div1, %div2
1046 define double @fmul_fdiv_factor(double %x, double %y, double %z) {
1047 ; CHECK-LABEL: @fmul_fdiv_factor(
1048 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc double [[X:%.*]], [[Z:%.*]]
1049 ; CHECK-NEXT: [[MUL:%.*]] = fdiv reassoc double [[TMP1]], [[Y:%.*]]
1050 ; CHECK-NEXT: ret double [[MUL]]
1052 %div = fdiv reassoc double %x, %y
1053 %mul = fmul reassoc double %div, %z
1057 define double @fmul_fdiv_factor_constant1(double %x, double %y) {
1058 ; CHECK-LABEL: @fmul_fdiv_factor_constant1(
1059 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc double [[X:%.*]], 4.200000e+01
1060 ; CHECK-NEXT: [[MUL:%.*]] = fdiv reassoc double [[TMP1]], [[Y:%.*]]
1061 ; CHECK-NEXT: ret double [[MUL]]
1063 %div = fdiv reassoc double %x, %y
1064 %mul = fmul reassoc double %div, 42.0
1068 define <2 x float> @fmul_fdiv_factor_constant2(<2 x float> %x, <2 x float> %y) {
1069 ; CHECK-LABEL: @fmul_fdiv_factor_constant2(
1070 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc <2 x float> [[X:%.*]], [[Y:%.*]]
1071 ; CHECK-NEXT: [[MUL:%.*]] = fdiv reassoc <2 x float> [[TMP1]], <float 4.200000e+01, float 1.200000e+01>
1072 ; CHECK-NEXT: ret <2 x float> [[MUL]]
1074 %div = fdiv reassoc <2 x float> %x, <float 42.0, float 12.0>
1075 %mul = fmul reassoc <2 x float> %div, %y
1076 ret <2 x float> %mul
1079 define float @fmul_fdiv_factor_extra_use(float %x, float %y) {
1080 ; CHECK-LABEL: @fmul_fdiv_factor_extra_use(
1081 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[X:%.*]], 4.200000e+01
1082 ; CHECK-NEXT: call void @use_f32(float [[DIV]])
1083 ; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc float [[DIV]], [[Y:%.*]]
1084 ; CHECK-NEXT: ret float [[MUL]]
1086 %div = fdiv float %x, 42.0
1087 call void @use_f32(float %div)
1088 %mul = fmul reassoc float %div, %y
1092 define void @fmul_loop_invariant_fdiv(ptr %a, float %x) {
1093 ; CHECK-LABEL: @fmul_loop_invariant_fdiv(
1094 ; CHECK-NEXT: entry:
1095 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
1096 ; CHECK: for.cond.cleanup:
1097 ; CHECK-NEXT: ret void
1099 ; CHECK-NEXT: [[I_08:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INC:%.*]], [[FOR_BODY]] ]
1100 ; CHECK-NEXT: [[IDXPROM:%.*]] = zext nneg i32 [[I_08]] to i64
1101 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds float, ptr [[A:%.*]], i64 [[IDXPROM]]
1102 ; CHECK-NEXT: [[F:%.*]] = load float, ptr [[ARRAYIDX]], align 4
1103 ; CHECK-NEXT: [[M:%.*]] = fdiv fast float [[F]], [[X:%.*]]
1104 ; CHECK-NEXT: store float [[M]], ptr [[ARRAYIDX]], align 4
1105 ; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[I_08]], 1
1106 ; CHECK-NEXT: [[CMP_NOT:%.*]] = icmp eq i32 [[INC]], 1024
1107 ; CHECK-NEXT: br i1 [[CMP_NOT]], label [[FOR_COND_CLEANUP:%.*]], label [[FOR_BODY]]
1110 %d = fdiv fast float 1.0, %x
1117 %i.08 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
1118 %idxprom = zext i32 %i.08 to i64
1119 %arrayidx = getelementptr inbounds float, ptr %a, i64 %idxprom
1120 %f = load float, ptr %arrayidx, align 4
1121 %m = fmul fast float %f, %d
1122 store float %m, ptr %arrayidx, align 4
1123 %inc = add nuw nsw i32 %i.08, 1
1124 %cmp.not = icmp eq i32 %inc, 1024
1125 br i1 %cmp.not, label %for.cond.cleanup, label %for.body
1128 ; Avoid infinite looping by moving negation out of a constant expression.
1130 @g = external global {[2 x ptr]}, align 1
1132 define double @fmul_negated_constant_expression(double %x) {
1133 ; CHECK-LABEL: @fmul_negated_constant_expression(
1134 ; CHECK-NEXT: [[FSUB:%.*]] = fneg double bitcast (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr @g, i64 16) to i64) to double)
1135 ; CHECK-NEXT: [[R:%.*]] = fmul double [[X:%.*]], [[FSUB]]
1136 ; CHECK-NEXT: ret double [[R]]
1138 %fsub = fsub double -0.000000e+00, bitcast (i64 ptrtoint (ptr getelementptr inbounds ({ [2 x ptr] }, ptr @g, i64 0, i32 0, i64 2) to i64) to double)
1139 %r = fmul double %x, %fsub
1143 define float @negate_if_true(float %x, i1 %cond) {
1144 ; CHECK-LABEL: @negate_if_true(
1145 ; CHECK-NEXT: [[TMP1:%.*]] = fneg float [[X:%.*]]
1146 ; CHECK-NEXT: [[R:%.*]] = select i1 [[COND:%.*]], float [[TMP1]], float [[X]]
1147 ; CHECK-NEXT: ret float [[R]]
1149 %sel = select i1 %cond, float -1.0, float 1.0
1150 %r = fmul float %sel, %x
1154 define float @negate_if_false(float %x, i1 %cond) {
1155 ; CHECK-LABEL: @negate_if_false(
1156 ; CHECK-NEXT: [[TMP1:%.*]] = fneg arcp float [[X:%.*]]
1157 ; CHECK-NEXT: [[R:%.*]] = select arcp i1 [[COND:%.*]], float [[X]], float [[TMP1]]
1158 ; CHECK-NEXT: ret float [[R]]
1160 %sel = select i1 %cond, float 1.0, float -1.0
1161 %r = fmul arcp float %sel, %x
1165 define <2 x double> @negate_if_true_commute(<2 x double> %px, i1 %cond) {
1166 ; CHECK-LABEL: @negate_if_true_commute(
1167 ; CHECK-NEXT: [[X:%.*]] = fdiv <2 x double> splat (double 4.200000e+01), [[PX:%.*]]
1168 ; CHECK-NEXT: [[TMP1:%.*]] = fneg ninf <2 x double> [[X]]
1169 ; CHECK-NEXT: [[R:%.*]] = select ninf i1 [[COND:%.*]], <2 x double> [[TMP1]], <2 x double> [[X]]
1170 ; CHECK-NEXT: ret <2 x double> [[R]]
1172 %x = fdiv <2 x double> <double 42.0, double 42.0>, %px ; thwart complexity-based canonicalization
1173 %sel = select i1 %cond, <2 x double> <double -1.0, double -1.0>, <2 x double> <double 1.0, double 1.0>
1174 %r = fmul ninf <2 x double> %x, %sel
1178 define <2 x double> @negate_if_false_commute(<2 x double> %px, <2 x i1> %cond) {
1179 ; CHECK-LABEL: @negate_if_false_commute(
1180 ; CHECK-NEXT: [[X:%.*]] = fdiv <2 x double> <double 4.200000e+01, double 5.100000e+00>, [[PX:%.*]]
1181 ; CHECK-NEXT: [[TMP1:%.*]] = fneg <2 x double> [[X]]
1182 ; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[COND:%.*]], <2 x double> [[X]], <2 x double> [[TMP1]]
1183 ; CHECK-NEXT: ret <2 x double> [[R]]
1185 %x = fdiv <2 x double> <double 42.0, double 5.1>, %px ; thwart complexity-based canonicalization
1186 %sel = select <2 x i1> %cond, <2 x double> <double 1.0, double 1.0>, <2 x double> <double -1.0, double -1.0>
1187 %r = fmul <2 x double> %x, %sel
1193 define float @negate_if_true_extra_use(float %x, i1 %cond) {
1194 ; CHECK-LABEL: @negate_if_true_extra_use(
1195 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], float -1.000000e+00, float 1.000000e+00
1196 ; CHECK-NEXT: call void @use_f32(float [[SEL]])
1197 ; CHECK-NEXT: [[R:%.*]] = fmul float [[SEL]], [[X:%.*]]
1198 ; CHECK-NEXT: ret float [[R]]
1200 %sel = select i1 %cond, float -1.0, float 1.0
1201 call void @use_f32(float %sel)
1202 %r = fmul float %sel, %x
1208 define <2 x double> @negate_if_true_wrong_constant(<2 x double> %px, i1 %cond) {
1209 ; CHECK-LABEL: @negate_if_true_wrong_constant(
1210 ; CHECK-NEXT: [[X:%.*]] = fdiv <2 x double> splat (double 4.200000e+01), [[PX:%.*]]
1211 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], <2 x double> <double -1.000000e+00, double 0.000000e+00>, <2 x double> splat (double 1.000000e+00)
1212 ; CHECK-NEXT: [[R:%.*]] = fmul <2 x double> [[X]], [[SEL]]
1213 ; CHECK-NEXT: ret <2 x double> [[R]]
1215 %x = fdiv <2 x double> <double 42.0, double 42.0>, %px ; thwart complexity-based canonicalization
1216 %sel = select i1 %cond, <2 x double> <double -1.0, double 0.0>, <2 x double> <double 1.0, double 1.0>
1217 %r = fmul <2 x double> %x, %sel
1221 ; X *fast (C ? 1.0 : 0.0) -> C ? X : 0.0
1222 define float @fmul_select(float %x, i1 %c) {
1223 ; CHECK-LABEL: @fmul_select(
1224 ; CHECK-NEXT: [[MUL:%.*]] = select fast i1 [[C:%.*]], float [[X:%.*]], float 0.000000e+00
1225 ; CHECK-NEXT: ret float [[MUL]]
1227 %sel = select i1 %c, float 1.0, float 0.0
1228 %mul = fmul fast float %sel, %x
1232 ; X *fast (C ? 1.0 : 0.0) -> C ? X : 0.0
1233 define <2 x float> @fmul_select_vec(<2 x float> %x, i1 %c) {
1234 ; CHECK-LABEL: @fmul_select_vec(
1235 ; CHECK-NEXT: [[MUL:%.*]] = select fast i1 [[C:%.*]], <2 x float> [[X:%.*]], <2 x float> zeroinitializer
1236 ; CHECK-NEXT: ret <2 x float> [[MUL]]
1238 %sel = select i1 %c, <2 x float> <float 1.0, float 1.0>, <2 x float> zeroinitializer
1239 %mul = fmul fast <2 x float> %sel, %x
1240 ret <2 x float> %mul
1243 ; Without fast math flags we can't optimize X * (C ? 1.0 : 0.0) -> C ? X : 0.0
1244 define float @fmul_select_strict(float %x, i1 %c) {
1245 ; CHECK-LABEL: @fmul_select_strict(
1246 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C:%.*]], float 1.000000e+00, float 0.000000e+00
1247 ; CHECK-NEXT: [[MUL:%.*]] = fmul float [[SEL]], [[X:%.*]]
1248 ; CHECK-NEXT: ret float [[MUL]]
1250 %sel = select i1 %c, float 1.0, float 0.0
1251 %mul = fmul float %sel, %x
1255 ; sqrt(X) *fast (C ? sqrt(X) : 1.0) -> C ? X : sqrt(X)
1256 define double @fmul_sqrt_select(double %x, i1 %c) {
1257 ; CHECK-LABEL: @fmul_sqrt_select(
1258 ; CHECK-NEXT: [[SQR:%.*]] = call double @llvm.sqrt.f64(double [[X:%.*]])
1259 ; CHECK-NEXT: [[MUL:%.*]] = select fast i1 [[C:%.*]], double [[X]], double [[SQR]]
1260 ; CHECK-NEXT: ret double [[MUL]]
1262 %sqr = call double @llvm.sqrt.f64(double %x)
1263 %sel = select i1 %c, double %sqr, double 1.0
1264 %mul = fmul fast double %sqr, %sel
1268 ; fastmath => z * splat(0) = splat(0), even for scalable vectors
1269 define <vscale x 2 x float> @mul_scalable_splat_zero(<vscale x 2 x float> %z) {
1270 ; CHECK-LABEL: @mul_scalable_splat_zero(
1271 ; CHECK-NEXT: ret <vscale x 2 x float> zeroinitializer
1273 %shuf = shufflevector <vscale x 2 x float> insertelement (<vscale x 2 x float> poison, float 0.0, i32 0), <vscale x 2 x float> poison, <vscale x 2 x i32> zeroinitializer
1274 %t3 = fmul fast <vscale x 2 x float> %shuf, %z
1275 ret <vscale x 2 x float> %t3
1278 define half @mul_zero_nnan(half %x) {
1279 ; CHECK-LABEL: @mul_zero_nnan(
1280 ; CHECK-NEXT: [[R:%.*]] = call nnan half @llvm.copysign.f16(half 0xH0000, half [[X:%.*]])
1281 ; CHECK-NEXT: ret half [[R]]
1283 %r = fmul nnan half %x, 0.0
1287 ; poison propagates through vector elements
1289 define <2 x float> @mul_zero_nnan_vec_poison(<2 x float> %x) {
1290 ; CHECK-LABEL: @mul_zero_nnan_vec_poison(
1291 ; CHECK-NEXT: [[R:%.*]] = call nnan <2 x float> @llvm.copysign.v2f32(<2 x float> <float 0.000000e+00, float poison>, <2 x float> [[X:%.*]])
1292 ; CHECK-NEXT: ret <2 x float> [[R]]
1294 %r = fmul nnan <2 x float> %x, <float 0.0, float poison>
1298 ; negative test - must have nnan
1300 define half @mul_zero(half %x) {
1301 ; CHECK-LABEL: @mul_zero(
1302 ; CHECK-NEXT: [[R:%.*]] = fmul ninf nsz half [[X:%.*]], 0xH0000
1303 ; CHECK-NEXT: ret half [[R]]
1305 %r = fmul ninf nsz half %x, 0.0
1309 define half @mul_negzero_nnan(half %x) {
1310 ; CHECK-LABEL: @mul_negzero_nnan(
1311 ; CHECK-NEXT: [[TMP1:%.*]] = fneg nnan half [[X:%.*]]
1312 ; CHECK-NEXT: [[R:%.*]] = call nnan half @llvm.copysign.f16(half 0xH0000, half [[TMP1]])
1313 ; CHECK-NEXT: ret half [[R]]
1315 %r = fmul nnan half %x, -0.0
1319 define float @mul_pos_zero_nnan_ninf(float nofpclass(inf nan) %a) {
1320 ; CHECK-LABEL: @mul_pos_zero_nnan_ninf(
1321 ; CHECK-NEXT: entry:
1322 ; CHECK-NEXT: [[RET:%.*]] = call float @llvm.copysign.f32(float 0.000000e+00, float [[A:%.*]])
1323 ; CHECK-NEXT: ret float [[RET]]
1326 %ret = fmul float %a, 0.000000e+00
1330 define float @mul_pos_zero_nnan(float nofpclass(nan) %a) {
1331 ; CHECK-LABEL: @mul_pos_zero_nnan(
1332 ; CHECK-NEXT: entry:
1333 ; CHECK-NEXT: [[RET:%.*]] = fmul float [[A:%.*]], 0.000000e+00
1334 ; CHECK-NEXT: ret float [[RET]]
1337 %ret = fmul float %a, 0.000000e+00
1341 define float @mul_pos_zero_nnan_ninf_fmf(float nofpclass(nan) %a) {
1342 ; CHECK-LABEL: @mul_pos_zero_nnan_ninf_fmf(
1343 ; CHECK-NEXT: entry:
1344 ; CHECK-NEXT: [[RET:%.*]] = call ninf float @llvm.copysign.f32(float 0.000000e+00, float [[A:%.*]])
1345 ; CHECK-NEXT: ret float [[RET]]
1348 %ret = fmul ninf float %a, 0.000000e+00
1352 define float @mul_neg_zero_nnan_ninf(float nofpclass(inf nan) %a) {
1353 ; CHECK-LABEL: @mul_neg_zero_nnan_ninf(
1354 ; CHECK-NEXT: entry:
1355 ; CHECK-NEXT: [[TMP0:%.*]] = fneg float [[A:%.*]]
1356 ; CHECK-NEXT: [[RET:%.*]] = call float @llvm.copysign.f32(float 0.000000e+00, float [[TMP0]])
1357 ; CHECK-NEXT: ret float [[RET]]
1360 %ret = fmul float %a, -0.000000e+00
1364 define float @mul_neg_zero_nnan_fmf(float %a) {
1365 ; CHECK-LABEL: @mul_neg_zero_nnan_fmf(
1366 ; CHECK-NEXT: entry:
1367 ; CHECK-NEXT: [[TMP0:%.*]] = fneg nnan float [[A:%.*]]
1368 ; CHECK-NEXT: [[RET:%.*]] = call nnan float @llvm.copysign.f32(float 0.000000e+00, float [[TMP0]])
1369 ; CHECK-NEXT: ret float [[RET]]
1372 %ret = fmul nnan float %a, -0.000000e+00
1376 define float @mul_neg_zero_nnan_ninf_fmf(float nofpclass(inf nan) %a) {
1377 ; CHECK-LABEL: @mul_neg_zero_nnan_ninf_fmf(
1378 ; CHECK-NEXT: entry:
1379 ; CHECK-NEXT: [[TMP0:%.*]] = fneg nnan ninf float [[A:%.*]]
1380 ; CHECK-NEXT: [[RET:%.*]] = call nnan ninf float @llvm.copysign.f32(float 0.000000e+00, float [[TMP0]])
1381 ; CHECK-NEXT: ret float [[RET]]
1384 %ret = fmul nnan ninf float %a, -0.000000e+00
1388 define <3 x float> @mul_neg_zero_nnan_ninf_vec(<3 x float> nofpclass(inf nan) %a) {
1389 ; CHECK-LABEL: @mul_neg_zero_nnan_ninf_vec(
1390 ; CHECK-NEXT: entry:
1391 ; CHECK-NEXT: [[TMP0:%.*]] = fneg <3 x float> [[A:%.*]]
1392 ; CHECK-NEXT: [[RET:%.*]] = call <3 x float> @llvm.copysign.v3f32(<3 x float> <float -0.000000e+00, float poison, float poison>, <3 x float> [[TMP0]])
1393 ; CHECK-NEXT: ret <3 x float> [[RET]]
1396 %ret = fmul <3 x float> %a, <float -0.0, float poison, float poison>
1397 ret <3 x float> %ret
1400 define <3 x float> @mul_mixed_zero_nnan_ninf_vec(<3 x float> nofpclass(inf nan) %a) {
1401 ; CHECK-LABEL: @mul_mixed_zero_nnan_ninf_vec(
1402 ; CHECK-NEXT: entry:
1403 ; CHECK-NEXT: [[RET:%.*]] = fmul <3 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00, float poison>
1404 ; CHECK-NEXT: ret <3 x float> [[RET]]
1407 %ret = fmul <3 x float> %a, <float -0.0, float 0.0, float poison>
1408 ret <3 x float> %ret