MATCH: Improve `A CMP 0 ? A : -A` set of patterns to use bitwise_equal_p.

Message ID 20231015215956.512326-1-pinskia@gmail.com
State Accepted
Headers
Series MATCH: Improve `A CMP 0 ? A : -A` set of patterns to use bitwise_equal_p. |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Andrew Pinski Oct. 15, 2023, 9:59 p.m. UTC
  This improves the `A CMP 0 ? A : -A` set of match patterns to use
bitwise_equal_p which allows an nop cast between signed and unsigned.
This allows catching a few extra cases which were not being caught before.

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

gcc/ChangeLog:

	PR tree-optimization/101541
	* match.pd (A CMP 0 ? A : -A): Improve
	using bitwise_equal_p.

gcc/testsuite/ChangeLog:

	PR tree-optimization/101541
	* gcc.dg/tree-ssa/phi-opt-36.c: New test.
	* gcc.dg/tree-ssa/phi-opt-37.c: New test.
---
 gcc/match.pd                               | 49 ++++++++++++---------
 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c | 51 ++++++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c | 24 ++++++++++
 3 files changed, 104 insertions(+), 20 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c
  

Comments

Richard Biener Oct. 16, 2023, 9:09 a.m. UTC | #1
On Mon, Oct 16, 2023 at 12:00 AM Andrew Pinski <pinskia@gmail.com> wrote:
>
> This improves the `A CMP 0 ? A : -A` set of match patterns to use
> bitwise_equal_p which allows an nop cast between signed and unsigned.
> This allows catching a few extra cases which were not being caught before.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

OK.

> gcc/ChangeLog:
>
>         PR tree-optimization/101541
>         * match.pd (A CMP 0 ? A : -A): Improve
>         using bitwise_equal_p.
>
> gcc/testsuite/ChangeLog:
>
>         PR tree-optimization/101541
>         * gcc.dg/tree-ssa/phi-opt-36.c: New test.
>         * gcc.dg/tree-ssa/phi-opt-37.c: New test.
> ---
>  gcc/match.pd                               | 49 ++++++++++++---------
>  gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c | 51 ++++++++++++++++++++++
>  gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c | 24 ++++++++++
>  3 files changed, 104 insertions(+), 20 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 45624f3dcb4..142e2dfbeb1 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -5668,42 +5668,51 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>   /* A == 0 ? A : -A    same as -A */
>   (for cmp (eq uneq)
>    (simplify
> -   (cnd (cmp @0 zerop) @0 (negate@1 @0))
> -    (if (!HONOR_SIGNED_ZEROS (type))
> +   (cnd (cmp @0 zerop) @2 (negate@1 @2))
> +    (if (!HONOR_SIGNED_ZEROS (type)
> +        && bitwise_equal_p (@0, @2))
>       @1))
>    (simplify
> -   (cnd (cmp @0 zerop) zerop (negate@1 @0))
> -    (if (!HONOR_SIGNED_ZEROS (type))
> +   (cnd (cmp @0 zerop) zerop (negate@1 @2))
> +    (if (!HONOR_SIGNED_ZEROS (type)
> +        && bitwise_equal_p (@0, @2))
>       @1))
>   )
>   /* A != 0 ? A : -A    same as A */
>   (for cmp (ne ltgt)
>    (simplify
> -   (cnd (cmp @0 zerop) @0 (negate @0))
> -    (if (!HONOR_SIGNED_ZEROS (type))
> -     @0))
> +   (cnd (cmp @0 zerop) @1 (negate @1))
> +    (if (!HONOR_SIGNED_ZEROS (type)
> +        && bitwise_equal_p (@0, @1))
> +     @1))
>    (simplify
> -   (cnd (cmp @0 zerop) @0 integer_zerop)
> -    (if (!HONOR_SIGNED_ZEROS (type))
> -     @0))
> +   (cnd (cmp @0 zerop) @1 integer_zerop)
> +    (if (!HONOR_SIGNED_ZEROS (type)
> +        && bitwise_equal_p (@0, @1))
> +     @1))
>   )
>   /* A >=/> 0 ? A : -A    same as abs (A) */
>   (for cmp (ge gt)
>    (simplify
> -   (cnd (cmp @0 zerop) @0 (negate @0))
> -    (if (!HONOR_SIGNED_ZEROS (type)
> -        && !TYPE_UNSIGNED (type))
> -     (abs @0))))
> +   (cnd (cmp @0 zerop) @1 (negate @1))
> +    (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
> +        && !TYPE_UNSIGNED (TREE_TYPE(@0))
> +        && bitwise_equal_p (@0, @1))
> +     (if (TYPE_UNSIGNED (type))
> +      (absu:type @0)
> +      (abs @0)))))
>   /* A <=/< 0 ? A : -A    same as -abs (A) */
>   (for cmp (le lt)
>    (simplify
> -   (cnd (cmp @0 zerop) @0 (negate @0))
> -    (if (!HONOR_SIGNED_ZEROS (type)
> -        && !TYPE_UNSIGNED (type))
> -     (if (ANY_INTEGRAL_TYPE_P (type)
> -         && !TYPE_OVERFLOW_WRAPS (type))
> +   (cnd (cmp @0 zerop) @1 (negate @1))
> +    (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
> +        && !TYPE_UNSIGNED (TREE_TYPE(@0))
> +        && bitwise_equal_p (@0, @1))
> +     (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
> +          && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
> +         || TYPE_UNSIGNED (type))
>        (with {
> -       tree utype = unsigned_type_for (type);
> +       tree utype = unsigned_type_for (TREE_TYPE(@0));
>         }
>         (convert (negate (absu:utype @0))))
>         (negate (abs @0)))))
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c
> new file mode 100644
> index 00000000000..4baf9f82a22
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c
> @@ -0,0 +1,51 @@
> +/* { dg-options "-O2 -fdump-tree-phiopt" } */
> +
> +unsigned f0(int A)
> +{
> +  unsigned t = A;
> +//     A == 0? A : -A    same as -A
> +  if (A == 0)  return t;
> +  return -t;
> +}
> +
> +unsigned f1(int A)
> +{
> +  unsigned t = A;
> +//     A != 0? A : -A    same as A
> +  if (A != 0)  return t;
> +  return -t;
> +}
> +unsigned f2(int A)
> +{
> +  unsigned t = A;
> +//     A >= 0? A : -A    same as abs (A)
> +  if (A >= 0)  return t;
> +  return -t;
> +}
> +unsigned f3(int A)
> +{
> +  unsigned t = A;
> +//     A > 0?  A : -A    same as abs (A)
> +  if (A > 0)  return t;
> +  return -t;
> +}
> +unsigned f4(int A)
> +{
> +  unsigned t = A;
> +//     A <= 0? A : -A    same as -abs (A)
> +  if (A <= 0)  return t;
> +  return -t;
> +}
> +unsigned f5(int A)
> +{
> +  unsigned t = A;
> +//     A < 0?  A : -A    same as -abs (A)
> +  if (A < 0)  return t;
> +  return -t;
> +}
> +
> +/* f4 and f5 are not allowed to be optimized in early phi-opt. */
> +/* { dg-final { scan-tree-dump-times "if " 2 "phiopt1" } } */
> +/* { dg-final { scan-tree-dump-not "if " "phiopt2" } } */
> +
> +
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c
> new file mode 100644
> index 00000000000..f1ff472aaff
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c
> @@ -0,0 +1,24 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O1 -fdump-tree-phiopt1" } */
> +
> +unsigned abs_with_convert0 (int x)
> +{
> +    unsigned int y = x;
> +
> +    if (x < 0)
> +        y = -y;
> +
> +  return y;
> +}
> +unsigned abs_with_convert1 (unsigned x)
> +{
> +    int y = x;
> +
> +    if (y < 0)
> +        x = -x;
> +
> +  return x;
> +}
> +
> +/* { dg-final { scan-tree-dump-times "ABSU_EXPR <"  2  "phiopt1" } } */
> +/* { dg-final { scan-tree-dump-not   "if "        "phiopt1" } } */
> --
> 2.39.3
>
  

Patch

diff --git a/gcc/match.pd b/gcc/match.pd
index 45624f3dcb4..142e2dfbeb1 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -5668,42 +5668,51 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  /* A == 0 ? A : -A    same as -A */
  (for cmp (eq uneq)
   (simplify
-   (cnd (cmp @0 zerop) @0 (negate@1 @0))
-    (if (!HONOR_SIGNED_ZEROS (type))
+   (cnd (cmp @0 zerop) @2 (negate@1 @2))
+    (if (!HONOR_SIGNED_ZEROS (type)
+	 && bitwise_equal_p (@0, @2))
      @1))
   (simplify
-   (cnd (cmp @0 zerop) zerop (negate@1 @0))
-    (if (!HONOR_SIGNED_ZEROS (type))
+   (cnd (cmp @0 zerop) zerop (negate@1 @2))
+    (if (!HONOR_SIGNED_ZEROS (type)
+	 && bitwise_equal_p (@0, @2))
      @1))
  )
  /* A != 0 ? A : -A    same as A */
  (for cmp (ne ltgt)
   (simplify
-   (cnd (cmp @0 zerop) @0 (negate @0))
-    (if (!HONOR_SIGNED_ZEROS (type))
-     @0))
+   (cnd (cmp @0 zerop) @1 (negate @1))
+    (if (!HONOR_SIGNED_ZEROS (type)
+	 && bitwise_equal_p (@0, @1))
+     @1))
   (simplify
-   (cnd (cmp @0 zerop) @0 integer_zerop)
-    (if (!HONOR_SIGNED_ZEROS (type))
-     @0))
+   (cnd (cmp @0 zerop) @1 integer_zerop)
+    (if (!HONOR_SIGNED_ZEROS (type)
+	 && bitwise_equal_p (@0, @1))
+     @1))
  )
  /* A >=/> 0 ? A : -A    same as abs (A) */
  (for cmp (ge gt)
   (simplify
-   (cnd (cmp @0 zerop) @0 (negate @0))
-    (if (!HONOR_SIGNED_ZEROS (type)
-	 && !TYPE_UNSIGNED (type))
-     (abs @0))))
+   (cnd (cmp @0 zerop) @1 (negate @1))
+    (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
+	 && !TYPE_UNSIGNED (TREE_TYPE(@0))
+	 && bitwise_equal_p (@0, @1))
+     (if (TYPE_UNSIGNED (type))
+      (absu:type @0)
+      (abs @0)))))
  /* A <=/< 0 ? A : -A    same as -abs (A) */
  (for cmp (le lt)
   (simplify
-   (cnd (cmp @0 zerop) @0 (negate @0))
-    (if (!HONOR_SIGNED_ZEROS (type)
-	 && !TYPE_UNSIGNED (type))
-     (if (ANY_INTEGRAL_TYPE_P (type)
-	  && !TYPE_OVERFLOW_WRAPS (type))
+   (cnd (cmp @0 zerop) @1 (negate @1))
+    (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
+	 && !TYPE_UNSIGNED (TREE_TYPE(@0))
+	 && bitwise_equal_p (@0, @1))
+     (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
+	   && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
+	  || TYPE_UNSIGNED (type))
       (with {
-	tree utype = unsigned_type_for (type);
+	tree utype = unsigned_type_for (TREE_TYPE(@0));
        }
        (convert (negate (absu:utype @0))))
        (negate (abs @0)))))
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c
new file mode 100644
index 00000000000..4baf9f82a22
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c
@@ -0,0 +1,51 @@ 
+/* { dg-options "-O2 -fdump-tree-phiopt" } */
+
+unsigned f0(int A)
+{
+  unsigned t = A;
+//     A == 0? A : -A    same as -A
+  if (A == 0)  return t;
+  return -t;
+}
+
+unsigned f1(int A)
+{
+  unsigned t = A;
+//     A != 0? A : -A    same as A
+  if (A != 0)  return t;
+  return -t;
+}
+unsigned f2(int A)
+{
+  unsigned t = A;
+//     A >= 0? A : -A    same as abs (A)
+  if (A >= 0)  return t;
+  return -t;
+}
+unsigned f3(int A)
+{
+  unsigned t = A;
+//     A > 0?  A : -A    same as abs (A)
+  if (A > 0)  return t;
+  return -t;
+}
+unsigned f4(int A)
+{
+  unsigned t = A;
+//     A <= 0? A : -A    same as -abs (A)
+  if (A <= 0)  return t;
+  return -t;
+}
+unsigned f5(int A)
+{
+  unsigned t = A;
+//     A < 0?  A : -A    same as -abs (A)
+  if (A < 0)  return t;
+  return -t;
+}
+
+/* f4 and f5 are not allowed to be optimized in early phi-opt. */
+/* { dg-final { scan-tree-dump-times "if " 2 "phiopt1" } } */
+/* { dg-final { scan-tree-dump-not "if " "phiopt2" } } */
+
+
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c
new file mode 100644
index 00000000000..f1ff472aaff
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c
@@ -0,0 +1,24 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-phiopt1" } */
+
+unsigned abs_with_convert0 (int x)
+{
+    unsigned int y = x;
+
+    if (x < 0)
+        y = -y;
+
+  return y;
+}
+unsigned abs_with_convert1 (unsigned x)
+{
+    int y = x;
+
+    if (y < 0)
+        x = -x;
+
+  return x;
+}
+
+/* { dg-final { scan-tree-dump-times "ABSU_EXPR <"  2  "phiopt1" } } */
+/* { dg-final { scan-tree-dump-not   "if "        "phiopt1" } } */