https://gcc.gnu.org/g:46dacd6b7581c9daee2f3cecc1d3b5af58e026d0
commit r17-1744-g46dacd6b7581c9daee2f3cecc1d3b5af58e026d0 Author: Zhongjie Guo <[email protected]> Date: Mon Jun 22 10:55:21 2026 +0000 i386: Use comparison operand mode for compare costs AVX512 vector comparisons produce mask results, but the instructions operate on vector operands. The instruction cost should therefore be based on the operand vector mode rather than the mask result mode. The mismatch is especially visible on AVX512 split-regs targets, where ix86_vec_cost scales 512-bit vector operations. Using the mask result type misses that scaling and can make 512-bit vectorization look too cheap. As Richi suggested, this can be extended to cover scalar as well. So this patch is to obtain the mode of comparison operand first, then continue with the existing comparison cost logic. It also adjusts truth_value_p with tcc_comparison check. gcc/ChangeLog: * config/i386/i386.cc (ix86_vector_costs::add_stmt_cost): Cost comparisons using the comparison operand mode. gcc/testsuite/ChangeLog: * gcc.target/i386/vect-compare-cost.c: New test. Signed-off-by: Zhongjie Guo <[email protected]> Co-Authored-By: Richard Biener <[email protected]> Diff: --- gcc/config/i386/i386.cc | 9 ++++++++- gcc/testsuite/gcc.target/i386/vect-compare-cost.c | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc index 2b4d878a550e..9bd118c5d6a2 100644 --- a/gcc/config/i386/i386.cc +++ b/gcc/config/i386/i386.cc @@ -26514,8 +26514,15 @@ ix86_vector_costs::add_stmt_cost (int count, vect_cost_for_stmt kind, break; default: - if (truth_value_p (subcode)) + if (TREE_CODE_CLASS (subcode) == tcc_comparison) { + tree op_type; + if (kind == vector_stmt) + op_type = SLP_TREE_VECTYPE (SLP_TREE_CHILDREN (node)[0]); + else + op_type = vect_comparison_type (stmt_info); + mode = TYPE_MODE (op_type); + if (SSE_FLOAT_MODE_SSEMATH_OR_HFBF_P (mode)) /* CMPccS? insructions are cheap, so use sse_op. While they produce a mask which may need to be turned to 0/1 by and, diff --git a/gcc/testsuite/gcc.target/i386/vect-compare-cost.c b/gcc/testsuite/gcc.target/i386/vect-compare-cost.c new file mode 100644 index 000000000000..5fb04773b4a6 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/vect-compare-cost.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -mavx512f -mtune=c86-4g-m7 --param ix86-vect-compare-costs=1 -fdump-tree-vect-details" } */ + +int +find_ll (const unsigned long long *p, unsigned long long x, int n) +{ + for (int i = 0; i < n; ++i) + if (p[i] == x) + return i; + return -1; +} + +/* The compare cost should make c86-4g-m7 prefer 32-byte vectors. */ +/* { dg-final { scan-tree-dump "loop vectorized using 32 byte vectors" "vect" } } */
