PR77644

Message ID CAAgBjMmQDKGJ6eLjajzLRkemQpG60LxK35EU8fkqODJDCAzzvw@mail.gmail.com
State New
Headers show

Commit Message

Prathamesh Kulkarni May 9, 2017, 4:01 p.m.
Hi,
The attached patch adds the following pattern to match.pd
sqrt(x) cmp sqrt(y) -> x cmp y.
and is enabled with -funsafe-math-optimization and -fno-math-errno.

Bootstrapped+tested on x86_64-unknown-linux-gnu.
Cross-tested on arm*-*-*, aarch64*-*-*.
OK for trunk ?

Thanks,
Prathamesh
2017-05-09  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>

	PR tree-optimization/77644
	* match.pd (sqrt(x) cmp sqrt(y) -> x cmp y): New pattern.

testsuite/
	* gcc.dg/tree-ssa/pr77644.c: New test-case.

Comments

Richard Biener May 10, 2017, 8:58 a.m. | #1
On Tue, 9 May 2017, Prathamesh Kulkarni wrote:

> Hi,

> The attached patch adds the following pattern to match.pd

> sqrt(x) cmp sqrt(y) -> x cmp y.

> and is enabled with -funsafe-math-optimization and -fno-math-errno.

> 

> Bootstrapped+tested on x86_64-unknown-linux-gnu.

> Cross-tested on arm*-*-*, aarch64*-*-*.

> OK for trunk ?


+          (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
+
+   /* PR77644: Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */

Do not reference PRs here please (and omit the vertical space before
the sub-pattern.

+   (simplify
+    (cmp (sq @0) (sq @1))
+      (if (! HONOR_NANS (type))
+       (cmp @0 @1))))))
 
It should be HONOR_NANS (@0), and not on 'type' (that's bool!).

Looks ok otherwise.

Thanks,
Richard.
Prathamesh Kulkarni May 10, 2017, 9:26 a.m. | #2
On 10 May 2017 at 14:28, Richard Biener <rguenther@suse.de> wrote:
> On Tue, 9 May 2017, Prathamesh Kulkarni wrote:

>

>> Hi,

>> The attached patch adds the following pattern to match.pd

>> sqrt(x) cmp sqrt(y) -> x cmp y.

>> and is enabled with -funsafe-math-optimization and -fno-math-errno.

>>

>> Bootstrapped+tested on x86_64-unknown-linux-gnu.

>> Cross-tested on arm*-*-*, aarch64*-*-*.

>> OK for trunk ?

>

> +          (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))

> +

> +   /* PR77644: Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */

>

> Do not reference PRs here please (and omit the vertical space before

> the sub-pattern.

>

> +   (simplify

> +    (cmp (sq @0) (sq @1))

> +      (if (! HONOR_NANS (type))

> +       (cmp @0 @1))))))

>

> It should be HONOR_NANS (@0), and not on 'type' (that's bool!).

Ah indeed, sorry about that :/
Does the attached version look OK ?

Thanks,
Prathamesh
>

> Looks ok otherwise.

>

> Thanks,

> Richard.diff --git a/gcc/match.pd b/gcc/match.pd
index e3d98baa12f..80a17ba3d23 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2633,7 +2633,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 	 (if (GENERIC)
 	  (truth_andif
 	   (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
-	   (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
+	   (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
+   /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
+   (simplify
+    (cmp (sq @0) (sq @1))
+      (if (! HONOR_NANS (@0))
+	(cmp @0 @1))))))
 
 /* Fold A /[ex] B CMP C to A CMP B * C.  */
 (for cmp (eq ne)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr77644.c b/gcc/testsuite/gcc.dg/tree-ssa/pr77644.c
new file mode 100644
index 00000000000..c73bb73afdb
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr77644.c
@@ -0,0 +1,28 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target c99_runtime } */
+/* { dg-options "-O2 -fdump-tree-optimized -funsafe-math-optimizations -fno-math-errno -ffinite-math-only" } */
+
+#define FOO(type, cmp, suffix, no)  \
+int f_##no(type x, type y) \
+{ \
+  type gen_##no(); \
+  type xs = __builtin_sqrt##suffix((gen_##no())); \
+  type xy = __builtin_sqrt##suffix((gen_##no())); \
+  return (xs cmp xy); \
+}
+
+#define GEN_FOO(type, suffix) \
+FOO(type, <, suffix, suffix##1) \
+FOO(type, <=, suffix, suffix##2) \
+FOO(type, >, suffix, suffix##3) \
+FOO(type, >=, suffix, suffix##4) \
+FOO(type, ==, suffix, suffix##5) \
+FOO(type, !=, suffix, suffix##6)
+
+GEN_FOO(float, f)
+GEN_FOO(double, )
+GEN_FOO(long double, l)
+
+/* { dg-final { scan-tree-dump-not "__builtin_sqrtf" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "__builtin_sqrt" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "__builtin_sqrtl" "optimized" } } */

Richard Biener May 10, 2017, 9:50 a.m. | #3
On Wed, 10 May 2017, Prathamesh Kulkarni wrote:

> On 10 May 2017 at 14:28, Richard Biener <rguenther@suse.de> wrote:

> > On Tue, 9 May 2017, Prathamesh Kulkarni wrote:

> >

> >> Hi,

> >> The attached patch adds the following pattern to match.pd

> >> sqrt(x) cmp sqrt(y) -> x cmp y.

> >> and is enabled with -funsafe-math-optimization and -fno-math-errno.

> >>

> >> Bootstrapped+tested on x86_64-unknown-linux-gnu.

> >> Cross-tested on arm*-*-*, aarch64*-*-*.

> >> OK for trunk ?

> >

> > +          (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))

> > +

> > +   /* PR77644: Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */

> >

> > Do not reference PRs here please (and omit the vertical space before

> > the sub-pattern.

> >

> > +   (simplify

> > +    (cmp (sq @0) (sq @1))

> > +      (if (! HONOR_NANS (type))

> > +       (cmp @0 @1))))))

> >

> > It should be HONOR_NANS (@0), and not on 'type' (that's bool!).

> Ah indeed, sorry about that :/

> Does the attached version look OK ?


Yes.

Thanks,
Richard.

Patch hide | download patch | download mbox

diff --git a/gcc/match.pd b/gcc/match.pd
index e3d98baa12f..9929f5a1c16 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2633,7 +2633,13 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 	 (if (GENERIC)
 	  (truth_andif
 	   (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
-	   (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
+	   (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
+
+   /* PR77644: Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
+   (simplify
+    (cmp (sq @0) (sq @1))
+      (if (! HONOR_NANS (type))
+	(cmp @0 @1))))))
 
 /* Fold A /[ex] B CMP C to A CMP B * C.  */
 (for cmp (eq ne)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr77644.c b/gcc/testsuite/gcc.dg/tree-ssa/pr77644.c
new file mode 100644
index 00000000000..30da66374e6
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr77644.c
@@ -0,0 +1,28 @@ 
+/* { dg-do compile } */
+/* { dg-require-effective-target c99_runtime } */
+/* { dg-options "-O2 -fdump-tree-optimized -funsafe-math-optimizations -fno-math-errno" } */
+
+#define FOO(type, cmp, suffix, no)  \
+int f_##no(type x, type y) \
+{ \
+  type gen_##no(); \
+  type xs = __builtin_sqrt##suffix((gen_##no())); \
+  type xy = __builtin_sqrt##suffix((gen_##no())); \
+  return (xs cmp xy); \
+}
+
+#define GEN_FOO(type, suffix) \
+FOO(type, <, suffix, suffix##1) \
+FOO(type, <=, suffix, suffix##2) \
+FOO(type, >, suffix, suffix##3) \
+FOO(type, >=, suffix, suffix##4) \
+FOO(type, ==, suffix, suffix##5) \
+FOO(type, !=, suffix, suffix##6)
+
+GEN_FOO(float, f)
+GEN_FOO(double, )
+GEN_FOO(long double, l)
+
+/* { dg-final { scan-tree-dump-not "__builtin_sqrtf" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "__builtin_sqrt" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "__builtin_sqrtl" "optimized" } } */