Hi,
this patch extends ipa-cp to prpagate nvariant base addresses when
offset is unknown. It is last bigger piece needed for cppcheck. Large
patch is quite mechanical. We basically need to add
IPA_JF_CONST_UNKNOWN_OFFSET (or alternatively add flag to the exisitng
const function, but we would need to check the flag in basically all
uses), extend agg files and jump function by uknown offsets and do the
checking.
Bootstrapped/regtested x86_64-linux. Martin, I wonder if you have ideas
for improvmeents on this.
Honza
gcc/ChangeLog:
* ipa-prop.h (enum jump_func_type): Add IPA_JF_CONST_UNKNOWN_OFFSET.
(struct ipa_agg_jf_item): Add unknown_offset field.
(struct ipa_argagg_value): Likewise.
(class ipa_argagg_value_list): Declare get_base_value.
(struct ipa_jump_func): Add constant_unknown_offset union member.
(ipa_get_jf_constant_unknown_offset): New inline helper.
(ipa_jf_pass_through_unknown_offset_p): New inline helper.
(class ipa_call_arg_values): Add m_known_base_vals and
safe_base_sval_at.
(class ipa_auto_call_arg_values): Add m_known_base_vals and
safe_base_sval_at.
(ipa_base_value_from_jfunc): Declare.
(ipa_push_agg_values_from_jfunc): Add default allow_unknown_offset
parameter.
* ipa-prop.cc (ipa_dump_jump_function): Dump unknown-offset constants
and pass-throughs.
(ipa_set_jf_constant): Handle constant_unknown_offset.
(ipa_set_jf_constant_unknown_offset): New function.
(ipa_set_jf_arith_pass_through): Allow NULL_TREE operand for
POINTER_PLUS_EXPR.
(struct ipa_unknown_off_parm_data): Track saw_offset and
allow_invariant_cst.
(ipa_unknown_off_parm_leaf): Recognize invariant base address
constants when allowed.
(analyze_unknown_offset_ptr_origin): New function.
(ipa_load_from_parm_agg_unknown_offset): Initialize new fields of
ipa_unknown_off_parm_data.
(struct ipa_known_agg_contents_list): Add unknown_offset field.
(build_agg_jump_func_from_list): Propagate unknown_offset flag.
(analyze_agg_content_value): Analyze unknown-offset pointer origins
for aggregate fields.
(extract_mem_content): Update for unknown-offset pointer items.
(ipa_compute_jump_functions_for_edge): Build IPA_JF_CONST_UNKNOWN_OFFSET
and unknown-offset POINTER_PLUS_EXPR pass-through jump functions.
(update_jump_functions_after_inlining): Handle unknown-offset scalar
and aggregate jump functions across inlining.
(ipa_duplicate_jump_function): Copy constant_unknown_offset.
(ipa_write_jump_function): Stream IPA_JF_CONST_UNKNOWN_OFFSET and
item unknown_offset flag.
(ipa_read_jump_function): Likewise.
(write_ipcp_transformation_info): Stream ipa_argagg_value
unknown_offset flag.
(read_ipcp_transformation_info): Likewise.
(ipcp_get_aggregate_const): Skip unknown_offset aggregate values.
(ipa_agg_jump_functions_equivalent_p): Compare unknown_offset flag.
(ipa_jump_functions_equivalent_p): Compare IPA_JF_CONST_UNKNOWN_OFFSET.
* ipa-cp.cc: Include "tree-dfa.h".
(ipa_argagg_value_list::dump): Dump unknown-offset items.
(ipa_argagg_value_list::get_value): Skip unknown_offset items and
check unit offsets consistently.
(ipa_argagg_value_list::get_base_value): New method.
(ipa_argagg_value_list::superset_of_p): Require exact unknown_offset
match.
(ipa_argagg_value_list::push_adjusted_values): Preserve unknown_offset.
(push_agg_values_from_plats): Handle unknown-offset aggregate values.
(intersect_argaggs_with): Preserve matching unknown_offset entries.
(ipa_get_jf_arith_result): Handle unknown-offset POINTER_PLUS_EXPR.
(ipa_value_from_jfunc): Ignore IPA_JF_CONST_UNKNOWN_OFFSET for exact
values.
(ipa_base_value_from_jfunc): New function.
(ipa_vr_intersect_with_arith_jfunc): Guard against NULL operand.
(ipa_agg_value_from_jfunc): Handle unknown-offset aggregate items.
(ipa_agg_base_value_from_jfunc): New function.
(ipa_push_agg_values_from_jfunc): Handle allow_unknown_offset.
(propagate_bits_across_jump_function): Skip unknown-offset
pass-throughs.
(propagate_aggregate_lattice): Propagate unknown-offset aggregate
values.
(estimate_local_effects): Evaluate base values for unknown offsets.
(push_agg_values_for_index_from_edge): Preserve unknown_offset values.
* ipa-predicate.h (struct condition): Update offset comment.
(ipa_predicate::remap_after_inlining): Add ipa_edge_args * parameter.
* ipa-predicate.cc (ipa_predicate::add_clause): Allow offset >= -1 for
unknown-offset conditions.
(dump_condition): Dump unknown-offset conditions.
(ipa_predicate::remap_after_inlining): Remap unknown-offset conditions
and 1-byte exact-offset by-ref conditions across POINTER_PLUS_EXPR
and aggregate jump functions.
(add_condition): Pass full condition position.
* ipa-fnsummary.cc (evaluate_unknown_offset_condition): Evaluate
1-byte integer unknown-offset and exact-offset by-ref conditions from
base values.
(evaluate_conditions_for_known_args): Fall back to
evaluate_unknown_offset_condition for exact-offset by-ref conditions.
(evaluate_properties_for_edge): Populate m_known_base_vals.
(unmodified_parm_or_parm_agg_item): Relax assertion for
aggpos->offset >= -1 with unknown_offset.
(ipa_cached_call_context::duplicate_from): Copy m_known_base_vals.
(ipa_cached_call_context::release): Release m_known_base_vals.
(ipa_call_context::equal_to): Compare m_known_base_vals.
(remap_edge_params): Handle IPA_JF_CONST_UNKNOWN_OFFSET.
(remap_edge_summaries): Thread args to remap_after_inlining.
(remap_freqcounting_predicate): Likewise.
(ipa_merge_fn_summary_after_inlining): Pass args when remapping edge
summaries and predicates.
(inline_read_section): Stream signed offset when agg_contents is set.
(ipa_fn_summary_write): Likewise.
gcc/testsuite/ChangeLog:
* gcc.dg/ipa/pr127171-4.c: New test.
* gcc.dg/ipa/pr127171-5.c: New test.
Assisted-by: Google Carbon
---
gcc/ipa-cp.cc | 304 ++++++++++++++++-
gcc/ipa-fnsummary.cc | 167 ++++++---
gcc/ipa-predicate.cc | 153 ++++++++-
gcc/ipa-predicate.h | 6 +-
gcc/ipa-prop.cc | 467 ++++++++++++++++++++++++--
gcc/ipa-prop.h | 67 +++-
gcc/testsuite/gcc.dg/ipa/pr127171-4.c | 52 +++
gcc/testsuite/gcc.dg/ipa/pr127171-5.c | 99 ++++++
8 files changed, 1224 insertions(+), 91 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/ipa/pr127171-4.c
create mode 100644 gcc/testsuite/gcc.dg/ipa/pr127171-5.c
diff --git a/gcc/ipa-cp.cc b/gcc/ipa-cp.cc
index a90c39568ae..cf36a26fa7a 100644
--- a/gcc/ipa-cp.cc
+++ b/gcc/ipa-cp.cc
@@ -126,6 +126,7 @@ along with GCC; see the file COPYING3. If not see
#include "ipa-fnsummary.h"
#include "ipa-utils.h"
#include "tree-ssa-ccp.h"
+#include "tree-dfa.h"
#include "stringpool.h"
#include "attribs.h"
#include "dbgcnt.h"
@@ -1107,6 +1108,8 @@ ipa_argagg_value_list::dump (FILE *f)
print_generic_expr (f, av.value);
if (av.by_ref)
fprintf (f, "(by_ref)");
+ if (av.unknown_offset)
+ fprintf (f, "(unknown_offset)");
if (av.killed)
fprintf (f, "(killed)");
comma = true;
@@ -1200,7 +1203,9 @@ tree
ipa_argagg_value_list::get_value (int index, unsigned unit_offset) const
{
const ipa_argagg_value *av = get_elt (index, unit_offset);
- return av ? av->value : NULL_TREE;
+ if (!av || av->unknown_offset)
+ return NULL_TREE;
+ return av->value;
}
/* Return the aggregate constant stored for INDEX at UNIT_OFFSET, if it is
@@ -1213,7 +1218,27 @@ ipa_argagg_value_list::get_value (int index, unsigned
unit_offset,
bool by_ref, bool maybe_modified) const
{
const ipa_argagg_value *av = get_elt (index, unit_offset);
- if (!av || av->by_ref != by_ref)
+ if (!av || av->by_ref != by_ref || av->unknown_offset)
+ 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 the base pointer constant stored for INDEX at UNIT_OFFSET if it is
+ 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), regardless of
+ unknown_offset, or NULL_TREE otherwise. */
+
+tree
+ipa_argagg_value_list::get_base_value (int index, unsigned unit_offset,
+ bool by_ref, bool maybe_modified) const
+{
+ const ipa_argagg_value *av = get_elt (index, unit_offset);
+ if (!av || av->by_ref != by_ref || av->killed)
return NULL_TREE;
if (maybe_modified
&& (!by_ref
@@ -1245,6 +1270,7 @@ ipa_argagg_value_list::superset_of_p (const
ipa_argagg_value_list &other) const
|| m_elts[j].index != other_index
|| m_elts[j].unit_offset != other_offset
|| m_elts[j].by_ref != other.m_elts[i].by_ref
+ || m_elts[j].unknown_offset != other.m_elts[i].unknown_offset
|| !m_elts[j].value
|| !values_equal_for_ipcp_p (m_elts[j].value, other.m_elts[i].value))
return false;
@@ -1283,6 +1309,7 @@ ipa_argagg_value_list::push_adjusted_values (unsigned
src_index,
new_av.by_ref = av->by_ref;
gcc_assert (!av->killed);
new_av.killed = false;
+ new_av.unknown_offset = av->unknown_offset;
/* Quick check that the offsets we push are indeed increasing. */
gcc_assert (first
@@ -1320,6 +1347,7 @@ push_agg_values_from_plats (ipcp_param_lattices *plats,
int dest_index,
iav.index = dest_index;
iav.by_ref = plats->aggs_by_ref;
iav.killed = false;
+ iav.unknown_offset = false;
gcc_assert (first
|| iav.unit_offset > prev_unit_offset);
@@ -1364,6 +1392,7 @@ intersect_argaggs_with (vec<ipa_argagg_value> &elts,
if (other[j].index == this_index
&& other[j].unit_offset == this_offset
&& other[j].by_ref == elts[i].by_ref
+ && other[j].unknown_offset == elts[i].unknown_offset
&& other[j].value
&& values_equal_for_ipcp_p (other[j].value, elts[i].value))
valid_entries++;
@@ -1602,6 +1631,8 @@ ipa_get_jf_arith_result (enum tree_code opcode, tree
input, tree operand,
if (TREE_CODE_CLASS (opcode) == tcc_unary)
res = fold_unary (opcode, res_type, input);
+ else if (!operand)
+ return NULL_TREE;
else
res = fold_binary (opcode, res_type, input, operand);
@@ -1698,6 +1729,77 @@ ipa_value_from_jfunc (class ipa_node_params *info,
struct ipa_jump_func *jfunc,
return NULL_TREE;
}
+/* If JFUNC describes an address constant or pointer pass-through (possibly
+ with unknown offset) rooted at a constant address or variable declaration,
+ return the canonical base ADDR_EXPR converted to PARM_TYPE, or NULL_TREE
+ otherwise. */
+
+tree
+ipa_base_value_from_jfunc (class ipa_node_params *info,
+ struct ipa_jump_func *jfunc, tree parm_type)
+{
+ if (!parm_type || !POINTER_TYPE_P (parm_type))
+ return NULL_TREE;
+
+ tree val = NULL_TREE;
+ switch (jfunc->type)
+ {
+ case IPA_JF_CONST_UNKNOWN_OFFSET:
+ val = ipa_get_jf_constant_unknown_offset (jfunc);
+ break;
+ case IPA_JF_CONST:
+ val = ipa_get_jf_constant (jfunc);
+ break;
+ case IPA_JF_PASS_THROUGH:
+ case IPA_JF_ANCESTOR:
+ {
+ int idx;
+ if (jfunc->type == IPA_JF_PASS_THROUGH)
+ {
+ enum tree_code op = ipa_get_jf_pass_through_operation (jfunc);
+ if (op != NOP_EXPR && op != POINTER_PLUS_EXPR)
+ return NULL_TREE;
+ idx = ipa_get_jf_pass_through_formal_id (jfunc);
+ }
+ else
+ idx = ipa_get_jf_ancestor_formal_id (jfunc);
+
+ if (info->ipcp_orig_node)
+ {
+ if ((unsigned) idx < info->known_csts.length ())
+ val = info->known_csts[idx];
+ }
+ else if (!info->lattices.is_empty ()
+ && idx < ipa_get_param_count (info))
+ {
+ ipcp_lattice<tree> *lat = ipa_get_scalar_lat (info, idx);
+ if (lat->is_single_const ())
+ val = lat->values->value;
+ }
+ }
+ break;
+ default:
+ return NULL_TREE;
+ }
+
+ if (!val)
+ return NULL_TREE;
+
+ STRIP_NOPS (val);
+ if (TREE_CODE (val) != ADDR_EXPR)
+ return NULL_TREE;
+
+ poly_int64 off;
+ tree base = get_addr_base_and_unit_offset (TREE_OPERAND (val, 0), &off);
+ if (!base || (!CONSTANT_CLASS_P (base) && !DECL_P (base)))
+ return NULL_TREE;
+
+ tree res = build_fold_addr_expr (base);
+ if (!is_gimple_ip_invariant (res))
+ return NULL_TREE;
+ return ipacp_value_safe_for_type (parm_type, res);
+}
+
/* Determine whether JFUNC evaluates to single known polymorphic context, given
that INFO describes the caller node or the one it is inlined to, CS is the
call graph edge corresponding to JFUNC and CSIDX index of the described
@@ -1853,6 +1955,8 @@ ipa_vr_intersect_with_arith_jfunc (vrange &vr,
}
tree operand = ipa_get_jf_pass_through_operand (jfunc);
+ if (!operand)
+ return;
range_op_handler handler (operation);
if (!handler)
return;
@@ -1927,6 +2031,7 @@ ipa_agg_value_from_jfunc (ipa_node_params *info,
cgraph_node *node,
if (item->offset < 0
|| item->jftype == IPA_JF_UNKNOWN
+ || item->jftype == IPA_JF_CONST_UNKNOWN_OFFSET
|| item->offset >= (HOST_WIDE_INT) UINT_MAX * BITS_PER_UNIT)
return NULL_TREE;
@@ -2065,22 +2170,204 @@ ipa_agg_value_from_jfunc (ipa_node_params *info,
cgraph_node *node,
return ipacp_value_safe_for_type (item->type, cstval);
}
+/* If ITEM, a jump function for an aggregate part, evaluates to an address
+ base constant (possibly with unknown offset), return its canonical base
+ ADDR_EXPR converted to ITEM->type, or NULL_TREE otherwise. */
+
+static tree
+ipa_agg_base_value_from_jfunc (ipa_node_params *info, cgraph_node *node,
+ const ipa_agg_jf_item *item)
+{
+ if (item->offset < 0
+ || item->jftype == IPA_JF_UNKNOWN
+ || item->offset >= (HOST_WIDE_INT) UINT_MAX * BITS_PER_UNIT
+ || !item->type
+ || !POINTER_TYPE_P (item->type))
+ return NULL_TREE;
+
+ tree val = NULL_TREE;
+ switch (item->jftype)
+ {
+ case IPA_JF_CONST_UNKNOWN_OFFSET:
+ val = item->value.constant_unknown_offset;
+ break;
+ case IPA_JF_CONST:
+ val = item->value.constant;
+ break;
+ case IPA_JF_PASS_THROUGH:
+ if (item->value.pass_through.operation != NOP_EXPR
+ && item->value.pass_through.operation != POINTER_PLUS_EXPR)
+ return NULL_TREE;
+ else
+ {
+ int src_idx = item->value.pass_through.formal_id;
+ if (info->ipcp_orig_node)
+ {
+ if ((unsigned) src_idx < info->known_csts.length ())
+ val = info->known_csts[src_idx];
+ }
+ else if (!info->lattices.is_empty ()
+ && src_idx < ipa_get_param_count (info))
+ {
+ ipcp_lattice<tree> *lat = ipa_get_scalar_lat (info, src_idx);
+ if (lat->is_single_const ())
+ val = lat->values->value;
+ }
+ }
+ break;
+ case IPA_JF_LOAD_AGG:
+ if (item->value.load_agg.pass_through.operation != NOP_EXPR
+ && item->value.load_agg.pass_through.operation != POINTER_PLUS_EXPR)
+ return NULL_TREE;
+ else
+ {
+ int src_idx = item->value.load_agg.pass_through.formal_id;
+ if (info->ipcp_orig_node)
+ {
+ if ((unsigned) src_idx < info->known_csts.length ()
+ && info->known_csts[src_idx])
+ val = ipa_find_agg_cst_from_init (info->known_csts[src_idx],
+ item->value.load_agg.offset,
+ item->value.load_agg.by_ref,
+ item->value.load_agg.type);
+ if (!val && (item->value.load_agg.offset % BITS_PER_UNIT) == 0)
+ if (ipcp_transformation *ts
+ = ipcp_get_transformation_summary (node))
+ {
+ ipa_argagg_value_list avl (ts);
+ val = avl.get_base_value (src_idx,
+ item->value.load_agg.offset
+ / BITS_PER_UNIT,
+ item->value.load_agg.by_ref);
+ }
+ }
+ else if (!info->lattices.is_empty ()
+ && src_idx < ipa_get_param_count (info))
+ {
+ ipcp_param_lattices *src_plats
+ = ipa_get_parm_lattices (info, src_idx);
+ if (!src_plats->itself.bottom
+ && !src_plats->itself.contains_variable
+ && src_plats->itself.values)
+ {
+ tree common = NULL_TREE;
+ for (ipcp_value<tree> *v = src_plats->itself.values;
+ v; v = v->next)
+ {
+ if (v->self_recursion_generated_p ())
+ {
+ common = NULL_TREE;
+ break;
+ }
+ tree cst = ipa_find_agg_cst_from_init
+ (v->value, item->value.load_agg.offset,
+ item->value.load_agg.by_ref,
+ item->value.load_agg.type);
+ if (!cst)
+ {
+ common = NULL_TREE;
+ break;
+ }
+ STRIP_NOPS (cst);
+ if (TREE_CODE (cst) != ADDR_EXPR)
+ {
+ common = NULL_TREE;
+ break;
+ }
+ poly_int64 off;
+ tree b = get_addr_base_and_unit_offset
+ (TREE_OPERAND (cst, 0), &off);
+ if (!b || (!CONSTANT_CLASS_P (b) && !DECL_P (b)))
+ {
+ common = NULL_TREE;
+ break;
+ }
+ tree bcst = build_fold_addr_expr (b);
+ if (!is_gimple_ip_invariant (bcst)
+ || !(bcst = ipacp_value_safe_for_type (item->type,
+ bcst)))
+ {
+ common = NULL_TREE;
+ break;
+ }
+ if (!common)
+ common = bcst;
+ else if (!values_equal_for_ipcp_p (common, bcst))
+ {
+ common = NULL_TREE;
+ break;
+ }
+ }
+ if (common)
+ return common;
+ }
+ if (src_plats->aggs
+ && !src_plats->aggs_bottom
+ && !src_plats->aggs_contain_variable
+ && src_plats->aggs_by_ref == item->value.load_agg.by_ref)
+ for (ipcp_agg_lattice *aglat = src_plats->aggs;
+ aglat; aglat = aglat->next)
+ {
+ if (aglat->offset > item->value.load_agg.offset)
+ break;
+ if (aglat->offset == item->value.load_agg.offset)
+ {
+ if (aglat->is_single_const ())
+ val = aglat->values->value;
+ break;
+ }
+ }
+ }
+ }
+ break;
+ default:
+ return NULL_TREE;
+ }
+
+ if (!val)
+ return NULL_TREE;
+
+ STRIP_NOPS (val);
+ if (TREE_CODE (val) != ADDR_EXPR)
+ return NULL_TREE;
+
+ poly_int64 off;
+ tree base = get_addr_base_and_unit_offset (TREE_OPERAND (val, 0), &off);
+ if (!base || (!CONSTANT_CLASS_P (base) && !DECL_P (base)))
+ return NULL_TREE;
+
+ tree res = build_fold_addr_expr (base);
+ if (!is_gimple_ip_invariant (res))
+ return NULL_TREE;
+ return ipacp_value_safe_for_type (item->type, res);
+}
+
/* Process all items in AGG_JFUNC relative to caller (or the node the original
caller is inlined to) NODE which described by INFO and push the results to
- RES as describing values passed in parameter DST_INDEX. */
+ RES as describing values passed in parameter DST_INDEX. If
+ ALLOW_UNKNOWN_OFFSET is true, also push base pointer values derived from
+ unknown-offset pointer pass-through items with the unknown_offset flag
+ set. */
void
ipa_push_agg_values_from_jfunc (ipa_node_params *info, cgraph_node *node,
ipa_agg_jump_function *agg_jfunc,
unsigned dst_index,
- vec<ipa_argagg_value> *res)
+ vec<ipa_argagg_value> *res,
+ bool allow_unknown_offset)
{
unsigned prev_unit_offset = 0;
bool first = true;
for (const ipa_agg_jf_item &item : agg_jfunc->items)
{
+ bool unknown_off = false;
tree value = ipa_agg_value_from_jfunc (info, node, &item);
+ if (!value && allow_unknown_offset)
+ {
+ value = ipa_agg_base_value_from_jfunc (info, node, &item);
+ unknown_off = true;
+ }
if (!value)
continue;
@@ -2090,6 +2377,7 @@ ipa_push_agg_values_from_jfunc (ipa_node_params *info,
cgraph_node *node,
iav.index = dst_index;
iav.by_ref = agg_jfunc->by_ref;
iav.killed = 0;
+ iav.unknown_offset = unknown_off;
gcc_assert (first
|| iav.unit_offset > prev_unit_offset);
@@ -2698,7 +2986,8 @@ propagate_bits_across_jump_function (cgraph_edge *cs, int
idx,
result of x & 0xff == 0xff, which gets computed during ccp1 pass
and we store it in jump function during analysis stage. */
- if (!src_lats->bits_lattice.bottom_p ()
+ if ((code == NOP_EXPR || TREE_CODE_CLASS (code) == tcc_unary || operand)
+ && !src_lats->bits_lattice.bottom_p ()
&& !src_lats->bits_lattice.recipient_only_p ())
{
if (!op_type)
@@ -2981,6 +3270,9 @@ propagate_aggregate_lattice (struct cgraph_edge *cs,
return aglat->add_value (value, cs, NULL, 0);
}
+ if (item->jftype == IPA_JF_CONST_UNKNOWN_OFFSET)
+ return aglat->set_contains_variable ();
+
gcc_checking_assert (item->jftype == IPA_JF_PASS_THROUGH
|| item->jftype == IPA_JF_LOAD_AGG);
@@ -3930,6 +4222,7 @@ estimate_local_effects (struct cgraph_node *node)
avals.m_known_aggs[j].index = index;
avals.m_known_aggs[j].by_ref = plats->aggs_by_ref;
avals.m_known_aggs[j].killed = false;
+ avals.m_known_aggs[j].unknown_offset = false;
perform_estimation_of_a_value (node, &avals,
removable_params_cost, 0, val);
@@ -5764,6 +6057,7 @@ push_agg_values_for_index_from_edge (struct cgraph_edge
*cs, int index,
iav.index = index;
iav.by_ref = jfunc->agg.by_ref;
iav.killed = false;
+ iav.unknown_offset = false;
gcc_assert (first
|| iav.unit_offset > prev_unit_offset);
diff --git a/gcc/ipa-fnsummary.cc b/gcc/ipa-fnsummary.cc
index f06d0999eaa..f00697ad15e 100644
--- a/gcc/ipa-fnsummary.cc
+++ b/gcc/ipa-fnsummary.cc
@@ -665,20 +665,73 @@ fold_string_char_compare (tree str, enum tree_code code,
tree val,
the offset, or NULL_TREE otherwise. */
static tree
-evaluate_unknown_offset_condition (struct condition *c,
+evaluate_unknown_offset_condition (struct cgraph_node *node,
+ struct condition *c,
ipa_auto_call_arg_values *avals)
{
- gcc_checking_assert (c->agg_contents && c->unknown_offset);
+ gcc_checking_assert (c->agg_contents);
+
+ if (!opt_for_fn (node->decl, flag_delete_null_pointer_checks)
+ || targetm.addr_space.zero_address_valid (ADDR_SPACE_GENERIC))
+ return NULL_TREE;
/* TODO: Support conversions in c->param_ops. */
if (!vec_safe_is_empty (c->param_ops))
return NULL_TREE;
- if (!c->by_ref || !c->val || TREE_CODE (c->val) != INTEGER_CST)
+ if (!c->val || TREE_CODE (c->val) != INTEGER_CST)
return NULL_TREE;
- tree sval = avals->safe_sval_at (c->operand_num);
- if (!sval || TREE_CODE (sval) != ADDR_EXPR)
+ if (!c->type
+ || TREE_CODE (c->type) != INTEGER_TYPE
+ || TYPE_PRECISION (c->type) != BITS_PER_UNIT)
+ return NULL_TREE;
+
+ switch (c->code)
+ {
+ case EQ_EXPR:
+ case NE_EXPR:
+ case LT_EXPR:
+ case LE_EXPR:
+ case GT_EXPR:
+ case GE_EXPR:
+ break;
+ default:
+ return NULL_TREE;
+ }
+
+ tree sval = NULL_TREE;
+ if (c->unknown_offset)
+ {
+ if (c->offset == -1)
+ {
+ if (!c->by_ref)
+ return NULL_TREE;
+ sval = avals->safe_sval_at (c->operand_num);
+ if (!sval || sval == error_mark_node)
+ sval = avals->safe_base_sval_at (c->operand_num);
+ }
+ else
+ {
+ if ((c->offset % BITS_PER_UNIT) != 0)
+ return NULL_TREE;
+ ipa_argagg_value_list avs (avals);
+ sval = avs.get_base_value (c->operand_num, c->offset / BITS_PER_UNIT,
+ c->by_ref, c->maybe_modified);
+ }
+ }
+ else
+ {
+ if (!c->by_ref || c->offset < 0)
+ return NULL_TREE;
+ sval = avals->safe_base_sval_at (c->operand_num);
+ }
+
+ if (!sval || sval == error_mark_node)
+ return NULL_TREE;
+
+ STRIP_NOPS (sval);
+ if (TREE_CODE (sval) != ADDR_EXPR)
return NULL_TREE;
poly_int64 byte_off;
@@ -731,20 +784,7 @@ evaluate_conditions_for_known_args (struct cgraph_node
*node,
&& (avals->safe_sval_at(c->operand_num) == error_mark_node))
continue;
- if (c->unknown_offset)
- {
- res = evaluate_unknown_offset_condition (c, avals);
- if (res && integer_zerop (res))
- continue;
- if (res && integer_onep (res))
- {
- clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
- nonspec_clause
- |= 1 << (i + ipa_predicate::first_dynamic_condition);
- continue;
- }
- }
- else
+ if (!c->unknown_offset)
{
if (tree sval = avals->safe_sval_at (c->operand_num))
val = ipa_find_agg_cst_from_init (sval, c->offset, c->by_ref,
@@ -760,6 +800,19 @@ evaluate_conditions_for_known_args (struct cgraph_node
*node,
c->by_ref, c->maybe_modified);
}
}
+ if (c->unknown_offset || (!val && c->by_ref))
+ {
+ res = evaluate_unknown_offset_condition (node, c, avals);
+ if (res && integer_zerop (res))
+ continue;
+ if (res && integer_onep (res))
+ {
+ clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
+ nonspec_clause
+ |= 1 << (i + ipa_predicate::first_dynamic_condition);
+ continue;
+ }
+ }
}
else
{
@@ -1017,11 +1070,25 @@ evaluate_properties_for_edge (struct cgraph_edge *e,
bool inline_p,
avals->m_known_vals.safe_grow_cleared (count, true);
avals->m_known_vals[i] = cst;
}
- else if (inline_p && !es->param[i].change_prob)
+ else
{
- if (!avals->m_known_vals.length ())
- avals->m_known_vals.safe_grow_cleared (count, true);
- avals->m_known_vals[i] = error_mark_node;
+ if (inline_p && !es->param[i].change_prob)
+ {
+ if (!avals->m_known_vals.length ())
+ avals->m_known_vals.safe_grow_cleared (count, true);
+ avals->m_known_vals[i] = error_mark_node;
+ }
+ if (tree base_cst
+ = ipa_base_value_from_jfunc (caller_parms_info, jf,
+ type))
+ {
+ gcc_checking_assert (TREE_CODE (base_cst)
+ != TREE_BINFO);
+ if (!avals->m_known_base_vals.length ())
+ avals->m_known_base_vals.safe_grow_cleared (count,
+ true);
+ avals->m_known_base_vals[i] = base_cst;
+ }
}
/* If we failed to get simple constant, try value range. */
@@ -1050,7 +1117,7 @@ evaluate_properties_for_edge (struct cgraph_edge *e, bool
inline_p,
if (fre_will_run_p (caller))
ipa_push_agg_values_from_jfunc (caller_parms_info,
caller, &jf->agg, i,
- &avals->m_known_aggs);
+ &avals->m_known_aggs, true);
}
/* For calls used in polymorphic calls we further determine
@@ -1675,7 +1742,7 @@ unmodified_parm_or_parm_agg_item (struct
ipa_func_body_info *fbi,
{
aggpos->maybe_modified = !guaranteed_unmodified;
gcc_checking_assert (!aggpos->maybe_modified || aggpos->by_ref);
- gcc_checking_assert (!aggpos->unknown_offset || aggpos->offset == -1);
+ gcc_checking_assert (!aggpos->unknown_offset || aggpos->offset >= -1);
return true;
}
return false;
@@ -4441,6 +4508,7 @@ ipa_cached_call_context::duplicate_from (const
ipa_call_context &ctx)
}
}
+ m_avals.m_known_base_vals = vNULL;
m_avals.m_known_value_ranges = vNULL;
}
@@ -4455,6 +4523,7 @@ ipa_cached_call_context::release ()
return;
m_avals.m_known_aggs.release ();
m_avals.m_known_vals.release ();
+ m_avals.m_known_base_vals.release ();
m_avals.m_known_contexts.release ();
m_inline_param_summary.release ();
}
@@ -4602,6 +4671,8 @@ ipa_call_context::equal_to (const ipa_call_context &ctx)
!= ctx.m_avals.m_known_aggs[j].unit_offset)
|| (m_avals.m_known_aggs[i].by_ref
!= ctx.m_avals.m_known_aggs[j].by_ref)
+ || (m_avals.m_known_aggs[i].unknown_offset
+ != ctx.m_avals.m_known_aggs[j].unknown_offset)
|| !operand_equal_p (m_avals.m_known_aggs[i].value,
ctx.m_avals.m_known_aggs[j].value))
return false;
@@ -4887,9 +4958,12 @@ remap_edge_params (struct cgraph_edge *inlined_edge,
es->param[i].points_to_possible_sra_candidate = true;
}
if (!es->param[i].points_to_local_or_readonly_memory
- && jfunc->type == IPA_JF_CONST
- && points_to_local_or_readonly_memory_p
- (ipa_get_jf_constant (jfunc)))
+ && ((jfunc->type == IPA_JF_CONST
+ && points_to_local_or_readonly_memory_p
+ (ipa_get_jf_constant (jfunc)))
+ || (jfunc->type == IPA_JF_CONST_UNKNOWN_OFFSET
+ && points_to_local_or_readonly_memory_p
+ (ipa_get_jf_constant_unknown_offset (jfunc)))))
es->param[i].points_to_local_or_readonly_memory = true;
}
}
@@ -4913,7 +4987,8 @@ remap_edge_summaries (struct cgraph_edge *inlined_edge,
const vec<HOST_WIDE_INT> &offset_map,
const vec<bool> &agg_preserved_map,
clause_t possible_truths,
- ipa_predicate *toplev_predicate)
+ ipa_predicate *toplev_predicate,
+ class ipa_edge_args *args)
{
struct cgraph_edge *e, *next;
for (e = node->callees; e; e = next)
@@ -4933,7 +5008,7 @@ remap_edge_summaries (struct cgraph_edge *inlined_edge,
callee_info, operand_map,
offset_map, agg_preserved_map,
possible_truths,
- *toplev_predicate);
+ *toplev_predicate, args);
edge_set_predicate (e, &p);
}
else
@@ -4943,7 +5018,7 @@ remap_edge_summaries (struct cgraph_edge *inlined_edge,
remap_edge_summaries (inlined_edge, e->callee, info,
params_summary, callee_info,
operand_map, offset_map, agg_preserved_map,
- possible_truths, toplev_predicate);
+ possible_truths, toplev_predicate, args);
}
for (e = node->indirect_calls; e; e = next)
{
@@ -4958,7 +5033,7 @@ remap_edge_summaries (struct cgraph_edge *inlined_edge,
(info, params_summary,
callee_info, operand_map, offset_map,
agg_preserved_map,
- possible_truths, *toplev_predicate);
+ possible_truths, *toplev_predicate, args);
edge_set_predicate (e, &p);
}
else
@@ -4977,7 +5052,8 @@ remap_freqcounting_predicate (class ipa_fn_summary *info,
const vec<HOST_WIDE_INT> &offset_map,
const vec<bool> &agg_preserved_map,
clause_t possible_truths,
- ipa_predicate *toplev_predicate)
+ ipa_predicate *toplev_predicate,
+ class ipa_edge_args *args)
{
ipa_freqcounting_predicate *fcp;
@@ -4988,7 +5064,7 @@ remap_freqcounting_predicate (class ipa_fn_summary *info,
callee_info, operand_map,
offset_map, agg_preserved_map,
possible_truths,
- *toplev_predicate);
+ *toplev_predicate, args);
if (p != false && p != true)
*fcp->predicate &= p;
}
@@ -5027,9 +5103,10 @@ ipa_merge_fn_summary_after_inlining (struct cgraph_edge
*edge)
ipa_auto_call_arg_values avals;
evaluate_properties_for_edge (edge, true, &clause, NULL, &avals, false);
}
+ ipa_edge_args *args
+ = ipa_edge_args_sum ? ipa_edge_args_sum->get (edge) : NULL;
if (ipa_node_params_sum && callee_info->conds)
{
- ipa_edge_args *args = ipa_edge_args_sum->get (edge);
int count = args ? ipa_get_cs_argument_count (args) : 0;
int i;
@@ -5082,13 +5159,13 @@ ipa_merge_fn_summary_after_inlining (struct cgraph_edge
*edge)
(info, params_summary,
callee_info, operand_map,
offset_map, agg_preserved_map, clause,
- toplev_predicate);
+ toplev_predicate, args);
ipa_predicate nonconstp;
nonconstp = e->nonconst_predicate.remap_after_inlining
(info, params_summary,
callee_info, operand_map,
offset_map, agg_preserved_map, clause,
- toplev_predicate);
+ toplev_predicate, args);
if (p != false && nonconstp != false)
{
sreal add_time = ((sreal)e->time * freq);
@@ -5108,15 +5185,15 @@ 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, agg_preserved_map, clause,
- &toplev_predicate);
+ &toplev_predicate, args);
remap_freqcounting_predicate (info, params_summary, callee_info,
info->loop_iterations, operand_map,
offset_map, agg_preserved_map, clause,
- &toplev_predicate);
+ &toplev_predicate, args);
remap_freqcounting_predicate (info, params_summary, callee_info,
info->loop_strides, operand_map,
offset_map, agg_preserved_map, clause,
- &toplev_predicate);
+ &toplev_predicate, args);
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;
@@ -5400,10 +5477,8 @@ inline_read_section (struct lto_file_decl_data
*file_data, const char *data,
c.by_ref = bp_unpack_value (&bp, 1);
c.maybe_modified = bp_unpack_value (&bp, 1);
c.unknown_offset = bp_unpack_value (&bp, 1);
- if (c.unknown_offset)
- c.offset = -1;
- else if (c.agg_contents)
- c.offset = streamer_read_uhwi (&ib);
+ if (c.agg_contents)
+ c.offset = streamer_read_hwi (&ib);
else
c.offset = 0;
count3 = streamer_read_uhwi (&ib);
@@ -5643,8 +5718,8 @@ ipa_fn_summary_write (void)
bp_pack_value (&bp, c->maybe_modified, 1);
bp_pack_value (&bp, c->unknown_offset, 1);
streamer_write_bitpack (&bp);
- if (c->agg_contents && !c->unknown_offset)
- streamer_write_uhwi (ob, c->offset);
+ if (c->agg_contents)
+ streamer_write_hwi (ob, c->offset);
streamer_write_uhwi (ob, vec_safe_length (c->param_ops));
for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
{
diff --git a/gcc/ipa-predicate.cc b/gcc/ipa-predicate.cc
index 452c1e5ecc1..c9e0a0891e1 100644
--- a/gcc/ipa-predicate.cc
+++ b/gcc/ipa-predicate.cc
@@ -151,7 +151,7 @@ ipa_predicate::add_clause (conditions conditions, clause_t
new_clause)
&& cc2->by_ref == cc1->by_ref
&& (!cc1->agg_contents
|| (cc1->unknown_offset == cc2->unknown_offset
- && (cc1->unknown_offset || cc2->offset == cc1->offset)
+ && cc2->offset == cc1->offset
&& cc2->maybe_modified == cc1->maybe_modified))
&& types_compatible_p (cc2->type, cc1->type)
&& cc1->code == invert_tree_comparison (cc2->code,
@@ -338,9 +338,18 @@ dump_condition (FILE *f, conditions conditions, int cond)
if (c->agg_contents)
{
if (c->unknown_offset)
- fprintf (f, "[%s%sunknown_offset]",
- c->by_ref ? "ref " : "",
- c->maybe_modified ? "maybe_modified " : "");
+ {
+ if (c->offset >= 0)
+ fprintf (f, "[%s%soffset: " HOST_WIDE_INT_PRINT_DEC
+ " unknown_offset]",
+ c->by_ref ? "ref " : "",
+ c->maybe_modified ? "maybe_modified " : "",
+ c->offset);
+ else
+ fprintf (f, "[%s%sunknown_offset]",
+ c->by_ref ? "ref " : "",
+ c->maybe_modified ? "maybe_modified " : "");
+ }
else
fprintf (f, "[%s%soffset: " HOST_WIDE_INT_PRINT_DEC "]",
c->by_ref ? "ref " : "",
@@ -534,7 +543,8 @@ ipa_predicate::remap_after_inlining (class ipa_fn_summary
*info,
const vec<HOST_WIDE_INT> &offset_map,
const vec<bool> &agg_preserved_map,
clause_t possible_truths,
- const ipa_predicate &toplev_predicate)
+ const ipa_predicate &toplev_predicate,
+ class ipa_edge_args *args)
{
int i;
ipa_predicate out = true;
@@ -563,9 +573,134 @@ ipa_predicate::remap_after_inlining (class ipa_fn_summary
*info,
int index = cond - ipa_predicate::first_dynamic_condition;
c = &(*callee_info->conds)[index];
+ bool remapped = false;
+ if (c->agg_contents
+ && args
+ && c->operand_num >= 0
+ && c->operand_num < ipa_get_cs_argument_count (args))
+ {
+ ipa_jump_func *jf
+ = ipa_get_ith_jump_func (args, c->operand_num);
+ if (((c->unknown_offset && c->offset < 0)
+ || (!c->unknown_offset
+ && c->offset >= 0
+ && c->type
+ && TREE_CODE (c->type) == INTEGER_TYPE
+ && TYPE_PRECISION (c->type) == BITS_PER_UNIT))
+ && c->by_ref
+ && (!operand_map.exists ()
+ || (int) operand_map.length () <= c->operand_num
+ || operand_map[c->operand_num] == -1)
+ && jf->type == IPA_JF_PASS_THROUGH
+ && (ipa_get_jf_pass_through_operation (jf)
+ == POINTER_PLUS_EXPR))
+ {
+ int fid = ipa_get_jf_pass_through_formal_id (jf);
+ if (fid >= 0
+ && (!params_summary
+ || fid < ipa_get_param_count (params_summary)))
+ {
+ struct agg_position_info ap;
+ ap.offset = -1;
+ ap.agg_contents = true;
+ ap.by_ref = true;
+ ap.maybe_modified = true;
+ ap.unknown_offset = true;
+ cond_predicate
+ = add_condition (info, params_summary, fid,
+ c->type, &ap, c->code, c->val,
+ c->param_ops);
+ remapped = true;
+ }
+ }
+ else if (c->unknown_offset
+ && c->offset >= 0
+ && !c->maybe_modified
+ && (!operand_map.exists ()
+ || ((int) operand_map.length ()
+ <= c->operand_num)
+ || operand_map[c->operand_num] == -1
+ || (c->by_ref
+ && !agg_preserved_map[c->operand_num]))
+ && jf->agg.items
+ && jf->agg.by_ref == c->by_ref)
+ {
+ for (const ipa_agg_jf_item &item : *jf->agg.items)
+ {
+ if (item.offset != c->offset
+ || !item.type
+ || !POINTER_TYPE_P (item.type))
+ continue;
+ if (item.jftype == IPA_JF_PASS_THROUGH
+ && (item.value.pass_through.operation
+ == NOP_EXPR
+ || (item.value.pass_through.operation
+ == POINTER_PLUS_EXPR)))
+ {
+ int fid = item.value.pass_through.formal_id;
+ if (fid >= 0
+ && (!params_summary
+ || fid < ipa_get_param_count
+ (params_summary)))
+ {
+ struct agg_position_info ap;
+ ap.offset = -1;
+ ap.agg_contents = true;
+ ap.by_ref = true;
+ bool mod
+ = (item.value.pass_through.operation
+ != NOP_EXPR
+ || !item.value.pass_through
+ .agg_preserved);
+ ap.maybe_modified = mod;
+ ap.unknown_offset = true;
+ cond_predicate
+ = add_condition (info, params_summary,
+ fid, c->type, &ap,
+ c->code, c->val,
+ c->param_ops);
+ remapped = true;
+ }
+ break;
+ }
+ if (item.jftype == IPA_JF_LOAD_AGG
+ && (item.value.load_agg.pass_through.operation
+ == NOP_EXPR
+ || (item.value.load_agg.pass_through
+ .operation
+ == POINTER_PLUS_EXPR))
+ && item.value.load_agg.offset >= 0)
+ {
+ int fid
+ = item.value.load_agg.pass_through.formal_id;
+ if (fid >= 0
+ && (!params_summary
+ || fid < ipa_get_param_count
+ (params_summary)))
+ {
+ struct agg_position_info ap;
+ ap.offset = item.value.load_agg.offset;
+ ap.agg_contents = true;
+ ap.by_ref = item.value.load_agg.by_ref;
+ ap.maybe_modified = false;
+ ap.unknown_offset = true;
+ cond_predicate
+ = add_condition (info, params_summary,
+ fid, c->type, &ap,
+ c->code, c->val,
+ c->param_ops);
+ remapped = true;
+ }
+ break;
+ }
+ }
+ }
+ }
+ if (remapped)
+ ;
/* See if we can remap condition operand to caller's operand.
Otherwise give up. */
- if (!operand_map.exists ()
+ else if (!operand_map.exists ()
|| (int) operand_map.length () <= c->operand_num
|| operand_map[c->operand_num] == -1
/* TODO: For non-aggregate conditions, adding an offset is
@@ -574,7 +709,7 @@ ipa_predicate::remap_after_inlining (class ipa_fn_summary
*info,
|| ((!c->agg_contents || !c->by_ref)
&& offset_map[c->operand_num] > 0)
|| (c->agg_contents
- && !c->unknown_offset
+ && c->offset >= 0
&& !wi::fits_shwi_p (offset_int (c->offset)
+ offset_map[c->operand_num])))
cond_predicate = true;
@@ -585,7 +720,7 @@ ipa_predicate::remap_after_inlining (class ipa_fn_summary
*info,
gcc_checking_assert (offset_delta >= 0);
gcc_assert (!c->agg_contents
|| c->by_ref || offset_delta == 0);
- if (c->unknown_offset)
+ if (c->offset < 0)
ap.offset = -1;
else
ap.offset = c->agg_contents ? c->offset + offset_delta :
0;
@@ -694,7 +829,7 @@ add_condition (class ipa_fn_summary *summary,
gcc_checking_assert (operand_num >= 0);
gcc_checking_assert (!maybe_modified || (agg_contents && by_ref));
- gcc_checking_assert (!unknown_offset || (agg_contents && offset == -1));
+ gcc_checking_assert (!unknown_offset || (agg_contents && offset >= -1));
gcc_checking_assert (!agg_contents || unknown_offset || offset >= 0);
for (i = 0; vec_safe_iterate (summary->conds, i, &c); i++)
{
diff --git a/gcc/ipa-predicate.h b/gcc/ipa-predicate.h
index 26e1b8e73c1..db0cb012288 100644
--- a/gcc/ipa-predicate.h
+++ b/gcc/ipa-predicate.h
@@ -48,7 +48,8 @@ typedef vec<expr_eval_op, va_gc> *expr_eval_ops;
struct GTY(()) condition
{
/* If agg_contents is set, this is the offset from which the used data was
- loaded, or -1 if unknown_offset is set. */
+ loaded, or -1 if unknown_offset describes a direct pointer dereference
+ rather than a pointer slot inside an aggregate parameter. */
HOST_WIDE_INT offset;
/* Type of the access reading the data (or the PARM_DECL SSA_NAME). */
tree type;
@@ -258,7 +259,8 @@ public:
const vec<int> &,
const vec<HOST_WIDE_INT> &,
const vec<bool> &,
- clause_t, const ipa_predicate &);
+ clause_t, const ipa_predicate &,
+ class ipa_edge_args * = NULL);
void stream_in (lto_input_block *);
void stream_out (output_block *);
diff --git a/gcc/ipa-prop.cc b/gcc/ipa-prop.cc
index 5489e58a5ca..5f227567c44 100644
--- a/gcc/ipa-prop.cc
+++ b/gcc/ipa-prop.cc
@@ -497,6 +497,12 @@ ipa_dump_jump_function (FILE *f, ipa_jump_func *jump_func,
ipa_print_constant_value (f, jump_func->value.constant.value);
fprintf (f, "\n");
}
+ else if (type == IPA_JF_CONST_UNKNOWN_OFFSET)
+ {
+ fprintf (f, "CONST UNKNOWN OFFSET: ");
+ ipa_print_constant_value (f, jump_func->value.constant_unknown_offset);
+ fprintf (f, "\n");
+ }
else if (type == IPA_JF_PASS_THROUGH)
{
fprintf (f, "PASS THROUGH: ");
@@ -508,6 +514,9 @@ ipa_dump_jump_function (FILE *f, ipa_jump_func *jump_func,
fprintf (f, " ");
if (jump_func->value.pass_through.operand)
print_generic_expr (f, jump_func->value.pass_through.operand);
+ else if (TREE_CODE_CLASS (jump_func->value.pass_through.operation)
+ != tcc_unary)
+ fprintf (f, "unknown_offset");
fprintf (f, " (in type ");
print_generic_expr (f, jump_func->value.pass_through.op_type);
fprintf (f, ")");
@@ -568,6 +577,9 @@ ipa_dump_jump_function (FILE *f, ipa_jump_func *jump_func,
fprintf (f, " ");
if (item->value.pass_through.operand)
print_generic_expr (f, item->value.pass_through.operand);
+ else if (TREE_CODE_CLASS (item->value.pass_through.operation)
+ != tcc_unary)
+ fprintf (f, "unknown_offset");
fprintf (f, " (in type ");
print_generic_expr (f, item->value.pass_through.op_type);
fprintf (f, ")");
@@ -578,6 +590,11 @@ ipa_dump_jump_function (FILE *f, ipa_jump_func *jump_func,
fprintf (f, "CONST: ");
ipa_print_constant_value (f, item->value.constant);
}
+ else if (item->jftype == IPA_JF_CONST_UNKNOWN_OFFSET)
+ {
+ fprintf (f, "CONST UNKNOWN OFFSET: ");
+ ipa_print_constant_value (f, item->value.constant_unknown_offset);
+ }
else if (item->jftype == IPA_JF_UNKNOWN)
fprintf (f, "UNKNOWN: " HOST_WIDE_INT_PRINT_DEC " bits",
tree_to_uhwi (TYPE_SIZE (item->type)));
@@ -751,6 +768,17 @@ ipa_set_jf_constant (struct ipa_jump_func *jfunc, tree
constant,
jfunc->value.constant.rdesc = NULL;
}
+/* Set JFUNC to be an unknown-offset constant jump function holding base
+ address CONSTANT. */
+
+static void
+ipa_set_jf_constant_unknown_offset (struct ipa_jump_func *jfunc, tree constant)
+{
+ jfunc->type = IPA_JF_CONST_UNKNOWN_OFFSET;
+ jfunc->value.constant_unknown_offset
+ = unshare_expr_without_location (constant);
+}
+
/* Set JFUNC to be a simple pass-through jump function. */
static void
ipa_set_jf_simple_pass_through (struct ipa_jump_func *jfunc, int formal_id,
@@ -787,7 +815,8 @@ ipa_set_jf_arith_pass_through (struct ipa_jump_func *jfunc,
int formal_id,
tree op_type)
{
jfunc->type = IPA_JF_PASS_THROUGH;
- jfunc->value.pass_through.operand = unshare_expr_without_location (operand);
+ jfunc->value.pass_through.operand
+ = operand ? unshare_expr_without_location (operand) : NULL_TREE;
jfunc->value.pass_through.op_type = op_type;
jfunc->value.pass_through.formal_id = formal_id;
jfunc->value.pass_through.operation = operation;
@@ -1419,17 +1448,29 @@ struct ipa_unknown_off_parm_data
vec<ipa_param_descriptor, va_gc> *descriptors;
int found_index;
tree parm_ptr;
+ bool is_agg;
+ HOST_WIDE_INT agg_offset;
+ bool by_ref;
+ bool *guaranteed_unmodified_p;
+ bool agg_guaranteed_unmodified;
+ bool allow_invariant_cst;
+ tree found_cst;
};
-/* Leaf callback for ipa_load_from_parm_agg_unknown_offset. Accepts default
- definitions of PARM_DECLs and unmodified parameter loads when all reached
- paths belong to the same parameter index. */
+/* Leaf callback for ipa_walk_ptr_adjustments. Accepts default definitions of
+ PARM_DECLs, unmodified parameter or parameter-aggregate slot loads, and
+ optionally interprocedural invariant base addresses when all reached paths
+ agree. */
bool
ipa_unknown_off_parm_leaf (gimple *stmt, tree expr, vec<tree> &, void *data)
{
ipa_unknown_off_parm_data *d = (ipa_unknown_off_parm_data *) data;
int index = -1;
+ bool is_agg = false;
+ HOST_WIDE_INT agg_off = -1;
+ bool by_ref = false;
+ bool agg_unmod = true;
tree ptr = NULL_TREE;
if (!stmt && TREE_CODE (expr) == SSA_NAME && SSA_NAME_IS_DEFAULT_DEF (expr))
@@ -1440,31 +1481,113 @@ ipa_unknown_off_parm_leaf (gimple *stmt, tree expr,
vec<tree> &, void *data)
index = ipa_get_param_decl_index_1 (d->descriptors, parm);
ptr = expr;
}
+ else if (d->allow_invariant_cst
+ && TREE_CODE (expr) == ADDR_EXPR
+ && is_gimple_ip_invariant (expr))
+ {
+ poly_int64 off;
+ tree base = get_addr_base_and_unit_offset (TREE_OPERAND (expr, 0), &off);
+ if (!base || (!CONSTANT_CLASS_P (base) && !DECL_P (base)))
+ return false;
+ tree cst = build_fold_addr_expr (base);
+ if (!is_gimple_ip_invariant (cst))
+ return false;
+ if (d->found_index != -1)
+ return false;
+ if (d->found_cst && !values_equal_for_ipcp_p (d->found_cst, cst))
+ return false;
+ d->found_cst = cst;
+ return true;
+ }
else if (stmt && is_gimple_assign (stmt) && TREE_CODE (expr) == SSA_NAME)
{
index = load_from_unmodified_param (d->fbi, d->descriptors, stmt);
+ if (index < 0 && d->fbi && gimple_assign_load_p (stmt))
+ {
+ tree rhs = gimple_assign_rhs1 (stmt);
+ for (tree t = rhs; handled_component_p (t); t = TREE_OPERAND (t, 0))
+ if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
+ return false;
+ if (ipa_load_from_parm_agg (d->fbi, d->descriptors, stmt, rhs,
+ &index, &agg_off, NULL, &by_ref,
+ d->guaranteed_unmodified_p
+ ? &agg_unmod : NULL))
+ is_agg = true;
+ else
+ return false;
+ }
ptr = expr;
}
else
return false;
- if (index < 0 || (d->found_index != -1 && d->found_index != index))
+ if (d->found_cst || index < 0)
+ return false;
+
+ if (d->found_index != -1
+ && (d->found_index != index
+ || d->is_agg != is_agg
+ || (is_agg
+ && (d->agg_offset != agg_off || d->by_ref != by_ref))))
return false;
d->found_index = index;
+ d->is_agg = is_agg;
+ if (is_agg)
+ {
+ d->agg_offset = agg_off;
+ d->by_ref = by_ref;
+ if (d->guaranteed_unmodified_p)
+ d->agg_guaranteed_unmodified &= agg_unmod;
+ }
d->parm_ptr = ptr;
return true;
}
} // anonymous namespace
+/* Walk pointer START_PTR through adjustments, copies, conversions and PHIs
+ to determine if all paths originate from a single parameter (or unmodified
+ parameter aggregate slot) or a single interprocedural invariant base
+ address. Fill OUT and return true if such an origin is found. */
+
+static bool
+analyze_unknown_offset_ptr_origin (struct ipa_func_body_info *fbi,
+ class ipa_node_params *info,
+ tree start_ptr,
+ ipa_unknown_off_parm_data *out)
+{
+ if (!start_ptr
+ || !POINTER_TYPE_P (TREE_TYPE (start_ptr))
+ || TREE_CODE (start_ptr) != SSA_NAME)
+ return false;
+
+ tree pointee_type = TREE_TYPE (TREE_TYPE (start_ptr));
+ bool null_invalid_p
+ = ((fbi && fbi->node
+ ? opt_for_fn (fbi->node->decl, flag_delete_null_pointer_checks)
+ : flag_delete_null_pointer_checks)
+ && !targetm.addr_space.zero_address_valid
+ (TYPE_ADDR_SPACE (pointee_type)));
+
+ *out = {fbi, info->descriptors, -1, NULL_TREE, false, -1, false, NULL, true,
+ true, NULL_TREE};
+ bool saw_phi = false;
+ if (!ipa_walk_ptr_adjustments (start_ptr, null_invalid_p,
+ ipa_unknown_off_parm_leaf, out, &saw_phi))
+ return false;
+
+ return out->found_index >= 0 || out->found_cst != NULL_TREE;
+}
+
/* Fallback for ipa_load_from_parm_agg when OP is a 1-byte integral (char)
load whose byte offset is unknown (for example due to a variable ARRAY_REF
index or pointer arithmetic from a parameter). Use get_base_address to
strip component references down to a MEM_REF and walk backwards through
pointer adjustments, copies, conversions and PHI nodes via
ipa_walk_ptr_adjustments, verifying that every reachable path originates
from
- the same function parameter. If so, record *OFFSET_P as -1 and set
+ the same function parameter (or unmodified parameter-aggregate slot). If
so,
+ record *OFFSET_P as -1 (or the slot offset for aggregate loads) and set
*UNKNOWN_OFFSET_P to true. */
static bool
@@ -1506,7 +1629,9 @@ ipa_load_from_parm_agg_unknown_offset (struct
ipa_func_body_info *fbi,
integer_zerop operands cannot reach a valid dereference when
NULL_INVALID_P holds and can be skipped (FOUND_INDEX < 0 still ensures
at least one parameter path exists). */
- ipa_unknown_off_parm_data d = {fbi, descriptors, -1, NULL_TREE};
+ ipa_unknown_off_parm_data d
+ = {fbi, descriptors, -1, NULL_TREE, false, -1, false,
+ guaranteed_unmodified, true, false, NULL_TREE};
bool saw_phi = false;
if (!ipa_walk_ptr_adjustments (start_ptr, null_invalid_p,
ipa_unknown_off_parm_leaf, &d, &saw_phi))
@@ -1515,6 +1640,22 @@ ipa_load_from_parm_agg_unknown_offset (struct
ipa_func_body_info *fbi,
if (d.found_index < 0)
return false;
+ if (d.is_agg)
+ {
+ if (!d.agg_guaranteed_unmodified && !guaranteed_unmodified)
+ return false;
+
+ *index_p = d.found_index;
+ *offset_p = d.agg_offset;
+ *by_ref_p = d.by_ref;
+ if (size_p)
+ *size_p = BITS_PER_UNIT;
+ if (guaranteed_unmodified)
+ *guaranteed_unmodified = d.agg_guaranteed_unmodified;
+ *unknown_offset_p = true;
+ return true;
+ }
+
/* When a PHI was crossed (for instance a pointer induction variable in a
loop), a store to ptr + 1 in a previous iteration aliases *ptr in the
next iteration even though both share the same SSA_NAME base, so check
@@ -2126,6 +2267,10 @@ struct ipa_known_agg_contents_list
/* Known constant value or jump function data describing contents. */
struct ipa_load_agg_data value;
+ /* True if value.pass_through.operand represents an invariant base address
+ with an unknown byte offset (IPA_JF_CONST_UNKNOWN_OFFSET). */
+ bool constant_unknown_offset;
+
/* Pointer to the next structure in the list. */
struct ipa_known_agg_contents_list *next;
};
@@ -2199,6 +2344,14 @@ build_agg_jump_func_from_list (struct
ipa_known_agg_contents_list *list,
item.value.pass_through.operand
= unshare_expr_without_location (operand);
}
+ else if (list->constant_unknown_offset)
+ {
+ /* Content value is an invariant base address with unknown offset. */
+ gcc_checking_assert (operand);
+ item.jftype = IPA_JF_CONST_UNKNOWN_OFFSET;
+ item.value.constant_unknown_offset
+ = unshare_expr_without_location (operand);
+ }
else if (operand)
{
/* Content value is known constant. */
@@ -2246,10 +2399,14 @@ build_agg_jump_func_from_list (struct
ipa_known_agg_contents_list *list,
static void
analyze_agg_content_value (struct ipa_func_body_info *fbi,
struct ipa_load_agg_data *agg_value,
- gimple *stmt)
+ gimple *stmt,
+ bool *constant_unknown_offset_p = NULL)
{
+ gimple *orig_stmt = stmt;
tree lhs = gimple_assign_lhs (stmt);
tree rhs1 = gimple_assign_rhs1 (stmt);
+ tree orig_lhs = lhs;
+ tree orig_rhs1 = rhs1;
enum tree_code code;
int index = -1;
@@ -2258,6 +2415,8 @@ analyze_agg_content_value (struct ipa_func_body_info *fbi,
agg_value->pass_through.operation = NOP_EXPR;
agg_value->pass_through.formal_id = -1;
agg_value->offset = -1;
+ if (constant_unknown_offset_p)
+ *constant_unknown_offset_p = false;
if (AGGREGATE_TYPE_P (TREE_TYPE (lhs)) /* TODO: Support aggregate type. */
|| TREE_THIS_VOLATILE (lhs)
@@ -2297,7 +2456,7 @@ analyze_agg_content_value (struct ipa_func_body_info *fbi,
if (gimple_phi_num_args (phi) != 2
|| lto_variably_modified_type_p (TREE_TYPE (lhs)))
- return;
+ goto try_unknown_offset;
tree arg0 = gimple_phi_arg_def (phi, 0);
tree arg1 = gimple_phi_arg_def (phi, 1);
tree operand;
@@ -2313,11 +2472,11 @@ analyze_agg_content_value (struct ipa_func_body_info
*fbi,
rhs1 = arg1;
}
else
- return;
+ goto try_unknown_offset;
rhs1 = get_ssa_def_if_simple_copy (rhs1, &stmt);
if (!is_gimple_assign (stmt))
- return;
+ goto try_unknown_offset;
code = ASSERT_EXPR;
agg_value->pass_through.operand = operand;
@@ -2348,7 +2507,7 @@ analyze_agg_content_value (struct ipa_func_body_info *fbi,
distinguish unary and binary. */
if (TREE_CODE_CLASS (code) != tcc_unary || CONVERT_EXPR_CODE_P (code)
|| lto_variably_modified_type_p (TREE_TYPE (lhs)))
- return;
+ goto try_unknown_offset;
rhs1 = get_ssa_def_if_simple_copy (rhs1, &stmt);
agg_value->pass_through.op_type = TREE_TYPE (lhs);
@@ -2361,7 +2520,7 @@ analyze_agg_content_value (struct ipa_func_body_info *fbi,
tree rhs2 = gimple_assign_rhs2 (stmt);
if (lto_variably_modified_type_p (TREE_TYPE (lhs)))
- return;
+ goto try_unknown_offset;
rhs1 = get_ssa_def_if_simple_copy (rhs1, &rhs1_stmt);
rhs2 = get_ssa_def_if_simple_copy (rhs2, &rhs2_stmt);
@@ -2377,7 +2536,7 @@ analyze_agg_content_value (struct ipa_func_body_info *fbi,
if (TREE_CODE_CLASS (code) == tcc_comparison)
code = swap_tree_comparison (code);
else if (!commutative_tree_code (code))
- return;
+ goto try_unknown_offset;
agg_value->pass_through.operand = rhs1;
agg_value->pass_through.op_type = TREE_TYPE (lhs);
@@ -2385,21 +2544,21 @@ analyze_agg_content_value (struct ipa_func_body_info
*fbi,
rhs1 = rhs2;
}
else
- return;
+ goto try_unknown_offset;
if (TREE_CODE_CLASS (code) != tcc_comparison
&& !useless_type_conversion_p (TREE_TYPE (lhs),
TREE_TYPE (rhs1)))
- return;
+ goto try_unknown_offset;
}
break;
default:
- return;
+ goto try_unknown_offset;
}
}
else
- return;
+ goto try_unknown_offset;
if (TREE_CODE (rhs1) != SSA_NAME)
index = load_from_unmodified_param_or_agg (fbi, fbi->info, stmt,
@@ -2414,9 +2573,44 @@ analyze_agg_content_value (struct ipa_func_body_info
*fbi,
agg_value->type = TREE_TYPE (rhs1);
agg_value->pass_through.formal_id = index;
agg_value->pass_through.operation = code;
+ return;
+ }
+
+try_unknown_offset:
+ memset (agg_value, 0, sizeof (*agg_value));
+ agg_value->pass_through.operation = NOP_EXPR;
+ agg_value->pass_through.formal_id = -1;
+ agg_value->offset = -1;
+
+ if (constant_unknown_offset_p
+ && gimple_assign_single_p (orig_stmt)
+ && POINTER_TYPE_P (TREE_TYPE (orig_lhs))
+ && !lto_variably_modified_type_p (TREE_TYPE (orig_lhs))
+ && TREE_CODE (orig_rhs1) == SSA_NAME)
+ {
+ ipa_unknown_off_parm_data d;
+ if (analyze_unknown_offset_ptr_origin (fbi, fbi->info, orig_rhs1, &d))
+ {
+ if (d.found_index >= 0)
+ {
+ agg_value->pass_through.formal_id = d.found_index;
+ agg_value->pass_through.operation = POINTER_PLUS_EXPR;
+ agg_value->pass_through.operand = NULL_TREE;
+ agg_value->pass_through.op_type = TREE_TYPE (orig_lhs);
+ if (d.is_agg)
+ {
+ agg_value->offset = d.agg_offset;
+ agg_value->by_ref = d.by_ref;
+ agg_value->type = TREE_TYPE (d.parm_ptr);
+ }
+ }
+ else if (d.found_cst)
+ {
+ agg_value->pass_through.operand = d.found_cst;
+ *constant_unknown_offset_p = true;
+ }
+ }
}
- else
- agg_value->pass_through.operand = NULL_TREE;
}
/* If STMT is a memory store to the object whose address is BASE, extract
@@ -2455,9 +2649,11 @@ extract_mem_content (struct ipa_func_body_info *fbi,
content->offset = lhs_offset;
content->size = lhs_size;
content->type = TREE_TYPE (lhs);
+ content->constant_unknown_offset = false;
content->next = NULL;
- analyze_agg_content_value (fbi, &content->value, stmt);
+ analyze_agg_content_value (fbi, &content->value, stmt,
+ &content->constant_unknown_offset);
return true;
}
@@ -2986,6 +3182,25 @@ ipa_compute_jump_functions_for_edge (struct
ipa_func_body_info *fbi,
compute_complex_ancestor_jump_func (fbi, info, jfunc,
call,
as_a <gphi *> (stmt));
+
+ if (jfunc->type == IPA_JF_UNKNOWN
+ && POINTER_TYPE_P (TREE_TYPE (arg))
+ && (!param_type || POINTER_TYPE_P (param_type)))
+ {
+ ipa_unknown_off_parm_data d;
+ if (analyze_unknown_offset_ptr_origin (fbi, info, arg, &d))
+ {
+ tree op_type = param_type ? param_type : TREE_TYPE (arg);
+ if (d.found_index >= 0 && !d.is_agg
+ && !lto_variably_modified_type_p (op_type))
+ ipa_set_jf_arith_pass_through (jfunc, d.found_index,
+ NULL_TREE,
+ POINTER_PLUS_EXPR,
+ op_type);
+ else if (d.found_cst)
+ ipa_set_jf_constant_unknown_offset (jfunc, d.found_cst);
+ }
+ }
}
}
@@ -4069,6 +4284,20 @@ update_jump_functions_after_inlining (struct cgraph_edge
*cs,
item->value.constant = src->value.constant.value;
continue;
}
+ if (item->value.pass_through.operation
+ == POINTER_PLUS_EXPR
+ && !item->value.pass_through.operand)
+ {
+ tree cst = ipa_base_value_from_jfunc
+ (new_inline_root_info, src, item->type);
+ if (cst)
+ {
+ item->jftype = IPA_JF_CONST_UNKNOWN_OFFSET;
+ item->value.constant_unknown_offset
+ = unshare_expr_without_location (cst);
+ continue;
+ }
+ }
tree cst = ipa_get_jf_arith_result
(item->value.pass_through.operation,
src->value.constant.value,
@@ -4094,6 +4323,37 @@ update_jump_functions_after_inlining (struct cgraph_edge
*cs,
&& useless_type_conversion_p
(item->value.load_agg.type, TREE_TYPE (val)))
{
+ if (item->value.pass_through.operation
+ == POINTER_PLUS_EXPR
+ && !item->value.pass_through.operand)
+ {
+ tree stripped = val;
+ STRIP_NOPS (stripped);
+ if (TREE_CODE (stripped) == ADDR_EXPR)
+ {
+ poly_int64 off;
+ tree base = get_addr_base_and_unit_offset
+ (TREE_OPERAND (stripped, 0), &off);
+ if (base
+ && (CONSTANT_CLASS_P (base)
+ || DECL_P (base)))
+ {
+ tree base_addr
+ = build_fold_addr_expr (base);
+ tree cst = ipacp_value_safe_for_type
+ (item->type, base_addr);
+ if (cst)
+ {
+ item->jftype
+ = IPA_JF_CONST_UNKNOWN_OFFSET;
+ item->value.constant_unknown_offset
+ = unshare_expr_without_location
+ (cst);
+ continue;
+ }
+ }
+ }
+ }
tree cst = ipa_get_jf_arith_result
(item->value.pass_through.operation,
val,
@@ -4110,8 +4370,35 @@ update_jump_functions_after_inlining (struct cgraph_edge
*cs,
}
}
}
+ else if (src->type == IPA_JF_CONST_UNKNOWN_OFFSET)
+ {
+ if (item->jftype == IPA_JF_PASS_THROUGH
+ && (item->value.pass_through.operation == NOP_EXPR
+ || item->value.pass_through.operation
+ == POINTER_PLUS_EXPR))
+ {
+ tree cst = ipa_base_value_from_jfunc
+ (new_inline_root_info, src, item->type);
+ if (cst)
+ {
+ item->jftype = IPA_JF_CONST_UNKNOWN_OFFSET;
+ item->value.constant_unknown_offset
+ = unshare_expr_without_location (cst);
+ continue;
+ }
+ }
+ }
else if (src->type == IPA_JF_PASS_THROUGH
- && src->value.pass_through.operation == NOP_EXPR)
+ && (src->value.pass_through.operation == NOP_EXPR
+ || (item->jftype == IPA_JF_PASS_THROUGH
+ && item->value.pass_through.operation
+ == POINTER_PLUS_EXPR
+ && !item->value.pass_through.operand
+ && src->value.pass_through.operation
+ == POINTER_PLUS_EXPR)
+ || (item->jftype == IPA_JF_PASS_THROUGH
+ && item->value.pass_through.operation == NOP_EXPR
+ && ipa_jf_pass_through_unknown_offset_p (src))))
{
if (item->jftype == IPA_JF_PASS_THROUGH
|| !item->value.load_agg.by_ref
@@ -4119,14 +4406,29 @@ update_jump_functions_after_inlining (struct
cgraph_edge *cs,
|| jfunc_points_to_readonly_memory_p
(new_inline_root_info, old_inline_root_info,
src, dst_fid))
- item->value.pass_through.formal_id
- = src->value.pass_through.formal_id;
+ {
+ item->value.pass_through.formal_id
+ = src->value.pass_through.formal_id;
+ if (item->jftype == IPA_JF_PASS_THROUGH
+ && item->value.pass_through.operation == NOP_EXPR
+ && ipa_jf_pass_through_unknown_offset_p (src))
+ {
+ item->value.pass_through.operation
+ = POINTER_PLUS_EXPR;
+ item->value.pass_through.operand = NULL_TREE;
+ item->value.pass_through.op_type
+ = src->value.pass_through.op_type;
+ }
+ }
}
else if (src->type == IPA_JF_ANCESTOR)
{
if (item->jftype == IPA_JF_PASS_THROUGH)
{
- if (!src->value.ancestor.offset)
+ if (!src->value.ancestor.offset
+ || (item->value.pass_through.operation
+ == POINTER_PLUS_EXPR
+ && !item->value.pass_through.operand))
item->value.pass_through.formal_id
= src->value.ancestor.formal_id;
}
@@ -4237,6 +4539,24 @@ update_jump_functions_after_inlining (struct cgraph_edge
*cs,
dst->value.ancestor.agg_preserved &=
src->value.pass_through.agg_preserved;
}
+ else if (src->type == IPA_JF_CONST_UNKNOWN_OFFSET)
+ {
+ tree parm_type = ipa_get_callee_param_type (e, i);
+ tree cst = ipa_get_jf_constant_unknown_offset (src);
+ if (cst && parm_type)
+ cst = ipacp_value_safe_for_type (parm_type, cst);
+ if (cst)
+ ipa_set_jf_constant_unknown_offset (dst, cst);
+ else
+ ipa_set_jf_unknown (dst);
+ }
+ else if (ipa_jf_pass_through_unknown_offset_p (src))
+ {
+ tree op_type = ipa_get_jf_pass_through_op_type (src);
+ int fid = ipa_get_jf_pass_through_formal_id (src);
+ ipa_set_jf_arith_pass_through (dst, fid, NULL_TREE,
+ POINTER_PLUS_EXPR, op_type);
+ }
else if (src->type == IPA_JF_ANCESTOR)
{
dst->value.ancestor.formal_id = src->value.ancestor.formal_id;
@@ -4291,6 +4611,18 @@ update_jump_functions_after_inlining (struct cgraph_edge
*cs,
ipa_get_type (old_inline_root_info,
dst_fid));
break;
+ case IPA_JF_CONST_UNKNOWN_OFFSET:
+ {
+ tree parm_type
+ = ipa_get_type (old_inline_root_info, dst_fid);
+ tree new_val = ipacp_value_safe_for_type
+ (parm_type, ipa_get_jf_constant_unknown_offset (src));
+ if (new_val)
+ ipa_set_jf_constant_unknown_offset (dst, new_val);
+ else
+ ipa_set_jf_unknown (dst);
+ break;
+ }
case IPA_JF_PASS_THROUGH:
{
@@ -4389,6 +4721,47 @@ update_jump_functions_after_inlining (struct cgraph_edge
*cs,
dst->agg.items = vec_safe_copy (src->agg.items);
}
}
+ else if (ipa_jf_pass_through_unknown_offset_p (dst)
+ && top
+ && (dst->value.pass_through.formal_id
+ < ipa_get_cs_argument_count (top)))
+ {
+ int dst_fid = dst->value.pass_through.formal_id;
+ src = ipa_get_ith_jump_func (top, dst_fid);
+ tree op_type = ipa_get_jf_pass_through_op_type (dst);
+
+ switch (src->type)
+ {
+ case IPA_JF_CONST:
+ case IPA_JF_CONST_UNKNOWN_OFFSET:
+ {
+ tree cst = ipa_base_value_from_jfunc (new_inline_root_info,
+ src, op_type);
+ if (cst)
+ ipa_set_jf_constant_unknown_offset (dst, cst);
+ else
+ ipa_set_jf_unknown (dst);
+ break;
+ }
+ case IPA_JF_PASS_THROUGH:
+ if (src->value.pass_through.operation == NOP_EXPR
+ || src->value.pass_through.operation == POINTER_PLUS_EXPR)
+ ipa_set_jf_arith_pass_through
+ (dst, ipa_get_jf_pass_through_formal_id (src),
+ NULL_TREE, POINTER_PLUS_EXPR, op_type);
+ else
+ ipa_set_jf_unknown (dst);
+ break;
+ case IPA_JF_ANCESTOR:
+ ipa_set_jf_arith_pass_through
+ (dst, ipa_get_jf_ancestor_formal_id (src),
+ NULL_TREE, POINTER_PLUS_EXPR, op_type);
+ break;
+ default:
+ ipa_set_jf_unknown (dst);
+ break;
+ }
+ }
else
ipa_set_jf_unknown (dst);
}
@@ -5566,6 +5939,9 @@ ipa_duplicate_jump_function (cgraph_edge *src,
cgraph_edge *dst,
dst_jf->value.constant.rdesc = dst_rdesc;
}
}
+ else if (src_jf->type == IPA_JF_CONST_UNKNOWN_OFFSET)
+ ipa_set_jf_constant_unknown_offset (dst_jf,
+ src_jf->value.constant_unknown_offset);
else if (src_jf->type == IPA_JF_PASS_THROUGH)
{
dst_jf->type = IPA_JF_PASS_THROUGH;
@@ -5805,8 +6181,10 @@ ipa_write_jump_function (struct output_block *ob,
/* ADDR_EXPRs are very common IP invariants; save some streamer data
as well as WPA memory by handling them specially. */
- if (jump_func->type == IPA_JF_CONST
- && TREE_CODE (jump_func->value.constant.value) == ADDR_EXPR)
+ if ((jump_func->type == IPA_JF_CONST
+ && TREE_CODE (jump_func->value.constant.value) == ADDR_EXPR)
+ || (jump_func->type == IPA_JF_CONST_UNKNOWN_OFFSET
+ && TREE_CODE (jump_func->value.constant_unknown_offset) == ADDR_EXPR))
flag = 1;
streamer_write_uhwi (ob, jump_func->type * 2 + flag);
@@ -5822,6 +6200,13 @@ ipa_write_jump_function (struct output_block *ob,
? TREE_OPERAND (jump_func->value.constant.value, 0)
: jump_func->value.constant.value, true);
break;
+ case IPA_JF_CONST_UNKNOWN_OFFSET:
+ {
+ tree cst = jump_func->value.constant_unknown_offset;
+ gcc_assert (EXPR_LOCATION (cst) == UNKNOWN_LOCATION);
+ stream_write_tree (ob, flag ? TREE_OPERAND (cst, 0) : cst, true);
+ break;
+ }
case IPA_JF_PASS_THROUGH:
streamer_write_uhwi (ob, jump_func->value.pass_through.operation);
if (jump_func->value.pass_through.operation == NOP_EXPR)
@@ -5878,6 +6263,9 @@ ipa_write_jump_function (struct output_block *ob,
case IPA_JF_CONST:
stream_write_tree (ob, item->value.constant, true);
break;
+ case IPA_JF_CONST_UNKNOWN_OFFSET:
+ stream_write_tree (ob, item->value.constant_unknown_offset, true);
+ break;
case IPA_JF_PASS_THROUGH:
case IPA_JF_LOAD_AGG:
streamer_write_uhwi (ob, item->value.pass_through.operation);
@@ -5941,6 +6329,14 @@ ipa_read_jump_function (class lto_input_block *ib,
ipa_set_jf_constant (jump_func, t, cs);
}
break;
+ case IPA_JF_CONST_UNKNOWN_OFFSET:
+ {
+ tree t = stream_read_tree (ib, data_in);
+ if (flag && prevails)
+ t = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (t)), t);
+ ipa_set_jf_constant_unknown_offset (jump_func, t);
+ }
+ break;
case IPA_JF_PASS_THROUGH:
operation = (enum tree_code) streamer_read_uhwi (ib);
if (operation == NOP_EXPR)
@@ -6006,6 +6402,9 @@ ipa_read_jump_function (class lto_input_block *ib,
case IPA_JF_CONST:
item.value.constant = stream_read_tree (ib, data_in);
break;
+ case IPA_JF_CONST_UNKNOWN_OFFSET:
+ item.value.constant_unknown_offset = stream_read_tree (ib, data_in);
+ break;
case IPA_JF_PASS_THROUGH:
case IPA_JF_LOAD_AGG:
operation = (enum tree_code) streamer_read_uhwi (ib);
@@ -6613,6 +7012,7 @@ write_ipcp_transformation_info (output_block *ob,
cgraph_node *node,
bp = bitpack_create (ob->main_stream);
bp_pack_value (&bp, av.by_ref, 1);
bp_pack_value (&bp, av.killed, 1);
+ bp_pack_value (&bp, av.unknown_offset, 1);
streamer_write_bitpack (&bp);
}
@@ -6652,6 +7052,7 @@ read_ipcp_transformation_info (lto_input_block *ib,
cgraph_node *node,
bitpack_d bp = streamer_read_bitpack (ib);
av->by_ref = bp_unpack_value (&bp, 1);
av->killed = bp_unpack_value (&bp, 1);
+ av->unknown_offset = bp_unpack_value (&bp, 1);
}
}
@@ -6965,7 +7366,7 @@ ipcp_get_aggregate_const (struct function *func, tree
parm, bool by_ref,
ipa_argagg_value_list avl (ts);
unsigned unit_offset = bit_offset / BITS_PER_UNIT;
const ipa_argagg_value *av = avl.get_elt (index, unit_offset);
- if (!av || av->by_ref != by_ref)
+ if (!av || av->by_ref != by_ref || av->unknown_offset)
return NULL_TREE;
gcc_assert (!av->killed);
tree v = av->value;
@@ -7270,6 +7671,11 @@ ipa_agg_jump_functions_equivalent_p (ipa_agg_jf_item
*ajf1,
ajf2->value.constant))
return false;
break;
+ case IPA_JF_CONST_UNKNOWN_OFFSET:
+ if (!values_equal_for_ipcp_p (ajf1->value.constant_unknown_offset,
+ ajf2->value.constant_unknown_offset))
+ return false;
+ break;
case IPA_JF_PASS_THROUGH:
{
ipa_pass_through_data *ipt1 = &ajf1->value.pass_through;
@@ -7332,6 +7738,11 @@ ipa_jump_functions_equivalent_p (ipa_jump_func *jf1,
ipa_jump_func *jf2)
return false;
}
break;
+ case IPA_JF_CONST_UNKNOWN_OFFSET:
+ if (!values_equal_for_ipcp_p (jf1->value.constant_unknown_offset,
+ jf2->value.constant_unknown_offset))
+ return false;
+ break;
case IPA_JF_PASS_THROUGH:
{
ipa_pass_through_data *ipt1 = &jf1->value.pass_through;
diff --git a/gcc/ipa-prop.h b/gcc/ipa-prop.h
index 5ff9ab84209..7691019dc77 100644
--- a/gcc/ipa-prop.h
+++ b/gcc/ipa-prop.h
@@ -73,6 +73,7 @@ enum jump_func_type
{
IPA_JF_UNKNOWN = 0, /* newly allocated and zeroed jump functions default */
IPA_JF_CONST, /* represented by field constant */
+ IPA_JF_CONST_UNKNOWN_OFFSET, /* Base addr constant with unknown offset. */
IPA_JF_PASS_THROUGH, /* represented by field pass_through */
IPA_JF_LOAD_AGG, /* represented by field load_agg */
IPA_JF_ANCESTOR /* represented by field ancestor */
@@ -180,6 +181,7 @@ struct GTY(()) ipa_agg_jf_item
union jump_func_agg_value
{
tree GTY ((tag ("IPA_JF_CONST"))) constant;
+ tree GTY ((tag ("IPA_JF_CONST_UNKNOWN_OFFSET"))) constant_unknown_offset;
struct ipa_pass_through_data GTY ((tag ("IPA_JF_PASS_THROUGH")))
pass_through;
struct ipa_load_agg_data GTY ((tag ("IPA_JF_LOAD_AGG"))) load_agg;
} GTY ((desc ("%1.jftype"))) value;
@@ -220,6 +222,9 @@ struct GTY(()) ipa_argagg_value
value_numbering. It is kept around just so that clone materialization can
distinguish a combined IPA-CP and IPA-SRA from a deleted argument. */
unsigned killed : 1;
+ /* Set if the value represents only a base pointer for an unknown-offset
+ pointer adjustment and must not be used for exact-offset lookups. */
+ unsigned unknown_offset : 1;
};
/* A view into a sorted list of aggregate values in a particular context, be it
@@ -256,6 +261,14 @@ public:
tree get_value (int index, unsigned unit_offset) const;
+ /* Return the base pointer constant stored for INDEX at UNIT_OFFSET if it
+ is 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),
regardless
+ of unknown_offset, or NULL_TREE otherwise. */
+
+ tree get_base_value (int index, unsigned unit_offset, bool by_ref,
+ bool maybe_modified = false) const;
+
/* Return the item describing a constant stored for INDEX at UNIT_OFFSET or
NULL if there is no such constant. */
@@ -358,6 +371,7 @@ struct GTY (()) ipa_jump_func
union jump_func_value
{
struct ipa_constant_data GTY ((tag ("IPA_JF_CONST"))) constant;
+ tree GTY ((tag ("IPA_JF_CONST_UNKNOWN_OFFSET"))) constant_unknown_offset;
struct ipa_pass_through_data GTY ((tag ("IPA_JF_PASS_THROUGH")))
pass_through;
struct ipa_ancestor_jf_data GTY ((tag ("IPA_JF_ANCESTOR"))) ancestor;
} GTY ((desc ("%1.type"))) value;
@@ -373,6 +387,27 @@ ipa_get_jf_constant (struct ipa_jump_func *jfunc)
return jfunc->value.constant.value;
}
+/* Return the base constant stored in an unknown-offset constant jump function
+ JFUNC. */
+
+inline tree
+ipa_get_jf_constant_unknown_offset (struct ipa_jump_func *jfunc)
+{
+ gcc_checking_assert (jfunc->type == IPA_JF_CONST_UNKNOWN_OFFSET);
+ return jfunc->value.constant_unknown_offset;
+}
+
+/* Return true if JFUNC is an unknown-offset pointer pass-through jump
+ function. */
+
+inline bool
+ipa_jf_pass_through_unknown_offset_p (struct ipa_jump_func *jfunc)
+{
+ return (jfunc->type == IPA_JF_PASS_THROUGH
+ && jfunc->value.pass_through.operation == POINTER_PLUS_EXPR
+ && !jfunc->value.pass_through.operand);
+}
+
inline struct ipa_cst_ref_desc *
ipa_get_jf_constant_rdesc (struct ipa_jump_func *jfunc)
{
@@ -534,9 +569,22 @@ public:
return NULL;
}
+ /* If m_known_base_vals is sufficiently long, return its element at INDEX,
+ otherwise return NULL. */
+ tree safe_base_sval_at (int index)
+ {
+ if ((unsigned) index < m_known_base_vals.length ())
+ return m_known_base_vals[index];
+ return NULL;
+ }
+
/* Vector describing known values of parameters. */
auto_vec<tree, 32> m_known_vals;
+ /* Vector describing known base pointer values of parameters with unknown
+ offsets. */
+ auto_vec<tree, 32> m_known_base_vals;
+
/* Vector describing known polymorphic call contexts. */
auto_vec<ipa_polymorphic_call_context, 32> m_known_contexts;
@@ -572,6 +620,7 @@ public:
out of sync. */
ipa_call_arg_values (ipa_auto_call_arg_values *aavals)
: m_known_vals (aavals->m_known_vals.to_vec_legacy ()),
+ m_known_base_vals (aavals->m_known_base_vals.to_vec_legacy ()),
m_known_contexts (aavals->m_known_contexts.to_vec_legacy ()),
m_known_aggs (aavals->m_known_aggs.to_vec_legacy ()),
m_known_value_ranges (aavals->m_known_value_ranges.to_vec_legacy ())
@@ -586,9 +635,22 @@ public:
return NULL;
}
+ /* If m_known_base_vals is sufficiently long, return its element at INDEX,
+ otherwise return NULL. */
+ tree safe_base_sval_at (int index)
+ {
+ if ((unsigned) index < m_known_base_vals.length ())
+ return m_known_base_vals[index];
+ return NULL;
+ }
+
/* Vector describing known values of parameters. */
vec<tree> m_known_vals = vNULL;
+ /* Vector describing known base pointer values of parameters with unknown
+ offsets. */
+ vec<tree> m_known_base_vals = vNULL;
+
/* Vector describing known polymorphic call contexts. */
vec<ipa_polymorphic_call_context> m_known_contexts = vNULL;
@@ -1268,6 +1330,8 @@ void ipcp_read_transformation_summaries (void);
int ipa_get_param_decl_index (class ipa_node_params *, tree);
tree ipa_value_from_jfunc (class ipa_node_params *info,
struct ipa_jump_func *jfunc, tree type);
+tree ipa_base_value_from_jfunc (class ipa_node_params *info,
+ struct ipa_jump_func *jfunc, tree type);
tree ipa_agg_value_from_jfunc (ipa_node_params *info, cgraph_node *node,
const ipa_agg_jf_item *item);
unsigned int ipcp_transform_function (struct cgraph_node *node);
@@ -1280,7 +1344,8 @@ void ipa_value_range_from_jfunc (vrange &,
ipa_node_params *, cgraph_edge *,
void ipa_push_agg_values_from_jfunc (ipa_node_params *info, cgraph_node *node,
ipa_agg_jump_function *agg_jfunc,
unsigned dst_index,
- vec<ipa_argagg_value> *res);
+ vec<ipa_argagg_value> *res,
+ bool allow_unknown_offset = false);
void ipa_dump_param (FILE *, class ipa_node_params *info, int i);
void ipa_dump_jump_function (FILE *f, ipa_jump_func *jfunc,
class ipa_polymorphic_call_context *ctx = NULL);
diff --git a/gcc/testsuite/gcc.dg/ipa/pr127171-4.c
b/gcc/testsuite/gcc.dg/ipa/pr127171-4.c
new file mode 100644
index 00000000000..1c8c660b1b9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ipa/pr127171-4.c
@@ -0,0 +1,52 @@
+/* { dg-do compile } */
+/* { dg-options "-Os -fno-early-inlining -fno-partial-inlining
-fdump-ipa-inline-details" } */
+
+extern void unknown_fn (const char *, int);
+extern volatile int g;
+
+static int
+leaf (const char *p)
+{
+ if (*p == 'Z')
+ {
+ unknown_fn (p, 1);
+ unknown_fn (p, 2);
+ unknown_fn (p, 3);
+ unknown_fn (p, 4);
+ unknown_fn (p, 5);
+ unknown_fn (p, 6);
+ unknown_fn (p, 7);
+ unknown_fn (p, 8);
+ g = 1;
+ return 1;
+ }
+ return 0;
+}
+
+static int
+mid (const char *q, int m)
+{
+ return leaf (q + m);
+}
+
+int
+caller1 (int n, int m)
+{
+ return mid ("abc" + n, m);
+}
+
+int
+caller2 (int n, int c)
+{
+ const char *p = c ? "abc" + n : "abc" + 1;
+ return leaf (p);
+}
+
+int
+caller_unknown (const char *p)
+{
+ return leaf (p) + mid (p, 1);
+}
+
+/* { dg-final { scan-ipa-dump-times "Inlined leaf" 2 "inline" } } */
+/* { dg-final { scan-ipa-dump-not "Inlined leaf\[^\n\]*into caller_unknown"
"inline" } } */
diff --git a/gcc/testsuite/gcc.dg/ipa/pr127171-5.c
b/gcc/testsuite/gcc.dg/ipa/pr127171-5.c
new file mode 100644
index 00000000000..564abf80ad4
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ipa/pr127171-5.c
@@ -0,0 +1,99 @@
+/* { dg-do compile } */
+/* { dg-options "-Os -fno-early-inlining -fno-ipa-sra -fno-partial-inlining
-fdump-ipa-inline-details" } */
+
+extern void unknown_fn (const char *, int);
+extern volatile int g;
+
+struct S
+{
+ int tag;
+ const char *p;
+};
+
+static int
+leaf_val (struct S s, int k)
+{
+ if (s.p[k] == 'Z')
+ {
+ unknown_fn (s.p, 1);
+ unknown_fn (s.p, 2);
+ unknown_fn (s.p, 3);
+ unknown_fn (s.p, 4);
+ unknown_fn (s.p, 5);
+ unknown_fn (s.p, 6);
+ unknown_fn (s.p, 7);
+ unknown_fn (s.p, 8);
+ g = 1;
+ return 1;
+ }
+ return 0;
+}
+
+static int
+leaf_ref (const struct S *s)
+{
+ if (s->p[0] == 'Z')
+ {
+ unknown_fn (s->p, 1);
+ unknown_fn (s->p, 2);
+ unknown_fn (s->p, 3);
+ unknown_fn (s->p, 4);
+ unknown_fn (s->p, 5);
+ unknown_fn (s->p, 6);
+ unknown_fn (s->p, 7);
+ unknown_fn (s->p, 8);
+ g = 1;
+ return 1;
+ }
+ return 0;
+}
+
+static int
+mid_ref (const struct S *s)
+{
+ return leaf_ref (s);
+}
+
+int
+caller_val (int n, int k)
+{
+ struct S s;
+ s.tag = 1;
+ s.p = "abc" + n;
+ return leaf_val (s, k);
+}
+
+int
+caller_ref (int n)
+{
+ struct S s;
+ s.tag = 2;
+ s.p = "ade" + n;
+ return leaf_ref (&s);
+}
+
+int
+caller_mid_ref (int n)
+{
+ struct S s;
+ s.tag = 3;
+ s.p = "afg" + n;
+ return mid_ref (&s);
+}
+
+int
+caller_unknown (struct S s, int k)
+{
+ return leaf_val (s, k) + leaf_ref (&s) + mid_ref (&s);
+}
+
+int
+caller_unknown2 (struct S s, int k)
+{
+ return leaf_val (s, k) + leaf_ref (&s);
+}
+
+/* { dg-final { scan-ipa-dump-times "Inlined leaf_val" 1 "inline" } } */
+/* { dg-final { scan-ipa-dump-times "Inlined leaf_ref" 2 "inline" } } */
+/* { dg-final { scan-ipa-dump-not "Inlined leaf_val\[^\n\]*into
caller_unknown" "inline" } } */
+/* { dg-final { scan-ipa-dump-not "Inlined leaf_ref\[^\n\]*into
caller_unknown" "inline" } } */
--
2.53.0