Remove unused variable and field
[official-gcc.git] / gcc / symtab.c
bloba86bf6b132791e6b670519f8e251aceadfab2b6c
1 /* Symbol table.
2 Copyright (C) 2012-2013 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 "tree.h"
26 #include "tree-inline.h"
27 #include "langhooks.h"
28 #include "hashtab.h"
29 #include "ggc.h"
30 #include "cgraph.h"
31 #include "diagnostic.h"
32 #include "timevar.h"
33 #include "lto-streamer.h"
34 #include "rtl.h"
36 const char * const ld_plugin_symbol_resolution_names[]=
38 "",
39 "undef",
40 "prevailing_def",
41 "prevailing_def_ironly",
42 "preempted_reg",
43 "preempted_ir",
44 "resolved_ir",
45 "resolved_exec",
46 "resolved_dyn",
47 "prevailing_def_ironly_exp"
50 /* Hash table used to convert declarations into nodes. */
51 static GTY((param_is (union symtab_node_def))) htab_t symtab_hash;
52 /* Hash table used to convert assembler names into nodes. */
53 static GTY((param_is (union symtab_node_def))) htab_t assembler_name_hash;
55 /* Linked list of symbol table nodes. */
56 symtab_node symtab_nodes;
58 /* The order index of the next symtab node to be created. This is
59 used so that we can sort the cgraph nodes in order by when we saw
60 them, to support -fno-toplevel-reorder. */
61 int symtab_order;
63 /* Returns a hash code for P. */
65 static hashval_t
66 hash_node (const void *p)
68 const_symtab_node n = (const_symtab_node ) p;
69 return (hashval_t) DECL_UID (n->symbol.decl);
73 /* Returns nonzero if P1 and P2 are equal. */
75 static int
76 eq_node (const void *p1, const void *p2)
78 const_symtab_node n1 = (const_symtab_node) p1;
79 const_symtab_node n2 = (const_symtab_node) p2;
80 return DECL_UID (n1->symbol.decl) == DECL_UID (n2->symbol.decl);
83 /* Returns a hash code for P. */
85 static hashval_t
86 hash_node_by_assembler_name (const void *p)
88 const_symtab_node n = (const_symtab_node) p;
89 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->symbol.decl));
92 /* Returns nonzero if P1 and P2 are equal. */
94 static int
95 eq_assembler_name (const void *p1, const void *p2)
97 const_symtab_node n1 = (const_symtab_node) p1;
98 const_tree name = (const_tree)p2;
99 return (decl_assembler_name_equal (n1->symbol.decl, name));
102 /* Insert NODE to assembler name hash. */
104 static void
105 insert_to_assembler_name_hash (symtab_node node, bool with_clones)
107 if (is_a <varpool_node> (node) && DECL_HARD_REGISTER (node->symbol.decl))
108 return;
109 gcc_checking_assert (!node->symbol.previous_sharing_asm_name
110 && !node->symbol.next_sharing_asm_name);
111 if (assembler_name_hash)
113 void **aslot;
114 struct cgraph_node *cnode;
115 tree decl = node->symbol.decl;
117 tree name = DECL_ASSEMBLER_NAME (node->symbol.decl);
119 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
120 decl_assembler_name_hash (name),
121 INSERT);
122 gcc_assert (*aslot != node);
123 node->symbol.next_sharing_asm_name = (symtab_node)*aslot;
124 if (*aslot != NULL)
125 ((symtab_node)*aslot)->symbol.previous_sharing_asm_name = node;
126 *aslot = node;
128 /* Update also possible inline clones sharing a decl. */
129 cnode = dyn_cast <cgraph_node> (node);
130 if (cnode && cnode->clones && with_clones)
131 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
132 if (cnode->symbol.decl == decl)
133 insert_to_assembler_name_hash ((symtab_node) cnode, true);
138 /* Remove NODE from assembler name hash. */
140 static void
141 unlink_from_assembler_name_hash (symtab_node node, bool with_clones)
143 if (assembler_name_hash)
145 struct cgraph_node *cnode;
146 tree decl = node->symbol.decl;
148 if (node->symbol.next_sharing_asm_name)
149 node->symbol.next_sharing_asm_name->symbol.previous_sharing_asm_name
150 = node->symbol.previous_sharing_asm_name;
151 if (node->symbol.previous_sharing_asm_name)
153 node->symbol.previous_sharing_asm_name->symbol.next_sharing_asm_name
154 = node->symbol.next_sharing_asm_name;
156 else
158 tree name = DECL_ASSEMBLER_NAME (node->symbol.decl);
159 void **slot;
160 slot = htab_find_slot_with_hash (assembler_name_hash, name,
161 decl_assembler_name_hash (name),
162 NO_INSERT);
163 gcc_assert (*slot == node);
164 if (!node->symbol.next_sharing_asm_name)
165 htab_clear_slot (assembler_name_hash, slot);
166 else
167 *slot = node->symbol.next_sharing_asm_name;
169 node->symbol.next_sharing_asm_name = NULL;
170 node->symbol.previous_sharing_asm_name = NULL;
172 /* Update also possible inline clones sharing a decl. */
173 cnode = dyn_cast <cgraph_node> (node);
174 if (cnode && cnode->clones && with_clones)
175 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
176 if (cnode->symbol.decl == decl)
177 unlink_from_assembler_name_hash ((symtab_node) cnode, true);
181 /* Arrange node to be first in its entry of assembler_name_hash. */
183 void
184 symtab_prevail_in_asm_name_hash (symtab_node node)
186 unlink_from_assembler_name_hash (node, false);
187 insert_to_assembler_name_hash (node, false);
191 /* Add node into symbol table. This function is not used directly, but via
192 cgraph/varpool node creation routines. */
194 void
195 symtab_register_node (symtab_node node)
197 struct symtab_node_base key;
198 symtab_node *slot;
200 node->symbol.next = symtab_nodes;
201 node->symbol.previous = NULL;
202 if (symtab_nodes)
203 symtab_nodes->symbol.previous = node;
204 symtab_nodes = node;
206 if (!symtab_hash)
207 symtab_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
208 key.decl = node->symbol.decl;
209 slot = (symtab_node *) htab_find_slot (symtab_hash, &key, INSERT);
210 if (*slot == NULL)
211 *slot = node;
213 ipa_empty_ref_list (&node->symbol.ref_list);
215 node->symbol.order = symtab_order++;
217 /* Be sure to do this last; C++ FE might create new nodes via
218 DECL_ASSEMBLER_NAME langhook! */
219 insert_to_assembler_name_hash (node, false);
222 /* Make NODE to be the one symtab hash is pointing to. Used when reshaping tree
223 of inline clones. */
225 void
226 symtab_insert_node_to_hashtable (symtab_node node)
228 struct symtab_node_base key;
229 symtab_node *slot;
231 if (!symtab_hash)
232 symtab_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
233 key.decl = node->symbol.decl;
234 slot = (symtab_node *) htab_find_slot (symtab_hash, &key, INSERT);
235 *slot = node;
238 /* Remove node from symbol table. This function is not used directly, but via
239 cgraph/varpool node removal routines. */
241 void
242 symtab_unregister_node (symtab_node node)
244 void **slot;
245 ipa_remove_all_references (&node->symbol.ref_list);
246 ipa_remove_all_referring (&node->symbol.ref_list);
248 if (node->symbol.same_comdat_group)
250 symtab_node prev;
251 for (prev = node->symbol.same_comdat_group;
252 prev->symbol.same_comdat_group != node;
253 prev = prev->symbol.same_comdat_group)
255 if (node->symbol.same_comdat_group == prev)
256 prev->symbol.same_comdat_group = NULL;
257 else
258 prev->symbol.same_comdat_group = node->symbol.same_comdat_group;
259 node->symbol.same_comdat_group = NULL;
262 if (node->symbol.previous)
263 node->symbol.previous->symbol.next = node->symbol.next;
264 else
265 symtab_nodes = node->symbol.next;
266 if (node->symbol.next)
267 node->symbol.next->symbol.previous = node->symbol.previous;
268 node->symbol.next = NULL;
269 node->symbol.previous = NULL;
271 slot = htab_find_slot (symtab_hash, node, NO_INSERT);
273 /* During LTO symtab merging we temporarily corrupt decl to symtab node
274 hash. */
275 gcc_assert ((slot && *slot) || in_lto_p);
276 if (slot && *slot && *slot == node)
278 symtab_node replacement_node = NULL;
279 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
280 replacement_node = (symtab_node)cgraph_find_replacement_node (cnode);
281 if (!replacement_node)
282 htab_clear_slot (symtab_hash, slot);
283 else
284 *slot = replacement_node;
286 unlink_from_assembler_name_hash (node, false);
289 /* Return symbol table node associated with DECL, if any,
290 and NULL otherwise. */
292 symtab_node
293 symtab_get_node (const_tree decl)
295 symtab_node *slot;
296 struct symtab_node_base key;
298 #ifdef ENABLE_CHECKING
299 /* Check that we are called for sane type of object - functions
300 and static or external variables. */
301 gcc_checking_assert (TREE_CODE (decl) == FUNCTION_DECL
302 || (TREE_CODE (decl) == VAR_DECL
303 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
304 || in_lto_p)));
305 #endif
307 if (!symtab_hash)
308 return NULL;
310 key.decl = CONST_CAST2 (tree, const_tree, decl);
312 slot = (symtab_node *) htab_find_slot (symtab_hash, &key,
313 NO_INSERT);
315 if (slot)
316 return *slot;
317 return NULL;
320 /* Remove symtab NODE from the symbol table. */
322 void
323 symtab_remove_node (symtab_node node)
325 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
326 cgraph_remove_node (cnode);
327 else if (varpool_node *vnode = dyn_cast <varpool_node> (node))
328 varpool_remove_node (vnode);
331 /* Initalize asm name hash unless. */
333 void
334 symtab_initialize_asm_name_hash (void)
336 symtab_node node;
337 if (!assembler_name_hash)
339 assembler_name_hash =
340 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
341 NULL);
342 FOR_EACH_SYMBOL (node)
343 insert_to_assembler_name_hash (node, false);
347 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
348 Return NULL if there's no such node. */
350 symtab_node
351 symtab_node_for_asm (const_tree asmname)
353 symtab_node node;
354 void **slot;
356 symtab_initialize_asm_name_hash ();
357 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
358 decl_assembler_name_hash (asmname),
359 NO_INSERT);
361 if (slot)
363 node = (symtab_node) *slot;
364 return node;
366 return NULL;
369 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
371 void
372 change_decl_assembler_name (tree decl, tree name)
374 symtab_node node = NULL;
376 /* We can have user ASM names on things, like global register variables, that
377 are not in the symbol table. */
378 if ((TREE_CODE (decl) == VAR_DECL
379 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
380 || TREE_CODE (decl) == FUNCTION_DECL)
381 node = symtab_get_node (decl);
382 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
384 SET_DECL_ASSEMBLER_NAME (decl, name);
385 if (node)
386 insert_to_assembler_name_hash (node, true);
388 else
390 if (name == DECL_ASSEMBLER_NAME (decl))
391 return;
393 if (node)
394 unlink_from_assembler_name_hash (node, true);
395 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
396 && DECL_RTL_SET_P (decl))
397 warning (0, "%D renamed after being referenced in assembly", decl);
399 SET_DECL_ASSEMBLER_NAME (decl, name);
400 if (node)
401 insert_to_assembler_name_hash (node, true);
405 /* Add NEW_ to the same comdat group that OLD is in. */
407 void
408 symtab_add_to_same_comdat_group (symtab_node new_node,
409 symtab_node old_node)
411 gcc_assert (DECL_ONE_ONLY (old_node->symbol.decl));
412 gcc_assert (!new_node->symbol.same_comdat_group);
413 gcc_assert (new_node != old_node);
415 DECL_COMDAT_GROUP (new_node->symbol.decl) = DECL_COMDAT_GROUP (old_node->symbol.decl);
416 new_node->symbol.same_comdat_group = old_node;
417 if (!old_node->symbol.same_comdat_group)
418 old_node->symbol.same_comdat_group = new_node;
419 else
421 symtab_node n;
422 for (n = old_node->symbol.same_comdat_group;
423 n->symbol.same_comdat_group != old_node;
424 n = n->symbol.same_comdat_group)
426 n->symbol.same_comdat_group = new_node;
430 /* Dissolve the same_comdat_group list in which NODE resides. */
432 void
433 symtab_dissolve_same_comdat_group_list (symtab_node node)
435 symtab_node n = node, next;
437 if (!node->symbol.same_comdat_group)
438 return;
441 next = n->symbol.same_comdat_group;
442 n->symbol.same_comdat_group = NULL;
443 n = next;
445 while (n != node);
448 /* Return printable assembler name of NODE.
449 This function is used only for debugging. When assembler name
450 is unknown go with identifier name. */
452 const char *
453 symtab_node_asm_name (symtab_node node)
455 if (!DECL_ASSEMBLER_NAME_SET_P (node->symbol.decl))
456 return lang_hooks.decl_printable_name (node->symbol.decl, 2);
457 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->symbol.decl));
460 /* Return printable identifier name. */
462 const char *
463 symtab_node_name (symtab_node node)
465 return lang_hooks.decl_printable_name (node->symbol.decl, 2);
468 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
470 /* Dump base fields of symtab nodes. Not to be used directly. */
472 void
473 dump_symtab_base (FILE *f, symtab_node node)
475 static const char * const visibility_types[] = {
476 "default", "protected", "hidden", "internal"
479 fprintf (f, "%s/%i (%s)",
480 symtab_node_asm_name (node),
481 node->symbol.order,
482 symtab_node_name (node));
483 dump_addr (f, " @", (void *)node);
484 fprintf (f, "\n Type: %s", symtab_type_names[node->symbol.type]);
486 if (node->symbol.definition)
487 fprintf (f, " definition");
488 if (node->symbol.analyzed)
489 fprintf (f, " analyzed");
490 if (node->symbol.alias)
491 fprintf (f, " alias");
492 if (node->symbol.weakref)
493 fprintf (f, " weakref");
494 if (node->symbol.cpp_implicit_alias)
495 fprintf (f, " cpp_implicit_alias");
496 if (node->symbol.alias_target)
497 fprintf (f, " target:%s",
498 DECL_P (node->symbol.alias_target)
499 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
500 (node->symbol.alias_target))
501 : IDENTIFIER_POINTER (node->symbol.alias_target));
502 fprintf (f, "\n Visibility:");
503 if (node->symbol.in_other_partition)
504 fprintf (f, " in_other_partition");
505 if (node->symbol.used_from_other_partition)
506 fprintf (f, " used_from_other_partition");
507 if (node->symbol.force_output)
508 fprintf (f, " force_output");
509 if (node->symbol.forced_by_abi)
510 fprintf (f, " forced_by_abi");
511 if (node->symbol.externally_visible)
512 fprintf (f, " externally_visible");
513 if (node->symbol.resolution != LDPR_UNKNOWN)
514 fprintf (f, " %s",
515 ld_plugin_symbol_resolution_names[(int)node->symbol.resolution]);
516 if (TREE_ASM_WRITTEN (node->symbol.decl))
517 fprintf (f, " asm_written");
518 if (DECL_EXTERNAL (node->symbol.decl))
519 fprintf (f, " external");
520 if (TREE_PUBLIC (node->symbol.decl))
521 fprintf (f, " public");
522 if (DECL_COMMON (node->symbol.decl))
523 fprintf (f, " common");
524 if (DECL_WEAK (node->symbol.decl))
525 fprintf (f, " weak");
526 if (DECL_DLLIMPORT_P (node->symbol.decl))
527 fprintf (f, " dll_import");
528 if (DECL_COMDAT (node->symbol.decl))
529 fprintf (f, " comdat");
530 if (DECL_COMDAT_GROUP (node->symbol.decl))
531 fprintf (f, " comdat_group:%s",
532 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (node->symbol.decl)));
533 if (DECL_ONE_ONLY (node->symbol.decl))
534 fprintf (f, " one_only");
535 if (DECL_SECTION_NAME (node->symbol.decl))
536 fprintf (f, " section_name:%s",
537 TREE_STRING_POINTER (DECL_SECTION_NAME (node->symbol.decl)));
538 if (DECL_VISIBILITY_SPECIFIED (node->symbol.decl))
539 fprintf (f, " visibility_specified");
540 if (DECL_VISIBILITY (node->symbol.decl))
541 fprintf (f, " visibility:%s",
542 visibility_types [DECL_VISIBILITY (node->symbol.decl)]);
543 if (DECL_VIRTUAL_P (node->symbol.decl))
544 fprintf (f, " virtual");
545 if (DECL_ARTIFICIAL (node->symbol.decl))
546 fprintf (f, " artificial");
547 if (TREE_CODE (node->symbol.decl) == FUNCTION_DECL)
549 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
550 fprintf (f, " constructor");
551 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
552 fprintf (f, " destructor");
554 fprintf (f, "\n");
556 if (node->symbol.same_comdat_group)
557 fprintf (f, " Same comdat group as: %s/%i\n",
558 symtab_node_asm_name (node->symbol.same_comdat_group),
559 node->symbol.same_comdat_group->symbol.order);
560 if (node->symbol.next_sharing_asm_name)
561 fprintf (f, " next sharing asm name: %i\n",
562 node->symbol.next_sharing_asm_name->symbol.order);
563 if (node->symbol.previous_sharing_asm_name)
564 fprintf (f, " previous sharing asm name: %i\n",
565 node->symbol.previous_sharing_asm_name->symbol.order);
567 if (node->symbol.address_taken)
568 fprintf (f, " Address is taken.\n");
569 if (node->symbol.aux)
571 fprintf (f, " Aux:");
572 dump_addr (f, " @", (void *)node->symbol.aux);
575 fprintf (f, " References: ");
576 ipa_dump_references (f, &node->symbol.ref_list);
577 fprintf (f, " Referring: ");
578 ipa_dump_referring (f, &node->symbol.ref_list);
579 if (node->symbol.lto_file_data)
580 fprintf (f, " Read from file: %s\n",
581 node->symbol.lto_file_data->file_name);
584 /* Dump symtab node. */
586 void
587 dump_symtab_node (FILE *f, symtab_node node)
589 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
590 dump_cgraph_node (f, cnode);
591 else if (varpool_node *vnode = dyn_cast <varpool_node> (node))
592 dump_varpool_node (f, vnode);
595 /* Dump symbol table. */
597 void
598 dump_symtab (FILE *f)
600 symtab_node node;
601 fprintf (f, "Symbol table:\n\n");
602 FOR_EACH_SYMBOL (node)
603 dump_symtab_node (f, node);
606 /* Dump symtab node NODE to stderr. */
608 DEBUG_FUNCTION void
609 debug_symtab_node (symtab_node node)
611 dump_symtab_node (stderr, node);
614 /* Dump symbol table to stderr. */
616 DEBUG_FUNCTION void
617 debug_symtab (void)
619 dump_symtab (stderr);
622 /* Verify common part of symtab nodes. */
624 DEBUG_FUNCTION bool
625 verify_symtab_base (symtab_node node)
627 bool error_found = false;
628 symtab_node hashed_node;
630 if (is_a <cgraph_node> (node))
632 if (TREE_CODE (node->symbol.decl) != FUNCTION_DECL)
634 error ("function symbol is not function");
635 error_found = true;
638 else if (is_a <varpool_node> (node))
640 if (TREE_CODE (node->symbol.decl) != VAR_DECL)
642 error ("variable symbol is not variable");
643 error_found = true;
646 else
648 error ("node has unknown type");
649 error_found = true;
652 if (cgraph_state != CGRAPH_LTO_STREAMING)
654 hashed_node = symtab_get_node (node->symbol.decl);
655 if (!hashed_node)
657 error ("node not found in symtab decl hashtable");
658 error_found = true;
660 if (hashed_node != node
661 && (!is_a <cgraph_node> (node)
662 || !dyn_cast <cgraph_node> (node)->clone_of
663 || dyn_cast <cgraph_node> (node)->clone_of->symbol.decl
664 != node->symbol.decl))
666 error ("node differs from symtab decl hashtable");
667 error_found = true;
670 if (assembler_name_hash)
672 hashed_node = symtab_node_for_asm (DECL_ASSEMBLER_NAME (node->symbol.decl));
673 if (hashed_node && hashed_node->symbol.previous_sharing_asm_name)
675 error ("assembler name hash list corrupted");
676 error_found = true;
678 while (hashed_node)
680 if (hashed_node == node)
681 break;
682 hashed_node = hashed_node->symbol.next_sharing_asm_name;
684 if (!hashed_node
685 && !(is_a <varpool_node> (node)
686 || DECL_HARD_REGISTER (node->symbol.decl)))
688 error ("node not found in symtab assembler name hash");
689 error_found = true;
692 if (node->symbol.previous_sharing_asm_name
693 && node->symbol.previous_sharing_asm_name->symbol.next_sharing_asm_name != node)
695 error ("double linked list of assembler names corrupted");
696 error_found = true;
698 if (node->symbol.analyzed && !node->symbol.definition)
700 error ("node is analyzed byt it is not a definition");
701 error_found = true;
703 if (node->symbol.cpp_implicit_alias && !node->symbol.alias)
705 error ("node is alias but not implicit alias");
706 error_found = true;
708 if (node->symbol.alias && !node->symbol.definition
709 && !node->symbol.weakref)
711 error ("node is alias but not definition");
712 error_found = true;
714 if (node->symbol.weakref && !node->symbol.alias)
716 error ("node is weakref but not an alias");
717 error_found = true;
719 if (node->symbol.same_comdat_group)
721 symtab_node n = node->symbol.same_comdat_group;
723 if (!DECL_ONE_ONLY (n->symbol.decl))
725 error ("non-DECL_ONE_ONLY node in a same_comdat_group list");
726 error_found = true;
728 if (n->symbol.type != node->symbol.type)
730 error ("mixing different types of symbol in same comdat groups is not supported");
731 error_found = true;
733 if (n == node)
735 error ("node is alone in a comdat group");
736 error_found = true;
740 if (!n->symbol.same_comdat_group)
742 error ("same_comdat_group is not a circular list");
743 error_found = true;
744 break;
746 n = n->symbol.same_comdat_group;
748 while (n != node);
750 return error_found;
753 /* Verify consistency of NODE. */
755 DEBUG_FUNCTION void
756 verify_symtab_node (symtab_node node)
758 if (seen_error ())
759 return;
761 timevar_push (TV_CGRAPH_VERIFY);
762 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
763 verify_cgraph_node (cnode);
764 else
765 if (verify_symtab_base (node))
767 dump_symtab_node (stderr, node);
768 internal_error ("verify_symtab_node failed");
770 timevar_pop (TV_CGRAPH_VERIFY);
773 /* Verify symbol table for internal consistency. */
775 DEBUG_FUNCTION void
776 verify_symtab (void)
778 symtab_node node;
779 FOR_EACH_SYMBOL (node)
780 verify_symtab_node (node);
783 /* Return true when RESOLUTION indicate that linker will use
784 the symbol from non-LTO object files. */
786 bool
787 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
789 return (resolution == LDPR_PREVAILING_DEF
790 || resolution == LDPR_PREEMPTED_REG
791 || resolution == LDPR_RESOLVED_EXEC
792 || resolution == LDPR_RESOLVED_DYN);
795 /* Return true when NODE is known to be used from other (non-LTO) object file.
796 Known only when doing LTO via linker plugin. */
798 bool
799 symtab_used_from_object_file_p (symtab_node node)
801 if (!TREE_PUBLIC (node->symbol.decl) || DECL_EXTERNAL (node->symbol.decl))
802 return false;
803 if (resolution_used_from_other_file_p (node->symbol.resolution))
804 return true;
805 return false;
808 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
809 but other code such as notice_global_symbol generates rtl. */
811 void
812 symtab_make_decl_local (tree decl)
814 rtx rtl, symbol;
816 if (TREE_CODE (decl) == VAR_DECL)
817 DECL_COMMON (decl) = 0;
818 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
820 if (DECL_ONE_ONLY (decl) || DECL_COMDAT (decl))
822 DECL_SECTION_NAME (decl) = 0;
823 DECL_COMDAT (decl) = 0;
825 DECL_COMDAT_GROUP (decl) = 0;
826 DECL_WEAK (decl) = 0;
827 DECL_EXTERNAL (decl) = 0;
828 DECL_VISIBILITY_SPECIFIED (decl) = 0;
829 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
830 TREE_PUBLIC (decl) = 0;
831 if (!DECL_RTL_SET_P (decl))
832 return;
834 /* Update rtl flags. */
835 make_decl_rtl (decl);
837 rtl = DECL_RTL (decl);
838 if (!MEM_P (rtl))
839 return;
841 symbol = XEXP (rtl, 0);
842 if (GET_CODE (symbol) != SYMBOL_REF)
843 return;
845 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
848 /* Return availability of NODE. */
850 enum availability
851 symtab_node_availability (symtab_node node)
853 if (is_a <cgraph_node> (node))
854 return cgraph_function_body_availability (cgraph (node));
855 else
856 return cgraph_variable_initializer_availability (varpool (node));
859 /* Given NODE, walk the alias chain to return the symbol NODE is alias of.
860 If NODE is not an alias, return NODE.
861 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
863 symtab_node
864 symtab_alias_ultimate_target (symtab_node node, enum availability *availability)
866 bool weakref_p = false;
868 if (!node->symbol.alias)
870 if (availability)
871 *availability = symtab_node_availability (node);
872 return node;
875 /* To determine visibility of the target, we follow ELF semantic of aliases.
876 Here alias is an alternative assembler name of a given definition. Its
877 availability prevails the availability of its target (i.e. static alias of
878 weak definition is available.
880 Weakref is a different animal (and not part of ELF per se). It is just
881 alternative name of a given symbol used within one complation unit
882 and is translated prior hitting the object file. It inherits the
883 visibility of its target (i.e. weakref of non-overwritable definition
884 is non-overwritable, while weakref of weak definition is weak).
886 If we ever get into supporting targets with different semantics, a target
887 hook will be needed here. */
889 if (availability)
891 weakref_p = node->symbol.weakref;
892 if (!weakref_p)
893 *availability = symtab_node_availability (node);
894 else
895 *availability = AVAIL_LOCAL;
897 while (node)
899 if (node->symbol.alias && node->symbol.analyzed)
900 node = symtab_alias_target (node);
901 else
903 if (!availability)
905 else if (node->symbol.analyzed)
907 if (weakref_p)
909 enum availability a = symtab_node_availability (node);
910 if (a < *availability)
911 *availability = a;
914 else
915 *availability = AVAIL_NOT_AVAILABLE;
916 return node;
918 if (node && availability && weakref_p)
920 enum availability a = symtab_node_availability (node);
921 if (a < *availability)
922 *availability = a;
923 weakref_p = node->symbol.weakref;
926 if (availability)
927 *availability = AVAIL_NOT_AVAILABLE;
928 return NULL;
931 /* C++ FE sometimes change linkage flags after producing same body aliases.
933 FIXME: C++ produce implicit aliases for virtual functions and vtables that
934 are obviously equivalent. The way it is doing so is however somewhat
935 kludgy and interferes with the visibility code. As a result we need to
936 copy the visibility from the target to get things right. */
938 void
939 fixup_same_cpp_alias_visibility (symtab_node node, symtab_node target)
941 if (is_a <cgraph_node> (node))
943 DECL_DECLARED_INLINE_P (node->symbol.decl)
944 = DECL_DECLARED_INLINE_P (target->symbol.decl);
945 DECL_DISREGARD_INLINE_LIMITS (node->symbol.decl)
946 = DECL_DISREGARD_INLINE_LIMITS (target->symbol.decl);
948 /* FIXME: It is not really clear why those flags should not be copied for
949 functions, too. */
950 else
952 DECL_WEAK (node->symbol.decl) = DECL_WEAK (target->symbol.decl);
953 DECL_EXTERNAL (node->symbol.decl) = DECL_EXTERNAL (target->symbol.decl);
954 DECL_VISIBILITY (node->symbol.decl) = DECL_VISIBILITY (target->symbol.decl);
956 DECL_VIRTUAL_P (node->symbol.decl) = DECL_VIRTUAL_P (target->symbol.decl);
957 if (TREE_PUBLIC (node->symbol.decl))
959 DECL_EXTERNAL (node->symbol.decl) = DECL_EXTERNAL (target->symbol.decl);
960 DECL_COMDAT (node->symbol.decl) = DECL_COMDAT (target->symbol.decl);
961 DECL_COMDAT_GROUP (node->symbol.decl)
962 = DECL_COMDAT_GROUP (target->symbol.decl);
963 if (DECL_ONE_ONLY (target->symbol.decl)
964 && !node->symbol.same_comdat_group)
965 symtab_add_to_same_comdat_group ((symtab_node)node, (symtab_node)target);
967 node->symbol.externally_visible = target->symbol.externally_visible;
970 /* Add reference recording that NODE is alias of TARGET.
971 The function can fail in the case of aliasing cycles; in this case
972 it returns false. */
974 bool
975 symtab_resolve_alias (symtab_node node, symtab_node target)
977 symtab_node n;
979 gcc_assert (!node->symbol.analyzed
980 && !vec_safe_length (node->symbol.ref_list.references));
982 /* Never let cycles to creep into the symbol table alias references;
983 those will make alias walkers to be infinite. */
984 for (n = target; n && n->symbol.alias;
985 n = n->symbol.analyzed ? symtab_alias_target (n) : NULL)
986 if (n == node)
988 if (is_a <cgraph_node> (node))
989 error ("function %q+D part of alias cycle", node->symbol.decl);
990 else if (is_a <varpool_node> (node))
991 error ("variable %q+D part of alias cycle", node->symbol.decl);
992 else
993 gcc_unreachable ();
994 node->symbol.alias = false;
995 return false;
998 /* "analyze" the node - i.e. mark the reference. */
999 node->symbol.definition = true;
1000 node->symbol.alias = true;
1001 node->symbol.analyzed = true;
1002 ipa_record_reference (node, target, IPA_REF_ALIAS, NULL);
1004 /* Alias targets become reudndant after alias is resolved into an reference.
1005 We do not want to keep it around or we would have to mind updating them
1006 when renaming symbols. */
1007 node->symbol.alias_target = NULL;
1009 if (node->symbol.cpp_implicit_alias && cgraph_state >= CGRAPH_STATE_CONSTRUCTION)
1010 fixup_same_cpp_alias_visibility (node, target);
1012 /* If alias has address taken, so does the target. */
1013 if (node->symbol.address_taken)
1014 symtab_alias_ultimate_target (target, NULL)->symbol.address_taken = true;
1015 return true;
1018 /* Call calback on NODE and aliases associated to NODE.
1019 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1020 skipped. */
1022 bool
1023 symtab_for_node_and_aliases (symtab_node node,
1024 bool (*callback) (symtab_node, void *),
1025 void *data,
1026 bool include_overwritable)
1028 int i;
1029 struct ipa_ref *ref;
1031 if (callback (node, data))
1032 return true;
1033 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
1034 if (ref->use == IPA_REF_ALIAS)
1036 symtab_node alias = ref->referring;
1037 if (include_overwritable
1038 || symtab_node_availability (alias) > AVAIL_OVERWRITABLE)
1039 if (symtab_for_node_and_aliases (alias, callback, data,
1040 include_overwritable))
1041 return true;
1043 return false;
1046 /* Worker searching nonoverwritable alias. */
1048 static bool
1049 symtab_nonoverwritable_alias_1 (symtab_node node, void *data)
1051 if (decl_binds_to_current_def_p (node->symbol.decl))
1053 *(symtab_node *)data = node;
1054 return true;
1056 return false;
1059 /* If NODE can not be overwriten by static or dynamic linker to point to different
1060 definition, return NODE. Otherwise look for alias with such property and if
1061 none exists, introduce new one. */
1063 symtab_node
1064 symtab_nonoverwritable_alias (symtab_node node)
1066 tree new_decl;
1067 symtab_node new_node = NULL;
1068 symtab_for_node_and_aliases (node, symtab_nonoverwritable_alias_1,
1069 (void *)&new_node, true);
1070 if (new_node)
1071 return new_node;
1073 new_decl = copy_node (node->symbol.decl);
1074 DECL_NAME (new_decl) = clone_function_name (node->symbol.decl, "localalias");
1075 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1076 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1077 DECL_INITIAL (new_decl) = NULL;
1078 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1079 SET_DECL_RTL (new_decl, NULL);
1081 /* Update the properties. */
1082 DECL_EXTERNAL (new_decl) = 0;
1083 if (DECL_ONE_ONLY (node->symbol.decl))
1084 DECL_SECTION_NAME (new_decl) = NULL;
1085 DECL_COMDAT_GROUP (new_decl) = 0;
1086 TREE_PUBLIC (new_decl) = 0;
1087 DECL_COMDAT (new_decl) = 0;
1088 DECL_WEAK (new_decl) = 0;
1089 DECL_VIRTUAL_P (new_decl) = 0;
1090 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1092 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1093 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1094 new_node = (symtab_node) cgraph_create_function_alias (new_decl, node->symbol.decl);
1096 else
1097 new_node = (symtab_node) varpool_create_variable_alias (new_decl, node->symbol.decl);
1098 symtab_resolve_alias (new_node, node);
1099 return new_node;
1101 #include "gt-symtab.h"