2014-11-18 Christophe Lyon <christophe.lyon@linaro.org>
[official-gcc.git] / gcc / symtab.c
blob29839e676f003cdf406d6059282bc035e112e4a5
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 "hashtab.h"
30 #include "hash-set.h"
31 #include "vec.h"
32 #include "machmode.h"
33 #include "hard-reg-set.h"
34 #include "input.h"
35 #include "function.h"
36 #include "emit-rtl.h"
37 #include "predict.h"
38 #include "basic-block.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "gimple-expr.h"
42 #include "is-a.h"
43 #include "gimple.h"
44 #include "tree-inline.h"
45 #include "langhooks.h"
46 #include "hash-map.h"
47 #include "plugin-api.h"
48 #include "ipa-ref.h"
49 #include "cgraph.h"
50 #include "diagnostic.h"
51 #include "timevar.h"
52 #include "lto-streamer.h"
53 #include "output.h"
54 #include "ipa-utils.h"
55 #include "calls.h"
57 static const char *ipa_ref_use_name[] = {"read","write","addr","alias","chkp"};
59 const char * const ld_plugin_symbol_resolution_names[]=
61 "",
62 "undef",
63 "prevailing_def",
64 "prevailing_def_ironly",
65 "preempted_reg",
66 "preempted_ir",
67 "resolved_ir",
68 "resolved_exec",
69 "resolved_dyn",
70 "prevailing_def_ironly_exp"
73 /* Hash asmnames ignoring the user specified marks. */
75 hashval_t
76 symbol_table::decl_assembler_name_hash (const_tree asmname)
78 if (IDENTIFIER_POINTER (asmname)[0] == '*')
80 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
81 size_t ulp_len = strlen (user_label_prefix);
83 if (ulp_len == 0)
85 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
86 decl_str += ulp_len;
88 return htab_hash_string (decl_str);
91 return htab_hash_string (IDENTIFIER_POINTER (asmname));
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. */
153 /* Insert NODE to assembler name hash. */
155 void
156 symbol_table::insert_to_assembler_name_hash (symtab_node *node,
157 bool with_clones)
159 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
160 return;
161 gcc_checking_assert (!node->previous_sharing_asm_name
162 && !node->next_sharing_asm_name);
163 if (assembler_name_hash)
165 symtab_node **aslot;
166 cgraph_node *cnode;
167 tree decl = node->decl;
169 tree name = DECL_ASSEMBLER_NAME (node->decl);
171 hashval_t hash = decl_assembler_name_hash (name);
172 aslot = assembler_name_hash->find_slot_with_hash (name, hash, INSERT);
173 gcc_assert (*aslot != node);
174 node->next_sharing_asm_name = (symtab_node *)*aslot;
175 if (*aslot != NULL)
176 (*aslot)->previous_sharing_asm_name = node;
177 *aslot = node;
179 /* Update also possible inline clones sharing a decl. */
180 cnode = dyn_cast <cgraph_node *> (node);
181 if (cnode && cnode->clones && with_clones)
182 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
183 if (cnode->decl == decl)
184 insert_to_assembler_name_hash (cnode, true);
189 /* Remove NODE from assembler name hash. */
191 void
192 symbol_table::unlink_from_assembler_name_hash (symtab_node *node,
193 bool with_clones)
195 if (assembler_name_hash)
197 cgraph_node *cnode;
198 tree decl = node->decl;
200 if (node->next_sharing_asm_name)
201 node->next_sharing_asm_name->previous_sharing_asm_name
202 = node->previous_sharing_asm_name;
203 if (node->previous_sharing_asm_name)
205 node->previous_sharing_asm_name->next_sharing_asm_name
206 = node->next_sharing_asm_name;
208 else
210 tree name = DECL_ASSEMBLER_NAME (node->decl);
211 symtab_node **slot;
212 hashval_t hash = decl_assembler_name_hash (name);
213 slot = assembler_name_hash->find_slot_with_hash (name, hash,
214 NO_INSERT);
215 gcc_assert (*slot == node);
216 if (!node->next_sharing_asm_name)
217 assembler_name_hash->clear_slot (slot);
218 else
219 *slot = node->next_sharing_asm_name;
221 node->next_sharing_asm_name = NULL;
222 node->previous_sharing_asm_name = NULL;
224 /* Update also possible inline clones sharing a decl. */
225 cnode = dyn_cast <cgraph_node *> (node);
226 if (cnode && cnode->clones && with_clones)
227 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
228 if (cnode->decl == decl)
229 unlink_from_assembler_name_hash (cnode, true);
233 /* Arrange node to be first in its entry of assembler_name_hash. */
235 void
236 symbol_table::symtab_prevail_in_asm_name_hash (symtab_node *node)
238 unlink_from_assembler_name_hash (node, false);
239 insert_to_assembler_name_hash (node, false);
242 /* Initalize asm name hash unless. */
244 void
245 symbol_table::symtab_initialize_asm_name_hash (void)
247 symtab_node *node;
248 if (!assembler_name_hash)
250 assembler_name_hash = hash_table<asmname_hasher>::create_ggc (10);
251 FOR_EACH_SYMBOL (node)
252 insert_to_assembler_name_hash (node, false);
256 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
258 void
259 symbol_table::change_decl_assembler_name (tree decl, tree name)
261 symtab_node *node = NULL;
263 /* We can have user ASM names on things, like global register variables, that
264 are not in the symbol table. */
265 if ((TREE_CODE (decl) == VAR_DECL
266 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
267 || TREE_CODE (decl) == FUNCTION_DECL)
268 node = symtab_node::get (decl);
269 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
271 SET_DECL_ASSEMBLER_NAME (decl, name);
272 if (node)
273 insert_to_assembler_name_hash (node, true);
275 else
277 if (name == DECL_ASSEMBLER_NAME (decl))
278 return;
280 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
281 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
282 : NULL);
283 if (node)
284 unlink_from_assembler_name_hash (node, true);
285 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
286 && DECL_RTL_SET_P (decl))
287 warning (0, "%D renamed after being referenced in assembly", decl);
289 SET_DECL_ASSEMBLER_NAME (decl, name);
290 if (alias)
292 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
293 TREE_CHAIN (name) = alias;
295 if (node)
296 insert_to_assembler_name_hash (node, true);
300 /* Return true when RESOLUTION indicate that linker will use
301 the symbol from non-LTO object files. */
303 bool
304 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
306 return (resolution == LDPR_PREVAILING_DEF
307 || resolution == LDPR_PREEMPTED_REG
308 || resolution == LDPR_RESOLVED_EXEC
309 || resolution == LDPR_RESOLVED_DYN);
312 /* Hash sections by their names. */
314 hashval_t
315 section_name_hasher::hash (section_hash_entry *n)
317 return htab_hash_string (n->name);
320 /* Return true if section P1 name equals to P2. */
322 bool
323 section_name_hasher::equal (section_hash_entry *n1, const char *name)
325 return n1->name == name || !strcmp (n1->name, name);
328 /* Add node into symbol table. This function is not used directly, but via
329 cgraph/varpool node creation routines. */
331 void
332 symtab_node::register_symbol (void)
334 symtab->register_symbol (this);
336 if (!decl->decl_with_vis.symtab_node)
337 decl->decl_with_vis.symtab_node = this;
339 ref_list.clear ();
341 /* Be sure to do this last; C++ FE might create new nodes via
342 DECL_ASSEMBLER_NAME langhook! */
343 symtab->insert_to_assembler_name_hash (this, false);
346 /* Remove NODE from same comdat group. */
348 void
349 symtab_node::remove_from_same_comdat_group (void)
351 if (same_comdat_group)
353 symtab_node *prev;
354 for (prev = same_comdat_group;
355 prev->same_comdat_group != this;
356 prev = prev->same_comdat_group)
358 if (same_comdat_group == prev)
359 prev->same_comdat_group = NULL;
360 else
361 prev->same_comdat_group = same_comdat_group;
362 same_comdat_group = NULL;
363 set_comdat_group (NULL);
367 /* Remove node from symbol table. This function is not used directly, but via
368 cgraph/varpool node removal routines. */
370 void
371 symtab_node::unregister (void)
373 remove_all_references ();
374 remove_all_referring ();
376 /* Remove reference to section. */
377 set_section_for_node (NULL);
379 remove_from_same_comdat_group ();
381 symtab->unregister (this);
383 /* During LTO symtab merging we temporarily corrupt decl to symtab node
384 hash. */
385 gcc_assert (decl->decl_with_vis.symtab_node || in_lto_p);
386 if (decl->decl_with_vis.symtab_node == this)
388 symtab_node *replacement_node = NULL;
389 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
390 replacement_node = cnode->find_replacement ();
391 decl->decl_with_vis.symtab_node = replacement_node;
393 if (!is_a <varpool_node *> (this) || !DECL_HARD_REGISTER (decl))
394 symtab->unlink_from_assembler_name_hash (this, false);
395 if (in_init_priority_hash)
396 symtab->init_priority_hash->remove (this);
400 /* Remove symbol from symbol table. */
402 void
403 symtab_node::remove (void)
405 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
406 cnode->remove ();
407 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
408 vnode->remove ();
411 /* Add NEW_ to the same comdat group that OLD is in. */
413 void
414 symtab_node::add_to_same_comdat_group (symtab_node *old_node)
416 gcc_assert (old_node->get_comdat_group ());
417 gcc_assert (!same_comdat_group);
418 gcc_assert (this != old_node);
420 set_comdat_group (old_node->get_comdat_group ());
421 same_comdat_group = old_node;
422 if (!old_node->same_comdat_group)
423 old_node->same_comdat_group = this;
424 else
426 symtab_node *n;
427 for (n = old_node->same_comdat_group;
428 n->same_comdat_group != old_node;
429 n = n->same_comdat_group)
431 n->same_comdat_group = this;
435 /* Dissolve the same_comdat_group list in which NODE resides. */
437 void
438 symtab_node::dissolve_same_comdat_group_list (void)
440 symtab_node *n = this;
441 symtab_node *next;
443 if (!same_comdat_group)
444 return;
447 next = n->same_comdat_group;
448 n->same_comdat_group = NULL;
449 /* Clear comdat_group for comdat locals, since
450 make_decl_local doesn't. */
451 if (!TREE_PUBLIC (n->decl))
452 n->set_comdat_group (NULL);
453 n = next;
455 while (n != this);
458 /* Return printable assembler name of NODE.
459 This function is used only for debugging. When assembler name
460 is unknown go with identifier name. */
462 const char *
463 symtab_node::asm_name () const
465 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
466 return lang_hooks.decl_printable_name (decl, 2);
467 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
470 /* Return printable identifier name. */
472 const char *
473 symtab_node::name () const
475 return lang_hooks.decl_printable_name (decl, 2);
478 /* Return ipa reference from this symtab_node to
479 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
480 of the use. */
482 ipa_ref *
483 symtab_node::create_reference (symtab_node *referred_node,
484 enum ipa_ref_use use_type)
486 return create_reference (referred_node, use_type, NULL);
490 /* Return ipa reference from this symtab_node to
491 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
492 of the use and STMT the statement (if it exists). */
494 ipa_ref *
495 symtab_node::create_reference (symtab_node *referred_node,
496 enum ipa_ref_use use_type, gimple stmt)
498 ipa_ref *ref = NULL, *ref2 = NULL;
499 ipa_ref_list *list, *list2;
500 ipa_ref_t *old_references;
502 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
503 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
505 list = &ref_list;
506 old_references = vec_safe_address (list->references);
507 vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
508 ref = &list->references->last ();
510 list2 = &referred_node->ref_list;
512 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
513 if(use_type == IPA_REF_ALIAS)
515 list2->referring.safe_insert (0, ref);
516 ref->referred_index = 0;
518 for (unsigned int i = 1; i < list2->referring.length (); i++)
519 list2->referring[i]->referred_index = i;
521 else
523 list2->referring.safe_push (ref);
524 ref->referred_index = list2->referring.length () - 1;
527 ref->referring = this;
528 ref->referred = referred_node;
529 ref->stmt = stmt;
530 ref->lto_stmt_uid = 0;
531 ref->use = use_type;
532 ref->speculative = 0;
534 /* If vector was moved in memory, update pointers. */
535 if (old_references != list->references->address ())
537 int i;
538 for (i = 0; iterate_reference(i, ref2); i++)
539 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
541 return ref;
544 /* If VAL is a reference to a function or a variable, add a reference from
545 this symtab_node to the corresponding symbol table node. USE_TYPE specify
546 type of the use and STMT the statement (if it exists). Return the new
547 reference or NULL if none was created. */
549 ipa_ref *
550 symtab_node::maybe_create_reference (tree val, enum ipa_ref_use use_type,
551 gimple stmt)
553 STRIP_NOPS (val);
554 if (TREE_CODE (val) != ADDR_EXPR)
555 return NULL;
556 val = get_base_var (val);
557 if (val && (TREE_CODE (val) == FUNCTION_DECL
558 || TREE_CODE (val) == VAR_DECL))
560 symtab_node *referred = symtab_node::get (val);
561 gcc_checking_assert (referred);
562 return create_reference (referred, use_type, stmt);
564 return NULL;
567 /* Clone all references from symtab NODE to this symtab_node. */
569 void
570 symtab_node::clone_references (symtab_node *node)
572 ipa_ref *ref = NULL, *ref2 = NULL;
573 int i;
574 for (i = 0; node->iterate_reference (i, ref); i++)
576 bool speculative = ref->speculative;
577 unsigned int stmt_uid = ref->lto_stmt_uid;
579 ref2 = create_reference (ref->referred, ref->use, ref->stmt);
580 ref2->speculative = speculative;
581 ref2->lto_stmt_uid = stmt_uid;
585 /* Clone all referring from symtab NODE to this symtab_node. */
587 void
588 symtab_node::clone_referring (symtab_node *node)
590 ipa_ref *ref = NULL, *ref2 = NULL;
591 int i;
592 for (i = 0; node->iterate_referring(i, ref); i++)
594 bool speculative = ref->speculative;
595 unsigned int stmt_uid = ref->lto_stmt_uid;
597 ref2 = ref->referring->create_reference (this, ref->use, ref->stmt);
598 ref2->speculative = speculative;
599 ref2->lto_stmt_uid = stmt_uid;
603 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
605 ipa_ref *
606 symtab_node::clone_reference (ipa_ref *ref, gimple stmt)
608 bool speculative = ref->speculative;
609 unsigned int stmt_uid = ref->lto_stmt_uid;
610 ipa_ref *ref2;
612 ref2 = create_reference (ref->referred, ref->use, stmt);
613 ref2->speculative = speculative;
614 ref2->lto_stmt_uid = stmt_uid;
615 return ref2;
618 /* Find the structure describing a reference to REFERRED_NODE
619 and associated with statement STMT. */
621 ipa_ref *
622 symtab_node::find_reference (symtab_node *referred_node,
623 gimple stmt, unsigned int lto_stmt_uid)
625 ipa_ref *r = NULL;
626 int i;
628 for (i = 0; iterate_reference (i, r); i++)
629 if (r->referred == referred_node
630 && !r->speculative
631 && ((stmt && r->stmt == stmt)
632 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
633 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
634 return r;
635 return NULL;
638 /* Remove all references that are associated with statement STMT. */
640 void
641 symtab_node::remove_stmt_references (gimple stmt)
643 ipa_ref *r = NULL;
644 int i = 0;
646 while (iterate_reference (i, r))
647 if (r->stmt == stmt)
648 r->remove_reference ();
649 else
650 i++;
653 /* Remove all stmt references in non-speculative references.
654 Those are not maintained during inlining & clonning.
655 The exception are speculative references that are updated along
656 with callgraph edges associated with them. */
658 void
659 symtab_node::clear_stmts_in_references (void)
661 ipa_ref *r = NULL;
662 int i;
664 for (i = 0; iterate_reference (i, r); i++)
665 if (!r->speculative)
667 r->stmt = NULL;
668 r->lto_stmt_uid = 0;
672 /* Remove all references in ref list. */
674 void
675 symtab_node::remove_all_references (void)
677 while (vec_safe_length (ref_list.references))
678 ref_list.references->last ().remove_reference ();
679 vec_free (ref_list.references);
682 /* Remove all referring items in ref list. */
684 void
685 symtab_node::remove_all_referring (void)
687 while (ref_list.referring.length ())
688 ref_list.referring.last ()->remove_reference ();
689 ref_list.referring.release ();
692 /* Dump references in ref list to FILE. */
694 void
695 symtab_node::dump_references (FILE *file)
697 ipa_ref *ref = NULL;
698 int i;
699 for (i = 0; iterate_reference (i, ref); i++)
701 fprintf (file, "%s/%i (%s)",
702 ref->referred->asm_name (),
703 ref->referred->order,
704 ipa_ref_use_name [ref->use]);
705 if (ref->speculative)
706 fprintf (file, " (speculative)");
708 fprintf (file, "\n");
711 /* Dump referring in list to FILE. */
713 void
714 symtab_node::dump_referring (FILE *file)
716 ipa_ref *ref = NULL;
717 int i;
718 for (i = 0; iterate_referring(i, ref); i++)
720 fprintf (file, "%s/%i (%s)",
721 ref->referring->asm_name (),
722 ref->referring->order,
723 ipa_ref_use_name [ref->use]);
724 if (ref->speculative)
725 fprintf (file, " (speculative)");
727 fprintf (file, "\n");
730 /* Return true if list contains an alias. */
731 bool
732 symtab_node::has_aliases_p (void)
734 ipa_ref *ref = NULL;
735 int i;
737 for (i = 0; iterate_referring (i, ref); i++)
738 if (ref->use == IPA_REF_ALIAS)
739 return true;
740 return false;
743 /* Iterates I-th reference in the list, REF is also set. */
745 ipa_ref *
746 symtab_node::iterate_reference (unsigned i, ipa_ref *&ref)
748 vec_safe_iterate (ref_list.references, i, &ref);
750 return ref;
753 /* Iterates I-th referring item in the list, REF is also set. */
755 ipa_ref *
756 symtab_node::iterate_referring (unsigned i, ipa_ref *&ref)
758 ref_list.referring.iterate (i, &ref);
760 return ref;
763 /* Iterates I-th referring alias item in the list, REF is also set. */
765 ipa_ref *
766 symtab_node::iterate_direct_aliases (unsigned i, ipa_ref *&ref)
768 ref_list.referring.iterate (i, &ref);
770 if (ref && ref->use != IPA_REF_ALIAS)
771 return NULL;
773 return ref;
776 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
778 /* Dump base fields of symtab nodes to F. Not to be used directly. */
780 void
781 symtab_node::dump_base (FILE *f)
783 static const char * const visibility_types[] = {
784 "default", "protected", "hidden", "internal"
787 fprintf (f, "%s/%i (%s)", asm_name (), order, name ());
788 dump_addr (f, " @", (void *)this);
789 fprintf (f, "\n Type: %s", symtab_type_names[type]);
791 if (definition)
792 fprintf (f, " definition");
793 if (analyzed)
794 fprintf (f, " analyzed");
795 if (alias)
796 fprintf (f, " alias");
797 if (weakref)
798 fprintf (f, " weakref");
799 if (cpp_implicit_alias)
800 fprintf (f, " cpp_implicit_alias");
801 if (alias_target)
802 fprintf (f, " target:%s",
803 DECL_P (alias_target)
804 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
805 (alias_target))
806 : IDENTIFIER_POINTER (alias_target));
807 if (body_removed)
808 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
809 fprintf (f, "\n Visibility:");
810 if (in_other_partition)
811 fprintf (f, " in_other_partition");
812 if (used_from_other_partition)
813 fprintf (f, " used_from_other_partition");
814 if (force_output)
815 fprintf (f, " force_output");
816 if (forced_by_abi)
817 fprintf (f, " forced_by_abi");
818 if (externally_visible)
819 fprintf (f, " externally_visible");
820 if (no_reorder)
821 fprintf (f, " no_reorder");
822 if (resolution != LDPR_UNKNOWN)
823 fprintf (f, " %s",
824 ld_plugin_symbol_resolution_names[(int)resolution]);
825 if (TREE_ASM_WRITTEN (decl))
826 fprintf (f, " asm_written");
827 if (DECL_EXTERNAL (decl))
828 fprintf (f, " external");
829 if (TREE_PUBLIC (decl))
830 fprintf (f, " public");
831 if (DECL_COMMON (decl))
832 fprintf (f, " common");
833 if (DECL_WEAK (decl))
834 fprintf (f, " weak");
835 if (DECL_DLLIMPORT_P (decl))
836 fprintf (f, " dll_import");
837 if (DECL_COMDAT (decl))
838 fprintf (f, " comdat");
839 if (get_comdat_group ())
840 fprintf (f, " comdat_group:%s",
841 IDENTIFIER_POINTER (get_comdat_group_id ()));
842 if (DECL_ONE_ONLY (decl))
843 fprintf (f, " one_only");
844 if (get_section ())
845 fprintf (f, " section:%s",
846 get_section ());
847 if (implicit_section)
848 fprintf (f," (implicit_section)");
849 if (DECL_VISIBILITY_SPECIFIED (decl))
850 fprintf (f, " visibility_specified");
851 if (DECL_VISIBILITY (decl))
852 fprintf (f, " visibility:%s",
853 visibility_types [DECL_VISIBILITY (decl)]);
854 if (DECL_VIRTUAL_P (decl))
855 fprintf (f, " virtual");
856 if (DECL_ARTIFICIAL (decl))
857 fprintf (f, " artificial");
858 if (TREE_CODE (decl) == FUNCTION_DECL)
860 if (DECL_STATIC_CONSTRUCTOR (decl))
861 fprintf (f, " constructor");
862 if (DECL_STATIC_DESTRUCTOR (decl))
863 fprintf (f, " destructor");
865 fprintf (f, "\n");
867 if (same_comdat_group)
868 fprintf (f, " Same comdat group as: %s/%i\n",
869 same_comdat_group->asm_name (),
870 same_comdat_group->order);
871 if (next_sharing_asm_name)
872 fprintf (f, " next sharing asm name: %i\n",
873 next_sharing_asm_name->order);
874 if (previous_sharing_asm_name)
875 fprintf (f, " previous sharing asm name: %i\n",
876 previous_sharing_asm_name->order);
878 if (address_taken)
879 fprintf (f, " Address is taken.\n");
880 if (aux)
882 fprintf (f, " Aux:");
883 dump_addr (f, " @", (void *)aux);
886 fprintf (f, " References: ");
887 dump_references (f);
888 fprintf (f, " Referring: ");
889 dump_referring (f);
890 if (lto_file_data)
891 fprintf (f, " Read from file: %s\n",
892 lto_file_data->file_name);
895 /* Dump symtab node to F. */
897 void
898 symtab_node::dump (FILE *f)
900 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
901 cnode->dump (f);
902 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
903 vnode->dump (f);
906 /* Dump symbol table to F. */
908 void
909 symtab_node::dump_table (FILE *f)
911 symtab_node *node;
912 fprintf (f, "Symbol table:\n\n");
913 FOR_EACH_SYMBOL (node)
914 node->dump (f);
918 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
919 Return NULL if there's no such node. */
921 symtab_node *
922 symtab_node::get_for_asmname (const_tree asmname)
924 symtab_node *node;
926 symtab->symtab_initialize_asm_name_hash ();
927 hashval_t hash = symtab->decl_assembler_name_hash (asmname);
928 symtab_node **slot
929 = symtab->assembler_name_hash->find_slot_with_hash (asmname, hash,
930 NO_INSERT);
932 if (slot)
934 node = *slot;
935 return node;
937 return NULL;
940 /* Dump symtab node NODE to stderr. */
942 DEBUG_FUNCTION void
943 symtab_node::debug (void)
945 dump (stderr);
948 /* Verify common part of symtab nodes. */
950 DEBUG_FUNCTION bool
951 symtab_node::verify_base (void)
953 bool error_found = false;
954 symtab_node *hashed_node;
956 if (is_a <cgraph_node *> (this))
958 if (TREE_CODE (decl) != FUNCTION_DECL)
960 error ("function symbol is not function");
961 error_found = true;
964 else if (is_a <varpool_node *> (this))
966 if (TREE_CODE (decl) != VAR_DECL)
968 error ("variable symbol is not variable");
969 error_found = true;
972 else
974 error ("node has unknown type");
975 error_found = true;
978 if (symtab->state != LTO_STREAMING)
980 hashed_node = symtab_node::get (decl);
981 if (!hashed_node)
983 error ("node not found node->decl->decl_with_vis.symtab_node");
984 error_found = true;
986 if (hashed_node != this
987 && (!is_a <cgraph_node *> (this)
988 || !dyn_cast <cgraph_node *> (this)->clone_of
989 || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl))
991 error ("node differs from node->decl->decl_with_vis.symtab_node");
992 error_found = true;
995 if (symtab->assembler_name_hash)
997 hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
998 if (hashed_node && hashed_node->previous_sharing_asm_name)
1000 error ("assembler name hash list corrupted");
1001 error_found = true;
1003 while (hashed_node)
1005 if (hashed_node == this)
1006 break;
1007 hashed_node = hashed_node->next_sharing_asm_name;
1009 if (!hashed_node
1010 && !(is_a <varpool_node *> (this)
1011 || DECL_HARD_REGISTER (decl)))
1013 error ("node not found in symtab assembler name hash");
1014 error_found = true;
1017 if (previous_sharing_asm_name
1018 && previous_sharing_asm_name->next_sharing_asm_name != this)
1020 error ("double linked list of assembler names corrupted");
1021 error_found = true;
1023 if (analyzed && !definition)
1025 error ("node is analyzed byt it is not a definition");
1026 error_found = true;
1028 if (cpp_implicit_alias && !alias)
1030 error ("node is alias but not implicit alias");
1031 error_found = true;
1033 if (alias && !definition && !weakref)
1035 error ("node is alias but not definition");
1036 error_found = true;
1038 if (weakref && !alias)
1040 error ("node is weakref but not an alias");
1041 error_found = true;
1043 if (same_comdat_group)
1045 symtab_node *n = same_comdat_group;
1047 if (!n->get_comdat_group ())
1049 error ("node is in same_comdat_group list but has no comdat_group");
1050 error_found = true;
1052 if (n->get_comdat_group () != get_comdat_group ())
1054 error ("same_comdat_group list across different groups");
1055 error_found = true;
1057 if (!n->definition)
1059 error ("Node has same_comdat_group but it is not a definition");
1060 error_found = true;
1062 if (n->type != type)
1064 error ("mixing different types of symbol in same comdat groups is not supported");
1065 error_found = true;
1067 if (n == this)
1069 error ("node is alone in a comdat group");
1070 error_found = true;
1074 if (!n->same_comdat_group)
1076 error ("same_comdat_group is not a circular list");
1077 error_found = true;
1078 break;
1080 n = n->same_comdat_group;
1082 while (n != this);
1083 if (comdat_local_p ())
1085 ipa_ref *ref = NULL;
1087 for (int i = 0; iterate_referring (i, ref); ++i)
1089 if (!in_same_comdat_group_p (ref->referring))
1091 error ("comdat-local symbol referred to by %s outside its "
1092 "comdat",
1093 identifier_to_locale (ref->referring->name()));
1094 error_found = true;
1099 if (implicit_section && !get_section ())
1101 error ("implicit_section flag is set but section isn't");
1102 error_found = true;
1104 if (get_section () && get_comdat_group ()
1105 && !implicit_section)
1107 error ("Both section and comdat group is set");
1108 error_found = true;
1110 /* TODO: Add string table for sections, so we do not keep holding duplicated
1111 strings. */
1112 if (alias && definition
1113 && get_section () != get_alias_target ()->get_section ()
1114 && (!get_section()
1115 || !get_alias_target ()->get_section ()
1116 || strcmp (get_section(),
1117 get_alias_target ()->get_section ())))
1119 error ("Alias and target's section differs");
1120 get_alias_target ()->dump (stderr);
1121 error_found = true;
1123 if (alias && definition
1124 && get_comdat_group () != get_alias_target ()->get_comdat_group ())
1126 error ("Alias and target's comdat groups differs");
1127 get_alias_target ()->dump (stderr);
1128 error_found = true;
1131 return error_found;
1134 /* Verify consistency of NODE. */
1136 DEBUG_FUNCTION void
1137 symtab_node::verify (void)
1139 if (seen_error ())
1140 return;
1142 timevar_push (TV_CGRAPH_VERIFY);
1143 if (cgraph_node *node = dyn_cast <cgraph_node *> (this))
1144 node->verify_node ();
1145 else
1146 if (verify_base ())
1148 debug ();
1149 internal_error ("symtab_node::verify failed");
1151 timevar_pop (TV_CGRAPH_VERIFY);
1154 /* Verify symbol table for internal consistency. */
1156 DEBUG_FUNCTION void
1157 symtab_node::verify_symtab_nodes (void)
1159 symtab_node *node;
1160 hash_map<tree, symtab_node *> comdat_head_map (251);
1162 FOR_EACH_SYMBOL (node)
1164 node->verify ();
1165 if (node->get_comdat_group ())
1167 symtab_node **entry, *s;
1168 bool existed;
1170 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1171 &existed);
1172 if (!existed)
1173 *entry = node;
1174 else
1175 for (s = (*entry)->same_comdat_group; s != NULL && s != node; s = s->same_comdat_group)
1176 if (!s || s == *entry)
1178 error ("Two symbols with same comdat_group are not linked by the same_comdat_group list.");
1179 (*entry)->debug ();
1180 node->debug ();
1181 internal_error ("symtab_node::verify failed");
1187 /* Return true when NODE is known to be used from other (non-LTO)
1188 object file. Known only when doing LTO via linker plugin. */
1190 bool
1191 symtab_node::used_from_object_file_p_worker (symtab_node *node)
1193 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
1194 return false;
1195 if (resolution_used_from_other_file_p (node->resolution))
1196 return true;
1197 return false;
1201 /* Return true when symtab_node is known to be used from other (non-LTO)
1202 object file. Known only when doing LTO via linker plugin. */
1204 bool
1205 symtab_node::used_from_object_file_p (void)
1207 return symtab_node::used_from_object_file_p_worker (this);
1210 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1211 but other code such as notice_global_symbol generates rtl. */
1213 void
1214 symtab_node::make_decl_local (void)
1216 rtx rtl, symbol;
1218 /* Avoid clearing comdat_groups on comdat-local decls. */
1219 if (TREE_PUBLIC (decl) == 0)
1220 return;
1222 if (TREE_CODE (decl) == VAR_DECL)
1223 DECL_COMMON (decl) = 0;
1224 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1226 DECL_COMDAT (decl) = 0;
1227 DECL_WEAK (decl) = 0;
1228 DECL_EXTERNAL (decl) = 0;
1229 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1230 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1231 TREE_PUBLIC (decl) = 0;
1232 if (!DECL_RTL_SET_P (decl))
1233 return;
1235 /* Update rtl flags. */
1236 make_decl_rtl (decl);
1238 rtl = DECL_RTL (decl);
1239 if (!MEM_P (rtl))
1240 return;
1242 symbol = XEXP (rtl, 0);
1243 if (GET_CODE (symbol) != SYMBOL_REF)
1244 return;
1246 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1249 /* Walk the alias chain to return the symbol NODE is alias of.
1250 If NODE is not an alias, return NODE.
1251 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
1253 symtab_node *
1254 symtab_node::ultimate_alias_target (enum availability *availability)
1256 bool weakref_p = false;
1258 if (!alias)
1260 if (availability)
1261 *availability = get_availability ();
1262 return this;
1265 /* To determine visibility of the target, we follow ELF semantic of aliases.
1266 Here alias is an alternative assembler name of a given definition. Its
1267 availability prevails the availability of its target (i.e. static alias of
1268 weak definition is available.
1270 Weakref is a different animal (and not part of ELF per se). It is just
1271 alternative name of a given symbol used within one complation unit
1272 and is translated prior hitting the object file. It inherits the
1273 visibility of its target (i.e. weakref of non-overwritable definition
1274 is non-overwritable, while weakref of weak definition is weak).
1276 If we ever get into supporting targets with different semantics, a target
1277 hook will be needed here. */
1279 if (availability)
1281 weakref_p = weakref;
1282 if (!weakref_p)
1283 *availability = get_availability ();
1284 else
1285 *availability = AVAIL_LOCAL;
1288 symtab_node *node = this;
1289 while (node)
1291 if (node->alias && node->analyzed)
1292 node = node->get_alias_target ();
1293 else
1295 if (!availability)
1297 else if (node->analyzed)
1299 if (weakref_p)
1301 enum availability a = node->get_availability ();
1302 if (a < *availability)
1303 *availability = a;
1306 else
1307 *availability = AVAIL_NOT_AVAILABLE;
1308 return node;
1310 if (node && availability && weakref_p)
1312 enum availability a = node->get_availability ();
1313 if (a < *availability)
1314 *availability = a;
1315 weakref_p = node->weakref;
1318 if (availability)
1319 *availability = AVAIL_NOT_AVAILABLE;
1320 return NULL;
1323 /* C++ FE sometimes change linkage flags after producing same body aliases.
1325 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1326 are obviously equivalent. The way it is doing so is however somewhat
1327 kludgy and interferes with the visibility code. As a result we need to
1328 copy the visibility from the target to get things right. */
1330 void
1331 symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target)
1333 if (is_a <cgraph_node *> (this))
1335 DECL_DECLARED_INLINE_P (decl)
1336 = DECL_DECLARED_INLINE_P (target->decl);
1337 DECL_DISREGARD_INLINE_LIMITS (decl)
1338 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1340 /* FIXME: It is not really clear why those flags should not be copied for
1341 functions, too. */
1342 else
1344 DECL_WEAK (decl) = DECL_WEAK (target->decl);
1345 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1346 DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl);
1348 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (target->decl);
1349 if (TREE_PUBLIC (decl))
1351 tree group;
1353 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1354 DECL_COMDAT (decl) = DECL_COMDAT (target->decl);
1355 group = target->get_comdat_group ();
1356 set_comdat_group (group);
1357 if (group && !same_comdat_group)
1358 add_to_same_comdat_group (target);
1360 externally_visible = target->externally_visible;
1363 /* Set section, do not recurse into aliases.
1364 When one wants to change section of symbol and its aliases,
1365 use set_section. */
1367 void
1368 symtab_node::set_section_for_node (const char *section)
1370 const char *current = get_section ();
1371 section_hash_entry **slot;
1373 if (current == section
1374 || (current && section
1375 && !strcmp (current, section)))
1376 return;
1378 if (current)
1380 x_section->ref_count--;
1381 if (!x_section->ref_count)
1383 hashval_t hash = htab_hash_string (x_section->name);
1384 slot = symtab->section_hash->find_slot_with_hash (x_section->name,
1385 hash, INSERT);
1386 ggc_free (x_section);
1387 symtab->section_hash->clear_slot (slot);
1389 x_section = NULL;
1391 if (!section)
1393 implicit_section = false;
1394 return;
1396 if (!symtab->section_hash)
1397 symtab->section_hash = hash_table<section_name_hasher>::create_ggc (10);
1398 slot = symtab->section_hash->find_slot_with_hash (section,
1399 htab_hash_string (section),
1400 INSERT);
1401 if (*slot)
1402 x_section = (section_hash_entry *)*slot;
1403 else
1405 int len = strlen (section);
1406 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1407 x_section->name = ggc_vec_alloc<char> (len + 1);
1408 memcpy (x_section->name, section, len + 1);
1410 x_section->ref_count++;
1413 /* Worker for set_section. */
1415 bool
1416 symtab_node::set_section (symtab_node *n, void *s)
1418 n->set_section_for_node ((char *)s);
1419 return false;
1422 /* Set section of symbol and its aliases. */
1424 void
1425 symtab_node::set_section (const char *section)
1427 gcc_assert (!this->alias);
1428 call_for_symbol_and_aliases
1429 (symtab_node::set_section, const_cast<char *>(section), true);
1432 /* Return the initialization priority. */
1434 priority_type
1435 symtab_node::get_init_priority ()
1437 if (!this->in_init_priority_hash)
1438 return DEFAULT_INIT_PRIORITY;
1440 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1441 return h ? h->init : DEFAULT_INIT_PRIORITY;
1444 /* Return availability of NODE. */
1445 enum availability symtab_node::get_availability (void)
1447 if (is_a <cgraph_node *> (this))
1448 return dyn_cast <cgraph_node *> (this)->get_availability ();
1449 else
1450 return dyn_cast <varpool_node *> (this)->get_availability ();;
1454 /* Return the finalization priority. */
1456 priority_type
1457 cgraph_node::get_fini_priority ()
1459 if (!this->in_init_priority_hash)
1460 return DEFAULT_INIT_PRIORITY;
1461 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1462 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1465 /* Return the initialization and finalization priority information for
1466 DECL. If there is no previous priority information, a freshly
1467 allocated structure is returned. */
1469 symbol_priority_map *
1470 symtab_node::priority_info (void)
1472 if (!symtab->init_priority_hash)
1473 symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13);
1475 bool existed;
1476 symbol_priority_map *h
1477 = &symtab->init_priority_hash->get_or_insert (this, &existed);
1478 if (!existed)
1480 h->init = DEFAULT_INIT_PRIORITY;
1481 h->fini = DEFAULT_INIT_PRIORITY;
1482 in_init_priority_hash = true;
1485 return h;
1488 /* Set initialization priority to PRIORITY. */
1490 void
1491 symtab_node::set_init_priority (priority_type priority)
1493 symbol_priority_map *h;
1495 if (is_a <cgraph_node *> (this))
1496 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1498 if (priority == DEFAULT_INIT_PRIORITY)
1500 gcc_assert (get_init_priority() == priority);
1501 return;
1503 h = priority_info ();
1504 h->init = priority;
1507 /* Set fialization priority to PRIORITY. */
1509 void
1510 cgraph_node::set_fini_priority (priority_type priority)
1512 symbol_priority_map *h;
1514 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1516 if (priority == DEFAULT_INIT_PRIORITY)
1518 gcc_assert (get_fini_priority() == priority);
1519 return;
1521 h = priority_info ();
1522 h->fini = priority;
1525 /* Worker for symtab_resolve_alias. */
1527 bool
1528 symtab_node::set_implicit_section (symtab_node *n,
1529 void *data ATTRIBUTE_UNUSED)
1531 n->implicit_section = true;
1532 return false;
1535 /* Add reference recording that symtab node is alias of TARGET.
1536 The function can fail in the case of aliasing cycles; in this case
1537 it returns false. */
1539 bool
1540 symtab_node::resolve_alias (symtab_node *target)
1542 symtab_node *n;
1544 gcc_assert (!analyzed && !vec_safe_length (ref_list.references));
1546 /* Never let cycles to creep into the symbol table alias references;
1547 those will make alias walkers to be infinite. */
1548 for (n = target; n && n->alias;
1549 n = n->analyzed ? n->get_alias_target () : NULL)
1550 if (n == this)
1552 if (is_a <cgraph_node *> (this))
1553 error ("function %q+D part of alias cycle", decl);
1554 else if (is_a <varpool_node *> (this))
1555 error ("variable %q+D part of alias cycle", decl);
1556 else
1557 gcc_unreachable ();
1558 alias = false;
1559 return false;
1562 /* "analyze" the node - i.e. mark the reference. */
1563 definition = true;
1564 alias = true;
1565 analyzed = true;
1566 create_reference (target, IPA_REF_ALIAS, NULL);
1568 /* Add alias into the comdat group of its target unless it is already there. */
1569 if (same_comdat_group)
1570 remove_from_same_comdat_group ();
1571 set_comdat_group (NULL);
1572 if (target->get_comdat_group ())
1573 add_to_same_comdat_group (target);
1575 if ((get_section () != target->get_section ()
1576 || target->get_comdat_group ()) && get_section () && !implicit_section)
1578 error ("section of alias %q+D must match section of its target", decl);
1580 call_for_symbol_and_aliases (symtab_node::set_section,
1581 const_cast<char *>(target->get_section ()), true);
1582 if (target->implicit_section)
1583 call_for_symbol_and_aliases (set_implicit_section, NULL, true);
1585 /* Alias targets become redundant after alias is resolved into an reference.
1586 We do not want to keep it around or we would have to mind updating them
1587 when renaming symbols. */
1588 alias_target = NULL;
1590 if (cpp_implicit_alias && symtab->state >= CONSTRUCTION)
1591 fixup_same_cpp_alias_visibility (target);
1593 /* If alias has address taken, so does the target. */
1594 if (address_taken)
1595 target->ultimate_alias_target ()->address_taken = true;
1596 return true;
1599 /* Call calback on symtab node and aliases associated to this node.
1600 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1601 skipped. */
1603 bool
1604 symtab_node::call_for_symbol_and_aliases (bool (*callback) (symtab_node *,
1605 void *),
1606 void *data, bool include_overwritable)
1608 int i;
1609 ipa_ref *ref;
1611 if (callback (this, data))
1612 return true;
1613 for (i = 0; iterate_referring (i, ref); i++)
1614 if (ref->use == IPA_REF_ALIAS)
1616 symtab_node *alias = ref->referring;
1617 if (include_overwritable
1618 || alias->get_availability () > AVAIL_INTERPOSABLE)
1619 if (alias->call_for_symbol_and_aliases (callback, data,
1620 include_overwritable))
1621 return true;
1623 return false;
1626 /* Worker searching noninterposable alias. */
1628 bool
1629 symtab_node::noninterposable_alias (symtab_node *node, void *data)
1631 if (decl_binds_to_current_def_p (node->decl))
1633 symtab_node *fn = node->ultimate_alias_target ();
1635 /* Ensure that the alias is well formed this may not be the case
1636 of user defined aliases and currently it is not always the case
1637 of C++ same body aliases (that is a bug). */
1638 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
1639 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
1640 || (TREE_CODE (node->decl) == FUNCTION_DECL
1641 && flags_from_decl_or_type (node->decl)
1642 != flags_from_decl_or_type (fn->decl))
1643 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
1644 return false;
1646 *(symtab_node **)data = node;
1647 return true;
1649 return false;
1652 /* If node can not be overwriten by static or dynamic linker to point to
1653 different definition, return NODE. Otherwise look for alias with such
1654 property and if none exists, introduce new one. */
1656 symtab_node *
1657 symtab_node::noninterposable_alias (void)
1659 tree new_decl;
1660 symtab_node *new_node = NULL;
1662 /* First try to look up existing alias or base object
1663 (if that is already non-overwritable). */
1664 symtab_node *node = ultimate_alias_target ();
1665 gcc_assert (!node->alias && !node->weakref);
1666 node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias,
1667 (void *)&new_node, true);
1668 if (new_node)
1669 return new_node;
1670 #ifndef ASM_OUTPUT_DEF
1671 /* If aliases aren't supported by the assembler, fail. */
1672 return NULL;
1673 #endif
1675 /* Otherwise create a new one. */
1676 new_decl = copy_node (node->decl);
1677 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1678 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1679 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1680 DECL_INITIAL (new_decl) = NULL;
1681 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1682 SET_DECL_RTL (new_decl, NULL);
1684 /* Update the properties. */
1685 DECL_EXTERNAL (new_decl) = 0;
1686 TREE_PUBLIC (new_decl) = 0;
1687 DECL_COMDAT (new_decl) = 0;
1688 DECL_WEAK (new_decl) = 0;
1690 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1691 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1692 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1694 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1695 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1696 new_node = cgraph_node::create_alias (new_decl, node->decl);
1698 else
1700 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1701 DECL_INITIAL (new_decl) = error_mark_node;
1702 new_node = varpool_node::create_alias (new_decl, node->decl);
1704 new_node->resolve_alias (node);
1705 gcc_assert (decl_binds_to_current_def_p (new_decl)
1706 && targetm.binds_local_p (new_decl));
1707 return new_node;
1710 /* Return true if symtab node and TARGET represents
1711 semantically equivalent symbols. */
1713 bool
1714 symtab_node::semantically_equivalent_p (symtab_node *target)
1716 enum availability avail;
1717 symtab_node *ba;
1718 symtab_node *bb;
1720 /* Equivalent functions are equivalent. */
1721 if (decl == target->decl)
1722 return true;
1724 /* If symbol is not overwritable by different implementation,
1725 walk to the base object it defines. */
1726 ba = ultimate_alias_target (&avail);
1727 if (avail >= AVAIL_AVAILABLE)
1729 if (target == ba)
1730 return true;
1732 else
1733 ba = this;
1734 bb = target->ultimate_alias_target (&avail);
1735 if (avail >= AVAIL_AVAILABLE)
1737 if (this == bb)
1738 return true;
1740 else
1741 bb = target;
1742 return bb == ba;
1745 /* Classify symbol symtab node for partitioning. */
1747 enum symbol_partitioning_class
1748 symtab_node::get_partitioning_class (void)
1750 /* Inline clones are always duplicated.
1751 This include external delcarations. */
1752 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
1754 if (DECL_ABSTRACT_P (decl))
1755 return SYMBOL_EXTERNAL;
1757 if (cnode && cnode->global.inlined_to)
1758 return SYMBOL_DUPLICATE;
1760 /* Weakref aliases are always duplicated. */
1761 if (weakref)
1762 return SYMBOL_DUPLICATE;
1764 /* External declarations are external. */
1765 if (DECL_EXTERNAL (decl))
1766 return SYMBOL_EXTERNAL;
1768 if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
1770 /* Constant pool references use local symbol names that can not
1771 be promoted global. We should never put into a constant pool
1772 objects that can not be duplicated across partitions. */
1773 if (DECL_IN_CONSTANT_POOL (decl))
1774 return SYMBOL_DUPLICATE;
1775 gcc_checking_assert (vnode->definition);
1777 /* Functions that are cloned may stay in callgraph even if they are unused.
1778 Handle them as external; compute_ltrans_boundary take care to make
1779 proper things to happen (i.e. to make them appear in the boundary but
1780 with body streamed, so clone can me materialized). */
1781 else if (!dyn_cast <cgraph_node *> (this)->definition)
1782 return SYMBOL_EXTERNAL;
1784 /* Linker discardable symbols are duplicated to every use unless they are
1785 keyed. */
1786 if (DECL_ONE_ONLY (decl)
1787 && !force_output
1788 && !forced_by_abi
1789 && !used_from_object_file_p ())
1790 return SYMBOL_DUPLICATE;
1792 return SYMBOL_PARTITION;
1795 /* Return true when symbol is known to be non-zero. */
1797 bool
1798 symtab_node::nonzero_address ()
1800 /* Weakrefs may be NULL when their target is not defined. */
1801 if (alias && weakref)
1803 if (analyzed)
1805 symtab_node *target = ultimate_alias_target ();
1807 if (target->alias && target->weakref)
1808 return false;
1809 /* We can not recurse to target::nonzero. It is possible that the
1810 target is used only via the alias.
1811 We may walk references and look for strong use, but we do not know
1812 if this strong use will survive to final binary, so be
1813 conservative here.
1814 ??? Maybe we could do the lookup during late optimization that
1815 could be useful to eliminate the NULL pointer checks in LTO
1816 programs. */
1817 if (target->definition && !DECL_EXTERNAL (target->decl))
1818 return true;
1819 if (target->resolution != LDPR_UNKNOWN
1820 && target->resolution != LDPR_UNDEF
1821 && flag_delete_null_pointer_checks)
1822 return true;
1823 return false;
1825 else
1826 return false;
1829 /* With !flag_delete_null_pointer_checks we assume that symbols may
1830 bind to NULL. This is on by default on embedded targets only.
1832 Otherwise all non-WEAK symbols must be defined and thus non-NULL or
1833 linking fails. Important case of WEAK we want to do well are comdats.
1834 Those are handled by later check for definition.
1836 When parsing, beware the cases when WEAK attribute is added later. */
1837 if (!DECL_WEAK (decl)
1838 && flag_delete_null_pointer_checks)
1840 refuse_visibility_changes = true;
1841 return true;
1844 /* If target is defined and not extern, we know it will be output and thus
1845 it will bind to non-NULL.
1846 Play safe for flag_delete_null_pointer_checks where weak definition maye
1847 be re-defined by NULL. */
1848 if (definition && !DECL_EXTERNAL (decl)
1849 && (flag_delete_null_pointer_checks || !DECL_WEAK (decl)))
1851 if (!DECL_WEAK (decl))
1852 refuse_visibility_changes = true;
1853 return true;
1856 /* As the last resort, check the resolution info. */
1857 if (resolution != LDPR_UNKNOWN
1858 && resolution != LDPR_UNDEF
1859 && flag_delete_null_pointer_checks)
1860 return true;
1861 return false;