Hi,
This patch adds EAF_POINTER_ADJUST which records that the function takes
pointer by refernece and adjusts it. This is needed to analyze
diplay_impl in cppcheck which needs to be inlined into match and does
precisely this.
The patch factors out existing analysis from ipa-prop and makes
ipa-modref to handle it. It can also be used by points-to for which
I will make separate patch.
Bootstrapped/regtested x86_64-linux. I plan to test this bit more
and would welcome feedback.
With some additional fixes to ipa-prop I will push out shortly this
makes the analysis to work on cppcheck testcase in PR ipa/127171
Next stage1, I think we may want to extend the pattern of pointer adjust
but more importantly, I think we want to have return functions for
parameters returned by reference, which would be more informative for
points-to as well.
gcc/ChangeLog:
PR ipa/127171
* tree-core.h (EAF_POINTER_ADJUST): Define (1 << 10).
* ipa-modref.h (implicit_const_eaf_flags): Include EAF_POINTER_ADJUST.
(implicit_pure_eaf_flags): Likewise.
(ignore_stores_eaf_flags): Likewise.
(interposable_eaf_flags): Restore EAF_POINTER_ADJUST when clearing
EAF_UNUSED.
* ipa-modref.cc (dump_eaf_flags): Dump EAF_POINTER_ADJUST as
"pointer_adjust".
(get_zero_offset_ptr_deref_base): New helper.
(strip_ptr_copies): New helper.
(struct modref_ptr_adjust_data): New struct.
(modref_ptr_adjust_leaf_cb): New helper.
(pointer_adjust_store_p): New helper using ipa_walk_ptr_adjustments.
(deref_flags): Preserve EAF_POINTER_ADJUST on dereference.
(modref_lattice::init): Initialize EAF_POINTER_ADJUST.
(modref_lattice::merge_direct_store): Clear EAF_POINTER_ADJUST.
(callee_to_caller_flags): Set EAF_POINTER_ADJUST when EAF_UNUSED or
EAF_NO_DIRECT_CLOBBER is set.
(modref_eaf_analysis::analyze_ssa_name): Clear EAF_POINTER_ADJUST on
call target and non-copy uses; preserve it on pointer-adjust stores
when clearing EAF_NO_DIRECT_CLOBBER.
(analyze_parms): Normalize fnspec attr_flags with EAF_POINTER_ADJUST
when EAF_UNUSED or EAF_NO_DIRECT_CLOBBER is set.
(modref_merge_call_site_flags): Likewise for merged flags.
(ipa_merge_modref_summary_after_inlining): Likewise.
* ipa-prop.h (ipa_ptr_leaf_fn): New typedef.
(ipa_walk_ptr_adjustments): Declare.
* ipa-prop.cc (ipa_walk_ptr_adjustments): New function factored out
from ipa_load_from_parm_agg_unknown_offset.
(ipa_unknown_off_parm_data): New struct.
(ipa_unknown_off_parm_leaf): New function.
(ipa_load_from_parm_agg_unknown_offset): Use ipa_walk_ptr_adjustments.
gcc/testsuite/ChangeLog:
* gcc.dg/ipa/modref-ptr-adjust-1.c: New test.
Assisted-by: Google Carbon
---
gcc/ipa-modref.cc | 133 +++++++++++++++++-
gcc/ipa-modref.h | 11 +-
.../gcc.dg/ipa/modref-ptr-adjust-1.c | 50 +++++++
gcc/tree-core.h | 4 +
4 files changed, 189 insertions(+), 9 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/ipa/modref-ptr-adjust-1.c
diff --git a/gcc/ipa-modref.cc b/gcc/ipa-modref.cc
index 7883a90bcdd..ae806beb19f 100644
--- a/gcc/ipa-modref.cc
+++ b/gcc/ipa-modref.cc
@@ -169,6 +169,8 @@ dump_eaf_flags (FILE *out, int flags, bool newline = true)
fprintf (out, " no_direct_read");
if (flags & EAF_NO_INDIRECT_READ)
fprintf (out, " no_indirect_read");
+ if (flags & EAF_POINTER_ADJUST)
+ fprintf (out, " pointer_adjust");
if (newline)
fprintf (out, "\n");
}
@@ -1956,6 +1958,102 @@ memory_access_to (tree op, tree ssa_name)
return TREE_OPERAND (base, 0) == ssa_name;
}
+/* If OP is a zero-offset pointer dereference of a base pointer SSA name,
+ return that base pointer; otherwise return NULL_TREE. */
+
+static tree
+get_zero_offset_ptr_deref_base (tree op)
+{
+ if (!op || !POINTER_TYPE_P (TREE_TYPE (op)) || TREE_THIS_VOLATILE (op))
+ return NULL_TREE;
+ while (TREE_CODE (op) == COMPONENT_REF)
+ {
+ tree field = TREE_OPERAND (op, 1);
+ if (!DECL_FIELD_OFFSET (field)
+ || !integer_zerop (DECL_FIELD_OFFSET (field))
+ || !DECL_FIELD_BIT_OFFSET (field)
+ || !integer_zerop (DECL_FIELD_BIT_OFFSET (field)))
+ return NULL_TREE;
+ op = TREE_OPERAND (op, 0);
+ }
+ if (TREE_CODE (op) == MEM_REF
+ && !TREE_THIS_VOLATILE (op)
+ && integer_zerop (TREE_OPERAND (op, 1)))
+ return TREE_OPERAND (op, 0);
+ return NULL_TREE;
+}
+
+/* Strip simple SSA pointer copies and pointer conversions from P. */
+
+static tree
+strip_ptr_copies (tree p)
+{
+ for (int depth = 0; depth < 8 && p && TREE_CODE (p) == SSA_NAME; depth++)
+ {
+ gimple *def = SSA_NAME_DEF_STMT (p);
+ if (!def || !is_gimple_assign (def))
+ break;
+ enum tree_code code = gimple_assign_rhs_code (def);
+ tree rhs1 = gimple_assign_rhs1 (def);
+ if (code == SSA_NAME
+ || (CONVERT_EXPR_CODE_P (code)
+ && POINTER_TYPE_P (TREE_TYPE (p))
+ && POINTER_TYPE_P (TREE_TYPE (rhs1))))
+ p = rhs1;
+ else
+ break;
+ }
+ return p;
+}
+
+/* Callback for ipa_walk_ptr_adjustments verifying that EXPR comes from NULL
+ or a load directly from *DATA->store_ptr. */
+
+static bool
+modref_ptr_adjust_leaf_cb (gimple *stmt, tree expr, vec<tree> &,
+ void *data)
+{
+ tree store_ptr = (tree) data;
+ if (expr && integer_zerop (expr))
+ return (stmt
+ && (gimple_code (stmt) == GIMPLE_PHI
+ || (is_gimple_assign (stmt)
+ && (gimple_assign_rhs_code (stmt) == INTEGER_CST
+ || CONVERT_EXPR_CODE_P
+ (gimple_assign_rhs_code (stmt))))));
+ if (!stmt || !gimple_assign_load_p (stmt))
+ return false;
+ tree load_ptr = get_zero_offset_ptr_deref_base (gimple_assign_rhs1 (stmt));
+ return (load_ptr
+ && strip_ptr_copies (load_ptr) == strip_ptr_copies (store_ptr));
+}
+
+/* Return true if ASSIGN only stores NULL or an adjustment of *store_ptr back
+ into *store_ptr at zero offset. */
+
+static bool
+pointer_adjust_store_p (gassign *assign)
+{
+ if (gimple_clobber_p (assign))
+ return false;
+ tree store_ptr = get_zero_offset_ptr_deref_base (gimple_assign_lhs (assign));
+ if (!store_ptr)
+ return false;
+ tree rhs = gimple_assign_rhs1 (assign);
+ if (integer_zerop (rhs))
+ return true;
+ if (TREE_CODE (rhs) != SSA_NAME)
+ return false;
+ modref_ptr_adjust_data data = { store_ptr };
+ return ipa_walk_ptr_adjustments (rhs, false, modref_ptr_adjust_leaf_cb,
+ &data);
+}
+
/* Consider statement val = *arg.
return EAF flags of ARG that can be determined from EAF flags of VAL
(which are known to be FLAGS). If IGNORE_STORES is true we can ignore
@@ -1967,7 +2065,7 @@ deref_flags (int flags, bool ignore_stores)
/* Dereference is also a direct read but dereferenced value does not
yield any other direct use. */
int ret = EAF_NO_DIRECT_CLOBBER | EAF_NO_DIRECT_ESCAPE
- | EAF_NOT_RETURNED_DIRECTLY;
+ | EAF_NOT_RETURNED_DIRECTLY | EAF_POINTER_ADJUST;
/* If argument is unused just account for
the read involved in dereference. */
if (flags & EAF_UNUSED)
@@ -2067,7 +2165,7 @@ modref_lattice::init ()
| EAF_NO_DIRECT_ESCAPE | EAF_NO_INDIRECT_ESCAPE
| EAF_NO_DIRECT_READ | EAF_NO_INDIRECT_READ
| EAF_NOT_RETURNED_DIRECTLY | EAF_NOT_RETURNED_INDIRECTLY
- | EAF_UNUSED;
+ | EAF_UNUSED | EAF_POINTER_ADJUST;
flags = f;
/* Check that eaf_flags_t is wide enough to hold all flags. */
gcc_checking_assert (f == flags);
@@ -2230,7 +2328,7 @@ modref_lattice::merge_direct_load ()
bool
modref_lattice::merge_direct_store ()
{
- return merge (~(EAF_UNUSED | EAF_NO_DIRECT_CLOBBER));
+ return merge (~(EAF_UNUSED | EAF_NO_DIRECT_CLOBBER | EAF_POINTER_ADJUST));
}
/* Analyzer of EAF flags.
@@ -2379,6 +2477,8 @@ callee_to_caller_flags (int call_flags, bool
ignore_stores,
}
else
call_flags |= ignore_stores_eaf_flags;
+ if (call_flags & (EAF_UNUSED | EAF_NO_DIRECT_CLOBBER))
+ call_flags |= EAF_POINTER_ADJUST;
return call_flags;
}
@@ -2473,7 +2573,8 @@ modref_eaf_analysis::analyze_ssa_name (tree name, bool
deferred)
is on since that would allow propagation of this from -fno-ipa-pta
to -fipa-pta functions. */
if (gimple_call_fn (use_stmt) == name)
- m_lattice[index].merge (~(EAF_NO_DIRECT_CLOBBER | EAF_UNUSED));
+ m_lattice[index].merge (~(EAF_NO_DIRECT_CLOBBER | EAF_UNUSED
+ | EAF_POINTER_ADJUST));
/* Recursion would require bit of propagation; give up for now. */
if (callee && !m_ipa && recursive_call_p (current_function_decl,
@@ -2651,7 +2752,14 @@ modref_eaf_analysis::analyze_ssa_name (tree name, bool
deferred)
from other functions.
Similar logic is in ipa-pure-const.cc. */
if (!cfun->after_inlining || !gimple_clobber_p (assign))
- m_lattice[index].merge_direct_store ();
+ {
+ if ((m_lattice[index].flags & EAF_POINTER_ADJUST)
+ && pointer_adjust_store_p (assign))
+ m_lattice[index].merge (~(EAF_UNUSED
+ | EAF_NO_DIRECT_CLOBBER));
+ else
+ m_lattice[index].merge_direct_store ();
+ }
}
/* ASM statements etc. */
else if (!assign)
@@ -2673,6 +2781,11 @@ modref_eaf_analysis::analyze_ssa_name (tree name, bool
deferred)
|| gimple_assign_rhs1 (assign) == name))
{
tree lhs = gimple_assign_lhs (assign);
+ if (code != SSA_NAME
+ && !(CONVERT_EXPR_CODE_P (code)
+ && POINTER_TYPE_P (TREE_TYPE (lhs))
+ && POINTER_TYPE_P (TREE_TYPE (name))))
+ m_lattice[index].merge (~(EAF_UNUSED | EAF_POINTER_ADJUST));
merge_with_ssa_name (name, lhs, false);
}
}
@@ -2993,6 +3106,8 @@ analyze_parms (modref_summary *summary,
modref_summary_lto *summary_lto,
}
int flags = eaf_analysis.get_ssa_name_flags (name);
int attr_flags = fnspec.arg_eaf_flags (parm_index);
+ if (attr_flags & (EAF_UNUSED | EAF_NO_DIRECT_CLOBBER))
+ attr_flags |= EAF_POINTER_ADJUST;
if (dump_file && (flags | attr_flags) != flags && !(flags & EAF_UNUSED))
{
@@ -5148,6 +5263,10 @@ modref_merge_call_site_flags (escape_summary *sum,
implicit_flags = deref_flags (implicit_flags, ignore_stores);
flags |= implicit_flags;
flags_lto |= implicit_flags;
+ if (flags & (EAF_UNUSED | EAF_NO_DIRECT_CLOBBER))
+ flags |= EAF_POINTER_ADJUST;
+ if (flags_lto & (EAF_UNUSED | EAF_NO_DIRECT_CLOBBER))
+ flags_lto |= EAF_POINTER_ADJUST;
if (!binds_to_current_def && (flags || flags_lto))
{
flags = interposable_eaf_flags (flags, implicit_flags);
@@ -5486,6 +5605,8 @@ ipa_merge_modref_summary_after_inlining (cgraph_edge
*edge)
if (!ee->direct)
flags = deref_flags (flags, ignore_stores);
flags |= ee->min_flags | implicit_flags;
+ if (flags & (EAF_UNUSED | EAF_NO_DIRECT_CLOBBER))
+ flags |= EAF_POINTER_ADJUST;
eaf_flags_t &f = ee->parm_index == MODREF_RETSLOT_PARM
? to_info->retslot_flags
: ee->parm_index == MODREF_STATIC_CHAIN_PARM
@@ -5504,6 +5625,8 @@ ipa_merge_modref_summary_after_inlining (cgraph_edge
*edge)
if (!ee->direct)
flags = deref_flags (flags, ignore_stores);
flags |= ee->min_flags | implicit_flags;
+ if (flags & (EAF_UNUSED | EAF_NO_DIRECT_CLOBBER))
+ flags |= EAF_POINTER_ADJUST;
eaf_flags_t &f = ee->parm_index == MODREF_RETSLOT_PARM
? to_info_lto->retslot_flags
: ee->parm_index == MODREF_STATIC_CHAIN_PARM
diff --git a/gcc/ipa-modref.h b/gcc/ipa-modref.h
index 8558dff8f2c..4c8f9fb26f6 100644
--- a/gcc/ipa-modref.h
+++ b/gcc/ipa-modref.h
@@ -119,7 +121,8 @@ interposable_eaf_flags (int modref_flags, int flags)
modref_flags &= ~EAF_UNUSED;
modref_flags |= EAF_NO_DIRECT_ESCAPE | EAF_NO_INDIRECT_ESCAPE
| EAF_NOT_RETURNED_DIRECTLY | EAF_NOT_RETURNED_INDIRECTLY
- | EAF_NO_DIRECT_CLOBBER | EAF_NO_INDIRECT_CLOBBER;
+ | EAF_NO_DIRECT_CLOBBER | EAF_NO_INDIRECT_CLOBBER
+ | EAF_POINTER_ADJUST;
}
/* We can not determine that value is not read at all. */
if ((modref_flags & EAF_NO_DIRECT_READ) && !(flags & EAF_NO_DIRECT_READ))
diff --git a/gcc/testsuite/gcc.dg/ipa/modref-ptr-adjust-1.c
b/gcc/testsuite/gcc.dg/ipa/modref-ptr-adjust-1.c
new file mode 100644
index 00000000000..61823f477dd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ipa/modref-ptr-adjust-1.c
@@ -0,0 +1,50 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-ipa-modref" } */
+
+extern void ext (const char **);
+
+__attribute__ ((noinline)) void
+adjust_simple (const char **p, int n)
+{
+ *p = *p + n;
+}
+
+__attribute__ ((noinline)) void
+adjust_loop_and_null (const char **p)
+{
+ const char *s = *p;
+ while (*s == ' ')
+ s++;
+ *p = (*s == '\0') ? 0 : s;
+}
+
+__attribute__ ((noinline)) void
+adjust_wrapper (const char **p)
+{
+ adjust_loop_and_null (p);
+}
+
+__attribute__ ((noinline)) void
+overwrite_other (const char **p, const char *q)
+{
+ *p = q;
+}
+
+__attribute__ ((noinline)) void
+overwrite_lit (const char **p)
+{
+ *p = "abc";
+}
+
+__attribute__ ((noinline)) void
+escape_ptr (const char **p)
+{
+ ext (p);
+}
+
+/* { dg-final { scan-ipa-dump "Propagated modref for
adjust_simple/\[^\n\]*\n(?:\[^\n\]+\n)+\[ \]*parm 0 flags: no_direct_escape
pointer_adjust" "modref" } } */
+/* { dg-final { scan-ipa-dump "Propagated modref for
adjust_loop_and_null/\[^\n\]*\n(?:\[^\n\]+\n)+\[ \]*parm 0 flags:
no_direct_escape pointer_adjust" "modref" } } */
+/* { dg-final { scan-ipa-dump "Propagated modref for
adjust_wrapper/\[^\n\]*\n(?:\[^\n\]+\n)+\[ \]*parm 0 flags: no_direct_escape
pointer_adjust" "modref" } } */
+/* { dg-final { scan-ipa-dump-not "Propagated modref for
overwrite_other/\[^\n\]*\n(?:\[^\n\]+\n)+\[^\n\]*pointer_adjust" "modref" } } */
+/* { dg-final { scan-ipa-dump-not "Propagated modref for
overwrite_lit/\[^\n\]*\n(?:\[^\n\]+\n)+\[^\n\]*pointer_adjust" "modref" } } */
+/* { dg-final { scan-ipa-dump-not "Propagated modref for
escape_ptr/\[^\n\]*\n(?:\[^\n\]+\n)+\[^\n\]*pointer_adjust" "modref" } } */
diff --git a/gcc/tree-core.h b/gcc/tree-core.h
index ae0a16d1241..f2f2cc1f857 100644
--- a/gcc/tree-core.h
+++ b/gcc/tree-core.h
@@ -131,6 +131,10 @@ class irange;
#define EAF_NO_DIRECT_READ (1 << 8)
#define EAF_NO_INDIRECT_READ (1 << 9)
+/* Nonzero if direct stores through the argument only store NULL or pointers
+ derived from *arg via pointer adjustments. */
+#define EAF_POINTER_ADJUST (1 << 10)
+
/* Call return flags. */
/* Mask for the argument number that is returned. Lower two bits of
the return flags, encodes argument slots zero to three. */
diff --git a/gcc/ipa-prop.cc b/gcc/ipa-prop.cc
index 9d4bdd81288..5489e58a5ca 100644
--- a/gcc/ipa-prop.cc
+++ b/gcc/ipa-prop.cc
@@ -1278,80 +1278,49 @@ parm_ref_data_pass_through_p (struct ipa_func_body_info
*fbi, int index,
return !modified;
}
-/* 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 using a worklist and
- a visited set of SSA_NAME versions, verifying that every reachable path
- originates from the same function parameter. If so, record *OFFSET_P as
- -1 and set *UNKNOWN_OFFSET_P to true. */
+/* Walk backwards from pointer SSA_NAME START_PTR through POINTER_PLUS_EXPR,
+ SSA copies, pointer-to-pointer conversions, ADDR_EXPR (MEM_REF) address
+ arithmetic and PHIs, invoking LEAF_CB on each non-interior leaf reached.
+ If ALLOW_NULL is true, null pointer operands are skipped (the caller is
+ responsible for ensuring at least one valid origin is found when
+ required). If SAW_PHI_P is non-NULL, *SAW_PHI_P is set to true whenever
+ a PHI node is traversed. Return true if every reachable leaf is accepted
+ by LEAF_CB within param_ipa_jump_function_lookups steps. */
-static bool
-ipa_load_from_parm_agg_unknown_offset (struct ipa_func_body_info *fbi,
- vec<ipa_param_descriptor, va_gc>
- *descriptors,
- gimple *stmt, tree op, int *index_p,
- HOST_WIDE_INT *offset_p,
- poly_int64 *size_p, bool *by_ref_p,
- bool *guaranteed_unmodified,
- bool *unknown_offset_p)
+bool
+ipa_walk_ptr_adjustments (tree start_ptr, bool allow_null,
+ ipa_ptr_leaf_fn leaf_cb, void *data,
+ bool *saw_phi_p)
{
- /* Currently we only work on STRING_CST when evaluating unknown-offset
- conditions, so we do not record other cases (such as by-value
- aggregates or general constructors).
- TODO: Support general constructors. */
- tree type = TREE_TYPE (op);
- if (TREE_CODE (type) != INTEGER_TYPE
- || TYPE_PRECISION (type) != BITS_PER_UNIT)
+ if (!start_ptr || TREE_CODE (start_ptr) != SSA_NAME)
return false;
- tree base = get_base_address (op);
- if (!base || TREE_CODE (base) != MEM_REF)
- return false;
-
- tree start_ptr = TREE_OPERAND (base, 0);
- if (TREE_CODE (start_ptr) != SSA_NAME)
- return false;
-
- 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 (TREE_TYPE (base))));
-
- /* Walk backwards through pointer adjustments, copies, conversions and PHIs,
- verifying that all reachable definitions originate from the same function
- parameter. Since BASE is dereferenced at STMT and POINTER_PLUS_EXPR,
- same-address-space conversions and PHIs preserve null provenance,
- 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). */
auto_vec<tree, 8> worklist;
+ auto_vec<tree, 4> more_ptrs;
hash_set<int_hash<unsigned, 0>> visited;
visited.add (SSA_NAME_VERSION (start_ptr));
worklist.quick_push (start_ptr);
-
- int found_index = -1;
- tree parm_ptr = NULL_TREE;
- bool saw_phi = false;
int steps = 0;
+ auto push_more = [&] (gimple *stmt, tree expr)
+ {
+ more_ptrs.truncate (0);
+ if (!leaf_cb (stmt, expr, more_ptrs, data))
+ return false;
+ for (tree p : more_ptrs)
+ if (TREE_CODE (p) == SSA_NAME
+ && !visited.add (SSA_NAME_VERSION (p)))
+ worklist.safe_push (p);
+ return true;
+ };
+
while (!worklist.is_empty ())
{
tree ptr = worklist.pop ();
if (SSA_NAME_IS_DEFAULT_DEF (ptr))
{
- tree parm = SSA_NAME_VAR (ptr);
- if (!parm || TREE_CODE (parm) != PARM_DECL)
- return false;
- int index = ipa_get_param_decl_index_1 (descriptors, parm);
- if (index < 0
- || (found_index != -1 && found_index != index))
+ if (!push_more (NULL, ptr))
return false;
- found_index = index;
- parm_ptr = ptr;
continue;
}
@@ -1359,20 +1328,28 @@ ipa_load_from_parm_agg_unknown_offset (struct
ipa_func_body_info *fbi,
return false;
gimple *def = SSA_NAME_DEF_STMT (ptr);
+ tree push_expr = NULL_TREE;
+ gimple *leaf_stmt = NULL;
+
if (gphi *phi = dyn_cast <gphi *> (def))
{
- saw_phi = true;
+ if (saw_phi_p)
+ *saw_phi_p = true;
unsigned int n = gimple_phi_num_args (phi);
for (unsigned int i = 0; i < n; i++)
{
tree arg = gimple_phi_arg_def (phi, i);
- if (null_invalid_p && integer_zerop (arg))
+ if (allow_null && integer_zerop (arg))
continue;
- if (TREE_CODE (arg) != SSA_NAME)
+ if (TREE_CODE (arg) == SSA_NAME)
+ {
+ if (!visited.add (SSA_NAME_VERSION (arg)))
+ worklist.safe_push (arg);
+ }
+ else if (!push_more (phi, arg))
return false;
- if (!visited.add (SSA_NAME_VERSION (arg)))
- worklist.safe_push (arg);
}
+ continue;
}
else if (is_gimple_assign (def))
{
@@ -1384,41 +1361,158 @@ ipa_load_from_parm_agg_unknown_offset (struct
ipa_func_body_info *fbi,
&& POINTER_TYPE_P (TREE_TYPE (ptr))
&& POINTER_TYPE_P (TREE_TYPE (rhs1))))
{
- if (null_invalid_p && integer_zerop (rhs1))
+ if (allow_null && integer_zerop (rhs1))
continue;
- if (TREE_CODE (rhs1) != SSA_NAME)
- return false;
- if (!visited.add (SSA_NAME_VERSION (rhs1)))
- worklist.safe_push (rhs1);
+ push_expr = rhs1;
+ leaf_stmt = def;
}
else if (rhs_code == ADDR_EXPR)
{
tree inner = get_base_address (TREE_OPERAND (rhs1, 0));
- if (!inner || TREE_CODE (inner) != MEM_REF)
- return false;
- tree next = TREE_OPERAND (inner, 0);
- if (null_invalid_p && integer_zerop (next))
- continue;
- if (TREE_CODE (next) != SSA_NAME)
- return false;
- if (!visited.add (SSA_NAME_VERSION (next)))
- worklist.safe_push (next);
+ if (inner && TREE_CODE (inner) == MEM_REF)
+ {
+ tree next = TREE_OPERAND (inner, 0);
+ if (allow_null && integer_zerop (next))
+ continue;
+ if (TREE_CODE (next) == SSA_NAME)
+ {
+ if (!visited.add (SSA_NAME_VERSION (next)))
+ worklist.safe_push (next);
+ continue;
+ }
+ }
+ push_expr = rhs1;
+ leaf_stmt = def;
}
else
{
- int index = load_from_unmodified_param (fbi, descriptors, def);
- if (index < 0
- || (found_index != -1 && found_index != index))
- return false;
- found_index = index;
- parm_ptr = ptr;
+ push_expr = ptr;
+ leaf_stmt = def;
}
}
else
+ {
+ push_expr = ptr;
+ leaf_stmt = def;
+ }
+
+ if (push_expr)
+ {
+ if (TREE_CODE (push_expr) == SSA_NAME && push_expr != ptr)
+ {
+ if (!visited.add (SSA_NAME_VERSION (push_expr)))
+ worklist.safe_push (push_expr);
+ }
+ else if (!push_more (leaf_stmt, push_expr))
+ return false;
+ }
+ }
+
+ return true;
+}
+
+namespace {
+
+struct ipa_unknown_off_parm_data
+{
+ struct ipa_func_body_info *fbi;
+ vec<ipa_param_descriptor, va_gc> *descriptors;
+ int found_index;
+ tree parm_ptr;
+};
+
+/* 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. */
+
+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;
+ tree ptr = NULL_TREE;
+
+ if (!stmt && TREE_CODE (expr) == SSA_NAME && SSA_NAME_IS_DEFAULT_DEF (expr))
+ {
+ tree parm = SSA_NAME_VAR (expr);
+ if (!parm || TREE_CODE (parm) != PARM_DECL)
return false;
+ index = ipa_get_param_decl_index_1 (d->descriptors, parm);
+ ptr = expr;
}
+ else if (stmt && is_gimple_assign (stmt) && TREE_CODE (expr) == SSA_NAME)
+ {
+ index = load_from_unmodified_param (d->fbi, d->descriptors, stmt);
+ ptr = expr;
+ }
+ else
+ return false;
+
+ if (index < 0 || (d->found_index != -1 && d->found_index != index))
+ return false;
+
+ d->found_index = index;
+ d->parm_ptr = ptr;
+ return true;
+}
+
+} // anonymous namespace
+
+/* 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
+ *UNKNOWN_OFFSET_P to true. */
+
+static bool
+ipa_load_from_parm_agg_unknown_offset (struct ipa_func_body_info *fbi,
+ vec<ipa_param_descriptor, va_gc>
+ *descriptors,
+ gimple *stmt, tree op, int *index_p,
+ HOST_WIDE_INT *offset_p,
+ poly_int64 *size_p, bool *by_ref_p,
+ bool *guaranteed_unmodified,
+ bool *unknown_offset_p)
+{
+ /* Currently we only work on STRING_CST when evaluating unknown-offset
+ conditions, so we do not record other cases (such as by-value
+ aggregates or general constructors).
+ TODO: Support general constructors. */
+ tree type = TREE_TYPE (op);
+ if (TREE_CODE (type) != INTEGER_TYPE
+ || TYPE_PRECISION (type) != BITS_PER_UNIT)
+ return false;
+
+ tree base = get_base_address (op);
+ if (!base || TREE_CODE (base) != MEM_REF)
+ return false;
+
+ tree start_ptr = TREE_OPERAND (base, 0);
+ if (TREE_CODE (start_ptr) != SSA_NAME)
+ return false;
+
+ 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 (TREE_TYPE (base))));
+
+ /* Since BASE is dereferenced at STMT and POINTER_PLUS_EXPR,
+ same-address-space conversions and PHIs preserve null provenance,
+ 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};
+ bool saw_phi = false;
+ if (!ipa_walk_ptr_adjustments (start_ptr, null_invalid_p,
+ ipa_unknown_off_parm_leaf, &d, &saw_phi))
+ return false;
- if (found_index < 0)
+ if (d.found_index < 0)
return false;
/* When a PHI was crossed (for instance a pointer induction variable in a
@@ -1429,13 +1523,13 @@ ipa_load_from_parm_agg_unknown_offset (struct
ipa_func_body_info *fbi,
bool data_preserved
= (gimple_vuse (stmt)
? (saw_phi
- ? parm_ref_data_pass_through_p (fbi, found_index, stmt, parm_ptr)
- : parm_ref_data_preserved_p (fbi, found_index, stmt, op))
+ ? parm_ref_data_pass_through_p (fbi, d.found_index, stmt, d.parm_ptr)
+ : parm_ref_data_preserved_p (fbi, d.found_index, stmt, op))
: false);
if (!data_preserved && !guaranteed_unmodified)
return false;
- *index_p = found_index;
+ *index_p = d.found_index;
*offset_p = -1;
*by_ref_p = true;
if (size_p)
diff --git a/gcc/ipa-prop.h b/gcc/ipa-prop.h
index 627753ce7e8..5ff9ab84209 100644
--- a/gcc/ipa-prop.h
+++ b/gcc/ipa-prop.h
@@ -1226,6 +1226,18 @@ bool ipa_load_from_parm_agg (struct ipa_func_body_info
*fbi,
bool *by_ref, bool *guaranteed_unmodified = NULL,
bool *unknown_offset_p = NULL);
+/* Callback used by ipa_walk_ptr_adjustments when reaching a pointer leaf.
+ STMT is NULL when EXPR is a default-definition SSA_NAME, or the defining
+ statement otherwise (for non-SSA PHI/copy/ADDR_EXPR operands, STMT is the
+ using PHI or assign statement). Implementations may push additional
+ SSA_NAMEs to traverse onto MORE_PTRS. */
+typedef bool (*ipa_ptr_leaf_fn) (gimple *stmt, tree expr,
+ vec<tree> &more_ptrs, void *data);
+
+bool ipa_walk_ptr_adjustments (tree start_ptr, bool allow_null,
+ ipa_ptr_leaf_fn leaf_cb, void *data,
+ bool *saw_phi_p = NULL);
+
/* Debugging interface. */
void ipa_print_node_params (FILE *, struct cgraph_node *node);
void ipa_print_all_params (FILE *);