Re: [PATCH v2] MATCH: Look through VIEW_CONVERT when folding VEC_PERM_EXPRs.

2024-05-24 Thread Philipp Tomsich
On Fri, 24 May 2024 at 13:02, Richard Biener  wrote:
>
> On Fri, 24 May 2024, Manolis Tsamis wrote:
>
> > The match.pd patterns to merge two vector permutes into one fail when a
> > potentially no-op view convert expressions is between the two permutes.
> > This change lifts this restriction.
>
> OK.

Applied to master, thanks!
--Philipp.


Re: [PATCH v2] MATCH: Look through VIEW_CONVERT when folding VEC_PERM_EXPRs.

2024-05-24 Thread Richard Biener
On Fri, 24 May 2024, Manolis Tsamis wrote:

> The match.pd patterns to merge two vector permutes into one fail when a
> potentially no-op view convert expressions is between the two permutes.
> This change lifts this restriction.

OK.

> gcc/ChangeLog:
> 
>   * match.pd: Allow no-op view_convert between permutes.
> 
> gcc/testsuite/ChangeLog:
> 
>   * gcc.dg/fold-perm-2.c: New test.
> 
> Signed-off-by: Manolis Tsamis 
> ---
> 
> Changes in v2:
> Use TYPE_SIZE (TREE_TYPE (TREE_TYPE (@))) instead of element_precision (@).
> 
>  gcc/match.pd   | 14 --
>  gcc/testsuite/gcc.dg/fold-perm-2.c | 16 
>  2 files changed, 24 insertions(+), 6 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/fold-perm-2.c
> 
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 07e743ae464..1f91b9857c8 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -10039,19 +10039,21 @@ and,
>   d = VEC_PERM_EXPR ;  */
>  
>  (simplify
> - (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
> + (vec_perm (view_convert?@0 (vec_perm@1 @2 @3 VECTOR_CST@4)) @0 VECTOR_CST@5)
>   (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
>(with
> {
>   machine_mode result_mode = TYPE_MODE (type);
> - machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
> + machine_mode op_mode = TYPE_MODE (TREE_TYPE (@2));
>   int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
>   vec_perm_builder builder0;
>   vec_perm_builder builder1;
>   vec_perm_builder builder2 (nelts, nelts, 1);
> }
> -   (if (tree_to_vec_perm_builder (, @3)
> - && tree_to_vec_perm_builder (, @4))
> +   (if (tree_to_vec_perm_builder (, @4)
> + && tree_to_vec_perm_builder (, @5)
> + && TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))
> +== TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1
>  (with
>   {
> vec_perm_indices sel0 (builder0, 2, nelts);
> @@ -10073,10 +10075,10 @@ and,
>  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
> || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
>  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
> -  op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
> +  op0 = vec_perm_indices_to_tree (TREE_TYPE (@5), sel2);
>   }
>   (if (op0)
> -  (vec_perm @1 @2 { op0; })))
> +  (view_convert (vec_perm @2 @3 { op0; }
>  
>  /* Merge
>   c = VEC_PERM_EXPR ;
> diff --git a/gcc/testsuite/gcc.dg/fold-perm-2.c 
> b/gcc/testsuite/gcc.dg/fold-perm-2.c
> new file mode 100644
> index 000..1a4ab4065de
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/fold-perm-2.c
> @@ -0,0 +1,16 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O -fdump-tree-fre1" } */
> +
> +typedef int veci __attribute__ ((vector_size (4 * sizeof (int;
> +typedef unsigned int vecu __attribute__ ((vector_size (4 * sizeof (unsigned 
> int;
> +
> +void fun (veci *a, veci *b, veci *c)
> +{
> +  veci r1 = __builtin_shufflevector (*a, *b, 0, 5, 2, 7);
> +  vecu r2 = __builtin_convertvector (r1, vecu);
> +  vecu r3 = __builtin_shufflevector (r2, r2, 2, 3, 1, 0);
> +  *c = __builtin_convertvector (r3, veci);
> +}
> +
> +/* { dg-final { scan-tree-dump "VEC_PERM_EXPR.*{ 2, 7, 5, 0 }" "fre1" } } */
> +/* { dg-final { scan-tree-dump-times "VEC_PERM_EXPR" 1 "fre1" } } */
> 

-- 
Richard Biener 
SUSE Software Solutions Germany GmbH,
Frankenstrasse 146, 90461 Nuernberg, Germany;
GF: Ivo Totev, Andrew McDonald, Werner Knoblich; (HRB 36809, AG Nuernberg)


[PATCH v2] MATCH: Look through VIEW_CONVERT when folding VEC_PERM_EXPRs.

2024-05-24 Thread Manolis Tsamis
The match.pd patterns to merge two vector permutes into one fail when a
potentially no-op view convert expressions is between the two permutes.
This change lifts this restriction.

gcc/ChangeLog:

* match.pd: Allow no-op view_convert between permutes.

gcc/testsuite/ChangeLog:

* gcc.dg/fold-perm-2.c: New test.

Signed-off-by: Manolis Tsamis 
---

Changes in v2:
Use TYPE_SIZE (TREE_TYPE (TREE_TYPE (@))) instead of element_precision (@).

 gcc/match.pd   | 14 --
 gcc/testsuite/gcc.dg/fold-perm-2.c | 16 
 2 files changed, 24 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/fold-perm-2.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 07e743ae464..1f91b9857c8 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -10039,19 +10039,21 @@ and,
  d = VEC_PERM_EXPR ;  */
 
 (simplify
- (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
+ (vec_perm (view_convert?@0 (vec_perm@1 @2 @3 VECTOR_CST@4)) @0 VECTOR_CST@5)
  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
   (with
{
  machine_mode result_mode = TYPE_MODE (type);
- machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
+ machine_mode op_mode = TYPE_MODE (TREE_TYPE (@2));
  int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
  vec_perm_builder builder0;
  vec_perm_builder builder1;
  vec_perm_builder builder2 (nelts, nelts, 1);
}
-   (if (tree_to_vec_perm_builder (, @3)
-   && tree_to_vec_perm_builder (, @4))
+   (if (tree_to_vec_perm_builder (, @4)
+   && tree_to_vec_perm_builder (, @5)
+   && TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))
+  == TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1
 (with
  {
vec_perm_indices sel0 (builder0, 2, nelts);
@@ -10073,10 +10075,10 @@ and,
   ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
  || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
   : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
-op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
+op0 = vec_perm_indices_to_tree (TREE_TYPE (@5), sel2);
  }
  (if (op0)
-  (vec_perm @1 @2 { op0; })))
+  (view_convert (vec_perm @2 @3 { op0; }
 
 /* Merge
  c = VEC_PERM_EXPR ;
diff --git a/gcc/testsuite/gcc.dg/fold-perm-2.c 
b/gcc/testsuite/gcc.dg/fold-perm-2.c
new file mode 100644
index 000..1a4ab4065de
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/fold-perm-2.c
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-fre1" } */
+
+typedef int veci __attribute__ ((vector_size (4 * sizeof (int;
+typedef unsigned int vecu __attribute__ ((vector_size (4 * sizeof (unsigned 
int;
+
+void fun (veci *a, veci *b, veci *c)
+{
+  veci r1 = __builtin_shufflevector (*a, *b, 0, 5, 2, 7);
+  vecu r2 = __builtin_convertvector (r1, vecu);
+  vecu r3 = __builtin_shufflevector (r2, r2, 2, 3, 1, 0);
+  *c = __builtin_convertvector (r3, veci);
+}
+
+/* { dg-final { scan-tree-dump "VEC_PERM_EXPR.*{ 2, 7, 5, 0 }" "fre1" } } */
+/* { dg-final { scan-tree-dump-times "VEC_PERM_EXPR" 1 "fre1" } } */
-- 
2.44.0