From 8b9a92f794b8ad8011e6beb11a609efa635c4600 Mon Sep 17 00:00:00 2001 From: Strager Neds Date: Tue, 10 Nov 2020 11:42:01 -0700 Subject: [PATCH] Refactor copying decl section names gcc/ * cgraph.h (symtab_node::get_section): Constify. (symtab_node::set_section): Declare new overload. * symtab.c (symtab_node::set_section): Define new overload. (symtab_node::copy_visibility_from): Use new overload of symtab_node::set_section. (symtab_node::resolve_alias): Same. * tree.h (set_decl_section_name): Declare new overload. * tree.c (set_decl_section_name): Define new overload. * tree-emutls.c (get_emutls_init_templ_addr): Same. * cgraphclones.c (cgraph_node::create_virtual_clone): Use new overload of symtab_node::set_section. (cgraph_node::create_version_clone_with_body): Same. * trans-mem.c (ipa_tm_create_version): Same. gcc/c * c-decl.c (merge_decls): Use new overload of set_decl_section_name. gcc/cp * decl.c (duplicate_decls): Use new overload of set_decl_section_name. * method.c (use_thunk): Same. * optimize.c (maybe_clone_body): Same. * coroutines.cc (act_des_fn): Same. gcc/d * decl.cc (finish_thunk): Use new overload of set_decl_section_name --- gcc/c/c-decl.c | 2 +- gcc/cgraph.h | 5 ++++- gcc/cgraphclones.c | 4 ++-- gcc/cp/coroutines.cc | 2 +- gcc/cp/decl.c | 2 +- gcc/cp/method.c | 2 +- gcc/cp/optimize.c | 2 +- gcc/d/decl.cc | 2 +- gcc/symtab.c | 14 ++++++++++---- gcc/trans-mem.c | 2 +- gcc/tree-emutls.c | 2 +- gcc/tree.c | 27 +++++++++++++++++++++++++++ gcc/tree.h | 1 + 13 files changed, 52 insertions(+), 15 deletions(-) diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index f19c82c81dd..d348e39c27a 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -2884,7 +2884,7 @@ merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype) || TREE_PUBLIC (olddecl) || TREE_STATIC (olddecl)) && DECL_SECTION_NAME (newdecl) != NULL) - set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl)); + set_decl_section_name (olddecl, newdecl); /* This isn't quite correct for something like int __thread x attribute ((tls_model ("local-exec"))); diff --git a/gcc/cgraph.h b/gcc/cgraph.h index 73c37d8807d..2e62db2a33b 100644 --- a/gcc/cgraph.h +++ b/gcc/cgraph.h @@ -263,7 +263,7 @@ public: } /* Return section as string. */ - const char * get_section () + const char * get_section () const { if (!x_section) return NULL; @@ -322,6 +322,9 @@ public: /* Set section for symbol and its aliases. */ void set_section (const char *section); + /* Like set_section, but copying the section name from another node. */ + void set_section (const symtab_node &other); + /* Set section, do not recurse into aliases. When one wants to change section of symbol and its aliases, use set_section. */ diff --git a/gcc/cgraphclones.c b/gcc/cgraphclones.c index bc590819f78..2e69689025a 100644 --- a/gcc/cgraphclones.c +++ b/gcc/cgraphclones.c @@ -626,7 +626,7 @@ cgraph_node::create_virtual_clone (vec redirect_callers, if (tree_map) clone_info::get_create (new_node)->tree_map = tree_map; if (!implicit_section) - new_node->set_section (get_section ()); + new_node->set_section (*this); /* Clones of global symbols or symbols with unique names are unique. */ if ((TREE_PUBLIC (old_decl) @@ -1060,7 +1060,7 @@ cgraph_node::create_version_clone_with_body new_version_node->local = 1; new_version_node->lowered = true; if (!implicit_section) - new_version_node->set_section (get_section ()); + new_version_node->set_section (*this); /* Clones of global symbols or symbols with unique names are unique. */ if ((TREE_PUBLIC (old_decl) && !DECL_EXTERNAL (old_decl) diff --git a/gcc/cp/coroutines.cc b/gcc/cp/coroutines.cc index 9b9141e51fd..fd6cda41991 100644 --- a/gcc/cp/coroutines.cc +++ b/gcc/cp/coroutines.cc @@ -3756,7 +3756,7 @@ act_des_fn (tree orig, tree fn_type, tree coro_frame_ptr, const char* name) /* Copy selected attributes from the original function. */ TREE_USED (fn) = TREE_USED (orig); if (DECL_SECTION_NAME (orig)) - set_decl_section_name (fn, DECL_SECTION_NAME (orig)); + set_decl_section_name (fn, orig); /* Copy any alignment that the FE added. */ if (DECL_ALIGN (orig)) SET_DECL_ALIGN (fn, DECL_ALIGN (orig)); diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 62648841ac3..42e704e7af2 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -2866,7 +2866,7 @@ duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden) done later in decl_attributes since we are called before attributes are assigned. */ if (DECL_SECTION_NAME (newdecl) != NULL) - set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl)); + set_decl_section_name (olddecl, newdecl); if (DECL_ONE_ONLY (newdecl)) { diff --git a/gcc/cp/method.c b/gcc/cp/method.c index 16e76351943..4de192fac00 100644 --- a/gcc/cp/method.c +++ b/gcc/cp/method.c @@ -351,7 +351,7 @@ use_thunk (tree thunk_fndecl, bool emit_p) resolve_unique_section (thunk_fndecl, 0, flag_function_sections); /* Output the thunk into the same section as function. */ - set_decl_section_name (thunk_fndecl, DECL_SECTION_NAME (fn)); + set_decl_section_name (thunk_fndecl, fn); symtab_node::get (thunk_fndecl)->implicit_section = symtab_node::get (fn)->implicit_section; } diff --git a/gcc/cp/optimize.c b/gcc/cp/optimize.c index 00621d636bf..127d7fa30fc 100644 --- a/gcc/cp/optimize.c +++ b/gcc/cp/optimize.c @@ -512,7 +512,7 @@ maybe_clone_body (tree fn) DECL_DLLIMPORT_P (clone) = DECL_DLLIMPORT_P (fn); DECL_ATTRIBUTES (clone) = copy_list (DECL_ATTRIBUTES (fn)); DECL_DISREGARD_INLINE_LIMITS (clone) = DECL_DISREGARD_INLINE_LIMITS (fn); - set_decl_section_name (clone, DECL_SECTION_NAME (fn)); + set_decl_section_name (clone, fn); /* Adjust the parameter names and locations. */ parm = DECL_ARGUMENTS (fn); diff --git a/gcc/d/decl.cc b/gcc/d/decl.cc index 457894fbe22..d668715af59 100644 --- a/gcc/d/decl.cc +++ b/gcc/d/decl.cc @@ -1663,7 +1663,7 @@ finish_thunk (tree thunk, tree function) resolve_unique_section (thunk, 0, flag_function_sections); /* Output the thunk into the same section as function. */ - set_decl_section_name (thunk, DECL_SECTION_NAME (fn)); + set_decl_section_name (thunk, fn); symtab_node::get (thunk)->implicit_section = symtab_node::get (fn)->implicit_section; } diff --git a/gcc/symtab.c b/gcc/symtab.c index 58b14f321c5..883cc3eae0d 100644 --- a/gcc/symtab.c +++ b/gcc/symtab.c @@ -1484,8 +1484,7 @@ symtab_node::copy_visibility_from (symtab_node *n) DECL_DLLIMPORT_P (decl) = DECL_DLLIMPORT_P (n->decl); resolution = n->resolution; set_comdat_group (n->get_comdat_group ()); - call_for_symbol_and_aliases (symtab_node::set_section, - const_cast(n->get_section ()), true); + set_section (*n); externally_visible = n->externally_visible; if (!DECL_RTL_SET_P (decl)) return; @@ -1671,6 +1670,14 @@ symtab_node::set_section (const char *section) (symtab_node::set_section, const_cast(section), true); } +void +symtab_node::set_section (const symtab_node &other) +{ + const char *section = other.get_section (); + call_for_symbol_and_aliases + (symtab_node::set_section, const_cast(section), true); +} + /* Return the initialization priority. */ priority_type @@ -1814,8 +1821,7 @@ symtab_node::resolve_alias (symtab_node *target, bool transparent) { error ("section of alias %q+D must match section of its target", decl); } - call_for_symbol_and_aliases (symtab_node::set_section, - const_cast(target->get_section ()), true); + set_section (*target); if (target->implicit_section) call_for_symbol_and_aliases (set_implicit_section, NULL, true); diff --git a/gcc/trans-mem.c b/gcc/trans-mem.c index e4f264d6ae0..c078a12ef84 100644 --- a/gcc/trans-mem.c +++ b/gcc/trans-mem.c @@ -4998,7 +4998,7 @@ ipa_tm_create_version (struct cgraph_node *old_node) new_node->lowered = true; new_node->tm_clone = 1; if (!old_node->implicit_section) - new_node->set_section (old_node->get_section ()); + new_node->set_section (*old_node); get_cg_data (&old_node, true)->clone = new_node; if (old_node->get_availability () >= AVAIL_INTERPOSABLE) diff --git a/gcc/tree-emutls.c b/gcc/tree-emutls.c index 44755dd406a..5b55832688c 100644 --- a/gcc/tree-emutls.c +++ b/gcc/tree-emutls.c @@ -259,7 +259,7 @@ get_emutls_init_templ_addr (tree decl) if (targetm.emutls.tmpl_section) set_decl_section_name (to, targetm.emutls.tmpl_section); else - set_decl_section_name (to, DECL_SECTION_NAME (decl)); + set_decl_section_name (to, decl); /* Create varpool node for the new variable and finalize it if it is not external one. */ diff --git a/gcc/tree.c b/gcc/tree.c index 9260772b846..663f3ecfd12 100644 --- a/gcc/tree.c +++ b/gcc/tree.c @@ -773,6 +773,33 @@ set_decl_section_name (tree node, const char *value) snode->set_section (value); } +/* Set section name of NODE to match the section name of OTHER. + + set_decl_section_name (decl, other) is equivalent to + set_decl_section_name (decl, DECL_SECTION_NAME (other)), but possibly more + efficient. */ +void +set_decl_section_name (tree decl, const_tree other) +{ + struct symtab_node *other_node = symtab_node::get (other); + if (other_node) + { + struct symtab_node *decl_node; + if (VAR_P (decl)) + decl_node = varpool_node::get_create (decl); + else + decl_node = cgraph_node::get_create (decl); + decl_node->set_section (*other_node); + } + else + { + struct symtab_node *decl_node = symtab_node::get (decl); + if (!decl_node) + return; + decl_node->set_section (NULL); + } +} + /* Return TLS model of a variable NODE. */ enum tls_model decl_tls_model (const_tree node) diff --git a/gcc/tree.h b/gcc/tree.h index f8f0a606439..684be10b440 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -4281,6 +4281,7 @@ extern tree decl_comdat_group (const_tree); extern tree decl_comdat_group_id (const_tree); extern const char *decl_section_name (const_tree); extern void set_decl_section_name (tree, const char *); +extern void set_decl_section_name (tree, const_tree); extern enum tls_model decl_tls_model (const_tree); extern void set_decl_tls_model (tree, enum tls_model); -- 2.30.2