https://gcc.gnu.org/g:4be7d2d340a013d01a47c43d2feb6826d1b67af0

commit r15-4267-g4be7d2d340a013d01a47c43d2feb6826d1b67af0
Author: Jennifer Schmitz <jschm...@nvidia.com>
Date:   Wed Sep 25 03:21:22 2024 -0700

    match.pd: Fold logarithmic identities.
    
    This patch implements 4 rules for logarithmic identities in match.pd
    under -funsafe-math-optimizations:
    1) logN(1.0/a) -> -logN(a). This avoids the division instruction.
    2) logN(C/a) -> logN(C) - logN(a), where C is a real constant. Same as 1).
    3) logN(a) + logN(b) -> logN(a*b). This reduces the number of calls to
    log function.
    4) logN(a) - logN(b) -> logN(a/b). Same as 4).
    Tests were added for float, double, and long double.
    
    The patch was bootstrapped and regtested on aarch64-linux-gnu and
    x86_64-linux-gnu, no regression.
    Additionally, SPEC 2017 fprate was run. While the transform does not seem
    to be triggered, we also see no non-noise impact on performance.
    OK for mainline?
    
    Signed-off-by: Jennifer Schmitz <jschm...@nvidia.com>
    
    gcc/
            PR tree-optimization/116826
            PR tree-optimization/86710
            * match.pd: Fold logN(1.0/a) -> -logN(a),
            logN(C/a) -> logN(C) - logN(a), logN(a) + logN(b) -> logN(a*b),
            and logN(a) - logN(b) -> logN(a/b).
    
    gcc/testsuite/
            PR tree-optimization/116826
            PR tree-optimization/86710
            * gcc.dg/tree-ssa/log_ident.c: New test.

Diff:
---
 gcc/match.pd                              | 25 ++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/log_ident.c | 56 +++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index 5ed1ea033053..78084bb38582 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -8182,6 +8182,31 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
    (rdiv @0 (exps:s @1))
     (mult @0 (exps (negate @1)))))
 
+ (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
+      && ! HONOR_NANS (type) && ! HONOR_INFINITIES (type)
+      && ! flag_trapping_math
+      && ! flag_errno_math)
+  (for logs (LOG LOG2 LOG10)
+   /* Simplify logN(1.0/a) into -logN(a).  */
+   (simplify
+    (logs (rdiv:s real_onep@0 @1))
+     (negate (logs @1)))
+
+   /* Simplify logN(C/a) into logN(C)-logN(a).  */
+   (simplify
+    (logs (rdiv:s REAL_CST@0 @1))
+     (minus (logs! @0) (logs @1)))
+
+   /* Simplify logN(a)+logN(b) into logN(a*b).  */
+   (simplify
+    (plus (logs:s @0) (logs:s @1))
+     (logs (mult @0 @1)))
+
+   /* Simplify logN(a)-logN(b) into logN(a/b).  */
+   (simplify
+    (minus (logs:s @0) (logs:s @1))
+     (logs (rdiv @0 @1)))))
+
  (for logs (LOG LOG2 LOG10 LOG10)
       exps (EXP EXP2 EXP10 POW10)
   /* logN(expN(x)) -> x.  */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/log_ident.c 
b/gcc/testsuite/gcc.dg/tree-ssa/log_ident.c
new file mode 100644
index 000000000000..80528beb7238
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/log_ident.c
@@ -0,0 +1,56 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -ffast-math" } */
+/* { dg-require-effective-target c99_runtime } */
+
+extern void link_error(void);
+
+#define T(TYPE, C_TY, FNAME)                                   \
+  void f_##FNAME##_1 (TYPE a)                                  \
+  {                                                            \
+    TYPE t1 = 1.0##C_TY / a;                                   \
+    TYPE t2 = __builtin_##FNAME (t1);                          \
+    TYPE t3 = __builtin_##FNAME (a);                           \
+    TYPE t4 = -t3;                                             \
+    if (t2 != t4)                                              \
+      link_error ();                                           \
+  }                                                            \
+  void f_##FNAME##_2 (TYPE a)                                  \
+  {                                                            \
+    TYPE t1 = 2.0##C_TY / a;                                   \
+    TYPE t2 = __builtin_##FNAME (t1);                          \
+    TYPE t3 = __builtin_##FNAME (2.0);                         \
+    TYPE t4 = __builtin_##FNAME (a);                           \
+    TYPE t5 = t3 - t4;                                         \
+    if (t2 != t5)                                              \
+      link_error ();                                           \
+  }                                                            \
+  void f_##FNAME##_3 (TYPE a, TYPE b)                          \
+  {                                                            \
+    TYPE t1 = __builtin_##FNAME (a);                           \
+    TYPE t2 = __builtin_##FNAME (b);                           \
+    TYPE t3 = t1 + t2;                                         \
+    TYPE t4 = a * b;                                           \
+    TYPE t5 = __builtin_##FNAME (t4);                          \
+    if (t3 != t5)                                              \
+      link_error ();                                           \
+  }                                                            \
+  void f_##FNAME##_4 (TYPE a, TYPE b)                          \
+  {                                                            \
+    TYPE t1 = __builtin_##FNAME (a);                           \
+    TYPE t2 = __builtin_##FNAME (b);                           \
+    TYPE t3 = t1 - t2;                                         \
+    TYPE t4 = a / b;                                           \
+    TYPE t5 = __builtin_##FNAME (t4);                          \
+    if (t3 != t5)                                              \
+      link_error ();                                           \
+  }
+
+#define TEST_LOGS(TYPE, C_TY, F_TY)                            \
+  T (TYPE, C_TY, log##F_TY)                                    \
+  T (TYPE, C_TY, log2##F_TY)                                   \
+  T (TYPE, C_TY, log10##F_TY)
+
+
+TEST_LOGS (double, , )
+TEST_LOGS (float, f, f)
+TEST_LOGS (long double, L, l)

Reply via email to