This moves looking at a functions fnspec to decide whether parameters point to read-only memory and do alias disambiugations based on that from a "hack" in VN to generic alias code.
This is to avoid regressions with a fix for PR87132 but looks useful generally. Bootstrap & regtest running on x86_64-unknown-linux-gnu. Richard. 2018-08-29 Richard Biener <rguent...@suse.de> * tree-core.h: Document use of deprecated_flag in SSA_NAME. * tree.h (SSA_NAME_POINTS_TO_READONLY_MEMORY): Define. * tree-into-ssa.c (pass_build_ssa::execute): Initialize function parameters SSA_NAME_POINTS_TO_READONLY_MEMORY from fnspec. * tree-ssa-sccvn.c (const_parms, init_const_parms): Remove. (vn_reference_lookup_3): Remove use of const_parms. (free_rpo_vn): Do not free const_parms. (do_rpo_vn): Do not call init_const_parms. * tree-ssa-alias.c (refs_may_alias_p_1): Honor SSA_NAME_POINTS_TO_READONLY_MEMORY. (call_may_clobber_ref_p_1): Likewise. Index: gcc/tree-core.h =================================================================== --- gcc/tree-core.h (revision 263944) +++ gcc/tree-core.h (working copy) @@ -1238,6 +1238,9 @@ struct GTY(()) tree_base { IDENTIFIER_TRANSPARENT_ALIAS in IDENTIFIER_NODE + SSA_NAME_POINTS_TO_READONLY_MEMORY in + SSA_NAME + visited: TREE_VISITED in Index: gcc/tree.h =================================================================== --- gcc/tree.h (revision 263944) +++ gcc/tree.h (working copy) @@ -1743,6 +1743,13 @@ extern tree maybe_wrap_with_location (tr #define SSA_NAME_IS_DEFAULT_DEF(NODE) \ SSA_NAME_CHECK (NODE)->base.default_def_flag +/* Nonzero if this SSA_NAME is known to point to memory that may not + be written to. This is set for default defs of function parameters + that have a corresponding r or R specification in the functions + fn spec attribute. This is used by alias analysis. */ +#define SSA_NAME_POINTS_TO_READONLY_MEMORY(NODE) \ + SSA_NAME_CHECK (NODE)->base.deprecated_flag + /* Attributes for SSA_NAMEs for pointer-type variables. */ #define SSA_NAME_PTR_INFO(N) \ SSA_NAME_CHECK (N)->ssa_name.info.ptr_info Index: gcc/tree-into-ssa.c =================================================================== --- gcc/tree-into-ssa.c (revision 263944) +++ gcc/tree-into-ssa.c (working copy) @@ -2490,6 +2490,28 @@ pass_build_ssa::execute (function *fun) SET_SSA_NAME_VAR_OR_IDENTIFIER (name, DECL_NAME (decl)); } + /* Initialize SSA_NAME_POINTS_TO_READONLY_MEMORY. */ + tree fnspec = lookup_attribute ("fn spec", + TYPE_ATTRIBUTES (TREE_TYPE (fun->decl))); + if (fnspec) + { + fnspec = TREE_VALUE (TREE_VALUE (fnspec)); + unsigned i = 1; + for (tree arg = DECL_ARGUMENTS (cfun->decl); + arg; arg = DECL_CHAIN (arg), ++i) + { + if (i >= (unsigned) TREE_STRING_LENGTH (fnspec)) + break; + if (TREE_STRING_POINTER (fnspec)[i] == 'R' + || TREE_STRING_POINTER (fnspec)[i] == 'r') + { + tree name = ssa_default_def (fun, arg); + if (name) + SSA_NAME_POINTS_TO_READONLY_MEMORY (name) = 1; + } + } + } + return 0; } Index: gcc/tree-ssa-sccvn.c =================================================================== --- gcc/tree-ssa-sccvn.c (revision 263945) +++ gcc/tree-ssa-sccvn.c (working copy) @@ -133,7 +133,6 @@ along with GCC; see the file COPYING3. static tree *last_vuse_ptr; static vn_lookup_kind vn_walk_kind; static vn_lookup_kind default_vn_walk_kind; -bitmap const_parms; /* vn_nary_op hashtable helpers. */ @@ -1863,18 +1864,6 @@ vn_reference_lookup_3 (ao_ref *ref, tree bool lhs_ref_ok = false; poly_int64 copy_size; - /* If the reference is based on a parameter that was determined as - pointing to readonly memory it doesn't change. */ - if (TREE_CODE (base) == MEM_REF - && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME - && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)) - && bitmap_bit_p (const_parms, - SSA_NAME_VERSION (TREE_OPERAND (base, 0)))) - { - *disambiguate_only = true; - return NULL; - } - /* First try to disambiguate after value-replacing in the definitions LHS. */ if (is_gimple_assign (def_stmt)) { @@ -4514,37 +4503,6 @@ set_hashtable_value_ids (void) set_value_id_for_result (vr->result, &vr->value_id); } - -/* Allocate and initialize CONST_PARAMS, a bitmap of parameter default defs - we know point to readonly memory. */ - -static void -init_const_parms () -{ - /* Collect pointers we know point to readonly memory. */ - const_parms = BITMAP_ALLOC (NULL); - tree fnspec = lookup_attribute ("fn spec", - TYPE_ATTRIBUTES (TREE_TYPE (cfun->decl))); - if (fnspec) - { - fnspec = TREE_VALUE (TREE_VALUE (fnspec)); - unsigned i = 1; - for (tree arg = DECL_ARGUMENTS (cfun->decl); - arg; arg = DECL_CHAIN (arg), ++i) - { - if (i >= (unsigned) TREE_STRING_LENGTH (fnspec)) - break; - if (TREE_STRING_POINTER (fnspec)[i] == 'R' - || TREE_STRING_POINTER (fnspec)[i] == 'r') - { - tree name = ssa_default_def (cfun, arg); - if (name) - bitmap_set_bit (const_parms, SSA_NAME_VERSION (name)); - } - } - } -} - /* Return the maximum value id we have ever seen. */ unsigned int @@ -5606,8 +5564,6 @@ free_rpo_vn (void) obstack_free (&vn_tables_obstack, NULL); obstack_free (&vn_tables_insert_obstack, NULL); - BITMAP_FREE (const_parms); - vn_ssa_aux_iterator_type it; vn_ssa_aux_t info; FOR_EACH_HASH_TABLE_ELEMENT (*vn_ssa_aux_hash, info, vn_ssa_aux_t, it) @@ -6326,7 +6277,6 @@ do_rpo_vn (function *fn, edge entry, bit unsigned region_size = (((unsigned HOST_WIDE_INT)n * num_ssa_names) / (n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS)); VN_TOP = create_tmp_var_raw (void_type_node, "vn_top"); - init_const_parms (); vn_ssa_aux_hash = new hash_table <vn_ssa_aux_hasher> (region_size * 2); gcc_obstack_init (&vn_ssa_aux_obstack); Index: gcc/tree-ssa-alias.c =================================================================== --- gcc/tree-ssa-alias.c (revision 263944) +++ gcc/tree-ssa-alias.c (working copy) @@ -1483,6 +1483,16 @@ refs_may_alias_p_1 (ao_ref *ref1, ao_ref ao_ref_alias_set (ref2))) return false; + /* If the reference is based on a pointer that points to memory + that may not be written to then the other reference cannot possibly + clobber it. */ + if ((TREE_CODE (TREE_OPERAND (base2, 0)) == SSA_NAME + && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base2, 0))) + || (ind1_p + && TREE_CODE (TREE_OPERAND (base1, 0)) == SSA_NAME + && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base1, 0)))) + return false; + /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */ if (var1_p && ind2_p) return indirect_ref_may_alias_decl_p (ref2->ref, base2, @@ -1991,6 +2001,14 @@ call_may_clobber_ref_p_1 (gcall *call, a || !is_global_var (base))) return false; + /* If the reference is based on a pointer that points to memory + that may not be written to then the call cannot possibly clobber it. */ + if ((TREE_CODE (base) == MEM_REF + || TREE_CODE (base) == TARGET_MEM_REF) + && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME + && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base, 0))) + return false; + callee = gimple_call_fndecl (call); /* Handle those builtin functions explicitly that do not act as