mirror of
git://gcc.gnu.org/git/gcc.git
synced 2025-03-29 07:50:27 +08:00
2006-03-02 Daniel Berlin <dberlin@dberlin.org> * gcc/tree-vrp.c (execute_vrp): Return value. * gcc/regrename.c (rest_of_handle_regrename): Ditto. * gcc/tree-into-ssa.c (rewrite_into_ssa): Ditto. * gcc/tree-complex.c (tree_lower_complex): Ditto. (tree_lower_complex_O0): Ditto. * gcc/tracer.c (rest_of_handle_tracer): Ditto. * gcc/postreload-gcse.c (rest_of_handle_gcse2): Ditto. * gcc/postreload.c (rest_of_handle_postreload): Ditto. * gcc/tree-tailcall.c (execute_tail_recursion): Ditto. (execute_tail_calls): Ditto. * gcc/tree-ssa-loop-ch.c (copy_loop_headers): Ditto. * gcc/tree.h (init_function_for_compilation): Ditto. * gcc/ipa-cp.c (ipcp_driver): Ditto. * gcc/tree-scalar-evolution.c (scev_const_prop): Ditto. * gcc/tree-scalar-evolution.h (scev_const_prop): Ditto. * gcc/final.c (compute_alignments): Ditto. (rest_of_handle_final): Ditto. (rest_of_handle_shorten_branches): Ditto. (rest_of_clean_state): Ditto. * gcc/omp-low.c (execute_expand_omp): Ditto. (execute_lower_omp): Ditto. * gcc/tree-ssa-dse.c (tree_ssa_dse): Ditto. * gcc/ipa-reference.c (static_execute): Ditto. * gcc/tree-ssa-uncprop.c (tree_ssa_uncprop): Ditto. * gcc/reorg.c (rest_of_handle_delay_slots): Ditto. (rest_of_handle_machine_reorg): Ditto. * gcc/cgraphunit.c (rebuild_cgraph_edges): Ditto. * gcc/flow.c (recompute_reg_usage): Ditto. (rest_of_handle_remove_death_notes): Ditto. (rest_of_handle_life): Ditto. (rest_of_handle_flow2): Ditto. * gcc/tree-ssa-copyrename.c (rename_ssa_copies): Ditto. * gcc/tree-ssa-ccp.c (do_ssa_ccp): Ditto. (do_ssa_store_ccp): Ditto. (execute_fold_all_builtins): Ditto. * gcc/mode-switching.c (rest_of_handle_mode_switching): Ditto. * gcc/modulo-sched.c (rest_of_handle_sms): Ditto. * gcc/ipa-pure-const.c (static_execute): Ditto. * gcc/cse.c (rest_of_handle_cse): Ditto. (rest_of_handle_cse2): Ditto. * gcc/web.c (rest_of_handle_web): Ditto. * gcc/tree-stdarg.c (execute_optimize_stdarg): Ditto. * gcc/tree-ssa-math-opts.c (execute_cse_reciprocals): Ditto. * gcc/tree-ssa-dom.c (tree_ssa_dominator_optimize): Ditto. * gcc/tree-nrv.c (tree_nrv): Ditto. (execute_return_slot_opt): Ditto. * gcc/tree-ssa-alias.c (compute_may_aliases): Ditto. (create_structure_vars): Ditto. * gcc/loop-init.c (rtl_loop_init): Ditto. (rtl_loop_done): Ditto. (rtl_move_loop_invariants): Ditto. (rtl_unswitch): Ditto. (rtl_unroll_and_peel_loops): Ditto. (rtl_doloop): Ditto. * gcc/gimple-low.c (lower_function_body): Ditto. (mark_used_blocks): Ditto. * gcc/tree-ssa-sink.c (execute_sink_code): Ditto. * gcc/ipa-inline.c (cgraph_decide_inlining): Ditto. (cgraph_early_inlining): Ditto. * gcc/global.c (rest_of_handle_global_alloc): Ditto. * gcc/jump.c (cleanup_barriers): Ditto. (purge_line_number_notes): Ditto. * gcc/ifcvt.c (rest_of_handle_if_conversion): Ditto. (rest_of_handle_if_after_reload): Ditto. * gcc/tree-ssa-loop.c (tree_ssa_loop_init): Ditto. (tree_ssa_loop_im): Ditto. (tree_ssa_loop_unswitch): Ditto. (tree_vectorize): Ditto. (tree_linear_transform): Ditto. (tree_ssa_loop_ivcanon): Ditto. (tree_ssa_empty_loop): Ditto. (tree_ssa_loop_bounds): Ditto. (tree_complete_unroll): Ditto. (tree_ssa_loop_prefetch): Ditto. (tree_ssa_loop_ivopts): Ditto. (tree_ssa_loop_done): Ditto. * gcc/predict.c (tree_estimate_probability): Ditto. * gcc/recog.c (split_all_insns_noflow): Ditto. (rest_of_handle_peephole2): Ditto. (rest_of_handle_split_all_insns): Ditto. * gcc/tree-eh.c (lower_eh_constructs): Ditto. * gcc/regmove.c (rest_of_handle_regmove): Ditto. (rest_of_handle_stack_adjustments): Ditto. * gcc/local-alloc.c (rest_of_handle_local_alloc): Ditto. * gcc/function.c (instantiate_virtual_regs): Ditto. (init_function_for_compilation): Ditto. (rest_of_handle_check_leaf_regs): Ditto. * gcc/gcse.c (rest_of_handle_jump_bypass): Ditto. (rest_of_handle_gcse): Ditto. * gcc/ipa-type-escape.c (type_escape_execute): Ditto. * gcc/alias.c (rest_of_handle_cfg): Ditto. * gcc/tree-if-conv.c (main_tree_if_conversion): Ditto. * gcc/profile.c (rest_of_handle_branch_prob): Ditto. * gcc/tree-ssa-phiopt.c (tree_ssa_phiopt): Ditto. * gcc/rtl-factoring.c (rest_of_rtl_seqabstr): Ditto. * gcc/bt-load.c (rest_of_handle_branch_target_load_optimize): Ditto * gcc/tree-dfa.c (find_referenced_vars): Ditto. * gcc/except.c (set_nothrow_function_flags): Ditto. (convert_to_eh_region_ranges): Ditto. (rest_of_handle_eh): Ditto. * gcc/emit-rtl.c (unshare_all_rtl): Ditto. (remove_unnecessary_notes): Ditto. * gcc/except.h (set_nothrow_function_flags): Ditto. (convert_to_eh_region_ranges): Ditto. * gcc/cfgexpand.c (tree_expand_cfg): Ditto. * gcc/tree-cfgcleanup.c (merge_phi_nodes): Ditto. * gcc/tree-ssa-pre.c (do_pre): Ditto. (execute_fre): Ditto. * gcc/cfgcleanup.c (rest_of_handle_jump): Ditto. (rest_of_handle_jump2): Ditto. * gcc/tree-sra.c (tree_sra): Ditto. * gcc/tree-mudflap.c (execute_mudflap_function_ops): Ditto. (execute_mudflap_function_decls): Ditto. * gcc/tree-ssa-copy.c (do_copy_prop): Ditto. (do_store_copy_prop): Ditto. * gcc/ipa-prop.h (ipcp_driver): Ditto. * gcc/cfglayout.c (insn_locators_initialize): Ditto. * gcc/tree-ssa-forwprop.c (tree_ssa_forward_propagate_single_use_vars): Ditto. * gcc/cfglayout.h (insn_locators_initialize): Ditto. * gcc/tree-ssa-dce.c (tree_ssa_dce): Ditto. * gcc/tree-ssa.c (execute_early_warn_uninitialized): Ditto. (execute_late_warn_uninitialized): Ditto. * gcc/rtl.h (cleanup_barriers): Ditto. (split_all_insns_noflow): Ditto. (purge_line_number_notes): Ditto. (unshare_all_rtl): Ditto. (remove_unnecessary_notes): Ditto. (recompute_reg_usage): Ditto. (variable_tracking_main): Ditto. * gcc/integrate.c (emit_initial_value_sets): Ditto. * gcc/integrate.h (emit_initial_value_sets): Ditto. * gcc/tree-optimize.c (execute_free_datastructures): Ditto (execute_free_cfg_annotations): Ditto. (execute_fixup_cfg): Ditto. (execute_cleanup_cfg_pre_ipa): Ditto. (execute_cleanup_cfg_post_optimizing): Ditto. (execute_init_datastructures): Ditto. * gcc/tree-object-size.c (compute_object_sizes): Ditto. * gcc/combine.c (rest_of_handle_combine): Ditto. * gcc/tree-outof-ssa.c (rewrite_out_of_ssa): Ditto. * gcc/bb-reorder.c (duplicate_computed_gotos): Ditto. (rest_of_handle_reorder_blocks): Ditto. (rest_of_handle_partition_blocks): Ditto. * gcc/var-tracking.c (variable_tracking_main): Ditto. * gcc/tree-profile.c (tree_profiling): Ditto. * gcc/tree-vect-generic.c (expand_vector_operations): Ditto. * gcc/reg-stack.c (rest_of_handle_stack_regs): Ditto. * gcc/sched-rgn.c (rest_of_handle_sched): Ditto. (rest_of_handle_sched2): Ditto. * gcc/basic-block.h (free_bb_insn): Ditto. * gcc/tree-ssa-structalias.c (ipa_pta_execute): Ditto. * gcc/tree-cfg.c (execute_build_cfg): Ditto. (remove_useless_stmts): Ditto. (split_critical_edges): Ditto. (execute_warn_function_return): Ditto. (execute_warn_function_noreturn): Ditto. * gcc/tree-ssa-reassoc.c (execute_reassoc): Ditto. * gcc/cfgrtl.c (free_bb_for_insn): Ditto. * gcc/passes.c (execute_one_pass): Run additional todos returned by execute function. * gcc/tree-pass.h (struct tree_opt_pass): Make execute return a value. From-SVN: r111643
310 lines
9.4 KiB
C
310 lines
9.4 KiB
C
/* Language independent return value optimizations
|
|
Copyright (C) 2004, 2005 Free Software Foundation, Inc.
|
|
|
|
This file is part of GCC.
|
|
|
|
GCC is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
any later version.
|
|
|
|
GCC is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with GCC; see the file COPYING. If not, write to
|
|
the Free Software Foundation, 51 Franklin Street, Fifth Floor,
|
|
Boston, MA 02110-1301, USA. */
|
|
|
|
#include "config.h"
|
|
#include "system.h"
|
|
#include "coretypes.h"
|
|
#include "tm.h"
|
|
#include "tree.h"
|
|
#include "rtl.h"
|
|
#include "function.h"
|
|
#include "basic-block.h"
|
|
#include "expr.h"
|
|
#include "diagnostic.h"
|
|
#include "tree-flow.h"
|
|
#include "timevar.h"
|
|
#include "tree-dump.h"
|
|
#include "tree-pass.h"
|
|
#include "langhooks.h"
|
|
|
|
/* This file implements return value optimizations for functions which
|
|
return aggregate types.
|
|
|
|
Basically this pass searches the function for return statements which
|
|
return a local aggregate. When converted to RTL such statements will
|
|
generate a copy from the local aggregate to final return value destination
|
|
mandated by the target's ABI.
|
|
|
|
That copy can often be avoided by directly constructing the return value
|
|
into the final destination mandated by the target's ABI.
|
|
|
|
This is basically a generic equivalent to the C++ front-end's
|
|
Named Return Value optimization. */
|
|
|
|
struct nrv_data
|
|
{
|
|
/* This is the temporary (a VAR_DECL) which appears in all of
|
|
this function's RETURN_EXPR statements. */
|
|
tree var;
|
|
|
|
/* This is the function's RESULT_DECL. We will replace all occurrences
|
|
of VAR with RESULT_DECL when we apply this optimization. */
|
|
tree result;
|
|
};
|
|
|
|
static tree finalize_nrv_r (tree *, int *, void *);
|
|
|
|
/* Callback for the tree walker.
|
|
|
|
If TP refers to a RETURN_EXPR, then set the expression being returned
|
|
to nrv_data->result.
|
|
|
|
If TP refers to nrv_data->var, then replace nrv_data->var with
|
|
nrv_data->result.
|
|
|
|
If we reach a node where we know all the subtrees are uninteresting,
|
|
then set *WALK_SUBTREES to zero. */
|
|
|
|
static tree
|
|
finalize_nrv_r (tree *tp, int *walk_subtrees, void *data)
|
|
{
|
|
struct nrv_data *dp = (struct nrv_data *)data;
|
|
|
|
/* No need to walk into types. */
|
|
if (TYPE_P (*tp))
|
|
*walk_subtrees = 0;
|
|
|
|
/* Otherwise replace all occurrences of VAR with RESULT. */
|
|
else if (*tp == dp->var)
|
|
*tp = dp->result;
|
|
|
|
/* Keep iterating. */
|
|
return NULL_TREE;
|
|
}
|
|
|
|
/* Main entry point for return value optimizations.
|
|
|
|
If this function always returns the same local variable, and that
|
|
local variable is an aggregate type, then replace the variable with
|
|
the function's DECL_RESULT.
|
|
|
|
This is the equivalent of the C++ named return value optimization
|
|
applied to optimized trees in a language independent form. If we
|
|
ever encounter languages which prevent this kind of optimization,
|
|
then we could either have the languages register the optimization or
|
|
we could change the gating function to check the current language. */
|
|
|
|
static unsigned int
|
|
tree_nrv (void)
|
|
{
|
|
tree result = DECL_RESULT (current_function_decl);
|
|
tree result_type = TREE_TYPE (result);
|
|
tree found = NULL;
|
|
basic_block bb;
|
|
block_stmt_iterator bsi;
|
|
struct nrv_data data;
|
|
|
|
/* If this function does not return an aggregate type in memory, then
|
|
there is nothing to do. */
|
|
if (!aggregate_value_p (result, current_function_decl))
|
|
return 0;
|
|
|
|
/* Look through each block for assignments to the RESULT_DECL. */
|
|
FOR_EACH_BB (bb)
|
|
{
|
|
for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
|
|
{
|
|
tree stmt = bsi_stmt (bsi);
|
|
tree ret_expr;
|
|
|
|
if (TREE_CODE (stmt) == RETURN_EXPR)
|
|
{
|
|
/* In a function with an aggregate return value, the
|
|
gimplifier has changed all non-empty RETURN_EXPRs to
|
|
return the RESULT_DECL. */
|
|
ret_expr = TREE_OPERAND (stmt, 0);
|
|
if (ret_expr)
|
|
gcc_assert (ret_expr == result);
|
|
}
|
|
else if (TREE_CODE (stmt) == MODIFY_EXPR
|
|
&& TREE_OPERAND (stmt, 0) == result)
|
|
{
|
|
ret_expr = TREE_OPERAND (stmt, 1);
|
|
|
|
/* Now verify that this return statement uses the same value
|
|
as any previously encountered return statement. */
|
|
if (found != NULL)
|
|
{
|
|
/* If we found a return statement using a different variable
|
|
than previous return statements, then we can not perform
|
|
NRV optimizations. */
|
|
if (found != ret_expr)
|
|
return 0;
|
|
}
|
|
else
|
|
found = ret_expr;
|
|
|
|
/* The returned value must be a local automatic variable of the
|
|
same type and alignment as the function's result. */
|
|
if (TREE_CODE (found) != VAR_DECL
|
|
|| TREE_THIS_VOLATILE (found)
|
|
|| DECL_CONTEXT (found) != current_function_decl
|
|
|| TREE_STATIC (found)
|
|
|| TREE_ADDRESSABLE (found)
|
|
|| DECL_ALIGN (found) > DECL_ALIGN (result)
|
|
|| !lang_hooks.types_compatible_p (TREE_TYPE (found),
|
|
result_type))
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!found)
|
|
return 0;
|
|
|
|
/* If dumping details, then note once and only the NRV replacement. */
|
|
if (dump_file && (dump_flags & TDF_DETAILS))
|
|
{
|
|
fprintf (dump_file, "NRV Replaced: ");
|
|
print_generic_expr (dump_file, found, dump_flags);
|
|
fprintf (dump_file, " with: ");
|
|
print_generic_expr (dump_file, result, dump_flags);
|
|
fprintf (dump_file, "\n");
|
|
}
|
|
|
|
/* At this point we know that all the return statements return the
|
|
same local which has suitable attributes for NRV. Copy debugging
|
|
information from FOUND to RESULT. */
|
|
DECL_NAME (result) = DECL_NAME (found);
|
|
DECL_SOURCE_LOCATION (result) = DECL_SOURCE_LOCATION (found);
|
|
DECL_ABSTRACT_ORIGIN (result) = DECL_ABSTRACT_ORIGIN (found);
|
|
TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (found);
|
|
|
|
/* Now walk through the function changing all references to VAR to be
|
|
RESULT. */
|
|
data.var = found;
|
|
data.result = result;
|
|
FOR_EACH_BB (bb)
|
|
{
|
|
for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
|
|
{
|
|
tree *tp = bsi_stmt_ptr (bsi);
|
|
/* If this is a copy from VAR to RESULT, remove it. */
|
|
if (TREE_CODE (*tp) == MODIFY_EXPR
|
|
&& TREE_OPERAND (*tp, 0) == result
|
|
&& TREE_OPERAND (*tp, 1) == found)
|
|
bsi_remove (&bsi, true);
|
|
else
|
|
{
|
|
walk_tree (tp, finalize_nrv_r, &data, 0);
|
|
bsi_next (&bsi);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* FOUND is no longer used. Ensure it gets removed. */
|
|
var_ann (found)->used = 0;
|
|
return 0;
|
|
}
|
|
|
|
struct tree_opt_pass pass_nrv =
|
|
{
|
|
"nrv", /* name */
|
|
NULL, /* gate */
|
|
tree_nrv, /* execute */
|
|
NULL, /* sub */
|
|
NULL, /* next */
|
|
0, /* static_pass_number */
|
|
TV_TREE_NRV, /* tv_id */
|
|
PROP_cfg, /* properties_required */
|
|
0, /* properties_provided */
|
|
0, /* properties_destroyed */
|
|
0, /* todo_flags_start */
|
|
TODO_dump_func | TODO_ggc_collect, /* todo_flags_finish */
|
|
0 /* letter */
|
|
};
|
|
|
|
/* Walk through the function looking for MODIFY_EXPRs with calls that
|
|
return in memory on the RHS. For each of these, determine whether it is
|
|
safe to pass the address of the LHS as the return slot, and mark the
|
|
call appropriately if so.
|
|
|
|
The NRV shares the return slot with a local variable in the callee; this
|
|
optimization shares the return slot with the target of the call within
|
|
the caller. If the NRV is performed (which we can't know in general),
|
|
this optimization is safe if the address of the target has not
|
|
escaped prior to the call. If it has, modifications to the local
|
|
variable will produce visible changes elsewhere, as in PR c++/19317. */
|
|
|
|
static unsigned int
|
|
execute_return_slot_opt (void)
|
|
{
|
|
basic_block bb;
|
|
|
|
FOR_EACH_BB (bb)
|
|
{
|
|
block_stmt_iterator i;
|
|
for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
|
|
{
|
|
tree stmt = bsi_stmt (i);
|
|
tree call;
|
|
|
|
if (TREE_CODE (stmt) == MODIFY_EXPR
|
|
&& (call = TREE_OPERAND (stmt, 1),
|
|
TREE_CODE (call) == CALL_EXPR)
|
|
&& !CALL_EXPR_RETURN_SLOT_OPT (call)
|
|
&& aggregate_value_p (call, call))
|
|
{
|
|
def_operand_p def_p;
|
|
ssa_op_iter op_iter;
|
|
|
|
/* We determine whether or not the LHS address escapes by
|
|
asking whether it is call clobbered. When the LHS isn't a
|
|
simple decl, we need to check the VDEFs, so it's simplest
|
|
to just loop through all the DEFs. */
|
|
FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_ALL_DEFS)
|
|
{
|
|
tree def = DEF_FROM_PTR (def_p);
|
|
if (TREE_CODE (def) == SSA_NAME)
|
|
def = SSA_NAME_VAR (def);
|
|
if (is_call_clobbered (def))
|
|
goto unsafe;
|
|
}
|
|
|
|
/* No defs are call clobbered, so the optimization is safe. */
|
|
CALL_EXPR_RETURN_SLOT_OPT (call) = 1;
|
|
/* This is too late to mark the target addressable like we do
|
|
in gimplify_modify_expr_rhs, but that's OK; anything that
|
|
wasn't already addressable was handled there. */
|
|
|
|
unsafe:;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
struct tree_opt_pass pass_return_slot =
|
|
{
|
|
"retslot", /* name */
|
|
NULL, /* gate */
|
|
execute_return_slot_opt, /* execute */
|
|
NULL, /* sub */
|
|
NULL, /* next */
|
|
0, /* static_pass_number */
|
|
0, /* tv_id */
|
|
PROP_ssa | PROP_alias, /* properties_required */
|
|
0, /* properties_provided */
|
|
0, /* properties_destroyed */
|
|
0, /* todo_flags_start */
|
|
0, /* todo_flags_finish */
|
|
0 /* letter */
|
|
};
|