static bool
var_used_in_not_loop_header_phi_node (tree var)
{
-
imm_use_iterator imm_iter;
gimple stmt;
bool result = false;
reductions. */
if (simple_iv (loop, loop, res, &iv, true))
{
- gsi_next (psi);
+ if (integer_zerop (iv.step))
+ remove_invariant_phi (region, psi);
+ else
+ gsi_next (psi);
+
return false;
}
switch (gimple_code (stmt))
{
+ case GIMPLE_DEBUG:
/* Control flow expressions can be ignored, as they are
represented in the iteration domains and will be
regenerated by graphite. */
return gbb;
}
+static void
+free_data_refs_aux (VEC (data_reference_p, heap) *datarefs)
+{
+ unsigned int i;
+ struct data_reference *dr;
+
+ for (i = 0; VEC_iterate (data_reference_p, datarefs, i, dr); i++)
+ if (!dr->aux)
+ {
+ free (dr->aux);
+ dr->aux = NULL;
+ }
+}
+
/* Frees GBB. */
static void
if (GBB_CLOOG_IV_TYPES (gbb))
htab_delete (GBB_CLOOG_IV_TYPES (gbb));
+ free_data_refs_aux (GBB_DATA_REFS (gbb));
free_data_refs (GBB_DATA_REFS (gbb));
VEC_free (gimple, heap, GBB_CONDITIONS (gbb));
information. */
static void
-try_generate_gimple_bb (scop_p scop, basic_block bb)
+try_generate_gimple_bb (scop_p scop, basic_block bb, sbitmap reductions)
{
VEC (data_reference_p, heap) *drs = VEC_alloc (data_reference_p, heap, 5);
loop_p nest = outermost_loop_in_sese (SCOP_REGION (scop), bb);
gimple_stmt_iterator gsi;
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
- graphite_find_data_references_in_stmt (nest, gsi_stmt (gsi), &drs);
+ {
+ gimple stmt = gsi_stmt (gsi);
+ if (!is_gimple_debug (stmt))
+ graphite_find_data_references_in_stmt (nest, stmt, &drs);
+ }
if (!graphite_stmt_p (SCOP_REGION (scop), bb, drs))
free_data_refs (drs);
else
- new_poly_bb (scop, new_gimple_bb (bb, drs));
+ new_poly_bb (scop, new_gimple_bb (bb, drs), TEST_BIT (reductions,
+ bb->index));
}
/* Returns true if all predecessors of BB, that are not dominated by BB, are
/* Recursive helper function for build_scops_bbs. */
static void
-build_scop_bbs_1 (scop_p scop, sbitmap visited, basic_block bb)
+build_scop_bbs_1 (scop_p scop, sbitmap visited, basic_block bb, sbitmap reductions)
{
sese region = SCOP_REGION (scop);
VEC (basic_block, heap) *dom;
|| !bb_in_sese_p (bb, region))
return;
- try_generate_gimple_bb (scop, bb);
+ try_generate_gimple_bb (scop, bb, reductions);
SET_BIT (visited, bb->index);
dom = get_dominated_by (CDI_DOMINATORS, bb);
for (i = 0; VEC_iterate (basic_block, dom, i, dom_bb); i++)
if (all_non_dominated_preds_marked_p (dom_bb, visited))
{
- build_scop_bbs_1 (scop, visited, dom_bb);
+ build_scop_bbs_1 (scop, visited, dom_bb, reductions);
VEC_unordered_remove (basic_block, dom, i);
break;
}
/* Gather the basic blocks belonging to the SCOP. */
-void
-build_scop_bbs (scop_p scop)
+static void
+build_scop_bbs (scop_p scop, sbitmap reductions)
{
sbitmap visited = sbitmap_alloc (last_basic_block);
sese region = SCOP_REGION (scop);
sbitmap_zero (visited);
- build_scop_bbs_1 (scop, visited, SESE_ENTRY_BB (region));
-
+ build_scop_bbs_1 (scop, visited, SESE_ENTRY_BB (region), reductions);
sbitmap_free (visited);
}
}
}
-/* Data structure for idx_record_params. */
-
-struct irp_data
-{
- struct loop *loop;
- sese region;
-};
-
-/* For a data reference with an ARRAY_REF as its BASE, record the
- parameters occurring in IDX. DTA is passed in as complementary
- information, and is used by the automatic walker function. This
- function is a callback for for_each_index. */
-
-static bool
-idx_record_params (tree base, tree *idx, void *dta)
-{
- struct irp_data *data = (struct irp_data *) dta;
-
- if (TREE_CODE (base) != ARRAY_REF)
- return true;
-
- if (TREE_CODE (*idx) == SSA_NAME)
- {
- tree scev;
- sese region = data->region;
- struct loop *loop = data->loop;
- Value one;
-
- scev = scalar_evolution_in_region (region, loop, *idx);
-
- value_init (one);
- value_set_si (one, 1);
- scan_tree_for_params (region, scev, NULL, one);
- value_clear (one);
- }
-
- return true;
-}
-
/* Find parameters with respect to REGION in BB. We are looking in memory
access functions, conditions and loop bounds. */
find_params_in_bb (sese region, gimple_bb_p gbb)
{
int i;
+ unsigned j;
data_reference_p dr;
gimple stmt;
loop_p loop = GBB_BB (gbb)->loop_father;
+ Value one;
- for (i = 0; VEC_iterate (data_reference_p, GBB_DATA_REFS (gbb), i, dr); i++)
- {
- struct irp_data irp;
+ value_init (one);
+ value_set_si (one, 1);
- irp.loop = loop;
- irp.region = region;
- for_each_index (&dr->ref, idx_record_params, &irp);
- }
+ /* Find parameters in the access functions of data references. */
+ for (i = 0; VEC_iterate (data_reference_p, GBB_DATA_REFS (gbb), i, dr); i++)
+ for (j = 0; j < DR_NUM_DIMENSIONS (dr); j++)
+ scan_tree_for_params (region, DR_ACCESS_FN (dr, j), NULL, one);
/* Find parameters in conditional statements. */
for (i = 0; VEC_iterate (gimple, GBB_CONDITIONS (gbb), i, stmt); i++)
{
- Value one;
tree lhs = scalar_evolution_in_region (region, loop,
gimple_cond_lhs (stmt));
tree rhs = scalar_evolution_in_region (region, loop,
gimple_cond_rhs (stmt));
- value_init (one);
- value_set_si (one, 1);
scan_tree_for_params (region, lhs, NULL, one);
scan_tree_for_params (region, rhs, NULL, one);
- value_clear (one);
}
+
+ value_clear (one);
}
/* Record the parameters used in the SCOP. A variable is a parameter
ppl_Linear_Expression_t res;
ppl_dimension_type dim;
sese region = SCOP_REGION (PBB_SCOP (pbb));
- loop_p loop = GBB_BB (PBB_BLACK_BOX (pbb))->loop_father;
+ loop_p loop = pbb_loop (pbb);
dim = pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb);
ppl_new_Linear_Expression_with_dimension (&res, dim);
int alias_set_num = 0;
if (dr->aux != NULL)
- {
- alias_set_num = *((int *)(dr->aux));
- free (dr->aux);
- dr->aux = NULL;
- }
+ alias_set_num = ((int *)(dr->aux))[ALIAS_SET_INDEX];
ppl_new_Linear_Expression_with_dimension (&alias, accessp_nb_dims);
ppl_Pointset_Powerset_C_Polyhedron_t accesses_ps;
ppl_dimension_type dom_nb_dims;
ppl_dimension_type accessp_nb_dims;
+ int dr_base_object_set;
ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb),
&dom_nb_dims);
ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron (&accesses_ps,
accesses);
ppl_delete_Polyhedron (accesses);
- new_poly_dr (pbb, accesses_ps, DR_IS_READ (dr) ? PDR_READ : PDR_WRITE, dr,
- DR_NUM_DIMENSIONS (dr));
+
+ dr_base_object_set = ((int *)(dr->aux))[BASE_OBJECT_SET_INDEX];
+
+ new_poly_dr (pbb, dr_base_object_set, accesses_ps, DR_IS_READ (dr) ? PDR_READ : PDR_WRITE,
+ dr, DR_NUM_DIMENSIONS (dr));
}
-/* Group each data reference in DRS with it's alias set num. */
+/* Write to FILE the alias graph of data references with DIMACS format. */
+
+static inline bool
+write_alias_graph_to_ascii_dimacs (FILE *file, char *comment,
+ VEC (data_reference_p, heap) *drs)
+{
+ int num_vertex = VEC_length (data_reference_p, drs);
+ int edge_num = 0;
+ data_reference_p dr1, dr2;
+ int i, j;
+
+ if (num_vertex == 0)
+ return true;
+
+ for (i = 0; VEC_iterate (data_reference_p, drs, i, dr1); i++)
+ for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
+ if (dr_may_alias_p (dr1, dr2))
+ edge_num++;
+
+ fprintf (file, "$\n");
+
+ if (comment)
+ fprintf (file, "c %s\n", comment);
+
+ fprintf (file, "p edge %d %d\n", num_vertex, edge_num);
+
+ for (i = 0; VEC_iterate (data_reference_p, drs, i, dr1); i++)
+ for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
+ if (dr_may_alias_p (dr1, dr2))
+ fprintf (file, "e %d %d\n", i + 1, j + 1);
+
+ return true;
+}
static void
-build_alias_set_for_drs (VEC (data_reference_p, heap) **drs)
+partition_drs_to_sets (VEC (data_reference_p, heap) *drs, int choice,
+ bool (* edge_exist_p) (const struct data_reference *,
+ const struct data_reference *))
{
- int num_vertex = VEC_length (data_reference_p, *drs);
+ int num_vertex = VEC_length (data_reference_p, drs);
struct graph *g = new_graph (num_vertex);
data_reference_p dr1, dr2;
int i, j;
int num_component;
int *queue;
- for (i = 0; VEC_iterate (data_reference_p, *drs, i, dr1); i++)
- for (j = i+1; VEC_iterate (data_reference_p, *drs, j, dr2); j++)
- if (dr_may_alias_p (dr1, dr2))
+ for (i = 0; VEC_iterate (data_reference_p, drs, i, dr1); i++)
+ for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
+ if ((*edge_exist_p) (dr1, dr2))
{
add_edge (g, i, j);
add_edge (g, j, i);
for (i = 0; i < g->n_vertices; i++)
{
- data_reference_p dr = VEC_index (data_reference_p, *drs, i);
- dr->aux = XNEW (int);
- *((int *)(dr->aux)) = g->vertices[i].component + 1;
+ data_reference_p dr = VEC_index (data_reference_p, drs, i);
+ if (!dr->aux)
+ dr->aux = XNEWVEC (int, 2);
+ ((int *)(dr->aux))[choice] = g->vertices[i].component + 1;
}
free (queue);
free_graph (g);
}
+static bool
+dr_same_base_object_p (const struct data_reference *dr1,
+ const struct data_reference *dr2)
+{
+ return operand_equal_p (DR_BASE_OBJECT (dr1), DR_BASE_OBJECT (dr2), 0);
+}
+
+/* Group each data reference in DRS with it's alias set num. */
+
+static void
+build_alias_set_for_drs (VEC (data_reference_p, heap) *drs)
+{
+ partition_drs_to_sets (drs, ALIAS_SET_INDEX, dr_may_alias_p);
+}
+
+/* Group each data reference in DRS with it's base object set num. */
+
+static void
+build_base_obj_set_for_drs (VEC (data_reference_p, heap) *drs)
+{
+ partition_drs_to_sets (drs, BASE_OBJECT_SET_INDEX, dr_same_base_object_p);
+}
+
/* Build the data references for PBB. */
static void
VEC (data_reference_p, heap) *drs = VEC_alloc (data_reference_p, heap, 3);
for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
- {
- VEC (data_reference_p, heap) *gbb_drs = GBB_DATA_REFS (PBB_BLACK_BOX (pbb));
- for (j = 0; VEC_iterate (data_reference_p, gbb_drs, j, dr); j++)
- VEC_safe_push (data_reference_p, heap, drs, dr);
- }
+ for (j = 0; VEC_iterate (data_reference_p,
+ GBB_DATA_REFS (PBB_BLACK_BOX (pbb)), j, dr); j++)
+ VEC_safe_push (data_reference_p, heap, drs, dr);
+
+ build_alias_set_for_drs (drs);
+ build_base_obj_set_for_drs (drs);
+
+ /* When debugging, enable the following code. This cannot be used
+ in production compilers. */
+#if 0
+ {
+ char comment[100];
+ FILE *file;
+
+ file = fopen ("/tmp/dr_alias_graph", "ab");
+ if (file)
+ {
+ snprintf (comment, sizeof (comment), "%s %s", main_input_filename,
+ current_function_name ());
+ write_alias_graph_to_ascii_dimacs (file, comment, drs);
+ fclose (file);
+ }
+ }
+#endif
- build_alias_set_for_drs (&drs);
VEC_free (data_reference_p, heap, drs);
for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
build_pbb_drs (pbb);
}
-/* Return a gsi at the position of the VAR definition. */
+/* Return a gsi at the position of the phi node STMT. */
static gimple_stmt_iterator
-gsi_for_ssa_name_def (tree var)
+gsi_for_phi_node (gimple stmt)
{
- gimple stmt;
- basic_block bb;
- gimple_stmt_iterator gsi;
gimple_stmt_iterator psi;
-
- gcc_assert (TREE_CODE (var) == SSA_NAME);
-
- stmt = SSA_NAME_DEF_STMT (var);
- bb = gimple_bb (stmt);
+ basic_block bb = gimple_bb (stmt);
for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
if (stmt == gsi_stmt (psi))
- return gsi_after_labels (bb);
-
- for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
- if (stmt == gsi_stmt (gsi))
- {
- gsi_next (&gsi);
- return gsi;
- }
+ return psi;
gcc_unreachable ();
- return gsi;
+ return psi;
}
/* Insert the assignment "RES := VAR" just after the definition of VAR. */
static void
insert_out_of_ssa_copy (tree res, tree var)
{
- gimple_stmt_iterator gsi = gsi_for_ssa_name_def (var);
gimple stmt;
gimple_seq stmts;
gimple_stmt_iterator si;
+ gimple_stmt_iterator gsi;
var = force_gimple_operand (var, &stmts, true, NULL_TREE);
stmt = gimple_build_assign (res, var);
stmts = gimple_seq_alloc ();
si = gsi_last (stmts);
gsi_insert_after (&si, stmt, GSI_NEW_STMT);
- gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
+
+ stmt = SSA_NAME_DEF_STMT (var);
+ if (gimple_code (stmt) == GIMPLE_PHI)
+ {
+ gsi = gsi_after_labels (gimple_bb (stmt));
+ gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
+ }
+ else
+ {
+ gsi = gsi_for_stmt (stmt);
+ gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT);
+ }
}
/* Insert on edge E the assignment "RES := EXPR". */
static bool
scalar_close_phi_node_p (gimple phi)
{
- gcc_assert (gimple_code (phi) == GIMPLE_PHI);
-
- if (!is_gimple_reg (gimple_phi_result (phi)))
+ if (gimple_code (phi) != GIMPLE_PHI
+ || !is_gimple_reg (gimple_phi_result (phi)))
return false;
return (gimple_phi_num_args (phi) == 1);
gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
}
+/* Return true when DEF can be analyzed in REGION by the scalar
+ evolution analyzer. */
+
+static bool
+scev_analyzable_p (tree def, sese region)
+{
+ gimple stmt = SSA_NAME_DEF_STMT (def);
+ loop_p loop = loop_containing_stmt (stmt);
+ tree scev = scalar_evolution_in_region (region, loop, def);
+
+ return !chrec_contains_undetermined (scev);
+}
+
+/* Rewrite the scalar dependence of DEF used in USE_STMT with a memory
+ read from ZERO_DIM_ARRAY. */
+
+static void
+rewrite_cross_bb_scalar_dependence (tree zero_dim_array, tree def, gimple use_stmt)
+{
+ tree var = SSA_NAME_VAR (def);
+ gimple name_stmt = gimple_build_assign (var, zero_dim_array);
+ tree name = make_ssa_name (var, name_stmt);
+ ssa_op_iter iter;
+ use_operand_p use_p;
+ gimple_stmt_iterator gsi;
+
+ gimple_assign_set_lhs (name_stmt, name);
+
+ if (gimple_code (use_stmt) == GIMPLE_PHI)
+ {
+ gimple phi = use_stmt;
+ edge entry;
+ unsigned i;
+
+ for (i = 0; i < gimple_phi_num_args (phi); i++)
+ if (operand_equal_p (def, gimple_phi_arg_def (phi, i), 0))
+ {
+ entry = gimple_phi_arg_edge (phi, i);
+ break;
+ }
+
+ FOR_EACH_PHI_ARG (use_p, phi, iter, SSA_OP_USE)
+ if (operand_equal_p (def, USE_FROM_PTR (use_p), 0))
+ {
+ gsi = gsi_last_bb (entry->src);
+ gsi_insert_after (&gsi, name_stmt, GSI_NEW_STMT);
+ SET_USE (use_p, name);
+ break;
+ }
+ }
+ else
+ {
+ gsi = gsi_for_stmt (use_stmt);
+ gsi_insert_before (&gsi, name_stmt, GSI_NEW_STMT);
+
+ FOR_EACH_SSA_USE_OPERAND (use_p, use_stmt, iter, SSA_OP_ALL_USES)
+ if (operand_equal_p (def, USE_FROM_PTR (use_p), 0))
+ replace_exp (use_p, name);
+ }
+
+ update_stmt (use_stmt);
+}
+
+/* Rewrite the scalar dependences crossing the boundary of the BB
+ containing STMT with an array. */
+
+static void
+rewrite_cross_bb_scalar_deps (sese region, gimple_stmt_iterator *gsi)
+{
+ gimple stmt = gsi_stmt (*gsi);
+ imm_use_iterator imm_iter;
+ tree def;
+ basic_block def_bb;
+ tree zero_dim_array = NULL_TREE;
+ gimple use_stmt;
+
+ if (gimple_code (stmt) != GIMPLE_ASSIGN)
+ return;
+
+ def = gimple_assign_lhs (stmt);
+ if (!is_gimple_reg (def)
+ || scev_analyzable_p (def, region))
+ return;
+
+ def_bb = gimple_bb (stmt);
+
+ FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
+ if (def_bb != gimple_bb (use_stmt))
+ {
+ if (!zero_dim_array)
+ {
+ zero_dim_array = create_zero_dim_array (SSA_NAME_VAR (def));
+ insert_out_of_ssa_copy (zero_dim_array, def);
+ gsi_next (gsi);
+ }
+
+ rewrite_cross_bb_scalar_dependence (zero_dim_array, def, use_stmt);
+ }
+}
+
/* Rewrite out of SSA all the reduction phi nodes of SCOP. */
static void
sese region = SCOP_REGION (scop);
FOR_EACH_BB (bb)
- if (bb_in_region (bb, SESE_ENTRY_BB (region), SESE_EXIT_BB (region)))
+ if (bb_in_sese_p (bb, region))
for (psi = gsi_start_phis (bb); !gsi_end_p (psi);)
{
if (scalar_close_phi_node_p (gsi_stmt (psi)))
verify_ssa (false);
verify_loop_closed_ssa ();
#endif
+
+ FOR_EACH_BB (bb)
+ if (bb_in_sese_p (bb, region))
+ for (psi = gsi_start_bb (bb); !gsi_end_p (psi); gsi_next (&psi))
+ rewrite_cross_bb_scalar_deps (region, &psi);
+
+ update_ssa (TODO_update_ssa);
+#ifdef ENABLE_CHECKING
+ verify_ssa (false);
+ verify_loop_closed_ssa ();
+#endif
}
/* Returns the number of pbbs that are in loops contained in SCOP. */
return res;
}
+/* Return the number of data references in BB that write in
+ memory. */
+
+static int
+nb_data_writes_in_bb (basic_block bb)
+{
+ int res = 0;
+ gimple_stmt_iterator gsi;
+
+ for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
+ if (gimple_vdef (gsi_stmt (gsi)))
+ res++;
+
+ return res;
+}
+
+/* Splits STMT out of its current BB. */
+
+static basic_block
+split_reduction_stmt (gimple stmt)
+{
+ gimple_stmt_iterator gsi;
+ basic_block bb = gimple_bb (stmt);
+ edge e;
+
+ /* Do not split basic blocks with no writes to memory: the reduction
+ will be the only write to memory. */
+ if (nb_data_writes_in_bb (bb) == 0)
+ return bb;
+
+ split_block (bb, stmt);
+
+ gsi = gsi_last_bb (bb);
+ gsi_prev (&gsi);
+ e = split_block (bb, gsi_stmt (gsi));
+
+ return e->dest;
+}
+
+/* Return true when stmt is a reduction operation. */
+
+static inline bool
+is_reduction_operation_p (gimple stmt)
+{
+ return flag_associative_math
+ && commutative_tree_code (gimple_assign_rhs_code (stmt))
+ && associative_tree_code (gimple_assign_rhs_code (stmt));
+}
+
+/* Returns true when PHI contains an argument ARG. */
+
+static bool
+phi_contains_arg (gimple phi, tree arg)
+{
+ size_t i;
+
+ for (i = 0; i < gimple_phi_num_args (phi); i++)
+ if (operand_equal_p (arg, gimple_phi_arg_def (phi, i), 0))
+ return true;
+
+ return false;
+}
+
+/* Return a loop phi node that corresponds to a reduction containing LHS. */
+
+static gimple
+follow_ssa_with_commutative_ops (tree arg, tree lhs)
+{
+ gimple stmt;
+
+ if (TREE_CODE (arg) != SSA_NAME)
+ return NULL;
+
+ stmt = SSA_NAME_DEF_STMT (arg);
+
+ if (gimple_code (stmt) == GIMPLE_PHI)
+ {
+ if (phi_contains_arg (stmt, lhs))
+ return stmt;
+ return NULL;
+ }
+
+ if (gimple_num_ops (stmt) == 2)
+ return follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt), lhs);
+
+ if (is_reduction_operation_p (stmt))
+ {
+ gimple res = follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt), lhs);
+
+ return res ? res :
+ follow_ssa_with_commutative_ops (gimple_assign_rhs2 (stmt), lhs);
+ }
+
+ return NULL;
+}
+
+/* Detect commutative and associative scalar reductions starting at
+ the STMT. */
+
+static gimple
+detect_commutative_reduction_arg (tree lhs, gimple stmt, tree arg,
+ VEC (gimple, heap) **in,
+ VEC (gimple, heap) **out)
+{
+ gimple phi = follow_ssa_with_commutative_ops (arg, lhs);
+
+ if (phi)
+ {
+ VEC_safe_push (gimple, heap, *in, stmt);
+ VEC_safe_push (gimple, heap, *out, stmt);
+ return phi;
+ }
+
+ return NULL;
+}
+
+/* Detect commutative and associative scalar reductions starting at
+ the STMT. */
+
+static gimple
+detect_commutative_reduction_assign (gimple stmt, VEC (gimple, heap) **in,
+ VEC (gimple, heap) **out)
+{
+ tree lhs = gimple_assign_lhs (stmt);
+
+ if (gimple_num_ops (stmt) == 2)
+ return detect_commutative_reduction_arg (lhs, stmt,
+ gimple_assign_rhs1 (stmt),
+ in, out);
+
+ if (is_reduction_operation_p (stmt))
+ {
+ gimple res = detect_commutative_reduction_arg (lhs, stmt,
+ gimple_assign_rhs1 (stmt),
+ in, out);
+ return res ? res
+ : detect_commutative_reduction_arg (lhs, stmt,
+ gimple_assign_rhs2 (stmt),
+ in, out);
+ }
+
+ return NULL;
+}
+
+/* Return a loop phi node that corresponds to a reduction containing LHS. */
+
+static gimple
+follow_inital_value_to_phi (tree arg, tree lhs)
+{
+ gimple stmt;
+
+ if (!arg || TREE_CODE (arg) != SSA_NAME)
+ return NULL;
+
+ stmt = SSA_NAME_DEF_STMT (arg);
+
+ if (gimple_code (stmt) == GIMPLE_PHI
+ && phi_contains_arg (stmt, lhs))
+ return stmt;
+
+ return NULL;
+}
+
+
+/* Return the argument of the loop PHI that is the inital value coming
+ from outside the loop. */
+
+static edge
+edge_initial_value_for_loop_phi (gimple phi)
+{
+ size_t i;
+
+ for (i = 0; i < gimple_phi_num_args (phi); i++)
+ {
+ edge e = gimple_phi_arg_edge (phi, i);
+
+ if (loop_depth (e->src->loop_father)
+ < loop_depth (e->dest->loop_father))
+ return e;
+ }
+
+ return NULL;
+}
+
+/* Return the argument of the loop PHI that is the inital value coming
+ from outside the loop. */
+
+static tree
+initial_value_for_loop_phi (gimple phi)
+{
+ size_t i;
+
+ for (i = 0; i < gimple_phi_num_args (phi); i++)
+ {
+ edge e = gimple_phi_arg_edge (phi, i);
+
+ if (loop_depth (e->src->loop_father)
+ < loop_depth (e->dest->loop_father))
+ return gimple_phi_arg_def (phi, i);
+ }
+
+ return NULL_TREE;
+}
+
+/* Detect commutative and associative scalar reductions starting at
+ the loop closed phi node CLOSE_PHI. */
+
+static gimple
+detect_commutative_reduction (gimple stmt, VEC (gimple, heap) **in,
+ VEC (gimple, heap) **out)
+{
+ if (scalar_close_phi_node_p (stmt))
+ {
+ tree arg = gimple_phi_arg_def (stmt, 0);
+ gimple def = SSA_NAME_DEF_STMT (arg);
+ gimple loop_phi = detect_commutative_reduction (def, in, out);
+
+ if (loop_phi)
+ {
+ tree lhs = gimple_phi_result (stmt);
+ tree init = initial_value_for_loop_phi (loop_phi);
+ gimple phi = follow_inital_value_to_phi (init, lhs);
+
+ VEC_safe_push (gimple, heap, *in, loop_phi);
+ VEC_safe_push (gimple, heap, *out, stmt);
+ return phi;
+ }
+ else
+ return NULL;
+ }
+
+ if (gimple_code (stmt) == GIMPLE_ASSIGN)
+ return detect_commutative_reduction_assign (stmt, in, out);
+
+ return NULL;
+}
+
+/* Translate the scalar reduction statement STMT to an array RED
+ knowing that its recursive phi node is LOOP_PHI. */
+
+static void
+translate_scalar_reduction_to_array_for_stmt (tree red, gimple stmt,
+ gimple loop_phi)
+{
+ basic_block bb = gimple_bb (stmt);
+ gimple_stmt_iterator insert_gsi = gsi_after_labels (bb);
+ tree res = gimple_phi_result (loop_phi);
+ gimple assign = gimple_build_assign (res, red);
+
+ gsi_insert_before (&insert_gsi, assign, GSI_SAME_STMT);
+
+ assign = gimple_build_assign (red, gimple_assign_lhs (stmt));
+ insert_gsi = gsi_for_stmt (stmt);
+ gsi_insert_after (&insert_gsi, assign, GSI_SAME_STMT);
+}
+
+/* Insert the assignment "result (CLOSE_PHI) = RED". */
+
+static void
+insert_copyout (tree red, gimple close_phi)
+{
+ tree res = gimple_phi_result (close_phi);
+ basic_block bb = gimple_bb (close_phi);
+ gimple_stmt_iterator insert_gsi = gsi_after_labels (bb);
+ gimple assign = gimple_build_assign (res, red);
+
+ gsi_insert_before (&insert_gsi, assign, GSI_SAME_STMT);
+}
+
+/* Insert the assignment "RED = initial_value (LOOP_PHI)". */
+
+static void
+insert_copyin (tree red, gimple loop_phi)
+{
+ gimple_seq stmts;
+ tree init = initial_value_for_loop_phi (loop_phi);
+ edge e = edge_initial_value_for_loop_phi (loop_phi);
+ basic_block bb = e->src;
+ gimple_stmt_iterator insert_gsi = gsi_last_bb (bb);
+ tree expr = build2 (MODIFY_EXPR, TREE_TYPE (init), red, init);
+
+ force_gimple_operand (expr, &stmts, true, NULL);
+ gsi_insert_seq_before (&insert_gsi, stmts, GSI_SAME_STMT);
+}
+
+/* Rewrite out of SSA the reduction described by the loop phi nodes
+ IN, and the close phi nodes OUT. IN and OUT are structured by loop
+ levels like this:
+
+ IN: stmt, loop_n, ..., loop_0
+ OUT: stmt, close_n, ..., close_0
+
+ the first element is the reduction statement, and the next elements
+ are the loop and close phi nodes of each of the outer loops. */
+
+static void
+translate_scalar_reduction_to_array (VEC (gimple, heap) *in,
+ VEC (gimple, heap) *out,
+ sbitmap reductions)
+{
+ unsigned int i;
+ gimple loop_phi;
+ tree red;
+ gimple_stmt_iterator gsi;
+
+ for (i = 0; VEC_iterate (gimple, in, i, loop_phi); i++)
+ {
+ gimple close_phi = VEC_index (gimple, out, i);
+
+ if (i == 0)
+ {
+ gimple stmt = loop_phi;
+ basic_block bb = split_reduction_stmt (stmt);
+
+ SET_BIT (reductions, bb->index);
+ gcc_assert (close_phi == loop_phi);
+
+ red = create_zero_dim_array (gimple_assign_lhs (stmt));
+ translate_scalar_reduction_to_array_for_stmt
+ (red, stmt, VEC_index (gimple, in, 1));
+ continue;
+ }
+
+ if (i == VEC_length (gimple, in) - 1)
+ {
+ insert_copyout (red, close_phi);
+ insert_copyin (red, loop_phi);
+ }
+
+ gsi = gsi_for_phi_node (loop_phi);
+ remove_phi_node (&gsi, false);
+
+ gsi = gsi_for_phi_node (close_phi);
+ remove_phi_node (&gsi, false);
+ }
+}
+
+/* Rewrites out of SSA a commutative reduction at CLOSE_PHI. */
+
+static void
+rewrite_commutative_reductions_out_of_ssa_close_phi (gimple close_phi,
+ sbitmap reductions)
+{
+ VEC (gimple, heap) *in = VEC_alloc (gimple, heap, 10);
+ VEC (gimple, heap) *out = VEC_alloc (gimple, heap, 10);
+
+ detect_commutative_reduction (close_phi, &in, &out);
+ if (VEC_length (gimple, in) > 0)
+ translate_scalar_reduction_to_array (in, out, reductions);
+
+ VEC_free (gimple, heap, in);
+ VEC_free (gimple, heap, out);
+}
+
+/* Rewrites all the commutative reductions from LOOP out of SSA. */
+
+static void
+rewrite_commutative_reductions_out_of_ssa_loop (loop_p loop,
+ sbitmap reductions)
+{
+ gimple_stmt_iterator gsi;
+ edge exit = single_exit (loop);
+
+ if (!exit)
+ return;
+
+ for (gsi = gsi_start_phis (exit->dest); !gsi_end_p (gsi); gsi_next (&gsi))
+ rewrite_commutative_reductions_out_of_ssa_close_phi (gsi_stmt (gsi),
+ reductions);
+}
+
+/* Rewrites all the commutative reductions from SCOP out of SSA. */
+
+static void
+rewrite_commutative_reductions_out_of_ssa (sese region, sbitmap reductions)
+{
+ loop_iterator li;
+ loop_p loop;
+
+ FOR_EACH_LOOP (li, loop, 0)
+ if (loop_in_sese_p (loop, region))
+ rewrite_commutative_reductions_out_of_ssa_loop (loop, reductions);
+}
+
/* Builds the polyhedral representation for a SESE region. */
bool
build_poly_scop (scop_p scop)
{
sese region = SCOP_REGION (scop);
+ sbitmap reductions = sbitmap_alloc (last_basic_block * 2);
+
+ sbitmap_zero (reductions);
+ rewrite_commutative_reductions_out_of_ssa (region, reductions);
rewrite_reductions_out_of_ssa (scop);
- build_scop_bbs (scop);
+ build_scop_bbs (scop, reductions);
+ sbitmap_free (reductions);
/* FIXME: This restriction is needed to avoid a problem in CLooG.
Once CLooG is fixed, remove this guard. Anyways, it makes no
build_scop_context (scop);
add_conditions_to_constraints (scop);
+ scop_to_lst (scop);
build_scop_scattering (scop);
build_scop_drs (scop);
/* Always return false. Exercise the scop_to_clast function. */
void
-check_poly_representation (scop_p scop)
+check_poly_representation (scop_p scop ATTRIBUTE_UNUSED)
{
#ifdef ENABLE_CHECKING
cloog_prog_clast pc = scop_to_clast (scop);