MATCH: Fix `(1 >> X) != 0` pattern for vector types

Message ID 20230914174847.1564836-1-apinski@marvell.com
State Unresolved
Headers
Series MATCH: Fix `(1 >> X) != 0` pattern for vector types |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Andrew Pinski Sept. 14, 2023, 5:48 p.m. UTC
  I had missed that integer_onep can match vector types with uniform constant of `1`.
This means the shifter could be an scalar type and then doing a comparison against `0`
would be an invalid transformation.
This fixes the problem by adding a check for the type of the integer_onep to make
sure it is a INTEGRAL_TYPE_P (which does not match a vector type).

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

	PR tree-optimization/111414

gcc/ChangeLog:

	* match.pd (`(1 >> X) != 0`): Check to see if
	the integer_onep was an integral type (not a vector type).

gcc/testsuite/ChangeLog:

	* gcc.c-torture/compile/pr111414-1.c: New test.
---
 gcc/match.pd                                     |  5 +++--
 gcc/testsuite/gcc.c-torture/compile/pr111414-1.c | 13 +++++++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr111414-1.c
  

Comments

Richard Biener Sept. 15, 2023, 6:17 a.m. UTC | #1
On Thu, Sep 14, 2023 at 7:49 PM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> I had missed that integer_onep can match vector types with uniform constant of `1`.
> This means the shifter could be an scalar type and then doing a comparison against `0`
> would be an invalid transformation.
> This fixes the problem by adding a check for the type of the integer_onep to make
> sure it is a INTEGRAL_TYPE_P (which does not match a vector type).
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

OK.

>         PR tree-optimization/111414
>
> gcc/ChangeLog:
>
>         * match.pd (`(1 >> X) != 0`): Check to see if
>         the integer_onep was an integral type (not a vector type).
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.c-torture/compile/pr111414-1.c: New test.
> ---
>  gcc/match.pd                                     |  5 +++--
>  gcc/testsuite/gcc.c-torture/compile/pr111414-1.c | 13 +++++++++++++
>  2 files changed, 16 insertions(+), 2 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr111414-1.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 07ffd831132..97db0eb5f25 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -4206,8 +4206,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>   /* `(1 >> X) != 0` -> `X == 0` */
>   /* `(1 >> X) == 0` -> `X != 0` */
>   (simplify
> -  (cmp (rshift integer_onep @0) integer_zerop)
> -   (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
> +  (cmp (rshift integer_onep@1 @0) integer_zerop)
> +   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
> +    (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
>
>  /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
>     (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
> diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111414-1.c b/gcc/testsuite/gcc.c-torture/compile/pr111414-1.c
> new file mode 100644
> index 00000000000..13fbdae7230
> --- /dev/null
> +++ b/gcc/testsuite/gcc.c-torture/compile/pr111414-1.c
> @@ -0,0 +1,13 @@
> +int a, b, c, d, e, f, g;
> +int h(int i) { return b >= 2 ?: i >> b; }
> +void j() {
> +  int k;
> +  int *l = &c;
> +  for (; d; d++) {
> +    g = h(0 != j);
> +    f = g >> a;
> +    k = f << 7;
> +    e = k > 5 ? k : 0;
> +    *l ^= e;
> +  }
> +}
> --
> 2.31.1
>
  

Patch

diff --git a/gcc/match.pd b/gcc/match.pd
index 07ffd831132..97db0eb5f25 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4206,8 +4206,9 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  /* `(1 >> X) != 0` -> `X == 0` */
  /* `(1 >> X) == 0` -> `X != 0` */
  (simplify
-  (cmp (rshift integer_onep @0) integer_zerop)
-   (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
+  (cmp (rshift integer_onep@1 @0) integer_zerop)
+   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
+    (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
 
 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111414-1.c b/gcc/testsuite/gcc.c-torture/compile/pr111414-1.c
new file mode 100644
index 00000000000..13fbdae7230
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/compile/pr111414-1.c
@@ -0,0 +1,13 @@ 
+int a, b, c, d, e, f, g;
+int h(int i) { return b >= 2 ?: i >> b; }
+void j() {
+  int k;
+  int *l = &c;
+  for (; d; d++) {
+    g = h(0 != j);
+    f = g >> a;
+    k = f << 7;
+    e = k > 5 ? k : 0;
+    *l ^= e;
+  }
+}