tree-ssa-loop-prefetch.c [plain text]
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "rtl.h"
#include "tm_p.h"
#include "hard-reg-set.h"
#include "basic-block.h"
#include "output.h"
#include "diagnostic.h"
#include "tree-flow.h"
#include "tree-dump.h"
#include "timevar.h"
#include "cfgloop.h"
#include "varray.h"
#include "expr.h"
#include "tree-pass.h"
#include "ggc.h"
#include "insn-config.h"
#include "recog.h"
#include "hashtab.h"
#include "tree-chrec.h"
#include "tree-scalar-evolution.h"
#ifndef PREFETCH_LATENCY
#define PREFETCH_LATENCY 50
#endif
#ifndef SIMULTANEOUS_PREFETCHES
#define SIMULTANEOUS_PREFETCHES 3
#endif
#ifndef WRITE_CAN_USE_READ_PREFETCH
#define WRITE_CAN_USE_READ_PREFETCH 1
#endif
#ifndef READ_CAN_USE_WRITE_PREFETCH
#define READ_CAN_USE_WRITE_PREFETCH 0
#endif
#ifndef PREFETCH_BLOCK
#define PREFETCH_BLOCK 32
#endif
#ifndef HAVE_FORWARD_PREFETCH
#define HAVE_FORWARD_PREFETCH 0
#endif
#ifndef HAVE_BACKWARD_PREFETCH
#define HAVE_BACKWARD_PREFETCH 0
#endif
#ifndef ACCEPTABLE_MISS_RATE
#define ACCEPTABLE_MISS_RATE 50
#endif
#ifndef HAVE_prefetch
#define HAVE_prefetch 0
#endif
struct mem_ref_group
{
tree base;
HOST_WIDE_INT step;
tree group_iv;
bool issue_prefetch_p;
struct mem_ref *refs;
struct mem_ref_group *next;
};
#define PREFETCH_ALL (~(unsigned HOST_WIDE_INT) 0)
struct mem_ref
{
HOST_WIDE_INT delta;
bool write_p;
struct mem_ref_group *group;
unsigned HOST_WIDE_INT prefetch_mod;
unsigned HOST_WIDE_INT prefetch_before;
bool issue_prefetch_p;
struct mem_ref *next;
};
static void
dump_mem_ref (FILE *file, struct mem_ref *ref)
{
fprintf (file, "Reference %p:\n", (void *) ref);
fprintf (file, " group %p (base ", (void *) ref->group);
print_generic_expr (file, ref->group->base, TDF_SLIM);
fprintf (file, ", step ");
fprintf (file, HOST_WIDE_INT_PRINT_DEC, ref->group->step);
fprintf (file, ")\n");
fprintf (dump_file, " delta ");
fprintf (file, HOST_WIDE_INT_PRINT_DEC, ref->delta);
fprintf (file, "\n");
fprintf (file, " %s\n", ref->write_p ? "write" : "read");
fprintf (file, "\n");
}
static struct mem_ref_group *
find_or_create_group (struct mem_ref_group **groups, tree base,
HOST_WIDE_INT step)
{
for (; *groups; groups = &(*groups)->next)
{
if ((*groups)->step == step
&& operand_equal_p ((*groups)->base, base, 0))
return *groups;
}
(*groups) = xcalloc (1, sizeof (struct mem_ref_group));
(*groups)->base = base;
(*groups)->step = step;
(*groups)->group_iv = NULL_TREE;
(*groups)->refs = NULL;
(*groups)->next = NULL;
return *groups;
}
static void
record_ref (struct mem_ref_group *group, HOST_WIDE_INT delta,
bool write_p)
{
struct mem_ref **aref;
for (aref = &group->refs; *aref; aref = &(*aref)->next)
{
if (!WRITE_CAN_USE_READ_PREFETCH
&& write_p
&& !(*aref)->write_p)
continue;
if (!READ_CAN_USE_WRITE_PREFETCH
&& !write_p
&& (*aref)->write_p)
continue;
if ((*aref)->delta == delta)
return;
}
(*aref) = xcalloc (1, sizeof (struct mem_ref));
(*aref)->delta = delta;
(*aref)->write_p = write_p;
(*aref)->prefetch_before = PREFETCH_ALL;
(*aref)->prefetch_mod = 1;
(*aref)->issue_prefetch_p = false;
(*aref)->group = group;
(*aref)->next = NULL;
if (dump_file && (dump_flags & TDF_DETAILS))
dump_mem_ref (dump_file, *aref);
}
static void
release_mem_refs (struct mem_ref_group *groups)
{
struct mem_ref_group *next_g;
struct mem_ref *ref, *next_r;
for (; groups; groups = next_g)
{
next_g = groups->next;
for (ref = groups->refs; ref; ref = next_r)
{
next_r = ref->next;
free (ref);
}
free (groups);
}
}
struct ar_data
{
struct loop *loop;
tree stmt;
HOST_WIDE_INT *step;
HOST_WIDE_INT *delta;
};
static bool
idx_analyze_ref (tree base, tree *index, void *data)
{
struct ar_data *ar_data = data;
tree ibase, step, stepsize;
HOST_WIDE_INT istep, idelta = 0, imult = 1;
if (!simple_iv (ar_data->loop, ar_data->stmt, *index, &ibase, &step))
return false;
if (zero_p (step))
istep = 0;
else
{
if (!cst_and_fits_in_hwi (step))
return false;
istep = int_cst_value (step);
}
if (TREE_CODE (ibase) == PLUS_EXPR
&& cst_and_fits_in_hwi (TREE_OPERAND (ibase, 1)))
{
idelta = int_cst_value (TREE_OPERAND (ibase, 1));
ibase = TREE_OPERAND (ibase, 0);
}
if (cst_and_fits_in_hwi (ibase))
{
idelta += int_cst_value (ibase);
ibase = fold_convert (TREE_TYPE (ibase), integer_zero_node);
}
if (base)
{
stepsize = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base)));
if (!cst_and_fits_in_hwi (stepsize))
return false;
imult = int_cst_value (stepsize);
istep *= imult;
idelta *= imult;
}
*ar_data->step += istep;
*ar_data->delta += idelta;
*index = ibase;
return true;
}
static bool
analyze_ref (struct loop *loop, tree ref, tree *base,
HOST_WIDE_INT *step, HOST_WIDE_INT *delta,
tree stmt)
{
struct ar_data ar_data;
tree off;
HOST_WIDE_INT bit_offset;
*step = 0;
*delta = 0;
if (TREE_CODE (ref) == COMPONENT_REF
&& DECL_NONADDRESSABLE_P (TREE_OPERAND (ref, 1)))
ref = TREE_OPERAND (ref, 0);
for (; TREE_CODE (ref) == COMPONENT_REF; ref = TREE_OPERAND (ref, 0))
{
off = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
bit_offset = TREE_INT_CST_LOW (off);
if (bit_offset % BITS_PER_UNIT)
abort ();
*delta += bit_offset / BITS_PER_UNIT;
}
*base = unshare_expr (ref);
ar_data.loop = loop;
ar_data.stmt = stmt;
ar_data.step = step;
ar_data.delta = delta;
return for_each_index (base, idx_analyze_ref, &ar_data);
}
static void
gather_memory_references_ref (struct loop *loop, struct mem_ref_group **refs,
tree ref, bool write_p, tree stmt)
{
tree base;
HOST_WIDE_INT step, delta;
struct mem_ref_group *agrp;
if (!analyze_ref (loop, ref, &base, &step, &delta, stmt))
return;
agrp = find_or_create_group (refs, base, step);
record_ref (agrp, delta, write_p);
}
static struct mem_ref_group *
gather_memory_references (struct loop *loop)
{
basic_block *body = get_loop_body_in_dom_order (loop);
basic_block bb;
unsigned i;
block_stmt_iterator bsi;
tree stmt, lhs, rhs;
struct mem_ref_group *refs = NULL;
for (i = 0; i < loop->num_nodes; i++)
{
bb = body[i];
if (bb->loop_father != loop)
continue;
for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
{
stmt = bsi_stmt (bsi);
if (TREE_CODE (stmt) != MODIFY_EXPR)
continue;
lhs = TREE_OPERAND (stmt, 0);
rhs = TREE_OPERAND (stmt, 1);
if (TREE_CODE_CLASS (TREE_CODE (rhs)) == 'r')
gather_memory_references_ref (loop, &refs, rhs, false, stmt);
if (TREE_CODE_CLASS (TREE_CODE (lhs)) == 'r')
gather_memory_references_ref (loop, &refs, lhs, true, stmt);
}
}
free (body);
return refs;
}
static void
prune_ref_by_self_reuse (struct mem_ref *ref)
{
HOST_WIDE_INT step = ref->group->step;
bool backward = step < 0;
if (step == 0)
{
ref->prefetch_before = 1;
return;
}
if (backward)
step = -step;
if (step > PREFETCH_BLOCK)
return;
if ((backward && HAVE_BACKWARD_PREFETCH)
|| (!backward && HAVE_FORWARD_PREFETCH))
{
ref->prefetch_before = 1;
return;
}
ref->prefetch_mod = PREFETCH_BLOCK / step;
}
static HOST_WIDE_INT
ddown (HOST_WIDE_INT x, unsigned HOST_WIDE_INT by)
{
if (by <= 0)
abort ();
if (x >= 0)
return x / by;
else
return (x + by - 1) / by;
}
static void
prune_ref_by_group_reuse (struct mem_ref *ref, struct mem_ref *by,
bool by_is_before)
{
HOST_WIDE_INT step = ref->group->step;
bool backward = step < 0;
HOST_WIDE_INT delta_r = ref->delta, delta_b = by->delta;
HOST_WIDE_INT delta = delta_b - delta_r;
HOST_WIDE_INT hit_from;
unsigned HOST_WIDE_INT prefetch_before, prefetch_block;
if (delta == 0)
{
if (by_is_before)
ref->prefetch_before = 0;
return;
}
if (!step)
{
if (!by_is_before)
return;
if (ddown (ref->delta, PREFETCH_BLOCK)
!= ddown (by->delta, PREFETCH_BLOCK))
return;
ref->prefetch_before = 0;
return;
}
if (backward)
{
if (delta > 0)
return;
delta = - delta;
step = -step;
delta_r = PREFETCH_BLOCK - 1 - delta_r;
delta_b = PREFETCH_BLOCK - 1 - delta_b;
}
else
{
if (delta < 0)
return;
}
if (step <= PREFETCH_BLOCK)
{
hit_from = ddown (delta_b, PREFETCH_BLOCK) * PREFETCH_BLOCK;
prefetch_before = (hit_from - delta_r + step - 1) / step;
if (prefetch_before < ref->prefetch_before)
ref->prefetch_before = prefetch_before;
return;
}
prefetch_block = PREFETCH_BLOCK;
while ((step & 1) == 0
&& prefetch_block > 1)
{
step >>= 1;
prefetch_block >>= 1;
delta >>= 1;
}
prefetch_before = delta / step;
delta %= step;
if ((unsigned HOST_WIDE_INT) delta
<= (prefetch_block * ACCEPTABLE_MISS_RATE / 1000))
{
if (prefetch_before < ref->prefetch_before)
ref->prefetch_before = prefetch_before;
return;
}
prefetch_before++;
delta = step - delta;
if ((unsigned HOST_WIDE_INT) delta
<= (prefetch_block * ACCEPTABLE_MISS_RATE / 1000))
{
if (prefetch_before < ref->prefetch_before)
ref->prefetch_before = prefetch_before;
return;
}
return;
}
static void
prune_ref_by_reuse (struct mem_ref *ref, struct mem_ref *refs)
{
struct mem_ref *prune_by;
bool before = true;
prune_ref_by_self_reuse (ref);
for (prune_by = refs; prune_by; prune_by = prune_by->next)
{
if (prune_by == ref)
{
before = false;
continue;
}
if (!WRITE_CAN_USE_READ_PREFETCH
&& ref->write_p
&& !prune_by->write_p)
continue;
if (!READ_CAN_USE_WRITE_PREFETCH
&& !ref->write_p
&& prune_by->write_p)
continue;
prune_ref_by_group_reuse (ref, prune_by, before);
}
}
static void
prune_group_by_reuse (struct mem_ref_group *group)
{
struct mem_ref *ref_pruned;
for (ref_pruned = group->refs; ref_pruned; ref_pruned = ref_pruned->next)
{
prune_ref_by_reuse (ref_pruned, group->refs);
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, "Reference %p:", (void *) ref_pruned);
if (ref_pruned->prefetch_before == PREFETCH_ALL
&& ref_pruned->prefetch_mod == 1)
fprintf (dump_file, " no restrictions");
else if (ref_pruned->prefetch_before == 0)
fprintf (dump_file, " do not prefetch");
else if (ref_pruned->prefetch_before <= ref_pruned->prefetch_mod)
fprintf (dump_file, " prefetch once");
else
{
if (ref_pruned->prefetch_before != PREFETCH_ALL)
{
fprintf (dump_file, " prefetch before ");
fprintf (dump_file, HOST_WIDE_INT_PRINT_DEC,
ref_pruned->prefetch_before);
}
if (ref_pruned->prefetch_mod != 1)
{
fprintf (dump_file, " prefetch mod ");
fprintf (dump_file, HOST_WIDE_INT_PRINT_DEC,
ref_pruned->prefetch_mod);
}
}
fprintf (dump_file, "\n");
}
}
}
static void
prune_by_reuse (struct mem_ref_group *groups)
{
for (; groups; groups = groups->next)
prune_group_by_reuse (groups);
}
static void
schedule_prefetches (struct mem_ref_group *groups, unsigned ahead)
{
unsigned max_prefetches = SIMULTANEOUS_PREFETCHES / ahead;
struct mem_ref *ref;
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "Max prefetches to issue: %d.\n", max_prefetches);
if (!max_prefetches)
return;
for (; groups; groups = groups->next)
for (ref = groups->refs; ref; ref = ref->next)
{
if (ref->prefetch_before != PREFETCH_ALL)
continue;
ref->issue_prefetch_p = true;
groups->issue_prefetch_p = true;
max_prefetches--;
if (!max_prefetches)
return;
}
}
static void
issue_prefetch_ref (struct loop *loop, struct mem_ref *ref, unsigned ahead)
{
HOST_WIDE_INT delta;
tree addr, stmts, prefetch, params, write_p;
block_stmt_iterator bsi;
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "Issued prefetch for %p.\n", (void *) ref);
delta = ahead * ref->group->step + ref->delta;
addr = ref->group->group_iv;
if (delta)
addr = build (PLUS_EXPR, ptr_type_node,
addr, build_int_cst (ptr_type_node, delta));
addr = force_gimple_operand (addr, &stmts, false,
SSA_NAME_VAR (ref->group->group_iv));
write_p = ref->write_p ? integer_one_node : integer_zero_node;
params = tree_cons (NULL_TREE, addr,
tree_cons (NULL_TREE, write_p, NULL_TREE));
prefetch = build_function_call_expr (built_in_decls[BUILT_IN_PREFETCH],
params);
bsi = bsi_after_labels (loop->header);
if (stmts)
bsi_insert_after (&bsi, stmts, BSI_CONTINUE_LINKING);
bsi_insert_after (&bsi, prefetch, BSI_NEW_STMT);
}
static void
issue_prefetches (struct loop *loop, struct mem_ref_group *groups,
unsigned ahead)
{
struct mem_ref *ref;
tree iv_var, base, step;
block_stmt_iterator bsi;
bool after;
for (; groups; groups = groups->next)
{
if (!groups->issue_prefetch_p)
continue;
iv_var = create_tmp_var (ptr_type_node, "prefetchtmp");
add_referenced_tmp_var (iv_var);
if (TREE_CODE (groups->base) == INDIRECT_REF)
base = fold_convert (ptr_type_node, TREE_OPERAND (groups->base, 0));
else
base = build (ADDR_EXPR, ptr_type_node, groups->base);
step = build_int_cst (ptr_type_node, groups->step);
standard_iv_increment_position (loop, &bsi, &after);
create_iv (base, step, iv_var, loop, &bsi, after, &groups->group_iv,
NULL);
for (ref = groups->refs; ref; ref = ref->next)
if (ref->issue_prefetch_p)
issue_prefetch_ref (loop, ref, ahead);
}
}
static void
loop_prefetch_arrays (struct loop *loop)
{
struct mem_ref_group *refs;
unsigned ahead, ninsns;
refs = gather_memory_references (loop);
prune_by_reuse (refs);
ninsns = tree_num_loop_insns (loop);
ahead = (PREFETCH_LATENCY + ninsns - 1) / ninsns;
schedule_prefetches (refs, ahead);
issue_prefetches (loop, refs, ahead);
release_mem_refs (refs);
}
void
tree_ssa_prefetch_arrays (struct loops *loops)
{
unsigned i;
struct loop *loop;
if (!HAVE_prefetch)
return;
if (PREFETCH_BLOCK & (PREFETCH_BLOCK - 1))
abort ();
for (i = 1; i < loops->num; i++)
{
loop = loops->parray[i];
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "Processing loop %d:\n", loop->num);
if (loop)
loop_prefetch_arrays (loop);
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "\n\n");
}
}