https://gcc.gnu.org/g:a08d0e9eeaac6c67c22490a016ed2ab42f042f70
commit r17-6276-ga08d0e9eeaac6c67c22490a016ed2ab42f042f70 Author: Jan Hubicka <[email protected]> Date: Fri Nov 13 21:14:07 2026 +0100 Improve IPA propagation of readonly data This patch extends IPA predicates to keep track of values that may be modified at the analysis time, but they cannot since the addrss readonly. It also exend unmodified_parm_or_parm_agg_item to handle STRING_CST and other cases while it previously only special cased constant variable constructors. Bootstrapped/regtested x86_64-linux. gcc/ChangeLog: * ipa-cp.cc (ipa_argagg_value_list::get_value): Add MAYBE_MODEIFIED parameter; document other params. (ipa_get_indirect_edge_target_1): Pass aroundguaranteed_unmodified. (ipcp_discover_new_direct_edges): Update construction of avx. * ipa-fnsummary.cc (evaluate_conditions_for_known_args): Pass type of access. (unmodified_parm_or_parm_agg_item): Set maybe_modified. (refs_local_or_readonly_memory_p): Accept constants. (remap_edge_summaries): Pass arround agg_preserved_map. (remap_freqcounting_predicate): Likewise. (ipa_merge_fn_summary_after_inlining): Likewise. (inline_read_section): Read maybe_modified flag. (ipa_fn_summary_write): Write maybe_modified flag. * ipa-fnsummary.h (struct agg_position_info): Add maybe_modified. * ipa-predicate.cc (ipa_predicate::add_clause): Handle maybe_modified flag. (ipa_predicate::probability): Handle maybe_modified flag. (dump_condition): Handle maybe_modified flag. (ipa_predicate::remap_after_duplication): Handle maybe_modified flag. (ipa_predicate::remap_after_inlining): Handle maybe_modified flag. (add_condition): Handle maybe_modified flag. * ipa-predicate.h (struct condition): Add maybe_modified flag. * ipa-prop.cc (ipa_find_agg_cst_from_init): Handle STRING_CST, ctors and scalars. (try_make_edge_direct_simple_call): Update. * ipa-prop.h (ipa_find_agg_cst_from_init): Update declarations * ipa-utils.cc (ipa_points_to_readonly_memory_p): Add known_vals. * ipa-utils.h (ipa_points_to_readonly_memory_p): Declare. gcc/testsuite/ChangeLog: * gcc.dg/ipa/fnsummary-2.c: New test. * gcc.dg/ipa/fnsummary-3.c: New test. * gcc.dg/ipa/fnsummary-4.c: New test. * gcc.dg/ipa/fnsummary-5.c: New test. * gcc.dg/ipa/fnsummary-6.c: New test. Diff: --- gcc/ipa-cp.cc | 24 +++++++---- gcc/ipa-fnsummary.cc | 67 +++++++++++++++++++++--------- gcc/ipa-fnsummary.h | 1 + gcc/ipa-predicate.cc | 47 ++++++++++++++------- gcc/ipa-predicate.h | 4 ++ gcc/ipa-prop.cc | 76 +++++++++++++++++++++++++++++----- gcc/ipa-prop.h | 28 ++++++++----- gcc/ipa-utils.cc | 11 +++++ gcc/ipa-utils.h | 1 + gcc/testsuite/gcc.dg/ipa/fnsummary-2.c | 37 +++++++++++++++++ gcc/testsuite/gcc.dg/ipa/fnsummary-3.c | 71 +++++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/ipa/fnsummary-4.c | 42 +++++++++++++++++++ gcc/testsuite/gcc.dg/ipa/fnsummary-5.c | 72 ++++++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/ipa/fnsummary-6.c | 50 ++++++++++++++++++++++ 14 files changed, 466 insertions(+), 65 deletions(-) diff --git a/gcc/ipa-cp.cc b/gcc/ipa-cp.cc index b07476afe403..c1fcead4c12e 100644 --- a/gcc/ipa-cp.cc +++ b/gcc/ipa-cp.cc @@ -1204,17 +1204,23 @@ ipa_argagg_value_list::get_value (int index, unsigned unit_offset) const } /* Return the aggregate constant stored for INDEX at UNIT_OFFSET, if it is - passed by reference or not according to BY_REF, or NULL_TREE if there is - no such constant. */ + passed by reference or not according to BY_REF (and if MAYBE_MODIFIED is + true, only when the memory passed by reference is read-only), or + NULL_TREE if there is no such constant. */ tree ipa_argagg_value_list::get_value (int index, unsigned unit_offset, - bool by_ref) const + bool by_ref, bool maybe_modified) const { const ipa_argagg_value *av = get_elt (index, unit_offset); - if (av && av->by_ref == by_ref) - return av->value; - return NULL_TREE; + if (!av || av->by_ref != by_ref) + return NULL_TREE; + if (maybe_modified + && (!by_ref + || (unsigned) index >= m_known_vals.size () + || !ipa_points_to_readonly_memory_p (m_known_vals[index]))) + return NULL_TREE; + return av->value; } /* Return true if all elements present in OTHER are also present in this @@ -3188,9 +3194,9 @@ ipa_get_indirect_edge_target_1 (struct cgraph_edge *ie, sii->offset, sii->by_ref); - if (!t && sii->guaranteed_unmodified) + if (!t) t = avs.get_value (param_index, sii->offset / BITS_PER_UNIT, - sii->by_ref); + sii->by_ref, !sii->guaranteed_unmodified); } else if ((unsigned) param_index < known_csts.length ()) t = known_csts[param_index]; @@ -4190,7 +4196,7 @@ ipcp_discover_new_direct_edges (struct cgraph_node *node, bool speculative; next_ie = ie->next_callee; - ipa_argagg_value_list avs (aggvals); + ipa_argagg_value_list avs (aggvals, known_csts); target = ipa_get_indirect_edge_target_1 (ie, known_csts, known_contexts, avs, &speculative); if (target) diff --git a/gcc/ipa-fnsummary.cc b/gcc/ipa-fnsummary.cc index d3225a084836..7c37802e8bb8 100644 --- a/gcc/ipa-fnsummary.cc +++ b/gcc/ipa-fnsummary.cc @@ -418,7 +418,7 @@ evaluate_conditions_for_known_args (struct cgraph_node *node, continue; if (tree sval = avals->safe_sval_at (c->operand_num)) - val = ipa_find_agg_cst_from_init (sval, c->offset, c->by_ref); + val = ipa_find_agg_cst_from_init (sval, c->offset, c->by_ref, c->type); /* ipa_argagg_value_list is indexed by byte offsets, so a condition which does not start at a byte boundary (a bit-field) cannot be looked up in it; the containing byte would be reinterpreted as @@ -427,7 +427,7 @@ evaluate_conditions_for_known_args (struct cgraph_node *node, { ipa_argagg_value_list avs (avals); val = avs.get_value (c->operand_num, c->offset / BITS_PER_UNIT, - c->by_ref); + c->by_ref, c->maybe_modified); } } else @@ -1317,6 +1317,7 @@ unmodified_parm_or_parm_agg_item (struct ipa_func_body_info *fbi, return false; aggpos->agg_contents = false; aggpos->by_ref = false; + aggpos->maybe_modified = false; return true; } @@ -1332,10 +1333,18 @@ unmodified_parm_or_parm_agg_item (struct ipa_func_body_info *fbi, aggpos); } + bool guaranteed_unmodified; aggpos->agg_contents = true; - return ipa_load_from_parm_agg (fbi, fbi->info->descriptors, - stmt, op, index_p, &aggpos->offset, - size_p, &aggpos->by_ref); + if (ipa_load_from_parm_agg (fbi, fbi->info->descriptors, + stmt, op, index_p, &aggpos->offset, + size_p, &aggpos->by_ref, + &guaranteed_unmodified)) + { + aggpos->maybe_modified = !guaranteed_unmodified; + gcc_checking_assert (!aggpos->maybe_modified || aggpos->by_ref); + return true; + } + return false; } /* If stmt is simple load or store of value pointed to by a function parameter, @@ -2645,7 +2654,9 @@ refs_local_or_readonly_memory_p (tree t) && auto_var_in_fn_p (t, current_function_decl)) return true; - /* Read-only variables are fine. */ + /* Read-only variables and constants are fine. */ + if (CONSTANT_CLASS_P (t)) + return true; if (DECL_P (t) && TREE_READONLY (t)) return true; @@ -4416,6 +4427,7 @@ remap_edge_summaries (struct cgraph_edge *inlined_edge, class ipa_fn_summary *callee_info, const vec<int> &operand_map, const vec<HOST_WIDE_INT> &offset_map, + const vec<bool> &agg_preserved_map, clause_t possible_truths, ipa_predicate *toplev_predicate) { @@ -4435,7 +4447,8 @@ remap_edge_summaries (struct cgraph_edge *inlined_edge, p = es->predicate->remap_after_inlining (info, params_summary, callee_info, operand_map, - offset_map, possible_truths, + offset_map, agg_preserved_map, + possible_truths, *toplev_predicate); edge_set_predicate (e, &p); } @@ -4445,8 +4458,8 @@ remap_edge_summaries (struct cgraph_edge *inlined_edge, else remap_edge_summaries (inlined_edge, e->callee, info, params_summary, callee_info, - operand_map, offset_map, possible_truths, - toplev_predicate); + operand_map, offset_map, agg_preserved_map, + possible_truths, toplev_predicate); } for (e = node->indirect_calls; e; e = next) { @@ -4460,6 +4473,7 @@ remap_edge_summaries (struct cgraph_edge *inlined_edge, p = es->predicate->remap_after_inlining (info, params_summary, callee_info, operand_map, offset_map, + agg_preserved_map, possible_truths, *toplev_predicate); edge_set_predicate (e, &p); } @@ -4477,6 +4491,7 @@ remap_freqcounting_predicate (class ipa_fn_summary *info, vec<ipa_freqcounting_predicate, va_gc> *v, const vec<int> &operand_map, const vec<HOST_WIDE_INT> &offset_map, + const vec<bool> &agg_preserved_map, clause_t possible_truths, ipa_predicate *toplev_predicate) @@ -4487,7 +4502,8 @@ remap_freqcounting_predicate (class ipa_fn_summary *info, ipa_predicate p = fcp->predicate->remap_after_inlining (info, params_summary, callee_info, operand_map, - offset_map, possible_truths, + offset_map, agg_preserved_map, + possible_truths, *toplev_predicate); if (p != false && p != true) *fcp->predicate &= p; @@ -4507,6 +4523,7 @@ ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge) size_time_entry *e; auto_vec<int, 8> operand_map; auto_vec<HOST_WIDE_INT, 8> offset_map; + auto_vec<bool, 8> agg_preserved_map; int i; ipa_predicate toplev_predicate; class ipa_call_summary *es = ipa_call_summaries->get (edge); @@ -4536,6 +4553,7 @@ ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge) { operand_map.safe_grow_cleared (count, true); offset_map.safe_grow_cleared (count, true); + agg_preserved_map.safe_grow_cleared (count, true); } for (i = 0; i < count; i++) { @@ -4546,9 +4564,11 @@ ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge) if (jfunc->type == IPA_JF_PASS_THROUGH) { if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR) - map = ipa_get_jf_pass_through_formal_id (jfunc); - if (!ipa_get_jf_pass_through_agg_preserved (jfunc)) - offset_map[i] = -1; + { + map = ipa_get_jf_pass_through_formal_id (jfunc); + agg_preserved_map[i] + = ipa_get_jf_pass_through_agg_preserved (jfunc); + } } else if (jfunc->type == IPA_JF_ANCESTOR) { @@ -4556,9 +4576,9 @@ ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge) if (offset >= 0 && offset < INT_MAX) { map = ipa_get_jf_ancestor_formal_id (jfunc); - if (!ipa_get_jf_ancestor_agg_preserved (jfunc)) - offset = -1; offset_map[i] = offset; + agg_preserved_map[i] + = ipa_get_jf_ancestor_agg_preserved (jfunc); } } operand_map[i] = map; @@ -4577,13 +4597,13 @@ ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge) p = e->exec_predicate.remap_after_inlining (info, params_summary, callee_info, operand_map, - offset_map, clause, + offset_map, agg_preserved_map, clause, toplev_predicate); ipa_predicate nonconstp; nonconstp = e->nonconst_predicate.remap_after_inlining (info, params_summary, callee_info, operand_map, - offset_map, clause, + offset_map, agg_preserved_map, clause, toplev_predicate); if (p != false && nonconstp != false) { @@ -4603,13 +4623,16 @@ ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge) } remap_edge_summaries (edge, edge->callee, info, params_summary, callee_info, operand_map, - offset_map, clause, &toplev_predicate); + offset_map, agg_preserved_map, clause, + &toplev_predicate); remap_freqcounting_predicate (info, params_summary, callee_info, info->loop_iterations, operand_map, - offset_map, clause, &toplev_predicate); + offset_map, agg_preserved_map, clause, + &toplev_predicate); remap_freqcounting_predicate (info, params_summary, callee_info, info->loop_strides, operand_map, - offset_map, clause, &toplev_predicate); + offset_map, agg_preserved_map, clause, + &toplev_predicate); HOST_WIDE_INT stack_frame_offset = ipa_get_stack_frame_offset (edge->callee); HOST_WIDE_INT peak = stack_frame_offset + callee_info->estimated_stack_size; @@ -4891,8 +4914,11 @@ inline_read_section (struct lto_file_decl_data *file_data, const char *data, bp = streamer_read_bitpack (&ib); c.agg_contents = bp_unpack_value (&bp, 1); c.by_ref = bp_unpack_value (&bp, 1); + c.maybe_modified = bp_unpack_value (&bp, 1); if (c.agg_contents) c.offset = streamer_read_uhwi (&ib); + else + c.offset = 0; count3 = streamer_read_uhwi (&ib); c.param_ops = NULL; if (info) @@ -5127,6 +5153,7 @@ ipa_fn_summary_write (void) bp = bitpack_create (ob->main_stream); bp_pack_value (&bp, c->agg_contents, 1); bp_pack_value (&bp, c->by_ref, 1); + bp_pack_value (&bp, c->maybe_modified, 1); streamer_write_bitpack (&bp); if (c->agg_contents) streamer_write_uhwi (ob, c->offset); diff --git a/gcc/ipa-fnsummary.h b/gcc/ipa-fnsummary.h index 0503ec4c8d75..59d1bd937b1b 100644 --- a/gcc/ipa-fnsummary.h +++ b/gcc/ipa-fnsummary.h @@ -67,6 +67,7 @@ struct agg_position_info HOST_WIDE_INT offset; bool agg_contents; bool by_ref; + bool maybe_modified; }; /* Representation of function body size and time depending on the call diff --git a/gcc/ipa-predicate.cc b/gcc/ipa-predicate.cc index 6f764ec39fc1..e07138958147 100644 --- a/gcc/ipa-predicate.cc +++ b/gcc/ipa-predicate.cc @@ -149,6 +149,9 @@ ipa_predicate::add_clause (conditions conditions, clause_t new_clause) && expr_eval_ops_equal_p (cc1->param_ops, cc2->param_ops) && cc2->agg_contents == cc1->agg_contents && cc2->by_ref == cc1->by_ref + && (!cc1->agg_contents + || (cc2->offset == cc1->offset + && cc2->maybe_modified == cc1->maybe_modified)) && types_compatible_p (cc2->type, cc1->type) && cc1->code == invert_tree_comparison (cc2->code, HONOR_NANS (cc1->val))) @@ -293,6 +296,7 @@ ipa_predicate::probability (conditions conds, condition *c = &(*conds)[i2 - ipa_predicate::first_dynamic_condition]; if (c->code == ipa_predicate::changed + && !c->maybe_modified && (c->operand_num < (int) inline_param_summary.length ())) { @@ -330,8 +334,10 @@ dump_condition (FILE *f, conditions conditions, int cond) c = &(*conditions)[cond - ipa_predicate::first_dynamic_condition]; fprintf (f, "op%i", c->operand_num); if (c->agg_contents) - fprintf (f, "[%soffset: " HOST_WIDE_INT_PRINT_DEC "]", - c->by_ref ? "ref " : "", c->offset); + fprintf (f, "[%s%soffset: " HOST_WIDE_INT_PRINT_DEC "]", + c->by_ref ? "ref " : "", + c->maybe_modified ? "maybe_modified " : "", + c->offset); for (unsigned i = 0; i < vec_safe_length (c->param_ops); i++) { @@ -506,10 +512,10 @@ ipa_predicate::remap_after_duplication (clause_t possible_truths) callee formal IDs the caller formal IDs. POSSSIBLE_TRUTHS is clause of all callee conditions that may be true in caller context. TOPLEV_PREDICATE is predicate under which callee is executed. OFFSET_MAP is an array of - offsets that need to be added to conditions, negative offset means that - conditions relying on values passed by reference have to be discarded - because they might not be preserved (and should be considered offset zero - for other purposes). */ + offsets that need to be added to conditions. AGG_PRESERVED_MAP indicates + whether memory referenced by each parameter is guaranteed to be preserved + until the call; when false for a by-reference condition, maybe_modified is + set on the remapped condition. */ ipa_predicate ipa_predicate::remap_after_inlining (class ipa_fn_summary *info, @@ -517,6 +523,7 @@ ipa_predicate::remap_after_inlining (class ipa_fn_summary *info, class ipa_fn_summary *callee_info, const vec<int> &operand_map, const vec<HOST_WIDE_INT> &offset_map, + const vec<bool> &agg_preserved_map, clause_t possible_truths, const ipa_predicate &toplev_predicate) { @@ -557,23 +564,24 @@ ipa_predicate::remap_after_inlining (class ipa_fn_summary *info, we should support in future. */ || ((!c->agg_contents || !c->by_ref) && offset_map[c->operand_num] > 0) - || (c->agg_contents && c->by_ref - && offset_map[c->operand_num] < 0)) + || (c->agg_contents + && !wi::fits_shwi_p (offset_int (c->offset) + + offset_map[c->operand_num]))) cond_predicate = true; else { struct agg_position_info ap; HOST_WIDE_INT offset_delta = offset_map[c->operand_num]; - if (offset_delta < 0) - { - gcc_checking_assert (!c->agg_contents || !c->by_ref); - offset_delta = 0; - } + gcc_checking_assert (offset_delta >= 0); gcc_assert (!c->agg_contents || c->by_ref || offset_delta == 0); - ap.offset = c->offset + offset_delta; + ap.offset = c->agg_contents ? c->offset + offset_delta : 0; ap.agg_contents = c->agg_contents; ap.by_ref = c->by_ref; + ap.maybe_modified + = c->maybe_modified + || (c->agg_contents && c->by_ref + && !agg_preserved_map[c->operand_num]); cond_predicate = add_condition (info, params_summary, operand_map[c->operand_num], c->type, &ap, c->code, @@ -647,7 +655,7 @@ add_condition (class ipa_fn_summary *summary, struct condition *c; struct condition new_cond; HOST_WIDE_INT offset; - bool agg_contents, by_ref; + bool agg_contents, by_ref, maybe_modified; expr_eval_op *op; if (params_summary) @@ -658,15 +666,18 @@ add_condition (class ipa_fn_summary *summary, offset = aggpos->offset; agg_contents = aggpos->agg_contents; by_ref = aggpos->by_ref; + maybe_modified = aggpos->maybe_modified; } else { offset = 0; agg_contents = false; by_ref = false; + maybe_modified = false; } gcc_checking_assert (operand_num >= 0); + gcc_checking_assert (!maybe_modified || (agg_contents && by_ref)); for (i = 0; vec_safe_iterate (summary->conds, i, &c); i++) { if (c->operand_num == operand_num @@ -675,7 +686,10 @@ add_condition (class ipa_fn_summary *summary, && vrp_operand_equal_p (c->val, val) && c->agg_contents == agg_contents && expr_eval_ops_equal_p (c->param_ops, param_ops) - && (!agg_contents || (c->offset == offset && c->by_ref == by_ref))) + && (!agg_contents + || (c->offset == offset + && c->by_ref == by_ref + && c->maybe_modified == maybe_modified))) return ipa_predicate::predicate_testing_cond (i); } /* Too many conditions. Give up and return constant true. */ @@ -688,6 +702,7 @@ add_condition (class ipa_fn_summary *summary, new_cond.val = val ? unshare_expr_without_location (val) : val; new_cond.agg_contents = agg_contents; new_cond.by_ref = by_ref; + new_cond.maybe_modified = maybe_modified; new_cond.offset = offset; new_cond.param_ops = vec_safe_copy (param_ops); diff --git a/gcc/ipa-predicate.h b/gcc/ipa-predicate.h index 5903b66bcdaa..19e106dfea8a 100644 --- a/gcc/ipa-predicate.h +++ b/gcc/ipa-predicate.h @@ -61,6 +61,9 @@ struct GTY(()) condition /* If agg_contents is set, this differentiates between loads from data passed by reference and by value. */ unsigned by_ref : 1; + /* If by_ref is set, this is set when we cannot prove that the value + pointed to is unmodified from the beginning of the function to use. */ + unsigned maybe_modified : 1; /* A set of sequential operations on the parameter, which can be seen as a mathematical function on the parameter. */ expr_eval_ops param_ops; @@ -252,6 +255,7 @@ public: ipa_fn_summary *, const vec<int> &, const vec<HOST_WIDE_INT> &, + const vec<bool> &, clause_t, const ipa_predicate &); void stream_in (lto_input_block *); diff --git a/gcc/ipa-prop.cc b/gcc/ipa-prop.cc index 69628bf962a0..e4f2d676c5f0 100644 --- a/gcc/ipa-prop.cc +++ b/gcc/ipa-prop.cc @@ -4285,23 +4285,77 @@ find_constructor_constant_at_offset (tree constructor, HOST_WIDE_INT req_offset) NULL. */ tree -ipa_find_agg_cst_from_init (tree scalar, HOST_WIDE_INT offset, bool by_ref) +ipa_find_agg_cst_from_init (tree scalar, HOST_WIDE_INT offset, bool by_ref, tree type) { + if (!scalar || scalar == error_mark_node) + return NULL_TREE; + if (by_ref) { if (TREE_CODE (scalar) != ADDR_EXPR) - return NULL; + return NULL_TREE; scalar = TREE_OPERAND (scalar, 0); } - if (!VAR_P (scalar) - || !is_global_var (scalar) - || !TREE_READONLY (scalar) - || !DECL_INITIAL (scalar) - || TREE_CODE (DECL_INITIAL (scalar)) != CONSTRUCTOR) - return NULL; + poly_int64 byte_offset = 0; + scalar = get_addr_base_and_unit_offset (scalar, &byte_offset); + if (!scalar) + return NULL_TREE; - return find_constructor_constant_at_offset (DECL_INITIAL (scalar), offset); + if (!CONSTANT_CLASS_P (scalar) + && TREE_CODE (scalar) != CONST_DECL + && !(VAR_P (scalar) || !TREE_READONLY (scalar))) + return NULL_TREE; + + poly_offset_int bit_off + = poly_offset_int (byte_offset) * BITS_PER_UNIT + offset; + poly_int64 const_bit_off; + if (!bit_off.to_shwi (&const_bit_off) + || !const_bit_off.is_constant (&offset) + || offset < 0) + return NULL_TREE; + + if (type && is_gimple_reg_type (type)) + { + if (!TYPE_SIZE (type) || !tree_fits_uhwi_p (TYPE_SIZE (type))) + return NULL_TREE; + unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE (type)); + if (size == 0) + return NULL_TREE; + + tree ctor = NULL_TREE; + tree from_decl = NULL_TREE; + if (VAR_P (scalar) || TREE_CODE (scalar) == CONST_DECL) + { + ctor = ctor_for_folding (scalar); + if (ctor == error_mark_node) + return NULL_TREE; + from_decl = scalar; + } + else + ctor = scalar; + + tree res = (ctor + ? fold_ctor_reference (type, ctor, offset, size, from_decl) + : build_zero_cst (type)); + if (res && is_gimple_ip_invariant (res)) + return res; + return NULL_TREE; + } + + if (VAR_P (scalar) || TREE_CODE (scalar) == CONST_DECL) + { + tree ctor = ctor_for_folding (scalar); + if (!ctor || ctor == error_mark_node) + return NULL_TREE; + if (TREE_CODE (ctor) == CONSTRUCTOR) + return find_constructor_constant_at_offset (ctor, offset); + if (offset == 0 + && is_gimple_reg_type (TREE_TYPE (ctor)) + && is_gimple_ip_invariant (ctor)) + return ctor; + } + return NULL_TREE; } /* Retrieve value from AGG_JFUNC for the given OFFSET or return NULL if there @@ -4426,7 +4480,9 @@ try_make_edge_direct_simple_call (struct cgraph_edge *ie, { if (scalar) target = ipa_find_agg_cst_from_init (scalar, sii->offset, sii->by_ref); - if (!target && sii->guaranteed_unmodified) + if (!target + && (sii->guaranteed_unmodified + || (sii->by_ref && ipa_points_to_readonly_memory_p (scalar)))) target = ipa_find_agg_cst_from_jfunc_items (&jfunc->agg, new_root_info, new_root, sii->offset, sii->by_ref); diff --git a/gcc/ipa-prop.h b/gcc/ipa-prop.h index 9099afaea304..176701f2614e 100644 --- a/gcc/ipa-prop.h +++ b/gcc/ipa-prop.h @@ -230,21 +230,25 @@ class ipa_argagg_value_list { public: ipa_argagg_value_list () = delete; - ipa_argagg_value_list (const vec<ipa_argagg_value, va_gc> *values) - : m_elts (values) + ipa_argagg_value_list (const vec<ipa_argagg_value, va_gc> *values, + array_slice<const tree> known_vals = {}) + : m_elts (values), m_known_vals (known_vals) {} - ipa_argagg_value_list (const vec<ipa_argagg_value> *values) - : m_elts (*values) + ipa_argagg_value_list (const vec<ipa_argagg_value> *values, + array_slice<const tree> known_vals = {}) + : m_elts (*values), m_known_vals (known_vals) {} ipa_argagg_value_list (const ipa_auto_call_arg_values *aavals); ipa_argagg_value_list (const ipa_call_arg_values *gavals); ipa_argagg_value_list (const ipcp_transformation *tinfo); /* Return the aggregate constant stored for INDEX at UNIT_OFFSET, if it is - passed by reference or not according to BY_REF, or NULL_TREE - otherwise. */ + passed by reference or not according to BY_REF (and if MAYBE_MODIFIED is + true, only when the memory passed by reference is read-only), or + NULL_TREE otherwise. */ - tree get_value (int index, unsigned unit_offset, bool by_ref) const; + tree get_value (int index, unsigned unit_offset, bool by_ref, + bool maybe_modified = false) const; /* Return the aggregate constant stored for INDEX at UNIT_OFFSET, not performing any check of whether value is passed by reference. Return @@ -297,6 +301,10 @@ public: /* Array slice pointing to the actual storage. */ array_slice<const ipa_argagg_value> m_elts; + + /* Array slice of known scalar values of arguments, if available. */ + + array_slice<const tree> m_known_vals; }; /* Info about value ranges. */ @@ -542,7 +550,7 @@ public: inline ipa_argagg_value_list ::ipa_argagg_value_list (const ipa_auto_call_arg_values *aavals) - : m_elts (aavals->m_known_aggs) + : m_elts (aavals->m_known_aggs), m_known_vals (aavals->m_known_vals) {} /* Class bundling the various potentially known properties about actual @@ -594,7 +602,7 @@ public: inline ipa_argagg_value_list ::ipa_argagg_value_list (const ipa_call_arg_values *gavals) - : m_elts (gavals->m_known_aggs) + : m_elts (gavals->m_known_aggs), m_known_vals (gavals->m_known_vals) {} /* Summary describing a single formal parameter. */ @@ -1210,7 +1218,7 @@ void ipa_analyze_var_static_initializer (varpool_node *node); /* Aggregate jump function related functions. */ tree ipa_find_agg_cst_from_init (tree scalar, HOST_WIDE_INT offset, - bool by_ref); + bool by_ref, tree type = NULL); bool ipa_load_from_parm_agg (struct ipa_func_body_info *fbi, vec<ipa_param_descriptor, va_gc> *descriptors, gimple *stmt, tree op, int *index_p, diff --git a/gcc/ipa-utils.cc b/gcc/ipa-utils.cc index ac052ad4b346..adf61e756a19 100644 --- a/gcc/ipa-utils.cc +++ b/gcc/ipa-utils.cc @@ -1046,3 +1046,14 @@ find_always_executed_bbs (function *fun, bool assume_return_or_eh) return ret; } + +/* Return true if T is an ADDR_EXPR of a read-only memory location. */ + +bool +ipa_points_to_readonly_memory_p (tree t) +{ + if (!t || TREE_CODE (t) != ADDR_EXPR) + return false; + tree base = get_base_address (TREE_OPERAND (t, 0)); + return (base && TREE_READONLY (base)); +} diff --git a/gcc/ipa-utils.h b/gcc/ipa-utils.h index d689d45c70c9..3dea564293fd 100644 --- a/gcc/ipa-utils.h +++ b/gcc/ipa-utils.h @@ -115,6 +115,7 @@ void set_type_canonical_for_odr_type (tree type, tree canonical); void warn_function_returns_nonnull (tree); void register_odr_enum (tree type); +bool ipa_points_to_readonly_memory_p (tree t); /* Return vector containing possible targets of polymorphic call E. If COMPLETEP is non-NULL, store true if the list is complete. diff --git a/gcc/testsuite/gcc.dg/ipa/fnsummary-2.c b/gcc/testsuite/gcc.dg/ipa/fnsummary-2.c new file mode 100644 index 000000000000..c6b56f918c26 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ipa/fnsummary-2.c @@ -0,0 +1,37 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-partial-inlining -fno-ipa-sra -fdump-ipa-fnsummary-details -fdump-ipa-inline-details" } */ + +struct S +{ + int a; + int b; +}; + +static const struct S cs = { 10, 42 }; + +void ext (void); +void cold_work (void); + +static int __attribute__((noinline)) +test_agg (const struct S *p) +{ + ext (); + if (p->b != 42) + { + cold_work (); + cold_work (); + cold_work (); + cold_work (); + return 1; + } + return 0; +} + +int +caller (void) +{ + return test_agg (&cs); +} + +/* { dg-final { scan-ipa-dump "op0\\\[ref maybe_modified offset: 32\\\] != 42" "fnsummary" } } */ +/* { dg-final { scan-ipa-dump "Known to be false:.*op0\\\[ref maybe_modified offset: 32\\\] != 42" "inline" } } */ diff --git a/gcc/testsuite/gcc.dg/ipa/fnsummary-3.c b/gcc/testsuite/gcc.dg/ipa/fnsummary-3.c new file mode 100644 index 000000000000..00f2457ba8a9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ipa/fnsummary-3.c @@ -0,0 +1,71 @@ +/* { dg-do run } */ +/* { dg-options "-O2 -fno-partial-inlining -fno-ipa-sra -fdump-ipa-cp-details -fdump-ipa-inline-details" } */ + +struct S +{ + int a; + int b; +}; + +struct S ms; + +__attribute__((noipa)) void +clobber (void) +{ + ms.b = 99; +} + +__attribute__((noipa)) void +cold_heavy (void) +{ + volatile int x = 1; + (void) x; +} + +static int __attribute__((noinline)) +callee (struct S *p) +{ + clobber (); + if (p->b != 42) + return 1; + cold_heavy (); + cold_heavy (); + cold_heavy (); + cold_heavy (); + return 0; +} + +static inline int +leaf_check (struct S *p) +{ + if (p->b != 42) + return 1; + cold_heavy (); + cold_heavy (); + cold_heavy (); + cold_heavy (); + return 0; +} + +static int __attribute__((noinline)) +mid (struct S *p) +{ + clobber (); + return leaf_check (p); +} + +int +main (void) +{ + ms.a = 10; + ms.b = 42; + if (callee (&ms) != 1) + __builtin_abort (); + ms.b = 42; + if (mid (&ms) != 1) + __builtin_abort (); + return 0; +} + +/* { dg-final { scan-ipa-dump-not {Known to be false:[^\n]*maybe_modified} "cp" } } */ +/* { dg-final { scan-ipa-dump-not {Known to be false:[^\n]*maybe_modified} "inline" } } */ diff --git a/gcc/testsuite/gcc.dg/ipa/fnsummary-4.c b/gcc/testsuite/gcc.dg/ipa/fnsummary-4.c new file mode 100644 index 000000000000..0e4b83d15e90 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ipa/fnsummary-4.c @@ -0,0 +1,42 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-partial-inlining -fno-ipa-cp -fno-ipa-sra -fno-early-inlining -fdump-ipa-inline-details" } */ + +struct S +{ + int a; + int b; +}; + +static const struct S cz = { 0, 0 }; +void ext (void); +void cold_work (void); + +static int +leaf (const struct S *p) +{ + if (p->b != 0) + { + cold_work (); + cold_work (); + cold_work (); + cold_work (); + return 1; + } + return 0; +} + +int +mid (const struct S *p) +{ + ext (); + return leaf (p); +} + +int +caller (void) +{ + return mid (&cz); +} + +/* { dg-final { scan-ipa-dump "op0\\\[ref maybe_modified offset: 32\\\] != 0" "inline" } } */ +/* { dg-final { scan-ipa-dump "Known to be false:.*op0\\\[ref offset: 32\\\] != 0" "inline" } } */ diff --git a/gcc/testsuite/gcc.dg/ipa/fnsummary-5.c b/gcc/testsuite/gcc.dg/ipa/fnsummary-5.c new file mode 100644 index 000000000000..59ecffc48f5d --- /dev/null +++ b/gcc/testsuite/gcc.dg/ipa/fnsummary-5.c @@ -0,0 +1,72 @@ +/* { dg-do run } */ +/* { dg-require-effective-target lto } */ +/* { dg-options "-O2 -flto -fno-partial-inlining -fno-ipa-sra -fdump-ipa-inline-details" } */ + +struct S +{ + int a; + int b; +}; + +struct S ms; + +__attribute__((noipa)) void +clobber (void) +{ + ms.b = 99; +} + +__attribute__((noipa)) void +cold_heavy (void) +{ + volatile int x = 1; + (void) x; +} + +static int __attribute__((noinline)) +callee (struct S *p) +{ + clobber (); + if (p->b != 42) + return 1; + cold_heavy (); + cold_heavy (); + cold_heavy (); + cold_heavy (); + return 0; +} + +static inline int +leaf_check (struct S *p) +{ + if (p->b != 42) + return 1; + cold_heavy (); + cold_heavy (); + cold_heavy (); + cold_heavy (); + return 0; +} + +static int __attribute__((noinline)) +mid (struct S *p) +{ + clobber (); + return leaf_check (p); +} + +int +main (void) +{ + ms.a = 10; + ms.b = 42; + if (callee (&ms) != 1) + __builtin_abort (); + ms.b = 42; + if (mid (&ms) != 1) + __builtin_abort (); + return 0; +} + +/* { dg-final { scan-wpa-ipa-dump "op0\\\[ref maybe_modified offset: 32\\\] == 42" "inline" } } */ +/* { dg-final { scan-wpa-ipa-dump-not {Known to be false:[^\n]*maybe_modified} "inline" } } */ diff --git a/gcc/testsuite/gcc.dg/ipa/fnsummary-6.c b/gcc/testsuite/gcc.dg/ipa/fnsummary-6.c new file mode 100644 index 000000000000..1e0032771daa --- /dev/null +++ b/gcc/testsuite/gcc.dg/ipa/fnsummary-6.c @@ -0,0 +1,50 @@ +/* { dg-do compile } */ +/* { dg-options "-Os -fdump-tree-einline-details -fdump-tree-optimized" } */ + +void long_call (void); + +void +test (const char *p) +{ + if (p[0] == '%') + { + long_call (); + long_call (); + long_call (); + long_call (); + long_call (); + long_call (); + long_call (); + long_call (); + long_call (); + long_call (); + long_call (); + } + long_call (); + if (p[0] == '%') + { + long_call (); + long_call (); + long_call (); + long_call (); + long_call (); + long_call (); + long_call (); + long_call (); + long_call (); + long_call (); + long_call (); + } +} + +int +main (void) +{ + test ("test"); + test ("xtest" + 1); + return 0; +} + +/* { dg-final { scan-tree-dump "Known to be false:.*op0\\\[ref maybe_modified offset: 0\\\] == 37" "einline" } } */ +/* { dg-final { scan-tree-dump-times "Inlining test/" 2 "einline" } } */ +/* { dg-final { scan-tree-dump-not " test \\(" "optimized" } } */
