]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[PATCH] match.pd: Fold x/sqrt(x) to sqrt(x)
authorJennifer Schmitz <jschmitz@nvidia.com>
Wed, 3 Jul 2024 12:40:42 +0000 (14:40 +0200)
committerKyrylo Tkachov <ktkachov@nvidia.com>
Wed, 3 Jul 2024 12:43:01 +0000 (14:43 +0200)
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.

gcc/match.pd
gcc/testsuite/gcc.dg/tree-ssa/sqrt_div.c [new file with mode: 0644]

index 7fff7b5f9fe9a8a9cd478b934f6851c89314e8ef..a2e205b3207141f2acdf98f52d3c0dca8e2afae9 100644 (file)
@@ -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 (file)
index 0000000..2ae481b
--- /dev/null
@@ -0,0 +1,23 @@
+/* { dg-do compile } */\r
+/* { dg-options "-O2 -ffast-math -fdump-tree-forwprop-details" } */\r
+/* { dg-require-effective-target c99_runtime } */\r
+\r
+#define T(n, type, fname)          \\r
+type f##n (type x)                 \\r
+{                                  \\r
+  type t1 = __builtin_##fname (x); \\r
+  type t2 = x / t1;                \\r
+  return t2;                       \\r
+}                               \r
+\r
+T(1, double, sqrt)\r
+\r
+/* { dg-final { scan-tree-dump "gimple_simplified to t2_\[0-9\]+ = __builtin_sqrt .x_\[0-9\]*.D.." "forwprop1" } }     */\r
+\r
+T(2, float, sqrtf )\r
+\r
+/* { dg-final { scan-tree-dump "gimple_simplified to t2_\[0-9\]+ = __builtin_sqrtf .x_\[0-9\]*.D.." "forwprop1" } }     */\r
+\r
+T(3, long double, sqrtl)\r
+\r
+/* { dg-final { scan-tree-dump "gimple_simplified to t2_\[0-9\]+ = __builtin_sqrtl .x_\[0-9\]*.D.." "forwprop1" } } */\r