From 84d658141e6b1d315b0eba5f19a3c57a7c51fd3f Mon Sep 17 00:00:00 2001 From: Diego Novillo Date: Fri, 22 Apr 2005 20:59:54 -0400 Subject: [PATCH] [multiple changes] 2005-04-22 Diego Novillo * Makefile.in (tree-into-ssa.o): Add dependency on PARAMS_H. * lambda-code.c (perfect_nestify): Mark virtual operands in the moved statement for renaming. * params.def (PARAM_MIN_VIRTUAL_MAPPINGS): Define. (PARAM_VIRTUAL_MAPPINGS_TO_SYMS_RATIO): Define. * params.h (MIN_VIRTUAL_MAPPINGS): Define. (VIRTUAL_MAPPINGS_TO_SYMS_RATIO): Define. * tree-flow.h (struct var_ann_d): Update comments. (rewrite_to_new_ssa_names_bb, rewrite_to_new_ssa_names, allocate_ssa_names, find_phi_node_for, dump_tree_ssa, debug_tree_ssa, debug_def_blocks, dump_tree_ssa_stats, debug_tree_ssa_stats, rewrite_ssa_into_ssa, dump_repl_tbl, debug_repl_tbl, dump_names_replaced_by, debug_names_replaced_by): Remove declarations. (delete_update_ssa, get_current_def, set_current_def): Declare. (rewrite_into_loop_closed_ssa): Add new argument. Update all callers. * tree-into-ssa.c: Include params.h. (old_virtual_ssa_names): Remove. Update all users. (need_to_replace_names_p): Remove. Update all users. (struct update_ssa_stats_d): Declare. (update_ssa_stats): New local. (dump_tree_ssa, debug_tree_ssa, debug_def_blocks, dump_tree_ssa_stats, debug_tree_ssa_stats, dump_update_ssa, debug_update_ssa, dump_names_replaced_by, debug_names_replaced_by): Declare. (get_current_def, set current_def): Make extern. (is_old_name, is_new_name): Protect against references past the end of the sets. (add_new_name_mapping): Assert that OLD and NEW are different SSA names for the same symbol. When adding a virtual mapping, update stats for virtual mapping heuristic. (insert_phi_nodes_for): If UPDATE_P is true, create the LHS of the new PHI by duplicating SSA name VAR. (insert_phi_nodes_1): Remove. Update all users. (insert_phi_nodes): Remove second argument. Update all users. (maybe_replace_use, maybe_replace_def): New. (rewrite_update_stmt): Call them. (mark_def_interesting): Remove calls to is_gimple_reg. (prepare_phi_args_for_update): Remove. Update all users. (prepare_block_for_update): Only process virtual operands and GIMPLE registers in normal form. (prepare_use_sites_for): New. (prepare_names_to_update): Rename from prepare_def_sites. Call prepare_use_sites_for. (dump_update_ssa): Rename from dump_repl_tbl. Update all users. Show statistics for virtual mapping heuristic. (debug_update_ssa): Rename from debug_debug_repl_tbl. (init_update_ssa): Initialize UPDATE_SSA_STATS. (delete_update_ssa): Make extern. (mark_set_for_renaming): If the set is empty, do nothing. (release_ssa_name_after_update_ssa): Update comment. (insert_updated_phi_nodes_for): Do not remove existing PHI nodes for symbols. (switch_virtuals_to_full_rewrite_p): New. (switch_virtuals_to_full_rewrite): New. (update_ssa): Call them. Clear REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT for every statement before updating. If all the names in NEW_SSA_NAMES have been marked for removal, do nothing. Only start at the top of the CFG if there are symbols in SYMS_TO_RENAME. (ssa_rewrite_finalize_block): Remove. (ssa_register_new_def): Remove. (ssa_rewrite_stmt): Remove. (ssa_rewrite_phi_arguments): Remove. (ssa_rewrite_initialize_block): Remove. (ssa_mark_def_sites): Remove. (ssa_mark_def_sites_initialize_block): Remove. (ssa_mark_phi_uses): Remove. (rewrite_ssa_into_ssa): Remove. * tree-phinodes.c (find_phi_node_for): Remove. * tree-ssa-loop-ivcanon.c (try_unroll_loop_completely): Call update_ssa. * tree-ssa-loop-unswitch.c (tree_ssa_unswitch_loops): Remove calls to verify_dominators and verify_loop_structure. (tree_unswitch_single_loop): Call update_ssa. * tree-ssa.c (verify_ssa): Assert that need_ssa_update_p returns false. Call verify_stmts. (delete_tree_ssa): Assert that need_ssa_update_p returns false. * tree-vect-transform.c (vect_transform_loop): Call update_ssa. * tree-vectorizer.c (allocate_new_names, rename_def_op, free_new_names): Remove. Update all users. (rename_use_op): Call get_current_def. (rename_variables_in_bb): Do not handle any real or virtual definition. (slpeel_update_phis_for_duplicate_loop): Call get_current_def and set_current_def instead of using SSA_NAME_AUX. (slpeel_update_phi_nodes_for_guard2): Reformat comments. (slpeel_can_duplicate_loop_p): Call need_ssa_update_p, ssa_names_to_replace and delete_update_ssa. * doc/invoke.texi: Document --param min-virtual-mappings and --param virtual-mappings-ratio. 2005-04-22 Zdenek Dvorak Diego Novillo * tree-cfg.c (tree_duplicate_bb): Call create_new_def_for for newly created PHI nodes. Call create_new_def_for for every new V_MAY_DEF and V_MUST_DEF on the copied statement. (struct ssa_name_map_entry): Remove. (ssa_name_map_entry_hash): Remove. (ssa_name_map_entry_eq): Remove. (allocate_ssa_names): Remove. (rewrite_to_new_ssa_names_def): Remove. (rewrite_to_new_ssa_names_use): Remove. (rewrite_to_new_ssa_names_bb): Remove. (rewrite_to_new_ssa_names): Remove. (tree_duplicate_sese_region): Remove variables ver, definitions, phi, ssa_name_map and bi. Call need_ssa_update_p instead of any_marked_for_rewrite_p. Call update_ssa. * tree-ssa-dom.c (tree_ssa_dominator_optimize): Call update_ssa instead of rewrite_ssa_into_ssa. * tree-ssa-loop-manip.c (add_exit_phis_edge): Call create_new_def_for. (find_uses_to_rename_use): Add argument 'need_phis'. (find_uses_to_rename_stmt): Do not scan virtual operands. (find_uses_to_rename): Only scan PHI nodes for non-virtual names. (rewrite_into_loop_closed_ssa): Call update_ssa. (check_loop_closed_ssa_use): Ignore virtual operands. (check_loop_closed_ssa_stmt): Likewise. (verify_loop_closed_ssa): Do nothing if CURRENT_LOOPS is NULL. (rename_variables, set_phi_def_stmts): Remove. (tree_duplicate_loop_to_header_edge): Reformat comment. Remove variables BB, I and DEFINITIONS. Call need_ssa_update_p. Call update_ssa. (lv_adjust_loop_header_phi): Reformat comment. * tree-ssanames.c (ssa_names_to_rewrite): Remove. (marked_for_rewrite_p, any_marked_for_rewrite_p, mark_for_rewrite, unmark_all_for_rewrite, marked_ssa_names): Remove. Update all users. (release_ssa_name): If VAR has been registered for SSA updating, do nothing. * tree-vrp.c (new_ssa_names, old_ssa_names): Remove. (build_assert_expr_for): Call register_new_name_mapping. (insert_range_assertions): Update call to update_ssa. * tree.h (mark_for_rewrite, unmark_all_for_rewrite, marked_for_rewrite_p, any_marked_for_rewrite_p, marked_ssa_names): Remove. From-SVN: r98599 --- gcc/ChangeLog | 155 ++++ gcc/Makefile.in | 2 +- gcc/doc/invoke.texi | 12 + gcc/lambda-code.c | 12 +- gcc/params.def | 24 + gcc/params.h | 4 + gcc/testsuite/ChangeLog | 4 + gcc/testsuite/gcc.dg/tree-ssa/ltrans-4.c | 2 +- gcc/tree-cfg.c | 276 +------ gcc/tree-flow.h | 30 +- gcc/tree-into-ssa.c | 1308 ++++++++++-------------------- gcc/tree-loop-linear.c | 3 +- gcc/tree-phinodes.c | 24 - gcc/tree-ssa-dom.c | 2 +- gcc/tree-ssa-loop-ch.c | 2 +- gcc/tree-ssa-loop-im.c | 8 +- gcc/tree-ssa-loop-ivcanon.c | 2 + gcc/tree-ssa-loop-ivopts.c | 4 +- gcc/tree-ssa-loop-manip.c | 165 ++-- gcc/tree-ssa-loop-unswitch.c | 7 +- gcc/tree-ssa-loop.c | 5 +- gcc/tree-ssa.c | 10 +- gcc/tree-ssanames.c | 67 +- gcc/tree-vect-transform.c | 5 + gcc/tree-vectorizer.c | 191 ++--- gcc/tree.h | 7 - 26 files changed, 803 insertions(+), 1528 deletions(-) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 3a5e70256d3..9bfd962b37b 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,158 @@ +2005-04-22 Diego Novillo + + * Makefile.in (tree-into-ssa.o): Add dependency on PARAMS_H. + * lambda-code.c (perfect_nestify): Mark virtual operands in + the moved statement for renaming. + * params.def (PARAM_MIN_VIRTUAL_MAPPINGS): Define. + (PARAM_VIRTUAL_MAPPINGS_TO_SYMS_RATIO): Define. + * params.h (MIN_VIRTUAL_MAPPINGS): Define. + (VIRTUAL_MAPPINGS_TO_SYMS_RATIO): Define. + * tree-flow.h (struct var_ann_d): Update comments. + (rewrite_to_new_ssa_names_bb, rewrite_to_new_ssa_names, + allocate_ssa_names, find_phi_node_for, dump_tree_ssa, + debug_tree_ssa, debug_def_blocks, dump_tree_ssa_stats, + debug_tree_ssa_stats, rewrite_ssa_into_ssa, + dump_repl_tbl, debug_repl_tbl, dump_names_replaced_by, + debug_names_replaced_by): Remove declarations. + (delete_update_ssa, get_current_def, set_current_def): Declare. + (rewrite_into_loop_closed_ssa): Add new argument. Update + all callers. + * tree-into-ssa.c: Include params.h. + (old_virtual_ssa_names): Remove. Update all users. + (need_to_replace_names_p): Remove. Update all users. + (struct update_ssa_stats_d): Declare. + (update_ssa_stats): New local. + (dump_tree_ssa, debug_tree_ssa, debug_def_blocks, + dump_tree_ssa_stats, debug_tree_ssa_stats, + dump_update_ssa, debug_update_ssa, + dump_names_replaced_by, debug_names_replaced_by): Declare. + (get_current_def, set current_def): Make extern. + (is_old_name, is_new_name): Protect against references + past the end of the sets. + (add_new_name_mapping): Assert that OLD and NEW are + different SSA names for the same symbol. + When adding a virtual mapping, update stats for virtual + mapping heuristic. + (insert_phi_nodes_for): If UPDATE_P is true, create the + LHS of the new PHI by duplicating SSA name VAR. + (insert_phi_nodes_1): Remove. Update all users. + (insert_phi_nodes): Remove second argument. Update all + users. + (maybe_replace_use, maybe_replace_def): New. + (rewrite_update_stmt): Call them. + (mark_def_interesting): Remove calls to is_gimple_reg. + (prepare_phi_args_for_update): Remove. Update all users. + (prepare_block_for_update): Only process virtual operands + and GIMPLE registers in normal form. + (prepare_use_sites_for): New. + (prepare_names_to_update): Rename from prepare_def_sites. + Call prepare_use_sites_for. + (dump_update_ssa): Rename from dump_repl_tbl. + Update all users. + Show statistics for virtual mapping heuristic. + (debug_update_ssa): Rename from debug_debug_repl_tbl. + (init_update_ssa): Initialize UPDATE_SSA_STATS. + (delete_update_ssa): Make extern. + (mark_set_for_renaming): If the set is empty, do nothing. + (release_ssa_name_after_update_ssa): Update comment. + (insert_updated_phi_nodes_for): Do not remove existing + PHI nodes for symbols. + (switch_virtuals_to_full_rewrite_p): New. + (switch_virtuals_to_full_rewrite): New. + (update_ssa): Call them. + Clear REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT + for every statement before updating. + If all the names in NEW_SSA_NAMES have been marked for + removal, do nothing. + Only start at the top of the CFG if there are symbols in + SYMS_TO_RENAME. + (ssa_rewrite_finalize_block): Remove. + (ssa_register_new_def): Remove. + (ssa_rewrite_stmt): Remove. + (ssa_rewrite_phi_arguments): Remove. + (ssa_rewrite_initialize_block): Remove. + (ssa_mark_def_sites): Remove. + (ssa_mark_def_sites_initialize_block): Remove. + (ssa_mark_phi_uses): Remove. + (rewrite_ssa_into_ssa): Remove. + * tree-phinodes.c (find_phi_node_for): Remove. + * tree-ssa-loop-ivcanon.c (try_unroll_loop_completely): + Call update_ssa. + * tree-ssa-loop-unswitch.c (tree_ssa_unswitch_loops): + Remove calls to verify_dominators and + verify_loop_structure. + (tree_unswitch_single_loop): Call update_ssa. + * tree-ssa.c (verify_ssa): Assert that need_ssa_update_p + returns false. + Call verify_stmts. + (delete_tree_ssa): Assert that need_ssa_update_p returns + false. + * tree-vect-transform.c (vect_transform_loop): Call update_ssa. + * tree-vectorizer.c (allocate_new_names, rename_def_op, + free_new_names): Remove. Update all users. + (rename_use_op): Call get_current_def. + (rename_variables_in_bb): Do not handle any real or + virtual definition. + (slpeel_update_phis_for_duplicate_loop): Call + get_current_def and set_current_def instead of using + SSA_NAME_AUX. + (slpeel_update_phi_nodes_for_guard2): Reformat comments. + (slpeel_can_duplicate_loop_p): Call need_ssa_update_p, + ssa_names_to_replace and delete_update_ssa. + * doc/invoke.texi: Document --param min-virtual-mappings + and --param virtual-mappings-ratio. + +2005-04-22 Zdenek Dvorak + Diego Novillo + + * tree-cfg.c (tree_duplicate_bb): Call create_new_def_for + for newly created PHI nodes. + Call create_new_def_for for every new V_MAY_DEF and + V_MUST_DEF on the copied statement. + (struct ssa_name_map_entry): Remove. + (ssa_name_map_entry_hash): Remove. + (ssa_name_map_entry_eq): Remove. + (allocate_ssa_names): Remove. + (rewrite_to_new_ssa_names_def): Remove. + (rewrite_to_new_ssa_names_use): Remove. + (rewrite_to_new_ssa_names_bb): Remove. + (rewrite_to_new_ssa_names): Remove. + (tree_duplicate_sese_region): Remove variables ver, + definitions, phi, ssa_name_map and bi. + Call need_ssa_update_p instead of any_marked_for_rewrite_p. + Call update_ssa. + * tree-ssa-dom.c (tree_ssa_dominator_optimize): Call + update_ssa instead of rewrite_ssa_into_ssa. + * tree-ssa-loop-manip.c (add_exit_phis_edge): Call + create_new_def_for. + (find_uses_to_rename_use): Add argument 'need_phis'. + (find_uses_to_rename_stmt): Do not scan virtual operands. + (find_uses_to_rename): Only scan PHI nodes for + non-virtual names. + (rewrite_into_loop_closed_ssa): Call update_ssa. + (check_loop_closed_ssa_use): Ignore virtual operands. + (check_loop_closed_ssa_stmt): Likewise. + (verify_loop_closed_ssa): Do nothing if CURRENT_LOOPS is + NULL. + (rename_variables, set_phi_def_stmts): Remove. + (tree_duplicate_loop_to_header_edge): Reformat comment. + Remove variables BB, I and DEFINITIONS. + Call need_ssa_update_p. + Call update_ssa. + (lv_adjust_loop_header_phi): Reformat comment. + * tree-ssanames.c (ssa_names_to_rewrite): Remove. + (marked_for_rewrite_p, any_marked_for_rewrite_p, + mark_for_rewrite, unmark_all_for_rewrite, + marked_ssa_names): Remove. Update all users. + (release_ssa_name): If VAR has been registered for SSA + updating, do nothing. + * tree-vrp.c (new_ssa_names, old_ssa_names): Remove. + (build_assert_expr_for): Call register_new_name_mapping. + (insert_range_assertions): Update call to update_ssa. + * tree.h (mark_for_rewrite, unmark_all_for_rewrite, + marked_for_rewrite_p, any_marked_for_rewrite_p, + marked_ssa_names): Remove. + 2005-04-22 Jeff Law * tree-ssa-dom.c (block_defs_stack): Remove, no longer needed. diff --git a/gcc/Makefile.in b/gcc/Makefile.in index e7d2b253814..70ffa3d33d9 100644 --- a/gcc/Makefile.in +++ b/gcc/Makefile.in @@ -1635,7 +1635,7 @@ tree-into-ssa.o : tree-into-ssa.c $(TREE_FLOW_H) $(CONFIG_H) $(SYSTEM_H) \ $(RTL_H) $(TREE_H) $(TM_P_H) $(EXPR_H) output.h diagnostic.h \ errors.h toplev.h function.h $(TIMEVAR_H) \ $(TM_H) coretypes.h $(TREE_DUMP_H) langhooks.h domwalk.h tree-pass.h \ - $(GGC_H) + $(GGC_H) $(PARAMS_H) tree-outof-ssa.o : tree-outof-ssa.c $(TREE_FLOW_H) $(CONFIG_H) $(SYSTEM_H) \ $(RTL_H) $(TREE_H) $(TM_P_H) $(EXPR_H) output.h diagnostic.h \ errors.h toplev.h function.h $(TIMEVAR_H) \ diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index f273c2c51d8..8419422c61c 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -5827,6 +5827,18 @@ Small integer constants can use a shared data structure, reducing the compiler's memory usage and increasing its speed. This sets the maximum value of a shared integer constant's. The default value is 256. +@item min-virtual-mappings +Specifies the minimum number of virtual mappings in the incremental +SSA updater that should be registered to trigger the virtual mappings +heuristic defined by virtual-mappings-ratio. The default value is +100. + +@item virtual-mappings-ratio +If the number of virtual mappings is virtual-mappings-ratio bigger +than the number of virtual symbols to be updated, then the incremental +SSA updater switches to a full update for those symbols. The default +ratio is 3. + @end table @end table diff --git a/gcc/lambda-code.c b/gcc/lambda-code.c index 35a5a43f682..a81af8f8314 100644 --- a/gcc/lambda-code.c +++ b/gcc/lambda-code.c @@ -2443,7 +2443,9 @@ perfect_nestify (struct loops *loops, incremented when we do. */ for (bsi = bsi_start (bbs[i]); !bsi_end_p (bsi);) { - tree stmt = bsi_stmt (bsi); + ssa_op_iter i; + tree n, stmt = bsi_stmt (bsi); + if (stmt == exit_condition || not_interesting_stmt (stmt) || stmt_is_bumper_for_loop (loop, stmt)) @@ -2451,11 +2453,19 @@ perfect_nestify (struct loops *loops, bsi_next (&bsi); continue; } + replace_uses_of_x_with_y (stmt, oldivvar, ivvar); bsi_move_before (&bsi, &tobsi); + + /* If the statement has any virtual operands, they may + need to be rewired because the original loop may + still reference them. */ + FOR_EACH_SSA_TREE_OPERAND (n, stmt, i, SSA_OP_ALL_VIRTUALS) + mark_sym_for_renaming (SSA_NAME_VAR (n)); } } } + free (bbs); return perfect_nest_p (loop); } diff --git a/gcc/params.def b/gcc/params.def index f2b25c0d450..d9871a5b00b 100644 --- a/gcc/params.def +++ b/gcc/params.def @@ -443,6 +443,30 @@ DEFPARAM (PARAM_INTEGER_SHARE_LIMIT, "The upper bound for sharing integer constants", 256, 2, 2) +/* Incremental SSA updates for virtual operands may be very slow if + there is a large number of mappings to process. In those cases, it + is faster to rewrite the virtual symbols from scratch as if they + had been recently introduced. This heuristic cannot be applied to + SSA mappings for real SSA names, only symbols kept in FUD chains. + + PARAM_MIN_VIRTUAL_MAPPINGS specifies the minimum number of virtual + mappings that should be registered to trigger the heuristic. + + PARAM_VIRTUAL_MAPPINGS_TO_SYMS_RATIO specifies the ratio between + mappings and symbols. If the number of virtual mappings is + PARAM_VIRTUAL_MAPPINGS_TO_SYMS_RATIO bigger than the number of + virtual symbols to be updated, then the updater switches to a full + update for those symbols. */ +DEFPARAM (PARAM_MIN_VIRTUAL_MAPPINGS, + "min-virtual-mappings", + "Minimum number of virtual mappings to consider switching to full virtual renames", + 100, 0, 0) + +DEFPARAM (PARAM_VIRTUAL_MAPPINGS_TO_SYMS_RATIO, + "virtual-mappings-ratio", + "Ratio between virtual mappings and virtual symbols to do full virtual renames", + 3, 0, 0) + /* Local variables: mode:c diff --git a/gcc/params.h b/gcc/params.h index 0f872d031d0..fc1c377fb3b 100644 --- a/gcc/params.h +++ b/gcc/params.h @@ -139,4 +139,8 @@ typedef enum compiler_param PARAM_VALUE (PARAM_INTEGER_SHARE_LIMIT) #define MAX_LAST_VALUE_RTL \ PARAM_VALUE (PARAM_MAX_LAST_VALUE_RTL) +#define MIN_VIRTUAL_MAPPINGS \ + PARAM_VALUE (PARAM_MIN_VIRTUAL_MAPPINGS) +#define VIRTUAL_MAPPINGS_TO_SYMS_RATIO \ + PARAM_VALUE (PARAM_VIRTUAL_MAPPINGS_TO_SYMS_RATIO) #endif /* ! GCC_PARAMS_H */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 95b879ae8f8..a73d7222879 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2005-04-22 Diego Novillo + + * gcc.dg/tree-ssa/ltrans-4.c: Fix typo in dg-options. + 2005-04-22 Jeff Law * gcc.dg/uninit-5.c: Remove xfails. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ltrans-4.c b/gcc/testsuite/gcc.dg/tree-ssa/ltrans-4.c index 06a4bc2cf68..4e60eae4d51 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/ltrans-4.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/ltrans-4.c @@ -1,5 +1,5 @@ /* { dg-do compile } */ -/* { dg-options "-O20 -ftree-loop-linear -fdump-tree-ltrans-all" } */ +/* { dg-options "-O2 -ftree-loop-linear -fdump-tree-ltrans-all" } */ double u[1782225]; int foo(int N, int *res) diff --git a/gcc/tree-cfg.c b/gcc/tree-cfg.c index d47c6b4d558..bf4d934e973 100644 --- a/gcc/tree-cfg.c +++ b/gcc/tree-cfg.c @@ -963,7 +963,7 @@ cleanup_tree_cfg_loop (void) /* This usually does nothing. But sometimes parts of cfg that originally were inside a loop get out of it due to edge removal (since they become unreachable by back edges from latch). */ - rewrite_into_loop_closed_ssa (changed_bbs); + rewrite_into_loop_closed_ssa (changed_bbs, TODO_update_ssa); BITMAP_FREE (changed_bbs); @@ -4698,6 +4698,7 @@ tree_can_duplicate_bb_p (basic_block bb ATTRIBUTE_UNUSED) return true; } + /* Create a duplicate of the basic block BB. NOTE: This does not preserve SSA form. */ @@ -4706,45 +4707,49 @@ tree_duplicate_bb (basic_block bb) { basic_block new_bb; block_stmt_iterator bsi, bsi_tgt; - tree phi, val; - ssa_op_iter op_iter; + tree phi; new_bb = create_empty_bb (EXIT_BLOCK_PTR->prev_bb); - /* First copy the phi nodes. We do not copy phi node arguments here, - since the edges are not ready yet. Keep the chain of phi nodes in - the same order, so that we can add them later. */ + /* Copy the PHI nodes. We ignore PHI node arguments here because + the incoming edges have not been setup yet. */ for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) { - mark_for_rewrite (PHI_RESULT (phi)); - create_phi_node (PHI_RESULT (phi), new_bb); + tree copy = create_phi_node (PHI_RESULT (phi), new_bb); + create_new_def_for (PHI_RESULT (copy), copy, PHI_RESULT_PTR (copy)); } + + /* Keep the chain of PHI nodes in the same order so that they can be + updated by ssa_redirect_edge. */ set_phi_nodes (new_bb, phi_reverse (phi_nodes (new_bb))); bsi_tgt = bsi_start (new_bb); for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi)) { - tree stmt = bsi_stmt (bsi); - tree copy; + def_operand_p def_p; + ssa_op_iter op_iter; + tree stmt, copy; + stmt = bsi_stmt (bsi); if (TREE_CODE (stmt) == LABEL_EXPR) continue; - FOR_EACH_SSA_TREE_OPERAND (val, stmt, op_iter, SSA_OP_ALL_DEFS) - mark_for_rewrite (val); - + /* Create a new copy of STMT and duplicate STMT's virtual + operands. */ copy = unshare_expr (stmt); - - /* Copy also the virtual operands. */ - get_stmt_ann (copy); - copy_virtual_operands (copy, stmt); - bsi_insert_after (&bsi_tgt, copy, BSI_NEW_STMT); + copy_virtual_operands (copy, stmt); + + /* Create new names for all the definitions created by COPY and + add replacement mappings for each new name. */ + FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS) + create_new_def_for (DEF_FROM_PTR (def_p), copy, def_p); } return new_bb; } + /* Basic block BB_COPY was created by code duplication. Add phi node arguments for edges going out of BB_COPY. The blocks that were duplicated have rbi->duplicated set to one. */ @@ -4788,8 +4793,6 @@ add_phi_args_after_copy_bb (basic_block bb_copy) phi = phi_next, phi_copy = PHI_CHAIN (phi_copy)) { phi_next = PHI_CHAIN (phi); - - gcc_assert (PHI_RESULT (phi) == PHI_RESULT (phi_copy)); def = PHI_ARG_DEF_FROM_EDGE (phi, e); add_phi_arg (phi_copy, def, e_copy); } @@ -4815,193 +4818,6 @@ add_phi_args_after_copy (basic_block *region_copy, unsigned n_region) region_copy[i]->rbi->duplicated = 0; } -/* Maps the old ssa name FROM_NAME to TO_NAME. */ - -struct ssa_name_map_entry -{ - tree from_name; - tree to_name; -}; - -/* Hash function for ssa_name_map_entry. */ - -static hashval_t -ssa_name_map_entry_hash (const void *entry) -{ - const struct ssa_name_map_entry *en = entry; - return SSA_NAME_VERSION (en->from_name); -} - -/* Equality function for ssa_name_map_entry. */ - -static int -ssa_name_map_entry_eq (const void *in_table, const void *ssa_name) -{ - const struct ssa_name_map_entry *en = in_table; - - return en->from_name == ssa_name; -} - -/* Allocate duplicates of ssa names in list DEFINITIONS and store the mapping - to MAP. */ - -void -allocate_ssa_names (bitmap definitions, htab_t *map) -{ - tree name; - struct ssa_name_map_entry *entry; - PTR *slot; - unsigned ver; - bitmap_iterator bi; - - if (!*map) - *map = htab_create (10, ssa_name_map_entry_hash, - ssa_name_map_entry_eq, free); - EXECUTE_IF_SET_IN_BITMAP (definitions, 0, ver, bi) - { - name = ssa_name (ver); - slot = htab_find_slot_with_hash (*map, name, SSA_NAME_VERSION (name), - INSERT); - if (*slot) - entry = *slot; - else - { - entry = xmalloc (sizeof (struct ssa_name_map_entry)); - entry->from_name = name; - *slot = entry; - } - entry->to_name = duplicate_ssa_name (name, SSA_NAME_DEF_STMT (name)); - } -} - -/* Rewrite the definition DEF in statement STMT to new ssa name as specified - by the mapping MAP. */ - -static void -rewrite_to_new_ssa_names_def (def_operand_p def, tree stmt, htab_t map) -{ - tree name = DEF_FROM_PTR (def); - struct ssa_name_map_entry *entry; - - gcc_assert (TREE_CODE (name) == SSA_NAME); - - entry = htab_find_with_hash (map, name, SSA_NAME_VERSION (name)); - if (!entry) - return; - - SET_DEF (def, entry->to_name); - SSA_NAME_DEF_STMT (entry->to_name) = stmt; -} - -/* Rewrite the USE to new ssa name as specified by the mapping MAP. */ - -static void -rewrite_to_new_ssa_names_use (use_operand_p use, htab_t map) -{ - tree name = USE_FROM_PTR (use); - struct ssa_name_map_entry *entry; - - if (TREE_CODE (name) != SSA_NAME) - return; - - entry = htab_find_with_hash (map, name, SSA_NAME_VERSION (name)); - if (!entry) - return; - - SET_USE (use, entry->to_name); -} - -/* Rewrite the ssa names in basic block BB to new ones as specified by the - mapping MAP. */ - -void -rewrite_to_new_ssa_names_bb (basic_block bb, htab_t map) -{ - unsigned i; - edge e; - edge_iterator ei; - tree phi, stmt; - block_stmt_iterator bsi; - use_optype uses; - vuse_optype vuses; - def_optype defs; - v_may_def_optype v_may_defs; - v_must_def_optype v_must_defs; - stmt_ann_t ann; - - FOR_EACH_EDGE (e, ei, bb->preds) - if (e->flags & EDGE_ABNORMAL) - break; - - for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) - { - rewrite_to_new_ssa_names_def (PHI_RESULT_PTR (phi), phi, map); - if (e) - SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)) = 1; - } - - for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi)) - { - stmt = bsi_stmt (bsi); - ann = stmt_ann (stmt); - - uses = USE_OPS (ann); - for (i = 0; i < NUM_USES (uses); i++) - rewrite_to_new_ssa_names_use (USE_OP_PTR (uses, i), map); - - defs = DEF_OPS (ann); - for (i = 0; i < NUM_DEFS (defs); i++) - rewrite_to_new_ssa_names_def (DEF_OP_PTR (defs, i), stmt, map); - - vuses = VUSE_OPS (ann); - for (i = 0; i < NUM_VUSES (vuses); i++) - rewrite_to_new_ssa_names_use (VUSE_OP_PTR (vuses, i), map); - - v_may_defs = V_MAY_DEF_OPS (ann); - for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++) - { - rewrite_to_new_ssa_names_use - (V_MAY_DEF_OP_PTR (v_may_defs, i), map); - rewrite_to_new_ssa_names_def - (V_MAY_DEF_RESULT_PTR (v_may_defs, i), stmt, map); - } - - v_must_defs = V_MUST_DEF_OPS (ann); - for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++) - { - rewrite_to_new_ssa_names_def - (V_MUST_DEF_RESULT_PTR (v_must_defs, i), stmt, map); - rewrite_to_new_ssa_names_use - (V_MUST_DEF_KILL_PTR (v_must_defs, i), map); - } - } - - FOR_EACH_EDGE (e, ei, bb->succs) - for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi)) - { - rewrite_to_new_ssa_names_use - (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e), map); - - if (e->flags & EDGE_ABNORMAL) - { - tree op = PHI_ARG_DEF_FROM_EDGE (phi, e); - SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op) = 1; - } - } -} - -/* Rewrite the ssa names in N_REGION blocks REGION to the new ones as specified - by the mapping MAP. */ - -void -rewrite_to_new_ssa_names (basic_block *region, unsigned n_region, htab_t map) -{ - unsigned r; - - for (r = 0; r < n_region; r++) - rewrite_to_new_ssa_names_bb (region[r], map); -} - /* Duplicates a REGION (set of N_REGION basic blocks) with just a single important exit edge EXIT. By important we mean that no SSA name defined inside region is live over the other exit edges of the region. All entry @@ -5017,16 +4833,12 @@ tree_duplicate_sese_region (edge entry, edge exit, basic_block *region, unsigned n_region, basic_block *region_copy) { - unsigned i, n_doms, ver; + unsigned i, n_doms; bool free_region_copy = false, copying_header = false; struct loop *loop = entry->dest->loop_father; edge exit_copy; - bitmap definitions; - tree phi; basic_block *doms; - htab_t ssa_name_map = NULL; edge redirected; - bitmap_iterator bi; if (!can_copy_bbs_p (region, n_region)) return false; @@ -5035,7 +4847,6 @@ tree_duplicate_sese_region (edge entry, edge exit, missuses of the functions. I.e. if you ask to copy something weird, it will work, but the state of structures probably will not be correct. */ - for (i = 0; i < n_region; i++) { /* We do not handle subloops, i.e. all the blocks must belong to the @@ -5072,7 +4883,7 @@ tree_duplicate_sese_region (edge entry, edge exit, free_region_copy = true; } - gcc_assert (!any_marked_for_rewrite_p ()); + gcc_assert (!need_ssa_update_p ()); /* Record blocks outside the region that are duplicated by something inside. */ @@ -5080,7 +4891,6 @@ tree_duplicate_sese_region (edge entry, edge exit, n_doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region, doms); copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop); - definitions = marked_ssa_names (); if (copying_header) { @@ -5094,49 +4904,27 @@ tree_duplicate_sese_region (edge entry, edge exit, flush_pending_stmts (entry); /* Concerning updating of dominators: We must recount dominators - for entry block and its copy. Anything that is outside of the region, but - was dominated by something inside needs recounting as well. */ + for entry block and its copy. Anything that is outside of the + region, but was dominated by something inside needs recounting as + well. */ set_immediate_dominator (CDI_DOMINATORS, entry->dest, entry->src); doms[n_doms++] = entry->dest->rbi->original; iterate_fix_dominators (CDI_DOMINATORS, doms, n_doms); free (doms); - /* Add the other phi node arguments. */ + /* Add the other PHI node arguments. */ add_phi_args_after_copy (region_copy, n_region); - /* Add phi nodes for definitions at exit. TODO -- once we have immediate - uses, it should be possible to emit phi nodes just for definitions that - are used outside region. */ - EXECUTE_IF_SET_IN_BITMAP (definitions, 0, ver, bi) - { - tree name = ssa_name (ver); - - phi = create_phi_node (name, exit->dest); - add_phi_arg (phi, name, exit); - add_phi_arg (phi, name, exit_copy); - - SSA_NAME_DEF_STMT (name) = phi; - } - - /* And create new definitions inside region and its copy. TODO -- once we - have immediate uses, it might be better to leave definitions in region - unchanged, create new ssa names for phi nodes on exit, and rewrite - the uses, to avoid changing the copied region. */ - allocate_ssa_names (definitions, &ssa_name_map); - rewrite_to_new_ssa_names (region, n_region, ssa_name_map); - allocate_ssa_names (definitions, &ssa_name_map); - rewrite_to_new_ssa_names (region_copy, n_region, ssa_name_map); - htab_delete (ssa_name_map); + /* Update the SSA web. */ + update_ssa (TODO_update_ssa); if (free_region_copy) free (region_copy); - unmark_all_for_rewrite (); - BITMAP_FREE (definitions); - return true; } + /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in tree.h) */ void diff --git a/gcc/tree-flow.h b/gcc/tree-flow.h index 74f625f5b34..378599eee23 100644 --- a/gcc/tree-flow.h +++ b/gcc/tree-flow.h @@ -253,15 +253,11 @@ struct var_ann_d GTY(()) tree default_def; /* During into-ssa and the dominator optimizer, this field holds the - current version of this variable (an SSA_NAME). - - This was previously two varrays (one in into-ssa the other in the - dominator optimizer). That is wasteful, particularly since the - dominator optimizer calls into-ssa resulting in having two varrays - live at the same time and this can happen for each call to the - dominator optimizer. */ + current version of this variable (an SSA_NAME). */ tree current_def; + /* If this variable is a structure, this fields holds a list of + symbols representing each of the fields of the structure. */ subvar_t subvars; }; @@ -540,9 +536,6 @@ extern bool tree_duplicate_sese_region (edge, edge, basic_block *, unsigned, basic_block *); extern void add_phi_args_after_copy_bb (basic_block); extern void add_phi_args_after_copy (basic_block *, unsigned); -extern void rewrite_to_new_ssa_names_bb (basic_block, struct htab *); -extern void rewrite_to_new_ssa_names (basic_block *, unsigned, htab_t); -extern void allocate_ssa_names (bitmap, struct htab **); extern bool tree_purge_dead_eh_edges (basic_block); extern bool tree_purge_all_dead_eh_edges (bitmap); extern tree gimplify_val (block_stmt_iterator *, tree, tree); @@ -565,7 +558,6 @@ extern tree create_phi_node (tree, basic_block); extern void add_phi_arg (tree, tree, edge); extern void remove_phi_args (edge); extern void remove_phi_node (tree, tree); -extern tree find_phi_node_for (basic_block, tree, tree *); extern tree phi_reverse (tree); extern void dump_dfa_stats (FILE *); extern void debug_dfa_stats (void); @@ -612,11 +604,6 @@ typedef bool (*walk_use_def_chains_fn) (tree, tree, void *); /* In tree-ssa.c */ extern void init_tree_ssa (void); -extern void dump_tree_ssa (FILE *); -extern void debug_tree_ssa (void); -extern void debug_def_blocks (void); -extern void dump_tree_ssa_stats (FILE *); -extern void debug_tree_ssa_stats (void); extern edge ssa_redirect_edge (edge, basic_block); extern void flush_pending_stmts (edge); extern bool tree_ssa_useless_type_conversion (tree); @@ -628,23 +615,20 @@ extern void walk_use_def_chains (tree, walk_use_def_chains_fn, void *, bool); extern bool stmt_references_memory_p (tree); /* In tree-into-ssa.c */ -extern void rewrite_ssa_into_ssa (void); - void update_ssa (unsigned); +void delete_update_ssa (void); void register_new_name_mapping (tree, tree); tree create_new_def_for (tree, tree, def_operand_p); bool need_ssa_update_p (void); bool name_registered_for_update_p (tree); bitmap ssa_names_to_replace (void); void release_ssa_name_after_update_ssa (tree name); -void dump_repl_tbl (FILE *); -void debug_repl_tbl (void); -void dump_names_replaced_by (FILE *, tree); -void debug_names_replaced_by (tree); void compute_global_livein (bitmap, bitmap); tree duplicate_ssa_name (tree, tree); void mark_sym_for_renaming (tree); void mark_set_for_renaming (bitmap); +tree get_current_def (tree); +void set_current_def (tree, tree); /* In tree-ssa-ccp.c */ bool fold_stmt (tree *); @@ -724,7 +708,7 @@ tree find_loop_niter_by_eval (struct loop *, edge *); void estimate_numbers_of_iterations (struct loops *); tree can_count_iv_in_wider_type (struct loop *, tree, tree, tree, tree); void free_numbers_of_iterations_estimates (struct loops *); -void rewrite_into_loop_closed_ssa (bitmap); +void rewrite_into_loop_closed_ssa (bitmap, unsigned); void verify_loop_closed_ssa (void); void loop_commit_inserts (void); bool for_each_index (tree *, bool (*) (tree, tree *, void *), void *); diff --git a/gcc/tree-into-ssa.c b/gcc/tree-into-ssa.c index d90c752e57b..f903eabf89d 100644 --- a/gcc/tree-into-ssa.c +++ b/gcc/tree-into-ssa.c @@ -47,6 +47,7 @@ Boston, MA 02111-1307, USA. */ #include "cfgloop.h" #include "domwalk.h" #include "ggc.h" +#include "params.h" /* This file builds the SSA form for a function as described in: R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and K. Zadeck. Efficiently @@ -117,13 +118,6 @@ static sbitmap old_ssa_names; the operations done on them are presence tests. */ static sbitmap new_ssa_names; -/* Set of virtual SSA names to be updated. Since virtuals are always - in FUD chain form, these names are not used as a mapping mechanism - like OLD_SSA_NAMES and NEW_SSA_NAMES. Instead, the names in this - set are used by ssa_names_to_replace to inform its caller which - names are going to be updated. */ -static bitmap old_virtual_ssa_names; - /* Symbols whose SSA form needs to be updated or created for the first time. */ static bitmap syms_to_rename; @@ -159,8 +153,20 @@ static bool need_to_initialize_update_ssa_p = true; /* true if update_ssa needs to update virtual operands. */ static bool need_to_update_vops_p = false; -/* true if update_ssa is replacing existing SSA names. */ -static bool need_to_replace_names_p = false; +/* Statistics kept by update_ssa to use in the virtual mapping + heuristic. If the number of virtual mappings is beyond certain + threshold, the updater will switch from using the mappings into + renaming the virtual symbols from scratch. In some cases, the + large number of name mappings for virtual names causes significant + slowdowns in the PHI insertion code. */ +struct update_ssa_stats_d +{ + unsigned num_virtual_mappings; + unsigned num_total_mappings; + bitmap virtual_symbols; + unsigned num_virtual_symbols; +}; +static struct update_ssa_stats_d update_ssa_stats; /* Global data to attach to the main dominator walk structure. */ struct mark_def_sites_global_data @@ -222,6 +228,17 @@ enum rewrite_mode { #define REGISTER_DEFS_IN_THIS_STMT(T) (T)->common.unsigned_flag +/* Prototypes for debugging functions. */ +extern void dump_tree_ssa (FILE *); +extern void debug_tree_ssa (void); +extern void debug_def_blocks (void); +extern void dump_tree_ssa_stats (FILE *); +extern void debug_tree_ssa_stats (void); +void dump_update_ssa (FILE *); +void debug_update_ssa (void); +void dump_names_replaced_by (FILE *, tree); +void debug_names_replaced_by (tree); + /* Get the information associated with NAME. */ static inline struct ssa_name_info * @@ -260,7 +277,7 @@ set_phi_state (tree var, enum need_phi_state state) /* Return the current definition for VAR. */ -static inline tree +tree get_current_def (tree var) { if (TREE_CODE (var) == SSA_NAME) @@ -272,7 +289,7 @@ get_current_def (tree var) /* Sets current definition of VAR to DEF. */ -static inline void +void set_current_def (tree var, tree def) { if (TREE_CODE (var) == SSA_NAME) @@ -447,10 +464,8 @@ symbol_marked_for_renaming (tree sym) static inline bool is_old_name (tree name) { - if (!need_to_replace_names_p) - return false; - - return TEST_BIT (old_ssa_names, SSA_NAME_VERSION (name)); + unsigned ver = SSA_NAME_VERSION (name); + return ver < new_ssa_names->n_bits && TEST_BIT (old_ssa_names, ver); } @@ -459,10 +474,8 @@ is_old_name (tree name) static inline bool is_new_name (tree name) { - if (!need_to_replace_names_p) - return false; - - return TEST_BIT (new_ssa_names, SSA_NAME_VERSION (name)); + unsigned ver = SSA_NAME_VERSION (name); + return ver < new_ssa_names->n_bits && TEST_BIT (new_ssa_names, ver); } @@ -542,6 +555,9 @@ add_new_name_mapping (tree new, tree old) { timevar_push (TV_TREE_SSA_INCREMENTAL); + /* OLD and NEW must be different SSA names for the same symbol. */ + gcc_assert (new != old && SSA_NAME_VAR (new) == SSA_NAME_VAR (old)); + /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our caller may have created new names since the set was created. */ if (new_ssa_names->n_bits <= num_ssa_names - 1) @@ -551,34 +567,31 @@ add_new_name_mapping (tree new, tree old) old_ssa_names = sbitmap_resize (old_ssa_names, new_sz, 0); } - /* We don't need to keep replacement mappings for virtual names. - Since these names are kept in FUD-chain form, we need to traverse - the CFG from ENTRY to repair FUD chains. */ + /* If this mapping is for virtual names, we will need to update + virtual operands. */ if (!is_gimple_reg (new)) { tree sym; + size_t uid; - gcc_assert (!is_gimple_reg (old)); + need_to_update_vops_p = true; - if (DECL_P (old)) - sym = new; - else + /* Keep counts of virtual mappings and symbols to use in the + virtual mapping heuristic. If we have large numbers of + virtual mappings for a relatively low number of symbols, it + will make more sense to rename the symbols from scratch. + Otherwise, the insertion of PHI nodes for each of the old + names in these mappings will be very slow. */ + sym = SSA_NAME_VAR (new); + uid = var_ann (sym)->uid; + update_ssa_stats.num_virtual_mappings++; + if (!bitmap_bit_p (update_ssa_stats.virtual_symbols, uid)) { - sym = SSA_NAME_VAR (old); - bitmap_set_bit (old_virtual_ssa_names, SSA_NAME_VERSION (old)); + bitmap_set_bit (update_ssa_stats.virtual_symbols, uid); + update_ssa_stats.num_virtual_symbols++; } - - mark_sym_for_renaming (sym); - need_to_update_vops_p = true; - - timevar_pop (TV_TREE_SSA_INCREMENTAL); - - return; } - /* Assume that OLD and NEW are different GIMPLE register names. */ - gcc_assert (new != old && is_gimple_reg (old)); - /* Update the REPL_TBL table. */ add_to_repl_tbl (new, old); @@ -592,8 +605,8 @@ add_new_name_mapping (tree new, tree old) SET_BIT (new_ssa_names, SSA_NAME_VERSION (new)); SET_BIT (old_ssa_names, SSA_NAME_VERSION (old)); - /* Indicate that we are going to be replacing existing names. */ - need_to_replace_names_p = true; + /* Update mapping counter to use in the virtual mapping heuristic. */ + update_ssa_stats.num_total_mappings++; timevar_pop (TV_TREE_SSA_INCREMENTAL); } @@ -802,24 +815,20 @@ insert_phi_nodes_for (tree var, bitmap phi_insertion_points, bool update_p) 0, bb_index, bi) { bb = BASIC_BLOCK (bb_index); - phi = create_phi_node (var, bb); - if (TREE_CODE (var) == SSA_NAME) + if (update_p && TREE_CODE (var) == SSA_NAME) { + /* If we are rewriting SSA names, create the LHS of the PHI + node by duplicating VAR. This is useful in the case of + pointers, to also duplicate pointer attributes (alias + information, in particular). */ edge_iterator ei; + tree new_lhs; - /* FIXME. After removing rewrite_ssa_into_ssa, change this - if() to gcc_assert(). */ - if (update_p) - { - /* If we are rewriting SSA names, create the LHS of the - PHI node by duplicating VAR. This is useful in the - case of pointers, to also duplicate pointer - attributes (alias information, in particular). */ - tree new_lhs = duplicate_ssa_name (var, phi); - SET_PHI_RESULT (phi, new_lhs); - add_new_name_mapping (new_lhs, var); - } + phi = create_phi_node (var, bb); + new_lhs = duplicate_ssa_name (var, phi); + SET_PHI_RESULT (phi, new_lhs); + add_new_name_mapping (new_lhs, var); /* Add VAR to every argument slot of PHI. We need VAR in every argument so that rewrite_update_phi_arguments knows @@ -830,6 +839,11 @@ insert_phi_nodes_for (tree var, bitmap phi_insertion_points, bool update_p) FOR_EACH_EDGE (e, ei, bb->preds) add_phi_arg (phi, var, e); } + else + { + tree sym = DECL_P (var) ? var : SSA_NAME_VAR (var); + phi = create_phi_node (sym, bb); + } /* Mark this PHI node as interesting for update_ssa. */ REGISTER_DEFS_IN_THIS_STMT (phi) = 1; @@ -838,55 +852,36 @@ insert_phi_nodes_for (tree var, bitmap phi_insertion_points, bool update_p) } -/* Helper for insert_phi_nodes. If VAR needs PHI nodes, insert them - at the dominance frontier (DFS) of blocks defining VAR. */ - -static inline void -insert_phi_nodes_1 (tree var, bitmap *dfs) -{ - struct def_blocks_d *def_map; - bitmap idf; - - def_map = find_def_blocks_for (var); - if (def_map == NULL) - return; - - if (get_phi_state (var) != NEED_PHI_STATE_NO) - { - idf = find_idf (def_map->def_blocks, dfs); - insert_phi_nodes_for (var, idf, false); - BITMAP_FREE (idf); - } -} - - /* Insert PHI nodes at the dominance frontier of blocks with variable definitions. DFS contains the dominance frontier information for the flowgraph. PHI nodes will only be inserted at the dominance frontier of definition blocks for variables whose NEED_PHI_STATE annotation is marked as ``maybe'' or ``unknown'' (computed by - mark_def_sites). If NAMES_TO_RENAME is not NULL, do the same but - for ssa name rewriting. */ + mark_def_sites). */ static void -insert_phi_nodes (bitmap *dfs, bitmap names_to_rename) +insert_phi_nodes (bitmap *dfs) { unsigned i; timevar_push (TV_TREE_INSERT_PHI_NODES); - if (names_to_rename) + for (i = 0; i < num_referenced_vars; i++) { - bitmap_iterator bi; + struct def_blocks_d *def_map; + bitmap idf; + tree var = referenced_var (i); - EXECUTE_IF_SET_IN_BITMAP (names_to_rename, 0, i, bi) - if (ssa_name (i)) - insert_phi_nodes_1 (ssa_name (i), dfs); - } - else - { - for (i = 0; i < num_referenced_vars; i++) - insert_phi_nodes_1 (referenced_var (i), dfs); + def_map = find_def_blocks_for (var); + if (def_map == NULL) + continue; + + if (get_phi_state (var) != NEED_PHI_STATE_NO) + { + idf = find_idf (def_map->def_blocks, dfs); + insert_phi_nodes_for (var, idf, false); + BITMAP_FREE (idf); + } } timevar_pop (TV_TREE_INSERT_PHI_NODES); @@ -1347,18 +1342,6 @@ rewrite_update_init_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED, } -/* Replace the operand pointed by USE_P with USE's current reaching - definition. */ - -static inline void -replace_use (use_operand_p use_p, tree use) -{ - tree rdef = get_reaching_def (use); - if (rdef != use) - SET_USE (use_p, rdef); -} - - /* Called after visiting block BB. Unwind BLOCK_DEFS_STACK to restore the current reaching definition of every name re-written in BB to the original reaching definition before visiting BB. This @@ -1385,6 +1368,65 @@ rewrite_update_fini_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED, } +/* If the operand pointed to by USE_P is a name in OLD_SSA_NAMES or + it is a symbol marked for renaming, replace it with USE_P's current + reaching definition. */ + +static inline void +maybe_replace_use (use_operand_p use_p) +{ + tree rdef = NULL_TREE; + tree use = USE_FROM_PTR (use_p); + tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use); + + if (symbol_marked_for_renaming (sym)) + rdef = get_reaching_def (sym); + else if (is_old_name (use)) + rdef = get_reaching_def (use); + + if (rdef && rdef != use) + SET_USE (use_p, rdef); +} + + +/* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES + or OLD_SSA_NAMES, or if it is a symbol marked for renaming, + register it as the current definition for the names replaced by + DEF_P. */ + +static inline void +maybe_register_def (def_operand_p def_p, tree stmt) +{ + tree def = DEF_FROM_PTR (def_p); + tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def); + + /* If DEF is a naked symbol that needs renaming, create a + new name for it. */ + if (symbol_marked_for_renaming (sym)) + { + if (DECL_P (def)) + { + def = make_ssa_name (def, stmt); + SET_DEF (def_p, def); + } + + register_new_update_single (def, sym); + } + else + { + /* If DEF is a new name, register it as a new definition + for all the names replaced by DEF. */ + if (is_new_name (def)) + register_new_update_set (def, names_replaced_by (def)); + + /* If DEF is an old name, register DEF as a new + definition for itself. */ + if (is_old_name (def)) + register_new_update_single (def, def); + } +} + + /* Update every variable used in the statement pointed-to by SI. The statement is assumed to be in SSA form already. Names in OLD_SSA_NAMES used by SI will be updated to their current reaching @@ -1422,26 +1464,12 @@ rewrite_update_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED, if (REWRITE_THIS_STMT (stmt)) { FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE) - { - tree use = USE_FROM_PTR (use_p); - tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use); - - if (symbol_marked_for_renaming (sym)) - replace_use (use_p, sym); - else if (is_old_name (use)) - replace_use (use_p, use); - } + maybe_replace_use (use_p); if (need_to_update_vops_p) FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_VIRTUAL_USES | SSA_OP_VIRTUAL_KILLS) - { - tree use = USE_FROM_PTR (use_p); - tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use); - - if (symbol_marked_for_renaming (sym)) - replace_use (use_p, sym); - } + maybe_replace_use (use_p); } /* Register definitions of names in NEW_SSA_NAMES and OLD_SSA_NAMES. @@ -1450,57 +1478,27 @@ rewrite_update_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED, if (REGISTER_DEFS_IN_THIS_STMT (stmt)) { FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_DEF) - { - tree def = DEF_FROM_PTR (def_p); - tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def); - - /* If DEF is a naked symbol that needs renaming, create a - new name for it. */ - if (symbol_marked_for_renaming (sym)) - { - if (DECL_P (def)) - { - def = make_ssa_name (def, stmt); - SET_DEF (def_p, def); - } - - register_new_update_single (def, sym); - } - else - { - /* If DEF is a new name, register it as a new definition - for all the names replaced by DEF. */ - if (is_new_name (def)) - register_new_update_set (def, names_replaced_by (def)); - - /* If DEF is an old name, register DEF as a new - definition for itself. */ - if (is_old_name (def)) - register_new_update_single (def, def); - } - } + maybe_register_def (def_p, stmt); if (need_to_update_vops_p) FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_VIRTUAL_DEFS) - { - tree def = DEF_FROM_PTR (def_p); - tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def); - - if (symbol_marked_for_renaming (sym)) - { - if (DECL_P (def)) - { - def = make_ssa_name (def, stmt); - SET_DEF (def_p, def); - } - - register_new_update_single (def, sym); - } - } + maybe_register_def (def_p, stmt); } } +/* Replace the operand pointed by USE_P with USE's current reaching + definition. */ + +static inline void +replace_use (use_operand_p use_p, tree use) +{ + tree rdef = get_reaching_def (use); + if (rdef != use) + SET_USE (use_p, rdef); +} + + /* Visit all the successor blocks of BB looking for PHI nodes. For every PHI node found, check if any of its arguments is in OLD_SSA_NAMES. If so, and if the argument has a current reaching @@ -1764,7 +1762,7 @@ rewrite_into_ssa (void) mark_def_site_blocks (interesting_blocks); /* 3- Insert PHI nodes at dominance frontiers of definition blocks. */ - insert_phi_nodes (dfs, NULL); + insert_phi_nodes (dfs); /* 4- Rename all the blocks. */ rewrite_blocks (ENTRY_BLOCK_PTR, REWRITE_ALL, interesting_blocks); @@ -1812,15 +1810,6 @@ mark_def_interesting (tree var, tree stmt, basic_block bb, bitmap blocks, { bool is_phi_p = TREE_CODE (stmt) == PHI_NODE; -#if defined ENABLE_CHECKING - /* If VAR is a virtual, then it had better be a symbol. - Virtuals are in FUD-chain form, so we are interested in the - definition and use sites of the symbol, not the individual - SSA names. */ - if (!is_gimple_reg (var)) - gcc_assert (DECL_P (var)); -#endif - set_def_block (var, bb, is_phi_p); /* If VAR is an SSA name in NEW_SSA_NAMES, this is a definition @@ -1856,146 +1845,43 @@ mark_use_interesting (tree var, tree stmt, basic_block bb, bitmap blocks, replace it). */ if (insert_phi_p) { - struct def_blocks_d *db_p; - -#if defined ENABLE_CHECKING - /* If VAR is a virtual, then it had better be a symbol. - Virtuals are in FUD-chain form, so we are interested in the - definition and use sites of the symbol, not the individual - SSA names. */ - if (!is_gimple_reg (var)) - gcc_assert (DECL_P (var)); -#endif - - db_p = get_def_blocks_for (var); + struct def_blocks_d *db_p = get_def_blocks_for (var); if (!bitmap_bit_p (db_p->def_blocks, bb->index)) set_livein_block (var, bb); } } -/* If any of the arguments of PHI is in OLD_SSA_NAMES, mark PHI to - be rewritten. BB is the block where PHI resides, BLOCKS is the - region to be renamed and INSERT_PHI_P is true if the updating - process should insert new PHI nodes. */ - -static void -prepare_phi_args_for_update (tree phi, basic_block bb, bitmap blocks, - bool insert_phi_p) -{ - int i; - - for (i = 0; i < PHI_NUM_ARGS (phi); i++) - { - tree arg = PHI_ARG_DEF (phi, i); - - if (TREE_CODE (arg) == SSA_NAME && is_old_name (arg)) - { - /* Mark this use of ARG interesting for the renamer. Notice - that we explicitly call mark_use_interesting with - INSERT_PHI_P == false. - - This is to avoid marking ARG as live-in in this block BB. - If we were to mark ARG live-in to BB, then ARG would be - considered live-in through ALL incoming edges to BB which - is not what we want. Since we are updating the SSA form - for ARG, we don't really know what other names of ARG are - coming in through other edges into BB. - - If we considered ARG live-in at BB, then the PHI - placement algorithm may try to insert PHI nodes in blocks - that are not only unnecessary but also the renamer would - not know how to fill in. */ - mark_use_interesting (arg, phi, bb, blocks, false); - - /* As discussed above, we only want to mark ARG live-in - through the edge corresponding to its slot inside the PHI - argument list. So, we look for the block BB1 where ARG is - flowing through. If BB1 does not contain a definition of - ARG, then consider ARG live-in at BB1. */ - if (insert_phi_p) - { - edge e = PHI_ARG_EDGE (phi, i); - basic_block bb1 = e->src; - struct def_blocks_d *db = get_def_blocks_for (arg); - - if (!bitmap_bit_p (db->def_blocks, bb1->index)) - set_livein_block (arg, bb1); - } - } - } -} - - /* Do a dominator walk starting at BB processing statements that - reference variables in OLD_SSA_NAMES and NEW_SSA_NAMES. - - 1- Mark in BLOCKS the defining block of every name N in - NEW_SSA_NAMES. - - 2- Mark in BLOCKS the defining block of every name O in - OLD_SSA_NAMES. - - 3- For every statement or PHI node that uses a name O in - OLD_SSA_NAMES. If INSERT_PHI_P is true, mark those uses as live - in the corresponding block. This is later used by the PHI - placement algorithm to make PHI pruning decisions. + reference symbols in SYMS_TO_RENAME. This is very similar to + mark_def_sites, but the scan handles statements whose operands may + already be SSA names. Blocks that contain defs or uses of symbols + in SYMS_TO_RENAME are added to BLOCKS. - If VISIT_DOM_P is true, all the dominator children of BB are also - visited. - - FIXME. This process is slower than necessary. Once we have - immediate uses merged in, we should be able to just visit the - immediate uses of all the names that we are about to replace, - instead of visiting the whole block. */ + If INSERT_PHI_P is true, mark those uses as live in the + corresponding block. This is later used by the PHI placement + algorithm to make PHI pruning decisions. */ static void -prepare_block_for_update (basic_block bb, bool insert_phi_p, - bitmap blocks, bool visit_dom_p) +prepare_block_for_update (basic_block bb, bitmap blocks, bool insert_phi_p) { basic_block son; block_stmt_iterator si; tree phi; /* Process PHI nodes marking interesting those that define or use - the names that we are interested in. */ + the symbols that we are interested in. */ for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) { tree lhs_sym, lhs = PHI_RESULT (phi); - REWRITE_THIS_STMT (phi) = 0; - REGISTER_DEFS_IN_THIS_STMT (phi) = 0; - - /* Ignore virtual PHIs if we are not updating virtual operands. - Note that even if NEED_TO_REPLACE_NAMES_P is false, we need - to process real PHIs because we may be rewriting GIMPLE regs - into SSA for the first time. Therefore, we cannot do a - similar shortcut for real PHIs. */ - if (!need_to_update_vops_p && !is_gimple_reg (lhs)) - continue; - lhs_sym = DECL_P (lhs) ? lhs : SSA_NAME_VAR (lhs); if (symbol_marked_for_renaming (lhs_sym)) { - /* If the LHS is a virtual symbol marked for renaming, then - we don't need to scan the argument list. Since virtual - operands are in FUD-chain form, all the arguments of this - PHI must be the same symbol as the LHS. So, we just need - to mark this site as both an interesting use and an - interesting def for the symbol. */ mark_use_interesting (lhs_sym, phi, bb, blocks, insert_phi_p); mark_def_interesting (lhs_sym, phi, bb, blocks, insert_phi_p); } - else if (need_to_replace_names_p) - { - /* If the LHS is in OLD_SSA_NAMES or NEW_SSA_NAMES, this is - a definition site for it. */ - if (is_old_name (lhs) || is_new_name (lhs)) - mark_def_interesting (lhs, phi, bb, blocks, insert_phi_p); - - prepare_phi_args_for_update (phi, bb, blocks, insert_phi_p); - } } /* Process the statements. */ @@ -2008,17 +1894,11 @@ prepare_block_for_update (basic_block bb, bool insert_phi_p, stmt = bsi_stmt (si); - REWRITE_THIS_STMT (stmt) = 0; - REGISTER_DEFS_IN_THIS_STMT (stmt) = 0; - - /* Note, even if NEED_TO_REPLACE_NAMES_P is false, we need to - scan real uses and defs, as we may be renaming a GIMPLE - register for the first time. */ FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_USE) { tree use = USE_FROM_PTR (use_p); tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use); - if (symbol_marked_for_renaming (sym) || is_old_name (use)) + if (symbol_marked_for_renaming (sym)) mark_use_interesting (use, stmt, bb, blocks, insert_phi_p); } @@ -2027,23 +1907,10 @@ prepare_block_for_update (basic_block bb, bool insert_phi_p, tree def = DEF_FROM_PTR (def_p); tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def); - if (symbol_marked_for_renaming (sym) - || is_new_name (def) - || is_old_name (def)) + if (symbol_marked_for_renaming (sym)) mark_def_interesting (def, stmt, bb, blocks, insert_phi_p); } - /* If we don't need to update virtual operands, continue to the - next statement. */ - if (!need_to_update_vops_p) - continue; - - /* For every interesting N_i = V_MAY_DEF and - N_i = V_MUST_DEF , mark the statement as interesting. - Notice that N_j may in fact be a naked symbol (if this - statement is the result of basic block duplication). The - rename process will later fill in the appropriate reaching - definition for the symbol. */ FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, i, SSA_OP_VIRTUAL_DEFS) { tree def = DEF_FROM_PTR (def_p); @@ -2056,7 +1923,6 @@ prepare_block_for_update (basic_block bb, bool insert_phi_p, } } - /* Similarly, for V_USE . */ FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_VUSE) { tree use = USE_FROM_PTR (use_p); @@ -2068,16 +1934,76 @@ prepare_block_for_update (basic_block bb, bool insert_phi_p, } /* Now visit all the blocks dominated by BB. */ - if (visit_dom_p) - for (son = first_dom_son (CDI_DOMINATORS, bb); - son; - son = next_dom_son (CDI_DOMINATORS, son)) - prepare_block_for_update (son, insert_phi_p, blocks, true); + for (son = first_dom_son (CDI_DOMINATORS, bb); + son; + son = next_dom_son (CDI_DOMINATORS, son)) + prepare_block_for_update (son, blocks, insert_phi_p); +} + + +/* Helper for prepare_names_to_update. Mark all the use sites for + NAME as interesting. BLOCKS and INSERT_PHI_P are as in + prepare_names_to_update. */ + +static void +prepare_use_sites_for (tree name, bitmap blocks, bool insert_phi_p) +{ + use_operand_p use_p; + imm_use_iterator iter; + + FOR_EACH_IMM_USE_FAST (use_p, iter, name) + { + tree stmt = USE_STMT (use_p); + basic_block bb = bb_for_stmt (stmt); + + if (TREE_CODE (stmt) == PHI_NODE) + { + /* Mark this use of NAME interesting for the renamer. + Notice that we explicitly call mark_use_interesting with + INSERT_PHI_P == false. + + This is to avoid marking NAME as live-in in this block + BB. If we were to mark NAME live-in to BB, then NAME + would be considered live-in through ALL incoming edges to + BB which is not what we want. Since we are updating the + SSA form for NAME, we don't really know what other names + of NAME are coming in through other edges into BB. + + If we considered NAME live-in at BB, then the PHI + placement algorithm may try to insert PHI nodes in blocks + that are not only unnecessary but also the renamer would + not know how to fill in. */ + mark_use_interesting (name, stmt, bb, blocks, false); + + /* As discussed above, we only want to mark NAME live-in + through the edge corresponding to its slot inside the PHI + argument list. So, we look for the block BB1 where NAME + is flowing through. If BB1 does not contain a definition + of NAME, then consider NAME live-in at BB1. */ + if (insert_phi_p) + { + int ix = PHI_ARG_INDEX_FROM_USE (use_p); + edge e = PHI_ARG_EDGE (stmt, ix); + basic_block bb1 = e->src; + struct def_blocks_d *db = get_def_blocks_for (name); + + if (!bitmap_bit_p (db->def_blocks, bb1->index)) + set_livein_block (name, bb1); + } + } + else + { + /* For regular statements, mark this as an interesting use + for NAME. */ + mark_use_interesting (name, stmt, bb, blocks, insert_phi_p); + } + } } -/* Helper for prepare_def_sites. Mark the definition site for NAME as - interesting. BLOCKS and INSERT_PHI_P are as in prepare_def_sites. */ +/* Helper for prepare_names_to_update. Mark the definition site for + NAME as interesting. BLOCKS and INSERT_PHI_P are as in + prepare_names_to_update. */ static void prepare_def_site_for (tree name, bitmap blocks, bool insert_phi_p) @@ -2085,7 +2011,6 @@ prepare_def_site_for (tree name, bitmap blocks, bool insert_phi_p) tree stmt; basic_block bb; - gcc_assert (name && is_gimple_reg (name)); gcc_assert (names_to_release == NULL || !bitmap_bit_p (names_to_release, SSA_NAME_VERSION (name))); @@ -2099,12 +2024,13 @@ prepare_def_site_for (tree name, bitmap blocks, bool insert_phi_p) } -/* Mark definition sites of names in NEW_SSA_NAMES and OLD_SSA_NAMES. - Add each definition block to BLOCKS. INSERT_PHI_P is true if the - caller wants to insert PHI nodes for newly created names. */ +/* Mark definition and use sites of names in NEW_SSA_NAMES and + OLD_SSA_NAMES. Add each definition block to BLOCKS. INSERT_PHI_P + is true if the caller wants to insert PHI nodes for newly created + names. */ static void -prepare_def_sites (bitmap blocks, bool insert_phi_p) +prepare_names_to_update (bitmap blocks, bool insert_phi_p) { unsigned i; bitmap_iterator bi; @@ -2118,14 +2044,20 @@ prepare_def_sites (bitmap blocks, bool insert_phi_p) EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi) RESET_BIT (new_ssa_names, i); + /* First process names in NEW_SSA_NAMES. Otherwise, uses of old + names may be considered to be live-in on blocks that contain + definitions for their replacements. */ + EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, + prepare_def_site_for (ssa_name (i), blocks, insert_phi_p)); + /* If an old name is in NAMES_TO_RELEASE, we cannot remove it from OLD_SSA_NAMES, but we have to ignore its definition site. */ EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, - if (names_to_release == NULL || !bitmap_bit_p (names_to_release, i)) - prepare_def_site_for (ssa_name (i), blocks, insert_phi_p)); - - EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, - prepare_def_site_for (ssa_name (i), blocks, insert_phi_p)); + { + if (names_to_release == NULL || !bitmap_bit_p (names_to_release, i)) + prepare_def_site_for (ssa_name (i), blocks, insert_phi_p); + prepare_use_sites_for (ssa_name (i), blocks, insert_phi_p); + }); } @@ -2161,10 +2093,10 @@ debug_names_replaced_by (tree name) } -/* Dump the SSA name replacement table to FILE. */ +/* Dump SSA update information to FILE. */ void -dump_repl_tbl (FILE *file) +dump_update_ssa (FILE *file) { unsigned i; bitmap_iterator bi; @@ -2180,6 +2112,18 @@ dump_repl_tbl (FILE *file) EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, dump_names_replaced_by (file, ssa_name (i))); + + fprintf (file, "\n"); + fprintf (file, "Number of virtual NEW -> OLD mappings: %7u\n", + update_ssa_stats.num_virtual_mappings); + fprintf (file, "Number of real NEW -> OLD mappings: %7u\n", + update_ssa_stats.num_total_mappings + - update_ssa_stats.num_virtual_mappings); + fprintf (file, "Number of total NEW -> OLD mappings: %7u\n", + update_ssa_stats.num_total_mappings); + + fprintf (file, "\nNumber of virtual symbols: %u\n", + update_ssa_stats.num_virtual_symbols); } if (syms_to_rename && !bitmap_empty_p (syms_to_rename)) @@ -2192,16 +2136,6 @@ dump_repl_tbl (FILE *file) } } - if (old_virtual_ssa_names && !bitmap_empty_p (old_virtual_ssa_names)) - { - fprintf (file, "\n\nVirtual SSA names to be updated\n\n"); - EXECUTE_IF_SET_IN_BITMAP (old_virtual_ssa_names, 0, i, bi) - { - print_generic_expr (file, ssa_name (i), 0); - fprintf (file, " "); - } - } - if (names_to_release && !bitmap_empty_p (names_to_release)) { fprintf (file, "\n\nSSA names to release after updating the SSA web\n\n"); @@ -2216,12 +2150,12 @@ dump_repl_tbl (FILE *file) } -/* Dump the SSA name replacement table to stderr. */ +/* Dump SSA update information to stderr. */ void -debug_repl_tbl (void) +debug_update_ssa (void) { - dump_repl_tbl (stderr); + dump_update_ssa (stderr); } @@ -2230,7 +2164,7 @@ debug_repl_tbl (void) static void init_update_ssa (void) { - /* Reserve 1/3 more than the current number of names. The calls to + /* Reserve more space than the current number of names. The calls to add_new_name_mapping are typically done after creating new SSA names, so we'll need to reallocate these arrays. */ old_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR); @@ -2242,16 +2176,16 @@ init_update_ssa (void) repl_tbl = htab_create (20, repl_map_hash, repl_map_eq, repl_map_free); need_to_initialize_update_ssa_p = false; need_to_update_vops_p = false; - need_to_replace_names_p = false; syms_to_rename = BITMAP_ALLOC (NULL); - old_virtual_ssa_names = BITMAP_ALLOC (NULL); names_to_release = NULL; + memset (&update_ssa_stats, 0, sizeof (update_ssa_stats)); + update_ssa_stats.virtual_symbols = BITMAP_ALLOC (NULL); } /* Deallocate data structures used for incremental SSA updates. */ -static void +void delete_update_ssa (void) { unsigned i; @@ -2268,9 +2202,8 @@ delete_update_ssa (void) need_to_initialize_update_ssa_p = true; need_to_update_vops_p = false; - need_to_replace_names_p = false; BITMAP_FREE (syms_to_rename); - BITMAP_FREE (old_virtual_ssa_names); + BITMAP_FREE (update_ssa_stats.virtual_symbols); if (names_to_release) { @@ -2289,10 +2222,6 @@ delete_update_ssa (void) SSA_NAME_AUX (n) = NULL; } } - - /* Unmark all the names we may have protected from being released in - insert_updated_phi_nodes_for. */ - unmark_all_for_rewrite (); } @@ -2371,6 +2300,9 @@ mark_set_for_renaming (bitmap set) bitmap_iterator bi; unsigned i; + if (bitmap_empty_p (set)) + return; + if (need_to_initialize_update_ssa_p) init_update_ssa (); @@ -2420,8 +2352,6 @@ ssa_names_to_replace (void) EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, bitmap_set_bit (ret, i)); - bitmap_ior_into (ret, old_virtual_ssa_names); - return ret; } @@ -2450,8 +2380,7 @@ release_ssa_name_after_update_ssa (tree name) - If UPDATE_FLAGS == TODO_update_ssa, we are only interested in PHI nodes inside the region affected by the block that defines VAR and the blocks that define all its replacements. All these - definition blocks have been gathered by prepare_block_for_update - and they are stored in DEF_BLOCKS[VAR]->DEF_BLOCKS. + definition blocks are stored in DEF_BLOCKS[VAR]->DEF_BLOCKS. First, we compute the entry point to the region (ENTRY). This is given by the nearest common dominator to all the definition @@ -2521,31 +2450,6 @@ insert_updated_phi_nodes_for (tree var, bitmap *dfs, bitmap blocks, for the first time, so we need to compute the full IDF for it. */ bitmap_copy (pruned_idf, idf); - - /* There may already be PHI nodes for VAR in the flowgraph. - Some of them are no longer necessary. PRUNED_IDF is - the set of blocks that need PHI nodes for VAR and - DB.PHI_BLOCKS is the set of blocks that already contain a PHI - node for VAR. Therefore, the set DB.PHI_BLOCKS - PRUNED_IDF - gives us the set of blocks that contain PHI nodes which are - no longer needed. */ - if (!bitmap_empty_p (db->phi_blocks) && !bitmap_empty_p (pruned_idf)) - EXECUTE_IF_AND_COMPL_IN_BITMAP (db->phi_blocks, pruned_idf, 0, i, bi) - { - tree phi, prev; - unsigned ver; - - phi = find_phi_node_for (BASIC_BLOCK (i), var, &prev); - - /* Protect the name on PHI's LHS from being released into - the SSA name free list. Since we have still not - updated the SSA form of the program, there may be - instances of PHI's LHS in the IL. */ - ver = SSA_NAME_VERSION (PHI_RESULT (phi)); - mark_for_rewrite (PHI_RESULT (phi)); - release_ssa_name_after_update_ssa (PHI_RESULT (phi)); - remove_phi_node (phi, prev); - } } if (!bitmap_empty_p (pruned_idf)) @@ -2574,6 +2478,72 @@ insert_updated_phi_nodes_for (tree var, bitmap *dfs, bitmap blocks, } +/* Heuristic to determine whether SSA name mappings for virtual names + should be discarded and their symbols rewritten from scratch. When + there is a large number of mappings for virtual names, the + insertion of PHI nodes for the old names in the mappings takes + considerable more time than if we inserted PHI nodes for the + symbols instead. + + Currently the heuristic takes these stats into account: + + - Number of mappings for virtual SSA names. + - Number of distinct virtual symbols involved in those mappings. + + If the number of virtual mappings is much larger than the number of + virtual symbols, then it will be faster to compute PHI insertion + spots for the symbols. Even if this involves traversing the whole + CFG, which is what happens when symbols are renamed from scratch. */ + +static bool +switch_virtuals_to_full_rewrite_p (void) +{ + if (update_ssa_stats.num_virtual_mappings < (unsigned) MIN_VIRTUAL_MAPPINGS) + return false; + + if (update_ssa_stats.num_virtual_mappings + > (unsigned) VIRTUAL_MAPPINGS_TO_SYMS_RATIO + * update_ssa_stats.num_virtual_symbols) + return true; + + return false; +} + + +/* Remove every virtual mapping and mark all the affected virtual + symbols for renaming. */ + +static void +switch_virtuals_to_full_rewrite (void) +{ + unsigned i; + + if (dump_file) + { + fprintf (dump_file, "\nEnabled virtual name mapping heuristic.\n"); + fprintf (dump_file, "\tNumber of virtual mappings: %7u\n", + update_ssa_stats.num_virtual_mappings); + fprintf (dump_file, "\tNumber of unique virtual symbols: %7u\n", + update_ssa_stats.num_virtual_symbols); + fprintf (dump_file, "Updating FUD-chains from top of CFG will be " + "faster than processing\nthe name mappings.\n\n"); + } + + /* Remove all virtual names from NEW_SSA_NAMES and OLD_SSA_NAMES. + Note that it is not really necessary to remove the mappings from + REPL_TBL, that would only waste time. */ + EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, + if (!is_gimple_reg (ssa_name (i))) + RESET_BIT (new_ssa_names, i)); + + EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, + if (!is_gimple_reg (ssa_name (i))) + RESET_BIT (old_ssa_names, i)); + + bitmap_ior_into (syms_to_rename, update_ssa_stats.virtual_symbols); +} + + /* Given a set of newly created SSA names (NEW_SSA_NAMES) and a set of existing SSA names (OLD_SSA_NAMES), update the SSA form so that: @@ -2663,21 +2633,16 @@ update_ssa (unsigned update_flags) || update_flags == TODO_update_ssa_only_virtuals); /* If we only need to update virtuals, remove all the mappings for - real names before proceeding. */ + real names before proceeding. The caller is responsible for + having dealt with the name mappings before calling update_ssa. */ if (update_flags == TODO_update_ssa_only_virtuals) { sbitmap_zero (old_ssa_names); sbitmap_zero (new_ssa_names); htab_empty (repl_tbl); - need_to_replace_names_p = false; } - if (update_flags == TODO_update_ssa - || update_flags == TODO_update_ssa_full_phi - || update_flags == TODO_update_ssa_only_virtuals) - insert_phi_p = true; - else - insert_phi_p = false; + insert_phi_p = (update_flags != TODO_update_ssa_no_phi); if (insert_phi_p) { @@ -2704,54 +2669,82 @@ update_ssa (unsigned update_flags) blocks = BITMAP_ALLOC (NULL); - /* Determine the CFG region that we are going to update. First add - all the blocks that define each of the names in NEW_SSA_NAMES - and OLD_SSA_NAMES. */ - prepare_def_sites (blocks, insert_phi_p); - - /* Next, determine the nearest common dominator START_BB for all the - blocks in the region. */ - if (!bitmap_empty_p (syms_to_rename) || bitmap_empty_p (blocks)) + /* Clear the REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT flags + for every statement and PHI node. */ + FOR_EACH_BB (bb) { - /* If the region to update is seemingly empty, or if we have to - rename some symbols from scratch, we need to start the - process at the root of the CFG. + block_stmt_iterator si; + tree phi; - FIXME, it should be possible to determine the nearest block - that had a definition for each of the symbols that are marked - for updating. For now this seems more work than it's worth. */ - start_bb = ENTRY_BLOCK_PTR; + for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) + { + REWRITE_THIS_STMT (phi) = 0; + REGISTER_DEFS_IN_THIS_STMT (phi) = 0; + } + + for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si)) + { + tree stmt = bsi_stmt (si); + REWRITE_THIS_STMT (stmt) = 0; + REGISTER_DEFS_IN_THIS_STMT (stmt) = 0; + } + } + + /* Heuristic to avoid massive slow downs when the replacement + mappings include lots of virtual names. */ + if (insert_phi_p && switch_virtuals_to_full_rewrite_p ()) + switch_virtuals_to_full_rewrite (); + + /* If there are names defined in the replacement table, prepare + definition and use sites for all the names in NEW_SSA_NAMES and + OLD_SSA_NAMES. */ + if (sbitmap_first_set_bit (new_ssa_names) >= 0) + { + prepare_names_to_update (blocks, insert_phi_p); + + /* If all the names in NEW_SSA_NAMES had been marked for + removal, and there are no symbols to rename, then there's + nothing else to do. */ + if (sbitmap_first_set_bit (new_ssa_names) < 0 + && bitmap_empty_p (syms_to_rename)) + goto done; + } + + /* Next, determine the block at which to start the renaming process. */ + if (!bitmap_empty_p (syms_to_rename)) + { + /* If we have to rename some symbols from scratch, we need to + start the process at the root of the CFG. FIXME, it should + be possible to determine the nearest block that had a + definition for each of the symbols that are marked for + updating. For now this seems more work than it's worth. */ + start_bb = ENTRY_BLOCK_PTR; + + /* Traverse the CFG looking for definitions and uses of symbols + in SYMS_TO_RENAME. Mark interesting blocks and statements + and set local live-in information for the PHI placement + heuristics. */ + prepare_block_for_update (start_bb, blocks, insert_phi_p); } else - start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS, blocks); - - /* Traverse all the blocks dominated by START_BB. Mark interesting - blocks and statements and set local live-in information for the - PHI placement heuristics. */ - prepare_block_for_update (start_bb, insert_phi_p, blocks, true); - - /* If are going to insert PHI nodes, blocks in the dominance - frontier of START_BB may be affected. Note that we don't need to - visit the dominator children of blocks in the dominance frontier - of START_BB. None of the changes inside this region can affect - blocks on the outside. */ - if (insert_phi_p && start_bb->index >= 0) - EXECUTE_IF_SET_IN_BITMAP (dfs[start_bb->index], 0, i, bi) - prepare_block_for_update (BASIC_BLOCK (i), insert_phi_p, - blocks, false); + { + /* Otherwise, the entry block to the region is the nearest + common dominator for the blocks in BLOCKS. */ + start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS, blocks); + } /* If requested, insert PHI nodes at the iterated dominance frontier - of every block making new definitions for names in OLD_SSA_NAMES + of every block, creating new definitions for names in OLD_SSA_NAMES and for symbols in SYMS_TO_RENAME. */ if (insert_phi_p) { if (sbitmap_first_set_bit (old_ssa_names) >= 0) { - /* insert_updated_phi_nodes_for will call - add_new_name_mapping when inserting new PHI nodes, so the - set OLD_SSA_NAMES will grow while we are traversing it - (but it will not gain any new members). Copy - OLD_SSA_NAMES to a temporary for traversal. */ + /* insert_update_phi_nodes_for will call add_new_name_mapping + when inserting new PHI nodes, so the set OLD_SSA_NAMES + will grow while we are traversing it (but it will not + gain any new members). Copy OLD_SSA_NAMES to a temporary + for traversal. */ sbitmap tmp = sbitmap_alloc (old_ssa_names->n_bits); sbitmap_copy (tmp, old_ssa_names); EXECUTE_IF_SET_IN_SBITMAP (tmp, 0, i, @@ -2773,22 +2766,8 @@ update_ssa (unsigned update_flags) /* Reset the current definition for name and symbol before renaming the sub-graph. */ - if (update_flags == TODO_update_ssa_full_phi) - { - /* If we are not prunning the IDF for new PHI nodes, set the - current name of every GIMPLE register to NULL. This way, PHI - arguments coming from edges with uninitialized values will be - renamed to use the symbol's default definition. */ - EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, - set_current_def (ssa_name (i), NULL_TREE)); - } - else - { - /* Otherwise, set each old name to be its current reaching - definition. */ - EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, - set_current_def (ssa_name (i), NULL_TREE)); - } + EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, + set_current_def (ssa_name (i), NULL_TREE)); EXECUTE_IF_SET_IN_BITMAP (syms_to_rename, 0, i, bi) set_current_def (referenced_var (i), NULL_TREE); @@ -2809,7 +2788,7 @@ update_ssa (unsigned update_flags) int c; unsigned i; - dump_repl_tbl (dump_file); + dump_update_ssa (dump_file); fprintf (dump_file, "Incremental SSA update started at block: %d\n\n", start_bb->index); @@ -2833,6 +2812,7 @@ update_ssa (unsigned update_flags) } /* Free allocated memory. */ +done: if (insert_phi_p) { FOR_EACH_BB (bb) @@ -2845,453 +2825,3 @@ update_ssa (unsigned update_flags) timevar_pop (TV_TREE_SSA_INCREMENTAL); } - - -/*--------------------------------------------------------------------------- - Functions to fix a program in invalid SSA form into valid SSA - form. The main entry point here is rewrite_ssa_into_ssa. ----------------------------------------------------------------------------*/ - -/* Called after visiting basic block BB. Restore CURRDEFS to its - original value. */ - -static void -ssa_rewrite_finalize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED, - basic_block bb ATTRIBUTE_UNUSED) -{ - - /* Step 5. Restore the current reaching definition for each variable - referenced in the block (in reverse order). */ - while (VEC_length (tree, block_defs_stack) > 0) - { - tree var = VEC_pop (tree, block_defs_stack); - tree saved_def; - - if (var == NULL) - break; - - saved_def = VEC_pop (tree, block_defs_stack); - set_current_def (var, saved_def); - } -} - - -/* Register DEF (an SSA_NAME) to be a new definition for the original - ssa name VAR and push VAR's current reaching definition - into the stack pointed by BLOCK_DEFS_P. */ - -static void -ssa_register_new_def (tree var, tree def) -{ - tree currdef; - - /* If this variable is set in a single basic block and all uses are - dominated by the set(s) in that single basic block, then there is - nothing to do. TODO we should not be called at all, and just - keep the original name. */ - if (get_phi_state (var) == NEED_PHI_STATE_NO) - { - set_current_def (var, def); - return; - } - - currdef = get_current_def (var); - - /* Push the current reaching definition into *BLOCK_DEFS_P. This stack is - later used by the dominator tree callbacks to restore the reaching - definitions for all the variables defined in the block after a recursive - visit to all its immediately dominated blocks. */ - VEC_reserve (tree, heap, block_defs_stack, 2); - VEC_quick_push (tree, block_defs_stack, currdef); - VEC_quick_push (tree, block_defs_stack, var); - - /* Set the current reaching definition for VAR to be DEF. */ - set_current_def (var, def); -} - - -/* Same as rewrite_stmt, for rewriting ssa names. */ - -static void -ssa_rewrite_stmt (struct dom_walk_data *walk_data, - basic_block bb ATTRIBUTE_UNUSED, - block_stmt_iterator si) -{ - stmt_ann_t ann; - tree stmt, var; - ssa_op_iter iter; - use_operand_p use_p; - def_operand_p def_p; - sbitmap names_to_rename = walk_data->global_data; - - stmt = bsi_stmt (si); - ann = stmt_ann (stmt); - - if (dump_file && (dump_flags & TDF_DETAILS)) - { - fprintf (dump_file, "Renaming statement "); - print_generic_stmt (dump_file, stmt, TDF_SLIM); - fprintf (dump_file, "\n"); - } - - /* We have just scanned the code for operands. No statement should - be modified. */ - gcc_assert (!ann->modified); - - /* Step 1. Rewrite USES and VUSES in the statement. */ - FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES | SSA_OP_ALL_KILLS) - { - if (TEST_BIT (names_to_rename, SSA_NAME_VERSION (USE_FROM_PTR (use_p)))) - SET_USE (use_p, get_reaching_def (USE_FROM_PTR (use_p))); - } - - /* Step 2. Register the statement's DEF and VDEF operands. */ - FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS) - { - var = DEF_FROM_PTR (def_p); - - if (!TEST_BIT (names_to_rename, SSA_NAME_VERSION (var))) - continue; - - SET_DEF (def_p, duplicate_ssa_name (var, stmt)); - ssa_register_new_def (var, DEF_FROM_PTR (def_p)); - } -} - - -/* Ditto, for ssa name rewriting. */ - -static void -ssa_rewrite_phi_arguments (struct dom_walk_data *walk_data, basic_block bb) -{ - edge e; - sbitmap names_to_rename = walk_data->global_data; - use_operand_p op; - edge_iterator ei; - - FOR_EACH_EDGE (e, ei, bb->succs) - { - tree phi; - - if (e->dest == EXIT_BLOCK_PTR) - continue; - - for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi)) - { - op = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e); - if (TREE_CODE (USE_FROM_PTR (op)) != SSA_NAME) - continue; - - if (!TEST_BIT (names_to_rename, SSA_NAME_VERSION (USE_FROM_PTR (op)))) - continue; - - SET_USE (op, get_reaching_def (USE_FROM_PTR (op))); - if (e->flags & EDGE_ABNORMAL) - SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (op)) = 1; - } - } -} - -/* Ditto, for rewriting ssa names. */ - -static void -ssa_rewrite_initialize_block (struct dom_walk_data *walk_data, basic_block bb) -{ - tree phi, new_name; - sbitmap names_to_rename = walk_data->global_data; - edge e; - bool abnormal_phi; - edge_iterator ei; - - if (dump_file && (dump_flags & TDF_DETAILS)) - fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index); - - /* Mark the unwind point for this block. */ - VEC_safe_push (tree, heap, block_defs_stack, NULL_TREE); - - FOR_EACH_EDGE (e, ei, bb->preds) - if (e->flags & EDGE_ABNORMAL) - break; - abnormal_phi = (e != NULL); - - /* Step 1. Register new definitions for every PHI node in the block. - Conceptually, all the PHI nodes are executed in parallel and each PHI - node introduces a new version for the associated variable. */ - for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) - { - tree result = PHI_RESULT (phi); - - if (TEST_BIT (names_to_rename, SSA_NAME_VERSION (result))) - { - new_name = duplicate_ssa_name (result, phi); - SET_PHI_RESULT (phi, new_name); - - if (abnormal_phi) - SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name) = 1; - ssa_register_new_def (result, new_name); - } - } -} - - -/* Same as mark_def_sites, but works over SSA names. */ - -static void -ssa_mark_def_sites (struct dom_walk_data *walk_data, - basic_block bb, - block_stmt_iterator bsi) -{ - struct mark_def_sites_global_data *gd = walk_data->global_data; - bitmap kills = gd->kills; - size_t uid, def_uid; - tree stmt, use, def; - ssa_op_iter iter; - - /* Mark all the blocks that have definitions for each variable in the - names_to_rename bitmap. */ - stmt = bsi_stmt (bsi); - update_stmt_if_modified (stmt); - - /* If a variable is used before being set, then the variable is live - across a block boundary, so mark it live-on-entry to BB. */ - FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_ALL_USES | SSA_OP_ALL_KILLS) - { - uid = SSA_NAME_VERSION (use); - - if (TEST_BIT (gd->names_to_rename, uid) - && !bitmap_bit_p (kills, uid)) - set_livein_block (use, bb); - } - - /* Now process the definition made by this statement. Mark the - variables in KILLS. */ - FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS) - { - def_uid = SSA_NAME_VERSION (def); - - if (TEST_BIT (gd->names_to_rename, def_uid)) - { - set_def_block (def, bb, false); - bitmap_set_bit (kills, def_uid); - } - } -} - - -/* Block initialization routine for mark_def_sites. Clear the - KILLS bitmap at the start of each block. */ - -static void -ssa_mark_def_sites_initialize_block (struct dom_walk_data *walk_data, - basic_block bb) -{ - struct mark_def_sites_global_data *gd = walk_data->global_data; - bitmap kills = gd->kills; - tree phi, def; - unsigned def_uid; - - bitmap_clear (kills); - - for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) - { - def = PHI_RESULT (phi); - def_uid = SSA_NAME_VERSION (def); - - if (!TEST_BIT (gd->names_to_rename, def_uid)) - continue; - - set_def_block (def, bb, true); - bitmap_set_bit (kills, def_uid); - } -} - -/* Marks ssa names used as arguments of phis at the end of BB. */ - -static void -ssa_mark_phi_uses (struct dom_walk_data *walk_data, basic_block bb) -{ - struct mark_def_sites_global_data *gd = walk_data->global_data; - bitmap kills = gd->kills; - edge e; - tree phi, use; - unsigned uid; - edge_iterator ei; - - FOR_EACH_EDGE (e, ei, bb->succs) - { - if (e->dest == EXIT_BLOCK_PTR) - continue; - - for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi)) - { - use = PHI_ARG_DEF_FROM_EDGE (phi, e); - if (TREE_CODE (use) != SSA_NAME) - continue; - - uid = SSA_NAME_VERSION (use); - - if (TEST_BIT (gd->names_to_rename, uid) - && !bitmap_bit_p (kills, uid)) - set_livein_block (use, bb); - } - } -} - - -/* The marked ssa names may have more than one definition; - add PHI nodes and rewrite them to fix this. */ - -void -rewrite_ssa_into_ssa (void) -{ - bitmap *dfs; - basic_block bb; - struct dom_walk_data walk_data; - struct mark_def_sites_global_data mark_def_sites_global_data; - unsigned i; - sbitmap snames_to_rename; - bitmap to_rename; - bitmap_iterator bi; - - if (!any_marked_for_rewrite_p ()) - return; - to_rename = marked_ssa_names (); - - timevar_push (TV_TREE_SSA_OTHER); - - /* Allocate memory for the DEF_BLOCKS hash table. */ - def_blocks = htab_create (num_ssa_names, - def_blocks_hash, def_blocks_eq, def_blocks_free); - - /* Initialize dominance frontier and immediate dominator bitmaps. - Also count the number of predecessors for each block. Doing so - can save significant time during PHI insertion for large graphs. */ - dfs = (bitmap *) xmalloc (last_basic_block * sizeof (bitmap *)); - FOR_EACH_BB (bb) - dfs[bb->index] = BITMAP_ALLOC (NULL); - - /* Ensure that the dominance information is OK. */ - calculate_dominance_info (CDI_DOMINATORS); - - /* Compute dominance frontiers. */ - compute_dominance_frontiers (dfs); - - /* Setup callbacks for the generic dominator tree walker to find and - mark definition sites. */ - walk_data.walk_stmts_backward = false; - walk_data.dom_direction = CDI_DOMINATORS; - walk_data.interesting_blocks = NULL; - walk_data.initialize_block_local_data = NULL; - walk_data.before_dom_children_before_stmts - = ssa_mark_def_sites_initialize_block; - walk_data.before_dom_children_walk_stmts = ssa_mark_def_sites; - walk_data.before_dom_children_after_stmts = ssa_mark_phi_uses; - walk_data.after_dom_children_before_stmts = NULL; - walk_data.after_dom_children_walk_stmts = NULL; - walk_data.after_dom_children_after_stmts = NULL; - - snames_to_rename = sbitmap_alloc (num_ssa_names); - sbitmap_zero (snames_to_rename); - EXECUTE_IF_SET_IN_BITMAP (to_rename, 0, i, bi) - { - SET_BIT (snames_to_rename, i); - set_current_def (ssa_name (i), NULL_TREE); - } - - mark_def_sites_global_data.kills = BITMAP_ALLOC (NULL); - mark_def_sites_global_data.names_to_rename = snames_to_rename; - walk_data.global_data = &mark_def_sites_global_data; - - block_defs_stack = VEC_alloc (tree, heap, 10); - - /* We do not have any local data. */ - walk_data.block_local_data_size = 0; - - /* Initialize the dominator walker. */ - init_walk_dominator_tree (&walk_data); - - /* Recursively walk the dominator tree. */ - walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR); - - /* Finalize the dominator walker. */ - fini_walk_dominator_tree (&walk_data); - - /* We no longer need this bitmap, clear and free it. */ - BITMAP_FREE (mark_def_sites_global_data.kills); - - /* Insert PHI nodes at dominance frontiers of definition blocks. */ - insert_phi_nodes (dfs, to_rename); - - /* Rewrite all the basic blocks in the program. */ - timevar_push (TV_TREE_SSA_REWRITE_BLOCKS); - - /* Setup callbacks for the generic dominator tree walker. */ - walk_data.walk_stmts_backward = false; - walk_data.dom_direction = CDI_DOMINATORS; - walk_data.interesting_blocks = NULL; - walk_data.initialize_block_local_data = NULL; - walk_data.before_dom_children_before_stmts = ssa_rewrite_initialize_block; - walk_data.before_dom_children_walk_stmts = ssa_rewrite_stmt; - walk_data.before_dom_children_after_stmts = ssa_rewrite_phi_arguments; - walk_data.after_dom_children_before_stmts = NULL; - walk_data.after_dom_children_walk_stmts = NULL; - walk_data.after_dom_children_after_stmts = ssa_rewrite_finalize_block; - walk_data.global_data = snames_to_rename; - walk_data.block_local_data_size = 0; - - /* Initialize the dominator walker. */ - init_walk_dominator_tree (&walk_data); - - /* Recursively walk the dominator tree rewriting each statement in - each basic block. */ - walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR); - - /* Finalize the dominator walker. */ - fini_walk_dominator_tree (&walk_data); - - unmark_all_for_rewrite (); - - EXECUTE_IF_SET_IN_BITMAP (to_rename, 0, i, bi) - { - /* Free SSA_NAME_AUX. We don't have to zero it because - release_ssa_name will. */ - if (SSA_NAME_AUX (ssa_name (i))) - free (SSA_NAME_AUX (ssa_name (i))); - - release_ssa_name (ssa_name (i)); - } - - sbitmap_free (snames_to_rename); - - timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS); - - /* Debugging dumps. */ - if (dump_file && (dump_flags & TDF_STATS)) - { - dump_dfa_stats (dump_file); - dump_tree_ssa_stats (dump_file); - } - - /* Free allocated memory. */ - FOR_EACH_BB (bb) - BITMAP_FREE (dfs[bb->index]); - free (dfs); - - htab_delete (def_blocks); - -#ifdef ENABLE_CHECKING - for (i = 1; i < num_ssa_names; i++) - { - tree name = ssa_name (i); - if (!name) - continue; - - gcc_assert (SSA_NAME_AUX (name) == NULL); - } -#endif - - BITMAP_FREE (to_rename); - - VEC_free (tree, heap, block_defs_stack); - timevar_pop (TV_TREE_SSA_OTHER); -} diff --git a/gcc/tree-loop-linear.c b/gcc/tree-loop-linear.c index de75de4e531..5c289964774 100644 --- a/gcc/tree-loop-linear.c +++ b/gcc/tree-loop-linear.c @@ -373,6 +373,5 @@ linear_transform_loops (struct loops *loops) VEC_free (tree, heap, oldivs); VEC_free (tree, heap, invariants); scev_reset (); - update_ssa (TODO_update_ssa); - rewrite_into_loop_closed_ssa (NULL); + rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa_full_phi); } diff --git a/gcc/tree-phinodes.c b/gcc/tree-phinodes.c index 929480f80c0..0c6693b85c2 100644 --- a/gcc/tree-phinodes.c +++ b/gcc/tree-phinodes.c @@ -466,30 +466,6 @@ remove_phi_node (tree phi, tree prev) } -/* Find the first PHI node P in basic block BB for symbol SYM. If - PREV_P is given, the PHI node preceding P is stored in *PREV_P. */ - -tree -find_phi_node_for (basic_block bb, tree sym, tree *prev_p) -{ - tree phi; - - if (prev_p) - *prev_p = NULL_TREE; - - for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) - { - if (SSA_NAME_VAR (PHI_RESULT (phi)) == sym) - return phi; - - if (prev_p) - *prev_p = phi; - } - - return NULL_TREE; -} - - /* Reverse the order of PHI nodes in the chain PHI. Return the new head of the chain (old last PHI node). */ diff --git a/gcc/tree-ssa-dom.c b/gcc/tree-ssa-dom.c index d78886b0a72..d4e50606bde 100644 --- a/gcc/tree-ssa-dom.c +++ b/gcc/tree-ssa-dom.c @@ -474,7 +474,7 @@ tree_ssa_dominator_optimize (void) calculate_dominance_info (CDI_DOMINATORS); - rewrite_ssa_into_ssa (); + update_ssa (TODO_update_ssa); /* Reinitialize the various tables. */ bitmap_clear (nonzero_vars); diff --git a/gcc/tree-ssa-loop-ch.c b/gcc/tree-ssa-loop-ch.c index c465e2e15d4..18afec1e079 100644 --- a/gcc/tree-ssa-loop-ch.c +++ b/gcc/tree-ssa-loop-ch.c @@ -136,7 +136,7 @@ copy_loop_headers (void) loops = loop_optimizer_init (dump_file); if (!loops) return; - rewrite_into_loop_closed_ssa (NULL); + rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa); /* We do not try to keep the information about irreducible regions up-to-date. */ diff --git a/gcc/tree-ssa-loop-im.c b/gcc/tree-ssa-loop-im.c index b51b5e11806..e34a8211375 100644 --- a/gcc/tree-ssa-loop-im.c +++ b/gcc/tree-ssa-loop-im.c @@ -757,14 +757,8 @@ move_computations (void) fini_walk_dominator_tree (&walk_data); loop_commit_inserts (); - if (need_ssa_update_p ()) - update_ssa (TODO_update_ssa); - - /* The movement of LI code may cause violation of loop closed SSA - form invariants. TODO -- avoid these rewrites completely. - Information in virtual phi nodes is sufficient for it. */ - rewrite_into_loop_closed_ssa (NULL); + rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa); } /* Checks whether the statement defining variable *INDEX can be hoisted diff --git a/gcc/tree-ssa-loop-ivcanon.c b/gcc/tree-ssa-loop-ivcanon.c index 43f3c15f634..f2898b5c781 100644 --- a/gcc/tree-ssa-loop-ivcanon.c +++ b/gcc/tree-ssa-loop-ivcanon.c @@ -185,6 +185,8 @@ try_unroll_loop_completely (struct loops *loops ATTRIBUTE_UNUSED, COND_EXPR_COND (cond) = do_exit; update_stmt (cond); + update_ssa (TODO_update_ssa); + if (dump_file && (dump_flags & TDF_DETAILS)) fprintf (dump_file, "Unrolled loop %d completely.\n", loop->num); diff --git a/gcc/tree-ssa-loop-ivopts.c b/gcc/tree-ssa-loop-ivopts.c index d1bf6a52dc1..d9ffc658d06 100644 --- a/gcc/tree-ssa-loop-ivopts.c +++ b/gcc/tree-ssa-loop-ivopts.c @@ -5416,10 +5416,8 @@ tree_ssa_iv_optimize (struct loops *loops) FOR_EACH_BB (bb) for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si)) update_stmt (bsi_stmt (si)); - - update_ssa (TODO_update_ssa); } - rewrite_into_loop_closed_ssa (NULL); + rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa); tree_ssa_iv_optimize_finalize (loops, &data); } diff --git a/gcc/tree-ssa-loop-manip.c b/gcc/tree-ssa-loop-manip.c index 02b21a32f94..f16ec3f7522 100644 --- a/gcc/tree-ssa-loop-manip.c +++ b/gcc/tree-ssa-loop-manip.c @@ -139,11 +139,9 @@ add_exit_phis_edge (basic_block exit, tree use) return; phi = create_phi_node (use, exit); - + create_new_def_for (PHI_RESULT (phi), phi, PHI_RESULT_PTR (phi)); FOR_EACH_EDGE (e, ei, exit->preds) add_phi_arg (phi, use, e); - - SSA_NAME_DEF_STMT (use) = def_stmt; } /* Add exit phis for VAR that is used in LIVEIN. @@ -215,10 +213,11 @@ get_loops_exits (void) /* For USE in BB, if it is used outside of the loop it is defined in, mark it for rewrite. Record basic block BB where it is used - to USE_BLOCKS. */ + to USE_BLOCKS. Record the ssa name index to NEED_PHIS bitmap. */ static void -find_uses_to_rename_use (basic_block bb, tree use, bitmap *use_blocks) +find_uses_to_rename_use (basic_block bb, tree use, bitmap *use_blocks, + bitmap need_phis) { unsigned ver; basic_block def_bb; @@ -227,6 +226,10 @@ find_uses_to_rename_use (basic_block bb, tree use, bitmap *use_blocks) if (TREE_CODE (use) != SSA_NAME) return; + /* We don't need to keep virtual operands in loop-closed form. */ + if (!is_gimple_reg (use)) + return; + ver = SSA_NAME_VERSION (use); def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (use)); if (!def_bb) @@ -241,31 +244,32 @@ find_uses_to_rename_use (basic_block bb, tree use, bitmap *use_blocks) use_blocks[ver] = BITMAP_ALLOC (NULL); bitmap_set_bit (use_blocks[ver], bb->index); - if (!flow_bb_inside_loop_p (def_loop, bb)) - mark_for_rewrite (use); + bitmap_set_bit (need_phis, ver); } /* For uses in STMT, mark names that are used outside of the loop they are defined to rewrite. Record the set of blocks in that the ssa - names are defined to USE_BLOCKS. */ + names are defined to USE_BLOCKS and the ssa names themselves to + NEED_PHIS. */ static void -find_uses_to_rename_stmt (tree stmt, bitmap *use_blocks) +find_uses_to_rename_stmt (tree stmt, bitmap *use_blocks, bitmap need_phis) { ssa_op_iter iter; tree var; basic_block bb = bb_for_stmt (stmt); FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_USES | SSA_OP_ALL_KILLS) - find_uses_to_rename_use (bb, var, use_blocks); + find_uses_to_rename_use (bb, var, use_blocks, need_phis); } /* Marks names that are used in BB and outside of the loop they are defined in for rewrite. Records the set of blocks in that the ssa - names are defined to USE_BLOCKS. */ + names are defined to USE_BLOCKS. Record the SSA names that will + need exit PHIs in NEED_PHIS. */ static void -find_uses_to_rename_bb (basic_block bb, bitmap *use_blocks) +find_uses_to_rename_bb (basic_block bb, bitmap *use_blocks, bitmap need_phis) { block_stmt_iterator bsi; edge e; @@ -275,10 +279,10 @@ find_uses_to_rename_bb (basic_block bb, bitmap *use_blocks) FOR_EACH_EDGE (e, ei, bb->succs) for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi)) find_uses_to_rename_use (bb, PHI_ARG_DEF_FROM_EDGE (phi, e), - use_blocks); + use_blocks, need_phis); for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi)) - find_uses_to_rename_stmt (bsi_stmt (bsi), use_blocks); + find_uses_to_rename_stmt (bsi_stmt (bsi), use_blocks, need_phis); } /* Marks names that are used outside of the loop they are defined in @@ -287,24 +291,24 @@ find_uses_to_rename_bb (basic_block bb, bitmap *use_blocks) scan only blocks in this set. */ static void -find_uses_to_rename (bitmap changed_bbs, bitmap *use_blocks) +find_uses_to_rename (bitmap changed_bbs, bitmap *use_blocks, bitmap need_phis) { basic_block bb; unsigned index; bitmap_iterator bi; - if (changed_bbs) + if (changed_bbs && !bitmap_empty_p (changed_bbs)) { EXECUTE_IF_SET_IN_BITMAP (changed_bbs, 0, index, bi) { - find_uses_to_rename_bb (BASIC_BLOCK (index), use_blocks); + find_uses_to_rename_bb (BASIC_BLOCK (index), use_blocks, need_phis); } } else { FOR_EACH_BB (bb) { - find_uses_to_rename_bb (bb, use_blocks); + find_uses_to_rename_bb (bb, use_blocks, need_phis); } } } @@ -336,43 +340,42 @@ find_uses_to_rename (bitmap changed_bbs, bitmap *use_blocks) base 99 and step 1. If CHANGED_BBS is not NULL, we look for uses outside loops only in - the basic blocks in this set. */ + the basic blocks in this set. + + UPDATE_FLAG is used in the call to update_ssa. See + TODO_update_ssa* for documentation. */ void -rewrite_into_loop_closed_ssa (bitmap changed_bbs) +rewrite_into_loop_closed_ssa (bitmap changed_bbs, unsigned update_flag) { bitmap loop_exits = get_loops_exits (); bitmap *use_blocks; - unsigned i; - bitmap names_to_rename; + unsigned i, old_num_ssa_names; + bitmap names_to_rename = BITMAP_ALLOC (NULL); - gcc_assert (!any_marked_for_rewrite_p ()); + /* If the pass has caused the SSA form to be out-of-date, update it + now. */ + update_ssa (update_flag); - use_blocks = xcalloc (num_ssa_names, sizeof (bitmap)); + old_num_ssa_names = num_ssa_names; + use_blocks = xcalloc (old_num_ssa_names, sizeof (bitmap)); /* Find the uses outside loops. */ - find_uses_to_rename (changed_bbs, use_blocks); + find_uses_to_rename (changed_bbs, use_blocks, names_to_rename); - if (!any_marked_for_rewrite_p ()) - { - free (use_blocks); - BITMAP_FREE (loop_exits); - return; - } - - /* Add the phi nodes on exits of the loops for the names we need to + /* Add the PHI nodes on exits of the loops for the names we need to rewrite. */ - names_to_rename = marked_ssa_names (); add_exit_phis (names_to_rename, use_blocks, loop_exits); - for (i = 0; i < num_ssa_names; i++) + for (i = 0; i < old_num_ssa_names; i++) BITMAP_FREE (use_blocks[i]); free (use_blocks); BITMAP_FREE (loop_exits); BITMAP_FREE (names_to_rename); - /* Do the rewriting. */ - rewrite_ssa_into_ssa (); + /* Fix up all the names found to be used outside their original + loops. */ + update_ssa (TODO_update_ssa); } /* Check invariants of the loop closed ssa form for the USE in BB. */ @@ -383,7 +386,7 @@ check_loop_closed_ssa_use (basic_block bb, tree use) tree def; basic_block def_bb; - if (TREE_CODE (use) != SSA_NAME) + if (TREE_CODE (use) != SSA_NAME || !is_gimple_reg (use)) return; def = SSA_NAME_DEF_STMT (use); @@ -400,7 +403,7 @@ check_loop_closed_ssa_stmt (basic_block bb, tree stmt) ssa_op_iter iter; tree var; - FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_USES) + FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_USES | SSA_OP_ALL_KILLS) check_loop_closed_ssa_use (bb, var); } @@ -414,6 +417,9 @@ verify_loop_closed_ssa (void) tree phi; unsigned i; + if (current_loops == NULL) + return; + verify_ssa (false); FOR_EACH_BB (bb) @@ -566,53 +572,15 @@ copy_phi_node_args (unsigned first_new_block) BASIC_BLOCK (i)->rbi->duplicated = 0; } -/* Renames variables in the area copied by tree_duplicate_loop_to_header_edge. - FIRST_NEW_BLOCK is the first block in the copied area. DEFINITIONS is - a bitmap of all ssa names defined inside the loop. */ - -static void -rename_variables (unsigned first_new_block, bitmap definitions) -{ - unsigned i, copy_number = 0; - basic_block bb; - htab_t ssa_name_map = NULL; - - for (i = first_new_block; i < (unsigned) last_basic_block; i++) - { - bb = BASIC_BLOCK (i); - /* We assume that first come all blocks from the first copy, then all - blocks from the second copy, etc. */ - if (copy_number != (unsigned) bb->rbi->copy_number) - { - allocate_ssa_names (definitions, &ssa_name_map); - copy_number = bb->rbi->copy_number; - } +/* The same as cfgloopmanip.c:duplicate_loop_to_header_edge, but also + updates the PHI nodes at start of the copied region. In order to + achieve this, only loops whose exits all lead to the same location + are handled. - rewrite_to_new_ssa_names_bb (bb, ssa_name_map); - } - - htab_delete (ssa_name_map); -} - -/* Sets SSA_NAME_DEF_STMT for results of all phi nodes in BB. */ - -static void -set_phi_def_stmts (basic_block bb) -{ - tree phi; - - for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) - SSA_NAME_DEF_STMT (PHI_RESULT (phi)) = phi; -} - -/* The same as cfgloopmanip.c:duplicate_loop_to_header_edge, but also updates - ssa. In order to achieve this, only loops whose exits all lead to the same - location are handled. - - FIXME: we create some degenerate phi nodes that could be avoided by copy - propagating them instead. Unfortunately this is not completely - straightforward due to problems with constant folding. */ + Notice that we do not completely update the SSA web after + duplication. The caller is responsible for calling update_ssa + after the loop has been duplicated. */ bool tree_duplicate_loop_to_header_edge (struct loop *loop, edge e, @@ -622,9 +590,6 @@ tree_duplicate_loop_to_header_edge (struct loop *loop, edge e, unsigned int *n_to_remove, int flags) { unsigned first_new_block; - basic_block bb; - unsigned i; - bitmap definitions; if (!(loops->state & LOOPS_HAVE_SIMPLE_LATCHES)) return false; @@ -635,8 +600,6 @@ tree_duplicate_loop_to_header_edge (struct loop *loop, edge e, verify_loop_closed_ssa (); #endif - gcc_assert (!any_marked_for_rewrite_p ()); - first_new_block = last_basic_block; if (!duplicate_loop_to_header_edge (loop, e, loops, ndupl, wont_exit, orig, to_remove, n_to_remove, flags)) @@ -648,31 +611,7 @@ tree_duplicate_loop_to_header_edge (struct loop *loop, edge e, /* Copy the phi node arguments. */ copy_phi_node_args (first_new_block); - /* Rename the variables. */ - definitions = marked_ssa_names (); - rename_variables (first_new_block, definitions); - unmark_all_for_rewrite (); - BITMAP_FREE (definitions); - - /* For some time we have the identical ssa names as results in multiple phi - nodes. When phi node is resized, it sets SSA_NAME_DEF_STMT of its result - to the new copy. This means that we cannot easily ensure that the ssa - names defined in those phis are pointing to the right one -- so just - recompute SSA_NAME_DEF_STMT for them. */ - - for (i = first_new_block; i < (unsigned) last_basic_block; i++) - { - bb = BASIC_BLOCK (i); - set_phi_def_stmts (bb); - if (bb->rbi->copy_number == 1) - set_phi_def_stmts (bb->rbi->original); - } - scev_reset (); -#ifdef ENABLE_CHECKING - verify_loop_closed_ssa (); -#endif return true; } - diff --git a/gcc/tree-ssa-loop-unswitch.c b/gcc/tree-ssa-loop-unswitch.c index 7b8855e7f58..e00fe189514 100644 --- a/gcc/tree-ssa-loop-unswitch.c +++ b/gcc/tree-ssa-loop-unswitch.c @@ -101,10 +101,6 @@ tree_ssa_unswitch_loops (struct loops *loops) continue; changed |= tree_unswitch_single_loop (loops, loop, 0); -#ifdef ENABLE_CHECKING - verify_dominators (CDI_DOMINATORS); - verify_loop_structure (loops); -#endif } if (changed) @@ -260,6 +256,9 @@ tree_unswitch_single_loop (struct loops *loops, struct loop *loop, int num) if (!nloop) return changed; + /* Update the SSA form after unswitching. */ + update_ssa (TODO_update_ssa); + /* Invoke itself on modified loops. */ tree_unswitch_single_loop (loops, nloop, num + 1); tree_unswitch_single_loop (loops, loop, num + 1); diff --git a/gcc/tree-ssa-loop.c b/gcc/tree-ssa-loop.c index 03c3249fe2e..b86820ecfa4 100644 --- a/gcc/tree-ssa-loop.c +++ b/gcc/tree-ssa-loop.c @@ -40,7 +40,7 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA /* The loop tree currently optimized. */ -struct loops *current_loops; +struct loops *current_loops = NULL; /* Initializes the loop structures. DUMP is the file to that the details about the analysis should be dumped. */ @@ -53,8 +53,7 @@ tree_loop_optimizer_init (FILE *dump) if (!loops) return NULL; - update_ssa (TODO_update_ssa); - rewrite_into_loop_closed_ssa (NULL); + rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa); return loops; } diff --git a/gcc/tree-ssa.c b/gcc/tree-ssa.c index 1538910993e..125e6c81583 100644 --- a/gcc/tree-ssa.c +++ b/gcc/tree-ssa.c @@ -644,6 +644,10 @@ verify_ssa (bool check_modified_stmt) enum dom_state orig_dom_state = dom_computed[CDI_DOMINATORS]; bitmap names_defined_in_bb = BITMAP_ALLOC (NULL); + gcc_assert (!need_ssa_update_p ()); + + verify_stmts (); + timevar_push (TV_TREE_SSA_VERIFY); /* Keep track of SSA names present in the IL. */ @@ -751,9 +755,7 @@ verify_ssa (bool check_modified_stmt) } FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_ALL_DEFS) - { - bitmap_set_bit (names_defined_in_bb, SSA_NAME_VERSION (op)); - } + bitmap_set_bit (names_defined_in_bb, SSA_NAME_VERSION (op)); } bitmap_clear (names_defined_in_bb); @@ -763,6 +765,7 @@ verify_ssa (bool check_modified_stmt) verify_alias_info (); free (definition_block); + /* Restore the dominance information to its prior known state, so that we do not perturb the compiler's subsequent behavior. */ if (orig_dom_state == DOM_NONE) @@ -835,6 +838,7 @@ delete_tree_ssa (void) addressable_vars = NULL; modified_noreturn_calls = NULL; aliases_computed_p = false; + gcc_assert (!need_ssa_update_p ()); } diff --git a/gcc/tree-ssanames.c b/gcc/tree-ssanames.c index b0c430b8f96..0f0381f4a98 100644 --- a/gcc/tree-ssanames.c +++ b/gcc/tree-ssanames.c @@ -61,9 +61,6 @@ Boston, MA 02111-1307, USA. */ /* Array of all SSA_NAMEs used in the function. */ varray_type ssa_names; -/* Bitmap of ssa names marked for rewriting. */ -static bitmap ssa_names_to_rewrite; - /* Free list of SSA_NAMEs. This list is wiped at the end of each function after we leave SSA form. */ static GTY (()) tree free_ssanames; @@ -77,54 +74,6 @@ unsigned int ssa_name_nodes_reused; unsigned int ssa_name_nodes_created; #endif -/* Returns true if ssa name VAR is marked for rewrite. */ - -bool -marked_for_rewrite_p (tree var) -{ - return bitmap_bit_p (ssa_names_to_rewrite, SSA_NAME_VERSION (var)); -} - -/* Returns true if any ssa name is marked for rewrite. */ - -bool -any_marked_for_rewrite_p (void) -{ - if (!ssa_names_to_rewrite) - return false; - - return !bitmap_empty_p (ssa_names_to_rewrite); -} - -/* Mark ssa name VAR for rewriting. */ - -void -mark_for_rewrite (tree var) -{ - bitmap_set_bit (ssa_names_to_rewrite, SSA_NAME_VERSION (var)); -} - -/* Unmark all ssa names marked for rewrite. */ - -void -unmark_all_for_rewrite (void) -{ - bitmap_clear (ssa_names_to_rewrite); -} - -/* Return the bitmap of ssa names to rewrite. Copy the bitmap, - so that the optimizers cannot access internals directly */ - -bitmap -marked_ssa_names (void) -{ - bitmap ret = BITMAP_ALLOC (NULL); - - bitmap_copy (ret, ssa_names_to_rewrite); - - return ret; -} - /* Initialize management of SSA_NAMEs. */ void @@ -138,7 +87,6 @@ init_ssanames (void) large. */ VARRAY_PUSH_TREE (ssa_names, NULL_TREE); free_ssanames = NULL; - ssa_names_to_rewrite = BITMAP_ALLOC (NULL); } /* Finalize management of SSA_NAMEs. */ @@ -146,7 +94,6 @@ init_ssanames (void) void fini_ssanames (void) { - BITMAP_FREE (ssa_names_to_rewrite); ggc_free (ssa_names); ssa_names = NULL; free_ssanames = NULL; @@ -237,11 +184,13 @@ release_ssa_name (tree var) if (var == var_ann (SSA_NAME_VAR (var))->default_def) return; - /* If the ssa name is marked for rewriting, it may have multiple definitions, - but we may happen to remove just one of them. So do not remove the - ssa name now. */ - if (marked_for_rewrite_p (var)) - return; + /* If VAR has been registered for SSA updating, don't remove it. + After update_ssa has run, the name will be released. */ + if (name_registered_for_update_p (var)) + { + release_ssa_name_after_update_ssa (var); + return; + } /* release_ssa_name can be called multiple times on a single SSA_NAME. However, it should only end up on our free list one time. We @@ -304,7 +253,7 @@ duplicate_ssa_name (tree name, tree stmt) /* Creates a duplicate of the ptr_info_def at PTR_INFO for use by - the ssa name NAME. */ + the SSA name NAME. */ void duplicate_ssa_name_ptr_info (tree name, struct ptr_info_def *ptr_info) diff --git a/gcc/tree-vect-transform.c b/gcc/tree-vect-transform.c index f07669bb8f7..7e2f37dbd67 100644 --- a/gcc/tree-vect-transform.c +++ b/gcc/tree-vect-transform.c @@ -1991,6 +1991,11 @@ vect_transform_loop (loop_vec_info loop_vinfo, slpeel_make_loop_iterate_ntimes (loop, ratio); + /* The memory tags and pointers in vectorized statements need to + have their SSA forms updated. FIXME, why can't this be delayed + until all the loops have been transformed? */ + update_ssa (TODO_update_ssa); + if (vect_print_dump_info (REPORT_VECTORIZED_LOOPS, LOOP_LOC (loop_vinfo))) fprintf (vect_dump, "LOOP VECTORIZED."); } diff --git a/gcc/tree-vectorizer.c b/gcc/tree-vectorizer.c index 13b2281e174..ed95eba2d80 100644 --- a/gcc/tree-vectorizer.c +++ b/gcc/tree-vectorizer.c @@ -159,11 +159,8 @@ static void slpeel_update_phi_nodes_for_guard2 (edge, struct loop *, bool, basic_block *); static edge slpeel_add_loop_guard (basic_block, tree, basic_block, basic_block); -static void allocate_new_names (bitmap); static void rename_use_op (use_operand_p); -static void rename_def_op (def_operand_p, tree); static void rename_variables_in_bb (basic_block); -static void free_new_names (bitmap); static void rename_variables_in_loop (struct loop *); /************************************************************************* @@ -188,72 +185,25 @@ unsigned int vect_loops_num; *************************************************************************/ -/* For each definition in DEFINITIONS this function allocates - new ssa name. */ - -static void -allocate_new_names (bitmap definitions) -{ - unsigned ver; - bitmap_iterator bi; - - EXECUTE_IF_SET_IN_BITMAP (definitions, 0, ver, bi) - { - tree def = ssa_name (ver); - tree *new_name_ptr = xmalloc (sizeof (tree)); - - bool abnormal = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def); - - *new_name_ptr = duplicate_ssa_name (def, SSA_NAME_DEF_STMT (def)); - SSA_NAME_OCCURS_IN_ABNORMAL_PHI (*new_name_ptr) = abnormal; - - SSA_NAME_AUX (def) = new_name_ptr; - } -} - - /* Renames the use *OP_P. */ static void rename_use_op (use_operand_p op_p) { - tree *new_name_ptr; + tree new_name; if (TREE_CODE (USE_FROM_PTR (op_p)) != SSA_NAME) return; - new_name_ptr = SSA_NAME_AUX (USE_FROM_PTR (op_p)); - - /* Something defined outside of the loop. */ - if (!new_name_ptr) - return; - - /* An ordinary ssa name defined in the loop. */ - - SET_USE (op_p, *new_name_ptr); -} - - -/* Renames the def *OP_P in statement STMT. */ - -static void -rename_def_op (def_operand_p op_p, tree stmt) -{ - tree *new_name_ptr; - - if (TREE_CODE (DEF_FROM_PTR (op_p)) != SSA_NAME) - return; - - new_name_ptr = SSA_NAME_AUX (DEF_FROM_PTR (op_p)); + new_name = get_current_def (USE_FROM_PTR (op_p)); /* Something defined outside of the loop. */ - if (!new_name_ptr) + if (!new_name) return; /* An ordinary ssa name defined in the loop. */ - SET_DEF (op_p, *new_name_ptr); - SSA_NAME_DEF_STMT (DEF_FROM_PTR (op_p)) = stmt; + SET_USE (op_p, new_name); } @@ -268,7 +218,6 @@ rename_variables_in_bb (basic_block bb) stmt_ann_t ann; use_optype uses; vuse_optype vuses; - def_optype defs; v_may_def_optype v_may_defs; v_must_def_optype v_must_defs; unsigned i; @@ -276,9 +225,6 @@ rename_variables_in_bb (basic_block bb) edge_iterator ei; struct loop *loop = bb->loop_father; - for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) - rename_def_op (PHI_RESULT_PTR (phi), phi); - for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi)) { stmt = bsi_stmt (bsi); @@ -288,27 +234,17 @@ rename_variables_in_bb (basic_block bb) for (i = 0; i < NUM_USES (uses); i++) rename_use_op (USE_OP_PTR (uses, i)); - defs = DEF_OPS (ann); - for (i = 0; i < NUM_DEFS (defs); i++) - rename_def_op (DEF_OP_PTR (defs, i), stmt); - vuses = VUSE_OPS (ann); for (i = 0; i < NUM_VUSES (vuses); i++) rename_use_op (VUSE_OP_PTR (vuses, i)); v_may_defs = V_MAY_DEF_OPS (ann); for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++) - { - rename_use_op (V_MAY_DEF_OP_PTR (v_may_defs, i)); - rename_def_op (V_MAY_DEF_RESULT_PTR (v_may_defs, i), stmt); - } + rename_use_op (V_MAY_DEF_OP_PTR (v_may_defs, i)); v_must_defs = V_MUST_DEF_OPS (ann); for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++) - { - rename_use_op (V_MUST_DEF_KILL_PTR (v_must_defs, i)); - rename_def_op (V_MUST_DEF_RESULT_PTR (v_must_defs, i), stmt); - } + rename_use_op (V_MUST_DEF_KILL_PTR (v_must_defs, i)); } FOR_EACH_EDGE (e, ei, bb->succs) @@ -321,27 +257,6 @@ rename_variables_in_bb (basic_block bb) } -/* Releases the structures holding the new ssa names. */ - -static void -free_new_names (bitmap definitions) -{ - unsigned ver; - bitmap_iterator bi; - - EXECUTE_IF_SET_IN_BITMAP (definitions, 0, ver, bi) - { - tree def = ssa_name (ver); - - if (SSA_NAME_AUX (def)) - { - free (SSA_NAME_AUX (def)); - SSA_NAME_AUX (def) = NULL; - } - } -} - - /* Renames variables in new generated LOOP. */ static void @@ -370,7 +285,7 @@ static void slpeel_update_phis_for_duplicate_loop (struct loop *orig_loop, struct loop *new_loop, bool after) { - tree *new_name_ptr, new_ssa_name; + tree new_ssa_name; tree phi_new, phi_orig; tree def; edge orig_loop_latch = loop_latch_edge (orig_loop); @@ -422,13 +337,12 @@ slpeel_update_phis_for_duplicate_loop (struct loop *orig_loop, if (TREE_CODE (def) != SSA_NAME) continue; - new_name_ptr = SSA_NAME_AUX (def); - if (!new_name_ptr) + new_ssa_name = get_current_def (def); + if (!new_ssa_name) /* Something defined outside of the loop. */ continue; /* An ordinary ssa name defined in the loop. */ - new_ssa_name = *new_name_ptr; add_phi_arg (phi_new, new_ssa_name, loop_latch_edge (new_loop)); /* step 3 (case 1). */ @@ -565,9 +479,9 @@ slpeel_update_phis_for_duplicate_loop (struct loop *orig_loop, new_merge next_bb - The ssa-names defined in the original loop have an SSA_NAME_AUX pointer - that records the corresponding new ssa-name used in the new duplicated - loop copy. + The SSA names defined in the original loop have a current + reaching definition that that records the corresponding new + ssa-name used in the new duplicated loop copy. */ /* Function slpeel_update_phi_nodes_for_guard1 @@ -603,7 +517,6 @@ slpeel_update_phi_nodes_for_guard1 (edge guard_edge, struct loop *loop, { tree orig_phi, new_phi; tree update_phi, update_phi2; - tree *new_name_ptr, *new_name_ptr2; tree guard_arg, loop_arg; basic_block new_merge_bb = guard_edge->dest; edge e = EDGE_SUCC (new_merge_bb, 0); @@ -656,31 +569,28 @@ slpeel_update_phi_nodes_for_guard1 (edge guard_edge, struct loop *loop, gcc_assert (PHI_ARG_DEF_FROM_EDGE (update_phi2, new_exit_e) == loop_arg); SET_PHI_ARG_DEF (update_phi2, new_exit_e->dest_idx, PHI_RESULT (new_phi)); - /* 2.4. Record the newly created name in SSA_NAME_AUX. + /* 2.4. Record the newly created name with set_current_def. We want to find a name such that - name = *(SSA_NAME_AUX (orig_loop_name)) - and to set its SSA_NAME_AUX as follows: - *(SSA_NAME_AUX (name)) = new_phi_name + name = get_current_def (orig_loop_name) + and to set its current definition as follows: + set_current_def (name, new_phi_name) If LOOP is a new loop then loop_arg is already the name we're looking for. If LOOP is the original loop, then loop_arg is the orig_loop_name and the relevant name is recorded in its - SSA_NAME_AUX */ + current reaching definition. */ if (is_new_loop) current_new_name = loop_arg; else { - new_name_ptr = SSA_NAME_AUX (loop_arg); - gcc_assert (new_name_ptr); - current_new_name = *new_name_ptr; + current_new_name = get_current_def (loop_arg); + gcc_assert (current_new_name); } #ifdef ENABLE_CHECKING - gcc_assert (! SSA_NAME_AUX (current_new_name)); + gcc_assert (get_current_def (current_new_name) == NULL_TREE); #endif - new_name_ptr2 = xmalloc (sizeof (tree)); - *new_name_ptr2 = PHI_RESULT (new_phi); - SSA_NAME_AUX (current_new_name) = new_name_ptr2; + set_current_def (current_new_name, PHI_RESULT (new_phi)); bitmap_set_bit (*defs, SSA_NAME_VERSION (current_new_name)); } @@ -720,13 +630,12 @@ slpeel_update_phi_nodes_for_guard2 (edge guard_edge, struct loop *loop, { tree orig_phi, new_phi; tree update_phi, update_phi2; - tree *new_name_ptr, *new_name_ptr2; tree guard_arg, loop_arg; basic_block new_merge_bb = guard_edge->dest; edge e = EDGE_SUCC (new_merge_bb, 0); basic_block update_bb = e->dest; edge new_exit_e; - tree orig_def; + tree orig_def, orig_def_new_name; tree new_name, new_name2; tree arg; @@ -741,7 +650,7 @@ slpeel_update_phi_nodes_for_guard2 (edge guard_edge, struct loop *loop, { orig_phi = update_phi; orig_def = PHI_ARG_DEF_FROM_EDGE (orig_phi, e); - new_name_ptr = SSA_NAME_AUX (orig_def); + orig_def_new_name = get_current_def (orig_def); arg = NULL_TREE; /** 1. Handle new-merge-point phis **/ @@ -751,19 +660,17 @@ slpeel_update_phi_nodes_for_guard2 (edge guard_edge, struct loop *loop, new_merge_bb); /* 1.2. NEW_MERGE_BB has two incoming edges: GUARD_EDGE and the exit-edge - of LOOP. Set the two phi args in NEW_PHI for these edges: */ + of LOOP. Set the two PHI args in NEW_PHI for these edges: */ new_name = orig_def; new_name2 = NULL_TREE; - if (new_name_ptr) + if (orig_def_new_name) { - new_name = *new_name_ptr; - new_name_ptr2 = SSA_NAME_AUX (new_name); - if (new_name_ptr2) - /* Some variables have both loop-entry-phis and loop-exit-phis. - Such variables were given yet newer names by phis placed in - guard_bb by slpeel_update_phi_nodes_for_guard1. I.e: - new_name2 = SSA_NAME_AUX (SSA_NAME_AUX (orig_name)). */ - new_name2 = *new_name_ptr2; + new_name = orig_def_new_name; + /* Some variables have both loop-entry-phis and loop-exit-phis. + Such variables were given yet newer names by phis placed in + guard_bb by slpeel_update_phi_nodes_for_guard1. I.e: + new_name2 = get_current_def (get_current_def (orig_name)). */ + new_name2 = get_current_def (new_name); } if (is_new_loop) @@ -804,20 +711,22 @@ slpeel_update_phi_nodes_for_guard2 (edge guard_edge, struct loop *loop, /** 3. Handle loop-closed-ssa-form phis for first loop **/ - /* 3.1. Find the relevant names that need an exit-phi in GUARD_BB, i.e. - names for which slpeel_update_phi_nodes_for_guard1 had not already - created a phi node. This is the case for names that are used outside + /* 3.1. Find the relevant names that need an exit-phi in + GUARD_BB, i.e. names for which + slpeel_update_phi_nodes_for_guard1 had not already created a + phi node. This is the case for names that are used outside the loop (and therefore need an exit phi) but are not updated - across loop iterations (and therefore don't have a loop-header-phi). - - slpeel_update_phi_nodes_for_guard1 is responsible for creating - loop-exit phis in GUARD_BB for names that have a loop-header-phi. When - such a phi is created we also record the new name in SSA_NAME_AUX. If - this new name exists, then guard_arg was set to this new name - (see 1.2 above). Therefore, if guard_arg is not this new name, this is - an indication that an exit-phi in GUARD_BB was not yet created, so we - take care of it here. - */ + across loop iterations (and therefore don't have a + loop-header-phi). + + slpeel_update_phi_nodes_for_guard1 is responsible for + creating loop-exit phis in GUARD_BB for names that have a + loop-header-phi. When such a phi is created we also record + the new name in its current definition. If this new name + exists, then guard_arg was set to this new name (see 1.2 + above). Therefore, if guard_arg is not this new name, this + is an indication that an exit-phi in GUARD_BB was not yet + created, so we take care of it here. */ if (guard_arg == new_name2) continue; arg = guard_arg; @@ -1059,7 +968,7 @@ slpeel_can_duplicate_loop_p (struct loop *loop, edge e) tree orig_cond = get_loop_exit_condition (loop); block_stmt_iterator loop_exit_bsi = bsi_last (exit_e->src); - if (any_marked_for_rewrite_p ()) + if (need_ssa_update_p ()) return false; if (loop->inner @@ -1214,8 +1123,7 @@ slpeel_tree_peel_loop_to_edge (struct loop *loop, struct loops *loops, second_loop = loop; } - definitions = marked_ssa_names (); - allocate_new_names (definitions); + definitions = ssa_names_to_replace (); slpeel_update_phis_for_duplicate_loop (loop, new_loop, e == exit_e); rename_variables_in_loop (new_loop); @@ -1296,9 +1204,8 @@ slpeel_tree_peel_loop_to_edge (struct loop *loop, struct loops *loops, if (update_first_loop_count) slpeel_make_loop_iterate_ntimes (first_loop, first_niters); - free_new_names (definitions); BITMAP_FREE (definitions); - unmark_all_for_rewrite (); + delete_update_ssa (); return new_loop; } diff --git a/gcc/tree.h b/gcc/tree.h index cc2fba1aff1..ba1e1fd5876 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -2833,13 +2833,6 @@ extern void replace_ssa_name_symbol (tree, tree); extern void ssanames_print_statistics (void); #endif -extern void mark_for_rewrite (tree); -extern void unmark_all_for_rewrite (void); -extern bool marked_for_rewrite_p (tree); -extern bool any_marked_for_rewrite_p (void); -extern struct bitmap_head_def *marked_ssa_names (void); - - /* Return the (unique) IDENTIFIER_NODE node for a given name. The name is supplied as a char *. */ -- 2.11.4.GIT