Hi,

在 2026/6/18 20:36, Richard Biener 写道:
> On Thu, Jun 18, 2026 at 2:13 PM Kewen Lin <[email protected]> wrote:
snip...>>>> Do you suggest something like:
>>
>> diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc
>> index b1fd86c2b32..964c4bdef90 100644
>> --- a/gcc/config/i386/i386.cc
>> +++ b/gcc/config/i386/i386.cc
>> @@ -26510,8 +26510,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 = vect_comparison_type (stmt_info);
>> +              if (VECTOR_TYPE_P (vectype))
>> +                op_type
>> +                  = get_related_vectype_for_scalar_type 
>> (m_vinfo->vector_mode,
>> +                                                         op_type);
>> +              mode = TYPE_MODE (op_type);
> 
> More like
> 
>               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);
> 

Got it, thanks Richi!  As suggested, patch v2 is as below.

-----

This patch is authored-by: Zhongjie Guo <[email protected]>

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.

Bootstrapped and regtested on a hygon c86-4g-m7 machine.

Is it ok for trunk?

BR,
Kewen
-----
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]>
---
 gcc/config/i386/i386.cc                           |  9 ++++++++-
 gcc/testsuite/gcc.target/i386/vect-compare-cost.c | 14 ++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.target/i386/vect-compare-cost.c

diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc
index b1fd86c2b32..4138e6f8289 100644
--- a/gcc/config/i386/i386.cc
+++ b/gcc/config/i386/i386.cc
@@ -26510,8 +26510,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 00000000000..5fb04773b4a
--- /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" 
} } */
-- 
2.34.1


Reply via email to