diff options
author | Diego Novillo <dnovillo@redhat.com> | 2005-04-09 01:37:54 +0000 |
---|---|---|
committer | Diego Novillo <dnovillo@gcc.gnu.org> | 2005-04-08 21:37:54 -0400 |
commit | 0bca51f080dfff5e943b1f1775d874a73bbc441a (patch) | |
tree | 271cffc60ad6d382edfc47d6caf78c333019cfca /gcc/tree-ssa-copy.c | |
parent | 45f9480e292e10312914733dd31d1030f4e089af (diff) | |
download | gcc-0bca51f080dfff5e943b1f1775d874a73bbc441a.zip gcc-0bca51f080dfff5e943b1f1775d874a73bbc441a.tar.gz gcc-0bca51f080dfff5e943b1f1775d874a73bbc441a.tar.bz2 |
backport: copy-prop, incremental SSA updating of FUD chains and newly exposed symbols.
Merge from tree-cleanup-branch: VRP, store CCP, store
copy-prop, incremental SSA updating of FUD chains and
newly exposed symbols.
* Makefile.in (tree-ssa-copy.o): Depend on tree-ssa-propagate.h.
(OBJS-common): Add tree-vrp.o.
(tree-vrp.o): New rule.
* basic-block.h (nearest_common_dominator_for_set): Declare.
* common.opt (ftree-store-ccp): New flag.
(ftree-copy-prop): New flag.
(ftree-vrp): New flag.
(ftree-store-copy-prop): New flag.
* dominance.c (nearest_common_dominator_for_set): New.
* domwalk.c (walk_dominator_tree): Only traverse
statements in blocks marked in walk_data->interesting_blocks.
* domwalk.h (struct dom_walk_data): Add field interesting_blocks.
* fold-const.c (fold): Handle ASSERT_EXPR.
* opts.c (decode_options): Set flag_tree_copy_prop at -O1.
Set flag_tree_store_ccp, flag_tree_store_copy_prop and
flag_tree_vrp at -O2.
* timevar.def (TV_TREE_VRP): Define.
(TV_TREE_COPY_PROP): Define.
(TV_TREE_STORE_COPY_PROP): Define.
(TV_TREE_SSA_INCREMENTAL): Define.
(TV_TREE_STORE_CCP): Define.
* tree-cfg.c (tree_can_merge_blocks_p): Remove reference
to kill_redundant_phi_nodes from comment.
(verify_expr): Handle ASSERT_EXPR.
* tree-dfa.c (mark_new_vars_to_rename): Remove second
argument. Update all users.
(mark_call_clobbered_vars_to_rename): Remove. Update all
users.
* tree-flow-inline.h (unmodifiable_var_p): New.
* tree-flow.h (enum value_range_type): Declare.
(struct value_range_def): Declare.
(value_range): Declare.
(remove_all_phi_nodes_for): Remove. Update all users.
(find_phi_node_for): Declare.
(add_type_alias): Declare.
(count_uses_and_derefs): Declare.
(kill_redundant_phi_nodes): Remove.
(rewrite_into_ssa): Remove.
(rewrite_def_def_chains): Remove.
(update_ssa, register_new_name_mapping, create_new_def_for,
need_ssa_update_p, name_registered_for_update_p,
release_ssa_name_after_update_ssa, dump_repl_tbl,
debug_repl_tbl, dump_names_replaced_by,
debug_names_replaced_by, mark_sym_for_renaming,
mark_set_for_renaming, get_current_def, set_current_def,
get_value_range, dump_value_range, debug_value_range,
dump_all_value_ranges, debug_all_value_ranges,
expr_computes_nonzero, loop_depth_of_name,
unmodifiable_var_p): Declare.
* tree-gimple.c (is_gimple_formal_tmp_rhs): Handle
ASSERT_EXPR.
* tree-into-ssa.c (block_defs_stack): Update comment.
(old_ssa_names, new_ssa_names, old_virtual_ssa_names,
syms_to_rename, names_to_release, repl_tbl,
need_to_initialize_update_ssa_p, need_to_update_vops_p,
need_to_replace_names_p): New locals.
(NAME_SETS_GROWTH_FACTOR): Define.
(struct repl_map_d): Declare.
(struct mark_def_sites_global_data): Add field
interesting_blocks.
(enum rewrite_mode): Declare.
(REGISTER_DEFS_IN_THIS_STMT): Define.
(compute_global_livein): Use last_basic_block instead of
n_basic_blocks.
(set_def_block): Remove last argument. Update all callers.
(prepare_use_operand_for_rename): Remove. Update all callers.
(prepare_def_operand_for_rename): Remove. Update all callers.
(symbol_marked_for_renaming): New.
(is_old_name): New.
(is_new_name): New.
(repl_map_hash): New.
(repl_map_eq): New.
(repl_map_free): New.
(names_replaced_by): New.
(add_to_repl_tbl): New.
(add_new_name_mapping): New.
(mark_def_sites): Assume that all the operands in the
statement are in normal form.
(find_idf): Assert that the block in the stack is valid.
(get_default_def_for): New.
(insert_phi_nodes_for): Add new argument 'update_p'.
Add documentation.
If update_p is true, add a new mapping between the LHS of
each new PHI and the name that it replaces.
(insert_phi_nodes_1): Only call find_idf if needed.
(get_reaching_def): Call get_default_def_for.
(rewrite_operand): Remove.
(rewrite_stmt): Do nothing if REGISTER_DEFS_IN_THIS_STMT
and REWRITE_THIS_STMT are false.
Assume that all the operands in the statement are in
normal form.
(rewrite_add_phi_arguments): Don't use PHI_REWRITTEN.
(rewrite_virtual_phi_arguments): Remove.
(invalidate_name_tags): Remove.
(register_new_update_single, register_new_update_set,
rewrite_update_init_block, replace_use,
rewrite_update_fini_block, rewrite_update_stmt,
rewrite_update_phi_arguments): New.
rewrite_blocks): Remove argument 'fix_virtual_phis'.
Add arguments 'entry', 'what' and 'blocks'.
Initialize the dominator walker according to 'what' and
'blocks'.
Start the dominator walk at 'entry'.
(mark_def_site_blocks): Add argument 'interesting_blocks'.
Use it to configure the dominator walker.
(rewrite_into_ssa): Remove argument 'all'.
Make internal.
(rewrite_all_into_ssa): Remove.
(rewrite_def_def_chains): Remove.
(mark_def_interesting, mark_use_interesting,
prepare_phi_args_for_update, prepare_block_for_update,
prepare_def_site_for, prepare_def_sites,
dump_names_replaced_by, debug_names_replaced_by,
dump_repl_tbl, debug_repl_tbl, init_update_ssa,
delete_update_ssa, create_new_def_for,
register_new_name_mapping, mark_sym_for_renaming,
mark_set_for_renaming, need_ssa_update_p,
name_registered_for_update_p, ssa_names_to_replace,
release_ssa_name_after_update_ssa,
insert_updated_phi_nodes_for, update_ssa): New.
* tree-loop-linear.c (linear_transform_loops): Call
update_ssa instead of rewrite_into_ssa.
* tree-optimize.c (vars_to_rename): Remove.
Update all users.
(init_tree_optimization_passes): Replace
pass_redundant_phi with pass_copy_prop.
Add pass_vrp.
Replace pass_ccp with pass_store_ccp.
Add pass_store_copy_prop after pass_store_ccp.
(execute_todo): If the TODO_ flags don't include updating
the SSA form, assert that it does not need to be updated.
Call update_ssa instead of rewrite_into_ssa and
rewrite_def_def_chains.
If TODO_verify_loops is set, call verify_loop_closed_ssa.
(tree_rest_of_compilation):
* tree-pass.h (TODO_dump_func, TODO_ggc_collect,
TODO_verify_ssa, TODO_verify_flow, TODO_verify_stmts,
TODO_cleanup_cfg): Renumber.
(TODO_verify_loops, TODO_update_ssa,
TODO_update_ssa_no_phi, TODO_update_ssa_full_phi,
TODO_update_ssa_only_virtuals): Define.
(pass_copy_prop, pass_store_ccp, pass_store_copy_prop, pass_vrp):
Declare.
* tree-phinodes.c (make_phi_node): Update documentation.
(remove_all_phi_nodes_for): Remove.
(find_phi_node_for): New.
* tree-pretty-print.c (dump_generic_node): Handle ASSERT_EXPR.
* tree-scalar-evolution.c (follow_ssa_edge_in_rhs): Likewise.
(interpret_rhs_modify_expr): Likewise.
* tree-sra.c (decide_instantiations): Mark all symbols in
SRA_CANDIDATES for renaming.
(mark_all_v_defs_1): Rename from mark_all_v_defs.
(mark_all_v_defs): New function. Update all users to call it
with the whole list of scalarized statements, not just the
first one.
* tree-ssa-alias.c (count_ptr_derefs): Make extern.
(compute_flow_insensitive_aliasing): If the tag is
unmodifiable and the variable isn't or vice-versa, don't
make them alias of each other.
(setup_pointers_and_addressables): If the type tag for
VAR is about to change, mark the old one for renaming.
(add_type_alias): New.
* tree-ssa-ccp.c: Document SSA-CCP and STORE-CCP.
(ccp_lattice_t): Rename from latticevalue.
(value): Remove. Update all users.
(const_val): New local variable.
(do_store_ccp): New local variable.
(dump_lattice_value): Handle UNINITIALIZED.
(debug_lattice_value): New.
(get_default_value): Re-write.
(set_lattice_value): Re-write.
(def_to_varying): Remove. Update all users.
(likely_value): Return VARYING for statements that make
stores when STORE_CCP is false.
Return VARYING for any statement other than MODIFY_EXPR,
COND_EXPR and SWITCH_EXPR.
(ccp_initialize): Re-write.
(replace_uses_in, replace_vuse_in, substitute_and_fold):
Move to tree-ssa-propagate.c.
(ccp_lattice_meet): Handle memory stores when
DO_STORE_CCP is true.
(ccp_visit_phi_node): Likewise.
(ccp_fold): Likewise.
(evaluate_stmt): Likewise.
(visit_assignment): Likewise.
(ccp_visit_stmt): Likewise.
(execute_ssa_ccp): Add argument 'store_ccp'. Copy it
into DO_STORE_CCP.
(do_ssa_ccp): New.
(pass_ccp): Use it.
(do_ssa_store_ccp): New.
(gate_store_ccp): New.
(pass_store_ccp): Declare.
* tree-ssa-copy.c: Include tree-ssa-propagate.h.
(may_propagate_copy): Reformat.
Don't abort if ORIG is a virtual and DEST isn't.
If NEW does not have alias information but DEST does,
copy it.
(copy_of, cached_last_copy_of, do_store_copy_prop, enum
copy_prop_kind, which_copy_prop): Declare.
(stmt_may_generate_copy, get_copy_of_val,
get_last_copy_of, set_copy_of_val, dump_copy_of,
copy_prop_visit_assignment, copy_prop_visit_cond_stmt,
copy_prop_visit_stmt, copy_prop_visit_phi_node,
init_copy_prop, fini_copy_prop, execute_copy_prop,
gate_copy_prop, do_copy_prop, gate_store_copy_prop,
store_copy_prop): New.
(pass_copy_prop, pass_store_copy_prop): Declare.
* tree-ssa-dom.c (struct opt_stats_d): Add fields
'num_const_prop' and 'num_copy_prop'.
(cprop_operand): Update them.
(dump_dominator_optimization_stats): Dump them.
(tree_ssa_dominator_optimize): Call update_ssa instead of
rewrite_into_ssa.
(loop_depth_of_name): Declare extern.
(simplify_cond_and_lookup_avail_expr): Guard against NULL
values for LOW or HIGH.
(cprop_into_successor_phis): Only propagate if NEW != ORIG.
(record_equivalences_from_stmt): Call expr_computes_nonzero.
(cprop_operand): Only propagate if VAL != OP.
* tree-ssa-dse.c (dse_optimize_stmt): Mark symbols in removed
statement for renaming.
* tree-ssa-loop-im.c (move_computations): Call update_ssa.
* tree-ssa-loop-ivopts.c (rewrite_address_base): Call
add_type_alias if necessary.
Call mark_new_vars_to_rename.
(tree_ssa_iv_optimize): If new symbols need to be renamed,
mark every statement updated, call update_ssa and
rewrite_into_loop_closed_ssa.
* tree-ssa-loop-manip.c (add_exit_phis): Do not remove DEF_BB
from LIVEIN if VAR is a virtual.
* tree-ssa-loop.c (tree_loop_optimizer_init): Call update_ssa.
* tree-ssa-operands.c (get_expr_operands): Handle ASSERT_EXPR.
(get_call_expr_operands): Reformat statement.
(add_stmt_operand): Don't create V_MAY_DEFs for read-only
symbols.
* tree-ssa-propagate.c (ssa_prop_init): Initialize
SSA_NAME_VALUE for every name.
(first_vdef, stmt_makes_single_load, stmt_makes_single_store,
get_value_loaded_by): New.
(replace_uses_in, replace_vuses_in, replace_phi_args_in,
substitute_and_fold): Move from tree-ssa-ccp.c.
* tree-ssa-propagate.h (struct prop_value_d, prop_value_t,
first_vdef, stmt_makes_single_load, stmt_makes_single_store,
get_value_loaded_by, replace_uses_in, substitute_and_fold):
Declare.
* tree-ssa.c (verify_use): Fix error message.
(propagate_into_addr, replace_immediate_uses, get_eq_name,
check_phi_redundancy, kill_redundant_phi_nodes,
pass_redundant_phi): Remove. Update all users.
* tree-vect-transform.c (vect_create_data_ref_ptr): Call
add_type_alias, if necessary.
* tree-vectorizer.h (struct _stmt_vect_info): Update
documentation for field 'memtag'.
* tree-vrp.c: New file.
* tree.def (ASSERT_EXPR): Define.
* tree.h (ASSERT_EXPR_VAR): Define.
(ASSERT_EXPR_COND): Define.
(SSA_NAME_VALUE_RANGE): Define.
(struct tree_ssa_name): Add field 'value_range'.
(PHI_REWRITTEN): Remove.
(struct tree_phi_node): Remove field 'rewritten'.
* doc/invoke.texi (-fdump-tree-storeccp, -ftree-copy-prop,
-ftree-store-copy-prop): Document.
* doc/tree-ssa.texi: Remove broken link to McCAT's compiler.
Document usage of update_ssa.
testsuite/ChangeLog
* g++.dg/tree-ssa/pr18178.C: New test.
* gcc.c-torture/execute/20030216-1.x: Ignore at -O1.
* gcc.c-torture/execute/20041019-1.c: New test.
* gcc.dg/tree-ssa/20041008-1.c: New test.
* gcc.dg/tree-ssa/ssa-ccp-12.c: New test.
* gcc.dg/tree-ssa/20030731-2.c: Update to use -fdump-tree-store_ccp.
* gcc.dg/tree-ssa/20030917-1.c: Likewise.
* gcc.dg/tree-ssa/20030917-3.c: Likewise.
* gcc.dg/tree-ssa/20040721-1.c: Likewise.
* gcc.dg/tree-ssa/ssa-ccp-1.c: Likewise.
* gcc.dg/tree-ssa/ssa-ccp-2.c: Likewise.
* gcc.dg/tree-ssa/ssa-ccp-3.c: Likewise.
* gcc.dg/tree-ssa/ssa-ccp-7.c: Likewise.
* gcc.dg/tree-ssa/ssa-ccp-9.c: Likewise.
From-SVN: r97884
Diffstat (limited to 'gcc/tree-ssa-copy.c')
-rw-r--r-- | gcc/tree-ssa-copy.c | 851 |
1 files changed, 811 insertions, 40 deletions
diff --git a/gcc/tree-ssa-copy.c b/gcc/tree-ssa-copy.c index 8350047..baca99c 100644 --- a/gcc/tree-ssa-copy.c +++ b/gcc/tree-ssa-copy.c @@ -1,4 +1,4 @@ -/* Const/copy propagation and SSA_NAME replacement support routines. +/* Copy propagation and SSA_NAME replacement support routines. Copyright (C) 2004, 2005 Free Software Foundation, Inc. This file is part of GCC. @@ -37,11 +37,13 @@ Boston, MA 02111-1307, USA. */ #include "tree-dump.h" #include "tree-flow.h" #include "tree-pass.h" +#include "tree-ssa-propagate.h" #include "langhooks.h" -/* This file provides a handful of interfaces for performing const/copy - propagation and simple expression replacement which keep variable - annotations up-to-date. +/* This file implements the copy propagation pass and provides a + handful of interfaces for performing const/copy propagation and + simple expression replacement which keep variable annotations + up-to-date. We require that for any copy operation where the RHS and LHS have a non-null memory tag the memory tag be the same. It is OK @@ -54,7 +56,6 @@ Boston, MA 02111-1307, USA. */ replacements of one SSA_NAME with a different SSA_NAME to use the APIs defined in this file. */ - /* Return true if we may propagate ORIG into DEST, false otherwise. */ bool @@ -103,8 +104,10 @@ may_propagate_copy (tree dest, tree orig) I think that GIMPLE should emit the appropriate type-casts. For the time being, blocking copy-propagation in these cases is the safe thing to do. */ - if (TREE_CODE (dest) == SSA_NAME && TREE_CODE (orig) == SSA_NAME - && POINTER_TYPE_P (type_d) && POINTER_TYPE_P (type_o)) + if (TREE_CODE (dest) == SSA_NAME + && TREE_CODE (orig) == SSA_NAME + && POINTER_TYPE_P (type_d) + && POINTER_TYPE_P (type_o)) { tree mt_dest = var_ann (SSA_NAME_VAR (dest))->type_mem_tag; tree mt_orig = var_ann (SSA_NAME_VAR (orig))->type_mem_tag; @@ -123,17 +126,9 @@ may_propagate_copy (tree dest, tree orig) { /* If both operands are SSA_NAMEs referring to virtual operands, then we can always propagate. */ - if (TREE_CODE (orig) == SSA_NAME) - { - if (!is_gimple_reg (orig)) - return true; - -#ifdef ENABLE_CHECKING - /* If we have one real and one virtual operand, then something has - gone terribly wrong. */ - gcc_assert (!is_gimple_reg (orig)); -#endif - } + if (TREE_CODE (orig) == SSA_NAME + && !is_gimple_reg (orig)) + return true; /* We have a "copy" from something like a constant into a virtual operand. Reject these. */ @@ -202,30 +197,33 @@ merge_alias_info (tree orig, tree new) else gcc_assert (new_ann->type_mem_tag == orig_ann->type_mem_tag); -#if defined ENABLE_CHECKING - { - struct ptr_info_def *orig_ptr_info = SSA_NAME_PTR_INFO (orig); - struct ptr_info_def *new_ptr_info = SSA_NAME_PTR_INFO (new); - - if (orig_ptr_info - && new_ptr_info - && orig_ptr_info->name_mem_tag - && new_ptr_info->name_mem_tag - && orig_ptr_info->pt_vars - && new_ptr_info->pt_vars) + /* Synchronize the name tags. If NEW did not have a name tag, get + it from ORIG. This happens when NEW is a compiler generated + temporary which still hasn't had its points-to information filled + in. */ + if (SSA_NAME_PTR_INFO (orig)) { - /* Note that pointer NEW may actually have a different set of - pointed-to variables. However, since NEW is being - copy-propagated into ORIG, it must always be true that the - pointed-to set for pointer NEW is the same, or a subset, of - the pointed-to set for pointer ORIG. If this isn't the case, - we shouldn't have been able to do the propagation of NEW into - ORIG. */ - gcc_assert (bitmap_intersect_p (new_ptr_info->pt_vars, - orig_ptr_info->pt_vars)); + struct ptr_info_def *orig_ptr_info = SSA_NAME_PTR_INFO (orig); + struct ptr_info_def *new_ptr_info = SSA_NAME_PTR_INFO (new); + + if (new_ptr_info == NULL) + duplicate_ssa_name_ptr_info (new, orig_ptr_info); + else if (orig_ptr_info->name_mem_tag + && new_ptr_info->name_mem_tag + && orig_ptr_info->pt_vars + && new_ptr_info->pt_vars) + { + /* Note that pointer NEW may actually have a different set + of pointed-to variables. However, since NEW is being + copy-propagated into ORIG, it must always be true that + the pointed-to set for pointer NEW is the same, or a + subset, of the pointed-to set for pointer ORIG. If this + isn't the case, we shouldn't have been able to do the + propagation of NEW into ORIG. */ + gcc_assert (bitmap_intersect_p (new_ptr_info->pt_vars, + orig_ptr_info->pt_vars)); + } } - } -#endif } @@ -310,3 +308,776 @@ replace_exp (use_operand_p op_p, tree val) { replace_exp_1 (op_p, val, false); } + + +/*--------------------------------------------------------------------------- + Copy propagation +---------------------------------------------------------------------------*/ +/* During propagation, we keep chains of variables that are copies of + one another. If variable X_i is a copy of X_j and X_j is a copy of + X_k, COPY_OF will contain: + + COPY_OF[i].VALUE = X_j + COPY_OF[j].VALUE = X_k + COPY_OF[k].VALUE = X_k + + After propagation, the copy-of value for each variable X_i is + converted into the final value by walking the copy-of chains and + updating COPY_OF[i].VALUE to be the last element of the chain. */ +static prop_value_t *copy_of; + +/* Used in set_copy_of_val to determine if the last link of a copy-of + chain has changed. */ +static tree *cached_last_copy_of; + +/* True if we are doing copy propagation on loads and stores. */ +static bool do_store_copy_prop; + + +/* Return true if this statement may generate a useful copy. */ + +static bool +stmt_may_generate_copy (tree stmt) +{ + tree lhs, rhs; + stmt_ann_t ann; + + if (TREE_CODE (stmt) == PHI_NODE) + return !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (stmt)); + + if (TREE_CODE (stmt) != MODIFY_EXPR) + return false; + + lhs = TREE_OPERAND (stmt, 0); + rhs = TREE_OPERAND (stmt, 1); + ann = stmt_ann (stmt); + + /* If the statement has volatile operands, it won't generate a + useful copy. */ + if (ann->has_volatile_ops) + return false; + + /* If we are not doing store copy-prop, statements with loads and/or + stores will never generate a useful copy. */ + if (!do_store_copy_prop + && (NUM_VUSES (VUSE_OPS (ann)) > 0 + || NUM_V_MAY_DEFS (V_MAY_DEF_OPS (ann)) > 0 + || NUM_V_MUST_DEFS (V_MUST_DEF_OPS (ann)) > 0)) + return false; + + /* Otherwise, the only statements that generate useful copies are + assignments whose RHS is just an SSA name that doesn't flow + through abnormal edges. */ + return TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs); +} + + +/* Return the copy-of value for VAR. */ + +static inline prop_value_t * +get_copy_of_val (tree var) +{ + prop_value_t *val = ©_of[SSA_NAME_VERSION (var)]; + + if (val->value == NULL_TREE + && !stmt_may_generate_copy (SSA_NAME_DEF_STMT (var))) + { + /* If the variable will never generate a useful copy relation, + make it its own copy. */ + val->value = var; + val->mem_ref = NULL_TREE; + } + + return val; +} + + +/* Return last link in the copy-of chain for VAR. */ + +static tree +get_last_copy_of (tree var) +{ + tree last; + int i; + + /* Traverse COPY_OF starting at VAR until we get to the last + link in the chain. Since it is possible to have cycles in PHI + nodes, the copy-of chain may also contain cycles. + + To avoid infinite loops and to avoid traversing lengthy copy-of + chains, we artificially limit the maximum number of chains we are + willing to traverse. + + The value 5 was taken from a compiler and runtime library + bootstrap and a mixture of C and C++ code from various sources. + More than 82% of all copy-of chains were shorter than 5 links. */ +#define LIMIT 5 + + last = var; + for (i = 0; i < LIMIT; i++) + { + tree copy = copy_of[SSA_NAME_VERSION (last)].value; + if (copy == NULL_TREE || copy == last) + break; + last = copy; + } + + /* If we have reached the limit, then we are either in a copy-of + cycle or the copy-of chain is too long. In this case, just + return VAR so that it is not considered a copy of anything. */ + return (i < LIMIT ? last : var); +} + + +/* Set FIRST to be the first variable in the copy-of chain for DEST. + If DEST's copy-of value or its copy-of chain have changed, return + true. + + MEM_REF is the memory reference where FIRST is stored. This is + used when DEST is a non-register and we are copy propagating loads + and stores. */ + +static inline bool +set_copy_of_val (tree dest, tree first, tree mem_ref) +{ + unsigned int dest_ver = SSA_NAME_VERSION (dest); + tree old_first, old_last, new_last; + + /* Set FIRST to be the first link in COPY_OF[DEST]. If that + changed, return true. */ + old_first = copy_of[dest_ver].value; + copy_of[dest_ver].value = first; + copy_of[dest_ver].mem_ref = mem_ref; + + if (old_first != first) + return true; + + /* If FIRST and OLD_FIRST are the same, we need to check whether the + copy-of chain starting at FIRST ends in a different variable. If + the copy-of chain starting at FIRST ends up in a different + variable than the last cached value we had for DEST, then return + true because DEST is now a copy of a different variable. + + This test is necessary because even though the first link in the + copy-of chain may not have changed, if any of the variables in + the copy-of chain changed its final value, DEST will now be the + copy of a different variable, so we have to do another round of + propagation for everything that depends on DEST. */ + old_last = cached_last_copy_of[dest_ver]; + new_last = get_last_copy_of (dest); + cached_last_copy_of[dest_ver] = new_last; + + return (old_last != new_last); +} + + +/* Dump the copy-of value for variable VAR to DUMP_FILE. */ + +static void +dump_copy_of (FILE *dump_file, tree var) +{ + tree val; + + print_generic_expr (dump_file, var, dump_flags); + + if (TREE_CODE (var) != SSA_NAME) + return; + + fprintf (dump_file, " copy-of chain: "); + + val = var; + print_generic_expr (dump_file, val, 0); + fprintf (dump_file, " "); + while (copy_of[SSA_NAME_VERSION (val)].value + && copy_of[SSA_NAME_VERSION (val)].value != val) + { + fprintf (dump_file, "-> "); + val = copy_of[SSA_NAME_VERSION (val)].value; + print_generic_expr (dump_file, val, 0); + fprintf (dump_file, " "); + } + + val = get_copy_of_val (var)->value; + if (val == NULL_TREE) + fprintf (dump_file, "[UNDEFINED]"); + else if (val != var) + fprintf (dump_file, "[COPY]"); + else + fprintf (dump_file, "[NOT A COPY]"); +} + + +/* Evaluate the RHS of STMT. If it produces a valid copy, set the LHS + value and store the LHS into *RESULT_P. If STMT generates more + than one name (i.e., STMT is an aliased store), it is enough to + store the first name in the V_MAY_DEF list into *RESULT_P. After + all, the names generated will be VUSEd in the same statements. */ + +static enum ssa_prop_result +copy_prop_visit_assignment (tree stmt, tree *result_p) +{ + tree lhs, rhs; + prop_value_t *rhs_val; + + lhs = TREE_OPERAND (stmt, 0); + rhs = TREE_OPERAND (stmt, 1); + + gcc_assert (TREE_CODE (rhs) == SSA_NAME); + + rhs_val = get_copy_of_val (rhs); + + if (TREE_CODE (lhs) == SSA_NAME) + { + /* Straight copy between two SSA names. First, make sure that + we can propagate the RHS into uses of LHS. */ + if (!may_propagate_copy (lhs, rhs)) + return SSA_PROP_VARYING; + + /* Avoid copy propagation from an inner into an outer loop. + Otherwise, this may move loop variant variables outside of + their loops and prevent coalescing opportunities. If the + value was loop invariant, it will be hoisted by LICM and + exposed for copy propagation. */ + if (loop_depth_of_name (rhs) > loop_depth_of_name (lhs)) + return SSA_PROP_VARYING; + + /* Notice that in the case of assignments, we make the LHS be a + copy of RHS's value, not of RHS itself. This avoids keeping + unnecessary copy-of chains (assignments cannot be in a cycle + like PHI nodes), speeding up the propagation process. + This is different from what we do in copy_prop_visit_phi_node. + In those cases, we are interested in the copy-of chains. */ + *result_p = lhs; + if (set_copy_of_val (*result_p, rhs_val->value, rhs_val->mem_ref)) + return SSA_PROP_INTERESTING; + else + return SSA_PROP_NOT_INTERESTING; + } + else if (stmt_makes_single_store (stmt)) + { + /* Otherwise, set the names in V_MAY_DEF/V_MUST_DEF operands + to be a copy of RHS. */ + ssa_op_iter i; + tree vdef; + bool changed; + + /* This should only be executed when doing store copy-prop. */ + gcc_assert (do_store_copy_prop); + + /* Set the value of every VDEF to RHS_VAL. */ + changed = false; + FOR_EACH_SSA_TREE_OPERAND (vdef, stmt, i, SSA_OP_VIRTUAL_DEFS) + changed |= set_copy_of_val (vdef, rhs_val->value, lhs); + + /* Note that for propagation purposes, we are only interested in + visiting statements that load the exact same memory reference + stored here. Those statements will have the exact same list + of virtual uses, so it is enough to set the output of this + statement to be its first virtual definition. */ + *result_p = first_vdef (stmt); + + if (changed) + return SSA_PROP_INTERESTING; + else + return SSA_PROP_NOT_INTERESTING; + } + + + return SSA_PROP_VARYING; +} + + +/* Visit the COND_EXPR STMT. Return SSA_PROP_INTERESTING + if it can determine which edge will be taken. Otherwise, return + SSA_PROP_VARYING. */ + +static enum ssa_prop_result +copy_prop_visit_cond_stmt (tree stmt, edge *taken_edge_p) +{ + enum ssa_prop_result retval; + tree cond; + use_optype uses; + + cond = COND_EXPR_COND (stmt); + uses = STMT_USE_OPS (stmt); + retval = SSA_PROP_VARYING; + + /* The only conditionals that we may be able to compute statically + are predicates involving at least one SSA_NAME. */ + if (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison + && NUM_USES (uses) >= 1) + { + unsigned i; + tree *orig; + + /* Save the original operands. */ + orig = xmalloc (sizeof (tree) * NUM_USES (uses)); + for (i = 0; i < NUM_USES (uses); i++) + { + orig[i] = USE_OP (uses, i); + SET_USE_OP (uses, i, get_last_copy_of (USE_OP (uses, i))); + } + + /* See if we can determine the predicate's value. */ + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fprintf (dump_file, "Trying to determine truth value of "); + fprintf (dump_file, "predicate "); + print_generic_stmt (dump_file, cond, 0); + } + + *taken_edge_p = find_taken_edge (bb_for_stmt (stmt), cond); + if (*taken_edge_p) + retval = SSA_PROP_INTERESTING; + + /* Restore the original operands. */ + for (i = 0; i < NUM_USES (uses); i++) + SET_USE_OP (uses, i, orig[i]); + free (orig); + } + + if (dump_file && (dump_flags & TDF_DETAILS) && *taken_edge_p) + fprintf (dump_file, "\nConditional will always take edge %d->%d\n", + (*taken_edge_p)->src->index, (*taken_edge_p)->dest->index); + + return retval; +} + + +/* Evaluate statement STMT. If the statement produces a new output + value, return SSA_PROP_INTERESTING and store the SSA_NAME holding + the new value in *RESULT_P. + + If STMT is a conditional branch and we can determine its truth + value, set *TAKEN_EDGE_P accordingly. + + If the new value produced by STMT is varying, return + SSA_PROP_VARYING. */ + +static enum ssa_prop_result +copy_prop_visit_stmt (tree stmt, edge *taken_edge_p, tree *result_p) +{ + stmt_ann_t ann; + enum ssa_prop_result retval; + + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fprintf (dump_file, "\nVisiting statement:\n"); + print_generic_stmt (dump_file, stmt, dump_flags); + fprintf (dump_file, "\n"); + } + + ann = stmt_ann (stmt); + + if (TREE_CODE (stmt) == MODIFY_EXPR + && TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME + && (do_store_copy_prop + || TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME)) + { + /* If the statement is a copy assignment, evaluate its RHS to + see if the lattice value of its output has changed. */ + retval = copy_prop_visit_assignment (stmt, result_p); + } + else if (TREE_CODE (stmt) == COND_EXPR) + { + /* See if we can determine which edge goes out of a conditional + jump. */ + retval = copy_prop_visit_cond_stmt (stmt, taken_edge_p); + } + else + retval = SSA_PROP_VARYING; + + if (retval == SSA_PROP_VARYING) + { + tree def; + ssa_op_iter i; + + /* Any other kind of statement is not interesting for constant + propagation and, therefore, not worth simulating. */ + if (dump_file && (dump_flags & TDF_DETAILS)) + fprintf (dump_file, "No interesting values produced.\n"); + + /* The assignment is not a copy operation. Don't visit this + statement again and mark all the definitions in the statement + to be copies of nothing. */ + FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_ALL_DEFS) + set_copy_of_val (def, def, NULL_TREE); + } + + return retval; +} + + +/* Visit PHI node PHI. If all the arguments produce the same value, + set it to be the value of the LHS of PHI. */ + +static enum ssa_prop_result +copy_prop_visit_phi_node (tree phi) +{ + enum ssa_prop_result retval; + int i; + tree lhs; + prop_value_t phi_val = { 0, NULL_TREE, NULL_TREE }; + + lhs = PHI_RESULT (phi); + + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fprintf (dump_file, "\nVisiting PHI node: "); + print_generic_expr (dump_file, phi, dump_flags); + fprintf (dump_file, "\n\n"); + } + + for (i = 0; i < PHI_NUM_ARGS (phi); i++) + { + prop_value_t *arg_val; + tree arg = PHI_ARG_DEF (phi, i); + edge e = PHI_ARG_EDGE (phi, i); + + /* We don't care about values flowing through non-executable + edges. */ + if (!(e->flags & EDGE_EXECUTABLE)) + continue; + + /* Constants in the argument list never generate a useful copy. + Similarly, names that flow through abnormal edges cannot be + used to derive copies. */ + if (TREE_CODE (arg) != SSA_NAME || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg)) + { + phi_val.value = lhs; + break; + } + + /* Avoid copy propagation from an inner into an outer loop. + Otherwise, this may move loop variant variables outside of + their loops and prevent coalescing opportunities. If the + value was loop invariant, it will be hoisted by LICM and + exposed for copy propagation. */ + if (loop_depth_of_name (arg) > loop_depth_of_name (lhs)) + { + phi_val.value = lhs; + break; + } + + /* If the LHS appears in the argument list, ignore it. It is + irrelevant as a copy. */ + if (arg == lhs || get_last_copy_of (arg) == lhs) + continue; + + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fprintf (dump_file, "\tArgument #%d: ", i); + dump_copy_of (dump_file, arg); + fprintf (dump_file, "\n"); + } + + arg_val = get_copy_of_val (arg); + + /* If the LHS didn't have a value yet, make it a copy of the + first argument we find. Notice that while we make the LHS be + a copy of the argument itself, we take the memory reference + from the argument's value so that we can compare it to the + memory reference of all the other arguments. */ + if (phi_val.value == NULL_TREE) + { + phi_val.value = arg; + phi_val.mem_ref = arg_val->mem_ref; + continue; + } + + /* If PHI_VAL and ARG don't have a common copy-of chain, then + this PHI node cannot be a copy operation. Also, if we are + copy propagating stores and these two arguments came from + different memory references, they cannot be considered + copies. */ + if (get_last_copy_of (phi_val.value) != get_last_copy_of (arg) + || (do_store_copy_prop + && phi_val.mem_ref + && arg_val->mem_ref + && simple_cst_equal (phi_val.mem_ref, arg_val->mem_ref) != 1)) + { + phi_val.value = lhs; + break; + } + } + + if (phi_val.value && set_copy_of_val (lhs, phi_val.value, phi_val.mem_ref)) + retval = (phi_val.value != lhs) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING; + else + retval = SSA_PROP_NOT_INTERESTING; + + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fprintf (dump_file, "\nPHI node "); + dump_copy_of (dump_file, lhs); + fprintf (dump_file, "\nTelling the propagator to "); + if (retval == SSA_PROP_INTERESTING) + fprintf (dump_file, "add SSA edges out of this PHI and continue."); + else if (retval == SSA_PROP_VARYING) + fprintf (dump_file, "add SSA edges out of this PHI and never visit again."); + else + fprintf (dump_file, "do nothing with SSA edges and keep iterating."); + fprintf (dump_file, "\n\n"); + } + + return retval; +} + + +/* Initialize structures used for copy propagation. */ + +static void +init_copy_prop (void) +{ + basic_block bb; + + copy_of = xmalloc (num_ssa_names * sizeof (*copy_of)); + memset (copy_of, 0, num_ssa_names * sizeof (*copy_of)); + + cached_last_copy_of = xmalloc (num_ssa_names * sizeof (*cached_last_copy_of)); + memset (cached_last_copy_of, 0, num_ssa_names * sizeof (*cached_last_copy_of)); + + FOR_EACH_BB (bb) + { + block_stmt_iterator si; + tree phi; + + for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si)) + { + tree stmt = bsi_stmt (si); + + /* The only statements that we care about are those that may + generate useful copies. We also need to mark conditional + jumps so that their outgoing edges are added to the work + lists of the propagator. */ + if (stmt_ends_bb_p (stmt)) + DONT_SIMULATE_AGAIN (stmt) = false; + else if (stmt_may_generate_copy (stmt)) + DONT_SIMULATE_AGAIN (stmt) = false; + else + { + tree def; + ssa_op_iter iter; + + /* No need to simulate this statement anymore. */ + DONT_SIMULATE_AGAIN (stmt) = true; + + /* Mark all the outputs of this statement as not being + the copy of anything. */ + FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS) + set_copy_of_val (def, def, NULL_TREE); + } + } + + for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) + DONT_SIMULATE_AGAIN (phi) = false; + } +} + + +/* Deallocate memory used in copy propagation and do final + substitution. */ + +static void +fini_copy_prop (void) +{ + size_t i; + + /* Set the final copy-of value for each variable by traversing the + copy-of chains. */ + for (i = 1; i < num_ssa_names; i++) + { + tree var = ssa_name (i); + if (var && copy_of[i].value && copy_of[i].value != var) + copy_of[i].value = get_last_copy_of (var); + } + + substitute_and_fold (copy_of); + + free (copy_of); +} + + +/* Main entry point to the copy propagator. The algorithm propagates + the value COPY-OF using ssa_propagate. For every variable X_i, + COPY-OF(X_i) indicates which variable is X_i created from. The + following example shows how the algorithm proceeds at a high level: + + 1 a_24 = x_1 + 2 a_2 = PHI <a_24, x_1> + 3 a_5 = PHI <a_2> + 4 x_1 = PHI <x_298, a_5, a_2> + + The end result should be that a_2, a_5, a_24 and x_1 are a copy of + x_298. Propagation proceeds as follows. + + Visit #1: a_24 is copy-of x_1. Value changed. + Visit #2: a_2 is copy-of x_1. Value changed. + Visit #3: a_5 is copy-of x_1. Value changed. + Visit #4: x_1 is copy-of x_298. Value changed. + Visit #1: a_24 is copy-of x_298. Value changed. + Visit #2: a_2 is copy-of x_298. Value changed. + Visit #3: a_5 is copy-of x_298. Value changed. + Visit #4: x_1 is copy-of x_298. Stable state reached. + + When visiting PHI nodes, we only consider arguments that flow + through edges marked executable by the propagation engine. So, + when visiting statement #2 for the first time, we will only look at + the first argument (a_24) and optimistically assume that its value + is the copy of a_24 (x_1). + + The problem with this approach is that it may fail to discover copy + relations in PHI cycles. Instead of propagating copy-of + values, we actually propagate copy-of chains. For instance: + + A_3 = B_1; + C_9 = A_3; + D_4 = C_9; + X_i = D_4; + + In this code fragment, COPY-OF (X_i) = { D_4, C_9, A_3, B_1 }. + Obviously, we are only really interested in the last value of the + chain, however the propagator needs to access the copy-of chain + when visiting PHI nodes. + + To represent the copy-of chain, we use the array COPY_CHAINS, which + holds the first link in the copy-of chain for every variable. + If variable X_i is a copy of X_j, which in turn is a copy of X_k, + the array will contain: + + COPY_CHAINS[i] = X_j + COPY_CHAINS[j] = X_k + COPY_CHAINS[k] = X_k + + Keeping copy-of chains instead of copy-of values directly becomes + important when visiting PHI nodes. Suppose that we had the + following PHI cycle, such that x_52 is already considered a copy of + x_53: + + 1 x_54 = PHI <x_53, x_52> + 2 x_53 = PHI <x_898, x_54> + + Visit #1: x_54 is copy-of x_53 (because x_52 is copy-of x_53) + Visit #2: x_53 is copy-of x_898 (because x_54 is a copy of x_53, + so it is considered irrelevant + as a copy). + Visit #1: x_54 is copy-of nothing (x_53 is a copy-of x_898 and + x_52 is a copy of x_53, so + they don't match) + Visit #2: x_53 is copy-of nothing + + This problem is avoided by keeping a chain of copies, instead of + the final copy-of value. Propagation will now only keep the first + element of a variable's copy-of chain. When visiting PHI nodes, + arguments are considered equal if their copy-of chains end in the + same variable. So, as long as their copy-of chains overlap, we + know that they will be a copy of the same variable, regardless of + which variable that may be). + + Propagation would then proceed as follows (the notation a -> b + means that a is a copy-of b): + + Visit #1: x_54 = PHI <x_53, x_52> + x_53 -> x_53 + x_52 -> x_53 + Result: x_54 -> x_53. Value changed. Add SSA edges. + + Visit #1: x_53 = PHI <x_898, x_54> + x_898 -> x_898 + x_54 -> x_53 + Result: x_53 -> x_898. Value changed. Add SSA edges. + + Visit #2: x_54 = PHI <x_53, x_52> + x_53 -> x_898 + x_52 -> x_53 -> x_898 + Result: x_54 -> x_898. Value changed. Add SSA edges. + + Visit #2: x_53 = PHI <x_898, x_54> + x_898 -> x_898 + x_54 -> x_898 + Result: x_53 -> x_898. Value didn't change. Stable state + + Once the propagator stabilizes, we end up with the desired result + x_53 and x_54 are both copies of x_898. */ + +static void +execute_copy_prop (bool store_copy_prop) +{ + do_store_copy_prop = store_copy_prop; + init_copy_prop (); + ssa_propagate (copy_prop_visit_stmt, copy_prop_visit_phi_node); + fini_copy_prop (); +} + + +static bool +gate_copy_prop (void) +{ + return flag_tree_copy_prop != 0; +} + +static void +do_copy_prop (void) +{ + execute_copy_prop (false); +} + +struct tree_opt_pass pass_copy_prop = +{ + "copyprop", /* name */ + gate_copy_prop, /* gate */ + do_copy_prop, /* execute */ + NULL, /* sub */ + NULL, /* next */ + 0, /* static_pass_number */ + TV_TREE_COPY_PROP, /* tv_id */ + PROP_ssa | PROP_alias | PROP_cfg, /* properties_required */ + 0, /* properties_provided */ + 0, /* properties_destroyed */ + 0, /* todo_flags_start */ + TODO_cleanup_cfg + | TODO_dump_func + | TODO_ggc_collect + | TODO_verify_ssa + | TODO_update_ssa, /* todo_flags_finish */ + 0 /* letter */ +}; + + +static bool +gate_store_copy_prop (void) +{ + /* STORE-COPY-PROP is enabled only with -ftree-store-copy-prop, but + when -fno-tree-store-copy-prop is specified, we should run + regular COPY-PROP. That's why the pass is enabled with either + flag. */ + return flag_tree_store_copy_prop != 0 || flag_tree_copy_prop != 0; +} + +static void +store_copy_prop (void) +{ + /* If STORE-COPY-PROP is not enabled, we just run regular COPY-PROP. */ + execute_copy_prop (flag_tree_store_copy_prop != 0); +} + +struct tree_opt_pass pass_store_copy_prop = +{ + "store_copyprop", /* name */ + gate_store_copy_prop, /* gate */ + store_copy_prop, /* execute */ + NULL, /* sub */ + NULL, /* next */ + 0, /* static_pass_number */ + TV_TREE_STORE_COPY_PROP, /* tv_id */ + PROP_ssa | PROP_alias | PROP_cfg, /* properties_required */ + 0, /* properties_provided */ + 0, /* properties_destroyed */ + 0, /* todo_flags_start */ + TODO_dump_func + | TODO_cleanup_cfg + | TODO_ggc_collect + | TODO_verify_ssa + | TODO_update_ssa, /* todo_flags_finish */ + 0 /* letter */ +}; |