match.pd: Fix ICE on BIT_INSERT_EXPR of BIT_FIELD_REF folding [PR113967]

Message ID ZdMKKVAg4WSNa/JG@tucnak
State Unresolved
Headers
Series match.pd: Fix ICE on BIT_INSERT_EXPR of BIT_FIELD_REF folding [PR113967] |

Checks

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

Commit Message

Jakub Jelinek Feb. 19, 2024, 7:58 a.m. UTC
  Hi!

The following testcase ICEs, because BIT_FIELD_REF's position is not
multiple of the vector element's bit size and the code uses exact_div
to divide those 2 values.

For BIT_INSERT_EXPR, the tree-cfg.cc verification verifies the position
is a multiple of the inserted bit size when inserting into vectors,
but for BIT_FIELD_REF the position can be arbitrary if within the range.

The following patch fixes that, bootstrapped/regtested on x86_64-linux and
i686-linux, ok for trunk?

2024-02-19  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/113967
	* match.pd (bit_insert @0 (BIT_FIELD_REF @1 ..) ..): Require
	in condition that @rpos is multiple of vector element size.

	* gcc.dg/pr113967.c: New test.


	Jakub
  

Comments

Richard Biener Feb. 19, 2024, 8:28 a.m. UTC | #1
On Mon, 19 Feb 2024, Jakub Jelinek wrote:

> Hi!
> 
> The following testcase ICEs, because BIT_FIELD_REF's position is not
> multiple of the vector element's bit size and the code uses exact_div
> to divide those 2 values.
> 
> For BIT_INSERT_EXPR, the tree-cfg.cc verification verifies the position
> is a multiple of the inserted bit size when inserting into vectors,
> but for BIT_FIELD_REF the position can be arbitrary if within the range.
> 
> The following patch fixes that, bootstrapped/regtested on x86_64-linux and
> i686-linux, ok for trunk?

OK.

Thanks,
Richard.

> 2024-02-19  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR tree-optimization/113967
> 	* match.pd (bit_insert @0 (BIT_FIELD_REF @1 ..) ..): Require
> 	in condition that @rpos is multiple of vector element size.
> 
> 	* gcc.dg/pr113967.c: New test.
> 
> --- gcc/match.pd.jj	2024-02-01 09:14:16.541550659 +0100
> +++ gcc/match.pd	2024-02-17 11:58:19.822913858 +0100
> @@ -8586,7 +8586,9 @@ (define_operator_list SYNC_FETCH_AND_AND
>  	  || optimize_vectors_before_lowering_p ())
>        && types_match (@0, @1)
>        && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
> -      && TYPE_VECTOR_SUBPARTS (type).is_constant ())
> +      && TYPE_VECTOR_SUBPARTS (type).is_constant ()
> +      && multiple_p (wi::to_poly_offset (@rpos),
> +		     wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (type)))))
>    (with
>     {
>       unsigned HOST_WIDE_INT elsz
> --- gcc/testsuite/gcc.dg/pr113967.c.jj	2024-02-17 12:01:06.784623682 +0100
> +++ gcc/testsuite/gcc.dg/pr113967.c	2024-02-17 12:00:19.384273862 +0100
> @@ -0,0 +1,14 @@
> +/* PR tree-optimization/113967 */
> +/* { dg-do compile } */
> +/* { dg-options "-O2" } */
> +
> +typedef unsigned short W __attribute__((vector_size (4 * sizeof (short))));
> +
> +void
> +foo (W *p)
> +{
> +  W x = *p;
> +  W y = {};
> +  __builtin_memcpy (&y, 1 + (char *) &x, sizeof (short));
> +  *p = y;
> +}
> 
> 	Jakub
> 
>
  

Patch

--- gcc/match.pd.jj	2024-02-01 09:14:16.541550659 +0100
+++ gcc/match.pd	2024-02-17 11:58:19.822913858 +0100
@@ -8586,7 +8586,9 @@  (define_operator_list SYNC_FETCH_AND_AND
 	  || optimize_vectors_before_lowering_p ())
       && types_match (@0, @1)
       && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
-      && TYPE_VECTOR_SUBPARTS (type).is_constant ())
+      && TYPE_VECTOR_SUBPARTS (type).is_constant ()
+      && multiple_p (wi::to_poly_offset (@rpos),
+		     wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (type)))))
   (with
    {
      unsigned HOST_WIDE_INT elsz
--- gcc/testsuite/gcc.dg/pr113967.c.jj	2024-02-17 12:01:06.784623682 +0100
+++ gcc/testsuite/gcc.dg/pr113967.c	2024-02-17 12:00:19.384273862 +0100
@@ -0,0 +1,14 @@ 
+/* PR tree-optimization/113967 */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+typedef unsigned short W __attribute__((vector_size (4 * sizeof (short))));
+
+void
+foo (W *p)
+{
+  W x = *p;
+  W y = {};
+  __builtin_memcpy (&y, 1 + (char *) &x, sizeof (short));
+  *p = y;
+}