8sa1-gcc/gcc/tree-nrv.c
Paolo Bonzini 9f8628bacd Unify the management of RTL and tree-level dump files.
2004-09-06  Paolo Bonzini  <bonzini@gnu.org>

	Unify the management of RTL and tree-level dump files.

	* cfgexpand.c (tree_expand_cfg): Fix incorrect comment.
	Don't print function name to the dump file, the pass manager
	would do this for us. Add code from the top of
	rest_of_compilation, up to the initial RTL dump.
	* passes.c (rest_of_handle_jump): Call fixup_tail_calls and
	close the DFI_sibling dump file.
	(rest_of_compilation): Don't do that here.  Remove code up to the
	initial RTL dump.
	(init_optimization_passes): Remove.
	(pass_rest_of_compilation): Change pass name to NULL.
	* toplev.c (lang_dependent_init): Do not use an empty dump file prefix.
	Do not call init_optimization_passes.
	* toplev.h (init_optimization_passes): Remove.

	* graph.c (print_rtl_graph_with_bb, clean_graph_dump_file,
	finish_graph_dump_file): Remove SUFFIX parameter.
	* graph.h (print_rtl_graph_with_bb, clean_graph_dump_file,
	finish_graph_dump_file): Likewise.

	* tree-pass.h (struct tree_opt_pass): Add `letter' field.
	* cfgexpand.c (pass_expand): Adjust.
	* gimple-low.c (pass_lower_cf, pass_remove_useless_vars): Adjust.
	* passes.c (pass_rest_of_compilation): Adjust.
	* predict.c (pass_profile): Adjust.
	* tree-alias-common.c (pass_build_pta, pass_del_pta): Adjust.
	* tree-cfg.c (pass_build_cfg, pass_remove_useless_stmts,
	pass_split_crit_edges, pass_warn_function_return): Adjust.
	* tree-complex.c (pass_lower_vector_ssa, pass_pre_expand): Adjust.
	* tree-dfa.c (pass_referenced_vars): Adjust.
	* tree-eh.c (pass_lower_eh): Adjust.
	* tree-if-conv.c (pass_build_ssa): Adjust.
	* tree-into-ssa.c (pass_build_ssa): Adjust.
	* tree-mudflap.c (pass_mudflap_1, pass_mudflap_2): Adjust.
	* tree-nomudflap.c (pass_mudflap_1, pass_mudflap_2): Adjust.
	* tree-nrv.c (pass_nrv): Adjust.
	* tree-optimize.c (pass_gimple, pass_all_optimizations,
	pass_cleanup_cfg_post_optimizing, pass_free_datastructures,
	pass_init_datastructures): Adjust.
	* tree-outof-ssa.c (pass_del_ssa): Adjust.
	* tree-profile.c (pass_tree_profile): Adjust.
	* tree-sra.c (pass_sra): Adjust.
	* tree-ssa-alias.c (pass_may_alias): Adjust.
	* tree-ssa-ccp.c (pass_ccp, pass_fold_builtins): Adjust.
	* tree-ssa-copyrename.c (pass_rename_ssa_copies): Adjust.
	* tree-ssa-dce.c (pass_dce, pass_cd_dce): Adjust.
	* tree-ssa-dom.c (pass_dominator): Adjust.
	* tree-ssa-dse.c (pass_dse): Adjust.
	* tree-ssa-forwprop.c (pass_forwprop): Adjust.
	* tree-ssa-if-conv.c (pass_if_conversion): Adjust.
	* tree-ssa-loop-ch.c (pass_ch): Adjust.
	* tree-ssa-loop.c (pass_loop, pass_loop_init, pass_lim,
	pass_loop_done, pass_complete_unroll, pass_iv_canon,
	pass_iv_optimize, pass_vectorize): Adjust.
	* tree-ssa-phiopt.c (pass_phiopt): Adjust.
	* tree-ssa-pre.c (pass_pre, pass_fre): Adjust.
	* tree-ssa.c (pass_redundant_phi, pass_early_warn_uninitialized,
	pass_late_warn_uninnitialized): Adjust.
	* tree-tailcall.c (pass_tail_recursion, pass_tail_calls): Adjust.

	* Makefile.in (tree-dump.o): Add new dependencies.
	* cgraph.c (cgraph_remove_node): TDF_all -> TDF_tree_all.
	* cgraphunit.c (cgraph_preserve_function_body_p, cgraph_optimize):
	Likewise.
	* toplev.c (dump_file_name): New.
	* tree-dump.c (dump_enable_all): Add LETTER parameter.
	(struct dump_file_info): Add NUM and LETTER fields.
	(dump_files): Adjust and add RTL dump files.
	(dump_register): Add NUM and LETTER fields.
	(get_dump_file_name, dump_initialized_p, enable_rtl_dump_file): New.
	(dump_begin): Use get_dump_file_name.
	(dump_switch_p_1): Adjust call to dump_enable_all.
	* tree-dump.h (dump_register): Adjust prototype.
	* tree-optimize.c (register_one_dump_file): Take dump file index.
	Support flags for RTL dumps.
	(register_dump_files): Fill in NUM field of struct dump_file_info.
	Track properties both when the gate is executed and when it is not.
	(execute_todo): Dump RTL.  Add PROPERTIES parameter.
	(execute_one_pass): Pass properties to execute_todo.  Handle VCG
	dumps of RTL.
	* tree-pass.h (dump_file_name): New.
	* tree.h (TDF_TREE, TDF_RTL, get_dump_file_name, dump_initialized_p):
	New.

	* Makefile.in (passes.o): Add new dependencies.
	* passes.c (struct dump_file_info, enum dump_file_index,
	dump_file_tbl, init_optimization_passes): Remove.
	(open_dump_file, close_dump_file): Use tree-dumping infrastructure.
	(rest_of_handle_new_regalloc, rest_of_handle_old_regalloc): Use
	dump_enabled_p.
	(finish_optimization_passes): Update finish_graph_dump_file loop.
	(enable_rtl_dump_file): Remove.
	* tree-dump.c (dump_files): Adjust and add RTL dump files.
	(enable_rtl_dump_file): Add here.
	* tree.h (enum tree_dump_index): Add RTL dump file indices.
	* doc/invoke.texi (Debugging options): Document new RTL debugging
	options.  Update.

From-SVN: r87113
2004-09-06 10:08:17 +00:00

220 lines
6.9 KiB
C

/* Language independent return value optimizations
Copyright (C) 2004 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, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, 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;
/* If this is a RETURN_EXPR, set the expression being returned to RESULT. */
else if (TREE_CODE (*tp) == RETURN_EXPR)
TREE_OPERAND (*tp, 0) = dp->result;
/* 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 void
tree_nrv (void)
{
tree result = DECL_RESULT (current_function_decl);
tree result_type = TREE_TYPE (result);
tree found = NULL;
basic_block bb;
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;
/* Look through each block for suitable return expressions. RETURN_EXPRs
end basic blocks, so we only have to look at the last statement in
each block. That makes this very fast. */
FOR_EACH_BB (bb)
{
tree stmt = last_stmt (bb);
if (stmt && TREE_CODE (stmt) == RETURN_EXPR)
{
tree ret_expr = TREE_OPERAND (stmt, 0);
/* This probably should not happen, but just to be safe do
not perform NRV optimizations if only some of the return
statement return a value. */
if (!ret_expr
|| TREE_CODE (ret_expr) != MODIFY_EXPR
|| TREE_CODE (TREE_OPERAND (ret_expr, 0)) != RESULT_DECL)
return;
/* 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 != TREE_OPERAND (ret_expr, 1))
return;
}
else
found = TREE_OPERAND (ret_expr, 1);
/* 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
|| 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;
}
}
if (!found)
return;
/* 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)
{
block_stmt_iterator bsi;
for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
walk_tree (bsi_stmt_ptr (bsi), finalize_nrv_r, &data, 0);
}
/* FOUND is no longer used. Ensure it gets removed. */
var_ann (found)->used = 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 */
};