aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorJennifer Schmitz <jschmitz@nvidia.com>2024-07-03 14:40:42 +0200
committerKyrylo Tkachov <ktkachov@nvidia.com>2024-07-03 14:43:01 +0200
commit8dc5ad3ce8d4d2cd6cc2b7516d282395502fdf7d (patch)
tree6428e44be83fc797406dd42f01921945d57dc85f /gcc
parent640f0f3e2b771e23665924f24527e6b1a5db8d3c (diff)
downloadgcc-8dc5ad3ce8d4d2cd6cc2b7516d282395502fdf7d.zip
gcc-8dc5ad3ce8d4d2cd6cc2b7516d282395502fdf7d.tar.gz
gcc-8dc5ad3ce8d4d2cd6cc2b7516d282395502fdf7d.tar.bz2
[PATCH] match.pd: Fold x/sqrt(x) to sqrt(x)
This patch adds a pattern in match.pd folding x/sqrt(x) to sqrt(x) for -funsafe-math-optimizations. Test cases were added for double, float, and long double. The patch was bootstrapped and regtested on aarch64-linux-gnu, no regression. Ok for mainline? Signed-off-by: Jennifer Schmitz <jschmitz@nvidia.com> gcc/ * match.pd: Fold x/sqrt(x) to sqrt(x). gcc/testsuite/ * gcc.dg/tree-ssa/sqrt_div.c: New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/match.pd4
-rw-r--r--gcc/testsuite/gcc.dg/tree-ssa/sqrt_div.c23
2 files changed, 27 insertions, 0 deletions
diff --git a/gcc/match.pd b/gcc/match.pd
index 7fff7b5..a2e205b 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -7770,6 +7770,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
when the operand has that value.) */
(if (flag_unsafe_math_optimizations)
+ /* Simplify x / sqrt(x) -> sqrt(x). */
+ (simplify
+ (rdiv @0 (SQRT @0)) (SQRT @0))
+
/* Simplify sqrt(x) * sqrt(x) -> x. */
(simplify
(mult (SQRT_ALL@1 @0) @1)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/sqrt_div.c b/gcc/testsuite/gcc.dg/tree-ssa/sqrt_div.c
new file mode 100644
index 0000000..2ae481b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/sqrt_div.c
@@ -0,0 +1,23 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -ffast-math -fdump-tree-forwprop-details" } */
+/* { dg-require-effective-target c99_runtime } */
+
+#define T(n, type, fname) \
+type f##n (type x) \
+{ \
+ type t1 = __builtin_##fname (x); \
+ type t2 = x / t1; \
+ return t2; \
+}
+
+T(1, double, sqrt)
+
+/* { dg-final { scan-tree-dump "gimple_simplified to t2_\[0-9\]+ = __builtin_sqrt .x_\[0-9\]*.D.." "forwprop1" } } */
+
+T(2, float, sqrtf )
+
+/* { dg-final { scan-tree-dump "gimple_simplified to t2_\[0-9\]+ = __builtin_sqrtf .x_\[0-9\]*.D.." "forwprop1" } } */
+
+T(3, long double, sqrtl)
+
+/* { dg-final { scan-tree-dump "gimple_simplified to t2_\[0-9\]+ = __builtin_sqrtl .x_\[0-9\]*.D.." "forwprop1" } } */