[1/2] Improve/Fix (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 pattern.

Message ID 1637562290-15155-1-git-send-email-apinski@marvell.com
State New
Headers
Series [1/2] Improve/Fix (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 pattern. |

Commit Message

Li, Pan2 via Gcc-patches Nov. 22, 2021, 6:24 a.m. UTC
  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
  

Comments

Richard Biener Nov. 22, 2021, 11:30 a.m. UTC | #1
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
>
  

Patch

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" } } */