From bcf5e687c917ed35c41395b92a5af8f162f89efc Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Mon, 17 Jan 2022 10:03:58 -0500 Subject: [PATCH] [InstCombine] add tests for fadd+fmul factorization fold; NFC As shown in the tests, this transform exists partially already. --- llvm/test/Transforms/InstCombine/fadd.ll | 60 ++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/llvm/test/Transforms/InstCombine/fadd.ll b/llvm/test/Transforms/InstCombine/fadd.ll index 8191375..d74d581 100644 --- a/llvm/test/Transforms/InstCombine/fadd.ll +++ b/llvm/test/Transforms/InstCombine/fadd.ll @@ -461,3 +461,63 @@ define float @fadd_rdx_nonzero_start_variable_op(float %x, <4 x float> %v) { %add = fadd fast float %rdx, %x ret float %add } + +; (X * C) + X --> X * (C + 1) + +define float @fadd_fmul_common_op(float %x) { +; CHECK-LABEL: @fadd_fmul_common_op( +; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc nsz float [[X:%.*]], 4.300000e+01 +; CHECK-NEXT: ret float [[TMP1]] +; + %m = fmul reassoc nsz float %x, 42.0 + %a = fadd reassoc nsz float %m, %x + ret float %a +} + +define <2 x float> @fadd_fmul_common_op_vec(<2 x float> %x) { +; CHECK-LABEL: @fadd_fmul_common_op_vec( +; CHECK-NEXT: [[M:%.*]] = fmul reassoc nsz <2 x float> [[X:%.*]], +; CHECK-NEXT: [[A:%.*]] = fadd reassoc nsz <2 x float> [[M]], [[X]] +; CHECK-NEXT: ret <2 x float> [[A]] +; + %m = fmul reassoc nsz <2 x float> %x, + %a = fadd reassoc nsz <2 x float> %m, %x + ret <2 x float> %a +} + +define <2 x float> @fadd_fmul_common_op_commute_vec(<2 x float> %px) { +; CHECK-LABEL: @fadd_fmul_common_op_commute_vec( +; CHECK-NEXT: [[X:%.*]] = fmul <2 x float> [[PX:%.*]], [[PX]] +; CHECK-NEXT: [[M:%.*]] = fmul reassoc nsz <2 x float> [[X]], +; CHECK-NEXT: [[A:%.*]] = fadd reassoc nsz <2 x float> [[X]], [[M]] +; CHECK-NEXT: ret <2 x float> [[A]] +; + %x = fmul <2 x float> %px, %px ; thwart complexity-based canonicalization + %m = fmul reassoc nsz <2 x float> %x, + %a = fadd reassoc nsz <2 x float> %x, %m + ret <2 x float> %a +} + +define float @fadd_fmul_common_op_use(float %x) { +; CHECK-LABEL: @fadd_fmul_common_op_use( +; CHECK-NEXT: [[M:%.*]] = fmul reassoc nsz float [[X:%.*]], 4.200000e+01 +; CHECK-NEXT: call void @use(float [[M]]) +; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc nsz float [[X]], 4.300000e+01 +; CHECK-NEXT: ret float [[TMP1]] +; + %m = fmul reassoc nsz float %x, 42.0 + call void @use(float %m) + %a = fadd reassoc nsz float %m, %x + ret float %a +} + +define float @fadd_fmul_common_op_wrong_fmf(float %x) { +; CHECK-LABEL: @fadd_fmul_common_op_wrong_fmf( +; CHECK-NEXT: [[M:%.*]] = fmul ninf nsz float [[X:%.*]], 4.200000e+01 +; CHECK-NEXT: [[A:%.*]] = fadd ninf nsz float [[M]], [[X]] +; CHECK-NEXT: ret float [[A]] +; + %m = fmul ninf nsz float %x, 42.0 + %a = fadd ninf nsz float %m, %x + ret float %a +} -- 2.7.4