Scalar IL costing receives a null vectype, causing floating-point
statements to use the integer cost. Recover the type from the statement
and skip vector-specific adjustments while costing scalar IL.
gcc/ChangeLog:
* config/riscv/riscv-vector-costs.cc (costs::add_stmt_cost): Recover
the scalar type and skip vector adjustments for scalar costing.
gcc/testsuite/ChangeLog:
* gcc.target/riscv/rvv/autovec/scalar-fp-cost.c: New test.
---
The issue was exposed by commit fb59c5719c17 ("Avoid passing vectype != NULL
when costing scalar IL"), which makes vectype null when costing scalar IL.
AArch64 fixed the same issue in commit 4982644625dc by recovering the scalar
type from the statement LHS. This patch follows the same approach for RISC-V.
gcc/config/riscv/riscv-vector-costs.cc | 10 +++++++++-
.../gcc.target/riscv/rvv/autovec/scalar-fp-cost.c | 12 ++++++++++++
2 files changed, 21 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..41c31dcf34 100644
--- a/gcc/config/riscv/riscv-vector-costs.cc
+++ b/gcc/config/riscv/riscv-vector-costs.cc
@@ -1584,6 +1584,14 @@ 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)
{
+ /* Recover the type since VECTYPE is null when costing scalar IL. */
+ if (costing_for_scalar () && 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);
@@ -1613,7 +1621,7 @@ costs::add_stmt_cost (int count, vect_cost_for_stmt kind,
m_unrolled_vls_stmts += count * m_unrolled_vls_niters;
}
- if (vectype)
+ if (vectype && !costing_for_scalar ())
stmt_cost = adjust_stmt_cost (kind, loop_vinfo, stmt_info, node, vectype,
stmt_cost);
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