1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -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_undef(<2 x float> %x) {
46 ; CHECK-LABEL: @neg_constant_vec_undef(
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 undef, 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_undef(<2 x float> %x, <2 x float> %y) {
166 ; CHECK-LABEL: @neg_neg_vec_undef(
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 undef>, %x
171 %sub2 = fsub <2 x float> <float undef, float -0.0>, %y
172 %mul = fmul arcp <2 x float> %sub1, %sub2
176 define <2 x float> @unary_neg_neg_vec_undef(<2 x float> %x, <2 x float> %y) {
177 ; CHECK-LABEL: @unary_neg_neg_vec_undef(
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 undef, float -0.0>, %y
183 %mul = fmul arcp <2 x float> %neg, %sub
187 define <2 x float> @neg_unary_neg_vec_undef(<2 x float> %x, <2 x float> %y) {
188 ; CHECK-LABEL: @neg_unary_neg_vec_undef(
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 undef>, %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 [[SUB]], [[Y:%.*]]
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 [[NEG]], [[Y:%.*]]
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> [[SUB]], [[Y:%.*]]
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> [[SUB]], [[Y:%.*]]
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_undef(<2 x float> %x, <2 x float> %y) {
326 ; CHECK-LABEL: @neg_mul_vec_undef(
327 ; CHECK-NEXT: [[SUB:%.*]] = fneg <2 x float> [[X:%.*]]
328 ; CHECK-NEXT: [[MUL:%.*]] = fmul <2 x float> [[SUB]], [[Y:%.*]]
329 ; CHECK-NEXT: ret <2 x float> [[MUL]]
331 %sub = fsub <2 x float> <float undef, 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 [[SUB1]], [[Y:%.*]]
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 [[SUB1]], [[Y:%.*]]
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 [[SUB1]], [[Y:%.*]]
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(i32* %inout) {
376 ; CHECK-LABEL: @test8(
378 ; CHECK-NEXT: br label [[FOR_COND:%.*]]
380 ; CHECK-NEXT: [[LOCAL_VAR_7_0:%.*]] = phi <4 x float> [ <float -0.000000e+00, float -0.000000e+00, float -0.000000e+00, float -0.000000e+00>, [[ENTRY:%.*]] ], [ [[TMP0:%.*]], [[FOR_BODY:%.*]] ]
381 ; CHECK-NEXT: br i1 undef, label [[FOR_BODY]], label [[FOR_END:%.*]]
383 ; CHECK-NEXT: [[TMP0]] = insertelement <4 x float> [[LOCAL_VAR_7_0]], float 0.000000e+00, i32 2
384 ; CHECK-NEXT: br label [[FOR_COND]]
386 ; CHECK-NEXT: ret void
389 %0 = load i32, i32* %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 undef>, 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> undef, <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 undef, 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 [[SQRT]], [[F]]
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: [[TMP1:%.*]] = call fast float @llvm.log2.f32(float [[Y:%.*]])
637 ; CHECK-NEXT: [[TMP2:%.*]] = fmul fast float [[TMP1]], [[X1:%.*]]
638 ; CHECK-NEXT: [[TMP3:%.*]] = fsub fast float [[TMP2]], [[X1]]
639 ; CHECK-NEXT: [[MUL:%.*]] = fmul fast float [[TMP3]], 0x3FC24924A0000000
640 ; CHECK-NEXT: ret float [[MUL]]
642 %x = fdiv float %x1, 7.0 ; thwart complexity-based canonicalization
643 %halfy = fmul float %y, 0.5
644 %log2 = call 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 float 2.0e+3, %x
657 %t3 = fmul reassoc float %t1, 6.0e+3
661 ; C1/X * C2 => (C1*C2) / X is disabled if C1/X has multiple uses
663 @fmul2_external = external global float
665 define float @fdiv_constant_numerator_fmul_extra_use(float %x) {
666 ; CHECK-LABEL: @fdiv_constant_numerator_fmul_extra_use(
667 ; CHECK-NEXT: [[DIV:%.*]] = fdiv fast float 1.000000e+00, [[X:%.*]]
668 ; CHECK-NEXT: store float [[DIV]], float* @fmul2_external, align 4
669 ; CHECK-NEXT: [[MUL:%.*]] = fmul fast float [[DIV]], 2.000000e+00
670 ; CHECK-NEXT: ret float [[MUL]]
672 %div = fdiv fast float 1.0, %x
673 store float %div, float* @fmul2_external
674 %mul = fmul fast float %div, 2.0
678 ; X/C1 * C2 => X * (C2/C1) (if C2/C1 is normal FP)
680 define float @fdiv_constant_denominator_fmul(float %x) {
681 ; CHECK-LABEL: @fdiv_constant_denominator_fmul(
682 ; CHECK-NEXT: [[T3:%.*]] = fmul reassoc float [[X:%.*]], 3.000000e+00
683 ; CHECK-NEXT: ret float [[T3]]
685 %t1 = fdiv float %x, 2.0e+3
686 %t3 = fmul reassoc float %t1, 6.0e+3
690 define <4 x float> @fdiv_constant_denominator_fmul_vec(<4 x float> %x) {
691 ; CHECK-LABEL: @fdiv_constant_denominator_fmul_vec(
692 ; 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>
693 ; CHECK-NEXT: ret <4 x float> [[T3]]
695 %t1 = fdiv <4 x float> %x, <float 2.0e+3, float 3.0e+3, float 2.0e+3, float 1.0e+3>
696 %t3 = fmul reassoc <4 x float> %t1, <float 6.0e+3, float 6.0e+3, float 2.0e+3, float 1.0e+3>
700 ; Make sure fmul with constant expression doesn't assert.
702 define <4 x float> @fdiv_constant_denominator_fmul_vec_constexpr(<4 x float> %x) {
703 ; CHECK-LABEL: @fdiv_constant_denominator_fmul_vec_constexpr(
704 ; 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>
705 ; CHECK-NEXT: ret <4 x float> [[T3]]
707 %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 undef> to i160) to i128) to <4 x float>
708 %t1 = fdiv <4 x float> %x, <float 2.0e+3, float 3.0e+3, float 2.0e+3, float 1.0e+3>
709 %t3 = fmul reassoc <4 x float> %t1, %constExprMul
713 ; This shows that at least part of instcombine does not check constant
714 ; values to see if it is creating denorms (0x3800000000000000 is a denorm
715 ; for 32-bit float), so protecting against denorms in other parts is
716 ; probably not doing the intended job.
718 define float @fmul_constant_reassociation(float %x) {
719 ; CHECK-LABEL: @fmul_constant_reassociation(
720 ; CHECK-NEXT: [[R:%.*]] = fmul reassoc nsz float [[X:%.*]], 0x3800000000000000
721 ; CHECK-NEXT: ret float [[R]]
723 %mul_flt_min = fmul reassoc nsz float %x, 0x3810000000000000
724 %r = fmul reassoc nsz float %mul_flt_min, 0.5
728 ; Canonicalization "X/C1 * C2 => X * (C2/C1)" still applies if C2/C1 is denormal
729 ; (otherwise, we should not have allowed the reassociation in the previous test).
730 ; 0x3810000000000000 == FLT_MIN
732 define float @fdiv_constant_denominator_fmul_denorm(float %x) {
733 ; CHECK-LABEL: @fdiv_constant_denominator_fmul_denorm(
734 ; CHECK-NEXT: [[T3:%.*]] = fmul fast float [[X:%.*]], 0x3760620000000000
735 ; CHECK-NEXT: ret float [[T3]]
737 %t1 = fdiv float %x, 2.0e+3
738 %t3 = fmul fast float %t1, 0x3810000000000000
742 ; X / C1 * C2 => X / (C2/C1) if C1/C2 is abnormal, but C2/C1 is a normal value.
743 ; TODO: We don't convert the fast fdiv to fmul because that would be multiplication
744 ; by a denormal, but we could do better when we know that denormals are not a problem.
746 define float @fdiv_constant_denominator_fmul_denorm_try_harder(float %x) {
747 ; CHECK-LABEL: @fdiv_constant_denominator_fmul_denorm_try_harder(
748 ; CHECK-NEXT: [[T3:%.*]] = fdiv reassoc float [[X:%.*]], 0x47E8000000000000
749 ; CHECK-NEXT: ret float [[T3]]
751 %t1 = fdiv float %x, 3.0
752 %t3 = fmul reassoc float %t1, 0x3810000000000000
756 ; Negative test: we should not have 2 divisions instead of the 1 we started with.
758 define float @fdiv_constant_denominator_fmul_denorm_try_harder_extra_use(float %x) {
759 ; CHECK-LABEL: @fdiv_constant_denominator_fmul_denorm_try_harder_extra_use(
760 ; CHECK-NEXT: [[T1:%.*]] = fdiv float [[X:%.*]], 3.000000e+00
761 ; CHECK-NEXT: [[T3:%.*]] = fmul fast float [[T1]], 0x3810000000000000
762 ; CHECK-NEXT: [[R:%.*]] = fadd float [[T1]], [[T3]]
763 ; CHECK-NEXT: ret float [[R]]
765 %t1 = fdiv float %x, 3.0e+0
766 %t3 = fmul fast float %t1, 0x3810000000000000
767 %r = fadd float %t1, %t3
771 ; (X + C1) * C2 --> (X * C2) + C1*C2
773 define float @fmul_fadd_distribute(float %x) {
774 ; CHECK-LABEL: @fmul_fadd_distribute(
775 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc float [[X:%.*]], 3.000000e+00
776 ; CHECK-NEXT: [[T3:%.*]] = fadd reassoc float [[TMP1]], 6.000000e+00
777 ; CHECK-NEXT: ret float [[T3]]
779 %t2 = fadd float %x, 2.0
780 %t3 = fmul reassoc float %t2, 3.0
784 define <2 x float> @fmul_fadd_distribute_vec(<2 x float> %x) {
785 ; CHECK-LABEL: @fmul_fadd_distribute_vec(
786 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc <2 x float> [[X:%.*]], <float 6.000000e+03, float 6.000000e+03>
787 ; CHECK-NEXT: [[T3:%.*]] = fadd reassoc <2 x float> [[TMP1]], <float 1.200000e+07, float 1.200000e+07>
788 ; CHECK-NEXT: ret <2 x float> [[T3]]
790 %t1 = fadd <2 x float> <float 2.0e+3, float 2.0e+3>, %x
791 %t3 = fmul reassoc <2 x float> %t1, <float 6.0e+3, float 6.0e+3>
795 define <vscale x 2 x float> @fmul_fadd_distribute_scalablevec(<vscale x 2 x float> %x) {
796 ; CHECK-LABEL: @fmul_fadd_distribute_scalablevec(
797 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc <vscale x 2 x float> [[X:%.*]], shufflevector (<vscale x 2 x float> insertelement (<vscale x 2 x float> undef, float 6.000000e+03, i32 0), <vscale x 2 x float> undef, <vscale x 2 x i32> zeroinitializer)
798 ; CHECK-NEXT: [[T3:%.*]] = fadd reassoc <vscale x 2 x float> [[TMP1]], shufflevector (<vscale x 2 x float> insertelement (<vscale x 2 x float> poison, float 1.200000e+07, i32 0), <vscale x 2 x float> poison, <vscale x 2 x i32> zeroinitializer)
799 ; CHECK-NEXT: ret <vscale x 2 x float> [[T3]]
801 %t1 = fadd <vscale x 2 x float> shufflevector (<vscale x 2 x float> insertelement (<vscale x 2 x float> undef, float 2.0e+3, i32 0), <vscale x 2 x float> undef, <vscale x 2 x i32> zeroinitializer), %x
802 %t3 = fmul reassoc <vscale x 2 x float> %t1, shufflevector (<vscale x 2 x float> insertelement (<vscale x 2 x float> undef, float 6.0e+3, i32 0), <vscale x 2 x float> undef, <vscale x 2 x i32> zeroinitializer)
805 ret <vscale x 2 x float> %t3
808 ; (X - C1) * C2 --> (X * C2) - C1*C2
810 define float @fmul_fsub_distribute1(float %x) {
811 ; CHECK-LABEL: @fmul_fsub_distribute1(
812 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc float [[X:%.*]], 3.000000e+00
813 ; CHECK-NEXT: [[T3:%.*]] = fadd reassoc float [[TMP1]], -6.000000e+00
814 ; CHECK-NEXT: ret float [[T3]]
816 %t2 = fsub float %x, 2.0
817 %t3 = fmul reassoc float %t2, 3.0
821 ; (C1 - X) * C2 --> C1*C2 - (X * C2)
823 define float @fmul_fsub_distribute2(float %x) {
824 ; CHECK-LABEL: @fmul_fsub_distribute2(
825 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc float [[X:%.*]], 3.000000e+00
826 ; CHECK-NEXT: [[T3:%.*]] = fsub reassoc float 6.000000e+00, [[TMP1]]
827 ; CHECK-NEXT: ret float [[T3]]
829 %t2 = fsub float 2.0, %x
830 %t3 = fmul reassoc float %t2, 3.0
834 ; FIXME: This should only need 'reassoc'.
835 ; ((X*C1) + C2) * C3 => (X * (C1*C3)) + (C2*C3)
837 define float @fmul_fadd_fmul_distribute(float %x) {
838 ; CHECK-LABEL: @fmul_fadd_fmul_distribute(
839 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[X:%.*]], 3.000000e+01
840 ; CHECK-NEXT: [[T3:%.*]] = fadd fast float [[TMP1]], 1.000000e+01
841 ; CHECK-NEXT: ret float [[T3]]
843 %t1 = fmul float %x, 6.0
844 %t2 = fadd float %t1, 2.0
845 %t3 = fmul fast float %t2, 5.0
849 define float @fmul_fadd_distribute_extra_use(float %x) {
850 ; CHECK-LABEL: @fmul_fadd_distribute_extra_use(
851 ; CHECK-NEXT: [[T1:%.*]] = fmul float [[X:%.*]], 6.000000e+00
852 ; CHECK-NEXT: [[T2:%.*]] = fadd float [[T1]], 2.000000e+00
853 ; CHECK-NEXT: [[T3:%.*]] = fmul fast float [[T2]], 5.000000e+00
854 ; CHECK-NEXT: call void @use_f32(float [[T2]])
855 ; CHECK-NEXT: ret float [[T3]]
857 %t1 = fmul float %x, 6.0
858 %t2 = fadd float %t1, 2.0
859 %t3 = fmul fast float %t2, 5.0
860 call void @use_f32(float %t2)
864 ; (X/C1 + C2) * C3 => X/(C1/C3) + C2*C3
865 ; 0x10000000000000 = DBL_MIN
866 ; TODO: We don't convert the fast fdiv to fmul because that would be multiplication
867 ; by a denormal, but we could do better when we know that denormals are not a problem.
869 define double @fmul_fadd_fdiv_distribute2(double %x) {
870 ; CHECK-LABEL: @fmul_fadd_fdiv_distribute2(
871 ; CHECK-NEXT: [[TMP1:%.*]] = fdiv reassoc double [[X:%.*]], 0x7FE8000000000000
872 ; CHECK-NEXT: [[T3:%.*]] = fadd reassoc double [[TMP1]], 0x34000000000000
873 ; CHECK-NEXT: ret double [[T3]]
875 %t1 = fdiv double %x, 3.0
876 %t2 = fadd double %t1, 5.0
877 %t3 = fmul reassoc double %t2, 0x10000000000000
881 ; 5.0e-1 * DBL_MIN yields denormal, so "(f1*3.0 + 5.0e-1) * DBL_MIN" cannot
882 ; be simplified into f1 * (3.0*DBL_MIN) + (5.0e-1*DBL_MIN)
884 define double @fmul_fadd_fdiv_distribute3(double %x) {
885 ; CHECK-LABEL: @fmul_fadd_fdiv_distribute3(
886 ; CHECK-NEXT: [[TMP1:%.*]] = fdiv reassoc double [[X:%.*]], 0x7FE8000000000000
887 ; CHECK-NEXT: [[T3:%.*]] = fadd reassoc double [[TMP1]], 0x34000000000000
888 ; CHECK-NEXT: ret double [[T3]]
890 %t1 = fdiv double %x, 3.0
891 %t2 = fadd double %t1, 5.0
892 %t3 = fmul reassoc double %t2, 0x10000000000000
896 ; FIXME: This should only need 'reassoc'.
897 ; (C2 - (X*C1)) * C3 => (C2*C3) - (X * (C1*C3))
899 define float @fmul_fsub_fmul_distribute(float %x) {
900 ; CHECK-LABEL: @fmul_fsub_fmul_distribute(
901 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[X:%.*]], 3.000000e+01
902 ; CHECK-NEXT: [[T3:%.*]] = fsub fast float 1.000000e+01, [[TMP1]]
903 ; CHECK-NEXT: ret float [[T3]]
905 %t1 = fmul float %x, 6.0
906 %t2 = fsub float 2.0, %t1
907 %t3 = fmul fast float %t2, 5.0
911 define float @fmul_fsub_fmul_distribute_extra_use(float %x) {
912 ; CHECK-LABEL: @fmul_fsub_fmul_distribute_extra_use(
913 ; CHECK-NEXT: [[T1:%.*]] = fmul float [[X:%.*]], 6.000000e+00
914 ; CHECK-NEXT: [[T2:%.*]] = fsub float 2.000000e+00, [[T1]]
915 ; CHECK-NEXT: [[T3:%.*]] = fmul fast float [[T2]], 5.000000e+00
916 ; CHECK-NEXT: call void @use_f32(float [[T2]])
917 ; CHECK-NEXT: ret float [[T3]]
919 %t1 = fmul float %x, 6.0
920 %t2 = fsub float 2.0, %t1
921 %t3 = fmul fast float %t2, 5.0
922 call void @use_f32(float %t2)
926 ; FIXME: This should only need 'reassoc'.
927 ; ((X*C1) - C2) * C3 => (X * (C1*C3)) - C2*C3
929 define float @fmul_fsub_fmul_distribute2(float %x) {
930 ; CHECK-LABEL: @fmul_fsub_fmul_distribute2(
931 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[X:%.*]], 3.000000e+01
932 ; CHECK-NEXT: [[T3:%.*]] = fadd fast float [[TMP1]], -1.000000e+01
933 ; CHECK-NEXT: ret float [[T3]]
935 %t1 = fmul float %x, 6.0
936 %t2 = fsub float %t1, 2.0
937 %t3 = fmul fast float %t2, 5.0
941 define float @fmul_fsub_fmul_distribute2_extra_use(float %x) {
942 ; CHECK-LABEL: @fmul_fsub_fmul_distribute2_extra_use(
943 ; CHECK-NEXT: [[T1:%.*]] = fmul float [[X:%.*]], 6.000000e+00
944 ; CHECK-NEXT: [[T2:%.*]] = fsub float 2.000000e+00, [[T1]]
945 ; CHECK-NEXT: [[T3:%.*]] = fmul fast float [[T2]], 5.000000e+00
946 ; CHECK-NEXT: call void @use_f32(float [[T2]])
947 ; CHECK-NEXT: ret float [[T3]]
949 %t1 = fmul float %x, 6.0
950 %t2 = fsub float 2.0, %t1
951 %t3 = fmul fast float %t2, 5.0
952 call void @use_f32(float %t2)
956 ; "(X*Y) * X => (X*X) * Y" is disabled if "X*Y" has multiple uses
958 define float @common_factor(float %x, float %y) {
959 ; CHECK-LABEL: @common_factor(
960 ; CHECK-NEXT: [[MUL:%.*]] = fmul float [[X:%.*]], [[Y:%.*]]
961 ; CHECK-NEXT: [[MUL1:%.*]] = fmul fast float [[MUL]], [[X]]
962 ; CHECK-NEXT: [[ADD:%.*]] = fadd float [[MUL1]], [[MUL]]
963 ; CHECK-NEXT: ret float [[ADD]]
965 %mul = fmul float %x, %y
966 %mul1 = fmul fast float %mul, %x
967 %add = fadd float %mul1, %mul
971 define double @fmul_fdiv_factor_squared(double %x, double %y) {
972 ; CHECK-LABEL: @fmul_fdiv_factor_squared(
973 ; CHECK-NEXT: [[DIV:%.*]] = fdiv fast double [[X:%.*]], [[Y:%.*]]
974 ; CHECK-NEXT: [[SQUARED:%.*]] = fmul fast double [[DIV]], [[DIV]]
975 ; CHECK-NEXT: ret double [[SQUARED]]
977 %div = fdiv fast double %x, %y
978 %squared = fmul fast double %div, %div
982 define double @fmul_fdivs_factor_common_denominator(double %x, double %y, double %z) {
983 ; CHECK-LABEL: @fmul_fdivs_factor_common_denominator(
984 ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast double [[Y:%.*]], [[X:%.*]]
985 ; CHECK-NEXT: [[TMP2:%.*]] = fmul fast double [[Z:%.*]], [[Z]]
986 ; CHECK-NEXT: [[MUL:%.*]] = fdiv fast double [[TMP1]], [[TMP2]]
987 ; CHECK-NEXT: ret double [[MUL]]
989 %div1 = fdiv double %x, %z
990 %div2 = fdiv double %y, %z
991 %mul = fmul fast double %div1, %div2
995 define double @fmul_fdivs_factor(double %x, double %y, double %z, double %w) {
996 ; CHECK-LABEL: @fmul_fdivs_factor(
997 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc double [[Z:%.*]], [[X:%.*]]
998 ; CHECK-NEXT: [[TMP2:%.*]] = fdiv reassoc double [[TMP1]], [[W:%.*]]
999 ; CHECK-NEXT: [[MUL:%.*]] = fdiv reassoc double [[TMP2]], [[Y:%.*]]
1000 ; CHECK-NEXT: ret double [[MUL]]
1002 %div1 = fdiv double %x, %y
1003 %div2 = fdiv double %z, %w
1004 %mul = fmul reassoc double %div1, %div2
1008 define double @fmul_fdiv_factor(double %x, double %y, double %z) {
1009 ; CHECK-LABEL: @fmul_fdiv_factor(
1010 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc double [[X:%.*]], [[Z:%.*]]
1011 ; CHECK-NEXT: [[MUL:%.*]] = fdiv reassoc double [[TMP1]], [[Y:%.*]]
1012 ; CHECK-NEXT: ret double [[MUL]]
1014 %div = fdiv double %x, %y
1015 %mul = fmul reassoc double %div, %z
1019 define double @fmul_fdiv_factor_constant1(double %x, double %y) {
1020 ; CHECK-LABEL: @fmul_fdiv_factor_constant1(
1021 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc double [[X:%.*]], 4.200000e+01
1022 ; CHECK-NEXT: [[MUL:%.*]] = fdiv reassoc double [[TMP1]], [[Y:%.*]]
1023 ; CHECK-NEXT: ret double [[MUL]]
1025 %div = fdiv double %x, %y
1026 %mul = fmul reassoc double %div, 42.0
1030 define <2 x float> @fmul_fdiv_factor_constant2(<2 x float> %x, <2 x float> %y) {
1031 ; CHECK-LABEL: @fmul_fdiv_factor_constant2(
1032 ; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc <2 x float> [[X:%.*]], [[Y:%.*]]
1033 ; CHECK-NEXT: [[MUL:%.*]] = fdiv reassoc <2 x float> [[TMP1]], <float 4.200000e+01, float 1.200000e+01>
1034 ; CHECK-NEXT: ret <2 x float> [[MUL]]
1036 %div = fdiv <2 x float> %x, <float 42.0, float 12.0>
1037 %mul = fmul reassoc <2 x float> %div, %y
1038 ret <2 x float> %mul
1041 define float @fmul_fdiv_factor_extra_use(float %x, float %y) {
1042 ; CHECK-LABEL: @fmul_fdiv_factor_extra_use(
1043 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[X:%.*]], 4.200000e+01
1044 ; CHECK-NEXT: call void @use_f32(float [[DIV]])
1045 ; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc float [[DIV]], [[Y:%.*]]
1046 ; CHECK-NEXT: ret float [[MUL]]
1048 %div = fdiv float %x, 42.0
1049 call void @use_f32(float %div)
1050 %mul = fmul reassoc float %div, %y
1054 ; Avoid infinite looping by moving negation out of a constant expression.
1056 @g = external global {[2 x i8*]}, align 1
1058 define double @fmul_negated_constant_expression(double %x) {
1059 ; CHECK-LABEL: @fmul_negated_constant_expression(
1060 ; CHECK-NEXT: [[R:%.*]] = fmul double [[X:%.*]], fsub (double -0.000000e+00, double bitcast (i64 ptrtoint (i8** getelementptr inbounds ({ [2 x i8*] }, { [2 x i8*] }* @g, i64 0, inrange i32 0, i64 2) to i64) to double))
1061 ; CHECK-NEXT: ret double [[R]]
1063 %r = fmul double %x, fsub (double -0.000000e+00, double bitcast (i64 ptrtoint (i8** getelementptr inbounds ({ [2 x i8*] }, { [2 x i8*] }* @g, i64 0, inrange i32 0, i64 2) to i64) to double))
1067 define float @negate_if_true(float %x, i1 %cond) {
1068 ; CHECK-LABEL: @negate_if_true(
1069 ; CHECK-NEXT: [[TMP1:%.*]] = fneg float [[X:%.*]]
1070 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[COND:%.*]], float [[TMP1]], float [[X]]
1071 ; CHECK-NEXT: ret float [[TMP2]]
1073 %sel = select i1 %cond, float -1.0, float 1.0
1074 %r = fmul float %sel, %x
1078 define float @negate_if_false(float %x, i1 %cond) {
1079 ; CHECK-LABEL: @negate_if_false(
1080 ; CHECK-NEXT: [[TMP1:%.*]] = fneg arcp float [[X:%.*]]
1081 ; CHECK-NEXT: [[TMP2:%.*]] = select arcp i1 [[COND:%.*]], float [[X]], float [[TMP1]]
1082 ; CHECK-NEXT: ret float [[TMP2]]
1084 %sel = select i1 %cond, float 1.0, float -1.0
1085 %r = fmul arcp float %sel, %x
1089 define <2 x double> @negate_if_true_commute(<2 x double> %px, i1 %cond) {
1090 ; CHECK-LABEL: @negate_if_true_commute(
1091 ; CHECK-NEXT: [[X:%.*]] = fdiv <2 x double> <double 4.200000e+01, double 4.200000e+01>, [[PX:%.*]]
1092 ; CHECK-NEXT: [[TMP1:%.*]] = fneg ninf <2 x double> [[X]]
1093 ; CHECK-NEXT: [[TMP2:%.*]] = select ninf i1 [[COND:%.*]], <2 x double> [[TMP1]], <2 x double> [[X]]
1094 ; CHECK-NEXT: ret <2 x double> [[TMP2]]
1096 %x = fdiv <2 x double> <double 42.0, double 42.0>, %px ; thwart complexity-based canonicalization
1097 %sel = select i1 %cond, <2 x double> <double -1.0, double -1.0>, <2 x double> <double 1.0, double 1.0>
1098 %r = fmul ninf <2 x double> %x, %sel
1102 define <2 x double> @negate_if_false_commute(<2 x double> %px, <2 x i1> %cond) {
1103 ; CHECK-LABEL: @negate_if_false_commute(
1104 ; CHECK-NEXT: [[X:%.*]] = fdiv <2 x double> <double 4.200000e+01, double 5.100000e+00>, [[PX:%.*]]
1105 ; CHECK-NEXT: [[TMP1:%.*]] = fneg <2 x double> [[X]]
1106 ; CHECK-NEXT: [[TMP2:%.*]] = select <2 x i1> [[COND:%.*]], <2 x double> [[X]], <2 x double> [[TMP1]]
1107 ; CHECK-NEXT: ret <2 x double> [[TMP2]]
1109 %x = fdiv <2 x double> <double 42.0, double 5.1>, %px ; thwart complexity-based canonicalization
1110 %sel = select <2 x i1> %cond, <2 x double> <double 1.0, double 1.0>, <2 x double> <double -1.0, double -1.0>
1111 %r = fmul <2 x double> %x, %sel
1117 define float @negate_if_true_extra_use(float %x, i1 %cond) {
1118 ; CHECK-LABEL: @negate_if_true_extra_use(
1119 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], float -1.000000e+00, float 1.000000e+00
1120 ; CHECK-NEXT: call void @use_f32(float [[SEL]])
1121 ; CHECK-NEXT: [[R:%.*]] = fmul float [[SEL]], [[X:%.*]]
1122 ; CHECK-NEXT: ret float [[R]]
1124 %sel = select i1 %cond, float -1.0, float 1.0
1125 call void @use_f32(float %sel)
1126 %r = fmul float %sel, %x
1132 define <2 x double> @negate_if_true_wrong_constant(<2 x double> %px, i1 %cond) {
1133 ; CHECK-LABEL: @negate_if_true_wrong_constant(
1134 ; CHECK-NEXT: [[X:%.*]] = fdiv <2 x double> <double 4.200000e+01, double 4.200000e+01>, [[PX:%.*]]
1135 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], <2 x double> <double -1.000000e+00, double 0.000000e+00>, <2 x double> <double 1.000000e+00, double 1.000000e+00>
1136 ; CHECK-NEXT: [[R:%.*]] = fmul <2 x double> [[X]], [[SEL]]
1137 ; CHECK-NEXT: ret <2 x double> [[R]]
1139 %x = fdiv <2 x double> <double 42.0, double 42.0>, %px ; thwart complexity-based canonicalization
1140 %sel = select i1 %cond, <2 x double> <double -1.0, double 0.0>, <2 x double> <double 1.0, double 1.0>
1141 %r = fmul <2 x double> %x, %sel
1145 ; X *fast (C ? 1.0 : 0.0) -> C ? X : 0.0
1146 define float @fmul_select(float %x, i1 %c) {
1147 ; CHECK-LABEL: @fmul_select(
1148 ; CHECK-NEXT: [[MUL:%.*]] = select fast i1 [[C:%.*]], float [[X:%.*]], float 0.000000e+00
1149 ; CHECK-NEXT: ret float [[MUL]]
1151 %sel = select i1 %c, float 1.0, float 0.0
1152 %mul = fmul fast float %sel, %x
1156 ; X *fast (C ? 1.0 : 0.0) -> C ? X : 0.0
1157 define <2 x float> @fmul_select_vec(<2 x float> %x, i1 %c) {
1158 ; CHECK-LABEL: @fmul_select_vec(
1159 ; CHECK-NEXT: [[MUL:%.*]] = select fast i1 [[C:%.*]], <2 x float> [[X:%.*]], <2 x float> zeroinitializer
1160 ; CHECK-NEXT: ret <2 x float> [[MUL]]
1162 %sel = select i1 %c, <2 x float> <float 1.0, float 1.0>, <2 x float> zeroinitializer
1163 %mul = fmul fast <2 x float> %sel, %x
1164 ret <2 x float> %mul
1167 ; Without fast math flags we can't optimize X * (C ? 1.0 : 0.0) -> C ? X : 0.0
1168 define float @fmul_select_strict(float %x, i1 %c) {
1169 ; CHECK-LABEL: @fmul_select_strict(
1170 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C:%.*]], float 1.000000e+00, float 0.000000e+00
1171 ; CHECK-NEXT: [[MUL:%.*]] = fmul float [[SEL]], [[X:%.*]]
1172 ; CHECK-NEXT: ret float [[MUL]]
1174 %sel = select i1 %c, float 1.0, float 0.0
1175 %mul = fmul float %sel, %x
1179 ; sqrt(X) *fast (C ? sqrt(X) : 1.0) -> C ? X : sqrt(X)
1180 define double @fmul_sqrt_select(double %x, i1 %c) {
1181 ; CHECK-LABEL: @fmul_sqrt_select(
1182 ; CHECK-NEXT: [[SQR:%.*]] = call double @llvm.sqrt.f64(double [[X:%.*]])
1183 ; CHECK-NEXT: [[MUL:%.*]] = select fast i1 [[C:%.*]], double [[X]], double [[SQR]]
1184 ; CHECK-NEXT: ret double [[MUL]]
1186 %sqr = call double @llvm.sqrt.f64(double %x)
1187 %sel = select i1 %c, double %sqr, double 1.0
1188 %mul = fmul fast double %sqr, %sel
1192 ; fastmath => z * splat(0) = splat(0), even for scalable vectors
1193 define <vscale x 2 x float> @mul_scalable_splat_zero(<vscale x 2 x float> %z) {
1194 ; CHECK-LABEL: @mul_scalable_splat_zero(
1195 ; CHECK-NEXT: ret <vscale x 2 x float> zeroinitializer
1197 %shuf = shufflevector <vscale x 2 x float> insertelement (<vscale x 2 x float> undef, float 0.0, i32 0), <vscale x 2 x float> undef, <vscale x 2 x i32> zeroinitializer
1198 %t3 = fmul fast <vscale x 2 x float> %shuf, %z
1199 ret <vscale x 2 x float> %t3