Scalar IL costing receives a null vectype, causing floating-point
statements to use the integer cost. Recover the scalar type and restrict
LMUL adjustments to vector types.
gcc/ChangeLog:
* config/riscv/riscv-vector-costs.cc (costs::adjust_stmt_cost):
Restrict LMUL scaling to vector types.
(costs::add_stmt_cost): Recover the scalar type for scalar costing.
gcc/testsuite/ChangeLog:
* gcc.target/riscv/rvv/autovec/scalar-fp-cost.c: New test.
---
Changes in v2:
- Use m_cost_type == SCALAR_COST consistently.
- Explain why the scalar type is recovered.
- Let adjust_stmt_cost handle scalar types and restrict LMUL scaling to
vector types.
gcc/config/riscv/riscv-vector-costs.cc | 11 ++++++++++-
.../gcc.target/riscv/rvv/autovec/scalar-fp-cost.c | 12 ++++++++++++
2 files changed, 22 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/autovec/scalar-fp-cost.c
diff --git a/gcc/config/riscv/riscv-vector-costs.cc
b/gcc/config/riscv/riscv-vector-costs.cc
index bfb8a0ca63..dfb80bee67 100644
--- a/gcc/config/riscv/riscv-vector-costs.cc
+++ b/gcc/config/riscv/riscv-vector-costs.cc
@@ -1569,7 +1569,7 @@ costs::adjust_stmt_cost (enum vect_cost_for_stmt kind,
loop_vec_info loop,
/* Apply LMUL cost scaling uniformly to all vector operations.
Larger LMUL values have higher latency and register pressure,
which affects performance regardless of loop structure. */
- if (vectype)
+ if (VECTOR_TYPE_P (vectype))
{
unsigned lmul_factor = get_lmul_cost_scaling (TYPE_MODE (vectype));
if (lmul_factor > 1)
@@ -1584,6 +1584,15 @@ costs::add_stmt_cost (int count, vect_cost_for_stmt kind,
stmt_vec_info stmt_info, slp_tree node, tree vectype,
int misalign, vect_cost_model_location where)
{
+ /* VECTYPE is null when costing scalar IL. Recover the scalar type to
+ distinguish floating-point statements from integer statements. */
+ if (m_cost_type == SCALAR_COST && stmt_info)
+ {
+ gcc_assert (!vectype);
+ if (tree lhs = gimple_get_lhs (STMT_VINFO_STMT (stmt_info)))
+ vectype = TREE_TYPE (lhs);
+ }
+
int stmt_cost
= targetm.vectorize.builtin_vectorization_cost (kind, vectype, misalign);
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/scalar-fp-cost.c
b/gcc/testsuite/gcc.target/riscv/rvv/autovec/scalar-fp-cost.c
new file mode 100644
index 0000000000..d729dd69d0
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/scalar-fp-cost.c
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -march=rv64gcv -mabi=lp64d -mtune=xt-c9501fdvt
-fdump-tree-vect-details" } */
+
+void
+foo (float *restrict dst, const float *restrict x,
+ const float *restrict y, long n)
+{
+ for (long i = 0; i < n; ++i)
+ dst[i] = x[i] + y[i];
+}
+
+/* { dg-final { scan-tree-dump {_[0-9]+ \+ _[0-9]+ .*scalar_stmt costs 2 in
body} "vect" } } */
--
2.47.1