aix: Fix _STDC_FORMAT_MACROS in inttypes.h [PR97044]
[official-gcc.git] / gcc / symtab.c
blob207c9e75890c0e39df94aedb4de26eba0803bfae
1 /* Symbol table.
2 Copyright (C) 2012-2020 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "timevar.h"
30 #include "cgraph.h"
31 #include "lto-streamer.h"
32 #include "print-tree.h"
33 #include "varasm.h"
34 #include "langhooks.h"
35 #include "output.h"
36 #include "ipa-utils.h"
37 #include "calls.h"
38 #include "stringpool.h"
39 #include "attribs.h"
40 #include "builtins.h"
42 static const char *ipa_ref_use_name[] = {"read","write","addr","alias"};
44 const char * const ld_plugin_symbol_resolution_names[]=
46 "",
47 "undef",
48 "prevailing_def",
49 "prevailing_def_ironly",
50 "preempted_reg",
51 "preempted_ir",
52 "resolved_ir",
53 "resolved_exec",
54 "resolved_dyn",
55 "prevailing_def_ironly_exp"
58 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at ALIAS
59 until we find an identifier that is not itself a transparent alias. */
61 static inline tree
62 ultimate_transparent_alias_target (tree alias)
64 tree target = alias;
66 while (IDENTIFIER_TRANSPARENT_ALIAS (target))
68 gcc_checking_assert (TREE_CHAIN (target));
69 target = TREE_CHAIN (target);
71 gcc_checking_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
72 && ! TREE_CHAIN (target));
74 return target;
78 /* Hash asmnames ignoring the user specified marks. */
80 hashval_t
81 symbol_table::decl_assembler_name_hash (const_tree asmname)
83 if (IDENTIFIER_POINTER (asmname)[0] == '*')
85 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
86 size_t ulp_len = strlen (user_label_prefix);
88 if (ulp_len == 0)
90 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
91 decl_str += ulp_len;
93 return htab_hash_string (decl_str);
96 return htab_hash_string (IDENTIFIER_POINTER (asmname));
99 /* Return true if assembler names NAME1 and NAME2 leads to the same symbol
100 name. */
102 bool
103 symbol_table::assembler_names_equal_p (const char *name1, const char *name2)
105 if (name1 != name2)
107 if (name1[0] == '*')
109 size_t ulp_len = strlen (user_label_prefix);
111 name1 ++;
113 if (ulp_len == 0)
115 else if (strncmp (name1, user_label_prefix, ulp_len) == 0)
116 name1 += ulp_len;
117 else
118 return false;
120 if (name2[0] == '*')
122 size_t ulp_len = strlen (user_label_prefix);
124 name2 ++;
126 if (ulp_len == 0)
128 else if (strncmp (name2, user_label_prefix, ulp_len) == 0)
129 name2 += ulp_len;
130 else
131 return false;
133 return !strcmp (name1, name2);
135 return true;
138 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
140 bool
141 symbol_table::decl_assembler_name_equal (tree decl, const_tree asmname)
143 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
144 const char *decl_str;
145 const char *asmname_str;
147 if (decl_asmname == asmname)
148 return true;
150 decl_str = IDENTIFIER_POINTER (decl_asmname);
151 asmname_str = IDENTIFIER_POINTER (asmname);
152 return assembler_names_equal_p (decl_str, asmname_str);
156 /* Returns nonzero if P1 and P2 are equal. */
158 /* Insert NODE to assembler name hash. */
160 void
161 symbol_table::insert_to_assembler_name_hash (symtab_node *node,
162 bool with_clones)
164 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
165 return;
166 gcc_checking_assert (!node->previous_sharing_asm_name
167 && !node->next_sharing_asm_name);
168 if (assembler_name_hash)
170 symtab_node **aslot;
171 cgraph_node *cnode;
172 tree decl = node->decl;
174 tree name = DECL_ASSEMBLER_NAME (node->decl);
176 /* C++ FE can produce decls without associated assembler name and insert
177 them to symtab to hold section or TLS information. */
178 if (!name)
179 return;
181 hashval_t hash = decl_assembler_name_hash (name);
182 aslot = assembler_name_hash->find_slot_with_hash (name, hash, INSERT);
183 gcc_assert (*aslot != node);
184 node->next_sharing_asm_name = (symtab_node *)*aslot;
185 if (*aslot != NULL)
186 (*aslot)->previous_sharing_asm_name = node;
187 *aslot = node;
189 /* Update also possible inline clones sharing a decl. */
190 cnode = dyn_cast <cgraph_node *> (node);
191 if (cnode && cnode->clones && with_clones)
192 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
193 if (cnode->decl == decl)
194 insert_to_assembler_name_hash (cnode, true);
199 /* Remove NODE from assembler name hash. */
201 void
202 symbol_table::unlink_from_assembler_name_hash (symtab_node *node,
203 bool with_clones)
205 if (assembler_name_hash)
207 cgraph_node *cnode;
208 tree decl = node->decl;
210 if (node->next_sharing_asm_name)
211 node->next_sharing_asm_name->previous_sharing_asm_name
212 = node->previous_sharing_asm_name;
213 if (node->previous_sharing_asm_name)
215 node->previous_sharing_asm_name->next_sharing_asm_name
216 = node->next_sharing_asm_name;
218 else
220 tree name = DECL_ASSEMBLER_NAME (node->decl);
221 symtab_node **slot;
223 if (!name)
224 return;
226 hashval_t hash = decl_assembler_name_hash (name);
227 slot = assembler_name_hash->find_slot_with_hash (name, hash,
228 NO_INSERT);
229 gcc_assert (*slot == node);
230 if (!node->next_sharing_asm_name)
231 assembler_name_hash->clear_slot (slot);
232 else
233 *slot = node->next_sharing_asm_name;
235 node->next_sharing_asm_name = NULL;
236 node->previous_sharing_asm_name = NULL;
238 /* Update also possible inline clones sharing a decl. */
239 cnode = dyn_cast <cgraph_node *> (node);
240 if (cnode && cnode->clones && with_clones)
241 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
242 if (cnode->decl == decl)
243 unlink_from_assembler_name_hash (cnode, true);
247 /* Arrange node to be first in its entry of assembler_name_hash. */
249 void
250 symbol_table::symtab_prevail_in_asm_name_hash (symtab_node *node)
252 unlink_from_assembler_name_hash (node, false);
253 insert_to_assembler_name_hash (node, false);
256 /* Initialize asm name hash unless. */
258 void
259 symbol_table::symtab_initialize_asm_name_hash (void)
261 symtab_node *node;
262 if (!assembler_name_hash)
264 assembler_name_hash = hash_table<asmname_hasher>::create_ggc (10);
265 FOR_EACH_SYMBOL (node)
266 insert_to_assembler_name_hash (node, false);
270 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
272 void
273 symbol_table::change_decl_assembler_name (tree decl, tree name)
275 symtab_node *node = NULL;
277 /* We can have user ASM names on things, like global register variables, that
278 are not in the symbol table. */
279 if ((VAR_P (decl) && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
280 || TREE_CODE (decl) == FUNCTION_DECL)
281 node = symtab_node::get (decl);
282 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
284 SET_DECL_ASSEMBLER_NAME (decl, name);
285 if (node)
286 insert_to_assembler_name_hash (node, true);
288 else
290 if (name == DECL_ASSEMBLER_NAME (decl))
291 return;
293 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
294 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
295 : NULL);
296 if (node)
297 unlink_from_assembler_name_hash (node, true);
299 const char *old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
300 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
301 && DECL_RTL_SET_P (decl))
302 warning (0, "%qD renamed after being referenced in assembly", decl);
304 SET_DECL_ASSEMBLER_NAME (decl, name);
305 if (alias)
307 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
308 TREE_CHAIN (name) = alias;
310 /* If we change assembler name, also all transparent aliases must
311 be updated. There are three kinds - those having same assembler name,
312 those being renamed in varasm.c and weakref being renamed by the
313 assembler. */
314 if (node)
316 insert_to_assembler_name_hash (node, true);
317 ipa_ref *ref;
318 for (unsigned i = 0; node->iterate_direct_aliases (i, ref); i++)
320 struct symtab_node *alias = ref->referring;
321 if (alias->transparent_alias && !alias->weakref
322 && symbol_table::assembler_names_equal_p
323 (old_name, IDENTIFIER_POINTER (
324 DECL_ASSEMBLER_NAME (alias->decl))))
325 change_decl_assembler_name (alias->decl, name);
326 else if (alias->transparent_alias
327 && IDENTIFIER_TRANSPARENT_ALIAS (alias->decl))
329 gcc_assert (TREE_CHAIN (DECL_ASSEMBLER_NAME (alias->decl))
330 && IDENTIFIER_TRANSPARENT_ALIAS
331 (DECL_ASSEMBLER_NAME (alias->decl)));
333 TREE_CHAIN (DECL_ASSEMBLER_NAME (alias->decl)) =
334 ultimate_transparent_alias_target
335 (DECL_ASSEMBLER_NAME (node->decl));
337 #ifdef ASM_OUTPUT_WEAKREF
338 else gcc_assert (!alias->transparent_alias || alias->weakref);
339 #else
340 else gcc_assert (!alias->transparent_alias);
341 #endif
343 gcc_assert (!node->transparent_alias || !node->definition
344 || node->weakref
345 || TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
346 || symbol_table::assembler_names_equal_p
347 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
348 IDENTIFIER_POINTER
349 (DECL_ASSEMBLER_NAME
350 (node->get_alias_target ()->decl))));
355 /* Hash sections by their names. */
357 hashval_t
358 section_name_hasher::hash (section_hash_entry *n)
360 return htab_hash_string (n->name);
363 /* Return true if section P1 name equals to P2. */
365 bool
366 section_name_hasher::equal (section_hash_entry *n1, const char *name)
368 return n1->name == name || !strcmp (n1->name, name);
371 /* Add node into symbol table. This function is not used directly, but via
372 cgraph/varpool node creation routines. */
374 void
375 symtab_node::register_symbol (void)
377 symtab->register_symbol (this);
379 if (!decl->decl_with_vis.symtab_node)
380 decl->decl_with_vis.symtab_node = this;
382 ref_list.clear ();
384 /* Be sure to do this last; C++ FE might create new nodes via
385 DECL_ASSEMBLER_NAME langhook! */
386 symtab->insert_to_assembler_name_hash (this, false);
389 /* Remove NODE from same comdat group. */
391 void
392 symtab_node::remove_from_same_comdat_group (void)
394 if (same_comdat_group)
396 symtab_node *prev;
397 for (prev = same_comdat_group;
398 prev->same_comdat_group != this;
399 prev = prev->same_comdat_group)
401 if (same_comdat_group == prev)
402 prev->same_comdat_group = NULL;
403 else
404 prev->same_comdat_group = same_comdat_group;
405 same_comdat_group = NULL;
406 set_comdat_group (NULL);
410 /* Remove node from symbol table. This function is not used directly, but via
411 cgraph/varpool node removal routines. */
413 void
414 symtab_node::unregister (void)
416 remove_all_references ();
417 remove_all_referring ();
419 /* Remove reference to section. */
420 set_section_for_node (NULL);
422 remove_from_same_comdat_group ();
424 symtab->unregister (this);
426 /* During LTO symtab merging we temporarily corrupt decl to symtab node
427 hash. */
428 gcc_assert (decl->decl_with_vis.symtab_node || in_lto_p);
429 if (decl->decl_with_vis.symtab_node == this)
431 symtab_node *replacement_node = NULL;
432 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
433 replacement_node = cnode->find_replacement ();
434 decl->decl_with_vis.symtab_node = replacement_node;
436 if (!is_a <varpool_node *> (this) || !DECL_HARD_REGISTER (decl))
437 symtab->unlink_from_assembler_name_hash (this, false);
438 if (in_init_priority_hash)
439 symtab->init_priority_hash->remove (this);
443 /* Remove symbol from symbol table. */
445 void
446 symtab_node::remove (void)
448 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
449 cnode->remove ();
450 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
451 vnode->remove ();
454 /* Add NEW_ to the same comdat group that OLD is in. */
456 void
457 symtab_node::add_to_same_comdat_group (symtab_node *old_node)
459 gcc_assert (old_node->get_comdat_group ());
460 gcc_assert (!same_comdat_group);
461 gcc_assert (this != old_node);
463 set_comdat_group (old_node->get_comdat_group ());
464 same_comdat_group = old_node;
465 if (!old_node->same_comdat_group)
466 old_node->same_comdat_group = this;
467 else
469 symtab_node *n;
470 for (n = old_node->same_comdat_group;
471 n->same_comdat_group != old_node;
472 n = n->same_comdat_group)
474 n->same_comdat_group = this;
477 cgraph_node *n;
478 if (comdat_local_p ()
479 && (n = dyn_cast <cgraph_node *> (this)) != NULL)
481 for (cgraph_edge *e = n->callers; e; e = e->next_caller)
482 if (e->caller->inlined_to)
483 e->caller->inlined_to->calls_comdat_local = true;
484 else
485 e->caller->calls_comdat_local = true;
489 /* Dissolve the same_comdat_group list in which NODE resides. */
491 void
492 symtab_node::dissolve_same_comdat_group_list (void)
494 symtab_node *n = this;
495 symtab_node *next;
497 if (!same_comdat_group)
498 return;
501 next = n->same_comdat_group;
502 n->same_comdat_group = NULL;
503 if (dyn_cast <cgraph_node *> (n))
504 dyn_cast <cgraph_node *> (n)->calls_comdat_local = false;
505 /* Clear comdat_group for comdat locals, since
506 make_decl_local doesn't. */
507 if (!TREE_PUBLIC (n->decl))
508 n->set_comdat_group (NULL);
509 n = next;
511 while (n != this);
514 /* Return printable assembler name of NODE.
515 This function is used only for debugging. When assembler name
516 is unknown go with identifier name. */
518 const char *
519 symtab_node::asm_name () const
521 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
522 return name ();
523 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
526 /* Return printable identifier name. */
528 const char *
529 symtab_node::name () const
531 if (!DECL_NAME (decl))
533 if (DECL_ASSEMBLER_NAME_SET_P (decl))
534 return asm_name ();
535 else
536 return "<unnamed>";
538 return lang_hooks.decl_printable_name (decl, 2);
541 const char *
542 symtab_node::get_dump_name (bool asm_name_p) const
544 #define EXTRA 16
545 const char *fname = asm_name_p ? asm_name () : name ();
546 unsigned l = strlen (fname);
548 char *s = (char *)ggc_internal_cleared_alloc (l + EXTRA);
549 snprintf (s, l + EXTRA, "%s/%d", fname, order);
551 return s;
554 const char *
555 symtab_node::dump_name () const
557 return get_dump_name (false);
560 const char *
561 symtab_node::dump_asm_name () const
563 return get_dump_name (true);
566 /* Return ipa reference from this symtab_node to
567 REFERRED_NODE or REFERRED_VARPOOL_NODE. USE_TYPE specify type
568 of the use. */
570 ipa_ref *
571 symtab_node::create_reference (symtab_node *referred_node,
572 enum ipa_ref_use use_type)
574 return create_reference (referred_node, use_type, NULL);
578 /* Return ipa reference from this symtab_node to
579 REFERRED_NODE or REFERRED_VARPOOL_NODE. USE_TYPE specify type
580 of the use and STMT the statement (if it exists). */
582 ipa_ref *
583 symtab_node::create_reference (symtab_node *referred_node,
584 enum ipa_ref_use use_type, gimple *stmt)
586 ipa_ref *ref = NULL, *ref2 = NULL;
587 ipa_ref_list *list, *list2;
588 ipa_ref_t *old_references;
590 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
591 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
593 list = &ref_list;
594 old_references = vec_safe_address (list->references);
595 vec_safe_grow (list->references, vec_safe_length (list->references) + 1,
596 true);
597 ref = &list->references->last ();
599 list2 = &referred_node->ref_list;
601 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
602 if(use_type == IPA_REF_ALIAS)
604 list2->referring.safe_insert (0, ref);
605 ref->referred_index = 0;
607 for (unsigned int i = 1; i < list2->referring.length (); i++)
608 list2->referring[i]->referred_index = i;
610 else
612 list2->referring.safe_push (ref);
613 ref->referred_index = list2->referring.length () - 1;
616 ref->referring = this;
617 ref->referred = referred_node;
618 ref->stmt = stmt;
619 ref->lto_stmt_uid = 0;
620 ref->speculative_id = 0;
621 ref->use = use_type;
622 ref->speculative = 0;
624 /* If vector was moved in memory, update pointers. */
625 if (old_references != list->references->address ())
627 int i;
628 for (i = 0; iterate_reference(i, ref2); i++)
629 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
631 return ref;
634 ipa_ref *
635 symtab_node::maybe_create_reference (tree val, gimple *stmt)
637 STRIP_NOPS (val);
638 ipa_ref_use use_type;
640 switch (TREE_CODE (val))
642 case VAR_DECL:
643 use_type = IPA_REF_LOAD;
644 break;
645 case ADDR_EXPR:
646 use_type = IPA_REF_ADDR;
647 break;
648 default:
649 gcc_assert (!handled_component_p (val));
650 return NULL;
653 val = get_base_var (val);
654 if (val && VAR_OR_FUNCTION_DECL_P (val))
656 symtab_node *referred = symtab_node::get (val);
657 gcc_checking_assert (referred);
658 return create_reference (referred, use_type, stmt);
660 return NULL;
663 /* Clone all references from symtab NODE to this symtab_node. */
665 void
666 symtab_node::clone_references (symtab_node *node)
668 ipa_ref *ref = NULL, *ref2 = NULL;
669 int i;
670 for (i = 0; node->iterate_reference (i, ref); i++)
672 bool speculative = ref->speculative;
673 unsigned int stmt_uid = ref->lto_stmt_uid;
674 unsigned int spec_id = ref->speculative_id;
676 ref2 = create_reference (ref->referred, ref->use, ref->stmt);
677 ref2->speculative = speculative;
678 ref2->lto_stmt_uid = stmt_uid;
679 ref2->speculative_id = spec_id;
683 /* Clone all referring from symtab NODE to this symtab_node. */
685 void
686 symtab_node::clone_referring (symtab_node *node)
688 ipa_ref *ref = NULL, *ref2 = NULL;
689 int i;
690 for (i = 0; node->iterate_referring(i, ref); i++)
692 bool speculative = ref->speculative;
693 unsigned int stmt_uid = ref->lto_stmt_uid;
694 unsigned int spec_id = ref->speculative_id;
696 ref2 = ref->referring->create_reference (this, ref->use, ref->stmt);
697 ref2->speculative = speculative;
698 ref2->lto_stmt_uid = stmt_uid;
699 ref2->speculative_id = spec_id;
703 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
705 ipa_ref *
706 symtab_node::clone_reference (ipa_ref *ref, gimple *stmt)
708 bool speculative = ref->speculative;
709 unsigned int stmt_uid = ref->lto_stmt_uid;
710 unsigned int spec_id = ref->speculative_id;
711 ipa_ref *ref2;
713 ref2 = create_reference (ref->referred, ref->use, stmt);
714 ref2->speculative = speculative;
715 ref2->lto_stmt_uid = stmt_uid;
716 ref2->speculative_id = spec_id;
717 return ref2;
720 /* Find the structure describing a reference to REFERRED_NODE
721 and associated with statement STMT. */
723 ipa_ref *
724 symtab_node::find_reference (symtab_node *referred_node,
725 gimple *stmt, unsigned int lto_stmt_uid)
727 ipa_ref *r = NULL;
728 int i;
730 for (i = 0; iterate_reference (i, r); i++)
731 if (r->referred == referred_node
732 && !r->speculative
733 && ((stmt && r->stmt == stmt)
734 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
735 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
736 return r;
737 return NULL;
740 /* Remove all references that are associated with statement STMT. */
742 void
743 symtab_node::remove_stmt_references (gimple *stmt)
745 ipa_ref *r = NULL;
746 int i = 0;
748 while (iterate_reference (i, r))
749 if (r->stmt == stmt)
750 r->remove_reference ();
751 else
752 i++;
755 /* Remove all stmt references in non-speculative references.
756 Those are not maintained during inlining & cloning.
757 The exception are speculative references that are updated along
758 with callgraph edges associated with them. */
760 void
761 symtab_node::clear_stmts_in_references (void)
763 ipa_ref *r = NULL;
764 int i;
766 for (i = 0; iterate_reference (i, r); i++)
767 if (!r->speculative)
769 r->stmt = NULL;
770 r->lto_stmt_uid = 0;
771 r->speculative_id = 0;
775 /* Remove all references in ref list. */
777 void
778 symtab_node::remove_all_references (void)
780 while (vec_safe_length (ref_list.references))
781 ref_list.references->last ().remove_reference ();
782 vec_free (ref_list.references);
785 /* Remove all referring items in ref list. */
787 void
788 symtab_node::remove_all_referring (void)
790 while (ref_list.referring.length ())
791 ref_list.referring.last ()->remove_reference ();
792 ref_list.referring.release ();
795 /* Dump references in ref list to FILE. */
797 void
798 symtab_node::dump_references (FILE *file)
800 ipa_ref *ref = NULL;
801 int i;
802 for (i = 0; iterate_reference (i, ref); i++)
804 fprintf (file, "%s (%s) ", ref->referred->dump_asm_name (),
805 ipa_ref_use_name[ref->use]);
806 if (ref->speculative)
807 fprintf (file, "(speculative) ");
809 fprintf (file, "\n");
812 /* Dump referring in list to FILE. */
814 void
815 symtab_node::dump_referring (FILE *file)
817 ipa_ref *ref = NULL;
818 int i;
819 for (i = 0; iterate_referring(i, ref); i++)
821 fprintf (file, "%s (%s) ", ref->referring->dump_asm_name (),
822 ipa_ref_use_name[ref->use]);
823 if (ref->speculative)
824 fprintf (file, "(speculative) ");
826 fprintf (file, "\n");
829 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
831 /* Dump the visibility of the symbol. */
833 const char *
834 symtab_node::get_visibility_string () const
836 static const char * const visibility_types[]
837 = { "default", "protected", "hidden", "internal" };
838 return visibility_types[DECL_VISIBILITY (decl)];
841 /* Dump the type_name of the symbol. */
842 const char *
843 symtab_node::get_symtab_type_string () const
845 return symtab_type_names[type];
848 /* Dump base fields of symtab nodes to F. Not to be used directly. */
850 void
851 symtab_node::dump_base (FILE *f)
853 static const char * const visibility_types[] = {
854 "default", "protected", "hidden", "internal"
857 fprintf (f, "%s (%s)", dump_asm_name (), name ());
858 dump_addr (f, " @", (void *)this);
859 fprintf (f, "\n Type: %s", symtab_type_names[type]);
861 if (definition)
862 fprintf (f, " definition");
863 if (analyzed)
864 fprintf (f, " analyzed");
865 if (alias)
866 fprintf (f, " alias");
867 if (transparent_alias)
868 fprintf (f, " transparent_alias");
869 if (weakref)
870 fprintf (f, " weakref");
871 if (symver)
872 fprintf (f, " symver");
873 if (cpp_implicit_alias)
874 fprintf (f, " cpp_implicit_alias");
875 if (alias_target)
876 fprintf (f, " target:%s",
877 DECL_P (alias_target)
878 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
879 (alias_target))
880 : IDENTIFIER_POINTER (alias_target));
881 if (body_removed)
882 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
883 fprintf (f, "\n Visibility:");
884 if (in_other_partition)
885 fprintf (f, " in_other_partition");
886 if (used_from_other_partition)
887 fprintf (f, " used_from_other_partition");
888 if (force_output)
889 fprintf (f, " force_output");
890 if (forced_by_abi)
891 fprintf (f, " forced_by_abi");
892 if (externally_visible)
893 fprintf (f, " externally_visible");
894 if (no_reorder)
895 fprintf (f, " no_reorder");
896 if (resolution != LDPR_UNKNOWN)
897 fprintf (f, " %s",
898 ld_plugin_symbol_resolution_names[(int)resolution]);
899 if (TREE_ASM_WRITTEN (decl))
900 fprintf (f, " asm_written");
901 if (DECL_EXTERNAL (decl))
902 fprintf (f, " external");
903 if (TREE_PUBLIC (decl))
904 fprintf (f, " public");
905 if (DECL_COMMON (decl))
906 fprintf (f, " common");
907 if (DECL_WEAK (decl))
908 fprintf (f, " weak");
909 if (DECL_DLLIMPORT_P (decl))
910 fprintf (f, " dll_import");
911 if (DECL_COMDAT (decl))
912 fprintf (f, " comdat");
913 if (get_comdat_group ())
914 fprintf (f, " comdat_group:%s",
915 IDENTIFIER_POINTER (get_comdat_group_id ()));
916 if (DECL_ONE_ONLY (decl))
917 fprintf (f, " one_only");
918 if (get_section ())
919 fprintf (f, " section:%s",
920 get_section ());
921 if (implicit_section)
922 fprintf (f," (implicit_section)");
923 if (DECL_VISIBILITY_SPECIFIED (decl))
924 fprintf (f, " visibility_specified");
925 if (DECL_VISIBILITY (decl))
926 fprintf (f, " visibility:%s",
927 visibility_types [DECL_VISIBILITY (decl)]);
928 if (DECL_VIRTUAL_P (decl))
929 fprintf (f, " virtual");
930 if (DECL_ARTIFICIAL (decl))
931 fprintf (f, " artificial");
932 if (TREE_CODE (decl) == FUNCTION_DECL)
934 if (DECL_STATIC_CONSTRUCTOR (decl))
935 fprintf (f, " constructor");
936 if (DECL_STATIC_DESTRUCTOR (decl))
937 fprintf (f, " destructor");
939 if (ifunc_resolver)
940 fprintf (f, " ifunc_resolver");
941 fprintf (f, "\n");
943 if (same_comdat_group)
944 fprintf (f, " Same comdat group as: %s\n",
945 same_comdat_group->dump_asm_name ());
946 if (next_sharing_asm_name)
947 fprintf (f, " next sharing asm name: %i\n",
948 next_sharing_asm_name->order);
949 if (previous_sharing_asm_name)
950 fprintf (f, " previous sharing asm name: %i\n",
951 previous_sharing_asm_name->order);
953 if (address_taken)
954 fprintf (f, " Address is taken.\n");
955 if (aux)
957 fprintf (f, " Aux:");
958 dump_addr (f, " @", (void *)aux);
959 fprintf (f, "\n");
962 fprintf (f, " References: ");
963 dump_references (f);
964 fprintf (f, " Referring: ");
965 dump_referring (f);
966 if (lto_file_data)
967 fprintf (f, " Read from file: %s\n",
968 lto_file_data->file_name);
971 /* Dump symtab node to F. */
973 void
974 symtab_node::dump (FILE *f)
976 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
977 cnode->dump (f);
978 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
979 vnode->dump (f);
982 void
983 symtab_node::dump_graphviz (FILE *f)
985 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
986 cnode->dump_graphviz (f);
989 void
990 symbol_table::dump (FILE *f)
992 symtab_node *node;
993 fprintf (f, "Symbol table:\n\n");
994 FOR_EACH_SYMBOL (node)
995 node->dump (f);
998 void
999 symbol_table::dump_graphviz (FILE *f)
1001 symtab_node *node;
1002 fprintf (f, "digraph symtab {\n");
1003 FOR_EACH_SYMBOL (node)
1004 node->dump_graphviz (f);
1005 fprintf (f, "}\n");
1008 DEBUG_FUNCTION void
1009 symbol_table::debug (void)
1011 dump (stderr);
1014 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
1015 Return NULL if there's no such node. */
1017 symtab_node *
1018 symtab_node::get_for_asmname (const_tree asmname)
1020 symtab_node *node;
1022 symtab->symtab_initialize_asm_name_hash ();
1023 hashval_t hash = symtab->decl_assembler_name_hash (asmname);
1024 symtab_node **slot
1025 = symtab->assembler_name_hash->find_slot_with_hash (asmname, hash,
1026 NO_INSERT);
1028 if (slot)
1030 node = *slot;
1031 return node;
1033 return NULL;
1036 /* Dump symtab node NODE to stderr. */
1038 DEBUG_FUNCTION void
1039 symtab_node::debug (void)
1041 dump (stderr);
1044 /* Verify common part of symtab nodes. */
1046 #if __GNUC__ >= 10
1047 /* Disable warnings about missing quoting in GCC diagnostics for
1048 the verification errors. Their format strings don't follow GCC
1049 diagnostic conventions and the calls are ultimately followed by
1050 one to internal_error. */
1051 # pragma GCC diagnostic push
1052 # pragma GCC diagnostic ignored "-Wformat-diag"
1053 #endif
1055 DEBUG_FUNCTION bool
1056 symtab_node::verify_base (void)
1058 bool error_found = false;
1059 symtab_node *hashed_node;
1061 if (is_a <cgraph_node *> (this))
1063 if (TREE_CODE (decl) != FUNCTION_DECL)
1065 error ("function symbol is not function");
1066 error_found = true;
1068 else if ((lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl))
1069 != NULL)
1070 != dyn_cast <cgraph_node *> (this)->ifunc_resolver)
1072 error ("inconsistent %<ifunc%> attribute");
1073 error_found = true;
1076 else if (is_a <varpool_node *> (this))
1078 if (!VAR_P (decl))
1080 error ("variable symbol is not variable");
1081 error_found = true;
1084 else
1086 error ("node has unknown type");
1087 error_found = true;
1089 if (order < 0 || order >= symtab->order)
1091 error ("node has invalid order %i", order);
1092 error_found = true;
1095 if (symtab->state != LTO_STREAMING)
1097 hashed_node = symtab_node::get (decl);
1098 if (!hashed_node)
1100 error ("node not found node->decl->decl_with_vis.symtab_node");
1101 error_found = true;
1103 if (hashed_node != this
1104 && (!is_a <cgraph_node *> (this)
1105 || !dyn_cast <cgraph_node *> (this)->clone_of
1106 || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl))
1108 error ("node differs from node->decl->decl_with_vis.symtab_node");
1109 error_found = true;
1112 if (symtab->assembler_name_hash)
1114 hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
1115 if (hashed_node)
1117 if (hashed_node->previous_sharing_asm_name)
1119 error ("assembler name hash list corrupted");
1120 error_found = true;
1122 else if (previous_sharing_asm_name == NULL)
1124 if (hashed_node != this)
1126 error ("assembler name hash list corrupted");
1127 error_found = true;
1130 else if (!(is_a <varpool_node *> (this) && DECL_HARD_REGISTER (decl)))
1132 if (!asmname_hasher::equal (previous_sharing_asm_name,
1133 DECL_ASSEMBLER_NAME (decl)))
1135 error ("node not found in symtab assembler name hash");
1136 error_found = true;
1141 if (previous_sharing_asm_name
1142 && previous_sharing_asm_name->next_sharing_asm_name != this)
1144 error ("double linked list of assembler names corrupted");
1145 error_found = true;
1147 if (body_removed && definition)
1149 error ("node has body_removed but is definition");
1150 error_found = true;
1152 if (analyzed && !definition)
1154 error ("node is analyzed but it is not a definition");
1155 error_found = true;
1157 if (cpp_implicit_alias && !alias)
1159 error ("node is alias but not implicit alias");
1160 error_found = true;
1162 if (alias && !definition && !weakref)
1164 error ("node is alias but not definition");
1165 error_found = true;
1167 if (weakref && !transparent_alias)
1169 error ("node is weakref but not an transparent_alias");
1170 error_found = true;
1172 if (transparent_alias && !alias)
1174 error ("node is transparent_alias but not an alias");
1175 error_found = true;
1177 if (symver && !alias)
1179 error ("node is symver but not alias");
1180 error_found = true;
1182 /* Limitation of gas requires us to output targets of symver aliases as
1183 global symbols. This is binutils PR 25295. */
1184 if (symver
1185 && (!TREE_PUBLIC (get_alias_target ()->decl)
1186 || DECL_VISIBILITY (get_alias_target ()->decl) != VISIBILITY_DEFAULT))
1188 error ("symver target is not exported with default visibility");
1189 error_found = true;
1191 if (symver
1192 && (!TREE_PUBLIC (decl)
1193 || DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT))
1195 error ("symver is not exported with default visibility");
1196 error_found = true;
1198 if (same_comdat_group)
1200 symtab_node *n = same_comdat_group;
1202 if (!n->get_comdat_group ())
1204 error ("node is in same_comdat_group list but has no comdat_group");
1205 error_found = true;
1207 if (n->get_comdat_group () != get_comdat_group ())
1209 error ("same_comdat_group list across different groups");
1210 error_found = true;
1212 if (n->type != type)
1214 error ("mixing different types of symbol in same comdat groups is not supported");
1215 error_found = true;
1217 if (n == this)
1219 error ("node is alone in a comdat group");
1220 error_found = true;
1224 if (!n->same_comdat_group)
1226 error ("same_comdat_group is not a circular list");
1227 error_found = true;
1228 break;
1230 n = n->same_comdat_group;
1232 while (n != this);
1233 if (comdat_local_p ())
1235 ipa_ref *ref = NULL;
1237 for (int i = 0; iterate_referring (i, ref); ++i)
1239 if (!in_same_comdat_group_p (ref->referring))
1241 error ("comdat-local symbol referred to by %s outside its "
1242 "comdat",
1243 identifier_to_locale (ref->referring->name()));
1244 error_found = true;
1249 if (implicit_section && !get_section ())
1251 error ("implicit_section flag is set but section isn%'t");
1252 error_found = true;
1254 if (get_section () && get_comdat_group ()
1255 && !implicit_section
1256 && !lookup_attribute ("section", DECL_ATTRIBUTES (decl)))
1258 error ("Both section and comdat group is set");
1259 error_found = true;
1261 /* TODO: Add string table for sections, so we do not keep holding duplicated
1262 strings. */
1263 if (alias && definition
1264 && get_section () != get_alias_target ()->get_section ()
1265 && (!get_section()
1266 || !get_alias_target ()->get_section ()
1267 || strcmp (get_section(),
1268 get_alias_target ()->get_section ())))
1270 error ("Alias and target%'s section differs");
1271 get_alias_target ()->dump (stderr);
1272 error_found = true;
1274 if (alias && definition
1275 && get_comdat_group () != get_alias_target ()->get_comdat_group ())
1277 error ("Alias and target%'s comdat groups differs");
1278 get_alias_target ()->dump (stderr);
1279 error_found = true;
1281 if (transparent_alias && definition && !weakref)
1283 symtab_node *to = get_alias_target ();
1284 const char *name1
1285 = IDENTIFIER_POINTER (
1286 ultimate_transparent_alias_target (DECL_ASSEMBLER_NAME (decl)));
1287 const char *name2
1288 = IDENTIFIER_POINTER (
1289 ultimate_transparent_alias_target (DECL_ASSEMBLER_NAME (to->decl)));
1290 if (!symbol_table::assembler_names_equal_p (name1, name2))
1292 error ("Transparent alias and target%'s assembler names differs");
1293 get_alias_target ()->dump (stderr);
1294 error_found = true;
1297 if (transparent_alias && definition
1298 && get_alias_target()->transparent_alias && get_alias_target()->analyzed)
1300 error ("Chained transparent aliases");
1301 get_alias_target ()->dump (stderr);
1302 error_found = true;
1305 return error_found;
1308 /* Verify consistency of NODE. */
1310 DEBUG_FUNCTION void
1311 symtab_node::verify (void)
1313 if (seen_error ())
1314 return;
1316 timevar_push (TV_CGRAPH_VERIFY);
1317 if (cgraph_node *node = dyn_cast <cgraph_node *> (this))
1318 node->verify_node ();
1319 else
1320 if (verify_base ())
1322 debug ();
1323 internal_error ("symtab_node::verify failed");
1325 timevar_pop (TV_CGRAPH_VERIFY);
1328 /* Verify symbol table for internal consistency. */
1330 DEBUG_FUNCTION void
1331 symtab_node::verify_symtab_nodes (void)
1333 symtab_node *node;
1334 hash_map<tree, symtab_node *> comdat_head_map (251);
1335 asm_node *anode;
1337 for (anode = symtab->first_asm_symbol (); anode; anode = anode->next)
1338 if (anode->order < 0 || anode->order >= symtab->order)
1340 error ("invalid order in asm node %i", anode->order);
1341 internal_error ("symtab_node::verify failed");
1344 FOR_EACH_SYMBOL (node)
1346 node->verify ();
1347 if (node->get_comdat_group ())
1349 symtab_node **entry, *s;
1350 bool existed;
1352 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1353 &existed);
1354 if (!existed)
1355 *entry = node;
1356 else if (!DECL_EXTERNAL (node->decl))
1358 for (s = (*entry)->same_comdat_group;
1359 s != NULL && s != node && s != *entry;
1360 s = s->same_comdat_group)
1362 if (!s || s == *entry)
1364 error ("Two symbols with same comdat_group are not linked by "
1365 "the same_comdat_group list.");
1366 (*entry)->debug ();
1367 node->debug ();
1368 internal_error ("symtab_node::verify failed");
1375 #if __GNUC__ >= 10
1376 # pragma GCC diagnostic pop
1377 #endif
1379 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1380 but other code such as notice_global_symbol generates rtl. */
1382 void
1383 symtab_node::make_decl_local (void)
1385 rtx rtl, symbol;
1387 if (weakref)
1389 weakref = false;
1390 IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl)) = 0;
1391 TREE_CHAIN (DECL_ASSEMBLER_NAME (decl)) = NULL_TREE;
1392 symtab->change_decl_assembler_name
1393 (decl, DECL_ASSEMBLER_NAME (get_alias_target ()->decl));
1394 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
1395 DECL_ATTRIBUTES (decl));
1397 /* Avoid clearing comdat_groups on comdat-local decls. */
1398 else if (TREE_PUBLIC (decl) == 0)
1399 return;
1401 /* Localizing a symbol also make all its transparent aliases local. */
1402 ipa_ref *ref;
1403 for (unsigned i = 0; iterate_direct_aliases (i, ref); i++)
1405 struct symtab_node *alias = ref->referring;
1406 if (alias->transparent_alias)
1407 alias->make_decl_local ();
1410 if (VAR_P (decl))
1412 DECL_COMMON (decl) = 0;
1413 /* ADDRESSABLE flag is not defined for public symbols. */
1414 TREE_ADDRESSABLE (decl) = 1;
1415 TREE_STATIC (decl) = 1;
1417 else
1418 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1420 DECL_COMDAT (decl) = 0;
1421 DECL_WEAK (decl) = 0;
1422 DECL_EXTERNAL (decl) = 0;
1423 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1424 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1425 TREE_PUBLIC (decl) = 0;
1426 DECL_DLLIMPORT_P (decl) = 0;
1427 if (!DECL_RTL_SET_P (decl))
1428 return;
1430 /* Update rtl flags. */
1431 make_decl_rtl (decl);
1433 rtl = DECL_RTL (decl);
1434 if (!MEM_P (rtl))
1435 return;
1437 symbol = XEXP (rtl, 0);
1438 if (GET_CODE (symbol) != SYMBOL_REF)
1439 return;
1441 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1444 /* Copy visibility from N.
1445 This is useful when THIS becomes a transparent alias of N. */
1447 void
1448 symtab_node::copy_visibility_from (symtab_node *n)
1450 gcc_checking_assert (n->weakref == weakref);
1452 ipa_ref *ref;
1453 for (unsigned i = 0; iterate_direct_aliases (i, ref); i++)
1455 struct symtab_node *alias = ref->referring;
1456 if (alias->transparent_alias)
1457 alias->copy_visibility_from (n);
1460 if (VAR_P (decl))
1462 DECL_COMMON (decl) = DECL_COMMON (n->decl);
1463 /* ADDRESSABLE flag is not defined for public symbols. */
1464 if (TREE_PUBLIC (decl) && !TREE_PUBLIC (n->decl))
1465 TREE_ADDRESSABLE (decl) = 1;
1466 TREE_STATIC (decl) = TREE_STATIC (n->decl);
1468 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1470 DECL_COMDAT (decl) = DECL_COMDAT (n->decl);
1471 DECL_WEAK (decl) = DECL_WEAK (n->decl);
1472 DECL_EXTERNAL (decl) = DECL_EXTERNAL (n->decl);
1473 DECL_VISIBILITY_SPECIFIED (decl) = DECL_VISIBILITY_SPECIFIED (n->decl);
1474 DECL_VISIBILITY (decl) = DECL_VISIBILITY (n->decl);
1475 TREE_PUBLIC (decl) = TREE_PUBLIC (n->decl);
1476 DECL_DLLIMPORT_P (decl) = DECL_DLLIMPORT_P (n->decl);
1477 resolution = n->resolution;
1478 set_comdat_group (n->get_comdat_group ());
1479 call_for_symbol_and_aliases (symtab_node::set_section,
1480 const_cast<char *>(n->get_section ()), true);
1481 externally_visible = n->externally_visible;
1482 if (!DECL_RTL_SET_P (decl))
1483 return;
1485 /* Update rtl flags. */
1486 make_decl_rtl (decl);
1488 rtx rtl = DECL_RTL (decl);
1489 if (!MEM_P (rtl))
1490 return;
1492 rtx symbol = XEXP (rtl, 0);
1493 if (GET_CODE (symbol) != SYMBOL_REF)
1494 return;
1496 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1499 /* Walk the alias chain to return the symbol NODE is alias of.
1500 If NODE is not an alias, return NODE.
1501 Assumes NODE is known to be alias. */
1503 symtab_node *
1504 symtab_node::ultimate_alias_target_1 (enum availability *availability,
1505 symtab_node *ref)
1507 bool transparent_p = false;
1509 /* To determine visibility of the target, we follow ELF semantic of aliases.
1510 Here alias is an alternative assembler name of a given definition. Its
1511 availability prevails the availability of its target (i.e. static alias of
1512 weak definition is available.
1514 Transparent alias is just alternative name of a given symbol used within
1515 one compilation unit and is translated prior hitting the object file. It
1516 inherits the visibility of its target.
1517 Weakref is a different animal (and noweak definition is weak).
1519 If we ever get into supporting targets with different semantics, a target
1520 hook will be needed here. */
1522 if (availability)
1524 transparent_p = transparent_alias;
1525 if (!transparent_p)
1526 *availability = get_availability (ref);
1527 else
1528 *availability = AVAIL_NOT_AVAILABLE;
1531 symtab_node *node = this;
1532 while (node)
1534 if (node->alias && node->analyzed)
1535 node = node->get_alias_target ();
1536 else
1538 if (!availability || (!transparent_p && node->analyzed))
1540 else if (node->analyzed && !node->transparent_alias)
1541 *availability = node->get_availability (ref);
1542 else
1543 *availability = AVAIL_NOT_AVAILABLE;
1544 return node;
1546 if (node && availability && transparent_p
1547 && node->transparent_alias)
1549 *availability = node->get_availability (ref);
1550 transparent_p = false;
1553 if (availability)
1554 *availability = AVAIL_NOT_AVAILABLE;
1555 return NULL;
1558 /* C++ FE sometimes change linkage flags after producing same body aliases.
1560 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1561 are obviously equivalent. The way it is doing so is however somewhat
1562 kludgy and interferes with the visibility code. As a result we need to
1563 copy the visibility from the target to get things right. */
1565 void
1566 symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target)
1568 if (is_a <cgraph_node *> (this))
1570 DECL_DECLARED_INLINE_P (decl)
1571 = DECL_DECLARED_INLINE_P (target->decl);
1572 DECL_DISREGARD_INLINE_LIMITS (decl)
1573 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1575 /* FIXME: It is not really clear why those flags should not be copied for
1576 functions, too. */
1577 else
1579 DECL_WEAK (decl) = DECL_WEAK (target->decl);
1580 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1581 DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl);
1583 if (TREE_PUBLIC (decl))
1585 tree group;
1587 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1588 DECL_COMDAT (decl) = DECL_COMDAT (target->decl);
1589 group = target->get_comdat_group ();
1590 set_comdat_group (group);
1591 if (group && !same_comdat_group)
1592 add_to_same_comdat_group (target);
1594 externally_visible = target->externally_visible;
1597 /* Set section, do not recurse into aliases.
1598 When one wants to change section of a symbol and its aliases,
1599 use set_section. */
1601 void
1602 symtab_node::set_section_for_node (const char *section)
1604 const char *current = get_section ();
1605 section_hash_entry **slot;
1607 if (current == section
1608 || (current && section
1609 && !strcmp (current, section)))
1610 return;
1612 if (current)
1614 x_section->ref_count--;
1615 if (!x_section->ref_count)
1617 hashval_t hash = htab_hash_string (x_section->name);
1618 slot = symtab->section_hash->find_slot_with_hash (x_section->name,
1619 hash, INSERT);
1620 ggc_free (x_section);
1621 symtab->section_hash->clear_slot (slot);
1623 x_section = NULL;
1625 if (!section)
1627 implicit_section = false;
1628 return;
1630 if (!symtab->section_hash)
1631 symtab->section_hash = hash_table<section_name_hasher>::create_ggc (10);
1632 slot = symtab->section_hash->find_slot_with_hash (section,
1633 htab_hash_string (section),
1634 INSERT);
1635 if (*slot)
1636 x_section = (section_hash_entry *)*slot;
1637 else
1639 int len = strlen (section);
1640 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1641 x_section->name = ggc_vec_alloc<char> (len + 1);
1642 memcpy (x_section->name, section, len + 1);
1644 x_section->ref_count++;
1647 /* Worker for set_section. */
1649 bool
1650 symtab_node::set_section (symtab_node *n, void *s)
1652 n->set_section_for_node ((char *)s);
1653 return false;
1656 /* Set section of symbol and its aliases. */
1658 void
1659 symtab_node::set_section (const char *section)
1661 gcc_assert (!this->alias || !this->analyzed);
1662 call_for_symbol_and_aliases
1663 (symtab_node::set_section, const_cast<char *>(section), true);
1666 /* Return the initialization priority. */
1668 priority_type
1669 symtab_node::get_init_priority ()
1671 if (!this->in_init_priority_hash)
1672 return DEFAULT_INIT_PRIORITY;
1674 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1675 return h ? h->init : DEFAULT_INIT_PRIORITY;
1678 /* Return the finalization priority. */
1680 priority_type
1681 cgraph_node::get_fini_priority ()
1683 if (!this->in_init_priority_hash)
1684 return DEFAULT_INIT_PRIORITY;
1685 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1686 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1689 /* Return the initialization and finalization priority information for
1690 DECL. If there is no previous priority information, a freshly
1691 allocated structure is returned. */
1693 symbol_priority_map *
1694 symtab_node::priority_info (void)
1696 if (!symtab->init_priority_hash)
1697 symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13);
1699 bool existed;
1700 symbol_priority_map *h
1701 = &symtab->init_priority_hash->get_or_insert (this, &existed);
1702 if (!existed)
1704 h->init = DEFAULT_INIT_PRIORITY;
1705 h->fini = DEFAULT_INIT_PRIORITY;
1706 in_init_priority_hash = true;
1709 return h;
1712 /* Set initialization priority to PRIORITY. */
1714 void
1715 symtab_node::set_init_priority (priority_type priority)
1717 symbol_priority_map *h;
1719 if (is_a <cgraph_node *> (this))
1720 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1722 if (priority == DEFAULT_INIT_PRIORITY)
1724 gcc_assert (get_init_priority() == priority);
1725 return;
1727 h = priority_info ();
1728 h->init = priority;
1731 /* Set finalization priority to PRIORITY. */
1733 void
1734 cgraph_node::set_fini_priority (priority_type priority)
1736 symbol_priority_map *h;
1738 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1740 if (priority == DEFAULT_INIT_PRIORITY)
1742 gcc_assert (get_fini_priority() == priority);
1743 return;
1745 h = priority_info ();
1746 h->fini = priority;
1749 /* Worker for symtab_resolve_alias. */
1751 bool
1752 symtab_node::set_implicit_section (symtab_node *n,
1753 void *data ATTRIBUTE_UNUSED)
1755 n->implicit_section = true;
1756 return false;
1759 /* Add reference recording that symtab node is alias of TARGET.
1760 The function can fail in the case of aliasing cycles; in this case
1761 it returns false. */
1763 bool
1764 symtab_node::resolve_alias (symtab_node *target, bool transparent)
1766 symtab_node *n;
1768 gcc_assert (!analyzed && !vec_safe_length (ref_list.references));
1770 /* Never let cycles to creep into the symbol table alias references;
1771 those will make alias walkers to be infinite. */
1772 for (n = target; n && n->alias;
1773 n = n->analyzed ? n->get_alias_target () : NULL)
1774 if (n == this)
1776 if (is_a <cgraph_node *> (this))
1777 error ("function %q+D part of alias cycle", decl);
1778 else if (is_a <varpool_node *> (this))
1779 error ("variable %q+D part of alias cycle", decl);
1780 else
1781 gcc_unreachable ();
1782 alias = false;
1783 return false;
1786 /* "analyze" the node - i.e. mark the reference. */
1787 definition = true;
1788 alias = true;
1789 analyzed = true;
1790 transparent |= transparent_alias;
1791 transparent_alias = transparent;
1792 if (transparent)
1793 while (target->transparent_alias && target->analyzed)
1794 target = target->get_alias_target ();
1795 create_reference (target, IPA_REF_ALIAS, NULL);
1797 /* Add alias into the comdat group of its target unless it is already there. */
1798 if (same_comdat_group)
1799 remove_from_same_comdat_group ();
1800 set_comdat_group (NULL);
1801 if (target->get_comdat_group ())
1802 add_to_same_comdat_group (target);
1804 if ((get_section () != target->get_section ()
1805 || target->get_comdat_group ()) && get_section () && !implicit_section)
1807 error ("section of alias %q+D must match section of its target", decl);
1809 call_for_symbol_and_aliases (symtab_node::set_section,
1810 const_cast<char *>(target->get_section ()), true);
1811 if (target->implicit_section)
1812 call_for_symbol_and_aliases (set_implicit_section, NULL, true);
1814 /* Alias targets become redundant after alias is resolved into an reference.
1815 We do not want to keep it around or we would have to mind updating them
1816 when renaming symbols. */
1817 alias_target = NULL;
1819 if (!transparent && cpp_implicit_alias && symtab->state >= CONSTRUCTION)
1820 fixup_same_cpp_alias_visibility (target);
1822 /* If alias has address taken, so does the target. */
1823 if (address_taken)
1824 target->ultimate_alias_target ()->address_taken = true;
1826 /* All non-transparent aliases of THIS are now in fact aliases of TARGET.
1827 If alias is transparent, also all transparent aliases of THIS are now
1828 aliases of TARGET.
1829 Also merge same comdat group lists. */
1830 ipa_ref *ref;
1831 for (unsigned i = 0; iterate_direct_aliases (i, ref);)
1833 struct symtab_node *alias_alias = ref->referring;
1834 if (alias_alias->get_comdat_group ())
1836 alias_alias->remove_from_same_comdat_group ();
1837 alias_alias->set_comdat_group (NULL);
1838 if (target->get_comdat_group ())
1839 alias_alias->add_to_same_comdat_group (target);
1841 if ((!alias_alias->transparent_alias
1842 && !alias_alias->symver)
1843 || transparent)
1845 alias_alias->remove_all_references ();
1846 alias_alias->create_reference (target, IPA_REF_ALIAS, NULL);
1848 else i++;
1850 return true;
1853 /* Worker searching noninterposable alias. */
1855 bool
1856 symtab_node::noninterposable_alias (symtab_node *node, void *data)
1858 if (!node->transparent_alias && decl_binds_to_current_def_p (node->decl))
1860 symtab_node *fn = node->ultimate_alias_target ();
1862 /* Ensure that the alias is well formed this may not be the case
1863 of user defined aliases and currently it is not always the case
1864 of C++ same body aliases (that is a bug). */
1865 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
1866 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
1867 || (TREE_CODE (node->decl) == FUNCTION_DECL
1868 && flags_from_decl_or_type (node->decl)
1869 != flags_from_decl_or_type (fn->decl))
1870 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
1871 return false;
1872 *(symtab_node **)data = node;
1873 return true;
1875 return false;
1878 /* If node cannot be overwriten by static or dynamic linker to point to
1879 different definition, return NODE. Otherwise look for alias with such
1880 property and if none exists, introduce new one. */
1882 symtab_node *
1883 symtab_node::noninterposable_alias (void)
1885 tree new_decl;
1886 symtab_node *new_node = NULL;
1888 /* First try to look up existing alias or base object
1889 (if that is already non-overwritable). */
1890 symtab_node *node = ultimate_alias_target ();
1891 gcc_assert (!node->alias && !node->weakref);
1892 node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias,
1893 (void *)&new_node, true);
1894 if (new_node)
1895 return new_node;
1897 /* If aliases aren't supported by the assembler, fail. */
1898 if (!TARGET_SUPPORTS_ALIASES)
1899 return NULL;
1901 /* Otherwise create a new one. */
1902 new_decl = copy_node (node->decl);
1903 DECL_DLLIMPORT_P (new_decl) = 0;
1904 tree name = clone_function_name (node->decl, "localalias");
1905 if (!flag_wpa)
1907 unsigned long num = 0;
1908 /* In the rare case we already have a localalias, but the above
1909 node->call_for_symbol_and_aliases call didn't find any suitable,
1910 iterate until we find one not used yet. */
1911 while (symtab_node::get_for_asmname (name))
1912 name = clone_function_name (node->decl, "localalias", num++);
1914 DECL_NAME (new_decl) = name;
1915 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1916 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1917 DECL_INITIAL (new_decl) = NULL;
1918 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1919 SET_DECL_RTL (new_decl, NULL);
1921 /* Update the properties. */
1922 DECL_EXTERNAL (new_decl) = 0;
1923 TREE_PUBLIC (new_decl) = 0;
1924 DECL_COMDAT (new_decl) = 0;
1925 DECL_WEAK (new_decl) = 0;
1927 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1928 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1929 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1931 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1932 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1933 new_node = cgraph_node::create_alias (new_decl, node->decl);
1935 cgraph_node *new_cnode = dyn_cast <cgraph_node *> (new_node),
1936 *cnode = dyn_cast <cgraph_node *> (node);
1938 new_cnode->unit_id = cnode->unit_id;
1939 new_cnode->merged_comdat = cnode->merged_comdat;
1940 new_cnode->merged_extern_inline = cnode->merged_extern_inline;
1942 else
1944 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1945 DECL_INITIAL (new_decl) = error_mark_node;
1946 new_node = varpool_node::create_alias (new_decl, node->decl);
1948 new_node->resolve_alias (node);
1949 gcc_assert (decl_binds_to_current_def_p (new_decl)
1950 && targetm.binds_local_p (new_decl));
1951 return new_node;
1954 /* Return true if symtab node and TARGET represents
1955 semantically equivalent symbols. */
1957 bool
1958 symtab_node::semantically_equivalent_p (symtab_node *target)
1960 enum availability avail;
1961 symtab_node *ba;
1962 symtab_node *bb;
1964 /* Equivalent functions are equivalent. */
1965 if (decl == target->decl)
1966 return true;
1968 /* If symbol is not overwritable by different implementation,
1969 walk to the base object it defines. */
1970 ba = ultimate_alias_target (&avail);
1971 if (avail >= AVAIL_AVAILABLE)
1973 if (target == ba)
1974 return true;
1976 else
1977 ba = this;
1978 bb = target->ultimate_alias_target (&avail);
1979 if (avail >= AVAIL_AVAILABLE)
1981 if (this == bb)
1982 return true;
1984 else
1985 bb = target;
1986 return bb == ba;
1989 /* Classify symbol symtab node for partitioning. */
1991 enum symbol_partitioning_class
1992 symtab_node::get_partitioning_class (void)
1994 /* Inline clones are always duplicated.
1995 This include external declarations. */
1996 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
1998 if (DECL_ABSTRACT_P (decl))
1999 return SYMBOL_EXTERNAL;
2001 if (cnode && cnode->inlined_to)
2002 return SYMBOL_DUPLICATE;
2004 /* Transparent aliases are always duplicated. */
2005 if (transparent_alias)
2006 return definition ? SYMBOL_DUPLICATE : SYMBOL_EXTERNAL;
2008 /* External declarations are external. */
2009 if (DECL_EXTERNAL (decl))
2010 return SYMBOL_EXTERNAL;
2012 /* Even static aliases of external functions as external. Those can happen
2013 when COMDAT got resolved to non-IL implementation. */
2014 if (alias && DECL_EXTERNAL (ultimate_alias_target ()->decl))
2015 return SYMBOL_EXTERNAL;
2017 if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
2019 if (alias && definition && !ultimate_alias_target ()->definition)
2020 return SYMBOL_EXTERNAL;
2021 /* Constant pool references use local symbol names that cannot
2022 be promoted global. We should never put into a constant pool
2023 objects that cannot be duplicated across partitions. */
2024 if (DECL_IN_CONSTANT_POOL (decl))
2025 return SYMBOL_DUPLICATE;
2026 if (DECL_HARD_REGISTER (decl))
2027 return SYMBOL_DUPLICATE;
2028 gcc_checking_assert (vnode->definition);
2030 /* Functions that are cloned may stay in callgraph even if they are unused.
2031 Handle them as external; compute_ltrans_boundary take care to make
2032 proper things to happen (i.e. to make them appear in the boundary but
2033 with body streamed, so clone can me materialized). */
2034 else if (!dyn_cast <cgraph_node *> (this)->function_symbol ()->definition)
2035 return SYMBOL_EXTERNAL;
2037 /* Linker discardable symbols are duplicated to every use unless they are
2038 keyed. */
2039 if (DECL_ONE_ONLY (decl)
2040 && !force_output
2041 && !forced_by_abi
2042 && !used_from_object_file_p ())
2043 return SYMBOL_DUPLICATE;
2045 return SYMBOL_PARTITION;
2048 /* Return true when symbol is known to be non-zero. */
2050 bool
2051 symtab_node::nonzero_address ()
2053 /* Weakrefs may be NULL when their target is not defined. */
2054 if (alias && weakref)
2056 if (analyzed)
2058 symtab_node *target = ultimate_alias_target ();
2060 if (target->alias && target->weakref)
2061 return false;
2062 /* We cannot recurse to target::nonzero. It is possible that the
2063 target is used only via the alias.
2064 We may walk references and look for strong use, but we do not know
2065 if this strong use will survive to final binary, so be
2066 conservative here.
2067 ??? Maybe we could do the lookup during late optimization that
2068 could be useful to eliminate the NULL pointer checks in LTO
2069 programs. */
2070 if (target->definition && !DECL_EXTERNAL (target->decl))
2071 return true;
2072 if (target->resolution != LDPR_UNKNOWN
2073 && target->resolution != LDPR_UNDEF
2074 && !target->can_be_discarded_p ()
2075 && flag_delete_null_pointer_checks)
2076 return true;
2077 return false;
2079 else
2080 return false;
2083 /* With !flag_delete_null_pointer_checks we assume that symbols may
2084 bind to NULL. This is on by default on embedded targets only.
2086 Otherwise all non-WEAK symbols must be defined and thus non-NULL or
2087 linking fails. Important case of WEAK we want to do well are comdats,
2088 which also must be defined somewhere.
2090 When parsing, beware the cases when WEAK attribute is added later. */
2091 if ((!DECL_WEAK (decl) || DECL_COMDAT (decl))
2092 && flag_delete_null_pointer_checks)
2094 refuse_visibility_changes = true;
2095 return true;
2098 /* If target is defined and not extern, we know it will be
2099 output and thus it will bind to non-NULL.
2100 Play safe for flag_delete_null_pointer_checks where weak definition may
2101 be re-defined by NULL. */
2102 if (definition && !DECL_EXTERNAL (decl)
2103 && (flag_delete_null_pointer_checks || !DECL_WEAK (decl)))
2105 if (!DECL_WEAK (decl))
2106 refuse_visibility_changes = true;
2107 return true;
2110 /* As the last resort, check the resolution info. */
2111 if (resolution != LDPR_UNKNOWN
2112 && resolution != LDPR_UNDEF
2113 && !can_be_discarded_p ()
2114 && flag_delete_null_pointer_checks)
2115 return true;
2116 return false;
2119 /* Return 0 if symbol is known to have different address than S2,
2120 Return 1 if symbol is known to have same address as S2,
2121 return -1 otherwise.
2123 If MEMORY_ACCESSED is true, assume that both memory pointer to THIS
2124 and S2 is going to be accessed. This eliminates the situations when
2125 either THIS or S2 is NULL and is useful for comparing bases when deciding
2126 about memory aliasing. */
2128 symtab_node::equal_address_to (symtab_node *s2, bool memory_accessed)
2130 enum availability avail1, avail2;
2132 /* A Shortcut: equivalent symbols are always equivalent. */
2133 if (this == s2)
2134 return 1;
2136 /* Unwind transparent aliases first; those are always equal to their
2137 target. */
2138 if (this->transparent_alias && this->analyzed)
2139 return this->get_alias_target ()->equal_address_to (s2);
2140 while (s2->transparent_alias && s2->analyzed)
2141 s2 = s2->get_alias_target();
2143 if (this == s2)
2144 return 1;
2146 /* For non-interposable aliases, lookup and compare their actual definitions.
2147 Also check if the symbol needs to bind to given definition. */
2148 symtab_node *rs1 = ultimate_alias_target (&avail1);
2149 symtab_node *rs2 = s2->ultimate_alias_target (&avail2);
2150 bool binds_local1 = rs1->analyzed && decl_binds_to_current_def_p (this->decl);
2151 bool binds_local2 = rs2->analyzed && decl_binds_to_current_def_p (s2->decl);
2152 bool really_binds_local1 = binds_local1;
2153 bool really_binds_local2 = binds_local2;
2155 /* Addresses of vtables and virtual functions cannot be used by user
2156 code and are used only within speculation. In this case we may make
2157 symbol equivalent to its alias even if interposition may break this
2158 rule. Doing so will allow us to turn speculative inlining into
2159 non-speculative more aggressively. */
2160 if (DECL_VIRTUAL_P (this->decl) && avail1 >= AVAIL_AVAILABLE)
2161 binds_local1 = true;
2162 if (DECL_VIRTUAL_P (s2->decl) && avail2 >= AVAIL_AVAILABLE)
2163 binds_local2 = true;
2165 /* If both definitions are available we know that even if they are bound
2166 to other unit they must be defined same way and therefore we can use
2167 equivalence test. */
2168 if (rs1 != rs2 && avail1 >= AVAIL_AVAILABLE && avail2 >= AVAIL_AVAILABLE)
2169 binds_local1 = binds_local2 = true;
2171 if (binds_local1 && binds_local2 && rs1 == rs2)
2173 /* We made use of the fact that alias is not weak. */
2174 if (rs1 != this)
2175 refuse_visibility_changes = true;
2176 if (rs2 != s2)
2177 s2->refuse_visibility_changes = true;
2178 return 1;
2181 /* If both symbols may resolve to NULL, we cannot really prove them
2182 different. */
2183 if (!memory_accessed && !nonzero_address () && !s2->nonzero_address ())
2184 return -1;
2186 /* Except for NULL, functions and variables never overlap. */
2187 if (TREE_CODE (decl) != TREE_CODE (s2->decl))
2188 return 0;
2190 /* If one of the symbols is unresolved alias, punt. */
2191 if (rs1->alias || rs2->alias)
2192 return -1;
2194 /* If we have a non-interposale definition of at least one of the symbols
2195 and the other symbol is different, we know other unit cannot interpose
2196 it to the first symbol; all aliases of the definition needs to be
2197 present in the current unit. */
2198 if (((really_binds_local1 || really_binds_local2)
2199 /* If we have both definitions and they are different, we know they
2200 will be different even in units they binds to. */
2201 || (binds_local1 && binds_local2))
2202 && rs1 != rs2)
2204 /* We make use of the fact that one symbol is not alias of the other
2205 and that the definition is non-interposable. */
2206 refuse_visibility_changes = true;
2207 s2->refuse_visibility_changes = true;
2208 rs1->refuse_visibility_changes = true;
2209 rs2->refuse_visibility_changes = true;
2210 return 0;
2213 /* TODO: Alias oracle basically assume that addresses of global variables
2214 are different unless they are declared as alias of one to another while
2215 the code folding comparisons doesn't.
2216 We probably should be consistent and use this fact here, too, but for
2217 the moment return false only when we are called from the alias oracle. */
2219 return memory_accessed && rs1 != rs2 ? 0 : -1;
2222 /* Worker for call_for_symbol_and_aliases. */
2224 bool
2225 symtab_node::call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *,
2226 void *),
2227 void *data,
2228 bool include_overwritable)
2230 ipa_ref *ref;
2231 FOR_EACH_ALIAS (this, ref)
2233 symtab_node *alias = ref->referring;
2234 if (include_overwritable
2235 || alias->get_availability () > AVAIL_INTERPOSABLE)
2236 if (alias->call_for_symbol_and_aliases (callback, data,
2237 include_overwritable))
2238 return true;
2240 return false;
2243 /* Return true if address of N is possibly compared. */
2245 static bool
2246 address_matters_1 (symtab_node *n, void *)
2248 struct ipa_ref *ref;
2250 if (!n->address_can_be_compared_p ())
2251 return false;
2252 if (n->externally_visible || n->force_output)
2253 return true;
2255 for (unsigned int i = 0; n->iterate_referring (i, ref); i++)
2256 if (ref->address_matters_p ())
2257 return true;
2258 return false;
2261 /* Return true if symbol's address may possibly be compared to other
2262 symbol's address. */
2264 bool
2265 symtab_node::address_matters_p ()
2267 gcc_assert (!alias);
2268 return call_for_symbol_and_aliases (address_matters_1, NULL, true);
2271 /* Return true if symbol's alignment may be increased. */
2273 bool
2274 symtab_node::can_increase_alignment_p (void)
2276 symtab_node *target = ultimate_alias_target ();
2278 /* For now support only variables. */
2279 if (!VAR_P (decl))
2280 return false;
2282 /* With -fno-toplevel-reorder we may have already output the constant. */
2283 if (TREE_ASM_WRITTEN (target->decl))
2284 return false;
2286 /* If target is already placed in an anchor, we cannot touch its
2287 alignment. */
2288 if (DECL_RTL_SET_P (target->decl)
2289 && MEM_P (DECL_RTL (target->decl))
2290 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (DECL_RTL (target->decl), 0)))
2291 return false;
2293 /* Constant pool entries may be shared. */
2294 if (DECL_IN_CONSTANT_POOL (target->decl))
2295 return false;
2297 /* We cannot change alignment of symbols that may bind to symbols
2298 in other translation unit that may contain a definition with lower
2299 alignment. */
2300 if (!decl_binds_to_current_def_p (decl))
2301 return false;
2303 /* When compiling partition, be sure the symbol is not output by other
2304 partition. */
2305 if (flag_ltrans
2306 && (target->in_other_partition
2307 || target->get_partitioning_class () == SYMBOL_DUPLICATE))
2308 return false;
2310 /* Do not override the alignment as specified by the ABI when the used
2311 attribute is set. */
2312 if (DECL_PRESERVE_P (decl) || DECL_PRESERVE_P (target->decl))
2313 return false;
2315 /* Do not override explicit alignment set by the user when an explicit
2316 section name is also used. This is a common idiom used by many
2317 software projects. */
2318 if (DECL_SECTION_NAME (target->decl) != NULL && !target->implicit_section)
2319 return false;
2321 return true;
2324 /* Worker for symtab_node::increase_alignment. */
2326 static bool
2327 increase_alignment_1 (symtab_node *n, void *v)
2329 unsigned int align = (size_t)v;
2330 if (DECL_ALIGN (n->decl) < align
2331 && n->can_increase_alignment_p ())
2333 SET_DECL_ALIGN (n->decl, align);
2334 DECL_USER_ALIGN (n->decl) = 1;
2336 return false;
2339 /* Increase alignment of THIS to ALIGN. */
2341 void
2342 symtab_node::increase_alignment (unsigned int align)
2344 gcc_assert (can_increase_alignment_p () && align <= MAX_OFILE_ALIGNMENT);
2345 ultimate_alias_target()->call_for_symbol_and_aliases (increase_alignment_1,
2346 (void *)(size_t) align,
2347 true);
2348 gcc_assert (DECL_ALIGN (decl) >= align);
2351 /* Helper for symtab_node::definition_alignment. */
2353 static bool
2354 get_alignment_1 (symtab_node *n, void *v)
2356 *((unsigned int *)v) = MAX (*((unsigned int *)v), DECL_ALIGN (n->decl));
2357 return false;
2360 /* Return desired alignment of the definition. This is NOT alignment useful
2361 to access THIS, because THIS may be interposable and DECL_ALIGN should
2362 be used instead. It however must be guaranteed when output definition
2363 of THIS. */
2365 unsigned int
2366 symtab_node::definition_alignment ()
2368 unsigned int align = 0;
2369 gcc_assert (!alias);
2370 call_for_symbol_and_aliases (get_alignment_1, &align, true);
2371 return align;
2374 /* Return symbol used to separate symbol name from suffix. */
2376 char
2377 symbol_table::symbol_suffix_separator ()
2379 #ifndef NO_DOT_IN_LABEL
2380 return '.';
2381 #elif !defined NO_DOLLAR_IN_LABEL
2382 return '$';
2383 #else
2384 return '_';
2385 #endif
2388 /* Return true when references to this symbol from REF must bind to current
2389 definition in final executable. */
2391 bool
2392 symtab_node::binds_to_current_def_p (symtab_node *ref)
2394 if (!definition)
2395 return false;
2396 if (transparent_alias)
2397 return definition
2398 && get_alias_target()->binds_to_current_def_p (ref);
2399 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
2400 if (cnode && cnode->ifunc_resolver)
2401 return false;
2402 if (decl_binds_to_current_def_p (decl))
2403 return true;
2405 /* Inline clones always binds locally. */
2406 if (cnode && cnode->inlined_to)
2407 return true;
2409 if (DECL_EXTERNAL (decl))
2410 return false;
2412 gcc_assert (externally_visible);
2414 if (ref)
2416 cgraph_node *cref = dyn_cast <cgraph_node *> (ref);
2417 if (cref)
2418 ref = cref->inlined_to;
2421 /* If this is a reference from symbol itself and there are no aliases, we
2422 may be sure that the symbol was not interposed by something else because
2423 the symbol itself would be unreachable otherwise. This is important
2424 to optimize recursive functions well.
2426 This assumption may be broken by inlining: if symbol is interposable
2427 but the body is available (i.e. declared inline), inliner may make
2428 the body reachable even with interposition. */
2429 if (this == ref && !has_aliases_p ()
2430 && (!cnode
2431 || symtab->state >= IPA_SSA_AFTER_INLINING
2432 || get_availability () >= AVAIL_INTERPOSABLE))
2433 return true;
2436 /* References within one comdat group are always bound in a group. */
2437 if (ref
2438 && symtab->state >= IPA_SSA_AFTER_INLINING
2439 && get_comdat_group ()
2440 && get_comdat_group () == ref->get_comdat_group ())
2441 return true;
2443 return false;
2446 /* Return true if symbol should be output to the symbol table. */
2448 bool
2449 symtab_node::output_to_lto_symbol_table_p (void)
2451 /* Only externally visible symbols matter. */
2452 if (!TREE_PUBLIC (decl))
2453 return false;
2454 if (!real_symbol_p ())
2455 return false;
2456 /* FIXME: variables probably should not be considered as real symbols at
2457 first place. */
2458 if (VAR_P (decl) && DECL_HARD_REGISTER (decl))
2459 return false;
2460 if (TREE_CODE (decl) == FUNCTION_DECL && !definition
2461 && fndecl_built_in_p (decl))
2463 /* Builtins like those for most math functions have actual implementations
2464 in libraries so make sure to output references into the symbol table to
2465 make those libraries referenced. Note this is incomplete handling for
2466 now and only covers math functions. */
2467 if (builtin_with_linkage_p (decl))
2468 return true;
2469 else
2470 return false;
2473 /* We have real symbol that should be in symbol table. However try to trim
2474 down the references to libraries bit more because linker will otherwise
2475 bring unnecessary object files into the final link.
2476 FIXME: The following checks can easily be confused i.e. by self recursive
2477 function or self-referring variable. */
2479 /* We keep external functions in symtab for sake of inlining
2480 and devirtualization. We do not want to see them in symbol table as
2481 references unless they are really used. */
2482 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
2483 if (cnode && (!definition || DECL_EXTERNAL (decl))
2484 && cnode->callers)
2485 return true;
2487 /* Ignore all references from external vars initializers - they are not really
2488 part of the compilation unit until they are used by folding. Some symbols,
2489 like references to external construction vtables cannot be referred to at
2490 all. We decide this at can_refer_decl_in_current_unit_p. */
2491 if (!definition || DECL_EXTERNAL (decl))
2493 int i;
2494 struct ipa_ref *ref;
2495 for (i = 0; iterate_referring (i, ref); i++)
2497 if (ref->use == IPA_REF_ALIAS)
2498 continue;
2499 if (is_a <cgraph_node *> (ref->referring))
2500 return true;
2501 if (!DECL_EXTERNAL (ref->referring->decl))
2502 return true;
2504 return false;
2506 return true;