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"
#include "toplev.h"
#include "params.h"
#include "langhooks.h"
#ifndef PREFETCH_LATENCY
#define PREFETCH_LATENCY 200
#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;
struct mem_ref *refs;
struct mem_ref_group *next;
};
#define PREFETCH_ALL (~(unsigned HOST_WIDE_INT) 0)
struct mem_ref
{
tree stmt;
tree mem;
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)
{
struct mem_ref_group *group;
for (; *groups; groups = &(*groups)->next)
{
if ((*groups)->step == step
&& operand_equal_p ((*groups)->base, base, 0))
return *groups;
if ((*groups)->step < step)
break;
}
group = xcalloc (1, sizeof (struct mem_ref_group));
group->base = base;
group->step = step;
group->refs = NULL;
group->next = *groups;
*groups = group;
return group;
}
static void
record_ref (struct mem_ref_group *group, tree stmt, tree mem,
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)->stmt = stmt;
(*aref)->mem = mem;
(*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;
affine_iv iv;
if (TREE_CODE (base) == MISALIGNED_INDIRECT_REF
|| TREE_CODE (base) == ALIGN_INDIRECT_REF)
return false;
if (!simple_iv (ar_data->loop, ar_data->stmt, *index, &iv, false))
return false;
ibase = iv.base;
step = iv.step;
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 = build_int_cst (TREE_TYPE (ibase), 0);
}
if (TREE_CODE (base) == ARRAY_REF)
{
stepsize = array_ref_element_size (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_p, tree *base,
HOST_WIDE_INT *step, HOST_WIDE_INT *delta,
tree stmt)
{
struct ar_data ar_data;
tree off;
HOST_WIDE_INT bit_offset;
tree ref = *ref_p;
*step = 0;
*delta = 0;
if (TREE_CODE (ref) == COMPONENT_REF
&& DECL_NONADDRESSABLE_P (TREE_OPERAND (ref, 1)))
ref = TREE_OPERAND (ref, 0);
*ref_p = ref;
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);
gcc_assert (bit_offset % BITS_PER_UNIT == 0);
*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, stmt, ref, 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 (REFERENCE_CLASS_P (rhs))
gather_memory_references_ref (loop, &refs, rhs, false, stmt);
if (REFERENCE_CLASS_P (lhs))
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)
{
gcc_assert (by > 0);
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 bool
should_issue_prefetch_p (struct mem_ref *ref)
{
if (ref->prefetch_before != PREFETCH_ALL)
return false;
return true;
}
static bool
schedule_prefetches (struct mem_ref_group *groups, unsigned unroll_factor,
unsigned ahead)
{
unsigned max_prefetches, n_prefetches;
struct mem_ref *ref;
bool any = false;
max_prefetches = (SIMULTANEOUS_PREFETCHES * unroll_factor) / ahead;
if (max_prefetches > (unsigned) SIMULTANEOUS_PREFETCHES)
max_prefetches = SIMULTANEOUS_PREFETCHES;
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "Max prefetches to issue: %d.\n", max_prefetches);
if (!max_prefetches)
return false;
for (; groups; groups = groups->next)
for (ref = groups->refs; ref; ref = ref->next)
{
if (!should_issue_prefetch_p (ref))
continue;
ref->issue_prefetch_p = true;
n_prefetches = ((unroll_factor + ref->prefetch_mod - 1)
/ ref->prefetch_mod);
if (max_prefetches <= n_prefetches)
return true;
max_prefetches -= n_prefetches;
any = true;
}
return any;
}
static bool
anything_to_prefetch_p (struct mem_ref_group *groups)
{
struct mem_ref *ref;
for (; groups; groups = groups->next)
for (ref = groups->refs; ref; ref = ref->next)
if (should_issue_prefetch_p (ref))
return true;
return false;
}
static void
issue_prefetch_ref (struct mem_ref *ref, unsigned unroll_factor, unsigned ahead)
{
HOST_WIDE_INT delta;
tree addr, addr_base, prefetch, params, write_p;
block_stmt_iterator bsi;
unsigned n_prefetches, ap;
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "Issued prefetch for %p.\n", (void *) ref);
bsi = bsi_for_stmt (ref->stmt);
n_prefetches = ((unroll_factor + ref->prefetch_mod - 1)
/ ref->prefetch_mod);
addr_base = build_fold_addr_expr_with_type (ref->mem, ptr_type_node);
addr_base = force_gimple_operand_bsi (&bsi, unshare_expr (addr_base), true, NULL);
for (ap = 0; ap < n_prefetches; ap++)
{
delta = (ahead + ap * ref->prefetch_mod) * ref->group->step;
addr = fold_build2 (PLUS_EXPR, ptr_type_node,
addr_base, build_int_cst (ptr_type_node, delta));
addr = force_gimple_operand_bsi (&bsi, unshare_expr (addr), true, NULL);
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_insert_before (&bsi, prefetch, BSI_SAME_STMT);
}
}
static void
issue_prefetches (struct mem_ref_group *groups,
unsigned unroll_factor, unsigned ahead)
{
struct mem_ref *ref;
for (; groups; groups = groups->next)
for (ref = groups->refs; ref; ref = ref->next)
if (ref->issue_prefetch_p)
issue_prefetch_ref (ref, unroll_factor, ahead);
}
static bool
should_unroll_loop_p (struct loop *loop, struct tree_niter_desc *desc,
unsigned factor)
{
if (!can_unroll_loop_p (loop, factor, desc))
return false;
if (loop->num_nodes > 2)
return false;
return true;
}
static unsigned
determine_unroll_factor (struct loop *loop, struct mem_ref_group *refs,
unsigned ahead, unsigned ninsns,
struct tree_niter_desc *desc)
{
unsigned upper_bound, size_factor, constraint_factor;
unsigned factor, max_mod_constraint, ahead_factor;
struct mem_ref_group *agp;
struct mem_ref *ref;
upper_bound = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES);
size_factor = PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS) / ninsns;
if (size_factor <= 1)
return 1;
if (size_factor < upper_bound)
upper_bound = size_factor;
max_mod_constraint = 1;
for (agp = refs; agp; agp = agp->next)
for (ref = agp->refs; ref; ref = ref->next)
if (should_issue_prefetch_p (ref)
&& ref->prefetch_mod > max_mod_constraint)
max_mod_constraint = ref->prefetch_mod;
constraint_factor = max_mod_constraint;
ahead_factor = ((ahead + SIMULTANEOUS_PREFETCHES - 1)
/ SIMULTANEOUS_PREFETCHES);
if (constraint_factor < ahead_factor)
factor = ahead_factor;
else
factor = constraint_factor;
if (factor > upper_bound)
factor = upper_bound;
if (!should_unroll_loop_p (loop, desc, factor))
return 1;
return factor;
}
static bool
loop_prefetch_arrays (struct loops *loops, struct loop *loop)
{
struct mem_ref_group *refs;
unsigned ahead, ninsns, unroll_factor;
struct tree_niter_desc desc;
bool unrolled = false;
refs = gather_memory_references (loop);
prune_by_reuse (refs);
if (!anything_to_prefetch_p (refs))
goto fail;
ninsns = tree_num_loop_insns (loop);
ahead = (PREFETCH_LATENCY + ninsns - 1) / ninsns;
unroll_factor = determine_unroll_factor (loop, refs, ahead, ninsns,
&desc);
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "Ahead %d, unroll factor %d\n", ahead, unroll_factor);
if (unroll_factor > 1
&& cst_and_fits_in_hwi (desc.niter)
&& (unsigned HOST_WIDE_INT) int_cst_value (desc.niter) < unroll_factor)
goto fail;
if (!schedule_prefetches (refs, unroll_factor, ahead))
goto fail;
if (unroll_factor != 1)
{
tree_unroll_loop (loops, loop, unroll_factor,
single_dom_exit (loop), &desc);
unrolled = true;
}
issue_prefetches (refs, unroll_factor, ahead);
fail:
release_mem_refs (refs);
return unrolled;
}
unsigned int
tree_ssa_prefetch_arrays (struct loops *loops)
{
unsigned i;
struct loop *loop;
bool unrolled = false;
int todo_flags = 0;
if (!HAVE_prefetch
|| PREFETCH_BLOCK == 0)
return 0;
initialize_original_copy_tables ();
if (!built_in_decls[BUILT_IN_PREFETCH])
{
tree type = build_function_type (void_type_node,
tree_cons (NULL_TREE,
const_ptr_type_node,
NULL_TREE));
tree decl = lang_hooks.builtin_function ("__builtin_prefetch", type,
BUILT_IN_PREFETCH, BUILT_IN_NORMAL,
NULL, NULL_TREE);
DECL_IS_NOVOPS (decl) = true;
built_in_decls[BUILT_IN_PREFETCH] = decl;
}
gcc_assert ((PREFETCH_BLOCK & (PREFETCH_BLOCK - 1)) == 0);
for (i = loops->num - 1; i > 0; i--)
{
loop = loops->parray[i];
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "Processing loop %d:\n", loop->num);
if (loop)
unrolled |= loop_prefetch_arrays (loops, loop);
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "\n\n");
}
if (unrolled)
{
scev_reset ();
todo_flags |= TODO_cleanup_cfg;
}
free_original_copy_tables ();
return todo_flags;
}