* config/xtensa/predicates.md (call expander): Update for
[official-gcc.git] / gcc / symtab.c
blobdeb317d1fb399f5f169f4c58e9b49f1a2d2ffa0b
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);
276 /* Add node into symbol table. This function is not used directly, but via
277 cgraph/varpool node creation routines. */
279 void
280 symtab_register_node (symtab_node *node)
282 node->next = symtab_nodes;
283 node->previous = NULL;
284 if (symtab_nodes)
285 symtab_nodes->previous = node;
286 symtab_nodes = node;
288 if (!node->decl->decl_with_vis.symtab_node)
289 node->decl->decl_with_vis.symtab_node = node;
291 node->ref_list.clear ();
293 node->order = symtab_order++;
295 /* Be sure to do this last; C++ FE might create new nodes via
296 DECL_ASSEMBLER_NAME langhook! */
297 insert_to_assembler_name_hash (node, false);
300 /* Remove NODE from same comdat group. */
302 void
303 symtab_remove_from_same_comdat_group (symtab_node *node)
305 if (node->same_comdat_group)
307 symtab_node *prev;
308 for (prev = node->same_comdat_group;
309 prev->same_comdat_group != node;
310 prev = prev->same_comdat_group)
312 if (node->same_comdat_group == prev)
313 prev->same_comdat_group = NULL;
314 else
315 prev->same_comdat_group = node->same_comdat_group;
316 node->same_comdat_group = NULL;
320 /* Remove node from symbol table. This function is not used directly, but via
321 cgraph/varpool node removal routines. */
323 void
324 symtab_unregister_node (symtab_node *node)
326 node->remove_all_references ();
327 node->remove_all_referring ();
329 /* Remove reference to section. */
330 node->set_section_for_node (NULL);
332 symtab_remove_from_same_comdat_group (node);
334 if (node->previous)
335 node->previous->next = node->next;
336 else
337 symtab_nodes = node->next;
338 if (node->next)
339 node->next->previous = node->previous;
340 node->next = NULL;
341 node->previous = NULL;
343 /* During LTO symtab merging we temporarily corrupt decl to symtab node
344 hash. */
345 gcc_assert (node->decl->decl_with_vis.symtab_node || in_lto_p);
346 if (node->decl->decl_with_vis.symtab_node == node)
348 symtab_node *replacement_node = NULL;
349 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
350 replacement_node = cgraph_find_replacement_node (cnode);
351 node->decl->decl_with_vis.symtab_node = replacement_node;
353 if (!is_a <varpool_node *> (node) || !DECL_HARD_REGISTER (node->decl))
354 unlink_from_assembler_name_hash (node, false);
355 if (node->in_init_priority_hash)
357 struct symbol_priority_map in;
358 void **slot;
359 in.symbol = node;
361 slot = htab_find_slot (init_priority_hash, &in, NO_INSERT);
362 if (slot)
363 htab_clear_slot (init_priority_hash, slot);
368 /* Remove symtab NODE from the symbol table. */
370 void
371 symtab_remove_node (symtab_node *node)
373 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
374 cgraph_remove_node (cnode);
375 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
376 varpool_remove_node (vnode);
379 /* Initalize asm name hash unless. */
381 void
382 symtab_initialize_asm_name_hash (void)
384 symtab_node *node;
385 if (!assembler_name_hash)
387 assembler_name_hash =
388 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
389 NULL);
390 FOR_EACH_SYMBOL (node)
391 insert_to_assembler_name_hash (node, false);
395 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
396 Return NULL if there's no such node. */
398 symtab_node *
399 symtab_node_for_asm (const_tree asmname)
401 symtab_node *node;
402 void **slot;
404 symtab_initialize_asm_name_hash ();
405 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
406 decl_assembler_name_hash (asmname),
407 NO_INSERT);
409 if (slot)
411 node = (symtab_node *) *slot;
412 return node;
414 return NULL;
417 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
419 void
420 change_decl_assembler_name (tree decl, tree name)
422 symtab_node *node = NULL;
424 /* We can have user ASM names on things, like global register variables, that
425 are not in the symbol table. */
426 if ((TREE_CODE (decl) == VAR_DECL
427 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
428 || TREE_CODE (decl) == FUNCTION_DECL)
429 node = symtab_get_node (decl);
430 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
432 SET_DECL_ASSEMBLER_NAME (decl, name);
433 if (node)
434 insert_to_assembler_name_hash (node, true);
436 else
438 if (name == DECL_ASSEMBLER_NAME (decl))
439 return;
441 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
442 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
443 : NULL);
444 if (node)
445 unlink_from_assembler_name_hash (node, true);
446 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
447 && DECL_RTL_SET_P (decl))
448 warning (0, "%D renamed after being referenced in assembly", decl);
450 SET_DECL_ASSEMBLER_NAME (decl, name);
451 if (alias)
453 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
454 TREE_CHAIN (name) = alias;
456 if (node)
457 insert_to_assembler_name_hash (node, true);
461 /* Add NEW_ to the same comdat group that OLD is in. */
463 void
464 symtab_add_to_same_comdat_group (symtab_node *new_node,
465 symtab_node *old_node)
467 gcc_assert (old_node->get_comdat_group ());
468 gcc_assert (!new_node->same_comdat_group);
469 gcc_assert (new_node != old_node);
471 new_node->set_comdat_group (old_node->get_comdat_group ());
472 new_node->same_comdat_group = old_node;
473 if (!old_node->same_comdat_group)
474 old_node->same_comdat_group = new_node;
475 else
477 symtab_node *n;
478 for (n = old_node->same_comdat_group;
479 n->same_comdat_group != old_node;
480 n = n->same_comdat_group)
482 n->same_comdat_group = new_node;
486 /* Dissolve the same_comdat_group list in which NODE resides. */
488 void
489 symtab_dissolve_same_comdat_group_list (symtab_node *node)
491 symtab_node *n = node;
492 symtab_node *next;
494 if (!node->same_comdat_group)
495 return;
498 next = n->same_comdat_group;
499 n->same_comdat_group = NULL;
500 /* Clear comdat_group for comdat locals, since
501 make_decl_local doesn't. */
502 if (!TREE_PUBLIC (n->decl))
503 n->set_comdat_group (NULL);
504 n = next;
506 while (n != node);
509 /* Return printable assembler name of NODE.
510 This function is used only for debugging. When assembler name
511 is unknown go with identifier name. */
513 const char *
514 symtab_node::asm_name () const
516 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
517 return lang_hooks.decl_printable_name (decl, 2);
518 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
521 /* Return printable identifier name. */
523 const char *
524 symtab_node::name () const
526 return lang_hooks.decl_printable_name (decl, 2);
529 /* Return ipa reference from this symtab_node to
530 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
531 of the use. */
533 struct ipa_ref *
534 symtab_node::add_reference (symtab_node *referred_node,
535 enum ipa_ref_use use_type)
537 return add_reference (referred_node, use_type, NULL);
541 /* Return ipa reference from this symtab_node to
542 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
543 of the use and STMT the statement (if it exists). */
545 struct ipa_ref *
546 symtab_node::add_reference (symtab_node *referred_node,
547 enum ipa_ref_use use_type, gimple stmt)
549 struct ipa_ref *ref = NULL, *ref2 = NULL;
550 struct ipa_ref_list *list, *list2;
551 ipa_ref_t *old_references;
553 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
554 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
556 list = &ref_list;
557 old_references = vec_safe_address (list->references);
558 vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
559 ref = &list->references->last ();
561 list2 = &referred_node->ref_list;
563 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
564 if(use_type == IPA_REF_ALIAS)
566 list2->referring.safe_insert (0, ref);
567 ref->referred_index = 0;
569 for (unsigned int i = 1; i < list2->referring.length (); i++)
570 list2->referring[i]->referred_index = i;
572 else
574 list2->referring.safe_push (ref);
575 ref->referred_index = list2->referring.length () - 1;
578 ref->referring = this;
579 ref->referred = referred_node;
580 ref->stmt = stmt;
581 ref->lto_stmt_uid = 0;
582 ref->use = use_type;
583 ref->speculative = 0;
585 /* If vector was moved in memory, update pointers. */
586 if (old_references != list->references->address ())
588 int i;
589 for (i = 0; iterate_reference(i, ref2); i++)
590 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
592 return ref;
595 /* If VAL is a reference to a function or a variable, add a reference from
596 this symtab_node to the corresponding symbol table node. USE_TYPE specify
597 type of the use and STMT the statement (if it exists). Return the new
598 reference or NULL if none was created. */
600 struct ipa_ref *
601 symtab_node::maybe_add_reference (tree val, enum ipa_ref_use use_type,
602 gimple stmt)
604 STRIP_NOPS (val);
605 if (TREE_CODE (val) != ADDR_EXPR)
606 return NULL;
607 val = get_base_var (val);
608 if (val && (TREE_CODE (val) == FUNCTION_DECL
609 || TREE_CODE (val) == VAR_DECL))
611 symtab_node *referred = symtab_get_node (val);
612 gcc_checking_assert (referred);
613 return add_reference (referred, use_type, stmt);
615 return NULL;
618 /* Clone all references from symtab NODE to this symtab_node. */
620 void
621 symtab_node::clone_references (struct symtab_node *node)
623 struct ipa_ref *ref = NULL, *ref2 = NULL;
624 int i;
625 for (i = 0; node->iterate_reference (i, ref); i++)
627 bool speculative = ref->speculative;
628 unsigned int stmt_uid = ref->lto_stmt_uid;
630 ref2 = add_reference (ref->referred, ref->use, ref->stmt);
631 ref2->speculative = speculative;
632 ref2->lto_stmt_uid = stmt_uid;
636 /* Clone all referring from symtab NODE to this symtab_node. */
638 void
639 symtab_node::clone_referring (struct symtab_node *node)
641 struct ipa_ref *ref = NULL, *ref2 = NULL;
642 int i;
643 for (i = 0; node->iterate_referring(i, ref); i++)
645 bool speculative = ref->speculative;
646 unsigned int stmt_uid = ref->lto_stmt_uid;
648 ref2 = ref->referring->add_reference (this, ref->use, ref->stmt);
649 ref2->speculative = speculative;
650 ref2->lto_stmt_uid = stmt_uid;
654 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
656 struct ipa_ref *
657 symtab_node::clone_reference (struct ipa_ref *ref, gimple stmt)
659 bool speculative = ref->speculative;
660 unsigned int stmt_uid = ref->lto_stmt_uid;
661 struct ipa_ref *ref2;
663 ref2 = add_reference (ref->referred, ref->use, stmt);
664 ref2->speculative = speculative;
665 ref2->lto_stmt_uid = stmt_uid;
666 return ref2;
669 /* Find the structure describing a reference to REFERRED_NODE
670 and associated with statement STMT. */
672 struct ipa_ref *
673 symtab_node::find_reference (symtab_node *referred_node,
674 gimple stmt, unsigned int lto_stmt_uid)
676 struct ipa_ref *r = NULL;
677 int i;
679 for (i = 0; iterate_reference (i, r); i++)
680 if (r->referred == referred_node
681 && !r->speculative
682 && ((stmt && r->stmt == stmt)
683 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
684 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
685 return r;
686 return NULL;
689 /* Remove all references that are associated with statement STMT. */
691 void
692 symtab_node::remove_stmt_references (gimple stmt)
694 struct ipa_ref *r = NULL;
695 int i = 0;
697 while (iterate_reference (i, r))
698 if (r->stmt == stmt)
699 r->remove_reference ();
700 else
701 i++;
704 /* Remove all stmt references in non-speculative references.
705 Those are not maintained during inlining & clonning.
706 The exception are speculative references that are updated along
707 with callgraph edges associated with them. */
709 void
710 symtab_node::clear_stmts_in_references (void)
712 struct ipa_ref *r = NULL;
713 int i;
715 for (i = 0; iterate_reference (i, r); i++)
716 if (!r->speculative)
718 r->stmt = NULL;
719 r->lto_stmt_uid = 0;
723 /* Remove all references in ref list. */
725 void
726 symtab_node::remove_all_references (void)
728 while (vec_safe_length (ref_list.references))
729 ref_list.references->last ().remove_reference ();
730 vec_free (ref_list.references);
733 /* Remove all referring items in ref list. */
735 void
736 symtab_node::remove_all_referring (void)
738 while (ref_list.referring.length ())
739 ref_list.referring.last ()->remove_reference ();
740 ref_list.referring.release ();
743 /* Dump references in ref list to FILE. */
745 void
746 symtab_node::dump_references (FILE *file)
748 struct ipa_ref *ref = NULL;
749 int i;
750 for (i = 0; iterate_reference (i, ref); i++)
752 fprintf (file, "%s/%i (%s)",
753 ref->referred->asm_name (),
754 ref->referred->order,
755 ipa_ref_use_name [ref->use]);
756 if (ref->speculative)
757 fprintf (file, " (speculative)");
759 fprintf (file, "\n");
762 /* Dump referring in list to FILE. */
764 void
765 symtab_node::dump_referring (FILE *file)
767 struct ipa_ref *ref = NULL;
768 int i;
769 for (i = 0; iterate_referring(i, ref); i++)
771 fprintf (file, "%s/%i (%s)",
772 ref->referring->asm_name (),
773 ref->referring->order,
774 ipa_ref_use_name [ref->use]);
775 if (ref->speculative)
776 fprintf (file, " (speculative)");
778 fprintf (file, "\n");
781 /* Return true if list contains an alias. */
782 bool
783 symtab_node::has_aliases_p (void)
785 struct ipa_ref *ref = NULL;
786 int i;
788 for (i = 0; iterate_referring (i, ref); i++)
789 if (ref->use == IPA_REF_ALIAS)
790 return true;
791 return false;
794 /* Iterates I-th reference in the list, REF is also set. */
796 struct ipa_ref *
797 symtab_node::iterate_reference (unsigned i, struct ipa_ref *&ref)
799 vec_safe_iterate (ref_list.references, i, &ref);
801 return ref;
804 /* Iterates I-th referring item in the list, REF is also set. */
806 struct ipa_ref *
807 symtab_node::iterate_referring (unsigned i, struct ipa_ref *&ref)
809 ref_list.referring.iterate (i, &ref);
811 return ref;
814 /* Iterates I-th referring alias item in the list, REF is also set. */
816 struct ipa_ref *
817 symtab_node::iterate_direct_aliases (unsigned i, struct ipa_ref *&ref)
819 ref_list.referring.iterate (i, &ref);
821 if (ref && ref->use != IPA_REF_ALIAS)
822 return NULL;
824 return ref;
828 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
830 /* Dump base fields of symtab nodes. Not to be used directly. */
832 void
833 dump_symtab_base (FILE *f, symtab_node *node)
835 static const char * const visibility_types[] = {
836 "default", "protected", "hidden", "internal"
839 fprintf (f, "%s/%i (%s)",
840 node->asm_name (),
841 node->order,
842 node->name ());
843 dump_addr (f, " @", (void *)node);
844 fprintf (f, "\n Type: %s", symtab_type_names[node->type]);
846 if (node->definition)
847 fprintf (f, " definition");
848 if (node->analyzed)
849 fprintf (f, " analyzed");
850 if (node->alias)
851 fprintf (f, " alias");
852 if (node->weakref)
853 fprintf (f, " weakref");
854 if (node->cpp_implicit_alias)
855 fprintf (f, " cpp_implicit_alias");
856 if (node->alias_target)
857 fprintf (f, " target:%s",
858 DECL_P (node->alias_target)
859 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
860 (node->alias_target))
861 : IDENTIFIER_POINTER (node->alias_target));
862 if (node->body_removed)
863 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
864 fprintf (f, "\n Visibility:");
865 if (node->in_other_partition)
866 fprintf (f, " in_other_partition");
867 if (node->used_from_other_partition)
868 fprintf (f, " used_from_other_partition");
869 if (node->force_output)
870 fprintf (f, " force_output");
871 if (node->forced_by_abi)
872 fprintf (f, " forced_by_abi");
873 if (node->externally_visible)
874 fprintf (f, " externally_visible");
875 if (node->resolution != LDPR_UNKNOWN)
876 fprintf (f, " %s",
877 ld_plugin_symbol_resolution_names[(int)node->resolution]);
878 if (TREE_ASM_WRITTEN (node->decl))
879 fprintf (f, " asm_written");
880 if (DECL_EXTERNAL (node->decl))
881 fprintf (f, " external");
882 if (TREE_PUBLIC (node->decl))
883 fprintf (f, " public");
884 if (DECL_COMMON (node->decl))
885 fprintf (f, " common");
886 if (DECL_WEAK (node->decl))
887 fprintf (f, " weak");
888 if (DECL_DLLIMPORT_P (node->decl))
889 fprintf (f, " dll_import");
890 if (DECL_COMDAT (node->decl))
891 fprintf (f, " comdat");
892 if (node->get_comdat_group ())
893 fprintf (f, " comdat_group:%s",
894 IDENTIFIER_POINTER (node->get_comdat_group_id ()));
895 if (DECL_ONE_ONLY (node->decl))
896 fprintf (f, " one_only");
897 if (node->get_section ())
898 fprintf (f, " section:%s",
899 node->get_section ());
900 if (node->implicit_section)
901 fprintf (f," (implicit_section)");
902 if (DECL_VISIBILITY_SPECIFIED (node->decl))
903 fprintf (f, " visibility_specified");
904 if (DECL_VISIBILITY (node->decl))
905 fprintf (f, " visibility:%s",
906 visibility_types [DECL_VISIBILITY (node->decl)]);
907 if (DECL_VIRTUAL_P (node->decl))
908 fprintf (f, " virtual");
909 if (DECL_ARTIFICIAL (node->decl))
910 fprintf (f, " artificial");
911 if (TREE_CODE (node->decl) == FUNCTION_DECL)
913 if (DECL_STATIC_CONSTRUCTOR (node->decl))
914 fprintf (f, " constructor");
915 if (DECL_STATIC_DESTRUCTOR (node->decl))
916 fprintf (f, " destructor");
918 fprintf (f, "\n");
920 if (node->same_comdat_group)
921 fprintf (f, " Same comdat group as: %s/%i\n",
922 node->same_comdat_group->asm_name (),
923 node->same_comdat_group->order);
924 if (node->next_sharing_asm_name)
925 fprintf (f, " next sharing asm name: %i\n",
926 node->next_sharing_asm_name->order);
927 if (node->previous_sharing_asm_name)
928 fprintf (f, " previous sharing asm name: %i\n",
929 node->previous_sharing_asm_name->order);
931 if (node->address_taken)
932 fprintf (f, " Address is taken.\n");
933 if (node->aux)
935 fprintf (f, " Aux:");
936 dump_addr (f, " @", (void *)node->aux);
939 fprintf (f, " References: ");
940 node->dump_references (f);
941 fprintf (f, " Referring: ");
942 node->dump_referring (f);
943 if (node->lto_file_data)
944 fprintf (f, " Read from file: %s\n",
945 node->lto_file_data->file_name);
948 /* Dump symtab node. */
950 void
951 dump_symtab_node (FILE *f, symtab_node *node)
953 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
954 dump_cgraph_node (f, cnode);
955 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
956 dump_varpool_node (f, vnode);
959 /* Dump symbol table. */
961 void
962 dump_symtab (FILE *f)
964 symtab_node *node;
965 fprintf (f, "Symbol table:\n\n");
966 FOR_EACH_SYMBOL (node)
967 dump_symtab_node (f, node);
970 /* Dump symtab node NODE to stderr. */
972 DEBUG_FUNCTION void
973 debug_symtab_node (symtab_node *node)
975 dump_symtab_node (stderr, node);
978 /* Dump symbol table to stderr. */
980 DEBUG_FUNCTION void
981 debug_symtab (void)
983 dump_symtab (stderr);
986 /* Verify common part of symtab nodes. */
988 DEBUG_FUNCTION bool
989 verify_symtab_base (symtab_node *node)
991 bool error_found = false;
992 symtab_node *hashed_node;
994 if (is_a <cgraph_node *> (node))
996 if (TREE_CODE (node->decl) != FUNCTION_DECL)
998 error ("function symbol is not function");
999 error_found = true;
1002 else if (is_a <varpool_node *> (node))
1004 if (TREE_CODE (node->decl) != VAR_DECL)
1006 error ("variable symbol is not variable");
1007 error_found = true;
1010 else
1012 error ("node has unknown type");
1013 error_found = true;
1016 if (cgraph_state != CGRAPH_LTO_STREAMING)
1018 hashed_node = symtab_get_node (node->decl);
1019 if (!hashed_node)
1021 error ("node not found node->decl->decl_with_vis.symtab_node");
1022 error_found = true;
1024 if (hashed_node != node
1025 && (!is_a <cgraph_node *> (node)
1026 || !dyn_cast <cgraph_node *> (node)->clone_of
1027 || dyn_cast <cgraph_node *> (node)->clone_of->decl
1028 != node->decl))
1030 error ("node differs from node->decl->decl_with_vis.symtab_node");
1031 error_found = true;
1034 if (assembler_name_hash)
1036 hashed_node = symtab_node_for_asm (DECL_ASSEMBLER_NAME (node->decl));
1037 if (hashed_node && hashed_node->previous_sharing_asm_name)
1039 error ("assembler name hash list corrupted");
1040 error_found = true;
1042 while (hashed_node)
1044 if (hashed_node == node)
1045 break;
1046 hashed_node = hashed_node->next_sharing_asm_name;
1048 if (!hashed_node
1049 && !(is_a <varpool_node *> (node)
1050 || DECL_HARD_REGISTER (node->decl)))
1052 error ("node not found in symtab assembler name hash");
1053 error_found = true;
1056 if (node->previous_sharing_asm_name
1057 && node->previous_sharing_asm_name->next_sharing_asm_name != node)
1059 error ("double linked list of assembler names corrupted");
1060 error_found = true;
1062 if (node->analyzed && !node->definition)
1064 error ("node is analyzed byt it is not a definition");
1065 error_found = true;
1067 if (node->cpp_implicit_alias && !node->alias)
1069 error ("node is alias but not implicit alias");
1070 error_found = true;
1072 if (node->alias && !node->definition
1073 && !node->weakref)
1075 error ("node is alias but not definition");
1076 error_found = true;
1078 if (node->weakref && !node->alias)
1080 error ("node is weakref but not an alias");
1081 error_found = true;
1083 if (node->same_comdat_group)
1085 symtab_node *n = node->same_comdat_group;
1087 if (!n->get_comdat_group ())
1089 error ("node is in same_comdat_group list but has no comdat_group");
1090 error_found = true;
1092 if (n->get_comdat_group () != node->get_comdat_group ())
1094 error ("same_comdat_group list across different groups");
1095 error_found = true;
1097 if (!n->definition)
1099 error ("Node has same_comdat_group but it is not a definition");
1100 error_found = true;
1102 if (n->type != node->type)
1104 error ("mixing different types of symbol in same comdat groups is not supported");
1105 error_found = true;
1107 if (n == node)
1109 error ("node is alone in a comdat group");
1110 error_found = true;
1114 if (!n->same_comdat_group)
1116 error ("same_comdat_group is not a circular list");
1117 error_found = true;
1118 break;
1120 n = n->same_comdat_group;
1122 while (n != node);
1123 if (symtab_comdat_local_p (node))
1125 struct ipa_ref *ref = NULL;
1127 for (int i = 0; node->iterate_referring (i, ref); ++i)
1129 if (!symtab_in_same_comdat_p (ref->referring, node))
1131 error ("comdat-local symbol referred to by %s outside its "
1132 "comdat",
1133 identifier_to_locale (ref->referring->name()));
1134 error_found = true;
1139 if (node->implicit_section && !node->get_section ())
1141 error ("implicit_section flag is set but section isn't");
1142 error_found = true;
1144 if (node->get_section () && node->get_comdat_group ()
1145 && !node->implicit_section)
1147 error ("Both section and comdat group is set");
1148 error_found = true;
1150 /* TODO: Add string table for sections, so we do not keep holding duplicated
1151 strings. */
1152 if (node->alias && node->definition
1153 && node->get_section () != symtab_alias_target (node)->get_section ()
1154 && (!node->get_section()
1155 || !symtab_alias_target (node)->get_section ()
1156 || strcmp (node->get_section(),
1157 symtab_alias_target (node)->get_section ())))
1159 error ("Alias and target's section differs");
1160 dump_symtab_node (stderr, symtab_alias_target (node));
1161 error_found = true;
1163 if (node->alias && node->definition
1164 && node->get_comdat_group () != symtab_alias_target (node)->get_comdat_group ())
1166 error ("Alias and target's comdat groups differs");
1167 dump_symtab_node (stderr, symtab_alias_target (node));
1168 error_found = true;
1171 return error_found;
1174 /* Verify consistency of NODE. */
1176 DEBUG_FUNCTION void
1177 verify_symtab_node (symtab_node *node)
1179 if (seen_error ())
1180 return;
1182 timevar_push (TV_CGRAPH_VERIFY);
1183 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1184 verify_cgraph_node (cnode);
1185 else
1186 if (verify_symtab_base (node))
1188 dump_symtab_node (stderr, node);
1189 internal_error ("verify_symtab_node failed");
1191 timevar_pop (TV_CGRAPH_VERIFY);
1194 /* Verify symbol table for internal consistency. */
1196 DEBUG_FUNCTION void
1197 verify_symtab (void)
1199 symtab_node *node;
1200 hash_map<tree, symtab_node *> comdat_head_map (251);
1202 FOR_EACH_SYMBOL (node)
1204 verify_symtab_node (node);
1205 if (node->get_comdat_group ())
1207 symtab_node **entry, *s;
1208 bool existed;
1210 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1211 &existed);
1212 if (!existed)
1213 *entry = node;
1214 else
1215 for (s = (*entry)->same_comdat_group; s != NULL && s != node; s = s->same_comdat_group)
1216 if (!s || s == *entry)
1218 error ("Two symbols with same comdat_group are not linked by the same_comdat_group list.");
1219 dump_symtab_node (stderr, *entry);
1220 dump_symtab_node (stderr, s);
1221 internal_error ("verify_symtab failed");
1227 /* Return true when RESOLUTION indicate that linker will use
1228 the symbol from non-LTO object files. */
1230 bool
1231 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
1233 return (resolution == LDPR_PREVAILING_DEF
1234 || resolution == LDPR_PREEMPTED_REG
1235 || resolution == LDPR_RESOLVED_EXEC
1236 || resolution == LDPR_RESOLVED_DYN);
1239 /* Return true when NODE is known to be used from other (non-LTO) object file.
1240 Known only when doing LTO via linker plugin. */
1242 bool
1243 symtab_used_from_object_file_p (symtab_node *node)
1245 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
1246 return false;
1247 if (resolution_used_from_other_file_p (node->resolution))
1248 return true;
1249 return false;
1252 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1253 but other code such as notice_global_symbol generates rtl. */
1255 void
1256 symtab_make_decl_local (tree decl)
1258 rtx rtl, symbol;
1260 /* Avoid clearing comdat_groups on comdat-local decls. */
1261 if (TREE_PUBLIC (decl) == 0)
1262 return;
1264 if (TREE_CODE (decl) == VAR_DECL)
1265 DECL_COMMON (decl) = 0;
1266 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1268 DECL_COMDAT (decl) = 0;
1269 DECL_WEAK (decl) = 0;
1270 DECL_EXTERNAL (decl) = 0;
1271 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1272 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1273 TREE_PUBLIC (decl) = 0;
1274 if (!DECL_RTL_SET_P (decl))
1275 return;
1277 /* Update rtl flags. */
1278 make_decl_rtl (decl);
1280 rtl = DECL_RTL (decl);
1281 if (!MEM_P (rtl))
1282 return;
1284 symbol = XEXP (rtl, 0);
1285 if (GET_CODE (symbol) != SYMBOL_REF)
1286 return;
1288 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1291 /* Return availability of NODE. */
1293 enum availability
1294 symtab_node_availability (symtab_node *node)
1296 if (is_a <cgraph_node *> (node))
1297 return cgraph_function_body_availability (cgraph (node));
1298 else
1299 return cgraph_variable_initializer_availability (varpool (node));
1302 /* Given NODE, walk the alias chain to return the symbol NODE is alias of.
1303 If NODE is not an alias, return NODE.
1304 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
1306 symtab_node *
1307 symtab_alias_ultimate_target (symtab_node *node, enum availability *availability)
1309 bool weakref_p = false;
1311 if (!node->alias)
1313 if (availability)
1314 *availability = symtab_node_availability (node);
1315 return node;
1318 /* To determine visibility of the target, we follow ELF semantic of aliases.
1319 Here alias is an alternative assembler name of a given definition. Its
1320 availability prevails the availability of its target (i.e. static alias of
1321 weak definition is available.
1323 Weakref is a different animal (and not part of ELF per se). It is just
1324 alternative name of a given symbol used within one complation unit
1325 and is translated prior hitting the object file. It inherits the
1326 visibility of its target (i.e. weakref of non-overwritable definition
1327 is non-overwritable, while weakref of weak definition is weak).
1329 If we ever get into supporting targets with different semantics, a target
1330 hook will be needed here. */
1332 if (availability)
1334 weakref_p = node->weakref;
1335 if (!weakref_p)
1336 *availability = symtab_node_availability (node);
1337 else
1338 *availability = AVAIL_LOCAL;
1340 while (node)
1342 if (node->alias && node->analyzed)
1343 node = symtab_alias_target (node);
1344 else
1346 if (!availability)
1348 else if (node->analyzed)
1350 if (weakref_p)
1352 enum availability a = symtab_node_availability (node);
1353 if (a < *availability)
1354 *availability = a;
1357 else
1358 *availability = AVAIL_NOT_AVAILABLE;
1359 return node;
1361 if (node && availability && weakref_p)
1363 enum availability a = symtab_node_availability (node);
1364 if (a < *availability)
1365 *availability = a;
1366 weakref_p = node->weakref;
1369 if (availability)
1370 *availability = AVAIL_NOT_AVAILABLE;
1371 return NULL;
1374 /* C++ FE sometimes change linkage flags after producing same body aliases.
1376 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1377 are obviously equivalent. The way it is doing so is however somewhat
1378 kludgy and interferes with the visibility code. As a result we need to
1379 copy the visibility from the target to get things right. */
1381 void
1382 fixup_same_cpp_alias_visibility (symtab_node *node, symtab_node *target)
1384 if (is_a <cgraph_node *> (node))
1386 DECL_DECLARED_INLINE_P (node->decl)
1387 = DECL_DECLARED_INLINE_P (target->decl);
1388 DECL_DISREGARD_INLINE_LIMITS (node->decl)
1389 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1391 /* FIXME: It is not really clear why those flags should not be copied for
1392 functions, too. */
1393 else
1395 DECL_WEAK (node->decl) = DECL_WEAK (target->decl);
1396 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1397 DECL_VISIBILITY (node->decl) = DECL_VISIBILITY (target->decl);
1399 DECL_VIRTUAL_P (node->decl) = DECL_VIRTUAL_P (target->decl);
1400 if (TREE_PUBLIC (node->decl))
1402 tree group;
1404 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1405 DECL_COMDAT (node->decl) = DECL_COMDAT (target->decl);
1406 group = target->get_comdat_group ();
1407 node->set_comdat_group (group);
1408 if (group
1409 && !node->same_comdat_group)
1410 symtab_add_to_same_comdat_group (node, target);
1412 node->externally_visible = target->externally_visible;
1415 /* Hash sections by their names. */
1417 static hashval_t
1418 hash_section_hash_entry (const void *p)
1420 const section_hash_entry *n = (const section_hash_entry *) p;
1421 return htab_hash_string (n->name);
1424 /* Return true if section P1 name equals to P2. */
1426 static int
1427 eq_sections (const void *p1, const void *p2)
1429 const section_hash_entry *n1 = (const section_hash_entry *) p1;
1430 const char *name = (const char *)p2;
1431 return n1->name == name || !strcmp (n1->name, name);
1434 /* Set section, do not recurse into aliases.
1435 When one wants to change section of symbol and its aliases,
1436 use set_section */
1438 void
1439 symtab_node::set_section_for_node (const char *section)
1441 const char *current = get_section ();
1442 void **slot;
1444 if (current == section
1445 || (current && section
1446 && !strcmp (current, section)))
1447 return;
1449 if (current)
1451 x_section->ref_count--;
1452 if (!x_section->ref_count)
1454 slot = htab_find_slot_with_hash (section_hash, x_section->name,
1455 htab_hash_string (x_section->name),
1456 INSERT);
1457 ggc_free (x_section);
1458 htab_clear_slot (section_hash, slot);
1460 x_section = NULL;
1462 if (!section)
1464 implicit_section = false;
1465 return;
1467 if (!section_hash)
1468 section_hash = htab_create_ggc (10, hash_section_hash_entry,
1469 eq_sections, NULL);
1470 slot = htab_find_slot_with_hash (section_hash, section,
1471 htab_hash_string (section),
1472 INSERT);
1473 if (*slot)
1474 x_section = (section_hash_entry *)*slot;
1475 else
1477 int len = strlen (section);
1478 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1479 x_section->name = ggc_vec_alloc<char> (len + 1);
1480 memcpy (x_section->name, section, len + 1);
1482 x_section->ref_count++;
1485 /* Worker for set_section. */
1487 static bool
1488 set_section_1 (struct symtab_node *n, void *s)
1490 n->set_section_for_node ((char *)s);
1491 return false;
1494 /* Set section of symbol and its aliases. */
1496 void
1497 symtab_node::set_section (const char *section)
1499 gcc_assert (!this->alias);
1500 symtab_for_node_and_aliases (this, set_section_1, const_cast<char *>(section), true);
1503 /* Return the initialization priority. */
1505 priority_type
1506 symtab_node::get_init_priority ()
1508 struct symbol_priority_map *h;
1509 struct symbol_priority_map in;
1511 if (!this->in_init_priority_hash)
1512 return DEFAULT_INIT_PRIORITY;
1513 in.symbol = this;
1514 h = (struct symbol_priority_map *) htab_find (init_priority_hash, &in);
1515 return h ? h->init : DEFAULT_INIT_PRIORITY;
1518 /* Return the finalization priority. */
1520 priority_type
1521 cgraph_node::get_fini_priority ()
1523 struct symbol_priority_map *h;
1524 struct symbol_priority_map in;
1526 if (!this->in_init_priority_hash)
1527 return DEFAULT_INIT_PRIORITY;
1528 in.symbol = this;
1529 h = (struct symbol_priority_map *) htab_find (init_priority_hash, &in);
1530 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1533 /* Return true if the from tree in both priority maps are equal. */
1536 symbol_priority_map_eq (const void *va, const void *vb)
1538 const struct symbol_priority_map *const a = (const struct symbol_priority_map *) va,
1539 *const b = (const struct symbol_priority_map *) vb;
1540 return (a->symbol == b->symbol);
1543 /* Hash a from symbol in a symbol_priority_map. */
1545 unsigned int
1546 symbol_priority_map_hash (const void *item)
1548 return htab_hash_pointer (((const struct symbol_priority_map *)item)->symbol);
1551 /* Return the initialization and finalization priority information for
1552 DECL. If there is no previous priority information, a freshly
1553 allocated structure is returned. */
1555 static struct symbol_priority_map *
1556 symbol_priority_info (struct symtab_node *symbol)
1558 struct symbol_priority_map in;
1559 struct symbol_priority_map *h;
1560 void **loc;
1562 in.symbol = symbol;
1563 if (!init_priority_hash)
1564 init_priority_hash = htab_create_ggc (512, symbol_priority_map_hash,
1565 symbol_priority_map_eq, 0);
1567 loc = htab_find_slot (init_priority_hash, &in, INSERT);
1568 h = (struct symbol_priority_map *) *loc;
1569 if (!h)
1571 h = ggc_cleared_alloc<symbol_priority_map> ();
1572 *loc = h;
1573 h->symbol = symbol;
1574 h->init = DEFAULT_INIT_PRIORITY;
1575 h->fini = DEFAULT_INIT_PRIORITY;
1576 symbol->in_init_priority_hash = true;
1579 return h;
1582 /* Set initialization priority to PRIORITY. */
1584 void
1585 symtab_node::set_init_priority (priority_type priority)
1587 struct symbol_priority_map *h;
1589 if (is_a <cgraph_node *> (this))
1590 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1592 if (priority == DEFAULT_INIT_PRIORITY)
1594 gcc_assert (get_init_priority() == priority);
1595 return;
1597 h = symbol_priority_info (this);
1598 h->init = priority;
1601 /* Set fialization priority to PRIORITY. */
1603 void
1604 cgraph_node::set_fini_priority (priority_type priority)
1606 struct symbol_priority_map *h;
1608 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1610 if (priority == DEFAULT_INIT_PRIORITY)
1612 gcc_assert (get_fini_priority() == priority);
1613 return;
1615 h = symbol_priority_info (this);
1616 h->fini = priority;
1619 /* Worker for symtab_resolve_alias. */
1621 static bool
1622 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
1624 n->implicit_section = true;
1625 return false;
1628 /* Add reference recording that NODE is alias of TARGET.
1629 The function can fail in the case of aliasing cycles; in this case
1630 it returns false. */
1632 bool
1633 symtab_resolve_alias (symtab_node *node, symtab_node *target)
1635 symtab_node *n;
1637 gcc_assert (!node->analyzed
1638 && !vec_safe_length (node->ref_list.references));
1640 /* Never let cycles to creep into the symbol table alias references;
1641 those will make alias walkers to be infinite. */
1642 for (n = target; n && n->alias;
1643 n = n->analyzed ? symtab_alias_target (n) : NULL)
1644 if (n == node)
1646 if (is_a <cgraph_node *> (node))
1647 error ("function %q+D part of alias cycle", node->decl);
1648 else if (is_a <varpool_node *> (node))
1649 error ("variable %q+D part of alias cycle", node->decl);
1650 else
1651 gcc_unreachable ();
1652 node->alias = false;
1653 return false;
1656 /* "analyze" the node - i.e. mark the reference. */
1657 node->definition = true;
1658 node->alias = true;
1659 node->analyzed = true;
1660 node->add_reference (target, IPA_REF_ALIAS, NULL);
1662 /* Add alias into the comdat group of its target unless it is already there. */
1663 if (node->same_comdat_group)
1664 symtab_remove_from_same_comdat_group (node);
1665 node->set_comdat_group (NULL);
1666 if (target->get_comdat_group ())
1667 symtab_add_to_same_comdat_group (node, target);
1669 if ((node->get_section () != target->get_section ()
1670 || target->get_comdat_group ())
1671 && node->get_section () && !node->implicit_section)
1673 error ("section of alias %q+D must match section of its target",
1674 node->decl);
1676 symtab_for_node_and_aliases (node, set_section_1,
1677 const_cast<char *>(target->get_section ()), true);
1678 if (target->implicit_section)
1679 symtab_for_node_and_aliases (node,
1680 set_implicit_section, NULL, true);
1682 /* Alias targets become redundant after alias is resolved into an reference.
1683 We do not want to keep it around or we would have to mind updating them
1684 when renaming symbols. */
1685 node->alias_target = NULL;
1687 if (node->cpp_implicit_alias && cgraph_state >= CGRAPH_STATE_CONSTRUCTION)
1688 fixup_same_cpp_alias_visibility (node, target);
1690 /* If alias has address taken, so does the target. */
1691 if (node->address_taken)
1692 symtab_alias_ultimate_target (target, NULL)->address_taken = true;
1693 return true;
1696 /* Call calback on NODE and aliases associated to NODE.
1697 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1698 skipped. */
1700 bool
1701 symtab_for_node_and_aliases (symtab_node *node,
1702 bool (*callback) (symtab_node *, void *),
1703 void *data,
1704 bool include_overwritable)
1706 int i;
1707 struct ipa_ref *ref;
1709 if (callback (node, data))
1710 return true;
1711 for (i = 0; node->iterate_referring (i, ref); i++)
1712 if (ref->use == IPA_REF_ALIAS)
1714 symtab_node *alias = ref->referring;
1715 if (include_overwritable
1716 || symtab_node_availability (alias) > AVAIL_OVERWRITABLE)
1717 if (symtab_for_node_and_aliases (alias, callback, data,
1718 include_overwritable))
1719 return true;
1721 return false;
1724 /* Worker searching nonoverwritable alias. */
1726 static bool
1727 symtab_nonoverwritable_alias_1 (symtab_node *node, void *data)
1729 if (decl_binds_to_current_def_p (node->decl))
1731 symtab_node *fn = symtab_alias_ultimate_target (node);
1733 /* Ensure that the alias is well formed this may not be the case
1734 of user defined aliases and currently it is not always the case
1735 of C++ same body aliases (that is a bug). */
1736 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
1737 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
1738 || (TREE_CODE (node->decl) == FUNCTION_DECL
1739 && flags_from_decl_or_type (node->decl)
1740 != flags_from_decl_or_type (fn->decl))
1741 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
1742 return false;
1744 *(symtab_node **)data = node;
1745 return true;
1747 return false;
1750 /* If NODE can not be overwriten by static or dynamic linker to point to different
1751 definition, return NODE. Otherwise look for alias with such property and if
1752 none exists, introduce new one. */
1754 symtab_node *
1755 symtab_nonoverwritable_alias (symtab_node *node)
1757 tree new_decl;
1758 symtab_node *new_node = NULL;
1760 /* First try to look up existing alias or base object
1761 (if that is already non-overwritable). */
1762 node = symtab_alias_ultimate_target (node, NULL);
1763 gcc_assert (!node->alias && !node->weakref);
1764 symtab_for_node_and_aliases (node, symtab_nonoverwritable_alias_1,
1765 (void *)&new_node, true);
1766 if (new_node)
1767 return new_node;
1768 #ifndef ASM_OUTPUT_DEF
1769 /* If aliases aren't supported by the assembler, fail. */
1770 return NULL;
1771 #endif
1773 /* Otherwise create a new one. */
1774 new_decl = copy_node (node->decl);
1775 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1776 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1777 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1778 DECL_INITIAL (new_decl) = NULL;
1779 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1780 SET_DECL_RTL (new_decl, NULL);
1782 /* Update the properties. */
1783 DECL_EXTERNAL (new_decl) = 0;
1784 TREE_PUBLIC (new_decl) = 0;
1785 DECL_COMDAT (new_decl) = 0;
1786 DECL_WEAK (new_decl) = 0;
1788 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1789 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1790 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1792 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1793 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1794 new_node = cgraph_create_function_alias
1795 (new_decl, node->decl);
1797 else
1799 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1800 DECL_INITIAL (new_decl) = error_mark_node;
1801 new_node = varpool_create_variable_alias (new_decl, node->decl);
1803 symtab_resolve_alias (new_node, node);
1804 gcc_assert (decl_binds_to_current_def_p (new_decl)
1805 && targetm.binds_local_p (new_decl));
1806 return new_node;
1809 /* Return true if A and B represents semantically equivalent symbols. */
1811 bool
1812 symtab_semantically_equivalent_p (symtab_node *a,
1813 symtab_node *b)
1815 enum availability avail;
1816 symtab_node *ba;
1817 symtab_node *bb;
1819 /* Equivalent functions are equivalent. */
1820 if (a->decl == b->decl)
1821 return true;
1823 /* If symbol is not overwritable by different implementation,
1824 walk to the base object it defines. */
1825 ba = symtab_alias_ultimate_target (a, &avail);
1826 if (avail >= AVAIL_AVAILABLE)
1828 if (ba == b)
1829 return true;
1831 else
1832 ba = a;
1833 bb = symtab_alias_ultimate_target (b, &avail);
1834 if (avail >= AVAIL_AVAILABLE)
1836 if (a == bb)
1837 return true;
1839 else
1840 bb = b;
1841 return bb == ba;
1844 /* Classify symbol NODE for partitioning. */
1846 enum symbol_partitioning_class
1847 symtab_get_symbol_partitioning_class (symtab_node *node)
1849 /* Inline clones are always duplicated.
1850 This include external delcarations. */
1851 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1853 if (DECL_ABSTRACT (node->decl))
1854 return SYMBOL_EXTERNAL;
1856 if (cnode && cnode->global.inlined_to)
1857 return SYMBOL_DUPLICATE;
1859 /* Weakref aliases are always duplicated. */
1860 if (node->weakref)
1861 return SYMBOL_DUPLICATE;
1863 /* External declarations are external. */
1864 if (DECL_EXTERNAL (node->decl))
1865 return SYMBOL_EXTERNAL;
1867 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
1869 /* Constant pool references use local symbol names that can not
1870 be promoted global. We should never put into a constant pool
1871 objects that can not be duplicated across partitions. */
1872 if (DECL_IN_CONSTANT_POOL (node->decl))
1873 return SYMBOL_DUPLICATE;
1874 gcc_checking_assert (vnode->definition);
1876 /* Functions that are cloned may stay in callgraph even if they are unused.
1877 Handle them as external; compute_ltrans_boundary take care to make
1878 proper things to happen (i.e. to make them appear in the boundary but
1879 with body streamed, so clone can me materialized). */
1880 else if (!cgraph (node)->definition)
1881 return SYMBOL_EXTERNAL;
1883 /* Linker discardable symbols are duplicated to every use unless they are
1884 keyed. */
1885 if (DECL_ONE_ONLY (node->decl)
1886 && !node->force_output
1887 && !node->forced_by_abi
1888 && !symtab_used_from_object_file_p (node))
1889 return SYMBOL_DUPLICATE;
1891 return SYMBOL_PARTITION;
1893 #include "gt-symtab.h"