From: Kyrylo Tkachov <[email protected]>
The BB SLP schedule places the vector code of an operation right after
its vector operands are available. It places the vector of an external
right after the definition of its last scalar operand. So the computed
schedule often places vector code before a branch, although the scalar
code that it replaces runs only in one arm. The cost model reads this
schedule. The slp1 dump of the loop in bb-slp-pr127246-5.c shows an
example (address computations, loads and most dead scalar code left
out):
<bb 3>:
x_53 = foo (_4);
y_55 = foo (_8);
_105 = {x_53, y_55, x_53, y_55, x_53, y_55, x_53, y_55};
vect__16.4_106 = _105 + { 3, 5, 7, 9, 11, 13, 15, 17 };
if (_11 == 42)
...
<bb 4>:
_16 = x_53 + 3;
...
MEM <vector(8) short unsigned int> [(short unsigned int *)vectp.6_107] =
vect__16.4_106;
Later passes can sink such code. But the costing cannot rely on that,
and the next patch checks the costs of each block.
Place the vector code of an operation right before its first scalar
stmt. Do this when the block of the vector operands strictly dominates
the block of that stmt. The block of the stmt must also be in the same
loop and outside of an irreducible region. Pre-scheduling does not see
vector defs before the region. So the transform does this only for the
operations that pre-scheduling placed there, and only when the actual
vector defs allow it. The rule for possibly trapping operations does
not change. The new rule does not apply to permutes and to nodes
without scalar stmts, for example the two halves of a node that mixes
additions and subtractions.
After a node is placed, place with it each external that only this node
uses. Do this when the external has an operand defined in the region
and the same condition holds for the block of the node. The node then
builds the vectors of the external right before its own vector code. A
vector whose operands are available in another loop stays after the
definition of its last operand. Externals of PHIs and externals with
operands in abnormal PHIs do not move, and neither do externals that
several nodes use. Constants and externals without operands defined in
the region stay at the region entry.
_slp_tree::si records these places, and the costing reads it. The new
places are in the same loop as the old ones, so the cost of each loop
stays the same. Live lanes are the exception.
vect_bb_slp_mark_live_stmts checks their uses against si, and against
the last scalar stmt of the node when the node is at the region entry.
So it can now extract a lane of a node that this patch moves from the
region entry to its first scalar stmt. This changes the costs of
gcc.dg/vect/pr97835.c, but not the decision. The example of
bb-slp-pr127246-5.c becomes:
<bb 3>:
x_53 = foo (_4);
y_55 = foo (_8);
if (_11 == 42)
...
<bb 4>:
_105 = {x_53, y_55, x_53, y_55, x_53, y_55, x_53, y_55};
vect__16.4_106 = _105 + { 3, 5, 7, 9, 11, 13, 15, 17 };
_16 = x_53 + 3;
...
MEM <vector(8) short unsigned int> [(short unsigned int *)vectp.6_107] =
vect__16.4_106;
Bootstrapped and regression tested together with the rest of the series
on aarch64-unknown-linux-gnu and x86_64-pc-linux-gnu.
gcc/ChangeLog:
PR tree-optimization/127246
* tree-vectorizer.h (_slp_tree::si): Say how pre-scheduling sets it
and where the vector code goes.
* tree-vect-slp.cc (vect_bb_slp_sink_p, vect_slp_external_at_user_p):
New functions.
(vect_create_constant_vectors): Add USER_GSI parameter. Build the
vectors before it when vect_bb_slp_sink_p allows it.
(vect_schedule_slp_node): Place operations of BB vectorization right
before their first scalar stmt when vect_bb_slp_sink_p allows it, in
the transform only when pre-scheduling placed them there. Place
with the node each external that only the node uses and that has an
operand defined in the region, and build it there.
gcc/testsuite/ChangeLog:
PR tree-optimization/127246
* gcc.dg/vect/bb-slp-pr127246-3.c: New test.
* gcc.dg/vect/bb-slp-pr127246-4.c: New test.
* gcc.dg/vect/bb-slp-pr127246-5.c: New test.
* gcc.dg/vect/bb-slp-pr127246-6.c: New test.
* gcc.dg/vect/bb-slp-pr127246-8.c: New test.
* gcc.dg/vect/bb-slp-pr127246-11.c: New test.
* gcc.dg/vect/bb-slp-pr127246-12.c: New test.
* gcc.dg/vect/bb-slp-pr127246-15.c: New test.
* gcc.dg/vect/bb-slp-pr127246-16.c: New test.
* gcc.dg/vect/bb-slp-pr127246-17.c: New test.
* gcc.dg/vect/bb-slp-pr127246-18.c: New test.
* gcc.dg/vect/bb-slp-pr127246-19.c: New test.
* gcc.dg/vect/bb-slp-pr127246-20.c: New test.
* g++.dg/vect/slp-pr127246.cc: New test.
Signed-off-by: Kyrylo Tkachov <[email protected]>
---
gcc/testsuite/g++.dg/vect/slp-pr127246.cc | 30 +++++
.../gcc.dg/vect/bb-slp-pr127246-11.c | 108 ++++++++++++++++
.../gcc.dg/vect/bb-slp-pr127246-12.c | 55 ++++++++
.../gcc.dg/vect/bb-slp-pr127246-15.c | 115 +++++++++++++++++
.../gcc.dg/vect/bb-slp-pr127246-16.c | 29 +++++
.../gcc.dg/vect/bb-slp-pr127246-17.c | 28 ++++
.../gcc.dg/vect/bb-slp-pr127246-18.c | 65 ++++++++++
.../gcc.dg/vect/bb-slp-pr127246-19.c | 26 ++++
.../gcc.dg/vect/bb-slp-pr127246-20.c | 29 +++++
gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-3.c | 50 +++++++
gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-4.c | 74 +++++++++++
gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-5.c | 33 +++++
gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-6.c | 54 ++++++++
gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-8.c | 88 +++++++++++++
gcc/tree-vect-slp.cc | 122 ++++++++++++++++--
gcc/tree-vectorizer.h | 10 +-
16 files changed, 905 insertions(+), 11 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/vect/slp-pr127246.cc
create mode 100644 gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-11.c
create mode 100644 gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-12.c
create mode 100644 gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-15.c
create mode 100644 gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-16.c
create mode 100644 gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-17.c
create mode 100644 gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-18.c
create mode 100644 gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-19.c
create mode 100644 gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-20.c
create mode 100644 gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-3.c
create mode 100644 gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-4.c
create mode 100644 gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-5.c
create mode 100644 gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-6.c
create mode 100644 gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-8.c
diff --git a/gcc/testsuite/g++.dg/vect/slp-pr127246.cc
b/gcc/testsuite/g++.dg/vect/slp-pr127246.cc
new file mode 100644
index 00000000000..3bc4a52f60e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/vect/slp-pr127246.cc
@@ -0,0 +1,30 @@
+// PR tree-optimization/127246
+// { dg-do compile }
+
+// v is loaded before the loop, and the region starts in the loop after
+// geti (), which can throw. The vector multiplications must not go after
+// the place that pre-scheduling computed for them.
+
+typedef int v4si __attribute__ ((vector_size (16)));
+int geti (void);
+struct s1 { ~s1 (); };
+
+void
+f (v4si *r, v4si *p, int *__restrict w, int *c, int n)
+{
+ s1 guard;
+ v4si v = *p;
+ for (int i = 0; i < n; i++)
+ {
+ int t = geti ();
+ int w0 = w[4 * i], w1 = w[4 * i + 1], w2 = w[4 * i + 2];
+ int w3 = w[4 * i + 3];
+ if (c[i] + t + w0)
+ {
+ int s0 = v[0] + 1, s1 = v[1] + 2, s2 = v[2] + 3, s3 = v[3] + 4;
+ r[i] = (v4si) { s0 * w0, s1 * w1, s2 * w2, s3 * w3 };
+ }
+ }
+}
+
+// { dg-final { scan-tree-dump "optimized: basic block part vectorized" "slp1"
{ target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } }
diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-11.c
b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-11.c
new file mode 100644
index 00000000000..81dda5cc42e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-11.c
@@ -0,0 +1,108 @@
+/* PR tree-optimization/127246 */
+/* { dg-additional-options "-mprefer-vector-width=128" { target i?86-*-*
x86_64-*-* } } */
+/* { dg-additional-options "-mautovec-preference=asimd-only" { target
aarch64*-*-* } } */
+
+/* The first four stored values are defined before the branch and the
+ others in the conditional block. The last of them is defined in the
+ block of the stores, so their vectors are not placed with the stores. */
+
+#include "tree-vect.h"
+
+__attribute__ ((noipa)) int
+foo (int i)
+{
+ return i * 3 + 1;
+}
+
+__attribute__ ((noipa)) void
+f (int *out, int c)
+{
+ int x0 = foo (0), x1 = foo (1), x2 = foo (2), x3 = foo (3);
+ if (c)
+ {
+ int x4 = foo (4), x5 = foo (5), x6 = foo (6), x7 = foo (7);
+ out[0] = x0;
+ out[1] = x1;
+ out[2] = x2;
+ out[3] = x3;
+ out[4] = x4;
+ out[5] = x5;
+ out[6] = x6;
+ out[7] = x7;
+ }
+}
+
+/* In g and h the first four stored values are defined before the loop and
+ the others in it. The vector of the first four is built before the
+ loop, also when the vector of the stored values is placed with the
+ stores in the conditional block of h. */
+
+__attribute__ ((noipa)) void
+g (int *out, int n)
+{
+ int x0 = foo (0), x1 = foo (1), x2 = foo (2), x3 = foo (3);
+ for (int i = 0; i < n; i++)
+ {
+ int x4 = foo (4), x5 = foo (5), x6 = foo (6), x7 = foo (7);
+ out[8 * i + 0] = x0;
+ out[8 * i + 1] = x1;
+ out[8 * i + 2] = x2;
+ out[8 * i + 3] = x3;
+ out[8 * i + 4] = x4;
+ out[8 * i + 5] = x5;
+ out[8 * i + 6] = x6;
+ out[8 * i + 7] = x7;
+ }
+}
+
+__attribute__ ((noipa)) void
+h (int *out, int *c, int n)
+{
+ int x0 = foo (0), x1 = foo (1), x2 = foo (2), x3 = foo (3);
+ for (int i = 0; i < n; i++)
+ {
+ int x4 = foo (4), x5 = foo (5), x6 = foo (6), x7 = foo (7);
+ if (c[i])
+ {
+ out[8 * i + 0] = x0;
+ out[8 * i + 1] = x1;
+ out[8 * i + 2] = x2;
+ out[8 * i + 3] = x3;
+ out[8 * i + 4] = x4;
+ out[8 * i + 5] = x5;
+ out[8 * i + 6] = x6;
+ out[8 * i + 7] = x7;
+ }
+ }
+}
+
+int
+main ()
+{
+ int out[16] = {};
+ int c[2] = { 1, 0 };
+
+ check_vect ();
+
+ f (out, 0);
+ for (int i = 0; i < 8; i++)
+ if (out[i] != 0)
+ abort ();
+ f (out, 1);
+ for (int i = 0; i < 8; i++)
+ if (out[i] != i * 3 + 1)
+ abort ();
+ g (out, 2);
+ for (int i = 0; i < 16; i++)
+ if (out[i] != (i % 8) * 3 + 1)
+ abort ();
+ __builtin_memset (out, 0, sizeof (out));
+ h (out, c, 2);
+ for (int i = 0; i < 16; i++)
+ if (out[i] != (i < 8 ? i * 3 + 1 : 0))
+ abort ();
+ return 0;
+}
+
+/* { dg-final { scan-tree-dump-times "foo \\(3\\);\n *_\[0-9\]+ = \{x0_" 2
"slp1" { target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } }
} */
+/* { dg-final { scan-tree-dump "placing node \[^\n\r\]* with node" "slp1" {
target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */
diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-12.c
b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-12.c
new file mode 100644
index 00000000000..5b5eda0baa6
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-12.c
@@ -0,0 +1,55 @@
+/* PR tree-optimization/127246 */
+/* { dg-do compile } */
+
+/* The stores are in an irreducible region. The vector code of the
+ additions in f and the vector of x and y in g stay in front of it. */
+
+extern void bar (unsigned short *);
+extern unsigned short foo (unsigned short);
+
+void
+f (unsigned short *out, unsigned short x, unsigned short y, int c, int n)
+{
+ int i = 0;
+ if (c)
+ goto second;
+first:
+ out[0] = x + 3;
+ out[1] = y + 5;
+ out[2] = x + 7;
+ out[3] = y + 9;
+ out[4] = x + 11;
+ out[5] = y + 13;
+ out[6] = x + 15;
+ out[7] = y + 17;
+second:
+ bar (out);
+ if (++i < n)
+ goto first;
+}
+
+void
+g (unsigned short *out, unsigned short a, unsigned short b, int c, int n)
+{
+ unsigned short x = foo (a), y = foo (b);
+ int i = 0;
+ if (c)
+ goto second;
+first:
+ out[0] = x;
+ out[1] = y;
+ out[2] = x;
+ out[3] = y;
+ out[4] = x;
+ out[5] = y;
+ out[6] = x;
+ out[7] = y;
+second:
+ bar (out);
+ if (++i < n)
+ goto first;
+}
+
+/* { dg-final { scan-tree-dump-times "optimized: basic block part vectorized"
2 "slp2" { target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } }
} } */
+/* { dg-final { scan-tree-dump-not "placing node \[^\n\r\]* before" "slp2" } }
*/
+/* { dg-final { scan-tree-dump-not "placing node \[^\n\r\]* with node" "slp2"
} } */
diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-15.c
b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-15.c
new file mode 100644
index 00000000000..3366147e15f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-15.c
@@ -0,0 +1,115 @@
+/* PR tree-optimization/127246 */
+/* { dg-do compile } */
+
+/* Externals whose vectors must stay where the region defines their
+ operands. */
+
+extern int rt (void) __attribute__ ((returns_twice));
+extern int foo (int);
+extern void bar (void);
+extern void baz (void);
+extern void use (int);
+
+/* v is used after rt (), and the calls to bar () can return to rt (), so
+ v occurs in abnormal PHIs. Lane 0 stores v before v is redefined, so a
+ vector of v and w placed with the stores would extend the live range
+ of v. */
+
+void
+f1 (short *restrict out, int a, int b, int d)
+{
+ short v = a * 5 + 1;
+ short w = b / d;
+ bar ();
+ out[0] = v;
+ v = v * 3;
+ out[1] = w;
+ out[2] = w;
+ out[3] = w;
+ out[4] = w;
+ out[5] = w;
+ out[6] = w;
+ out[7] = w;
+ bar ();
+ rt ();
+ use (v);
+}
+
+/* The same for the user of the vector of v and w being an addition, which
+ is placed after the late definition of y. */
+
+void
+f2 (int *restrict out, int *restrict in, int a, int b, int d, int e,
+ int q0, int q1, int q2)
+{
+ int v = a / d;
+ int w = b / d;
+ bar ();
+ int m0 = in[0] * q0;
+ int m1 = in[1] * q1;
+ int m2 = in[2] * q2;
+ int l0 = m0 + v;
+ v = v * 3;
+ int y = e / d;
+ int m3 = in[3] * y;
+ int l1 = m1 + w;
+ int l2 = m2 + w;
+ int l3 = m3 + w;
+ out[0] = l0;
+ out[1] = l1;
+ out[2] = l2;
+ out[3] = l3;
+ bar ();
+ rt ();
+ use (v);
+}
+
+/* The first addition comes before the definition of t in the same block,
+ so the vector addition must follow t. */
+
+void
+f3 (int *restrict out, int a, int b, int c, int d)
+{
+ int x0 = a + 1;
+ int x1 = b + 2;
+ int t = foo (c);
+ int x2 = t + 3;
+ int x3 = d + 4;
+ out[0] = x0;
+ out[1] = x1;
+ out[2] = x2;
+ out[3] = x3;
+}
+
+/* The PHIs of s0 to s3 use the vectors of a and b and of c and d on the
+ incoming edges. */
+
+void
+f4 (int *out, int k)
+{
+ int a = foo (1), b = foo (2), c = foo (3), d = foo (4);
+ int s0, s1, s2, s3;
+ if (k)
+ {
+ bar ();
+ s0 = a;
+ s1 = b;
+ s2 = a;
+ s3 = b;
+ }
+ else
+ {
+ baz ();
+ s0 = c;
+ s1 = d;
+ s2 = c;
+ s3 = d;
+ }
+ out[0] = s0 + 1;
+ out[1] = s1 + 2;
+ out[2] = s2 + 3;
+ out[3] = s3 + 4;
+}
+
+/* { dg-final { scan-tree-dump-times "optimized: basic block part vectorized"
4 "slp2" { target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } }
} } */
+/* { dg-final { scan-tree-dump-not "placing node \[^\n\r\]* with node" "slp2"
} } */
diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-16.c
b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-16.c
new file mode 100644
index 00000000000..e4d36dda27f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-16.c
@@ -0,0 +1,29 @@
+/* PR tree-optimization/127246 */
+/* { dg-do compile } */
+/* { dg-additional-options "-mautovec-preference=asimd-only" { target
aarch64*-*-* } } */
+
+/* The additions and subtractions form one node with lanes in two blocks.
+ The stores before the branch use its low part. The halves of the node
+ have no scalar stmts, so they keep their place before the branch. */
+
+void
+f (int *restrict out, int *restrict out2, int *restrict in, int c)
+{
+ int a0 = in[0], a1 = in[1], a2 = in[2], a3 = in[3];
+ int b0 = in[4], b1 = in[5], b2 = in[6], b3 = in[7];
+ int x0 = a0 + b0;
+ int x1 = a1 - b1;
+ out2[0] = x0;
+ out2[1] = x1;
+ if (c)
+ {
+ int x2 = a2 + b2;
+ int x3 = a3 - b3;
+ out[0] = x0;
+ out[1] = x1;
+ out[2] = x2;
+ out[3] = x3;
+ }
+}
+
+/* { dg-final { scan-tree-dump-times "optimized: basic block part vectorized"
2 "slp2" { target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } }
} } */
diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-17.c
b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-17.c
new file mode 100644
index 00000000000..a8440b4c478
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-17.c
@@ -0,0 +1,28 @@
+/* PR tree-optimization/127246 */
+/* { dg-do compile } */
+/* { dg-additional-options "-fvect-cost-model=dynamic" } */
+/* { dg-additional-options "-mtune=generic" { target aarch64*-*-* i?86-*-*
x86_64-*-* } } */
+
+/* The vector of t and t2 is used only by the additions in the conditional
+ block, so it is placed with them and not after the calls in the loop
+ header. */
+
+extern int foo (int), bar (int);
+
+void
+f (int *restrict out, int *restrict a, int *restrict c, int n)
+{
+ for (int i = 0; i < n; i++)
+ {
+ int t = foo (i), t2 = bar (i);
+ if (c[i])
+ {
+ out[4 * i + 0] = a[4 * i + 0] + t;
+ out[4 * i + 1] = a[4 * i + 1] + t2;
+ out[4 * i + 2] = a[4 * i + 2] + t;
+ out[4 * i + 3] = a[4 * i + 3] + t2;
+ }
+ }
+}
+
+/* { dg-final { scan-tree-dump "placing node \[^\n\r\]* with node" "slp1" {
target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */
diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-18.c
b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-18.c
new file mode 100644
index 00000000000..90a7851d969
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-18.c
@@ -0,0 +1,65 @@
+/* PR tree-optimization/127246 */
+/* { dg-do compile } */
+
+/* v occurs in abnormal PHIs. The vector additions go before their first
+ scalar addition, after the redefinition of v, so they must not use v. */
+
+typedef int v4si __attribute__ ((vector_size (16)));
+extern int rt (void) __attribute__ ((returns_twice));
+extern void bar (void);
+extern void use (v4si);
+
+void
+f1 (int *out, v4si v, int c)
+{
+ int a0 = v[0], a1 = v[1], a2 = v[2], a3 = v[3];
+ bar ();
+ v = v * 3;
+ if (c)
+ {
+ out[0] = a0 + 1;
+ out[1] = a1 + 2;
+ out[2] = a2 + 3;
+ out[3] = a3 + 4;
+ }
+ bar ();
+ rt ();
+ use (v);
+}
+
+void
+f2 (int *out, v4si v)
+{
+ int a0 = v[0], a1 = v[1], a2 = v[2], a3 = v[3];
+ bar ();
+ v = v * 3;
+ out[0] = a0 + 1;
+ out[1] = a1 + 2;
+ out[2] = a2 + 3;
+ out[3] = a3 + 4;
+ bar ();
+ rt ();
+ use (v);
+}
+
+/* The vector of a0, a1, a2 and x goes with the addition. */
+
+void
+f3 (int *out, v4si v, int *p)
+{
+ int a0 = v[0], a1 = v[1], a2 = v[2];
+ int x = *p;
+ bar ();
+ v = v * 3;
+ out[0] = a0 + 1;
+ out[1] = a1 + 2;
+ out[2] = a2 + 3;
+ out[3] = x + 4;
+ bar ();
+ rt ();
+ use (v);
+}
+
+/* { dg-final { scan-tree-dump-times "placing node \[^\n\r\]* before" 3 "slp2"
{ target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */
+/* { dg-final { scan-tree-dump-times "placing node \[^\n\r\]* with node" 3
"slp2" { target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } }
} */
+/* { dg-final { scan-tree-dump-times "optimized: basic block part vectorized"
3 "slp2" { target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } }
} } */
diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-19.c
b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-19.c
new file mode 100644
index 00000000000..5971edde015
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-19.c
@@ -0,0 +1,26 @@
+/* PR tree-optimization/127246 */
+/* { dg-do compile } */
+
+/* The lanes of the additions are extracts of the vector v. sink1 moves
+ the extracts into the arm. The permute node that stands for v stays
+ at the region entry, and the vector addition goes into the arm, right
+ before the first scalar addition. */
+
+typedef int v4si __attribute__ ((vector_size (16)));
+
+void
+f (int *restrict out, v4si v, int c)
+{
+ int a0 = v[0], a1 = v[1], a2 = v[2], a3 = v[3];
+ if (c)
+ {
+ out[0] = a0 + 1;
+ out[1] = a1 + 2;
+ out[2] = a2 + 3;
+ out[3] = a3 + 4;
+ }
+}
+
+/* { dg-final { scan-tree-dump-times "placing node \[^\n\r\]* before" 1 "slp2"
{ target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */
+/* { dg-final { scan-tree-dump {vect_[^\n\r]* = [^\n\r]* \+ \{ 1, 2, 3, 4
\};\n *_[0-9]+ = a0_[0-9]+ \+ 1;} "slp2" { target { { { aarch64*-*-* ||
i?86-*-* } || x86_64-*-* } && lp64 } } } } */
+/* { dg-final { scan-tree-dump "optimized: basic block part vectorized" "slp2"
{ target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */
diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-20.c
b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-20.c
new file mode 100644
index 00000000000..6c50ad64bea
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-20.c
@@ -0,0 +1,29 @@
+/* PR tree-optimization/127246 */
+/* { dg-do compile } */
+
+/* v is defined in the block of rt (), which starts no region, so
+ pre-scheduling does not see its definition. The vector code of the
+ additions and multiplications must not go after the place that
+ pre-scheduling computed for it. */
+
+typedef int v4si __attribute__ ((vector_size (16)));
+extern int rt (void) __attribute__ ((returns_twice, leaf));
+extern v4si getv (void) __attribute__ ((leaf));
+extern int foo (void);
+
+void
+f (v4si *r, int *restrict p, int c, int *s)
+{
+ int t = foo ();
+ rt ();
+ v4si v = getv ();
+ if (c)
+ {
+ int a0 = p[0], a1 = p[1], a2 = p[2], a3 = p[3];
+ *r = (v4si) { (v[0] + 1) * a0, (v[1] + 2) * a1, (v[2] + 3) * a2,
+ (v[3] + 4) * a3 };
+ }
+ *s = t;
+}
+
+/* { dg-final { scan-tree-dump "optimized: basic block part vectorized" "slp2"
{ target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */
diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-3.c
b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-3.c
new file mode 100644
index 00000000000..eac4c60ac3c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-3.c
@@ -0,0 +1,50 @@
+/* PR tree-optimization/127246 */
+/* { dg-do compile } */
+/* { dg-additional-options "-fvect-cost-model=dynamic" } */
+/* { dg-additional-options "-mtune=generic" { target aarch64*-*-* i?86-*-*
x86_64-*-* } } */
+
+/* The additions only run in the unlikely arm, so their vector code is placed
+ there, right before the first addition. The region does not define x and
+ y, so their vector stays at the region entry. */
+
+void
+f (unsigned short *out, unsigned short x, unsigned short y, int c)
+{
+ if (__builtin_expect (c, 0))
+ {
+ out[0] = x + 3;
+ out[1] = y + 5;
+ out[2] = x + 7;
+ out[3] = y + 9;
+ out[4] = x + 11;
+ out[5] = y + 13;
+ out[6] = x + 15;
+ out[7] = y + 17;
+ }
+}
+
+/* The vector load of q is also used before the branch. The addition only
+ runs in the arm, so its vector code is placed there. */
+
+void
+g (int *restrict p, int *restrict q, int *restrict r)
+{
+ int a = q[0], b = q[1], d = q[2], e = q[3];
+ r[0] = a;
+ r[1] = b;
+ r[2] = d;
+ r[3] = e;
+ if (a > 0)
+ {
+ p[0] = a + 1;
+ p[1] = b + 2;
+ p[2] = d + 3;
+ p[3] = e + 4;
+ }
+}
+
+/* { dg-final { scan-tree-dump "optimized: basic block part vectorized" "slp2"
{ target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */
+/* { dg-final { scan-tree-dump-times "placing node \[^\n\r\]* before" 2 "slp2"
{ target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */
+/* { dg-final { scan-tree-dump {:\n *vect_[^\n\r]* = _[0-9]+ \+ \{ 3, 5, 7, 9,
11, 13, 15, 17 \};} "slp2" { target { { { aarch64*-*-* || i?86-*-* } ||
x86_64-*-* } && lp64 } } } } */
+/* { dg-final { scan-tree-dump {:\n *vect_[^\n\r]* = vect_[^\n\r]* \+ \{ 1, 2,
3, 4 \};} "slp2" { target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } &&
lp64 } } } } */
+/* { dg-final { scan-tree-dump-not "placing node \[^\n\r\]* with node" "slp2"
} } */
diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-4.c
b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-4.c
new file mode 100644
index 00000000000..77d55990578
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-4.c
@@ -0,0 +1,74 @@
+/* PR tree-optimization/127246 */
+/* { dg-do compile } */
+
+/* The quotients feed vector stores, and in f, f1 and f2 also a reduction.
+ Without a vector division the store instances turn the quotients into
+ an external, and the reduction instance keeps that external as its
+ root. In f3 the store instances of both arms share the external, in f4
+ a store before the branch and one in its arm. The vector of the
+ external must dominate all of its users, also when they are in
+ different blocks. */
+
+void
+f (long *restrict p, long *restrict q, long *s)
+{
+ long a = q[0] / q[2], b = q[1] / q[3];
+ p[0] = a;
+ p[1] = b;
+ *s = a + b;
+}
+
+void
+f1 (long *restrict p, long *restrict q, long *s, int c)
+{
+ long a = q[0] / q[2], b = q[1] / q[3];
+ if (c)
+ {
+ p[0] = a;
+ p[1] = b;
+ }
+ *s = a + b;
+}
+
+void
+f2 (long *restrict p, long *restrict q, long *s, int c)
+{
+ long a = q[0] / q[2], b = q[1] / q[3];
+ if (c)
+ *s = a + b;
+ p[0] = a;
+ p[1] = b;
+}
+
+void
+f3 (long *restrict p, long *restrict q, long *restrict r, int c)
+{
+ long a = q[0] / q[2], b = q[1] / q[3];
+ if (c)
+ {
+ p[0] = a;
+ p[1] = b;
+ }
+ else
+ {
+ r[0] = a;
+ r[1] = b;
+ }
+}
+
+void
+f4 (long *restrict p, long *restrict q, long *restrict r, int c)
+{
+ long a = q[0] / q[2], b = q[1] / q[3];
+ p[0] = a;
+ p[1] = b;
+ if (c)
+ {
+ r[0] = a;
+ r[1] = b;
+ }
+}
+
+/* { dg-final { scan-tree-dump-times "from scalars instead" 5 "slp2" { target
{ { { aarch64*-*-* && { ! aarch64_sve } } || { i?86-*-* || x86_64-*-* } } &&
lp64 } } } } */
+/* { dg-final { scan-tree-dump-times "optimized: basic block part vectorized"
10 "slp2" { target { { { aarch64*-*-* && { ! aarch64_sve } } || { i?86-*-* ||
x86_64-*-* } } && lp64 } } } } */
+/* { dg-final { scan-tree-dump-not "cannot schedule" "slp2" } } */
diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-5.c
b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-5.c
new file mode 100644
index 00000000000..de2b001a2f5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-5.c
@@ -0,0 +1,33 @@
+/* PR tree-optimization/127246 */
+/* { dg-do compile } */
+/* { dg-additional-options "-fvect-cost-model=dynamic" } */
+/* { dg-additional-options "-mtune=generic" { target aarch64*-*-* i?86-*-*
x86_64-*-* } } */
+
+/* The additions only run in the conditional block, so their vector code
+ and the vector of x and y go there and not after the calls in the loop
+ header. */
+
+extern unsigned short foo (unsigned short);
+
+void
+f (unsigned short *restrict out, unsigned short *a, int *c, int n)
+{
+ for (int i = 0; i < n; i++)
+ {
+ unsigned short x = foo (a[i]), y = foo (a[i + 1]);
+ if (c[i] == 42)
+ {
+ out[8 * i + 0] = x + 3;
+ out[8 * i + 1] = y + 5;
+ out[8 * i + 2] = x + 7;
+ out[8 * i + 3] = y + 9;
+ out[8 * i + 4] = x + 11;
+ out[8 * i + 5] = y + 13;
+ out[8 * i + 6] = x + 15;
+ out[8 * i + 7] = y + 17;
+ }
+ }
+}
+
+/* { dg-final { scan-tree-dump "placing node \[^\n\r\]* before" "slp1" {
target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */
+/* { dg-final { scan-tree-dump "placing node \[^\n\r\]* with node" "slp1" {
target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */
diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-6.c
b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-6.c
new file mode 100644
index 00000000000..93e371fff9f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-6.c
@@ -0,0 +1,54 @@
+/* PR tree-optimization/127246 */
+/* { dg-do compile } */
+/* { dg-additional-options "-fvect-cost-model=dynamic" } */
+/* { dg-additional-options "-mtune=generic" { target aarch64*-*-* i?86-*-*
x86_64-*-* } } */
+
+/* x and y are defined before the loop and stored only in its conditional
+ block. Their vector stays after their definitions, not in the loop. */
+
+extern unsigned short foo (unsigned short);
+
+void
+f (unsigned short *restrict out, unsigned short a, unsigned short b,
+ int *c, int n)
+{
+ unsigned short x = foo (a), y = foo (b);
+ for (int i = 0; i < n; i++)
+ if (c[i] == 42)
+ {
+ out[8 * i + 0] = x;
+ out[8 * i + 1] = y;
+ out[8 * i + 2] = x;
+ out[8 * i + 3] = y;
+ out[8 * i + 4] = x;
+ out[8 * i + 5] = y;
+ out[8 * i + 6] = x;
+ out[8 * i + 7] = y;
+ }
+}
+
+/* The additions use only x and y, so their vector code also stays before
+ the loop. */
+
+void
+g (unsigned short *restrict out, unsigned short a, unsigned short b,
+ int *c, int n)
+{
+ unsigned short x = foo (a), y = foo (b);
+ for (int i = 0; i < n; i++)
+ if (c[i] == 42)
+ {
+ out[8 * i + 0] = x + 3;
+ out[8 * i + 1] = y + 5;
+ out[8 * i + 2] = x + 7;
+ out[8 * i + 3] = y + 9;
+ out[8 * i + 4] = x + 11;
+ out[8 * i + 5] = y + 13;
+ out[8 * i + 6] = x + 15;
+ out[8 * i + 7] = y + 17;
+ }
+}
+
+/* { dg-final { scan-tree-dump-times "optimized: basic block part vectorized"
2 "slp1" { target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } }
} } */
+/* { dg-final { scan-tree-dump-not "placing node \[^\n\r\]* before" "slp1" } }
*/
+/* { dg-final { scan-tree-dump-not "placing node \[^\n\r\]* with node" "slp1"
} } */
diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-8.c
b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-8.c
new file mode 100644
index 00000000000..1d3fc1a6997
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-8.c
@@ -0,0 +1,88 @@
+/* PR tree-optimization/127246 */
+/* { dg-additional-options "-fvect-cost-model=dynamic" } */
+/* { dg-additional-options "-mtune=generic" { target aarch64*-*-* i?86-*-*
x86_64-*-* } } */
+
+/* The vector code of the additions is placed in the conditional block.
+ The first lane is also used after it. */
+
+#include "tree-vect.h"
+
+__attribute__ ((noipa)) unsigned short
+f (unsigned short *out, unsigned short x, unsigned short y, int c)
+{
+ unsigned short r = 0;
+ if (c)
+ {
+ unsigned short a = x + 3;
+ out[0] = a;
+ out[1] = y + 5;
+ out[2] = x + 7;
+ out[3] = y + 9;
+ out[4] = x + 11;
+ out[5] = y + 13;
+ out[6] = x + 15;
+ out[7] = y + 17;
+ r = a;
+ }
+ return r;
+}
+
+/* The vector additions and multiplications go into their arms. The
+ region does not define x and y, so their vectors stay at the region
+ entry. */
+
+__attribute__ ((noipa)) void
+g (unsigned short *out, unsigned short x, unsigned short y, int c)
+{
+ if (c)
+ {
+ out[0] = x + 3;
+ out[1] = y + 5;
+ out[2] = x + 7;
+ out[3] = y + 9;
+ out[4] = x + 11;
+ out[5] = y + 13;
+ out[6] = x + 15;
+ out[7] = y + 17;
+ }
+ else
+ {
+ out[0] = x * 3;
+ out[1] = y * 5;
+ out[2] = x * 7;
+ out[3] = y * 9;
+ out[4] = x * 11;
+ out[5] = y * 13;
+ out[6] = x * 15;
+ out[7] = y * 17;
+ }
+}
+
+int
+main ()
+{
+ unsigned short out[8];
+ static const unsigned short add[8] = { 3, 5, 7, 9, 11, 13, 15, 17 };
+
+ check_vect ();
+
+ for (int c = 0; c < 2; c++)
+ {
+ __builtin_memset (out, 0, sizeof (out));
+ unsigned short r = f (out, 10, 20, c);
+ if (r != (c ? 13 : 0))
+ abort ();
+ for (int i = 0; i < 8; i++)
+ if (out[i] != (c ? (i & 1 ? 20 : 10) + add[i] : 0))
+ abort ();
+
+ g (out, 10, 20, c);
+ for (int i = 0; i < 8; i++)
+ if (out[i] != (c ? (i & 1 ? 20 : 10) + add[i]
+ : (unsigned short) ((i & 1 ? 20 : 10) * add[i])))
+ abort ();
+ }
+ return 0;
+}
+
+/* { dg-final { scan-tree-dump-times "placing node \[^\n\r\]* before" 3 "slp2"
{ target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */
diff --git a/gcc/tree-vect-slp.cc b/gcc/tree-vect-slp.cc
index ee05e1767cc..6fb0b185c18 100644
--- a/gcc/tree-vect-slp.cc
+++ b/gcc/tree-vect-slp.cc
@@ -11049,12 +11049,46 @@ duplicate_and_interleave (vec_info *vinfo, gimple_seq
*seq, tree vector_type,
}
+/* Return true if BB vectorization may place vector code whose operands are
+ available in block FROM in block BB instead. BB has to be strictly
+ dominated by FROM, in the same loop and outside of an irreducible
+ region, so that the code does not run more often there. */
+
+static bool
+vect_bb_slp_sink_p (basic_block from, basic_block bb)
+{
+ return (bb != from
+ && bb->loop_father == from->loop_father
+ && !(bb->flags & BB_IRREDUCIBLE_LOOP)
+ && dominated_by_p (CDI_DOMINATORS, bb, from));
+}
+
+/* Return true if pre-scheduling placed the external SLP node NODE with its
+ only user, see vect_schedule_slp_node. NODE->si is then the si of that
+ user instead of the definition of an operand of NODE. */
+
+static bool
+vect_slp_external_at_user_p (slp_tree node)
+{
+ if (SLP_TREE_DEF_TYPE (node) != vect_external_def
+ || !node->si
+ || SLP_TREE_SCALAR_OPS (node).is_empty ())
+ return false;
+ for (tree op : SLP_TREE_SCALAR_OPS (node))
+ if (TREE_CODE (op) == SSA_NAME && SSA_NAME_DEF_STMT (op) == node->si)
+ return false;
+ return true;
+}
+
/* For constant and loop invariant defs in OP_NODE this function creates
vector defs that will be used in the vectorized stmts and stores them
- to SLP_TREE_VEC_DEFS of OP_NODE. */
+ to SLP_TREE_VEC_DEFS of OP_NODE. If USER_GSI is not NULL, OP_NODE is
+ placed with its only user and USER_GSI is the place of the vector code
+ of that user. */
static void
-vect_create_constant_vectors (vec_info *vinfo, slp_tree op_node)
+vect_create_constant_vectors (vec_info *vinfo, slp_tree op_node,
+ gimple_stmt_iterator *user_gsi = NULL)
{
unsigned HOST_WIDE_INT nunits;
tree vec_cst;
@@ -11211,7 +11245,15 @@ vect_create_constant_vectors (vec_info *vinfo,
slp_tree op_node)
}
if (!gimple_seq_empty_p (ctor_seq))
{
- if (insert_after)
+ /* Build the vector right before the vector code of the user
+ unless its operands are available in another loop. */
+ if (user_gsi
+ && vect_bb_slp_sink_p (insert_after
+ ? gimple_bb (insert_after->stmt)
+ : vinfo->bbs[0],
+ gsi_bb (*user_gsi)))
+ gsi_insert_seq_before (user_gsi, ctor_seq, GSI_SAME_STMT);
+ else if (insert_after)
{
gimple_stmt_iterator gsi;
if (gimple_code (insert_after->stmt) == GIMPLE_PHI)
@@ -12197,8 +12239,10 @@ vect_schedule_slp_node (vec_info *vinfo,
is that we are vectorizing an existing vector def. The second is
when performing BB vectorization shared constant/external nodes
are not split apart during partitioning so during the code-gen
- DFS walk we can end up visiting them twice. */
- if (! SLP_TREE_VEC_DEFS (node).exists ())
+ DFS walk we can end up visiting them twice. The user of NODE
+ builds it when pre-scheduling placed it there. */
+ if (! SLP_TREE_VEC_DEFS (node).exists ()
+ && !vect_slp_external_at_user_p (node))
vect_create_constant_vectors (vinfo, node);
return true;
}
@@ -12209,7 +12253,8 @@ vect_schedule_slp_node (vec_info *vinfo,
if (!place_only && SLP_TREE_VECTYPE (node))
SLP_TREE_VEC_DEFS (node).create (vect_get_num_copies (vinfo, node));
- gimple *last_stmt;
+ gimple *last_stmt, *sink = NULL;
+ stmt_vec_info first;
gimple_stmt_iterator si;
/* ??? When !place_only we'd like to re-use place_only computed info,
but this is a bit awkward due to using gsi_insert_before and the
@@ -12332,9 +12377,11 @@ vect_schedule_slp_node (vec_info *vinfo,
gcc_unreachable ();
}
}
- else if (!SLP_TREE_VECTYPE (child))
+ else if (!SLP_TREE_VECTYPE (child)
+ || vect_slp_external_at_user_p (child))
{
- /* For externals we use unvectorized at all scalar defs. */
+ /* Look at all scalar defs of externals we use unvectorized and
+ of externals that NODE builds itself below. */
unsigned j;
tree def;
FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_OPS (child), j, def)
@@ -12421,6 +12468,23 @@ vect_schedule_slp_node (vec_info *vinfo,
si = gsi_after_labels (gimple_bb (stmt_info->stmt));
last_stmt = gsi_stmt (si);
}
+ /* Place other operations of BB vectorization right before their
+ first scalar stmt when vect_bb_slp_sink_p allows it, so that the
+ vector code runs where the scalar code it replaces runs.
+ Pre-scheduling does not see vector defs before the region, so the
+ transform does this only for the operations that pre-scheduling
+ placed there. */
+ else if (is_a <bb_vec_info> (vinfo)
+ && !SLP_TREE_PERMUTE_P (node)
+ && (first = vect_find_first_scalar_stmt_in_slp (node))
+ && (place_only || node->si == first->stmt)
+ && vect_bb_slp_sink_p (last_stmt
+ ? gimple_bb (last_stmt) : vinfo->bbs[0],
+ gimple_bb (first->stmt)))
+ {
+ last_stmt = sink = first->stmt;
+ si = gsi_for_stmt (sink);
+ }
/* When there is no in-region child def to guide placement, insert
at region boundary. */
else if (!last_stmt)
@@ -12478,11 +12542,42 @@ vect_schedule_slp_node (vec_info *vinfo,
{
if (dump_enabled_p () && last_stmt)
dump_printf_loc (MSG_NOTE, vect_location,
- "placing node %p at %G:", (void *)node, last_stmt);
+ "placing node %p %s %G:", (void *)node,
+ sink ? "before" : "at", last_stmt);
/* Verify we either get a stmt anchor or region start. */
gcc_assert ((last_stmt && gimple_bb (last_stmt))
|| (!last_stmt && gsi_bb (si)));
node->si = last_stmt;
+
+ /* When vect_bb_slp_sink_p allows it, place with NODE each external
+ that only NODE uses and that has operands defined in the region.
+ NODE then builds it right before its own vector code. PHIs use
+ their operands on the incoming edges. Do not extend the live
+ ranges of SSA names that occur in abnormal PHIs. */
+ if (!stmt_info || !is_a <gphi *> (stmt_info->stmt))
+ FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
+ if (SLP_TREE_DEF_TYPE (child) == vect_external_def
+ && child->si
+ && SLP_TREE_REF_COUNT (child) == 1
+ && SLP_TREE_VECTYPE (child)
+ && !SLP_TREE_SCALAR_OPS (child).is_empty ()
+ && vect_bb_slp_sink_p (gimple_bb (child->si),
+ gimple_bb (last_stmt)))
+ {
+ bool abnormal = false;
+ for (tree op : SLP_TREE_SCALAR_OPS (child))
+ if (TREE_CODE (op) == SSA_NAME
+ && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
+ abnormal = true;
+ if (abnormal)
+ continue;
+ if (dump_enabled_p ())
+ dump_printf_loc (MSG_NOTE, vect_location,
+ "placing node %p with node %p\n",
+ (void *) child, (void *) node);
+ child->si = last_stmt;
+ gcc_checking_assert (vect_slp_external_at_user_p (child));
+ }
return true;
}
@@ -12527,6 +12622,15 @@ vect_schedule_slp_node (vec_info *vinfo,
vect_print_slp_tree (MSG_NOTE, vect_location, node);
}
}
+ /* Build the externals that pre-scheduling placed with NODE right before
+ the vector code of NODE. */
+ FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
+ if (child && vect_slp_external_at_user_p (child))
+ {
+ gcc_checking_assert (child->si == node->si
+ && !SLP_TREE_VEC_DEFS (child).exists ());
+ vect_create_constant_vectors (vinfo, child, &si);
+ }
vect_transform_stmt (vinfo, stmt_info, &si, node, instance);
return true;
}
diff --git a/gcc/tree-vectorizer.h b/gcc/tree-vectorizer.h
index 750b46a97b9..19b8cfafed0 100644
--- a/gcc/tree-vectorizer.h
+++ b/gcc/tree-vectorizer.h
@@ -374,8 +374,14 @@ struct _slp_tree {
tree vectype;
/* Vectorized defs. */
vec<tree> vec_defs;
- /* Insertion place for verification purposes. Only set for
- BB vectorization. NULL denotes region entry. */
+ /* Insertion place computed by pre-scheduling. Only set for BB
+ vectorization. NULL denotes region entry. The vector code of a
+ load, a store and a possibly trapping operation placed at the start
+ of its block goes right before si. The vector code of an operation
+ placed right before its first scalar stmt goes there or earlier.
+ Other vector code goes right after si or earlier. An external that
+ only one node uses can have the si of that node, which then builds
+ it. */
gimple *si;
/* Reference count in the SLP graph. */
--
2.54.0 (Apple Git-157)