[PR31531] MATCH: Improve ~a < ~b and ~a < CST, allow a nop cast inbetween ~ and a/b

Message ID 20231016023357.3394538-1-pinskia@gmail.com
State Accepted
Headers
Series [PR31531] MATCH: Improve ~a < ~b and ~a < CST, allow a nop cast inbetween ~ and a/b |

Checks

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

Commit Message

Andrew Pinski Oct. 16, 2023, 2:33 a.m. UTC
  Currently we able to simplify `~a CMP ~b` to `b CMP a` but we should allow a nop
conversion in between the `~` and the `a` which can show up. A similarly thing should
be done for `~a CMP CST`.

I had originally submitted the `~a CMP CST` case as
https://gcc.gnu.org/pipermail/gcc-patches/2021-November/585088.html;
I noticed we should do the same thing for the `~a CMP ~b` case and combined
it with that one here.

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

	PR tree-optimization/31531

gcc/ChangeLog:

	* match.pd (~X op ~Y): Allow for an optional nop convert.
	(~X op C): Likewise.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/pr31531-1.c: New test.
	* gcc.dg/tree-ssa/pr31531-2.c: New test.
---
 gcc/match.pd                              | 10 ++++---
 gcc/testsuite/gcc.dg/tree-ssa/pr31531-1.c | 19 +++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr31531-2.c | 34 +++++++++++++++++++++++
 3 files changed, 59 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr31531-1.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr31531-2.c
  

Comments

Richard Biener Oct. 16, 2023, 9:54 a.m. UTC | #1
On Mon, Oct 16, 2023 at 4:34 AM Andrew Pinski <pinskia@gmail.com> wrote:
>
> Currently we able to simplify `~a CMP ~b` to `b CMP a` but we should allow a nop
> conversion in between the `~` and the `a` which can show up. A similarly thing should
> be done for `~a CMP CST`.
>
> I had originally submitted the `~a CMP CST` case as
> https://gcc.gnu.org/pipermail/gcc-patches/2021-November/585088.html;
> I noticed we should do the same thing for the `~a CMP ~b` case and combined
> it with that one here.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

OK.

>         PR tree-optimization/31531
>
> gcc/ChangeLog:
>
>         * match.pd (~X op ~Y): Allow for an optional nop convert.
>         (~X op C): Likewise.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/tree-ssa/pr31531-1.c: New test.
>         * gcc.dg/tree-ssa/pr31531-2.c: New test.
> ---
>  gcc/match.pd                              | 10 ++++---
>  gcc/testsuite/gcc.dg/tree-ssa/pr31531-1.c | 19 +++++++++++++
>  gcc/testsuite/gcc.dg/tree-ssa/pr31531-2.c | 34 +++++++++++++++++++++++
>  3 files changed, 59 insertions(+), 4 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr31531-1.c
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr31531-2.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 51e5065d086..e76ec1ec034 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -5944,18 +5944,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>  /* Fold ~X op ~Y as Y op X.  */
>  (for cmp (simple_comparison)
>   (simplify
> -  (cmp (bit_not@2 @0) (bit_not@3 @1))
> +  (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
>    (if (single_use (@2) && single_use (@3))
> -   (cmp @1 @0))))
> +   (with { tree otype = TREE_TYPE (@4); }
> +    (cmp (convert:otype @1) (convert:otype @0))))))
>
>  /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
>  (for cmp (simple_comparison)
>       scmp (swapped_simple_comparison)
>   (simplify
> -  (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
> +  (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
>    (if (single_use (@2)
>         && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
> -   (scmp @0 (bit_not @1)))))
> +   (with { tree otype = TREE_TYPE (@1); }
> +    (scmp (convert:otype @0) (bit_not @1))))))
>
>  (for cmp (simple_comparison)
>   (simplify
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr31531-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr31531-1.c
> new file mode 100644
> index 00000000000..c27299151eb
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr31531-1.c
> @@ -0,0 +1,19 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +/* PR tree-optimization/31531 */
> +
> +int f(int a)
> +{
> +  int b = ~a;
> +  return b<0;
> +}
> +
> +
> +int f1(unsigned a)
> +{
> +  int b = ~a;
> +  return b<0;
> +}
> +/* We should convert the above two functions from b <0 to ((int)a) >= 0. */
> +/* { dg-final { scan-tree-dump-times ">= 0" 2 "optimized"} } */
> +/* { dg-final { scan-tree-dump-times "~" 0 "optimized"} } */
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr31531-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr31531-2.c
> new file mode 100644
> index 00000000000..865ea292215
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr31531-2.c
> @@ -0,0 +1,34 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +/* PR tree-optimization/31531 */
> +
> +int f0(unsigned x, unsigned t)
> +{
> +        x = ~x;
> +        t = ~t;
> +        int xx = x;
> +        int tt = t;
> +        return tt < xx;
> +}
> +
> +int f1(unsigned x, int t)
> +{
> +        x = ~x;
> +        t = ~t;
> +        int xx = x;
> +        int tt = t;
> +        return tt < xx;
> +}
> +
> +int f2(int x, unsigned t)
> +{
> +        x = ~x;
> +        t = ~t;
> +        int xx = x;
> +        int tt = t;
> +        return tt < xx;
> +}
> +
> +
> +/* We should be able to remove all ~ from the above functions. */
> +/* { dg-final { scan-tree-dump-times "~" 0 "optimized"} } */
> --
> 2.39.3
>
  

Patch

diff --git a/gcc/match.pd b/gcc/match.pd
index 51e5065d086..e76ec1ec034 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -5944,18 +5944,20 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 /* Fold ~X op ~Y as Y op X.  */
 (for cmp (simple_comparison)
  (simplify
-  (cmp (bit_not@2 @0) (bit_not@3 @1))
+  (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
   (if (single_use (@2) && single_use (@3))
-   (cmp @1 @0))))
+   (with { tree otype = TREE_TYPE (@4); }
+    (cmp (convert:otype @1) (convert:otype @0))))))
 
 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
 (for cmp (simple_comparison)
      scmp (swapped_simple_comparison)
  (simplify
-  (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
+  (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
   (if (single_use (@2)
        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
-   (scmp @0 (bit_not @1)))))
+   (with { tree otype = TREE_TYPE (@1); }
+    (scmp (convert:otype @0) (bit_not @1))))))
 
 (for cmp (simple_comparison)
  (simplify
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr31531-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr31531-1.c
new file mode 100644
index 00000000000..c27299151eb
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr31531-1.c
@@ -0,0 +1,19 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/31531 */
+
+int f(int a)
+{
+  int b = ~a;
+  return b<0;
+}
+
+
+int f1(unsigned a)
+{
+  int b = ~a;
+  return b<0;
+}
+/* We should convert the above two functions from b <0 to ((int)a) >= 0. */
+/* { dg-final { scan-tree-dump-times ">= 0" 2 "optimized"} } */
+/* { dg-final { scan-tree-dump-times "~" 0 "optimized"} } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr31531-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr31531-2.c
new file mode 100644
index 00000000000..865ea292215
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr31531-2.c
@@ -0,0 +1,34 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/31531 */
+
+int f0(unsigned x, unsigned t)
+{
+        x = ~x;
+        t = ~t;
+        int xx = x;
+        int tt = t;
+        return tt < xx;
+}
+
+int f1(unsigned x, int t)
+{
+        x = ~x;
+        t = ~t;
+        int xx = x;
+        int tt = t;
+        return tt < xx;
+}
+
+int f2(int x, unsigned t)
+{
+        x = ~x;
+        t = ~t;
+        int xx = x;
+        int tt = t;
+        return tt < xx;
+}
+
+
+/* We should be able to remove all ~ from the above functions. */
+/* { dg-final { scan-tree-dump-times "~" 0 "optimized"} } */