Daily bump.
[official-gcc.git] / gcc / symtab.c
blobaa5162e5b3fe429a1869ae8ac6afbf0408f31975
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 "tm.h"
25 #include "rtl.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "print-tree.h"
31 #include "varasm.h"
32 #include "hard-reg-set.h"
33 #include "function.h"
34 #include "emit-rtl.h"
35 #include "predict.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-expr.h"
40 #include "gimple.h"
41 #include "tree-inline.h"
42 #include "langhooks.h"
43 #include "cgraph.h"
44 #include "diagnostic.h"
45 #include "timevar.h"
46 #include "lto-streamer.h"
47 #include "output.h"
48 #include "ipa-utils.h"
49 #include "calls.h"
51 static const char *ipa_ref_use_name[] = {"read","write","addr","alias","chkp"};
53 const char * const ld_plugin_symbol_resolution_names[]=
55 "",
56 "undef",
57 "prevailing_def",
58 "prevailing_def_ironly",
59 "preempted_reg",
60 "preempted_ir",
61 "resolved_ir",
62 "resolved_exec",
63 "resolved_dyn",
64 "prevailing_def_ironly_exp"
67 /* Hash asmnames ignoring the user specified marks. */
69 hashval_t
70 symbol_table::decl_assembler_name_hash (const_tree asmname)
72 if (IDENTIFIER_POINTER (asmname)[0] == '*')
74 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
75 size_t ulp_len = strlen (user_label_prefix);
77 if (ulp_len == 0)
79 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
80 decl_str += ulp_len;
82 return htab_hash_string (decl_str);
85 return htab_hash_string (IDENTIFIER_POINTER (asmname));
89 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
91 bool
92 symbol_table::decl_assembler_name_equal (tree decl, const_tree asmname)
94 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
95 const char *decl_str;
96 const char *asmname_str;
97 bool test = false;
99 if (decl_asmname == asmname)
100 return true;
102 decl_str = IDENTIFIER_POINTER (decl_asmname);
103 asmname_str = IDENTIFIER_POINTER (asmname);
106 /* If the target assembler name was set by the user, things are trickier.
107 We have a leading '*' to begin with. After that, it's arguable what
108 is the correct thing to do with -fleading-underscore. Arguably, we've
109 historically been doing the wrong thing in assemble_alias by always
110 printing the leading underscore. Since we're not changing that, make
111 sure user_label_prefix follows the '*' before matching. */
112 if (decl_str[0] == '*')
114 size_t ulp_len = strlen (user_label_prefix);
116 decl_str ++;
118 if (ulp_len == 0)
119 test = true;
120 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
121 decl_str += ulp_len, test=true;
122 else
123 decl_str --;
125 if (asmname_str[0] == '*')
127 size_t ulp_len = strlen (user_label_prefix);
129 asmname_str ++;
131 if (ulp_len == 0)
132 test = true;
133 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
134 asmname_str += ulp_len, test=true;
135 else
136 asmname_str --;
139 if (!test)
140 return false;
141 return strcmp (decl_str, asmname_str) == 0;
145 /* Returns nonzero if P1 and P2 are equal. */
147 /* Insert NODE to assembler name hash. */
149 void
150 symbol_table::insert_to_assembler_name_hash (symtab_node *node,
151 bool with_clones)
153 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
154 return;
155 gcc_checking_assert (!node->previous_sharing_asm_name
156 && !node->next_sharing_asm_name);
157 if (assembler_name_hash)
159 symtab_node **aslot;
160 cgraph_node *cnode;
161 tree decl = node->decl;
163 tree name = DECL_ASSEMBLER_NAME (node->decl);
165 /* C++ FE can produce decls without associated assembler name and insert
166 them to symtab to hold section or TLS information. */
167 if (!name)
168 return;
170 hashval_t hash = decl_assembler_name_hash (name);
171 aslot = assembler_name_hash->find_slot_with_hash (name, hash, INSERT);
172 gcc_assert (*aslot != node);
173 node->next_sharing_asm_name = (symtab_node *)*aslot;
174 if (*aslot != NULL)
175 (*aslot)->previous_sharing_asm_name = node;
176 *aslot = node;
178 /* Update also possible inline clones sharing a decl. */
179 cnode = dyn_cast <cgraph_node *> (node);
180 if (cnode && cnode->clones && with_clones)
181 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
182 if (cnode->decl == decl)
183 insert_to_assembler_name_hash (cnode, true);
188 /* Remove NODE from assembler name hash. */
190 void
191 symbol_table::unlink_from_assembler_name_hash (symtab_node *node,
192 bool with_clones)
194 if (assembler_name_hash)
196 cgraph_node *cnode;
197 tree decl = node->decl;
199 if (node->next_sharing_asm_name)
200 node->next_sharing_asm_name->previous_sharing_asm_name
201 = node->previous_sharing_asm_name;
202 if (node->previous_sharing_asm_name)
204 node->previous_sharing_asm_name->next_sharing_asm_name
205 = node->next_sharing_asm_name;
207 else
209 tree name = DECL_ASSEMBLER_NAME (node->decl);
210 symtab_node **slot;
212 if (!name)
213 return;
215 hashval_t hash = decl_assembler_name_hash (name);
216 slot = assembler_name_hash->find_slot_with_hash (name, hash,
217 NO_INSERT);
218 gcc_assert (*slot == node);
219 if (!node->next_sharing_asm_name)
220 assembler_name_hash->clear_slot (slot);
221 else
222 *slot = node->next_sharing_asm_name;
224 node->next_sharing_asm_name = NULL;
225 node->previous_sharing_asm_name = NULL;
227 /* Update also possible inline clones sharing a decl. */
228 cnode = dyn_cast <cgraph_node *> (node);
229 if (cnode && cnode->clones && with_clones)
230 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
231 if (cnode->decl == decl)
232 unlink_from_assembler_name_hash (cnode, true);
236 /* Arrange node to be first in its entry of assembler_name_hash. */
238 void
239 symbol_table::symtab_prevail_in_asm_name_hash (symtab_node *node)
241 unlink_from_assembler_name_hash (node, false);
242 insert_to_assembler_name_hash (node, false);
245 /* Initalize asm name hash unless. */
247 void
248 symbol_table::symtab_initialize_asm_name_hash (void)
250 symtab_node *node;
251 if (!assembler_name_hash)
253 assembler_name_hash = hash_table<asmname_hasher>::create_ggc (10);
254 FOR_EACH_SYMBOL (node)
255 insert_to_assembler_name_hash (node, false);
259 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
261 void
262 symbol_table::change_decl_assembler_name (tree decl, tree name)
264 symtab_node *node = NULL;
266 /* We can have user ASM names on things, like global register variables, that
267 are not in the symbol table. */
268 if ((TREE_CODE (decl) == VAR_DECL
269 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
270 || TREE_CODE (decl) == FUNCTION_DECL)
271 node = symtab_node::get (decl);
272 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
274 SET_DECL_ASSEMBLER_NAME (decl, name);
275 if (node)
276 insert_to_assembler_name_hash (node, true);
278 else
280 if (name == DECL_ASSEMBLER_NAME (decl))
281 return;
283 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
284 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
285 : NULL);
286 if (node)
287 unlink_from_assembler_name_hash (node, true);
288 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
289 && DECL_RTL_SET_P (decl))
290 warning (0, "%D renamed after being referenced in assembly", decl);
292 SET_DECL_ASSEMBLER_NAME (decl, name);
293 if (alias)
295 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
296 TREE_CHAIN (name) = alias;
298 if (node)
299 insert_to_assembler_name_hash (node, true);
303 /* Hash sections by their names. */
305 hashval_t
306 section_name_hasher::hash (section_hash_entry *n)
308 return htab_hash_string (n->name);
311 /* Return true if section P1 name equals to P2. */
313 bool
314 section_name_hasher::equal (section_hash_entry *n1, const char *name)
316 return n1->name == name || !strcmp (n1->name, name);
319 /* Add node into symbol table. This function is not used directly, but via
320 cgraph/varpool node creation routines. */
322 void
323 symtab_node::register_symbol (void)
325 symtab->register_symbol (this);
327 if (!decl->decl_with_vis.symtab_node)
328 decl->decl_with_vis.symtab_node = this;
330 ref_list.clear ();
332 /* Be sure to do this last; C++ FE might create new nodes via
333 DECL_ASSEMBLER_NAME langhook! */
334 symtab->insert_to_assembler_name_hash (this, false);
337 /* Remove NODE from same comdat group. */
339 void
340 symtab_node::remove_from_same_comdat_group (void)
342 if (same_comdat_group)
344 symtab_node *prev;
345 for (prev = same_comdat_group;
346 prev->same_comdat_group != this;
347 prev = prev->same_comdat_group)
349 if (same_comdat_group == prev)
350 prev->same_comdat_group = NULL;
351 else
352 prev->same_comdat_group = same_comdat_group;
353 same_comdat_group = NULL;
354 set_comdat_group (NULL);
358 /* Remove node from symbol table. This function is not used directly, but via
359 cgraph/varpool node removal routines. */
361 void
362 symtab_node::unregister (void)
364 remove_all_references ();
365 remove_all_referring ();
367 /* Remove reference to section. */
368 set_section_for_node (NULL);
370 remove_from_same_comdat_group ();
372 symtab->unregister (this);
374 /* During LTO symtab merging we temporarily corrupt decl to symtab node
375 hash. */
376 gcc_assert (decl->decl_with_vis.symtab_node || in_lto_p);
377 if (decl->decl_with_vis.symtab_node == this)
379 symtab_node *replacement_node = NULL;
380 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
381 replacement_node = cnode->find_replacement ();
382 decl->decl_with_vis.symtab_node = replacement_node;
384 if (!is_a <varpool_node *> (this) || !DECL_HARD_REGISTER (decl))
385 symtab->unlink_from_assembler_name_hash (this, false);
386 if (in_init_priority_hash)
387 symtab->init_priority_hash->remove (this);
391 /* Remove symbol from symbol table. */
393 void
394 symtab_node::remove (void)
396 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
397 cnode->remove ();
398 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
399 vnode->remove ();
402 /* Add NEW_ to the same comdat group that OLD is in. */
404 void
405 symtab_node::add_to_same_comdat_group (symtab_node *old_node)
407 gcc_assert (old_node->get_comdat_group ());
408 gcc_assert (!same_comdat_group);
409 gcc_assert (this != old_node);
411 set_comdat_group (old_node->get_comdat_group ());
412 same_comdat_group = old_node;
413 if (!old_node->same_comdat_group)
414 old_node->same_comdat_group = this;
415 else
417 symtab_node *n;
418 for (n = old_node->same_comdat_group;
419 n->same_comdat_group != old_node;
420 n = n->same_comdat_group)
422 n->same_comdat_group = this;
426 /* Dissolve the same_comdat_group list in which NODE resides. */
428 void
429 symtab_node::dissolve_same_comdat_group_list (void)
431 symtab_node *n = this;
432 symtab_node *next;
434 if (!same_comdat_group)
435 return;
438 next = n->same_comdat_group;
439 n->same_comdat_group = NULL;
440 /* Clear comdat_group for comdat locals, since
441 make_decl_local doesn't. */
442 if (!TREE_PUBLIC (n->decl))
443 n->set_comdat_group (NULL);
444 n = next;
446 while (n != this);
449 /* Return printable assembler name of NODE.
450 This function is used only for debugging. When assembler name
451 is unknown go with identifier name. */
453 const char *
454 symtab_node::asm_name () const
456 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
457 return lang_hooks.decl_printable_name (decl, 2);
458 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
461 /* Return printable identifier name. */
463 const char *
464 symtab_node::name () const
466 return lang_hooks.decl_printable_name (decl, 2);
469 /* Return ipa reference from this symtab_node to
470 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
471 of the use. */
473 ipa_ref *
474 symtab_node::create_reference (symtab_node *referred_node,
475 enum ipa_ref_use use_type)
477 return create_reference (referred_node, use_type, NULL);
481 /* Return ipa reference from this symtab_node to
482 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
483 of the use and STMT the statement (if it exists). */
485 ipa_ref *
486 symtab_node::create_reference (symtab_node *referred_node,
487 enum ipa_ref_use use_type, gimple stmt)
489 ipa_ref *ref = NULL, *ref2 = NULL;
490 ipa_ref_list *list, *list2;
491 ipa_ref_t *old_references;
493 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
494 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
496 list = &ref_list;
497 old_references = vec_safe_address (list->references);
498 vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
499 ref = &list->references->last ();
501 list2 = &referred_node->ref_list;
503 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
504 if(use_type == IPA_REF_ALIAS)
506 list2->referring.safe_insert (0, ref);
507 ref->referred_index = 0;
509 for (unsigned int i = 1; i < list2->referring.length (); i++)
510 list2->referring[i]->referred_index = i;
512 else
514 list2->referring.safe_push (ref);
515 ref->referred_index = list2->referring.length () - 1;
518 ref->referring = this;
519 ref->referred = referred_node;
520 ref->stmt = stmt;
521 ref->lto_stmt_uid = 0;
522 ref->use = use_type;
523 ref->speculative = 0;
525 /* If vector was moved in memory, update pointers. */
526 if (old_references != list->references->address ())
528 int i;
529 for (i = 0; iterate_reference(i, ref2); i++)
530 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
532 return ref;
535 /* If VAL is a reference to a function or a variable, add a reference from
536 this symtab_node to the corresponding symbol table node. USE_TYPE specify
537 type of the use and STMT the statement (if it exists). Return the new
538 reference or NULL if none was created. */
540 ipa_ref *
541 symtab_node::maybe_create_reference (tree val, enum ipa_ref_use use_type,
542 gimple stmt)
544 STRIP_NOPS (val);
545 if (TREE_CODE (val) != ADDR_EXPR)
546 return NULL;
547 val = get_base_var (val);
548 if (val && (TREE_CODE (val) == FUNCTION_DECL
549 || TREE_CODE (val) == VAR_DECL))
551 symtab_node *referred = symtab_node::get (val);
552 gcc_checking_assert (referred);
553 return create_reference (referred, use_type, stmt);
555 return NULL;
558 /* Clone all references from symtab NODE to this symtab_node. */
560 void
561 symtab_node::clone_references (symtab_node *node)
563 ipa_ref *ref = NULL, *ref2 = NULL;
564 int i;
565 for (i = 0; node->iterate_reference (i, ref); i++)
567 bool speculative = ref->speculative;
568 unsigned int stmt_uid = ref->lto_stmt_uid;
570 ref2 = create_reference (ref->referred, ref->use, ref->stmt);
571 ref2->speculative = speculative;
572 ref2->lto_stmt_uid = stmt_uid;
576 /* Clone all referring from symtab NODE to this symtab_node. */
578 void
579 symtab_node::clone_referring (symtab_node *node)
581 ipa_ref *ref = NULL, *ref2 = NULL;
582 int i;
583 for (i = 0; node->iterate_referring(i, ref); i++)
585 bool speculative = ref->speculative;
586 unsigned int stmt_uid = ref->lto_stmt_uid;
588 ref2 = ref->referring->create_reference (this, ref->use, ref->stmt);
589 ref2->speculative = speculative;
590 ref2->lto_stmt_uid = stmt_uid;
594 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
596 ipa_ref *
597 symtab_node::clone_reference (ipa_ref *ref, gimple stmt)
599 bool speculative = ref->speculative;
600 unsigned int stmt_uid = ref->lto_stmt_uid;
601 ipa_ref *ref2;
603 ref2 = create_reference (ref->referred, ref->use, stmt);
604 ref2->speculative = speculative;
605 ref2->lto_stmt_uid = stmt_uid;
606 return ref2;
609 /* Find the structure describing a reference to REFERRED_NODE
610 and associated with statement STMT. */
612 ipa_ref *
613 symtab_node::find_reference (symtab_node *referred_node,
614 gimple stmt, unsigned int lto_stmt_uid)
616 ipa_ref *r = NULL;
617 int i;
619 for (i = 0; iterate_reference (i, r); i++)
620 if (r->referred == referred_node
621 && !r->speculative
622 && ((stmt && r->stmt == stmt)
623 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
624 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
625 return r;
626 return NULL;
629 /* Remove all references that are associated with statement STMT. */
631 void
632 symtab_node::remove_stmt_references (gimple stmt)
634 ipa_ref *r = NULL;
635 int i = 0;
637 while (iterate_reference (i, r))
638 if (r->stmt == stmt)
639 r->remove_reference ();
640 else
641 i++;
644 /* Remove all stmt references in non-speculative references.
645 Those are not maintained during inlining & clonning.
646 The exception are speculative references that are updated along
647 with callgraph edges associated with them. */
649 void
650 symtab_node::clear_stmts_in_references (void)
652 ipa_ref *r = NULL;
653 int i;
655 for (i = 0; iterate_reference (i, r); i++)
656 if (!r->speculative)
658 r->stmt = NULL;
659 r->lto_stmt_uid = 0;
663 /* Remove all references in ref list. */
665 void
666 symtab_node::remove_all_references (void)
668 while (vec_safe_length (ref_list.references))
669 ref_list.references->last ().remove_reference ();
670 vec_free (ref_list.references);
673 /* Remove all referring items in ref list. */
675 void
676 symtab_node::remove_all_referring (void)
678 while (ref_list.referring.length ())
679 ref_list.referring.last ()->remove_reference ();
680 ref_list.referring.release ();
683 /* Dump references in ref list to FILE. */
685 void
686 symtab_node::dump_references (FILE *file)
688 ipa_ref *ref = NULL;
689 int i;
690 for (i = 0; iterate_reference (i, ref); i++)
692 fprintf (file, "%s/%i (%s)",
693 ref->referred->asm_name (),
694 ref->referred->order,
695 ipa_ref_use_name [ref->use]);
696 if (ref->speculative)
697 fprintf (file, " (speculative)");
699 fprintf (file, "\n");
702 /* Dump referring in list to FILE. */
704 void
705 symtab_node::dump_referring (FILE *file)
707 ipa_ref *ref = NULL;
708 int i;
709 for (i = 0; iterate_referring(i, ref); i++)
711 fprintf (file, "%s/%i (%s)",
712 ref->referring->asm_name (),
713 ref->referring->order,
714 ipa_ref_use_name [ref->use]);
715 if (ref->speculative)
716 fprintf (file, " (speculative)");
718 fprintf (file, "\n");
721 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
723 /* Dump base fields of symtab nodes to F. Not to be used directly. */
725 void
726 symtab_node::dump_base (FILE *f)
728 static const char * const visibility_types[] = {
729 "default", "protected", "hidden", "internal"
732 fprintf (f, "%s/%i (%s)", asm_name (), order, name ());
733 dump_addr (f, " @", (void *)this);
734 fprintf (f, "\n Type: %s", symtab_type_names[type]);
736 if (definition)
737 fprintf (f, " definition");
738 if (analyzed)
739 fprintf (f, " analyzed");
740 if (alias)
741 fprintf (f, " alias");
742 if (weakref)
743 fprintf (f, " weakref");
744 if (cpp_implicit_alias)
745 fprintf (f, " cpp_implicit_alias");
746 if (alias_target)
747 fprintf (f, " target:%s",
748 DECL_P (alias_target)
749 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
750 (alias_target))
751 : IDENTIFIER_POINTER (alias_target));
752 if (body_removed)
753 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
754 fprintf (f, "\n Visibility:");
755 if (in_other_partition)
756 fprintf (f, " in_other_partition");
757 if (used_from_other_partition)
758 fprintf (f, " used_from_other_partition");
759 if (force_output)
760 fprintf (f, " force_output");
761 if (forced_by_abi)
762 fprintf (f, " forced_by_abi");
763 if (externally_visible)
764 fprintf (f, " externally_visible");
765 if (no_reorder)
766 fprintf (f, " no_reorder");
767 if (resolution != LDPR_UNKNOWN)
768 fprintf (f, " %s",
769 ld_plugin_symbol_resolution_names[(int)resolution]);
770 if (TREE_ASM_WRITTEN (decl))
771 fprintf (f, " asm_written");
772 if (DECL_EXTERNAL (decl))
773 fprintf (f, " external");
774 if (TREE_PUBLIC (decl))
775 fprintf (f, " public");
776 if (DECL_COMMON (decl))
777 fprintf (f, " common");
778 if (DECL_WEAK (decl))
779 fprintf (f, " weak");
780 if (DECL_DLLIMPORT_P (decl))
781 fprintf (f, " dll_import");
782 if (DECL_COMDAT (decl))
783 fprintf (f, " comdat");
784 if (get_comdat_group ())
785 fprintf (f, " comdat_group:%s",
786 IDENTIFIER_POINTER (get_comdat_group_id ()));
787 if (DECL_ONE_ONLY (decl))
788 fprintf (f, " one_only");
789 if (get_section ())
790 fprintf (f, " section:%s",
791 get_section ());
792 if (implicit_section)
793 fprintf (f," (implicit_section)");
794 if (DECL_VISIBILITY_SPECIFIED (decl))
795 fprintf (f, " visibility_specified");
796 if (DECL_VISIBILITY (decl))
797 fprintf (f, " visibility:%s",
798 visibility_types [DECL_VISIBILITY (decl)]);
799 if (DECL_VIRTUAL_P (decl))
800 fprintf (f, " virtual");
801 if (DECL_ARTIFICIAL (decl))
802 fprintf (f, " artificial");
803 if (TREE_CODE (decl) == FUNCTION_DECL)
805 if (DECL_STATIC_CONSTRUCTOR (decl))
806 fprintf (f, " constructor");
807 if (DECL_STATIC_DESTRUCTOR (decl))
808 fprintf (f, " destructor");
810 fprintf (f, "\n");
812 if (same_comdat_group)
813 fprintf (f, " Same comdat group as: %s/%i\n",
814 same_comdat_group->asm_name (),
815 same_comdat_group->order);
816 if (next_sharing_asm_name)
817 fprintf (f, " next sharing asm name: %i\n",
818 next_sharing_asm_name->order);
819 if (previous_sharing_asm_name)
820 fprintf (f, " previous sharing asm name: %i\n",
821 previous_sharing_asm_name->order);
823 if (address_taken)
824 fprintf (f, " Address is taken.\n");
825 if (aux)
827 fprintf (f, " Aux:");
828 dump_addr (f, " @", (void *)aux);
831 fprintf (f, " References: ");
832 dump_references (f);
833 fprintf (f, " Referring: ");
834 dump_referring (f);
835 if (lto_file_data)
836 fprintf (f, " Read from file: %s\n",
837 lto_file_data->file_name);
840 /* Dump symtab node to F. */
842 void
843 symtab_node::dump (FILE *f)
845 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
846 cnode->dump (f);
847 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
848 vnode->dump (f);
851 /* Dump symbol table to F. */
853 void
854 symtab_node::dump_table (FILE *f)
856 symtab_node *node;
857 fprintf (f, "Symbol table:\n\n");
858 FOR_EACH_SYMBOL (node)
859 node->dump (f);
863 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
864 Return NULL if there's no such node. */
866 symtab_node *
867 symtab_node::get_for_asmname (const_tree asmname)
869 symtab_node *node;
871 symtab->symtab_initialize_asm_name_hash ();
872 hashval_t hash = symtab->decl_assembler_name_hash (asmname);
873 symtab_node **slot
874 = symtab->assembler_name_hash->find_slot_with_hash (asmname, hash,
875 NO_INSERT);
877 if (slot)
879 node = *slot;
880 return node;
882 return NULL;
885 /* Dump symtab node NODE to stderr. */
887 DEBUG_FUNCTION void
888 symtab_node::debug (void)
890 dump (stderr);
893 /* Verify common part of symtab nodes. */
895 DEBUG_FUNCTION bool
896 symtab_node::verify_base (void)
898 bool error_found = false;
899 symtab_node *hashed_node;
901 if (is_a <cgraph_node *> (this))
903 if (TREE_CODE (decl) != FUNCTION_DECL)
905 error ("function symbol is not function");
906 error_found = true;
909 else if (is_a <varpool_node *> (this))
911 if (TREE_CODE (decl) != VAR_DECL)
913 error ("variable symbol is not variable");
914 error_found = true;
917 else
919 error ("node has unknown type");
920 error_found = true;
923 if (symtab->state != LTO_STREAMING)
925 hashed_node = symtab_node::get (decl);
926 if (!hashed_node)
928 error ("node not found node->decl->decl_with_vis.symtab_node");
929 error_found = true;
931 if (hashed_node != this
932 && (!is_a <cgraph_node *> (this)
933 || !dyn_cast <cgraph_node *> (this)->clone_of
934 || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl))
936 error ("node differs from node->decl->decl_with_vis.symtab_node");
937 error_found = true;
940 if (symtab->assembler_name_hash)
942 hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
943 if (hashed_node && hashed_node->previous_sharing_asm_name)
945 error ("assembler name hash list corrupted");
946 error_found = true;
948 while (hashed_node)
950 if (hashed_node == this)
951 break;
952 hashed_node = hashed_node->next_sharing_asm_name;
954 if (!hashed_node
955 && !(is_a <varpool_node *> (this)
956 || DECL_HARD_REGISTER (decl)))
958 error ("node not found in symtab assembler name hash");
959 error_found = true;
962 if (previous_sharing_asm_name
963 && previous_sharing_asm_name->next_sharing_asm_name != this)
965 error ("double linked list of assembler names corrupted");
966 error_found = true;
968 if (body_removed && definition)
970 error ("node has body_removed but is definition");
971 error_found = true;
973 if (analyzed && !definition)
975 error ("node is analyzed byt it is not a definition");
976 error_found = true;
978 if (cpp_implicit_alias && !alias)
980 error ("node is alias but not implicit alias");
981 error_found = true;
983 if (alias && !definition && !weakref)
985 error ("node is alias but not definition");
986 error_found = true;
988 if (weakref && !alias)
990 error ("node is weakref but not an alias");
991 error_found = true;
993 if (same_comdat_group)
995 symtab_node *n = same_comdat_group;
997 if (!n->get_comdat_group ())
999 error ("node is in same_comdat_group list but has no comdat_group");
1000 error_found = true;
1002 if (n->get_comdat_group () != get_comdat_group ())
1004 error ("same_comdat_group list across different groups");
1005 error_found = true;
1007 if (n->type != type)
1009 error ("mixing different types of symbol in same comdat groups is not supported");
1010 error_found = true;
1012 if (n == this)
1014 error ("node is alone in a comdat group");
1015 error_found = true;
1019 if (!n->same_comdat_group)
1021 error ("same_comdat_group is not a circular list");
1022 error_found = true;
1023 break;
1025 n = n->same_comdat_group;
1027 while (n != this);
1028 if (comdat_local_p ())
1030 ipa_ref *ref = NULL;
1032 for (int i = 0; iterate_referring (i, ref); ++i)
1034 if (!in_same_comdat_group_p (ref->referring))
1036 error ("comdat-local symbol referred to by %s outside its "
1037 "comdat",
1038 identifier_to_locale (ref->referring->name()));
1039 error_found = true;
1044 if (implicit_section && !get_section ())
1046 error ("implicit_section flag is set but section isn't");
1047 error_found = true;
1049 if (get_section () && get_comdat_group ()
1050 && !implicit_section
1051 && !lookup_attribute ("section", DECL_ATTRIBUTES (decl)))
1053 error ("Both section and comdat group is set");
1054 error_found = true;
1056 /* TODO: Add string table for sections, so we do not keep holding duplicated
1057 strings. */
1058 if (alias && definition
1059 && get_section () != get_alias_target ()->get_section ()
1060 && (!get_section()
1061 || !get_alias_target ()->get_section ()
1062 || strcmp (get_section(),
1063 get_alias_target ()->get_section ())))
1065 error ("Alias and target's section differs");
1066 get_alias_target ()->dump (stderr);
1067 error_found = true;
1069 if (alias && definition
1070 && get_comdat_group () != get_alias_target ()->get_comdat_group ())
1072 error ("Alias and target's comdat groups differs");
1073 get_alias_target ()->dump (stderr);
1074 error_found = true;
1077 return error_found;
1080 /* Verify consistency of NODE. */
1082 DEBUG_FUNCTION void
1083 symtab_node::verify (void)
1085 if (seen_error ())
1086 return;
1088 timevar_push (TV_CGRAPH_VERIFY);
1089 if (cgraph_node *node = dyn_cast <cgraph_node *> (this))
1090 node->verify_node ();
1091 else
1092 if (verify_base ())
1094 debug ();
1095 internal_error ("symtab_node::verify failed");
1097 timevar_pop (TV_CGRAPH_VERIFY);
1100 /* Verify symbol table for internal consistency. */
1102 DEBUG_FUNCTION void
1103 symtab_node::verify_symtab_nodes (void)
1105 symtab_node *node;
1106 hash_map<tree, symtab_node *> comdat_head_map (251);
1108 FOR_EACH_SYMBOL (node)
1110 node->verify ();
1111 if (node->get_comdat_group ())
1113 symtab_node **entry, *s;
1114 bool existed;
1116 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1117 &existed);
1118 if (!existed)
1119 *entry = node;
1120 else if (!DECL_EXTERNAL (node->decl))
1122 for (s = (*entry)->same_comdat_group;
1123 s != NULL && s != node && s != *entry;
1124 s = s->same_comdat_group)
1126 if (!s || s == *entry)
1128 error ("Two symbols with same comdat_group are not linked by "
1129 "the same_comdat_group list.");
1130 (*entry)->debug ();
1131 node->debug ();
1132 internal_error ("symtab_node::verify failed");
1139 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1140 but other code such as notice_global_symbol generates rtl. */
1142 void
1143 symtab_node::make_decl_local (void)
1145 rtx rtl, symbol;
1147 /* Avoid clearing comdat_groups on comdat-local decls. */
1148 if (TREE_PUBLIC (decl) == 0)
1149 return;
1151 if (TREE_CODE (decl) == VAR_DECL)
1153 DECL_COMMON (decl) = 0;
1154 /* ADDRESSABLE flag is not defined for public symbols. */
1155 TREE_ADDRESSABLE (decl) = 1;
1157 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1159 DECL_COMDAT (decl) = 0;
1160 DECL_WEAK (decl) = 0;
1161 DECL_EXTERNAL (decl) = 0;
1162 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1163 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1164 TREE_PUBLIC (decl) = 0;
1165 DECL_DLLIMPORT_P (decl) = 0;
1166 if (!DECL_RTL_SET_P (decl))
1167 return;
1169 /* Update rtl flags. */
1170 make_decl_rtl (decl);
1172 rtl = DECL_RTL (decl);
1173 if (!MEM_P (rtl))
1174 return;
1176 symbol = XEXP (rtl, 0);
1177 if (GET_CODE (symbol) != SYMBOL_REF)
1178 return;
1180 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1183 /* Walk the alias chain to return the symbol NODE is alias of.
1184 If NODE is not an alias, return NODE.
1185 Assumes NODE is known to be alias. */
1187 symtab_node *
1188 symtab_node::ultimate_alias_target_1 (enum availability *availability)
1190 bool weakref_p = false;
1192 /* To determine visibility of the target, we follow ELF semantic of aliases.
1193 Here alias is an alternative assembler name of a given definition. Its
1194 availability prevails the availability of its target (i.e. static alias of
1195 weak definition is available.
1197 Weakref is a different animal (and not part of ELF per se). It is just
1198 alternative name of a given symbol used within one complation unit
1199 and is translated prior hitting the object file. It inherits the
1200 visibility of its target (i.e. weakref of non-overwritable definition
1201 is non-overwritable, while weakref of weak definition is weak).
1203 If we ever get into supporting targets with different semantics, a target
1204 hook will be needed here. */
1206 if (availability)
1208 weakref_p = weakref;
1209 if (!weakref_p)
1210 *availability = get_availability ();
1211 else
1212 *availability = AVAIL_LOCAL;
1215 symtab_node *node = this;
1216 while (node)
1218 if (node->alias && node->analyzed)
1219 node = node->get_alias_target ();
1220 else
1222 if (!availability)
1224 else if (node->analyzed)
1226 if (weakref_p)
1228 enum availability a = node->get_availability ();
1229 if (a < *availability)
1230 *availability = a;
1233 else
1234 *availability = AVAIL_NOT_AVAILABLE;
1235 return node;
1237 if (node && availability && weakref_p)
1239 enum availability a = node->get_availability ();
1240 if (a < *availability)
1241 *availability = a;
1242 weakref_p = node->weakref;
1245 if (availability)
1246 *availability = AVAIL_NOT_AVAILABLE;
1247 return NULL;
1250 /* C++ FE sometimes change linkage flags after producing same body aliases.
1252 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1253 are obviously equivalent. The way it is doing so is however somewhat
1254 kludgy and interferes with the visibility code. As a result we need to
1255 copy the visibility from the target to get things right. */
1257 void
1258 symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target)
1260 if (is_a <cgraph_node *> (this))
1262 DECL_DECLARED_INLINE_P (decl)
1263 = DECL_DECLARED_INLINE_P (target->decl);
1264 DECL_DISREGARD_INLINE_LIMITS (decl)
1265 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1267 /* FIXME: It is not really clear why those flags should not be copied for
1268 functions, too. */
1269 else
1271 DECL_WEAK (decl) = DECL_WEAK (target->decl);
1272 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1273 DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl);
1275 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (target->decl);
1276 if (TREE_PUBLIC (decl))
1278 tree group;
1280 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1281 DECL_COMDAT (decl) = DECL_COMDAT (target->decl);
1282 group = target->get_comdat_group ();
1283 set_comdat_group (group);
1284 if (group && !same_comdat_group)
1285 add_to_same_comdat_group (target);
1287 externally_visible = target->externally_visible;
1290 /* Set section, do not recurse into aliases.
1291 When one wants to change section of symbol and its aliases,
1292 use set_section. */
1294 void
1295 symtab_node::set_section_for_node (const char *section)
1297 const char *current = get_section ();
1298 section_hash_entry **slot;
1300 if (current == section
1301 || (current && section
1302 && !strcmp (current, section)))
1303 return;
1305 if (current)
1307 x_section->ref_count--;
1308 if (!x_section->ref_count)
1310 hashval_t hash = htab_hash_string (x_section->name);
1311 slot = symtab->section_hash->find_slot_with_hash (x_section->name,
1312 hash, INSERT);
1313 ggc_free (x_section);
1314 symtab->section_hash->clear_slot (slot);
1316 x_section = NULL;
1318 if (!section)
1320 implicit_section = false;
1321 return;
1323 if (!symtab->section_hash)
1324 symtab->section_hash = hash_table<section_name_hasher>::create_ggc (10);
1325 slot = symtab->section_hash->find_slot_with_hash (section,
1326 htab_hash_string (section),
1327 INSERT);
1328 if (*slot)
1329 x_section = (section_hash_entry *)*slot;
1330 else
1332 int len = strlen (section);
1333 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1334 x_section->name = ggc_vec_alloc<char> (len + 1);
1335 memcpy (x_section->name, section, len + 1);
1337 x_section->ref_count++;
1340 /* Worker for set_section. */
1342 bool
1343 symtab_node::set_section (symtab_node *n, void *s)
1345 n->set_section_for_node ((char *)s);
1346 return false;
1349 /* Set section of symbol and its aliases. */
1351 void
1352 symtab_node::set_section (const char *section)
1354 gcc_assert (!this->alias);
1355 call_for_symbol_and_aliases
1356 (symtab_node::set_section, const_cast<char *>(section), true);
1359 /* Return the initialization priority. */
1361 priority_type
1362 symtab_node::get_init_priority ()
1364 if (!this->in_init_priority_hash)
1365 return DEFAULT_INIT_PRIORITY;
1367 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1368 return h ? h->init : DEFAULT_INIT_PRIORITY;
1371 /* Return the finalization priority. */
1373 priority_type
1374 cgraph_node::get_fini_priority ()
1376 if (!this->in_init_priority_hash)
1377 return DEFAULT_INIT_PRIORITY;
1378 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1379 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1382 /* Return the initialization and finalization priority information for
1383 DECL. If there is no previous priority information, a freshly
1384 allocated structure is returned. */
1386 symbol_priority_map *
1387 symtab_node::priority_info (void)
1389 if (!symtab->init_priority_hash)
1390 symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13);
1392 bool existed;
1393 symbol_priority_map *h
1394 = &symtab->init_priority_hash->get_or_insert (this, &existed);
1395 if (!existed)
1397 h->init = DEFAULT_INIT_PRIORITY;
1398 h->fini = DEFAULT_INIT_PRIORITY;
1399 in_init_priority_hash = true;
1402 return h;
1405 /* Set initialization priority to PRIORITY. */
1407 void
1408 symtab_node::set_init_priority (priority_type priority)
1410 symbol_priority_map *h;
1412 if (is_a <cgraph_node *> (this))
1413 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1415 if (priority == DEFAULT_INIT_PRIORITY)
1417 gcc_assert (get_init_priority() == priority);
1418 return;
1420 h = priority_info ();
1421 h->init = priority;
1424 /* Set fialization priority to PRIORITY. */
1426 void
1427 cgraph_node::set_fini_priority (priority_type priority)
1429 symbol_priority_map *h;
1431 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1433 if (priority == DEFAULT_INIT_PRIORITY)
1435 gcc_assert (get_fini_priority() == priority);
1436 return;
1438 h = priority_info ();
1439 h->fini = priority;
1442 /* Worker for symtab_resolve_alias. */
1444 bool
1445 symtab_node::set_implicit_section (symtab_node *n,
1446 void *data ATTRIBUTE_UNUSED)
1448 n->implicit_section = true;
1449 return false;
1452 /* Add reference recording that symtab node is alias of TARGET.
1453 The function can fail in the case of aliasing cycles; in this case
1454 it returns false. */
1456 bool
1457 symtab_node::resolve_alias (symtab_node *target)
1459 symtab_node *n;
1461 gcc_assert (!analyzed && !vec_safe_length (ref_list.references));
1463 /* Never let cycles to creep into the symbol table alias references;
1464 those will make alias walkers to be infinite. */
1465 for (n = target; n && n->alias;
1466 n = n->analyzed ? n->get_alias_target () : NULL)
1467 if (n == this)
1469 if (is_a <cgraph_node *> (this))
1470 error ("function %q+D part of alias cycle", decl);
1471 else if (is_a <varpool_node *> (this))
1472 error ("variable %q+D part of alias cycle", decl);
1473 else
1474 gcc_unreachable ();
1475 alias = false;
1476 return false;
1479 /* "analyze" the node - i.e. mark the reference. */
1480 definition = true;
1481 alias = true;
1482 analyzed = true;
1483 create_reference (target, IPA_REF_ALIAS, NULL);
1485 /* Add alias into the comdat group of its target unless it is already there. */
1486 if (same_comdat_group)
1487 remove_from_same_comdat_group ();
1488 set_comdat_group (NULL);
1489 if (target->get_comdat_group ())
1490 add_to_same_comdat_group (target);
1492 if ((get_section () != target->get_section ()
1493 || target->get_comdat_group ()) && get_section () && !implicit_section)
1495 error ("section of alias %q+D must match section of its target", decl);
1497 call_for_symbol_and_aliases (symtab_node::set_section,
1498 const_cast<char *>(target->get_section ()), true);
1499 if (target->implicit_section)
1500 call_for_symbol_and_aliases (set_implicit_section, NULL, true);
1502 /* Alias targets become redundant after alias is resolved into an reference.
1503 We do not want to keep it around or we would have to mind updating them
1504 when renaming symbols. */
1505 alias_target = NULL;
1507 if (cpp_implicit_alias && symtab->state >= CONSTRUCTION)
1508 fixup_same_cpp_alias_visibility (target);
1510 /* If alias has address taken, so does the target. */
1511 if (address_taken)
1512 target->ultimate_alias_target ()->address_taken = true;
1514 /* All non-weakref aliases of THIS are now in fact aliases of TARGET. */
1515 ipa_ref *ref;
1516 for (unsigned i = 0; iterate_direct_aliases (i, ref);)
1518 struct symtab_node *alias_alias = ref->referring;
1519 if (!alias_alias->weakref)
1521 alias_alias->remove_all_references ();
1522 alias_alias->create_reference (target, IPA_REF_ALIAS, NULL);
1524 else i++;
1526 return true;
1529 /* Worker searching noninterposable alias. */
1531 bool
1532 symtab_node::noninterposable_alias (symtab_node *node, void *data)
1534 if (decl_binds_to_current_def_p (node->decl))
1536 symtab_node *fn = node->ultimate_alias_target ();
1538 /* Ensure that the alias is well formed this may not be the case
1539 of user defined aliases and currently it is not always the case
1540 of C++ same body aliases (that is a bug). */
1541 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
1542 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
1543 || (TREE_CODE (node->decl) == FUNCTION_DECL
1544 && flags_from_decl_or_type (node->decl)
1545 != flags_from_decl_or_type (fn->decl))
1546 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
1547 return false;
1548 *(symtab_node **)data = node;
1549 return true;
1551 return false;
1554 /* If node can not be overwriten by static or dynamic linker to point to
1555 different definition, return NODE. Otherwise look for alias with such
1556 property and if none exists, introduce new one. */
1558 symtab_node *
1559 symtab_node::noninterposable_alias (void)
1561 tree new_decl;
1562 symtab_node *new_node = NULL;
1564 /* First try to look up existing alias or base object
1565 (if that is already non-overwritable). */
1566 symtab_node *node = ultimate_alias_target ();
1567 gcc_assert (!node->alias && !node->weakref);
1568 node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias,
1569 (void *)&new_node, true);
1570 if (new_node)
1571 return new_node;
1572 #ifndef ASM_OUTPUT_DEF
1573 /* If aliases aren't supported by the assembler, fail. */
1574 return NULL;
1575 #endif
1577 /* Otherwise create a new one. */
1578 new_decl = copy_node (node->decl);
1579 DECL_DLLIMPORT_P (new_decl) = 0;
1580 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1581 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1582 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1583 DECL_INITIAL (new_decl) = NULL;
1584 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1585 SET_DECL_RTL (new_decl, NULL);
1587 /* Update the properties. */
1588 DECL_EXTERNAL (new_decl) = 0;
1589 TREE_PUBLIC (new_decl) = 0;
1590 DECL_COMDAT (new_decl) = 0;
1591 DECL_WEAK (new_decl) = 0;
1593 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1594 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1595 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1597 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1598 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1599 new_node = cgraph_node::create_alias (new_decl, node->decl);
1601 else
1603 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1604 DECL_INITIAL (new_decl) = error_mark_node;
1605 new_node = varpool_node::create_alias (new_decl, node->decl);
1607 new_node->resolve_alias (node);
1608 gcc_assert (decl_binds_to_current_def_p (new_decl)
1609 && targetm.binds_local_p (new_decl));
1610 return new_node;
1613 /* Return true if symtab node and TARGET represents
1614 semantically equivalent symbols. */
1616 bool
1617 symtab_node::semantically_equivalent_p (symtab_node *target)
1619 enum availability avail;
1620 symtab_node *ba;
1621 symtab_node *bb;
1623 /* Equivalent functions are equivalent. */
1624 if (decl == target->decl)
1625 return true;
1627 /* If symbol is not overwritable by different implementation,
1628 walk to the base object it defines. */
1629 ba = ultimate_alias_target (&avail);
1630 if (avail >= AVAIL_AVAILABLE)
1632 if (target == ba)
1633 return true;
1635 else
1636 ba = this;
1637 bb = target->ultimate_alias_target (&avail);
1638 if (avail >= AVAIL_AVAILABLE)
1640 if (this == bb)
1641 return true;
1643 else
1644 bb = target;
1645 return bb == ba;
1648 /* Classify symbol symtab node for partitioning. */
1650 enum symbol_partitioning_class
1651 symtab_node::get_partitioning_class (void)
1653 /* Inline clones are always duplicated.
1654 This include external delcarations. */
1655 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
1657 if (DECL_ABSTRACT_P (decl))
1658 return SYMBOL_EXTERNAL;
1660 if (cnode && cnode->global.inlined_to)
1661 return SYMBOL_DUPLICATE;
1663 /* Weakref aliases are always duplicated. */
1664 if (weakref)
1665 return SYMBOL_DUPLICATE;
1667 /* External declarations are external. */
1668 if (DECL_EXTERNAL (decl))
1669 return SYMBOL_EXTERNAL;
1671 if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
1673 if (alias && definition && !ultimate_alias_target ()->definition)
1674 return SYMBOL_EXTERNAL;
1675 /* Constant pool references use local symbol names that can not
1676 be promoted global. We should never put into a constant pool
1677 objects that can not be duplicated across partitions. */
1678 if (DECL_IN_CONSTANT_POOL (decl))
1679 return SYMBOL_DUPLICATE;
1680 if (DECL_HARD_REGISTER (decl))
1681 return SYMBOL_DUPLICATE;
1682 gcc_checking_assert (vnode->definition);
1684 /* Functions that are cloned may stay in callgraph even if they are unused.
1685 Handle them as external; compute_ltrans_boundary take care to make
1686 proper things to happen (i.e. to make them appear in the boundary but
1687 with body streamed, so clone can me materialized). */
1688 else if (!dyn_cast <cgraph_node *> (this)->function_symbol ()->definition)
1689 return SYMBOL_EXTERNAL;
1691 /* Linker discardable symbols are duplicated to every use unless they are
1692 keyed. */
1693 if (DECL_ONE_ONLY (decl)
1694 && !force_output
1695 && !forced_by_abi
1696 && !used_from_object_file_p ())
1697 return SYMBOL_DUPLICATE;
1699 return SYMBOL_PARTITION;
1702 /* Return true when symbol is known to be non-zero. */
1704 bool
1705 symtab_node::nonzero_address ()
1707 /* Weakrefs may be NULL when their target is not defined. */
1708 if (alias && weakref)
1710 if (analyzed)
1712 symtab_node *target = ultimate_alias_target ();
1714 if (target->alias && target->weakref)
1715 return false;
1716 /* We can not recurse to target::nonzero. It is possible that the
1717 target is used only via the alias.
1718 We may walk references and look for strong use, but we do not know
1719 if this strong use will survive to final binary, so be
1720 conservative here.
1721 ??? Maybe we could do the lookup during late optimization that
1722 could be useful to eliminate the NULL pointer checks in LTO
1723 programs. */
1724 if (target->definition && !DECL_EXTERNAL (target->decl))
1725 return true;
1726 if (target->resolution != LDPR_UNKNOWN
1727 && target->resolution != LDPR_UNDEF
1728 && flag_delete_null_pointer_checks)
1729 return true;
1730 return false;
1732 else
1733 return false;
1736 /* With !flag_delete_null_pointer_checks we assume that symbols may
1737 bind to NULL. This is on by default on embedded targets only.
1739 Otherwise all non-WEAK symbols must be defined and thus non-NULL or
1740 linking fails. Important case of WEAK we want to do well are comdats.
1741 Those are handled by later check for definition.
1743 When parsing, beware the cases when WEAK attribute is added later. */
1744 if (!DECL_WEAK (decl)
1745 && flag_delete_null_pointer_checks)
1747 refuse_visibility_changes = true;
1748 return true;
1751 /* If target is defined and not extern, we know it will be output and thus
1752 it will bind to non-NULL.
1753 Play safe for flag_delete_null_pointer_checks where weak definition maye
1754 be re-defined by NULL. */
1755 if (definition && !DECL_EXTERNAL (decl)
1756 && (flag_delete_null_pointer_checks || !DECL_WEAK (decl)))
1758 if (!DECL_WEAK (decl))
1759 refuse_visibility_changes = true;
1760 return true;
1763 /* As the last resort, check the resolution info. */
1764 if (resolution != LDPR_UNKNOWN
1765 && resolution != LDPR_UNDEF
1766 && flag_delete_null_pointer_checks)
1767 return true;
1768 return false;
1771 /* Return 0 if symbol is known to have different address than S2,
1772 Return 1 if symbol is known to have same address as S2,
1773 return 2 otherwise. */
1775 symtab_node::equal_address_to (symtab_node *s2)
1777 enum availability avail1, avail2;
1779 /* A Shortcut: equivalent symbols are always equivalent. */
1780 if (this == s2)
1781 return 1;
1783 /* For non-interposable aliases, lookup and compare their actual definitions.
1784 Also check if the symbol needs to bind to given definition. */
1785 symtab_node *rs1 = ultimate_alias_target (&avail1);
1786 symtab_node *rs2 = s2->ultimate_alias_target (&avail2);
1787 bool binds_local1 = rs1->analyzed && decl_binds_to_current_def_p (this->decl);
1788 bool binds_local2 = rs2->analyzed && decl_binds_to_current_def_p (s2->decl);
1789 bool really_binds_local1 = binds_local1;
1790 bool really_binds_local2 = binds_local2;
1792 /* Addresses of vtables and virtual functions can not be used by user
1793 code and are used only within speculation. In this case we may make
1794 symbol equivalent to its alias even if interposition may break this
1795 rule. Doing so will allow us to turn speculative inlining into
1796 non-speculative more agressively. */
1797 if (DECL_VIRTUAL_P (this->decl) && avail1 >= AVAIL_AVAILABLE)
1798 binds_local1 = true;
1799 if (DECL_VIRTUAL_P (s2->decl) && avail2 >= AVAIL_AVAILABLE)
1800 binds_local2 = true;
1802 /* If both definitions are available we know that even if they are bound
1803 to other unit they must be defined same way and therefore we can use
1804 equivalence test. */
1805 if (rs1 != rs2 && avail1 >= AVAIL_AVAILABLE && avail2 >= AVAIL_AVAILABLE)
1806 binds_local1 = binds_local2 = true;
1808 if ((binds_local1 ? rs1 : this)
1809 == (binds_local2 ? rs2 : s2))
1811 /* We made use of the fact that alias is not weak. */
1812 if (binds_local1 && rs1 != this)
1813 refuse_visibility_changes = true;
1814 if (binds_local2 && rs2 != s2)
1815 s2->refuse_visibility_changes = true;
1816 return 1;
1819 /* If both symbols may resolve to NULL, we can not really prove them different. */
1820 if (!nonzero_address () && !s2->nonzero_address ())
1821 return 2;
1823 /* Except for NULL, functions and variables never overlap. */
1824 if (TREE_CODE (decl) != TREE_CODE (s2->decl))
1825 return 0;
1827 /* If one of the symbols is unresolved alias, punt. */
1828 if (rs1->alias || rs2->alias)
1829 return 2;
1831 /* If we have a non-interposale definition of at least one of the symbols
1832 and the other symbol is different, we know other unit can not interpose
1833 it to the first symbol; all aliases of the definition needs to be
1834 present in the current unit. */
1835 if (((really_binds_local1 || really_binds_local2)
1836 /* If we have both definitions and they are different, we know they
1837 will be different even in units they binds to. */
1838 || (binds_local1 && binds_local2))
1839 && rs1 != rs2)
1841 /* We make use of the fact that one symbol is not alias of the other
1842 and that the definition is non-interposable. */
1843 refuse_visibility_changes = true;
1844 s2->refuse_visibility_changes = true;
1845 rs1->refuse_visibility_changes = true;
1846 rs2->refuse_visibility_changes = true;
1847 return 0;
1850 /* TODO: Alias oracle basically assume that addresses of global variables
1851 are different unless they are declared as alias of one to another.
1852 We probably should be consistent and use this fact here, too, and update
1853 alias oracle to use this predicate. */
1855 return 2;
1858 /* Worker for call_for_symbol_and_aliases. */
1860 bool
1861 symtab_node::call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *,
1862 void *),
1863 void *data,
1864 bool include_overwritable)
1866 ipa_ref *ref;
1867 FOR_EACH_ALIAS (this, ref)
1869 symtab_node *alias = ref->referring;
1870 if (include_overwritable
1871 || alias->get_availability () > AVAIL_INTERPOSABLE)
1872 if (alias->call_for_symbol_and_aliases (callback, data,
1873 include_overwritable))
1874 return true;
1876 return false;
1879 /* Return ture if address of N is possibly compared. */
1881 static bool
1882 address_matters_1 (symtab_node *n, void *)
1884 struct ipa_ref *ref;
1886 if (!n->address_can_be_compared_p ())
1887 return false;
1888 if (n->externally_visible || n->force_output)
1889 return true;
1891 for (unsigned int i = 0; n->iterate_referring (i, ref); i++)
1892 if (ref->address_matters_p ())
1893 return true;
1894 return false;
1897 /* Return true if symbol's address may possibly be compared to other
1898 symbol's address. */
1900 bool
1901 symtab_node::address_matters_p ()
1903 gcc_assert (!alias);
1904 return call_for_symbol_and_aliases (address_matters_1, NULL, true);
1907 /* Return ture if symbol's alignment may be increased. */
1909 bool
1910 symtab_node::can_increase_alignment_p (void)
1912 symtab_node *target = ultimate_alias_target ();
1914 /* For now support only variables. */
1915 if (TREE_CODE (decl) != VAR_DECL)
1916 return false;
1918 /* With -fno-toplevel-reorder we may have already output the constant. */
1919 if (TREE_ASM_WRITTEN (target->decl))
1920 return false;
1922 /* If target is already placed in an anchor, we can not touch its
1923 alignment. */
1924 if (DECL_RTL_SET_P (target->decl)
1925 && MEM_P (DECL_RTL (target->decl))
1926 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (DECL_RTL (target->decl), 0)))
1927 return false;
1929 /* Constant pool entries may be shared. */
1930 if (DECL_IN_CONSTANT_POOL (target->decl))
1931 return false;
1933 /* We cannot change alignment of symbols that may bind to symbols
1934 in other translation unit that may contain a definition with lower
1935 alignment. */
1936 if (!decl_binds_to_current_def_p (decl))
1937 return false;
1939 /* When compiling partition, be sure the symbol is not output by other
1940 partition. */
1941 if (flag_ltrans
1942 && (target->in_other_partition
1943 || target->get_partitioning_class () == SYMBOL_DUPLICATE))
1944 return false;
1946 /* Do not override the alignment as specified by the ABI when the used
1947 attribute is set. */
1948 if (DECL_PRESERVE_P (decl) || DECL_PRESERVE_P (target->decl))
1949 return false;
1951 /* Do not override explicit alignment set by the user when an explicit
1952 section name is also used. This is a common idiom used by many
1953 software projects. */
1954 if (DECL_SECTION_NAME (target->decl) != NULL && !target->implicit_section)
1955 return false;
1957 return true;
1960 /* Worker for symtab_node::increase_alignment. */
1962 static bool
1963 increase_alignment_1 (symtab_node *n, void *v)
1965 unsigned int align = (size_t)v;
1966 if (DECL_ALIGN (n->decl) < align
1967 && n->can_increase_alignment_p ())
1969 DECL_ALIGN (n->decl) = align;
1970 DECL_USER_ALIGN (n->decl) = 1;
1972 return false;
1975 /* Increase alignment of THIS to ALIGN. */
1977 void
1978 symtab_node::increase_alignment (unsigned int align)
1980 gcc_assert (can_increase_alignment_p () && align < MAX_OFILE_ALIGNMENT);
1981 ultimate_alias_target()->call_for_symbol_and_aliases (increase_alignment_1,
1982 (void *)(size_t) align,
1983 true);
1984 gcc_assert (DECL_ALIGN (decl) >= align);
1987 /* Helper for symtab_node::definition_alignment. */
1989 static bool
1990 get_alignment_1 (symtab_node *n, void *v)
1992 *((unsigned int *)v) = MAX (*((unsigned int *)v), DECL_ALIGN (n->decl));
1993 return false;
1996 /* Return desired alignment of the definition. This is NOT alignment useful
1997 to access THIS, because THIS may be interposable and DECL_ALIGN should
1998 be used instead. It however must be guaranteed when output definition
1999 of THIS. */
2001 unsigned int
2002 symtab_node::definition_alignment ()
2004 unsigned int align = 0;
2005 gcc_assert (!alias);
2006 call_for_symbol_and_aliases (get_alignment_1, &align, true);
2007 return align;