With APX most general registers are call-clobbered, so keep a loop
region with one entry and one exit edge when its calls are more
frequent than the moves on its border.
gcc/ChangeLog:
* config/i386/i386.cc (ix86_ira_loop_region_flatten_cost): New
function.
(TARGET_IRA_LOOP_REGION_FLATTEN_COST): Define.
gcc/testsuite/ChangeLog:
* gcc.target/i386/ira-loop-region-flatten-1.c: New test.
* gcc.target/i386/ira-loop-region-flatten-2.c: New test.
* gcc.target/i386/ira-loop-region-flatten-3.c: New test.
* gcc.target/i386/ira-loop-region-flatten-4.c: New test.
* gcc.target/i386/ira-loop-region-flatten-5.c: New test.
---
gcc/config/i386/i386.cc | 20 ++++
.../i386/ira-loop-region-flatten-1.c | 94 +++++++++++++++++++
.../i386/ira-loop-region-flatten-2.c | 29 ++++++
.../i386/ira-loop-region-flatten-3.c | 6 ++
.../i386/ira-loop-region-flatten-4.c | 26 +++++
.../i386/ira-loop-region-flatten-5.c | 25 +++++
6 files changed, 200 insertions(+)
create mode 100644 gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-1.c
create mode 100644 gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-2.c
create mode 100644 gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-3.c
create mode 100644 gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-4.c
create mode 100644 gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-5.c
diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc
index d1e16e6ecaf..ee2377c2dda 100644
--- a/gcc/config/i386/i386.cc
+++ b/gcc/config/i386/i386.cc
@@ -21634,6 +21634,23 @@ ix86_callee_save_cost (spill_cost_type, unsigned int
hard_regno, machine_mode,
return mem_cost;
}
+/* Implement TARGET_IRA_LOOP_REGION_FLATTEN_COST. With APX, 25 of the 31
+ allocatable general registers are call-clobbered, so keep a loop with
+ one entry and one exit edge when its calls are more frequent than the
+ moves on its border. */
+
+static int
+ix86_ira_loop_region_flatten_cost (int call_freq, int border_freq,
+ int border_edges)
+{
+ if (!TARGET_APX_EGPR || !optimize_function_for_speed_p (cfun))
+ return 0;
+ /* Each further border edge needs its own moves. */
+ if (border_edges > 2)
+ return 0;
+ return call_freq - border_freq;
+}
+
/* Return true if a set of DST by the expression SRC should be allowed.
This prevents complex sets of likely_spilled hard regs before split1. */
@@ -29546,6 +29563,9 @@ ix86_libgcc_floating_mode_supported_p
#undef TARGET_CALLEE_SAVE_COST
#define TARGET_CALLEE_SAVE_COST ix86_callee_save_cost
+#undef TARGET_IRA_LOOP_REGION_FLATTEN_COST
+#define TARGET_IRA_LOOP_REGION_FLATTEN_COST ix86_ira_loop_region_flatten_cost
+
#undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
#define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
ix86_builtin_vectorization_cost
diff --git a/gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-1.c
b/gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-1.c
new file mode 100644
index 00000000000..9370eaf6f8b
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-1.c
@@ -0,0 +1,94 @@
+/* { dg-do compile { target { ! ia32 } } } */
+/* { dg-options "-O2 -mabi=sysv -mapxf -fdump-rtl-ira" } */
+
+/* The pseudos of the outer walk live through the two list walks without
+ a reference, so the list walks stay regions and the list pointer gets
+ a call-saved register. */
+
+typedef double fp_t;
+typedef struct { void (*process)(const void *, void *); } vtable;
+typedef struct { unsigned int id; void *next; vtable *vt; } item;
+typedef struct item_list { struct item_list *next; item *obj; } item_list;
+
+typedef struct {
+ int dim_x, dim_y;
+ fp_t origin_x, origin_y, step_x, step_y;
+ item_list **buckets;
+} spatial_grid;
+
+typedef struct {
+ fp_t pos_x, pos_y, dir_x, dir_y, limit;
+ unsigned int flags;
+ unsigned long seq;
+ unsigned long *seen;
+} query;
+
+extern int check_bounds(const spatial_grid *, const query *, fp_t *, fp_t *);
+
+void traverse_grid(const spatial_grid *grid, query *q) {
+ fp_t tnear, tfar, tmax_x, tmax_y, tdelta_x, tdelta_y, cx, cy;
+ int cur_x, cur_y, inc_x, inc_y, end_x, end_y;
+ long idx, stride_y;
+ unsigned long seq = q->seq;
+ unsigned long *seen = q->seen;
+ item_list *cur;
+
+ if (!check_bounds(grid, q, &tnear, &tfar))
+ return;
+
+ cx = q->pos_x + q->dir_x * tnear;
+ cy = q->pos_y + q->dir_y * tnear;
+ cur_x = (int)((cx - grid->origin_x) / grid->step_x);
+ cur_y = (int)((cy - grid->origin_y) / grid->step_y);
+
+ tmax_x = tnear + ((cur_x * grid->step_x + grid->origin_x) - cx) / q->dir_x;
+ tdelta_x = grid->step_x / q->dir_x;
+ inc_x = (q->dir_x > 0) ? 1 : -1;
+ end_x = (q->dir_x > 0) ? grid->dim_x : -1;
+
+ tmax_y = tnear + ((cur_y * grid->step_y + grid->origin_y) - cy) / q->dir_y;
+ tdelta_y = grid->step_y / q->dir_y;
+ inc_y = (q->dir_y > 0) ? 1 : -1;
+ end_y = (q->dir_y > 0) ? grid->dim_y : -1;
+
+ stride_y = inc_y * (long)grid->dim_x;
+ idx = cur_y * (long)grid->dim_x + cur_x;
+
+ cur = grid->buckets[idx];
+ while (cur != 0) {
+ if (seen[cur->obj->id] != seq) {
+ seen[cur->obj->id] = seq;
+ cur->obj->vt->process(cur->obj, q);
+ }
+ cur = cur->next;
+ }
+
+ for (;;) {
+ if (tmax_x < tmax_y) {
+ cur_x += inc_x;
+ if (q->limit < tmax_x || cur_x == end_x) break;
+ tmax_x += tdelta_x;
+ idx += inc_x;
+ } else {
+ cur_y += inc_y;
+ if (q->limit < tmax_y || cur_y == end_y) break;
+ tmax_y += tdelta_y;
+ idx += stride_y;
+ }
+
+ cur = grid->buckets[idx];
+ while (cur != 0) {
+ if (seen[cur->obj->id] != seq) {
+ seen[cur->obj->id] = seq;
+ cur->obj->vt->process(cur->obj, q);
+ }
+ cur = cur->next;
+ }
+ }
+}
+
+/* No caller-saved spills around indirect calls in the loop. */
+
+/* { dg-final { scan-rtl-dump {Loop 1 flatten-cost:[^\n]*keep} "ira" } } */
+/* { dg-final { scan-rtl-dump {Loop 3 flatten-cost:[^\n]*keep} "ira" } } */
+/* { dg-final { scan-assembler-not {call\t\*[^\n]*\n\tmovq\t[0-9]+\(%rsp\),
%r} } } */
diff --git a/gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-2.c
b/gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-2.c
new file mode 100644
index 00000000000..bd16cd327e6
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-2.c
@@ -0,0 +1,29 @@
+/* { dg-do compile { target { ! ia32 } } } */
+/* { dg-options "-O2 -mabi=sysv -mapxf -fdump-rtl-ira" } */
+
+typedef float v4sf __attribute__ ((vector_size (16)));
+extern void use (float);
+
+v4sf
+vec_cross (v4sf *a, int n)
+{
+ v4sf f0 = a[0], f1 = a[1], f2 = a[2], f3 = a[3];
+ v4sf f4 = a[4], f5 = a[5], f6 = a[6], f7 = a[7];
+ for (int k = 0; k < n; k++)
+ {
+ use (a[k][0]);
+ f0 += f1;
+ f3 += f4;
+ f6 += f7;
+ use (a[k][1]);
+ f1 += f2;
+ f4 += f5;
+ f7 += f0;
+ use (a[k][2]);
+ f2 += f3;
+ f5 += f6;
+ }
+ return f0 + f1 + f2 + f3 + f4 + f5 + f6 + f7;
+}
+
+/* { dg-final { scan-rtl-dump-not {flatten-cost:[^\n]*keep} "ira" } } */
diff --git a/gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-3.c
b/gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-3.c
new file mode 100644
index 00000000000..a4d8457738a
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-3.c
@@ -0,0 +1,6 @@
+/* { dg-do compile { target { ! ia32 } } } */
+/* { dg-options "-O2 -mabi=sysv -mno-apxf -fdump-rtl-ira" } */
+
+#include "ira-loop-region-flatten-1.c"
+
+/* { dg-final { scan-rtl-dump-not "flatten-cost" "ira" } } */
diff --git a/gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-4.c
b/gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-4.c
new file mode 100644
index 00000000000..e2e7fbf8df7
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-4.c
@@ -0,0 +1,26 @@
+/* { dg-do compile { target { ! ia32 } } } */
+/* { dg-options "-O2 -mabi=sysv -march=diamondrapids -mapxf -fdump-rtl-ira" }
*/
+
+/* All pseudos living through the calls are referenced in the loop, so
+ a region cannot give them call-saved registers. */
+
+extern double parse_field (const char *line, int i);
+extern void refill (char *line);
+
+void
+load_coords (int natom, double *x, double *v, double *w, char *line)
+{
+ int n = 3 * natom;
+ for (int i = 0; i < n; i++)
+ {
+ double a = parse_field (line, i);
+ x[i] = a;
+ v[i] = a * 2.0;
+ w[i] = a + x[i > 0 ? i - 1 : 0];
+ if (i % 6 == 5 && i < n - 1)
+ refill (line);
+ }
+}
+
+/* { dg-final { scan-rtl-dump {Loop 2 flatten-cost: excess not covered} "ira"
} } */
+/* { dg-final { scan-rtl-dump-not {flatten-cost:[^\n]*keep} "ira" } } */
diff --git a/gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-5.c
b/gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-5.c
new file mode 100644
index 00000000000..27dc22a2ca8
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-5.c
@@ -0,0 +1,25 @@
+/* { dg-do compile { target { ! ia32 } } } */
+/* { dg-options "-O2 -mabi=sysv -march=diamondrapids -mapxf -fdump-rtl-ira" }
*/
+
+/* The call is on a cold path, so the moves on the loop border cost more
+ than the saves around it. */
+
+extern void slow (long *) __attribute__((cold));
+
+long
+scan (long *p, long n, long *q)
+{
+ long a = q[0], b = q[1], c = q[2], d = q[3];
+ long e = q[4], f = q[5], g = q[6], h = q[7];
+ long s = 0;
+ for (long i = 0; i < n; i++)
+ {
+ s += p[i];
+ if (__builtin_expect (p[i] < 0, 0))
+ slow (p);
+ }
+ return s + a + b + c + d + e + f + g + h;
+}
+
+/* { dg-final { scan-rtl-dump {Loop 1 flatten-cost:[^\n]*bias=-[0-9]+ \(allow
flatten\)} "ira" } } */
+/* { dg-final { scan-rtl-dump-not {flatten-cost:[^\n]*keep} "ira" } } */
--
2.31.1