Commit 08dab97a by Razya Ladelsky Committed by Razya Ladelsky

ssa-loop-manip.c: Include langhooks.h.

2009-07-30  Razya Ladelsky <razya@il.ibm.com>

        * ssa-loop-manip.c: Include langhooks.h.
        (rewrite_phi_with_iv): New.
        (rewrite_all_phi_nodes_with_iv): New.
        (canonicalize_loop_ivs): Move here from tree-parloops.c.
        Remove reduction_list argument. Use rewrite_all_phi_nodes_with_iv.
        * tree-parloops.c (loop_parallel_p): Move out all conditions
        except dependency check.
        (canonicalize_loop_ivs): Move to tree-ssa-loop-manip.c.
        (gen_parallel_loop): Call canonicalize_loop_ivs without
        reduction_list argument.
        (build_new_reduction): New.
        (gather_scalar_reductions): New.
        (try_get_loop_niter): New.
        (try_create_reduction_list): New.
        (parallleize_loops): Change the parallel conditions check.
        * tree-flow.h (canonicalize_loop_ivs): Remove one argument.
        * Makefile.in (tree-ssa-loop-manip.o): Add langhooks.h dependency.

From-SVN: r150250
parent 88c2fd3d
2009-07-30 Razya Ladelsky <razya@il.ibm.com>
* ssa-loop-manip.c: Include langhooks.h.
(rewrite_phi_with_iv): New.
(rewrite_all_phi_nodes_with_iv): New.
(canonicalize_loop_ivs): Move here from tree-parloops.c.
Remove reduction_list argument. Use rewrite_all_phi_nodes_with_iv.
* tree-parloops.c (loop_parallel_p): Move out all conditions
except dependency check.
(canonicalize_loop_ivs): Move to tree-ssa-loop-manip.c.
(gen_parallel_loop): Call canonicalize_loop_ivs without
reduction_list argument.
(build_new_reduction): New.
(gather_scalar_reductions): New.
(try_get_loop_niter): New.
(try_create_reduction_list): New.
(parallleize_loops): Change the parallel conditions check.
* tree-flow.h (canonicalize_loop_ivs): Remove one argument.
* Makefile.in (tree-ssa-loop-manip.o): Add langhooks.h dependency.
2009-07-30 Dave Korn <dave.korn.cygwin@gmail.com> 2009-07-30 Dave Korn <dave.korn.cygwin@gmail.com>
* opt-functions.awk (opt_args): Allow argument to be enclosed in * opt-functions.awk (opt_args): Allow argument to be enclosed in
......
...@@ -2356,7 +2356,7 @@ tree-ssa-loop-manip.o : tree-ssa-loop-manip.c $(TREE_FLOW_H) $(CONFIG_H) \ ...@@ -2356,7 +2356,7 @@ tree-ssa-loop-manip.o : tree-ssa-loop-manip.c $(TREE_FLOW_H) $(CONFIG_H) \
$(SYSTEM_H) coretypes.h $(TM_H) $(TREE_H) $(RTL_H) $(TM_P_H) hard-reg-set.h \ $(SYSTEM_H) coretypes.h $(TM_H) $(TREE_H) $(RTL_H) $(TM_P_H) hard-reg-set.h \
$(BASIC_BLOCK_H) output.h $(DIAGNOSTIC_H) $(TREE_FLOW_H) $(TREE_DUMP_H) \ $(BASIC_BLOCK_H) output.h $(DIAGNOSTIC_H) $(TREE_FLOW_H) $(TREE_DUMP_H) \
$(TIMEVAR_H) $(CFGLOOP_H) $(TREE_PASS_H) $(CFGLAYOUT_H) \ $(TIMEVAR_H) $(CFGLOOP_H) $(TREE_PASS_H) $(CFGLAYOUT_H) \
$(SCEV_H) $(PARAMS_H) $(TREE_INLINE_H) $(SCEV_H) $(PARAMS_H) $(TREE_INLINE_H) langhooks.h
tree-ssa-loop-im.o : tree-ssa-loop-im.c $(TREE_FLOW_H) $(CONFIG_H) \ tree-ssa-loop-im.o : tree-ssa-loop-im.c $(TREE_FLOW_H) $(CONFIG_H) \
$(SYSTEM_H) $(RTL_H) $(TREE_H) $(TM_P_H) $(CFGLOOP_H) domwalk.h \ $(SYSTEM_H) $(RTL_H) $(TREE_H) $(TM_P_H) $(CFGLOOP_H) domwalk.h \
$(PARAMS_H) output.h $(DIAGNOSTIC_H) $(TIMEVAR_H) $(TM_H) coretypes.h \ $(PARAMS_H) output.h $(DIAGNOSTIC_H) $(TIMEVAR_H) $(TM_H) coretypes.h \
......
...@@ -746,7 +746,7 @@ unsigned int tree_unroll_loops_completely (bool, bool); ...@@ -746,7 +746,7 @@ unsigned int tree_unroll_loops_completely (bool, bool);
unsigned int tree_ssa_prefetch_arrays (void); unsigned int tree_ssa_prefetch_arrays (void);
void tree_ssa_iv_optimize (void); void tree_ssa_iv_optimize (void);
unsigned tree_predictive_commoning (void); unsigned tree_predictive_commoning (void);
tree canonicalize_loop_ivs (struct loop *, htab_t, tree *); tree canonicalize_loop_ivs (struct loop *, tree *);
bool parallelize_loops (void); bool parallelize_loops (void);
bool loop_only_exit_p (const struct loop *, const_edge); bool loop_only_exit_p (const struct loop *, const_edge);
......
...@@ -241,175 +241,22 @@ name_to_copy_elt_hash (const void *aa) ...@@ -241,175 +241,22 @@ name_to_copy_elt_hash (const void *aa)
return (hashval_t) a->version; return (hashval_t) a->version;
} }
/* Returns true if the iterations of LOOP are independent on each other (that
is, if we can execute them in parallel), and if LOOP satisfies other /* Data dependency analysis. Returns true if the iterations of LOOP
conditions that we need to be able to parallelize it. Description of number are independent on each other (that is, if we can execute them
of iterations is stored to NITER. Reduction analysis is done, if in parallel). */
reductions are found, they are inserted to the REDUCTION_LIST. */
static bool static bool
loop_parallel_p (struct loop *loop, htab_t reduction_list, loop_parallel_p (struct loop *loop)
struct tree_niter_desc *niter)
{ {
edge exit = single_dom_exit (loop);
VEC (ddr_p, heap) * dependence_relations; VEC (ddr_p, heap) * dependence_relations;
VEC (data_reference_p, heap) *datarefs; VEC (data_reference_p, heap) *datarefs;
lambda_trans_matrix trans; lambda_trans_matrix trans;
bool ret = false; bool ret = false;
gimple_stmt_iterator gsi;
loop_vec_info simple_loop_info;
/* Only consider innermost loops with just one exit. The innermost-loop
restriction is not necessary, but it makes things simpler. */
if (loop->inner || !exit)
return false;
if (dump_file && (dump_flags & TDF_DETAILS)) if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "\nConsidering loop %d\n", loop->num); fprintf (dump_file, "\nConsidering loop %d\n", loop->num);
/* We need to know # of iterations, and there should be no uses of values
defined inside loop outside of it, unless the values are invariants of
the loop. */
if (!number_of_iterations_exit (loop, exit, niter, false))
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, " FAILED: number of iterations not known\n");
return false;
}
vect_dump = NULL;
simple_loop_info = vect_analyze_loop_form (loop);
for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple phi = gsi_stmt (gsi);
gimple reduc_stmt = NULL;
bool dummy;
/* ??? TODO: Change this into a generic function that
recognizes reductions. */
if (!is_gimple_reg (PHI_RESULT (phi)))
continue;
if (simple_loop_info)
reduc_stmt = vect_is_simple_reduction (simple_loop_info, phi, true,
&dummy);
/* Create a reduction_info struct, initialize it and insert it to
the reduction list. */
if (reduc_stmt)
{
PTR *slot;
struct reduction_info *new_reduction;
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file,
"Detected reduction. reduction stmt is: \n");
print_gimple_stmt (dump_file, reduc_stmt, 0, 0);
fprintf (dump_file, "\n");
}
new_reduction = XCNEW (struct reduction_info);
new_reduction->reduc_stmt = reduc_stmt;
new_reduction->reduc_phi = phi;
new_reduction->reduction_code = gimple_assign_rhs_code (reduc_stmt);
slot = htab_find_slot (reduction_list, new_reduction, INSERT);
*slot = new_reduction;
}
}
/* Get rid of the information created by the vectorizer functions. */
destroy_loop_vec_info (simple_loop_info, true);
for (gsi = gsi_start_phis (exit->dest); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple phi = gsi_stmt (gsi);
struct reduction_info *red;
imm_use_iterator imm_iter;
use_operand_p use_p;
gimple reduc_phi;
tree val = PHI_ARG_DEF_FROM_EDGE (phi, exit);
if (is_gimple_reg (val))
{
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, "phi is ");
print_gimple_stmt (dump_file, phi, 0, 0);
fprintf (dump_file, "arg of phi to exit: value ");
print_generic_expr (dump_file, val, 0);
fprintf (dump_file, " used outside loop\n");
fprintf (dump_file,
" checking if it a part of reduction pattern: \n");
}
if (htab_elements (reduction_list) == 0)
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file,
" FAILED: it is not a part of reduction.\n");
return false;
}
reduc_phi = NULL;
FOR_EACH_IMM_USE_FAST (use_p, imm_iter, val)
{
if (flow_bb_inside_loop_p (loop, gimple_bb (USE_STMT (use_p))))
{
reduc_phi = USE_STMT (use_p);
break;
}
}
red = reduction_phi (reduction_list, reduc_phi);
if (red == NULL)
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file,
" FAILED: it is not a part of reduction.\n");
return false;
}
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, "reduction phi is ");
print_gimple_stmt (dump_file, red->reduc_phi, 0, 0);
fprintf (dump_file, "reduction stmt is ");
print_gimple_stmt (dump_file, red->reduc_stmt, 0, 0);
}
}
}
/* The iterations of the loop may communicate only through bivs whose
iteration space can be distributed efficiently. */
for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple phi = gsi_stmt (gsi);
tree def = PHI_RESULT (phi);
affine_iv iv;
if (is_gimple_reg (def) && !simple_iv (loop, loop, def, &iv, true))
{
struct reduction_info *red;
red = reduction_phi (reduction_list, phi);
if (red == NULL)
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file,
" FAILED: scalar dependency between iterations\n");
return false;
}
}
}
/* We need to version the loop to verify assumptions in runtime. */
if (!can_duplicate_loop_p (loop))
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, " FAILED: cannot be duplicated\n");
return false;
}
/* Check for problems with dependences. If the loop can be reversed, /* Check for problems with dependences. If the loop can be reversed,
the iterations are independent. */ the iterations are independent. */
datarefs = VEC_alloc (data_reference_p, heap, 10); datarefs = VEC_alloc (data_reference_p, heap, 10);
...@@ -1330,114 +1177,6 @@ create_loop_fn (void) ...@@ -1330,114 +1177,6 @@ create_loop_fn (void)
return decl; return decl;
} }
/* Bases all the induction variables in LOOP on a single induction
variable (unsigned with base 0 and step 1), whose final value is
compared with *NIT. When the IV type precision has to be larger
than *NIT type precision, *NIT is converted to the larger type, the
conversion code is inserted before the loop, and *NIT is updated to
the new definition. The induction variable is incremented in the
loop latch. REDUCTION_LIST describes the reductions in LOOP.
Return the induction variable that was created. */
tree
canonicalize_loop_ivs (struct loop *loop, htab_t reduction_list, tree *nit)
{
unsigned precision = TYPE_PRECISION (TREE_TYPE (*nit));
unsigned original_precision = precision;
tree res, type, var_before, val, atype, mtype;
gimple_stmt_iterator gsi, psi;
gimple phi, stmt;
bool ok;
affine_iv iv;
edge exit = single_dom_exit (loop);
struct reduction_info *red;
gimple_seq stmts;
for (psi = gsi_start_phis (loop->header);
!gsi_end_p (psi); gsi_next (&psi))
{
phi = gsi_stmt (psi);
res = PHI_RESULT (phi);
if (is_gimple_reg (res) && TYPE_PRECISION (TREE_TYPE (res)) > precision)
precision = TYPE_PRECISION (TREE_TYPE (res));
}
type = lang_hooks.types.type_for_size (precision, 1);
if (original_precision != precision)
{
*nit = fold_convert (type, *nit);
*nit = force_gimple_operand (*nit, &stmts, true, NULL_TREE);
if (stmts)
gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
}
gsi = gsi_last_bb (loop->latch);
create_iv (build_int_cst_type (type, 0), build_int_cst (type, 1), NULL_TREE,
loop, &gsi, true, &var_before, NULL);
gsi = gsi_after_labels (loop->header);
for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); )
{
phi = gsi_stmt (psi);
res = PHI_RESULT (phi);
if (!is_gimple_reg (res) || res == var_before)
{
gsi_next (&psi);
continue;
}
ok = simple_iv (loop, loop, res, &iv, true);
if (reduction_list)
red = reduction_phi (reduction_list, phi);
else
red = NULL;
/* We preserve the reduction phi nodes. */
if (!ok && red)
{
gsi_next (&psi);
continue;
}
else
gcc_assert (ok);
remove_phi_node (&psi, false);
atype = TREE_TYPE (res);
mtype = POINTER_TYPE_P (atype) ? sizetype : atype;
val = fold_build2 (MULT_EXPR, mtype, unshare_expr (iv.step),
fold_convert (mtype, var_before));
val = fold_build2 (POINTER_TYPE_P (atype)
? POINTER_PLUS_EXPR : PLUS_EXPR,
atype, unshare_expr (iv.base), val);
val = force_gimple_operand_gsi (&gsi, val, false, NULL_TREE, true,
GSI_SAME_STMT);
stmt = gimple_build_assign (res, val);
gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
SSA_NAME_DEF_STMT (res) = stmt;
}
stmt = last_stmt (exit->src);
/* Make the loop exit if the control condition is not satisfied. */
if (exit->flags & EDGE_TRUE_VALUE)
{
edge te, fe;
extract_true_false_edges_from_block (exit->src, &te, &fe);
te->flags = EDGE_FALSE_VALUE;
fe->flags = EDGE_TRUE_VALUE;
}
gimple_cond_set_code (stmt, LT_EXPR);
gimple_cond_set_lhs (stmt, var_before);
gimple_cond_set_rhs (stmt, *nit);
update_stmt (stmt);
return var_before;
}
/* Moves the exit condition of LOOP to the beginning of its header, and /* Moves the exit condition of LOOP to the beginning of its header, and
duplicates the part of the last iteration that gets disabled to the duplicates the part of the last iteration that gets disabled to the
exit of the loop. NIT is the number of iterations of the loop exit of the loop. NIT is the number of iterations of the loop
...@@ -1675,12 +1414,14 @@ create_parallel_loop (struct loop *loop, tree loop_fn, tree data, ...@@ -1675,12 +1414,14 @@ create_parallel_loop (struct loop *loop, tree loop_fn, tree data,
return paral_bb; return paral_bb;
} }
/* Generates code to execute the iterations of LOOP in N_THREADS threads in /* Generates code to execute the iterations of LOOP in N_THREADS
parallel. NITER describes number of iterations of LOOP. threads in parallel.
NITER describes number of iterations of LOOP.
REDUCTION_LIST describes the reductions existent in the LOOP. */ REDUCTION_LIST describes the reductions existent in the LOOP. */
static void static void
gen_parallel_loop (struct loop *loop, htab_t reduction_list, gen_parallel_loop (struct loop *loop, htab_t reduction_list,
unsigned n_threads, struct tree_niter_desc *niter) unsigned n_threads, struct tree_niter_desc *niter)
{ {
struct loop *nloop; struct loop *nloop;
...@@ -1782,7 +1523,7 @@ gen_parallel_loop (struct loop *loop, htab_t reduction_list, ...@@ -1782,7 +1523,7 @@ gen_parallel_loop (struct loop *loop, htab_t reduction_list,
free_original_copy_tables (); free_original_copy_tables ();
/* Base all the induction variables in LOOP on a single control one. */ /* Base all the induction variables in LOOP on a single control one. */
canonicalize_loop_ivs (loop, reduction_list, &nit); canonicalize_loop_ivs (loop, &nit);
/* Ensure that the exit condition is the first statement in the loop. */ /* Ensure that the exit condition is the first statement in the loop. */
transform_to_exit_first_loop (loop, reduction_list, nit); transform_to_exit_first_loop (loop, reduction_list, nit);
...@@ -1848,6 +1589,184 @@ loop_has_vector_phi_nodes (struct loop *loop ATTRIBUTE_UNUSED) ...@@ -1848,6 +1589,184 @@ loop_has_vector_phi_nodes (struct loop *loop ATTRIBUTE_UNUSED)
return res; return res;
} }
/* Create a reduction_info struct, initialize it with REDUC_STMT
and PHI, insert it to the REDUCTION_LIST. */
static void
build_new_reduction (htab_t reduction_list, gimple reduc_stmt, gimple phi)
{
PTR *slot;
struct reduction_info *new_reduction;
gcc_assert (reduc_stmt);
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file,
"Detected reduction. reduction stmt is: \n");
print_gimple_stmt (dump_file, reduc_stmt, 0, 0);
fprintf (dump_file, "\n");
}
new_reduction = XCNEW (struct reduction_info);
new_reduction->reduc_stmt = reduc_stmt;
new_reduction->reduc_phi = phi;
new_reduction->reduction_code = gimple_assign_rhs_code (reduc_stmt);
slot = htab_find_slot (reduction_list, new_reduction, INSERT);
*slot = new_reduction;
}
/* Detect all reductions in the LOOP, insert them into REDUCTION_LIST. */
static void
gather_scalar_reductions (loop_p loop, htab_t reduction_list)
{
gimple_stmt_iterator gsi;
loop_vec_info simple_loop_info;
vect_dump = NULL;
simple_loop_info = vect_analyze_loop_form (loop);
for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple phi = gsi_stmt (gsi);
affine_iv iv;
tree res = PHI_RESULT (phi);
bool double_reduc;
if (!is_gimple_reg (res))
continue;
if (!simple_iv (loop, loop, res, &iv, true)
&& simple_loop_info)
{
gimple reduc_stmt = vect_is_simple_reduction (simple_loop_info, phi, true, &double_reduc);
if (reduc_stmt)
build_new_reduction (reduction_list, reduc_stmt, phi);
}
}
destroy_loop_vec_info (simple_loop_info, true);
}
/* Try to initialize NITER for code generation part. */
static bool
try_get_loop_niter (loop_p loop, struct tree_niter_desc *niter)
{
edge exit = single_dom_exit (loop);
gcc_assert (exit);
/* We need to know # of iterations, and there should be no uses of values
defined inside loop outside of it, unless the values are invariants of
the loop. */
if (!number_of_iterations_exit (loop, exit, niter, false))
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, " FAILED: number of iterations not known\n");
return false;
}
return true;
}
/* Try to initialize REDUCTION_LIST for code generation part.
REDUCTION_LIST describes the reductions. */
static bool
try_create_reduction_list (loop_p loop, htab_t reduction_list)
{
edge exit = single_dom_exit (loop);
gimple_stmt_iterator gsi;
gcc_assert (exit);
gather_scalar_reductions (loop, reduction_list);
for (gsi = gsi_start_phis (exit->dest); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple phi = gsi_stmt (gsi);
struct reduction_info *red;
imm_use_iterator imm_iter;
use_operand_p use_p;
gimple reduc_phi;
tree val = PHI_ARG_DEF_FROM_EDGE (phi, exit);
if (is_gimple_reg (val))
{
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, "phi is ");
print_gimple_stmt (dump_file, phi, 0, 0);
fprintf (dump_file, "arg of phi to exit: value ");
print_generic_expr (dump_file, val, 0);
fprintf (dump_file, " used outside loop\n");
fprintf (dump_file,
" checking if it a part of reduction pattern: \n");
}
if (htab_elements (reduction_list) == 0)
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file,
" FAILED: it is not a part of reduction.\n");
return false;
}
reduc_phi = NULL;
FOR_EACH_IMM_USE_FAST (use_p, imm_iter, val)
{
if (flow_bb_inside_loop_p (loop, gimple_bb (USE_STMT (use_p))))
{
reduc_phi = USE_STMT (use_p);
break;
}
}
red = reduction_phi (reduction_list, reduc_phi);
if (red == NULL)
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file,
" FAILED: it is not a part of reduction.\n");
return false;
}
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, "reduction phi is ");
print_gimple_stmt (dump_file, red->reduc_phi, 0, 0);
fprintf (dump_file, "reduction stmt is ");
print_gimple_stmt (dump_file, red->reduc_stmt, 0, 0);
}
}
}
/* The iterations of the loop may communicate only through bivs whose
iteration space can be distributed efficiently. */
for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple phi = gsi_stmt (gsi);
tree def = PHI_RESULT (phi);
affine_iv iv;
if (is_gimple_reg (def) && !simple_iv (loop, loop, def, &iv, true))
{
struct reduction_info *red;
red = reduction_phi (reduction_list, phi);
if (red == NULL)
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file,
" FAILED: scalar dependency between iterations\n");
return false;
}
}
}
return true;
}
/* Detect parallel loops and generate parallel code using libgomp /* Detect parallel loops and generate parallel code using libgomp
primitives. Returns true if some loop was parallelized, false primitives. Returns true if some loop was parallelized, false
otherwise. */ otherwise. */
...@@ -1867,26 +1786,41 @@ parallelize_loops (void) ...@@ -1867,26 +1786,41 @@ parallelize_loops (void)
return false; return false;
reduction_list = htab_create (10, reduction_info_hash, reduction_list = htab_create (10, reduction_info_hash,
reduction_info_eq, free); reduction_info_eq, free);
init_stmt_vec_info_vec (); init_stmt_vec_info_vec ();
FOR_EACH_LOOP (li, loop, 0) FOR_EACH_LOOP (li, loop, 0)
{ {
htab_empty (reduction_list); htab_empty (reduction_list);
if (/* Do not bother with loops in cold areas. */
optimize_loop_nest_for_size_p (loop) /* FIXME: Only consider innermost loops with just one exit. */
/* Or loops that roll too little. */ if (loop->inner || !single_dom_exit (loop))
|| expected_loop_iterations (loop) <= n_threads continue;
/* And of course, the loop must be parallelizable. */
|| !can_duplicate_loop_p (loop) if (/* And of course, the loop must be parallelizable. */
!can_duplicate_loop_p (loop)
|| loop_has_blocks_with_irreducible_flag (loop) || loop_has_blocks_with_irreducible_flag (loop)
/* FIXME: the check for vector phi nodes could be removed. */ /* FIXME: the check for vector phi nodes could be removed. */
|| loop_has_vector_phi_nodes (loop) || loop_has_vector_phi_nodes (loop))
|| !loop_parallel_p (loop, reduction_list, &niter_desc)) continue;
if (/* Do not bother with loops in cold areas. */
optimize_loop_nest_for_size_p (loop)
/* Or loops that roll too little. */
|| expected_loop_iterations (loop) <= n_threads)
continue;
if (!try_get_loop_niter (loop, &niter_desc))
continue;
if (!try_create_reduction_list (loop, reduction_list))
continue;
if (!loop_parallel_p (loop))
continue; continue;
changed = true; changed = true;
gen_parallel_loop (loop, reduction_list, n_threads, &niter_desc); gen_parallel_loop (loop, reduction_list,
n_threads, &niter_desc);
verify_flow_info (); verify_flow_info ();
verify_dominators (CDI_DOMINATORS); verify_dominators (CDI_DOMINATORS);
verify_loop_structure (); verify_loop_structure ();
......
...@@ -37,6 +37,7 @@ along with GCC; see the file COPYING3. If not see ...@@ -37,6 +37,7 @@ along with GCC; see the file COPYING3. If not see
#include "tree-scalar-evolution.h" #include "tree-scalar-evolution.h"
#include "params.h" #include "params.h"
#include "tree-inline.h" #include "tree-inline.h"
#include "langhooks.h"
/* Creates an induction variable with value BASE + STEP * iteration in LOOP. /* Creates an induction variable with value BASE + STEP * iteration in LOOP.
It is expected that neither BASE nor STEP are shared with other expressions It is expected that neither BASE nor STEP are shared with other expressions
...@@ -1100,3 +1101,132 @@ tree_unroll_loop (struct loop *loop, unsigned factor, ...@@ -1100,3 +1101,132 @@ tree_unroll_loop (struct loop *loop, unsigned factor,
tree_transform_and_unroll_loop (loop, factor, exit, desc, tree_transform_and_unroll_loop (loop, factor, exit, desc,
NULL, NULL); NULL, NULL);
} }
/* Rewrite the phi node at position PSI in function of the main
induction variable MAIN_IV and insert the generated code at GSI. */
static void
rewrite_phi_with_iv (loop_p loop,
gimple_stmt_iterator *psi,
gimple_stmt_iterator *gsi,
tree main_iv)
{
affine_iv iv;
gimple stmt, phi = gsi_stmt (*psi);
tree atype, mtype, val, res = PHI_RESULT (phi);
if (!is_gimple_reg (res) || res == main_iv)
{
gsi_next (psi);
return;
}
if (!simple_iv (loop, loop, res, &iv, true))
{
gsi_next (psi);
return;
}
remove_phi_node (psi, false);
atype = TREE_TYPE (res);
mtype = POINTER_TYPE_P (atype) ? sizetype : atype;
val = fold_build2 (MULT_EXPR, mtype, unshare_expr (iv.step),
fold_convert (mtype, main_iv));
val = fold_build2 (POINTER_TYPE_P (atype)
? POINTER_PLUS_EXPR : PLUS_EXPR,
atype, unshare_expr (iv.base), val);
val = force_gimple_operand_gsi (gsi, val, false, NULL_TREE, true,
GSI_SAME_STMT);
stmt = gimple_build_assign (res, val);
gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
SSA_NAME_DEF_STMT (res) = stmt;
}
/* Rewrite all the phi nodes of LOOP in function of the main induction
variable MAIN_IV. */
static void
rewrite_all_phi_nodes_with_iv (loop_p loop, tree main_iv)
{
unsigned i;
basic_block *bbs = get_loop_body_in_dom_order (loop);
gimple_stmt_iterator psi;
for (i = 0; i < loop->num_nodes; i++)
{
basic_block bb = bbs[i];
gimple_stmt_iterator gsi = gsi_after_labels (bb);
if (bb->loop_father != loop)
continue;
for (psi = gsi_start_phis (bb); !gsi_end_p (psi); )
rewrite_phi_with_iv (loop, &psi, &gsi, main_iv);
}
free (bbs);
}
/* Bases all the induction variables in LOOP on a single induction
variable (unsigned with base 0 and step 1), whose final value is
compared with *NIT. When the IV type precision has to be larger
than *NIT type precision, *NIT is converted to the larger type, the
conversion code is inserted before the loop, and *NIT is updated to
the new definition. The induction variable is incremented in the
loop latch. Return the induction variable that was created. */
tree
canonicalize_loop_ivs (struct loop *loop, tree *nit)
{
unsigned precision = TYPE_PRECISION (TREE_TYPE (*nit));
unsigned original_precision = precision;
tree type, var_before;
gimple_stmt_iterator gsi, psi;
gimple stmt;
edge exit = single_dom_exit (loop);
gimple_seq stmts;
for (psi = gsi_start_phis (loop->header);
!gsi_end_p (psi); gsi_next (&psi))
{
gimple phi = gsi_stmt (psi);
tree res = PHI_RESULT (phi);
if (is_gimple_reg (res) && TYPE_PRECISION (TREE_TYPE (res)) > precision)
precision = TYPE_PRECISION (TREE_TYPE (res));
}
type = lang_hooks.types.type_for_size (precision, 1);
if (original_precision != precision)
{
*nit = fold_convert (type, *nit);
*nit = force_gimple_operand (*nit, &stmts, true, NULL_TREE);
if (stmts)
gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
}
gsi = gsi_last_bb (loop->latch);
create_iv (build_int_cst_type (type, 0), build_int_cst (type, 1), NULL_TREE,
loop, &gsi, true, &var_before, NULL);
rewrite_all_phi_nodes_with_iv (loop, var_before);
stmt = last_stmt (exit->src);
/* Make the loop exit if the control condition is not satisfied. */
if (exit->flags & EDGE_TRUE_VALUE)
{
edge te, fe;
extract_true_false_edges_from_block (exit->src, &te, &fe);
te->flags = EDGE_FALSE_VALUE;
fe->flags = EDGE_TRUE_VALUE;
}
gimple_cond_set_code (stmt, LT_EXPR);
gimple_cond_set_lhs (stmt, var_before);
gimple_cond_set_rhs (stmt, *nit);
update_stmt (stmt);
return var_before;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment