public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 1/2] Improve/Fix (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 pattern.
@ 2021-11-22  6:24 apinski
  2021-11-22  6:24 ` [PATCH 2/2] tree-optimization: [PR92342] Move b & -(a==c) optimization to the gimple level apinski
  2021-11-22 11:30 ` [PATCH 1/2] Improve/Fix (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 pattern Richard Biener
  0 siblings, 2 replies; 4+ messages in thread
From: apinski @ 2021-11-22  6:24 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

From: Andrew Pinski <apinski@marvell.com>

The pattern here was not catching all comparisons and the multiply
was not commutative when it should have been. This patches fixes
that by using tcc_comparison and adding :c to the multiply.

OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

gcc/ChangeLog:

	* match.pd ((m1 CMP m2) * d -> (m1 CMP m2) ? d : 0):
	Use tcc_comparison and :c for the multiply.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/multcmp-1.c: New test.
	* gcc.dg/tree-ssa/multcmp-2.c: New test.
---
 gcc/match.pd                              |  4 ++--
 gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c | 12 ++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c | 12 ++++++++++++
 3 files changed, 26 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c

diff --git a/gcc/match.pd b/gcc/match.pd
index ca6c9eff624..ed43c321cbc 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1791,9 +1791,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 
 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
 (if (!canonicalize_math_p ())
- (for cmp (gt lt ge le)
+ (for cmp (tcc_comparison)
   (simplify
-   (mult (convert (cmp @0 @1)) @2)
+   (mult:c (convert (cmp @0 @1)) @2)
    (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
 
 /* For integral types with undefined overflow and C != 0 fold
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c
new file mode 100644
index 00000000000..fb44cacde77
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+f (int m1, int m2, int c)
+{
+  int d = m1 == m2;
+  int e = d * c;
+  return e;
+}
+
+/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c
new file mode 100644
index 00000000000..be38b2e0044
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+f (int m1, int m2, int c)
+{
+  int d = m1 != m2;
+  int e = c * d;
+  return e;
+}
+
+/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */
-- 
2.17.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/2] tree-optimization: [PR92342] Move b & -(a==c) optimization to the gimple level
  2021-11-22  6:24 [PATCH 1/2] Improve/Fix (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 pattern apinski
@ 2021-11-22  6:24 ` apinski
  2021-11-22 11:34   ` Richard Biener
  2021-11-22 11:30 ` [PATCH 1/2] Improve/Fix (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 pattern Richard Biener
  1 sibling, 1 reply; 4+ messages in thread
From: apinski @ 2021-11-22  6:24 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

From: Andrew Pinski <apinski@marvell.com>

Combine disabled this optimization in r10-254-gddbb5da5199fb42 but it makes
sense to do this on the gimple level and then let expand decide which way is
better. So this adds the transformation on the gimple level (late like was
done for the multiply case).

OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

	PR tree-optimization/92342

gcc/ChangeLog:

	* match.pd (b & -(a CMP c) -> (a CMP c)?b:0): New pattern.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/andnegcmp-1.c: New test.
	* gcc.dg/tree-ssa/andnegcmp-2.c: New test.
---
 gcc/match.pd                                |  8 +++++++-
 gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c | 14 ++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c | 14 ++++++++++++++
 3 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c

diff --git a/gcc/match.pd b/gcc/match.pd
index ed43c321cbc..b55cbc91b57 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1794,7 +1794,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (for cmp (tcc_comparison)
   (simplify
    (mult:c (convert (cmp @0 @1)) @2)
-   (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
+   (cond (cmp @0 @1) @2 { build_zero_cst (type); }))
+/* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
+  (simplify
+   (bit_and:c (negate (convert (cmp @0 @1))) @2)
+   (cond (cmp @0 @1) @2 { build_zero_cst (type); }))
+ )
+)
 
 /* For integral types with undefined overflow and C != 0 fold
    x * C EQ/NE y * C into x EQ/NE y.  */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c
new file mode 100644
index 00000000000..6f16783f169
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/92342 */
+
+int
+f (int m1, int m2, int c)
+{
+  int d = m1 == m2;
+  d = -d;
+  int e = d & c;
+  return e;
+}
+
+/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c
new file mode 100644
index 00000000000..0e25c8abc39
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/92342 */
+
+int
+f (int m1, int m2, int c)
+{
+  int d = m1 < m2;
+  d = -d;
+  int e = c & d;
+  return e;
+}
+
+/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */
-- 
2.17.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] Improve/Fix (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 pattern.
  2021-11-22  6:24 [PATCH 1/2] Improve/Fix (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 pattern apinski
  2021-11-22  6:24 ` [PATCH 2/2] tree-optimization: [PR92342] Move b & -(a==c) optimization to the gimple level apinski
@ 2021-11-22 11:30 ` Richard Biener
  1 sibling, 0 replies; 4+ messages in thread
From: Richard Biener @ 2021-11-22 11:30 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: GCC Patches

On Mon, Nov 22, 2021 at 7:25 AM apinski--- via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> From: Andrew Pinski <apinski@marvell.com>
>
> The pattern here was not catching all comparisons and the multiply
> was not commutative when it should have been. This patches fixes
> that by using tcc_comparison and adding :c to the multiply.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

OK.

Thanks,
Richard.

> gcc/ChangeLog:
>
>         * match.pd ((m1 CMP m2) * d -> (m1 CMP m2) ? d : 0):
>         Use tcc_comparison and :c for the multiply.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/tree-ssa/multcmp-1.c: New test.
>         * gcc.dg/tree-ssa/multcmp-2.c: New test.
> ---
>  gcc/match.pd                              |  4 ++--
>  gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c | 12 ++++++++++++
>  gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c | 12 ++++++++++++
>  3 files changed, 26 insertions(+), 2 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index ca6c9eff624..ed43c321cbc 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -1791,9 +1791,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>
>  /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
>  (if (!canonicalize_math_p ())
> - (for cmp (gt lt ge le)
> + (for cmp (tcc_comparison)
>    (simplify
> -   (mult (convert (cmp @0 @1)) @2)
> +   (mult:c (convert (cmp @0 @1)) @2)
>     (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
>
>  /* For integral types with undefined overflow and C != 0 fold
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c
> new file mode 100644
> index 00000000000..fb44cacde77
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c
> @@ -0,0 +1,12 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +
> +int
> +f (int m1, int m2, int c)
> +{
> +  int d = m1 == m2;
> +  int e = d * c;
> +  return e;
> +}
> +
> +/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c
> new file mode 100644
> index 00000000000..be38b2e0044
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c
> @@ -0,0 +1,12 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +
> +int
> +f (int m1, int m2, int c)
> +{
> +  int d = m1 != m2;
> +  int e = c * d;
> +  return e;
> +}
> +
> +/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */
> --
> 2.17.1
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] tree-optimization: [PR92342] Move b & -(a==c) optimization to the gimple level
  2021-11-22  6:24 ` [PATCH 2/2] tree-optimization: [PR92342] Move b & -(a==c) optimization to the gimple level apinski
@ 2021-11-22 11:34   ` Richard Biener
  0 siblings, 0 replies; 4+ messages in thread
From: Richard Biener @ 2021-11-22 11:34 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: GCC Patches

On Mon, Nov 22, 2021 at 7:26 AM apinski--- via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> From: Andrew Pinski <apinski@marvell.com>
>
> Combine disabled this optimization in r10-254-gddbb5da5199fb42 but it makes
> sense to do this on the gimple level and then let expand decide which way is
> better. So this adds the transformation on the gimple level (late like was
> done for the multiply case).
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
>
>         PR tree-optimization/92342
>
> gcc/ChangeLog:
>
>         * match.pd (b & -(a CMP c) -> (a CMP c)?b:0): New pattern.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/tree-ssa/andnegcmp-1.c: New test.
>         * gcc.dg/tree-ssa/andnegcmp-2.c: New test.
> ---
>  gcc/match.pd                                |  8 +++++++-
>  gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c | 14 ++++++++++++++
>  gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c | 14 ++++++++++++++
>  3 files changed, 35 insertions(+), 1 deletion(-)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index ed43c321cbc..b55cbc91b57 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -1794,7 +1794,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>   (for cmp (tcc_comparison)
>    (simplify
>     (mult:c (convert (cmp @0 @1)) @2)
> -   (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
> +   (cond (cmp @0 @1) @2 { build_zero_cst (type); }))
> +/* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
> +  (simplify
> +   (bit_and:c (negate (convert (cmp @0 @1))) @2)

I think you need to guard against signed bools (and vector compares,
and allow view_convert for vector compare results?)?

> +   (cond (cmp @0 @1) @2 { build_zero_cst (type); }))
> + )
> +)
>
>  /* For integral types with undefined overflow and C != 0 fold
>     x * C EQ/NE y * C into x EQ/NE y.  */
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c
> new file mode 100644
> index 00000000000..6f16783f169
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c
> @@ -0,0 +1,14 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +/* PR tree-optimization/92342 */
> +
> +int
> +f (int m1, int m2, int c)
> +{
> +  int d = m1 == m2;
> +  d = -d;
> +  int e = d & c;
> +  return e;
> +}
> +
> +/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c
> new file mode 100644
> index 00000000000..0e25c8abc39
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c
> @@ -0,0 +1,14 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +/* PR tree-optimization/92342 */
> +
> +int
> +f (int m1, int m2, int c)
> +{
> +  int d = m1 < m2;
> +  d = -d;
> +  int e = c & d;
> +  return e;
> +}
> +
> +/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */
> --
> 2.17.1
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-11-22 11:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-22  6:24 [PATCH 1/2] Improve/Fix (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 pattern apinski
2021-11-22  6:24 ` [PATCH 2/2] tree-optimization: [PR92342] Move b & -(a==c) optimization to the gimple level apinski
2021-11-22 11:34   ` Richard Biener
2021-11-22 11:30 ` [PATCH 1/2] Improve/Fix (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 pattern Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).