2014-09-18 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / symtab.c
blob792b3b50ea6a04bf1ea228a6201475df825c3c45
1 /* Symbol table.
2 Copyright (C) 2012-2014 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 "tm.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "print-tree.h"
28 #include "varasm.h"
29 #include "function.h"
30 #include "emit-rtl.h"
31 #include "basic-block.h"
32 #include "tree-ssa-alias.h"
33 #include "internal-fn.h"
34 #include "gimple-expr.h"
35 #include "is-a.h"
36 #include "gimple.h"
37 #include "tree-inline.h"
38 #include "langhooks.h"
39 #include "hashtab.h"
40 #include "cgraph.h"
41 #include "diagnostic.h"
42 #include "timevar.h"
43 #include "lto-streamer.h"
44 #include "output.h"
45 #include "ipa-utils.h"
46 #include "calls.h"
48 static const char *ipa_ref_use_name[] = {"read","write","addr","alias"};
50 const char * const ld_plugin_symbol_resolution_names[]=
52 "",
53 "undef",
54 "prevailing_def",
55 "prevailing_def_ironly",
56 "preempted_reg",
57 "preempted_ir",
58 "resolved_ir",
59 "resolved_exec",
60 "resolved_dyn",
61 "prevailing_def_ironly_exp"
64 /* Hash asmnames ignoring the user specified marks. */
66 hashval_t
67 symbol_table::decl_assembler_name_hash (const_tree asmname)
69 if (IDENTIFIER_POINTER (asmname)[0] == '*')
71 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
72 size_t ulp_len = strlen (user_label_prefix);
74 if (ulp_len == 0)
76 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
77 decl_str += ulp_len;
79 return htab_hash_string (decl_str);
82 return htab_hash_string (IDENTIFIER_POINTER (asmname));
86 /* Returns a hash code for P. */
88 hashval_t
89 symbol_table::hash_node_by_assembler_name (const void *p)
91 const symtab_node *n = (const symtab_node *) p;
92 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
95 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
97 bool
98 symbol_table::decl_assembler_name_equal (tree decl, const_tree asmname)
100 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
101 const char *decl_str;
102 const char *asmname_str;
103 bool test = false;
105 if (decl_asmname == asmname)
106 return true;
108 decl_str = IDENTIFIER_POINTER (decl_asmname);
109 asmname_str = IDENTIFIER_POINTER (asmname);
112 /* If the target assembler name was set by the user, things are trickier.
113 We have a leading '*' to begin with. After that, it's arguable what
114 is the correct thing to do with -fleading-underscore. Arguably, we've
115 historically been doing the wrong thing in assemble_alias by always
116 printing the leading underscore. Since we're not changing that, make
117 sure user_label_prefix follows the '*' before matching. */
118 if (decl_str[0] == '*')
120 size_t ulp_len = strlen (user_label_prefix);
122 decl_str ++;
124 if (ulp_len == 0)
125 test = true;
126 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
127 decl_str += ulp_len, test=true;
128 else
129 decl_str --;
131 if (asmname_str[0] == '*')
133 size_t ulp_len = strlen (user_label_prefix);
135 asmname_str ++;
137 if (ulp_len == 0)
138 test = true;
139 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
140 asmname_str += ulp_len, test=true;
141 else
142 asmname_str --;
145 if (!test)
146 return false;
147 return strcmp (decl_str, asmname_str) == 0;
151 /* Returns nonzero if P1 and P2 are equal. */
154 symbol_table::eq_assembler_name (const void *p1, const void *p2)
156 const symtab_node *n1 = (const symtab_node *) p1;
157 const_tree name = (const_tree)p2;
158 return (decl_assembler_name_equal (n1->decl, name));
161 /* Insert NODE to assembler name hash. */
163 void
164 symbol_table::insert_to_assembler_name_hash (symtab_node *node,
165 bool with_clones)
167 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
168 return;
169 gcc_checking_assert (!node->previous_sharing_asm_name
170 && !node->next_sharing_asm_name);
171 if (assembler_name_hash)
173 void **aslot;
174 cgraph_node *cnode;
175 tree decl = node->decl;
177 tree name = DECL_ASSEMBLER_NAME (node->decl);
179 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
180 decl_assembler_name_hash (name),
181 INSERT);
182 gcc_assert (*aslot != node);
183 node->next_sharing_asm_name = (symtab_node *)*aslot;
184 if (*aslot != NULL)
185 ((symtab_node *)*aslot)->previous_sharing_asm_name = node;
186 *aslot = node;
188 /* Update also possible inline clones sharing a decl. */
189 cnode = dyn_cast <cgraph_node *> (node);
190 if (cnode && cnode->clones && with_clones)
191 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
192 if (cnode->decl == decl)
193 insert_to_assembler_name_hash (cnode, true);
198 /* Remove NODE from assembler name hash. */
200 void
201 symbol_table::unlink_from_assembler_name_hash (symtab_node *node,
202 bool with_clones)
204 if (assembler_name_hash)
206 cgraph_node *cnode;
207 tree decl = node->decl;
209 if (node->next_sharing_asm_name)
210 node->next_sharing_asm_name->previous_sharing_asm_name
211 = node->previous_sharing_asm_name;
212 if (node->previous_sharing_asm_name)
214 node->previous_sharing_asm_name->next_sharing_asm_name
215 = node->next_sharing_asm_name;
217 else
219 tree name = DECL_ASSEMBLER_NAME (node->decl);
220 void **slot;
221 slot = htab_find_slot_with_hash (assembler_name_hash, name,
222 decl_assembler_name_hash (name),
223 NO_INSERT);
224 gcc_assert (*slot == node);
225 if (!node->next_sharing_asm_name)
226 htab_clear_slot (assembler_name_hash, slot);
227 else
228 *slot = node->next_sharing_asm_name;
230 node->next_sharing_asm_name = NULL;
231 node->previous_sharing_asm_name = NULL;
233 /* Update also possible inline clones sharing a decl. */
234 cnode = dyn_cast <cgraph_node *> (node);
235 if (cnode && cnode->clones && with_clones)
236 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
237 if (cnode->decl == decl)
238 unlink_from_assembler_name_hash (cnode, true);
242 /* Arrange node to be first in its entry of assembler_name_hash. */
244 void
245 symbol_table::symtab_prevail_in_asm_name_hash (symtab_node *node)
247 unlink_from_assembler_name_hash (node, false);
248 insert_to_assembler_name_hash (node, false);
251 /* Initalize asm name hash unless. */
253 void
254 symbol_table::symtab_initialize_asm_name_hash (void)
256 symtab_node *node;
257 if (!assembler_name_hash)
259 assembler_name_hash =
260 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
261 NULL);
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);
296 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
297 && DECL_RTL_SET_P (decl))
298 warning (0, "%D renamed after being referenced in assembly", decl);
300 SET_DECL_ASSEMBLER_NAME (decl, name);
301 if (alias)
303 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
304 TREE_CHAIN (name) = alias;
306 if (node)
307 insert_to_assembler_name_hash (node, true);
311 /* Return true when RESOLUTION indicate that linker will use
312 the symbol from non-LTO object files. */
314 bool
315 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
317 return (resolution == LDPR_PREVAILING_DEF
318 || resolution == LDPR_PREEMPTED_REG
319 || resolution == LDPR_RESOLVED_EXEC
320 || resolution == LDPR_RESOLVED_DYN);
323 /* Hash sections by their names. */
325 static hashval_t
326 hash_section_hash_entry (const void *p)
328 const section_hash_entry *n = (const section_hash_entry *) p;
329 return htab_hash_string (n->name);
332 /* Return true if section P1 name equals to P2. */
334 static int
335 eq_sections (const void *p1, const void *p2)
337 const section_hash_entry *n1 = (const section_hash_entry *) p1;
338 const char *name = (const char *)p2;
339 return n1->name == name || !strcmp (n1->name, name);
342 /* Add node into symbol table. This function is not used directly, but via
343 cgraph/varpool node creation routines. */
345 void
346 symtab_node::register_symbol (void)
348 symtab->register_symbol (this);
350 if (!decl->decl_with_vis.symtab_node)
351 decl->decl_with_vis.symtab_node = this;
353 ref_list.clear ();
355 /* Be sure to do this last; C++ FE might create new nodes via
356 DECL_ASSEMBLER_NAME langhook! */
357 symtab->insert_to_assembler_name_hash (this, false);
360 /* Remove NODE from same comdat group. */
362 void
363 symtab_node::remove_from_same_comdat_group (void)
365 if (same_comdat_group)
367 symtab_node *prev;
368 for (prev = same_comdat_group;
369 prev->same_comdat_group != this;
370 prev = prev->same_comdat_group)
372 if (same_comdat_group == prev)
373 prev->same_comdat_group = NULL;
374 else
375 prev->same_comdat_group = same_comdat_group;
376 same_comdat_group = NULL;
377 set_comdat_group (NULL);
381 /* Remove node from symbol table. This function is not used directly, but via
382 cgraph/varpool node removal routines. */
384 void
385 symtab_node::unregister (void)
387 remove_all_references ();
388 remove_all_referring ();
390 /* Remove reference to section. */
391 set_section_for_node (NULL);
393 remove_from_same_comdat_group ();
395 symtab->unregister (this);
397 /* During LTO symtab merging we temporarily corrupt decl to symtab node
398 hash. */
399 gcc_assert (decl->decl_with_vis.symtab_node || in_lto_p);
400 if (decl->decl_with_vis.symtab_node == this)
402 symtab_node *replacement_node = NULL;
403 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
404 replacement_node = cnode->find_replacement ();
405 decl->decl_with_vis.symtab_node = replacement_node;
407 if (!is_a <varpool_node *> (this) || !DECL_HARD_REGISTER (decl))
408 symtab->unlink_from_assembler_name_hash (this, false);
409 if (in_init_priority_hash)
410 symtab->init_priority_hash->remove (this);
414 /* Remove symbol from symbol table. */
416 void
417 symtab_node::remove (void)
419 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
420 cnode->remove ();
421 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
422 vnode->remove ();
425 /* Add NEW_ to the same comdat group that OLD is in. */
427 void
428 symtab_node::add_to_same_comdat_group (symtab_node *old_node)
430 gcc_assert (old_node->get_comdat_group ());
431 gcc_assert (!same_comdat_group);
432 gcc_assert (this != old_node);
434 set_comdat_group (old_node->get_comdat_group ());
435 same_comdat_group = old_node;
436 if (!old_node->same_comdat_group)
437 old_node->same_comdat_group = this;
438 else
440 symtab_node *n;
441 for (n = old_node->same_comdat_group;
442 n->same_comdat_group != old_node;
443 n = n->same_comdat_group)
445 n->same_comdat_group = this;
449 /* Dissolve the same_comdat_group list in which NODE resides. */
451 void
452 symtab_node::dissolve_same_comdat_group_list (void)
454 symtab_node *n = this;
455 symtab_node *next;
457 if (!same_comdat_group)
458 return;
461 next = n->same_comdat_group;
462 n->same_comdat_group = NULL;
463 /* Clear comdat_group for comdat locals, since
464 make_decl_local doesn't. */
465 if (!TREE_PUBLIC (n->decl))
466 n->set_comdat_group (NULL);
467 n = next;
469 while (n != this);
472 /* Return printable assembler name of NODE.
473 This function is used only for debugging. When assembler name
474 is unknown go with identifier name. */
476 const char *
477 symtab_node::asm_name () const
479 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
480 return lang_hooks.decl_printable_name (decl, 2);
481 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
484 /* Return printable identifier name. */
486 const char *
487 symtab_node::name () const
489 return lang_hooks.decl_printable_name (decl, 2);
492 /* Return ipa reference from this symtab_node to
493 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
494 of the use. */
496 ipa_ref *
497 symtab_node::create_reference (symtab_node *referred_node,
498 enum ipa_ref_use use_type)
500 return create_reference (referred_node, use_type, NULL);
504 /* Return ipa reference from this symtab_node to
505 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
506 of the use and STMT the statement (if it exists). */
508 ipa_ref *
509 symtab_node::create_reference (symtab_node *referred_node,
510 enum ipa_ref_use use_type, gimple stmt)
512 ipa_ref *ref = NULL, *ref2 = NULL;
513 ipa_ref_list *list, *list2;
514 ipa_ref_t *old_references;
516 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
517 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
519 list = &ref_list;
520 old_references = vec_safe_address (list->references);
521 vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
522 ref = &list->references->last ();
524 list2 = &referred_node->ref_list;
526 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
527 if(use_type == IPA_REF_ALIAS)
529 list2->referring.safe_insert (0, ref);
530 ref->referred_index = 0;
532 for (unsigned int i = 1; i < list2->referring.length (); i++)
533 list2->referring[i]->referred_index = i;
535 else
537 list2->referring.safe_push (ref);
538 ref->referred_index = list2->referring.length () - 1;
541 ref->referring = this;
542 ref->referred = referred_node;
543 ref->stmt = stmt;
544 ref->lto_stmt_uid = 0;
545 ref->use = use_type;
546 ref->speculative = 0;
548 /* If vector was moved in memory, update pointers. */
549 if (old_references != list->references->address ())
551 int i;
552 for (i = 0; iterate_reference(i, ref2); i++)
553 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
555 return ref;
558 /* If VAL is a reference to a function or a variable, add a reference from
559 this symtab_node to the corresponding symbol table node. USE_TYPE specify
560 type of the use and STMT the statement (if it exists). Return the new
561 reference or NULL if none was created. */
563 ipa_ref *
564 symtab_node::maybe_create_reference (tree val, enum ipa_ref_use use_type,
565 gimple stmt)
567 STRIP_NOPS (val);
568 if (TREE_CODE (val) != ADDR_EXPR)
569 return NULL;
570 val = get_base_var (val);
571 if (val && (TREE_CODE (val) == FUNCTION_DECL
572 || TREE_CODE (val) == VAR_DECL))
574 symtab_node *referred = symtab_node::get (val);
575 gcc_checking_assert (referred);
576 return create_reference (referred, use_type, stmt);
578 return NULL;
581 /* Clone all references from symtab NODE to this symtab_node. */
583 void
584 symtab_node::clone_references (symtab_node *node)
586 ipa_ref *ref = NULL, *ref2 = NULL;
587 int i;
588 for (i = 0; node->iterate_reference (i, ref); i++)
590 bool speculative = ref->speculative;
591 unsigned int stmt_uid = ref->lto_stmt_uid;
593 ref2 = create_reference (ref->referred, ref->use, ref->stmt);
594 ref2->speculative = speculative;
595 ref2->lto_stmt_uid = stmt_uid;
599 /* Clone all referring from symtab NODE to this symtab_node. */
601 void
602 symtab_node::clone_referring (symtab_node *node)
604 ipa_ref *ref = NULL, *ref2 = NULL;
605 int i;
606 for (i = 0; node->iterate_referring(i, ref); i++)
608 bool speculative = ref->speculative;
609 unsigned int stmt_uid = ref->lto_stmt_uid;
611 ref2 = ref->referring->create_reference (this, ref->use, ref->stmt);
612 ref2->speculative = speculative;
613 ref2->lto_stmt_uid = stmt_uid;
617 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
619 ipa_ref *
620 symtab_node::clone_reference (ipa_ref *ref, gimple stmt)
622 bool speculative = ref->speculative;
623 unsigned int stmt_uid = ref->lto_stmt_uid;
624 ipa_ref *ref2;
626 ref2 = create_reference (ref->referred, ref->use, stmt);
627 ref2->speculative = speculative;
628 ref2->lto_stmt_uid = stmt_uid;
629 return ref2;
632 /* Find the structure describing a reference to REFERRED_NODE
633 and associated with statement STMT. */
635 ipa_ref *
636 symtab_node::find_reference (symtab_node *referred_node,
637 gimple stmt, unsigned int lto_stmt_uid)
639 ipa_ref *r = NULL;
640 int i;
642 for (i = 0; iterate_reference (i, r); i++)
643 if (r->referred == referred_node
644 && !r->speculative
645 && ((stmt && r->stmt == stmt)
646 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
647 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
648 return r;
649 return NULL;
652 /* Remove all references that are associated with statement STMT. */
654 void
655 symtab_node::remove_stmt_references (gimple stmt)
657 ipa_ref *r = NULL;
658 int i = 0;
660 while (iterate_reference (i, r))
661 if (r->stmt == stmt)
662 r->remove_reference ();
663 else
664 i++;
667 /* Remove all stmt references in non-speculative references.
668 Those are not maintained during inlining & clonning.
669 The exception are speculative references that are updated along
670 with callgraph edges associated with them. */
672 void
673 symtab_node::clear_stmts_in_references (void)
675 ipa_ref *r = NULL;
676 int i;
678 for (i = 0; iterate_reference (i, r); i++)
679 if (!r->speculative)
681 r->stmt = NULL;
682 r->lto_stmt_uid = 0;
686 /* Remove all references in ref list. */
688 void
689 symtab_node::remove_all_references (void)
691 while (vec_safe_length (ref_list.references))
692 ref_list.references->last ().remove_reference ();
693 vec_free (ref_list.references);
696 /* Remove all referring items in ref list. */
698 void
699 symtab_node::remove_all_referring (void)
701 while (ref_list.referring.length ())
702 ref_list.referring.last ()->remove_reference ();
703 ref_list.referring.release ();
706 /* Dump references in ref list to FILE. */
708 void
709 symtab_node::dump_references (FILE *file)
711 ipa_ref *ref = NULL;
712 int i;
713 for (i = 0; iterate_reference (i, ref); i++)
715 fprintf (file, "%s/%i (%s)",
716 ref->referred->asm_name (),
717 ref->referred->order,
718 ipa_ref_use_name [ref->use]);
719 if (ref->speculative)
720 fprintf (file, " (speculative)");
722 fprintf (file, "\n");
725 /* Dump referring in list to FILE. */
727 void
728 symtab_node::dump_referring (FILE *file)
730 ipa_ref *ref = NULL;
731 int i;
732 for (i = 0; iterate_referring(i, ref); i++)
734 fprintf (file, "%s/%i (%s)",
735 ref->referring->asm_name (),
736 ref->referring->order,
737 ipa_ref_use_name [ref->use]);
738 if (ref->speculative)
739 fprintf (file, " (speculative)");
741 fprintf (file, "\n");
744 /* Return true if list contains an alias. */
745 bool
746 symtab_node::has_aliases_p (void)
748 ipa_ref *ref = NULL;
749 int i;
751 for (i = 0; iterate_referring (i, ref); i++)
752 if (ref->use == IPA_REF_ALIAS)
753 return true;
754 return false;
757 /* Iterates I-th reference in the list, REF is also set. */
759 ipa_ref *
760 symtab_node::iterate_reference (unsigned i, ipa_ref *&ref)
762 vec_safe_iterate (ref_list.references, i, &ref);
764 return ref;
767 /* Iterates I-th referring item in the list, REF is also set. */
769 ipa_ref *
770 symtab_node::iterate_referring (unsigned i, ipa_ref *&ref)
772 ref_list.referring.iterate (i, &ref);
774 return ref;
777 /* Iterates I-th referring alias item in the list, REF is also set. */
779 ipa_ref *
780 symtab_node::iterate_direct_aliases (unsigned i, ipa_ref *&ref)
782 ref_list.referring.iterate (i, &ref);
784 if (ref && ref->use != IPA_REF_ALIAS)
785 return NULL;
787 return ref;
790 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
792 /* Dump base fields of symtab nodes to F. Not to be used directly. */
794 void
795 symtab_node::dump_base (FILE *f)
797 static const char * const visibility_types[] = {
798 "default", "protected", "hidden", "internal"
801 fprintf (f, "%s/%i (%s)", asm_name (), order, name ());
802 dump_addr (f, " @", (void *)this);
803 fprintf (f, "\n Type: %s", symtab_type_names[type]);
805 if (definition)
806 fprintf (f, " definition");
807 if (analyzed)
808 fprintf (f, " analyzed");
809 if (alias)
810 fprintf (f, " alias");
811 if (weakref)
812 fprintf (f, " weakref");
813 if (cpp_implicit_alias)
814 fprintf (f, " cpp_implicit_alias");
815 if (alias_target)
816 fprintf (f, " target:%s",
817 DECL_P (alias_target)
818 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
819 (alias_target))
820 : IDENTIFIER_POINTER (alias_target));
821 if (body_removed)
822 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
823 fprintf (f, "\n Visibility:");
824 if (in_other_partition)
825 fprintf (f, " in_other_partition");
826 if (used_from_other_partition)
827 fprintf (f, " used_from_other_partition");
828 if (force_output)
829 fprintf (f, " force_output");
830 if (forced_by_abi)
831 fprintf (f, " forced_by_abi");
832 if (externally_visible)
833 fprintf (f, " externally_visible");
834 if (resolution != LDPR_UNKNOWN)
835 fprintf (f, " %s",
836 ld_plugin_symbol_resolution_names[(int)resolution]);
837 if (TREE_ASM_WRITTEN (decl))
838 fprintf (f, " asm_written");
839 if (DECL_EXTERNAL (decl))
840 fprintf (f, " external");
841 if (TREE_PUBLIC (decl))
842 fprintf (f, " public");
843 if (DECL_COMMON (decl))
844 fprintf (f, " common");
845 if (DECL_WEAK (decl))
846 fprintf (f, " weak");
847 if (DECL_DLLIMPORT_P (decl))
848 fprintf (f, " dll_import");
849 if (DECL_COMDAT (decl))
850 fprintf (f, " comdat");
851 if (get_comdat_group ())
852 fprintf (f, " comdat_group:%s",
853 IDENTIFIER_POINTER (get_comdat_group_id ()));
854 if (DECL_ONE_ONLY (decl))
855 fprintf (f, " one_only");
856 if (get_section ())
857 fprintf (f, " section:%s",
858 get_section ());
859 if (implicit_section)
860 fprintf (f," (implicit_section)");
861 if (DECL_VISIBILITY_SPECIFIED (decl))
862 fprintf (f, " visibility_specified");
863 if (DECL_VISIBILITY (decl))
864 fprintf (f, " visibility:%s",
865 visibility_types [DECL_VISIBILITY (decl)]);
866 if (DECL_VIRTUAL_P (decl))
867 fprintf (f, " virtual");
868 if (DECL_ARTIFICIAL (decl))
869 fprintf (f, " artificial");
870 if (TREE_CODE (decl) == FUNCTION_DECL)
872 if (DECL_STATIC_CONSTRUCTOR (decl))
873 fprintf (f, " constructor");
874 if (DECL_STATIC_DESTRUCTOR (decl))
875 fprintf (f, " destructor");
877 fprintf (f, "\n");
879 if (same_comdat_group)
880 fprintf (f, " Same comdat group as: %s/%i\n",
881 same_comdat_group->asm_name (),
882 same_comdat_group->order);
883 if (next_sharing_asm_name)
884 fprintf (f, " next sharing asm name: %i\n",
885 next_sharing_asm_name->order);
886 if (previous_sharing_asm_name)
887 fprintf (f, " previous sharing asm name: %i\n",
888 previous_sharing_asm_name->order);
890 if (address_taken)
891 fprintf (f, " Address is taken.\n");
892 if (aux)
894 fprintf (f, " Aux:");
895 dump_addr (f, " @", (void *)aux);
898 fprintf (f, " References: ");
899 dump_references (f);
900 fprintf (f, " Referring: ");
901 dump_referring (f);
902 if (lto_file_data)
903 fprintf (f, " Read from file: %s\n",
904 lto_file_data->file_name);
907 /* Dump symtab node to F. */
909 void
910 symtab_node::dump (FILE *f)
912 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
913 cnode->dump (f);
914 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
915 vnode->dump (f);
918 /* Dump symbol table to F. */
920 void
921 symtab_node::dump_table (FILE *f)
923 symtab_node *node;
924 fprintf (f, "Symbol table:\n\n");
925 FOR_EACH_SYMBOL (node)
926 node->dump (f);
930 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
931 Return NULL if there's no such node. */
933 symtab_node *
934 symtab_node::get_for_asmname (const_tree asmname)
936 symtab_node *node;
937 void **slot;
939 symtab->symtab_initialize_asm_name_hash ();
940 slot = htab_find_slot_with_hash (symtab->assembler_name_hash, asmname,
941 symtab->decl_assembler_name_hash (asmname),
942 NO_INSERT);
944 if (slot)
946 node = (symtab_node *) *slot;
947 return node;
949 return NULL;
952 /* Dump symtab node NODE to stderr. */
954 DEBUG_FUNCTION void
955 symtab_node::debug (void)
957 dump (stderr);
960 /* Verify common part of symtab nodes. */
962 DEBUG_FUNCTION bool
963 symtab_node::verify_base (void)
965 bool error_found = false;
966 symtab_node *hashed_node;
968 if (is_a <cgraph_node *> (this))
970 if (TREE_CODE (decl) != FUNCTION_DECL)
972 error ("function symbol is not function");
973 error_found = true;
976 else if (is_a <varpool_node *> (this))
978 if (TREE_CODE (decl) != VAR_DECL)
980 error ("variable symbol is not variable");
981 error_found = true;
984 else
986 error ("node has unknown type");
987 error_found = true;
990 if (symtab->state != LTO_STREAMING)
992 hashed_node = symtab_node::get (decl);
993 if (!hashed_node)
995 error ("node not found node->decl->decl_with_vis.symtab_node");
996 error_found = true;
998 if (hashed_node != this
999 && (!is_a <cgraph_node *> (this)
1000 || !dyn_cast <cgraph_node *> (this)->clone_of
1001 || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl))
1003 error ("node differs from node->decl->decl_with_vis.symtab_node");
1004 error_found = true;
1007 if (symtab->assembler_name_hash)
1009 hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
1010 if (hashed_node && hashed_node->previous_sharing_asm_name)
1012 error ("assembler name hash list corrupted");
1013 error_found = true;
1015 while (hashed_node)
1017 if (hashed_node == this)
1018 break;
1019 hashed_node = hashed_node->next_sharing_asm_name;
1021 if (!hashed_node
1022 && !(is_a <varpool_node *> (this)
1023 || DECL_HARD_REGISTER (decl)))
1025 error ("node not found in symtab assembler name hash");
1026 error_found = true;
1029 if (previous_sharing_asm_name
1030 && previous_sharing_asm_name->next_sharing_asm_name != this)
1032 error ("double linked list of assembler names corrupted");
1033 error_found = true;
1035 if (analyzed && !definition)
1037 error ("node is analyzed byt it is not a definition");
1038 error_found = true;
1040 if (cpp_implicit_alias && !alias)
1042 error ("node is alias but not implicit alias");
1043 error_found = true;
1045 if (alias && !definition && !weakref)
1047 error ("node is alias but not definition");
1048 error_found = true;
1050 if (weakref && !alias)
1052 error ("node is weakref but not an alias");
1053 error_found = true;
1055 if (same_comdat_group)
1057 symtab_node *n = same_comdat_group;
1059 if (!n->get_comdat_group ())
1061 error ("node is in same_comdat_group list but has no comdat_group");
1062 error_found = true;
1064 if (n->get_comdat_group () != get_comdat_group ())
1066 error ("same_comdat_group list across different groups");
1067 error_found = true;
1069 if (!n->definition)
1071 error ("Node has same_comdat_group but it is not a definition");
1072 error_found = true;
1074 if (n->type != type)
1076 error ("mixing different types of symbol in same comdat groups is not supported");
1077 error_found = true;
1079 if (n == this)
1081 error ("node is alone in a comdat group");
1082 error_found = true;
1086 if (!n->same_comdat_group)
1088 error ("same_comdat_group is not a circular list");
1089 error_found = true;
1090 break;
1092 n = n->same_comdat_group;
1094 while (n != this);
1095 if (comdat_local_p ())
1097 ipa_ref *ref = NULL;
1099 for (int i = 0; iterate_referring (i, ref); ++i)
1101 if (!in_same_comdat_group_p (ref->referring))
1103 error ("comdat-local symbol referred to by %s outside its "
1104 "comdat",
1105 identifier_to_locale (ref->referring->name()));
1106 error_found = true;
1111 if (implicit_section && !get_section ())
1113 error ("implicit_section flag is set but section isn't");
1114 error_found = true;
1116 if (get_section () && get_comdat_group ()
1117 && !implicit_section)
1119 error ("Both section and comdat group is set");
1120 error_found = true;
1122 /* TODO: Add string table for sections, so we do not keep holding duplicated
1123 strings. */
1124 if (alias && definition
1125 && get_section () != get_alias_target ()->get_section ()
1126 && (!get_section()
1127 || !get_alias_target ()->get_section ()
1128 || strcmp (get_section(),
1129 get_alias_target ()->get_section ())))
1131 error ("Alias and target's section differs");
1132 get_alias_target ()->dump (stderr);
1133 error_found = true;
1135 if (alias && definition
1136 && get_comdat_group () != get_alias_target ()->get_comdat_group ())
1138 error ("Alias and target's comdat groups differs");
1139 get_alias_target ()->dump (stderr);
1140 error_found = true;
1143 return error_found;
1146 /* Verify consistency of NODE. */
1148 DEBUG_FUNCTION void
1149 symtab_node::verify (void)
1151 if (seen_error ())
1152 return;
1154 timevar_push (TV_CGRAPH_VERIFY);
1155 if (cgraph_node *node = dyn_cast <cgraph_node *> (this))
1156 node->verify_node ();
1157 else
1158 if (verify_base ())
1160 debug ();
1161 internal_error ("symtab_node::verify failed");
1163 timevar_pop (TV_CGRAPH_VERIFY);
1166 /* Verify symbol table for internal consistency. */
1168 DEBUG_FUNCTION void
1169 symtab_node::verify_symtab_nodes (void)
1171 symtab_node *node;
1172 hash_map<tree, symtab_node *> comdat_head_map (251);
1174 FOR_EACH_SYMBOL (node)
1176 node->verify ();
1177 if (node->get_comdat_group ())
1179 symtab_node **entry, *s;
1180 bool existed;
1182 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1183 &existed);
1184 if (!existed)
1185 *entry = node;
1186 else
1187 for (s = (*entry)->same_comdat_group; s != NULL && s != node; s = s->same_comdat_group)
1188 if (!s || s == *entry)
1190 error ("Two symbols with same comdat_group are not linked by the same_comdat_group list.");
1191 (*entry)->debug ();
1192 node->debug ();
1193 internal_error ("symtab_node::verify failed");
1199 /* Return true when NODE is known to be used from other (non-LTO)
1200 object file. Known only when doing LTO via linker plugin. */
1202 bool
1203 symtab_node::used_from_object_file_p_worker (symtab_node *node)
1205 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
1206 return false;
1207 if (resolution_used_from_other_file_p (node->resolution))
1208 return true;
1209 return false;
1213 /* Return true when symtab_node is known to be used from other (non-LTO)
1214 object file. Known only when doing LTO via linker plugin. */
1216 bool
1217 symtab_node::used_from_object_file_p (void)
1219 return symtab_node::used_from_object_file_p_worker (this);
1222 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1223 but other code such as notice_global_symbol generates rtl. */
1225 void
1226 symtab_node::make_decl_local (void)
1228 rtx rtl, symbol;
1230 /* Avoid clearing comdat_groups on comdat-local decls. */
1231 if (TREE_PUBLIC (decl) == 0)
1232 return;
1234 if (TREE_CODE (decl) == VAR_DECL)
1235 DECL_COMMON (decl) = 0;
1236 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1238 DECL_COMDAT (decl) = 0;
1239 DECL_WEAK (decl) = 0;
1240 DECL_EXTERNAL (decl) = 0;
1241 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1242 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1243 TREE_PUBLIC (decl) = 0;
1244 if (!DECL_RTL_SET_P (decl))
1245 return;
1247 /* Update rtl flags. */
1248 make_decl_rtl (decl);
1250 rtl = DECL_RTL (decl);
1251 if (!MEM_P (rtl))
1252 return;
1254 symbol = XEXP (rtl, 0);
1255 if (GET_CODE (symbol) != SYMBOL_REF)
1256 return;
1258 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1261 /* Walk the alias chain to return the symbol NODE is alias of.
1262 If NODE is not an alias, return NODE.
1263 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
1265 symtab_node *
1266 symtab_node::ultimate_alias_target (enum availability *availability)
1268 bool weakref_p = false;
1270 if (!alias)
1272 if (availability)
1273 *availability = get_availability ();
1274 return this;
1277 /* To determine visibility of the target, we follow ELF semantic of aliases.
1278 Here alias is an alternative assembler name of a given definition. Its
1279 availability prevails the availability of its target (i.e. static alias of
1280 weak definition is available.
1282 Weakref is a different animal (and not part of ELF per se). It is just
1283 alternative name of a given symbol used within one complation unit
1284 and is translated prior hitting the object file. It inherits the
1285 visibility of its target (i.e. weakref of non-overwritable definition
1286 is non-overwritable, while weakref of weak definition is weak).
1288 If we ever get into supporting targets with different semantics, a target
1289 hook will be needed here. */
1291 if (availability)
1293 weakref_p = weakref;
1294 if (!weakref_p)
1295 *availability = get_availability ();
1296 else
1297 *availability = AVAIL_LOCAL;
1300 symtab_node *node = this;
1301 while (node)
1303 if (node->alias && node->analyzed)
1304 node = node->get_alias_target ();
1305 else
1307 if (!availability)
1309 else if (node->analyzed)
1311 if (weakref_p)
1313 enum availability a = node->get_availability ();
1314 if (a < *availability)
1315 *availability = a;
1318 else
1319 *availability = AVAIL_NOT_AVAILABLE;
1320 return node;
1322 if (node && availability && weakref_p)
1324 enum availability a = node->get_availability ();
1325 if (a < *availability)
1326 *availability = a;
1327 weakref_p = node->weakref;
1330 if (availability)
1331 *availability = AVAIL_NOT_AVAILABLE;
1332 return NULL;
1335 /* C++ FE sometimes change linkage flags after producing same body aliases.
1337 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1338 are obviously equivalent. The way it is doing so is however somewhat
1339 kludgy and interferes with the visibility code. As a result we need to
1340 copy the visibility from the target to get things right. */
1342 void
1343 symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target)
1345 if (is_a <cgraph_node *> (this))
1347 DECL_DECLARED_INLINE_P (decl)
1348 = DECL_DECLARED_INLINE_P (target->decl);
1349 DECL_DISREGARD_INLINE_LIMITS (decl)
1350 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1352 /* FIXME: It is not really clear why those flags should not be copied for
1353 functions, too. */
1354 else
1356 DECL_WEAK (decl) = DECL_WEAK (target->decl);
1357 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1358 DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl);
1360 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (target->decl);
1361 if (TREE_PUBLIC (decl))
1363 tree group;
1365 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1366 DECL_COMDAT (decl) = DECL_COMDAT (target->decl);
1367 group = target->get_comdat_group ();
1368 set_comdat_group (group);
1369 if (group && !same_comdat_group)
1370 add_to_same_comdat_group (target);
1372 externally_visible = target->externally_visible;
1375 /* Set section, do not recurse into aliases.
1376 When one wants to change section of symbol and its aliases,
1377 use set_section. */
1379 void
1380 symtab_node::set_section_for_node (const char *section)
1382 const char *current = get_section ();
1383 void **slot;
1385 if (current == section
1386 || (current && section
1387 && !strcmp (current, section)))
1388 return;
1390 if (current)
1392 x_section->ref_count--;
1393 if (!x_section->ref_count)
1395 slot = htab_find_slot_with_hash (symtab->section_hash, x_section->name,
1396 htab_hash_string (x_section->name),
1397 INSERT);
1398 ggc_free (x_section);
1399 htab_clear_slot (symtab->section_hash, slot);
1401 x_section = NULL;
1403 if (!section)
1405 implicit_section = false;
1406 return;
1408 if (!symtab->section_hash)
1409 symtab->section_hash = htab_create_ggc (10, hash_section_hash_entry,
1410 eq_sections, NULL);
1411 slot = htab_find_slot_with_hash (symtab->section_hash, section,
1412 htab_hash_string (section),
1413 INSERT);
1414 if (*slot)
1415 x_section = (section_hash_entry *)*slot;
1416 else
1418 int len = strlen (section);
1419 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1420 x_section->name = ggc_vec_alloc<char> (len + 1);
1421 memcpy (x_section->name, section, len + 1);
1423 x_section->ref_count++;
1426 /* Worker for set_section. */
1428 bool
1429 symtab_node::set_section (symtab_node *n, void *s)
1431 n->set_section_for_node ((char *)s);
1432 return false;
1435 /* Set section of symbol and its aliases. */
1437 void
1438 symtab_node::set_section (const char *section)
1440 gcc_assert (!this->alias);
1441 call_for_symbol_and_aliases
1442 (symtab_node::set_section, const_cast<char *>(section), true);
1445 /* Return the initialization priority. */
1447 priority_type
1448 symtab_node::get_init_priority ()
1450 if (!this->in_init_priority_hash)
1451 return DEFAULT_INIT_PRIORITY;
1453 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1454 return h ? h->init : DEFAULT_INIT_PRIORITY;
1457 /* Return availability of NODE. */
1458 enum availability symtab_node::get_availability (void)
1460 if (is_a <cgraph_node *> (this))
1461 return dyn_cast <cgraph_node *> (this)->get_availability ();
1462 else
1463 return dyn_cast <varpool_node *> (this)->get_availability ();;
1467 /* Return the finalization priority. */
1469 priority_type
1470 cgraph_node::get_fini_priority ()
1472 if (!this->in_init_priority_hash)
1473 return DEFAULT_INIT_PRIORITY;
1474 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1475 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1478 /* Return the initialization and finalization priority information for
1479 DECL. If there is no previous priority information, a freshly
1480 allocated structure is returned. */
1482 symbol_priority_map *
1483 symtab_node::priority_info (void)
1485 if (!symtab->init_priority_hash)
1486 symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13);
1488 bool existed;
1489 symbol_priority_map *h
1490 = &symtab->init_priority_hash->get_or_insert (this, &existed);
1491 if (!existed)
1493 h->init = DEFAULT_INIT_PRIORITY;
1494 h->fini = DEFAULT_INIT_PRIORITY;
1495 in_init_priority_hash = true;
1498 return h;
1501 /* Set initialization priority to PRIORITY. */
1503 void
1504 symtab_node::set_init_priority (priority_type priority)
1506 symbol_priority_map *h;
1508 if (is_a <cgraph_node *> (this))
1509 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1511 if (priority == DEFAULT_INIT_PRIORITY)
1513 gcc_assert (get_init_priority() == priority);
1514 return;
1516 h = priority_info ();
1517 h->init = priority;
1520 /* Set fialization priority to PRIORITY. */
1522 void
1523 cgraph_node::set_fini_priority (priority_type priority)
1525 symbol_priority_map *h;
1527 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1529 if (priority == DEFAULT_INIT_PRIORITY)
1531 gcc_assert (get_fini_priority() == priority);
1532 return;
1534 h = priority_info ();
1535 h->fini = priority;
1538 /* Worker for symtab_resolve_alias. */
1540 bool
1541 symtab_node::set_implicit_section (symtab_node *n,
1542 void *data ATTRIBUTE_UNUSED)
1544 n->implicit_section = true;
1545 return false;
1548 /* Add reference recording that symtab node is alias of TARGET.
1549 The function can fail in the case of aliasing cycles; in this case
1550 it returns false. */
1552 bool
1553 symtab_node::resolve_alias (symtab_node *target)
1555 symtab_node *n;
1557 gcc_assert (!analyzed && !vec_safe_length (ref_list.references));
1559 /* Never let cycles to creep into the symbol table alias references;
1560 those will make alias walkers to be infinite. */
1561 for (n = target; n && n->alias;
1562 n = n->analyzed ? n->get_alias_target () : NULL)
1563 if (n == this)
1565 if (is_a <cgraph_node *> (this))
1566 error ("function %q+D part of alias cycle", decl);
1567 else if (is_a <varpool_node *> (this))
1568 error ("variable %q+D part of alias cycle", decl);
1569 else
1570 gcc_unreachable ();
1571 alias = false;
1572 return false;
1575 /* "analyze" the node - i.e. mark the reference. */
1576 definition = true;
1577 alias = true;
1578 analyzed = true;
1579 create_reference (target, IPA_REF_ALIAS, NULL);
1581 /* Add alias into the comdat group of its target unless it is already there. */
1582 if (same_comdat_group)
1583 remove_from_same_comdat_group ();
1584 set_comdat_group (NULL);
1585 if (target->get_comdat_group ())
1586 add_to_same_comdat_group (target);
1588 if ((get_section () != target->get_section ()
1589 || target->get_comdat_group ()) && get_section () && !implicit_section)
1591 error ("section of alias %q+D must match section of its target", decl);
1593 call_for_symbol_and_aliases (symtab_node::set_section,
1594 const_cast<char *>(target->get_section ()), true);
1595 if (target->implicit_section)
1596 call_for_symbol_and_aliases (set_implicit_section, NULL, true);
1598 /* Alias targets become redundant after alias is resolved into an reference.
1599 We do not want to keep it around or we would have to mind updating them
1600 when renaming symbols. */
1601 alias_target = NULL;
1603 if (cpp_implicit_alias && symtab->state >= CONSTRUCTION)
1604 fixup_same_cpp_alias_visibility (target);
1606 /* If alias has address taken, so does the target. */
1607 if (address_taken)
1608 target->ultimate_alias_target ()->address_taken = true;
1609 return true;
1612 /* Call calback on symtab node and aliases associated to this node.
1613 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1614 skipped. */
1616 bool
1617 symtab_node::call_for_symbol_and_aliases (bool (*callback) (symtab_node *,
1618 void *),
1619 void *data, bool include_overwritable)
1621 int i;
1622 ipa_ref *ref;
1624 if (callback (this, data))
1625 return true;
1626 for (i = 0; iterate_referring (i, ref); i++)
1627 if (ref->use == IPA_REF_ALIAS)
1629 symtab_node *alias = ref->referring;
1630 if (include_overwritable
1631 || alias->get_availability () > AVAIL_INTERPOSABLE)
1632 if (alias->call_for_symbol_and_aliases (callback, data,
1633 include_overwritable))
1634 return true;
1636 return false;
1639 /* Worker searching noninterposable alias. */
1641 bool
1642 symtab_node::noninterposable_alias (symtab_node *node, void *data)
1644 if (decl_binds_to_current_def_p (node->decl))
1646 symtab_node *fn = node->ultimate_alias_target ();
1648 /* Ensure that the alias is well formed this may not be the case
1649 of user defined aliases and currently it is not always the case
1650 of C++ same body aliases (that is a bug). */
1651 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
1652 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
1653 || (TREE_CODE (node->decl) == FUNCTION_DECL
1654 && flags_from_decl_or_type (node->decl)
1655 != flags_from_decl_or_type (fn->decl))
1656 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
1657 return false;
1659 *(symtab_node **)data = node;
1660 return true;
1662 return false;
1665 /* If node can not be overwriten by static or dynamic linker to point to
1666 different definition, return NODE. Otherwise look for alias with such
1667 property and if none exists, introduce new one. */
1669 symtab_node *
1670 symtab_node::noninterposable_alias (void)
1672 tree new_decl;
1673 symtab_node *new_node = NULL;
1675 /* First try to look up existing alias or base object
1676 (if that is already non-overwritable). */
1677 symtab_node *node = ultimate_alias_target ();
1678 gcc_assert (!node->alias && !node->weakref);
1679 node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias,
1680 (void *)&new_node, true);
1681 if (new_node)
1682 return new_node;
1683 #ifndef ASM_OUTPUT_DEF
1684 /* If aliases aren't supported by the assembler, fail. */
1685 return NULL;
1686 #endif
1688 /* Otherwise create a new one. */
1689 new_decl = copy_node (node->decl);
1690 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1691 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1692 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1693 DECL_INITIAL (new_decl) = NULL;
1694 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1695 SET_DECL_RTL (new_decl, NULL);
1697 /* Update the properties. */
1698 DECL_EXTERNAL (new_decl) = 0;
1699 TREE_PUBLIC (new_decl) = 0;
1700 DECL_COMDAT (new_decl) = 0;
1701 DECL_WEAK (new_decl) = 0;
1703 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1704 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1705 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1707 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1708 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1709 new_node = cgraph_node::create_alias (new_decl, node->decl);
1711 else
1713 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1714 DECL_INITIAL (new_decl) = error_mark_node;
1715 new_node = varpool_node::create_alias (new_decl, node->decl);
1717 new_node->resolve_alias (node);
1718 gcc_assert (decl_binds_to_current_def_p (new_decl)
1719 && targetm.binds_local_p (new_decl));
1720 return new_node;
1723 /* Return true if symtab node and TARGET represents
1724 semantically equivalent symbols. */
1726 bool
1727 symtab_node::semantically_equivalent_p (symtab_node *target)
1729 enum availability avail;
1730 symtab_node *ba;
1731 symtab_node *bb;
1733 /* Equivalent functions are equivalent. */
1734 if (decl == target->decl)
1735 return true;
1737 /* If symbol is not overwritable by different implementation,
1738 walk to the base object it defines. */
1739 ba = ultimate_alias_target (&avail);
1740 if (avail >= AVAIL_AVAILABLE)
1742 if (target == ba)
1743 return true;
1745 else
1746 ba = this;
1747 bb = target->ultimate_alias_target (&avail);
1748 if (avail >= AVAIL_AVAILABLE)
1750 if (this == bb)
1751 return true;
1753 else
1754 bb = target;
1755 return bb == ba;
1758 /* Classify symbol symtab node for partitioning. */
1760 enum symbol_partitioning_class
1761 symtab_node::get_partitioning_class (void)
1763 /* Inline clones are always duplicated.
1764 This include external delcarations. */
1765 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
1767 if (DECL_ABSTRACT (decl))
1768 return SYMBOL_EXTERNAL;
1770 if (cnode && cnode->global.inlined_to)
1771 return SYMBOL_DUPLICATE;
1773 /* Weakref aliases are always duplicated. */
1774 if (weakref)
1775 return SYMBOL_DUPLICATE;
1777 /* External declarations are external. */
1778 if (DECL_EXTERNAL (decl))
1779 return SYMBOL_EXTERNAL;
1781 if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
1783 /* Constant pool references use local symbol names that can not
1784 be promoted global. We should never put into a constant pool
1785 objects that can not be duplicated across partitions. */
1786 if (DECL_IN_CONSTANT_POOL (decl))
1787 return SYMBOL_DUPLICATE;
1788 gcc_checking_assert (vnode->definition);
1790 /* Functions that are cloned may stay in callgraph even if they are unused.
1791 Handle them as external; compute_ltrans_boundary take care to make
1792 proper things to happen (i.e. to make them appear in the boundary but
1793 with body streamed, so clone can me materialized). */
1794 else if (!dyn_cast <cgraph_node *> (this)->definition)
1795 return SYMBOL_EXTERNAL;
1797 /* Linker discardable symbols are duplicated to every use unless they are
1798 keyed. */
1799 if (DECL_ONE_ONLY (decl)
1800 && !force_output
1801 && !forced_by_abi
1802 && !used_from_object_file_p ())
1803 return SYMBOL_DUPLICATE;
1805 return SYMBOL_PARTITION;
1808 /* Return true when symbol is known to be non-zero. */
1810 bool
1811 symtab_node::nonzero_address ()
1813 /* Weakrefs may be NULL when their target is not defined. */
1814 if (this->alias && this->weakref)
1816 if (this->analyzed)
1818 symtab_node *target = ultimate_alias_target ();
1820 if (target->alias && target->weakref)
1821 return false;
1822 /* We can not recurse to target::nonzero. It is possible that the
1823 target is used only via the alias.
1824 We may walk references and look for strong use, but we do not know
1825 if this strong use will survive to final binary, so be
1826 conservative here.
1827 ??? Maybe we could do the lookup during late optimization that
1828 could be useful to eliminate the NULL pointer checks in LTO
1829 programs. */
1830 if (target->definition && !DECL_EXTERNAL (target->decl))
1831 return true;
1832 if (target->resolution != LDPR_UNKNOWN
1833 && target->resolution != LDPR_UNDEF
1834 && flag_delete_null_pointer_checks)
1835 return true;
1836 return false;
1838 else
1839 return false;
1842 /* With !flag_delete_null_pointer_checks we assume that symbols may
1843 bind to NULL. This is on by default on embedded targets only.
1845 Otherwise all non-WEAK symbols must be defined and thus non-NULL or
1846 linking fails. Important case of WEAK we want to do well are comdats.
1847 Those are handled by later check for definition.
1849 When parsing, beware the cases when WEAK attribute is added later. */
1850 if (!DECL_WEAK (this->decl)
1851 && flag_delete_null_pointer_checks
1852 && symtab->state > PARSING)
1853 return true;
1855 /* If target is defined and not extern, we know it will be output and thus
1856 it will bind to non-NULL.
1857 Play safe for flag_delete_null_pointer_checks where weak definition maye
1858 be re-defined by NULL. */
1859 if (this->definition && !DECL_EXTERNAL (this->decl)
1860 && (flag_delete_null_pointer_checks || !DECL_WEAK (this->decl)))
1861 return true;
1863 /* As the last resort, check the resolution info. */
1864 if (this->resolution != LDPR_UNKNOWN
1865 && this->resolution != LDPR_UNDEF
1866 && flag_delete_null_pointer_checks)
1867 return true;
1868 return false;