remove pointer-set.[ch]
[official-gcc.git] / gcc / symtab.c
bloba93c299ec234cea448b721d0486ce40275c698ee
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"
65 /* Hash table used to hold sectoons. */
66 static GTY((param_is (section_hash_entry))) htab_t section_hash;
68 /* Hash table used to convert assembler names into nodes. */
69 static GTY((param_is (symtab_node))) htab_t assembler_name_hash;
71 /* Map from a symbol to initialization/finalization priorities. */
72 struct GTY(()) symbol_priority_map {
73 symtab_node *symbol;
74 priority_type init;
75 priority_type fini;
78 /* Hash table used to hold init priorities. */
79 static GTY ((param_is (struct symbol_priority_map)))
80 htab_t init_priority_hash;
82 /* Linked list of symbol table nodes. */
83 symtab_node *symtab_nodes;
85 /* The order index of the next symtab node to be created. This is
86 used so that we can sort the cgraph nodes in order by when we saw
87 them, to support -fno-toplevel-reorder. */
88 int symtab_order;
90 /* Hash asmnames ignoring the user specified marks. */
92 static hashval_t
93 decl_assembler_name_hash (const_tree asmname)
95 if (IDENTIFIER_POINTER (asmname)[0] == '*')
97 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
98 size_t ulp_len = strlen (user_label_prefix);
100 if (ulp_len == 0)
102 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
103 decl_str += ulp_len;
105 return htab_hash_string (decl_str);
108 return htab_hash_string (IDENTIFIER_POINTER (asmname));
112 /* Returns a hash code for P. */
114 static hashval_t
115 hash_node_by_assembler_name (const void *p)
117 const symtab_node *n = (const symtab_node *) p;
118 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
121 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
123 static bool
124 decl_assembler_name_equal (tree decl, const_tree asmname)
126 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
127 const char *decl_str;
128 const char *asmname_str;
129 bool test = false;
131 if (decl_asmname == asmname)
132 return true;
134 decl_str = IDENTIFIER_POINTER (decl_asmname);
135 asmname_str = IDENTIFIER_POINTER (asmname);
138 /* If the target assembler name was set by the user, things are trickier.
139 We have a leading '*' to begin with. After that, it's arguable what
140 is the correct thing to do with -fleading-underscore. Arguably, we've
141 historically been doing the wrong thing in assemble_alias by always
142 printing the leading underscore. Since we're not changing that, make
143 sure user_label_prefix follows the '*' before matching. */
144 if (decl_str[0] == '*')
146 size_t ulp_len = strlen (user_label_prefix);
148 decl_str ++;
150 if (ulp_len == 0)
151 test = true;
152 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
153 decl_str += ulp_len, test=true;
154 else
155 decl_str --;
157 if (asmname_str[0] == '*')
159 size_t ulp_len = strlen (user_label_prefix);
161 asmname_str ++;
163 if (ulp_len == 0)
164 test = true;
165 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
166 asmname_str += ulp_len, test=true;
167 else
168 asmname_str --;
171 if (!test)
172 return false;
173 return strcmp (decl_str, asmname_str) == 0;
177 /* Returns nonzero if P1 and P2 are equal. */
179 static int
180 eq_assembler_name (const void *p1, const void *p2)
182 const symtab_node *n1 = (const symtab_node *) p1;
183 const_tree name = (const_tree)p2;
184 return (decl_assembler_name_equal (n1->decl, name));
187 /* Insert NODE to assembler name hash. */
189 static void
190 insert_to_assembler_name_hash (symtab_node *node, bool with_clones)
192 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
193 return;
194 gcc_checking_assert (!node->previous_sharing_asm_name
195 && !node->next_sharing_asm_name);
196 if (assembler_name_hash)
198 void **aslot;
199 struct cgraph_node *cnode;
200 tree decl = node->decl;
202 tree name = DECL_ASSEMBLER_NAME (node->decl);
204 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
205 decl_assembler_name_hash (name),
206 INSERT);
207 gcc_assert (*aslot != node);
208 node->next_sharing_asm_name = (symtab_node *)*aslot;
209 if (*aslot != NULL)
210 ((symtab_node *)*aslot)->previous_sharing_asm_name = node;
211 *aslot = node;
213 /* Update also possible inline clones sharing a decl. */
214 cnode = dyn_cast <cgraph_node *> (node);
215 if (cnode && cnode->clones && with_clones)
216 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
217 if (cnode->decl == decl)
218 insert_to_assembler_name_hash (cnode, true);
223 /* Remove NODE from assembler name hash. */
225 static void
226 unlink_from_assembler_name_hash (symtab_node *node, bool with_clones)
228 if (assembler_name_hash)
230 struct cgraph_node *cnode;
231 tree decl = node->decl;
233 if (node->next_sharing_asm_name)
234 node->next_sharing_asm_name->previous_sharing_asm_name
235 = node->previous_sharing_asm_name;
236 if (node->previous_sharing_asm_name)
238 node->previous_sharing_asm_name->next_sharing_asm_name
239 = node->next_sharing_asm_name;
241 else
243 tree name = DECL_ASSEMBLER_NAME (node->decl);
244 void **slot;
245 slot = htab_find_slot_with_hash (assembler_name_hash, name,
246 decl_assembler_name_hash (name),
247 NO_INSERT);
248 gcc_assert (*slot == node);
249 if (!node->next_sharing_asm_name)
250 htab_clear_slot (assembler_name_hash, slot);
251 else
252 *slot = node->next_sharing_asm_name;
254 node->next_sharing_asm_name = NULL;
255 node->previous_sharing_asm_name = NULL;
257 /* Update also possible inline clones sharing a decl. */
258 cnode = dyn_cast <cgraph_node *> (node);
259 if (cnode && cnode->clones && with_clones)
260 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
261 if (cnode->decl == decl)
262 unlink_from_assembler_name_hash (cnode, true);
266 /* Arrange node to be first in its entry of assembler_name_hash. */
268 void
269 symtab_prevail_in_asm_name_hash (symtab_node *node)
271 unlink_from_assembler_name_hash (node, false);
272 insert_to_assembler_name_hash (node, false);
275 /* Initalize asm name hash unless. */
277 void
278 symtab_initialize_asm_name_hash (void)
280 symtab_node *node;
281 if (!assembler_name_hash)
283 assembler_name_hash =
284 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
285 NULL);
286 FOR_EACH_SYMBOL (node)
287 insert_to_assembler_name_hash (node, false);
291 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
292 Return NULL if there's no such node. */
294 symtab_node *
295 symtab_node_for_asm (const_tree asmname)
297 symtab_node *node;
298 void **slot;
300 symtab_initialize_asm_name_hash ();
301 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
302 decl_assembler_name_hash (asmname),
303 NO_INSERT);
305 if (slot)
307 node = (symtab_node *) *slot;
308 return node;
310 return NULL;
313 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
315 void
316 change_decl_assembler_name (tree decl, tree name)
318 symtab_node *node = NULL;
320 /* We can have user ASM names on things, like global register variables, that
321 are not in the symbol table. */
322 if ((TREE_CODE (decl) == VAR_DECL
323 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
324 || TREE_CODE (decl) == FUNCTION_DECL)
325 node = symtab_node::get (decl);
326 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
328 SET_DECL_ASSEMBLER_NAME (decl, name);
329 if (node)
330 insert_to_assembler_name_hash (node, true);
332 else
334 if (name == DECL_ASSEMBLER_NAME (decl))
335 return;
337 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
338 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
339 : NULL);
340 if (node)
341 unlink_from_assembler_name_hash (node, true);
342 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
343 && DECL_RTL_SET_P (decl))
344 warning (0, "%D renamed after being referenced in assembly", decl);
346 SET_DECL_ASSEMBLER_NAME (decl, name);
347 if (alias)
349 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
350 TREE_CHAIN (name) = alias;
352 if (node)
353 insert_to_assembler_name_hash (node, true);
357 /* Return true when RESOLUTION indicate that linker will use
358 the symbol from non-LTO object files. */
360 bool
361 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
363 return (resolution == LDPR_PREVAILING_DEF
364 || resolution == LDPR_PREEMPTED_REG
365 || resolution == LDPR_RESOLVED_EXEC
366 || resolution == LDPR_RESOLVED_DYN);
369 /* Hash sections by their names. */
371 static hashval_t
372 hash_section_hash_entry (const void *p)
374 const section_hash_entry *n = (const section_hash_entry *) p;
375 return htab_hash_string (n->name);
378 /* Return true if section P1 name equals to P2. */
380 static int
381 eq_sections (const void *p1, const void *p2)
383 const section_hash_entry *n1 = (const section_hash_entry *) p1;
384 const char *name = (const char *)p2;
385 return n1->name == name || !strcmp (n1->name, name);
388 /* Add node into symbol table. This function is not used directly, but via
389 cgraph/varpool node creation routines. */
391 void
392 symtab_node::register_symbol (void)
394 next = symtab_nodes;
395 previous = NULL;
396 if (symtab_nodes)
397 symtab_nodes->previous = this;
398 symtab_nodes = this;
400 if (!decl->decl_with_vis.symtab_node)
401 decl->decl_with_vis.symtab_node = this;
403 ref_list.clear ();
405 order = symtab_order++;
407 /* Be sure to do this last; C++ FE might create new nodes via
408 DECL_ASSEMBLER_NAME langhook! */
409 insert_to_assembler_name_hash (this, false);
412 /* Remove NODE from same comdat group. */
414 void
415 symtab_node::remove_from_same_comdat_group (void)
417 if (same_comdat_group)
419 symtab_node *prev;
420 for (prev = same_comdat_group;
421 prev->same_comdat_group != this;
422 prev = prev->same_comdat_group)
424 if (same_comdat_group == prev)
425 prev->same_comdat_group = NULL;
426 else
427 prev->same_comdat_group = same_comdat_group;
428 same_comdat_group = NULL;
429 set_comdat_group (NULL);
433 /* Remove node from symbol table. This function is not used directly, but via
434 cgraph/varpool node removal routines. */
436 void
437 symtab_node::unregister (void)
439 remove_all_references ();
440 remove_all_referring ();
442 /* Remove reference to section. */
443 set_section_for_node (NULL);
445 remove_from_same_comdat_group ();
447 if (previous)
448 previous->next = next;
449 else
450 symtab_nodes = next;
451 if (next)
452 next->previous = previous;
453 next = NULL;
454 previous = NULL;
456 /* During LTO symtab merging we temporarily corrupt decl to symtab node
457 hash. */
458 gcc_assert (decl->decl_with_vis.symtab_node || in_lto_p);
459 if (decl->decl_with_vis.symtab_node == this)
461 symtab_node *replacement_node = NULL;
462 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
463 replacement_node = cnode->find_replacement ();
464 decl->decl_with_vis.symtab_node = replacement_node;
466 if (!is_a <varpool_node *> (this) || !DECL_HARD_REGISTER (decl))
467 unlink_from_assembler_name_hash (this, false);
468 if (in_init_priority_hash)
470 struct symbol_priority_map in;
471 void **slot;
472 in.symbol = this;
474 slot = htab_find_slot (init_priority_hash, &in, NO_INSERT);
475 if (slot)
476 htab_clear_slot (init_priority_hash, slot);
481 /* Remove symbol from symbol table. */
483 void
484 symtab_node::remove (void)
486 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
487 cnode->remove ();
488 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
489 vnode->remove ();
492 /* Add NEW_ to the same comdat group that OLD is in. */
494 void
495 symtab_node::add_to_same_comdat_group (symtab_node *old_node)
497 gcc_assert (old_node->get_comdat_group ());
498 gcc_assert (!same_comdat_group);
499 gcc_assert (this != old_node);
501 set_comdat_group (old_node->get_comdat_group ());
502 same_comdat_group = old_node;
503 if (!old_node->same_comdat_group)
504 old_node->same_comdat_group = this;
505 else
507 symtab_node *n;
508 for (n = old_node->same_comdat_group;
509 n->same_comdat_group != old_node;
510 n = n->same_comdat_group)
512 n->same_comdat_group = this;
516 /* Dissolve the same_comdat_group list in which NODE resides. */
518 void
519 symtab_node::dissolve_same_comdat_group_list (void)
521 symtab_node *n = this;
522 symtab_node *next;
524 if (!same_comdat_group)
525 return;
528 next = n->same_comdat_group;
529 n->same_comdat_group = NULL;
530 /* Clear comdat_group for comdat locals, since
531 make_decl_local doesn't. */
532 if (!TREE_PUBLIC (n->decl))
533 n->set_comdat_group (NULL);
534 n = next;
536 while (n != this);
539 /* Return printable assembler name of NODE.
540 This function is used only for debugging. When assembler name
541 is unknown go with identifier name. */
543 const char *
544 symtab_node::asm_name () const
546 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
547 return lang_hooks.decl_printable_name (decl, 2);
548 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
551 /* Return printable identifier name. */
553 const char *
554 symtab_node::name () const
556 return lang_hooks.decl_printable_name (decl, 2);
559 /* Return ipa reference from this symtab_node to
560 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
561 of the use. */
563 struct ipa_ref *
564 symtab_node::add_reference (symtab_node *referred_node,
565 enum ipa_ref_use use_type)
567 return add_reference (referred_node, use_type, NULL);
571 /* Return ipa reference from this symtab_node to
572 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
573 of the use and STMT the statement (if it exists). */
575 struct ipa_ref *
576 symtab_node::add_reference (symtab_node *referred_node,
577 enum ipa_ref_use use_type, gimple stmt)
579 struct ipa_ref *ref = NULL, *ref2 = NULL;
580 struct ipa_ref_list *list, *list2;
581 ipa_ref_t *old_references;
583 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
584 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
586 list = &ref_list;
587 old_references = vec_safe_address (list->references);
588 vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
589 ref = &list->references->last ();
591 list2 = &referred_node->ref_list;
593 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
594 if(use_type == IPA_REF_ALIAS)
596 list2->referring.safe_insert (0, ref);
597 ref->referred_index = 0;
599 for (unsigned int i = 1; i < list2->referring.length (); i++)
600 list2->referring[i]->referred_index = i;
602 else
604 list2->referring.safe_push (ref);
605 ref->referred_index = list2->referring.length () - 1;
608 ref->referring = this;
609 ref->referred = referred_node;
610 ref->stmt = stmt;
611 ref->lto_stmt_uid = 0;
612 ref->use = use_type;
613 ref->speculative = 0;
615 /* If vector was moved in memory, update pointers. */
616 if (old_references != list->references->address ())
618 int i;
619 for (i = 0; iterate_reference(i, ref2); i++)
620 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
622 return ref;
625 /* If VAL is a reference to a function or a variable, add a reference from
626 this symtab_node to the corresponding symbol table node. USE_TYPE specify
627 type of the use and STMT the statement (if it exists). Return the new
628 reference or NULL if none was created. */
630 struct ipa_ref *
631 symtab_node::maybe_add_reference (tree val, enum ipa_ref_use use_type,
632 gimple stmt)
634 STRIP_NOPS (val);
635 if (TREE_CODE (val) != ADDR_EXPR)
636 return NULL;
637 val = get_base_var (val);
638 if (val && (TREE_CODE (val) == FUNCTION_DECL
639 || TREE_CODE (val) == VAR_DECL))
641 symtab_node *referred = symtab_node::get (val);
642 gcc_checking_assert (referred);
643 return add_reference (referred, use_type, stmt);
645 return NULL;
648 /* Clone all references from symtab NODE to this symtab_node. */
650 void
651 symtab_node::clone_references (struct symtab_node *node)
653 struct ipa_ref *ref = NULL, *ref2 = NULL;
654 int i;
655 for (i = 0; node->iterate_reference (i, ref); i++)
657 bool speculative = ref->speculative;
658 unsigned int stmt_uid = ref->lto_stmt_uid;
660 ref2 = add_reference (ref->referred, ref->use, ref->stmt);
661 ref2->speculative = speculative;
662 ref2->lto_stmt_uid = stmt_uid;
666 /* Clone all referring from symtab NODE to this symtab_node. */
668 void
669 symtab_node::clone_referring (struct symtab_node *node)
671 struct ipa_ref *ref = NULL, *ref2 = NULL;
672 int i;
673 for (i = 0; node->iterate_referring(i, ref); i++)
675 bool speculative = ref->speculative;
676 unsigned int stmt_uid = ref->lto_stmt_uid;
678 ref2 = ref->referring->add_reference (this, ref->use, ref->stmt);
679 ref2->speculative = speculative;
680 ref2->lto_stmt_uid = stmt_uid;
684 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
686 struct ipa_ref *
687 symtab_node::clone_reference (struct ipa_ref *ref, gimple stmt)
689 bool speculative = ref->speculative;
690 unsigned int stmt_uid = ref->lto_stmt_uid;
691 struct ipa_ref *ref2;
693 ref2 = add_reference (ref->referred, ref->use, stmt);
694 ref2->speculative = speculative;
695 ref2->lto_stmt_uid = stmt_uid;
696 return ref2;
699 /* Find the structure describing a reference to REFERRED_NODE
700 and associated with statement STMT. */
702 struct ipa_ref *
703 symtab_node::find_reference (symtab_node *referred_node,
704 gimple stmt, unsigned int lto_stmt_uid)
706 struct ipa_ref *r = NULL;
707 int i;
709 for (i = 0; iterate_reference (i, r); i++)
710 if (r->referred == referred_node
711 && !r->speculative
712 && ((stmt && r->stmt == stmt)
713 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
714 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
715 return r;
716 return NULL;
719 /* Remove all references that are associated with statement STMT. */
721 void
722 symtab_node::remove_stmt_references (gimple stmt)
724 struct ipa_ref *r = NULL;
725 int i = 0;
727 while (iterate_reference (i, r))
728 if (r->stmt == stmt)
729 r->remove_reference ();
730 else
731 i++;
734 /* Remove all stmt references in non-speculative references.
735 Those are not maintained during inlining & clonning.
736 The exception are speculative references that are updated along
737 with callgraph edges associated with them. */
739 void
740 symtab_node::clear_stmts_in_references (void)
742 struct ipa_ref *r = NULL;
743 int i;
745 for (i = 0; iterate_reference (i, r); i++)
746 if (!r->speculative)
748 r->stmt = NULL;
749 r->lto_stmt_uid = 0;
753 /* Remove all references in ref list. */
755 void
756 symtab_node::remove_all_references (void)
758 while (vec_safe_length (ref_list.references))
759 ref_list.references->last ().remove_reference ();
760 vec_free (ref_list.references);
763 /* Remove all referring items in ref list. */
765 void
766 symtab_node::remove_all_referring (void)
768 while (ref_list.referring.length ())
769 ref_list.referring.last ()->remove_reference ();
770 ref_list.referring.release ();
773 /* Dump references in ref list to FILE. */
775 void
776 symtab_node::dump_references (FILE *file)
778 struct ipa_ref *ref = NULL;
779 int i;
780 for (i = 0; iterate_reference (i, ref); i++)
782 fprintf (file, "%s/%i (%s)",
783 ref->referred->asm_name (),
784 ref->referred->order,
785 ipa_ref_use_name [ref->use]);
786 if (ref->speculative)
787 fprintf (file, " (speculative)");
789 fprintf (file, "\n");
792 /* Dump referring in list to FILE. */
794 void
795 symtab_node::dump_referring (FILE *file)
797 struct ipa_ref *ref = NULL;
798 int i;
799 for (i = 0; iterate_referring(i, ref); i++)
801 fprintf (file, "%s/%i (%s)",
802 ref->referring->asm_name (),
803 ref->referring->order,
804 ipa_ref_use_name [ref->use]);
805 if (ref->speculative)
806 fprintf (file, " (speculative)");
808 fprintf (file, "\n");
811 /* Return true if list contains an alias. */
812 bool
813 symtab_node::has_aliases_p (void)
815 struct ipa_ref *ref = NULL;
816 int i;
818 for (i = 0; iterate_referring (i, ref); i++)
819 if (ref->use == IPA_REF_ALIAS)
820 return true;
821 return false;
824 /* Iterates I-th reference in the list, REF is also set. */
826 struct ipa_ref *
827 symtab_node::iterate_reference (unsigned i, struct ipa_ref *&ref)
829 vec_safe_iterate (ref_list.references, i, &ref);
831 return ref;
834 /* Iterates I-th referring item in the list, REF is also set. */
836 struct ipa_ref *
837 symtab_node::iterate_referring (unsigned i, struct ipa_ref *&ref)
839 ref_list.referring.iterate (i, &ref);
841 return ref;
844 /* Iterates I-th referring alias item in the list, REF is also set. */
846 struct ipa_ref *
847 symtab_node::iterate_direct_aliases (unsigned i, struct ipa_ref *&ref)
849 ref_list.referring.iterate (i, &ref);
851 if (ref && ref->use != IPA_REF_ALIAS)
852 return NULL;
854 return ref;
857 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
859 /* Dump base fields of symtab nodes to F. Not to be used directly. */
861 void
862 symtab_node::dump_base (FILE *f)
864 static const char * const visibility_types[] = {
865 "default", "protected", "hidden", "internal"
868 fprintf (f, "%s/%i (%s)", asm_name (), order, name ());
869 dump_addr (f, " @", (void *)this);
870 fprintf (f, "\n Type: %s", symtab_type_names[type]);
872 if (definition)
873 fprintf (f, " definition");
874 if (analyzed)
875 fprintf (f, " analyzed");
876 if (alias)
877 fprintf (f, " alias");
878 if (weakref)
879 fprintf (f, " weakref");
880 if (cpp_implicit_alias)
881 fprintf (f, " cpp_implicit_alias");
882 if (alias_target)
883 fprintf (f, " target:%s",
884 DECL_P (alias_target)
885 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
886 (alias_target))
887 : IDENTIFIER_POINTER (alias_target));
888 if (body_removed)
889 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
890 fprintf (f, "\n Visibility:");
891 if (in_other_partition)
892 fprintf (f, " in_other_partition");
893 if (used_from_other_partition)
894 fprintf (f, " used_from_other_partition");
895 if (force_output)
896 fprintf (f, " force_output");
897 if (forced_by_abi)
898 fprintf (f, " forced_by_abi");
899 if (externally_visible)
900 fprintf (f, " externally_visible");
901 if (resolution != LDPR_UNKNOWN)
902 fprintf (f, " %s",
903 ld_plugin_symbol_resolution_names[(int)resolution]);
904 if (TREE_ASM_WRITTEN (decl))
905 fprintf (f, " asm_written");
906 if (DECL_EXTERNAL (decl))
907 fprintf (f, " external");
908 if (TREE_PUBLIC (decl))
909 fprintf (f, " public");
910 if (DECL_COMMON (decl))
911 fprintf (f, " common");
912 if (DECL_WEAK (decl))
913 fprintf (f, " weak");
914 if (DECL_DLLIMPORT_P (decl))
915 fprintf (f, " dll_import");
916 if (DECL_COMDAT (decl))
917 fprintf (f, " comdat");
918 if (get_comdat_group ())
919 fprintf (f, " comdat_group:%s",
920 IDENTIFIER_POINTER (get_comdat_group_id ()));
921 if (DECL_ONE_ONLY (decl))
922 fprintf (f, " one_only");
923 if (get_section ())
924 fprintf (f, " section:%s",
925 get_section ());
926 if (implicit_section)
927 fprintf (f," (implicit_section)");
928 if (DECL_VISIBILITY_SPECIFIED (decl))
929 fprintf (f, " visibility_specified");
930 if (DECL_VISIBILITY (decl))
931 fprintf (f, " visibility:%s",
932 visibility_types [DECL_VISIBILITY (decl)]);
933 if (DECL_VIRTUAL_P (decl))
934 fprintf (f, " virtual");
935 if (DECL_ARTIFICIAL (decl))
936 fprintf (f, " artificial");
937 if (TREE_CODE (decl) == FUNCTION_DECL)
939 if (DECL_STATIC_CONSTRUCTOR (decl))
940 fprintf (f, " constructor");
941 if (DECL_STATIC_DESTRUCTOR (decl))
942 fprintf (f, " destructor");
944 fprintf (f, "\n");
946 if (same_comdat_group)
947 fprintf (f, " Same comdat group as: %s/%i\n",
948 same_comdat_group->asm_name (),
949 same_comdat_group->order);
950 if (next_sharing_asm_name)
951 fprintf (f, " next sharing asm name: %i\n",
952 next_sharing_asm_name->order);
953 if (previous_sharing_asm_name)
954 fprintf (f, " previous sharing asm name: %i\n",
955 previous_sharing_asm_name->order);
957 if (address_taken)
958 fprintf (f, " Address is taken.\n");
959 if (aux)
961 fprintf (f, " Aux:");
962 dump_addr (f, " @", (void *)aux);
965 fprintf (f, " References: ");
966 dump_references (f);
967 fprintf (f, " Referring: ");
968 dump_referring (f);
969 if (lto_file_data)
970 fprintf (f, " Read from file: %s\n",
971 lto_file_data->file_name);
974 /* Dump symtab node to F. */
976 void
977 symtab_node::dump (FILE *f)
979 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
980 cnode->dump (f);
981 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
982 vnode->dump (f);
985 /* Dump symbol table to F. */
987 void
988 symtab_node::dump_table (FILE *f)
990 symtab_node *node;
991 fprintf (f, "Symbol table:\n\n");
992 FOR_EACH_SYMBOL (node)
993 node->dump (f);
996 /* Dump symtab node NODE to stderr. */
998 DEBUG_FUNCTION void
999 symtab_node::debug (void)
1001 dump (stderr);
1004 /* Verify common part of symtab nodes. */
1006 DEBUG_FUNCTION bool
1007 symtab_node::verify_base (void)
1009 bool error_found = false;
1010 symtab_node *hashed_node;
1012 if (is_a <cgraph_node *> (this))
1014 if (TREE_CODE (decl) != FUNCTION_DECL)
1016 error ("function symbol is not function");
1017 error_found = true;
1020 else if (is_a <varpool_node *> (this))
1022 if (TREE_CODE (decl) != VAR_DECL)
1024 error ("variable symbol is not variable");
1025 error_found = true;
1028 else
1030 error ("node has unknown type");
1031 error_found = true;
1034 if (cgraph_state != CGRAPH_LTO_STREAMING)
1036 hashed_node = symtab_node::get (decl);
1037 if (!hashed_node)
1039 error ("node not found node->decl->decl_with_vis.symtab_node");
1040 error_found = true;
1042 if (hashed_node != this
1043 && (!is_a <cgraph_node *> (this)
1044 || !dyn_cast <cgraph_node *> (this)->clone_of
1045 || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl))
1047 error ("node differs from node->decl->decl_with_vis.symtab_node");
1048 error_found = true;
1051 if (assembler_name_hash)
1053 hashed_node = symtab_node_for_asm (DECL_ASSEMBLER_NAME (decl));
1054 if (hashed_node && hashed_node->previous_sharing_asm_name)
1056 error ("assembler name hash list corrupted");
1057 error_found = true;
1059 while (hashed_node)
1061 if (hashed_node == this)
1062 break;
1063 hashed_node = hashed_node->next_sharing_asm_name;
1065 if (!hashed_node
1066 && !(is_a <varpool_node *> (this)
1067 || DECL_HARD_REGISTER (decl)))
1069 error ("node not found in symtab assembler name hash");
1070 error_found = true;
1073 if (previous_sharing_asm_name
1074 && previous_sharing_asm_name->next_sharing_asm_name != this)
1076 error ("double linked list of assembler names corrupted");
1077 error_found = true;
1079 if (analyzed && !definition)
1081 error ("node is analyzed byt it is not a definition");
1082 error_found = true;
1084 if (cpp_implicit_alias && !alias)
1086 error ("node is alias but not implicit alias");
1087 error_found = true;
1089 if (alias && !definition && !weakref)
1091 error ("node is alias but not definition");
1092 error_found = true;
1094 if (weakref && !alias)
1096 error ("node is weakref but not an alias");
1097 error_found = true;
1099 if (same_comdat_group)
1101 symtab_node *n = same_comdat_group;
1103 if (!n->get_comdat_group ())
1105 error ("node is in same_comdat_group list but has no comdat_group");
1106 error_found = true;
1108 if (n->get_comdat_group () != get_comdat_group ())
1110 error ("same_comdat_group list across different groups");
1111 error_found = true;
1113 if (!n->definition)
1115 error ("Node has same_comdat_group but it is not a definition");
1116 error_found = true;
1118 if (n->type != type)
1120 error ("mixing different types of symbol in same comdat groups is not supported");
1121 error_found = true;
1123 if (n == this)
1125 error ("node is alone in a comdat group");
1126 error_found = true;
1130 if (!n->same_comdat_group)
1132 error ("same_comdat_group is not a circular list");
1133 error_found = true;
1134 break;
1136 n = n->same_comdat_group;
1138 while (n != this);
1139 if (comdat_local_p ())
1141 struct ipa_ref *ref = NULL;
1143 for (int i = 0; iterate_referring (i, ref); ++i)
1145 if (!in_same_comdat_group_p (ref->referring))
1147 error ("comdat-local symbol referred to by %s outside its "
1148 "comdat",
1149 identifier_to_locale (ref->referring->name()));
1150 error_found = true;
1155 if (implicit_section && !get_section ())
1157 error ("implicit_section flag is set but section isn't");
1158 error_found = true;
1160 if (get_section () && get_comdat_group ()
1161 && !implicit_section)
1163 error ("Both section and comdat group is set");
1164 error_found = true;
1166 /* TODO: Add string table for sections, so we do not keep holding duplicated
1167 strings. */
1168 if (alias && definition
1169 && get_section () != get_alias_target ()->get_section ()
1170 && (!get_section()
1171 || !get_alias_target ()->get_section ()
1172 || strcmp (get_section(),
1173 get_alias_target ()->get_section ())))
1175 error ("Alias and target's section differs");
1176 get_alias_target ()->dump (stderr);
1177 error_found = true;
1179 if (alias && definition
1180 && get_comdat_group () != get_alias_target ()->get_comdat_group ())
1182 error ("Alias and target's comdat groups differs");
1183 get_alias_target ()->dump (stderr);
1184 error_found = true;
1187 return error_found;
1190 /* Verify consistency of NODE. */
1192 DEBUG_FUNCTION void
1193 symtab_node::verify (void)
1195 if (seen_error ())
1196 return;
1198 timevar_push (TV_CGRAPH_VERIFY);
1199 if (cgraph_node *node = dyn_cast <cgraph_node *> (this))
1200 node->verify_node ();
1201 else
1202 if (verify_base ())
1204 debug ();
1205 internal_error ("symtab_node::verify failed");
1207 timevar_pop (TV_CGRAPH_VERIFY);
1210 /* Verify symbol table for internal consistency. */
1212 DEBUG_FUNCTION void
1213 symtab_node::verify_symtab_nodes (void)
1215 symtab_node *node;
1216 hash_map<tree, symtab_node *> comdat_head_map (251);
1218 FOR_EACH_SYMBOL (node)
1220 node->verify ();
1221 if (node->get_comdat_group ())
1223 symtab_node **entry, *s;
1224 bool existed;
1226 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1227 &existed);
1228 if (!existed)
1229 *entry = node;
1230 else
1231 for (s = (*entry)->same_comdat_group; s != NULL && s != node; s = s->same_comdat_group)
1232 if (!s || s == *entry)
1234 error ("Two symbols with same comdat_group are not linked by the same_comdat_group list.");
1235 (*entry)->debug ();
1236 node->debug ();
1237 internal_error ("symtab_node::verify failed");
1243 /* Return true when NODE is known to be used from other (non-LTO)
1244 object file. Known only when doing LTO via linker plugin. */
1246 bool
1247 symtab_node::used_from_object_file_p_worker (symtab_node *node)
1249 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
1250 return false;
1251 if (resolution_used_from_other_file_p (node->resolution))
1252 return true;
1253 return false;
1257 /* Return true when symtab_node is known to be used from other (non-LTO)
1258 object file. Known only when doing LTO via linker plugin. */
1260 bool
1261 symtab_node::used_from_object_file_p (void)
1263 return symtab_node::used_from_object_file_p_worker (this);
1266 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1267 but other code such as notice_global_symbol generates rtl. */
1269 void
1270 symtab_node::make_decl_local (void)
1272 rtx rtl, symbol;
1274 /* Avoid clearing comdat_groups on comdat-local decls. */
1275 if (TREE_PUBLIC (decl) == 0)
1276 return;
1278 if (TREE_CODE (decl) == VAR_DECL)
1279 DECL_COMMON (decl) = 0;
1280 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1282 DECL_COMDAT (decl) = 0;
1283 DECL_WEAK (decl) = 0;
1284 DECL_EXTERNAL (decl) = 0;
1285 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1286 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1287 TREE_PUBLIC (decl) = 0;
1288 if (!DECL_RTL_SET_P (decl))
1289 return;
1291 /* Update rtl flags. */
1292 make_decl_rtl (decl);
1294 rtl = DECL_RTL (decl);
1295 if (!MEM_P (rtl))
1296 return;
1298 symbol = XEXP (rtl, 0);
1299 if (GET_CODE (symbol) != SYMBOL_REF)
1300 return;
1302 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1305 /* Walk the alias chain to return the symbol NODE is alias of.
1306 If NODE is not an alias, return NODE.
1307 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
1309 symtab_node *
1310 symtab_node::ultimate_alias_target (enum availability *availability)
1312 bool weakref_p = false;
1314 if (!alias)
1316 if (availability)
1317 *availability = get_availability ();
1318 return this;
1321 /* To determine visibility of the target, we follow ELF semantic of aliases.
1322 Here alias is an alternative assembler name of a given definition. Its
1323 availability prevails the availability of its target (i.e. static alias of
1324 weak definition is available.
1326 Weakref is a different animal (and not part of ELF per se). It is just
1327 alternative name of a given symbol used within one complation unit
1328 and is translated prior hitting the object file. It inherits the
1329 visibility of its target (i.e. weakref of non-overwritable definition
1330 is non-overwritable, while weakref of weak definition is weak).
1332 If we ever get into supporting targets with different semantics, a target
1333 hook will be needed here. */
1335 if (availability)
1337 weakref_p = weakref;
1338 if (!weakref_p)
1339 *availability = get_availability ();
1340 else
1341 *availability = AVAIL_LOCAL;
1344 symtab_node *node = this;
1345 while (node)
1347 if (node->alias && node->analyzed)
1348 node = node->get_alias_target ();
1349 else
1351 if (!availability)
1353 else if (node->analyzed)
1355 if (weakref_p)
1357 enum availability a = node->get_availability ();
1358 if (a < *availability)
1359 *availability = a;
1362 else
1363 *availability = AVAIL_NOT_AVAILABLE;
1364 return node;
1366 if (node && availability && weakref_p)
1368 enum availability a = node->get_availability ();
1369 if (a < *availability)
1370 *availability = a;
1371 weakref_p = node->weakref;
1374 if (availability)
1375 *availability = AVAIL_NOT_AVAILABLE;
1376 return NULL;
1379 /* C++ FE sometimes change linkage flags after producing same body aliases.
1381 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1382 are obviously equivalent. The way it is doing so is however somewhat
1383 kludgy and interferes with the visibility code. As a result we need to
1384 copy the visibility from the target to get things right. */
1386 void
1387 symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target)
1389 if (is_a <cgraph_node *> (this))
1391 DECL_DECLARED_INLINE_P (decl)
1392 = DECL_DECLARED_INLINE_P (target->decl);
1393 DECL_DISREGARD_INLINE_LIMITS (decl)
1394 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1396 /* FIXME: It is not really clear why those flags should not be copied for
1397 functions, too. */
1398 else
1400 DECL_WEAK (decl) = DECL_WEAK (target->decl);
1401 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1402 DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl);
1404 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (target->decl);
1405 if (TREE_PUBLIC (decl))
1407 tree group;
1409 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1410 DECL_COMDAT (decl) = DECL_COMDAT (target->decl);
1411 group = target->get_comdat_group ();
1412 set_comdat_group (group);
1413 if (group && !same_comdat_group)
1414 add_to_same_comdat_group (target);
1416 externally_visible = target->externally_visible;
1419 /* Set section, do not recurse into aliases.
1420 When one wants to change section of symbol and its aliases,
1421 use set_section. */
1423 void
1424 symtab_node::set_section_for_node (const char *section)
1426 const char *current = get_section ();
1427 void **slot;
1429 if (current == section
1430 || (current && section
1431 && !strcmp (current, section)))
1432 return;
1434 if (current)
1436 x_section->ref_count--;
1437 if (!x_section->ref_count)
1439 slot = htab_find_slot_with_hash (section_hash, x_section->name,
1440 htab_hash_string (x_section->name),
1441 INSERT);
1442 ggc_free (x_section);
1443 htab_clear_slot (section_hash, slot);
1445 x_section = NULL;
1447 if (!section)
1449 implicit_section = false;
1450 return;
1452 if (!section_hash)
1453 section_hash = htab_create_ggc (10, hash_section_hash_entry,
1454 eq_sections, NULL);
1455 slot = htab_find_slot_with_hash (section_hash, section,
1456 htab_hash_string (section),
1457 INSERT);
1458 if (*slot)
1459 x_section = (section_hash_entry *)*slot;
1460 else
1462 int len = strlen (section);
1463 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1464 x_section->name = ggc_vec_alloc<char> (len + 1);
1465 memcpy (x_section->name, section, len + 1);
1467 x_section->ref_count++;
1470 /* Worker for set_section. */
1472 bool
1473 symtab_node::set_section (symtab_node *n, void *s)
1475 n->set_section_for_node ((char *)s);
1476 return false;
1479 /* Set section of symbol and its aliases. */
1481 void
1482 symtab_node::set_section (const char *section)
1484 gcc_assert (!this->alias);
1485 call_for_symbol_and_aliases
1486 (symtab_node::set_section, const_cast<char *>(section), true);
1489 /* Return the initialization priority. */
1491 priority_type
1492 symtab_node::get_init_priority ()
1494 struct symbol_priority_map *h;
1495 struct symbol_priority_map in;
1497 if (!this->in_init_priority_hash)
1498 return DEFAULT_INIT_PRIORITY;
1499 in.symbol = this;
1500 h = (struct symbol_priority_map *) htab_find (init_priority_hash, &in);
1501 return h ? h->init : DEFAULT_INIT_PRIORITY;
1504 /* Return availability of NODE. */
1505 enum availability symtab_node::get_availability (void)
1507 if (is_a <cgraph_node *> (this))
1508 return dyn_cast <cgraph_node *> (this)->get_availability ();
1509 else
1510 return dyn_cast <varpool_node *> (this)->get_availability ();;
1514 /* Return the finalization priority. */
1516 priority_type
1517 cgraph_node::get_fini_priority ()
1519 struct symbol_priority_map *h;
1520 struct symbol_priority_map in;
1522 if (!this->in_init_priority_hash)
1523 return DEFAULT_INIT_PRIORITY;
1524 in.symbol = this;
1525 h = (struct symbol_priority_map *) htab_find (init_priority_hash, &in);
1526 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1529 /* Return true if the from tree in both priority maps are equal. */
1532 symbol_priority_map_eq (const void *va, const void *vb)
1534 const struct symbol_priority_map *const a = (const struct symbol_priority_map *) va,
1535 *const b = (const struct symbol_priority_map *) vb;
1536 return (a->symbol == b->symbol);
1539 /* Hash a from symbol in a symbol_priority_map. */
1541 unsigned int
1542 symbol_priority_map_hash (const void *item)
1544 return htab_hash_pointer (((const struct symbol_priority_map *)item)->symbol);
1547 /* Return the initialization and finalization priority information for
1548 DECL. If there is no previous priority information, a freshly
1549 allocated structure is returned. */
1551 struct symbol_priority_map *
1552 symtab_node::priority_info (void)
1554 struct symbol_priority_map in;
1555 struct symbol_priority_map *h;
1556 void **loc;
1558 in.symbol = this;
1559 if (!init_priority_hash)
1560 init_priority_hash = htab_create_ggc (512, symbol_priority_map_hash,
1561 symbol_priority_map_eq, 0);
1563 loc = htab_find_slot (init_priority_hash, &in, INSERT);
1564 h = (struct symbol_priority_map *) *loc;
1565 if (!h)
1567 h = ggc_cleared_alloc<symbol_priority_map> ();
1568 *loc = h;
1569 h->symbol = this;
1570 h->init = DEFAULT_INIT_PRIORITY;
1571 h->fini = DEFAULT_INIT_PRIORITY;
1572 in_init_priority_hash = true;
1575 return h;
1578 /* Set initialization priority to PRIORITY. */
1580 void
1581 symtab_node::set_init_priority (priority_type priority)
1583 struct symbol_priority_map *h;
1585 if (is_a <cgraph_node *> (this))
1586 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1588 if (priority == DEFAULT_INIT_PRIORITY)
1590 gcc_assert (get_init_priority() == priority);
1591 return;
1593 h = priority_info ();
1594 h->init = priority;
1597 /* Set fialization priority to PRIORITY. */
1599 void
1600 cgraph_node::set_fini_priority (priority_type priority)
1602 struct symbol_priority_map *h;
1604 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1606 if (priority == DEFAULT_INIT_PRIORITY)
1608 gcc_assert (get_fini_priority() == priority);
1609 return;
1611 h = priority_info ();
1612 h->fini = priority;
1615 /* Worker for symtab_resolve_alias. */
1617 bool
1618 symtab_node::set_implicit_section (symtab_node *n,
1619 void *data ATTRIBUTE_UNUSED)
1621 n->implicit_section = true;
1622 return false;
1625 /* Add reference recording that symtab node is alias of TARGET.
1626 The function can fail in the case of aliasing cycles; in this case
1627 it returns false. */
1629 bool
1630 symtab_node::resolve_alias (symtab_node *target)
1632 symtab_node *n;
1634 gcc_assert (!analyzed && !vec_safe_length (ref_list.references));
1636 /* Never let cycles to creep into the symbol table alias references;
1637 those will make alias walkers to be infinite. */
1638 for (n = target; n && n->alias;
1639 n = n->analyzed ? n->get_alias_target () : NULL)
1640 if (n == this)
1642 if (is_a <cgraph_node *> (this))
1643 error ("function %q+D part of alias cycle", decl);
1644 else if (is_a <varpool_node *> (this))
1645 error ("variable %q+D part of alias cycle", decl);
1646 else
1647 gcc_unreachable ();
1648 alias = false;
1649 return false;
1652 /* "analyze" the node - i.e. mark the reference. */
1653 definition = true;
1654 alias = true;
1655 analyzed = true;
1656 add_reference (target, IPA_REF_ALIAS, NULL);
1658 /* Add alias into the comdat group of its target unless it is already there. */
1659 if (same_comdat_group)
1660 remove_from_same_comdat_group ();
1661 set_comdat_group (NULL);
1662 if (target->get_comdat_group ())
1663 add_to_same_comdat_group (target);
1665 if ((get_section () != target->get_section ()
1666 || target->get_comdat_group ()) && get_section () && !implicit_section)
1668 error ("section of alias %q+D must match section of its target", decl);
1670 call_for_symbol_and_aliases (symtab_node::set_section,
1671 const_cast<char *>(target->get_section ()), true);
1672 if (target->implicit_section)
1673 call_for_symbol_and_aliases (set_implicit_section, NULL, true);
1675 /* Alias targets become redundant after alias is resolved into an reference.
1676 We do not want to keep it around or we would have to mind updating them
1677 when renaming symbols. */
1678 alias_target = NULL;
1680 if (cpp_implicit_alias && cgraph_state >= CGRAPH_STATE_CONSTRUCTION)
1681 fixup_same_cpp_alias_visibility (target);
1683 /* If alias has address taken, so does the target. */
1684 if (address_taken)
1685 target->ultimate_alias_target ()->address_taken = true;
1686 return true;
1689 /* Call calback on symtab node and aliases associated to this node.
1690 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1691 skipped. */
1693 bool
1694 symtab_node::call_for_symbol_and_aliases (bool (*callback) (symtab_node *,
1695 void *),
1696 void *data, bool include_overwritable)
1698 int i;
1699 struct ipa_ref *ref;
1701 if (callback (this, data))
1702 return true;
1703 for (i = 0; iterate_referring (i, ref); i++)
1704 if (ref->use == IPA_REF_ALIAS)
1706 symtab_node *alias = ref->referring;
1707 if (include_overwritable
1708 || alias->get_availability () > AVAIL_INTERPOSABLE)
1709 if (alias->call_for_symbol_and_aliases (callback, data,
1710 include_overwritable))
1711 return true;
1713 return false;
1716 /* Worker searching noninterposable alias. */
1718 bool
1719 symtab_node::noninterposable_alias (symtab_node *node, void *data)
1721 if (decl_binds_to_current_def_p (node->decl))
1723 symtab_node *fn = node->ultimate_alias_target ();
1725 /* Ensure that the alias is well formed this may not be the case
1726 of user defined aliases and currently it is not always the case
1727 of C++ same body aliases (that is a bug). */
1728 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
1729 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
1730 || (TREE_CODE (node->decl) == FUNCTION_DECL
1731 && flags_from_decl_or_type (node->decl)
1732 != flags_from_decl_or_type (fn->decl))
1733 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
1734 return false;
1736 *(symtab_node **)data = node;
1737 return true;
1739 return false;
1742 /* If node can not be overwriten by static or dynamic linker to point to
1743 different definition, return NODE. Otherwise look for alias with such
1744 property and if none exists, introduce new one. */
1746 symtab_node *
1747 symtab_node::noninterposable_alias (void)
1749 tree new_decl;
1750 symtab_node *new_node = NULL;
1752 /* First try to look up existing alias or base object
1753 (if that is already non-overwritable). */
1754 symtab_node *node = ultimate_alias_target ();
1755 gcc_assert (!node->alias && !node->weakref);
1756 node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias,
1757 (void *)&new_node, true);
1758 if (new_node)
1759 return new_node;
1760 #ifndef ASM_OUTPUT_DEF
1761 /* If aliases aren't supported by the assembler, fail. */
1762 return NULL;
1763 #endif
1765 /* Otherwise create a new one. */
1766 new_decl = copy_node (node->decl);
1767 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1768 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1769 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1770 DECL_INITIAL (new_decl) = NULL;
1771 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1772 SET_DECL_RTL (new_decl, NULL);
1774 /* Update the properties. */
1775 DECL_EXTERNAL (new_decl) = 0;
1776 TREE_PUBLIC (new_decl) = 0;
1777 DECL_COMDAT (new_decl) = 0;
1778 DECL_WEAK (new_decl) = 0;
1780 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1781 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1782 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1784 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1785 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1786 new_node = cgraph_node::create_alias (new_decl, node->decl);
1788 else
1790 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1791 DECL_INITIAL (new_decl) = error_mark_node;
1792 new_node = varpool_node::create_alias (new_decl, node->decl);
1794 new_node->resolve_alias (node);
1795 gcc_assert (decl_binds_to_current_def_p (new_decl)
1796 && targetm.binds_local_p (new_decl));
1797 return new_node;
1800 /* Return true if symtab node and TARGET represents
1801 semantically equivalent symbols. */
1803 bool
1804 symtab_node::semantically_equivalent_p (symtab_node *target)
1806 enum availability avail;
1807 symtab_node *ba;
1808 symtab_node *bb;
1810 /* Equivalent functions are equivalent. */
1811 if (decl == target->decl)
1812 return true;
1814 /* If symbol is not overwritable by different implementation,
1815 walk to the base object it defines. */
1816 ba = ultimate_alias_target (&avail);
1817 if (avail >= AVAIL_AVAILABLE)
1819 if (target == ba)
1820 return true;
1822 else
1823 ba = this;
1824 bb = target->ultimate_alias_target (&avail);
1825 if (avail >= AVAIL_AVAILABLE)
1827 if (this == bb)
1828 return true;
1830 else
1831 bb = target;
1832 return bb == ba;
1835 /* Classify symbol symtab node for partitioning. */
1837 enum symbol_partitioning_class
1838 symtab_node::get_partitioning_class (void)
1840 /* Inline clones are always duplicated.
1841 This include external delcarations. */
1842 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
1844 if (DECL_ABSTRACT (decl))
1845 return SYMBOL_EXTERNAL;
1847 if (cnode && cnode->global.inlined_to)
1848 return SYMBOL_DUPLICATE;
1850 /* Weakref aliases are always duplicated. */
1851 if (weakref)
1852 return SYMBOL_DUPLICATE;
1854 /* External declarations are external. */
1855 if (DECL_EXTERNAL (decl))
1856 return SYMBOL_EXTERNAL;
1858 if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
1860 /* Constant pool references use local symbol names that can not
1861 be promoted global. We should never put into a constant pool
1862 objects that can not be duplicated across partitions. */
1863 if (DECL_IN_CONSTANT_POOL (decl))
1864 return SYMBOL_DUPLICATE;
1865 gcc_checking_assert (vnode->definition);
1867 /* Functions that are cloned may stay in callgraph even if they are unused.
1868 Handle them as external; compute_ltrans_boundary take care to make
1869 proper things to happen (i.e. to make them appear in the boundary but
1870 with body streamed, so clone can me materialized). */
1871 else if (!dyn_cast <cgraph_node *> (this)->definition)
1872 return SYMBOL_EXTERNAL;
1874 /* Linker discardable symbols are duplicated to every use unless they are
1875 keyed. */
1876 if (DECL_ONE_ONLY (decl)
1877 && !force_output
1878 && !forced_by_abi
1879 && !used_from_object_file_p ())
1880 return SYMBOL_DUPLICATE;
1882 return SYMBOL_PARTITION;
1885 /* Return true when symbol is known to be non-zero. */
1887 bool
1888 symtab_node::nonzero_address ()
1890 /* Weakrefs may be NULL when their target is not defined. */
1891 if (this->alias && this->weakref)
1893 if (this->analyzed)
1895 symtab_node *target = ultimate_alias_target ();
1897 if (target->alias && target->weakref)
1898 return false;
1899 /* We can not recurse to target::nonzero. It is possible that the
1900 target is used only via the alias.
1901 We may walk references and look for strong use, but we do not know
1902 if this strong use will survive to final binary, so be
1903 conservative here.
1904 ??? Maybe we could do the lookup during late optimization that
1905 could be useful to eliminate the NULL pointer checks in LTO
1906 programs. */
1907 if (target->definition && !DECL_EXTERNAL (target->decl))
1908 return true;
1909 if (target->resolution != LDPR_UNKNOWN
1910 && target->resolution != LDPR_UNDEF
1911 && flag_delete_null_pointer_checks)
1912 return true;
1913 return false;
1915 else
1916 return false;
1919 /* With !flag_delete_null_pointer_checks we assume that symbols may
1920 bind to NULL. This is on by default on embedded targets only.
1922 Otherwise all non-WEAK symbols must be defined and thus non-NULL or
1923 linking fails. Important case of WEAK we want to do well are comdats.
1924 Those are handled by later check for definition.
1926 When parsing, beware the cases when WEAK attribute is added later. */
1927 if (!DECL_WEAK (this->decl)
1928 && flag_delete_null_pointer_checks
1929 && cgraph_state > CGRAPH_STATE_PARSING)
1930 return true;
1932 /* If target is defined and not extern, we know it will be output and thus
1933 it will bind to non-NULL.
1934 Play safe for flag_delete_null_pointer_checks where weak definition maye
1935 be re-defined by NULL. */
1936 if (this->definition && !DECL_EXTERNAL (this->decl)
1937 && (flag_delete_null_pointer_checks || !DECL_WEAK (this->decl)))
1938 return true;
1940 /* As the last resort, check the resolution info. */
1941 if (this->resolution != LDPR_UNKNOWN
1942 && this->resolution != LDPR_UNDEF
1943 && flag_delete_null_pointer_checks)
1944 return true;
1945 return false;
1947 #include "gt-symtab.h"