2017-05-11 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / symtab.c
blob6c41dcff4b70b6143b99d190f366fb88583d01e9
1 /* Symbol table.
2 Copyright (C) 2012-2017 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"
39 static const char *ipa_ref_use_name[] = {"read","write","addr","alias","chkp"};
41 const char * const ld_plugin_symbol_resolution_names[]=
43 "",
44 "undef",
45 "prevailing_def",
46 "prevailing_def_ironly",
47 "preempted_reg",
48 "preempted_ir",
49 "resolved_ir",
50 "resolved_exec",
51 "resolved_dyn",
52 "prevailing_def_ironly_exp"
55 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at ALIAS
56 until we find an identifier that is not itself a transparent alias. */
58 static inline tree
59 ultimate_transparent_alias_target (tree alias)
61 tree target = alias;
63 while (IDENTIFIER_TRANSPARENT_ALIAS (target))
65 gcc_checking_assert (TREE_CHAIN (target));
66 target = TREE_CHAIN (target);
68 gcc_checking_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
69 && ! TREE_CHAIN (target));
71 return target;
75 /* Hash asmnames ignoring the user specified marks. */
77 hashval_t
78 symbol_table::decl_assembler_name_hash (const_tree asmname)
80 if (IDENTIFIER_POINTER (asmname)[0] == '*')
82 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
83 size_t ulp_len = strlen (user_label_prefix);
85 if (ulp_len == 0)
87 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
88 decl_str += ulp_len;
90 return htab_hash_string (decl_str);
93 return htab_hash_string (IDENTIFIER_POINTER (asmname));
96 /* Return true if assembler names NAME1 and NAME2 leads to the same symbol
97 name. */
99 bool
100 symbol_table::assembler_names_equal_p (const char *name1, const char *name2)
102 if (name1 != name2)
104 if (name1[0] == '*')
106 size_t ulp_len = strlen (user_label_prefix);
108 name1 ++;
110 if (ulp_len == 0)
112 else if (strncmp (name1, user_label_prefix, ulp_len) == 0)
113 name1 += ulp_len;
114 else
115 return false;
117 if (name2[0] == '*')
119 size_t ulp_len = strlen (user_label_prefix);
121 name2 ++;
123 if (ulp_len == 0)
125 else if (strncmp (name2, user_label_prefix, ulp_len) == 0)
126 name2 += ulp_len;
127 else
128 return false;
130 return !strcmp (name1, name2);
132 return true;
135 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
137 bool
138 symbol_table::decl_assembler_name_equal (tree decl, const_tree asmname)
140 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
141 const char *decl_str;
142 const char *asmname_str;
144 if (decl_asmname == asmname)
145 return true;
147 decl_str = IDENTIFIER_POINTER (decl_asmname);
148 asmname_str = IDENTIFIER_POINTER (asmname);
149 return assembler_names_equal_p (decl_str, asmname_str);
153 /* Returns nonzero if P1 and P2 are equal. */
155 /* Insert NODE to assembler name hash. */
157 void
158 symbol_table::insert_to_assembler_name_hash (symtab_node *node,
159 bool with_clones)
161 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
162 return;
163 gcc_checking_assert (!node->previous_sharing_asm_name
164 && !node->next_sharing_asm_name);
165 if (assembler_name_hash)
167 symtab_node **aslot;
168 cgraph_node *cnode;
169 tree decl = node->decl;
171 tree name = DECL_ASSEMBLER_NAME (node->decl);
173 /* C++ FE can produce decls without associated assembler name and insert
174 them to symtab to hold section or TLS information. */
175 if (!name)
176 return;
178 hashval_t hash = decl_assembler_name_hash (name);
179 aslot = assembler_name_hash->find_slot_with_hash (name, hash, INSERT);
180 gcc_assert (*aslot != node);
181 node->next_sharing_asm_name = (symtab_node *)*aslot;
182 if (*aslot != NULL)
183 (*aslot)->previous_sharing_asm_name = node;
184 *aslot = node;
186 /* Update also possible inline clones sharing a decl. */
187 cnode = dyn_cast <cgraph_node *> (node);
188 if (cnode && cnode->clones && with_clones)
189 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
190 if (cnode->decl == decl)
191 insert_to_assembler_name_hash (cnode, true);
196 /* Remove NODE from assembler name hash. */
198 void
199 symbol_table::unlink_from_assembler_name_hash (symtab_node *node,
200 bool with_clones)
202 if (assembler_name_hash)
204 cgraph_node *cnode;
205 tree decl = node->decl;
207 if (node->next_sharing_asm_name)
208 node->next_sharing_asm_name->previous_sharing_asm_name
209 = node->previous_sharing_asm_name;
210 if (node->previous_sharing_asm_name)
212 node->previous_sharing_asm_name->next_sharing_asm_name
213 = node->next_sharing_asm_name;
215 else
217 tree name = DECL_ASSEMBLER_NAME (node->decl);
218 symtab_node **slot;
220 if (!name)
221 return;
223 hashval_t hash = decl_assembler_name_hash (name);
224 slot = assembler_name_hash->find_slot_with_hash (name, hash,
225 NO_INSERT);
226 gcc_assert (*slot == node);
227 if (!node->next_sharing_asm_name)
228 assembler_name_hash->clear_slot (slot);
229 else
230 *slot = node->next_sharing_asm_name;
232 node->next_sharing_asm_name = NULL;
233 node->previous_sharing_asm_name = NULL;
235 /* Update also possible inline clones sharing a decl. */
236 cnode = dyn_cast <cgraph_node *> (node);
237 if (cnode && cnode->clones && with_clones)
238 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
239 if (cnode->decl == decl)
240 unlink_from_assembler_name_hash (cnode, true);
244 /* Arrange node to be first in its entry of assembler_name_hash. */
246 void
247 symbol_table::symtab_prevail_in_asm_name_hash (symtab_node *node)
249 unlink_from_assembler_name_hash (node, false);
250 insert_to_assembler_name_hash (node, false);
253 /* Initalize asm name hash unless. */
255 void
256 symbol_table::symtab_initialize_asm_name_hash (void)
258 symtab_node *node;
259 if (!assembler_name_hash)
261 assembler_name_hash = hash_table<asmname_hasher>::create_ggc (10);
262 FOR_EACH_SYMBOL (node)
263 insert_to_assembler_name_hash (node, false);
267 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
269 void
270 symbol_table::change_decl_assembler_name (tree decl, tree name)
272 symtab_node *node = NULL;
274 /* We can have user ASM names on things, like global register variables, that
275 are not in the symbol table. */
276 if ((VAR_P (decl) && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
277 || TREE_CODE (decl) == FUNCTION_DECL)
278 node = symtab_node::get (decl);
279 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
281 SET_DECL_ASSEMBLER_NAME (decl, name);
282 if (node)
283 insert_to_assembler_name_hash (node, true);
285 else
287 if (name == DECL_ASSEMBLER_NAME (decl))
288 return;
290 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
291 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
292 : NULL);
293 if (node)
294 unlink_from_assembler_name_hash (node, true);
296 const char *old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
297 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
298 && DECL_RTL_SET_P (decl))
299 warning (0, "%qD renamed after being referenced in assembly", decl);
301 SET_DECL_ASSEMBLER_NAME (decl, name);
302 if (alias)
304 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
305 TREE_CHAIN (name) = alias;
307 /* If we change assembler name, also all transparent aliases must
308 be updated. There are three kinds - those having same assembler name,
309 those being renamed in varasm.c and weakref being renamed by the
310 assembler. */
311 if (node)
313 insert_to_assembler_name_hash (node, true);
314 ipa_ref *ref;
315 for (unsigned i = 0; node->iterate_direct_aliases (i, ref); i++)
317 struct symtab_node *alias = ref->referring;
318 if (alias->transparent_alias && !alias->weakref
319 && symbol_table::assembler_names_equal_p
320 (old_name, IDENTIFIER_POINTER (
321 DECL_ASSEMBLER_NAME (alias->decl))))
322 change_decl_assembler_name (alias->decl, name);
323 else if (alias->transparent_alias
324 && IDENTIFIER_TRANSPARENT_ALIAS (alias->decl))
326 gcc_assert (TREE_CHAIN (DECL_ASSEMBLER_NAME (alias->decl))
327 && IDENTIFIER_TRANSPARENT_ALIAS
328 (DECL_ASSEMBLER_NAME (alias->decl)));
330 TREE_CHAIN (DECL_ASSEMBLER_NAME (alias->decl)) =
331 ultimate_transparent_alias_target
332 (DECL_ASSEMBLER_NAME (node->decl));
334 #ifdef ASM_OUTPUT_WEAKREF
335 else gcc_assert (!alias->transparent_alias || alias->weakref);
336 #else
337 else gcc_assert (!alias->transparent_alias);
338 #endif
340 gcc_assert (!node->transparent_alias || !node->definition
341 || node->weakref
342 || TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
343 || symbol_table::assembler_names_equal_p
344 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
345 IDENTIFIER_POINTER
346 (DECL_ASSEMBLER_NAME
347 (node->get_alias_target ()->decl))));
352 /* Hash sections by their names. */
354 hashval_t
355 section_name_hasher::hash (section_hash_entry *n)
357 return htab_hash_string (n->name);
360 /* Return true if section P1 name equals to P2. */
362 bool
363 section_name_hasher::equal (section_hash_entry *n1, const char *name)
365 return n1->name == name || !strcmp (n1->name, name);
368 /* Add node into symbol table. This function is not used directly, but via
369 cgraph/varpool node creation routines. */
371 void
372 symtab_node::register_symbol (void)
374 symtab->register_symbol (this);
376 if (!decl->decl_with_vis.symtab_node)
377 decl->decl_with_vis.symtab_node = this;
379 ref_list.clear ();
381 /* Be sure to do this last; C++ FE might create new nodes via
382 DECL_ASSEMBLER_NAME langhook! */
383 symtab->insert_to_assembler_name_hash (this, false);
386 /* Remove NODE from same comdat group. */
388 void
389 symtab_node::remove_from_same_comdat_group (void)
391 if (same_comdat_group)
393 symtab_node *prev;
394 for (prev = same_comdat_group;
395 prev->same_comdat_group != this;
396 prev = prev->same_comdat_group)
398 if (same_comdat_group == prev)
399 prev->same_comdat_group = NULL;
400 else
401 prev->same_comdat_group = same_comdat_group;
402 same_comdat_group = NULL;
403 set_comdat_group (NULL);
407 /* Remove node from symbol table. This function is not used directly, but via
408 cgraph/varpool node removal routines. */
410 void
411 symtab_node::unregister (void)
413 remove_all_references ();
414 remove_all_referring ();
416 /* Remove reference to section. */
417 set_section_for_node (NULL);
419 remove_from_same_comdat_group ();
421 symtab->unregister (this);
423 /* During LTO symtab merging we temporarily corrupt decl to symtab node
424 hash. */
425 gcc_assert (decl->decl_with_vis.symtab_node || in_lto_p);
426 if (decl->decl_with_vis.symtab_node == this)
428 symtab_node *replacement_node = NULL;
429 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
430 replacement_node = cnode->find_replacement ();
431 decl->decl_with_vis.symtab_node = replacement_node;
433 if (!is_a <varpool_node *> (this) || !DECL_HARD_REGISTER (decl))
434 symtab->unlink_from_assembler_name_hash (this, false);
435 if (in_init_priority_hash)
436 symtab->init_priority_hash->remove (this);
440 /* Remove symbol from symbol table. */
442 void
443 symtab_node::remove (void)
445 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
446 cnode->remove ();
447 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
448 vnode->remove ();
451 /* Add NEW_ to the same comdat group that OLD is in. */
453 void
454 symtab_node::add_to_same_comdat_group (symtab_node *old_node)
456 gcc_assert (old_node->get_comdat_group ());
457 gcc_assert (!same_comdat_group);
458 gcc_assert (this != old_node);
460 set_comdat_group (old_node->get_comdat_group ());
461 same_comdat_group = old_node;
462 if (!old_node->same_comdat_group)
463 old_node->same_comdat_group = this;
464 else
466 symtab_node *n;
467 for (n = old_node->same_comdat_group;
468 n->same_comdat_group != old_node;
469 n = n->same_comdat_group)
471 n->same_comdat_group = this;
475 /* Dissolve the same_comdat_group list in which NODE resides. */
477 void
478 symtab_node::dissolve_same_comdat_group_list (void)
480 symtab_node *n = this;
481 symtab_node *next;
483 if (!same_comdat_group)
484 return;
487 next = n->same_comdat_group;
488 n->same_comdat_group = NULL;
489 /* Clear comdat_group for comdat locals, since
490 make_decl_local doesn't. */
491 if (!TREE_PUBLIC (n->decl))
492 n->set_comdat_group (NULL);
493 n = next;
495 while (n != this);
498 /* Return printable assembler name of NODE.
499 This function is used only for debugging. When assembler name
500 is unknown go with identifier name. */
502 const char *
503 symtab_node::asm_name () const
505 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
506 return name ();
507 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
510 /* Return printable identifier name. */
512 const char *
513 symtab_node::name () const
515 if (!DECL_NAME (decl))
517 if (DECL_ASSEMBLER_NAME_SET_P (decl))
518 return asm_name ();
519 else
520 return "<unnamed>";
522 return lang_hooks.decl_printable_name (decl, 2);
525 /* Return ipa reference from this symtab_node to
526 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
527 of the use. */
529 ipa_ref *
530 symtab_node::create_reference (symtab_node *referred_node,
531 enum ipa_ref_use use_type)
533 return create_reference (referred_node, use_type, NULL);
537 /* Return ipa reference from this symtab_node to
538 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
539 of the use and STMT the statement (if it exists). */
541 ipa_ref *
542 symtab_node::create_reference (symtab_node *referred_node,
543 enum ipa_ref_use use_type, gimple *stmt)
545 ipa_ref *ref = NULL, *ref2 = NULL;
546 ipa_ref_list *list, *list2;
547 ipa_ref_t *old_references;
549 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
550 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
552 list = &ref_list;
553 old_references = vec_safe_address (list->references);
554 vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
555 ref = &list->references->last ();
557 list2 = &referred_node->ref_list;
559 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
560 if(use_type == IPA_REF_ALIAS)
562 list2->referring.safe_insert (0, ref);
563 ref->referred_index = 0;
565 for (unsigned int i = 1; i < list2->referring.length (); i++)
566 list2->referring[i]->referred_index = i;
568 else
570 list2->referring.safe_push (ref);
571 ref->referred_index = list2->referring.length () - 1;
574 ref->referring = this;
575 ref->referred = referred_node;
576 ref->stmt = stmt;
577 ref->lto_stmt_uid = 0;
578 ref->use = use_type;
579 ref->speculative = 0;
581 /* If vector was moved in memory, update pointers. */
582 if (old_references != list->references->address ())
584 int i;
585 for (i = 0; iterate_reference(i, ref2); i++)
586 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
588 return ref;
591 ipa_ref *
592 symtab_node::maybe_create_reference (tree val, gimple *stmt)
594 STRIP_NOPS (val);
595 ipa_ref_use use_type;
597 switch (TREE_CODE (val))
599 case VAR_DECL:
600 use_type = IPA_REF_LOAD;
601 break;
602 case ADDR_EXPR:
603 use_type = IPA_REF_ADDR;
604 break;
605 default:
606 gcc_assert (!handled_component_p (val));
607 return NULL;
610 val = get_base_var (val);
611 if (val && VAR_OR_FUNCTION_DECL_P (val))
613 symtab_node *referred = symtab_node::get (val);
614 gcc_checking_assert (referred);
615 return create_reference (referred, use_type, stmt);
617 return NULL;
620 /* Clone all references from symtab NODE to this symtab_node. */
622 void
623 symtab_node::clone_references (symtab_node *node)
625 ipa_ref *ref = NULL, *ref2 = NULL;
626 int i;
627 for (i = 0; node->iterate_reference (i, ref); i++)
629 bool speculative = ref->speculative;
630 unsigned int stmt_uid = ref->lto_stmt_uid;
632 ref2 = create_reference (ref->referred, ref->use, ref->stmt);
633 ref2->speculative = speculative;
634 ref2->lto_stmt_uid = stmt_uid;
638 /* Clone all referring from symtab NODE to this symtab_node. */
640 void
641 symtab_node::clone_referring (symtab_node *node)
643 ipa_ref *ref = NULL, *ref2 = NULL;
644 int i;
645 for (i = 0; node->iterate_referring(i, ref); i++)
647 bool speculative = ref->speculative;
648 unsigned int stmt_uid = ref->lto_stmt_uid;
650 ref2 = ref->referring->create_reference (this, ref->use, ref->stmt);
651 ref2->speculative = speculative;
652 ref2->lto_stmt_uid = stmt_uid;
656 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
658 ipa_ref *
659 symtab_node::clone_reference (ipa_ref *ref, gimple *stmt)
661 bool speculative = ref->speculative;
662 unsigned int stmt_uid = ref->lto_stmt_uid;
663 ipa_ref *ref2;
665 ref2 = create_reference (ref->referred, ref->use, stmt);
666 ref2->speculative = speculative;
667 ref2->lto_stmt_uid = stmt_uid;
668 return ref2;
671 /* Find the structure describing a reference to REFERRED_NODE
672 and associated with statement STMT. */
674 ipa_ref *
675 symtab_node::find_reference (symtab_node *referred_node,
676 gimple *stmt, unsigned int lto_stmt_uid)
678 ipa_ref *r = NULL;
679 int i;
681 for (i = 0; iterate_reference (i, r); i++)
682 if (r->referred == referred_node
683 && !r->speculative
684 && ((stmt && r->stmt == stmt)
685 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
686 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
687 return r;
688 return NULL;
691 /* Remove all references that are associated with statement STMT. */
693 void
694 symtab_node::remove_stmt_references (gimple *stmt)
696 ipa_ref *r = NULL;
697 int i = 0;
699 while (iterate_reference (i, r))
700 if (r->stmt == stmt)
701 r->remove_reference ();
702 else
703 i++;
706 /* Remove all stmt references in non-speculative references.
707 Those are not maintained during inlining & clonning.
708 The exception are speculative references that are updated along
709 with callgraph edges associated with them. */
711 void
712 symtab_node::clear_stmts_in_references (void)
714 ipa_ref *r = NULL;
715 int i;
717 for (i = 0; iterate_reference (i, r); i++)
718 if (!r->speculative)
720 r->stmt = NULL;
721 r->lto_stmt_uid = 0;
725 /* Remove all references in ref list. */
727 void
728 symtab_node::remove_all_references (void)
730 while (vec_safe_length (ref_list.references))
731 ref_list.references->last ().remove_reference ();
732 vec_free (ref_list.references);
735 /* Remove all referring items in ref list. */
737 void
738 symtab_node::remove_all_referring (void)
740 while (ref_list.referring.length ())
741 ref_list.referring.last ()->remove_reference ();
742 ref_list.referring.release ();
745 /* Dump references in ref list to FILE. */
747 void
748 symtab_node::dump_references (FILE *file)
750 ipa_ref *ref = NULL;
751 int i;
752 for (i = 0; iterate_reference (i, ref); i++)
754 fprintf (file, "%s/%i (%s)",
755 ref->referred->asm_name (),
756 ref->referred->order,
757 ipa_ref_use_name [ref->use]);
758 if (ref->speculative)
759 fprintf (file, " (speculative)");
761 fprintf (file, "\n");
764 /* Dump referring in list to FILE. */
766 void
767 symtab_node::dump_referring (FILE *file)
769 ipa_ref *ref = NULL;
770 int i;
771 for (i = 0; iterate_referring(i, ref); i++)
773 fprintf (file, "%s/%i (%s)",
774 ref->referring->asm_name (),
775 ref->referring->order,
776 ipa_ref_use_name [ref->use]);
777 if (ref->speculative)
778 fprintf (file, " (speculative)");
780 fprintf (file, "\n");
783 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
785 /* Dump base fields of symtab nodes to F. Not to be used directly. */
787 void
788 symtab_node::dump_base (FILE *f)
790 static const char * const visibility_types[] = {
791 "default", "protected", "hidden", "internal"
794 fprintf (f, "%s/%i (%s)", asm_name (), order, name ());
795 dump_addr (f, " @", (void *)this);
796 fprintf (f, "\n Type: %s", symtab_type_names[type]);
798 if (definition)
799 fprintf (f, " definition");
800 if (analyzed)
801 fprintf (f, " analyzed");
802 if (alias)
803 fprintf (f, " alias");
804 if (transparent_alias)
805 fprintf (f, " transparent_alias");
806 if (weakref)
807 fprintf (f, " weakref");
808 if (cpp_implicit_alias)
809 fprintf (f, " cpp_implicit_alias");
810 if (alias_target)
811 fprintf (f, " target:%s",
812 DECL_P (alias_target)
813 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
814 (alias_target))
815 : IDENTIFIER_POINTER (alias_target));
816 if (body_removed)
817 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
818 fprintf (f, "\n Visibility:");
819 if (in_other_partition)
820 fprintf (f, " in_other_partition");
821 if (used_from_other_partition)
822 fprintf (f, " used_from_other_partition");
823 if (force_output)
824 fprintf (f, " force_output");
825 if (forced_by_abi)
826 fprintf (f, " forced_by_abi");
827 if (externally_visible)
828 fprintf (f, " externally_visible");
829 if (no_reorder)
830 fprintf (f, " no_reorder");
831 if (resolution != LDPR_UNKNOWN)
832 fprintf (f, " %s",
833 ld_plugin_symbol_resolution_names[(int)resolution]);
834 if (TREE_ASM_WRITTEN (decl))
835 fprintf (f, " asm_written");
836 if (DECL_EXTERNAL (decl))
837 fprintf (f, " external");
838 if (TREE_PUBLIC (decl))
839 fprintf (f, " public");
840 if (DECL_COMMON (decl))
841 fprintf (f, " common");
842 if (DECL_WEAK (decl))
843 fprintf (f, " weak");
844 if (DECL_DLLIMPORT_P (decl))
845 fprintf (f, " dll_import");
846 if (DECL_COMDAT (decl))
847 fprintf (f, " comdat");
848 if (get_comdat_group ())
849 fprintf (f, " comdat_group:%s",
850 IDENTIFIER_POINTER (get_comdat_group_id ()));
851 if (DECL_ONE_ONLY (decl))
852 fprintf (f, " one_only");
853 if (get_section ())
854 fprintf (f, " section:%s",
855 get_section ());
856 if (implicit_section)
857 fprintf (f," (implicit_section)");
858 if (DECL_VISIBILITY_SPECIFIED (decl))
859 fprintf (f, " visibility_specified");
860 if (DECL_VISIBILITY (decl))
861 fprintf (f, " visibility:%s",
862 visibility_types [DECL_VISIBILITY (decl)]);
863 if (DECL_VIRTUAL_P (decl))
864 fprintf (f, " virtual");
865 if (DECL_ARTIFICIAL (decl))
866 fprintf (f, " artificial");
867 if (TREE_CODE (decl) == FUNCTION_DECL)
869 if (DECL_STATIC_CONSTRUCTOR (decl))
870 fprintf (f, " constructor");
871 if (DECL_STATIC_DESTRUCTOR (decl))
872 fprintf (f, " destructor");
874 fprintf (f, "\n");
876 if (same_comdat_group)
877 fprintf (f, " Same comdat group as: %s/%i\n",
878 same_comdat_group->asm_name (),
879 same_comdat_group->order);
880 if (next_sharing_asm_name)
881 fprintf (f, " next sharing asm name: %i\n",
882 next_sharing_asm_name->order);
883 if (previous_sharing_asm_name)
884 fprintf (f, " previous sharing asm name: %i\n",
885 previous_sharing_asm_name->order);
887 if (address_taken)
888 fprintf (f, " Address is taken.\n");
889 if (aux)
891 fprintf (f, " Aux:");
892 dump_addr (f, " @", (void *)aux);
893 fprintf (f, "\n");
896 fprintf (f, " References: ");
897 dump_references (f);
898 fprintf (f, " Referring: ");
899 dump_referring (f);
900 if (lto_file_data)
901 fprintf (f, " Read from file: %s\n",
902 lto_file_data->file_name);
905 /* Dump symtab node to F. */
907 void
908 symtab_node::dump (FILE *f)
910 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
911 cnode->dump (f);
912 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
913 vnode->dump (f);
916 /* Dump symbol table to F. */
918 void
919 symtab_node::dump_table (FILE *f)
921 symtab_node *node;
922 fprintf (f, "Symbol table:\n\n");
923 FOR_EACH_SYMBOL (node)
924 node->dump (f);
927 /* Dump symbol table to stderr. */
929 DEBUG_FUNCTION void
930 symtab_node::debug_symtab (void)
932 dump_table (stderr);
936 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
937 Return NULL if there's no such node. */
939 symtab_node *
940 symtab_node::get_for_asmname (const_tree asmname)
942 symtab_node *node;
944 symtab->symtab_initialize_asm_name_hash ();
945 hashval_t hash = symtab->decl_assembler_name_hash (asmname);
946 symtab_node **slot
947 = symtab->assembler_name_hash->find_slot_with_hash (asmname, hash,
948 NO_INSERT);
950 if (slot)
952 node = *slot;
953 return node;
955 return NULL;
958 /* Dump symtab node NODE to stderr. */
960 DEBUG_FUNCTION void
961 symtab_node::debug (void)
963 dump (stderr);
966 /* Verify common part of symtab nodes. */
968 DEBUG_FUNCTION bool
969 symtab_node::verify_base (void)
971 bool error_found = false;
972 symtab_node *hashed_node;
974 if (is_a <cgraph_node *> (this))
976 if (TREE_CODE (decl) != FUNCTION_DECL)
978 error ("function symbol is not function");
979 error_found = true;
982 else if (is_a <varpool_node *> (this))
984 if (!VAR_P (decl))
986 error ("variable symbol is not variable");
987 error_found = true;
990 else
992 error ("node has unknown type");
993 error_found = true;
996 if (symtab->state != LTO_STREAMING)
998 hashed_node = symtab_node::get (decl);
999 if (!hashed_node)
1001 error ("node not found node->decl->decl_with_vis.symtab_node");
1002 error_found = true;
1004 if (hashed_node != this
1005 && (!is_a <cgraph_node *> (this)
1006 || !dyn_cast <cgraph_node *> (this)->clone_of
1007 || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl))
1009 error ("node differs from node->decl->decl_with_vis.symtab_node");
1010 error_found = true;
1013 if (symtab->assembler_name_hash)
1015 hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
1016 if (hashed_node && hashed_node->previous_sharing_asm_name)
1018 error ("assembler name hash list corrupted");
1019 error_found = true;
1021 while (hashed_node)
1023 if (hashed_node == this)
1024 break;
1025 hashed_node = hashed_node->next_sharing_asm_name;
1027 if (!hashed_node
1028 && !(is_a <varpool_node *> (this)
1029 && DECL_HARD_REGISTER (decl)))
1031 error ("node not found in symtab assembler name hash");
1032 error_found = true;
1035 if (previous_sharing_asm_name
1036 && previous_sharing_asm_name->next_sharing_asm_name != this)
1038 error ("double linked list of assembler names corrupted");
1039 error_found = true;
1041 if (body_removed && definition)
1043 error ("node has body_removed but is definition");
1044 error_found = true;
1046 if (analyzed && !definition)
1048 error ("node is analyzed but it is not a definition");
1049 error_found = true;
1051 if (cpp_implicit_alias && !alias)
1053 error ("node is alias but not implicit alias");
1054 error_found = true;
1056 if (alias && !definition && !weakref)
1058 error ("node is alias but not definition");
1059 error_found = true;
1061 if (weakref && !transparent_alias)
1063 error ("node is weakref but not an transparent_alias");
1064 error_found = true;
1066 if (transparent_alias && !alias)
1068 error ("node is transparent_alias but not an alias");
1069 error_found = true;
1071 if (same_comdat_group)
1073 symtab_node *n = same_comdat_group;
1075 if (!n->get_comdat_group ())
1077 error ("node is in same_comdat_group list but has no comdat_group");
1078 error_found = true;
1080 if (n->get_comdat_group () != get_comdat_group ())
1082 error ("same_comdat_group list across different groups");
1083 error_found = true;
1085 if (n->type != type)
1087 error ("mixing different types of symbol in same comdat groups is not supported");
1088 error_found = true;
1090 if (n == this)
1092 error ("node is alone in a comdat group");
1093 error_found = true;
1097 if (!n->same_comdat_group)
1099 error ("same_comdat_group is not a circular list");
1100 error_found = true;
1101 break;
1103 n = n->same_comdat_group;
1105 while (n != this);
1106 if (comdat_local_p ())
1108 ipa_ref *ref = NULL;
1110 for (int i = 0; iterate_referring (i, ref); ++i)
1112 if (!in_same_comdat_group_p (ref->referring))
1114 error ("comdat-local symbol referred to by %s outside its "
1115 "comdat",
1116 identifier_to_locale (ref->referring->name()));
1117 error_found = true;
1122 if (implicit_section && !get_section ())
1124 error ("implicit_section flag is set but section isn't");
1125 error_found = true;
1127 if (get_section () && get_comdat_group ()
1128 && !implicit_section
1129 && !lookup_attribute ("section", DECL_ATTRIBUTES (decl)))
1131 error ("Both section and comdat group is set");
1132 error_found = true;
1134 /* TODO: Add string table for sections, so we do not keep holding duplicated
1135 strings. */
1136 if (alias && definition
1137 && get_section () != get_alias_target ()->get_section ()
1138 && (!get_section()
1139 || !get_alias_target ()->get_section ()
1140 || strcmp (get_section(),
1141 get_alias_target ()->get_section ())))
1143 error ("Alias and target's section differs");
1144 get_alias_target ()->dump (stderr);
1145 error_found = true;
1147 if (alias && definition
1148 && get_comdat_group () != get_alias_target ()->get_comdat_group ())
1150 error ("Alias and target's comdat groups differs");
1151 get_alias_target ()->dump (stderr);
1152 error_found = true;
1154 if (transparent_alias && definition && !weakref)
1156 symtab_node *to = get_alias_target ();
1157 const char *name1
1158 = IDENTIFIER_POINTER (
1159 ultimate_transparent_alias_target (DECL_ASSEMBLER_NAME (decl)));
1160 const char *name2
1161 = IDENTIFIER_POINTER (
1162 ultimate_transparent_alias_target (DECL_ASSEMBLER_NAME (to->decl)));
1163 if (!symbol_table::assembler_names_equal_p (name1, name2))
1165 error ("Transparent alias and target's assembler names differs");
1166 get_alias_target ()->dump (stderr);
1167 error_found = true;
1170 if (transparent_alias && definition
1171 && get_alias_target()->transparent_alias && get_alias_target()->analyzed)
1173 error ("Chained transparent aliases");
1174 get_alias_target ()->dump (stderr);
1175 error_found = true;
1178 return error_found;
1181 /* Verify consistency of NODE. */
1183 DEBUG_FUNCTION void
1184 symtab_node::verify (void)
1186 if (seen_error ())
1187 return;
1189 timevar_push (TV_CGRAPH_VERIFY);
1190 if (cgraph_node *node = dyn_cast <cgraph_node *> (this))
1191 node->verify_node ();
1192 else
1193 if (verify_base ())
1195 debug ();
1196 internal_error ("symtab_node::verify failed");
1198 timevar_pop (TV_CGRAPH_VERIFY);
1201 /* Verify symbol table for internal consistency. */
1203 DEBUG_FUNCTION void
1204 symtab_node::verify_symtab_nodes (void)
1206 symtab_node *node;
1207 hash_map<tree, symtab_node *> comdat_head_map (251);
1209 FOR_EACH_SYMBOL (node)
1211 node->verify ();
1212 if (node->get_comdat_group ())
1214 symtab_node **entry, *s;
1215 bool existed;
1217 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1218 &existed);
1219 if (!existed)
1220 *entry = node;
1221 else if (!DECL_EXTERNAL (node->decl))
1223 for (s = (*entry)->same_comdat_group;
1224 s != NULL && s != node && s != *entry;
1225 s = s->same_comdat_group)
1227 if (!s || s == *entry)
1229 error ("Two symbols with same comdat_group are not linked by "
1230 "the same_comdat_group list.");
1231 (*entry)->debug ();
1232 node->debug ();
1233 internal_error ("symtab_node::verify failed");
1240 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1241 but other code such as notice_global_symbol generates rtl. */
1243 void
1244 symtab_node::make_decl_local (void)
1246 rtx rtl, symbol;
1248 if (weakref)
1250 weakref = false;
1251 IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl)) = 0;
1252 TREE_CHAIN (DECL_ASSEMBLER_NAME (decl)) = NULL_TREE;
1253 symtab->change_decl_assembler_name
1254 (decl, DECL_ASSEMBLER_NAME (get_alias_target ()->decl));
1255 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
1256 DECL_ATTRIBUTES (decl));
1258 /* Avoid clearing comdat_groups on comdat-local decls. */
1259 else if (TREE_PUBLIC (decl) == 0)
1260 return;
1262 /* Localizing a symbol also make all its transparent aliases local. */
1263 ipa_ref *ref;
1264 for (unsigned i = 0; iterate_direct_aliases (i, ref); i++)
1266 struct symtab_node *alias = ref->referring;
1267 if (alias->transparent_alias)
1268 alias->make_decl_local ();
1271 if (VAR_P (decl))
1273 DECL_COMMON (decl) = 0;
1274 /* ADDRESSABLE flag is not defined for public symbols. */
1275 TREE_ADDRESSABLE (decl) = 1;
1276 TREE_STATIC (decl) = 1;
1278 else
1279 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1281 DECL_COMDAT (decl) = 0;
1282 DECL_WEAK (decl) = 0;
1283 DECL_EXTERNAL (decl) = 0;
1284 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1285 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1286 TREE_PUBLIC (decl) = 0;
1287 DECL_DLLIMPORT_P (decl) = 0;
1288 if (!DECL_RTL_SET_P (decl))
1289 return;
1291 /* Update rtl flags. */
1292 make_decl_rtl (decl);
1294 rtl = DECL_RTL (decl);
1295 if (!MEM_P (rtl))
1296 return;
1298 symbol = XEXP (rtl, 0);
1299 if (GET_CODE (symbol) != SYMBOL_REF)
1300 return;
1302 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1305 /* Copy visibility from N.
1306 This is useful when THIS becomes a transparent alias of N. */
1308 void
1309 symtab_node::copy_visibility_from (symtab_node *n)
1311 gcc_checking_assert (n->weakref == weakref);
1313 ipa_ref *ref;
1314 for (unsigned i = 0; iterate_direct_aliases (i, ref); i++)
1316 struct symtab_node *alias = ref->referring;
1317 if (alias->transparent_alias)
1318 alias->copy_visibility_from (n);
1321 if (VAR_P (decl))
1323 DECL_COMMON (decl) = DECL_COMMON (n->decl);
1324 /* ADDRESSABLE flag is not defined for public symbols. */
1325 if (TREE_PUBLIC (decl) && !TREE_PUBLIC (n->decl))
1326 TREE_ADDRESSABLE (decl) = 1;
1327 TREE_STATIC (decl) = TREE_STATIC (n->decl);
1329 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1331 DECL_COMDAT (decl) = DECL_COMDAT (n->decl);
1332 DECL_WEAK (decl) = DECL_WEAK (n->decl);
1333 DECL_EXTERNAL (decl) = DECL_EXTERNAL (n->decl);
1334 DECL_VISIBILITY_SPECIFIED (decl) = DECL_VISIBILITY_SPECIFIED (n->decl);
1335 DECL_VISIBILITY (decl) = DECL_VISIBILITY (n->decl);
1336 TREE_PUBLIC (decl) = TREE_PUBLIC (n->decl);
1337 DECL_DLLIMPORT_P (decl) = DECL_DLLIMPORT_P (n->decl);
1338 resolution = n->resolution;
1339 set_comdat_group (n->get_comdat_group ());
1340 call_for_symbol_and_aliases (symtab_node::set_section,
1341 const_cast<char *>(n->get_section ()), true);
1342 externally_visible = n->externally_visible;
1343 if (!DECL_RTL_SET_P (decl))
1344 return;
1346 /* Update rtl flags. */
1347 make_decl_rtl (decl);
1349 rtx rtl = DECL_RTL (decl);
1350 if (!MEM_P (rtl))
1351 return;
1353 rtx symbol = XEXP (rtl, 0);
1354 if (GET_CODE (symbol) != SYMBOL_REF)
1355 return;
1357 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1360 /* Walk the alias chain to return the symbol NODE is alias of.
1361 If NODE is not an alias, return NODE.
1362 Assumes NODE is known to be alias. */
1364 symtab_node *
1365 symtab_node::ultimate_alias_target_1 (enum availability *availability,
1366 symtab_node *ref)
1368 bool transparent_p = false;
1370 /* To determine visibility of the target, we follow ELF semantic of aliases.
1371 Here alias is an alternative assembler name of a given definition. Its
1372 availability prevails the availability of its target (i.e. static alias of
1373 weak definition is available.
1375 Transaparent alias is just alternative anme of a given symbol used within
1376 one compilation unit and is translated prior hitting the object file. It
1377 inherits the visibility of its target.
1378 Weakref is a different animal (and noweak definition is weak).
1380 If we ever get into supporting targets with different semantics, a target
1381 hook will be needed here. */
1383 if (availability)
1385 transparent_p = transparent_alias;
1386 if (!transparent_p)
1387 *availability = get_availability (ref);
1388 else
1389 *availability = AVAIL_NOT_AVAILABLE;
1392 symtab_node *node = this;
1393 while (node)
1395 if (node->alias && node->analyzed)
1396 node = node->get_alias_target ();
1397 else
1399 if (!availability || (!transparent_p && node->analyzed))
1401 else if (node->analyzed && !node->transparent_alias)
1402 *availability = node->get_availability (ref);
1403 else
1404 *availability = AVAIL_NOT_AVAILABLE;
1405 return node;
1407 if (node && availability && transparent_p
1408 && node->transparent_alias)
1410 *availability = node->get_availability (ref);
1411 transparent_p = false;
1414 if (availability)
1415 *availability = AVAIL_NOT_AVAILABLE;
1416 return NULL;
1419 /* C++ FE sometimes change linkage flags after producing same body aliases.
1421 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1422 are obviously equivalent. The way it is doing so is however somewhat
1423 kludgy and interferes with the visibility code. As a result we need to
1424 copy the visibility from the target to get things right. */
1426 void
1427 symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target)
1429 if (is_a <cgraph_node *> (this))
1431 DECL_DECLARED_INLINE_P (decl)
1432 = DECL_DECLARED_INLINE_P (target->decl);
1433 DECL_DISREGARD_INLINE_LIMITS (decl)
1434 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1436 /* FIXME: It is not really clear why those flags should not be copied for
1437 functions, too. */
1438 else
1440 DECL_WEAK (decl) = DECL_WEAK (target->decl);
1441 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1442 DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl);
1444 if (TREE_PUBLIC (decl))
1446 tree group;
1448 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1449 DECL_COMDAT (decl) = DECL_COMDAT (target->decl);
1450 group = target->get_comdat_group ();
1451 set_comdat_group (group);
1452 if (group && !same_comdat_group)
1453 add_to_same_comdat_group (target);
1455 externally_visible = target->externally_visible;
1458 /* Set section, do not recurse into aliases.
1459 When one wants to change section of a symbol and its aliases,
1460 use set_section. */
1462 void
1463 symtab_node::set_section_for_node (const char *section)
1465 const char *current = get_section ();
1466 section_hash_entry **slot;
1468 if (current == section
1469 || (current && section
1470 && !strcmp (current, section)))
1471 return;
1473 if (current)
1475 x_section->ref_count--;
1476 if (!x_section->ref_count)
1478 hashval_t hash = htab_hash_string (x_section->name);
1479 slot = symtab->section_hash->find_slot_with_hash (x_section->name,
1480 hash, INSERT);
1481 ggc_free (x_section);
1482 symtab->section_hash->clear_slot (slot);
1484 x_section = NULL;
1486 if (!section)
1488 implicit_section = false;
1489 return;
1491 if (!symtab->section_hash)
1492 symtab->section_hash = hash_table<section_name_hasher>::create_ggc (10);
1493 slot = symtab->section_hash->find_slot_with_hash (section,
1494 htab_hash_string (section),
1495 INSERT);
1496 if (*slot)
1497 x_section = (section_hash_entry *)*slot;
1498 else
1500 int len = strlen (section);
1501 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1502 x_section->name = ggc_vec_alloc<char> (len + 1);
1503 memcpy (x_section->name, section, len + 1);
1505 x_section->ref_count++;
1508 /* Worker for set_section. */
1510 bool
1511 symtab_node::set_section (symtab_node *n, void *s)
1513 n->set_section_for_node ((char *)s);
1514 return false;
1517 /* Set section of symbol and its aliases. */
1519 void
1520 symtab_node::set_section (const char *section)
1522 gcc_assert (!this->alias);
1523 call_for_symbol_and_aliases
1524 (symtab_node::set_section, const_cast<char *>(section), true);
1527 /* Return the initialization priority. */
1529 priority_type
1530 symtab_node::get_init_priority ()
1532 if (!this->in_init_priority_hash)
1533 return DEFAULT_INIT_PRIORITY;
1535 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1536 return h ? h->init : DEFAULT_INIT_PRIORITY;
1539 /* Return the finalization priority. */
1541 priority_type
1542 cgraph_node::get_fini_priority ()
1544 if (!this->in_init_priority_hash)
1545 return DEFAULT_INIT_PRIORITY;
1546 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1547 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1550 /* Return the initialization and finalization priority information for
1551 DECL. If there is no previous priority information, a freshly
1552 allocated structure is returned. */
1554 symbol_priority_map *
1555 symtab_node::priority_info (void)
1557 if (!symtab->init_priority_hash)
1558 symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13);
1560 bool existed;
1561 symbol_priority_map *h
1562 = &symtab->init_priority_hash->get_or_insert (this, &existed);
1563 if (!existed)
1565 h->init = DEFAULT_INIT_PRIORITY;
1566 h->fini = DEFAULT_INIT_PRIORITY;
1567 in_init_priority_hash = true;
1570 return h;
1573 /* Set initialization priority to PRIORITY. */
1575 void
1576 symtab_node::set_init_priority (priority_type priority)
1578 symbol_priority_map *h;
1580 if (is_a <cgraph_node *> (this))
1581 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1583 if (priority == DEFAULT_INIT_PRIORITY)
1585 gcc_assert (get_init_priority() == priority);
1586 return;
1588 h = priority_info ();
1589 h->init = priority;
1592 /* Set fialization priority to PRIORITY. */
1594 void
1595 cgraph_node::set_fini_priority (priority_type priority)
1597 symbol_priority_map *h;
1599 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1601 if (priority == DEFAULT_INIT_PRIORITY)
1603 gcc_assert (get_fini_priority() == priority);
1604 return;
1606 h = priority_info ();
1607 h->fini = priority;
1610 /* Worker for symtab_resolve_alias. */
1612 bool
1613 symtab_node::set_implicit_section (symtab_node *n,
1614 void *data ATTRIBUTE_UNUSED)
1616 n->implicit_section = true;
1617 return false;
1620 /* Add reference recording that symtab node is alias of TARGET.
1621 The function can fail in the case of aliasing cycles; in this case
1622 it returns false. */
1624 bool
1625 symtab_node::resolve_alias (symtab_node *target, bool transparent)
1627 symtab_node *n;
1629 gcc_assert (!analyzed && !vec_safe_length (ref_list.references));
1631 /* Never let cycles to creep into the symbol table alias references;
1632 those will make alias walkers to be infinite. */
1633 for (n = target; n && n->alias;
1634 n = n->analyzed ? n->get_alias_target () : NULL)
1635 if (n == this)
1637 if (is_a <cgraph_node *> (this))
1638 error ("function %q+D part of alias cycle", decl);
1639 else if (is_a <varpool_node *> (this))
1640 error ("variable %q+D part of alias cycle", decl);
1641 else
1642 gcc_unreachable ();
1643 alias = false;
1644 return false;
1647 /* "analyze" the node - i.e. mark the reference. */
1648 definition = true;
1649 alias = true;
1650 analyzed = true;
1651 transparent |= transparent_alias;
1652 transparent_alias = transparent;
1653 if (transparent)
1654 while (target->transparent_alias && target->analyzed)
1655 target = target->get_alias_target ();
1656 create_reference (target, IPA_REF_ALIAS, NULL);
1658 /* Add alias into the comdat group of its target unless it is already there. */
1659 if (same_comdat_group)
1660 remove_from_same_comdat_group ();
1661 set_comdat_group (NULL);
1662 if (target->get_comdat_group ())
1663 add_to_same_comdat_group (target);
1665 if ((get_section () != target->get_section ()
1666 || target->get_comdat_group ()) && get_section () && !implicit_section)
1668 error ("section of alias %q+D must match section of its target", decl);
1670 call_for_symbol_and_aliases (symtab_node::set_section,
1671 const_cast<char *>(target->get_section ()), true);
1672 if (target->implicit_section)
1673 call_for_symbol_and_aliases (set_implicit_section, NULL, true);
1675 /* Alias targets become redundant after alias is resolved into an reference.
1676 We do not want to keep it around or we would have to mind updating them
1677 when renaming symbols. */
1678 alias_target = NULL;
1680 if (!transparent && cpp_implicit_alias && symtab->state >= CONSTRUCTION)
1681 fixup_same_cpp_alias_visibility (target);
1683 /* If alias has address taken, so does the target. */
1684 if (address_taken)
1685 target->ultimate_alias_target ()->address_taken = true;
1687 /* All non-transparent aliases of THIS are now in fact aliases of TARGET.
1688 If alias is transparent, also all transparent aliases of THIS are now
1689 aliases of TARGET.
1690 Also merge same comdat group lists. */
1691 ipa_ref *ref;
1692 for (unsigned i = 0; iterate_direct_aliases (i, ref);)
1694 struct symtab_node *alias_alias = ref->referring;
1695 if (alias_alias->get_comdat_group ())
1697 alias_alias->remove_from_same_comdat_group ();
1698 alias_alias->set_comdat_group (NULL);
1699 if (target->get_comdat_group ())
1700 alias_alias->add_to_same_comdat_group (target);
1702 if (!alias_alias->transparent_alias || transparent)
1704 alias_alias->remove_all_references ();
1705 alias_alias->create_reference (target, IPA_REF_ALIAS, NULL);
1707 else i++;
1709 return true;
1712 /* Worker searching noninterposable alias. */
1714 bool
1715 symtab_node::noninterposable_alias (symtab_node *node, void *data)
1717 if (!node->transparent_alias && decl_binds_to_current_def_p (node->decl))
1719 symtab_node *fn = node->ultimate_alias_target ();
1721 /* Ensure that the alias is well formed this may not be the case
1722 of user defined aliases and currently it is not always the case
1723 of C++ same body aliases (that is a bug). */
1724 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
1725 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
1726 || (TREE_CODE (node->decl) == FUNCTION_DECL
1727 && flags_from_decl_or_type (node->decl)
1728 != flags_from_decl_or_type (fn->decl))
1729 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
1730 return false;
1731 *(symtab_node **)data = node;
1732 return true;
1734 return false;
1737 /* If node can not be overwriten by static or dynamic linker to point to
1738 different definition, return NODE. Otherwise look for alias with such
1739 property and if none exists, introduce new one. */
1741 symtab_node *
1742 symtab_node::noninterposable_alias (void)
1744 tree new_decl;
1745 symtab_node *new_node = NULL;
1747 /* First try to look up existing alias or base object
1748 (if that is already non-overwritable). */
1749 symtab_node *node = ultimate_alias_target ();
1750 gcc_assert (!node->alias && !node->weakref);
1751 node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias,
1752 (void *)&new_node, true);
1753 if (new_node)
1754 return new_node;
1755 #ifndef ASM_OUTPUT_DEF
1756 /* If aliases aren't supported by the assembler, fail. */
1757 return NULL;
1758 #endif
1760 /* Otherwise create a new one. */
1761 new_decl = copy_node (node->decl);
1762 DECL_DLLIMPORT_P (new_decl) = 0;
1763 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1764 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1765 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1766 DECL_INITIAL (new_decl) = NULL;
1767 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1768 SET_DECL_RTL (new_decl, NULL);
1770 /* Update the properties. */
1771 DECL_EXTERNAL (new_decl) = 0;
1772 TREE_PUBLIC (new_decl) = 0;
1773 DECL_COMDAT (new_decl) = 0;
1774 DECL_WEAK (new_decl) = 0;
1776 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1777 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1778 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1780 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1781 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1782 new_node = cgraph_node::create_alias (new_decl, node->decl);
1784 else
1786 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1787 DECL_INITIAL (new_decl) = error_mark_node;
1788 new_node = varpool_node::create_alias (new_decl, node->decl);
1790 new_node->resolve_alias (node);
1791 gcc_assert (decl_binds_to_current_def_p (new_decl)
1792 && targetm.binds_local_p (new_decl));
1793 return new_node;
1796 /* Return true if symtab node and TARGET represents
1797 semantically equivalent symbols. */
1799 bool
1800 symtab_node::semantically_equivalent_p (symtab_node *target)
1802 enum availability avail;
1803 symtab_node *ba;
1804 symtab_node *bb;
1806 /* Equivalent functions are equivalent. */
1807 if (decl == target->decl)
1808 return true;
1810 /* If symbol is not overwritable by different implementation,
1811 walk to the base object it defines. */
1812 ba = ultimate_alias_target (&avail);
1813 if (avail >= AVAIL_AVAILABLE)
1815 if (target == ba)
1816 return true;
1818 else
1819 ba = this;
1820 bb = target->ultimate_alias_target (&avail);
1821 if (avail >= AVAIL_AVAILABLE)
1823 if (this == bb)
1824 return true;
1826 else
1827 bb = target;
1828 return bb == ba;
1831 /* Classify symbol symtab node for partitioning. */
1833 enum symbol_partitioning_class
1834 symtab_node::get_partitioning_class (void)
1836 /* Inline clones are always duplicated.
1837 This include external delcarations. */
1838 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
1840 if (DECL_ABSTRACT_P (decl))
1841 return SYMBOL_EXTERNAL;
1843 if (cnode && cnode->global.inlined_to)
1844 return SYMBOL_DUPLICATE;
1846 /* Transparent aliases are always duplicated. */
1847 if (transparent_alias)
1848 return definition ? SYMBOL_DUPLICATE : SYMBOL_EXTERNAL;
1850 /* External declarations are external. */
1851 if (DECL_EXTERNAL (decl))
1852 return SYMBOL_EXTERNAL;
1854 if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
1856 if (alias && definition && !ultimate_alias_target ()->definition)
1857 return SYMBOL_EXTERNAL;
1858 /* Constant pool references use local symbol names that can not
1859 be promoted global. We should never put into a constant pool
1860 objects that can not be duplicated across partitions. */
1861 if (DECL_IN_CONSTANT_POOL (decl))
1862 return SYMBOL_DUPLICATE;
1863 if (DECL_HARD_REGISTER (decl))
1864 return SYMBOL_DUPLICATE;
1865 gcc_checking_assert (vnode->definition);
1867 /* Functions that are cloned may stay in callgraph even if they are unused.
1868 Handle them as external; compute_ltrans_boundary take care to make
1869 proper things to happen (i.e. to make them appear in the boundary but
1870 with body streamed, so clone can me materialized). */
1871 else if (!dyn_cast <cgraph_node *> (this)->function_symbol ()->definition)
1872 return SYMBOL_EXTERNAL;
1874 /* Linker discardable symbols are duplicated to every use unless they are
1875 keyed. */
1876 if (DECL_ONE_ONLY (decl)
1877 && !force_output
1878 && !forced_by_abi
1879 && !used_from_object_file_p ())
1880 return SYMBOL_DUPLICATE;
1882 return SYMBOL_PARTITION;
1885 /* Return true when symbol is known to be non-zero. */
1887 bool
1888 symtab_node::nonzero_address ()
1890 /* Weakrefs may be NULL when their target is not defined. */
1891 if (alias && weakref)
1893 if (analyzed)
1895 symtab_node *target = ultimate_alias_target ();
1897 if (target->alias && target->weakref)
1898 return false;
1899 /* We can not recurse to target::nonzero. It is possible that the
1900 target is used only via the alias.
1901 We may walk references and look for strong use, but we do not know
1902 if this strong use will survive to final binary, so be
1903 conservative here.
1904 ??? Maybe we could do the lookup during late optimization that
1905 could be useful to eliminate the NULL pointer checks in LTO
1906 programs. */
1907 if (target->definition && !DECL_EXTERNAL (target->decl))
1908 return true;
1909 if (target->resolution != LDPR_UNKNOWN
1910 && target->resolution != LDPR_UNDEF
1911 && !target->can_be_discarded_p ()
1912 && flag_delete_null_pointer_checks)
1913 return true;
1914 return false;
1916 else
1917 return false;
1920 /* With !flag_delete_null_pointer_checks we assume that symbols may
1921 bind to NULL. This is on by default on embedded targets only.
1923 Otherwise all non-WEAK symbols must be defined and thus non-NULL or
1924 linking fails. Important case of WEAK we want to do well are comdats.
1925 Those are handled by later check for definition.
1927 When parsing, beware the cases when WEAK attribute is added later. */
1928 if (!DECL_WEAK (decl)
1929 && flag_delete_null_pointer_checks)
1931 refuse_visibility_changes = true;
1932 return true;
1935 /* If target is defined and not extern, we know it will be output and thus
1936 it will bind to non-NULL.
1937 Play safe for flag_delete_null_pointer_checks where weak definition maye
1938 be re-defined by NULL. */
1939 if (definition && !DECL_EXTERNAL (decl)
1940 && (flag_delete_null_pointer_checks || !DECL_WEAK (decl)))
1942 if (!DECL_WEAK (decl))
1943 refuse_visibility_changes = true;
1944 return true;
1947 /* As the last resort, check the resolution info. */
1948 if (resolution != LDPR_UNKNOWN
1949 && resolution != LDPR_UNDEF
1950 && !can_be_discarded_p ()
1951 && flag_delete_null_pointer_checks)
1952 return true;
1953 return false;
1956 /* Return 0 if symbol is known to have different address than S2,
1957 Return 1 if symbol is known to have same address as S2,
1958 return -1 otherwise.
1960 If MEMORY_ACCESSED is true, assume that both memory pointer to THIS
1961 and S2 is going to be accessed. This eliminates the situations when
1962 either THIS or S2 is NULL and is seful for comparing bases when deciding
1963 about memory aliasing. */
1965 symtab_node::equal_address_to (symtab_node *s2, bool memory_accessed)
1967 enum availability avail1, avail2;
1969 /* A Shortcut: equivalent symbols are always equivalent. */
1970 if (this == s2)
1971 return 1;
1973 /* Unwind transparent aliases first; those are always equal to their
1974 target. */
1975 if (this->transparent_alias && this->analyzed)
1976 return this->get_alias_target ()->equal_address_to (s2);
1977 while (s2->transparent_alias && s2->analyzed)
1978 s2 = s2->get_alias_target();
1980 if (this == s2)
1981 return 1;
1983 /* For non-interposable aliases, lookup and compare their actual definitions.
1984 Also check if the symbol needs to bind to given definition. */
1985 symtab_node *rs1 = ultimate_alias_target (&avail1);
1986 symtab_node *rs2 = s2->ultimate_alias_target (&avail2);
1987 bool binds_local1 = rs1->analyzed && decl_binds_to_current_def_p (this->decl);
1988 bool binds_local2 = rs2->analyzed && decl_binds_to_current_def_p (s2->decl);
1989 bool really_binds_local1 = binds_local1;
1990 bool really_binds_local2 = binds_local2;
1992 /* Addresses of vtables and virtual functions can not be used by user
1993 code and are used only within speculation. In this case we may make
1994 symbol equivalent to its alias even if interposition may break this
1995 rule. Doing so will allow us to turn speculative inlining into
1996 non-speculative more agressively. */
1997 if (DECL_VIRTUAL_P (this->decl) && avail1 >= AVAIL_AVAILABLE)
1998 binds_local1 = true;
1999 if (DECL_VIRTUAL_P (s2->decl) && avail2 >= AVAIL_AVAILABLE)
2000 binds_local2 = true;
2002 /* If both definitions are available we know that even if they are bound
2003 to other unit they must be defined same way and therefore we can use
2004 equivalence test. */
2005 if (rs1 != rs2 && avail1 >= AVAIL_AVAILABLE && avail2 >= AVAIL_AVAILABLE)
2006 binds_local1 = binds_local2 = true;
2008 if (binds_local1 && binds_local2 && rs1 == rs2)
2010 /* We made use of the fact that alias is not weak. */
2011 if (rs1 != this)
2012 refuse_visibility_changes = true;
2013 if (rs2 != s2)
2014 s2->refuse_visibility_changes = true;
2015 return 1;
2018 /* If both symbols may resolve to NULL, we can not really prove them
2019 different. */
2020 if (!memory_accessed && !nonzero_address () && !s2->nonzero_address ())
2021 return -1;
2023 /* Except for NULL, functions and variables never overlap. */
2024 if (TREE_CODE (decl) != TREE_CODE (s2->decl))
2025 return 0;
2027 /* If one of the symbols is unresolved alias, punt. */
2028 if (rs1->alias || rs2->alias)
2029 return -1;
2031 /* If we have a non-interposale definition of at least one of the symbols
2032 and the other symbol is different, we know other unit can not interpose
2033 it to the first symbol; all aliases of the definition needs to be
2034 present in the current unit. */
2035 if (((really_binds_local1 || really_binds_local2)
2036 /* If we have both definitions and they are different, we know they
2037 will be different even in units they binds to. */
2038 || (binds_local1 && binds_local2))
2039 && rs1 != rs2)
2041 /* We make use of the fact that one symbol is not alias of the other
2042 and that the definition is non-interposable. */
2043 refuse_visibility_changes = true;
2044 s2->refuse_visibility_changes = true;
2045 rs1->refuse_visibility_changes = true;
2046 rs2->refuse_visibility_changes = true;
2047 return 0;
2050 /* TODO: Alias oracle basically assume that addresses of global variables
2051 are different unless they are declared as alias of one to another while
2052 the code folding comparsions doesn't.
2053 We probably should be consistent and use this fact here, too, but for
2054 the moment return false only when we are called from the alias oracle. */
2056 return memory_accessed && rs1 != rs2 ? 0 : -1;
2059 /* Worker for call_for_symbol_and_aliases. */
2061 bool
2062 symtab_node::call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *,
2063 void *),
2064 void *data,
2065 bool include_overwritable)
2067 ipa_ref *ref;
2068 FOR_EACH_ALIAS (this, ref)
2070 symtab_node *alias = ref->referring;
2071 if (include_overwritable
2072 || alias->get_availability () > AVAIL_INTERPOSABLE)
2073 if (alias->call_for_symbol_and_aliases (callback, data,
2074 include_overwritable))
2075 return true;
2077 return false;
2080 /* Return true if address of N is possibly compared. */
2082 static bool
2083 address_matters_1 (symtab_node *n, void *)
2085 struct ipa_ref *ref;
2087 if (!n->address_can_be_compared_p ())
2088 return false;
2089 if (n->externally_visible || n->force_output)
2090 return true;
2092 for (unsigned int i = 0; n->iterate_referring (i, ref); i++)
2093 if (ref->address_matters_p ())
2094 return true;
2095 return false;
2098 /* Return true if symbol's address may possibly be compared to other
2099 symbol's address. */
2101 bool
2102 symtab_node::address_matters_p ()
2104 gcc_assert (!alias);
2105 return call_for_symbol_and_aliases (address_matters_1, NULL, true);
2108 /* Return true if symbol's alignment may be increased. */
2110 bool
2111 symtab_node::can_increase_alignment_p (void)
2113 symtab_node *target = ultimate_alias_target ();
2115 /* For now support only variables. */
2116 if (!VAR_P (decl))
2117 return false;
2119 /* With -fno-toplevel-reorder we may have already output the constant. */
2120 if (TREE_ASM_WRITTEN (target->decl))
2121 return false;
2123 /* If target is already placed in an anchor, we can not touch its
2124 alignment. */
2125 if (DECL_RTL_SET_P (target->decl)
2126 && MEM_P (DECL_RTL (target->decl))
2127 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (DECL_RTL (target->decl), 0)))
2128 return false;
2130 /* Constant pool entries may be shared. */
2131 if (DECL_IN_CONSTANT_POOL (target->decl))
2132 return false;
2134 /* We cannot change alignment of symbols that may bind to symbols
2135 in other translation unit that may contain a definition with lower
2136 alignment. */
2137 if (!decl_binds_to_current_def_p (decl))
2138 return false;
2140 /* When compiling partition, be sure the symbol is not output by other
2141 partition. */
2142 if (flag_ltrans
2143 && (target->in_other_partition
2144 || target->get_partitioning_class () == SYMBOL_DUPLICATE))
2145 return false;
2147 /* Do not override the alignment as specified by the ABI when the used
2148 attribute is set. */
2149 if (DECL_PRESERVE_P (decl) || DECL_PRESERVE_P (target->decl))
2150 return false;
2152 /* Do not override explicit alignment set by the user when an explicit
2153 section name is also used. This is a common idiom used by many
2154 software projects. */
2155 if (DECL_SECTION_NAME (target->decl) != NULL && !target->implicit_section)
2156 return false;
2158 return true;
2161 /* Worker for symtab_node::increase_alignment. */
2163 static bool
2164 increase_alignment_1 (symtab_node *n, void *v)
2166 unsigned int align = (size_t)v;
2167 if (DECL_ALIGN (n->decl) < align
2168 && n->can_increase_alignment_p ())
2170 SET_DECL_ALIGN (n->decl, align);
2171 DECL_USER_ALIGN (n->decl) = 1;
2173 return false;
2176 /* Increase alignment of THIS to ALIGN. */
2178 void
2179 symtab_node::increase_alignment (unsigned int align)
2181 gcc_assert (can_increase_alignment_p () && align < MAX_OFILE_ALIGNMENT);
2182 ultimate_alias_target()->call_for_symbol_and_aliases (increase_alignment_1,
2183 (void *)(size_t) align,
2184 true);
2185 gcc_assert (DECL_ALIGN (decl) >= align);
2188 /* Helper for symtab_node::definition_alignment. */
2190 static bool
2191 get_alignment_1 (symtab_node *n, void *v)
2193 *((unsigned int *)v) = MAX (*((unsigned int *)v), DECL_ALIGN (n->decl));
2194 return false;
2197 /* Return desired alignment of the definition. This is NOT alignment useful
2198 to access THIS, because THIS may be interposable and DECL_ALIGN should
2199 be used instead. It however must be guaranteed when output definition
2200 of THIS. */
2202 unsigned int
2203 symtab_node::definition_alignment ()
2205 unsigned int align = 0;
2206 gcc_assert (!alias);
2207 call_for_symbol_and_aliases (get_alignment_1, &align, true);
2208 return align;
2211 /* Return symbol used to separate symbol name from suffix. */
2213 char
2214 symbol_table::symbol_suffix_separator ()
2216 #ifndef NO_DOT_IN_LABEL
2217 return '.';
2218 #elif !defined NO_DOLLAR_IN_LABEL
2219 return '$';
2220 #else
2221 return '_';
2222 #endif
2225 /* Return true when references to this symbol from REF must bind to current
2226 definition in final executable. */
2228 bool
2229 symtab_node::binds_to_current_def_p (symtab_node *ref)
2231 if (!definition)
2232 return false;
2233 if (transparent_alias)
2234 return definition
2235 && get_alias_target()->binds_to_current_def_p (ref);
2236 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
2237 return false;
2238 if (decl_binds_to_current_def_p (decl))
2239 return true;
2241 /* Inline clones always binds locally. */
2242 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
2243 if (cnode && cnode->global.inlined_to)
2244 return true;
2246 if (DECL_EXTERNAL (decl))
2247 return false;
2249 gcc_assert (externally_visible);
2251 if (ref)
2253 cgraph_node *cref = dyn_cast <cgraph_node *> (ref);
2254 if (cref)
2255 ref = cref->global.inlined_to;
2258 /* If this is a reference from symbol itself and there are no aliases, we
2259 may be sure that the symbol was not interposed by something else because
2260 the symbol itself would be unreachable otherwise. This is important
2261 to optimize recursive functions well.
2263 This assumption may be broken by inlining: if symbol is interposable
2264 but the body is available (i.e. declared inline), inliner may make
2265 the body reachable even with interposition. */
2266 if (this == ref && !has_aliases_p ()
2267 && (!cnode
2268 || symtab->state >= IPA_SSA_AFTER_INLINING
2269 || get_availability () >= AVAIL_INTERPOSABLE))
2270 return true;
2273 /* References within one comdat group are always bound in a group. */
2274 if (ref
2275 && symtab->state >= IPA_SSA_AFTER_INLINING
2276 && get_comdat_group ()
2277 && get_comdat_group () == ref->get_comdat_group ())
2278 return true;
2280 return false;