https://gcc.gnu.org/g:a03bb1a1500f0ac7cd6e67b1bbd2ac6e510a5e7c
commit r17-6681-ga03bb1a1500f0ac7cd6e67b1bbd2ac6e510a5e7c Author: Kyrylo Tkachov <[email protected]> Date: Tue Nov 17 06:58:29 2026 -0800 aarch64: Cost vec_deconstruct as per-lane vec_to_scalar [PR128998] The vectorizer now costs the lane extraction needed by emulated gathers, strided accesses and reductions as one vec_deconstruct per vector instead of one vec_to_scalar per lane. The AArch64 hook handles vec_deconstruct like vec_construct, which charges elements / 2 + 1 for the whole vector. This is much cheaper than the previous elements * vec_to_scalar cost. The AArch64 costing code that looks for vec_to_scalar, such as the adjustment for index loads of emulated Advanced SIMD gathers, no longer matches either. As a result, for double f (double *restrict x, double *restrict y, int *restrict z) { double res = 0.0; for (int i = 0; i < 100; ++i) res += x[i] * y[z[i]]; return res; } at -Ofast -mtune=neoverse-v1 -march=armv8.2-a+sve, the Advanced SIMD loop that emulates the gather is now estimated as cheaper than the SVE loop that uses a real gather. The vectorizer picks V16QI instead of VNx2DF, which regresses gcc.target/aarch64/sve/cost_model_15.c. The same happens with -mcpu=neoverse-v2. PR128998 reports a calculix slowdown on neoverse-n1 that bisects to the vectorizer change that introduced vec_deconstruct. Convert vec_deconstruct to vec_to_scalar, scaled by the number of lanes in the vector, at the start of add_stmt_cost. This restores the per-lane costs and lets the existing vec_to_scalar handling apply. The strided SLP store path now costs every lane of the vector, which can slightly exceed the previous count when the stores use multi-lane pieces. Bootstrapped and tested on aarch64-linux-gnu. gcc/ChangeLog: PR target/128998 * config/aarch64/aarch64.cc (aarch64_vector_costs::add_stmt_cost): Treat vec_deconstruct as one vec_to_scalar per vector lane. (aarch64_vector_costs::count_ops): Make the vec_deconstruct case unreachable. Signed-off-by: Kyrylo Tkachov <[email protected]> Diff: --- gcc/config/aarch64/aarch64.cc | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc index 0f4a1fab7a19..98f4f88deada 100644 --- a/gcc/config/aarch64/aarch64.cc +++ b/gcc/config/aarch64/aarch64.cc @@ -19069,10 +19069,13 @@ aarch64_vector_costs::count_ops (unsigned int count, vect_cost_for_stmt kind, /* We currently don't expect these to be used in a loop body. */ break; + case vec_deconstruct: + /* add_stmt_cost converts vec_deconstruct to vec_to_scalar. */ + gcc_unreachable (); + case vec_perm: case vec_promote_demote: case vec_construct: - case vec_deconstruct: case vec_to_scalar: case scalar_to_vec: case vector_stmt: @@ -19486,6 +19489,16 @@ aarch64_vector_costs::add_stmt_cost (int count, vect_cost_for_stmt kind, vectype = TREE_TYPE (lhs); } + /* A vec_deconstruct extracts every lane of a vector. Cost it as one + vec_to_scalar per lane, so that the code below that handles + vec_to_scalar also applies to it. */ + if (kind == vec_deconstruct) + { + gcc_checking_assert (vectype); + count *= vect_nunits_for_cost (vectype); + kind = vec_to_scalar; + } + fractional_cost stmt_cost = aarch64_builtin_vectorization_cost (kind, vectype, misalign);
