* ChangeLog-2013: Correct an old entry.
[official-gcc.git] / gcc / symtab.c
blob8abb7a13b6d81c9179f730a34402e0dd07f36177
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 assembler names into nodes. */
61 static GTY((param_is (symtab_node))) htab_t assembler_name_hash;
63 /* Linked list of symbol table nodes. */
64 symtab_node *symtab_nodes;
66 /* The order index of the next symtab node to be created. This is
67 used so that we can sort the cgraph nodes in order by when we saw
68 them, to support -fno-toplevel-reorder. */
69 int symtab_order;
71 /* Hash asmnames ignoring the user specified marks. */
73 static hashval_t
74 decl_assembler_name_hash (const_tree asmname)
76 if (IDENTIFIER_POINTER (asmname)[0] == '*')
78 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
79 size_t ulp_len = strlen (user_label_prefix);
81 if (ulp_len == 0)
83 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
84 decl_str += ulp_len;
86 return htab_hash_string (decl_str);
89 return htab_hash_string (IDENTIFIER_POINTER (asmname));
93 /* Returns a hash code for P. */
95 static hashval_t
96 hash_node_by_assembler_name (const void *p)
98 const symtab_node *n = (const symtab_node *) p;
99 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
102 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
104 static bool
105 decl_assembler_name_equal (tree decl, const_tree asmname)
107 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
108 const char *decl_str;
109 const char *asmname_str;
110 bool test = false;
112 if (decl_asmname == asmname)
113 return true;
115 decl_str = IDENTIFIER_POINTER (decl_asmname);
116 asmname_str = IDENTIFIER_POINTER (asmname);
119 /* If the target assembler name was set by the user, things are trickier.
120 We have a leading '*' to begin with. After that, it's arguable what
121 is the correct thing to do with -fleading-underscore. Arguably, we've
122 historically been doing the wrong thing in assemble_alias by always
123 printing the leading underscore. Since we're not changing that, make
124 sure user_label_prefix follows the '*' before matching. */
125 if (decl_str[0] == '*')
127 size_t ulp_len = strlen (user_label_prefix);
129 decl_str ++;
131 if (ulp_len == 0)
132 test = true;
133 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
134 decl_str += ulp_len, test=true;
135 else
136 decl_str --;
138 if (asmname_str[0] == '*')
140 size_t ulp_len = strlen (user_label_prefix);
142 asmname_str ++;
144 if (ulp_len == 0)
145 test = true;
146 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
147 asmname_str += ulp_len, test=true;
148 else
149 asmname_str --;
152 if (!test)
153 return false;
154 return strcmp (decl_str, asmname_str) == 0;
158 /* Returns nonzero if P1 and P2 are equal. */
160 static int
161 eq_assembler_name (const void *p1, const void *p2)
163 const symtab_node *n1 = (const symtab_node *) p1;
164 const_tree name = (const_tree)p2;
165 return (decl_assembler_name_equal (n1->decl, name));
168 /* Insert NODE to assembler name hash. */
170 static void
171 insert_to_assembler_name_hash (symtab_node *node, bool with_clones)
173 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
174 return;
175 gcc_checking_assert (!node->previous_sharing_asm_name
176 && !node->next_sharing_asm_name);
177 if (assembler_name_hash)
179 void **aslot;
180 struct cgraph_node *cnode;
181 tree decl = node->decl;
183 tree name = DECL_ASSEMBLER_NAME (node->decl);
185 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
186 decl_assembler_name_hash (name),
187 INSERT);
188 gcc_assert (*aslot != node);
189 node->next_sharing_asm_name = (symtab_node *)*aslot;
190 if (*aslot != NULL)
191 ((symtab_node *)*aslot)->previous_sharing_asm_name = node;
192 *aslot = node;
194 /* Update also possible inline clones sharing a decl. */
195 cnode = dyn_cast <cgraph_node *> (node);
196 if (cnode && cnode->clones && with_clones)
197 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
198 if (cnode->decl == decl)
199 insert_to_assembler_name_hash (cnode, true);
204 /* Remove NODE from assembler name hash. */
206 static void
207 unlink_from_assembler_name_hash (symtab_node *node, bool with_clones)
209 if (assembler_name_hash)
211 struct cgraph_node *cnode;
212 tree decl = node->decl;
214 if (node->next_sharing_asm_name)
215 node->next_sharing_asm_name->previous_sharing_asm_name
216 = node->previous_sharing_asm_name;
217 if (node->previous_sharing_asm_name)
219 node->previous_sharing_asm_name->next_sharing_asm_name
220 = node->next_sharing_asm_name;
222 else
224 tree name = DECL_ASSEMBLER_NAME (node->decl);
225 void **slot;
226 slot = htab_find_slot_with_hash (assembler_name_hash, name,
227 decl_assembler_name_hash (name),
228 NO_INSERT);
229 gcc_assert (*slot == node);
230 if (!node->next_sharing_asm_name)
231 htab_clear_slot (assembler_name_hash, slot);
232 else
233 *slot = node->next_sharing_asm_name;
235 node->next_sharing_asm_name = NULL;
236 node->previous_sharing_asm_name = NULL;
238 /* Update also possible inline clones sharing a decl. */
239 cnode = dyn_cast <cgraph_node *> (node);
240 if (cnode && cnode->clones && with_clones)
241 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
242 if (cnode->decl == decl)
243 unlink_from_assembler_name_hash (cnode, true);
247 /* Arrange node to be first in its entry of assembler_name_hash. */
249 void
250 symtab_prevail_in_asm_name_hash (symtab_node *node)
252 unlink_from_assembler_name_hash (node, false);
253 insert_to_assembler_name_hash (node, false);
257 /* Add node into symbol table. This function is not used directly, but via
258 cgraph/varpool node creation routines. */
260 void
261 symtab_register_node (symtab_node *node)
263 node->next = symtab_nodes;
264 node->previous = NULL;
265 if (symtab_nodes)
266 symtab_nodes->previous = node;
267 symtab_nodes = node;
269 if (!node->decl->decl_with_vis.symtab_node)
270 node->decl->decl_with_vis.symtab_node = node;
272 ipa_empty_ref_list (&node->ref_list);
274 node->order = symtab_order++;
276 /* Be sure to do this last; C++ FE might create new nodes via
277 DECL_ASSEMBLER_NAME langhook! */
278 insert_to_assembler_name_hash (node, false);
281 /* Remove NODE from same comdat group. */
283 void
284 symtab_remove_from_same_comdat_group (symtab_node *node)
286 if (node->same_comdat_group)
288 symtab_node *prev;
289 for (prev = node->same_comdat_group;
290 prev->same_comdat_group != node;
291 prev = prev->same_comdat_group)
293 if (node->same_comdat_group == prev)
294 prev->same_comdat_group = NULL;
295 else
296 prev->same_comdat_group = node->same_comdat_group;
297 node->same_comdat_group = NULL;
301 /* Remove node from symbol table. This function is not used directly, but via
302 cgraph/varpool node removal routines. */
304 void
305 symtab_unregister_node (symtab_node *node)
307 ipa_remove_all_references (&node->ref_list);
308 ipa_remove_all_referring (&node->ref_list);
310 symtab_remove_from_same_comdat_group (node);
312 if (node->previous)
313 node->previous->next = node->next;
314 else
315 symtab_nodes = node->next;
316 if (node->next)
317 node->next->previous = node->previous;
318 node->next = NULL;
319 node->previous = NULL;
321 /* During LTO symtab merging we temporarily corrupt decl to symtab node
322 hash. */
323 gcc_assert (node->decl->decl_with_vis.symtab_node || in_lto_p);
324 if (node->decl->decl_with_vis.symtab_node == node)
326 symtab_node *replacement_node = NULL;
327 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
328 replacement_node = cgraph_find_replacement_node (cnode);
329 node->decl->decl_with_vis.symtab_node = replacement_node;
331 if (!is_a <varpool_node *> (node) || !DECL_HARD_REGISTER (node->decl))
332 unlink_from_assembler_name_hash (node, false);
336 /* Remove symtab NODE from the symbol table. */
338 void
339 symtab_remove_node (symtab_node *node)
341 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
342 cgraph_remove_node (cnode);
343 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
344 varpool_remove_node (vnode);
347 /* Initalize asm name hash unless. */
349 void
350 symtab_initialize_asm_name_hash (void)
352 symtab_node *node;
353 if (!assembler_name_hash)
355 assembler_name_hash =
356 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
357 NULL);
358 FOR_EACH_SYMBOL (node)
359 insert_to_assembler_name_hash (node, false);
363 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
364 Return NULL if there's no such node. */
366 symtab_node *
367 symtab_node_for_asm (const_tree asmname)
369 symtab_node *node;
370 void **slot;
372 symtab_initialize_asm_name_hash ();
373 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
374 decl_assembler_name_hash (asmname),
375 NO_INSERT);
377 if (slot)
379 node = (symtab_node *) *slot;
380 return node;
382 return NULL;
385 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
387 void
388 change_decl_assembler_name (tree decl, tree name)
390 symtab_node *node = NULL;
392 /* We can have user ASM names on things, like global register variables, that
393 are not in the symbol table. */
394 if ((TREE_CODE (decl) == VAR_DECL
395 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
396 || TREE_CODE (decl) == FUNCTION_DECL)
397 node = symtab_get_node (decl);
398 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
400 SET_DECL_ASSEMBLER_NAME (decl, name);
401 if (node)
402 insert_to_assembler_name_hash (node, true);
404 else
406 if (name == DECL_ASSEMBLER_NAME (decl))
407 return;
409 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
410 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
411 : NULL);
412 if (node)
413 unlink_from_assembler_name_hash (node, true);
414 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
415 && DECL_RTL_SET_P (decl))
416 warning (0, "%D renamed after being referenced in assembly", decl);
418 SET_DECL_ASSEMBLER_NAME (decl, name);
419 if (alias)
421 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
422 TREE_CHAIN (name) = alias;
424 if (node)
425 insert_to_assembler_name_hash (node, true);
429 /* Add NEW_ to the same comdat group that OLD is in. */
431 void
432 symtab_add_to_same_comdat_group (symtab_node *new_node,
433 symtab_node *old_node)
435 gcc_assert (old_node->get_comdat_group ());
436 gcc_assert (!new_node->same_comdat_group);
437 gcc_assert (new_node != old_node);
439 new_node->set_comdat_group (old_node->get_comdat_group ());
440 new_node->same_comdat_group = old_node;
441 if (!old_node->same_comdat_group)
442 old_node->same_comdat_group = new_node;
443 else
445 symtab_node *n;
446 for (n = old_node->same_comdat_group;
447 n->same_comdat_group != old_node;
448 n = n->same_comdat_group)
450 n->same_comdat_group = new_node;
454 /* Dissolve the same_comdat_group list in which NODE resides. */
456 void
457 symtab_dissolve_same_comdat_group_list (symtab_node *node)
459 symtab_node *n = node;
460 symtab_node *next;
462 if (!node->same_comdat_group)
463 return;
466 next = n->same_comdat_group;
467 n->same_comdat_group = NULL;
468 /* Clear comdat_group for comdat locals, since
469 make_decl_local doesn't. */
470 if (!TREE_PUBLIC (n->decl))
471 n->set_comdat_group (NULL);
472 n = next;
474 while (n != node);
477 /* Return printable assembler name of NODE.
478 This function is used only for debugging. When assembler name
479 is unknown go with identifier name. */
481 const char *
482 symtab_node::asm_name () const
484 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
485 return lang_hooks.decl_printable_name (decl, 2);
486 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
489 /* Return printable identifier name. */
491 const char *
492 symtab_node::name () const
494 return lang_hooks.decl_printable_name (decl, 2);
497 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
499 /* Dump base fields of symtab nodes. Not to be used directly. */
501 void
502 dump_symtab_base (FILE *f, symtab_node *node)
504 static const char * const visibility_types[] = {
505 "default", "protected", "hidden", "internal"
508 fprintf (f, "%s/%i (%s)",
509 node->asm_name (),
510 node->order,
511 node->name ());
512 dump_addr (f, " @", (void *)node);
513 fprintf (f, "\n Type: %s", symtab_type_names[node->type]);
515 if (node->definition)
516 fprintf (f, " definition");
517 if (node->analyzed)
518 fprintf (f, " analyzed");
519 if (node->alias)
520 fprintf (f, " alias");
521 if (node->weakref)
522 fprintf (f, " weakref");
523 if (node->cpp_implicit_alias)
524 fprintf (f, " cpp_implicit_alias");
525 if (node->alias_target)
526 fprintf (f, " target:%s",
527 DECL_P (node->alias_target)
528 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
529 (node->alias_target))
530 : IDENTIFIER_POINTER (node->alias_target));
531 if (node->body_removed)
532 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
533 fprintf (f, "\n Visibility:");
534 if (node->in_other_partition)
535 fprintf (f, " in_other_partition");
536 if (node->used_from_other_partition)
537 fprintf (f, " used_from_other_partition");
538 if (node->force_output)
539 fprintf (f, " force_output");
540 if (node->forced_by_abi)
541 fprintf (f, " forced_by_abi");
542 if (node->externally_visible)
543 fprintf (f, " externally_visible");
544 if (node->resolution != LDPR_UNKNOWN)
545 fprintf (f, " %s",
546 ld_plugin_symbol_resolution_names[(int)node->resolution]);
547 if (TREE_ASM_WRITTEN (node->decl))
548 fprintf (f, " asm_written");
549 if (DECL_EXTERNAL (node->decl))
550 fprintf (f, " external");
551 if (TREE_PUBLIC (node->decl))
552 fprintf (f, " public");
553 if (DECL_COMMON (node->decl))
554 fprintf (f, " common");
555 if (DECL_WEAK (node->decl))
556 fprintf (f, " weak");
557 if (DECL_DLLIMPORT_P (node->decl))
558 fprintf (f, " dll_import");
559 if (DECL_COMDAT (node->decl))
560 fprintf (f, " comdat");
561 if (node->get_comdat_group ())
562 fprintf (f, " comdat_group:%s",
563 IDENTIFIER_POINTER (node->get_comdat_group ()));
564 if (DECL_ONE_ONLY (node->decl))
565 fprintf (f, " one_only");
566 if (DECL_SECTION_NAME (node->decl))
567 fprintf (f, " section_name:%s",
568 TREE_STRING_POINTER (DECL_SECTION_NAME (node->decl)));
569 if (DECL_VISIBILITY_SPECIFIED (node->decl))
570 fprintf (f, " visibility_specified");
571 if (DECL_VISIBILITY (node->decl))
572 fprintf (f, " visibility:%s",
573 visibility_types [DECL_VISIBILITY (node->decl)]);
574 if (DECL_VIRTUAL_P (node->decl))
575 fprintf (f, " virtual");
576 if (DECL_ARTIFICIAL (node->decl))
577 fprintf (f, " artificial");
578 if (TREE_CODE (node->decl) == FUNCTION_DECL)
580 if (DECL_STATIC_CONSTRUCTOR (node->decl))
581 fprintf (f, " constructor");
582 if (DECL_STATIC_DESTRUCTOR (node->decl))
583 fprintf (f, " destructor");
585 fprintf (f, "\n");
587 if (node->same_comdat_group)
588 fprintf (f, " Same comdat group as: %s/%i\n",
589 node->same_comdat_group->asm_name (),
590 node->same_comdat_group->order);
591 if (node->next_sharing_asm_name)
592 fprintf (f, " next sharing asm name: %i\n",
593 node->next_sharing_asm_name->order);
594 if (node->previous_sharing_asm_name)
595 fprintf (f, " previous sharing asm name: %i\n",
596 node->previous_sharing_asm_name->order);
598 if (node->address_taken)
599 fprintf (f, " Address is taken.\n");
600 if (node->aux)
602 fprintf (f, " Aux:");
603 dump_addr (f, " @", (void *)node->aux);
606 fprintf (f, " References: ");
607 ipa_dump_references (f, &node->ref_list);
608 fprintf (f, " Referring: ");
609 ipa_dump_referring (f, &node->ref_list);
610 if (node->lto_file_data)
611 fprintf (f, " Read from file: %s\n",
612 node->lto_file_data->file_name);
615 /* Dump symtab node. */
617 void
618 dump_symtab_node (FILE *f, symtab_node *node)
620 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
621 dump_cgraph_node (f, cnode);
622 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
623 dump_varpool_node (f, vnode);
626 /* Dump symbol table. */
628 void
629 dump_symtab (FILE *f)
631 symtab_node *node;
632 fprintf (f, "Symbol table:\n\n");
633 FOR_EACH_SYMBOL (node)
634 dump_symtab_node (f, node);
637 /* Dump symtab node NODE to stderr. */
639 DEBUG_FUNCTION void
640 debug_symtab_node (symtab_node *node)
642 dump_symtab_node (stderr, node);
645 /* Dump symbol table to stderr. */
647 DEBUG_FUNCTION void
648 debug_symtab (void)
650 dump_symtab (stderr);
653 /* Verify common part of symtab nodes. */
655 DEBUG_FUNCTION bool
656 verify_symtab_base (symtab_node *node)
658 bool error_found = false;
659 symtab_node *hashed_node;
661 if (is_a <cgraph_node *> (node))
663 if (TREE_CODE (node->decl) != FUNCTION_DECL)
665 error ("function symbol is not function");
666 error_found = true;
669 else if (is_a <varpool_node *> (node))
671 if (TREE_CODE (node->decl) != VAR_DECL)
673 error ("variable symbol is not variable");
674 error_found = true;
677 else
679 error ("node has unknown type");
680 error_found = true;
683 if (cgraph_state != CGRAPH_LTO_STREAMING)
685 hashed_node = symtab_get_node (node->decl);
686 if (!hashed_node)
688 error ("node not found node->decl->decl_with_vis.symtab_node");
689 error_found = true;
691 if (hashed_node != node
692 && (!is_a <cgraph_node *> (node)
693 || !dyn_cast <cgraph_node *> (node)->clone_of
694 || dyn_cast <cgraph_node *> (node)->clone_of->decl
695 != node->decl))
697 error ("node differs from node->decl->decl_with_vis.symtab_node");
698 error_found = true;
701 if (assembler_name_hash)
703 hashed_node = symtab_node_for_asm (DECL_ASSEMBLER_NAME (node->decl));
704 if (hashed_node && hashed_node->previous_sharing_asm_name)
706 error ("assembler name hash list corrupted");
707 error_found = true;
709 while (hashed_node)
711 if (hashed_node == node)
712 break;
713 hashed_node = hashed_node->next_sharing_asm_name;
715 if (!hashed_node
716 && !(is_a <varpool_node *> (node)
717 || DECL_HARD_REGISTER (node->decl)))
719 error ("node not found in symtab assembler name hash");
720 error_found = true;
723 if (node->previous_sharing_asm_name
724 && node->previous_sharing_asm_name->next_sharing_asm_name != node)
726 error ("double linked list of assembler names corrupted");
727 error_found = true;
729 if (node->analyzed && !node->definition)
731 error ("node is analyzed byt it is not a definition");
732 error_found = true;
734 if (node->cpp_implicit_alias && !node->alias)
736 error ("node is alias but not implicit alias");
737 error_found = true;
739 if (node->alias && !node->definition
740 && !node->weakref)
742 error ("node is alias but not definition");
743 error_found = true;
745 if (node->weakref && !node->alias)
747 error ("node is weakref but not an alias");
748 error_found = true;
750 if (node->same_comdat_group)
752 symtab_node *n = node->same_comdat_group;
754 if (!n->get_comdat_group ())
756 error ("node is in same_comdat_group list but has no comdat_group");
757 error_found = true;
759 if (n->get_comdat_group () != node->get_comdat_group ())
761 error ("same_comdat_group list across different groups");
762 error_found = true;
764 if (!n->definition)
766 error ("Node has same_comdat_group but it is not a definition");
767 error_found = true;
769 if (n->type != node->type)
771 error ("mixing different types of symbol in same comdat groups is not supported");
772 error_found = true;
774 if (n == node)
776 error ("node is alone in a comdat group");
777 error_found = true;
781 if (!n->same_comdat_group)
783 error ("same_comdat_group is not a circular list");
784 error_found = true;
785 break;
787 n = n->same_comdat_group;
789 while (n != node);
790 if (symtab_comdat_local_p (node))
792 struct ipa_ref_list *refs = &node->ref_list;
793 struct ipa_ref *ref;
794 for (int i = 0; ipa_ref_list_referring_iterate (refs, i, ref); ++i)
796 if (!symtab_in_same_comdat_p (ref->referring, node))
798 error ("comdat-local symbol referred to by %s outside its "
799 "comdat",
800 identifier_to_locale (ref->referring->name()));
801 error_found = true;
806 return error_found;
809 /* Verify consistency of NODE. */
811 DEBUG_FUNCTION void
812 verify_symtab_node (symtab_node *node)
814 if (seen_error ())
815 return;
817 timevar_push (TV_CGRAPH_VERIFY);
818 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
819 verify_cgraph_node (cnode);
820 else
821 if (verify_symtab_base (node))
823 dump_symtab_node (stderr, node);
824 internal_error ("verify_symtab_node failed");
826 timevar_pop (TV_CGRAPH_VERIFY);
829 /* Verify symbol table for internal consistency. */
831 DEBUG_FUNCTION void
832 verify_symtab (void)
834 symtab_node *node;
835 FOR_EACH_SYMBOL (node)
836 verify_symtab_node (node);
839 /* Return true when RESOLUTION indicate that linker will use
840 the symbol from non-LTO object files. */
842 bool
843 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
845 return (resolution == LDPR_PREVAILING_DEF
846 || resolution == LDPR_PREEMPTED_REG
847 || resolution == LDPR_RESOLVED_EXEC
848 || resolution == LDPR_RESOLVED_DYN);
851 /* Return true when NODE is known to be used from other (non-LTO) object file.
852 Known only when doing LTO via linker plugin. */
854 bool
855 symtab_used_from_object_file_p (symtab_node *node)
857 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
858 return false;
859 if (resolution_used_from_other_file_p (node->resolution))
860 return true;
861 return false;
864 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
865 but other code such as notice_global_symbol generates rtl. */
867 void
868 symtab_make_decl_local (tree decl)
870 rtx rtl, symbol;
872 /* Avoid clearing comdat_groups on comdat-local decls. */
873 if (TREE_PUBLIC (decl) == 0)
874 return;
876 if (TREE_CODE (decl) == VAR_DECL)
877 DECL_COMMON (decl) = 0;
878 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
880 if (DECL_COMDAT (decl))
882 DECL_SECTION_NAME (decl) = 0;
883 DECL_COMDAT (decl) = 0;
885 DECL_WEAK (decl) = 0;
886 DECL_EXTERNAL (decl) = 0;
887 DECL_VISIBILITY_SPECIFIED (decl) = 0;
888 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
889 TREE_PUBLIC (decl) = 0;
890 if (!DECL_RTL_SET_P (decl))
891 return;
893 /* Update rtl flags. */
894 make_decl_rtl (decl);
896 rtl = DECL_RTL (decl);
897 if (!MEM_P (rtl))
898 return;
900 symbol = XEXP (rtl, 0);
901 if (GET_CODE (symbol) != SYMBOL_REF)
902 return;
904 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
907 /* Return availability of NODE. */
909 enum availability
910 symtab_node_availability (symtab_node *node)
912 if (is_a <cgraph_node *> (node))
913 return cgraph_function_body_availability (cgraph (node));
914 else
915 return cgraph_variable_initializer_availability (varpool (node));
918 /* Given NODE, walk the alias chain to return the symbol NODE is alias of.
919 If NODE is not an alias, return NODE.
920 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
922 symtab_node *
923 symtab_alias_ultimate_target (symtab_node *node, enum availability *availability)
925 bool weakref_p = false;
927 if (!node->alias)
929 if (availability)
930 *availability = symtab_node_availability (node);
931 return node;
934 /* To determine visibility of the target, we follow ELF semantic of aliases.
935 Here alias is an alternative assembler name of a given definition. Its
936 availability prevails the availability of its target (i.e. static alias of
937 weak definition is available.
939 Weakref is a different animal (and not part of ELF per se). It is just
940 alternative name of a given symbol used within one complation unit
941 and is translated prior hitting the object file. It inherits the
942 visibility of its target (i.e. weakref of non-overwritable definition
943 is non-overwritable, while weakref of weak definition is weak).
945 If we ever get into supporting targets with different semantics, a target
946 hook will be needed here. */
948 if (availability)
950 weakref_p = node->weakref;
951 if (!weakref_p)
952 *availability = symtab_node_availability (node);
953 else
954 *availability = AVAIL_LOCAL;
956 while (node)
958 if (node->alias && node->analyzed)
959 node = symtab_alias_target (node);
960 else
962 if (!availability)
964 else if (node->analyzed)
966 if (weakref_p)
968 enum availability a = symtab_node_availability (node);
969 if (a < *availability)
970 *availability = a;
973 else
974 *availability = AVAIL_NOT_AVAILABLE;
975 return node;
977 if (node && availability && weakref_p)
979 enum availability a = symtab_node_availability (node);
980 if (a < *availability)
981 *availability = a;
982 weakref_p = node->weakref;
985 if (availability)
986 *availability = AVAIL_NOT_AVAILABLE;
987 return NULL;
990 /* C++ FE sometimes change linkage flags after producing same body aliases.
992 FIXME: C++ produce implicit aliases for virtual functions and vtables that
993 are obviously equivalent. The way it is doing so is however somewhat
994 kludgy and interferes with the visibility code. As a result we need to
995 copy the visibility from the target to get things right. */
997 void
998 fixup_same_cpp_alias_visibility (symtab_node *node, symtab_node *target)
1000 if (is_a <cgraph_node *> (node))
1002 DECL_DECLARED_INLINE_P (node->decl)
1003 = DECL_DECLARED_INLINE_P (target->decl);
1004 DECL_DISREGARD_INLINE_LIMITS (node->decl)
1005 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1007 /* FIXME: It is not really clear why those flags should not be copied for
1008 functions, too. */
1009 else
1011 DECL_WEAK (node->decl) = DECL_WEAK (target->decl);
1012 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1013 DECL_VISIBILITY (node->decl) = DECL_VISIBILITY (target->decl);
1015 DECL_VIRTUAL_P (node->decl) = DECL_VIRTUAL_P (target->decl);
1016 if (TREE_PUBLIC (node->decl))
1018 tree group;
1020 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1021 DECL_COMDAT (node->decl) = DECL_COMDAT (target->decl);
1022 group = target->get_comdat_group ();
1023 node->set_comdat_group (group);
1024 if (group
1025 && !node->same_comdat_group)
1026 symtab_add_to_same_comdat_group (node, target);
1028 node->externally_visible = target->externally_visible;
1031 /* Add reference recording that NODE is alias of TARGET.
1032 The function can fail in the case of aliasing cycles; in this case
1033 it returns false. */
1035 bool
1036 symtab_resolve_alias (symtab_node *node, symtab_node *target)
1038 symtab_node *n;
1040 gcc_assert (!node->analyzed
1041 && !vec_safe_length (node->ref_list.references));
1043 /* Never let cycles to creep into the symbol table alias references;
1044 those will make alias walkers to be infinite. */
1045 for (n = target; n && n->alias;
1046 n = n->analyzed ? symtab_alias_target (n) : NULL)
1047 if (n == node)
1049 if (is_a <cgraph_node *> (node))
1050 error ("function %q+D part of alias cycle", node->decl);
1051 else if (is_a <varpool_node *> (node))
1052 error ("variable %q+D part of alias cycle", node->decl);
1053 else
1054 gcc_unreachable ();
1055 node->alias = false;
1056 return false;
1059 /* "analyze" the node - i.e. mark the reference. */
1060 node->definition = true;
1061 node->alias = true;
1062 node->analyzed = true;
1063 ipa_record_reference (node, target, IPA_REF_ALIAS, NULL);
1065 /* Alias targets become reudndant after alias is resolved into an reference.
1066 We do not want to keep it around or we would have to mind updating them
1067 when renaming symbols. */
1068 node->alias_target = NULL;
1070 if (node->cpp_implicit_alias && cgraph_state >= CGRAPH_STATE_CONSTRUCTION)
1071 fixup_same_cpp_alias_visibility (node, target);
1073 /* If alias has address taken, so does the target. */
1074 if (node->address_taken)
1075 symtab_alias_ultimate_target (target, NULL)->address_taken = true;
1076 return true;
1079 /* Call calback on NODE and aliases associated to NODE.
1080 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1081 skipped. */
1083 bool
1084 symtab_for_node_and_aliases (symtab_node *node,
1085 bool (*callback) (symtab_node *, void *),
1086 void *data,
1087 bool include_overwritable)
1089 int i;
1090 struct ipa_ref *ref;
1092 if (callback (node, data))
1093 return true;
1094 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
1095 if (ref->use == IPA_REF_ALIAS)
1097 symtab_node *alias = ref->referring;
1098 if (include_overwritable
1099 || symtab_node_availability (alias) > AVAIL_OVERWRITABLE)
1100 if (symtab_for_node_and_aliases (alias, callback, data,
1101 include_overwritable))
1102 return true;
1104 return false;
1107 /* Worker searching nonoverwritable alias. */
1109 static bool
1110 symtab_nonoverwritable_alias_1 (symtab_node *node, void *data)
1112 if (decl_binds_to_current_def_p (node->decl))
1114 *(symtab_node **)data = node;
1115 return true;
1117 return false;
1120 /* If NODE can not be overwriten by static or dynamic linker to point to different
1121 definition, return NODE. Otherwise look for alias with such property and if
1122 none exists, introduce new one. */
1124 symtab_node *
1125 symtab_nonoverwritable_alias (symtab_node *node)
1127 tree new_decl;
1128 symtab_node *new_node = NULL;
1130 /* First try to look up existing alias or base object
1131 (if that is already non-overwritable). */
1132 node = symtab_alias_ultimate_target (node, NULL);
1133 gcc_assert (!node->alias && !node->weakref);
1134 symtab_for_node_and_aliases (node, symtab_nonoverwritable_alias_1,
1135 (void *)&new_node, true);
1136 if (new_node)
1137 return new_node;
1138 #ifndef ASM_OUTPUT_DEF
1139 /* If aliases aren't supported by the assembler, fail. */
1140 return NULL;
1141 #endif
1143 /* Otherwise create a new one. */
1144 new_decl = copy_node (node->decl);
1145 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1146 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1147 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1148 DECL_INITIAL (new_decl) = NULL;
1149 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1150 SET_DECL_RTL (new_decl, NULL);
1152 /* Update the properties. */
1153 DECL_EXTERNAL (new_decl) = 0;
1154 TREE_PUBLIC (new_decl) = 0;
1155 DECL_COMDAT (new_decl) = 0;
1156 DECL_WEAK (new_decl) = 0;
1157 DECL_VIRTUAL_P (new_decl) = 0;
1158 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1160 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1161 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1162 new_node = cgraph_create_function_alias
1163 (new_decl, node->decl);
1165 else
1167 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1168 new_node = varpool_create_variable_alias (new_decl, node->decl);
1170 symtab_resolve_alias (new_node, node);
1171 gcc_assert (decl_binds_to_current_def_p (new_decl)
1172 && targetm.binds_local_p (new_decl));
1173 return new_node;
1176 /* Return true if A and B represents semantically equivalent symbols. */
1178 bool
1179 symtab_semantically_equivalent_p (symtab_node *a,
1180 symtab_node *b)
1182 enum availability avail;
1183 symtab_node *ba;
1184 symtab_node *bb;
1186 /* Equivalent functions are equivalent. */
1187 if (a->decl == b->decl)
1188 return true;
1190 /* If symbol is not overwritable by different implementation,
1191 walk to the base object it defines. */
1192 ba = symtab_alias_ultimate_target (a, &avail);
1193 if (avail >= AVAIL_AVAILABLE)
1195 if (ba == b)
1196 return true;
1198 else
1199 ba = a;
1200 bb = symtab_alias_ultimate_target (b, &avail);
1201 if (avail >= AVAIL_AVAILABLE)
1203 if (a == bb)
1204 return true;
1206 else
1207 bb = b;
1208 return bb == ba;
1211 /* Classify symbol NODE for partitioning. */
1213 enum symbol_partitioning_class
1214 symtab_get_symbol_partitioning_class (symtab_node *node)
1216 /* Inline clones are always duplicated.
1217 This include external delcarations. */
1218 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1220 if (DECL_ABSTRACT (node->decl))
1221 return SYMBOL_EXTERNAL;
1223 if (cnode && cnode->global.inlined_to)
1224 return SYMBOL_DUPLICATE;
1226 /* Weakref aliases are always duplicated. */
1227 if (node->weakref)
1228 return SYMBOL_DUPLICATE;
1230 /* External declarations are external. */
1231 if (DECL_EXTERNAL (node->decl))
1232 return SYMBOL_EXTERNAL;
1234 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
1236 /* Constant pool references use local symbol names that can not
1237 be promoted global. We should never put into a constant pool
1238 objects that can not be duplicated across partitions. */
1239 if (DECL_IN_CONSTANT_POOL (node->decl))
1240 return SYMBOL_DUPLICATE;
1241 gcc_checking_assert (vnode->definition);
1243 /* Functions that are cloned may stay in callgraph even if they are unused.
1244 Handle them as external; compute_ltrans_boundary take care to make
1245 proper things to happen (i.e. to make them appear in the boundary but
1246 with body streamed, so clone can me materialized). */
1247 else if (!cgraph (node)->definition)
1248 return SYMBOL_EXTERNAL;
1250 /* Linker discardable symbols are duplicated to every use unless they are
1251 keyed. */
1252 if (DECL_ONE_ONLY (node->decl)
1253 && !node->force_output
1254 && !node->forced_by_abi
1255 && !symtab_used_from_object_file_p (node))
1256 return SYMBOL_DUPLICATE;
1258 return SYMBOL_PARTITION;
1260 #include "gt-symtab.h"