https://gcc.gnu.org/g:b8643036af048b8985dd69d294538e496d975af7
commit r17-6622-gb8643036af048b8985dd69d294538e496d975af7 Author: Hongyu Wang <[email protected]> Date: Tue Nov 10 14:49:29 2026 +0530 i386: Implement TARGET_IRA_LOOP_REGION_FLATTEN_COST for APX 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: Likewise. * gcc.target/i386/ira-loop-region-flatten-3.c: Likewise. * gcc.target/i386/ira-loop-region-flatten-4.c: Likewise. * gcc.target/i386/ira-loop-region-flatten-5.c: Likewise. * gcc.target/i386/ira-loop-region-flatten-6.c: Likewise. Diff: --- gcc/config/i386/i386.cc | 20 +++++ .../gcc.target/i386/ira-loop-region-flatten-1.c | 94 ++++++++++++++++++++++ .../gcc.target/i386/ira-loop-region-flatten-2.c | 29 +++++++ .../gcc.target/i386/ira-loop-region-flatten-3.c | 6 ++ .../gcc.target/i386/ira-loop-region-flatten-4.c | 26 ++++++ .../gcc.target/i386/ira-loop-region-flatten-5.c | 25 ++++++ .../gcc.target/i386/ira-loop-region-flatten-6.c | 24 ++++++ 7 files changed, 224 insertions(+) diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc index cd4ea96c536c..57bd673cfce3 100644 --- a/gcc/config/i386/i386.cc +++ b/gcc/config/i386/i386.cc @@ -21656,6 +21656,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. */ @@ -29535,6 +29552,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 000000000000..9370eaf6f8b5 --- /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 000000000000..bd16cd327e6e --- /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 000000000000..a4d8457738a4 --- /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 000000000000..e2e7fbf8df78 --- /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 000000000000..27dc22a2ca86 --- /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" } } */ diff --git a/gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-6.c b/gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-6.c new file mode 100644 index 000000000000..3b1fd59968e9 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-6.c @@ -0,0 +1,24 @@ +/* { dg-do compile { target { ! ia32 } } } */ +/* { dg-options "-O2 -mabi=sysv -mapxf -fdump-rtl-ira" } */ + +/* B0-B2 are referenced only in the inner loop, which stays a region, so + they live through the outer loop without a reference in it. */ + +extern void f (long, long, long, long); +extern void g (long, long, long, long); + +void +foo (long *a, long *b, long n, long m) +{ + long a0 = a[0], a1 = a[1], a2 = a[2]; + long b0 = b[0], b1 = b[1], b2 = b[2]; + for (long i = 0; i < n; i++) + { + f (i, a0, a1, a2); + for (long j = 0; j < m; j++) + g (j, b0, b1, b2); + } +} + +/* { dg-final { scan-rtl-dump {Loop 2 flatten-cost:[^\n]*keep} "ira" } } */ +/* { dg-final { scan-rtl-dump {Loop 1 flatten-cost:[^\n]*keep} "ira" } } */
