Configury changes for obstack optimization
[official-gcc.git] / gcc / symtab.c
blob9677131562815478b8c139be31a37ef0db7e4919
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 "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "timevar.h"
30 #include "cgraph.h"
31 #include "lto-streamer.h"
32 #include "print-tree.h"
33 #include "varasm.h"
34 #include "langhooks.h"
35 #include "output.h"
36 #include "ipa-utils.h"
37 #include "calls.h"
39 static const char *ipa_ref_use_name[] = {"read","write","addr","alias","chkp"};
41 const char * const ld_plugin_symbol_resolution_names[]=
43 "",
44 "undef",
45 "prevailing_def",
46 "prevailing_def_ironly",
47 "preempted_reg",
48 "preempted_ir",
49 "resolved_ir",
50 "resolved_exec",
51 "resolved_dyn",
52 "prevailing_def_ironly_exp"
55 /* Hash asmnames ignoring the user specified marks. */
57 hashval_t
58 symbol_table::decl_assembler_name_hash (const_tree asmname)
60 if (IDENTIFIER_POINTER (asmname)[0] == '*')
62 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
63 size_t ulp_len = strlen (user_label_prefix);
65 if (ulp_len == 0)
67 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
68 decl_str += ulp_len;
70 return htab_hash_string (decl_str);
73 return htab_hash_string (IDENTIFIER_POINTER (asmname));
77 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
79 bool
80 symbol_table::decl_assembler_name_equal (tree decl, const_tree asmname)
82 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
83 const char *decl_str;
84 const char *asmname_str;
85 bool test = false;
87 if (decl_asmname == asmname)
88 return true;
90 decl_str = IDENTIFIER_POINTER (decl_asmname);
91 asmname_str = IDENTIFIER_POINTER (asmname);
94 /* If the target assembler name was set by the user, things are trickier.
95 We have a leading '*' to begin with. After that, it's arguable what
96 is the correct thing to do with -fleading-underscore. Arguably, we've
97 historically been doing the wrong thing in assemble_alias by always
98 printing the leading underscore. Since we're not changing that, make
99 sure user_label_prefix follows the '*' before matching. */
100 if (decl_str[0] == '*')
102 size_t ulp_len = strlen (user_label_prefix);
104 decl_str ++;
106 if (ulp_len == 0)
107 test = true;
108 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
109 decl_str += ulp_len, test=true;
110 else
111 decl_str --;
113 if (asmname_str[0] == '*')
115 size_t ulp_len = strlen (user_label_prefix);
117 asmname_str ++;
119 if (ulp_len == 0)
120 test = true;
121 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
122 asmname_str += ulp_len, test=true;
123 else
124 asmname_str --;
127 if (!test)
128 return false;
129 return strcmp (decl_str, asmname_str) == 0;
133 /* Returns nonzero if P1 and P2 are equal. */
135 /* Insert NODE to assembler name hash. */
137 void
138 symbol_table::insert_to_assembler_name_hash (symtab_node *node,
139 bool with_clones)
141 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
142 return;
143 gcc_checking_assert (!node->previous_sharing_asm_name
144 && !node->next_sharing_asm_name);
145 if (assembler_name_hash)
147 symtab_node **aslot;
148 cgraph_node *cnode;
149 tree decl = node->decl;
151 tree name = DECL_ASSEMBLER_NAME (node->decl);
153 /* C++ FE can produce decls without associated assembler name and insert
154 them to symtab to hold section or TLS information. */
155 if (!name)
156 return;
158 hashval_t hash = decl_assembler_name_hash (name);
159 aslot = assembler_name_hash->find_slot_with_hash (name, hash, INSERT);
160 gcc_assert (*aslot != node);
161 node->next_sharing_asm_name = (symtab_node *)*aslot;
162 if (*aslot != NULL)
163 (*aslot)->previous_sharing_asm_name = node;
164 *aslot = node;
166 /* Update also possible inline clones sharing a decl. */
167 cnode = dyn_cast <cgraph_node *> (node);
168 if (cnode && cnode->clones && with_clones)
169 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
170 if (cnode->decl == decl)
171 insert_to_assembler_name_hash (cnode, true);
176 /* Remove NODE from assembler name hash. */
178 void
179 symbol_table::unlink_from_assembler_name_hash (symtab_node *node,
180 bool with_clones)
182 if (assembler_name_hash)
184 cgraph_node *cnode;
185 tree decl = node->decl;
187 if (node->next_sharing_asm_name)
188 node->next_sharing_asm_name->previous_sharing_asm_name
189 = node->previous_sharing_asm_name;
190 if (node->previous_sharing_asm_name)
192 node->previous_sharing_asm_name->next_sharing_asm_name
193 = node->next_sharing_asm_name;
195 else
197 tree name = DECL_ASSEMBLER_NAME (node->decl);
198 symtab_node **slot;
200 if (!name)
201 return;
203 hashval_t hash = decl_assembler_name_hash (name);
204 slot = assembler_name_hash->find_slot_with_hash (name, hash,
205 NO_INSERT);
206 gcc_assert (*slot == node);
207 if (!node->next_sharing_asm_name)
208 assembler_name_hash->clear_slot (slot);
209 else
210 *slot = node->next_sharing_asm_name;
212 node->next_sharing_asm_name = NULL;
213 node->previous_sharing_asm_name = NULL;
215 /* Update also possible inline clones sharing a decl. */
216 cnode = dyn_cast <cgraph_node *> (node);
217 if (cnode && cnode->clones && with_clones)
218 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
219 if (cnode->decl == decl)
220 unlink_from_assembler_name_hash (cnode, true);
224 /* Arrange node to be first in its entry of assembler_name_hash. */
226 void
227 symbol_table::symtab_prevail_in_asm_name_hash (symtab_node *node)
229 unlink_from_assembler_name_hash (node, false);
230 insert_to_assembler_name_hash (node, false);
233 /* Initalize asm name hash unless. */
235 void
236 symbol_table::symtab_initialize_asm_name_hash (void)
238 symtab_node *node;
239 if (!assembler_name_hash)
241 assembler_name_hash = hash_table<asmname_hasher>::create_ggc (10);
242 FOR_EACH_SYMBOL (node)
243 insert_to_assembler_name_hash (node, false);
247 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
249 void
250 symbol_table::change_decl_assembler_name (tree decl, tree name)
252 symtab_node *node = NULL;
254 /* We can have user ASM names on things, like global register variables, that
255 are not in the symbol table. */
256 if ((TREE_CODE (decl) == VAR_DECL
257 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
258 || TREE_CODE (decl) == FUNCTION_DECL)
259 node = symtab_node::get (decl);
260 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
262 SET_DECL_ASSEMBLER_NAME (decl, name);
263 if (node)
264 insert_to_assembler_name_hash (node, true);
266 else
268 if (name == DECL_ASSEMBLER_NAME (decl))
269 return;
271 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
272 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
273 : NULL);
274 if (node)
275 unlink_from_assembler_name_hash (node, true);
276 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
277 && DECL_RTL_SET_P (decl))
278 warning (0, "%D renamed after being referenced in assembly", decl);
280 SET_DECL_ASSEMBLER_NAME (decl, name);
281 if (alias)
283 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
284 TREE_CHAIN (name) = alias;
286 if (node)
287 insert_to_assembler_name_hash (node, true);
291 /* Hash sections by their names. */
293 hashval_t
294 section_name_hasher::hash (section_hash_entry *n)
296 return htab_hash_string (n->name);
299 /* Return true if section P1 name equals to P2. */
301 bool
302 section_name_hasher::equal (section_hash_entry *n1, const char *name)
304 return n1->name == name || !strcmp (n1->name, name);
307 /* Add node into symbol table. This function is not used directly, but via
308 cgraph/varpool node creation routines. */
310 void
311 symtab_node::register_symbol (void)
313 symtab->register_symbol (this);
315 if (!decl->decl_with_vis.symtab_node)
316 decl->decl_with_vis.symtab_node = this;
318 ref_list.clear ();
320 /* Be sure to do this last; C++ FE might create new nodes via
321 DECL_ASSEMBLER_NAME langhook! */
322 symtab->insert_to_assembler_name_hash (this, false);
325 /* Remove NODE from same comdat group. */
327 void
328 symtab_node::remove_from_same_comdat_group (void)
330 if (same_comdat_group)
332 symtab_node *prev;
333 for (prev = same_comdat_group;
334 prev->same_comdat_group != this;
335 prev = prev->same_comdat_group)
337 if (same_comdat_group == prev)
338 prev->same_comdat_group = NULL;
339 else
340 prev->same_comdat_group = same_comdat_group;
341 same_comdat_group = NULL;
342 set_comdat_group (NULL);
346 /* Remove node from symbol table. This function is not used directly, but via
347 cgraph/varpool node removal routines. */
349 void
350 symtab_node::unregister (void)
352 remove_all_references ();
353 remove_all_referring ();
355 /* Remove reference to section. */
356 set_section_for_node (NULL);
358 remove_from_same_comdat_group ();
360 symtab->unregister (this);
362 /* During LTO symtab merging we temporarily corrupt decl to symtab node
363 hash. */
364 gcc_assert (decl->decl_with_vis.symtab_node || in_lto_p);
365 if (decl->decl_with_vis.symtab_node == this)
367 symtab_node *replacement_node = NULL;
368 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
369 replacement_node = cnode->find_replacement ();
370 decl->decl_with_vis.symtab_node = replacement_node;
372 if (!is_a <varpool_node *> (this) || !DECL_HARD_REGISTER (decl))
373 symtab->unlink_from_assembler_name_hash (this, false);
374 if (in_init_priority_hash)
375 symtab->init_priority_hash->remove (this);
379 /* Remove symbol from symbol table. */
381 void
382 symtab_node::remove (void)
384 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
385 cnode->remove ();
386 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
387 vnode->remove ();
390 /* Add NEW_ to the same comdat group that OLD is in. */
392 void
393 symtab_node::add_to_same_comdat_group (symtab_node *old_node)
395 gcc_assert (old_node->get_comdat_group ());
396 gcc_assert (!same_comdat_group);
397 gcc_assert (this != old_node);
399 set_comdat_group (old_node->get_comdat_group ());
400 same_comdat_group = old_node;
401 if (!old_node->same_comdat_group)
402 old_node->same_comdat_group = this;
403 else
405 symtab_node *n;
406 for (n = old_node->same_comdat_group;
407 n->same_comdat_group != old_node;
408 n = n->same_comdat_group)
410 n->same_comdat_group = this;
414 /* Dissolve the same_comdat_group list in which NODE resides. */
416 void
417 symtab_node::dissolve_same_comdat_group_list (void)
419 symtab_node *n = this;
420 symtab_node *next;
422 if (!same_comdat_group)
423 return;
426 next = n->same_comdat_group;
427 n->same_comdat_group = NULL;
428 /* Clear comdat_group for comdat locals, since
429 make_decl_local doesn't. */
430 if (!TREE_PUBLIC (n->decl))
431 n->set_comdat_group (NULL);
432 n = next;
434 while (n != this);
437 /* Return printable assembler name of NODE.
438 This function is used only for debugging. When assembler name
439 is unknown go with identifier name. */
441 const char *
442 symtab_node::asm_name () const
444 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
445 return lang_hooks.decl_printable_name (decl, 2);
446 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
449 /* Return printable identifier name. */
451 const char *
452 symtab_node::name () const
454 return lang_hooks.decl_printable_name (decl, 2);
457 /* Return ipa reference from this symtab_node to
458 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
459 of the use. */
461 ipa_ref *
462 symtab_node::create_reference (symtab_node *referred_node,
463 enum ipa_ref_use use_type)
465 return create_reference (referred_node, use_type, NULL);
469 /* Return ipa reference from this symtab_node to
470 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
471 of the use and STMT the statement (if it exists). */
473 ipa_ref *
474 symtab_node::create_reference (symtab_node *referred_node,
475 enum ipa_ref_use use_type, gimple *stmt)
477 ipa_ref *ref = NULL, *ref2 = NULL;
478 ipa_ref_list *list, *list2;
479 ipa_ref_t *old_references;
481 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
482 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
484 list = &ref_list;
485 old_references = vec_safe_address (list->references);
486 vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
487 ref = &list->references->last ();
489 list2 = &referred_node->ref_list;
491 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
492 if(use_type == IPA_REF_ALIAS)
494 list2->referring.safe_insert (0, ref);
495 ref->referred_index = 0;
497 for (unsigned int i = 1; i < list2->referring.length (); i++)
498 list2->referring[i]->referred_index = i;
500 else
502 list2->referring.safe_push (ref);
503 ref->referred_index = list2->referring.length () - 1;
506 ref->referring = this;
507 ref->referred = referred_node;
508 ref->stmt = stmt;
509 ref->lto_stmt_uid = 0;
510 ref->use = use_type;
511 ref->speculative = 0;
513 /* If vector was moved in memory, update pointers. */
514 if (old_references != list->references->address ())
516 int i;
517 for (i = 0; iterate_reference(i, ref2); i++)
518 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
520 return ref;
523 /* If VAL is a reference to a function or a variable, add a reference from
524 this symtab_node to the corresponding symbol table node. USE_TYPE specify
525 type of the use and STMT the statement (if it exists). Return the new
526 reference or NULL if none was created. */
528 ipa_ref *
529 symtab_node::maybe_create_reference (tree val, enum ipa_ref_use use_type,
530 gimple *stmt)
532 STRIP_NOPS (val);
533 if (TREE_CODE (val) != ADDR_EXPR)
534 return NULL;
535 val = get_base_var (val);
536 if (val && (TREE_CODE (val) == FUNCTION_DECL
537 || TREE_CODE (val) == VAR_DECL))
539 symtab_node *referred = symtab_node::get (val);
540 gcc_checking_assert (referred);
541 return create_reference (referred, use_type, stmt);
543 return NULL;
546 /* Clone all references from symtab NODE to this symtab_node. */
548 void
549 symtab_node::clone_references (symtab_node *node)
551 ipa_ref *ref = NULL, *ref2 = NULL;
552 int i;
553 for (i = 0; node->iterate_reference (i, ref); i++)
555 bool speculative = ref->speculative;
556 unsigned int stmt_uid = ref->lto_stmt_uid;
558 ref2 = create_reference (ref->referred, ref->use, ref->stmt);
559 ref2->speculative = speculative;
560 ref2->lto_stmt_uid = stmt_uid;
564 /* Clone all referring from symtab NODE to this symtab_node. */
566 void
567 symtab_node::clone_referring (symtab_node *node)
569 ipa_ref *ref = NULL, *ref2 = NULL;
570 int i;
571 for (i = 0; node->iterate_referring(i, ref); i++)
573 bool speculative = ref->speculative;
574 unsigned int stmt_uid = ref->lto_stmt_uid;
576 ref2 = ref->referring->create_reference (this, ref->use, ref->stmt);
577 ref2->speculative = speculative;
578 ref2->lto_stmt_uid = stmt_uid;
582 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
584 ipa_ref *
585 symtab_node::clone_reference (ipa_ref *ref, gimple *stmt)
587 bool speculative = ref->speculative;
588 unsigned int stmt_uid = ref->lto_stmt_uid;
589 ipa_ref *ref2;
591 ref2 = create_reference (ref->referred, ref->use, stmt);
592 ref2->speculative = speculative;
593 ref2->lto_stmt_uid = stmt_uid;
594 return ref2;
597 /* Find the structure describing a reference to REFERRED_NODE
598 and associated with statement STMT. */
600 ipa_ref *
601 symtab_node::find_reference (symtab_node *referred_node,
602 gimple *stmt, unsigned int lto_stmt_uid)
604 ipa_ref *r = NULL;
605 int i;
607 for (i = 0; iterate_reference (i, r); i++)
608 if (r->referred == referred_node
609 && !r->speculative
610 && ((stmt && r->stmt == stmt)
611 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
612 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
613 return r;
614 return NULL;
617 /* Remove all references that are associated with statement STMT. */
619 void
620 symtab_node::remove_stmt_references (gimple *stmt)
622 ipa_ref *r = NULL;
623 int i = 0;
625 while (iterate_reference (i, r))
626 if (r->stmt == stmt)
627 r->remove_reference ();
628 else
629 i++;
632 /* Remove all stmt references in non-speculative references.
633 Those are not maintained during inlining & clonning.
634 The exception are speculative references that are updated along
635 with callgraph edges associated with them. */
637 void
638 symtab_node::clear_stmts_in_references (void)
640 ipa_ref *r = NULL;
641 int i;
643 for (i = 0; iterate_reference (i, r); i++)
644 if (!r->speculative)
646 r->stmt = NULL;
647 r->lto_stmt_uid = 0;
651 /* Remove all references in ref list. */
653 void
654 symtab_node::remove_all_references (void)
656 while (vec_safe_length (ref_list.references))
657 ref_list.references->last ().remove_reference ();
658 vec_free (ref_list.references);
661 /* Remove all referring items in ref list. */
663 void
664 symtab_node::remove_all_referring (void)
666 while (ref_list.referring.length ())
667 ref_list.referring.last ()->remove_reference ();
668 ref_list.referring.release ();
671 /* Dump references in ref list to FILE. */
673 void
674 symtab_node::dump_references (FILE *file)
676 ipa_ref *ref = NULL;
677 int i;
678 for (i = 0; iterate_reference (i, ref); i++)
680 fprintf (file, "%s/%i (%s)",
681 ref->referred->asm_name (),
682 ref->referred->order,
683 ipa_ref_use_name [ref->use]);
684 if (ref->speculative)
685 fprintf (file, " (speculative)");
687 fprintf (file, "\n");
690 /* Dump referring in list to FILE. */
692 void
693 symtab_node::dump_referring (FILE *file)
695 ipa_ref *ref = NULL;
696 int i;
697 for (i = 0; iterate_referring(i, ref); i++)
699 fprintf (file, "%s/%i (%s)",
700 ref->referring->asm_name (),
701 ref->referring->order,
702 ipa_ref_use_name [ref->use]);
703 if (ref->speculative)
704 fprintf (file, " (speculative)");
706 fprintf (file, "\n");
709 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
711 /* Dump base fields of symtab nodes to F. Not to be used directly. */
713 void
714 symtab_node::dump_base (FILE *f)
716 static const char * const visibility_types[] = {
717 "default", "protected", "hidden", "internal"
720 fprintf (f, "%s/%i (%s)", asm_name (), order, name ());
721 dump_addr (f, " @", (void *)this);
722 fprintf (f, "\n Type: %s", symtab_type_names[type]);
724 if (definition)
725 fprintf (f, " definition");
726 if (analyzed)
727 fprintf (f, " analyzed");
728 if (alias)
729 fprintf (f, " alias");
730 if (weakref)
731 fprintf (f, " weakref");
732 if (cpp_implicit_alias)
733 fprintf (f, " cpp_implicit_alias");
734 if (alias_target)
735 fprintf (f, " target:%s",
736 DECL_P (alias_target)
737 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
738 (alias_target))
739 : IDENTIFIER_POINTER (alias_target));
740 if (body_removed)
741 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
742 fprintf (f, "\n Visibility:");
743 if (in_other_partition)
744 fprintf (f, " in_other_partition");
745 if (used_from_other_partition)
746 fprintf (f, " used_from_other_partition");
747 if (force_output)
748 fprintf (f, " force_output");
749 if (forced_by_abi)
750 fprintf (f, " forced_by_abi");
751 if (externally_visible)
752 fprintf (f, " externally_visible");
753 if (no_reorder)
754 fprintf (f, " no_reorder");
755 if (resolution != LDPR_UNKNOWN)
756 fprintf (f, " %s",
757 ld_plugin_symbol_resolution_names[(int)resolution]);
758 if (TREE_ASM_WRITTEN (decl))
759 fprintf (f, " asm_written");
760 if (DECL_EXTERNAL (decl))
761 fprintf (f, " external");
762 if (TREE_PUBLIC (decl))
763 fprintf (f, " public");
764 if (DECL_COMMON (decl))
765 fprintf (f, " common");
766 if (DECL_WEAK (decl))
767 fprintf (f, " weak");
768 if (DECL_DLLIMPORT_P (decl))
769 fprintf (f, " dll_import");
770 if (DECL_COMDAT (decl))
771 fprintf (f, " comdat");
772 if (get_comdat_group ())
773 fprintf (f, " comdat_group:%s",
774 IDENTIFIER_POINTER (get_comdat_group_id ()));
775 if (DECL_ONE_ONLY (decl))
776 fprintf (f, " one_only");
777 if (get_section ())
778 fprintf (f, " section:%s",
779 get_section ());
780 if (implicit_section)
781 fprintf (f," (implicit_section)");
782 if (DECL_VISIBILITY_SPECIFIED (decl))
783 fprintf (f, " visibility_specified");
784 if (DECL_VISIBILITY (decl))
785 fprintf (f, " visibility:%s",
786 visibility_types [DECL_VISIBILITY (decl)]);
787 if (DECL_VIRTUAL_P (decl))
788 fprintf (f, " virtual");
789 if (DECL_ARTIFICIAL (decl))
790 fprintf (f, " artificial");
791 if (TREE_CODE (decl) == FUNCTION_DECL)
793 if (DECL_STATIC_CONSTRUCTOR (decl))
794 fprintf (f, " constructor");
795 if (DECL_STATIC_DESTRUCTOR (decl))
796 fprintf (f, " destructor");
798 fprintf (f, "\n");
800 if (same_comdat_group)
801 fprintf (f, " Same comdat group as: %s/%i\n",
802 same_comdat_group->asm_name (),
803 same_comdat_group->order);
804 if (next_sharing_asm_name)
805 fprintf (f, " next sharing asm name: %i\n",
806 next_sharing_asm_name->order);
807 if (previous_sharing_asm_name)
808 fprintf (f, " previous sharing asm name: %i\n",
809 previous_sharing_asm_name->order);
811 if (address_taken)
812 fprintf (f, " Address is taken.\n");
813 if (aux)
815 fprintf (f, " Aux:");
816 dump_addr (f, " @", (void *)aux);
819 fprintf (f, " References: ");
820 dump_references (f);
821 fprintf (f, " Referring: ");
822 dump_referring (f);
823 if (lto_file_data)
824 fprintf (f, " Read from file: %s\n",
825 lto_file_data->file_name);
828 /* Dump symtab node to F. */
830 void
831 symtab_node::dump (FILE *f)
833 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
834 cnode->dump (f);
835 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
836 vnode->dump (f);
839 /* Dump symbol table to F. */
841 void
842 symtab_node::dump_table (FILE *f)
844 symtab_node *node;
845 fprintf (f, "Symbol table:\n\n");
846 FOR_EACH_SYMBOL (node)
847 node->dump (f);
851 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
852 Return NULL if there's no such node. */
854 symtab_node *
855 symtab_node::get_for_asmname (const_tree asmname)
857 symtab_node *node;
859 symtab->symtab_initialize_asm_name_hash ();
860 hashval_t hash = symtab->decl_assembler_name_hash (asmname);
861 symtab_node **slot
862 = symtab->assembler_name_hash->find_slot_with_hash (asmname, hash,
863 NO_INSERT);
865 if (slot)
867 node = *slot;
868 return node;
870 return NULL;
873 /* Dump symtab node NODE to stderr. */
875 DEBUG_FUNCTION void
876 symtab_node::debug (void)
878 dump (stderr);
881 /* Verify common part of symtab nodes. */
883 DEBUG_FUNCTION bool
884 symtab_node::verify_base (void)
886 bool error_found = false;
887 symtab_node *hashed_node;
889 if (is_a <cgraph_node *> (this))
891 if (TREE_CODE (decl) != FUNCTION_DECL)
893 error ("function symbol is not function");
894 error_found = true;
897 else if (is_a <varpool_node *> (this))
899 if (TREE_CODE (decl) != VAR_DECL)
901 error ("variable symbol is not variable");
902 error_found = true;
905 else
907 error ("node has unknown type");
908 error_found = true;
911 if (symtab->state != LTO_STREAMING)
913 hashed_node = symtab_node::get (decl);
914 if (!hashed_node)
916 error ("node not found node->decl->decl_with_vis.symtab_node");
917 error_found = true;
919 if (hashed_node != this
920 && (!is_a <cgraph_node *> (this)
921 || !dyn_cast <cgraph_node *> (this)->clone_of
922 || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl))
924 error ("node differs from node->decl->decl_with_vis.symtab_node");
925 error_found = true;
928 if (symtab->assembler_name_hash)
930 hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
931 if (hashed_node && hashed_node->previous_sharing_asm_name)
933 error ("assembler name hash list corrupted");
934 error_found = true;
936 while (hashed_node)
938 if (hashed_node == this)
939 break;
940 hashed_node = hashed_node->next_sharing_asm_name;
942 if (!hashed_node
943 && !(is_a <varpool_node *> (this)
944 || DECL_HARD_REGISTER (decl)))
946 error ("node not found in symtab assembler name hash");
947 error_found = true;
950 if (previous_sharing_asm_name
951 && previous_sharing_asm_name->next_sharing_asm_name != this)
953 error ("double linked list of assembler names corrupted");
954 error_found = true;
956 if (body_removed && definition)
958 error ("node has body_removed but is definition");
959 error_found = true;
961 if (analyzed && !definition)
963 error ("node is analyzed byt it is not a definition");
964 error_found = true;
966 if (cpp_implicit_alias && !alias)
968 error ("node is alias but not implicit alias");
969 error_found = true;
971 if (alias && !definition && !weakref)
973 error ("node is alias but not definition");
974 error_found = true;
976 if (weakref && !alias)
978 error ("node is weakref but not an alias");
979 error_found = true;
981 if (same_comdat_group)
983 symtab_node *n = same_comdat_group;
985 if (!n->get_comdat_group ())
987 error ("node is in same_comdat_group list but has no comdat_group");
988 error_found = true;
990 if (n->get_comdat_group () != get_comdat_group ())
992 error ("same_comdat_group list across different groups");
993 error_found = true;
995 if (n->type != type)
997 error ("mixing different types of symbol in same comdat groups is not supported");
998 error_found = true;
1000 if (n == this)
1002 error ("node is alone in a comdat group");
1003 error_found = true;
1007 if (!n->same_comdat_group)
1009 error ("same_comdat_group is not a circular list");
1010 error_found = true;
1011 break;
1013 n = n->same_comdat_group;
1015 while (n != this);
1016 if (comdat_local_p ())
1018 ipa_ref *ref = NULL;
1020 for (int i = 0; iterate_referring (i, ref); ++i)
1022 if (!in_same_comdat_group_p (ref->referring))
1024 error ("comdat-local symbol referred to by %s outside its "
1025 "comdat",
1026 identifier_to_locale (ref->referring->name()));
1027 error_found = true;
1032 if (implicit_section && !get_section ())
1034 error ("implicit_section flag is set but section isn't");
1035 error_found = true;
1037 if (get_section () && get_comdat_group ()
1038 && !implicit_section
1039 && !lookup_attribute ("section", DECL_ATTRIBUTES (decl)))
1041 error ("Both section and comdat group is set");
1042 error_found = true;
1044 /* TODO: Add string table for sections, so we do not keep holding duplicated
1045 strings. */
1046 if (alias && definition
1047 && get_section () != get_alias_target ()->get_section ()
1048 && (!get_section()
1049 || !get_alias_target ()->get_section ()
1050 || strcmp (get_section(),
1051 get_alias_target ()->get_section ())))
1053 error ("Alias and target's section differs");
1054 get_alias_target ()->dump (stderr);
1055 error_found = true;
1057 if (alias && definition
1058 && get_comdat_group () != get_alias_target ()->get_comdat_group ())
1060 error ("Alias and target's comdat groups differs");
1061 get_alias_target ()->dump (stderr);
1062 error_found = true;
1065 return error_found;
1068 /* Verify consistency of NODE. */
1070 DEBUG_FUNCTION void
1071 symtab_node::verify (void)
1073 if (seen_error ())
1074 return;
1076 timevar_push (TV_CGRAPH_VERIFY);
1077 if (cgraph_node *node = dyn_cast <cgraph_node *> (this))
1078 node->verify_node ();
1079 else
1080 if (verify_base ())
1082 debug ();
1083 internal_error ("symtab_node::verify failed");
1085 timevar_pop (TV_CGRAPH_VERIFY);
1088 /* Verify symbol table for internal consistency. */
1090 DEBUG_FUNCTION void
1091 symtab_node::verify_symtab_nodes (void)
1093 symtab_node *node;
1094 hash_map<tree, symtab_node *> comdat_head_map (251);
1096 FOR_EACH_SYMBOL (node)
1098 node->verify ();
1099 if (node->get_comdat_group ())
1101 symtab_node **entry, *s;
1102 bool existed;
1104 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1105 &existed);
1106 if (!existed)
1107 *entry = node;
1108 else if (!DECL_EXTERNAL (node->decl))
1110 for (s = (*entry)->same_comdat_group;
1111 s != NULL && s != node && s != *entry;
1112 s = s->same_comdat_group)
1114 if (!s || s == *entry)
1116 error ("Two symbols with same comdat_group are not linked by "
1117 "the same_comdat_group list.");
1118 (*entry)->debug ();
1119 node->debug ();
1120 internal_error ("symtab_node::verify failed");
1127 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1128 but other code such as notice_global_symbol generates rtl. */
1130 void
1131 symtab_node::make_decl_local (void)
1133 rtx rtl, symbol;
1135 /* Avoid clearing comdat_groups on comdat-local decls. */
1136 if (TREE_PUBLIC (decl) == 0)
1137 return;
1139 if (TREE_CODE (decl) == VAR_DECL)
1141 DECL_COMMON (decl) = 0;
1142 /* ADDRESSABLE flag is not defined for public symbols. */
1143 TREE_ADDRESSABLE (decl) = 1;
1145 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1147 DECL_COMDAT (decl) = 0;
1148 DECL_WEAK (decl) = 0;
1149 DECL_EXTERNAL (decl) = 0;
1150 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1151 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1152 TREE_PUBLIC (decl) = 0;
1153 DECL_DLLIMPORT_P (decl) = 0;
1154 if (!DECL_RTL_SET_P (decl))
1155 return;
1157 /* Update rtl flags. */
1158 make_decl_rtl (decl);
1160 rtl = DECL_RTL (decl);
1161 if (!MEM_P (rtl))
1162 return;
1164 symbol = XEXP (rtl, 0);
1165 if (GET_CODE (symbol) != SYMBOL_REF)
1166 return;
1168 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1171 /* Walk the alias chain to return the symbol NODE is alias of.
1172 If NODE is not an alias, return NODE.
1173 Assumes NODE is known to be alias. */
1175 symtab_node *
1176 symtab_node::ultimate_alias_target_1 (enum availability *availability)
1178 bool weakref_p = false;
1180 /* To determine visibility of the target, we follow ELF semantic of aliases.
1181 Here alias is an alternative assembler name of a given definition. Its
1182 availability prevails the availability of its target (i.e. static alias of
1183 weak definition is available.
1185 Weakref is a different animal (and not part of ELF per se). It is just
1186 alternative name of a given symbol used within one complation unit
1187 and is translated prior hitting the object file. It inherits the
1188 visibility of its target (i.e. weakref of non-overwritable definition
1189 is non-overwritable, while weakref of weak definition is weak).
1191 If we ever get into supporting targets with different semantics, a target
1192 hook will be needed here. */
1194 if (availability)
1196 weakref_p = weakref;
1197 if (!weakref_p)
1198 *availability = get_availability ();
1199 else
1200 *availability = AVAIL_LOCAL;
1203 symtab_node *node = this;
1204 while (node)
1206 if (node->alias && node->analyzed)
1207 node = node->get_alias_target ();
1208 else
1210 if (!availability)
1212 else if (node->analyzed)
1214 if (weakref_p)
1216 enum availability a = node->get_availability ();
1217 if (a < *availability)
1218 *availability = a;
1221 else
1222 *availability = AVAIL_NOT_AVAILABLE;
1223 return node;
1225 if (node && availability && weakref_p)
1227 enum availability a = node->get_availability ();
1228 if (a < *availability)
1229 *availability = a;
1230 weakref_p = node->weakref;
1233 if (availability)
1234 *availability = AVAIL_NOT_AVAILABLE;
1235 return NULL;
1238 /* C++ FE sometimes change linkage flags after producing same body aliases.
1240 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1241 are obviously equivalent. The way it is doing so is however somewhat
1242 kludgy and interferes with the visibility code. As a result we need to
1243 copy the visibility from the target to get things right. */
1245 void
1246 symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target)
1248 if (is_a <cgraph_node *> (this))
1250 DECL_DECLARED_INLINE_P (decl)
1251 = DECL_DECLARED_INLINE_P (target->decl);
1252 DECL_DISREGARD_INLINE_LIMITS (decl)
1253 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1255 /* FIXME: It is not really clear why those flags should not be copied for
1256 functions, too. */
1257 else
1259 DECL_WEAK (decl) = DECL_WEAK (target->decl);
1260 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1261 DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl);
1263 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (target->decl);
1264 if (TREE_PUBLIC (decl))
1266 tree group;
1268 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1269 DECL_COMDAT (decl) = DECL_COMDAT (target->decl);
1270 group = target->get_comdat_group ();
1271 set_comdat_group (group);
1272 if (group && !same_comdat_group)
1273 add_to_same_comdat_group (target);
1275 externally_visible = target->externally_visible;
1278 /* Set section, do not recurse into aliases.
1279 When one wants to change section of symbol and its aliases,
1280 use set_section. */
1282 void
1283 symtab_node::set_section_for_node (const char *section)
1285 const char *current = get_section ();
1286 section_hash_entry **slot;
1288 if (current == section
1289 || (current && section
1290 && !strcmp (current, section)))
1291 return;
1293 if (current)
1295 x_section->ref_count--;
1296 if (!x_section->ref_count)
1298 hashval_t hash = htab_hash_string (x_section->name);
1299 slot = symtab->section_hash->find_slot_with_hash (x_section->name,
1300 hash, INSERT);
1301 ggc_free (x_section);
1302 symtab->section_hash->clear_slot (slot);
1304 x_section = NULL;
1306 if (!section)
1308 implicit_section = false;
1309 return;
1311 if (!symtab->section_hash)
1312 symtab->section_hash = hash_table<section_name_hasher>::create_ggc (10);
1313 slot = symtab->section_hash->find_slot_with_hash (section,
1314 htab_hash_string (section),
1315 INSERT);
1316 if (*slot)
1317 x_section = (section_hash_entry *)*slot;
1318 else
1320 int len = strlen (section);
1321 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1322 x_section->name = ggc_vec_alloc<char> (len + 1);
1323 memcpy (x_section->name, section, len + 1);
1325 x_section->ref_count++;
1328 /* Worker for set_section. */
1330 bool
1331 symtab_node::set_section (symtab_node *n, void *s)
1333 n->set_section_for_node ((char *)s);
1334 return false;
1337 /* Set section of symbol and its aliases. */
1339 void
1340 symtab_node::set_section (const char *section)
1342 gcc_assert (!this->alias);
1343 call_for_symbol_and_aliases
1344 (symtab_node::set_section, const_cast<char *>(section), true);
1347 /* Return the initialization priority. */
1349 priority_type
1350 symtab_node::get_init_priority ()
1352 if (!this->in_init_priority_hash)
1353 return DEFAULT_INIT_PRIORITY;
1355 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1356 return h ? h->init : DEFAULT_INIT_PRIORITY;
1359 /* Return the finalization priority. */
1361 priority_type
1362 cgraph_node::get_fini_priority ()
1364 if (!this->in_init_priority_hash)
1365 return DEFAULT_INIT_PRIORITY;
1366 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1367 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1370 /* Return the initialization and finalization priority information for
1371 DECL. If there is no previous priority information, a freshly
1372 allocated structure is returned. */
1374 symbol_priority_map *
1375 symtab_node::priority_info (void)
1377 if (!symtab->init_priority_hash)
1378 symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13);
1380 bool existed;
1381 symbol_priority_map *h
1382 = &symtab->init_priority_hash->get_or_insert (this, &existed);
1383 if (!existed)
1385 h->init = DEFAULT_INIT_PRIORITY;
1386 h->fini = DEFAULT_INIT_PRIORITY;
1387 in_init_priority_hash = true;
1390 return h;
1393 /* Set initialization priority to PRIORITY. */
1395 void
1396 symtab_node::set_init_priority (priority_type priority)
1398 symbol_priority_map *h;
1400 if (is_a <cgraph_node *> (this))
1401 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1403 if (priority == DEFAULT_INIT_PRIORITY)
1405 gcc_assert (get_init_priority() == priority);
1406 return;
1408 h = priority_info ();
1409 h->init = priority;
1412 /* Set fialization priority to PRIORITY. */
1414 void
1415 cgraph_node::set_fini_priority (priority_type priority)
1417 symbol_priority_map *h;
1419 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1421 if (priority == DEFAULT_INIT_PRIORITY)
1423 gcc_assert (get_fini_priority() == priority);
1424 return;
1426 h = priority_info ();
1427 h->fini = priority;
1430 /* Worker for symtab_resolve_alias. */
1432 bool
1433 symtab_node::set_implicit_section (symtab_node *n,
1434 void *data ATTRIBUTE_UNUSED)
1436 n->implicit_section = true;
1437 return false;
1440 /* Add reference recording that symtab node is alias of TARGET.
1441 The function can fail in the case of aliasing cycles; in this case
1442 it returns false. */
1444 bool
1445 symtab_node::resolve_alias (symtab_node *target)
1447 symtab_node *n;
1449 gcc_assert (!analyzed && !vec_safe_length (ref_list.references));
1451 /* Never let cycles to creep into the symbol table alias references;
1452 those will make alias walkers to be infinite. */
1453 for (n = target; n && n->alias;
1454 n = n->analyzed ? n->get_alias_target () : NULL)
1455 if (n == this)
1457 if (is_a <cgraph_node *> (this))
1458 error ("function %q+D part of alias cycle", decl);
1459 else if (is_a <varpool_node *> (this))
1460 error ("variable %q+D part of alias cycle", decl);
1461 else
1462 gcc_unreachable ();
1463 alias = false;
1464 return false;
1467 /* "analyze" the node - i.e. mark the reference. */
1468 definition = true;
1469 alias = true;
1470 analyzed = true;
1471 create_reference (target, IPA_REF_ALIAS, NULL);
1473 /* Add alias into the comdat group of its target unless it is already there. */
1474 if (same_comdat_group)
1475 remove_from_same_comdat_group ();
1476 set_comdat_group (NULL);
1477 if (target->get_comdat_group ())
1478 add_to_same_comdat_group (target);
1480 if ((get_section () != target->get_section ()
1481 || target->get_comdat_group ()) && get_section () && !implicit_section)
1483 error ("section of alias %q+D must match section of its target", decl);
1485 call_for_symbol_and_aliases (symtab_node::set_section,
1486 const_cast<char *>(target->get_section ()), true);
1487 if (target->implicit_section)
1488 call_for_symbol_and_aliases (set_implicit_section, NULL, true);
1490 /* Alias targets become redundant after alias is resolved into an reference.
1491 We do not want to keep it around or we would have to mind updating them
1492 when renaming symbols. */
1493 alias_target = NULL;
1495 if (cpp_implicit_alias && symtab->state >= CONSTRUCTION)
1496 fixup_same_cpp_alias_visibility (target);
1498 /* If alias has address taken, so does the target. */
1499 if (address_taken)
1500 target->ultimate_alias_target ()->address_taken = true;
1502 /* All non-weakref aliases of THIS are now in fact aliases of TARGET. */
1503 ipa_ref *ref;
1504 for (unsigned i = 0; iterate_direct_aliases (i, ref);)
1506 struct symtab_node *alias_alias = ref->referring;
1507 if (!alias_alias->weakref)
1509 alias_alias->remove_all_references ();
1510 alias_alias->create_reference (target, IPA_REF_ALIAS, NULL);
1512 else i++;
1514 return true;
1517 /* Worker searching noninterposable alias. */
1519 bool
1520 symtab_node::noninterposable_alias (symtab_node *node, void *data)
1522 if (decl_binds_to_current_def_p (node->decl))
1524 symtab_node *fn = node->ultimate_alias_target ();
1526 /* Ensure that the alias is well formed this may not be the case
1527 of user defined aliases and currently it is not always the case
1528 of C++ same body aliases (that is a bug). */
1529 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
1530 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
1531 || (TREE_CODE (node->decl) == FUNCTION_DECL
1532 && flags_from_decl_or_type (node->decl)
1533 != flags_from_decl_or_type (fn->decl))
1534 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
1535 return false;
1536 *(symtab_node **)data = node;
1537 return true;
1539 return false;
1542 /* If node can not be overwriten by static or dynamic linker to point to
1543 different definition, return NODE. Otherwise look for alias with such
1544 property and if none exists, introduce new one. */
1546 symtab_node *
1547 symtab_node::noninterposable_alias (void)
1549 tree new_decl;
1550 symtab_node *new_node = NULL;
1552 /* First try to look up existing alias or base object
1553 (if that is already non-overwritable). */
1554 symtab_node *node = ultimate_alias_target ();
1555 gcc_assert (!node->alias && !node->weakref);
1556 node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias,
1557 (void *)&new_node, true);
1558 if (new_node)
1559 return new_node;
1560 #ifndef ASM_OUTPUT_DEF
1561 /* If aliases aren't supported by the assembler, fail. */
1562 return NULL;
1563 #endif
1565 /* Otherwise create a new one. */
1566 new_decl = copy_node (node->decl);
1567 DECL_DLLIMPORT_P (new_decl) = 0;
1568 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1569 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1570 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1571 DECL_INITIAL (new_decl) = NULL;
1572 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1573 SET_DECL_RTL (new_decl, NULL);
1575 /* Update the properties. */
1576 DECL_EXTERNAL (new_decl) = 0;
1577 TREE_PUBLIC (new_decl) = 0;
1578 DECL_COMDAT (new_decl) = 0;
1579 DECL_WEAK (new_decl) = 0;
1581 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1582 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1583 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1585 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1586 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1587 new_node = cgraph_node::create_alias (new_decl, node->decl);
1589 else
1591 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1592 DECL_INITIAL (new_decl) = error_mark_node;
1593 new_node = varpool_node::create_alias (new_decl, node->decl);
1595 new_node->resolve_alias (node);
1596 gcc_assert (decl_binds_to_current_def_p (new_decl)
1597 && targetm.binds_local_p (new_decl));
1598 return new_node;
1601 /* Return true if symtab node and TARGET represents
1602 semantically equivalent symbols. */
1604 bool
1605 symtab_node::semantically_equivalent_p (symtab_node *target)
1607 enum availability avail;
1608 symtab_node *ba;
1609 symtab_node *bb;
1611 /* Equivalent functions are equivalent. */
1612 if (decl == target->decl)
1613 return true;
1615 /* If symbol is not overwritable by different implementation,
1616 walk to the base object it defines. */
1617 ba = ultimate_alias_target (&avail);
1618 if (avail >= AVAIL_AVAILABLE)
1620 if (target == ba)
1621 return true;
1623 else
1624 ba = this;
1625 bb = target->ultimate_alias_target (&avail);
1626 if (avail >= AVAIL_AVAILABLE)
1628 if (this == bb)
1629 return true;
1631 else
1632 bb = target;
1633 return bb == ba;
1636 /* Classify symbol symtab node for partitioning. */
1638 enum symbol_partitioning_class
1639 symtab_node::get_partitioning_class (void)
1641 /* Inline clones are always duplicated.
1642 This include external delcarations. */
1643 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
1645 if (DECL_ABSTRACT_P (decl))
1646 return SYMBOL_EXTERNAL;
1648 if (cnode && cnode->global.inlined_to)
1649 return SYMBOL_DUPLICATE;
1651 /* Weakref aliases are always duplicated. */
1652 if (weakref)
1653 return SYMBOL_DUPLICATE;
1655 /* External declarations are external. */
1656 if (DECL_EXTERNAL (decl))
1657 return SYMBOL_EXTERNAL;
1659 if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
1661 if (alias && definition && !ultimate_alias_target ()->definition)
1662 return SYMBOL_EXTERNAL;
1663 /* Constant pool references use local symbol names that can not
1664 be promoted global. We should never put into a constant pool
1665 objects that can not be duplicated across partitions. */
1666 if (DECL_IN_CONSTANT_POOL (decl))
1667 return SYMBOL_DUPLICATE;
1668 if (DECL_HARD_REGISTER (decl))
1669 return SYMBOL_DUPLICATE;
1670 gcc_checking_assert (vnode->definition);
1672 /* Functions that are cloned may stay in callgraph even if they are unused.
1673 Handle them as external; compute_ltrans_boundary take care to make
1674 proper things to happen (i.e. to make them appear in the boundary but
1675 with body streamed, so clone can me materialized). */
1676 else if (!dyn_cast <cgraph_node *> (this)->function_symbol ()->definition)
1677 return SYMBOL_EXTERNAL;
1679 /* Linker discardable symbols are duplicated to every use unless they are
1680 keyed. */
1681 if (DECL_ONE_ONLY (decl)
1682 && !force_output
1683 && !forced_by_abi
1684 && !used_from_object_file_p ())
1685 return SYMBOL_DUPLICATE;
1687 return SYMBOL_PARTITION;
1690 /* Return true when symbol is known to be non-zero. */
1692 bool
1693 symtab_node::nonzero_address ()
1695 /* Weakrefs may be NULL when their target is not defined. */
1696 if (alias && weakref)
1698 if (analyzed)
1700 symtab_node *target = ultimate_alias_target ();
1702 if (target->alias && target->weakref)
1703 return false;
1704 /* We can not recurse to target::nonzero. It is possible that the
1705 target is used only via the alias.
1706 We may walk references and look for strong use, but we do not know
1707 if this strong use will survive to final binary, so be
1708 conservative here.
1709 ??? Maybe we could do the lookup during late optimization that
1710 could be useful to eliminate the NULL pointer checks in LTO
1711 programs. */
1712 if (target->definition && !DECL_EXTERNAL (target->decl))
1713 return true;
1714 if (target->resolution != LDPR_UNKNOWN
1715 && target->resolution != LDPR_UNDEF
1716 && flag_delete_null_pointer_checks)
1717 return true;
1718 return false;
1720 else
1721 return false;
1724 /* With !flag_delete_null_pointer_checks we assume that symbols may
1725 bind to NULL. This is on by default on embedded targets only.
1727 Otherwise all non-WEAK symbols must be defined and thus non-NULL or
1728 linking fails. Important case of WEAK we want to do well are comdats.
1729 Those are handled by later check for definition.
1731 When parsing, beware the cases when WEAK attribute is added later. */
1732 if (!DECL_WEAK (decl)
1733 && flag_delete_null_pointer_checks)
1735 refuse_visibility_changes = true;
1736 return true;
1739 /* If target is defined and not extern, we know it will be output and thus
1740 it will bind to non-NULL.
1741 Play safe for flag_delete_null_pointer_checks where weak definition maye
1742 be re-defined by NULL. */
1743 if (definition && !DECL_EXTERNAL (decl)
1744 && (flag_delete_null_pointer_checks || !DECL_WEAK (decl)))
1746 if (!DECL_WEAK (decl))
1747 refuse_visibility_changes = true;
1748 return true;
1751 /* As the last resort, check the resolution info. */
1752 if (resolution != LDPR_UNKNOWN
1753 && resolution != LDPR_UNDEF
1754 && flag_delete_null_pointer_checks)
1755 return true;
1756 return false;
1759 /* Return 0 if symbol is known to have different address than S2,
1760 Return 1 if symbol is known to have same address as S2,
1761 return 2 otherwise. */
1763 symtab_node::equal_address_to (symtab_node *s2)
1765 enum availability avail1, avail2;
1767 /* A Shortcut: equivalent symbols are always equivalent. */
1768 if (this == s2)
1769 return 1;
1771 /* For non-interposable aliases, lookup and compare their actual definitions.
1772 Also check if the symbol needs to bind to given definition. */
1773 symtab_node *rs1 = ultimate_alias_target (&avail1);
1774 symtab_node *rs2 = s2->ultimate_alias_target (&avail2);
1775 bool binds_local1 = rs1->analyzed && decl_binds_to_current_def_p (this->decl);
1776 bool binds_local2 = rs2->analyzed && decl_binds_to_current_def_p (s2->decl);
1777 bool really_binds_local1 = binds_local1;
1778 bool really_binds_local2 = binds_local2;
1780 /* Addresses of vtables and virtual functions can not be used by user
1781 code and are used only within speculation. In this case we may make
1782 symbol equivalent to its alias even if interposition may break this
1783 rule. Doing so will allow us to turn speculative inlining into
1784 non-speculative more agressively. */
1785 if (DECL_VIRTUAL_P (this->decl) && avail1 >= AVAIL_AVAILABLE)
1786 binds_local1 = true;
1787 if (DECL_VIRTUAL_P (s2->decl) && avail2 >= AVAIL_AVAILABLE)
1788 binds_local2 = true;
1790 /* If both definitions are available we know that even if they are bound
1791 to other unit they must be defined same way and therefore we can use
1792 equivalence test. */
1793 if (rs1 != rs2 && avail1 >= AVAIL_AVAILABLE && avail2 >= AVAIL_AVAILABLE)
1794 binds_local1 = binds_local2 = true;
1796 if ((binds_local1 ? rs1 : this)
1797 == (binds_local2 ? rs2 : s2))
1799 /* We made use of the fact that alias is not weak. */
1800 if (binds_local1 && rs1 != this)
1801 refuse_visibility_changes = true;
1802 if (binds_local2 && rs2 != s2)
1803 s2->refuse_visibility_changes = true;
1804 return 1;
1807 /* If both symbols may resolve to NULL, we can not really prove them different. */
1808 if (!nonzero_address () && !s2->nonzero_address ())
1809 return 2;
1811 /* Except for NULL, functions and variables never overlap. */
1812 if (TREE_CODE (decl) != TREE_CODE (s2->decl))
1813 return 0;
1815 /* If one of the symbols is unresolved alias, punt. */
1816 if (rs1->alias || rs2->alias)
1817 return 2;
1819 /* If we have a non-interposale definition of at least one of the symbols
1820 and the other symbol is different, we know other unit can not interpose
1821 it to the first symbol; all aliases of the definition needs to be
1822 present in the current unit. */
1823 if (((really_binds_local1 || really_binds_local2)
1824 /* If we have both definitions and they are different, we know they
1825 will be different even in units they binds to. */
1826 || (binds_local1 && binds_local2))
1827 && rs1 != rs2)
1829 /* We make use of the fact that one symbol is not alias of the other
1830 and that the definition is non-interposable. */
1831 refuse_visibility_changes = true;
1832 s2->refuse_visibility_changes = true;
1833 rs1->refuse_visibility_changes = true;
1834 rs2->refuse_visibility_changes = true;
1835 return 0;
1838 /* TODO: Alias oracle basically assume that addresses of global variables
1839 are different unless they are declared as alias of one to another.
1840 We probably should be consistent and use this fact here, too, and update
1841 alias oracle to use this predicate. */
1843 return 2;
1846 /* Worker for call_for_symbol_and_aliases. */
1848 bool
1849 symtab_node::call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *,
1850 void *),
1851 void *data,
1852 bool include_overwritable)
1854 ipa_ref *ref;
1855 FOR_EACH_ALIAS (this, ref)
1857 symtab_node *alias = ref->referring;
1858 if (include_overwritable
1859 || alias->get_availability () > AVAIL_INTERPOSABLE)
1860 if (alias->call_for_symbol_and_aliases (callback, data,
1861 include_overwritable))
1862 return true;
1864 return false;
1867 /* Return true if address of N is possibly compared. */
1869 static bool
1870 address_matters_1 (symtab_node *n, void *)
1872 struct ipa_ref *ref;
1874 if (!n->address_can_be_compared_p ())
1875 return false;
1876 if (n->externally_visible || n->force_output)
1877 return true;
1879 for (unsigned int i = 0; n->iterate_referring (i, ref); i++)
1880 if (ref->address_matters_p ())
1881 return true;
1882 return false;
1885 /* Return true if symbol's address may possibly be compared to other
1886 symbol's address. */
1888 bool
1889 symtab_node::address_matters_p ()
1891 gcc_assert (!alias);
1892 return call_for_symbol_and_aliases (address_matters_1, NULL, true);
1895 /* Return true if symbol's alignment may be increased. */
1897 bool
1898 symtab_node::can_increase_alignment_p (void)
1900 symtab_node *target = ultimate_alias_target ();
1902 /* For now support only variables. */
1903 if (TREE_CODE (decl) != VAR_DECL)
1904 return false;
1906 /* With -fno-toplevel-reorder we may have already output the constant. */
1907 if (TREE_ASM_WRITTEN (target->decl))
1908 return false;
1910 /* If target is already placed in an anchor, we can not touch its
1911 alignment. */
1912 if (DECL_RTL_SET_P (target->decl)
1913 && MEM_P (DECL_RTL (target->decl))
1914 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (DECL_RTL (target->decl), 0)))
1915 return false;
1917 /* Constant pool entries may be shared. */
1918 if (DECL_IN_CONSTANT_POOL (target->decl))
1919 return false;
1921 /* We cannot change alignment of symbols that may bind to symbols
1922 in other translation unit that may contain a definition with lower
1923 alignment. */
1924 if (!decl_binds_to_current_def_p (decl))
1925 return false;
1927 /* When compiling partition, be sure the symbol is not output by other
1928 partition. */
1929 if (flag_ltrans
1930 && (target->in_other_partition
1931 || target->get_partitioning_class () == SYMBOL_DUPLICATE))
1932 return false;
1934 /* Do not override the alignment as specified by the ABI when the used
1935 attribute is set. */
1936 if (DECL_PRESERVE_P (decl) || DECL_PRESERVE_P (target->decl))
1937 return false;
1939 /* Do not override explicit alignment set by the user when an explicit
1940 section name is also used. This is a common idiom used by many
1941 software projects. */
1942 if (DECL_SECTION_NAME (target->decl) != NULL && !target->implicit_section)
1943 return false;
1945 return true;
1948 /* Worker for symtab_node::increase_alignment. */
1950 static bool
1951 increase_alignment_1 (symtab_node *n, void *v)
1953 unsigned int align = (size_t)v;
1954 if (DECL_ALIGN (n->decl) < align
1955 && n->can_increase_alignment_p ())
1957 DECL_ALIGN (n->decl) = align;
1958 DECL_USER_ALIGN (n->decl) = 1;
1960 return false;
1963 /* Increase alignment of THIS to ALIGN. */
1965 void
1966 symtab_node::increase_alignment (unsigned int align)
1968 gcc_assert (can_increase_alignment_p () && align < MAX_OFILE_ALIGNMENT);
1969 ultimate_alias_target()->call_for_symbol_and_aliases (increase_alignment_1,
1970 (void *)(size_t) align,
1971 true);
1972 gcc_assert (DECL_ALIGN (decl) >= align);
1975 /* Helper for symtab_node::definition_alignment. */
1977 static bool
1978 get_alignment_1 (symtab_node *n, void *v)
1980 *((unsigned int *)v) = MAX (*((unsigned int *)v), DECL_ALIGN (n->decl));
1981 return false;
1984 /* Return desired alignment of the definition. This is NOT alignment useful
1985 to access THIS, because THIS may be interposable and DECL_ALIGN should
1986 be used instead. It however must be guaranteed when output definition
1987 of THIS. */
1989 unsigned int
1990 symtab_node::definition_alignment ()
1992 unsigned int align = 0;
1993 gcc_assert (!alias);
1994 call_for_symbol_and_aliases (get_alignment_1, &align, true);
1995 return align;