[llvm] bcf5e68 - [InstCombine] add tests for fadd+fmul factorization fold; NFC

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 17 08:00:00 PST 2022


Author: Sanjay Patel
Date: 2022-01-17T10:38:05-05:00
New Revision: bcf5e687c917ed35c41395b92a5af8f162f89efc

URL: https://github.com/llvm/llvm-project/commit/bcf5e687c917ed35c41395b92a5af8f162f89efc
DIFF: https://github.com/llvm/llvm-project/commit/bcf5e687c917ed35c41395b92a5af8f162f89efc.diff

LOG: [InstCombine] add tests for fadd+fmul factorization fold; NFC

As shown in the tests, this transform exists partially already.

Added: 
    

Modified: 
    llvm/test/Transforms/InstCombine/fadd.ll

Removed: 
    


################################################################################
diff  --git a/llvm/test/Transforms/InstCombine/fadd.ll b/llvm/test/Transforms/InstCombine/fadd.ll
index 8191375fda44..d74d58117756 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:%.*]], <float 4.200000e+01, float 4.200000e+01>
+; 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, <float 42.0, float 42.0>
+  %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]], <float 4.200000e+01, float -4.300000e+01>
+; 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, <float 42.0, float -43.0>
+  %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
+}


        


More information about the llvm-commits mailing list