]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
MATCH: Look through VIEW_CONVERT when folding VEC_PERM_EXPRs.
authorManolis Tsamis <manolis.tsamis@vrull.eu>
Wed, 1 Nov 2023 11:27:28 +0000 (12:27 +0100)
committerPhilipp Tomsich <philipp.tomsich@vrull.eu>
Fri, 24 May 2024 12:11:51 +0000 (14:11 +0200)
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.

gcc/match.pd
gcc/testsuite/gcc.dg/fold-perm-2.c [new file with mode: 0644]

index 7081d76d56a6ea7cdd4c30ec8e757a4d8c49424c..024e33504652c47e735fd22777523284ef5ec4da 100644 (file)
@@ -10082,19 +10082,21 @@ and,
      d = VEC_PERM_EXPR <a, b, NEW_VCST>;  */
 
 (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 (&builder0, @3)
-       && tree_to_vec_perm_builder (&builder1, @4))
+   (if (tree_to_vec_perm_builder (&builder0, @4)
+       && tree_to_vec_perm_builder (&builder1, @5)
+       && TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))
+          == TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))))
     (with
      {
        vec_perm_indices sel0 (builder0, 2, nelts);
@@ -10116,10 +10118,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 <a, b, VCST0>;
diff --git a/gcc/testsuite/gcc.dg/fold-perm-2.c b/gcc/testsuite/gcc.dg/fold-perm-2.c
new file mode 100644 (file)
index 0000000..1a4ab40
--- /dev/null
@@ -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" } } */