2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / symtab.c
blob9dc07f5b655ea4b3e6d6da39e120deed91facc00
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 "input.h"
27 #include "alias.h"
28 #include "symtab.h"
29 #include "tree.h"
30 #include "fold-const.h"
31 #include "print-tree.h"
32 #include "varasm.h"
33 #include "hard-reg-set.h"
34 #include "input.h"
35 #include "function.h"
36 #include "emit-rtl.h"
37 #include "predict.h"
38 #include "basic-block.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "gimple-expr.h"
42 #include "is-a.h"
43 #include "gimple.h"
44 #include "tree-inline.h"
45 #include "langhooks.h"
46 #include "plugin-api.h"
47 #include "ipa-ref.h"
48 #include "cgraph.h"
49 #include "diagnostic.h"
50 #include "timevar.h"
51 #include "lto-streamer.h"
52 #include "output.h"
53 #include "ipa-utils.h"
54 #include "calls.h"
56 static const char *ipa_ref_use_name[] = {"read","write","addr","alias","chkp"};
58 const char * const ld_plugin_symbol_resolution_names[]=
60 "",
61 "undef",
62 "prevailing_def",
63 "prevailing_def_ironly",
64 "preempted_reg",
65 "preempted_ir",
66 "resolved_ir",
67 "resolved_exec",
68 "resolved_dyn",
69 "prevailing_def_ironly_exp"
72 /* Hash asmnames ignoring the user specified marks. */
74 hashval_t
75 symbol_table::decl_assembler_name_hash (const_tree asmname)
77 if (IDENTIFIER_POINTER (asmname)[0] == '*')
79 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
80 size_t ulp_len = strlen (user_label_prefix);
82 if (ulp_len == 0)
84 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
85 decl_str += ulp_len;
87 return htab_hash_string (decl_str);
90 return htab_hash_string (IDENTIFIER_POINTER (asmname));
94 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
96 bool
97 symbol_table::decl_assembler_name_equal (tree decl, const_tree asmname)
99 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
100 const char *decl_str;
101 const char *asmname_str;
102 bool test = false;
104 if (decl_asmname == asmname)
105 return true;
107 decl_str = IDENTIFIER_POINTER (decl_asmname);
108 asmname_str = IDENTIFIER_POINTER (asmname);
111 /* If the target assembler name was set by the user, things are trickier.
112 We have a leading '*' to begin with. After that, it's arguable what
113 is the correct thing to do with -fleading-underscore. Arguably, we've
114 historically been doing the wrong thing in assemble_alias by always
115 printing the leading underscore. Since we're not changing that, make
116 sure user_label_prefix follows the '*' before matching. */
117 if (decl_str[0] == '*')
119 size_t ulp_len = strlen (user_label_prefix);
121 decl_str ++;
123 if (ulp_len == 0)
124 test = true;
125 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
126 decl_str += ulp_len, test=true;
127 else
128 decl_str --;
130 if (asmname_str[0] == '*')
132 size_t ulp_len = strlen (user_label_prefix);
134 asmname_str ++;
136 if (ulp_len == 0)
137 test = true;
138 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
139 asmname_str += ulp_len, test=true;
140 else
141 asmname_str --;
144 if (!test)
145 return false;
146 return strcmp (decl_str, asmname_str) == 0;
150 /* Returns nonzero if P1 and P2 are equal. */
152 /* Insert NODE to assembler name hash. */
154 void
155 symbol_table::insert_to_assembler_name_hash (symtab_node *node,
156 bool with_clones)
158 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
159 return;
160 gcc_checking_assert (!node->previous_sharing_asm_name
161 && !node->next_sharing_asm_name);
162 if (assembler_name_hash)
164 symtab_node **aslot;
165 cgraph_node *cnode;
166 tree decl = node->decl;
168 tree name = DECL_ASSEMBLER_NAME (node->decl);
170 /* C++ FE can produce decls without associated assembler name and insert
171 them to symtab to hold section or TLS information. */
172 if (!name)
173 return;
175 hashval_t hash = decl_assembler_name_hash (name);
176 aslot = assembler_name_hash->find_slot_with_hash (name, hash, INSERT);
177 gcc_assert (*aslot != node);
178 node->next_sharing_asm_name = (symtab_node *)*aslot;
179 if (*aslot != NULL)
180 (*aslot)->previous_sharing_asm_name = node;
181 *aslot = node;
183 /* Update also possible inline clones sharing a decl. */
184 cnode = dyn_cast <cgraph_node *> (node);
185 if (cnode && cnode->clones && with_clones)
186 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
187 if (cnode->decl == decl)
188 insert_to_assembler_name_hash (cnode, true);
193 /* Remove NODE from assembler name hash. */
195 void
196 symbol_table::unlink_from_assembler_name_hash (symtab_node *node,
197 bool with_clones)
199 if (assembler_name_hash)
201 cgraph_node *cnode;
202 tree decl = node->decl;
204 if (node->next_sharing_asm_name)
205 node->next_sharing_asm_name->previous_sharing_asm_name
206 = node->previous_sharing_asm_name;
207 if (node->previous_sharing_asm_name)
209 node->previous_sharing_asm_name->next_sharing_asm_name
210 = node->next_sharing_asm_name;
212 else
214 tree name = DECL_ASSEMBLER_NAME (node->decl);
215 symtab_node **slot;
217 if (!name)
218 return;
220 hashval_t hash = decl_assembler_name_hash (name);
221 slot = assembler_name_hash->find_slot_with_hash (name, hash,
222 NO_INSERT);
223 gcc_assert (*slot == node);
224 if (!node->next_sharing_asm_name)
225 assembler_name_hash->clear_slot (slot);
226 else
227 *slot = node->next_sharing_asm_name;
229 node->next_sharing_asm_name = NULL;
230 node->previous_sharing_asm_name = NULL;
232 /* Update also possible inline clones sharing a decl. */
233 cnode = dyn_cast <cgraph_node *> (node);
234 if (cnode && cnode->clones && with_clones)
235 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
236 if (cnode->decl == decl)
237 unlink_from_assembler_name_hash (cnode, true);
241 /* Arrange node to be first in its entry of assembler_name_hash. */
243 void
244 symbol_table::symtab_prevail_in_asm_name_hash (symtab_node *node)
246 unlink_from_assembler_name_hash (node, false);
247 insert_to_assembler_name_hash (node, false);
250 /* Initalize asm name hash unless. */
252 void
253 symbol_table::symtab_initialize_asm_name_hash (void)
255 symtab_node *node;
256 if (!assembler_name_hash)
258 assembler_name_hash = hash_table<asmname_hasher>::create_ggc (10);
259 FOR_EACH_SYMBOL (node)
260 insert_to_assembler_name_hash (node, false);
264 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
266 void
267 symbol_table::change_decl_assembler_name (tree decl, tree name)
269 symtab_node *node = NULL;
271 /* We can have user ASM names on things, like global register variables, that
272 are not in the symbol table. */
273 if ((TREE_CODE (decl) == VAR_DECL
274 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
275 || TREE_CODE (decl) == FUNCTION_DECL)
276 node = symtab_node::get (decl);
277 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
279 SET_DECL_ASSEMBLER_NAME (decl, name);
280 if (node)
281 insert_to_assembler_name_hash (node, true);
283 else
285 if (name == DECL_ASSEMBLER_NAME (decl))
286 return;
288 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
289 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
290 : NULL);
291 if (node)
292 unlink_from_assembler_name_hash (node, true);
293 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
294 && DECL_RTL_SET_P (decl))
295 warning (0, "%D renamed after being referenced in assembly", decl);
297 SET_DECL_ASSEMBLER_NAME (decl, name);
298 if (alias)
300 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
301 TREE_CHAIN (name) = alias;
303 if (node)
304 insert_to_assembler_name_hash (node, true);
308 /* Hash sections by their names. */
310 hashval_t
311 section_name_hasher::hash (section_hash_entry *n)
313 return htab_hash_string (n->name);
316 /* Return true if section P1 name equals to P2. */
318 bool
319 section_name_hasher::equal (section_hash_entry *n1, const char *name)
321 return n1->name == name || !strcmp (n1->name, name);
324 /* Add node into symbol table. This function is not used directly, but via
325 cgraph/varpool node creation routines. */
327 void
328 symtab_node::register_symbol (void)
330 symtab->register_symbol (this);
332 if (!decl->decl_with_vis.symtab_node)
333 decl->decl_with_vis.symtab_node = this;
335 ref_list.clear ();
337 /* Be sure to do this last; C++ FE might create new nodes via
338 DECL_ASSEMBLER_NAME langhook! */
339 symtab->insert_to_assembler_name_hash (this, false);
342 /* Remove NODE from same comdat group. */
344 void
345 symtab_node::remove_from_same_comdat_group (void)
347 if (same_comdat_group)
349 symtab_node *prev;
350 for (prev = same_comdat_group;
351 prev->same_comdat_group != this;
352 prev = prev->same_comdat_group)
354 if (same_comdat_group == prev)
355 prev->same_comdat_group = NULL;
356 else
357 prev->same_comdat_group = same_comdat_group;
358 same_comdat_group = NULL;
359 set_comdat_group (NULL);
363 /* Remove node from symbol table. This function is not used directly, but via
364 cgraph/varpool node removal routines. */
366 void
367 symtab_node::unregister (void)
369 remove_all_references ();
370 remove_all_referring ();
372 /* Remove reference to section. */
373 set_section_for_node (NULL);
375 remove_from_same_comdat_group ();
377 symtab->unregister (this);
379 /* During LTO symtab merging we temporarily corrupt decl to symtab node
380 hash. */
381 gcc_assert (decl->decl_with_vis.symtab_node || in_lto_p);
382 if (decl->decl_with_vis.symtab_node == this)
384 symtab_node *replacement_node = NULL;
385 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
386 replacement_node = cnode->find_replacement ();
387 decl->decl_with_vis.symtab_node = replacement_node;
389 if (!is_a <varpool_node *> (this) || !DECL_HARD_REGISTER (decl))
390 symtab->unlink_from_assembler_name_hash (this, false);
391 if (in_init_priority_hash)
392 symtab->init_priority_hash->remove (this);
396 /* Remove symbol from symbol table. */
398 void
399 symtab_node::remove (void)
401 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
402 cnode->remove ();
403 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
404 vnode->remove ();
407 /* Add NEW_ to the same comdat group that OLD is in. */
409 void
410 symtab_node::add_to_same_comdat_group (symtab_node *old_node)
412 gcc_assert (old_node->get_comdat_group ());
413 gcc_assert (!same_comdat_group);
414 gcc_assert (this != old_node);
416 set_comdat_group (old_node->get_comdat_group ());
417 same_comdat_group = old_node;
418 if (!old_node->same_comdat_group)
419 old_node->same_comdat_group = this;
420 else
422 symtab_node *n;
423 for (n = old_node->same_comdat_group;
424 n->same_comdat_group != old_node;
425 n = n->same_comdat_group)
427 n->same_comdat_group = this;
431 /* Dissolve the same_comdat_group list in which NODE resides. */
433 void
434 symtab_node::dissolve_same_comdat_group_list (void)
436 symtab_node *n = this;
437 symtab_node *next;
439 if (!same_comdat_group)
440 return;
443 next = n->same_comdat_group;
444 n->same_comdat_group = NULL;
445 /* Clear comdat_group for comdat locals, since
446 make_decl_local doesn't. */
447 if (!TREE_PUBLIC (n->decl))
448 n->set_comdat_group (NULL);
449 n = next;
451 while (n != this);
454 /* Return printable assembler name of NODE.
455 This function is used only for debugging. When assembler name
456 is unknown go with identifier name. */
458 const char *
459 symtab_node::asm_name () const
461 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
462 return lang_hooks.decl_printable_name (decl, 2);
463 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
466 /* Return printable identifier name. */
468 const char *
469 symtab_node::name () const
471 return lang_hooks.decl_printable_name (decl, 2);
474 /* Return ipa reference from this symtab_node to
475 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
476 of the use. */
478 ipa_ref *
479 symtab_node::create_reference (symtab_node *referred_node,
480 enum ipa_ref_use use_type)
482 return create_reference (referred_node, use_type, NULL);
486 /* Return ipa reference from this symtab_node to
487 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
488 of the use and STMT the statement (if it exists). */
490 ipa_ref *
491 symtab_node::create_reference (symtab_node *referred_node,
492 enum ipa_ref_use use_type, gimple stmt)
494 ipa_ref *ref = NULL, *ref2 = NULL;
495 ipa_ref_list *list, *list2;
496 ipa_ref_t *old_references;
498 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
499 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
501 list = &ref_list;
502 old_references = vec_safe_address (list->references);
503 vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
504 ref = &list->references->last ();
506 list2 = &referred_node->ref_list;
508 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
509 if(use_type == IPA_REF_ALIAS)
511 list2->referring.safe_insert (0, ref);
512 ref->referred_index = 0;
514 for (unsigned int i = 1; i < list2->referring.length (); i++)
515 list2->referring[i]->referred_index = i;
517 else
519 list2->referring.safe_push (ref);
520 ref->referred_index = list2->referring.length () - 1;
523 ref->referring = this;
524 ref->referred = referred_node;
525 ref->stmt = stmt;
526 ref->lto_stmt_uid = 0;
527 ref->use = use_type;
528 ref->speculative = 0;
530 /* If vector was moved in memory, update pointers. */
531 if (old_references != list->references->address ())
533 int i;
534 for (i = 0; iterate_reference(i, ref2); i++)
535 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
537 return ref;
540 /* If VAL is a reference to a function or a variable, add a reference from
541 this symtab_node to the corresponding symbol table node. USE_TYPE specify
542 type of the use and STMT the statement (if it exists). Return the new
543 reference or NULL if none was created. */
545 ipa_ref *
546 symtab_node::maybe_create_reference (tree val, enum ipa_ref_use use_type,
547 gimple stmt)
549 STRIP_NOPS (val);
550 if (TREE_CODE (val) != ADDR_EXPR)
551 return NULL;
552 val = get_base_var (val);
553 if (val && (TREE_CODE (val) == FUNCTION_DECL
554 || TREE_CODE (val) == VAR_DECL))
556 symtab_node *referred = symtab_node::get (val);
557 gcc_checking_assert (referred);
558 return create_reference (referred, use_type, stmt);
560 return NULL;
563 /* Clone all references from symtab NODE to this symtab_node. */
565 void
566 symtab_node::clone_references (symtab_node *node)
568 ipa_ref *ref = NULL, *ref2 = NULL;
569 int i;
570 for (i = 0; node->iterate_reference (i, ref); i++)
572 bool speculative = ref->speculative;
573 unsigned int stmt_uid = ref->lto_stmt_uid;
575 ref2 = create_reference (ref->referred, ref->use, ref->stmt);
576 ref2->speculative = speculative;
577 ref2->lto_stmt_uid = stmt_uid;
581 /* Clone all referring from symtab NODE to this symtab_node. */
583 void
584 symtab_node::clone_referring (symtab_node *node)
586 ipa_ref *ref = NULL, *ref2 = NULL;
587 int i;
588 for (i = 0; node->iterate_referring(i, ref); i++)
590 bool speculative = ref->speculative;
591 unsigned int stmt_uid = ref->lto_stmt_uid;
593 ref2 = ref->referring->create_reference (this, ref->use, ref->stmt);
594 ref2->speculative = speculative;
595 ref2->lto_stmt_uid = stmt_uid;
599 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
601 ipa_ref *
602 symtab_node::clone_reference (ipa_ref *ref, gimple stmt)
604 bool speculative = ref->speculative;
605 unsigned int stmt_uid = ref->lto_stmt_uid;
606 ipa_ref *ref2;
608 ref2 = create_reference (ref->referred, ref->use, stmt);
609 ref2->speculative = speculative;
610 ref2->lto_stmt_uid = stmt_uid;
611 return ref2;
614 /* Find the structure describing a reference to REFERRED_NODE
615 and associated with statement STMT. */
617 ipa_ref *
618 symtab_node::find_reference (symtab_node *referred_node,
619 gimple stmt, unsigned int lto_stmt_uid)
621 ipa_ref *r = NULL;
622 int i;
624 for (i = 0; iterate_reference (i, r); i++)
625 if (r->referred == referred_node
626 && !r->speculative
627 && ((stmt && r->stmt == stmt)
628 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
629 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
630 return r;
631 return NULL;
634 /* Remove all references that are associated with statement STMT. */
636 void
637 symtab_node::remove_stmt_references (gimple stmt)
639 ipa_ref *r = NULL;
640 int i = 0;
642 while (iterate_reference (i, r))
643 if (r->stmt == stmt)
644 r->remove_reference ();
645 else
646 i++;
649 /* Remove all stmt references in non-speculative references.
650 Those are not maintained during inlining & clonning.
651 The exception are speculative references that are updated along
652 with callgraph edges associated with them. */
654 void
655 symtab_node::clear_stmts_in_references (void)
657 ipa_ref *r = NULL;
658 int i;
660 for (i = 0; iterate_reference (i, r); i++)
661 if (!r->speculative)
663 r->stmt = NULL;
664 r->lto_stmt_uid = 0;
668 /* Remove all references in ref list. */
670 void
671 symtab_node::remove_all_references (void)
673 while (vec_safe_length (ref_list.references))
674 ref_list.references->last ().remove_reference ();
675 vec_free (ref_list.references);
678 /* Remove all referring items in ref list. */
680 void
681 symtab_node::remove_all_referring (void)
683 while (ref_list.referring.length ())
684 ref_list.referring.last ()->remove_reference ();
685 ref_list.referring.release ();
688 /* Dump references in ref list to FILE. */
690 void
691 symtab_node::dump_references (FILE *file)
693 ipa_ref *ref = NULL;
694 int i;
695 for (i = 0; iterate_reference (i, ref); i++)
697 fprintf (file, "%s/%i (%s)",
698 ref->referred->asm_name (),
699 ref->referred->order,
700 ipa_ref_use_name [ref->use]);
701 if (ref->speculative)
702 fprintf (file, " (speculative)");
704 fprintf (file, "\n");
707 /* Dump referring in list to FILE. */
709 void
710 symtab_node::dump_referring (FILE *file)
712 ipa_ref *ref = NULL;
713 int i;
714 for (i = 0; iterate_referring(i, ref); i++)
716 fprintf (file, "%s/%i (%s)",
717 ref->referring->asm_name (),
718 ref->referring->order,
719 ipa_ref_use_name [ref->use]);
720 if (ref->speculative)
721 fprintf (file, " (speculative)");
723 fprintf (file, "\n");
726 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
728 /* Dump base fields of symtab nodes to F. Not to be used directly. */
730 void
731 symtab_node::dump_base (FILE *f)
733 static const char * const visibility_types[] = {
734 "default", "protected", "hidden", "internal"
737 fprintf (f, "%s/%i (%s)", asm_name (), order, name ());
738 dump_addr (f, " @", (void *)this);
739 fprintf (f, "\n Type: %s", symtab_type_names[type]);
741 if (definition)
742 fprintf (f, " definition");
743 if (analyzed)
744 fprintf (f, " analyzed");
745 if (alias)
746 fprintf (f, " alias");
747 if (weakref)
748 fprintf (f, " weakref");
749 if (cpp_implicit_alias)
750 fprintf (f, " cpp_implicit_alias");
751 if (alias_target)
752 fprintf (f, " target:%s",
753 DECL_P (alias_target)
754 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
755 (alias_target))
756 : IDENTIFIER_POINTER (alias_target));
757 if (body_removed)
758 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
759 fprintf (f, "\n Visibility:");
760 if (in_other_partition)
761 fprintf (f, " in_other_partition");
762 if (used_from_other_partition)
763 fprintf (f, " used_from_other_partition");
764 if (force_output)
765 fprintf (f, " force_output");
766 if (forced_by_abi)
767 fprintf (f, " forced_by_abi");
768 if (externally_visible)
769 fprintf (f, " externally_visible");
770 if (no_reorder)
771 fprintf (f, " no_reorder");
772 if (resolution != LDPR_UNKNOWN)
773 fprintf (f, " %s",
774 ld_plugin_symbol_resolution_names[(int)resolution]);
775 if (TREE_ASM_WRITTEN (decl))
776 fprintf (f, " asm_written");
777 if (DECL_EXTERNAL (decl))
778 fprintf (f, " external");
779 if (TREE_PUBLIC (decl))
780 fprintf (f, " public");
781 if (DECL_COMMON (decl))
782 fprintf (f, " common");
783 if (DECL_WEAK (decl))
784 fprintf (f, " weak");
785 if (DECL_DLLIMPORT_P (decl))
786 fprintf (f, " dll_import");
787 if (DECL_COMDAT (decl))
788 fprintf (f, " comdat");
789 if (get_comdat_group ())
790 fprintf (f, " comdat_group:%s",
791 IDENTIFIER_POINTER (get_comdat_group_id ()));
792 if (DECL_ONE_ONLY (decl))
793 fprintf (f, " one_only");
794 if (get_section ())
795 fprintf (f, " section:%s",
796 get_section ());
797 if (implicit_section)
798 fprintf (f," (implicit_section)");
799 if (DECL_VISIBILITY_SPECIFIED (decl))
800 fprintf (f, " visibility_specified");
801 if (DECL_VISIBILITY (decl))
802 fprintf (f, " visibility:%s",
803 visibility_types [DECL_VISIBILITY (decl)]);
804 if (DECL_VIRTUAL_P (decl))
805 fprintf (f, " virtual");
806 if (DECL_ARTIFICIAL (decl))
807 fprintf (f, " artificial");
808 if (TREE_CODE (decl) == FUNCTION_DECL)
810 if (DECL_STATIC_CONSTRUCTOR (decl))
811 fprintf (f, " constructor");
812 if (DECL_STATIC_DESTRUCTOR (decl))
813 fprintf (f, " destructor");
815 fprintf (f, "\n");
817 if (same_comdat_group)
818 fprintf (f, " Same comdat group as: %s/%i\n",
819 same_comdat_group->asm_name (),
820 same_comdat_group->order);
821 if (next_sharing_asm_name)
822 fprintf (f, " next sharing asm name: %i\n",
823 next_sharing_asm_name->order);
824 if (previous_sharing_asm_name)
825 fprintf (f, " previous sharing asm name: %i\n",
826 previous_sharing_asm_name->order);
828 if (address_taken)
829 fprintf (f, " Address is taken.\n");
830 if (aux)
832 fprintf (f, " Aux:");
833 dump_addr (f, " @", (void *)aux);
836 fprintf (f, " References: ");
837 dump_references (f);
838 fprintf (f, " Referring: ");
839 dump_referring (f);
840 if (lto_file_data)
841 fprintf (f, " Read from file: %s\n",
842 lto_file_data->file_name);
845 /* Dump symtab node to F. */
847 void
848 symtab_node::dump (FILE *f)
850 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
851 cnode->dump (f);
852 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
853 vnode->dump (f);
856 /* Dump symbol table to F. */
858 void
859 symtab_node::dump_table (FILE *f)
861 symtab_node *node;
862 fprintf (f, "Symbol table:\n\n");
863 FOR_EACH_SYMBOL (node)
864 node->dump (f);
868 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
869 Return NULL if there's no such node. */
871 symtab_node *
872 symtab_node::get_for_asmname (const_tree asmname)
874 symtab_node *node;
876 symtab->symtab_initialize_asm_name_hash ();
877 hashval_t hash = symtab->decl_assembler_name_hash (asmname);
878 symtab_node **slot
879 = symtab->assembler_name_hash->find_slot_with_hash (asmname, hash,
880 NO_INSERT);
882 if (slot)
884 node = *slot;
885 return node;
887 return NULL;
890 /* Dump symtab node NODE to stderr. */
892 DEBUG_FUNCTION void
893 symtab_node::debug (void)
895 dump (stderr);
898 /* Verify common part of symtab nodes. */
900 DEBUG_FUNCTION bool
901 symtab_node::verify_base (void)
903 bool error_found = false;
904 symtab_node *hashed_node;
906 if (is_a <cgraph_node *> (this))
908 if (TREE_CODE (decl) != FUNCTION_DECL)
910 error ("function symbol is not function");
911 error_found = true;
914 else if (is_a <varpool_node *> (this))
916 if (TREE_CODE (decl) != VAR_DECL)
918 error ("variable symbol is not variable");
919 error_found = true;
922 else
924 error ("node has unknown type");
925 error_found = true;
928 if (symtab->state != LTO_STREAMING)
930 hashed_node = symtab_node::get (decl);
931 if (!hashed_node)
933 error ("node not found node->decl->decl_with_vis.symtab_node");
934 error_found = true;
936 if (hashed_node != this
937 && (!is_a <cgraph_node *> (this)
938 || !dyn_cast <cgraph_node *> (this)->clone_of
939 || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl))
941 error ("node differs from node->decl->decl_with_vis.symtab_node");
942 error_found = true;
945 if (symtab->assembler_name_hash)
947 hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
948 if (hashed_node && hashed_node->previous_sharing_asm_name)
950 error ("assembler name hash list corrupted");
951 error_found = true;
953 while (hashed_node)
955 if (hashed_node == this)
956 break;
957 hashed_node = hashed_node->next_sharing_asm_name;
959 if (!hashed_node
960 && !(is_a <varpool_node *> (this)
961 || DECL_HARD_REGISTER (decl)))
963 error ("node not found in symtab assembler name hash");
964 error_found = true;
967 if (previous_sharing_asm_name
968 && previous_sharing_asm_name->next_sharing_asm_name != this)
970 error ("double linked list of assembler names corrupted");
971 error_found = true;
973 if (body_removed && definition)
975 error ("node has body_removed but is definition");
976 error_found = true;
978 if (analyzed && !definition)
980 error ("node is analyzed byt it is not a definition");
981 error_found = true;
983 if (cpp_implicit_alias && !alias)
985 error ("node is alias but not implicit alias");
986 error_found = true;
988 if (alias && !definition && !weakref)
990 error ("node is alias but not definition");
991 error_found = true;
993 if (weakref && !alias)
995 error ("node is weakref but not an alias");
996 error_found = true;
998 if (same_comdat_group)
1000 symtab_node *n = same_comdat_group;
1002 if (!n->get_comdat_group ())
1004 error ("node is in same_comdat_group list but has no comdat_group");
1005 error_found = true;
1007 if (n->get_comdat_group () != get_comdat_group ())
1009 error ("same_comdat_group list across different groups");
1010 error_found = true;
1012 if (n->type != type)
1014 error ("mixing different types of symbol in same comdat groups is not supported");
1015 error_found = true;
1017 if (n == this)
1019 error ("node is alone in a comdat group");
1020 error_found = true;
1024 if (!n->same_comdat_group)
1026 error ("same_comdat_group is not a circular list");
1027 error_found = true;
1028 break;
1030 n = n->same_comdat_group;
1032 while (n != this);
1033 if (comdat_local_p ())
1035 ipa_ref *ref = NULL;
1037 for (int i = 0; iterate_referring (i, ref); ++i)
1039 if (!in_same_comdat_group_p (ref->referring))
1041 error ("comdat-local symbol referred to by %s outside its "
1042 "comdat",
1043 identifier_to_locale (ref->referring->name()));
1044 error_found = true;
1049 if (implicit_section && !get_section ())
1051 error ("implicit_section flag is set but section isn't");
1052 error_found = true;
1054 if (get_section () && get_comdat_group ()
1055 && !implicit_section
1056 && !lookup_attribute ("section", DECL_ATTRIBUTES (decl)))
1058 error ("Both section and comdat group is set");
1059 error_found = true;
1061 /* TODO: Add string table for sections, so we do not keep holding duplicated
1062 strings. */
1063 if (alias && definition
1064 && get_section () != get_alias_target ()->get_section ()
1065 && (!get_section()
1066 || !get_alias_target ()->get_section ()
1067 || strcmp (get_section(),
1068 get_alias_target ()->get_section ())))
1070 error ("Alias and target's section differs");
1071 get_alias_target ()->dump (stderr);
1072 error_found = true;
1074 if (alias && definition
1075 && get_comdat_group () != get_alias_target ()->get_comdat_group ())
1077 error ("Alias and target's comdat groups differs");
1078 get_alias_target ()->dump (stderr);
1079 error_found = true;
1082 return error_found;
1085 /* Verify consistency of NODE. */
1087 DEBUG_FUNCTION void
1088 symtab_node::verify (void)
1090 if (seen_error ())
1091 return;
1093 timevar_push (TV_CGRAPH_VERIFY);
1094 if (cgraph_node *node = dyn_cast <cgraph_node *> (this))
1095 node->verify_node ();
1096 else
1097 if (verify_base ())
1099 debug ();
1100 internal_error ("symtab_node::verify failed");
1102 timevar_pop (TV_CGRAPH_VERIFY);
1105 /* Verify symbol table for internal consistency. */
1107 DEBUG_FUNCTION void
1108 symtab_node::verify_symtab_nodes (void)
1110 symtab_node *node;
1111 hash_map<tree, symtab_node *> comdat_head_map (251);
1113 FOR_EACH_SYMBOL (node)
1115 node->verify ();
1116 if (node->get_comdat_group ())
1118 symtab_node **entry, *s;
1119 bool existed;
1121 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1122 &existed);
1123 if (!existed)
1124 *entry = node;
1125 else if (!DECL_EXTERNAL (node->decl))
1127 for (s = (*entry)->same_comdat_group;
1128 s != NULL && s != node && s != *entry;
1129 s = s->same_comdat_group)
1131 if (!s || s == *entry)
1133 error ("Two symbols with same comdat_group are not linked by "
1134 "the same_comdat_group list.");
1135 (*entry)->debug ();
1136 node->debug ();
1137 internal_error ("symtab_node::verify failed");
1144 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1145 but other code such as notice_global_symbol generates rtl. */
1147 void
1148 symtab_node::make_decl_local (void)
1150 rtx rtl, symbol;
1152 /* Avoid clearing comdat_groups on comdat-local decls. */
1153 if (TREE_PUBLIC (decl) == 0)
1154 return;
1156 if (TREE_CODE (decl) == VAR_DECL)
1158 DECL_COMMON (decl) = 0;
1159 /* ADDRESSABLE flag is not defined for public symbols. */
1160 TREE_ADDRESSABLE (decl) = 1;
1162 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1164 DECL_COMDAT (decl) = 0;
1165 DECL_WEAK (decl) = 0;
1166 DECL_EXTERNAL (decl) = 0;
1167 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1168 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1169 TREE_PUBLIC (decl) = 0;
1170 DECL_DLLIMPORT_P (decl) = 0;
1171 if (!DECL_RTL_SET_P (decl))
1172 return;
1174 /* Update rtl flags. */
1175 make_decl_rtl (decl);
1177 rtl = DECL_RTL (decl);
1178 if (!MEM_P (rtl))
1179 return;
1181 symbol = XEXP (rtl, 0);
1182 if (GET_CODE (symbol) != SYMBOL_REF)
1183 return;
1185 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1188 /* Walk the alias chain to return the symbol NODE is alias of.
1189 If NODE is not an alias, return NODE.
1190 Assumes NODE is known to be alias. */
1192 symtab_node *
1193 symtab_node::ultimate_alias_target_1 (enum availability *availability)
1195 bool weakref_p = false;
1197 /* To determine visibility of the target, we follow ELF semantic of aliases.
1198 Here alias is an alternative assembler name of a given definition. Its
1199 availability prevails the availability of its target (i.e. static alias of
1200 weak definition is available.
1202 Weakref is a different animal (and not part of ELF per se). It is just
1203 alternative name of a given symbol used within one complation unit
1204 and is translated prior hitting the object file. It inherits the
1205 visibility of its target (i.e. weakref of non-overwritable definition
1206 is non-overwritable, while weakref of weak definition is weak).
1208 If we ever get into supporting targets with different semantics, a target
1209 hook will be needed here. */
1211 if (availability)
1213 weakref_p = weakref;
1214 if (!weakref_p)
1215 *availability = get_availability ();
1216 else
1217 *availability = AVAIL_LOCAL;
1220 symtab_node *node = this;
1221 while (node)
1223 if (node->alias && node->analyzed)
1224 node = node->get_alias_target ();
1225 else
1227 if (!availability)
1229 else if (node->analyzed)
1231 if (weakref_p)
1233 enum availability a = node->get_availability ();
1234 if (a < *availability)
1235 *availability = a;
1238 else
1239 *availability = AVAIL_NOT_AVAILABLE;
1240 return node;
1242 if (node && availability && weakref_p)
1244 enum availability a = node->get_availability ();
1245 if (a < *availability)
1246 *availability = a;
1247 weakref_p = node->weakref;
1250 if (availability)
1251 *availability = AVAIL_NOT_AVAILABLE;
1252 return NULL;
1255 /* C++ FE sometimes change linkage flags after producing same body aliases.
1257 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1258 are obviously equivalent. The way it is doing so is however somewhat
1259 kludgy and interferes with the visibility code. As a result we need to
1260 copy the visibility from the target to get things right. */
1262 void
1263 symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target)
1265 if (is_a <cgraph_node *> (this))
1267 DECL_DECLARED_INLINE_P (decl)
1268 = DECL_DECLARED_INLINE_P (target->decl);
1269 DECL_DISREGARD_INLINE_LIMITS (decl)
1270 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1272 /* FIXME: It is not really clear why those flags should not be copied for
1273 functions, too. */
1274 else
1276 DECL_WEAK (decl) = DECL_WEAK (target->decl);
1277 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1278 DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl);
1280 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (target->decl);
1281 if (TREE_PUBLIC (decl))
1283 tree group;
1285 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1286 DECL_COMDAT (decl) = DECL_COMDAT (target->decl);
1287 group = target->get_comdat_group ();
1288 set_comdat_group (group);
1289 if (group && !same_comdat_group)
1290 add_to_same_comdat_group (target);
1292 externally_visible = target->externally_visible;
1295 /* Set section, do not recurse into aliases.
1296 When one wants to change section of symbol and its aliases,
1297 use set_section. */
1299 void
1300 symtab_node::set_section_for_node (const char *section)
1302 const char *current = get_section ();
1303 section_hash_entry **slot;
1305 if (current == section
1306 || (current && section
1307 && !strcmp (current, section)))
1308 return;
1310 if (current)
1312 x_section->ref_count--;
1313 if (!x_section->ref_count)
1315 hashval_t hash = htab_hash_string (x_section->name);
1316 slot = symtab->section_hash->find_slot_with_hash (x_section->name,
1317 hash, INSERT);
1318 ggc_free (x_section);
1319 symtab->section_hash->clear_slot (slot);
1321 x_section = NULL;
1323 if (!section)
1325 implicit_section = false;
1326 return;
1328 if (!symtab->section_hash)
1329 symtab->section_hash = hash_table<section_name_hasher>::create_ggc (10);
1330 slot = symtab->section_hash->find_slot_with_hash (section,
1331 htab_hash_string (section),
1332 INSERT);
1333 if (*slot)
1334 x_section = (section_hash_entry *)*slot;
1335 else
1337 int len = strlen (section);
1338 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1339 x_section->name = ggc_vec_alloc<char> (len + 1);
1340 memcpy (x_section->name, section, len + 1);
1342 x_section->ref_count++;
1345 /* Worker for set_section. */
1347 bool
1348 symtab_node::set_section (symtab_node *n, void *s)
1350 n->set_section_for_node ((char *)s);
1351 return false;
1354 /* Set section of symbol and its aliases. */
1356 void
1357 symtab_node::set_section (const char *section)
1359 gcc_assert (!this->alias);
1360 call_for_symbol_and_aliases
1361 (symtab_node::set_section, const_cast<char *>(section), true);
1364 /* Return the initialization priority. */
1366 priority_type
1367 symtab_node::get_init_priority ()
1369 if (!this->in_init_priority_hash)
1370 return DEFAULT_INIT_PRIORITY;
1372 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1373 return h ? h->init : DEFAULT_INIT_PRIORITY;
1376 /* Return the finalization priority. */
1378 priority_type
1379 cgraph_node::get_fini_priority ()
1381 if (!this->in_init_priority_hash)
1382 return DEFAULT_INIT_PRIORITY;
1383 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1384 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1387 /* Return the initialization and finalization priority information for
1388 DECL. If there is no previous priority information, a freshly
1389 allocated structure is returned. */
1391 symbol_priority_map *
1392 symtab_node::priority_info (void)
1394 if (!symtab->init_priority_hash)
1395 symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13);
1397 bool existed;
1398 symbol_priority_map *h
1399 = &symtab->init_priority_hash->get_or_insert (this, &existed);
1400 if (!existed)
1402 h->init = DEFAULT_INIT_PRIORITY;
1403 h->fini = DEFAULT_INIT_PRIORITY;
1404 in_init_priority_hash = true;
1407 return h;
1410 /* Set initialization priority to PRIORITY. */
1412 void
1413 symtab_node::set_init_priority (priority_type priority)
1415 symbol_priority_map *h;
1417 if (is_a <cgraph_node *> (this))
1418 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1420 if (priority == DEFAULT_INIT_PRIORITY)
1422 gcc_assert (get_init_priority() == priority);
1423 return;
1425 h = priority_info ();
1426 h->init = priority;
1429 /* Set fialization priority to PRIORITY. */
1431 void
1432 cgraph_node::set_fini_priority (priority_type priority)
1434 symbol_priority_map *h;
1436 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1438 if (priority == DEFAULT_INIT_PRIORITY)
1440 gcc_assert (get_fini_priority() == priority);
1441 return;
1443 h = priority_info ();
1444 h->fini = priority;
1447 /* Worker for symtab_resolve_alias. */
1449 bool
1450 symtab_node::set_implicit_section (symtab_node *n,
1451 void *data ATTRIBUTE_UNUSED)
1453 n->implicit_section = true;
1454 return false;
1457 /* Add reference recording that symtab node is alias of TARGET.
1458 The function can fail in the case of aliasing cycles; in this case
1459 it returns false. */
1461 bool
1462 symtab_node::resolve_alias (symtab_node *target)
1464 symtab_node *n;
1466 gcc_assert (!analyzed && !vec_safe_length (ref_list.references));
1468 /* Never let cycles to creep into the symbol table alias references;
1469 those will make alias walkers to be infinite. */
1470 for (n = target; n && n->alias;
1471 n = n->analyzed ? n->get_alias_target () : NULL)
1472 if (n == this)
1474 if (is_a <cgraph_node *> (this))
1475 error ("function %q+D part of alias cycle", decl);
1476 else if (is_a <varpool_node *> (this))
1477 error ("variable %q+D part of alias cycle", decl);
1478 else
1479 gcc_unreachable ();
1480 alias = false;
1481 return false;
1484 /* "analyze" the node - i.e. mark the reference. */
1485 definition = true;
1486 alias = true;
1487 analyzed = true;
1488 create_reference (target, IPA_REF_ALIAS, NULL);
1490 /* Add alias into the comdat group of its target unless it is already there. */
1491 if (same_comdat_group)
1492 remove_from_same_comdat_group ();
1493 set_comdat_group (NULL);
1494 if (target->get_comdat_group ())
1495 add_to_same_comdat_group (target);
1497 if ((get_section () != target->get_section ()
1498 || target->get_comdat_group ()) && get_section () && !implicit_section)
1500 error ("section of alias %q+D must match section of its target", decl);
1502 call_for_symbol_and_aliases (symtab_node::set_section,
1503 const_cast<char *>(target->get_section ()), true);
1504 if (target->implicit_section)
1505 call_for_symbol_and_aliases (set_implicit_section, NULL, true);
1507 /* Alias targets become redundant after alias is resolved into an reference.
1508 We do not want to keep it around or we would have to mind updating them
1509 when renaming symbols. */
1510 alias_target = NULL;
1512 if (cpp_implicit_alias && symtab->state >= CONSTRUCTION)
1513 fixup_same_cpp_alias_visibility (target);
1515 /* If alias has address taken, so does the target. */
1516 if (address_taken)
1517 target->ultimate_alias_target ()->address_taken = true;
1519 /* All non-weakref aliases of THIS are now in fact aliases of TARGET. */
1520 ipa_ref *ref;
1521 for (unsigned i = 0; iterate_direct_aliases (i, ref);)
1523 struct symtab_node *alias_alias = ref->referring;
1524 if (!alias_alias->weakref)
1526 alias_alias->remove_all_references ();
1527 alias_alias->create_reference (target, IPA_REF_ALIAS, NULL);
1529 else i++;
1531 return true;
1534 /* Worker searching noninterposable alias. */
1536 bool
1537 symtab_node::noninterposable_alias (symtab_node *node, void *data)
1539 if (decl_binds_to_current_def_p (node->decl))
1541 symtab_node *fn = node->ultimate_alias_target ();
1543 /* Ensure that the alias is well formed this may not be the case
1544 of user defined aliases and currently it is not always the case
1545 of C++ same body aliases (that is a bug). */
1546 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
1547 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
1548 || (TREE_CODE (node->decl) == FUNCTION_DECL
1549 && flags_from_decl_or_type (node->decl)
1550 != flags_from_decl_or_type (fn->decl))
1551 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
1552 return false;
1553 *(symtab_node **)data = node;
1554 return true;
1556 return false;
1559 /* If node can not be overwriten by static or dynamic linker to point to
1560 different definition, return NODE. Otherwise look for alias with such
1561 property and if none exists, introduce new one. */
1563 symtab_node *
1564 symtab_node::noninterposable_alias (void)
1566 tree new_decl;
1567 symtab_node *new_node = NULL;
1569 /* First try to look up existing alias or base object
1570 (if that is already non-overwritable). */
1571 symtab_node *node = ultimate_alias_target ();
1572 gcc_assert (!node->alias && !node->weakref);
1573 node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias,
1574 (void *)&new_node, true);
1575 if (new_node)
1576 return new_node;
1577 #ifndef ASM_OUTPUT_DEF
1578 /* If aliases aren't supported by the assembler, fail. */
1579 return NULL;
1580 #endif
1582 /* Otherwise create a new one. */
1583 new_decl = copy_node (node->decl);
1584 DECL_DLLIMPORT_P (new_decl) = 0;
1585 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1586 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1587 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1588 DECL_INITIAL (new_decl) = NULL;
1589 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1590 SET_DECL_RTL (new_decl, NULL);
1592 /* Update the properties. */
1593 DECL_EXTERNAL (new_decl) = 0;
1594 TREE_PUBLIC (new_decl) = 0;
1595 DECL_COMDAT (new_decl) = 0;
1596 DECL_WEAK (new_decl) = 0;
1598 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1599 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1600 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1602 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1603 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1604 new_node = cgraph_node::create_alias (new_decl, node->decl);
1606 else
1608 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1609 DECL_INITIAL (new_decl) = error_mark_node;
1610 new_node = varpool_node::create_alias (new_decl, node->decl);
1612 new_node->resolve_alias (node);
1613 gcc_assert (decl_binds_to_current_def_p (new_decl)
1614 && targetm.binds_local_p (new_decl));
1615 return new_node;
1618 /* Return true if symtab node and TARGET represents
1619 semantically equivalent symbols. */
1621 bool
1622 symtab_node::semantically_equivalent_p (symtab_node *target)
1624 enum availability avail;
1625 symtab_node *ba;
1626 symtab_node *bb;
1628 /* Equivalent functions are equivalent. */
1629 if (decl == target->decl)
1630 return true;
1632 /* If symbol is not overwritable by different implementation,
1633 walk to the base object it defines. */
1634 ba = ultimate_alias_target (&avail);
1635 if (avail >= AVAIL_AVAILABLE)
1637 if (target == ba)
1638 return true;
1640 else
1641 ba = this;
1642 bb = target->ultimate_alias_target (&avail);
1643 if (avail >= AVAIL_AVAILABLE)
1645 if (this == bb)
1646 return true;
1648 else
1649 bb = target;
1650 return bb == ba;
1653 /* Classify symbol symtab node for partitioning. */
1655 enum symbol_partitioning_class
1656 symtab_node::get_partitioning_class (void)
1658 /* Inline clones are always duplicated.
1659 This include external delcarations. */
1660 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
1662 if (DECL_ABSTRACT_P (decl))
1663 return SYMBOL_EXTERNAL;
1665 if (cnode && cnode->global.inlined_to)
1666 return SYMBOL_DUPLICATE;
1668 /* Weakref aliases are always duplicated. */
1669 if (weakref)
1670 return SYMBOL_DUPLICATE;
1672 /* External declarations are external. */
1673 if (DECL_EXTERNAL (decl))
1674 return SYMBOL_EXTERNAL;
1676 if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
1678 if (alias && definition && !ultimate_alias_target ()->definition)
1679 return SYMBOL_EXTERNAL;
1680 /* Constant pool references use local symbol names that can not
1681 be promoted global. We should never put into a constant pool
1682 objects that can not be duplicated across partitions. */
1683 if (DECL_IN_CONSTANT_POOL (decl))
1684 return SYMBOL_DUPLICATE;
1685 if (DECL_HARD_REGISTER (decl))
1686 return SYMBOL_DUPLICATE;
1687 gcc_checking_assert (vnode->definition);
1689 /* Functions that are cloned may stay in callgraph even if they are unused.
1690 Handle them as external; compute_ltrans_boundary take care to make
1691 proper things to happen (i.e. to make them appear in the boundary but
1692 with body streamed, so clone can me materialized). */
1693 else if (!dyn_cast <cgraph_node *> (this)->function_symbol ()->definition)
1694 return SYMBOL_EXTERNAL;
1696 /* Linker discardable symbols are duplicated to every use unless they are
1697 keyed. */
1698 if (DECL_ONE_ONLY (decl)
1699 && !force_output
1700 && !forced_by_abi
1701 && !used_from_object_file_p ())
1702 return SYMBOL_DUPLICATE;
1704 return SYMBOL_PARTITION;
1707 /* Return true when symbol is known to be non-zero. */
1709 bool
1710 symtab_node::nonzero_address ()
1712 /* Weakrefs may be NULL when their target is not defined. */
1713 if (alias && weakref)
1715 if (analyzed)
1717 symtab_node *target = ultimate_alias_target ();
1719 if (target->alias && target->weakref)
1720 return false;
1721 /* We can not recurse to target::nonzero. It is possible that the
1722 target is used only via the alias.
1723 We may walk references and look for strong use, but we do not know
1724 if this strong use will survive to final binary, so be
1725 conservative here.
1726 ??? Maybe we could do the lookup during late optimization that
1727 could be useful to eliminate the NULL pointer checks in LTO
1728 programs. */
1729 if (target->definition && !DECL_EXTERNAL (target->decl))
1730 return true;
1731 if (target->resolution != LDPR_UNKNOWN
1732 && target->resolution != LDPR_UNDEF
1733 && flag_delete_null_pointer_checks)
1734 return true;
1735 return false;
1737 else
1738 return false;
1741 /* With !flag_delete_null_pointer_checks we assume that symbols may
1742 bind to NULL. This is on by default on embedded targets only.
1744 Otherwise all non-WEAK symbols must be defined and thus non-NULL or
1745 linking fails. Important case of WEAK we want to do well are comdats.
1746 Those are handled by later check for definition.
1748 When parsing, beware the cases when WEAK attribute is added later. */
1749 if (!DECL_WEAK (decl)
1750 && flag_delete_null_pointer_checks)
1752 refuse_visibility_changes = true;
1753 return true;
1756 /* If target is defined and not extern, we know it will be output and thus
1757 it will bind to non-NULL.
1758 Play safe for flag_delete_null_pointer_checks where weak definition maye
1759 be re-defined by NULL. */
1760 if (definition && !DECL_EXTERNAL (decl)
1761 && (flag_delete_null_pointer_checks || !DECL_WEAK (decl)))
1763 if (!DECL_WEAK (decl))
1764 refuse_visibility_changes = true;
1765 return true;
1768 /* As the last resort, check the resolution info. */
1769 if (resolution != LDPR_UNKNOWN
1770 && resolution != LDPR_UNDEF
1771 && flag_delete_null_pointer_checks)
1772 return true;
1773 return false;
1776 /* Return 0 if symbol is known to have different address than S2,
1777 Return 1 if symbol is known to have same address as S2,
1778 return 2 otherwise. */
1780 symtab_node::equal_address_to (symtab_node *s2)
1782 enum availability avail1, avail2;
1784 /* A Shortcut: equivalent symbols are always equivalent. */
1785 if (this == s2)
1786 return 1;
1788 /* For non-interposable aliases, lookup and compare their actual definitions.
1789 Also check if the symbol needs to bind to given definition. */
1790 symtab_node *rs1 = ultimate_alias_target (&avail1);
1791 symtab_node *rs2 = s2->ultimate_alias_target (&avail2);
1792 bool binds_local1 = rs1->analyzed && decl_binds_to_current_def_p (this->decl);
1793 bool binds_local2 = rs2->analyzed && decl_binds_to_current_def_p (s2->decl);
1794 bool really_binds_local1 = binds_local1;
1795 bool really_binds_local2 = binds_local2;
1797 /* Addresses of vtables and virtual functions can not be used by user
1798 code and are used only within speculation. In this case we may make
1799 symbol equivalent to its alias even if interposition may break this
1800 rule. Doing so will allow us to turn speculative inlining into
1801 non-speculative more agressively. */
1802 if (DECL_VIRTUAL_P (this->decl) && avail1 >= AVAIL_AVAILABLE)
1803 binds_local1 = true;
1804 if (DECL_VIRTUAL_P (s2->decl) && avail2 >= AVAIL_AVAILABLE)
1805 binds_local2 = true;
1807 /* If both definitions are available we know that even if they are bound
1808 to other unit they must be defined same way and therefore we can use
1809 equivalence test. */
1810 if (rs1 != rs2 && avail1 >= AVAIL_AVAILABLE && avail2 >= AVAIL_AVAILABLE)
1811 binds_local1 = binds_local2 = true;
1813 if ((binds_local1 ? rs1 : this)
1814 == (binds_local2 ? rs2 : s2))
1816 /* We made use of the fact that alias is not weak. */
1817 if (binds_local1 && rs1 != this)
1818 refuse_visibility_changes = true;
1819 if (binds_local2 && rs2 != s2)
1820 s2->refuse_visibility_changes = true;
1821 return 1;
1824 /* If both symbols may resolve to NULL, we can not really prove them different. */
1825 if (!nonzero_address () && !s2->nonzero_address ())
1826 return 2;
1828 /* Except for NULL, functions and variables never overlap. */
1829 if (TREE_CODE (decl) != TREE_CODE (s2->decl))
1830 return 0;
1832 /* If one of the symbols is unresolved alias, punt. */
1833 if (rs1->alias || rs2->alias)
1834 return 2;
1836 /* If we have a non-interposale definition of at least one of the symbols
1837 and the other symbol is different, we know other unit can not interpose
1838 it to the first symbol; all aliases of the definition needs to be
1839 present in the current unit. */
1840 if (((really_binds_local1 || really_binds_local2)
1841 /* If we have both definitions and they are different, we know they
1842 will be different even in units they binds to. */
1843 || (binds_local1 && binds_local2))
1844 && rs1 != rs2)
1846 /* We make use of the fact that one symbol is not alias of the other
1847 and that the definition is non-interposable. */
1848 refuse_visibility_changes = true;
1849 s2->refuse_visibility_changes = true;
1850 rs1->refuse_visibility_changes = true;
1851 rs2->refuse_visibility_changes = true;
1852 return 0;
1855 /* TODO: Alias oracle basically assume that addresses of global variables
1856 are different unless they are declared as alias of one to another.
1857 We probably should be consistent and use this fact here, too, and update
1858 alias oracle to use this predicate. */
1860 return 2;
1863 /* Worker for call_for_symbol_and_aliases. */
1865 bool
1866 symtab_node::call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *,
1867 void *),
1868 void *data,
1869 bool include_overwritable)
1871 ipa_ref *ref;
1872 FOR_EACH_ALIAS (this, ref)
1874 symtab_node *alias = ref->referring;
1875 if (include_overwritable
1876 || alias->get_availability () > AVAIL_INTERPOSABLE)
1877 if (alias->call_for_symbol_and_aliases (callback, data,
1878 include_overwritable))
1879 return true;
1881 return false;
1884 /* Return ture if address of N is possibly compared. */
1886 static bool
1887 address_matters_1 (symtab_node *n, void *)
1889 struct ipa_ref *ref;
1891 if (!n->address_can_be_compared_p ())
1892 return false;
1893 if (n->externally_visible || n->force_output)
1894 return true;
1896 for (unsigned int i = 0; n->iterate_referring (i, ref); i++)
1897 if (ref->address_matters_p ())
1898 return true;
1899 return false;
1902 /* Return true if symbol's address may possibly be compared to other
1903 symbol's address. */
1905 bool
1906 symtab_node::address_matters_p ()
1908 gcc_assert (!alias);
1909 return call_for_symbol_and_aliases (address_matters_1, NULL, true);
1912 /* Return ture if symbol's alignment may be increased. */
1914 bool
1915 symtab_node::can_increase_alignment_p (void)
1917 symtab_node *target = ultimate_alias_target ();
1919 /* For now support only variables. */
1920 if (TREE_CODE (decl) != VAR_DECL)
1921 return false;
1923 /* With -fno-toplevel-reorder we may have already output the constant. */
1924 if (TREE_ASM_WRITTEN (target->decl))
1925 return false;
1927 /* If target is already placed in an anchor, we can not touch its
1928 alignment. */
1929 if (DECL_RTL_SET_P (target->decl)
1930 && MEM_P (DECL_RTL (target->decl))
1931 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (DECL_RTL (target->decl), 0)))
1932 return false;
1934 /* Constant pool entries may be shared. */
1935 if (DECL_IN_CONSTANT_POOL (target->decl))
1936 return false;
1938 /* We cannot change alignment of symbols that may bind to symbols
1939 in other translation unit that may contain a definition with lower
1940 alignment. */
1941 if (!decl_binds_to_current_def_p (decl))
1942 return false;
1944 /* When compiling partition, be sure the symbol is not output by other
1945 partition. */
1946 if (flag_ltrans
1947 && (target->in_other_partition
1948 || target->get_partitioning_class () == SYMBOL_DUPLICATE))
1949 return false;
1951 /* Do not override the alignment as specified by the ABI when the used
1952 attribute is set. */
1953 if (DECL_PRESERVE_P (decl) || DECL_PRESERVE_P (target->decl))
1954 return false;
1956 /* Do not override explicit alignment set by the user when an explicit
1957 section name is also used. This is a common idiom used by many
1958 software projects. */
1959 if (DECL_SECTION_NAME (target->decl) != NULL && !target->implicit_section)
1960 return false;
1962 return true;
1965 /* Worker for symtab_node::increase_alignment. */
1967 static bool
1968 increase_alignment_1 (symtab_node *n, void *v)
1970 unsigned int align = (size_t)v;
1971 if (DECL_ALIGN (n->decl) < align
1972 && n->can_increase_alignment_p ())
1974 DECL_ALIGN (n->decl) = align;
1975 DECL_USER_ALIGN (n->decl) = 1;
1977 return false;
1980 /* Increase alignment of THIS to ALIGN. */
1982 void
1983 symtab_node::increase_alignment (unsigned int align)
1985 gcc_assert (can_increase_alignment_p () && align < MAX_OFILE_ALIGNMENT);
1986 ultimate_alias_target()->call_for_symbol_and_aliases (increase_alignment_1,
1987 (void *)(size_t) align,
1988 true);
1989 gcc_assert (DECL_ALIGN (decl) >= align);
1992 /* Helper for symtab_node::definition_alignment. */
1994 static bool
1995 get_alignment_1 (symtab_node *n, void *v)
1997 *((unsigned int *)v) = MAX (*((unsigned int *)v), DECL_ALIGN (n->decl));
1998 return false;
2001 /* Return desired alignment of the definition. This is NOT alignment useful
2002 to access THIS, because THIS may be interposable and DECL_ALIGN should
2003 be used instead. It however must be guaranteed when output definition
2004 of THIS. */
2006 unsigned int
2007 symtab_node::definition_alignment ()
2009 unsigned int align = 0;
2010 gcc_assert (!alias);
2011 call_for_symbol_and_aliases (get_alignment_1, &align, true);
2012 return align;