From 30b5dc92a3e539577e966766d2fe35fd6d0aa148 Mon Sep 17 00:00:00 2001
From: Jennifer Schmitz <jschmitz@nvidia.com>
Date: Wed, 25 Sep 2024 03:21:22 -0700
Subject: [PATCH] [PR86710][PR116826] 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 <jschmitz@nvidia.com>

gcc/
	* 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/
	* gcc.dg/tree-ssa/log_ident.c: New test.
---
 gcc/match.pd                              | 21 +++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/log_ident.c | 43 +++++++++++++++++++++++
 2 files changed, 64 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/log_ident.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 940292d0d49..b08fec525ce 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -8031,6 +8031,27 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
    (rdiv @0 (exps:s @1))
     (mult @0 (exps (negate @1)))))
 
+ (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 00000000000..9619a57f8de
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/log_ident.c
@@ -0,0 +1,43 @@
+/* { 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)					\
+  {								\
+    if (__builtin_##FNAME (1.0##C_TY / a)			\
+	!= -__builtin_##FNAME (a))				\
+      link_error ();						\
+  }								\
+  void f_##FNAME##_2 (TYPE a)					\
+  {								\
+    if (__builtin_##FNAME (2.0##C_TY / a)			\
+	!= (__builtin_##FNAME (2.0) - __builtin_##FNAME (a)))	\
+      link_error ();						\
+  }								\
+  void f_##FNAME##_3 (TYPE a, TYPE b)				\
+  {								\
+    TYPE t1 = __builtin_##FNAME (a);				\
+    TYPE t2 = __builtin_##FNAME (b);				\
+    if (t1 + t2 != __builtin_##FNAME (t1 * t2))			\
+      link_error ();						\
+  }								\
+  void f_##FNAME##_4 (TYPE a, TYPE b)				\
+  {								\
+    TYPE t1 = __builtin_##FNAME (a);				\
+    TYPE t2 = __builtin_##FNAME (b);				\
+    if (t1 - t2 != __builtin_##FNAME (t1 / t2))			\
+      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)
-- 
2.44.0

