From 9288daeedd0a4ca35dd6ab482c2b099f9b53ccb7 Mon Sep 17 00:00:00 2001 From: Jan Hubicka Date: Wed, 23 Apr 2025 17:04:32 +0200 Subject: [PATCH 1/4] [Backport] i386: Cost truth_value exprs in vectorizer costs Backport GCC upstream commit 9b9d605d68cf27a24e8ed9d4f1ead1f00131cec1. Account for truth-value expressions in the i386 vectorizer cost model. Adapt the floating-point mode predicate to the GCC 12 branch, which does not yet have the HFBF variant. Signed-off-by: Zhongjie Guo --- gcc/config/i386/i386.cc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc index 169b2dd24b6..45310a4b2f9 100644 --- a/gcc/config/i386/i386.cc +++ b/gcc/config/i386/i386.cc @@ -23230,6 +23230,23 @@ ix86_vector_costs::add_stmt_cost (int count, vect_cost_for_stmt kind, stmt_cost = ix86_cost->add; break; default: + if (truth_value_p (subcode)) + { + if (SSE_FLOAT_MODE_SSEMATH_OR_HF_P (mode)) + /* CMPccS? instructions are cheap, so use sse_op. While they + produce a mask which may need to be turned to 0/1 by and, + expect that this will be optimized away in a common case. */ + stmt_cost = ix86_cost->sse_op; + else if (X87_FLOAT_MODE_P (mode)) + /* fcmp + setcc. */ + stmt_cost = ix86_cost->fadd + ix86_cost->add; + else if (VECTOR_MODE_P (mode)) + stmt_cost = ix86_vec_cost (mode, ix86_cost->sse_op); + else + /* setcc. */ + stmt_cost = ix86_cost->add; + break; + } break; } } -- Gitee From 52c1c3da5ab2e4d1fe8f565446d87ee07721d199 Mon Sep 17 00:00:00 2001 From: Richard Biener Date: Fri, 16 Jan 2026 10:22:17 +0100 Subject: [PATCH 2/4] [Backport] target/123603: Add --param ix86-vect-compare-costs Backport GCC upstream commit 4b2db74430233302e4c711d6f958a2e7fbc643f3. Allow the x86 vectorizer to compare the estimated costs of candidate vector modes. The parameter is introduced as opt-in in this commit. Signed-off-by: Zhongjie Guo --- gcc/config/i386/i386.cc | 2 +- gcc/config/i386/i386.opt | 4 ++++ gcc/doc/invoke.texi | 3 +++ .../vect/costmodel/x86_64/costmodel-pr123603.c | 15 +++++++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr123603.c diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc index 45310a4b2f9..cb3c14bbb34 100644 --- a/gcc/config/i386/i386.cc +++ b/gcc/config/i386/i386.cc @@ -22975,7 +22975,7 @@ ix86_autovectorize_vector_modes (vector_modes *modes, bool all) if (TARGET_SSE2) modes->safe_push (V4QImode); - return 0; + return ix86_vect_compare_costs ? VECT_COMPARE_COSTS : 0; } /* Implemenation of targetm.vectorize.get_mask_mode. */ diff --git a/gcc/config/i386/i386.opt b/gcc/config/i386/i386.opt index ed31cab0a46..aeefefaaae1 100644 --- a/gcc/config/i386/i386.opt +++ b/gcc/config/i386/i386.opt @@ -1219,6 +1219,10 @@ Do not use GOT to access external symbols. Target Joined UInteger Var(x86_stlf_window_ninsns) Init(64) Param Instructions number above which STFL stall penalty can be compensated. +-param=ix86-vect-compare-costs= +Target Joined UInteger Var(ix86_vect_compare_costs) Init(0) IntegerRange(0, 1) Param Optimization +Whether x86 vectorizer cost modeling compares costs of different vector sizes. + mgather Target Alias(mtune-ctrl=, use_gather, ^use_gather) Enable vectorization for gather instruction. diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index baf066400da..54f5d1b5062 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -15302,6 +15302,9 @@ The following choices of @var{name} are available on i386 and x86_64 targets: @item x86-stlf-window-ninsns Instructions number above which STFL stall penalty can be compensated. +@item ix86-vect-compare-costs +Whether x86 vectorizer cost modeling compares costs of different vector sizes. + @end table @end table diff --git a/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr123603.c b/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr123603.c new file mode 100644 index 00000000000..c074176a7e4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr123603.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-additional-options "--param ix86-vect-compare-costs=1" } */ + +void foo (int *block) +{ + for (int i = 0; i < 3; ++i) + { + int a = block[i*9]; + int b = block[i*9+1]; + block[i*9] = a + 10; + block[i*9+1] = b + 10; + } +} + +/* { dg-final { scan-tree-dump "optimized: loop vectorized using 8 byte vectors" "vect" } } */ -- Gitee From 74709b2094be0a4fd7fa4752c22ccd65dc6e611c Mon Sep 17 00:00:00 2001 From: Richard Biener Date: Fri, 24 Apr 2026 14:35:49 +0200 Subject: [PATCH 3/4] [Backport] i386: Enable vector cost comparison by default Backport GCC upstream commit a22b31304e0a1ad21751f882c02c32c167c78793. Enable ix86-vect-compare-costs by default and adjust the applicable tests. gcc.target/i386/vect-epilogues-10.c from the upstream change is absent on this GCC 12 branch. Signed-off-by: Zhongjie Guo --- gcc/config/i386/i386.opt | 2 +- .../vect/costmodel/x86_64/costmodel-pr120398.c | 17 +++++++++++++++++ .../vect/costmodel/x86_64/costmodel-pr123603.c | 2 -- .../gcc.target/i386/vect-alignment-peeling-1.c | 2 +- .../gcc.target/i386/vect-alignment-peeling-2.c | 2 +- 5 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr120398.c diff --git a/gcc/config/i386/i386.opt b/gcc/config/i386/i386.opt index aeefefaaae1..d8cd58f5291 100644 --- a/gcc/config/i386/i386.opt +++ b/gcc/config/i386/i386.opt @@ -1220,7 +1220,7 @@ Target Joined UInteger Var(x86_stlf_window_ninsns) Init(64) Param Instructions number above which STFL stall penalty can be compensated. -param=ix86-vect-compare-costs= -Target Joined UInteger Var(ix86_vect_compare_costs) Init(0) IntegerRange(0, 1) Param Optimization +Target Joined UInteger Var(ix86_vect_compare_costs) Init(1) IntegerRange(0, 1) Param Optimization Whether x86 vectorizer cost modeling compares costs of different vector sizes. mgather diff --git a/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr120398.c b/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr120398.c new file mode 100644 index 00000000000..e348c8eb550 --- /dev/null +++ b/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr120398.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ + +static float muladd(float x, float y, float z) +{ + return x * y + z; +} +float g(float x[], long n) +{ + float r0 = 0, r1 = 0; + for (; n; x += 2, n--) { + r0 = muladd(x[0], x[0], r0); + r1 = muladd(x[1], x[1], r1); + } + return r0 + r1; +} + +/* { dg-final { scan-tree-dump "optimized: loop vectorized using 8 byte vectors" "vect" } } */ diff --git a/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr123603.c b/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr123603.c index c074176a7e4..34d11fe64ad 100644 --- a/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr123603.c +++ b/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr123603.c @@ -1,6 +1,4 @@ /* { dg-do compile } */ -/* { dg-additional-options "--param ix86-vect-compare-costs=1" } */ - void foo (int *block) { for (int i = 0; i < 3; ++i) diff --git a/gcc/testsuite/gcc.target/i386/vect-alignment-peeling-1.c b/gcc/testsuite/gcc.target/i386/vect-alignment-peeling-1.c index 4aa536ba86c..39ca8b13fdf 100644 --- a/gcc/testsuite/gcc.target/i386/vect-alignment-peeling-1.c +++ b/gcc/testsuite/gcc.target/i386/vect-alignment-peeling-1.c @@ -85,6 +85,6 @@ int main() __builtin_abort (); } -/* { dg-final { scan-tree-dump-times "Alignment of access forced using peeling" 4 "vect" } } */ +/* { dg-final { scan-tree-dump-times "Alignment of access forced using peeling" 7 "vect" } } */ /* Verify all vector accesses are emitted as aligned. */ /* { dg-final { scan-assembler-not "movup" } } */ diff --git a/gcc/testsuite/gcc.target/i386/vect-alignment-peeling-2.c b/gcc/testsuite/gcc.target/i386/vect-alignment-peeling-2.c index 834bf0f770d..9557e620a18 100644 --- a/gcc/testsuite/gcc.target/i386/vect-alignment-peeling-2.c +++ b/gcc/testsuite/gcc.target/i386/vect-alignment-peeling-2.c @@ -85,6 +85,6 @@ int main() __builtin_abort (); } -/* { dg-final { scan-tree-dump-times "Alignment of access forced using peeling" 4 "vect" } } */ +/* { dg-final { scan-tree-dump-times "Alignment of access forced using peeling" 7 "vect" } } */ /* Verify all vector accesses are emitted as aligned. */ /* { dg-final { scan-assembler-not "movup" } } */ -- Gitee From cddfa3d921082f1a69197bfdaaafd1d771e36c6e Mon Sep 17 00:00:00 2001 From: Zhongjie Guo Date: Tue, 11 Aug 2026 20:19:16 +0000 Subject: [PATCH 4/4] [Backport] testsuite: Adapt vector cost tests for GCC 12 and c86-4g-m7 Adapt the upstream vector cost comparison tests to GCC 12 dump output and the c86-4g-m7 cost model. Extend the parameter documentation with its accepted values, default, and compatibility setting. Signed-off-by: Zhongjie Guo --- gcc/doc/invoke.texi | 2 ++ .../costmodel/x86_64/costmodel-pr120398.c | 22 +++++++++++-------- .../costmodel/x86_64/costmodel-pr123603.c | 11 ++++++---- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 54f5d1b5062..7df9e16fdf3 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -15304,6 +15304,8 @@ Instructions number above which STFL stall penalty can be compensated. @item ix86-vect-compare-costs Whether x86 vectorizer cost modeling compares costs of different vector sizes. +The value may be 0 or 1. The default is 1; set it to 0 to restore the +previous behavior. @end table diff --git a/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr120398.c b/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr120398.c index e348c8eb550..39be0b61efd 100644 --- a/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr120398.c +++ b/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr120398.c @@ -1,17 +1,21 @@ /* { dg-do compile } */ -static float muladd(float x, float y, float z) +static float +muladd (float x, float y, float z) { - return x * y + z; + return x * y + z; } -float g(float x[], long n) + +float +g (float x[], long n) { - float r0 = 0, r1 = 0; - for (; n; x += 2, n--) { - r0 = muladd(x[0], x[0], r0); - r1 = muladd(x[1], x[1], r1); + float r0 = 0, r1 = 0; + for (; n; x += 2, n--) + { + r0 = muladd (x[0], x[0], r0); + r1 = muladd (x[1], x[1], r1); } - return r0 + r1; + return r0 + r1; } -/* { dg-final { scan-tree-dump "optimized: loop vectorized using 8 byte vectors" "vect" } } */ +/* { dg-final { scan-tree-dump "Choosing vector mode V8QI" "vect" } } */ diff --git a/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr123603.c b/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr123603.c index 34d11fe64ad..b9844867ac1 100644 --- a/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr123603.c +++ b/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr123603.c @@ -1,12 +1,15 @@ /* { dg-do compile } */ +/* This test relies on the EulerGCC-specific c86-4g-m7 cost model. */ +/* { dg-additional-options "-mtune=c86-4g-m7" } */ + void foo (int *block) { for (int i = 0; i < 3; ++i) { - int a = block[i*9]; - int b = block[i*9+1]; - block[i*9] = a + 10; - block[i*9+1] = b + 10; + int a = block[i * 9]; + int b = block[i * 9 + 1]; + block[i * 9] = a + 10; + block[i * 9 + 1] = b + 10; } } -- Gitee