/cp
[official-gcc.git] / gcc / symtab.c
blob756a62d8c7b13ae7e92e63547ade7c9d1622edd0
1 /* Symbol table.
2 Copyright (C) 2012-2015 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 "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "rtl.h"
28 #include "alias.h"
29 #include "fold-const.h"
30 #include "print-tree.h"
31 #include "varasm.h"
32 #include "emit-rtl.h"
33 #include "internal-fn.h"
34 #include "tree-inline.h"
35 #include "langhooks.h"
36 #include "cgraph.h"
37 #include "diagnostic.h"
38 #include "timevar.h"
39 #include "target.h"
40 #include "lto-streamer.h"
41 #include "output.h"
42 #include "ipa-utils.h"
43 #include "calls.h"
45 static const char *ipa_ref_use_name[] = {"read","write","addr","alias","chkp"};
47 const char * const ld_plugin_symbol_resolution_names[]=
49 "",
50 "undef",
51 "prevailing_def",
52 "prevailing_def_ironly",
53 "preempted_reg",
54 "preempted_ir",
55 "resolved_ir",
56 "resolved_exec",
57 "resolved_dyn",
58 "prevailing_def_ironly_exp"
61 /* Hash asmnames ignoring the user specified marks. */
63 hashval_t
64 symbol_table::decl_assembler_name_hash (const_tree asmname)
66 if (IDENTIFIER_POINTER (asmname)[0] == '*')
68 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
69 size_t ulp_len = strlen (user_label_prefix);
71 if (ulp_len == 0)
73 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
74 decl_str += ulp_len;
76 return htab_hash_string (decl_str);
79 return htab_hash_string (IDENTIFIER_POINTER (asmname));
83 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
85 bool
86 symbol_table::decl_assembler_name_equal (tree decl, const_tree asmname)
88 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
89 const char *decl_str;
90 const char *asmname_str;
91 bool test = false;
93 if (decl_asmname == asmname)
94 return true;
96 decl_str = IDENTIFIER_POINTER (decl_asmname);
97 asmname_str = IDENTIFIER_POINTER (asmname);
100 /* If the target assembler name was set by the user, things are trickier.
101 We have a leading '*' to begin with. After that, it's arguable what
102 is the correct thing to do with -fleading-underscore. Arguably, we've
103 historically been doing the wrong thing in assemble_alias by always
104 printing the leading underscore. Since we're not changing that, make
105 sure user_label_prefix follows the '*' before matching. */
106 if (decl_str[0] == '*')
108 size_t ulp_len = strlen (user_label_prefix);
110 decl_str ++;
112 if (ulp_len == 0)
113 test = true;
114 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
115 decl_str += ulp_len, test=true;
116 else
117 decl_str --;
119 if (asmname_str[0] == '*')
121 size_t ulp_len = strlen (user_label_prefix);
123 asmname_str ++;
125 if (ulp_len == 0)
126 test = true;
127 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
128 asmname_str += ulp_len, test=true;
129 else
130 asmname_str --;
133 if (!test)
134 return false;
135 return strcmp (decl_str, asmname_str) == 0;
139 /* Returns nonzero if P1 and P2 are equal. */
141 /* Insert NODE to assembler name hash. */
143 void
144 symbol_table::insert_to_assembler_name_hash (symtab_node *node,
145 bool with_clones)
147 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
148 return;
149 gcc_checking_assert (!node->previous_sharing_asm_name
150 && !node->next_sharing_asm_name);
151 if (assembler_name_hash)
153 symtab_node **aslot;
154 cgraph_node *cnode;
155 tree decl = node->decl;
157 tree name = DECL_ASSEMBLER_NAME (node->decl);
159 /* C++ FE can produce decls without associated assembler name and insert
160 them to symtab to hold section or TLS information. */
161 if (!name)
162 return;
164 hashval_t hash = decl_assembler_name_hash (name);
165 aslot = assembler_name_hash->find_slot_with_hash (name, hash, INSERT);
166 gcc_assert (*aslot != node);
167 node->next_sharing_asm_name = (symtab_node *)*aslot;
168 if (*aslot != NULL)
169 (*aslot)->previous_sharing_asm_name = node;
170 *aslot = node;
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->decl == decl)
177 insert_to_assembler_name_hash (cnode, true);
182 /* Remove NODE from assembler name hash. */
184 void
185 symbol_table::unlink_from_assembler_name_hash (symtab_node *node,
186 bool with_clones)
188 if (assembler_name_hash)
190 cgraph_node *cnode;
191 tree decl = node->decl;
193 if (node->next_sharing_asm_name)
194 node->next_sharing_asm_name->previous_sharing_asm_name
195 = node->previous_sharing_asm_name;
196 if (node->previous_sharing_asm_name)
198 node->previous_sharing_asm_name->next_sharing_asm_name
199 = node->next_sharing_asm_name;
201 else
203 tree name = DECL_ASSEMBLER_NAME (node->decl);
204 symtab_node **slot;
206 if (!name)
207 return;
209 hashval_t hash = decl_assembler_name_hash (name);
210 slot = assembler_name_hash->find_slot_with_hash (name, hash,
211 NO_INSERT);
212 gcc_assert (*slot == node);
213 if (!node->next_sharing_asm_name)
214 assembler_name_hash->clear_slot (slot);
215 else
216 *slot = node->next_sharing_asm_name;
218 node->next_sharing_asm_name = NULL;
219 node->previous_sharing_asm_name = NULL;
221 /* Update also possible inline clones sharing a decl. */
222 cnode = dyn_cast <cgraph_node *> (node);
223 if (cnode && cnode->clones && with_clones)
224 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
225 if (cnode->decl == decl)
226 unlink_from_assembler_name_hash (cnode, true);
230 /* Arrange node to be first in its entry of assembler_name_hash. */
232 void
233 symbol_table::symtab_prevail_in_asm_name_hash (symtab_node *node)
235 unlink_from_assembler_name_hash (node, false);
236 insert_to_assembler_name_hash (node, false);
239 /* Initalize asm name hash unless. */
241 void
242 symbol_table::symtab_initialize_asm_name_hash (void)
244 symtab_node *node;
245 if (!assembler_name_hash)
247 assembler_name_hash = hash_table<asmname_hasher>::create_ggc (10);
248 FOR_EACH_SYMBOL (node)
249 insert_to_assembler_name_hash (node, false);
253 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
255 void
256 symbol_table::change_decl_assembler_name (tree decl, tree name)
258 symtab_node *node = NULL;
260 /* We can have user ASM names on things, like global register variables, that
261 are not in the symbol table. */
262 if ((TREE_CODE (decl) == VAR_DECL
263 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
264 || TREE_CODE (decl) == FUNCTION_DECL)
265 node = symtab_node::get (decl);
266 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
268 SET_DECL_ASSEMBLER_NAME (decl, name);
269 if (node)
270 insert_to_assembler_name_hash (node, true);
272 else
274 if (name == DECL_ASSEMBLER_NAME (decl))
275 return;
277 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
278 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
279 : NULL);
280 if (node)
281 unlink_from_assembler_name_hash (node, true);
282 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
283 && DECL_RTL_SET_P (decl))
284 warning (0, "%D renamed after being referenced in assembly", decl);
286 SET_DECL_ASSEMBLER_NAME (decl, name);
287 if (alias)
289 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
290 TREE_CHAIN (name) = alias;
292 if (node)
293 insert_to_assembler_name_hash (node, true);
297 /* Hash sections by their names. */
299 hashval_t
300 section_name_hasher::hash (section_hash_entry *n)
302 return htab_hash_string (n->name);
305 /* Return true if section P1 name equals to P2. */
307 bool
308 section_name_hasher::equal (section_hash_entry *n1, const char *name)
310 return n1->name == name || !strcmp (n1->name, name);
313 /* Add node into symbol table. This function is not used directly, but via
314 cgraph/varpool node creation routines. */
316 void
317 symtab_node::register_symbol (void)
319 symtab->register_symbol (this);
321 if (!decl->decl_with_vis.symtab_node)
322 decl->decl_with_vis.symtab_node = this;
324 ref_list.clear ();
326 /* Be sure to do this last; C++ FE might create new nodes via
327 DECL_ASSEMBLER_NAME langhook! */
328 symtab->insert_to_assembler_name_hash (this, false);
331 /* Remove NODE from same comdat group. */
333 void
334 symtab_node::remove_from_same_comdat_group (void)
336 if (same_comdat_group)
338 symtab_node *prev;
339 for (prev = same_comdat_group;
340 prev->same_comdat_group != this;
341 prev = prev->same_comdat_group)
343 if (same_comdat_group == prev)
344 prev->same_comdat_group = NULL;
345 else
346 prev->same_comdat_group = same_comdat_group;
347 same_comdat_group = NULL;
348 set_comdat_group (NULL);
352 /* Remove node from symbol table. This function is not used directly, but via
353 cgraph/varpool node removal routines. */
355 void
356 symtab_node::unregister (void)
358 remove_all_references ();
359 remove_all_referring ();
361 /* Remove reference to section. */
362 set_section_for_node (NULL);
364 remove_from_same_comdat_group ();
366 symtab->unregister (this);
368 /* During LTO symtab merging we temporarily corrupt decl to symtab node
369 hash. */
370 gcc_assert (decl->decl_with_vis.symtab_node || in_lto_p);
371 if (decl->decl_with_vis.symtab_node == this)
373 symtab_node *replacement_node = NULL;
374 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
375 replacement_node = cnode->find_replacement ();
376 decl->decl_with_vis.symtab_node = replacement_node;
378 if (!is_a <varpool_node *> (this) || !DECL_HARD_REGISTER (decl))
379 symtab->unlink_from_assembler_name_hash (this, false);
380 if (in_init_priority_hash)
381 symtab->init_priority_hash->remove (this);
385 /* Remove symbol from symbol table. */
387 void
388 symtab_node::remove (void)
390 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
391 cnode->remove ();
392 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
393 vnode->remove ();
396 /* Add NEW_ to the same comdat group that OLD is in. */
398 void
399 symtab_node::add_to_same_comdat_group (symtab_node *old_node)
401 gcc_assert (old_node->get_comdat_group ());
402 gcc_assert (!same_comdat_group);
403 gcc_assert (this != old_node);
405 set_comdat_group (old_node->get_comdat_group ());
406 same_comdat_group = old_node;
407 if (!old_node->same_comdat_group)
408 old_node->same_comdat_group = this;
409 else
411 symtab_node *n;
412 for (n = old_node->same_comdat_group;
413 n->same_comdat_group != old_node;
414 n = n->same_comdat_group)
416 n->same_comdat_group = this;
420 /* Dissolve the same_comdat_group list in which NODE resides. */
422 void
423 symtab_node::dissolve_same_comdat_group_list (void)
425 symtab_node *n = this;
426 symtab_node *next;
428 if (!same_comdat_group)
429 return;
432 next = n->same_comdat_group;
433 n->same_comdat_group = NULL;
434 /* Clear comdat_group for comdat locals, since
435 make_decl_local doesn't. */
436 if (!TREE_PUBLIC (n->decl))
437 n->set_comdat_group (NULL);
438 n = next;
440 while (n != this);
443 /* Return printable assembler name of NODE.
444 This function is used only for debugging. When assembler name
445 is unknown go with identifier name. */
447 const char *
448 symtab_node::asm_name () const
450 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
451 return lang_hooks.decl_printable_name (decl, 2);
452 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
455 /* Return printable identifier name. */
457 const char *
458 symtab_node::name () const
460 return lang_hooks.decl_printable_name (decl, 2);
463 /* Return ipa reference from this symtab_node to
464 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
465 of the use. */
467 ipa_ref *
468 symtab_node::create_reference (symtab_node *referred_node,
469 enum ipa_ref_use use_type)
471 return create_reference (referred_node, use_type, NULL);
475 /* Return ipa reference from this symtab_node to
476 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
477 of the use and STMT the statement (if it exists). */
479 ipa_ref *
480 symtab_node::create_reference (symtab_node *referred_node,
481 enum ipa_ref_use use_type, gimple stmt)
483 ipa_ref *ref = NULL, *ref2 = NULL;
484 ipa_ref_list *list, *list2;
485 ipa_ref_t *old_references;
487 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
488 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
490 list = &ref_list;
491 old_references = vec_safe_address (list->references);
492 vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
493 ref = &list->references->last ();
495 list2 = &referred_node->ref_list;
497 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
498 if(use_type == IPA_REF_ALIAS)
500 list2->referring.safe_insert (0, ref);
501 ref->referred_index = 0;
503 for (unsigned int i = 1; i < list2->referring.length (); i++)
504 list2->referring[i]->referred_index = i;
506 else
508 list2->referring.safe_push (ref);
509 ref->referred_index = list2->referring.length () - 1;
512 ref->referring = this;
513 ref->referred = referred_node;
514 ref->stmt = stmt;
515 ref->lto_stmt_uid = 0;
516 ref->use = use_type;
517 ref->speculative = 0;
519 /* If vector was moved in memory, update pointers. */
520 if (old_references != list->references->address ())
522 int i;
523 for (i = 0; iterate_reference(i, ref2); i++)
524 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
526 return ref;
529 /* If VAL is a reference to a function or a variable, add a reference from
530 this symtab_node to the corresponding symbol table node. USE_TYPE specify
531 type of the use and STMT the statement (if it exists). Return the new
532 reference or NULL if none was created. */
534 ipa_ref *
535 symtab_node::maybe_create_reference (tree val, enum ipa_ref_use use_type,
536 gimple stmt)
538 STRIP_NOPS (val);
539 if (TREE_CODE (val) != ADDR_EXPR)
540 return NULL;
541 val = get_base_var (val);
542 if (val && (TREE_CODE (val) == FUNCTION_DECL
543 || TREE_CODE (val) == VAR_DECL))
545 symtab_node *referred = symtab_node::get (val);
546 gcc_checking_assert (referred);
547 return create_reference (referred, use_type, stmt);
549 return NULL;
552 /* Clone all references from symtab NODE to this symtab_node. */
554 void
555 symtab_node::clone_references (symtab_node *node)
557 ipa_ref *ref = NULL, *ref2 = NULL;
558 int i;
559 for (i = 0; node->iterate_reference (i, ref); i++)
561 bool speculative = ref->speculative;
562 unsigned int stmt_uid = ref->lto_stmt_uid;
564 ref2 = create_reference (ref->referred, ref->use, ref->stmt);
565 ref2->speculative = speculative;
566 ref2->lto_stmt_uid = stmt_uid;
570 /* Clone all referring from symtab NODE to this symtab_node. */
572 void
573 symtab_node::clone_referring (symtab_node *node)
575 ipa_ref *ref = NULL, *ref2 = NULL;
576 int i;
577 for (i = 0; node->iterate_referring(i, ref); i++)
579 bool speculative = ref->speculative;
580 unsigned int stmt_uid = ref->lto_stmt_uid;
582 ref2 = ref->referring->create_reference (this, ref->use, ref->stmt);
583 ref2->speculative = speculative;
584 ref2->lto_stmt_uid = stmt_uid;
588 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
590 ipa_ref *
591 symtab_node::clone_reference (ipa_ref *ref, gimple stmt)
593 bool speculative = ref->speculative;
594 unsigned int stmt_uid = ref->lto_stmt_uid;
595 ipa_ref *ref2;
597 ref2 = create_reference (ref->referred, ref->use, stmt);
598 ref2->speculative = speculative;
599 ref2->lto_stmt_uid = stmt_uid;
600 return ref2;
603 /* Find the structure describing a reference to REFERRED_NODE
604 and associated with statement STMT. */
606 ipa_ref *
607 symtab_node::find_reference (symtab_node *referred_node,
608 gimple stmt, unsigned int lto_stmt_uid)
610 ipa_ref *r = NULL;
611 int i;
613 for (i = 0; iterate_reference (i, r); i++)
614 if (r->referred == referred_node
615 && !r->speculative
616 && ((stmt && r->stmt == stmt)
617 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
618 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
619 return r;
620 return NULL;
623 /* Remove all references that are associated with statement STMT. */
625 void
626 symtab_node::remove_stmt_references (gimple stmt)
628 ipa_ref *r = NULL;
629 int i = 0;
631 while (iterate_reference (i, r))
632 if (r->stmt == stmt)
633 r->remove_reference ();
634 else
635 i++;
638 /* Remove all stmt references in non-speculative references.
639 Those are not maintained during inlining & clonning.
640 The exception are speculative references that are updated along
641 with callgraph edges associated with them. */
643 void
644 symtab_node::clear_stmts_in_references (void)
646 ipa_ref *r = NULL;
647 int i;
649 for (i = 0; iterate_reference (i, r); i++)
650 if (!r->speculative)
652 r->stmt = NULL;
653 r->lto_stmt_uid = 0;
657 /* Remove all references in ref list. */
659 void
660 symtab_node::remove_all_references (void)
662 while (vec_safe_length (ref_list.references))
663 ref_list.references->last ().remove_reference ();
664 vec_free (ref_list.references);
667 /* Remove all referring items in ref list. */
669 void
670 symtab_node::remove_all_referring (void)
672 while (ref_list.referring.length ())
673 ref_list.referring.last ()->remove_reference ();
674 ref_list.referring.release ();
677 /* Dump references in ref list to FILE. */
679 void
680 symtab_node::dump_references (FILE *file)
682 ipa_ref *ref = NULL;
683 int i;
684 for (i = 0; iterate_reference (i, ref); i++)
686 fprintf (file, "%s/%i (%s)",
687 ref->referred->asm_name (),
688 ref->referred->order,
689 ipa_ref_use_name [ref->use]);
690 if (ref->speculative)
691 fprintf (file, " (speculative)");
693 fprintf (file, "\n");
696 /* Dump referring in list to FILE. */
698 void
699 symtab_node::dump_referring (FILE *file)
701 ipa_ref *ref = NULL;
702 int i;
703 for (i = 0; iterate_referring(i, ref); i++)
705 fprintf (file, "%s/%i (%s)",
706 ref->referring->asm_name (),
707 ref->referring->order,
708 ipa_ref_use_name [ref->use]);
709 if (ref->speculative)
710 fprintf (file, " (speculative)");
712 fprintf (file, "\n");
715 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
717 /* Dump base fields of symtab nodes to F. Not to be used directly. */
719 void
720 symtab_node::dump_base (FILE *f)
722 static const char * const visibility_types[] = {
723 "default", "protected", "hidden", "internal"
726 fprintf (f, "%s/%i (%s)", asm_name (), order, name ());
727 dump_addr (f, " @", (void *)this);
728 fprintf (f, "\n Type: %s", symtab_type_names[type]);
730 if (definition)
731 fprintf (f, " definition");
732 if (analyzed)
733 fprintf (f, " analyzed");
734 if (alias)
735 fprintf (f, " alias");
736 if (weakref)
737 fprintf (f, " weakref");
738 if (cpp_implicit_alias)
739 fprintf (f, " cpp_implicit_alias");
740 if (alias_target)
741 fprintf (f, " target:%s",
742 DECL_P (alias_target)
743 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
744 (alias_target))
745 : IDENTIFIER_POINTER (alias_target));
746 if (body_removed)
747 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
748 fprintf (f, "\n Visibility:");
749 if (in_other_partition)
750 fprintf (f, " in_other_partition");
751 if (used_from_other_partition)
752 fprintf (f, " used_from_other_partition");
753 if (force_output)
754 fprintf (f, " force_output");
755 if (forced_by_abi)
756 fprintf (f, " forced_by_abi");
757 if (externally_visible)
758 fprintf (f, " externally_visible");
759 if (no_reorder)
760 fprintf (f, " no_reorder");
761 if (resolution != LDPR_UNKNOWN)
762 fprintf (f, " %s",
763 ld_plugin_symbol_resolution_names[(int)resolution]);
764 if (TREE_ASM_WRITTEN (decl))
765 fprintf (f, " asm_written");
766 if (DECL_EXTERNAL (decl))
767 fprintf (f, " external");
768 if (TREE_PUBLIC (decl))
769 fprintf (f, " public");
770 if (DECL_COMMON (decl))
771 fprintf (f, " common");
772 if (DECL_WEAK (decl))
773 fprintf (f, " weak");
774 if (DECL_DLLIMPORT_P (decl))
775 fprintf (f, " dll_import");
776 if (DECL_COMDAT (decl))
777 fprintf (f, " comdat");
778 if (get_comdat_group ())
779 fprintf (f, " comdat_group:%s",
780 IDENTIFIER_POINTER (get_comdat_group_id ()));
781 if (DECL_ONE_ONLY (decl))
782 fprintf (f, " one_only");
783 if (get_section ())
784 fprintf (f, " section:%s",
785 get_section ());
786 if (implicit_section)
787 fprintf (f," (implicit_section)");
788 if (DECL_VISIBILITY_SPECIFIED (decl))
789 fprintf (f, " visibility_specified");
790 if (DECL_VISIBILITY (decl))
791 fprintf (f, " visibility:%s",
792 visibility_types [DECL_VISIBILITY (decl)]);
793 if (DECL_VIRTUAL_P (decl))
794 fprintf (f, " virtual");
795 if (DECL_ARTIFICIAL (decl))
796 fprintf (f, " artificial");
797 if (TREE_CODE (decl) == FUNCTION_DECL)
799 if (DECL_STATIC_CONSTRUCTOR (decl))
800 fprintf (f, " constructor");
801 if (DECL_STATIC_DESTRUCTOR (decl))
802 fprintf (f, " destructor");
804 fprintf (f, "\n");
806 if (same_comdat_group)
807 fprintf (f, " Same comdat group as: %s/%i\n",
808 same_comdat_group->asm_name (),
809 same_comdat_group->order);
810 if (next_sharing_asm_name)
811 fprintf (f, " next sharing asm name: %i\n",
812 next_sharing_asm_name->order);
813 if (previous_sharing_asm_name)
814 fprintf (f, " previous sharing asm name: %i\n",
815 previous_sharing_asm_name->order);
817 if (address_taken)
818 fprintf (f, " Address is taken.\n");
819 if (aux)
821 fprintf (f, " Aux:");
822 dump_addr (f, " @", (void *)aux);
825 fprintf (f, " References: ");
826 dump_references (f);
827 fprintf (f, " Referring: ");
828 dump_referring (f);
829 if (lto_file_data)
830 fprintf (f, " Read from file: %s\n",
831 lto_file_data->file_name);
834 /* Dump symtab node to F. */
836 void
837 symtab_node::dump (FILE *f)
839 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
840 cnode->dump (f);
841 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
842 vnode->dump (f);
845 /* Dump symbol table to F. */
847 void
848 symtab_node::dump_table (FILE *f)
850 symtab_node *node;
851 fprintf (f, "Symbol table:\n\n");
852 FOR_EACH_SYMBOL (node)
853 node->dump (f);
857 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
858 Return NULL if there's no such node. */
860 symtab_node *
861 symtab_node::get_for_asmname (const_tree asmname)
863 symtab_node *node;
865 symtab->symtab_initialize_asm_name_hash ();
866 hashval_t hash = symtab->decl_assembler_name_hash (asmname);
867 symtab_node **slot
868 = symtab->assembler_name_hash->find_slot_with_hash (asmname, hash,
869 NO_INSERT);
871 if (slot)
873 node = *slot;
874 return node;
876 return NULL;
879 /* Dump symtab node NODE to stderr. */
881 DEBUG_FUNCTION void
882 symtab_node::debug (void)
884 dump (stderr);
887 /* Verify common part of symtab nodes. */
889 DEBUG_FUNCTION bool
890 symtab_node::verify_base (void)
892 bool error_found = false;
893 symtab_node *hashed_node;
895 if (is_a <cgraph_node *> (this))
897 if (TREE_CODE (decl) != FUNCTION_DECL)
899 error ("function symbol is not function");
900 error_found = true;
903 else if (is_a <varpool_node *> (this))
905 if (TREE_CODE (decl) != VAR_DECL)
907 error ("variable symbol is not variable");
908 error_found = true;
911 else
913 error ("node has unknown type");
914 error_found = true;
917 if (symtab->state != LTO_STREAMING)
919 hashed_node = symtab_node::get (decl);
920 if (!hashed_node)
922 error ("node not found node->decl->decl_with_vis.symtab_node");
923 error_found = true;
925 if (hashed_node != this
926 && (!is_a <cgraph_node *> (this)
927 || !dyn_cast <cgraph_node *> (this)->clone_of
928 || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl))
930 error ("node differs from node->decl->decl_with_vis.symtab_node");
931 error_found = true;
934 if (symtab->assembler_name_hash)
936 hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
937 if (hashed_node && hashed_node->previous_sharing_asm_name)
939 error ("assembler name hash list corrupted");
940 error_found = true;
942 while (hashed_node)
944 if (hashed_node == this)
945 break;
946 hashed_node = hashed_node->next_sharing_asm_name;
948 if (!hashed_node
949 && !(is_a <varpool_node *> (this)
950 || DECL_HARD_REGISTER (decl)))
952 error ("node not found in symtab assembler name hash");
953 error_found = true;
956 if (previous_sharing_asm_name
957 && previous_sharing_asm_name->next_sharing_asm_name != this)
959 error ("double linked list of assembler names corrupted");
960 error_found = true;
962 if (body_removed && definition)
964 error ("node has body_removed but is definition");
965 error_found = true;
967 if (analyzed && !definition)
969 error ("node is analyzed byt it is not a definition");
970 error_found = true;
972 if (cpp_implicit_alias && !alias)
974 error ("node is alias but not implicit alias");
975 error_found = true;
977 if (alias && !definition && !weakref)
979 error ("node is alias but not definition");
980 error_found = true;
982 if (weakref && !alias)
984 error ("node is weakref but not an alias");
985 error_found = true;
987 if (same_comdat_group)
989 symtab_node *n = same_comdat_group;
991 if (!n->get_comdat_group ())
993 error ("node is in same_comdat_group list but has no comdat_group");
994 error_found = true;
996 if (n->get_comdat_group () != get_comdat_group ())
998 error ("same_comdat_group list across different groups");
999 error_found = true;
1001 if (n->type != type)
1003 error ("mixing different types of symbol in same comdat groups is not supported");
1004 error_found = true;
1006 if (n == this)
1008 error ("node is alone in a comdat group");
1009 error_found = true;
1013 if (!n->same_comdat_group)
1015 error ("same_comdat_group is not a circular list");
1016 error_found = true;
1017 break;
1019 n = n->same_comdat_group;
1021 while (n != this);
1022 if (comdat_local_p ())
1024 ipa_ref *ref = NULL;
1026 for (int i = 0; iterate_referring (i, ref); ++i)
1028 if (!in_same_comdat_group_p (ref->referring))
1030 error ("comdat-local symbol referred to by %s outside its "
1031 "comdat",
1032 identifier_to_locale (ref->referring->name()));
1033 error_found = true;
1038 if (implicit_section && !get_section ())
1040 error ("implicit_section flag is set but section isn't");
1041 error_found = true;
1043 if (get_section () && get_comdat_group ()
1044 && !implicit_section
1045 && !lookup_attribute ("section", DECL_ATTRIBUTES (decl)))
1047 error ("Both section and comdat group is set");
1048 error_found = true;
1050 /* TODO: Add string table for sections, so we do not keep holding duplicated
1051 strings. */
1052 if (alias && definition
1053 && get_section () != get_alias_target ()->get_section ()
1054 && (!get_section()
1055 || !get_alias_target ()->get_section ()
1056 || strcmp (get_section(),
1057 get_alias_target ()->get_section ())))
1059 error ("Alias and target's section differs");
1060 get_alias_target ()->dump (stderr);
1061 error_found = true;
1063 if (alias && definition
1064 && get_comdat_group () != get_alias_target ()->get_comdat_group ())
1066 error ("Alias and target's comdat groups differs");
1067 get_alias_target ()->dump (stderr);
1068 error_found = true;
1071 return error_found;
1074 /* Verify consistency of NODE. */
1076 DEBUG_FUNCTION void
1077 symtab_node::verify (void)
1079 if (seen_error ())
1080 return;
1082 timevar_push (TV_CGRAPH_VERIFY);
1083 if (cgraph_node *node = dyn_cast <cgraph_node *> (this))
1084 node->verify_node ();
1085 else
1086 if (verify_base ())
1088 debug ();
1089 internal_error ("symtab_node::verify failed");
1091 timevar_pop (TV_CGRAPH_VERIFY);
1094 /* Verify symbol table for internal consistency. */
1096 DEBUG_FUNCTION void
1097 symtab_node::verify_symtab_nodes (void)
1099 symtab_node *node;
1100 hash_map<tree, symtab_node *> comdat_head_map (251);
1102 FOR_EACH_SYMBOL (node)
1104 node->verify ();
1105 if (node->get_comdat_group ())
1107 symtab_node **entry, *s;
1108 bool existed;
1110 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1111 &existed);
1112 if (!existed)
1113 *entry = node;
1114 else if (!DECL_EXTERNAL (node->decl))
1116 for (s = (*entry)->same_comdat_group;
1117 s != NULL && s != node && s != *entry;
1118 s = s->same_comdat_group)
1120 if (!s || s == *entry)
1122 error ("Two symbols with same comdat_group are not linked by "
1123 "the same_comdat_group list.");
1124 (*entry)->debug ();
1125 node->debug ();
1126 internal_error ("symtab_node::verify failed");
1133 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1134 but other code such as notice_global_symbol generates rtl. */
1136 void
1137 symtab_node::make_decl_local (void)
1139 rtx rtl, symbol;
1141 /* Avoid clearing comdat_groups on comdat-local decls. */
1142 if (TREE_PUBLIC (decl) == 0)
1143 return;
1145 if (TREE_CODE (decl) == VAR_DECL)
1147 DECL_COMMON (decl) = 0;
1148 /* ADDRESSABLE flag is not defined for public symbols. */
1149 TREE_ADDRESSABLE (decl) = 1;
1151 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1153 DECL_COMDAT (decl) = 0;
1154 DECL_WEAK (decl) = 0;
1155 DECL_EXTERNAL (decl) = 0;
1156 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1157 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1158 TREE_PUBLIC (decl) = 0;
1159 DECL_DLLIMPORT_P (decl) = 0;
1160 if (!DECL_RTL_SET_P (decl))
1161 return;
1163 /* Update rtl flags. */
1164 make_decl_rtl (decl);
1166 rtl = DECL_RTL (decl);
1167 if (!MEM_P (rtl))
1168 return;
1170 symbol = XEXP (rtl, 0);
1171 if (GET_CODE (symbol) != SYMBOL_REF)
1172 return;
1174 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1177 /* Walk the alias chain to return the symbol NODE is alias of.
1178 If NODE is not an alias, return NODE.
1179 Assumes NODE is known to be alias. */
1181 symtab_node *
1182 symtab_node::ultimate_alias_target_1 (enum availability *availability)
1184 bool weakref_p = false;
1186 /* To determine visibility of the target, we follow ELF semantic of aliases.
1187 Here alias is an alternative assembler name of a given definition. Its
1188 availability prevails the availability of its target (i.e. static alias of
1189 weak definition is available.
1191 Weakref is a different animal (and not part of ELF per se). It is just
1192 alternative name of a given symbol used within one complation unit
1193 and is translated prior hitting the object file. It inherits the
1194 visibility of its target (i.e. weakref of non-overwritable definition
1195 is non-overwritable, while weakref of weak definition is weak).
1197 If we ever get into supporting targets with different semantics, a target
1198 hook will be needed here. */
1200 if (availability)
1202 weakref_p = weakref;
1203 if (!weakref_p)
1204 *availability = get_availability ();
1205 else
1206 *availability = AVAIL_LOCAL;
1209 symtab_node *node = this;
1210 while (node)
1212 if (node->alias && node->analyzed)
1213 node = node->get_alias_target ();
1214 else
1216 if (!availability)
1218 else if (node->analyzed)
1220 if (weakref_p)
1222 enum availability a = node->get_availability ();
1223 if (a < *availability)
1224 *availability = a;
1227 else
1228 *availability = AVAIL_NOT_AVAILABLE;
1229 return node;
1231 if (node && availability && weakref_p)
1233 enum availability a = node->get_availability ();
1234 if (a < *availability)
1235 *availability = a;
1236 weakref_p = node->weakref;
1239 if (availability)
1240 *availability = AVAIL_NOT_AVAILABLE;
1241 return NULL;
1244 /* C++ FE sometimes change linkage flags after producing same body aliases.
1246 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1247 are obviously equivalent. The way it is doing so is however somewhat
1248 kludgy and interferes with the visibility code. As a result we need to
1249 copy the visibility from the target to get things right. */
1251 void
1252 symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target)
1254 if (is_a <cgraph_node *> (this))
1256 DECL_DECLARED_INLINE_P (decl)
1257 = DECL_DECLARED_INLINE_P (target->decl);
1258 DECL_DISREGARD_INLINE_LIMITS (decl)
1259 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1261 /* FIXME: It is not really clear why those flags should not be copied for
1262 functions, too. */
1263 else
1265 DECL_WEAK (decl) = DECL_WEAK (target->decl);
1266 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1267 DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl);
1269 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (target->decl);
1270 if (TREE_PUBLIC (decl))
1272 tree group;
1274 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1275 DECL_COMDAT (decl) = DECL_COMDAT (target->decl);
1276 group = target->get_comdat_group ();
1277 set_comdat_group (group);
1278 if (group && !same_comdat_group)
1279 add_to_same_comdat_group (target);
1281 externally_visible = target->externally_visible;
1284 /* Set section, do not recurse into aliases.
1285 When one wants to change section of symbol and its aliases,
1286 use set_section. */
1288 void
1289 symtab_node::set_section_for_node (const char *section)
1291 const char *current = get_section ();
1292 section_hash_entry **slot;
1294 if (current == section
1295 || (current && section
1296 && !strcmp (current, section)))
1297 return;
1299 if (current)
1301 x_section->ref_count--;
1302 if (!x_section->ref_count)
1304 hashval_t hash = htab_hash_string (x_section->name);
1305 slot = symtab->section_hash->find_slot_with_hash (x_section->name,
1306 hash, INSERT);
1307 ggc_free (x_section);
1308 symtab->section_hash->clear_slot (slot);
1310 x_section = NULL;
1312 if (!section)
1314 implicit_section = false;
1315 return;
1317 if (!symtab->section_hash)
1318 symtab->section_hash = hash_table<section_name_hasher>::create_ggc (10);
1319 slot = symtab->section_hash->find_slot_with_hash (section,
1320 htab_hash_string (section),
1321 INSERT);
1322 if (*slot)
1323 x_section = (section_hash_entry *)*slot;
1324 else
1326 int len = strlen (section);
1327 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1328 x_section->name = ggc_vec_alloc<char> (len + 1);
1329 memcpy (x_section->name, section, len + 1);
1331 x_section->ref_count++;
1334 /* Worker for set_section. */
1336 bool
1337 symtab_node::set_section (symtab_node *n, void *s)
1339 n->set_section_for_node ((char *)s);
1340 return false;
1343 /* Set section of symbol and its aliases. */
1345 void
1346 symtab_node::set_section (const char *section)
1348 gcc_assert (!this->alias);
1349 call_for_symbol_and_aliases
1350 (symtab_node::set_section, const_cast<char *>(section), true);
1353 /* Return the initialization priority. */
1355 priority_type
1356 symtab_node::get_init_priority ()
1358 if (!this->in_init_priority_hash)
1359 return DEFAULT_INIT_PRIORITY;
1361 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1362 return h ? h->init : DEFAULT_INIT_PRIORITY;
1365 /* Return the finalization priority. */
1367 priority_type
1368 cgraph_node::get_fini_priority ()
1370 if (!this->in_init_priority_hash)
1371 return DEFAULT_INIT_PRIORITY;
1372 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1373 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1376 /* Return the initialization and finalization priority information for
1377 DECL. If there is no previous priority information, a freshly
1378 allocated structure is returned. */
1380 symbol_priority_map *
1381 symtab_node::priority_info (void)
1383 if (!symtab->init_priority_hash)
1384 symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13);
1386 bool existed;
1387 symbol_priority_map *h
1388 = &symtab->init_priority_hash->get_or_insert (this, &existed);
1389 if (!existed)
1391 h->init = DEFAULT_INIT_PRIORITY;
1392 h->fini = DEFAULT_INIT_PRIORITY;
1393 in_init_priority_hash = true;
1396 return h;
1399 /* Set initialization priority to PRIORITY. */
1401 void
1402 symtab_node::set_init_priority (priority_type priority)
1404 symbol_priority_map *h;
1406 if (is_a <cgraph_node *> (this))
1407 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1409 if (priority == DEFAULT_INIT_PRIORITY)
1411 gcc_assert (get_init_priority() == priority);
1412 return;
1414 h = priority_info ();
1415 h->init = priority;
1418 /* Set fialization priority to PRIORITY. */
1420 void
1421 cgraph_node::set_fini_priority (priority_type priority)
1423 symbol_priority_map *h;
1425 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1427 if (priority == DEFAULT_INIT_PRIORITY)
1429 gcc_assert (get_fini_priority() == priority);
1430 return;
1432 h = priority_info ();
1433 h->fini = priority;
1436 /* Worker for symtab_resolve_alias. */
1438 bool
1439 symtab_node::set_implicit_section (symtab_node *n,
1440 void *data ATTRIBUTE_UNUSED)
1442 n->implicit_section = true;
1443 return false;
1446 /* Add reference recording that symtab node is alias of TARGET.
1447 The function can fail in the case of aliasing cycles; in this case
1448 it returns false. */
1450 bool
1451 symtab_node::resolve_alias (symtab_node *target)
1453 symtab_node *n;
1455 gcc_assert (!analyzed && !vec_safe_length (ref_list.references));
1457 /* Never let cycles to creep into the symbol table alias references;
1458 those will make alias walkers to be infinite. */
1459 for (n = target; n && n->alias;
1460 n = n->analyzed ? n->get_alias_target () : NULL)
1461 if (n == this)
1463 if (is_a <cgraph_node *> (this))
1464 error ("function %q+D part of alias cycle", decl);
1465 else if (is_a <varpool_node *> (this))
1466 error ("variable %q+D part of alias cycle", decl);
1467 else
1468 gcc_unreachable ();
1469 alias = false;
1470 return false;
1473 /* "analyze" the node - i.e. mark the reference. */
1474 definition = true;
1475 alias = true;
1476 analyzed = true;
1477 create_reference (target, IPA_REF_ALIAS, NULL);
1479 /* Add alias into the comdat group of its target unless it is already there. */
1480 if (same_comdat_group)
1481 remove_from_same_comdat_group ();
1482 set_comdat_group (NULL);
1483 if (target->get_comdat_group ())
1484 add_to_same_comdat_group (target);
1486 if ((get_section () != target->get_section ()
1487 || target->get_comdat_group ()) && get_section () && !implicit_section)
1489 error ("section of alias %q+D must match section of its target", decl);
1491 call_for_symbol_and_aliases (symtab_node::set_section,
1492 const_cast<char *>(target->get_section ()), true);
1493 if (target->implicit_section)
1494 call_for_symbol_and_aliases (set_implicit_section, NULL, true);
1496 /* Alias targets become redundant after alias is resolved into an reference.
1497 We do not want to keep it around or we would have to mind updating them
1498 when renaming symbols. */
1499 alias_target = NULL;
1501 if (cpp_implicit_alias && symtab->state >= CONSTRUCTION)
1502 fixup_same_cpp_alias_visibility (target);
1504 /* If alias has address taken, so does the target. */
1505 if (address_taken)
1506 target->ultimate_alias_target ()->address_taken = true;
1508 /* All non-weakref aliases of THIS are now in fact aliases of TARGET. */
1509 ipa_ref *ref;
1510 for (unsigned i = 0; iterate_direct_aliases (i, ref);)
1512 struct symtab_node *alias_alias = ref->referring;
1513 if (!alias_alias->weakref)
1515 alias_alias->remove_all_references ();
1516 alias_alias->create_reference (target, IPA_REF_ALIAS, NULL);
1518 else i++;
1520 return true;
1523 /* Worker searching noninterposable alias. */
1525 bool
1526 symtab_node::noninterposable_alias (symtab_node *node, void *data)
1528 if (decl_binds_to_current_def_p (node->decl))
1530 symtab_node *fn = node->ultimate_alias_target ();
1532 /* Ensure that the alias is well formed this may not be the case
1533 of user defined aliases and currently it is not always the case
1534 of C++ same body aliases (that is a bug). */
1535 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
1536 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
1537 || (TREE_CODE (node->decl) == FUNCTION_DECL
1538 && flags_from_decl_or_type (node->decl)
1539 != flags_from_decl_or_type (fn->decl))
1540 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
1541 return false;
1542 *(symtab_node **)data = node;
1543 return true;
1545 return false;
1548 /* If node can not be overwriten by static or dynamic linker to point to
1549 different definition, return NODE. Otherwise look for alias with such
1550 property and if none exists, introduce new one. */
1552 symtab_node *
1553 symtab_node::noninterposable_alias (void)
1555 tree new_decl;
1556 symtab_node *new_node = NULL;
1558 /* First try to look up existing alias or base object
1559 (if that is already non-overwritable). */
1560 symtab_node *node = ultimate_alias_target ();
1561 gcc_assert (!node->alias && !node->weakref);
1562 node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias,
1563 (void *)&new_node, true);
1564 if (new_node)
1565 return new_node;
1566 #ifndef ASM_OUTPUT_DEF
1567 /* If aliases aren't supported by the assembler, fail. */
1568 return NULL;
1569 #endif
1571 /* Otherwise create a new one. */
1572 new_decl = copy_node (node->decl);
1573 DECL_DLLIMPORT_P (new_decl) = 0;
1574 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1575 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1576 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1577 DECL_INITIAL (new_decl) = NULL;
1578 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1579 SET_DECL_RTL (new_decl, NULL);
1581 /* Update the properties. */
1582 DECL_EXTERNAL (new_decl) = 0;
1583 TREE_PUBLIC (new_decl) = 0;
1584 DECL_COMDAT (new_decl) = 0;
1585 DECL_WEAK (new_decl) = 0;
1587 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1588 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1589 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1591 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1592 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1593 new_node = cgraph_node::create_alias (new_decl, node->decl);
1595 else
1597 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1598 DECL_INITIAL (new_decl) = error_mark_node;
1599 new_node = varpool_node::create_alias (new_decl, node->decl);
1601 new_node->resolve_alias (node);
1602 gcc_assert (decl_binds_to_current_def_p (new_decl)
1603 && targetm.binds_local_p (new_decl));
1604 return new_node;
1607 /* Return true if symtab node and TARGET represents
1608 semantically equivalent symbols. */
1610 bool
1611 symtab_node::semantically_equivalent_p (symtab_node *target)
1613 enum availability avail;
1614 symtab_node *ba;
1615 symtab_node *bb;
1617 /* Equivalent functions are equivalent. */
1618 if (decl == target->decl)
1619 return true;
1621 /* If symbol is not overwritable by different implementation,
1622 walk to the base object it defines. */
1623 ba = ultimate_alias_target (&avail);
1624 if (avail >= AVAIL_AVAILABLE)
1626 if (target == ba)
1627 return true;
1629 else
1630 ba = this;
1631 bb = target->ultimate_alias_target (&avail);
1632 if (avail >= AVAIL_AVAILABLE)
1634 if (this == bb)
1635 return true;
1637 else
1638 bb = target;
1639 return bb == ba;
1642 /* Classify symbol symtab node for partitioning. */
1644 enum symbol_partitioning_class
1645 symtab_node::get_partitioning_class (void)
1647 /* Inline clones are always duplicated.
1648 This include external delcarations. */
1649 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
1651 if (DECL_ABSTRACT_P (decl))
1652 return SYMBOL_EXTERNAL;
1654 if (cnode && cnode->global.inlined_to)
1655 return SYMBOL_DUPLICATE;
1657 /* Weakref aliases are always duplicated. */
1658 if (weakref)
1659 return SYMBOL_DUPLICATE;
1661 /* External declarations are external. */
1662 if (DECL_EXTERNAL (decl))
1663 return SYMBOL_EXTERNAL;
1665 if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
1667 if (alias && definition && !ultimate_alias_target ()->definition)
1668 return SYMBOL_EXTERNAL;
1669 /* Constant pool references use local symbol names that can not
1670 be promoted global. We should never put into a constant pool
1671 objects that can not be duplicated across partitions. */
1672 if (DECL_IN_CONSTANT_POOL (decl))
1673 return SYMBOL_DUPLICATE;
1674 if (DECL_HARD_REGISTER (decl))
1675 return SYMBOL_DUPLICATE;
1676 gcc_checking_assert (vnode->definition);
1678 /* Functions that are cloned may stay in callgraph even if they are unused.
1679 Handle them as external; compute_ltrans_boundary take care to make
1680 proper things to happen (i.e. to make them appear in the boundary but
1681 with body streamed, so clone can me materialized). */
1682 else if (!dyn_cast <cgraph_node *> (this)->function_symbol ()->definition)
1683 return SYMBOL_EXTERNAL;
1685 /* Linker discardable symbols are duplicated to every use unless they are
1686 keyed. */
1687 if (DECL_ONE_ONLY (decl)
1688 && !force_output
1689 && !forced_by_abi
1690 && !used_from_object_file_p ())
1691 return SYMBOL_DUPLICATE;
1693 return SYMBOL_PARTITION;
1696 /* Return true when symbol is known to be non-zero. */
1698 bool
1699 symtab_node::nonzero_address ()
1701 /* Weakrefs may be NULL when their target is not defined. */
1702 if (alias && weakref)
1704 if (analyzed)
1706 symtab_node *target = ultimate_alias_target ();
1708 if (target->alias && target->weakref)
1709 return false;
1710 /* We can not recurse to target::nonzero. It is possible that the
1711 target is used only via the alias.
1712 We may walk references and look for strong use, but we do not know
1713 if this strong use will survive to final binary, so be
1714 conservative here.
1715 ??? Maybe we could do the lookup during late optimization that
1716 could be useful to eliminate the NULL pointer checks in LTO
1717 programs. */
1718 if (target->definition && !DECL_EXTERNAL (target->decl))
1719 return true;
1720 if (target->resolution != LDPR_UNKNOWN
1721 && target->resolution != LDPR_UNDEF
1722 && flag_delete_null_pointer_checks)
1723 return true;
1724 return false;
1726 else
1727 return false;
1730 /* With !flag_delete_null_pointer_checks we assume that symbols may
1731 bind to NULL. This is on by default on embedded targets only.
1733 Otherwise all non-WEAK symbols must be defined and thus non-NULL or
1734 linking fails. Important case of WEAK we want to do well are comdats.
1735 Those are handled by later check for definition.
1737 When parsing, beware the cases when WEAK attribute is added later. */
1738 if (!DECL_WEAK (decl)
1739 && flag_delete_null_pointer_checks)
1741 refuse_visibility_changes = true;
1742 return true;
1745 /* If target is defined and not extern, we know it will be output and thus
1746 it will bind to non-NULL.
1747 Play safe for flag_delete_null_pointer_checks where weak definition maye
1748 be re-defined by NULL. */
1749 if (definition && !DECL_EXTERNAL (decl)
1750 && (flag_delete_null_pointer_checks || !DECL_WEAK (decl)))
1752 if (!DECL_WEAK (decl))
1753 refuse_visibility_changes = true;
1754 return true;
1757 /* As the last resort, check the resolution info. */
1758 if (resolution != LDPR_UNKNOWN
1759 && resolution != LDPR_UNDEF
1760 && flag_delete_null_pointer_checks)
1761 return true;
1762 return false;
1765 /* Return 0 if symbol is known to have different address than S2,
1766 Return 1 if symbol is known to have same address as S2,
1767 return 2 otherwise. */
1769 symtab_node::equal_address_to (symtab_node *s2)
1771 enum availability avail1, avail2;
1773 /* A Shortcut: equivalent symbols are always equivalent. */
1774 if (this == s2)
1775 return 1;
1777 /* For non-interposable aliases, lookup and compare their actual definitions.
1778 Also check if the symbol needs to bind to given definition. */
1779 symtab_node *rs1 = ultimate_alias_target (&avail1);
1780 symtab_node *rs2 = s2->ultimate_alias_target (&avail2);
1781 bool binds_local1 = rs1->analyzed && decl_binds_to_current_def_p (this->decl);
1782 bool binds_local2 = rs2->analyzed && decl_binds_to_current_def_p (s2->decl);
1783 bool really_binds_local1 = binds_local1;
1784 bool really_binds_local2 = binds_local2;
1786 /* Addresses of vtables and virtual functions can not be used by user
1787 code and are used only within speculation. In this case we may make
1788 symbol equivalent to its alias even if interposition may break this
1789 rule. Doing so will allow us to turn speculative inlining into
1790 non-speculative more agressively. */
1791 if (DECL_VIRTUAL_P (this->decl) && avail1 >= AVAIL_AVAILABLE)
1792 binds_local1 = true;
1793 if (DECL_VIRTUAL_P (s2->decl) && avail2 >= AVAIL_AVAILABLE)
1794 binds_local2 = true;
1796 /* If both definitions are available we know that even if they are bound
1797 to other unit they must be defined same way and therefore we can use
1798 equivalence test. */
1799 if (rs1 != rs2 && avail1 >= AVAIL_AVAILABLE && avail2 >= AVAIL_AVAILABLE)
1800 binds_local1 = binds_local2 = true;
1802 if ((binds_local1 ? rs1 : this)
1803 == (binds_local2 ? rs2 : s2))
1805 /* We made use of the fact that alias is not weak. */
1806 if (binds_local1 && rs1 != this)
1807 refuse_visibility_changes = true;
1808 if (binds_local2 && rs2 != s2)
1809 s2->refuse_visibility_changes = true;
1810 return 1;
1813 /* If both symbols may resolve to NULL, we can not really prove them different. */
1814 if (!nonzero_address () && !s2->nonzero_address ())
1815 return 2;
1817 /* Except for NULL, functions and variables never overlap. */
1818 if (TREE_CODE (decl) != TREE_CODE (s2->decl))
1819 return 0;
1821 /* If one of the symbols is unresolved alias, punt. */
1822 if (rs1->alias || rs2->alias)
1823 return 2;
1825 /* If we have a non-interposale definition of at least one of the symbols
1826 and the other symbol is different, we know other unit can not interpose
1827 it to the first symbol; all aliases of the definition needs to be
1828 present in the current unit. */
1829 if (((really_binds_local1 || really_binds_local2)
1830 /* If we have both definitions and they are different, we know they
1831 will be different even in units they binds to. */
1832 || (binds_local1 && binds_local2))
1833 && rs1 != rs2)
1835 /* We make use of the fact that one symbol is not alias of the other
1836 and that the definition is non-interposable. */
1837 refuse_visibility_changes = true;
1838 s2->refuse_visibility_changes = true;
1839 rs1->refuse_visibility_changes = true;
1840 rs2->refuse_visibility_changes = true;
1841 return 0;
1844 /* TODO: Alias oracle basically assume that addresses of global variables
1845 are different unless they are declared as alias of one to another.
1846 We probably should be consistent and use this fact here, too, and update
1847 alias oracle to use this predicate. */
1849 return 2;
1852 /* Worker for call_for_symbol_and_aliases. */
1854 bool
1855 symtab_node::call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *,
1856 void *),
1857 void *data,
1858 bool include_overwritable)
1860 ipa_ref *ref;
1861 FOR_EACH_ALIAS (this, ref)
1863 symtab_node *alias = ref->referring;
1864 if (include_overwritable
1865 || alias->get_availability () > AVAIL_INTERPOSABLE)
1866 if (alias->call_for_symbol_and_aliases (callback, data,
1867 include_overwritable))
1868 return true;
1870 return false;
1873 /* Return true if address of N is possibly compared. */
1875 static bool
1876 address_matters_1 (symtab_node *n, void *)
1878 struct ipa_ref *ref;
1880 if (!n->address_can_be_compared_p ())
1881 return false;
1882 if (n->externally_visible || n->force_output)
1883 return true;
1885 for (unsigned int i = 0; n->iterate_referring (i, ref); i++)
1886 if (ref->address_matters_p ())
1887 return true;
1888 return false;
1891 /* Return true if symbol's address may possibly be compared to other
1892 symbol's address. */
1894 bool
1895 symtab_node::address_matters_p ()
1897 gcc_assert (!alias);
1898 return call_for_symbol_and_aliases (address_matters_1, NULL, true);
1901 /* Return true if symbol's alignment may be increased. */
1903 bool
1904 symtab_node::can_increase_alignment_p (void)
1906 symtab_node *target = ultimate_alias_target ();
1908 /* For now support only variables. */
1909 if (TREE_CODE (decl) != VAR_DECL)
1910 return false;
1912 /* With -fno-toplevel-reorder we may have already output the constant. */
1913 if (TREE_ASM_WRITTEN (target->decl))
1914 return false;
1916 /* If target is already placed in an anchor, we can not touch its
1917 alignment. */
1918 if (DECL_RTL_SET_P (target->decl)
1919 && MEM_P (DECL_RTL (target->decl))
1920 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (DECL_RTL (target->decl), 0)))
1921 return false;
1923 /* Constant pool entries may be shared. */
1924 if (DECL_IN_CONSTANT_POOL (target->decl))
1925 return false;
1927 /* We cannot change alignment of symbols that may bind to symbols
1928 in other translation unit that may contain a definition with lower
1929 alignment. */
1930 if (!decl_binds_to_current_def_p (decl))
1931 return false;
1933 /* When compiling partition, be sure the symbol is not output by other
1934 partition. */
1935 if (flag_ltrans
1936 && (target->in_other_partition
1937 || target->get_partitioning_class () == SYMBOL_DUPLICATE))
1938 return false;
1940 /* Do not override the alignment as specified by the ABI when the used
1941 attribute is set. */
1942 if (DECL_PRESERVE_P (decl) || DECL_PRESERVE_P (target->decl))
1943 return false;
1945 /* Do not override explicit alignment set by the user when an explicit
1946 section name is also used. This is a common idiom used by many
1947 software projects. */
1948 if (DECL_SECTION_NAME (target->decl) != NULL && !target->implicit_section)
1949 return false;
1951 return true;
1954 /* Worker for symtab_node::increase_alignment. */
1956 static bool
1957 increase_alignment_1 (symtab_node *n, void *v)
1959 unsigned int align = (size_t)v;
1960 if (DECL_ALIGN (n->decl) < align
1961 && n->can_increase_alignment_p ())
1963 DECL_ALIGN (n->decl) = align;
1964 DECL_USER_ALIGN (n->decl) = 1;
1966 return false;
1969 /* Increase alignment of THIS to ALIGN. */
1971 void
1972 symtab_node::increase_alignment (unsigned int align)
1974 gcc_assert (can_increase_alignment_p () && align < MAX_OFILE_ALIGNMENT);
1975 ultimate_alias_target()->call_for_symbol_and_aliases (increase_alignment_1,
1976 (void *)(size_t) align,
1977 true);
1978 gcc_assert (DECL_ALIGN (decl) >= align);
1981 /* Helper for symtab_node::definition_alignment. */
1983 static bool
1984 get_alignment_1 (symtab_node *n, void *v)
1986 *((unsigned int *)v) = MAX (*((unsigned int *)v), DECL_ALIGN (n->decl));
1987 return false;
1990 /* Return desired alignment of the definition. This is NOT alignment useful
1991 to access THIS, because THIS may be interposable and DECL_ALIGN should
1992 be used instead. It however must be guaranteed when output definition
1993 of THIS. */
1995 unsigned int
1996 symtab_node::definition_alignment ()
1998 unsigned int align = 0;
1999 gcc_assert (!alias);
2000 call_for_symbol_and_aliases (get_alignment_1, &align, true);
2001 return align;