gcc/ChangeLog: * tree-ssa-forwprop.cc (is_combined_permutation_identity): New parameter same_p. Try to simplify two successive VEC_PERM_EXPRs with single operand and same mask, where mask chooses elements in reverse order. gcc/testesuite/ChangeLog: * gcc.target/aarch64/sve/acle/general/rev-1.c: New test. diff --git a/gcc/testsuite/gcc.target/aarch64/sve/acle/general/rev-1.c b/gcc/testsuite/gcc.target/aarch64/sve/acle/general/rev-1.c new file mode 100644 index 00000000000..e57ee67d716 --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/sve/acle/general/rev-1.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -fdump-tree-optimized" } */ + +#include + +svint32_t f(svint32_t v) +{ + return svrev_s32 (svrev_s32 (v)); +} + +/* { dg-final { scan-tree-dump "return v_1\\(D\\)" "optimized" } } */ +/* { dg-final { scan-tree-dump-not "VEC_PERM_EXPR" "optimized" } } */ diff --git a/gcc/tree-ssa-forwprop.cc b/gcc/tree-ssa-forwprop.cc index 9b567440ba4..ebd4a368ae9 100644 --- a/gcc/tree-ssa-forwprop.cc +++ b/gcc/tree-ssa-forwprop.cc @@ -2528,11 +2528,16 @@ simplify_bitfield_ref (gimple_stmt_iterator *gsi) return true; } -/* Determine whether applying the 2 permutations (mask1 then mask2) - gives back one of the input. */ +/* For the following sequence: + tem = VEC_PERM_EXPR + res = VEC_PERM_EXPR + + Determine whether applying the 2 permutations (mask1 then mask2) + gives back one of the input. SAME_P specifies whether op0 + and op1 compare equal. */ static int -is_combined_permutation_identity (tree mask1, tree mask2) +is_combined_permutation_identity (tree mask1, tree mask2, bool same_p) { tree mask; unsigned HOST_WIDE_INT nelts, i, j; @@ -2541,6 +2546,29 @@ is_combined_permutation_identity (tree mask1, tree mask2) gcc_checking_assert (TREE_CODE (mask1) == VECTOR_CST && TREE_CODE (mask2) == VECTOR_CST); + + /* For VLA masks, check for the following pattern: + v1 = VEC_PERM_EXPR (v0, v0, mask1) + v2 = VEC_PERM_EXPR (v1, v1, mask2) + --> + v2 = v0 + if mask1 == mask2 == {nelts - 1, nelts - 2, ...}. */ + + if (operand_equal_p (mask1, mask2, 0) + && !VECTOR_CST_NELTS (mask1).is_constant () + && same_p) + { + vec_perm_builder builder; + if (tree_to_vec_perm_builder (&builder, mask1)) + { + poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask1)); + vec_perm_indices sel (builder, 1, nelts); + if (sel.series_p (0, 1, nelts - 1, -1)) + return 1; + } + return 0; + } + mask = fold_ternary (VEC_PERM_EXPR, TREE_TYPE (mask1), mask1, mask1, mask2); if (mask == NULL_TREE || TREE_CODE (mask) != VECTOR_CST) return 0; @@ -2629,7 +2657,9 @@ simplify_permutation (gimple_stmt_iterator *gsi) op3 = gimple_assign_rhs3 (def_stmt); if (TREE_CODE (op3) != VECTOR_CST) return 0; - ident = is_combined_permutation_identity (op3, op2); + bool same_p = operand_equal_p (gimple_assign_rhs1 (def_stmt), + gimple_assign_rhs2 (def_stmt), 0); + ident = is_combined_permutation_identity (op3, op2, same_p); if (!ident) return 0; orig = (ident == 1) ? gimple_assign_rhs1 (def_stmt)