gcc/ * basic-block.h (FOR_EACH_BB): Eliminate macro. * asan.c (transform_statements, execute_sanopt): Eliminate use of FOR_EACH_BB in favor of FOR_EACH_BB_FN, to make use of cfun explicit. * auto-inc-dec.c (rest_of_handle_auto_inc_dec): Likewise. * bb-reorder.c (find_rarely_executed_basic_blocks_and_crossing_edges, set_edge_can_fallthru_flag, fix_up_fall_thru_edges, fix_crossing_unconditional_branches, add_reg_crossing_jump_notes, insert_section_boundary_note, rest_of_handle_reorder_blocks, duplicate_computed_gotos): Likewise. * cfg.c (clear_edges, compact_blocks, brief_dump_cfg): Likewise. * cfganal.c (find_unreachable_blocks, add_noreturn_fake_exit_edges, compute_dominance_frontiers_1, single_pred_before_succ_order): Likewise. * cfgbuild.c (find_many_sub_basic_blocks): Likewise. * cfgcleanup.c (try_optimize_cfg, delete_dead_jumptables): Likewise. * cfgexpand.c (add_scope_conflicts, discover_nonconstant_array_refs): Likewise. * cfgloop.c (flow_loops_cfg_dump, get_loop_body, record_loop_exits, verify_loop_structure): Likewise. * cfgloopanal.c (mark_loop_exit_edges): Likewise. * cfgrtl.c (compute_bb_for_insn, find_partition_fixes, verify_hot_cold_block_grouping, purge_all_dead_edges, fixup_abnormal_edges, record_effective_endpoints, outof_cfg_layout_mode, fixup_reorder_chain, force_one_exit_fallthru, break_superblocks): Likewise. * cgraphbuild.c (build_cgraph_edges, rebuild_cgraph_edges, cgraph_rebuild_references): Likewise. * combine-stack-adj.c (combine_stack_adjustments): Likewise. * combine.c (delete_noop_moves, create_log_links, combine_instructions): Likewise. * config/arm/arm.c (thumb1_reorg, thumb2_reorg): Likewise. * config/bfin/bfin.c (bfin_gen_bundles, reorder_var_tracking_notes): Likewise. * config/c6x/c6x.c (c6x_gen_bundles, conditionalize_after_sched, c6x_reorg): Likewise. * config/epiphany/resolve-sw-modes.c (resolve_sw_modes): Likewise. * config/frv/frv.c (frv_optimize_membar): Likewise. * config/i386/i386.c (ix86_finalize_stack_realign_flags): Likewise. * config/ia64/ia64.c (ia64_reorg): Likewise. * config/mips/mips.c (mips_annotate_pic_calls): Likewise. * config/picochip/picochip.c (reorder_var_tracking_notes): Likewise. * config/rs6000/rs6000.c (rs6000_alloc_sdmode_stack_slot): Likewise. * config/s390/s390.c (s390_regs_ever_clobbered): Likewise. * config/sh/sh_treg_combine.cc (sh_treg_combine::execute): Likewise. * config/spu/spu.c (spu_machine_dependent_reorg): Likewise. * config/tilegx/tilegx.c (tilegx_gen_bundles, reorder_var_tracking_notes): Likewise. * config/tilepro/tilepro.c (tilepro_gen_bundles, reorder_var_tracking_notes): Likewise. * coverage.c (coverage_compute_cfg_checksum): Likewise. * cprop.c (compute_hash_table_work, compute_cprop_data, local_cprop_pass, find_implicit_sets): Likewise. * cse.c (cse_condition_code_reg): Likewise. * dce.c (prescan_insns_for_dce): Likewise. * df-core.c (df_compact_blocks): Likewise. * df-problems.c (df_word_lr_alloc): Likewise. * df-scan.c (df_scan_start_dump, df_scan_blocks, df_insn_rescan_all, df_update_entry_exit_and_calls): Likewise. * dominance.c (calculate_dominance_info, verify_dominators, debug_dominance_info): Likewise. * dse.c (dse_step5_nospill): Likewise. * except.c (finish_eh_generation): Likewise. * final.c (compute_alignments): Likewise. * function.c (thread_prologue_and_epilogue_insns, rest_of_match_asm_constraints): Likewise. * gcse.c (compute_hash_table_work, prune_expressions, compute_pre_data, compute_code_hoist_vbeinout, hoist_code, calculate_bb_reg_pressure, compute_ld_motion_mems): Likewise. * gimple-iterator.c (gsi_commit_edge_inserts): Likewise. * gimple-ssa-isolate-paths.c (find_implicit_erroneous_behaviour, find_explicit_erroneous_behaviour): Likewise. * graphite-sese-to-poly.c (rewrite_reductions_out_of_ssa, rewrite_cross_bb_scalar_deps_out_of_ssa): Likewise. * haifa-sched.c (haifa_sched_init): Likewise. * hw-doloop.c (discover_loops, set_bb_indices, reorder_loops): Likewise. * ifcvt.c (if_convert): Likewise. * init-regs.c (initialize_uninitialized_regs): Likewise. * ipa-prop.c (ipcp_transform_function): Likewise. * ipa-pure-const.c (analyze_function): Likewise. * ipa-split.c (find_split_points, execute_split_functions): Likewise. * ira-build.c (form_loop_tree): Likewise. * ira-costs.c (find_costs_and_classes): Likewise. * ira-emit.c (emit_moves, add_ranges_and_copies, ira_emit): Likewise. * ira.c (decrease_live_ranges_number, compute_regs_asm_clobbered, mark_elimination, update_equiv_regs, find_moveable_pseudos, split_live_ranges_for_shrink_wrap, allocate_initial_values): Likewise. * jump.c (mark_all_labels): Likewise. * lcm.c (compute_laterin, compute_insert_delete, compute_available, compute_nearerout, compute_rev_insert_delete): Likewise. * loop-init.c (fix_loop_structure): Likewise. * loop-invariant.c (calculate_loop_reg_pressure): Likewise. * lower-subreg.c (decompose_multiword_subregs, decompose_multiword_subregs): Likewise. * lra-assigns.c (assign_by_spills): Likewise. * lra-coalesce.c (lra_coalesce): Likewise. * lra-constraints.c (lra_inheritance, remove_inheritance_pseudos): Likewise. * lra-eliminations.c (lra_init_elimination): Likewise. * lra-spills.c (assign_spill_hard_regs, spill_pseudos, lra_final_code_change): Likewise. * lra.c (remove_scratches, check_rtl, has_nonexceptional_receiver, update_inc_notes): Likewise. * mcf.c (adjust_cfg_counts): Likewise. * mode-switching.c (optimize_mode_switching): Likewise. * modulo-sched.c (rest_of_handle_sms): Likewise. * omp-low.c (optimize_omp_library_calls, expand_omp_taskreg, expand_omp_target): Likewise. * postreload-gcse.c (alloc_mem, compute_hash_table): Likewise. * postreload.c (reload_cse_regs_1): Likewise. * predict.c (strip_predict_hints, tree_bb_level_predictions, tree_estimate_probability, expensive_function_p, estimate_bb_frequencies, compute_function_frequency): Likewise. * profile.c (is_inconsistent, compute_branch_probabilities, branch_prob): Likewise. * ree.c (find_removable_extensions): Likewise. * reg-stack.c (compensate_edges, convert_regs, reg_to_stack): Likewise. * regcprop.c (copyprop_hardreg_forward): Likewise. * reginfo.c (init_subregs_of_mode): Likewise. * regrename.c (regrename_analyze): Likewise. * regstat.c (regstat_compute_ri, regstat_compute_calls_crossed): Likewise. * reload1.c (has_nonexceptional_receiver, reload, calculate_elim_costs_all_insns): Likewise. * resource.c (init_resource_info, free_resource_info): Likewise. * sched-ebb.c (schedule_ebbs): Likewise. * sched-rgn.c (is_cfg_nonregular, find_single_block_region, haifa_find_rgns, sched_rgn_local_init): Likewise. * sel-sched-dump.c (sel_dump_cfg_2): Likewise. * sel-sched-ir.c (init_lv_sets, free_lv_sets, make_regions_from_the_rest): Likewise. * sese.c (build_sese_loop_nests, sese_build_liveouts): Likewise. * stack-ptr-mod.c (notice_stack_pointer_modification): Likewise. * store-motion.c (compute_store_table, build_store_vectors, one_store_motion_pass): Likewise. * tracer.c (tail_duplicate): Likewise. * trans-mem.c (compute_transaction_bits): Likewise. * tree-call-cdce.c (tree_call_cdce): Likewise. * tree-cfg.c (replace_loop_annotate, factor_computed_gotos, fold_cond_expr_cond, make_edges, assign_discriminators, make_abnormal_goto_edges, cleanup_dead_labels, group_case_labels, dump_cfg_stats, gimple_verify_flow_info, print_loop, execute_fixup_cfg): Likewise. * tree-cfgcleanup.c (cleanup_tree_cfg_1, merge_phi_nodes): Likewise. * tree-complex.c (init_dont_simulate_again, tree_lower_complex): Likewise. * tree-dfa.c (collect_dfa_stats, dump_enumerated_decls): Likewise. * tree-eh.c (execute_lower_resx, execute_lower_eh_dispatch, mark_reachable_handlers): Likewise. * tree-emutls.c (lower_emutls_function_body): Likewise. * tree-if-conv.c (main_tree_if_conversion): Likewise. * tree-inline.c (optimize_inline_calls): Likewise. * tree-into-ssa.c (rewrite_into_ssa, update_ssa): Likewise. * tree-nrv.c (tree_nrv, execute_return_slot_opt): Likewise. * tree-object-size.c (compute_object_sizes): Likewise. * tree-outof-ssa.c (eliminate_useless_phis, rewrite_trees, insert_backedge_copies, tree_profiling): Likewise. * tree-scalar-evolution.c (scev_const_prop): Likewise. * tree-sra.c (scan_function, sra_modify_function_body, propagate_dereference_distances, ipa_sra_modify_function_body, convert_callers): Likewise. * tree-ssa-ccp.c (ccp_initialize, execute_fold_all_builtins): Likewise. * tree-ssa-coalesce.c (build_ssa_conflict_graph): Likewise. create_outofssa_var_map, coalesce_partitions): Likewise. * tree-ssa-copy.c (init_copy_prop): Likewise. * tree-ssa-copyrename.c (rename_ssa_copies): Likewise. * tree-ssa-dce.c (find_obviously_necessary_stmts, eliminate_unnecessary_stmts): Likewise. * tree-ssa-dom.c (free_all_edge_infos, tree_ssa_dominator_optimize): Likewise. * tree-ssa-forwprop.c (ssa_forward_propagate_and_combine): Likewise. * tree-ssa-live.c (clear_unused_block_pointer, remove_unused_locals, new_tree_live_info, calculate_live_on_exit, dump_live_info, analyze_memory_references, fill_always_executed_in, tree_ssa_lim_finalize): Likewise. * tree-ssa-loop-manip.c (find_uses_to_rename, verify_loop_closed_ssa): Likewise. * tree-ssa-math-opts.c (execute_cse_reciprocals, execute_cse_sincos, execute_optimize_bswap, execute_optimize_widening_mul): Likewise. * tree-ssa-propagate.c (substitute_and_fold): Likewise. * tree-ssa-structalias.c (compute_points_to_sets): Likewise. * tree-ssa-tail-merge.c (find_same_succ, reset_cluster_vectors): Likewise. * tree-ssa-ter.c (find_replaceable_exprs): Likewise. * tree-ssa-threadupdate.c (thread_through_all_blocks): Likewise. * tree-ssa-uncprop.c (associate_equivalences_with_edges, tree_ssa_uncprop): Likewise. * tree-ssa-uninit.c (warn_uninitialized_vars, execute_late_warn_uninitialized): Likewise. * tree-ssa.c (verify_ssa, execute_update_addresses_taken): Likewise. * tree-stdarg.c (check_all_va_list_escapes, execute_optimize_stdarg): Likewise. * tree-switch-conversion.c (do_switchconv): Likewise. * tree-vect-generic.c (expand_vector_operations): Likewise. * tree-vectorizer.c (adjust_simduid_builtins, note_simd_array_uses, execute_vect_slp): Likewise. * tree-vrp.c (check_all_array_refs, remove_range_assertions, vrp_initialize, identify_jump_threads, instrument_memory_accesses): Likewise. * ubsan.c (ubsan_pass): Likewise. * value-prof.c (verify_histograms, gimple_value_profile_transformations, gimple_find_values_to_profile): Likewise. * var-tracking.c (vt_find_locations, dump_dataflow_sets, vt_emit_notes, vt_initialize, delete_debug_insns, vt_finalize): Likewise. gcc/testsuite/ * g++.dg/plugin/selfassign.c (execute_warn_self_assign): Eliminate use of FOR_EACH_BB in favor of FOR_EACH_BB_FN, to make use of cfun explicit. * gcc.dg/plugin/selfassign.c (execute_warn_self_assign): Likewise. From-SVN: r205828
421 lines
12 KiB
C
421 lines
12 KiB
C
/* Language independent return value optimizations
|
|
Copyright (C) 2004-2013 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 3, 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 COPYING3. If not see
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
#include "config.h"
|
|
#include "system.h"
|
|
#include "coretypes.h"
|
|
#include "tm.h"
|
|
#include "tree.h"
|
|
#include "function.h"
|
|
#include "basic-block.h"
|
|
#include "tree-pretty-print.h"
|
|
#include "tree-ssa-alias.h"
|
|
#include "internal-fn.h"
|
|
#include "gimple-expr.h"
|
|
#include "is-a.h"
|
|
#include "gimple.h"
|
|
#include "gimple-iterator.h"
|
|
#include "gimple-walk.h"
|
|
#include "gimple-ssa.h"
|
|
#include "stringpool.h"
|
|
#include "tree-ssanames.h"
|
|
#include "tree-pass.h"
|
|
#include "langhooks.h"
|
|
#include "flags.h" /* For "optimize" in gate_pass_return_slot.
|
|
FIXME: That should be up to the pass manager,
|
|
but pass_nrv is not in pass_all_optimizations. */
|
|
|
|
/* 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;
|
|
int modified;
|
|
};
|
|
|
|
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 walk_stmt_info *wi = (struct walk_stmt_info *) data;
|
|
struct nrv_data *dp = (struct nrv_data *) wi->info;
|
|
|
|
/* 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;
|
|
dp->modified = 1;
|
|
}
|
|
|
|
/* 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;
|
|
gimple_stmt_iterator gsi;
|
|
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;
|
|
|
|
/* If a GIMPLE type is returned in memory, finalize_nrv_r might create
|
|
non-GIMPLE. */
|
|
if (is_gimple_reg_type (result_type))
|
|
return 0;
|
|
|
|
/* If the front end already did something like this, don't do it here. */
|
|
if (DECL_NAME (result))
|
|
return 0;
|
|
|
|
/* If the result has its address taken then it might be modified
|
|
by means not detected in the following loop. Bail out in this
|
|
case. */
|
|
if (TREE_ADDRESSABLE (result))
|
|
return 0;
|
|
|
|
/* Look through each block for assignments to the RESULT_DECL. */
|
|
FOR_EACH_BB_FN (bb, cfun)
|
|
{
|
|
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
|
|
{
|
|
gimple stmt = gsi_stmt (gsi);
|
|
tree ret_val;
|
|
|
|
if (gimple_code (stmt) == GIMPLE_RETURN)
|
|
{
|
|
/* In a function with an aggregate return value, the
|
|
gimplifier has changed all non-empty RETURN_EXPRs to
|
|
return the RESULT_DECL. */
|
|
ret_val = gimple_return_retval (stmt);
|
|
if (ret_val)
|
|
gcc_assert (ret_val == result);
|
|
}
|
|
else if (gimple_has_lhs (stmt)
|
|
&& gimple_get_lhs (stmt) == result)
|
|
{
|
|
tree rhs;
|
|
|
|
if (!gimple_assign_copy_p (stmt))
|
|
return 0;
|
|
|
|
rhs = gimple_assign_rhs1 (stmt);
|
|
|
|
/* 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 != rhs)
|
|
return 0;
|
|
}
|
|
else
|
|
found = rhs;
|
|
|
|
/* 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)
|
|
|| !useless_type_conversion_p (result_type,
|
|
TREE_TYPE (found)))
|
|
return 0;
|
|
}
|
|
else if (gimple_has_lhs (stmt))
|
|
{
|
|
tree addr = get_base_address (gimple_get_lhs (stmt));
|
|
/* If there's any MODIFY of component of RESULT,
|
|
then bail out. */
|
|
if (addr && addr == result)
|
|
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 if it will be useful. But don't set
|
|
DECL_ABSTRACT_ORIGIN to point at another function. */
|
|
if (!DECL_IGNORED_P (found)
|
|
&& !(DECL_ABSTRACT_ORIGIN (found)
|
|
&& DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (found)) != current_function_decl))
|
|
{
|
|
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_FN (bb, cfun)
|
|
{
|
|
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
|
|
{
|
|
gimple stmt = gsi_stmt (gsi);
|
|
/* If this is a copy from VAR to RESULT, remove it. */
|
|
if (gimple_assign_copy_p (stmt)
|
|
&& gimple_assign_lhs (stmt) == result
|
|
&& gimple_assign_rhs1 (stmt) == found)
|
|
{
|
|
unlink_stmt_vdef (stmt);
|
|
gsi_remove (&gsi, true);
|
|
release_defs (stmt);
|
|
}
|
|
else
|
|
{
|
|
struct walk_stmt_info wi;
|
|
memset (&wi, 0, sizeof (wi));
|
|
wi.info = &data;
|
|
data.modified = 0;
|
|
walk_gimple_op (stmt, finalize_nrv_r, &wi);
|
|
if (data.modified)
|
|
update_stmt (stmt);
|
|
gsi_next (&gsi);
|
|
}
|
|
}
|
|
}
|
|
|
|
SET_DECL_VALUE_EXPR (found, result);
|
|
DECL_HAS_VALUE_EXPR_P (found) = 1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static bool
|
|
gate_pass_return_slot (void)
|
|
{
|
|
return optimize > 0;
|
|
}
|
|
|
|
namespace {
|
|
|
|
const pass_data pass_data_nrv =
|
|
{
|
|
GIMPLE_PASS, /* type */
|
|
"nrv", /* name */
|
|
OPTGROUP_NONE, /* optinfo_flags */
|
|
true, /* has_gate */
|
|
true, /* has_execute */
|
|
TV_TREE_NRV, /* tv_id */
|
|
( PROP_ssa | PROP_cfg ), /* properties_required */
|
|
0, /* properties_provided */
|
|
0, /* properties_destroyed */
|
|
0, /* todo_flags_start */
|
|
0, /* todo_flags_finish */
|
|
};
|
|
|
|
class pass_nrv : public gimple_opt_pass
|
|
{
|
|
public:
|
|
pass_nrv (gcc::context *ctxt)
|
|
: gimple_opt_pass (pass_data_nrv, ctxt)
|
|
{}
|
|
|
|
/* opt_pass methods: */
|
|
bool gate () { return gate_pass_return_slot (); }
|
|
unsigned int execute () { return tree_nrv (); }
|
|
|
|
}; // class pass_nrv
|
|
|
|
} // anon namespace
|
|
|
|
gimple_opt_pass *
|
|
make_pass_nrv (gcc::context *ctxt)
|
|
{
|
|
return new pass_nrv (ctxt);
|
|
}
|
|
|
|
/* Determine (pessimistically) whether DEST is available for NRV
|
|
optimization, where DEST is expected to be the LHS of a modify
|
|
expression where the RHS is a function returning an aggregate.
|
|
|
|
DEST is available if it is not clobbered or used by the call. */
|
|
|
|
static bool
|
|
dest_safe_for_nrv_p (gimple call)
|
|
{
|
|
tree dest = gimple_call_lhs (call);
|
|
|
|
dest = get_base_address (dest);
|
|
if (! dest)
|
|
return false;
|
|
|
|
if (TREE_CODE (dest) == SSA_NAME)
|
|
return true;
|
|
|
|
if (call_may_clobber_ref_p (call, dest)
|
|
|| ref_maybe_used_by_stmt_p (call, dest))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
/* Walk through the function looking for GIMPLE_ASSIGNs 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_FN (bb, cfun)
|
|
{
|
|
gimple_stmt_iterator gsi;
|
|
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
|
|
{
|
|
gimple stmt = gsi_stmt (gsi);
|
|
bool slot_opt_p;
|
|
|
|
if (is_gimple_call (stmt)
|
|
&& gimple_call_lhs (stmt)
|
|
&& !gimple_call_return_slot_opt_p (stmt)
|
|
&& aggregate_value_p (TREE_TYPE (gimple_call_lhs (stmt)),
|
|
gimple_call_fndecl (stmt)))
|
|
{
|
|
/* Check if the location being assigned to is
|
|
clobbered by the call. */
|
|
slot_opt_p = dest_safe_for_nrv_p (stmt);
|
|
gimple_call_set_return_slot_opt (stmt, slot_opt_p);
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
namespace {
|
|
|
|
const pass_data pass_data_return_slot =
|
|
{
|
|
GIMPLE_PASS, /* type */
|
|
"retslot", /* name */
|
|
OPTGROUP_NONE, /* optinfo_flags */
|
|
false, /* has_gate */
|
|
true, /* has_execute */
|
|
TV_NONE, /* tv_id */
|
|
PROP_ssa, /* properties_required */
|
|
0, /* properties_provided */
|
|
0, /* properties_destroyed */
|
|
0, /* todo_flags_start */
|
|
0, /* todo_flags_finish */
|
|
};
|
|
|
|
class pass_return_slot : public gimple_opt_pass
|
|
{
|
|
public:
|
|
pass_return_slot (gcc::context *ctxt)
|
|
: gimple_opt_pass (pass_data_return_slot, ctxt)
|
|
{}
|
|
|
|
/* opt_pass methods: */
|
|
unsigned int execute () { return execute_return_slot_opt (); }
|
|
|
|
}; // class pass_return_slot
|
|
|
|
} // anon namespace
|
|
|
|
gimple_opt_pass *
|
|
make_pass_return_slot (gcc::context *ctxt)
|
|
{
|
|
return new pass_return_slot (ctxt);
|
|
}
|