Hi,
This patch tries to narrow the precision of a reduction accumulator with
the help of range information.
Instead of forcing pattern matching, the approach is to go over all
reductions in a loop and find the ones we support. Then go over the
initial as well as the reduction statements, determine their range and
check if the range fits a smaller type. If so, we change the vectype of
the affected nodes.
I tried to keep the implementation small even though we could support
several other cases still. The one I have in mind is x264's SAD loop
which just fits into u16 but RISC-V only has a 2x (qi -> hi or hi -> si)
SAD instruction.
Theoretically, this could be extended to all lane-reducing operations as
well as regular PLUS_EXPR/MINUS_EXPR. Also, in this patch I opted for
requiring target support for both the 4x as well as the 2x SAD
in order to ensure we can always code-gen, even if narrowing fails.
A better solution is to enhance e.g. vectorizable_lane_reducing so it
can do a 2x -> 4x fallback itself by extending inputs or the output.
I guess the approach here will be a bit controversial but I didn't
really find a pattern-match approach much easier. As mentioned in the
PR, at first I had a split approach where I determined the reduced
precision in vect_get_simple_reduction and pass it on via
STMT_VINFO_REDUC_PREC. Later I realized that having everything in one
place is simpler.
A slightly older patch version was bootstrapped and regtested on x86,
power10, and aarch64 as well as regtested on riscv64.
This one is in progress, but the changes are minimal and criticism of
the approach is valid nonetheless :)
The patch depends on SAD being a conv optab for which I just sent
another patch. The riscv check in the test depends on riscv's SAD
autovec support which I'm going to post later.
Assisted-By: Claude Code for tests -1 and -2.
Regards
Robin
gcc/ChangeLog:
* tree-vect-loop.cc (vect_narrow_reductions): New function.
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 | 192 +++++++++
4 files changed, 809 insertions(+)
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 7ed3f58bf23..3cba6a06b7e 100644
--- a/gcc/tree-vect-loop.cc
+++ b/gcc/tree-vect-loop.cc
@@ -163,6 +163,7 @@ static void vect_estimate_min_profitable_iters
(loop_vec_info, int *, int *,
unsigned *);
static stmt_vec_info vect_is_simple_reduction (loop_vec_info, stmt_vec_info,
gphi **);
+static void vect_narrow_reductions (vec_info *);
/* Function vect_is_simple_iv_evolution.
@@ -2374,6 +2375,8 @@ start_over:
if (!ok)
return ok;
+ vect_narrow_reductions (loop_vinfo);
+
/* If there are any SLP instances mark them as pure_slp and compute
the overall vectorization factor. */
if (!vect_make_slp_decision (loop_vinfo))
@@ -12077,3 +12080,192 @@ vect_rgroup_iv_might_wrap_p (loop_vec_info
loop_vinfo, rgroup_controls *rgc)
return false;
}
+
+/* Iterate over all reductions in the loop, get the ones that we know how to
+ narrow and try to narrow the accumulator using range information.
+ The function is kept simple for now and only handles SAD_EXPR reductions.
+ We expect the SAD_EXPR to be pattern recognized before and determine the
+ range of the original statements. Both the wide as well as the narrow
+ SAD_EXPR must be supported by the target. */
+
+void
+vect_narrow_reductions (vec_info *vinfo)
+{
+ if (!is_a <loop_vec_info> (vinfo))
+ return;
+
+ DUMP_VECT_SCOPE ("vect_narrow_reductions");
+
+ /* Only loops with a proper niter estimate for now */
+ loop_vec_info loop_vinfo = as_a <loop_vec_info> (vinfo);
+
+ widest_int niters;
+ if (!max_stmt_executions (LOOP_VINFO_LOOP (loop_vinfo), &niters))
+ return;
+
+ edge latch = loop_latch_edge (LOOP_VINFO_LOOP (loop_vinfo));
+ slp_instance instance;
+ unsigned i;
+ FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (loop_vinfo), i, instance)
+ {
+ /* Look only for reductions. */
+ if (SLP_INSTANCE_KIND (instance) != slp_inst_kind_reduc_chain
+ && SLP_INSTANCE_KIND (instance) != slp_inst_kind_reduc_group)
+ continue;
+
+ slp_tree root = SLP_INSTANCE_TREE (instance);
+
+ /* ??? We could handle reduction groups with multiple lanes as well. */
+ if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_reduc_group
+ && SLP_TREE_LANES (root) > 1)
+ continue;
+
+ /* Get the reduction path. */
+ auto_vec<slp_tree> path;
+ slp_tree node = root;
+ while (node && SLP_TREE_REDUC_IDX (node) != -1)
+ {
+ path.safe_push (node);
+ node = SLP_TREE_CHILDREN (node)[SLP_TREE_REDUC_IDX (node)];
+ }
+ if (!node || path.is_empty ())
+ continue;
+
+ /* Once we're here, NODE should be the PHI. */
+ stmt_vec_info phi_info = SLP_TREE_REPRESENTATIVE (node);
+ if (!phi_info
+ || !is_a <gphi *> (phi_info->stmt)
+ || STMT_VINFO_DEF_TYPE (phi_info) != vect_reduction_def
+ || SLP_TREE_CHILDREN (node).length () <= latch->dest_idx
+ || SLP_TREE_CHILDREN (node)[latch->dest_idx] != root)
+ continue;
+
+ tree type = TREE_TYPE (gimple_phi_result (phi_info->stmt));
+ if (!INTEGRAL_TYPE_P (type))
+ continue;
+
+ wide_int minv, maxv;
+ if (!vect_get_range_info
+ (vect_phi_initial_value (as_a <gphi *> (phi_info->stmt)),
+ &minv, &maxv))
+ continue;
+
+ if (wi::neg_p (minv) || wi::neg_p (maxv))
+ continue;
+
+ widest_int initial = widest_int::from (maxv, TYPE_SIGN (type));
+
+ /* Verify that we're only dealing with SAD_EXPRs.
+ ??? We could allow all lane-reducing ops here, as well as
+ regular plus/minus. */
+ bool narrow_p = true;
+ widest_int sum = 0;
+ for (slp_tree nd : path)
+ {
+ stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (nd);
+ gassign *assign
+ = stmt_info ? dyn_cast <gassign *> (stmt_info->stmt) : NULL;
+ if (!assign
+ || !stmt_info->pattern_stmt_p
+ || gimple_assign_rhs_code (assign) != SAD_EXPR)
+ {
+ narrow_p = false;
+ break;
+ }
+
+ /* Get the replaced statement(s). */
+ for (stmt_vec_info si : SLP_TREE_SCALAR_STMTS (nd))
+ {
+ if (!si)
+ continue;
+ 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)
+ {
+ narrow_p = false;
+ break;
+ }
+
+ /* 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))
+ {
+ narrow_p = false;
+ break;
+ }
+ sum = wi::add (sum, widest_int::from (maxv, TYPE_SIGN (type)));
+ }
+ }
+
+ if (!narrow_p)
+ continue;
+
+ widest_int needed = initial + sum * niters;
+
+ /* Get the next power of two. */
+ int prec = 1 << ceil_log2 (wi::min_precision (needed, UNSIGNED));
+ prec = MAX (prec, BITS_PER_UNIT);
+
+ /* Loop until we find a supported SAD or we reached the original
+ precision. */
+ narrow_p = false;
+ tree narrow_type, narrow_vectype;
+ for (; prec < TYPE_PRECISION (type); prec <<= 1)
+ {
+ /* Build a narrower type. Unsigned is fine here as long as we bail
+ for negative ranges. */
+ narrow_type = build_nonstandard_integer_type (prec, UNSIGNED);
+ narrow_vectype
+ = get_vectype_for_scalar_type (loop_vinfo, narrow_type, node);
+ if (!narrow_vectype)
+ continue;
+
+ /* Check if SAD is still supported in the new type. */
+ bool ok = true;
+ for (slp_tree nd : path)
+ {
+ slp_tree op = SLP_TREE_CHILDREN (nd)[0];
+ tree vectype = SLP_TREE_VECTYPE (op);
+ /* The vectype can still be unset, e.g. for globals.
+ Get it from the SAD's input. */
+ gassign *assign = as_a <gassign *>
+ (SLP_TREE_REPRESENTATIVE (nd)->stmt);
+ if (!vectype)
+ vectype = get_vectype_for_scalar_type
+ (loop_vinfo, TREE_TYPE (gimple_assign_rhs1 (assign)),
+ op);
+
+ if (!vectype
+ || !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;
+
+ narrow_p = true;
+ break;
+ }
+
+ if (!narrow_p)
+ continue;
+
+ /* By now, we can update the precision of the phi as well as the
+ individual statements. */
+ SLP_TREE_VECTYPE (node) = narrow_vectype;
+ for (slp_tree n : path)
+ SLP_TREE_VECTYPE (n) = narrow_vectype;
+
+ if (dump_enabled_p ())
+ dump_printf_loc (MSG_NOTE, vect_location,
+ "narrowing reduction accumulator to %T: %G",
+ narrow_vectype, phi_info->stmt);
+ }
+}
--
2.56.0