2014-09-01 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / symtab.c
blob739a8e4a308df4a0851498759bb7743fe3efd0ed
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)
411 symbol_priority_map in;
412 void **slot;
413 in.symbol = this;
415 slot = htab_find_slot (symtab->init_priority_hash, &in, NO_INSERT);
416 if (slot)
417 htab_clear_slot (symtab->init_priority_hash, slot);
422 /* Remove symbol from symbol table. */
424 void
425 symtab_node::remove (void)
427 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
428 cnode->remove ();
429 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
430 vnode->remove ();
433 /* Add NEW_ to the same comdat group that OLD is in. */
435 void
436 symtab_node::add_to_same_comdat_group (symtab_node *old_node)
438 gcc_assert (old_node->get_comdat_group ());
439 gcc_assert (!same_comdat_group);
440 gcc_assert (this != old_node);
442 set_comdat_group (old_node->get_comdat_group ());
443 same_comdat_group = old_node;
444 if (!old_node->same_comdat_group)
445 old_node->same_comdat_group = this;
446 else
448 symtab_node *n;
449 for (n = old_node->same_comdat_group;
450 n->same_comdat_group != old_node;
451 n = n->same_comdat_group)
453 n->same_comdat_group = this;
457 /* Dissolve the same_comdat_group list in which NODE resides. */
459 void
460 symtab_node::dissolve_same_comdat_group_list (void)
462 symtab_node *n = this;
463 symtab_node *next;
465 if (!same_comdat_group)
466 return;
469 next = n->same_comdat_group;
470 n->same_comdat_group = NULL;
471 /* Clear comdat_group for comdat locals, since
472 make_decl_local doesn't. */
473 if (!TREE_PUBLIC (n->decl))
474 n->set_comdat_group (NULL);
475 n = next;
477 while (n != this);
480 /* Return printable assembler name of NODE.
481 This function is used only for debugging. When assembler name
482 is unknown go with identifier name. */
484 const char *
485 symtab_node::asm_name () const
487 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
488 return lang_hooks.decl_printable_name (decl, 2);
489 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
492 /* Return printable identifier name. */
494 const char *
495 symtab_node::name () const
497 return lang_hooks.decl_printable_name (decl, 2);
500 /* Return ipa reference from this symtab_node to
501 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
502 of the use. */
504 ipa_ref *
505 symtab_node::create_reference (symtab_node *referred_node,
506 enum ipa_ref_use use_type)
508 return create_reference (referred_node, use_type, NULL);
512 /* Return ipa reference from this symtab_node to
513 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
514 of the use and STMT the statement (if it exists). */
516 ipa_ref *
517 symtab_node::create_reference (symtab_node *referred_node,
518 enum ipa_ref_use use_type, gimple stmt)
520 ipa_ref *ref = NULL, *ref2 = NULL;
521 ipa_ref_list *list, *list2;
522 ipa_ref_t *old_references;
524 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
525 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
527 list = &ref_list;
528 old_references = vec_safe_address (list->references);
529 vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
530 ref = &list->references->last ();
532 list2 = &referred_node->ref_list;
534 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
535 if(use_type == IPA_REF_ALIAS)
537 list2->referring.safe_insert (0, ref);
538 ref->referred_index = 0;
540 for (unsigned int i = 1; i < list2->referring.length (); i++)
541 list2->referring[i]->referred_index = i;
543 else
545 list2->referring.safe_push (ref);
546 ref->referred_index = list2->referring.length () - 1;
549 ref->referring = this;
550 ref->referred = referred_node;
551 ref->stmt = stmt;
552 ref->lto_stmt_uid = 0;
553 ref->use = use_type;
554 ref->speculative = 0;
556 /* If vector was moved in memory, update pointers. */
557 if (old_references != list->references->address ())
559 int i;
560 for (i = 0; iterate_reference(i, ref2); i++)
561 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
563 return ref;
566 /* If VAL is a reference to a function or a variable, add a reference from
567 this symtab_node to the corresponding symbol table node. USE_TYPE specify
568 type of the use and STMT the statement (if it exists). Return the new
569 reference or NULL if none was created. */
571 ipa_ref *
572 symtab_node::maybe_create_reference (tree val, enum ipa_ref_use use_type,
573 gimple stmt)
575 STRIP_NOPS (val);
576 if (TREE_CODE (val) != ADDR_EXPR)
577 return NULL;
578 val = get_base_var (val);
579 if (val && (TREE_CODE (val) == FUNCTION_DECL
580 || TREE_CODE (val) == VAR_DECL))
582 symtab_node *referred = symtab_node::get (val);
583 gcc_checking_assert (referred);
584 return create_reference (referred, use_type, stmt);
586 return NULL;
589 /* Clone all references from symtab NODE to this symtab_node. */
591 void
592 symtab_node::clone_references (symtab_node *node)
594 ipa_ref *ref = NULL, *ref2 = NULL;
595 int i;
596 for (i = 0; node->iterate_reference (i, ref); i++)
598 bool speculative = ref->speculative;
599 unsigned int stmt_uid = ref->lto_stmt_uid;
601 ref2 = create_reference (ref->referred, ref->use, ref->stmt);
602 ref2->speculative = speculative;
603 ref2->lto_stmt_uid = stmt_uid;
607 /* Clone all referring from symtab NODE to this symtab_node. */
609 void
610 symtab_node::clone_referring (symtab_node *node)
612 ipa_ref *ref = NULL, *ref2 = NULL;
613 int i;
614 for (i = 0; node->iterate_referring(i, ref); i++)
616 bool speculative = ref->speculative;
617 unsigned int stmt_uid = ref->lto_stmt_uid;
619 ref2 = ref->referring->create_reference (this, ref->use, ref->stmt);
620 ref2->speculative = speculative;
621 ref2->lto_stmt_uid = stmt_uid;
625 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
627 ipa_ref *
628 symtab_node::clone_reference (ipa_ref *ref, gimple stmt)
630 bool speculative = ref->speculative;
631 unsigned int stmt_uid = ref->lto_stmt_uid;
632 ipa_ref *ref2;
634 ref2 = create_reference (ref->referred, ref->use, stmt);
635 ref2->speculative = speculative;
636 ref2->lto_stmt_uid = stmt_uid;
637 return ref2;
640 /* Find the structure describing a reference to REFERRED_NODE
641 and associated with statement STMT. */
643 ipa_ref *
644 symtab_node::find_reference (symtab_node *referred_node,
645 gimple stmt, unsigned int lto_stmt_uid)
647 ipa_ref *r = NULL;
648 int i;
650 for (i = 0; iterate_reference (i, r); i++)
651 if (r->referred == referred_node
652 && !r->speculative
653 && ((stmt && r->stmt == stmt)
654 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
655 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
656 return r;
657 return NULL;
660 /* Remove all references that are associated with statement STMT. */
662 void
663 symtab_node::remove_stmt_references (gimple stmt)
665 ipa_ref *r = NULL;
666 int i = 0;
668 while (iterate_reference (i, r))
669 if (r->stmt == stmt)
670 r->remove_reference ();
671 else
672 i++;
675 /* Remove all stmt references in non-speculative references.
676 Those are not maintained during inlining & clonning.
677 The exception are speculative references that are updated along
678 with callgraph edges associated with them. */
680 void
681 symtab_node::clear_stmts_in_references (void)
683 ipa_ref *r = NULL;
684 int i;
686 for (i = 0; iterate_reference (i, r); i++)
687 if (!r->speculative)
689 r->stmt = NULL;
690 r->lto_stmt_uid = 0;
694 /* Remove all references in ref list. */
696 void
697 symtab_node::remove_all_references (void)
699 while (vec_safe_length (ref_list.references))
700 ref_list.references->last ().remove_reference ();
701 vec_free (ref_list.references);
704 /* Remove all referring items in ref list. */
706 void
707 symtab_node::remove_all_referring (void)
709 while (ref_list.referring.length ())
710 ref_list.referring.last ()->remove_reference ();
711 ref_list.referring.release ();
714 /* Dump references in ref list to FILE. */
716 void
717 symtab_node::dump_references (FILE *file)
719 ipa_ref *ref = NULL;
720 int i;
721 for (i = 0; iterate_reference (i, ref); i++)
723 fprintf (file, "%s/%i (%s)",
724 ref->referred->asm_name (),
725 ref->referred->order,
726 ipa_ref_use_name [ref->use]);
727 if (ref->speculative)
728 fprintf (file, " (speculative)");
730 fprintf (file, "\n");
733 /* Dump referring in list to FILE. */
735 void
736 symtab_node::dump_referring (FILE *file)
738 ipa_ref *ref = NULL;
739 int i;
740 for (i = 0; iterate_referring(i, ref); i++)
742 fprintf (file, "%s/%i (%s)",
743 ref->referring->asm_name (),
744 ref->referring->order,
745 ipa_ref_use_name [ref->use]);
746 if (ref->speculative)
747 fprintf (file, " (speculative)");
749 fprintf (file, "\n");
752 /* Return true if list contains an alias. */
753 bool
754 symtab_node::has_aliases_p (void)
756 ipa_ref *ref = NULL;
757 int i;
759 for (i = 0; iterate_referring (i, ref); i++)
760 if (ref->use == IPA_REF_ALIAS)
761 return true;
762 return false;
765 /* Iterates I-th reference in the list, REF is also set. */
767 ipa_ref *
768 symtab_node::iterate_reference (unsigned i, ipa_ref *&ref)
770 vec_safe_iterate (ref_list.references, i, &ref);
772 return ref;
775 /* Iterates I-th referring item in the list, REF is also set. */
777 ipa_ref *
778 symtab_node::iterate_referring (unsigned i, ipa_ref *&ref)
780 ref_list.referring.iterate (i, &ref);
782 return ref;
785 /* Iterates I-th referring alias item in the list, REF is also set. */
787 ipa_ref *
788 symtab_node::iterate_direct_aliases (unsigned i, ipa_ref *&ref)
790 ref_list.referring.iterate (i, &ref);
792 if (ref && ref->use != IPA_REF_ALIAS)
793 return NULL;
795 return ref;
798 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
800 /* Dump base fields of symtab nodes to F. Not to be used directly. */
802 void
803 symtab_node::dump_base (FILE *f)
805 static const char * const visibility_types[] = {
806 "default", "protected", "hidden", "internal"
809 fprintf (f, "%s/%i (%s)", asm_name (), order, name ());
810 dump_addr (f, " @", (void *)this);
811 fprintf (f, "\n Type: %s", symtab_type_names[type]);
813 if (definition)
814 fprintf (f, " definition");
815 if (analyzed)
816 fprintf (f, " analyzed");
817 if (alias)
818 fprintf (f, " alias");
819 if (weakref)
820 fprintf (f, " weakref");
821 if (cpp_implicit_alias)
822 fprintf (f, " cpp_implicit_alias");
823 if (alias_target)
824 fprintf (f, " target:%s",
825 DECL_P (alias_target)
826 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
827 (alias_target))
828 : IDENTIFIER_POINTER (alias_target));
829 if (body_removed)
830 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
831 fprintf (f, "\n Visibility:");
832 if (in_other_partition)
833 fprintf (f, " in_other_partition");
834 if (used_from_other_partition)
835 fprintf (f, " used_from_other_partition");
836 if (force_output)
837 fprintf (f, " force_output");
838 if (forced_by_abi)
839 fprintf (f, " forced_by_abi");
840 if (externally_visible)
841 fprintf (f, " externally_visible");
842 if (resolution != LDPR_UNKNOWN)
843 fprintf (f, " %s",
844 ld_plugin_symbol_resolution_names[(int)resolution]);
845 if (TREE_ASM_WRITTEN (decl))
846 fprintf (f, " asm_written");
847 if (DECL_EXTERNAL (decl))
848 fprintf (f, " external");
849 if (TREE_PUBLIC (decl))
850 fprintf (f, " public");
851 if (DECL_COMMON (decl))
852 fprintf (f, " common");
853 if (DECL_WEAK (decl))
854 fprintf (f, " weak");
855 if (DECL_DLLIMPORT_P (decl))
856 fprintf (f, " dll_import");
857 if (DECL_COMDAT (decl))
858 fprintf (f, " comdat");
859 if (get_comdat_group ())
860 fprintf (f, " comdat_group:%s",
861 IDENTIFIER_POINTER (get_comdat_group_id ()));
862 if (DECL_ONE_ONLY (decl))
863 fprintf (f, " one_only");
864 if (get_section ())
865 fprintf (f, " section:%s",
866 get_section ());
867 if (implicit_section)
868 fprintf (f," (implicit_section)");
869 if (DECL_VISIBILITY_SPECIFIED (decl))
870 fprintf (f, " visibility_specified");
871 if (DECL_VISIBILITY (decl))
872 fprintf (f, " visibility:%s",
873 visibility_types [DECL_VISIBILITY (decl)]);
874 if (DECL_VIRTUAL_P (decl))
875 fprintf (f, " virtual");
876 if (DECL_ARTIFICIAL (decl))
877 fprintf (f, " artificial");
878 if (TREE_CODE (decl) == FUNCTION_DECL)
880 if (DECL_STATIC_CONSTRUCTOR (decl))
881 fprintf (f, " constructor");
882 if (DECL_STATIC_DESTRUCTOR (decl))
883 fprintf (f, " destructor");
885 fprintf (f, "\n");
887 if (same_comdat_group)
888 fprintf (f, " Same comdat group as: %s/%i\n",
889 same_comdat_group->asm_name (),
890 same_comdat_group->order);
891 if (next_sharing_asm_name)
892 fprintf (f, " next sharing asm name: %i\n",
893 next_sharing_asm_name->order);
894 if (previous_sharing_asm_name)
895 fprintf (f, " previous sharing asm name: %i\n",
896 previous_sharing_asm_name->order);
898 if (address_taken)
899 fprintf (f, " Address is taken.\n");
900 if (aux)
902 fprintf (f, " Aux:");
903 dump_addr (f, " @", (void *)aux);
906 fprintf (f, " References: ");
907 dump_references (f);
908 fprintf (f, " Referring: ");
909 dump_referring (f);
910 if (lto_file_data)
911 fprintf (f, " Read from file: %s\n",
912 lto_file_data->file_name);
915 /* Dump symtab node to F. */
917 void
918 symtab_node::dump (FILE *f)
920 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
921 cnode->dump (f);
922 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
923 vnode->dump (f);
926 /* Dump symbol table to F. */
928 void
929 symtab_node::dump_table (FILE *f)
931 symtab_node *node;
932 fprintf (f, "Symbol table:\n\n");
933 FOR_EACH_SYMBOL (node)
934 node->dump (f);
938 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
939 Return NULL if there's no such node. */
941 symtab_node *
942 symtab_node::get_for_asmname (const_tree asmname)
944 symtab_node *node;
945 void **slot;
947 symtab->symtab_initialize_asm_name_hash ();
948 slot = htab_find_slot_with_hash (symtab->assembler_name_hash, asmname,
949 symtab->decl_assembler_name_hash (asmname),
950 NO_INSERT);
952 if (slot)
954 node = (symtab_node *) *slot;
955 return node;
957 return NULL;
960 /* Dump symtab node NODE to stderr. */
962 DEBUG_FUNCTION void
963 symtab_node::debug (void)
965 dump (stderr);
968 /* Verify common part of symtab nodes. */
970 DEBUG_FUNCTION bool
971 symtab_node::verify_base (void)
973 bool error_found = false;
974 symtab_node *hashed_node;
976 if (is_a <cgraph_node *> (this))
978 if (TREE_CODE (decl) != FUNCTION_DECL)
980 error ("function symbol is not function");
981 error_found = true;
984 else if (is_a <varpool_node *> (this))
986 if (TREE_CODE (decl) != VAR_DECL)
988 error ("variable symbol is not variable");
989 error_found = true;
992 else
994 error ("node has unknown type");
995 error_found = true;
998 if (symtab->state != LTO_STREAMING)
1000 hashed_node = symtab_node::get (decl);
1001 if (!hashed_node)
1003 error ("node not found node->decl->decl_with_vis.symtab_node");
1004 error_found = true;
1006 if (hashed_node != this
1007 && (!is_a <cgraph_node *> (this)
1008 || !dyn_cast <cgraph_node *> (this)->clone_of
1009 || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl))
1011 error ("node differs from node->decl->decl_with_vis.symtab_node");
1012 error_found = true;
1015 if (symtab->assembler_name_hash)
1017 hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
1018 if (hashed_node && hashed_node->previous_sharing_asm_name)
1020 error ("assembler name hash list corrupted");
1021 error_found = true;
1023 while (hashed_node)
1025 if (hashed_node == this)
1026 break;
1027 hashed_node = hashed_node->next_sharing_asm_name;
1029 if (!hashed_node
1030 && !(is_a <varpool_node *> (this)
1031 || DECL_HARD_REGISTER (decl)))
1033 error ("node not found in symtab assembler name hash");
1034 error_found = true;
1037 if (previous_sharing_asm_name
1038 && previous_sharing_asm_name->next_sharing_asm_name != this)
1040 error ("double linked list of assembler names corrupted");
1041 error_found = true;
1043 if (analyzed && !definition)
1045 error ("node is analyzed byt it is not a definition");
1046 error_found = true;
1048 if (cpp_implicit_alias && !alias)
1050 error ("node is alias but not implicit alias");
1051 error_found = true;
1053 if (alias && !definition && !weakref)
1055 error ("node is alias but not definition");
1056 error_found = true;
1058 if (weakref && !alias)
1060 error ("node is weakref but not an alias");
1061 error_found = true;
1063 if (same_comdat_group)
1065 symtab_node *n = same_comdat_group;
1067 if (!n->get_comdat_group ())
1069 error ("node is in same_comdat_group list but has no comdat_group");
1070 error_found = true;
1072 if (n->get_comdat_group () != get_comdat_group ())
1074 error ("same_comdat_group list across different groups");
1075 error_found = true;
1077 if (!n->definition)
1079 error ("Node has same_comdat_group but it is not a definition");
1080 error_found = true;
1082 if (n->type != type)
1084 error ("mixing different types of symbol in same comdat groups is not supported");
1085 error_found = true;
1087 if (n == this)
1089 error ("node is alone in a comdat group");
1090 error_found = true;
1094 if (!n->same_comdat_group)
1096 error ("same_comdat_group is not a circular list");
1097 error_found = true;
1098 break;
1100 n = n->same_comdat_group;
1102 while (n != this);
1103 if (comdat_local_p ())
1105 ipa_ref *ref = NULL;
1107 for (int i = 0; iterate_referring (i, ref); ++i)
1109 if (!in_same_comdat_group_p (ref->referring))
1111 error ("comdat-local symbol referred to by %s outside its "
1112 "comdat",
1113 identifier_to_locale (ref->referring->name()));
1114 error_found = true;
1119 if (implicit_section && !get_section ())
1121 error ("implicit_section flag is set but section isn't");
1122 error_found = true;
1124 if (get_section () && get_comdat_group ()
1125 && !implicit_section)
1127 error ("Both section and comdat group is set");
1128 error_found = true;
1130 /* TODO: Add string table for sections, so we do not keep holding duplicated
1131 strings. */
1132 if (alias && definition
1133 && get_section () != get_alias_target ()->get_section ()
1134 && (!get_section()
1135 || !get_alias_target ()->get_section ()
1136 || strcmp (get_section(),
1137 get_alias_target ()->get_section ())))
1139 error ("Alias and target's section differs");
1140 get_alias_target ()->dump (stderr);
1141 error_found = true;
1143 if (alias && definition
1144 && get_comdat_group () != get_alias_target ()->get_comdat_group ())
1146 error ("Alias and target's comdat groups differs");
1147 get_alias_target ()->dump (stderr);
1148 error_found = true;
1151 return error_found;
1154 /* Verify consistency of NODE. */
1156 DEBUG_FUNCTION void
1157 symtab_node::verify (void)
1159 if (seen_error ())
1160 return;
1162 timevar_push (TV_CGRAPH_VERIFY);
1163 if (cgraph_node *node = dyn_cast <cgraph_node *> (this))
1164 node->verify_node ();
1165 else
1166 if (verify_base ())
1168 debug ();
1169 internal_error ("symtab_node::verify failed");
1171 timevar_pop (TV_CGRAPH_VERIFY);
1174 /* Verify symbol table for internal consistency. */
1176 DEBUG_FUNCTION void
1177 symtab_node::verify_symtab_nodes (void)
1179 symtab_node *node;
1180 hash_map<tree, symtab_node *> comdat_head_map (251);
1182 FOR_EACH_SYMBOL (node)
1184 node->verify ();
1185 if (node->get_comdat_group ())
1187 symtab_node **entry, *s;
1188 bool existed;
1190 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1191 &existed);
1192 if (!existed)
1193 *entry = node;
1194 else
1195 for (s = (*entry)->same_comdat_group; s != NULL && s != node; s = s->same_comdat_group)
1196 if (!s || s == *entry)
1198 error ("Two symbols with same comdat_group are not linked by the same_comdat_group list.");
1199 (*entry)->debug ();
1200 node->debug ();
1201 internal_error ("symtab_node::verify failed");
1207 /* Return true when NODE is known to be used from other (non-LTO)
1208 object file. Known only when doing LTO via linker plugin. */
1210 bool
1211 symtab_node::used_from_object_file_p_worker (symtab_node *node)
1213 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
1214 return false;
1215 if (resolution_used_from_other_file_p (node->resolution))
1216 return true;
1217 return false;
1221 /* Return true when symtab_node is known to be used from other (non-LTO)
1222 object file. Known only when doing LTO via linker plugin. */
1224 bool
1225 symtab_node::used_from_object_file_p (void)
1227 return symtab_node::used_from_object_file_p_worker (this);
1230 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1231 but other code such as notice_global_symbol generates rtl. */
1233 void
1234 symtab_node::make_decl_local (void)
1236 rtx rtl, symbol;
1238 /* Avoid clearing comdat_groups on comdat-local decls. */
1239 if (TREE_PUBLIC (decl) == 0)
1240 return;
1242 if (TREE_CODE (decl) == VAR_DECL)
1243 DECL_COMMON (decl) = 0;
1244 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1246 DECL_COMDAT (decl) = 0;
1247 DECL_WEAK (decl) = 0;
1248 DECL_EXTERNAL (decl) = 0;
1249 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1250 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1251 TREE_PUBLIC (decl) = 0;
1252 if (!DECL_RTL_SET_P (decl))
1253 return;
1255 /* Update rtl flags. */
1256 make_decl_rtl (decl);
1258 rtl = DECL_RTL (decl);
1259 if (!MEM_P (rtl))
1260 return;
1262 symbol = XEXP (rtl, 0);
1263 if (GET_CODE (symbol) != SYMBOL_REF)
1264 return;
1266 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1269 /* Walk the alias chain to return the symbol NODE is alias of.
1270 If NODE is not an alias, return NODE.
1271 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
1273 symtab_node *
1274 symtab_node::ultimate_alias_target (enum availability *availability)
1276 bool weakref_p = false;
1278 if (!alias)
1280 if (availability)
1281 *availability = get_availability ();
1282 return this;
1285 /* To determine visibility of the target, we follow ELF semantic of aliases.
1286 Here alias is an alternative assembler name of a given definition. Its
1287 availability prevails the availability of its target (i.e. static alias of
1288 weak definition is available.
1290 Weakref is a different animal (and not part of ELF per se). It is just
1291 alternative name of a given symbol used within one complation unit
1292 and is translated prior hitting the object file. It inherits the
1293 visibility of its target (i.e. weakref of non-overwritable definition
1294 is non-overwritable, while weakref of weak definition is weak).
1296 If we ever get into supporting targets with different semantics, a target
1297 hook will be needed here. */
1299 if (availability)
1301 weakref_p = weakref;
1302 if (!weakref_p)
1303 *availability = get_availability ();
1304 else
1305 *availability = AVAIL_LOCAL;
1308 symtab_node *node = this;
1309 while (node)
1311 if (node->alias && node->analyzed)
1312 node = node->get_alias_target ();
1313 else
1315 if (!availability)
1317 else if (node->analyzed)
1319 if (weakref_p)
1321 enum availability a = node->get_availability ();
1322 if (a < *availability)
1323 *availability = a;
1326 else
1327 *availability = AVAIL_NOT_AVAILABLE;
1328 return node;
1330 if (node && availability && weakref_p)
1332 enum availability a = node->get_availability ();
1333 if (a < *availability)
1334 *availability = a;
1335 weakref_p = node->weakref;
1338 if (availability)
1339 *availability = AVAIL_NOT_AVAILABLE;
1340 return NULL;
1343 /* C++ FE sometimes change linkage flags after producing same body aliases.
1345 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1346 are obviously equivalent. The way it is doing so is however somewhat
1347 kludgy and interferes with the visibility code. As a result we need to
1348 copy the visibility from the target to get things right. */
1350 void
1351 symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target)
1353 if (is_a <cgraph_node *> (this))
1355 DECL_DECLARED_INLINE_P (decl)
1356 = DECL_DECLARED_INLINE_P (target->decl);
1357 DECL_DISREGARD_INLINE_LIMITS (decl)
1358 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1360 /* FIXME: It is not really clear why those flags should not be copied for
1361 functions, too. */
1362 else
1364 DECL_WEAK (decl) = DECL_WEAK (target->decl);
1365 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1366 DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl);
1368 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (target->decl);
1369 if (TREE_PUBLIC (decl))
1371 tree group;
1373 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1374 DECL_COMDAT (decl) = DECL_COMDAT (target->decl);
1375 group = target->get_comdat_group ();
1376 set_comdat_group (group);
1377 if (group && !same_comdat_group)
1378 add_to_same_comdat_group (target);
1380 externally_visible = target->externally_visible;
1383 /* Set section, do not recurse into aliases.
1384 When one wants to change section of symbol and its aliases,
1385 use set_section. */
1387 void
1388 symtab_node::set_section_for_node (const char *section)
1390 const char *current = get_section ();
1391 void **slot;
1393 if (current == section
1394 || (current && section
1395 && !strcmp (current, section)))
1396 return;
1398 if (current)
1400 x_section->ref_count--;
1401 if (!x_section->ref_count)
1403 slot = htab_find_slot_with_hash (symtab->section_hash, x_section->name,
1404 htab_hash_string (x_section->name),
1405 INSERT);
1406 ggc_free (x_section);
1407 htab_clear_slot (symtab->section_hash, slot);
1409 x_section = NULL;
1411 if (!section)
1413 implicit_section = false;
1414 return;
1416 if (!symtab->section_hash)
1417 symtab->section_hash = htab_create_ggc (10, hash_section_hash_entry,
1418 eq_sections, NULL);
1419 slot = htab_find_slot_with_hash (symtab->section_hash, section,
1420 htab_hash_string (section),
1421 INSERT);
1422 if (*slot)
1423 x_section = (section_hash_entry *)*slot;
1424 else
1426 int len = strlen (section);
1427 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1428 x_section->name = ggc_vec_alloc<char> (len + 1);
1429 memcpy (x_section->name, section, len + 1);
1431 x_section->ref_count++;
1434 /* Worker for set_section. */
1436 bool
1437 symtab_node::set_section (symtab_node *n, void *s)
1439 n->set_section_for_node ((char *)s);
1440 return false;
1443 /* Set section of symbol and its aliases. */
1445 void
1446 symtab_node::set_section (const char *section)
1448 gcc_assert (!this->alias);
1449 call_for_symbol_and_aliases
1450 (symtab_node::set_section, const_cast<char *>(section), true);
1453 /* Return the initialization priority. */
1455 priority_type
1456 symtab_node::get_init_priority ()
1458 symbol_priority_map *h;
1459 symbol_priority_map in;
1461 if (!this->in_init_priority_hash)
1462 return DEFAULT_INIT_PRIORITY;
1463 in.symbol = this;
1464 h = (symbol_priority_map *) htab_find (symtab->init_priority_hash,
1465 &in);
1466 return h ? h->init : DEFAULT_INIT_PRIORITY;
1469 /* Return availability of NODE. */
1470 enum availability symtab_node::get_availability (void)
1472 if (is_a <cgraph_node *> (this))
1473 return dyn_cast <cgraph_node *> (this)->get_availability ();
1474 else
1475 return dyn_cast <varpool_node *> (this)->get_availability ();;
1479 /* Return the finalization priority. */
1481 priority_type
1482 cgraph_node::get_fini_priority ()
1484 symbol_priority_map *h;
1485 symbol_priority_map in;
1487 if (!this->in_init_priority_hash)
1488 return DEFAULT_INIT_PRIORITY;
1489 in.symbol = this;
1490 h = (symbol_priority_map *) htab_find (symtab->init_priority_hash,
1491 &in);
1492 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1495 /* Return true if the from tree in both priority maps are equal. */
1498 symbol_priority_map_eq (const void *va, const void *vb)
1500 const symbol_priority_map *const a = (const symbol_priority_map *) va,
1501 *const b = (const symbol_priority_map *) vb;
1502 return (a->symbol == b->symbol);
1505 /* Hash a from symbol in a symbol_priority_map. */
1507 unsigned int
1508 symbol_priority_map_hash (const void *item)
1510 return htab_hash_pointer (((const symbol_priority_map *)item)->symbol);
1513 /* Return the initialization and finalization priority information for
1514 DECL. If there is no previous priority information, a freshly
1515 allocated structure is returned. */
1517 symbol_priority_map *
1518 symtab_node::priority_info (void)
1520 symbol_priority_map in;
1521 symbol_priority_map *h;
1522 void **loc;
1524 in.symbol = this;
1525 if (!symtab->init_priority_hash)
1526 symtab->init_priority_hash = htab_create_ggc (512,
1527 symbol_priority_map_hash,
1528 symbol_priority_map_eq, 0);
1530 loc = htab_find_slot (symtab->init_priority_hash, &in, INSERT);
1531 h = (symbol_priority_map *) *loc;
1532 if (!h)
1534 h = ggc_cleared_alloc<symbol_priority_map> ();
1535 *loc = h;
1536 h->symbol = this;
1537 h->init = DEFAULT_INIT_PRIORITY;
1538 h->fini = DEFAULT_INIT_PRIORITY;
1539 in_init_priority_hash = true;
1542 return h;
1545 /* Set initialization priority to PRIORITY. */
1547 void
1548 symtab_node::set_init_priority (priority_type priority)
1550 symbol_priority_map *h;
1552 if (is_a <cgraph_node *> (this))
1553 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1555 if (priority == DEFAULT_INIT_PRIORITY)
1557 gcc_assert (get_init_priority() == priority);
1558 return;
1560 h = priority_info ();
1561 h->init = priority;
1564 /* Set fialization priority to PRIORITY. */
1566 void
1567 cgraph_node::set_fini_priority (priority_type priority)
1569 symbol_priority_map *h;
1571 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1573 if (priority == DEFAULT_INIT_PRIORITY)
1575 gcc_assert (get_fini_priority() == priority);
1576 return;
1578 h = priority_info ();
1579 h->fini = priority;
1582 /* Worker for symtab_resolve_alias. */
1584 bool
1585 symtab_node::set_implicit_section (symtab_node *n,
1586 void *data ATTRIBUTE_UNUSED)
1588 n->implicit_section = true;
1589 return false;
1592 /* Add reference recording that symtab node is alias of TARGET.
1593 The function can fail in the case of aliasing cycles; in this case
1594 it returns false. */
1596 bool
1597 symtab_node::resolve_alias (symtab_node *target)
1599 symtab_node *n;
1601 gcc_assert (!analyzed && !vec_safe_length (ref_list.references));
1603 /* Never let cycles to creep into the symbol table alias references;
1604 those will make alias walkers to be infinite. */
1605 for (n = target; n && n->alias;
1606 n = n->analyzed ? n->get_alias_target () : NULL)
1607 if (n == this)
1609 if (is_a <cgraph_node *> (this))
1610 error ("function %q+D part of alias cycle", decl);
1611 else if (is_a <varpool_node *> (this))
1612 error ("variable %q+D part of alias cycle", decl);
1613 else
1614 gcc_unreachable ();
1615 alias = false;
1616 return false;
1619 /* "analyze" the node - i.e. mark the reference. */
1620 definition = true;
1621 alias = true;
1622 analyzed = true;
1623 create_reference (target, IPA_REF_ALIAS, NULL);
1625 /* Add alias into the comdat group of its target unless it is already there. */
1626 if (same_comdat_group)
1627 remove_from_same_comdat_group ();
1628 set_comdat_group (NULL);
1629 if (target->get_comdat_group ())
1630 add_to_same_comdat_group (target);
1632 if ((get_section () != target->get_section ()
1633 || target->get_comdat_group ()) && get_section () && !implicit_section)
1635 error ("section of alias %q+D must match section of its target", decl);
1637 call_for_symbol_and_aliases (symtab_node::set_section,
1638 const_cast<char *>(target->get_section ()), true);
1639 if (target->implicit_section)
1640 call_for_symbol_and_aliases (set_implicit_section, NULL, true);
1642 /* Alias targets become redundant after alias is resolved into an reference.
1643 We do not want to keep it around or we would have to mind updating them
1644 when renaming symbols. */
1645 alias_target = NULL;
1647 if (cpp_implicit_alias && symtab->state >= CONSTRUCTION)
1648 fixup_same_cpp_alias_visibility (target);
1650 /* If alias has address taken, so does the target. */
1651 if (address_taken)
1652 target->ultimate_alias_target ()->address_taken = true;
1653 return true;
1656 /* Call calback on symtab node and aliases associated to this node.
1657 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1658 skipped. */
1660 bool
1661 symtab_node::call_for_symbol_and_aliases (bool (*callback) (symtab_node *,
1662 void *),
1663 void *data, bool include_overwritable)
1665 int i;
1666 ipa_ref *ref;
1668 if (callback (this, data))
1669 return true;
1670 for (i = 0; iterate_referring (i, ref); i++)
1671 if (ref->use == IPA_REF_ALIAS)
1673 symtab_node *alias = ref->referring;
1674 if (include_overwritable
1675 || alias->get_availability () > AVAIL_INTERPOSABLE)
1676 if (alias->call_for_symbol_and_aliases (callback, data,
1677 include_overwritable))
1678 return true;
1680 return false;
1683 /* Worker searching noninterposable alias. */
1685 bool
1686 symtab_node::noninterposable_alias (symtab_node *node, void *data)
1688 if (decl_binds_to_current_def_p (node->decl))
1690 symtab_node *fn = node->ultimate_alias_target ();
1692 /* Ensure that the alias is well formed this may not be the case
1693 of user defined aliases and currently it is not always the case
1694 of C++ same body aliases (that is a bug). */
1695 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
1696 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
1697 || (TREE_CODE (node->decl) == FUNCTION_DECL
1698 && flags_from_decl_or_type (node->decl)
1699 != flags_from_decl_or_type (fn->decl))
1700 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
1701 return false;
1703 *(symtab_node **)data = node;
1704 return true;
1706 return false;
1709 /* If node can not be overwriten by static or dynamic linker to point to
1710 different definition, return NODE. Otherwise look for alias with such
1711 property and if none exists, introduce new one. */
1713 symtab_node *
1714 symtab_node::noninterposable_alias (void)
1716 tree new_decl;
1717 symtab_node *new_node = NULL;
1719 /* First try to look up existing alias or base object
1720 (if that is already non-overwritable). */
1721 symtab_node *node = ultimate_alias_target ();
1722 gcc_assert (!node->alias && !node->weakref);
1723 node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias,
1724 (void *)&new_node, true);
1725 if (new_node)
1726 return new_node;
1727 #ifndef ASM_OUTPUT_DEF
1728 /* If aliases aren't supported by the assembler, fail. */
1729 return NULL;
1730 #endif
1732 /* Otherwise create a new one. */
1733 new_decl = copy_node (node->decl);
1734 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1735 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1736 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1737 DECL_INITIAL (new_decl) = NULL;
1738 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1739 SET_DECL_RTL (new_decl, NULL);
1741 /* Update the properties. */
1742 DECL_EXTERNAL (new_decl) = 0;
1743 TREE_PUBLIC (new_decl) = 0;
1744 DECL_COMDAT (new_decl) = 0;
1745 DECL_WEAK (new_decl) = 0;
1747 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1748 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1749 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1751 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1752 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1753 new_node = cgraph_node::create_alias (new_decl, node->decl);
1755 else
1757 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1758 DECL_INITIAL (new_decl) = error_mark_node;
1759 new_node = varpool_node::create_alias (new_decl, node->decl);
1761 new_node->resolve_alias (node);
1762 gcc_assert (decl_binds_to_current_def_p (new_decl)
1763 && targetm.binds_local_p (new_decl));
1764 return new_node;
1767 /* Return true if symtab node and TARGET represents
1768 semantically equivalent symbols. */
1770 bool
1771 symtab_node::semantically_equivalent_p (symtab_node *target)
1773 enum availability avail;
1774 symtab_node *ba;
1775 symtab_node *bb;
1777 /* Equivalent functions are equivalent. */
1778 if (decl == target->decl)
1779 return true;
1781 /* If symbol is not overwritable by different implementation,
1782 walk to the base object it defines. */
1783 ba = ultimate_alias_target (&avail);
1784 if (avail >= AVAIL_AVAILABLE)
1786 if (target == ba)
1787 return true;
1789 else
1790 ba = this;
1791 bb = target->ultimate_alias_target (&avail);
1792 if (avail >= AVAIL_AVAILABLE)
1794 if (this == bb)
1795 return true;
1797 else
1798 bb = target;
1799 return bb == ba;
1802 /* Classify symbol symtab node for partitioning. */
1804 enum symbol_partitioning_class
1805 symtab_node::get_partitioning_class (void)
1807 /* Inline clones are always duplicated.
1808 This include external delcarations. */
1809 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
1811 if (DECL_ABSTRACT (decl))
1812 return SYMBOL_EXTERNAL;
1814 if (cnode && cnode->global.inlined_to)
1815 return SYMBOL_DUPLICATE;
1817 /* Weakref aliases are always duplicated. */
1818 if (weakref)
1819 return SYMBOL_DUPLICATE;
1821 /* External declarations are external. */
1822 if (DECL_EXTERNAL (decl))
1823 return SYMBOL_EXTERNAL;
1825 if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
1827 /* Constant pool references use local symbol names that can not
1828 be promoted global. We should never put into a constant pool
1829 objects that can not be duplicated across partitions. */
1830 if (DECL_IN_CONSTANT_POOL (decl))
1831 return SYMBOL_DUPLICATE;
1832 gcc_checking_assert (vnode->definition);
1834 /* Functions that are cloned may stay in callgraph even if they are unused.
1835 Handle them as external; compute_ltrans_boundary take care to make
1836 proper things to happen (i.e. to make them appear in the boundary but
1837 with body streamed, so clone can me materialized). */
1838 else if (!dyn_cast <cgraph_node *> (this)->definition)
1839 return SYMBOL_EXTERNAL;
1841 /* Linker discardable symbols are duplicated to every use unless they are
1842 keyed. */
1843 if (DECL_ONE_ONLY (decl)
1844 && !force_output
1845 && !forced_by_abi
1846 && !used_from_object_file_p ())
1847 return SYMBOL_DUPLICATE;
1849 return SYMBOL_PARTITION;
1852 /* Return true when symbol is known to be non-zero. */
1854 bool
1855 symtab_node::nonzero_address ()
1857 /* Weakrefs may be NULL when their target is not defined. */
1858 if (this->alias && this->weakref)
1860 if (this->analyzed)
1862 symtab_node *target = ultimate_alias_target ();
1864 if (target->alias && target->weakref)
1865 return false;
1866 /* We can not recurse to target::nonzero. It is possible that the
1867 target is used only via the alias.
1868 We may walk references and look for strong use, but we do not know
1869 if this strong use will survive to final binary, so be
1870 conservative here.
1871 ??? Maybe we could do the lookup during late optimization that
1872 could be useful to eliminate the NULL pointer checks in LTO
1873 programs. */
1874 if (target->definition && !DECL_EXTERNAL (target->decl))
1875 return true;
1876 if (target->resolution != LDPR_UNKNOWN
1877 && target->resolution != LDPR_UNDEF
1878 && flag_delete_null_pointer_checks)
1879 return true;
1880 return false;
1882 else
1883 return false;
1886 /* With !flag_delete_null_pointer_checks we assume that symbols may
1887 bind to NULL. This is on by default on embedded targets only.
1889 Otherwise all non-WEAK symbols must be defined and thus non-NULL or
1890 linking fails. Important case of WEAK we want to do well are comdats.
1891 Those are handled by later check for definition.
1893 When parsing, beware the cases when WEAK attribute is added later. */
1894 if (!DECL_WEAK (this->decl)
1895 && flag_delete_null_pointer_checks
1896 && symtab->state > PARSING)
1897 return true;
1899 /* If target is defined and not extern, we know it will be output and thus
1900 it will bind to non-NULL.
1901 Play safe for flag_delete_null_pointer_checks where weak definition maye
1902 be re-defined by NULL. */
1903 if (this->definition && !DECL_EXTERNAL (this->decl)
1904 && (flag_delete_null_pointer_checks || !DECL_WEAK (this->decl)))
1905 return true;
1907 /* As the last resort, check the resolution info. */
1908 if (this->resolution != LDPR_UNKNOWN
1909 && this->resolution != LDPR_UNDEF
1910 && flag_delete_null_pointer_checks)
1911 return true;
1912 return false;