From a3317f7b3c02907a122f89879e5b6e90c386e64d Mon Sep 17 00:00:00 2001 From: Richard Sandiford Date: Thu, 15 Apr 2021 11:37:38 +0100 Subject: c: Don't drop vector attributes that affect type identity [PR98852] types are distinct from GNU vector types in at least their mangling. However, there used to be nothing explicit in the VECTOR_TYPE itself to indicate the difference: we simply treated them as distinct TYPE_MAIN_VARIANTs. This caused problems like the ones reported in PR95726. The fix for that PR was to add type attributes to the types, in order to maintain the distinction between them and GNU vectors. However, this in turn caused PR98852, where c_common_type would unconditionally drop the attributes on the source types. This meant that: vector + vector had a GNU vector type rather than an vector type. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96377#c2 for Jakub's analysis of the history of this c_common_type code. TBH I'm not sure which case the build_type_attribute_variant code is handling, but I think we should at least avoid dropping attributes that affect type identity. I've tried to audit the C and target-specific attributes to look for other types that might be affected by this, but I couldn't see any. We are only dealing with: gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE || code1 == INTEGER_TYPE); which excludes most affects_type_identity attributes. The closest was s390_vector_bool, but the handler for that attribute changes the type node and drops the attribute itself (*no_add_attrs = true). I put the main list handling into a separate function (remove_attributes_matching) because a later patch will need it for something else. gcc/ PR c/98852 * attribs.h (affects_type_identity_attributes): Declare. * attribs.c (remove_attributes_matching): New function. (affects_type_identity_attributes): Likewise. gcc/c/ PR c/98852 * c-typeck.c (c_common_type): Do not drop attributes that affect type identity. gcc/testsuite/ PR c/98852 * gcc.target/aarch64/advsimd-intrinsics/pr98852.c: New test. --- gcc/attribs.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'gcc/attribs.c') diff --git a/gcc/attribs.c b/gcc/attribs.c index 16c6b12..2fb2954 100644 --- a/gcc/attribs.c +++ b/gcc/attribs.c @@ -1366,6 +1366,60 @@ comp_type_attributes (const_tree type1, const_tree type2) return targetm.comp_type_attributes (type1, type2); } +/* PREDICATE acts as a function of type: + + (const_tree attr, const attribute_spec *as) -> bool + + where ATTR is an attribute and AS is its possibly-null specification. + Return a list of every attribute in attribute list ATTRS for which + PREDICATE is true. Return ATTRS itself if PREDICATE returns true + for every attribute. */ + +template +tree +remove_attributes_matching (tree attrs, Predicate predicate) +{ + tree new_attrs = NULL_TREE; + tree *ptr = &new_attrs; + const_tree start = attrs; + for (const_tree attr = attrs; attr; attr = TREE_CHAIN (attr)) + { + tree name = get_attribute_name (attr); + const attribute_spec *as = lookup_attribute_spec (name); + const_tree end; + if (!predicate (attr, as)) + end = attr; + else if (start == attrs) + continue; + else + end = TREE_CHAIN (attr); + + for (; start != end; start = TREE_CHAIN (start)) + { + *ptr = tree_cons (TREE_PURPOSE (start), + TREE_VALUE (start), NULL_TREE); + TREE_CHAIN (*ptr) = NULL_TREE; + ptr = &TREE_CHAIN (*ptr); + } + start = TREE_CHAIN (attr); + } + gcc_assert (!start || start == attrs); + return start ? attrs : new_attrs; +} + +/* If VALUE is true, return the subset of ATTRS that affect type identity, + otherwise return the subset of ATTRS that don't affect type identity. */ + +tree +affects_type_identity_attributes (tree attrs, bool value) +{ + auto predicate = [value](const_tree, const attribute_spec *as) -> bool + { + return bool (as && as->affects_type_identity) == value; + }; + return remove_attributes_matching (attrs, predicate); +} + /* Return a type like TTYPE except that its TYPE_ATTRIBUTE is ATTRIBUTE. -- cgit v1.1 From 1696fc1ea01d5c9dce96b5d3122921aab9308f59 Mon Sep 17 00:00:00 2001 From: Richard Sandiford Date: Thu, 15 Apr 2021 11:37:39 +0100 Subject: c++: Tweak merging of vector attributes that affect type identity [PR98852] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit types are distinct from GNU vector types in at least their mangling. However, there used to be nothing explicit in the VECTOR_TYPE itself to indicate the difference: we simply treated them as distinct TYPE_MAIN_VARIANTs. This caused problems like the ones reported in PR95726. The fix for that PR was to add type attributes to the types, in order to maintain the distinction between them and GNU vectors. However, this in turn caused PR98852, where cp_common_type would merge the type attributes from the two source types and attach the result to the common type. For example: unsigned vector with no attribute + signed vector with attribute X would get converted to: unsigned vector with attribute X That isn't what we want in this case, since X describes the mangling of the original type. But even if we dropped the mangling from X and worked it out from context, we would still have a situation in which the common type was provably distinct from both of the source types: it would take its -ness from one side and its signedness from the other. I guess there are other cases where the common type doesn't match either side, but I'm not sure it's the obvious behaviour here. It's also different from GCC 10.1 and earlier, where the unsigned vector “won” in its original form. This patch instead merges only the attributes that don't affect type identity. For now I've restricted it to vector types, since we're so close to GCC 11, but it might make sense to use this elsewhere. I've tried to audit the C and target-specific attributes to look for other types that might be affected by this, but I couldn't see any. The closest was s390_vector_bool, but the handler for that attribute changes the type node and drops the attribute itself (*no_add_attrs = true). gcc/ PR c++/98852 * attribs.h (restrict_type_identity_attributes_to): Declare. * attribs.c (restrict_type_identity_attributes_to): New function. gcc/cp/ PR c++/98852 * typeck.c (merge_type_attributes_from): New function. (cp_common_type): Use it for vector types. --- gcc/attribs.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'gcc/attribs.c') diff --git a/gcc/attribs.c b/gcc/attribs.c index 2fb2954..3ffa1b6 100644 --- a/gcc/attribs.c +++ b/gcc/attribs.c @@ -1420,6 +1420,29 @@ affects_type_identity_attributes (tree attrs, bool value) return remove_attributes_matching (attrs, predicate); } +/* Remove attributes that affect type identity from ATTRS unless the + same attributes occur in OK_ATTRS. */ + +tree +restrict_type_identity_attributes_to (tree attrs, tree ok_attrs) +{ + auto predicate = [ok_attrs](const_tree attr, + const attribute_spec *as) -> bool + { + if (!as || !as->affects_type_identity) + return true; + + for (tree ok_attr = lookup_attribute (as->name, ok_attrs); + ok_attr; + ok_attr = lookup_attribute (as->name, TREE_CHAIN (ok_attr))) + if (simple_cst_equal (TREE_VALUE (ok_attr), TREE_VALUE (attr)) == 1) + return true; + + return false; + }; + return remove_attributes_matching (attrs, predicate); +} + /* Return a type like TTYPE except that its TYPE_ATTRIBUTE is ATTRIBUTE. -- cgit v1.1 From 2c8bffa184dffba7976ba807ef0a1bbb6f66aa2d Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Wed, 28 Apr 2021 19:11:34 -0600 Subject: PR middle-end/100250 - ICE related to -Wmaybe-uninitialized gcc/ChangeLog: PR middle-end/100250 * attribs.c (attr_access::array_as_string): Avoid dereferencing a pointer when it's null. gcc/testsuite/ChangeLog: PR middle-end/100250 * gcc.dg/uninit-pr100250.c: New test. --- gcc/attribs.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'gcc/attribs.c') diff --git a/gcc/attribs.c b/gcc/attribs.c index 3ffa1b6..ebc0783 100644 --- a/gcc/attribs.c +++ b/gcc/attribs.c @@ -2388,7 +2388,8 @@ attr_access::array_as_string (tree type) const const char *p = end; for ( ; p != str && *p-- != ']'; ); if (*p == '$') - index_type = build_index_type (TREE_VALUE (size)); + /* SIZE may have been cleared. Use it with care. */ + index_type = build_index_type (size ? TREE_VALUE (size) : size); } else if (minsize) index_type = build_index_type (size_int (minsize - 1)); -- cgit v1.1 From c6503fa93b5565c922f76611a55b0a53cd940a5f Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Fri, 4 Jun 2021 10:35:27 -0600 Subject: PR c/100719 - missing -Wvla-parameter on a mismatch in second parameter gcc/ChangeLog: * attribs.c (init_attr_rdwr_indices): Use VLA bounds in the expected order. (attr_access::vla_bounds): Also handle VLA bounds. gcc/c-family/ChangeLog: * c-warn.c (warn_parm_array_mismatch): Check TREE_PURPOSE to test for element presence. gcc/testsuite/ChangeLog: * gcc.dg/Wvla-parameter-10.c: New test. * gcc.dg/Wvla-parameter-11.c: New test. --- gcc/attribs.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'gcc/attribs.c') diff --git a/gcc/attribs.c b/gcc/attribs.c index ebc0783..70e0a2f 100644 --- a/gcc/attribs.c +++ b/gcc/attribs.c @@ -2126,14 +2126,14 @@ init_attr_rdwr_indices (rdwr_map *rwm, tree attrs) /* The (optional) list of VLA bounds. */ tree vblist = TREE_CHAIN (mode); - if (vblist) - vblist = TREE_VALUE (vblist); - mode = TREE_VALUE (mode); if (TREE_CODE (mode) != STRING_CST) continue; gcc_assert (TREE_CODE (mode) == STRING_CST); + if (vblist) + vblist = nreverse (copy_list (TREE_VALUE (vblist))); + for (const char *m = TREE_STRING_POINTER (mode); *m; ) { attr_access acc = { }; @@ -2308,11 +2308,18 @@ attr_access::to_external_string () const unsigned attr_access::vla_bounds (unsigned *nunspec) const { + unsigned nbounds = 0; *nunspec = 0; - for (const char* p = strrchr (str, ']'); p && *p != '['; --p) - if (*p == '*') - ++*nunspec; - return list_length (size); + /* STR points to the beginning of the specified string for the current + argument that may be followed by the string for the next argument. */ + for (const char* p = strchr (str, ']'); p && *p != '['; --p) + { + if (*p == '*') + ++*nunspec; + else if (*p == '$') + ++nbounds; + } + return nbounds; } /* Reset front end-specific attribute access data from ATTRS. -- cgit v1.1 From 3f207ab314c071c6060c7c9a429fcf29fd87b594 Mon Sep 17 00:00:00 2001 From: Trevor Saunders Date: Fri, 11 Jun 2021 23:49:22 -0400 Subject: use range based for loops to iterate over vec<> This changes users of FOR_EACH_VEC_ELT to use range based for loops, where the index variables are otherwise unused. As such the index variables are all deleted, producing shorter and simpler code. Signed-off-by: Trevor Saunders gcc/analyzer/ChangeLog: * call-string.cc (call_string::call_string): Use range based for to iterate over vec<>. (call_string::to_json): Likewise. (call_string::hash): Likewise. (call_string::calc_recursion_depth): Likewise. * checker-path.cc (checker_path::fixup_locations): Likewise. * constraint-manager.cc (equiv_class::equiv_class): Likewise. (equiv_class::to_json): Likewise. (equiv_class::hash): Likewise. (constraint_manager::to_json): Likewise. * engine.cc (impl_region_model_context::on_svalue_leak): Likewise. (on_liveness_change): Likewise. (impl_region_model_context::on_unknown_change): Likewise. * program-state.cc (sm_state_map::set_state): Likewise. * region-model.cc (test_canonicalization_4): Likewise. gcc/ChangeLog: * attribs.c (find_attribute_namespace): Iterate over vec<> with range based for. * auto-profile.c (afdo_find_equiv_class): Likewise. * gcc.c (do_specs_vec): Likewise. (do_spec_1): Likewise. (driver::set_up_specs): Likewise. * gimple-loop-jam.c (any_access_function_variant_p): Likewise. * gimple-ssa-store-merging.c (compatible_load_p): Likewise. (imm_store_chain_info::try_coalesce_bswap): Likewise. (imm_store_chain_info::coalesce_immediate_stores): Likewise. (get_location_for_stmts): Likewise. * graphite-poly.c (print_iteration_domains): Likewise. (free_poly_bb): Likewise. (remove_gbbs_in_scop): Likewise. (free_scop): Likewise. (dump_gbb_cases): Likewise. (dump_gbb_conditions): Likewise. (print_pdrs): Likewise. (print_scop): Likewise. * ifcvt.c (cond_move_process_if_block): Likewise. * lower-subreg.c (decompose_multiword_subregs): Likewise. * regcprop.c (pass_cprop_hardreg::execute): Likewise. * sanopt.c (sanitize_rewrite_addressable_params): Likewise. * sel-sched-dump.c (dump_insn_vector): Likewise. * store-motion.c (store_ops_ok): Likewise. (store_killed_in_insn): Likewise. * timevar.c (timer::named_items::print): Likewise. * tree-cfgcleanup.c (cleanup_control_flow_pre): Likewise. (cleanup_tree_cfg_noloop): Likewise. * tree-data-ref.c (dump_data_references): Likewise. (print_dir_vectors): Likewise. (print_dist_vectors): Likewise. (dump_data_dependence_relations): Likewise. (dump_dist_dir_vectors): Likewise. (dump_ddrs): Likewise. (create_runtime_alias_checks): Likewise. (free_subscripts): Likewise. (save_dist_v): Likewise. (save_dir_v): Likewise. (invariant_access_functions): Likewise. (same_access_functions): Likewise. (access_functions_are_affine_or_constant_p): Likewise. (find_data_references_in_stmt): Likewise. (graphite_find_data_references_in_stmt): Likewise. (free_dependence_relations): Likewise. (free_data_refs): Likewise. * tree-inline.c (copy_debug_stmts): Likewise. * tree-into-ssa.c (dump_currdefs): Likewise. (rewrite_update_phi_arguments): Likewise. * tree-ssa-propagate.c (clean_up_loop_closed_phi): Likewise. * tree-vect-data-refs.c (vect_analyze_possibly_independent_ddr): Likewise. (vect_slp_analyze_node_dependences): Likewise. (vect_slp_analyze_instance_dependence): Likewise. (vect_record_base_alignments): Likewise. (vect_get_peeling_costs_all_drs): Likewise. (vect_peeling_supportable): Likewise. * tree-vectorizer.c (vec_info::~vec_info): Likewise. (vec_info::free_stmt_vec_infos): Likewise. gcc/cp/ChangeLog: * constexpr.c (cxx_eval_call_expression): Iterate over vec<> with range based for. (cxx_eval_store_expression): Likewise. (cxx_eval_loop_expr): Likewise. * decl.c (wrapup_namespace_globals): Likewise. (cp_finish_decl): Likewise. (cxx_simulate_enum_decl): Likewise. * parser.c (cp_parser_postfix_expression): Likewise. --- gcc/attribs.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'gcc/attribs.c') diff --git a/gcc/attribs.c b/gcc/attribs.c index 70e0a2f..afa485e 100644 --- a/gcc/attribs.c +++ b/gcc/attribs.c @@ -165,15 +165,12 @@ register_scoped_attributes (const struct attribute_spec *attributes, static scoped_attributes* find_attribute_namespace (const char* ns) { - unsigned ix; - scoped_attributes *iter; - - FOR_EACH_VEC_ELT (attributes_table, ix, iter) - if (ns == iter->ns - || (iter->ns != NULL + for (scoped_attributes &iter : attributes_table) + if (ns == iter.ns + || (iter.ns != NULL && ns != NULL - && !strcmp (iter->ns, ns))) - return iter; + && !strcmp (iter.ns, ns))) + return &iter; return NULL; } -- cgit v1.1 From 4998404915bba9cb04c438a926cdf7126782a767 Mon Sep 17 00:00:00 2001 From: Martin Liska Date: Thu, 12 Aug 2021 17:26:51 +0200 Subject: ipa: "naked" attribute implies "noipa" attribute PR ipa/101354 gcc/ChangeLog: * attribs.c (decl_attributes): Make naked functions "noipa" functions. --- gcc/attribs.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'gcc/attribs.c') diff --git a/gcc/attribs.c b/gcc/attribs.c index afa485e..0d22c20 100644 --- a/gcc/attribs.c +++ b/gcc/attribs.c @@ -517,14 +517,9 @@ decl_attributes (tree *node, tree attributes, int flags, if (TREE_CODE (*node) == FUNCTION_DECL && attributes && lookup_attribute ("naked", attributes) != NULL - && lookup_attribute_spec (get_identifier ("naked"))) - { - if (lookup_attribute ("noinline", attributes) == NULL) - attributes = tree_cons (get_identifier ("noinline"), NULL, attributes); - - if (lookup_attribute ("noclone", attributes) == NULL) - attributes = tree_cons (get_identifier ("noclone"), NULL, attributes); - } + && lookup_attribute_spec (get_identifier ("naked")) + && lookup_attribute ("noipa", attributes) == NULL) + attributes = tree_cons (get_identifier ("noipa"), NULL, attributes); /* A "noipa" function attribute implies "noinline", "noclone" and "no_icf" for those targets that support it. */ -- cgit v1.1