2014-04-15 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / symtab.c
blob4db4870fa4e5175ae6b537cc7bcb9bb0ad20a294
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"
46 const char * const ld_plugin_symbol_resolution_names[]=
48 "",
49 "undef",
50 "prevailing_def",
51 "prevailing_def_ironly",
52 "preempted_reg",
53 "preempted_ir",
54 "resolved_ir",
55 "resolved_exec",
56 "resolved_dyn",
57 "prevailing_def_ironly_exp"
60 /* Hash table used to convert declarations into nodes. */
61 static GTY((param_is (symtab_node))) htab_t symtab_hash;
62 /* Hash table used to convert assembler names into nodes. */
63 static GTY((param_is (symtab_node))) htab_t assembler_name_hash;
65 /* Linked list of symbol table nodes. */
66 symtab_node *symtab_nodes;
68 /* The order index of the next symtab node to be created. This is
69 used so that we can sort the cgraph nodes in order by when we saw
70 them, to support -fno-toplevel-reorder. */
71 int symtab_order;
73 /* Returns a hash code for P. */
75 static hashval_t
76 hash_node (const void *p)
78 const symtab_node *n = (const symtab_node *) p;
79 return (hashval_t) DECL_UID (n->decl);
83 /* Returns nonzero if P1 and P2 are equal. */
85 static int
86 eq_node (const void *p1, const void *p2)
88 const symtab_node *n1 = (const symtab_node *) p1;
89 const symtab_node *n2 = (const symtab_node *) p2;
90 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
93 /* Hash asmnames ignoring the user specified marks. */
95 static hashval_t
96 decl_assembler_name_hash (const_tree asmname)
98 if (IDENTIFIER_POINTER (asmname)[0] == '*')
100 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
101 size_t ulp_len = strlen (user_label_prefix);
103 if (ulp_len == 0)
105 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
106 decl_str += ulp_len;
108 return htab_hash_string (decl_str);
111 return htab_hash_string (IDENTIFIER_POINTER (asmname));
115 /* Returns a hash code for P. */
117 static hashval_t
118 hash_node_by_assembler_name (const void *p)
120 const symtab_node *n = (const symtab_node *) p;
121 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
124 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
126 static bool
127 decl_assembler_name_equal (tree decl, const_tree asmname)
129 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
130 const char *decl_str;
131 const char *asmname_str;
132 bool test = false;
134 if (decl_asmname == asmname)
135 return true;
137 decl_str = IDENTIFIER_POINTER (decl_asmname);
138 asmname_str = IDENTIFIER_POINTER (asmname);
141 /* If the target assembler name was set by the user, things are trickier.
142 We have a leading '*' to begin with. After that, it's arguable what
143 is the correct thing to do with -fleading-underscore. Arguably, we've
144 historically been doing the wrong thing in assemble_alias by always
145 printing the leading underscore. Since we're not changing that, make
146 sure user_label_prefix follows the '*' before matching. */
147 if (decl_str[0] == '*')
149 size_t ulp_len = strlen (user_label_prefix);
151 decl_str ++;
153 if (ulp_len == 0)
154 test = true;
155 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
156 decl_str += ulp_len, test=true;
157 else
158 decl_str --;
160 if (asmname_str[0] == '*')
162 size_t ulp_len = strlen (user_label_prefix);
164 asmname_str ++;
166 if (ulp_len == 0)
167 test = true;
168 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
169 asmname_str += ulp_len, test=true;
170 else
171 asmname_str --;
174 if (!test)
175 return false;
176 return strcmp (decl_str, asmname_str) == 0;
180 /* Returns nonzero if P1 and P2 are equal. */
182 static int
183 eq_assembler_name (const void *p1, const void *p2)
185 const symtab_node *n1 = (const symtab_node *) p1;
186 const_tree name = (const_tree)p2;
187 return (decl_assembler_name_equal (n1->decl, name));
190 /* Insert NODE to assembler name hash. */
192 static void
193 insert_to_assembler_name_hash (symtab_node *node, bool with_clones)
195 if (is_a <varpool_node> (node) && DECL_HARD_REGISTER (node->decl))
196 return;
197 gcc_checking_assert (!node->previous_sharing_asm_name
198 && !node->next_sharing_asm_name);
199 if (assembler_name_hash)
201 void **aslot;
202 struct cgraph_node *cnode;
203 tree decl = node->decl;
205 tree name = DECL_ASSEMBLER_NAME (node->decl);
207 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
208 decl_assembler_name_hash (name),
209 INSERT);
210 gcc_assert (*aslot != node);
211 node->next_sharing_asm_name = (symtab_node *)*aslot;
212 if (*aslot != NULL)
213 ((symtab_node *)*aslot)->previous_sharing_asm_name = node;
214 *aslot = node;
216 /* Update also possible inline clones sharing a decl. */
217 cnode = dyn_cast <cgraph_node> (node);
218 if (cnode && cnode->clones && with_clones)
219 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
220 if (cnode->decl == decl)
221 insert_to_assembler_name_hash (cnode, true);
226 /* Remove NODE from assembler name hash. */
228 static void
229 unlink_from_assembler_name_hash (symtab_node *node, bool with_clones)
231 if (assembler_name_hash)
233 struct cgraph_node *cnode;
234 tree decl = node->decl;
236 if (node->next_sharing_asm_name)
237 node->next_sharing_asm_name->previous_sharing_asm_name
238 = node->previous_sharing_asm_name;
239 if (node->previous_sharing_asm_name)
241 node->previous_sharing_asm_name->next_sharing_asm_name
242 = node->next_sharing_asm_name;
244 else
246 tree name = DECL_ASSEMBLER_NAME (node->decl);
247 void **slot;
248 slot = htab_find_slot_with_hash (assembler_name_hash, name,
249 decl_assembler_name_hash (name),
250 NO_INSERT);
251 gcc_assert (*slot == node);
252 if (!node->next_sharing_asm_name)
253 htab_clear_slot (assembler_name_hash, slot);
254 else
255 *slot = node->next_sharing_asm_name;
257 node->next_sharing_asm_name = NULL;
258 node->previous_sharing_asm_name = NULL;
260 /* Update also possible inline clones sharing a decl. */
261 cnode = dyn_cast <cgraph_node> (node);
262 if (cnode && cnode->clones && with_clones)
263 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
264 if (cnode->decl == decl)
265 unlink_from_assembler_name_hash (cnode, true);
269 /* Arrange node to be first in its entry of assembler_name_hash. */
271 void
272 symtab_prevail_in_asm_name_hash (symtab_node *node)
274 unlink_from_assembler_name_hash (node, false);
275 insert_to_assembler_name_hash (node, false);
279 /* Add node into symbol table. This function is not used directly, but via
280 cgraph/varpool node creation routines. */
282 void
283 symtab_register_node (symtab_node *node)
285 struct symtab_node key;
286 symtab_node **slot;
288 node->next = symtab_nodes;
289 node->previous = NULL;
290 if (symtab_nodes)
291 symtab_nodes->previous = node;
292 symtab_nodes = node;
294 if (!symtab_hash)
295 symtab_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
296 key.decl = node->decl;
297 slot = (symtab_node **) htab_find_slot (symtab_hash, &key, INSERT);
298 if (*slot == NULL)
299 *slot = node;
301 ipa_empty_ref_list (&node->ref_list);
303 node->order = symtab_order++;
305 /* Be sure to do this last; C++ FE might create new nodes via
306 DECL_ASSEMBLER_NAME langhook! */
307 insert_to_assembler_name_hash (node, false);
310 /* Make NODE to be the one symtab hash is pointing to. Used when reshaping tree
311 of inline clones. */
313 void
314 symtab_insert_node_to_hashtable (symtab_node *node)
316 struct symtab_node key;
317 symtab_node **slot;
319 if (!symtab_hash)
320 symtab_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
321 key.decl = node->decl;
322 slot = (symtab_node **) htab_find_slot (symtab_hash, &key, INSERT);
323 *slot = node;
326 /* Remove node from symbol table. This function is not used directly, but via
327 cgraph/varpool node removal routines. */
329 void
330 symtab_unregister_node (symtab_node *node)
332 void **slot;
333 ipa_remove_all_references (&node->ref_list);
334 ipa_remove_all_referring (&node->ref_list);
336 if (node->same_comdat_group)
338 symtab_node *prev;
339 for (prev = node->same_comdat_group;
340 prev->same_comdat_group != node;
341 prev = prev->same_comdat_group)
343 if (node->same_comdat_group == prev)
344 prev->same_comdat_group = NULL;
345 else
346 prev->same_comdat_group = node->same_comdat_group;
347 node->same_comdat_group = NULL;
350 if (node->previous)
351 node->previous->next = node->next;
352 else
353 symtab_nodes = node->next;
354 if (node->next)
355 node->next->previous = node->previous;
356 node->next = NULL;
357 node->previous = NULL;
359 slot = htab_find_slot (symtab_hash, node, NO_INSERT);
361 /* During LTO symtab merging we temporarily corrupt decl to symtab node
362 hash. */
363 gcc_assert ((slot && *slot) || in_lto_p);
364 if (slot && *slot && *slot == node)
366 symtab_node *replacement_node = NULL;
367 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
368 replacement_node = cgraph_find_replacement_node (cnode);
369 if (!replacement_node)
370 htab_clear_slot (symtab_hash, slot);
371 else
372 *slot = replacement_node;
374 if (!is_a <varpool_node> (node) || !DECL_HARD_REGISTER (node->decl))
375 unlink_from_assembler_name_hash (node, false);
378 /* Return symbol table node associated with DECL, if any,
379 and NULL otherwise. */
381 symtab_node *
382 symtab_get_node (const_tree decl)
384 symtab_node **slot;
385 struct symtab_node key;
387 #ifdef ENABLE_CHECKING
388 /* Check that we are called for sane type of object - functions
389 and static or external variables. */
390 gcc_checking_assert (TREE_CODE (decl) == FUNCTION_DECL
391 || (TREE_CODE (decl) == VAR_DECL
392 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
393 || in_lto_p)));
394 #endif
396 if (!symtab_hash)
397 return NULL;
399 key.decl = CONST_CAST2 (tree, const_tree, decl);
401 slot = (symtab_node **) htab_find_slot (symtab_hash, &key,
402 NO_INSERT);
404 if (slot)
405 return *slot;
406 return NULL;
409 /* Remove symtab NODE from the symbol table. */
411 void
412 symtab_remove_node (symtab_node *node)
414 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
415 cgraph_remove_node (cnode);
416 else if (varpool_node *vnode = dyn_cast <varpool_node> (node))
417 varpool_remove_node (vnode);
420 /* Initalize asm name hash unless. */
422 void
423 symtab_initialize_asm_name_hash (void)
425 symtab_node *node;
426 if (!assembler_name_hash)
428 assembler_name_hash =
429 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
430 NULL);
431 FOR_EACH_SYMBOL (node)
432 insert_to_assembler_name_hash (node, false);
436 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
437 Return NULL if there's no such node. */
439 symtab_node *
440 symtab_node_for_asm (const_tree asmname)
442 symtab_node *node;
443 void **slot;
445 symtab_initialize_asm_name_hash ();
446 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
447 decl_assembler_name_hash (asmname),
448 NO_INSERT);
450 if (slot)
452 node = (symtab_node *) *slot;
453 return node;
455 return NULL;
458 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
460 void
461 change_decl_assembler_name (tree decl, tree name)
463 symtab_node *node = NULL;
465 /* We can have user ASM names on things, like global register variables, that
466 are not in the symbol table. */
467 if ((TREE_CODE (decl) == VAR_DECL
468 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
469 || TREE_CODE (decl) == FUNCTION_DECL)
470 node = symtab_get_node (decl);
471 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
473 SET_DECL_ASSEMBLER_NAME (decl, name);
474 if (node)
475 insert_to_assembler_name_hash (node, true);
477 else
479 if (name == DECL_ASSEMBLER_NAME (decl))
480 return;
482 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
483 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
484 : NULL);
485 if (node)
486 unlink_from_assembler_name_hash (node, true);
487 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
488 && DECL_RTL_SET_P (decl))
489 warning (0, "%D renamed after being referenced in assembly", decl);
491 SET_DECL_ASSEMBLER_NAME (decl, name);
492 if (alias)
494 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
495 TREE_CHAIN (name) = alias;
497 if (node)
498 insert_to_assembler_name_hash (node, true);
502 /* Add NEW_ to the same comdat group that OLD is in. */
504 void
505 symtab_add_to_same_comdat_group (symtab_node *new_node,
506 symtab_node *old_node)
508 gcc_assert (DECL_ONE_ONLY (old_node->decl));
509 gcc_assert (!new_node->same_comdat_group);
510 gcc_assert (new_node != old_node);
512 DECL_COMDAT_GROUP (new_node->decl) = DECL_COMDAT_GROUP (old_node->decl);
513 new_node->same_comdat_group = old_node;
514 if (!old_node->same_comdat_group)
515 old_node->same_comdat_group = new_node;
516 else
518 symtab_node *n;
519 for (n = old_node->same_comdat_group;
520 n->same_comdat_group != old_node;
521 n = n->same_comdat_group)
523 n->same_comdat_group = new_node;
527 /* Dissolve the same_comdat_group list in which NODE resides. */
529 void
530 symtab_dissolve_same_comdat_group_list (symtab_node *node)
532 symtab_node *n = node;
533 symtab_node *next;
535 if (!node->same_comdat_group)
536 return;
539 next = n->same_comdat_group;
540 n->same_comdat_group = NULL;
541 /* Clear DECL_COMDAT_GROUP for comdat locals, since
542 make_decl_local doesn't. */
543 if (!TREE_PUBLIC (n->decl))
544 DECL_COMDAT_GROUP (n->decl) = NULL_TREE;
545 n = next;
547 while (n != node);
550 /* Return printable assembler name of NODE.
551 This function is used only for debugging. When assembler name
552 is unknown go with identifier name. */
554 const char *
555 symtab_node::asm_name () const
557 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
558 return lang_hooks.decl_printable_name (decl, 2);
559 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
562 /* Return printable identifier name. */
564 const char *
565 symtab_node::name () const
567 return lang_hooks.decl_printable_name (decl, 2);
570 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
572 /* Dump base fields of symtab nodes. Not to be used directly. */
574 void
575 dump_symtab_base (FILE *f, symtab_node *node)
577 static const char * const visibility_types[] = {
578 "default", "protected", "hidden", "internal"
581 fprintf (f, "%s/%i (%s)",
582 node->asm_name (),
583 node->order,
584 node->name ());
585 dump_addr (f, " @", (void *)node);
586 fprintf (f, "\n Type: %s", symtab_type_names[node->type]);
588 if (node->definition)
589 fprintf (f, " definition");
590 if (node->analyzed)
591 fprintf (f, " analyzed");
592 if (node->alias)
593 fprintf (f, " alias");
594 if (node->weakref)
595 fprintf (f, " weakref");
596 if (node->cpp_implicit_alias)
597 fprintf (f, " cpp_implicit_alias");
598 if (node->alias_target)
599 fprintf (f, " target:%s",
600 DECL_P (node->alias_target)
601 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
602 (node->alias_target))
603 : IDENTIFIER_POINTER (node->alias_target));
604 if (node->body_removed)
605 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
606 fprintf (f, "\n Visibility:");
607 if (node->in_other_partition)
608 fprintf (f, " in_other_partition");
609 if (node->used_from_other_partition)
610 fprintf (f, " used_from_other_partition");
611 if (node->force_output)
612 fprintf (f, " force_output");
613 if (node->forced_by_abi)
614 fprintf (f, " forced_by_abi");
615 if (node->externally_visible)
616 fprintf (f, " externally_visible");
617 if (node->resolution != LDPR_UNKNOWN)
618 fprintf (f, " %s",
619 ld_plugin_symbol_resolution_names[(int)node->resolution]);
620 if (TREE_ASM_WRITTEN (node->decl))
621 fprintf (f, " asm_written");
622 if (DECL_EXTERNAL (node->decl))
623 fprintf (f, " external");
624 if (TREE_PUBLIC (node->decl))
625 fprintf (f, " public");
626 if (DECL_COMMON (node->decl))
627 fprintf (f, " common");
628 if (DECL_WEAK (node->decl))
629 fprintf (f, " weak");
630 if (DECL_DLLIMPORT_P (node->decl))
631 fprintf (f, " dll_import");
632 if (DECL_COMDAT (node->decl))
633 fprintf (f, " comdat");
634 if (DECL_COMDAT_GROUP (node->decl))
635 fprintf (f, " comdat_group:%s",
636 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (node->decl)));
637 if (DECL_ONE_ONLY (node->decl))
638 fprintf (f, " one_only");
639 if (DECL_SECTION_NAME (node->decl))
640 fprintf (f, " section_name:%s",
641 TREE_STRING_POINTER (DECL_SECTION_NAME (node->decl)));
642 if (DECL_VISIBILITY_SPECIFIED (node->decl))
643 fprintf (f, " visibility_specified");
644 if (DECL_VISIBILITY (node->decl))
645 fprintf (f, " visibility:%s",
646 visibility_types [DECL_VISIBILITY (node->decl)]);
647 if (DECL_VIRTUAL_P (node->decl))
648 fprintf (f, " virtual");
649 if (DECL_ARTIFICIAL (node->decl))
650 fprintf (f, " artificial");
651 if (TREE_CODE (node->decl) == FUNCTION_DECL)
653 if (DECL_STATIC_CONSTRUCTOR (node->decl))
654 fprintf (f, " constructor");
655 if (DECL_STATIC_DESTRUCTOR (node->decl))
656 fprintf (f, " destructor");
658 fprintf (f, "\n");
660 if (node->same_comdat_group)
661 fprintf (f, " Same comdat group as: %s/%i\n",
662 node->same_comdat_group->asm_name (),
663 node->same_comdat_group->order);
664 if (node->next_sharing_asm_name)
665 fprintf (f, " next sharing asm name: %i\n",
666 node->next_sharing_asm_name->order);
667 if (node->previous_sharing_asm_name)
668 fprintf (f, " previous sharing asm name: %i\n",
669 node->previous_sharing_asm_name->order);
671 if (node->address_taken)
672 fprintf (f, " Address is taken.\n");
673 if (node->aux)
675 fprintf (f, " Aux:");
676 dump_addr (f, " @", (void *)node->aux);
679 fprintf (f, " References: ");
680 ipa_dump_references (f, &node->ref_list);
681 fprintf (f, " Referring: ");
682 ipa_dump_referring (f, &node->ref_list);
683 if (node->lto_file_data)
684 fprintf (f, " Read from file: %s\n",
685 node->lto_file_data->file_name);
688 /* Dump symtab node. */
690 void
691 dump_symtab_node (FILE *f, symtab_node *node)
693 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
694 dump_cgraph_node (f, cnode);
695 else if (varpool_node *vnode = dyn_cast <varpool_node> (node))
696 dump_varpool_node (f, vnode);
699 /* Dump symbol table. */
701 void
702 dump_symtab (FILE *f)
704 symtab_node *node;
705 fprintf (f, "Symbol table:\n\n");
706 FOR_EACH_SYMBOL (node)
707 dump_symtab_node (f, node);
710 /* Dump symtab node NODE to stderr. */
712 DEBUG_FUNCTION void
713 debug_symtab_node (symtab_node *node)
715 dump_symtab_node (stderr, node);
718 /* Dump symbol table to stderr. */
720 DEBUG_FUNCTION void
721 debug_symtab (void)
723 dump_symtab (stderr);
726 /* Verify common part of symtab nodes. */
728 DEBUG_FUNCTION bool
729 verify_symtab_base (symtab_node *node)
731 bool error_found = false;
732 symtab_node *hashed_node;
734 if (is_a <cgraph_node> (node))
736 if (TREE_CODE (node->decl) != FUNCTION_DECL)
738 error ("function symbol is not function");
739 error_found = true;
742 else if (is_a <varpool_node> (node))
744 if (TREE_CODE (node->decl) != VAR_DECL)
746 error ("variable symbol is not variable");
747 error_found = true;
750 else
752 error ("node has unknown type");
753 error_found = true;
756 if (cgraph_state != CGRAPH_LTO_STREAMING)
758 hashed_node = symtab_get_node (node->decl);
759 if (!hashed_node)
761 error ("node not found in symtab decl hashtable");
762 error_found = true;
764 if (hashed_node != node
765 && (!is_a <cgraph_node> (node)
766 || !dyn_cast <cgraph_node> (node)->clone_of
767 || dyn_cast <cgraph_node> (node)->clone_of->decl
768 != node->decl))
770 error ("node differs from symtab decl hashtable");
771 error_found = true;
774 if (assembler_name_hash)
776 hashed_node = symtab_node_for_asm (DECL_ASSEMBLER_NAME (node->decl));
777 if (hashed_node && hashed_node->previous_sharing_asm_name)
779 error ("assembler name hash list corrupted");
780 error_found = true;
782 while (hashed_node)
784 if (hashed_node == node)
785 break;
786 hashed_node = hashed_node->next_sharing_asm_name;
788 if (!hashed_node
789 && !(is_a <varpool_node> (node)
790 || DECL_HARD_REGISTER (node->decl)))
792 error ("node not found in symtab assembler name hash");
793 error_found = true;
796 if (node->previous_sharing_asm_name
797 && node->previous_sharing_asm_name->next_sharing_asm_name != node)
799 error ("double linked list of assembler names corrupted");
800 error_found = true;
802 if (node->analyzed && !node->definition)
804 error ("node is analyzed byt it is not a definition");
805 error_found = true;
807 if (node->cpp_implicit_alias && !node->alias)
809 error ("node is alias but not implicit alias");
810 error_found = true;
812 if (node->alias && !node->definition
813 && !node->weakref)
815 error ("node is alias but not definition");
816 error_found = true;
818 if (node->weakref && !node->alias)
820 error ("node is weakref but not an alias");
821 error_found = true;
823 if (node->same_comdat_group)
825 symtab_node *n = node->same_comdat_group;
827 if (!DECL_ONE_ONLY (n->decl))
829 error ("non-DECL_ONE_ONLY node in a same_comdat_group list");
830 error_found = true;
832 if (n->type != node->type)
834 error ("mixing different types of symbol in same comdat groups is not supported");
835 error_found = true;
837 if (n == node)
839 error ("node is alone in a comdat group");
840 error_found = true;
844 if (!n->same_comdat_group)
846 error ("same_comdat_group is not a circular list");
847 error_found = true;
848 break;
850 n = n->same_comdat_group;
852 while (n != node);
853 if (symtab_comdat_local_p (node))
855 struct ipa_ref_list *refs = &node->ref_list;
856 struct ipa_ref *ref;
857 for (int i = 0; ipa_ref_list_referring_iterate (refs, i, ref); ++i)
859 if (!symtab_in_same_comdat_p (ref->referring, node))
861 error ("comdat-local symbol referred to by %s outside its "
862 "comdat",
863 identifier_to_locale (ref->referring->name()));
864 error_found = true;
869 return error_found;
872 /* Verify consistency of NODE. */
874 DEBUG_FUNCTION void
875 verify_symtab_node (symtab_node *node)
877 if (seen_error ())
878 return;
880 timevar_push (TV_CGRAPH_VERIFY);
881 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
882 verify_cgraph_node (cnode);
883 else
884 if (verify_symtab_base (node))
886 dump_symtab_node (stderr, node);
887 internal_error ("verify_symtab_node failed");
889 timevar_pop (TV_CGRAPH_VERIFY);
892 /* Verify symbol table for internal consistency. */
894 DEBUG_FUNCTION void
895 verify_symtab (void)
897 symtab_node *node;
898 FOR_EACH_SYMBOL (node)
899 verify_symtab_node (node);
902 /* Return true when RESOLUTION indicate that linker will use
903 the symbol from non-LTO object files. */
905 bool
906 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
908 return (resolution == LDPR_PREVAILING_DEF
909 || resolution == LDPR_PREEMPTED_REG
910 || resolution == LDPR_RESOLVED_EXEC
911 || resolution == LDPR_RESOLVED_DYN);
914 /* Return true when NODE is known to be used from other (non-LTO) object file.
915 Known only when doing LTO via linker plugin. */
917 bool
918 symtab_used_from_object_file_p (symtab_node *node)
920 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
921 return false;
922 if (resolution_used_from_other_file_p (node->resolution))
923 return true;
924 return false;
927 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
928 but other code such as notice_global_symbol generates rtl. */
930 void
931 symtab_make_decl_local (tree decl)
933 rtx rtl, symbol;
935 /* Avoid clearing DECL_COMDAT_GROUP on comdat-local decls. */
936 if (TREE_PUBLIC (decl) == 0)
937 return;
939 if (TREE_CODE (decl) == VAR_DECL)
940 DECL_COMMON (decl) = 0;
941 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
943 if (DECL_ONE_ONLY (decl) || DECL_COMDAT (decl))
945 DECL_SECTION_NAME (decl) = 0;
946 DECL_COMDAT (decl) = 0;
948 DECL_COMDAT_GROUP (decl) = 0;
949 DECL_WEAK (decl) = 0;
950 DECL_EXTERNAL (decl) = 0;
951 DECL_VISIBILITY_SPECIFIED (decl) = 0;
952 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
953 TREE_PUBLIC (decl) = 0;
954 if (!DECL_RTL_SET_P (decl))
955 return;
957 /* Update rtl flags. */
958 make_decl_rtl (decl);
960 rtl = DECL_RTL (decl);
961 if (!MEM_P (rtl))
962 return;
964 symbol = XEXP (rtl, 0);
965 if (GET_CODE (symbol) != SYMBOL_REF)
966 return;
968 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
971 /* Return availability of NODE. */
973 enum availability
974 symtab_node_availability (symtab_node *node)
976 if (is_a <cgraph_node> (node))
977 return cgraph_function_body_availability (cgraph (node));
978 else
979 return cgraph_variable_initializer_availability (varpool (node));
982 /* Given NODE, walk the alias chain to return the symbol NODE is alias of.
983 If NODE is not an alias, return NODE.
984 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
986 symtab_node *
987 symtab_alias_ultimate_target (symtab_node *node, enum availability *availability)
989 bool weakref_p = false;
991 if (!node->alias)
993 if (availability)
994 *availability = symtab_node_availability (node);
995 return node;
998 /* To determine visibility of the target, we follow ELF semantic of aliases.
999 Here alias is an alternative assembler name of a given definition. Its
1000 availability prevails the availability of its target (i.e. static alias of
1001 weak definition is available.
1003 Weakref is a different animal (and not part of ELF per se). It is just
1004 alternative name of a given symbol used within one complation unit
1005 and is translated prior hitting the object file. It inherits the
1006 visibility of its target (i.e. weakref of non-overwritable definition
1007 is non-overwritable, while weakref of weak definition is weak).
1009 If we ever get into supporting targets with different semantics, a target
1010 hook will be needed here. */
1012 if (availability)
1014 weakref_p = node->weakref;
1015 if (!weakref_p)
1016 *availability = symtab_node_availability (node);
1017 else
1018 *availability = AVAIL_LOCAL;
1020 while (node)
1022 if (node->alias && node->analyzed)
1023 node = symtab_alias_target (node);
1024 else
1026 if (!availability)
1028 else if (node->analyzed)
1030 if (weakref_p)
1032 enum availability a = symtab_node_availability (node);
1033 if (a < *availability)
1034 *availability = a;
1037 else
1038 *availability = AVAIL_NOT_AVAILABLE;
1039 return node;
1041 if (node && availability && weakref_p)
1043 enum availability a = symtab_node_availability (node);
1044 if (a < *availability)
1045 *availability = a;
1046 weakref_p = node->weakref;
1049 if (availability)
1050 *availability = AVAIL_NOT_AVAILABLE;
1051 return NULL;
1054 /* C++ FE sometimes change linkage flags after producing same body aliases.
1056 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1057 are obviously equivalent. The way it is doing so is however somewhat
1058 kludgy and interferes with the visibility code. As a result we need to
1059 copy the visibility from the target to get things right. */
1061 void
1062 fixup_same_cpp_alias_visibility (symtab_node *node, symtab_node *target)
1064 if (is_a <cgraph_node> (node))
1066 DECL_DECLARED_INLINE_P (node->decl)
1067 = DECL_DECLARED_INLINE_P (target->decl);
1068 DECL_DISREGARD_INLINE_LIMITS (node->decl)
1069 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1071 /* FIXME: It is not really clear why those flags should not be copied for
1072 functions, too. */
1073 else
1075 DECL_WEAK (node->decl) = DECL_WEAK (target->decl);
1076 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1077 DECL_VISIBILITY (node->decl) = DECL_VISIBILITY (target->decl);
1079 DECL_VIRTUAL_P (node->decl) = DECL_VIRTUAL_P (target->decl);
1080 if (TREE_PUBLIC (node->decl))
1082 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1083 DECL_COMDAT (node->decl) = DECL_COMDAT (target->decl);
1084 DECL_COMDAT_GROUP (node->decl)
1085 = DECL_COMDAT_GROUP (target->decl);
1086 if (DECL_ONE_ONLY (target->decl)
1087 && !node->same_comdat_group)
1088 symtab_add_to_same_comdat_group (node, target);
1090 node->externally_visible = target->externally_visible;
1093 /* Add reference recording that NODE is alias of TARGET.
1094 The function can fail in the case of aliasing cycles; in this case
1095 it returns false. */
1097 bool
1098 symtab_resolve_alias (symtab_node *node, symtab_node *target)
1100 symtab_node *n;
1102 gcc_assert (!node->analyzed
1103 && !vec_safe_length (node->ref_list.references));
1105 /* Never let cycles to creep into the symbol table alias references;
1106 those will make alias walkers to be infinite. */
1107 for (n = target; n && n->alias;
1108 n = n->analyzed ? symtab_alias_target (n) : NULL)
1109 if (n == node)
1111 if (is_a <cgraph_node> (node))
1112 error ("function %q+D part of alias cycle", node->decl);
1113 else if (is_a <varpool_node> (node))
1114 error ("variable %q+D part of alias cycle", node->decl);
1115 else
1116 gcc_unreachable ();
1117 node->alias = false;
1118 return false;
1121 /* "analyze" the node - i.e. mark the reference. */
1122 node->definition = true;
1123 node->alias = true;
1124 node->analyzed = true;
1125 ipa_record_reference (node, target, IPA_REF_ALIAS, NULL);
1127 /* Alias targets become reudndant after alias is resolved into an reference.
1128 We do not want to keep it around or we would have to mind updating them
1129 when renaming symbols. */
1130 node->alias_target = NULL;
1132 if (node->cpp_implicit_alias && cgraph_state >= CGRAPH_STATE_CONSTRUCTION)
1133 fixup_same_cpp_alias_visibility (node, target);
1135 /* If alias has address taken, so does the target. */
1136 if (node->address_taken)
1137 symtab_alias_ultimate_target (target, NULL)->address_taken = true;
1138 return true;
1141 /* Call calback on NODE and aliases associated to NODE.
1142 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1143 skipped. */
1145 bool
1146 symtab_for_node_and_aliases (symtab_node *node,
1147 bool (*callback) (symtab_node *, void *),
1148 void *data,
1149 bool include_overwritable)
1151 int i;
1152 struct ipa_ref *ref;
1154 if (callback (node, data))
1155 return true;
1156 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
1157 if (ref->use == IPA_REF_ALIAS)
1159 symtab_node *alias = ref->referring;
1160 if (include_overwritable
1161 || symtab_node_availability (alias) > AVAIL_OVERWRITABLE)
1162 if (symtab_for_node_and_aliases (alias, callback, data,
1163 include_overwritable))
1164 return true;
1166 return false;
1169 /* Worker searching nonoverwritable alias. */
1171 static bool
1172 symtab_nonoverwritable_alias_1 (symtab_node *node, void *data)
1174 if (decl_binds_to_current_def_p (node->decl))
1176 *(symtab_node **)data = node;
1177 return true;
1179 return false;
1182 /* If NODE can not be overwriten by static or dynamic linker to point to different
1183 definition, return NODE. Otherwise look for alias with such property and if
1184 none exists, introduce new one. */
1186 symtab_node *
1187 symtab_nonoverwritable_alias (symtab_node *node)
1189 tree new_decl;
1190 symtab_node *new_node = NULL;
1192 /* First try to look up existing alias or base object
1193 (if that is already non-overwritable). */
1194 node = symtab_alias_ultimate_target (node, NULL);
1195 gcc_assert (!node->alias && !node->weakref);
1196 symtab_for_node_and_aliases (node, symtab_nonoverwritable_alias_1,
1197 (void *)&new_node, true);
1198 if (new_node)
1199 return new_node;
1200 #ifndef ASM_OUTPUT_DEF
1201 /* If aliases aren't supported by the assembler, fail. */
1202 return NULL;
1203 #endif
1205 /* Otherwise create a new one. */
1206 new_decl = copy_node (node->decl);
1207 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1208 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1209 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1210 DECL_INITIAL (new_decl) = NULL;
1211 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1212 SET_DECL_RTL (new_decl, NULL);
1214 /* Update the properties. */
1215 DECL_EXTERNAL (new_decl) = 0;
1216 if (DECL_ONE_ONLY (node->decl))
1217 DECL_SECTION_NAME (new_decl) = NULL;
1218 DECL_COMDAT_GROUP (new_decl) = 0;
1219 TREE_PUBLIC (new_decl) = 0;
1220 DECL_COMDAT (new_decl) = 0;
1221 DECL_WEAK (new_decl) = 0;
1222 DECL_VIRTUAL_P (new_decl) = 0;
1223 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1225 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1226 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1227 new_node = cgraph_create_function_alias
1228 (new_decl, node->decl);
1230 else
1231 new_node = varpool_create_variable_alias (new_decl,
1232 node->decl);
1233 symtab_resolve_alias (new_node, node);
1234 gcc_assert (decl_binds_to_current_def_p (new_decl));
1235 return new_node;
1238 /* Return true if A and B represents semantically equivalent symbols. */
1240 bool
1241 symtab_semantically_equivalent_p (symtab_node *a,
1242 symtab_node *b)
1244 enum availability avail;
1245 symtab_node *ba;
1246 symtab_node *bb;
1248 /* Equivalent functions are equivalent. */
1249 if (a->decl == b->decl)
1250 return true;
1252 /* If symbol is not overwritable by different implementation,
1253 walk to the base object it defines. */
1254 ba = symtab_alias_ultimate_target (a, &avail);
1255 if (avail >= AVAIL_AVAILABLE)
1257 if (ba == b)
1258 return true;
1260 else
1261 ba = a;
1262 bb = symtab_alias_ultimate_target (b, &avail);
1263 if (avail >= AVAIL_AVAILABLE)
1265 if (a == bb)
1266 return true;
1268 else
1269 bb = b;
1270 return bb == ba;
1273 /* Classify symbol NODE for partitioning. */
1275 enum symbol_partitioning_class
1276 symtab_get_symbol_partitioning_class (symtab_node *node)
1278 /* Inline clones are always duplicated.
1279 This include external delcarations. */
1280 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1282 if (DECL_ABSTRACT (node->decl))
1283 return SYMBOL_EXTERNAL;
1285 if (cnode && cnode->global.inlined_to)
1286 return SYMBOL_DUPLICATE;
1288 /* Weakref aliases are always duplicated. */
1289 if (node->weakref)
1290 return SYMBOL_DUPLICATE;
1292 /* External declarations are external. */
1293 if (DECL_EXTERNAL (node->decl))
1294 return SYMBOL_EXTERNAL;
1296 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
1298 /* Constant pool references use local symbol names that can not
1299 be promoted global. We should never put into a constant pool
1300 objects that can not be duplicated across partitions. */
1301 if (DECL_IN_CONSTANT_POOL (node->decl))
1302 return SYMBOL_DUPLICATE;
1303 gcc_checking_assert (vnode->definition);
1305 /* Functions that are cloned may stay in callgraph even if they are unused.
1306 Handle them as external; compute_ltrans_boundary take care to make
1307 proper things to happen (i.e. to make them appear in the boundary but
1308 with body streamed, so clone can me materialized). */
1309 else if (!cgraph (node)->definition)
1310 return SYMBOL_EXTERNAL;
1312 /* Linker discardable symbols are duplicated to every use unless they are
1313 keyed.
1314 Keyed symbols or those. */
1315 if (DECL_ONE_ONLY (node->decl)
1316 && !node->force_output
1317 && !node->forced_by_abi
1318 && !symtab_used_from_object_file_p (node))
1319 return SYMBOL_DUPLICATE;
1321 return SYMBOL_PARTITION;
1323 #include "gt-symtab.h"