aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVitor Guidi <vitor.guidi@usp.br>2020-06-03 17:01:21 -0600
committerJeff Law <law@redhat.com>2020-06-03 17:01:21 -0600
commit29e304fd5f9097335c5d7d1b16f139439eeabe1f (patch)
treeb600d7d8c33f52e02cc021659ed599916f173660
parent817738fdf1ea368e47da4822dfe9ee1a7deb477b (diff)
downloadgcc-29e304fd5f9097335c5d7d1b16f139439eeabe1f.zip
gcc-29e304fd5f9097335c5d7d1b16f139439eeabe1f.tar.gz
gcc-29e304fd5f9097335c5d7d1b16f139439eeabe1f.tar.bz2
optimize tanh(x) / sinh (x) to 1/ cosh (x)
gcc/ * match.pd (tanh/sinh -> 1/cosh): New simplification. gcc/testsuite * gcc.dg/tanhbysinh.c: New testcase.
-rw-r--r--gcc/match.pd5
-rw-r--r--gcc/testsuite/gcc.dg/tanhbysinh.c40
2 files changed, 45 insertions, 0 deletions
diff --git a/gcc/match.pd b/gcc/match.pd
index 33ee1a9..2b8c37e 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -5229,6 +5229,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(rdiv (SINH:s @0) (COSH:s @0))
(TANH @0))
+ /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
+ (simplify
+ (rdiv (TANH:s @0) (SINH:s @0))
+ (rdiv {build_one_cst (type);} (COSH @0)))
+
/* Simplify cos(x) / sin(x) -> 1 / tan(x). */
(simplify
(rdiv (COS:s @0) (SIN:s @0))
diff --git a/gcc/testsuite/gcc.dg/tanhbysinh.c b/gcc/testsuite/gcc.dg/tanhbysinh.c
new file mode 100644
index 0000000..fde72c2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tanhbysinh.c
@@ -0,0 +1,40 @@
+/* { dg-do compile } */
+/* { dg-options "-Ofast -fdump-tree-optimized" } */
+
+extern float sinhf (float);
+extern float tanhf (float);
+extern double sinh (double);
+extern double tanh (double);
+extern long double sinhl (long double);
+extern long double tanhl (long double);
+
+double __attribute__ ((noinline))
+tanhbysinh_ (double x)
+{
+ return tanh (x) / sinh (x);
+}
+
+float __attribute__ ((noinline))
+tanhbysinhf_ (float x)
+{
+ return tanhf (x) / sinhf (x);
+}
+
+long double __attribute__ ((noinline))
+tanhbysinhl_ (long double x)
+{
+ return tanhl (x) / sinhl (x);
+}
+
+
+/* There must be no calls to sinh or atanh */
+/* There must be calls to cosh */
+/* {dg-final { scan-tree-dump-not "sinh " "optimized" } } */
+/* {dg-final { scan-tree-dump-not "tanh " "optimized" }} */
+/* {dg-final { scan-tree-dump-not "sinhf " "optimized" } } */
+/* {dg-final { scan-tree-dump-not "tanhf " "optimized" }} */
+/* {dg-final { scan-tree-dump-not "sinhl " "optimized" } } */
+/* {dg-final { scan-tree-dump-not "tanhl " "optimized" }} */
+/* { dg-final { scan-tree-dump "cosh " "optimized" } } */
+/* { dg-final { scan-tree-dump "coshf " "optimized" } } */
+/* { dg-final { scan-tree-dump "coshl " "optimized" } } */ \ No newline at end of file