- replace_exp (use_p, use_expr);
- }
-
- update_stmt (stmt);
-}
-
-/* Copies at the beginning of BB all the scalar computations on which
- BB depends on in the SESE: these are all the scalar variables used
- in BB, defined outside BB and still defined in the SESE, i.e. not a
- parameter of the SESE. The expression that is returned contains
- only induction variables from the generated code: MAP contains the
- induction variables renaming mapping, and is used to translate the
- names of induction variables. */
-
-static void
-expand_scalar_variables (basic_block bb, sese region, htab_t map)
-{
- gimple_stmt_iterator gsi;
-
- for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi);)
- {
- gimple stmt = gsi_stmt (gsi);
- expand_scalar_variables_stmt (stmt, bb, region, map, &gsi);
- gsi_next (&gsi);
- }
-}
-
-/* Rename all the SSA_NAMEs from block BB according to the MAP. */
-
-static void
-rename_variables (basic_block bb, htab_t map)
-{
- gimple_stmt_iterator gsi;
- gimple_stmt_iterator insert_gsi = gsi_start_bb (bb);
-
- for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
- rename_variables_in_stmt (gsi_stmt (gsi), map, &insert_gsi);
-}
-
-/* Remove condition from BB. */
-
-static void
-remove_condition (basic_block bb)
-{
- gimple last = last_stmt (bb);
-
- if (last && gimple_code (last) == GIMPLE_COND)
- {
- gimple_stmt_iterator gsi = gsi_last_bb (bb);
- gsi_remove (&gsi, true);
- }
-}
-
-/* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag set. */
-
-edge
-get_true_edge_from_guard_bb (basic_block bb)
-{
- edge e;
- edge_iterator ei;
-
- FOR_EACH_EDGE (e, ei, bb->succs)
- if (e->flags & EDGE_TRUE_VALUE)
- return e;
-
- gcc_unreachable ();
- return NULL;
-}
-
-/* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag cleared. */
-
-edge
-get_false_edge_from_guard_bb (basic_block bb)
-{
- edge e;
- edge_iterator ei;
-
- FOR_EACH_EDGE (e, ei, bb->succs)
- if (!(e->flags & EDGE_TRUE_VALUE))
- return e;
-
- gcc_unreachable ();
- return NULL;
-}
-
-/* Returns true when NAME is defined in LOOP. */
-
-static bool
-defined_in_loop_p (tree name, loop_p loop)
-{
- gimple stmt = SSA_NAME_DEF_STMT (name);
-
- return (gimple_bb (stmt)->loop_father == loop);
-}
-
-/* Returns the gimple statement that uses NAME outside the loop it is
- defined in, returns NULL if there is no such loop close phi node.
- An invariant of the loop closed SSA form is that the only use of a
- variable, outside the loop it is defined in, is in the loop close
- phi node that just follows the loop. */
-
-static gimple
-alive_after_loop (tree name)
-{
- use_operand_p use_p;
- imm_use_iterator imm_iter;
- loop_p loop = gimple_bb (SSA_NAME_DEF_STMT (name))->loop_father;
-
- FOR_EACH_IMM_USE_FAST (use_p, imm_iter, name)
- {
- gimple stmt = USE_STMT (use_p);
-
- if (gimple_code (stmt) == GIMPLE_PHI
- && gimple_bb (stmt)->loop_father != loop)
- return stmt;
- }
-
- return NULL;
-}
-
-/* Return true if a close phi has not yet been inserted for the use of
- variable NAME on the single exit of LOOP. */
-
-static bool
-close_phi_not_yet_inserted_p (loop_p loop, tree name)
-{
- gimple_stmt_iterator psi;
- basic_block bb = single_exit (loop)->dest;
-
- for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
- if (gimple_phi_arg_def (gsi_stmt (psi), 0) == name)
- return false;
-
- return true;
-}
-
-/* A structure for passing parameters to add_loop_exit_phis. */
-
-typedef struct alep {
- loop_p loop;
- VEC (rename_map_elt, heap) *new_renames;
-} *alep_p;
-
-/* Helper function for htab_traverse in insert_loop_close_phis. */
-
-static int
-add_loop_exit_phis (void **slot, void *data)
-{
- struct rename_map_elt_s *entry;
- alep_p a;
- loop_p loop;
- tree expr, new_name;
- bool def_in_loop_p, used_outside_p, need_close_phi_p;
- gimple old_close_phi;
-
- if (!slot || !data)
- return 1;
-
- entry = (struct rename_map_elt_s *) *slot;
- a = (alep_p) data;
- loop = a->loop;
- expr = entry->expr;
-
- if (TREE_CODE (expr) != SSA_NAME)
- return 1;
-
- new_name = expr;
- def_in_loop_p = defined_in_loop_p (new_name, loop);
- old_close_phi = alive_after_loop (entry->old_name);
- used_outside_p = (old_close_phi != NULL);
- need_close_phi_p = (def_in_loop_p && used_outside_p
- && close_phi_not_yet_inserted_p (loop, new_name));
-
- /* Insert a loop close phi node. */
- if (need_close_phi_p)
- {
- basic_block bb = single_exit (loop)->dest;
- gimple phi = create_phi_node (new_name, bb);
- tree new_res = create_new_def_for (gimple_phi_result (phi), phi,
- gimple_phi_result_ptr (phi));
-
- add_phi_arg (phi, new_name, single_pred_edge (bb), UNKNOWN_LOCATION);
- VEC_safe_push (rename_map_elt, heap, a->new_renames,
- new_rename_map_elt (gimple_phi_result (old_close_phi),
- new_res));
- }
-
- /* Remove the old rename from the map. */
- if (def_in_loop_p && *slot)
- {
- free (*slot);
- *slot = NULL;
- }
-
- return 1;
-}
-
-/* Traverses MAP and removes from it all the tuples (OLD, NEW) where
- NEW is defined in LOOP. Inserts on the exit of LOOP the close phi
- node "RES = phi (NEW)" corresponding to "OLD_RES = phi (OLD)" in
- the original code. Inserts in MAP the tuple (OLD_RES, RES). */
-
-void
-insert_loop_close_phis (htab_t map, loop_p loop)
-{
- int i;
- struct alep a;
- rename_map_elt elt;
-
- a.loop = loop;
- a.new_renames = VEC_alloc (rename_map_elt, heap, 3);
- update_ssa (TODO_update_ssa);
- htab_traverse (map, add_loop_exit_phis, &a);
- update_ssa (TODO_update_ssa);
-
- for (i = 0; VEC_iterate (rename_map_elt, a.new_renames, i, elt); i++)
- {
- set_rename (map, elt->old_name, elt->expr);
- free (elt);
- }
-
- VEC_free (rename_map_elt, heap, a.new_renames);
-}
-
-/* Helper structure for htab_traverse in insert_guard_phis. */
-
-struct igp {
- basic_block bb;
- edge true_edge, false_edge;
- htab_t before_guard;
-};
-
-/* Return the default name that is before the guard. */
-
-static tree
-default_before_guard (htab_t before_guard, tree old_name)
-{
- tree res = get_rename (before_guard, old_name);
-
- if (res == old_name)
- {
- if (is_gimple_reg (res))
- return fold_convert (TREE_TYPE (res), integer_zero_node);
- return gimple_default_def (cfun, SSA_NAME_VAR (res));