> With doing this as SLP pattern you'd have the chance to invent
> new SLP_TREE_REPRESENTATIVE stmts (also for the PHI). There's
> still the remaining issue that for the live lane we want to
> have the "true" scalar def, and there's no way to do that given
> we do not represent the LC PHI in the SLP graph (we do not
> represent any reduction result or live lane extract result).
>
> But in principle this would be the way to go, and represent the
> reduction result and possibly LC PHIs. So if we can do it as
> SLP pattern, leaving the missed representation aside and instead
> use some kind of implicit thing like a flag (aka "demoted vector
> element"), that would sound most future-proof to me?
The attached is where I arrived, it's actually a mix of my first attempt
and an SLP pattern (most of v1 I just copied). But re-reading your
message, I think you had something else in mind still, i.e. adding an
SLP_TREE_VECTYPE_NARROWED or something. Then in
vect_create_epilog_for_reduction and other users, check that and adjust
accordingly?
Regards
Robin
gcc/ChangeLog:
* tree-vect-loop.cc (vect_find_reusable_accumulator): Compare
VECT_REDUC_INFO_NARROWED.
* tree-vect-slp-patterns.cc (class narrow_reduc_pattern):
(narrow_reduc_pattern::recognize): New function.
(narrow_reduc_pattern::build): Ditto.
* tree-vectorizer.h (VECT_REDUC_INFO_NARROWED): New property.
gcc/testsuite/ChangeLog:
* gcc.dg/vect/vect-reduc-narrow-prec-1.c: New test.
* gcc.dg/vect/vect-reduc-narrow-prec-2.c: New test.
* gcc.dg/vect/vect-reduc-narrow-prec-3.c: New test.
---
.../gcc.dg/vect/vect-reduc-narrow-prec-1.c | 185 +++++++++
.../gcc.dg/vect/vect-reduc-narrow-prec-2.c | 385 ++++++++++++++++++
.../gcc.dg/vect/vect-reduc-narrow-prec-3.c | 47 +++
gcc/tree-vect-loop.cc | 4 +-
gcc/tree-vect-slp-patterns.cc | 197 ++++++++-
gcc/tree-vectorizer.h | 4 +
6 files changed, 820 insertions(+), 2 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/vect/vect-reduc-narrow-prec-1.c
create mode 100644 gcc/testsuite/gcc.dg/vect/vect-reduc-narrow-prec-2.c
create mode 100644 gcc/testsuite/gcc.dg/vect/vect-reduc-narrow-prec-3.c
diff --git a/gcc/testsuite/gcc.dg/vect/vect-reduc-narrow-prec-1.c
b/gcc/testsuite/gcc.dg/vect/vect-reduc-narrow-prec-1.c
new file mode 100644
index 00000000000..bfdc773d5fc
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/vect-reduc-narrow-prec-1.c
@@ -0,0 +1,185 @@
+/* { dg-require-effective-target vect_int } */
+/* { dg-additional-options "-fno-vect-cost-model --param
vect-epilogues-nomask=1" } */
+/* { dg-add-options riscv_zvabd } */
+
+#include "tree-vect.h"
+
+#define N 300
+#define M 128
+#define W 16
+#define KN 257
+
+unsigned char a[N], b[N], c[N], d[N];
+unsigned char p[16 * W], q[16 * W];
+unsigned char ka[2 * KN], kb[2 * KN];
+
+unsigned int __attribute__ ((noipa))
+f257 (void)
+{
+ unsigned int s = 0;
+ for (int i = 0; i < 257; ++i)
+ s += __builtin_abs (a[i] - b[i]);
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+f255 (void)
+{
+ unsigned int s = 0;
+ for (int i = 0; i < 255; ++i)
+ s += __builtin_abs (a[i] - b[i]);
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+fn257 (int n)
+{
+ if (n > 257)
+ __builtin_unreachable ();
+ unsigned int s = 0;
+ for (int i = 0; i < n; ++i)
+ s += __builtin_abs (a[i] - b[i]);
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+fuc (unsigned char n)
+{
+ unsigned int s = 0;
+ for (unsigned char i = 0; i < n; ++i)
+ s += __builtin_abs (a[i] - b[i]);
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+h_n1 (void)
+{
+ unsigned int s = 0;
+ for (int i = 0; i < M; ++i)
+ {
+ s += __builtin_abs (a[i] - b[i]);
+ s += __builtin_abs (c[i] - d[i]);
+ }
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+h_chain16 (unsigned char *x, int sx, unsigned char *y, int sy)
+{
+ unsigned int s = 0;
+ for (int j = 0; j < 16; ++j)
+ {
+#pragma GCC unroll 16
+ for (int i = 0; i < W; ++i)
+ s += __builtin_abs (x[i] - y[i]);
+ x += sx;
+ y += sy;
+ }
+ return s;
+}
+
+void __attribute__ ((noipa))
+k_unknown (unsigned int init, unsigned int *o)
+{
+ unsigned int s0 = 0;
+ unsigned int s1 = init;
+ for (int i = 0; i < KN; ++i)
+ {
+ s0 += __builtin_abs (ka[2 * i] - kb[2 * i]);
+ s1 += __builtin_abs (ka[2 * i + 1] - kb[2 * i + 1]);
+ }
+ o[0] = s0;
+ o[1] = s1;
+}
+
+void __attribute__ ((noipa))
+k_lane1 (unsigned int init, unsigned int *o)
+{
+ unsigned int s0 = 0;
+ unsigned int s1 = init & 1;
+ for (int i = 0; i < KN; ++i)
+ {
+ s0 += __builtin_abs (ka[2 * i] - kb[2 * i]);
+ s1 += __builtin_abs (ka[2 * i + 1] - kb[2 * i + 1]);
+ }
+ o[0] = s0;
+ o[1] = s1;
+}
+
+void __attribute__ ((noipa))
+k_lane0 (unsigned int init, unsigned int *o)
+{
+ unsigned int s0 = init & 1;
+ unsigned int s1 = 0;
+ for (int i = 0; i < KN; ++i)
+ {
+ s0 += __builtin_abs (ka[2 * i] - kb[2 * i]);
+ s1 += __builtin_abs (ka[2 * i + 1] - kb[2 * i + 1]);
+ }
+ o[0] = s0;
+ o[1] = s1;
+}
+
+int
+main (void)
+{
+ check_vect ();
+
+ for (int i = 0; i < N; ++i)
+ {
+ a[i] = 255;
+ b[i] = 0;
+ c[i] = 0;
+ d[i] = 255;
+ asm volatile ("" ::: "memory");
+ }
+ for (int i = 0; i < 16 * W; ++i)
+ {
+ p[i] = 255;
+ q[i] = 0;
+ asm volatile ("" ::: "memory");
+ }
+ for (int i = 0; i < 2 * KN; ++i)
+ {
+ ka[i] = 255;
+ kb[i] = 0;
+ asm volatile ("" ::: "memory");
+ }
+
+ if (f257 () != 257 * 255)
+ __builtin_abort ();
+ if (fn257 (257) != 257 * 255)
+ __builtin_abort ();
+ if (fuc (255) != 255 * 255)
+ __builtin_abort ();
+ if (h_n1 () != 2 * 255 * M)
+ __builtin_abort ();
+ if (h_chain16 (p, W, q, W) != 16 * W * 255)
+ __builtin_abort ();
+
+ unsigned int o[2];
+ k_unknown (100000, o);
+ if (o[0] != 65535 || o[1] != 100000 + 65535)
+ __builtin_abort ();
+ k_lane1 (1, o);
+ if (o[0] != 65535 || o[1] != 65536)
+ __builtin_abort ();
+ k_lane0 (1, o);
+ if (o[0] != 65536 || o[1] != 65535)
+ __builtin_abort ();
+
+ unsigned int expected = 0;
+ for (int i = 0; i < 255; ++i)
+ {
+ a[i] = 255 - (i & 3);
+ b[i] = i & 7;
+ asm volatile ("" ::: "memory");
+ expected += __builtin_abs (a[i] - b[i]);
+ }
+ if (f255 () != expected)
+ __builtin_abort ();
+ return 0;
+}
+
+/* { dg-final { scan-tree-dump "vect_recog_sad_pattern: detected" "vect" } } */
+/* { dg-final { scan-tree-dump "narrowing reduction accumulator to" "vect" {
target riscv_zvabd_ok } } } */
diff --git a/gcc/testsuite/gcc.dg/vect/vect-reduc-narrow-prec-2.c
b/gcc/testsuite/gcc.dg/vect/vect-reduc-narrow-prec-2.c
new file mode 100644
index 00000000000..32301670917
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/vect-reduc-narrow-prec-2.c
@@ -0,0 +1,385 @@
+/* { dg-require-effective-target vect_int } */
+/* { dg-additional-options "-fno-vect-cost-model --param
vect-epilogues-nomask=1" } */
+/* { dg-add-options riscv_zvabd } */
+
+#include "tree-vect.h"
+
+#define N 300
+#define HN 129
+#define W 16
+#define H 64
+#define H1 257
+#define H2 258
+
+unsigned char a[N], b[N], cond[N];
+unsigned char c[H * W], d[H * W];
+unsigned char e[16], f[16];
+signed char sc[N];
+unsigned short x[N];
+unsigned char ha[HN], hb[HN], hc[HN], hd[HN], he[HN];
+unsigned char p[17 * W], q[17 * W];
+unsigned char kc[H2 * W], kd[H2 * W];
+unsigned int out[W];
+
+unsigned int __attribute__ ((noipa))
+fn258 (int n)
+{
+ if (n > 258)
+ __builtin_unreachable ();
+ unsigned int s = 0;
+ for (int i = 0; i < n; ++i)
+ s += __builtin_abs (a[i] - b[i]);
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+fnest (void)
+{
+ unsigned int s = 0;
+ for (int j = 0; j < H; ++j)
+ for (int i = 0; i < W; ++i)
+ s += __builtin_abs (c[j * W + i] - d[j * W + i]);
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+g_bit (unsigned int init)
+{
+ unsigned int s = init & 1;
+ for (int i = 0; i < 257; ++i)
+ s += __builtin_abs (a[i] - b[i]);
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+g_unknown (unsigned int init)
+{
+ unsigned int s = init;
+ for (int i = 0; i < 64; ++i)
+ s += __builtin_abs (a[i] - b[i]);
+ return s;
+}
+
+int __attribute__ ((noipa))
+g_neg (int init)
+{
+ int s = -(init & 1023) - 1;
+ for (int i = 0; i < 16; ++i)
+ s += __builtin_abs (e[i] - f[i]);
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+g_ptr (unsigned char *pp, unsigned char *qq, unsigned int init)
+{
+ unsigned int s = init & 1;
+ for (int i = 0; i < 257; ++i)
+ s += __builtin_abs (pp[i] - qq[i]);
+ return s;
+}
+
+unsigned long __attribute__ ((noipa))
+g_ulong (unsigned long init)
+{
+ unsigned long s = init & 1;
+ for (int i = 0; i < 257; ++i)
+ s += __builtin_abs (a[i] - b[i]);
+ return s;
+}
+
+long __attribute__ ((noipa))
+g_long (void)
+{
+ long s = 0;
+ for (int i = 0; i < 257; ++i)
+ s += __builtin_abs (a[i] - b[i]);
+ return s;
+}
+
+int __attribute__ ((noipa))
+p_schar (void)
+{
+ int s = 0;
+ for (int i = 0; i < 257; ++i)
+ s += sc[i];
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+p_schar_u (void)
+{
+ unsigned int s = 0;
+ for (int i = 0; i < 257; ++i)
+ s += (int) sc[i];
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+p_uchar (unsigned int init)
+{
+ unsigned int s = init & 1;
+ for (int i = 0; i < 257; ++i)
+ s += a[i];
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+p_minus (unsigned int init)
+{
+ unsigned int s = init & 1;
+ for (int i = 0; i < 257; ++i)
+ s -= __builtin_abs (a[i] - b[i]);
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+p_cond_sad (void)
+{
+ unsigned int s = 0;
+ for (int i = 0; i < 257; ++i)
+ if (cond[i])
+ s += __builtin_abs (a[i] - b[i]);
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+p_cond_sum (void)
+{
+ unsigned int s = 0;
+ for (int i = 0; i < 257; ++i)
+ {
+ unsigned int v = x[i];
+ if (v < 256)
+ s += v;
+ }
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+h_live (unsigned int *o)
+{
+ unsigned int s = 0, m = 0;
+ for (int i = 0; i < HN; ++i)
+ {
+ m = s + __builtin_abs (ha[i] - hb[i]);
+ s = m + __builtin_abs (hc[i] - hd[i]);
+ }
+ *o = m;
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+h_var (int n)
+{
+ unsigned int s = 0;
+ for (int i = 0; i < n; ++i)
+ {
+ s += __builtin_abs (ha[i] - hb[i]);
+ s += __builtin_abs (hc[i] - hd[i]);
+ }
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+h_n (void)
+{
+ unsigned int s = 0;
+ for (int i = 0; i < HN; ++i)
+ {
+ s += __builtin_abs (ha[i] - hb[i]);
+ s += __builtin_abs (hc[i] - hd[i]);
+ }
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+h_swap (void)
+{
+ unsigned int s = 0;
+ for (int i = 0; i < HN; ++i)
+ s = __builtin_abs (ha[i] - hb[i]) + (__builtin_abs (hc[i] - hd[i]) + s);
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+h_mix_n (void)
+{
+ unsigned int s = 0;
+ for (int i = 0; i < HN; ++i)
+ {
+ s += __builtin_abs (ha[i] - hb[i]);
+ s += he[i];
+ }
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+h_mix_n1 (void)
+{
+ unsigned int s = 0;
+ for (int i = 0; i < HN - 1; ++i)
+ {
+ s += __builtin_abs (ha[i] - hb[i]);
+ s += he[i];
+ }
+ return s;
+}
+
+unsigned int __attribute__ ((noipa))
+h_chain17 (unsigned char *xx, int sx, unsigned char *yy, int sy)
+{
+ unsigned int s = 0;
+ for (int j = 0; j < 17; ++j)
+ {
+#pragma GCC unroll 16
+ for (int i = 0; i < W; ++i)
+ s += __builtin_abs (xx[i] - yy[i]);
+ xx += sx;
+ yy += sy;
+ }
+ return s;
+}
+
+void __attribute__ ((noipa))
+k_outer1 (void)
+{
+ for (int j = 0; j < W; ++j)
+ {
+ unsigned int s = 0;
+ for (int i = 0; i < H1; ++i)
+ s += __builtin_abs (kc[i * W + j] - kd[i * W + j]);
+ out[j] = s;
+ }
+}
+
+void __attribute__ ((noipa))
+k_outer2 (void)
+{
+ for (int j = 0; j < W; ++j)
+ {
+ unsigned int s = 0;
+ for (int i = 0; i < H2; ++i)
+ s += __builtin_abs (kc[i * W + j] - kd[i * W + j]);
+ out[j] = s;
+ }
+}
+
+int
+main (void)
+{
+ check_vect ();
+
+ int expected_sc = 0, expected_neg = -1024;
+ unsigned int expected_cond_sad = 0, expected_cond_sum = 0;
+ for (int i = 0; i < N; ++i)
+ {
+ a[i] = 255;
+ b[i] = 0;
+ cond[i] = i % 3;
+ sc[i] = (i & 3) ? -128 : 127;
+ x[i] = (i & 1) ? 65535 : 255;
+ asm volatile ("" ::: "memory");
+ if (i < 257)
+ {
+ expected_sc += sc[i];
+ if (cond[i])
+ expected_cond_sad += 255;
+ if (x[i] < 256)
+ expected_cond_sum += x[i];
+ }
+ }
+ for (int i = 0; i < H * W; ++i)
+ {
+ c[i] = 255;
+ d[i] = 0;
+ asm volatile ("" ::: "memory");
+ }
+ for (int i = 0; i < 16; ++i)
+ {
+ e[i] = 100 + i;
+ f[i] = 90;
+ asm volatile ("" ::: "memory");
+ expected_neg += __builtin_abs (e[i] - f[i]);
+ }
+ for (int i = 0; i < HN; ++i)
+ {
+ ha[i] = 255;
+ hb[i] = 0;
+ hc[i] = 0;
+ hd[i] = 255;
+ he[i] = 255;
+ asm volatile ("" ::: "memory");
+ }
+ for (int i = 0; i < 17 * W; ++i)
+ {
+ p[i] = 255;
+ q[i] = 0;
+ asm volatile ("" ::: "memory");
+ }
+ for (int i = 0; i < H2 * W; ++i)
+ {
+ kc[i] = 255;
+ kd[i] = 0;
+ asm volatile ("" ::: "memory");
+ }
+
+ if (fn258 (258) != 258 * 255)
+ __builtin_abort ();
+ if (fnest () != H * W * 255)
+ __builtin_abort ();
+ if (g_bit (0) != 65535 || g_bit (1) != 65536)
+ __builtin_abort ();
+ if (g_unknown (70000) != 70000 + 64 * 255)
+ __builtin_abort ();
+ if (g_neg (1023) != expected_neg)
+ __builtin_abort ();
+ for (int off = 0; off < 8; ++off)
+ if (g_ptr (a + off, b + 7 - off, 0) != 65535
+ || g_ptr (a + off, b + 7 - off, 1) != 65536)
+ __builtin_abort ();
+ if (g_ulong (0) != 65535 || g_ulong (1) != 65536)
+ __builtin_abort ();
+ if (g_long () != 65535)
+ __builtin_abort ();
+ if (p_schar () != expected_sc)
+ __builtin_abort ();
+ if (p_schar_u () != (unsigned int) expected_sc)
+ __builtin_abort ();
+ if (p_uchar (0) != 65535 || p_uchar (1) != 65536)
+ __builtin_abort ();
+ if (p_minus (1) != 1u - 65535u)
+ __builtin_abort ();
+ if (p_cond_sad () != expected_cond_sad)
+ __builtin_abort ();
+ if (p_cond_sum () != expected_cond_sum)
+ __builtin_abort ();
+
+ unsigned int m;
+ if (h_live (&m) != 2 * 255 * HN || m != 2 * 255 * HN - 255)
+ __builtin_abort ();
+ if (h_var (HN) != 2 * 255 * HN)
+ __builtin_abort ();
+ if (h_n () != 2 * 255 * HN)
+ __builtin_abort ();
+ if (h_swap () != 2 * 255 * HN)
+ __builtin_abort ();
+ if (h_mix_n () != 2 * 255 * HN)
+ __builtin_abort ();
+ if (h_mix_n1 () != 2 * 255 * (HN - 1))
+ __builtin_abort ();
+ if (h_chain17 (p, W, q, W) != 17 * W * 255)
+ __builtin_abort ();
+
+ k_outer1 ();
+ for (int j = 0; j < W; ++j)
+ if (out[j] != H1 * 255)
+ __builtin_abort ();
+ k_outer2 ();
+ for (int j = 0; j < W; ++j)
+ if (out[j] != H2 * 255)
+ __builtin_abort ();
+ return 0;
+}
+
+/* { dg-final { scan-tree-dump "vect_recog_sad_pattern: detected" "vect" } } */
+/* { dg-final { scan-tree-dump-not "narrowing reduction accumulator to" "vect"
} } */
diff --git a/gcc/testsuite/gcc.dg/vect/vect-reduc-narrow-prec-3.c
b/gcc/testsuite/gcc.dg/vect/vect-reduc-narrow-prec-3.c
new file mode 100644
index 00000000000..e9a7776e0b6
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/vect-reduc-narrow-prec-3.c
@@ -0,0 +1,47 @@
+/* { dg-add-options vect_early_break } */
+/* { dg-require-effective-target vect_early_break_hw } */
+/* { dg-require-effective-target vect_int } */
+/* { dg-add-options riscv_zvabd } */
+
+#include "tree-vect.h"
+
+#define N 257
+
+unsigned char a[N], b[N];
+int stop[N];
+
+unsigned int __attribute__ ((noipa))
+f (void)
+{
+ unsigned int s = 0;
+ for (int i = 0; i < N; ++i)
+ {
+ s += __builtin_abs (a[i] - b[i]);
+ if (stop[i])
+ break;
+ }
+ return s;
+}
+
+int
+main (void)
+{
+ check_vect ();
+
+ for (int i = 0; i < N; ++i)
+ {
+ a[i] = 255;
+ b[i] = 0;
+ stop[i] = 0;
+ asm volatile ("" ::: "memory");
+ }
+ if (f () != 65535)
+ __builtin_abort ();
+
+ stop[200] = 1;
+ if (f () != 201 * 255)
+ __builtin_abort ();
+ return 0;
+}
+
+/* { dg-final { scan-tree-dump "narrowing reduction accumulator to" "vect" {
target riscv_zvabd_ok } } } */
diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
index 78bfd607c5d..cc4a694e722 100644
--- a/gcc/tree-vect-loop.cc
+++ b/gcc/tree-vect-loop.cc
@@ -5210,7 +5210,9 @@ vect_find_reusable_accumulator (loop_vec_info loop_vinfo,
if (!accumulator
|| num_phis != VECT_REDUC_INFO_SCALAR_RESULTS
(accumulator->reduc_info).length ()
|| !std::equal (main_loop_results.begin (), main_loop_results.end (),
- VECT_REDUC_INFO_SCALAR_RESULTS
(accumulator->reduc_info).begin ()))
+ VECT_REDUC_INFO_SCALAR_RESULTS
(accumulator->reduc_info).begin ())
+ || (VECT_REDUC_INFO_NARROWED (accumulator->reduc_info)
+ != VECT_REDUC_INFO_NARROWED (reduc_info)))
return false;
/* Handle the case where we can reduce wider vectors to narrower ones. */
diff --git a/gcc/tree-vect-slp-patterns.cc b/gcc/tree-vect-slp-patterns.cc
index c53325fecdc..61b61112068 100644
--- a/gcc/tree-vect-slp-patterns.cc
+++ b/gcc/tree-vect-slp-patterns.cc
@@ -34,6 +34,7 @@ along with GCC; see the file COPYING3. If not see
#include "stor-layout.h"
#include "gimple-iterator.h"
#include "cfgloop.h"
+#include "tree-ssa-loop-niter.h"
#include "tree-vectorizer.h"
#include "langhooks.h"
#include "gimple-walk.h"
@@ -1809,6 +1810,199 @@ addsub_pattern::build (vec_info *vinfo)
}
}
+class narrow_reduc_pattern : public vect_pattern
+{
+ public:
+ narrow_reduc_pattern (slp_tree *node, slp_tree phi, vec<slp_tree> *path,
+ tree vectype)
+ : vect_pattern (node, path, IFN_LAST), m_phi (phi), m_vectype (vectype)
+ {}
+
+ void build (vec_info *) final override;
+
+ static vect_pattern *
+ recognize (slp_tree_to_load_perm_map_t *, slp_compat_nodes_map_t *,
+ slp_tree *);
+
+ private:
+ slp_tree m_phi;
+ tree m_vectype;
+};
+
+vect_pattern *
+narrow_reduc_pattern::recognize (slp_tree_to_load_perm_map_t *,
+ slp_compat_nodes_map_t *, slp_tree *node_)
+{
+ slp_tree root = *node_;
+ if (SLP_TREE_REDUC_IDX (root) == -1 || !SLP_TREE_VECTYPE (root))
+ return NULL;
+
+ /* Get the reduction path. */
+ auto_vec<slp_tree, 4> path;
+ slp_tree node = root;
+ while (node && SLP_TREE_REDUC_IDX (node) != -1)
+ {
+ /* Verify that we're only dealing with SAD_EXPRs.
+ ??? We could allow all lane-reducing ops here, as well as
+ regular plus. */
+ stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node);
+ gassign *assign = rep ? dyn_cast <gassign *> (rep->stmt) : NULL;
+ if (!assign
+ || gimple_assign_rhs_code (assign) != SAD_EXPR
+ || SLP_TREE_REDUC_IDX (node) != 2
+ || !SLP_TREE_VECTYPE (SLP_TREE_CHILDREN (node)[0]))
+ return NULL;
+
+ path.safe_push (node);
+ node = SLP_TREE_CHILDREN (node)[SLP_TREE_REDUC_IDX (node)];
+ }
+
+ if (!node || path.is_empty ())
+ return NULL;
+
+ /* Once we're here, NODE should be the PHI. */
+ stmt_vec_info phi_info = SLP_TREE_REPRESENTATIVE (node);
+ gphi *phi = phi_info ? dyn_cast <gphi *> (phi_info->stmt) : NULL;
+ if (!phi
+ || STMT_VINFO_DEF_TYPE (phi_info) != vect_reduction_def)
+ return NULL;
+
+ class loop *loop = gimple_bb (phi)->loop_father;
+ edge latch = loop_latch_edge (loop);
+ if (SLP_TREE_CHILDREN (node).length () <= latch->dest_idx
+ || SLP_TREE_CHILDREN (node)[latch->dest_idx] != root)
+ return NULL;
+
+ tree type = TREE_TYPE (gimple_phi_result (phi));
+ if (!INTEGRAL_TYPE_P (type))
+ return NULL;
+
+ /* Only loops with a proper niter estimate for now. */
+ widest_int niters;
+ if (!max_stmt_executions (loop, &niters))
+ return NULL;
+
+ /* ??? We could handle reduction groups with multiple lanes as well. */
+ for (stmt_vec_info p : SLP_TREE_SCALAR_STMTS (node))
+ if (p != phi_info)
+ return NULL;
+
+ wide_int minv, maxv;
+ if (!vect_get_range_info (vect_phi_initial_value (phi), &minv, &maxv)
+ || wi::neg_p (minv, TYPE_SIGN (type)))
+ return NULL;
+
+ widest_int initial = widest_int::from (maxv, TYPE_SIGN (type));
+
+ widest_int sum = 0;
+ for (slp_tree nd : path)
+ {
+ /* Get the replaced statement(s). */
+ for (stmt_vec_info si : SLP_TREE_SCALAR_STMTS (nd))
+ {
+ stmt_vec_info orig = vect_orig_stmt (si);
+ int reduc_idx = STMT_VINFO_REDUC_IDX (orig);
+ if (reduc_idx < 0
+ || gimple_assign_rhs_code (orig->stmt) != PLUS_EXPR)
+ return NULL;
+
+ /* Determine the range of the non-accumulator operand. */
+ int other_idx = reduc_idx ? 0 : 1;
+ if (!vect_get_range_info (gimple_arg (orig->stmt, other_idx),
+ &minv, &maxv)
+ || wi::neg_p (minv, TYPE_SIGN (type)))
+ return NULL;
+ sum += widest_int::from (maxv, TYPE_SIGN (type));
+ }
+ }
+
+ widest_int needed = initial + sum * niters;
+
+ /* Get the next power of two. */
+ unsigned prec = MAX (wi::min_precision (needed, UNSIGNED), 1u);
+ prec = 1u << ceil_log2 (prec);
+ prec = MAX (prec, (unsigned) BITS_PER_UNIT);
+
+ /* Loop until we find a supported SAD or we reached the original
+ precision. */
+ for (; prec < TYPE_PRECISION (type); prec <<= 1)
+ {
+ /* Build a narrower type. Unsigned is fine here as long as we bail
+ for negative ranges. */
+ tree vectype = SLP_TREE_VECTYPE (SLP_TREE_CHILDREN (path[0])[0]);
+ tree narrow_type = build_nonstandard_integer_type (prec, UNSIGNED);
+ tree narrow_vectype
+ = get_related_vectype_for_scalar_type (TYPE_MODE (vectype),
+ narrow_type);
+
+ if (!narrow_vectype)
+ continue;
+
+ /* Check if SAD is still supported in the new type. Also, ensure that
+ all input vectypes are the same. */
+ bool ok = true;
+ for (slp_tree nd : path)
+ {
+ tree vtype = SLP_TREE_VECTYPE (SLP_TREE_CHILDREN (nd)[0]);
+ if (vtype != vectype)
+ return NULL;
+ if (!directly_supported_p (SAD_EXPR, narrow_vectype, vectype))
+ {
+ ok = false;
+ break;
+ }
+ }
+
+ /* Also ensure that we can reduce in the new type. */
+ if (!ok || !direct_internal_fn_supported_p
+ (IFN_REDUC_PLUS, narrow_vectype, OPTIMIZE_FOR_SPEED))
+ continue;
+
+ return new narrow_reduc_pattern (node_, node, &path, narrow_vectype);
+ }
+
+ return NULL;
+}
+
+void
+narrow_reduc_pattern::build (vec_info *vinfo)
+{
+ loop_vec_info loop_vinfo = as_a <loop_vec_info> (vinfo);
+ tree narrow_type = TREE_TYPE (m_vectype);
+ tree acc = NULL_TREE;
+ for (unsigned i = m_ops.length (); i-- > 0;)
+ {
+ slp_tree n = m_ops[i];
+ stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (n);
+ stmt_vec_info orig = vect_orig_stmt (rep);
+ gassign *old = as_a <gassign *> (rep->stmt);
+ tree lhs = make_ssa_name (narrow_type);
+ gassign *sad = gimple_build_assign (lhs, SAD_EXPR,
+ gimple_assign_rhs1 (old),
+ gimple_assign_rhs2 (old),
+ acc ? acc : gimple_assign_rhs3 (old));
+ gimple_set_bb (sad, gimple_bb (orig->stmt));
+ stmt_vec_info new_rep = vinfo->add_pattern_stmt (sad, orig);
+ STMT_VINFO_DEF_TYPE (new_rep) = STMT_VINFO_DEF_TYPE (rep);
+ STMT_VINFO_REDUC_IDX (new_rep) = STMT_VINFO_REDUC_IDX (rep);
+ STMT_VINFO_REDUC_DEF (new_rep) = STMT_VINFO_REDUC_DEF (rep);
+ STMT_VINFO_RELEVANT (new_rep) = STMT_VINFO_RELEVANT (rep);
+ STMT_VINFO_LIVE_P (new_rep) = STMT_VINFO_LIVE_P (rep);
+ STMT_SLP_TYPE (new_rep) = pure_slp;
+ STMT_VINFO_VECTYPE (new_rep) = m_vectype;
+ SLP_TREE_REPRESENTATIVE (n) = new_rep;
+ SLP_TREE_VECTYPE (n) = m_vectype;
+ acc = lhs;
+ }
+ SLP_TREE_VECTYPE (m_phi) = m_vectype;
+ VECT_REDUC_INFO_NARROWED (info_for_reduction (loop_vinfo, m_phi)) = true;
+
+ if (dump_enabled_p ())
+ dump_printf_loc (MSG_NOTE, vect_location,
+ "narrowing reduction accumulator to %T: %G",
+ m_vectype, SLP_TREE_REPRESENTATIVE (m_phi)->stmt);
+}
+
/*******************************************************************************
* Pattern matching definitions
******************************************************************************/
@@ -1821,7 +2015,8 @@ vect_pattern_decl_t slp_patterns[]
overlap in what they can detect. */
SLP_PATTERN (complex_operations_pattern),
- SLP_PATTERN (addsub_pattern)
+ SLP_PATTERN (addsub_pattern),
+ SLP_PATTERN (narrow_reduc_pattern)
};
#undef SLP_PATTERN
diff --git a/gcc/tree-vectorizer.h b/gcc/tree-vectorizer.h
index 7184f193b07..26c83fbab2b 100644
--- a/gcc/tree-vectorizer.h
+++ b/gcc/tree-vectorizer.h
@@ -900,6 +900,9 @@ public:
/* The vector type for performing the actual reduction operation. */
tree reduc_vectype;
+ /* Whether this reduction's precision was narrowed. */
+ bool narrowed;
+
/* The vector type we should use for the final reduction in the epilogue
when we reduce a mask. */
tree reduc_vectype_for_mask;
@@ -934,6 +937,7 @@ typedef class vect_reduc_info_s *vect_reduc_info;
#define VECT_REDUC_INFO_TYPE(I) ((I)->reduc_type)
#define VECT_REDUC_INFO_CODE(I) ((I)->reduc_code)
#define VECT_REDUC_INFO_FN(I) ((I)->reduc_fn)
+#define VECT_REDUC_INFO_NARROWED(I) ((I)->narrowed)
#define VECT_REDUC_INFO_SCALAR_RESULTS(I) ((I)->reduc_scalar_results)
#define VECT_REDUC_INFO_INITIAL_VALUES(I) ((I)->reduc_initial_values)
#define VECT_REDUC_INFO_REUSED_ACCUMULATOR(I) ((I)->reused_accumulator)
--
2.56.0