PR c++/65509
[official-gcc.git] / gcc / symtab.c
blob84a0981fab0796bde3c24f00ec8cf245349edded
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 "hash-set.h"
27 #include "machmode.h"
28 #include "vec.h"
29 #include "double-int.h"
30 #include "input.h"
31 #include "alias.h"
32 #include "symtab.h"
33 #include "wide-int.h"
34 #include "inchash.h"
35 #include "tree.h"
36 #include "fold-const.h"
37 #include "print-tree.h"
38 #include "varasm.h"
39 #include "hashtab.h"
40 #include "hard-reg-set.h"
41 #include "input.h"
42 #include "function.h"
43 #include "emit-rtl.h"
44 #include "predict.h"
45 #include "basic-block.h"
46 #include "tree-ssa-alias.h"
47 #include "internal-fn.h"
48 #include "gimple-expr.h"
49 #include "is-a.h"
50 #include "gimple.h"
51 #include "tree-inline.h"
52 #include "langhooks.h"
53 #include "hash-map.h"
54 #include "plugin-api.h"
55 #include "ipa-ref.h"
56 #include "cgraph.h"
57 #include "diagnostic.h"
58 #include "timevar.h"
59 #include "lto-streamer.h"
60 #include "output.h"
61 #include "ipa-utils.h"
62 #include "calls.h"
64 static const char *ipa_ref_use_name[] = {"read","write","addr","alias","chkp"};
66 const char * const ld_plugin_symbol_resolution_names[]=
68 "",
69 "undef",
70 "prevailing_def",
71 "prevailing_def_ironly",
72 "preempted_reg",
73 "preempted_ir",
74 "resolved_ir",
75 "resolved_exec",
76 "resolved_dyn",
77 "prevailing_def_ironly_exp"
80 /* Hash asmnames ignoring the user specified marks. */
82 hashval_t
83 symbol_table::decl_assembler_name_hash (const_tree asmname)
85 if (IDENTIFIER_POINTER (asmname)[0] == '*')
87 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
88 size_t ulp_len = strlen (user_label_prefix);
90 if (ulp_len == 0)
92 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
93 decl_str += ulp_len;
95 return htab_hash_string (decl_str);
98 return htab_hash_string (IDENTIFIER_POINTER (asmname));
102 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
104 bool
105 symbol_table::decl_assembler_name_equal (tree decl, const_tree asmname)
107 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
108 const char *decl_str;
109 const char *asmname_str;
110 bool test = false;
112 if (decl_asmname == asmname)
113 return true;
115 decl_str = IDENTIFIER_POINTER (decl_asmname);
116 asmname_str = IDENTIFIER_POINTER (asmname);
119 /* If the target assembler name was set by the user, things are trickier.
120 We have a leading '*' to begin with. After that, it's arguable what
121 is the correct thing to do with -fleading-underscore. Arguably, we've
122 historically been doing the wrong thing in assemble_alias by always
123 printing the leading underscore. Since we're not changing that, make
124 sure user_label_prefix follows the '*' before matching. */
125 if (decl_str[0] == '*')
127 size_t ulp_len = strlen (user_label_prefix);
129 decl_str ++;
131 if (ulp_len == 0)
132 test = true;
133 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
134 decl_str += ulp_len, test=true;
135 else
136 decl_str --;
138 if (asmname_str[0] == '*')
140 size_t ulp_len = strlen (user_label_prefix);
142 asmname_str ++;
144 if (ulp_len == 0)
145 test = true;
146 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
147 asmname_str += ulp_len, test=true;
148 else
149 asmname_str --;
152 if (!test)
153 return false;
154 return strcmp (decl_str, asmname_str) == 0;
158 /* Returns nonzero if P1 and P2 are equal. */
160 /* Insert NODE to assembler name hash. */
162 void
163 symbol_table::insert_to_assembler_name_hash (symtab_node *node,
164 bool with_clones)
166 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
167 return;
168 gcc_checking_assert (!node->previous_sharing_asm_name
169 && !node->next_sharing_asm_name);
170 if (assembler_name_hash)
172 symtab_node **aslot;
173 cgraph_node *cnode;
174 tree decl = node->decl;
176 tree name = DECL_ASSEMBLER_NAME (node->decl);
178 /* C++ FE can produce decls without associated assembler name and insert
179 them to symtab to hold section or TLS information. */
180 if (!name)
181 return;
183 hashval_t hash = decl_assembler_name_hash (name);
184 aslot = assembler_name_hash->find_slot_with_hash (name, hash, INSERT);
185 gcc_assert (*aslot != node);
186 node->next_sharing_asm_name = (symtab_node *)*aslot;
187 if (*aslot != NULL)
188 (*aslot)->previous_sharing_asm_name = node;
189 *aslot = node;
191 /* Update also possible inline clones sharing a decl. */
192 cnode = dyn_cast <cgraph_node *> (node);
193 if (cnode && cnode->clones && with_clones)
194 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
195 if (cnode->decl == decl)
196 insert_to_assembler_name_hash (cnode, true);
201 /* Remove NODE from assembler name hash. */
203 void
204 symbol_table::unlink_from_assembler_name_hash (symtab_node *node,
205 bool with_clones)
207 if (assembler_name_hash)
209 cgraph_node *cnode;
210 tree decl = node->decl;
212 if (node->next_sharing_asm_name)
213 node->next_sharing_asm_name->previous_sharing_asm_name
214 = node->previous_sharing_asm_name;
215 if (node->previous_sharing_asm_name)
217 node->previous_sharing_asm_name->next_sharing_asm_name
218 = node->next_sharing_asm_name;
220 else
222 tree name = DECL_ASSEMBLER_NAME (node->decl);
223 symtab_node **slot;
225 if (!name)
226 return;
228 hashval_t hash = decl_assembler_name_hash (name);
229 slot = assembler_name_hash->find_slot_with_hash (name, hash,
230 NO_INSERT);
231 gcc_assert (*slot == node);
232 if (!node->next_sharing_asm_name)
233 assembler_name_hash->clear_slot (slot);
234 else
235 *slot = node->next_sharing_asm_name;
237 node->next_sharing_asm_name = NULL;
238 node->previous_sharing_asm_name = NULL;
240 /* Update also possible inline clones sharing a decl. */
241 cnode = dyn_cast <cgraph_node *> (node);
242 if (cnode && cnode->clones && with_clones)
243 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
244 if (cnode->decl == decl)
245 unlink_from_assembler_name_hash (cnode, true);
249 /* Arrange node to be first in its entry of assembler_name_hash. */
251 void
252 symbol_table::symtab_prevail_in_asm_name_hash (symtab_node *node)
254 unlink_from_assembler_name_hash (node, false);
255 insert_to_assembler_name_hash (node, false);
258 /* Initalize asm name hash unless. */
260 void
261 symbol_table::symtab_initialize_asm_name_hash (void)
263 symtab_node *node;
264 if (!assembler_name_hash)
266 assembler_name_hash = hash_table<asmname_hasher>::create_ggc (10);
267 FOR_EACH_SYMBOL (node)
268 insert_to_assembler_name_hash (node, false);
272 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
274 void
275 symbol_table::change_decl_assembler_name (tree decl, tree name)
277 symtab_node *node = NULL;
279 /* We can have user ASM names on things, like global register variables, that
280 are not in the symbol table. */
281 if ((TREE_CODE (decl) == VAR_DECL
282 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
283 || TREE_CODE (decl) == FUNCTION_DECL)
284 node = symtab_node::get (decl);
285 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
287 SET_DECL_ASSEMBLER_NAME (decl, name);
288 if (node)
289 insert_to_assembler_name_hash (node, true);
291 else
293 if (name == DECL_ASSEMBLER_NAME (decl))
294 return;
296 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
297 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
298 : NULL);
299 if (node)
300 unlink_from_assembler_name_hash (node, true);
301 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
302 && DECL_RTL_SET_P (decl))
303 warning (0, "%D renamed after being referenced in assembly", decl);
305 SET_DECL_ASSEMBLER_NAME (decl, name);
306 if (alias)
308 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
309 TREE_CHAIN (name) = alias;
311 if (node)
312 insert_to_assembler_name_hash (node, true);
316 /* Hash sections by their names. */
318 hashval_t
319 section_name_hasher::hash (section_hash_entry *n)
321 return htab_hash_string (n->name);
324 /* Return true if section P1 name equals to P2. */
326 bool
327 section_name_hasher::equal (section_hash_entry *n1, const char *name)
329 return n1->name == name || !strcmp (n1->name, name);
332 /* Add node into symbol table. This function is not used directly, but via
333 cgraph/varpool node creation routines. */
335 void
336 symtab_node::register_symbol (void)
338 symtab->register_symbol (this);
340 if (!decl->decl_with_vis.symtab_node)
341 decl->decl_with_vis.symtab_node = this;
343 ref_list.clear ();
345 /* Be sure to do this last; C++ FE might create new nodes via
346 DECL_ASSEMBLER_NAME langhook! */
347 symtab->insert_to_assembler_name_hash (this, false);
350 /* Remove NODE from same comdat group. */
352 void
353 symtab_node::remove_from_same_comdat_group (void)
355 if (same_comdat_group)
357 symtab_node *prev;
358 for (prev = same_comdat_group;
359 prev->same_comdat_group != this;
360 prev = prev->same_comdat_group)
362 if (same_comdat_group == prev)
363 prev->same_comdat_group = NULL;
364 else
365 prev->same_comdat_group = same_comdat_group;
366 same_comdat_group = NULL;
367 set_comdat_group (NULL);
371 /* Remove node from symbol table. This function is not used directly, but via
372 cgraph/varpool node removal routines. */
374 void
375 symtab_node::unregister (void)
377 remove_all_references ();
378 remove_all_referring ();
380 /* Remove reference to section. */
381 set_section_for_node (NULL);
383 remove_from_same_comdat_group ();
385 symtab->unregister (this);
387 /* During LTO symtab merging we temporarily corrupt decl to symtab node
388 hash. */
389 gcc_assert (decl->decl_with_vis.symtab_node || in_lto_p);
390 if (decl->decl_with_vis.symtab_node == this)
392 symtab_node *replacement_node = NULL;
393 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
394 replacement_node = cnode->find_replacement ();
395 decl->decl_with_vis.symtab_node = replacement_node;
397 if (!is_a <varpool_node *> (this) || !DECL_HARD_REGISTER (decl))
398 symtab->unlink_from_assembler_name_hash (this, false);
399 if (in_init_priority_hash)
400 symtab->init_priority_hash->remove (this);
404 /* Remove symbol from symbol table. */
406 void
407 symtab_node::remove (void)
409 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
410 cnode->remove ();
411 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
412 vnode->remove ();
415 /* Add NEW_ to the same comdat group that OLD is in. */
417 void
418 symtab_node::add_to_same_comdat_group (symtab_node *old_node)
420 gcc_assert (old_node->get_comdat_group ());
421 gcc_assert (!same_comdat_group);
422 gcc_assert (this != old_node);
424 set_comdat_group (old_node->get_comdat_group ());
425 same_comdat_group = old_node;
426 if (!old_node->same_comdat_group)
427 old_node->same_comdat_group = this;
428 else
430 symtab_node *n;
431 for (n = old_node->same_comdat_group;
432 n->same_comdat_group != old_node;
433 n = n->same_comdat_group)
435 n->same_comdat_group = this;
439 /* Dissolve the same_comdat_group list in which NODE resides. */
441 void
442 symtab_node::dissolve_same_comdat_group_list (void)
444 symtab_node *n = this;
445 symtab_node *next;
447 if (!same_comdat_group)
448 return;
451 next = n->same_comdat_group;
452 n->same_comdat_group = NULL;
453 /* Clear comdat_group for comdat locals, since
454 make_decl_local doesn't. */
455 if (!TREE_PUBLIC (n->decl))
456 n->set_comdat_group (NULL);
457 n = next;
459 while (n != this);
462 /* Return printable assembler name of NODE.
463 This function is used only for debugging. When assembler name
464 is unknown go with identifier name. */
466 const char *
467 symtab_node::asm_name () const
469 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
470 return lang_hooks.decl_printable_name (decl, 2);
471 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
474 /* Return printable identifier name. */
476 const char *
477 symtab_node::name () const
479 return lang_hooks.decl_printable_name (decl, 2);
482 /* Return ipa reference from this symtab_node to
483 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
484 of the use. */
486 ipa_ref *
487 symtab_node::create_reference (symtab_node *referred_node,
488 enum ipa_ref_use use_type)
490 return create_reference (referred_node, use_type, NULL);
494 /* Return ipa reference from this symtab_node to
495 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
496 of the use and STMT the statement (if it exists). */
498 ipa_ref *
499 symtab_node::create_reference (symtab_node *referred_node,
500 enum ipa_ref_use use_type, gimple stmt)
502 ipa_ref *ref = NULL, *ref2 = NULL;
503 ipa_ref_list *list, *list2;
504 ipa_ref_t *old_references;
506 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
507 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
509 list = &ref_list;
510 old_references = vec_safe_address (list->references);
511 vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
512 ref = &list->references->last ();
514 list2 = &referred_node->ref_list;
516 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
517 if(use_type == IPA_REF_ALIAS)
519 list2->referring.safe_insert (0, ref);
520 ref->referred_index = 0;
522 for (unsigned int i = 1; i < list2->referring.length (); i++)
523 list2->referring[i]->referred_index = i;
525 else
527 list2->referring.safe_push (ref);
528 ref->referred_index = list2->referring.length () - 1;
531 ref->referring = this;
532 ref->referred = referred_node;
533 ref->stmt = stmt;
534 ref->lto_stmt_uid = 0;
535 ref->use = use_type;
536 ref->speculative = 0;
538 /* If vector was moved in memory, update pointers. */
539 if (old_references != list->references->address ())
541 int i;
542 for (i = 0; iterate_reference(i, ref2); i++)
543 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
545 return ref;
548 /* If VAL is a reference to a function or a variable, add a reference from
549 this symtab_node to the corresponding symbol table node. USE_TYPE specify
550 type of the use and STMT the statement (if it exists). Return the new
551 reference or NULL if none was created. */
553 ipa_ref *
554 symtab_node::maybe_create_reference (tree val, enum ipa_ref_use use_type,
555 gimple stmt)
557 STRIP_NOPS (val);
558 if (TREE_CODE (val) != ADDR_EXPR)
559 return NULL;
560 val = get_base_var (val);
561 if (val && (TREE_CODE (val) == FUNCTION_DECL
562 || TREE_CODE (val) == VAR_DECL))
564 symtab_node *referred = symtab_node::get (val);
565 gcc_checking_assert (referred);
566 return create_reference (referred, use_type, stmt);
568 return NULL;
571 /* Clone all references from symtab NODE to this symtab_node. */
573 void
574 symtab_node::clone_references (symtab_node *node)
576 ipa_ref *ref = NULL, *ref2 = NULL;
577 int i;
578 for (i = 0; node->iterate_reference (i, ref); i++)
580 bool speculative = ref->speculative;
581 unsigned int stmt_uid = ref->lto_stmt_uid;
583 ref2 = create_reference (ref->referred, ref->use, ref->stmt);
584 ref2->speculative = speculative;
585 ref2->lto_stmt_uid = stmt_uid;
589 /* Clone all referring from symtab NODE to this symtab_node. */
591 void
592 symtab_node::clone_referring (symtab_node *node)
594 ipa_ref *ref = NULL, *ref2 = NULL;
595 int i;
596 for (i = 0; node->iterate_referring(i, ref); i++)
598 bool speculative = ref->speculative;
599 unsigned int stmt_uid = ref->lto_stmt_uid;
601 ref2 = ref->referring->create_reference (this, ref->use, ref->stmt);
602 ref2->speculative = speculative;
603 ref2->lto_stmt_uid = stmt_uid;
607 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
609 ipa_ref *
610 symtab_node::clone_reference (ipa_ref *ref, gimple stmt)
612 bool speculative = ref->speculative;
613 unsigned int stmt_uid = ref->lto_stmt_uid;
614 ipa_ref *ref2;
616 ref2 = create_reference (ref->referred, ref->use, stmt);
617 ref2->speculative = speculative;
618 ref2->lto_stmt_uid = stmt_uid;
619 return ref2;
622 /* Find the structure describing a reference to REFERRED_NODE
623 and associated with statement STMT. */
625 ipa_ref *
626 symtab_node::find_reference (symtab_node *referred_node,
627 gimple stmt, unsigned int lto_stmt_uid)
629 ipa_ref *r = NULL;
630 int i;
632 for (i = 0; iterate_reference (i, r); i++)
633 if (r->referred == referred_node
634 && !r->speculative
635 && ((stmt && r->stmt == stmt)
636 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
637 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
638 return r;
639 return NULL;
642 /* Remove all references that are associated with statement STMT. */
644 void
645 symtab_node::remove_stmt_references (gimple stmt)
647 ipa_ref *r = NULL;
648 int i = 0;
650 while (iterate_reference (i, r))
651 if (r->stmt == stmt)
652 r->remove_reference ();
653 else
654 i++;
657 /* Remove all stmt references in non-speculative references.
658 Those are not maintained during inlining & clonning.
659 The exception are speculative references that are updated along
660 with callgraph edges associated with them. */
662 void
663 symtab_node::clear_stmts_in_references (void)
665 ipa_ref *r = NULL;
666 int i;
668 for (i = 0; iterate_reference (i, r); i++)
669 if (!r->speculative)
671 r->stmt = NULL;
672 r->lto_stmt_uid = 0;
676 /* Remove all references in ref list. */
678 void
679 symtab_node::remove_all_references (void)
681 while (vec_safe_length (ref_list.references))
682 ref_list.references->last ().remove_reference ();
683 vec_free (ref_list.references);
686 /* Remove all referring items in ref list. */
688 void
689 symtab_node::remove_all_referring (void)
691 while (ref_list.referring.length ())
692 ref_list.referring.last ()->remove_reference ();
693 ref_list.referring.release ();
696 /* Dump references in ref list to FILE. */
698 void
699 symtab_node::dump_references (FILE *file)
701 ipa_ref *ref = NULL;
702 int i;
703 for (i = 0; iterate_reference (i, ref); i++)
705 fprintf (file, "%s/%i (%s)",
706 ref->referred->asm_name (),
707 ref->referred->order,
708 ipa_ref_use_name [ref->use]);
709 if (ref->speculative)
710 fprintf (file, " (speculative)");
712 fprintf (file, "\n");
715 /* Dump referring in list to FILE. */
717 void
718 symtab_node::dump_referring (FILE *file)
720 ipa_ref *ref = NULL;
721 int i;
722 for (i = 0; iterate_referring(i, ref); i++)
724 fprintf (file, "%s/%i (%s)",
725 ref->referring->asm_name (),
726 ref->referring->order,
727 ipa_ref_use_name [ref->use]);
728 if (ref->speculative)
729 fprintf (file, " (speculative)");
731 fprintf (file, "\n");
734 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
736 /* Dump base fields of symtab nodes to F. Not to be used directly. */
738 void
739 symtab_node::dump_base (FILE *f)
741 static const char * const visibility_types[] = {
742 "default", "protected", "hidden", "internal"
745 fprintf (f, "%s/%i (%s)", asm_name (), order, name ());
746 dump_addr (f, " @", (void *)this);
747 fprintf (f, "\n Type: %s", symtab_type_names[type]);
749 if (definition)
750 fprintf (f, " definition");
751 if (analyzed)
752 fprintf (f, " analyzed");
753 if (alias)
754 fprintf (f, " alias");
755 if (weakref)
756 fprintf (f, " weakref");
757 if (cpp_implicit_alias)
758 fprintf (f, " cpp_implicit_alias");
759 if (alias_target)
760 fprintf (f, " target:%s",
761 DECL_P (alias_target)
762 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
763 (alias_target))
764 : IDENTIFIER_POINTER (alias_target));
765 if (body_removed)
766 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
767 fprintf (f, "\n Visibility:");
768 if (in_other_partition)
769 fprintf (f, " in_other_partition");
770 if (used_from_other_partition)
771 fprintf (f, " used_from_other_partition");
772 if (force_output)
773 fprintf (f, " force_output");
774 if (forced_by_abi)
775 fprintf (f, " forced_by_abi");
776 if (externally_visible)
777 fprintf (f, " externally_visible");
778 if (no_reorder)
779 fprintf (f, " no_reorder");
780 if (resolution != LDPR_UNKNOWN)
781 fprintf (f, " %s",
782 ld_plugin_symbol_resolution_names[(int)resolution]);
783 if (TREE_ASM_WRITTEN (decl))
784 fprintf (f, " asm_written");
785 if (DECL_EXTERNAL (decl))
786 fprintf (f, " external");
787 if (TREE_PUBLIC (decl))
788 fprintf (f, " public");
789 if (DECL_COMMON (decl))
790 fprintf (f, " common");
791 if (DECL_WEAK (decl))
792 fprintf (f, " weak");
793 if (DECL_DLLIMPORT_P (decl))
794 fprintf (f, " dll_import");
795 if (DECL_COMDAT (decl))
796 fprintf (f, " comdat");
797 if (get_comdat_group ())
798 fprintf (f, " comdat_group:%s",
799 IDENTIFIER_POINTER (get_comdat_group_id ()));
800 if (DECL_ONE_ONLY (decl))
801 fprintf (f, " one_only");
802 if (get_section ())
803 fprintf (f, " section:%s",
804 get_section ());
805 if (implicit_section)
806 fprintf (f," (implicit_section)");
807 if (DECL_VISIBILITY_SPECIFIED (decl))
808 fprintf (f, " visibility_specified");
809 if (DECL_VISIBILITY (decl))
810 fprintf (f, " visibility:%s",
811 visibility_types [DECL_VISIBILITY (decl)]);
812 if (DECL_VIRTUAL_P (decl))
813 fprintf (f, " virtual");
814 if (DECL_ARTIFICIAL (decl))
815 fprintf (f, " artificial");
816 if (TREE_CODE (decl) == FUNCTION_DECL)
818 if (DECL_STATIC_CONSTRUCTOR (decl))
819 fprintf (f, " constructor");
820 if (DECL_STATIC_DESTRUCTOR (decl))
821 fprintf (f, " destructor");
823 fprintf (f, "\n");
825 if (same_comdat_group)
826 fprintf (f, " Same comdat group as: %s/%i\n",
827 same_comdat_group->asm_name (),
828 same_comdat_group->order);
829 if (next_sharing_asm_name)
830 fprintf (f, " next sharing asm name: %i\n",
831 next_sharing_asm_name->order);
832 if (previous_sharing_asm_name)
833 fprintf (f, " previous sharing asm name: %i\n",
834 previous_sharing_asm_name->order);
836 if (address_taken)
837 fprintf (f, " Address is taken.\n");
838 if (aux)
840 fprintf (f, " Aux:");
841 dump_addr (f, " @", (void *)aux);
844 fprintf (f, " References: ");
845 dump_references (f);
846 fprintf (f, " Referring: ");
847 dump_referring (f);
848 if (lto_file_data)
849 fprintf (f, " Read from file: %s\n",
850 lto_file_data->file_name);
853 /* Dump symtab node to F. */
855 void
856 symtab_node::dump (FILE *f)
858 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
859 cnode->dump (f);
860 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
861 vnode->dump (f);
864 /* Dump symbol table to F. */
866 void
867 symtab_node::dump_table (FILE *f)
869 symtab_node *node;
870 fprintf (f, "Symbol table:\n\n");
871 FOR_EACH_SYMBOL (node)
872 node->dump (f);
876 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
877 Return NULL if there's no such node. */
879 symtab_node *
880 symtab_node::get_for_asmname (const_tree asmname)
882 symtab_node *node;
884 symtab->symtab_initialize_asm_name_hash ();
885 hashval_t hash = symtab->decl_assembler_name_hash (asmname);
886 symtab_node **slot
887 = symtab->assembler_name_hash->find_slot_with_hash (asmname, hash,
888 NO_INSERT);
890 if (slot)
892 node = *slot;
893 return node;
895 return NULL;
898 /* Dump symtab node NODE to stderr. */
900 DEBUG_FUNCTION void
901 symtab_node::debug (void)
903 dump (stderr);
906 /* Verify common part of symtab nodes. */
908 DEBUG_FUNCTION bool
909 symtab_node::verify_base (void)
911 bool error_found = false;
912 symtab_node *hashed_node;
914 if (is_a <cgraph_node *> (this))
916 if (TREE_CODE (decl) != FUNCTION_DECL)
918 error ("function symbol is not function");
919 error_found = true;
922 else if (is_a <varpool_node *> (this))
924 if (TREE_CODE (decl) != VAR_DECL)
926 error ("variable symbol is not variable");
927 error_found = true;
930 else
932 error ("node has unknown type");
933 error_found = true;
936 if (symtab->state != LTO_STREAMING)
938 hashed_node = symtab_node::get (decl);
939 if (!hashed_node)
941 error ("node not found node->decl->decl_with_vis.symtab_node");
942 error_found = true;
944 if (hashed_node != this
945 && (!is_a <cgraph_node *> (this)
946 || !dyn_cast <cgraph_node *> (this)->clone_of
947 || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl))
949 error ("node differs from node->decl->decl_with_vis.symtab_node");
950 error_found = true;
953 if (symtab->assembler_name_hash)
955 hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
956 if (hashed_node && hashed_node->previous_sharing_asm_name)
958 error ("assembler name hash list corrupted");
959 error_found = true;
961 while (hashed_node)
963 if (hashed_node == this)
964 break;
965 hashed_node = hashed_node->next_sharing_asm_name;
967 if (!hashed_node
968 && !(is_a <varpool_node *> (this)
969 || DECL_HARD_REGISTER (decl)))
971 error ("node not found in symtab assembler name hash");
972 error_found = true;
975 if (previous_sharing_asm_name
976 && previous_sharing_asm_name->next_sharing_asm_name != this)
978 error ("double linked list of assembler names corrupted");
979 error_found = true;
981 if (body_removed && definition)
983 error ("node has body_removed but is definition");
984 error_found = true;
986 if (analyzed && !definition)
988 error ("node is analyzed byt it is not a definition");
989 error_found = true;
991 if (cpp_implicit_alias && !alias)
993 error ("node is alias but not implicit alias");
994 error_found = true;
996 if (alias && !definition && !weakref)
998 error ("node is alias but not definition");
999 error_found = true;
1001 if (weakref && !alias)
1003 error ("node is weakref but not an alias");
1004 error_found = true;
1006 if (same_comdat_group)
1008 symtab_node *n = same_comdat_group;
1010 if (!n->get_comdat_group ())
1012 error ("node is in same_comdat_group list but has no comdat_group");
1013 error_found = true;
1015 if (n->get_comdat_group () != get_comdat_group ())
1017 error ("same_comdat_group list across different groups");
1018 error_found = true;
1020 if (n->type != type)
1022 error ("mixing different types of symbol in same comdat groups is not supported");
1023 error_found = true;
1025 if (n == this)
1027 error ("node is alone in a comdat group");
1028 error_found = true;
1032 if (!n->same_comdat_group)
1034 error ("same_comdat_group is not a circular list");
1035 error_found = true;
1036 break;
1038 n = n->same_comdat_group;
1040 while (n != this);
1041 if (comdat_local_p ())
1043 ipa_ref *ref = NULL;
1045 for (int i = 0; iterate_referring (i, ref); ++i)
1047 if (!in_same_comdat_group_p (ref->referring))
1049 error ("comdat-local symbol referred to by %s outside its "
1050 "comdat",
1051 identifier_to_locale (ref->referring->name()));
1052 error_found = true;
1057 if (implicit_section && !get_section ())
1059 error ("implicit_section flag is set but section isn't");
1060 error_found = true;
1062 if (get_section () && get_comdat_group ()
1063 && !implicit_section
1064 && !lookup_attribute ("section", DECL_ATTRIBUTES (decl)))
1066 error ("Both section and comdat group is set");
1067 error_found = true;
1069 /* TODO: Add string table for sections, so we do not keep holding duplicated
1070 strings. */
1071 if (alias && definition
1072 && get_section () != get_alias_target ()->get_section ()
1073 && (!get_section()
1074 || !get_alias_target ()->get_section ()
1075 || strcmp (get_section(),
1076 get_alias_target ()->get_section ())))
1078 error ("Alias and target's section differs");
1079 get_alias_target ()->dump (stderr);
1080 error_found = true;
1082 if (alias && definition
1083 && get_comdat_group () != get_alias_target ()->get_comdat_group ())
1085 error ("Alias and target's comdat groups differs");
1086 get_alias_target ()->dump (stderr);
1087 error_found = true;
1090 return error_found;
1093 /* Verify consistency of NODE. */
1095 DEBUG_FUNCTION void
1096 symtab_node::verify (void)
1098 if (seen_error ())
1099 return;
1101 timevar_push (TV_CGRAPH_VERIFY);
1102 if (cgraph_node *node = dyn_cast <cgraph_node *> (this))
1103 node->verify_node ();
1104 else
1105 if (verify_base ())
1107 debug ();
1108 internal_error ("symtab_node::verify failed");
1110 timevar_pop (TV_CGRAPH_VERIFY);
1113 /* Verify symbol table for internal consistency. */
1115 DEBUG_FUNCTION void
1116 symtab_node::verify_symtab_nodes (void)
1118 symtab_node *node;
1119 hash_map<tree, symtab_node *> comdat_head_map (251);
1121 FOR_EACH_SYMBOL (node)
1123 node->verify ();
1124 if (node->get_comdat_group ())
1126 symtab_node **entry, *s;
1127 bool existed;
1129 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1130 &existed);
1131 if (!existed)
1132 *entry = node;
1133 else if (!DECL_EXTERNAL (node->decl))
1135 for (s = (*entry)->same_comdat_group; s != NULL && s != node;
1136 s = s->same_comdat_group)
1138 if (!s || s == *entry)
1140 error ("Two symbols with same comdat_group are not linked by "
1141 "the same_comdat_group list.");
1142 (*entry)->debug ();
1143 node->debug ();
1144 internal_error ("symtab_node::verify failed");
1151 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1152 but other code such as notice_global_symbol generates rtl. */
1154 void
1155 symtab_node::make_decl_local (void)
1157 rtx rtl, symbol;
1159 /* Avoid clearing comdat_groups on comdat-local decls. */
1160 if (TREE_PUBLIC (decl) == 0)
1161 return;
1163 if (TREE_CODE (decl) == VAR_DECL)
1165 DECL_COMMON (decl) = 0;
1166 /* ADDRESSABLE flag is not defined for public symbols. */
1167 TREE_ADDRESSABLE (decl) = 1;
1169 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1171 DECL_COMDAT (decl) = 0;
1172 DECL_WEAK (decl) = 0;
1173 DECL_EXTERNAL (decl) = 0;
1174 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1175 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1176 TREE_PUBLIC (decl) = 0;
1177 DECL_DLLIMPORT_P (decl) = 0;
1178 if (!DECL_RTL_SET_P (decl))
1179 return;
1181 /* Update rtl flags. */
1182 make_decl_rtl (decl);
1184 rtl = DECL_RTL (decl);
1185 if (!MEM_P (rtl))
1186 return;
1188 symbol = XEXP (rtl, 0);
1189 if (GET_CODE (symbol) != SYMBOL_REF)
1190 return;
1192 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1195 /* Walk the alias chain to return the symbol NODE is alias of.
1196 If NODE is not an alias, return NODE.
1197 Assumes NODE is known to be alias. */
1199 symtab_node *
1200 symtab_node::ultimate_alias_target_1 (enum availability *availability)
1202 bool weakref_p = false;
1204 /* To determine visibility of the target, we follow ELF semantic of aliases.
1205 Here alias is an alternative assembler name of a given definition. Its
1206 availability prevails the availability of its target (i.e. static alias of
1207 weak definition is available.
1209 Weakref is a different animal (and not part of ELF per se). It is just
1210 alternative name of a given symbol used within one complation unit
1211 and is translated prior hitting the object file. It inherits the
1212 visibility of its target (i.e. weakref of non-overwritable definition
1213 is non-overwritable, while weakref of weak definition is weak).
1215 If we ever get into supporting targets with different semantics, a target
1216 hook will be needed here. */
1218 if (availability)
1220 weakref_p = weakref;
1221 if (!weakref_p)
1222 *availability = get_availability ();
1223 else
1224 *availability = AVAIL_LOCAL;
1227 symtab_node *node = this;
1228 while (node)
1230 if (node->alias && node->analyzed)
1231 node = node->get_alias_target ();
1232 else
1234 if (!availability)
1236 else if (node->analyzed)
1238 if (weakref_p)
1240 enum availability a = node->get_availability ();
1241 if (a < *availability)
1242 *availability = a;
1245 else
1246 *availability = AVAIL_NOT_AVAILABLE;
1247 return node;
1249 if (node && availability && weakref_p)
1251 enum availability a = node->get_availability ();
1252 if (a < *availability)
1253 *availability = a;
1254 weakref_p = node->weakref;
1257 if (availability)
1258 *availability = AVAIL_NOT_AVAILABLE;
1259 return NULL;
1262 /* C++ FE sometimes change linkage flags after producing same body aliases.
1264 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1265 are obviously equivalent. The way it is doing so is however somewhat
1266 kludgy and interferes with the visibility code. As a result we need to
1267 copy the visibility from the target to get things right. */
1269 void
1270 symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target)
1272 if (is_a <cgraph_node *> (this))
1274 DECL_DECLARED_INLINE_P (decl)
1275 = DECL_DECLARED_INLINE_P (target->decl);
1276 DECL_DISREGARD_INLINE_LIMITS (decl)
1277 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1279 /* FIXME: It is not really clear why those flags should not be copied for
1280 functions, too. */
1281 else
1283 DECL_WEAK (decl) = DECL_WEAK (target->decl);
1284 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1285 DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl);
1287 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (target->decl);
1288 if (TREE_PUBLIC (decl))
1290 tree group;
1292 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1293 DECL_COMDAT (decl) = DECL_COMDAT (target->decl);
1294 group = target->get_comdat_group ();
1295 set_comdat_group (group);
1296 if (group && !same_comdat_group)
1297 add_to_same_comdat_group (target);
1299 externally_visible = target->externally_visible;
1302 /* Set section, do not recurse into aliases.
1303 When one wants to change section of symbol and its aliases,
1304 use set_section. */
1306 void
1307 symtab_node::set_section_for_node (const char *section)
1309 const char *current = get_section ();
1310 section_hash_entry **slot;
1312 if (current == section
1313 || (current && section
1314 && !strcmp (current, section)))
1315 return;
1317 if (current)
1319 x_section->ref_count--;
1320 if (!x_section->ref_count)
1322 hashval_t hash = htab_hash_string (x_section->name);
1323 slot = symtab->section_hash->find_slot_with_hash (x_section->name,
1324 hash, INSERT);
1325 ggc_free (x_section);
1326 symtab->section_hash->clear_slot (slot);
1328 x_section = NULL;
1330 if (!section)
1332 implicit_section = false;
1333 return;
1335 if (!symtab->section_hash)
1336 symtab->section_hash = hash_table<section_name_hasher>::create_ggc (10);
1337 slot = symtab->section_hash->find_slot_with_hash (section,
1338 htab_hash_string (section),
1339 INSERT);
1340 if (*slot)
1341 x_section = (section_hash_entry *)*slot;
1342 else
1344 int len = strlen (section);
1345 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1346 x_section->name = ggc_vec_alloc<char> (len + 1);
1347 memcpy (x_section->name, section, len + 1);
1349 x_section->ref_count++;
1352 /* Worker for set_section. */
1354 bool
1355 symtab_node::set_section (symtab_node *n, void *s)
1357 n->set_section_for_node ((char *)s);
1358 return false;
1361 /* Set section of symbol and its aliases. */
1363 void
1364 symtab_node::set_section (const char *section)
1366 gcc_assert (!this->alias);
1367 call_for_symbol_and_aliases
1368 (symtab_node::set_section, const_cast<char *>(section), true);
1371 /* Return the initialization priority. */
1373 priority_type
1374 symtab_node::get_init_priority ()
1376 if (!this->in_init_priority_hash)
1377 return DEFAULT_INIT_PRIORITY;
1379 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1380 return h ? h->init : DEFAULT_INIT_PRIORITY;
1383 /* Return the finalization priority. */
1385 priority_type
1386 cgraph_node::get_fini_priority ()
1388 if (!this->in_init_priority_hash)
1389 return DEFAULT_INIT_PRIORITY;
1390 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1391 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1394 /* Return the initialization and finalization priority information for
1395 DECL. If there is no previous priority information, a freshly
1396 allocated structure is returned. */
1398 symbol_priority_map *
1399 symtab_node::priority_info (void)
1401 if (!symtab->init_priority_hash)
1402 symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13);
1404 bool existed;
1405 symbol_priority_map *h
1406 = &symtab->init_priority_hash->get_or_insert (this, &existed);
1407 if (!existed)
1409 h->init = DEFAULT_INIT_PRIORITY;
1410 h->fini = DEFAULT_INIT_PRIORITY;
1411 in_init_priority_hash = true;
1414 return h;
1417 /* Set initialization priority to PRIORITY. */
1419 void
1420 symtab_node::set_init_priority (priority_type priority)
1422 symbol_priority_map *h;
1424 if (is_a <cgraph_node *> (this))
1425 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1427 if (priority == DEFAULT_INIT_PRIORITY)
1429 gcc_assert (get_init_priority() == priority);
1430 return;
1432 h = priority_info ();
1433 h->init = priority;
1436 /* Set fialization priority to PRIORITY. */
1438 void
1439 cgraph_node::set_fini_priority (priority_type priority)
1441 symbol_priority_map *h;
1443 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1445 if (priority == DEFAULT_INIT_PRIORITY)
1447 gcc_assert (get_fini_priority() == priority);
1448 return;
1450 h = priority_info ();
1451 h->fini = priority;
1454 /* Worker for symtab_resolve_alias. */
1456 bool
1457 symtab_node::set_implicit_section (symtab_node *n,
1458 void *data ATTRIBUTE_UNUSED)
1460 n->implicit_section = true;
1461 return false;
1464 /* Add reference recording that symtab node is alias of TARGET.
1465 The function can fail in the case of aliasing cycles; in this case
1466 it returns false. */
1468 bool
1469 symtab_node::resolve_alias (symtab_node *target)
1471 symtab_node *n;
1473 gcc_assert (!analyzed && !vec_safe_length (ref_list.references));
1475 /* Never let cycles to creep into the symbol table alias references;
1476 those will make alias walkers to be infinite. */
1477 for (n = target; n && n->alias;
1478 n = n->analyzed ? n->get_alias_target () : NULL)
1479 if (n == this)
1481 if (is_a <cgraph_node *> (this))
1482 error ("function %q+D part of alias cycle", decl);
1483 else if (is_a <varpool_node *> (this))
1484 error ("variable %q+D part of alias cycle", decl);
1485 else
1486 gcc_unreachable ();
1487 alias = false;
1488 return false;
1491 /* "analyze" the node - i.e. mark the reference. */
1492 definition = true;
1493 alias = true;
1494 analyzed = true;
1495 create_reference (target, IPA_REF_ALIAS, NULL);
1497 /* Add alias into the comdat group of its target unless it is already there. */
1498 if (same_comdat_group)
1499 remove_from_same_comdat_group ();
1500 set_comdat_group (NULL);
1501 if (target->get_comdat_group ())
1502 add_to_same_comdat_group (target);
1504 if ((get_section () != target->get_section ()
1505 || target->get_comdat_group ()) && get_section () && !implicit_section)
1507 error ("section of alias %q+D must match section of its target", decl);
1509 call_for_symbol_and_aliases (symtab_node::set_section,
1510 const_cast<char *>(target->get_section ()), true);
1511 if (target->implicit_section)
1512 call_for_symbol_and_aliases (set_implicit_section, NULL, true);
1514 /* Alias targets become redundant after alias is resolved into an reference.
1515 We do not want to keep it around or we would have to mind updating them
1516 when renaming symbols. */
1517 alias_target = NULL;
1519 if (cpp_implicit_alias && symtab->state >= CONSTRUCTION)
1520 fixup_same_cpp_alias_visibility (target);
1522 /* If alias has address taken, so does the target. */
1523 if (address_taken)
1524 target->ultimate_alias_target ()->address_taken = true;
1526 /* All non-weakref aliases of THIS are now in fact aliases of TARGET. */
1527 ipa_ref *ref;
1528 for (unsigned i = 0; iterate_direct_aliases (i, ref);)
1530 struct symtab_node *alias_alias = ref->referring;
1531 if (!alias_alias->weakref)
1533 alias_alias->remove_all_references ();
1534 alias_alias->create_reference (target, IPA_REF_ALIAS, NULL);
1536 else i++;
1538 return true;
1541 /* Worker searching noninterposable alias. */
1543 bool
1544 symtab_node::noninterposable_alias (symtab_node *node, void *data)
1546 if (decl_binds_to_current_def_p (node->decl))
1548 symtab_node *fn = node->ultimate_alias_target ();
1550 /* Ensure that the alias is well formed this may not be the case
1551 of user defined aliases and currently it is not always the case
1552 of C++ same body aliases (that is a bug). */
1553 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
1554 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
1555 || (TREE_CODE (node->decl) == FUNCTION_DECL
1556 && flags_from_decl_or_type (node->decl)
1557 != flags_from_decl_or_type (fn->decl))
1558 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
1559 return false;
1560 *(symtab_node **)data = node;
1561 return true;
1563 return false;
1566 /* If node can not be overwriten by static or dynamic linker to point to
1567 different definition, return NODE. Otherwise look for alias with such
1568 property and if none exists, introduce new one. */
1570 symtab_node *
1571 symtab_node::noninterposable_alias (void)
1573 tree new_decl;
1574 symtab_node *new_node = NULL;
1576 /* First try to look up existing alias or base object
1577 (if that is already non-overwritable). */
1578 symtab_node *node = ultimate_alias_target ();
1579 gcc_assert (!node->alias && !node->weakref);
1580 node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias,
1581 (void *)&new_node, true);
1582 if (new_node)
1583 return new_node;
1584 #ifndef ASM_OUTPUT_DEF
1585 /* If aliases aren't supported by the assembler, fail. */
1586 return NULL;
1587 #endif
1589 /* Otherwise create a new one. */
1590 new_decl = copy_node (node->decl);
1591 DECL_DLLIMPORT_P (new_decl) = 0;
1592 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1593 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1594 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1595 DECL_INITIAL (new_decl) = NULL;
1596 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1597 SET_DECL_RTL (new_decl, NULL);
1599 /* Update the properties. */
1600 DECL_EXTERNAL (new_decl) = 0;
1601 TREE_PUBLIC (new_decl) = 0;
1602 DECL_COMDAT (new_decl) = 0;
1603 DECL_WEAK (new_decl) = 0;
1605 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1606 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1607 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1609 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1610 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1611 new_node = cgraph_node::create_alias (new_decl, node->decl);
1613 else
1615 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1616 DECL_INITIAL (new_decl) = error_mark_node;
1617 new_node = varpool_node::create_alias (new_decl, node->decl);
1619 new_node->resolve_alias (node);
1620 gcc_assert (decl_binds_to_current_def_p (new_decl)
1621 && targetm.binds_local_p (new_decl));
1622 return new_node;
1625 /* Return true if symtab node and TARGET represents
1626 semantically equivalent symbols. */
1628 bool
1629 symtab_node::semantically_equivalent_p (symtab_node *target)
1631 enum availability avail;
1632 symtab_node *ba;
1633 symtab_node *bb;
1635 /* Equivalent functions are equivalent. */
1636 if (decl == target->decl)
1637 return true;
1639 /* If symbol is not overwritable by different implementation,
1640 walk to the base object it defines. */
1641 ba = ultimate_alias_target (&avail);
1642 if (avail >= AVAIL_AVAILABLE)
1644 if (target == ba)
1645 return true;
1647 else
1648 ba = this;
1649 bb = target->ultimate_alias_target (&avail);
1650 if (avail >= AVAIL_AVAILABLE)
1652 if (this == bb)
1653 return true;
1655 else
1656 bb = target;
1657 return bb == ba;
1660 /* Classify symbol symtab node for partitioning. */
1662 enum symbol_partitioning_class
1663 symtab_node::get_partitioning_class (void)
1665 /* Inline clones are always duplicated.
1666 This include external delcarations. */
1667 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
1669 if (DECL_ABSTRACT_P (decl))
1670 return SYMBOL_EXTERNAL;
1672 if (cnode && cnode->global.inlined_to)
1673 return SYMBOL_DUPLICATE;
1675 /* Weakref aliases are always duplicated. */
1676 if (weakref)
1677 return SYMBOL_DUPLICATE;
1679 /* External declarations are external. */
1680 if (DECL_EXTERNAL (decl))
1681 return SYMBOL_EXTERNAL;
1683 if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
1685 if (alias && definition && !ultimate_alias_target ()->definition)
1686 return SYMBOL_EXTERNAL;
1687 /* Constant pool references use local symbol names that can not
1688 be promoted global. We should never put into a constant pool
1689 objects that can not be duplicated across partitions. */
1690 if (DECL_IN_CONSTANT_POOL (decl))
1691 return SYMBOL_DUPLICATE;
1692 gcc_checking_assert (vnode->definition);
1694 /* Functions that are cloned may stay in callgraph even if they are unused.
1695 Handle them as external; compute_ltrans_boundary take care to make
1696 proper things to happen (i.e. to make them appear in the boundary but
1697 with body streamed, so clone can me materialized). */
1698 else if (!dyn_cast <cgraph_node *> (this)->function_symbol ()->definition)
1699 return SYMBOL_EXTERNAL;
1701 /* Linker discardable symbols are duplicated to every use unless they are
1702 keyed. */
1703 if (DECL_ONE_ONLY (decl)
1704 && !force_output
1705 && !forced_by_abi
1706 && !used_from_object_file_p ())
1707 return SYMBOL_DUPLICATE;
1709 return SYMBOL_PARTITION;
1712 /* Return true when symbol is known to be non-zero. */
1714 bool
1715 symtab_node::nonzero_address ()
1717 /* Weakrefs may be NULL when their target is not defined. */
1718 if (alias && weakref)
1720 if (analyzed)
1722 symtab_node *target = ultimate_alias_target ();
1724 if (target->alias && target->weakref)
1725 return false;
1726 /* We can not recurse to target::nonzero. It is possible that the
1727 target is used only via the alias.
1728 We may walk references and look for strong use, but we do not know
1729 if this strong use will survive to final binary, so be
1730 conservative here.
1731 ??? Maybe we could do the lookup during late optimization that
1732 could be useful to eliminate the NULL pointer checks in LTO
1733 programs. */
1734 if (target->definition && !DECL_EXTERNAL (target->decl))
1735 return true;
1736 if (target->resolution != LDPR_UNKNOWN
1737 && target->resolution != LDPR_UNDEF
1738 && flag_delete_null_pointer_checks)
1739 return true;
1740 return false;
1742 else
1743 return false;
1746 /* With !flag_delete_null_pointer_checks we assume that symbols may
1747 bind to NULL. This is on by default on embedded targets only.
1749 Otherwise all non-WEAK symbols must be defined and thus non-NULL or
1750 linking fails. Important case of WEAK we want to do well are comdats.
1751 Those are handled by later check for definition.
1753 When parsing, beware the cases when WEAK attribute is added later. */
1754 if (!DECL_WEAK (decl)
1755 && flag_delete_null_pointer_checks)
1757 refuse_visibility_changes = true;
1758 return true;
1761 /* If target is defined and not extern, we know it will be output and thus
1762 it will bind to non-NULL.
1763 Play safe for flag_delete_null_pointer_checks where weak definition maye
1764 be re-defined by NULL. */
1765 if (definition && !DECL_EXTERNAL (decl)
1766 && (flag_delete_null_pointer_checks || !DECL_WEAK (decl)))
1768 if (!DECL_WEAK (decl))
1769 refuse_visibility_changes = true;
1770 return true;
1773 /* As the last resort, check the resolution info. */
1774 if (resolution != LDPR_UNKNOWN
1775 && resolution != LDPR_UNDEF
1776 && flag_delete_null_pointer_checks)
1777 return true;
1778 return false;
1781 /* Return 0 if symbol is known to have different address than S2,
1782 Return 1 if symbol is known to have same address as S2,
1783 return 2 otherwise. */
1785 symtab_node::equal_address_to (symtab_node *s2)
1787 enum availability avail1, avail2;
1789 /* A Shortcut: equivalent symbols are always equivalent. */
1790 if (this == s2)
1791 return 1;
1793 /* For non-interposable aliases, lookup and compare their actual definitions.
1794 Also check if the symbol needs to bind to given definition. */
1795 symtab_node *rs1 = ultimate_alias_target (&avail1);
1796 symtab_node *rs2 = s2->ultimate_alias_target (&avail2);
1797 bool binds_local1 = rs1->analyzed && decl_binds_to_current_def_p (this->decl);
1798 bool binds_local2 = rs2->analyzed && decl_binds_to_current_def_p (s2->decl);
1799 bool really_binds_local1 = binds_local1;
1800 bool really_binds_local2 = binds_local2;
1802 /* Addresses of vtables and virtual functions can not be used by user
1803 code and are used only within speculation. In this case we may make
1804 symbol equivalent to its alias even if interposition may break this
1805 rule. Doing so will allow us to turn speculative inlining into
1806 non-speculative more agressively. */
1807 if (DECL_VIRTUAL_P (this->decl) && avail1 >= AVAIL_AVAILABLE)
1808 binds_local1 = true;
1809 if (DECL_VIRTUAL_P (s2->decl) && avail2 >= AVAIL_AVAILABLE)
1810 binds_local2 = true;
1812 /* If both definitions are available we know that even if they are bound
1813 to other unit they must be defined same way and therefore we can use
1814 equivalence test. */
1815 if (rs1 != rs2 && avail1 >= AVAIL_AVAILABLE && avail2 >= AVAIL_AVAILABLE)
1816 binds_local1 = binds_local2 = true;
1818 if ((binds_local1 ? rs1 : this)
1819 == (binds_local2 ? rs2 : s2))
1821 /* We made use of the fact that alias is not weak. */
1822 if (binds_local1 && rs1 != this)
1823 refuse_visibility_changes = true;
1824 if (binds_local2 && rs2 != s2)
1825 s2->refuse_visibility_changes = true;
1826 return 1;
1829 /* If both symbols may resolve to NULL, we can not really prove them different. */
1830 if (!nonzero_address () && !s2->nonzero_address ())
1831 return 2;
1833 /* Except for NULL, functions and variables never overlap. */
1834 if (TREE_CODE (decl) != TREE_CODE (s2->decl))
1835 return 0;
1837 /* If one of the symbols is unresolved alias, punt. */
1838 if (rs1->alias || rs2->alias)
1839 return 2;
1841 /* If we have a non-interposale definition of at least one of the symbols
1842 and the other symbol is different, we know other unit can not interpose
1843 it to the first symbol; all aliases of the definition needs to be
1844 present in the current unit. */
1845 if (((really_binds_local1 || really_binds_local2)
1846 /* If we have both definitions and they are different, we know they
1847 will be different even in units they binds to. */
1848 || (binds_local1 && binds_local2))
1849 && rs1 != rs2)
1851 /* We make use of the fact that one symbol is not alias of the other
1852 and that the definition is non-interposable. */
1853 refuse_visibility_changes = true;
1854 s2->refuse_visibility_changes = true;
1855 rs1->refuse_visibility_changes = true;
1856 rs2->refuse_visibility_changes = true;
1857 return 0;
1860 /* TODO: Alias oracle basically assume that addresses of global variables
1861 are different unless they are declared as alias of one to another.
1862 We probably should be consistent and use this fact here, too, and update
1863 alias oracle to use this predicate. */
1865 return 2;
1868 /* Worker for call_for_symbol_and_aliases. */
1870 bool
1871 symtab_node::call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *,
1872 void *),
1873 void *data,
1874 bool include_overwritable)
1876 ipa_ref *ref;
1877 FOR_EACH_ALIAS (this, ref)
1879 symtab_node *alias = ref->referring;
1880 if (include_overwritable
1881 || alias->get_availability () > AVAIL_INTERPOSABLE)
1882 if (alias->call_for_symbol_and_aliases (callback, data,
1883 include_overwritable))
1884 return true;
1886 return false;
1889 /* Return ture if address of N is possibly compared. */
1891 static bool
1892 address_matters_1 (symtab_node *n, void *)
1894 struct ipa_ref *ref;
1896 if (!n->address_can_be_compared_p ())
1897 return false;
1898 if (n->externally_visible || n->force_output)
1899 return true;
1901 for (unsigned int i = 0; n->iterate_referring (i, ref); i++)
1902 if (ref->address_matters_p ())
1903 return true;
1904 return false;
1907 /* Return true if symbol's address may possibly be compared to other
1908 symbol's address. */
1910 bool
1911 symtab_node::address_matters_p ()
1913 gcc_assert (!alias);
1914 return call_for_symbol_and_aliases (address_matters_1, NULL, true);
1917 /* Return ture if symbol's alignment may be increased. */
1919 bool
1920 symtab_node::can_increase_alignment_p (void)
1922 symtab_node *target = ultimate_alias_target ();
1924 /* For now support only variables. */
1925 if (TREE_CODE (decl) != VAR_DECL)
1926 return false;
1928 /* With -fno-toplevel-reorder we may have already output the constant. */
1929 if (TREE_ASM_WRITTEN (target->decl))
1930 return false;
1932 /* If target is already placed in an anchor, we can not touch its
1933 alignment. */
1934 if (DECL_RTL_SET_P (target->decl)
1935 && MEM_P (DECL_RTL (target->decl))
1936 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (DECL_RTL (target->decl), 0)))
1937 return false;
1939 /* Constant pool entries may be shared. */
1940 if (DECL_IN_CONSTANT_POOL (target->decl))
1941 return false;
1943 /* We cannot change alignment of symbols that may bind to symbols
1944 in other translation unit that may contain a definition with lower
1945 alignment. */
1946 if (!decl_binds_to_current_def_p (decl))
1947 return false;
1949 /* When compiling partition, be sure the symbol is not output by other
1950 partition. */
1951 if (flag_ltrans
1952 && (target->in_other_partition
1953 || target->get_partitioning_class () == SYMBOL_DUPLICATE))
1954 return false;
1956 /* Do not override the alignment as specified by the ABI when the used
1957 attribute is set. */
1958 if (DECL_PRESERVE_P (decl) || DECL_PRESERVE_P (target->decl))
1959 return false;
1961 /* Do not override explicit alignment set by the user when an explicit
1962 section name is also used. This is a common idiom used by many
1963 software projects. */
1964 if (DECL_SECTION_NAME (target->decl) != NULL && !target->implicit_section)
1965 return false;
1967 return true;
1970 /* Worker for symtab_node::increase_alignment. */
1972 static bool
1973 increase_alignment_1 (symtab_node *n, void *v)
1975 unsigned int align = (size_t)v;
1976 if (DECL_ALIGN (n->decl) < align
1977 && n->can_increase_alignment_p ())
1979 DECL_ALIGN (n->decl) = align;
1980 DECL_USER_ALIGN (n->decl) = 1;
1982 return false;
1985 /* Increase alignment of THIS to ALIGN. */
1987 void
1988 symtab_node::increase_alignment (unsigned int align)
1990 gcc_assert (can_increase_alignment_p () && align < MAX_OFILE_ALIGNMENT);
1991 ultimate_alias_target()->call_for_symbol_and_aliases (increase_alignment_1,
1992 (void *)(size_t) align,
1993 true);
1994 gcc_assert (DECL_ALIGN (decl) >= align);
1997 /* Helper for symtab_node::definition_alignment. */
1999 static bool
2000 get_alignment_1 (symtab_node *n, void *v)
2002 *((unsigned int *)v) = MAX (*((unsigned int *)v), DECL_ALIGN (n->decl));
2003 return false;
2006 /* Return desired alignment of the definition. This is NOT alignment useful
2007 to access THIS, because THIS may be interposable and DECL_ALIGN should
2008 be used instead. It however must be guaranteed when output definition
2009 of THIS. */
2011 unsigned int
2012 symtab_node::definition_alignment ()
2014 unsigned int align = 0;
2015 gcc_assert (!alias);
2016 call_for_symbol_and_aliases (get_alignment_1, &align, true);
2017 return align;