Update ChangeLog and version files for release
[official-gcc.git] / gcc / symtab.c
blob3d3cc4f738c3676e752780dae8325899986ce946
1 /* Symbol table.
2 Copyright (C) 2012-2016 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 ((TREE_CODE (decl) == VAR_DECL
277 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
278 || TREE_CODE (decl) == FUNCTION_DECL)
279 node = symtab_node::get (decl);
280 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
282 SET_DECL_ASSEMBLER_NAME (decl, name);
283 if (node)
284 insert_to_assembler_name_hash (node, true);
286 else
288 if (name == DECL_ASSEMBLER_NAME (decl))
289 return;
291 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
292 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
293 : NULL);
294 if (node)
295 unlink_from_assembler_name_hash (node, true);
297 const char *old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
298 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
299 && DECL_RTL_SET_P (decl))
300 warning (0, "%D renamed after being referenced in assembly", decl);
302 SET_DECL_ASSEMBLER_NAME (decl, name);
303 if (alias)
305 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
306 TREE_CHAIN (name) = alias;
308 /* If we change assembler name, also all transparent aliases must
309 be updated. There are three kinds - those having same assembler name,
310 those being renamed in varasm.c and weakref being renamed by the
311 assembler. */
312 if (node)
314 insert_to_assembler_name_hash (node, true);
315 ipa_ref *ref;
316 for (unsigned i = 0; node->iterate_direct_aliases (i, ref); i++)
318 struct symtab_node *alias = ref->referring;
319 if (alias->transparent_alias && !alias->weakref
320 && symbol_table::assembler_names_equal_p
321 (old_name, IDENTIFIER_POINTER (
322 DECL_ASSEMBLER_NAME (alias->decl))))
323 change_decl_assembler_name (alias->decl, name);
324 else if (alias->transparent_alias
325 && IDENTIFIER_TRANSPARENT_ALIAS (alias->decl))
327 gcc_assert (TREE_CHAIN (DECL_ASSEMBLER_NAME (alias->decl))
328 && IDENTIFIER_TRANSPARENT_ALIAS
329 (DECL_ASSEMBLER_NAME (alias->decl)));
331 TREE_CHAIN (DECL_ASSEMBLER_NAME (alias->decl)) =
332 ultimate_transparent_alias_target
333 (DECL_ASSEMBLER_NAME (node->decl));
335 #ifdef ASM_OUTPUT_WEAKREF
336 else gcc_assert (!alias->transparent_alias || alias->weakref);
337 #else
338 else gcc_assert (!alias->transparent_alias);
339 #endif
341 gcc_assert (!node->transparent_alias || !node->definition
342 || node->weakref
343 || TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
344 || symbol_table::assembler_names_equal_p
345 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
346 IDENTIFIER_POINTER
347 (DECL_ASSEMBLER_NAME
348 (node->get_alias_target ()->decl))));
353 /* Hash sections by their names. */
355 hashval_t
356 section_name_hasher::hash (section_hash_entry *n)
358 return htab_hash_string (n->name);
361 /* Return true if section P1 name equals to P2. */
363 bool
364 section_name_hasher::equal (section_hash_entry *n1, const char *name)
366 return n1->name == name || !strcmp (n1->name, name);
369 /* Add node into symbol table. This function is not used directly, but via
370 cgraph/varpool node creation routines. */
372 void
373 symtab_node::register_symbol (void)
375 symtab->register_symbol (this);
377 if (!decl->decl_with_vis.symtab_node)
378 decl->decl_with_vis.symtab_node = this;
380 ref_list.clear ();
382 /* Be sure to do this last; C++ FE might create new nodes via
383 DECL_ASSEMBLER_NAME langhook! */
384 symtab->insert_to_assembler_name_hash (this, false);
387 /* Remove NODE from same comdat group. */
389 void
390 symtab_node::remove_from_same_comdat_group (void)
392 if (same_comdat_group)
394 symtab_node *prev;
395 for (prev = same_comdat_group;
396 prev->same_comdat_group != this;
397 prev = prev->same_comdat_group)
399 if (same_comdat_group == prev)
400 prev->same_comdat_group = NULL;
401 else
402 prev->same_comdat_group = same_comdat_group;
403 same_comdat_group = NULL;
404 set_comdat_group (NULL);
408 /* Remove node from symbol table. This function is not used directly, but via
409 cgraph/varpool node removal routines. */
411 void
412 symtab_node::unregister (void)
414 remove_all_references ();
415 remove_all_referring ();
417 /* Remove reference to section. */
418 set_section_for_node (NULL);
420 remove_from_same_comdat_group ();
422 symtab->unregister (this);
424 /* During LTO symtab merging we temporarily corrupt decl to symtab node
425 hash. */
426 gcc_assert (decl->decl_with_vis.symtab_node || in_lto_p);
427 if (decl->decl_with_vis.symtab_node == this)
429 symtab_node *replacement_node = NULL;
430 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
431 replacement_node = cnode->find_replacement ();
432 decl->decl_with_vis.symtab_node = replacement_node;
434 if (!is_a <varpool_node *> (this) || !DECL_HARD_REGISTER (decl))
435 symtab->unlink_from_assembler_name_hash (this, false);
436 if (in_init_priority_hash)
437 symtab->init_priority_hash->remove (this);
441 /* Remove symbol from symbol table. */
443 void
444 symtab_node::remove (void)
446 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
447 cnode->remove ();
448 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
449 vnode->remove ();
452 /* Add NEW_ to the same comdat group that OLD is in. */
454 void
455 symtab_node::add_to_same_comdat_group (symtab_node *old_node)
457 gcc_assert (old_node->get_comdat_group ());
458 gcc_assert (!same_comdat_group);
459 gcc_assert (this != old_node);
461 set_comdat_group (old_node->get_comdat_group ());
462 same_comdat_group = old_node;
463 if (!old_node->same_comdat_group)
464 old_node->same_comdat_group = this;
465 else
467 symtab_node *n;
468 for (n = old_node->same_comdat_group;
469 n->same_comdat_group != old_node;
470 n = n->same_comdat_group)
472 n->same_comdat_group = this;
476 /* Dissolve the same_comdat_group list in which NODE resides. */
478 void
479 symtab_node::dissolve_same_comdat_group_list (void)
481 symtab_node *n = this;
482 symtab_node *next;
484 if (!same_comdat_group)
485 return;
488 next = n->same_comdat_group;
489 n->same_comdat_group = NULL;
490 /* Clear comdat_group for comdat locals, since
491 make_decl_local doesn't. */
492 if (!TREE_PUBLIC (n->decl))
493 n->set_comdat_group (NULL);
494 n = next;
496 while (n != this);
499 /* Return printable assembler name of NODE.
500 This function is used only for debugging. When assembler name
501 is unknown go with identifier name. */
503 const char *
504 symtab_node::asm_name () const
506 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
507 return name ();
508 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
511 /* Return printable identifier name. */
513 const char *
514 symtab_node::name () const
516 if (!DECL_NAME (decl))
518 if (DECL_ASSEMBLER_NAME_SET_P (decl))
519 return asm_name ();
520 else
521 return "<unnamed>";
523 return lang_hooks.decl_printable_name (decl, 2);
526 /* Return ipa reference from this symtab_node to
527 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
528 of the use. */
530 ipa_ref *
531 symtab_node::create_reference (symtab_node *referred_node,
532 enum ipa_ref_use use_type)
534 return create_reference (referred_node, use_type, NULL);
538 /* Return ipa reference from this symtab_node to
539 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
540 of the use and STMT the statement (if it exists). */
542 ipa_ref *
543 symtab_node::create_reference (symtab_node *referred_node,
544 enum ipa_ref_use use_type, gimple *stmt)
546 ipa_ref *ref = NULL, *ref2 = NULL;
547 ipa_ref_list *list, *list2;
548 ipa_ref_t *old_references;
550 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
551 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
553 list = &ref_list;
554 old_references = vec_safe_address (list->references);
555 vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
556 ref = &list->references->last ();
558 list2 = &referred_node->ref_list;
560 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
561 if(use_type == IPA_REF_ALIAS)
563 list2->referring.safe_insert (0, ref);
564 ref->referred_index = 0;
566 for (unsigned int i = 1; i < list2->referring.length (); i++)
567 list2->referring[i]->referred_index = i;
569 else
571 list2->referring.safe_push (ref);
572 ref->referred_index = list2->referring.length () - 1;
575 ref->referring = this;
576 ref->referred = referred_node;
577 ref->stmt = stmt;
578 ref->lto_stmt_uid = 0;
579 ref->use = use_type;
580 ref->speculative = 0;
582 /* If vector was moved in memory, update pointers. */
583 if (old_references != list->references->address ())
585 int i;
586 for (i = 0; iterate_reference(i, ref2); i++)
587 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
589 return ref;
592 /* If VAL is a reference to a function or a variable, add a reference from
593 this symtab_node to the corresponding symbol table node. USE_TYPE specify
594 type of the use and STMT the statement (if it exists). Return the new
595 reference or NULL if none was created. */
597 ipa_ref *
598 symtab_node::maybe_create_reference (tree val, enum ipa_ref_use use_type,
599 gimple *stmt)
601 STRIP_NOPS (val);
602 if (TREE_CODE (val) != ADDR_EXPR)
603 return NULL;
604 val = get_base_var (val);
605 if (val && (TREE_CODE (val) == FUNCTION_DECL
606 || TREE_CODE (val) == VAR_DECL))
608 symtab_node *referred = symtab_node::get (val);
609 gcc_checking_assert (referred);
610 return create_reference (referred, use_type, stmt);
612 return NULL;
615 /* Clone all references from symtab NODE to this symtab_node. */
617 void
618 symtab_node::clone_references (symtab_node *node)
620 ipa_ref *ref = NULL, *ref2 = NULL;
621 int i;
622 for (i = 0; node->iterate_reference (i, ref); i++)
624 bool speculative = ref->speculative;
625 unsigned int stmt_uid = ref->lto_stmt_uid;
627 ref2 = create_reference (ref->referred, ref->use, ref->stmt);
628 ref2->speculative = speculative;
629 ref2->lto_stmt_uid = stmt_uid;
633 /* Clone all referring from symtab NODE to this symtab_node. */
635 void
636 symtab_node::clone_referring (symtab_node *node)
638 ipa_ref *ref = NULL, *ref2 = NULL;
639 int i;
640 for (i = 0; node->iterate_referring(i, ref); i++)
642 bool speculative = ref->speculative;
643 unsigned int stmt_uid = ref->lto_stmt_uid;
645 ref2 = ref->referring->create_reference (this, ref->use, ref->stmt);
646 ref2->speculative = speculative;
647 ref2->lto_stmt_uid = stmt_uid;
651 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
653 ipa_ref *
654 symtab_node::clone_reference (ipa_ref *ref, gimple *stmt)
656 bool speculative = ref->speculative;
657 unsigned int stmt_uid = ref->lto_stmt_uid;
658 ipa_ref *ref2;
660 ref2 = create_reference (ref->referred, ref->use, stmt);
661 ref2->speculative = speculative;
662 ref2->lto_stmt_uid = stmt_uid;
663 return ref2;
666 /* Find the structure describing a reference to REFERRED_NODE
667 and associated with statement STMT. */
669 ipa_ref *
670 symtab_node::find_reference (symtab_node *referred_node,
671 gimple *stmt, unsigned int lto_stmt_uid)
673 ipa_ref *r = NULL;
674 int i;
676 for (i = 0; iterate_reference (i, r); i++)
677 if (r->referred == referred_node
678 && !r->speculative
679 && ((stmt && r->stmt == stmt)
680 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
681 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
682 return r;
683 return NULL;
686 /* Remove all references that are associated with statement STMT. */
688 void
689 symtab_node::remove_stmt_references (gimple *stmt)
691 ipa_ref *r = NULL;
692 int i = 0;
694 while (iterate_reference (i, r))
695 if (r->stmt == stmt)
696 r->remove_reference ();
697 else
698 i++;
701 /* Remove all stmt references in non-speculative references.
702 Those are not maintained during inlining & clonning.
703 The exception are speculative references that are updated along
704 with callgraph edges associated with them. */
706 void
707 symtab_node::clear_stmts_in_references (void)
709 ipa_ref *r = NULL;
710 int i;
712 for (i = 0; iterate_reference (i, r); i++)
713 if (!r->speculative)
715 r->stmt = NULL;
716 r->lto_stmt_uid = 0;
720 /* Remove all references in ref list. */
722 void
723 symtab_node::remove_all_references (void)
725 while (vec_safe_length (ref_list.references))
726 ref_list.references->last ().remove_reference ();
727 vec_free (ref_list.references);
730 /* Remove all referring items in ref list. */
732 void
733 symtab_node::remove_all_referring (void)
735 while (ref_list.referring.length ())
736 ref_list.referring.last ()->remove_reference ();
737 ref_list.referring.release ();
740 /* Dump references in ref list to FILE. */
742 void
743 symtab_node::dump_references (FILE *file)
745 ipa_ref *ref = NULL;
746 int i;
747 for (i = 0; iterate_reference (i, ref); i++)
749 fprintf (file, "%s/%i (%s)",
750 ref->referred->asm_name (),
751 ref->referred->order,
752 ipa_ref_use_name [ref->use]);
753 if (ref->speculative)
754 fprintf (file, " (speculative)");
756 fprintf (file, "\n");
759 /* Dump referring in list to FILE. */
761 void
762 symtab_node::dump_referring (FILE *file)
764 ipa_ref *ref = NULL;
765 int i;
766 for (i = 0; iterate_referring(i, ref); i++)
768 fprintf (file, "%s/%i (%s)",
769 ref->referring->asm_name (),
770 ref->referring->order,
771 ipa_ref_use_name [ref->use]);
772 if (ref->speculative)
773 fprintf (file, " (speculative)");
775 fprintf (file, "\n");
778 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
780 /* Dump base fields of symtab nodes to F. Not to be used directly. */
782 void
783 symtab_node::dump_base (FILE *f)
785 static const char * const visibility_types[] = {
786 "default", "protected", "hidden", "internal"
789 fprintf (f, "%s/%i (%s)", asm_name (), order, name ());
790 dump_addr (f, " @", (void *)this);
791 fprintf (f, "\n Type: %s", symtab_type_names[type]);
793 if (definition)
794 fprintf (f, " definition");
795 if (analyzed)
796 fprintf (f, " analyzed");
797 if (alias)
798 fprintf (f, " alias");
799 if (transparent_alias)
800 fprintf (f, " transparent_alias");
801 if (weakref)
802 fprintf (f, " weakref");
803 if (cpp_implicit_alias)
804 fprintf (f, " cpp_implicit_alias");
805 if (alias_target)
806 fprintf (f, " target:%s",
807 DECL_P (alias_target)
808 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
809 (alias_target))
810 : IDENTIFIER_POINTER (alias_target));
811 if (body_removed)
812 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
813 fprintf (f, "\n Visibility:");
814 if (in_other_partition)
815 fprintf (f, " in_other_partition");
816 if (used_from_other_partition)
817 fprintf (f, " used_from_other_partition");
818 if (force_output)
819 fprintf (f, " force_output");
820 if (forced_by_abi)
821 fprintf (f, " forced_by_abi");
822 if (externally_visible)
823 fprintf (f, " externally_visible");
824 if (no_reorder)
825 fprintf (f, " no_reorder");
826 if (resolution != LDPR_UNKNOWN)
827 fprintf (f, " %s",
828 ld_plugin_symbol_resolution_names[(int)resolution]);
829 if (TREE_ASM_WRITTEN (decl))
830 fprintf (f, " asm_written");
831 if (DECL_EXTERNAL (decl))
832 fprintf (f, " external");
833 if (TREE_PUBLIC (decl))
834 fprintf (f, " public");
835 if (DECL_COMMON (decl))
836 fprintf (f, " common");
837 if (DECL_WEAK (decl))
838 fprintf (f, " weak");
839 if (DECL_DLLIMPORT_P (decl))
840 fprintf (f, " dll_import");
841 if (DECL_COMDAT (decl))
842 fprintf (f, " comdat");
843 if (get_comdat_group ())
844 fprintf (f, " comdat_group:%s",
845 IDENTIFIER_POINTER (get_comdat_group_id ()));
846 if (DECL_ONE_ONLY (decl))
847 fprintf (f, " one_only");
848 if (get_section ())
849 fprintf (f, " section:%s",
850 get_section ());
851 if (implicit_section)
852 fprintf (f," (implicit_section)");
853 if (DECL_VISIBILITY_SPECIFIED (decl))
854 fprintf (f, " visibility_specified");
855 if (DECL_VISIBILITY (decl))
856 fprintf (f, " visibility:%s",
857 visibility_types [DECL_VISIBILITY (decl)]);
858 if (DECL_VIRTUAL_P (decl))
859 fprintf (f, " virtual");
860 if (DECL_ARTIFICIAL (decl))
861 fprintf (f, " artificial");
862 if (TREE_CODE (decl) == FUNCTION_DECL)
864 if (DECL_STATIC_CONSTRUCTOR (decl))
865 fprintf (f, " constructor");
866 if (DECL_STATIC_DESTRUCTOR (decl))
867 fprintf (f, " destructor");
869 fprintf (f, "\n");
871 if (same_comdat_group)
872 fprintf (f, " Same comdat group as: %s/%i\n",
873 same_comdat_group->asm_name (),
874 same_comdat_group->order);
875 if (next_sharing_asm_name)
876 fprintf (f, " next sharing asm name: %i\n",
877 next_sharing_asm_name->order);
878 if (previous_sharing_asm_name)
879 fprintf (f, " previous sharing asm name: %i\n",
880 previous_sharing_asm_name->order);
882 if (address_taken)
883 fprintf (f, " Address is taken.\n");
884 if (aux)
886 fprintf (f, " Aux:");
887 dump_addr (f, " @", (void *)aux);
890 fprintf (f, " References: ");
891 dump_references (f);
892 fprintf (f, " Referring: ");
893 dump_referring (f);
894 if (lto_file_data)
895 fprintf (f, " Read from file: %s\n",
896 lto_file_data->file_name);
899 /* Dump symtab node to F. */
901 void
902 symtab_node::dump (FILE *f)
904 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
905 cnode->dump (f);
906 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
907 vnode->dump (f);
910 /* Dump symbol table to F. */
912 void
913 symtab_node::dump_table (FILE *f)
915 symtab_node *node;
916 fprintf (f, "Symbol table:\n\n");
917 FOR_EACH_SYMBOL (node)
918 node->dump (f);
922 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
923 Return NULL if there's no such node. */
925 symtab_node *
926 symtab_node::get_for_asmname (const_tree asmname)
928 symtab_node *node;
930 symtab->symtab_initialize_asm_name_hash ();
931 hashval_t hash = symtab->decl_assembler_name_hash (asmname);
932 symtab_node **slot
933 = symtab->assembler_name_hash->find_slot_with_hash (asmname, hash,
934 NO_INSERT);
936 if (slot)
938 node = *slot;
939 return node;
941 return NULL;
944 /* Dump symtab node NODE to stderr. */
946 DEBUG_FUNCTION void
947 symtab_node::debug (void)
949 dump (stderr);
952 /* Verify common part of symtab nodes. */
954 DEBUG_FUNCTION bool
955 symtab_node::verify_base (void)
957 bool error_found = false;
958 symtab_node *hashed_node;
960 if (is_a <cgraph_node *> (this))
962 if (TREE_CODE (decl) != FUNCTION_DECL)
964 error ("function symbol is not function");
965 error_found = true;
968 else if (is_a <varpool_node *> (this))
970 if (TREE_CODE (decl) != VAR_DECL)
972 error ("variable symbol is not variable");
973 error_found = true;
976 else
978 error ("node has unknown type");
979 error_found = true;
982 if (symtab->state != LTO_STREAMING)
984 hashed_node = symtab_node::get (decl);
985 if (!hashed_node)
987 error ("node not found node->decl->decl_with_vis.symtab_node");
988 error_found = true;
990 if (hashed_node != this
991 && (!is_a <cgraph_node *> (this)
992 || !dyn_cast <cgraph_node *> (this)->clone_of
993 || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl))
995 error ("node differs from node->decl->decl_with_vis.symtab_node");
996 error_found = true;
999 if (symtab->assembler_name_hash)
1001 hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
1002 if (hashed_node && hashed_node->previous_sharing_asm_name)
1004 error ("assembler name hash list corrupted");
1005 error_found = true;
1007 while (hashed_node)
1009 if (hashed_node == this)
1010 break;
1011 hashed_node = hashed_node->next_sharing_asm_name;
1013 if (!hashed_node
1014 && !(is_a <varpool_node *> (this)
1015 && DECL_HARD_REGISTER (decl)))
1017 error ("node not found in symtab assembler name hash");
1018 error_found = true;
1021 if (previous_sharing_asm_name
1022 && previous_sharing_asm_name->next_sharing_asm_name != this)
1024 error ("double linked list of assembler names corrupted");
1025 error_found = true;
1027 if (body_removed && definition)
1029 error ("node has body_removed but is definition");
1030 error_found = true;
1032 if (analyzed && !definition)
1034 error ("node is analyzed byt it is not a definition");
1035 error_found = true;
1037 if (cpp_implicit_alias && !alias)
1039 error ("node is alias but not implicit alias");
1040 error_found = true;
1042 if (alias && !definition && !weakref)
1044 error ("node is alias but not definition");
1045 error_found = true;
1047 if (weakref && !transparent_alias)
1049 error ("node is weakref but not an transparent_alias");
1050 error_found = true;
1052 if (transparent_alias && !alias)
1054 error ("node is transparent_alias but not an alias");
1055 error_found = true;
1057 if (same_comdat_group)
1059 symtab_node *n = same_comdat_group;
1061 if (!n->get_comdat_group ())
1063 error ("node is in same_comdat_group list but has no comdat_group");
1064 error_found = true;
1066 if (n->get_comdat_group () != get_comdat_group ())
1068 error ("same_comdat_group list across different groups");
1069 error_found = true;
1071 if (n->type != type)
1073 error ("mixing different types of symbol in same comdat groups is not supported");
1074 error_found = true;
1076 if (n == this)
1078 error ("node is alone in a comdat group");
1079 error_found = true;
1083 if (!n->same_comdat_group)
1085 error ("same_comdat_group is not a circular list");
1086 error_found = true;
1087 break;
1089 n = n->same_comdat_group;
1091 while (n != this);
1092 if (comdat_local_p ())
1094 ipa_ref *ref = NULL;
1096 for (int i = 0; iterate_referring (i, ref); ++i)
1098 if (!in_same_comdat_group_p (ref->referring))
1100 error ("comdat-local symbol referred to by %s outside its "
1101 "comdat",
1102 identifier_to_locale (ref->referring->name()));
1103 error_found = true;
1108 if (implicit_section && !get_section ())
1110 error ("implicit_section flag is set but section isn't");
1111 error_found = true;
1113 if (get_section () && get_comdat_group ()
1114 && !implicit_section
1115 && !lookup_attribute ("section", DECL_ATTRIBUTES (decl)))
1117 error ("Both section and comdat group is set");
1118 error_found = true;
1120 /* TODO: Add string table for sections, so we do not keep holding duplicated
1121 strings. */
1122 if (alias && definition
1123 && get_section () != get_alias_target ()->get_section ()
1124 && (!get_section()
1125 || !get_alias_target ()->get_section ()
1126 || strcmp (get_section(),
1127 get_alias_target ()->get_section ())))
1129 error ("Alias and target's section differs");
1130 get_alias_target ()->dump (stderr);
1131 error_found = true;
1133 if (alias && definition
1134 && get_comdat_group () != get_alias_target ()->get_comdat_group ())
1136 error ("Alias and target's comdat groups differs");
1137 get_alias_target ()->dump (stderr);
1138 error_found = true;
1140 if (transparent_alias && definition && !weakref)
1142 symtab_node *to = get_alias_target ();
1143 const char *name1
1144 = IDENTIFIER_POINTER (
1145 ultimate_transparent_alias_target (DECL_ASSEMBLER_NAME (decl)));
1146 const char *name2
1147 = IDENTIFIER_POINTER (
1148 ultimate_transparent_alias_target (DECL_ASSEMBLER_NAME (to->decl)));
1149 if (!symbol_table::assembler_names_equal_p (name1, name2))
1151 error ("Transparent alias and target's assembler names differs");
1152 get_alias_target ()->dump (stderr);
1153 error_found = true;
1156 if (transparent_alias && definition
1157 && get_alias_target()->transparent_alias && get_alias_target()->analyzed)
1159 error ("Chained transparent aliases");
1160 get_alias_target ()->dump (stderr);
1161 error_found = true;
1164 return error_found;
1167 /* Verify consistency of NODE. */
1169 DEBUG_FUNCTION void
1170 symtab_node::verify (void)
1172 if (seen_error ())
1173 return;
1175 timevar_push (TV_CGRAPH_VERIFY);
1176 if (cgraph_node *node = dyn_cast <cgraph_node *> (this))
1177 node->verify_node ();
1178 else
1179 if (verify_base ())
1181 debug ();
1182 internal_error ("symtab_node::verify failed");
1184 timevar_pop (TV_CGRAPH_VERIFY);
1187 /* Verify symbol table for internal consistency. */
1189 DEBUG_FUNCTION void
1190 symtab_node::verify_symtab_nodes (void)
1192 symtab_node *node;
1193 hash_map<tree, symtab_node *> comdat_head_map (251);
1195 FOR_EACH_SYMBOL (node)
1197 node->verify ();
1198 if (node->get_comdat_group ())
1200 symtab_node **entry, *s;
1201 bool existed;
1203 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1204 &existed);
1205 if (!existed)
1206 *entry = node;
1207 else if (!DECL_EXTERNAL (node->decl))
1209 for (s = (*entry)->same_comdat_group;
1210 s != NULL && s != node && s != *entry;
1211 s = s->same_comdat_group)
1213 if (!s || s == *entry)
1215 error ("Two symbols with same comdat_group are not linked by "
1216 "the same_comdat_group list.");
1217 (*entry)->debug ();
1218 node->debug ();
1219 internal_error ("symtab_node::verify failed");
1226 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1227 but other code such as notice_global_symbol generates rtl. */
1229 void
1230 symtab_node::make_decl_local (void)
1232 rtx rtl, symbol;
1234 if (weakref)
1236 weakref = false;
1237 IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl)) = 0;
1238 TREE_CHAIN (DECL_ASSEMBLER_NAME (decl)) = NULL_TREE;
1239 symtab->change_decl_assembler_name
1240 (decl, DECL_ASSEMBLER_NAME (get_alias_target ()->decl));
1241 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
1242 DECL_ATTRIBUTES (decl));
1244 /* Avoid clearing comdat_groups on comdat-local decls. */
1245 else if (TREE_PUBLIC (decl) == 0)
1246 return;
1248 /* Localizing a symbol also make all its transparent aliases local. */
1249 ipa_ref *ref;
1250 for (unsigned i = 0; iterate_direct_aliases (i, ref); i++)
1252 struct symtab_node *alias = ref->referring;
1253 if (alias->transparent_alias)
1254 alias->make_decl_local ();
1257 if (TREE_CODE (decl) == VAR_DECL)
1259 DECL_COMMON (decl) = 0;
1260 /* ADDRESSABLE flag is not defined for public symbols. */
1261 TREE_ADDRESSABLE (decl) = 1;
1262 TREE_STATIC (decl) = 1;
1264 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1266 DECL_COMDAT (decl) = 0;
1267 DECL_WEAK (decl) = 0;
1268 DECL_EXTERNAL (decl) = 0;
1269 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1270 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1271 TREE_PUBLIC (decl) = 0;
1272 DECL_DLLIMPORT_P (decl) = 0;
1273 if (!DECL_RTL_SET_P (decl))
1274 return;
1276 /* Update rtl flags. */
1277 make_decl_rtl (decl);
1279 rtl = DECL_RTL (decl);
1280 if (!MEM_P (rtl))
1281 return;
1283 symbol = XEXP (rtl, 0);
1284 if (GET_CODE (symbol) != SYMBOL_REF)
1285 return;
1287 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1290 /* Copy visibility from N.
1291 This is useful when THIS becomes a transparent alias of N. */
1293 void
1294 symtab_node::copy_visibility_from (symtab_node *n)
1296 gcc_checking_assert (n->weakref == weakref);
1298 ipa_ref *ref;
1299 for (unsigned i = 0; iterate_direct_aliases (i, ref); i++)
1301 struct symtab_node *alias = ref->referring;
1302 if (alias->transparent_alias)
1303 alias->copy_visibility_from (n);
1306 if (TREE_CODE (decl) == VAR_DECL)
1308 DECL_COMMON (decl) = DECL_COMMON (n->decl);
1309 /* ADDRESSABLE flag is not defined for public symbols. */
1310 if (TREE_PUBLIC (decl) && !TREE_PUBLIC (n->decl))
1311 TREE_ADDRESSABLE (decl) = 1;
1312 TREE_STATIC (decl) = TREE_STATIC (n->decl);
1314 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1316 DECL_COMDAT (decl) = DECL_COMDAT (n->decl);
1317 DECL_WEAK (decl) = DECL_WEAK (n->decl);
1318 DECL_EXTERNAL (decl) = DECL_EXTERNAL (n->decl);
1319 DECL_VISIBILITY_SPECIFIED (decl) = DECL_VISIBILITY_SPECIFIED (n->decl);
1320 DECL_VISIBILITY (decl) = DECL_VISIBILITY (n->decl);
1321 TREE_PUBLIC (decl) = TREE_PUBLIC (n->decl);
1322 DECL_DLLIMPORT_P (decl) = DECL_DLLIMPORT_P (n->decl);
1323 resolution = n->resolution;
1324 set_comdat_group (n->get_comdat_group ());
1325 call_for_symbol_and_aliases (symtab_node::set_section,
1326 const_cast<char *>(n->get_section ()), true);
1327 externally_visible = n->externally_visible;
1328 if (!DECL_RTL_SET_P (decl))
1329 return;
1331 /* Update rtl flags. */
1332 make_decl_rtl (decl);
1334 rtx rtl = DECL_RTL (decl);
1335 if (!MEM_P (rtl))
1336 return;
1338 rtx symbol = XEXP (rtl, 0);
1339 if (GET_CODE (symbol) != SYMBOL_REF)
1340 return;
1342 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1345 /* Walk the alias chain to return the symbol NODE is alias of.
1346 If NODE is not an alias, return NODE.
1347 Assumes NODE is known to be alias. */
1349 symtab_node *
1350 symtab_node::ultimate_alias_target_1 (enum availability *availability)
1352 bool transparent_p = false;
1354 /* To determine visibility of the target, we follow ELF semantic of aliases.
1355 Here alias is an alternative assembler name of a given definition. Its
1356 availability prevails the availability of its target (i.e. static alias of
1357 weak definition is available.
1359 Transaparent alias is just alternative anme of a given symbol used within
1360 one compilation unit and is translated prior hitting the object file. It
1361 inherits the visibility of its target.
1362 Weakref is a different animal (and noweak definition is weak).
1364 If we ever get into supporting targets with different semantics, a target
1365 hook will be needed here. */
1367 if (availability)
1369 transparent_p = transparent_alias;
1370 if (!transparent_p)
1371 *availability = get_availability ();
1372 else
1373 *availability = AVAIL_NOT_AVAILABLE;
1376 symtab_node *node = this;
1377 while (node)
1379 if (node->alias && node->analyzed)
1380 node = node->get_alias_target ();
1381 else
1383 if (!availability || (!transparent_p && node->analyzed))
1385 else if (node->analyzed && !node->transparent_alias)
1386 *availability = node->get_availability ();
1387 else
1388 *availability = AVAIL_NOT_AVAILABLE;
1389 return node;
1391 if (node && availability && transparent_p
1392 && node->transparent_alias)
1394 *availability = node->get_availability ();
1395 transparent_p = false;
1398 if (availability)
1399 *availability = AVAIL_NOT_AVAILABLE;
1400 return NULL;
1403 /* C++ FE sometimes change linkage flags after producing same body aliases.
1405 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1406 are obviously equivalent. The way it is doing so is however somewhat
1407 kludgy and interferes with the visibility code. As a result we need to
1408 copy the visibility from the target to get things right. */
1410 void
1411 symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target)
1413 if (is_a <cgraph_node *> (this))
1415 DECL_DECLARED_INLINE_P (decl)
1416 = DECL_DECLARED_INLINE_P (target->decl);
1417 DECL_DISREGARD_INLINE_LIMITS (decl)
1418 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1420 /* FIXME: It is not really clear why those flags should not be copied for
1421 functions, too. */
1422 else
1424 DECL_WEAK (decl) = DECL_WEAK (target->decl);
1425 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1426 DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl);
1428 if (TREE_PUBLIC (decl))
1430 tree group;
1432 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1433 DECL_COMDAT (decl) = DECL_COMDAT (target->decl);
1434 group = target->get_comdat_group ();
1435 set_comdat_group (group);
1436 if (group && !same_comdat_group)
1437 add_to_same_comdat_group (target);
1439 externally_visible = target->externally_visible;
1442 /* Set section, do not recurse into aliases.
1443 When one wants to change section of symbol and its aliases,
1444 use set_section. */
1446 void
1447 symtab_node::set_section_for_node (const char *section)
1449 const char *current = get_section ();
1450 section_hash_entry **slot;
1452 if (current == section
1453 || (current && section
1454 && !strcmp (current, section)))
1455 return;
1457 if (current)
1459 x_section->ref_count--;
1460 if (!x_section->ref_count)
1462 hashval_t hash = htab_hash_string (x_section->name);
1463 slot = symtab->section_hash->find_slot_with_hash (x_section->name,
1464 hash, INSERT);
1465 ggc_free (x_section);
1466 symtab->section_hash->clear_slot (slot);
1468 x_section = NULL;
1470 if (!section)
1472 implicit_section = false;
1473 return;
1475 if (!symtab->section_hash)
1476 symtab->section_hash = hash_table<section_name_hasher>::create_ggc (10);
1477 slot = symtab->section_hash->find_slot_with_hash (section,
1478 htab_hash_string (section),
1479 INSERT);
1480 if (*slot)
1481 x_section = (section_hash_entry *)*slot;
1482 else
1484 int len = strlen (section);
1485 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1486 x_section->name = ggc_vec_alloc<char> (len + 1);
1487 memcpy (x_section->name, section, len + 1);
1489 x_section->ref_count++;
1492 /* Worker for set_section. */
1494 bool
1495 symtab_node::set_section (symtab_node *n, void *s)
1497 n->set_section_for_node ((char *)s);
1498 return false;
1501 /* Set section of symbol and its aliases. */
1503 void
1504 symtab_node::set_section (const char *section)
1506 gcc_assert (!this->alias);
1507 call_for_symbol_and_aliases
1508 (symtab_node::set_section, const_cast<char *>(section), true);
1511 /* Return the initialization priority. */
1513 priority_type
1514 symtab_node::get_init_priority ()
1516 if (!this->in_init_priority_hash)
1517 return DEFAULT_INIT_PRIORITY;
1519 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1520 return h ? h->init : DEFAULT_INIT_PRIORITY;
1523 /* Return the finalization priority. */
1525 priority_type
1526 cgraph_node::get_fini_priority ()
1528 if (!this->in_init_priority_hash)
1529 return DEFAULT_INIT_PRIORITY;
1530 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1531 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1534 /* Return the initialization and finalization priority information for
1535 DECL. If there is no previous priority information, a freshly
1536 allocated structure is returned. */
1538 symbol_priority_map *
1539 symtab_node::priority_info (void)
1541 if (!symtab->init_priority_hash)
1542 symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13);
1544 bool existed;
1545 symbol_priority_map *h
1546 = &symtab->init_priority_hash->get_or_insert (this, &existed);
1547 if (!existed)
1549 h->init = DEFAULT_INIT_PRIORITY;
1550 h->fini = DEFAULT_INIT_PRIORITY;
1551 in_init_priority_hash = true;
1554 return h;
1557 /* Set initialization priority to PRIORITY. */
1559 void
1560 symtab_node::set_init_priority (priority_type priority)
1562 symbol_priority_map *h;
1564 if (is_a <cgraph_node *> (this))
1565 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1567 if (priority == DEFAULT_INIT_PRIORITY)
1569 gcc_assert (get_init_priority() == priority);
1570 return;
1572 h = priority_info ();
1573 h->init = priority;
1576 /* Set fialization priority to PRIORITY. */
1578 void
1579 cgraph_node::set_fini_priority (priority_type priority)
1581 symbol_priority_map *h;
1583 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1585 if (priority == DEFAULT_INIT_PRIORITY)
1587 gcc_assert (get_fini_priority() == priority);
1588 return;
1590 h = priority_info ();
1591 h->fini = priority;
1594 /* Worker for symtab_resolve_alias. */
1596 bool
1597 symtab_node::set_implicit_section (symtab_node *n,
1598 void *data ATTRIBUTE_UNUSED)
1600 n->implicit_section = true;
1601 return false;
1604 /* Add reference recording that symtab node is alias of TARGET.
1605 The function can fail in the case of aliasing cycles; in this case
1606 it returns false. */
1608 bool
1609 symtab_node::resolve_alias (symtab_node *target, bool transparent)
1611 symtab_node *n;
1613 gcc_assert (!analyzed && !vec_safe_length (ref_list.references));
1615 /* Never let cycles to creep into the symbol table alias references;
1616 those will make alias walkers to be infinite. */
1617 for (n = target; n && n->alias;
1618 n = n->analyzed ? n->get_alias_target () : NULL)
1619 if (n == this)
1621 if (is_a <cgraph_node *> (this))
1622 error ("function %q+D part of alias cycle", decl);
1623 else if (is_a <varpool_node *> (this))
1624 error ("variable %q+D part of alias cycle", decl);
1625 else
1626 gcc_unreachable ();
1627 alias = false;
1628 return false;
1631 /* "analyze" the node - i.e. mark the reference. */
1632 definition = true;
1633 alias = true;
1634 analyzed = true;
1635 transparent |= transparent_alias;
1636 transparent_alias = transparent;
1637 if (transparent)
1638 while (target->transparent_alias && target->analyzed)
1639 target = target->get_alias_target ();
1640 create_reference (target, IPA_REF_ALIAS, NULL);
1642 /* Add alias into the comdat group of its target unless it is already there. */
1643 if (same_comdat_group)
1644 remove_from_same_comdat_group ();
1645 set_comdat_group (NULL);
1646 if (target->get_comdat_group ())
1647 add_to_same_comdat_group (target);
1649 if ((get_section () != target->get_section ()
1650 || target->get_comdat_group ()) && get_section () && !implicit_section)
1652 error ("section of alias %q+D must match section of its target", decl);
1654 call_for_symbol_and_aliases (symtab_node::set_section,
1655 const_cast<char *>(target->get_section ()), true);
1656 if (target->implicit_section)
1657 call_for_symbol_and_aliases (set_implicit_section, NULL, true);
1659 /* Alias targets become redundant after alias is resolved into an reference.
1660 We do not want to keep it around or we would have to mind updating them
1661 when renaming symbols. */
1662 alias_target = NULL;
1664 if (!transparent && cpp_implicit_alias && symtab->state >= CONSTRUCTION)
1665 fixup_same_cpp_alias_visibility (target);
1667 /* If alias has address taken, so does the target. */
1668 if (address_taken)
1669 target->ultimate_alias_target ()->address_taken = true;
1671 /* All non-transparent aliases of THIS are now in fact aliases of TARGET.
1672 If alias is transparent, also all transparent aliases of THIS are now
1673 aliases of TARGET.
1674 Also merge same comdat group lists. */
1675 ipa_ref *ref;
1676 for (unsigned i = 0; iterate_direct_aliases (i, ref);)
1678 struct symtab_node *alias_alias = ref->referring;
1679 if (alias_alias->get_comdat_group ())
1681 alias_alias->remove_from_same_comdat_group ();
1682 alias_alias->set_comdat_group (NULL);
1683 if (target->get_comdat_group ())
1684 alias_alias->add_to_same_comdat_group (target);
1686 if (!alias_alias->transparent_alias || transparent)
1688 alias_alias->remove_all_references ();
1689 alias_alias->create_reference (target, IPA_REF_ALIAS, NULL);
1691 else i++;
1693 return true;
1696 /* Worker searching noninterposable alias. */
1698 bool
1699 symtab_node::noninterposable_alias (symtab_node *node, void *data)
1701 if (!node->transparent_alias && decl_binds_to_current_def_p (node->decl))
1703 symtab_node *fn = node->ultimate_alias_target ();
1705 /* Ensure that the alias is well formed this may not be the case
1706 of user defined aliases and currently it is not always the case
1707 of C++ same body aliases (that is a bug). */
1708 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
1709 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
1710 || (TREE_CODE (node->decl) == FUNCTION_DECL
1711 && flags_from_decl_or_type (node->decl)
1712 != flags_from_decl_or_type (fn->decl))
1713 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
1714 return false;
1715 *(symtab_node **)data = node;
1716 return true;
1718 return false;
1721 /* If node can not be overwriten by static or dynamic linker to point to
1722 different definition, return NODE. Otherwise look for alias with such
1723 property and if none exists, introduce new one. */
1725 symtab_node *
1726 symtab_node::noninterposable_alias (void)
1728 tree new_decl;
1729 symtab_node *new_node = NULL;
1731 /* First try to look up existing alias or base object
1732 (if that is already non-overwritable). */
1733 symtab_node *node = ultimate_alias_target ();
1734 gcc_assert (!node->alias && !node->weakref);
1735 node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias,
1736 (void *)&new_node, true);
1737 if (new_node)
1738 return new_node;
1739 #ifndef ASM_OUTPUT_DEF
1740 /* If aliases aren't supported by the assembler, fail. */
1741 return NULL;
1742 #endif
1744 /* Otherwise create a new one. */
1745 new_decl = copy_node (node->decl);
1746 DECL_DLLIMPORT_P (new_decl) = 0;
1747 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1748 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1749 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1750 DECL_INITIAL (new_decl) = NULL;
1751 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1752 SET_DECL_RTL (new_decl, NULL);
1754 /* Update the properties. */
1755 DECL_EXTERNAL (new_decl) = 0;
1756 TREE_PUBLIC (new_decl) = 0;
1757 DECL_COMDAT (new_decl) = 0;
1758 DECL_WEAK (new_decl) = 0;
1760 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1761 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1762 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1764 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1765 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1766 new_node = cgraph_node::create_alias (new_decl, node->decl);
1768 else
1770 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1771 DECL_INITIAL (new_decl) = error_mark_node;
1772 new_node = varpool_node::create_alias (new_decl, node->decl);
1774 new_node->resolve_alias (node);
1775 gcc_assert (decl_binds_to_current_def_p (new_decl)
1776 && targetm.binds_local_p (new_decl));
1777 return new_node;
1780 /* Return true if symtab node and TARGET represents
1781 semantically equivalent symbols. */
1783 bool
1784 symtab_node::semantically_equivalent_p (symtab_node *target)
1786 enum availability avail;
1787 symtab_node *ba;
1788 symtab_node *bb;
1790 /* Equivalent functions are equivalent. */
1791 if (decl == target->decl)
1792 return true;
1794 /* If symbol is not overwritable by different implementation,
1795 walk to the base object it defines. */
1796 ba = ultimate_alias_target (&avail);
1797 if (avail >= AVAIL_AVAILABLE)
1799 if (target == ba)
1800 return true;
1802 else
1803 ba = this;
1804 bb = target->ultimate_alias_target (&avail);
1805 if (avail >= AVAIL_AVAILABLE)
1807 if (this == bb)
1808 return true;
1810 else
1811 bb = target;
1812 return bb == ba;
1815 /* Classify symbol symtab node for partitioning. */
1817 enum symbol_partitioning_class
1818 symtab_node::get_partitioning_class (void)
1820 /* Inline clones are always duplicated.
1821 This include external delcarations. */
1822 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
1824 if (DECL_ABSTRACT_P (decl))
1825 return SYMBOL_EXTERNAL;
1827 if (cnode && cnode->global.inlined_to)
1828 return SYMBOL_DUPLICATE;
1830 /* Transparent aliases are always duplicated. */
1831 if (transparent_alias)
1832 return definition ? SYMBOL_DUPLICATE : SYMBOL_EXTERNAL;
1834 /* External declarations are external. */
1835 if (DECL_EXTERNAL (decl))
1836 return SYMBOL_EXTERNAL;
1838 if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
1840 if (alias && definition && !ultimate_alias_target ()->definition)
1841 return SYMBOL_EXTERNAL;
1842 /* Constant pool references use local symbol names that can not
1843 be promoted global. We should never put into a constant pool
1844 objects that can not be duplicated across partitions. */
1845 if (DECL_IN_CONSTANT_POOL (decl))
1846 return SYMBOL_DUPLICATE;
1847 if (DECL_HARD_REGISTER (decl))
1848 return SYMBOL_DUPLICATE;
1849 gcc_checking_assert (vnode->definition);
1851 /* Functions that are cloned may stay in callgraph even if they are unused.
1852 Handle them as external; compute_ltrans_boundary take care to make
1853 proper things to happen (i.e. to make them appear in the boundary but
1854 with body streamed, so clone can me materialized). */
1855 else if (!dyn_cast <cgraph_node *> (this)->function_symbol ()->definition)
1856 return SYMBOL_EXTERNAL;
1858 /* Linker discardable symbols are duplicated to every use unless they are
1859 keyed. */
1860 if (DECL_ONE_ONLY (decl)
1861 && !force_output
1862 && !forced_by_abi
1863 && !used_from_object_file_p ())
1864 return SYMBOL_DUPLICATE;
1866 return SYMBOL_PARTITION;
1869 /* Return true when symbol is known to be non-zero. */
1871 bool
1872 symtab_node::nonzero_address ()
1874 /* Weakrefs may be NULL when their target is not defined. */
1875 if (alias && weakref)
1877 if (analyzed)
1879 symtab_node *target = ultimate_alias_target ();
1881 if (target->alias && target->weakref)
1882 return false;
1883 /* We can not recurse to target::nonzero. It is possible that the
1884 target is used only via the alias.
1885 We may walk references and look for strong use, but we do not know
1886 if this strong use will survive to final binary, so be
1887 conservative here.
1888 ??? Maybe we could do the lookup during late optimization that
1889 could be useful to eliminate the NULL pointer checks in LTO
1890 programs. */
1891 if (target->definition && !DECL_EXTERNAL (target->decl))
1892 return true;
1893 if (target->resolution != LDPR_UNKNOWN
1894 && target->resolution != LDPR_UNDEF
1895 && !target->can_be_discarded_p ()
1896 && flag_delete_null_pointer_checks)
1897 return true;
1898 return false;
1900 else
1901 return false;
1904 /* With !flag_delete_null_pointer_checks we assume that symbols may
1905 bind to NULL. This is on by default on embedded targets only.
1907 Otherwise all non-WEAK symbols must be defined and thus non-NULL or
1908 linking fails. Important case of WEAK we want to do well are comdats.
1909 Those are handled by later check for definition.
1911 When parsing, beware the cases when WEAK attribute is added later. */
1912 if (!DECL_WEAK (decl)
1913 && flag_delete_null_pointer_checks)
1915 refuse_visibility_changes = true;
1916 return true;
1919 /* If target is defined and not extern, we know it will be output and thus
1920 it will bind to non-NULL.
1921 Play safe for flag_delete_null_pointer_checks where weak definition maye
1922 be re-defined by NULL. */
1923 if (definition && !DECL_EXTERNAL (decl)
1924 && (flag_delete_null_pointer_checks || !DECL_WEAK (decl)))
1926 if (!DECL_WEAK (decl))
1927 refuse_visibility_changes = true;
1928 return true;
1931 /* As the last resort, check the resolution info. */
1932 if (resolution != LDPR_UNKNOWN
1933 && resolution != LDPR_UNDEF
1934 && !can_be_discarded_p ()
1935 && flag_delete_null_pointer_checks)
1936 return true;
1937 return false;
1940 /* Return 0 if symbol is known to have different address than S2,
1941 Return 1 if symbol is known to have same address as S2,
1942 return -1 otherwise.
1944 If MEMORY_ACCESSED is true, assume that both memory pointer to THIS
1945 and S2 is going to be accessed. This eliminates the situations when
1946 either THIS or S2 is NULL and is seful for comparing bases when deciding
1947 about memory aliasing. */
1949 symtab_node::equal_address_to (symtab_node *s2, bool memory_accessed)
1951 enum availability avail1, avail2;
1953 /* A Shortcut: equivalent symbols are always equivalent. */
1954 if (this == s2)
1955 return 1;
1957 /* Unwind transparent aliases first; those are always equal to their
1958 target. */
1959 if (this->transparent_alias && this->analyzed)
1960 return this->get_alias_target ()->equal_address_to (s2);
1961 while (s2->transparent_alias && s2->analyzed)
1962 s2 = s2->get_alias_target();
1964 if (this == s2)
1965 return 1;
1967 /* For non-interposable aliases, lookup and compare their actual definitions.
1968 Also check if the symbol needs to bind to given definition. */
1969 symtab_node *rs1 = ultimate_alias_target (&avail1);
1970 symtab_node *rs2 = s2->ultimate_alias_target (&avail2);
1971 bool binds_local1 = rs1->analyzed && decl_binds_to_current_def_p (this->decl);
1972 bool binds_local2 = rs2->analyzed && decl_binds_to_current_def_p (s2->decl);
1973 bool really_binds_local1 = binds_local1;
1974 bool really_binds_local2 = binds_local2;
1976 /* Addresses of vtables and virtual functions can not be used by user
1977 code and are used only within speculation. In this case we may make
1978 symbol equivalent to its alias even if interposition may break this
1979 rule. Doing so will allow us to turn speculative inlining into
1980 non-speculative more agressively. */
1981 if (DECL_VIRTUAL_P (this->decl) && avail1 >= AVAIL_AVAILABLE)
1982 binds_local1 = true;
1983 if (DECL_VIRTUAL_P (s2->decl) && avail2 >= AVAIL_AVAILABLE)
1984 binds_local2 = true;
1986 /* If both definitions are available we know that even if they are bound
1987 to other unit they must be defined same way and therefore we can use
1988 equivalence test. */
1989 if (rs1 != rs2 && avail1 >= AVAIL_AVAILABLE && avail2 >= AVAIL_AVAILABLE)
1990 binds_local1 = binds_local2 = true;
1992 if ((binds_local1 ? rs1 : this)
1993 == (binds_local2 ? rs2 : s2))
1995 /* We made use of the fact that alias is not weak. */
1996 if (binds_local1 && rs1 != this)
1997 refuse_visibility_changes = true;
1998 if (binds_local2 && rs2 != s2)
1999 s2->refuse_visibility_changes = true;
2000 return 1;
2003 /* If both symbols may resolve to NULL, we can not really prove them
2004 different. */
2005 if (!memory_accessed && !nonzero_address () && !s2->nonzero_address ())
2006 return -1;
2008 /* Except for NULL, functions and variables never overlap. */
2009 if (TREE_CODE (decl) != TREE_CODE (s2->decl))
2010 return 0;
2012 /* If one of the symbols is unresolved alias, punt. */
2013 if (rs1->alias || rs2->alias)
2014 return -1;
2016 /* If we have a non-interposale definition of at least one of the symbols
2017 and the other symbol is different, we know other unit can not interpose
2018 it to the first symbol; all aliases of the definition needs to be
2019 present in the current unit. */
2020 if (((really_binds_local1 || really_binds_local2)
2021 /* If we have both definitions and they are different, we know they
2022 will be different even in units they binds to. */
2023 || (binds_local1 && binds_local2))
2024 && rs1 != rs2)
2026 /* We make use of the fact that one symbol is not alias of the other
2027 and that the definition is non-interposable. */
2028 refuse_visibility_changes = true;
2029 s2->refuse_visibility_changes = true;
2030 rs1->refuse_visibility_changes = true;
2031 rs2->refuse_visibility_changes = true;
2032 return 0;
2035 /* TODO: Alias oracle basically assume that addresses of global variables
2036 are different unless they are declared as alias of one to another while
2037 the code folding comparsions doesn't.
2038 We probably should be consistent and use this fact here, too, but for
2039 the moment return false only when we are called from the alias oracle. */
2041 return memory_accessed && rs1 != rs2 ? 0 : -1;
2044 /* Worker for call_for_symbol_and_aliases. */
2046 bool
2047 symtab_node::call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *,
2048 void *),
2049 void *data,
2050 bool include_overwritable)
2052 ipa_ref *ref;
2053 FOR_EACH_ALIAS (this, ref)
2055 symtab_node *alias = ref->referring;
2056 if (include_overwritable
2057 || alias->get_availability () > AVAIL_INTERPOSABLE)
2058 if (alias->call_for_symbol_and_aliases (callback, data,
2059 include_overwritable))
2060 return true;
2062 return false;
2065 /* Return true if address of N is possibly compared. */
2067 static bool
2068 address_matters_1 (symtab_node *n, void *)
2070 struct ipa_ref *ref;
2072 if (!n->address_can_be_compared_p ())
2073 return false;
2074 if (n->externally_visible || n->force_output)
2075 return true;
2077 for (unsigned int i = 0; n->iterate_referring (i, ref); i++)
2078 if (ref->address_matters_p ())
2079 return true;
2080 return false;
2083 /* Return true if symbol's address may possibly be compared to other
2084 symbol's address. */
2086 bool
2087 symtab_node::address_matters_p ()
2089 gcc_assert (!alias);
2090 return call_for_symbol_and_aliases (address_matters_1, NULL, true);
2093 /* Return true if symbol's alignment may be increased. */
2095 bool
2096 symtab_node::can_increase_alignment_p (void)
2098 symtab_node *target = ultimate_alias_target ();
2100 /* For now support only variables. */
2101 if (TREE_CODE (decl) != VAR_DECL)
2102 return false;
2104 /* With -fno-toplevel-reorder we may have already output the constant. */
2105 if (TREE_ASM_WRITTEN (target->decl))
2106 return false;
2108 /* If target is already placed in an anchor, we can not touch its
2109 alignment. */
2110 if (DECL_RTL_SET_P (target->decl)
2111 && MEM_P (DECL_RTL (target->decl))
2112 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (DECL_RTL (target->decl), 0)))
2113 return false;
2115 /* Constant pool entries may be shared. */
2116 if (DECL_IN_CONSTANT_POOL (target->decl))
2117 return false;
2119 /* We cannot change alignment of symbols that may bind to symbols
2120 in other translation unit that may contain a definition with lower
2121 alignment. */
2122 if (!decl_binds_to_current_def_p (decl))
2123 return false;
2125 /* When compiling partition, be sure the symbol is not output by other
2126 partition. */
2127 if (flag_ltrans
2128 && (target->in_other_partition
2129 || target->get_partitioning_class () == SYMBOL_DUPLICATE))
2130 return false;
2132 /* Do not override the alignment as specified by the ABI when the used
2133 attribute is set. */
2134 if (DECL_PRESERVE_P (decl) || DECL_PRESERVE_P (target->decl))
2135 return false;
2137 /* Do not override explicit alignment set by the user when an explicit
2138 section name is also used. This is a common idiom used by many
2139 software projects. */
2140 if (DECL_SECTION_NAME (target->decl) != NULL && !target->implicit_section)
2141 return false;
2143 return true;
2146 /* Worker for symtab_node::increase_alignment. */
2148 static bool
2149 increase_alignment_1 (symtab_node *n, void *v)
2151 unsigned int align = (size_t)v;
2152 if (DECL_ALIGN (n->decl) < align
2153 && n->can_increase_alignment_p ())
2155 DECL_ALIGN (n->decl) = align;
2156 DECL_USER_ALIGN (n->decl) = 1;
2158 return false;
2161 /* Increase alignment of THIS to ALIGN. */
2163 void
2164 symtab_node::increase_alignment (unsigned int align)
2166 gcc_assert (can_increase_alignment_p () && align < MAX_OFILE_ALIGNMENT);
2167 ultimate_alias_target()->call_for_symbol_and_aliases (increase_alignment_1,
2168 (void *)(size_t) align,
2169 true);
2170 gcc_assert (DECL_ALIGN (decl) >= align);
2173 /* Helper for symtab_node::definition_alignment. */
2175 static bool
2176 get_alignment_1 (symtab_node *n, void *v)
2178 *((unsigned int *)v) = MAX (*((unsigned int *)v), DECL_ALIGN (n->decl));
2179 return false;
2182 /* Return desired alignment of the definition. This is NOT alignment useful
2183 to access THIS, because THIS may be interposable and DECL_ALIGN should
2184 be used instead. It however must be guaranteed when output definition
2185 of THIS. */
2187 unsigned int
2188 symtab_node::definition_alignment ()
2190 unsigned int align = 0;
2191 gcc_assert (!alias);
2192 call_for_symbol_and_aliases (get_alignment_1, &align, true);
2193 return align;
2196 /* Return symbol used to separate symbol name from suffix. */
2198 char
2199 symbol_table::symbol_suffix_separator ()
2201 #ifndef NO_DOT_IN_LABEL
2202 return '.';
2203 #elif !defined NO_DOLLAR_IN_LABEL
2204 return '$';
2205 #else
2206 return '_';
2207 #endif