2 Copyright (C) 2012-2019 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
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
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/>. */
23 #include "coretypes.h"
31 #include "lto-streamer.h"
32 #include "print-tree.h"
34 #include "langhooks.h"
36 #include "ipa-utils.h"
38 #include "stringpool.h"
42 static const char *ipa_ref_use_name
[] = {"read","write","addr","alias"};
44 const char * const ld_plugin_symbol_resolution_names
[]=
49 "prevailing_def_ironly",
55 "prevailing_def_ironly_exp"
58 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at ALIAS
59 until we find an identifier that is not itself a transparent alias. */
62 ultimate_transparent_alias_target (tree alias
)
66 while (IDENTIFIER_TRANSPARENT_ALIAS (target
))
68 gcc_checking_assert (TREE_CHAIN (target
));
69 target
= TREE_CHAIN (target
);
71 gcc_checking_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target
)
72 && ! TREE_CHAIN (target
));
78 /* Hash asmnames ignoring the user specified marks. */
81 symbol_table::decl_assembler_name_hash (const_tree asmname
)
83 if (IDENTIFIER_POINTER (asmname
)[0] == '*')
85 const char *decl_str
= IDENTIFIER_POINTER (asmname
) + 1;
86 size_t ulp_len
= strlen (user_label_prefix
);
90 else if (strncmp (decl_str
, user_label_prefix
, ulp_len
) == 0)
93 return htab_hash_string (decl_str
);
96 return htab_hash_string (IDENTIFIER_POINTER (asmname
));
99 /* Return true if assembler names NAME1 and NAME2 leads to the same symbol
103 symbol_table::assembler_names_equal_p (const char *name1
, const char *name2
)
109 size_t ulp_len
= strlen (user_label_prefix
);
115 else if (strncmp (name1
, user_label_prefix
, ulp_len
) == 0)
122 size_t ulp_len
= strlen (user_label_prefix
);
128 else if (strncmp (name2
, user_label_prefix
, ulp_len
) == 0)
133 return !strcmp (name1
, name2
);
138 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
141 symbol_table::decl_assembler_name_equal (tree decl
, const_tree asmname
)
143 tree decl_asmname
= DECL_ASSEMBLER_NAME (decl
);
144 const char *decl_str
;
145 const char *asmname_str
;
147 if (decl_asmname
== asmname
)
150 decl_str
= IDENTIFIER_POINTER (decl_asmname
);
151 asmname_str
= IDENTIFIER_POINTER (asmname
);
152 return assembler_names_equal_p (decl_str
, asmname_str
);
156 /* Returns nonzero if P1 and P2 are equal. */
158 /* Insert NODE to assembler name hash. */
161 symbol_table::insert_to_assembler_name_hash (symtab_node
*node
,
164 if (is_a
<varpool_node
*> (node
) && DECL_HARD_REGISTER (node
->decl
))
166 gcc_checking_assert (!node
->previous_sharing_asm_name
167 && !node
->next_sharing_asm_name
);
168 if (assembler_name_hash
)
172 tree decl
= node
->decl
;
174 tree name
= DECL_ASSEMBLER_NAME (node
->decl
);
176 /* C++ FE can produce decls without associated assembler name and insert
177 them to symtab to hold section or TLS information. */
181 hashval_t hash
= decl_assembler_name_hash (name
);
182 aslot
= assembler_name_hash
->find_slot_with_hash (name
, hash
, INSERT
);
183 gcc_assert (*aslot
!= node
);
184 node
->next_sharing_asm_name
= (symtab_node
*)*aslot
;
186 (*aslot
)->previous_sharing_asm_name
= node
;
189 /* Update also possible inline clones sharing a decl. */
190 cnode
= dyn_cast
<cgraph_node
*> (node
);
191 if (cnode
&& cnode
->clones
&& with_clones
)
192 for (cnode
= cnode
->clones
; cnode
; cnode
= cnode
->next_sibling_clone
)
193 if (cnode
->decl
== decl
)
194 insert_to_assembler_name_hash (cnode
, true);
199 /* Remove NODE from assembler name hash. */
202 symbol_table::unlink_from_assembler_name_hash (symtab_node
*node
,
205 if (assembler_name_hash
)
208 tree decl
= node
->decl
;
210 if (node
->next_sharing_asm_name
)
211 node
->next_sharing_asm_name
->previous_sharing_asm_name
212 = node
->previous_sharing_asm_name
;
213 if (node
->previous_sharing_asm_name
)
215 node
->previous_sharing_asm_name
->next_sharing_asm_name
216 = node
->next_sharing_asm_name
;
220 tree name
= DECL_ASSEMBLER_NAME (node
->decl
);
226 hashval_t hash
= decl_assembler_name_hash (name
);
227 slot
= assembler_name_hash
->find_slot_with_hash (name
, hash
,
229 gcc_assert (*slot
== node
);
230 if (!node
->next_sharing_asm_name
)
231 assembler_name_hash
->clear_slot (slot
);
233 *slot
= node
->next_sharing_asm_name
;
235 node
->next_sharing_asm_name
= NULL
;
236 node
->previous_sharing_asm_name
= NULL
;
238 /* Update also possible inline clones sharing a decl. */
239 cnode
= dyn_cast
<cgraph_node
*> (node
);
240 if (cnode
&& cnode
->clones
&& with_clones
)
241 for (cnode
= cnode
->clones
; cnode
; cnode
= cnode
->next_sibling_clone
)
242 if (cnode
->decl
== decl
)
243 unlink_from_assembler_name_hash (cnode
, true);
247 /* Arrange node to be first in its entry of assembler_name_hash. */
250 symbol_table::symtab_prevail_in_asm_name_hash (symtab_node
*node
)
252 unlink_from_assembler_name_hash (node
, false);
253 insert_to_assembler_name_hash (node
, false);
256 /* Initalize asm name hash unless. */
259 symbol_table::symtab_initialize_asm_name_hash (void)
262 if (!assembler_name_hash
)
264 assembler_name_hash
= hash_table
<asmname_hasher
>::create_ggc (10);
265 FOR_EACH_SYMBOL (node
)
266 insert_to_assembler_name_hash (node
, false);
270 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
273 symbol_table::change_decl_assembler_name (tree decl
, tree name
)
275 symtab_node
*node
= NULL
;
277 /* We can have user ASM names on things, like global register variables, that
278 are not in the symbol table. */
279 if ((VAR_P (decl
) && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
280 || TREE_CODE (decl
) == FUNCTION_DECL
)
281 node
= symtab_node::get (decl
);
282 if (!DECL_ASSEMBLER_NAME_SET_P (decl
))
284 SET_DECL_ASSEMBLER_NAME (decl
, name
);
286 insert_to_assembler_name_hash (node
, true);
290 if (name
== DECL_ASSEMBLER_NAME (decl
))
293 tree alias
= (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl
))
294 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl
))
297 unlink_from_assembler_name_hash (node
, true);
299 const char *old_name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
300 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
))
301 && DECL_RTL_SET_P (decl
))
302 warning (0, "%qD renamed after being referenced in assembly", decl
);
304 SET_DECL_ASSEMBLER_NAME (decl
, name
);
307 IDENTIFIER_TRANSPARENT_ALIAS (name
) = 1;
308 TREE_CHAIN (name
) = alias
;
310 /* If we change assembler name, also all transparent aliases must
311 be updated. There are three kinds - those having same assembler name,
312 those being renamed in varasm.c and weakref being renamed by the
316 insert_to_assembler_name_hash (node
, true);
318 for (unsigned i
= 0; node
->iterate_direct_aliases (i
, ref
); i
++)
320 struct symtab_node
*alias
= ref
->referring
;
321 if (alias
->transparent_alias
&& !alias
->weakref
322 && symbol_table::assembler_names_equal_p
323 (old_name
, IDENTIFIER_POINTER (
324 DECL_ASSEMBLER_NAME (alias
->decl
))))
325 change_decl_assembler_name (alias
->decl
, name
);
326 else if (alias
->transparent_alias
327 && IDENTIFIER_TRANSPARENT_ALIAS (alias
->decl
))
329 gcc_assert (TREE_CHAIN (DECL_ASSEMBLER_NAME (alias
->decl
))
330 && IDENTIFIER_TRANSPARENT_ALIAS
331 (DECL_ASSEMBLER_NAME (alias
->decl
)));
333 TREE_CHAIN (DECL_ASSEMBLER_NAME (alias
->decl
)) =
334 ultimate_transparent_alias_target
335 (DECL_ASSEMBLER_NAME (node
->decl
));
337 #ifdef ASM_OUTPUT_WEAKREF
338 else gcc_assert (!alias
->transparent_alias
|| alias
->weakref
);
340 else gcc_assert (!alias
->transparent_alias
);
343 gcc_assert (!node
->transparent_alias
|| !node
->definition
345 || TREE_CHAIN (DECL_ASSEMBLER_NAME (decl
))
346 || symbol_table::assembler_names_equal_p
347 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
)),
350 (node
->get_alias_target ()->decl
))));
355 /* Hash sections by their names. */
358 section_name_hasher::hash (section_hash_entry
*n
)
360 return htab_hash_string (n
->name
);
363 /* Return true if section P1 name equals to P2. */
366 section_name_hasher::equal (section_hash_entry
*n1
, const char *name
)
368 return n1
->name
== name
|| !strcmp (n1
->name
, name
);
371 /* Add node into symbol table. This function is not used directly, but via
372 cgraph/varpool node creation routines. */
375 symtab_node::register_symbol (void)
377 symtab
->register_symbol (this);
379 if (!decl
->decl_with_vis
.symtab_node
)
380 decl
->decl_with_vis
.symtab_node
= this;
384 /* Be sure to do this last; C++ FE might create new nodes via
385 DECL_ASSEMBLER_NAME langhook! */
386 symtab
->insert_to_assembler_name_hash (this, false);
389 /* Remove NODE from same comdat group. */
392 symtab_node::remove_from_same_comdat_group (void)
394 if (same_comdat_group
)
397 for (prev
= same_comdat_group
;
398 prev
->same_comdat_group
!= this;
399 prev
= prev
->same_comdat_group
)
401 if (same_comdat_group
== prev
)
402 prev
->same_comdat_group
= NULL
;
404 prev
->same_comdat_group
= same_comdat_group
;
405 same_comdat_group
= NULL
;
406 set_comdat_group (NULL
);
410 /* Remove node from symbol table. This function is not used directly, but via
411 cgraph/varpool node removal routines. */
414 symtab_node::unregister (void)
416 remove_all_references ();
417 remove_all_referring ();
419 /* Remove reference to section. */
420 set_section_for_node (NULL
);
422 remove_from_same_comdat_group ();
424 symtab
->unregister (this);
426 /* During LTO symtab merging we temporarily corrupt decl to symtab node
428 gcc_assert (decl
->decl_with_vis
.symtab_node
|| in_lto_p
);
429 if (decl
->decl_with_vis
.symtab_node
== this)
431 symtab_node
*replacement_node
= NULL
;
432 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (this))
433 replacement_node
= cnode
->find_replacement ();
434 decl
->decl_with_vis
.symtab_node
= replacement_node
;
436 if (!is_a
<varpool_node
*> (this) || !DECL_HARD_REGISTER (decl
))
437 symtab
->unlink_from_assembler_name_hash (this, false);
438 if (in_init_priority_hash
)
439 symtab
->init_priority_hash
->remove (this);
443 /* Remove symbol from symbol table. */
446 symtab_node::remove (void)
448 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (this))
450 else if (varpool_node
*vnode
= dyn_cast
<varpool_node
*> (this))
454 /* Add NEW_ to the same comdat group that OLD is in. */
457 symtab_node::add_to_same_comdat_group (symtab_node
*old_node
)
459 gcc_assert (old_node
->get_comdat_group ());
460 gcc_assert (!same_comdat_group
);
461 gcc_assert (this != old_node
);
463 set_comdat_group (old_node
->get_comdat_group ());
464 same_comdat_group
= old_node
;
465 if (!old_node
->same_comdat_group
)
466 old_node
->same_comdat_group
= this;
470 for (n
= old_node
->same_comdat_group
;
471 n
->same_comdat_group
!= old_node
;
472 n
= n
->same_comdat_group
)
474 n
->same_comdat_group
= this;
478 /* Dissolve the same_comdat_group list in which NODE resides. */
481 symtab_node::dissolve_same_comdat_group_list (void)
483 symtab_node
*n
= this;
486 if (!same_comdat_group
)
490 next
= n
->same_comdat_group
;
491 n
->same_comdat_group
= NULL
;
492 /* Clear comdat_group for comdat locals, since
493 make_decl_local doesn't. */
494 if (!TREE_PUBLIC (n
->decl
))
495 n
->set_comdat_group (NULL
);
501 /* Return printable assembler name of NODE.
502 This function is used only for debugging. When assembler name
503 is unknown go with identifier name. */
506 symtab_node::asm_name () const
508 if (!DECL_ASSEMBLER_NAME_SET_P (decl
))
510 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
513 /* Return printable identifier name. */
516 symtab_node::name () const
518 if (!DECL_NAME (decl
))
520 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
525 return lang_hooks
.decl_printable_name (decl
, 2);
529 symtab_node::get_dump_name (bool asm_name_p
) const
532 const char *fname
= asm_name_p
? asm_name () : name ();
533 unsigned l
= strlen (fname
);
535 char *s
= (char *)ggc_internal_cleared_alloc (l
+ EXTRA
);
536 snprintf (s
, l
+ EXTRA
, "%s/%d", fname
, order
);
542 symtab_node::dump_name () const
544 return get_dump_name (false);
548 symtab_node::dump_asm_name () const
550 return get_dump_name (true);
553 /* Return ipa reference from this symtab_node to
554 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
558 symtab_node::create_reference (symtab_node
*referred_node
,
559 enum ipa_ref_use use_type
)
561 return create_reference (referred_node
, use_type
, NULL
);
565 /* Return ipa reference from this symtab_node to
566 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
567 of the use and STMT the statement (if it exists). */
570 symtab_node::create_reference (symtab_node
*referred_node
,
571 enum ipa_ref_use use_type
, gimple
*stmt
)
573 ipa_ref
*ref
= NULL
, *ref2
= NULL
;
574 ipa_ref_list
*list
, *list2
;
575 ipa_ref_t
*old_references
;
577 gcc_checking_assert (!stmt
|| is_a
<cgraph_node
*> (this));
578 gcc_checking_assert (use_type
!= IPA_REF_ALIAS
|| !stmt
);
581 old_references
= vec_safe_address (list
->references
);
582 vec_safe_grow (list
->references
, vec_safe_length (list
->references
) + 1);
583 ref
= &list
->references
->last ();
585 list2
= &referred_node
->ref_list
;
587 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
588 if(use_type
== IPA_REF_ALIAS
)
590 list2
->referring
.safe_insert (0, ref
);
591 ref
->referred_index
= 0;
593 for (unsigned int i
= 1; i
< list2
->referring
.length (); i
++)
594 list2
->referring
[i
]->referred_index
= i
;
598 list2
->referring
.safe_push (ref
);
599 ref
->referred_index
= list2
->referring
.length () - 1;
602 ref
->referring
= this;
603 ref
->referred
= referred_node
;
605 ref
->lto_stmt_uid
= 0;
607 ref
->speculative
= 0;
609 /* If vector was moved in memory, update pointers. */
610 if (old_references
!= list
->references
->address ())
613 for (i
= 0; iterate_reference(i
, ref2
); i
++)
614 ref2
->referred_ref_list ()->referring
[ref2
->referred_index
] = ref2
;
620 symtab_node::maybe_create_reference (tree val
, gimple
*stmt
)
623 ipa_ref_use use_type
;
625 switch (TREE_CODE (val
))
628 use_type
= IPA_REF_LOAD
;
631 use_type
= IPA_REF_ADDR
;
634 gcc_assert (!handled_component_p (val
));
638 val
= get_base_var (val
);
639 if (val
&& VAR_OR_FUNCTION_DECL_P (val
))
641 symtab_node
*referred
= symtab_node::get (val
);
642 gcc_checking_assert (referred
);
643 return create_reference (referred
, use_type
, stmt
);
648 /* Clone all references from symtab NODE to this symtab_node. */
651 symtab_node::clone_references (symtab_node
*node
)
653 ipa_ref
*ref
= NULL
, *ref2
= NULL
;
655 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
657 bool speculative
= ref
->speculative
;
658 unsigned int stmt_uid
= ref
->lto_stmt_uid
;
660 ref2
= create_reference (ref
->referred
, ref
->use
, ref
->stmt
);
661 ref2
->speculative
= speculative
;
662 ref2
->lto_stmt_uid
= stmt_uid
;
666 /* Clone all referring from symtab NODE to this symtab_node. */
669 symtab_node::clone_referring (symtab_node
*node
)
671 ipa_ref
*ref
= NULL
, *ref2
= NULL
;
673 for (i
= 0; node
->iterate_referring(i
, ref
); i
++)
675 bool speculative
= ref
->speculative
;
676 unsigned int stmt_uid
= ref
->lto_stmt_uid
;
678 ref2
= ref
->referring
->create_reference (this, ref
->use
, ref
->stmt
);
679 ref2
->speculative
= speculative
;
680 ref2
->lto_stmt_uid
= stmt_uid
;
684 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
687 symtab_node::clone_reference (ipa_ref
*ref
, gimple
*stmt
)
689 bool speculative
= ref
->speculative
;
690 unsigned int stmt_uid
= ref
->lto_stmt_uid
;
693 ref2
= create_reference (ref
->referred
, ref
->use
, stmt
);
694 ref2
->speculative
= speculative
;
695 ref2
->lto_stmt_uid
= stmt_uid
;
699 /* Find the structure describing a reference to REFERRED_NODE
700 and associated with statement STMT. */
703 symtab_node::find_reference (symtab_node
*referred_node
,
704 gimple
*stmt
, unsigned int lto_stmt_uid
)
709 for (i
= 0; iterate_reference (i
, r
); i
++)
710 if (r
->referred
== referred_node
712 && ((stmt
&& r
->stmt
== stmt
)
713 || (lto_stmt_uid
&& r
->lto_stmt_uid
== lto_stmt_uid
)
714 || (!stmt
&& !lto_stmt_uid
&& !r
->stmt
&& !r
->lto_stmt_uid
)))
719 /* Remove all references that are associated with statement STMT. */
722 symtab_node::remove_stmt_references (gimple
*stmt
)
727 while (iterate_reference (i
, r
))
729 r
->remove_reference ();
734 /* Remove all stmt references in non-speculative references.
735 Those are not maintained during inlining & clonning.
736 The exception are speculative references that are updated along
737 with callgraph edges associated with them. */
740 symtab_node::clear_stmts_in_references (void)
745 for (i
= 0; iterate_reference (i
, r
); i
++)
753 /* Remove all references in ref list. */
756 symtab_node::remove_all_references (void)
758 while (vec_safe_length (ref_list
.references
))
759 ref_list
.references
->last ().remove_reference ();
760 vec_free (ref_list
.references
);
763 /* Remove all referring items in ref list. */
766 symtab_node::remove_all_referring (void)
768 while (ref_list
.referring
.length ())
769 ref_list
.referring
.last ()->remove_reference ();
770 ref_list
.referring
.release ();
773 /* Dump references in ref list to FILE. */
776 symtab_node::dump_references (FILE *file
)
780 for (i
= 0; iterate_reference (i
, ref
); i
++)
782 fprintf (file
, "%s (%s)",
783 ref
->referred
->dump_asm_name (),
784 ipa_ref_use_name
[ref
->use
]);
785 if (ref
->speculative
)
786 fprintf (file
, " (speculative)");
788 fprintf (file
, "\n");
791 /* Dump referring in list to FILE. */
794 symtab_node::dump_referring (FILE *file
)
798 for (i
= 0; iterate_referring(i
, ref
); i
++)
800 fprintf (file
, "%s (%s)",
801 ref
->referring
->dump_asm_name (),
802 ipa_ref_use_name
[ref
->use
]);
803 if (ref
->speculative
)
804 fprintf (file
, " (speculative)");
806 fprintf (file
, "\n");
809 static const char * const symtab_type_names
[] = {"symbol", "function", "variable"};
811 /* Dump the visibility of the symbol. */
814 symtab_node::get_visibility_string () const
816 static const char * const visibility_types
[]
817 = { "default", "protected", "hidden", "internal" };
818 return visibility_types
[DECL_VISIBILITY (decl
)];
821 /* Dump the type_name of the symbol. */
823 symtab_node::get_symtab_type_string () const
825 return symtab_type_names
[type
];
828 /* Dump base fields of symtab nodes to F. Not to be used directly. */
831 symtab_node::dump_base (FILE *f
)
833 static const char * const visibility_types
[] = {
834 "default", "protected", "hidden", "internal"
837 fprintf (f
, "%s (%s)", dump_asm_name (), name ());
838 dump_addr (f
, " @", (void *)this);
839 fprintf (f
, "\n Type: %s", symtab_type_names
[type
]);
842 fprintf (f
, " definition");
844 fprintf (f
, " analyzed");
846 fprintf (f
, " alias");
847 if (transparent_alias
)
848 fprintf (f
, " transparent_alias");
850 fprintf (f
, " weakref");
851 if (cpp_implicit_alias
)
852 fprintf (f
, " cpp_implicit_alias");
854 fprintf (f
, " target:%s",
855 DECL_P (alias_target
)
856 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
858 : IDENTIFIER_POINTER (alias_target
));
860 fprintf (f
, "\n Body removed by symtab_remove_unreachable_nodes");
861 fprintf (f
, "\n Visibility:");
862 if (in_other_partition
)
863 fprintf (f
, " in_other_partition");
864 if (used_from_other_partition
)
865 fprintf (f
, " used_from_other_partition");
867 fprintf (f
, " force_output");
869 fprintf (f
, " forced_by_abi");
870 if (externally_visible
)
871 fprintf (f
, " externally_visible");
873 fprintf (f
, " no_reorder");
874 if (resolution
!= LDPR_UNKNOWN
)
876 ld_plugin_symbol_resolution_names
[(int)resolution
]);
877 if (TREE_ASM_WRITTEN (decl
))
878 fprintf (f
, " asm_written");
879 if (DECL_EXTERNAL (decl
))
880 fprintf (f
, " external");
881 if (TREE_PUBLIC (decl
))
882 fprintf (f
, " public");
883 if (DECL_COMMON (decl
))
884 fprintf (f
, " common");
885 if (DECL_WEAK (decl
))
886 fprintf (f
, " weak");
887 if (DECL_DLLIMPORT_P (decl
))
888 fprintf (f
, " dll_import");
889 if (DECL_COMDAT (decl
))
890 fprintf (f
, " comdat");
891 if (get_comdat_group ())
892 fprintf (f
, " comdat_group:%s",
893 IDENTIFIER_POINTER (get_comdat_group_id ()));
894 if (DECL_ONE_ONLY (decl
))
895 fprintf (f
, " one_only");
897 fprintf (f
, " section:%s",
899 if (implicit_section
)
900 fprintf (f
," (implicit_section)");
901 if (DECL_VISIBILITY_SPECIFIED (decl
))
902 fprintf (f
, " visibility_specified");
903 if (DECL_VISIBILITY (decl
))
904 fprintf (f
, " visibility:%s",
905 visibility_types
[DECL_VISIBILITY (decl
)]);
906 if (DECL_VIRTUAL_P (decl
))
907 fprintf (f
, " virtual");
908 if (DECL_ARTIFICIAL (decl
))
909 fprintf (f
, " artificial");
910 if (TREE_CODE (decl
) == FUNCTION_DECL
)
912 if (DECL_STATIC_CONSTRUCTOR (decl
))
913 fprintf (f
, " constructor");
914 if (DECL_STATIC_DESTRUCTOR (decl
))
915 fprintf (f
, " destructor");
919 if (same_comdat_group
)
920 fprintf (f
, " Same comdat group as: %s\n",
921 same_comdat_group
->dump_asm_name ());
922 if (next_sharing_asm_name
)
923 fprintf (f
, " next sharing asm name: %i\n",
924 next_sharing_asm_name
->order
);
925 if (previous_sharing_asm_name
)
926 fprintf (f
, " previous sharing asm name: %i\n",
927 previous_sharing_asm_name
->order
);
930 fprintf (f
, " Address is taken.\n");
933 fprintf (f
, " Aux:");
934 dump_addr (f
, " @", (void *)aux
);
938 fprintf (f
, " References: ");
940 fprintf (f
, " Referring: ");
943 fprintf (f
, " Read from file: %s\n",
944 lto_file_data
->file_name
);
947 /* Dump symtab node to F. */
950 symtab_node::dump (FILE *f
)
952 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (this))
954 else if (varpool_node
*vnode
= dyn_cast
<varpool_node
*> (this))
959 symbol_table::dump (FILE *f
)
962 fprintf (f
, "Symbol table:\n\n");
963 FOR_EACH_SYMBOL (node
)
968 symbol_table::debug (void)
973 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
974 Return NULL if there's no such node. */
977 symtab_node::get_for_asmname (const_tree asmname
)
981 symtab
->symtab_initialize_asm_name_hash ();
982 hashval_t hash
= symtab
->decl_assembler_name_hash (asmname
);
984 = symtab
->assembler_name_hash
->find_slot_with_hash (asmname
, hash
,
995 /* Dump symtab node NODE to stderr. */
998 symtab_node::debug (void)
1003 /* Verify common part of symtab nodes. */
1006 symtab_node::verify_base (void)
1008 bool error_found
= false;
1009 symtab_node
*hashed_node
;
1011 if (is_a
<cgraph_node
*> (this))
1013 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1015 error ("function symbol is not function");
1018 else if ((lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl
))
1020 != dyn_cast
<cgraph_node
*> (this)->ifunc_resolver
)
1022 error ("inconsistent %<ifunc%> attribute");
1026 else if (is_a
<varpool_node
*> (this))
1030 error ("variable symbol is not variable");
1036 error ("node has unknown type");
1040 if (symtab
->state
!= LTO_STREAMING
)
1042 hashed_node
= symtab_node::get (decl
);
1045 error ("node not found node->decl->decl_with_vis.symtab_node");
1048 if (hashed_node
!= this
1049 && (!is_a
<cgraph_node
*> (this)
1050 || !dyn_cast
<cgraph_node
*> (this)->clone_of
1051 || dyn_cast
<cgraph_node
*> (this)->clone_of
->decl
!= decl
))
1053 error ("node differs from node->decl->decl_with_vis.symtab_node");
1057 if (symtab
->assembler_name_hash
)
1059 hashed_node
= symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl
));
1062 if (hashed_node
->previous_sharing_asm_name
)
1064 error ("assembler name hash list corrupted");
1067 else if (previous_sharing_asm_name
== NULL
)
1069 if (hashed_node
!= this)
1071 error ("assembler name hash list corrupted");
1075 else if (!(is_a
<varpool_node
*> (this) && DECL_HARD_REGISTER (decl
)))
1077 if (!asmname_hasher::equal (previous_sharing_asm_name
,
1078 DECL_ASSEMBLER_NAME (decl
)))
1080 error ("node not found in symtab assembler name hash");
1086 if (previous_sharing_asm_name
1087 && previous_sharing_asm_name
->next_sharing_asm_name
!= this)
1089 error ("double linked list of assembler names corrupted");
1092 if (body_removed
&& definition
)
1094 error ("node has body_removed but is definition");
1097 if (analyzed
&& !definition
)
1099 error ("node is analyzed but it is not a definition");
1102 if (cpp_implicit_alias
&& !alias
)
1104 error ("node is alias but not implicit alias");
1107 if (alias
&& !definition
&& !weakref
)
1109 error ("node is alias but not definition");
1112 if (weakref
&& !transparent_alias
)
1114 error ("node is weakref but not an transparent_alias");
1117 if (transparent_alias
&& !alias
)
1119 error ("node is transparent_alias but not an alias");
1122 if (same_comdat_group
)
1124 symtab_node
*n
= same_comdat_group
;
1126 if (!n
->get_comdat_group ())
1128 error ("node is in same_comdat_group list but has no comdat_group");
1131 if (n
->get_comdat_group () != get_comdat_group ())
1133 error ("same_comdat_group list across different groups");
1136 if (n
->type
!= type
)
1138 error ("mixing different types of symbol in same comdat groups is not supported");
1143 error ("node is alone in a comdat group");
1148 if (!n
->same_comdat_group
)
1150 error ("same_comdat_group is not a circular list");
1154 n
= n
->same_comdat_group
;
1157 if (comdat_local_p ())
1159 ipa_ref
*ref
= NULL
;
1161 for (int i
= 0; iterate_referring (i
, ref
); ++i
)
1163 if (!in_same_comdat_group_p (ref
->referring
))
1165 error ("comdat-local symbol referred to by %s outside its "
1167 identifier_to_locale (ref
->referring
->name()));
1173 if (implicit_section
&& !get_section ())
1175 error ("implicit_section flag is set but section isn%'t");
1178 if (get_section () && get_comdat_group ()
1179 && !implicit_section
1180 && !lookup_attribute ("section", DECL_ATTRIBUTES (decl
)))
1182 error ("Both section and comdat group is set");
1185 /* TODO: Add string table for sections, so we do not keep holding duplicated
1187 if (alias
&& definition
1188 && get_section () != get_alias_target ()->get_section ()
1190 || !get_alias_target ()->get_section ()
1191 || strcmp (get_section(),
1192 get_alias_target ()->get_section ())))
1194 error ("Alias and target%'s section differs");
1195 get_alias_target ()->dump (stderr
);
1198 if (alias
&& definition
1199 && get_comdat_group () != get_alias_target ()->get_comdat_group ())
1201 error ("Alias and target%'s comdat groups differs");
1202 get_alias_target ()->dump (stderr
);
1205 if (transparent_alias
&& definition
&& !weakref
)
1207 symtab_node
*to
= get_alias_target ();
1209 = IDENTIFIER_POINTER (
1210 ultimate_transparent_alias_target (DECL_ASSEMBLER_NAME (decl
)));
1212 = IDENTIFIER_POINTER (
1213 ultimate_transparent_alias_target (DECL_ASSEMBLER_NAME (to
->decl
)));
1214 if (!symbol_table::assembler_names_equal_p (name1
, name2
))
1216 error ("Transparent alias and target%'s assembler names differs");
1217 get_alias_target ()->dump (stderr
);
1221 if (transparent_alias
&& definition
1222 && get_alias_target()->transparent_alias
&& get_alias_target()->analyzed
)
1224 error ("Chained transparent aliases");
1225 get_alias_target ()->dump (stderr
);
1232 /* Verify consistency of NODE. */
1235 symtab_node::verify (void)
1240 timevar_push (TV_CGRAPH_VERIFY
);
1241 if (cgraph_node
*node
= dyn_cast
<cgraph_node
*> (this))
1242 node
->verify_node ();
1247 internal_error ("symtab_node::verify failed");
1249 timevar_pop (TV_CGRAPH_VERIFY
);
1252 /* Verify symbol table for internal consistency. */
1255 symtab_node::verify_symtab_nodes (void)
1258 hash_map
<tree
, symtab_node
*> comdat_head_map (251);
1260 FOR_EACH_SYMBOL (node
)
1263 if (node
->get_comdat_group ())
1265 symtab_node
**entry
, *s
;
1268 entry
= &comdat_head_map
.get_or_insert (node
->get_comdat_group (),
1272 else if (!DECL_EXTERNAL (node
->decl
))
1274 for (s
= (*entry
)->same_comdat_group
;
1275 s
!= NULL
&& s
!= node
&& s
!= *entry
;
1276 s
= s
->same_comdat_group
)
1278 if (!s
|| s
== *entry
)
1280 error ("Two symbols with same comdat_group are not linked by "
1281 "the same_comdat_group list.");
1284 internal_error ("symtab_node::verify failed");
1291 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1292 but other code such as notice_global_symbol generates rtl. */
1295 symtab_node::make_decl_local (void)
1302 IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl
)) = 0;
1303 TREE_CHAIN (DECL_ASSEMBLER_NAME (decl
)) = NULL_TREE
;
1304 symtab
->change_decl_assembler_name
1305 (decl
, DECL_ASSEMBLER_NAME (get_alias_target ()->decl
));
1306 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
1307 DECL_ATTRIBUTES (decl
));
1309 /* Avoid clearing comdat_groups on comdat-local decls. */
1310 else if (TREE_PUBLIC (decl
) == 0)
1313 /* Localizing a symbol also make all its transparent aliases local. */
1315 for (unsigned i
= 0; iterate_direct_aliases (i
, ref
); i
++)
1317 struct symtab_node
*alias
= ref
->referring
;
1318 if (alias
->transparent_alias
)
1319 alias
->make_decl_local ();
1324 DECL_COMMON (decl
) = 0;
1325 /* ADDRESSABLE flag is not defined for public symbols. */
1326 TREE_ADDRESSABLE (decl
) = 1;
1327 TREE_STATIC (decl
) = 1;
1330 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
1332 DECL_COMDAT (decl
) = 0;
1333 DECL_WEAK (decl
) = 0;
1334 DECL_EXTERNAL (decl
) = 0;
1335 DECL_VISIBILITY_SPECIFIED (decl
) = 0;
1336 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
1337 TREE_PUBLIC (decl
) = 0;
1338 DECL_DLLIMPORT_P (decl
) = 0;
1339 if (!DECL_RTL_SET_P (decl
))
1342 /* Update rtl flags. */
1343 make_decl_rtl (decl
);
1345 rtl
= DECL_RTL (decl
);
1349 symbol
= XEXP (rtl
, 0);
1350 if (GET_CODE (symbol
) != SYMBOL_REF
)
1353 SYMBOL_REF_WEAK (symbol
) = DECL_WEAK (decl
);
1356 /* Copy visibility from N.
1357 This is useful when THIS becomes a transparent alias of N. */
1360 symtab_node::copy_visibility_from (symtab_node
*n
)
1362 gcc_checking_assert (n
->weakref
== weakref
);
1365 for (unsigned i
= 0; iterate_direct_aliases (i
, ref
); i
++)
1367 struct symtab_node
*alias
= ref
->referring
;
1368 if (alias
->transparent_alias
)
1369 alias
->copy_visibility_from (n
);
1374 DECL_COMMON (decl
) = DECL_COMMON (n
->decl
);
1375 /* ADDRESSABLE flag is not defined for public symbols. */
1376 if (TREE_PUBLIC (decl
) && !TREE_PUBLIC (n
->decl
))
1377 TREE_ADDRESSABLE (decl
) = 1;
1378 TREE_STATIC (decl
) = TREE_STATIC (n
->decl
);
1380 else gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
1382 DECL_COMDAT (decl
) = DECL_COMDAT (n
->decl
);
1383 DECL_WEAK (decl
) = DECL_WEAK (n
->decl
);
1384 DECL_EXTERNAL (decl
) = DECL_EXTERNAL (n
->decl
);
1385 DECL_VISIBILITY_SPECIFIED (decl
) = DECL_VISIBILITY_SPECIFIED (n
->decl
);
1386 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (n
->decl
);
1387 TREE_PUBLIC (decl
) = TREE_PUBLIC (n
->decl
);
1388 DECL_DLLIMPORT_P (decl
) = DECL_DLLIMPORT_P (n
->decl
);
1389 resolution
= n
->resolution
;
1390 set_comdat_group (n
->get_comdat_group ());
1391 call_for_symbol_and_aliases (symtab_node::set_section
,
1392 const_cast<char *>(n
->get_section ()), true);
1393 externally_visible
= n
->externally_visible
;
1394 if (!DECL_RTL_SET_P (decl
))
1397 /* Update rtl flags. */
1398 make_decl_rtl (decl
);
1400 rtx rtl
= DECL_RTL (decl
);
1404 rtx symbol
= XEXP (rtl
, 0);
1405 if (GET_CODE (symbol
) != SYMBOL_REF
)
1408 SYMBOL_REF_WEAK (symbol
) = DECL_WEAK (decl
);
1411 /* Walk the alias chain to return the symbol NODE is alias of.
1412 If NODE is not an alias, return NODE.
1413 Assumes NODE is known to be alias. */
1416 symtab_node::ultimate_alias_target_1 (enum availability
*availability
,
1419 bool transparent_p
= false;
1421 /* To determine visibility of the target, we follow ELF semantic of aliases.
1422 Here alias is an alternative assembler name of a given definition. Its
1423 availability prevails the availability of its target (i.e. static alias of
1424 weak definition is available.
1426 Transaparent alias is just alternative anme of a given symbol used within
1427 one compilation unit and is translated prior hitting the object file. It
1428 inherits the visibility of its target.
1429 Weakref is a different animal (and noweak definition is weak).
1431 If we ever get into supporting targets with different semantics, a target
1432 hook will be needed here. */
1436 transparent_p
= transparent_alias
;
1438 *availability
= get_availability (ref
);
1440 *availability
= AVAIL_NOT_AVAILABLE
;
1443 symtab_node
*node
= this;
1446 if (node
->alias
&& node
->analyzed
)
1447 node
= node
->get_alias_target ();
1450 if (!availability
|| (!transparent_p
&& node
->analyzed
))
1452 else if (node
->analyzed
&& !node
->transparent_alias
)
1453 *availability
= node
->get_availability (ref
);
1455 *availability
= AVAIL_NOT_AVAILABLE
;
1458 if (node
&& availability
&& transparent_p
1459 && node
->transparent_alias
)
1461 *availability
= node
->get_availability (ref
);
1462 transparent_p
= false;
1466 *availability
= AVAIL_NOT_AVAILABLE
;
1470 /* C++ FE sometimes change linkage flags after producing same body aliases.
1472 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1473 are obviously equivalent. The way it is doing so is however somewhat
1474 kludgy and interferes with the visibility code. As a result we need to
1475 copy the visibility from the target to get things right. */
1478 symtab_node::fixup_same_cpp_alias_visibility (symtab_node
*target
)
1480 if (is_a
<cgraph_node
*> (this))
1482 DECL_DECLARED_INLINE_P (decl
)
1483 = DECL_DECLARED_INLINE_P (target
->decl
);
1484 DECL_DISREGARD_INLINE_LIMITS (decl
)
1485 = DECL_DISREGARD_INLINE_LIMITS (target
->decl
);
1487 /* FIXME: It is not really clear why those flags should not be copied for
1491 DECL_WEAK (decl
) = DECL_WEAK (target
->decl
);
1492 DECL_EXTERNAL (decl
) = DECL_EXTERNAL (target
->decl
);
1493 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (target
->decl
);
1495 if (TREE_PUBLIC (decl
))
1499 DECL_EXTERNAL (decl
) = DECL_EXTERNAL (target
->decl
);
1500 DECL_COMDAT (decl
) = DECL_COMDAT (target
->decl
);
1501 group
= target
->get_comdat_group ();
1502 set_comdat_group (group
);
1503 if (group
&& !same_comdat_group
)
1504 add_to_same_comdat_group (target
);
1506 externally_visible
= target
->externally_visible
;
1509 /* Set section, do not recurse into aliases.
1510 When one wants to change section of a symbol and its aliases,
1514 symtab_node::set_section_for_node (const char *section
)
1516 const char *current
= get_section ();
1517 section_hash_entry
**slot
;
1519 if (current
== section
1520 || (current
&& section
1521 && !strcmp (current
, section
)))
1526 x_section
->ref_count
--;
1527 if (!x_section
->ref_count
)
1529 hashval_t hash
= htab_hash_string (x_section
->name
);
1530 slot
= symtab
->section_hash
->find_slot_with_hash (x_section
->name
,
1532 ggc_free (x_section
);
1533 symtab
->section_hash
->clear_slot (slot
);
1539 implicit_section
= false;
1542 if (!symtab
->section_hash
)
1543 symtab
->section_hash
= hash_table
<section_name_hasher
>::create_ggc (10);
1544 slot
= symtab
->section_hash
->find_slot_with_hash (section
,
1545 htab_hash_string (section
),
1548 x_section
= (section_hash_entry
*)*slot
;
1551 int len
= strlen (section
);
1552 *slot
= x_section
= ggc_cleared_alloc
<section_hash_entry
> ();
1553 x_section
->name
= ggc_vec_alloc
<char> (len
+ 1);
1554 memcpy (x_section
->name
, section
, len
+ 1);
1556 x_section
->ref_count
++;
1559 /* Worker for set_section. */
1562 symtab_node::set_section (symtab_node
*n
, void *s
)
1564 n
->set_section_for_node ((char *)s
);
1568 /* Set section of symbol and its aliases. */
1571 symtab_node::set_section (const char *section
)
1573 gcc_assert (!this->alias
);
1574 call_for_symbol_and_aliases
1575 (symtab_node::set_section
, const_cast<char *>(section
), true);
1578 /* Return the initialization priority. */
1581 symtab_node::get_init_priority ()
1583 if (!this->in_init_priority_hash
)
1584 return DEFAULT_INIT_PRIORITY
;
1586 symbol_priority_map
*h
= symtab
->init_priority_hash
->get (this);
1587 return h
? h
->init
: DEFAULT_INIT_PRIORITY
;
1590 /* Return the finalization priority. */
1593 cgraph_node::get_fini_priority ()
1595 if (!this->in_init_priority_hash
)
1596 return DEFAULT_INIT_PRIORITY
;
1597 symbol_priority_map
*h
= symtab
->init_priority_hash
->get (this);
1598 return h
? h
->fini
: DEFAULT_INIT_PRIORITY
;
1601 /* Return the initialization and finalization priority information for
1602 DECL. If there is no previous priority information, a freshly
1603 allocated structure is returned. */
1605 symbol_priority_map
*
1606 symtab_node::priority_info (void)
1608 if (!symtab
->init_priority_hash
)
1609 symtab
->init_priority_hash
= hash_map
<symtab_node
*, symbol_priority_map
>::create_ggc (13);
1612 symbol_priority_map
*h
1613 = &symtab
->init_priority_hash
->get_or_insert (this, &existed
);
1616 h
->init
= DEFAULT_INIT_PRIORITY
;
1617 h
->fini
= DEFAULT_INIT_PRIORITY
;
1618 in_init_priority_hash
= true;
1624 /* Set initialization priority to PRIORITY. */
1627 symtab_node::set_init_priority (priority_type priority
)
1629 symbol_priority_map
*h
;
1631 if (is_a
<cgraph_node
*> (this))
1632 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl
));
1634 if (priority
== DEFAULT_INIT_PRIORITY
)
1636 gcc_assert (get_init_priority() == priority
);
1639 h
= priority_info ();
1643 /* Set fialization priority to PRIORITY. */
1646 cgraph_node::set_fini_priority (priority_type priority
)
1648 symbol_priority_map
*h
;
1650 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl
));
1652 if (priority
== DEFAULT_INIT_PRIORITY
)
1654 gcc_assert (get_fini_priority() == priority
);
1657 h
= priority_info ();
1661 /* Worker for symtab_resolve_alias. */
1664 symtab_node::set_implicit_section (symtab_node
*n
,
1665 void *data ATTRIBUTE_UNUSED
)
1667 n
->implicit_section
= true;
1671 /* Add reference recording that symtab node is alias of TARGET.
1672 The function can fail in the case of aliasing cycles; in this case
1673 it returns false. */
1676 symtab_node::resolve_alias (symtab_node
*target
, bool transparent
)
1680 gcc_assert (!analyzed
&& !vec_safe_length (ref_list
.references
));
1682 /* Never let cycles to creep into the symbol table alias references;
1683 those will make alias walkers to be infinite. */
1684 for (n
= target
; n
&& n
->alias
;
1685 n
= n
->analyzed
? n
->get_alias_target () : NULL
)
1688 if (is_a
<cgraph_node
*> (this))
1689 error ("function %q+D part of alias cycle", decl
);
1690 else if (is_a
<varpool_node
*> (this))
1691 error ("variable %q+D part of alias cycle", decl
);
1698 /* "analyze" the node - i.e. mark the reference. */
1702 transparent
|= transparent_alias
;
1703 transparent_alias
= transparent
;
1705 while (target
->transparent_alias
&& target
->analyzed
)
1706 target
= target
->get_alias_target ();
1707 create_reference (target
, IPA_REF_ALIAS
, NULL
);
1709 /* Add alias into the comdat group of its target unless it is already there. */
1710 if (same_comdat_group
)
1711 remove_from_same_comdat_group ();
1712 set_comdat_group (NULL
);
1713 if (target
->get_comdat_group ())
1714 add_to_same_comdat_group (target
);
1716 if ((get_section () != target
->get_section ()
1717 || target
->get_comdat_group ()) && get_section () && !implicit_section
)
1719 error ("section of alias %q+D must match section of its target", decl
);
1721 call_for_symbol_and_aliases (symtab_node::set_section
,
1722 const_cast<char *>(target
->get_section ()), true);
1723 if (target
->implicit_section
)
1724 call_for_symbol_and_aliases (set_implicit_section
, NULL
, true);
1726 /* Alias targets become redundant after alias is resolved into an reference.
1727 We do not want to keep it around or we would have to mind updating them
1728 when renaming symbols. */
1729 alias_target
= NULL
;
1731 if (!transparent
&& cpp_implicit_alias
&& symtab
->state
>= CONSTRUCTION
)
1732 fixup_same_cpp_alias_visibility (target
);
1734 /* If alias has address taken, so does the target. */
1736 target
->ultimate_alias_target ()->address_taken
= true;
1738 /* All non-transparent aliases of THIS are now in fact aliases of TARGET.
1739 If alias is transparent, also all transparent aliases of THIS are now
1741 Also merge same comdat group lists. */
1743 for (unsigned i
= 0; iterate_direct_aliases (i
, ref
);)
1745 struct symtab_node
*alias_alias
= ref
->referring
;
1746 if (alias_alias
->get_comdat_group ())
1748 alias_alias
->remove_from_same_comdat_group ();
1749 alias_alias
->set_comdat_group (NULL
);
1750 if (target
->get_comdat_group ())
1751 alias_alias
->add_to_same_comdat_group (target
);
1753 if (!alias_alias
->transparent_alias
|| transparent
)
1755 alias_alias
->remove_all_references ();
1756 alias_alias
->create_reference (target
, IPA_REF_ALIAS
, NULL
);
1763 /* Worker searching noninterposable alias. */
1766 symtab_node::noninterposable_alias (symtab_node
*node
, void *data
)
1768 if (!node
->transparent_alias
&& decl_binds_to_current_def_p (node
->decl
))
1770 symtab_node
*fn
= node
->ultimate_alias_target ();
1772 /* Ensure that the alias is well formed this may not be the case
1773 of user defined aliases and currently it is not always the case
1774 of C++ same body aliases (that is a bug). */
1775 if (TREE_TYPE (node
->decl
) != TREE_TYPE (fn
->decl
)
1776 || DECL_CONTEXT (node
->decl
) != DECL_CONTEXT (fn
->decl
)
1777 || (TREE_CODE (node
->decl
) == FUNCTION_DECL
1778 && flags_from_decl_or_type (node
->decl
)
1779 != flags_from_decl_or_type (fn
->decl
))
1780 || DECL_ATTRIBUTES (node
->decl
) != DECL_ATTRIBUTES (fn
->decl
))
1782 *(symtab_node
**)data
= node
;
1788 /* If node cannot be overwriten by static or dynamic linker to point to
1789 different definition, return NODE. Otherwise look for alias with such
1790 property and if none exists, introduce new one. */
1793 symtab_node::noninterposable_alias (void)
1796 symtab_node
*new_node
= NULL
;
1798 /* First try to look up existing alias or base object
1799 (if that is already non-overwritable). */
1800 symtab_node
*node
= ultimate_alias_target ();
1801 gcc_assert (!node
->alias
&& !node
->weakref
);
1802 node
->call_for_symbol_and_aliases (symtab_node::noninterposable_alias
,
1803 (void *)&new_node
, true);
1807 /* If aliases aren't supported by the assembler, fail. */
1808 if (!TARGET_SUPPORTS_ALIASES
)
1811 /* Otherwise create a new one. */
1812 new_decl
= copy_node (node
->decl
);
1813 DECL_DLLIMPORT_P (new_decl
) = 0;
1814 DECL_NAME (new_decl
) = clone_function_name (node
->decl
, "localalias");
1815 if (TREE_CODE (new_decl
) == FUNCTION_DECL
)
1816 DECL_STRUCT_FUNCTION (new_decl
) = NULL
;
1817 DECL_INITIAL (new_decl
) = NULL
;
1818 SET_DECL_ASSEMBLER_NAME (new_decl
, DECL_NAME (new_decl
));
1819 SET_DECL_RTL (new_decl
, NULL
);
1821 /* Update the properties. */
1822 DECL_EXTERNAL (new_decl
) = 0;
1823 TREE_PUBLIC (new_decl
) = 0;
1824 DECL_COMDAT (new_decl
) = 0;
1825 DECL_WEAK (new_decl
) = 0;
1827 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1828 DECL_VIRTUAL_P (new_decl
) = DECL_VIRTUAL_P (node
->decl
);
1829 if (TREE_CODE (new_decl
) == FUNCTION_DECL
)
1831 DECL_STATIC_CONSTRUCTOR (new_decl
) = 0;
1832 DECL_STATIC_DESTRUCTOR (new_decl
) = 0;
1833 new_node
= cgraph_node::create_alias (new_decl
, node
->decl
);
1837 TREE_READONLY (new_decl
) = TREE_READONLY (node
->decl
);
1838 DECL_INITIAL (new_decl
) = error_mark_node
;
1839 new_node
= varpool_node::create_alias (new_decl
, node
->decl
);
1841 new_node
->resolve_alias (node
);
1842 gcc_assert (decl_binds_to_current_def_p (new_decl
)
1843 && targetm
.binds_local_p (new_decl
));
1847 /* Return true if symtab node and TARGET represents
1848 semantically equivalent symbols. */
1851 symtab_node::semantically_equivalent_p (symtab_node
*target
)
1853 enum availability avail
;
1857 /* Equivalent functions are equivalent. */
1858 if (decl
== target
->decl
)
1861 /* If symbol is not overwritable by different implementation,
1862 walk to the base object it defines. */
1863 ba
= ultimate_alias_target (&avail
);
1864 if (avail
>= AVAIL_AVAILABLE
)
1871 bb
= target
->ultimate_alias_target (&avail
);
1872 if (avail
>= AVAIL_AVAILABLE
)
1882 /* Classify symbol symtab node for partitioning. */
1884 enum symbol_partitioning_class
1885 symtab_node::get_partitioning_class (void)
1887 /* Inline clones are always duplicated.
1888 This include external delcarations. */
1889 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (this);
1891 if (DECL_ABSTRACT_P (decl
))
1892 return SYMBOL_EXTERNAL
;
1894 if (cnode
&& cnode
->global
.inlined_to
)
1895 return SYMBOL_DUPLICATE
;
1897 /* Transparent aliases are always duplicated. */
1898 if (transparent_alias
)
1899 return definition
? SYMBOL_DUPLICATE
: SYMBOL_EXTERNAL
;
1901 /* External declarations are external. */
1902 if (DECL_EXTERNAL (decl
))
1903 return SYMBOL_EXTERNAL
;
1905 if (varpool_node
*vnode
= dyn_cast
<varpool_node
*> (this))
1907 if (alias
&& definition
&& !ultimate_alias_target ()->definition
)
1908 return SYMBOL_EXTERNAL
;
1909 /* Constant pool references use local symbol names that cannot
1910 be promoted global. We should never put into a constant pool
1911 objects that cannot be duplicated across partitions. */
1912 if (DECL_IN_CONSTANT_POOL (decl
))
1913 return SYMBOL_DUPLICATE
;
1914 if (DECL_HARD_REGISTER (decl
))
1915 return SYMBOL_DUPLICATE
;
1916 gcc_checking_assert (vnode
->definition
);
1918 /* Functions that are cloned may stay in callgraph even if they are unused.
1919 Handle them as external; compute_ltrans_boundary take care to make
1920 proper things to happen (i.e. to make them appear in the boundary but
1921 with body streamed, so clone can me materialized). */
1922 else if (!dyn_cast
<cgraph_node
*> (this)->function_symbol ()->definition
)
1923 return SYMBOL_EXTERNAL
;
1925 /* Linker discardable symbols are duplicated to every use unless they are
1927 if (DECL_ONE_ONLY (decl
)
1930 && !used_from_object_file_p ())
1931 return SYMBOL_DUPLICATE
;
1933 return SYMBOL_PARTITION
;
1936 /* Return true when symbol is known to be non-zero. */
1939 symtab_node::nonzero_address ()
1941 /* Weakrefs may be NULL when their target is not defined. */
1942 if (alias
&& weakref
)
1946 symtab_node
*target
= ultimate_alias_target ();
1948 if (target
->alias
&& target
->weakref
)
1950 /* We cannot recurse to target::nonzero. It is possible that the
1951 target is used only via the alias.
1952 We may walk references and look for strong use, but we do not know
1953 if this strong use will survive to final binary, so be
1955 ??? Maybe we could do the lookup during late optimization that
1956 could be useful to eliminate the NULL pointer checks in LTO
1958 if (target
->definition
&& !DECL_EXTERNAL (target
->decl
))
1960 if (target
->resolution
!= LDPR_UNKNOWN
1961 && target
->resolution
!= LDPR_UNDEF
1962 && !target
->can_be_discarded_p ()
1963 && flag_delete_null_pointer_checks
)
1971 /* With !flag_delete_null_pointer_checks we assume that symbols may
1972 bind to NULL. This is on by default on embedded targets only.
1974 Otherwise all non-WEAK symbols must be defined and thus non-NULL or
1975 linking fails. Important case of WEAK we want to do well are comdats.
1976 Those are handled by later check for definition.
1978 When parsing, beware the cases when WEAK attribute is added later. */
1979 if (!DECL_WEAK (decl
)
1980 && flag_delete_null_pointer_checks
)
1982 refuse_visibility_changes
= true;
1986 /* If target is defined and either comdat or not extern, we know it will be
1987 output and thus it will bind to non-NULL.
1988 Play safe for flag_delete_null_pointer_checks where weak definition may
1989 be re-defined by NULL. */
1990 if (definition
&& (!DECL_EXTERNAL (decl
) || DECL_COMDAT (decl
))
1991 && (flag_delete_null_pointer_checks
|| !DECL_WEAK (decl
)))
1993 if (!DECL_WEAK (decl
))
1994 refuse_visibility_changes
= true;
1998 /* As the last resort, check the resolution info. */
1999 if (resolution
!= LDPR_UNKNOWN
2000 && resolution
!= LDPR_UNDEF
2001 && !can_be_discarded_p ()
2002 && flag_delete_null_pointer_checks
)
2007 /* Return 0 if symbol is known to have different address than S2,
2008 Return 1 if symbol is known to have same address as S2,
2009 return -1 otherwise.
2011 If MEMORY_ACCESSED is true, assume that both memory pointer to THIS
2012 and S2 is going to be accessed. This eliminates the situations when
2013 either THIS or S2 is NULL and is seful for comparing bases when deciding
2014 about memory aliasing. */
2016 symtab_node::equal_address_to (symtab_node
*s2
, bool memory_accessed
)
2018 enum availability avail1
, avail2
;
2020 /* A Shortcut: equivalent symbols are always equivalent. */
2024 /* Unwind transparent aliases first; those are always equal to their
2026 if (this->transparent_alias
&& this->analyzed
)
2027 return this->get_alias_target ()->equal_address_to (s2
);
2028 while (s2
->transparent_alias
&& s2
->analyzed
)
2029 s2
= s2
->get_alias_target();
2034 /* For non-interposable aliases, lookup and compare their actual definitions.
2035 Also check if the symbol needs to bind to given definition. */
2036 symtab_node
*rs1
= ultimate_alias_target (&avail1
);
2037 symtab_node
*rs2
= s2
->ultimate_alias_target (&avail2
);
2038 bool binds_local1
= rs1
->analyzed
&& decl_binds_to_current_def_p (this->decl
);
2039 bool binds_local2
= rs2
->analyzed
&& decl_binds_to_current_def_p (s2
->decl
);
2040 bool really_binds_local1
= binds_local1
;
2041 bool really_binds_local2
= binds_local2
;
2043 /* Addresses of vtables and virtual functions cannot be used by user
2044 code and are used only within speculation. In this case we may make
2045 symbol equivalent to its alias even if interposition may break this
2046 rule. Doing so will allow us to turn speculative inlining into
2047 non-speculative more agressively. */
2048 if (DECL_VIRTUAL_P (this->decl
) && avail1
>= AVAIL_AVAILABLE
)
2049 binds_local1
= true;
2050 if (DECL_VIRTUAL_P (s2
->decl
) && avail2
>= AVAIL_AVAILABLE
)
2051 binds_local2
= true;
2053 /* If both definitions are available we know that even if they are bound
2054 to other unit they must be defined same way and therefore we can use
2055 equivalence test. */
2056 if (rs1
!= rs2
&& avail1
>= AVAIL_AVAILABLE
&& avail2
>= AVAIL_AVAILABLE
)
2057 binds_local1
= binds_local2
= true;
2059 if (binds_local1
&& binds_local2
&& rs1
== rs2
)
2061 /* We made use of the fact that alias is not weak. */
2063 refuse_visibility_changes
= true;
2065 s2
->refuse_visibility_changes
= true;
2069 /* If both symbols may resolve to NULL, we cannot really prove them
2071 if (!memory_accessed
&& !nonzero_address () && !s2
->nonzero_address ())
2074 /* Except for NULL, functions and variables never overlap. */
2075 if (TREE_CODE (decl
) != TREE_CODE (s2
->decl
))
2078 /* If one of the symbols is unresolved alias, punt. */
2079 if (rs1
->alias
|| rs2
->alias
)
2082 /* If we have a non-interposale definition of at least one of the symbols
2083 and the other symbol is different, we know other unit cannot interpose
2084 it to the first symbol; all aliases of the definition needs to be
2085 present in the current unit. */
2086 if (((really_binds_local1
|| really_binds_local2
)
2087 /* If we have both definitions and they are different, we know they
2088 will be different even in units they binds to. */
2089 || (binds_local1
&& binds_local2
))
2092 /* We make use of the fact that one symbol is not alias of the other
2093 and that the definition is non-interposable. */
2094 refuse_visibility_changes
= true;
2095 s2
->refuse_visibility_changes
= true;
2096 rs1
->refuse_visibility_changes
= true;
2097 rs2
->refuse_visibility_changes
= true;
2101 /* TODO: Alias oracle basically assume that addresses of global variables
2102 are different unless they are declared as alias of one to another while
2103 the code folding comparsions doesn't.
2104 We probably should be consistent and use this fact here, too, but for
2105 the moment return false only when we are called from the alias oracle. */
2107 return memory_accessed
&& rs1
!= rs2
? 0 : -1;
2110 /* Worker for call_for_symbol_and_aliases. */
2113 symtab_node::call_for_symbol_and_aliases_1 (bool (*callback
) (symtab_node
*,
2116 bool include_overwritable
)
2119 FOR_EACH_ALIAS (this, ref
)
2121 symtab_node
*alias
= ref
->referring
;
2122 if (include_overwritable
2123 || alias
->get_availability () > AVAIL_INTERPOSABLE
)
2124 if (alias
->call_for_symbol_and_aliases (callback
, data
,
2125 include_overwritable
))
2131 /* Return true if address of N is possibly compared. */
2134 address_matters_1 (symtab_node
*n
, void *)
2136 struct ipa_ref
*ref
;
2138 if (!n
->address_can_be_compared_p ())
2140 if (n
->externally_visible
|| n
->force_output
)
2143 for (unsigned int i
= 0; n
->iterate_referring (i
, ref
); i
++)
2144 if (ref
->address_matters_p ())
2149 /* Return true if symbol's address may possibly be compared to other
2150 symbol's address. */
2153 symtab_node::address_matters_p ()
2155 gcc_assert (!alias
);
2156 return call_for_symbol_and_aliases (address_matters_1
, NULL
, true);
2159 /* Return true if symbol's alignment may be increased. */
2162 symtab_node::can_increase_alignment_p (void)
2164 symtab_node
*target
= ultimate_alias_target ();
2166 /* For now support only variables. */
2170 /* With -fno-toplevel-reorder we may have already output the constant. */
2171 if (TREE_ASM_WRITTEN (target
->decl
))
2174 /* If target is already placed in an anchor, we cannot touch its
2176 if (DECL_RTL_SET_P (target
->decl
)
2177 && MEM_P (DECL_RTL (target
->decl
))
2178 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (DECL_RTL (target
->decl
), 0)))
2181 /* Constant pool entries may be shared. */
2182 if (DECL_IN_CONSTANT_POOL (target
->decl
))
2185 /* We cannot change alignment of symbols that may bind to symbols
2186 in other translation unit that may contain a definition with lower
2188 if (!decl_binds_to_current_def_p (decl
))
2191 /* When compiling partition, be sure the symbol is not output by other
2194 && (target
->in_other_partition
2195 || target
->get_partitioning_class () == SYMBOL_DUPLICATE
))
2198 /* Do not override the alignment as specified by the ABI when the used
2199 attribute is set. */
2200 if (DECL_PRESERVE_P (decl
) || DECL_PRESERVE_P (target
->decl
))
2203 /* Do not override explicit alignment set by the user when an explicit
2204 section name is also used. This is a common idiom used by many
2205 software projects. */
2206 if (DECL_SECTION_NAME (target
->decl
) != NULL
&& !target
->implicit_section
)
2212 /* Worker for symtab_node::increase_alignment. */
2215 increase_alignment_1 (symtab_node
*n
, void *v
)
2217 unsigned int align
= (size_t)v
;
2218 if (DECL_ALIGN (n
->decl
) < align
2219 && n
->can_increase_alignment_p ())
2221 SET_DECL_ALIGN (n
->decl
, align
);
2222 DECL_USER_ALIGN (n
->decl
) = 1;
2227 /* Increase alignment of THIS to ALIGN. */
2230 symtab_node::increase_alignment (unsigned int align
)
2232 gcc_assert (can_increase_alignment_p () && align
<= MAX_OFILE_ALIGNMENT
);
2233 ultimate_alias_target()->call_for_symbol_and_aliases (increase_alignment_1
,
2234 (void *)(size_t) align
,
2236 gcc_assert (DECL_ALIGN (decl
) >= align
);
2239 /* Helper for symtab_node::definition_alignment. */
2242 get_alignment_1 (symtab_node
*n
, void *v
)
2244 *((unsigned int *)v
) = MAX (*((unsigned int *)v
), DECL_ALIGN (n
->decl
));
2248 /* Return desired alignment of the definition. This is NOT alignment useful
2249 to access THIS, because THIS may be interposable and DECL_ALIGN should
2250 be used instead. It however must be guaranteed when output definition
2254 symtab_node::definition_alignment ()
2256 unsigned int align
= 0;
2257 gcc_assert (!alias
);
2258 call_for_symbol_and_aliases (get_alignment_1
, &align
, true);
2262 /* Return symbol used to separate symbol name from suffix. */
2265 symbol_table::symbol_suffix_separator ()
2267 #ifndef NO_DOT_IN_LABEL
2269 #elif !defined NO_DOLLAR_IN_LABEL
2276 /* Return true when references to this symbol from REF must bind to current
2277 definition in final executable. */
2280 symtab_node::binds_to_current_def_p (symtab_node
*ref
)
2284 if (transparent_alias
)
2286 && get_alias_target()->binds_to_current_def_p (ref
);
2287 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (this);
2288 if (cnode
&& cnode
->ifunc_resolver
)
2290 if (decl_binds_to_current_def_p (decl
))
2293 /* Inline clones always binds locally. */
2294 if (cnode
&& cnode
->global
.inlined_to
)
2297 if (DECL_EXTERNAL (decl
))
2300 gcc_assert (externally_visible
);
2304 cgraph_node
*cref
= dyn_cast
<cgraph_node
*> (ref
);
2306 ref
= cref
->global
.inlined_to
;
2309 /* If this is a reference from symbol itself and there are no aliases, we
2310 may be sure that the symbol was not interposed by something else because
2311 the symbol itself would be unreachable otherwise. This is important
2312 to optimize recursive functions well.
2314 This assumption may be broken by inlining: if symbol is interposable
2315 but the body is available (i.e. declared inline), inliner may make
2316 the body reachable even with interposition. */
2317 if (this == ref
&& !has_aliases_p ()
2319 || symtab
->state
>= IPA_SSA_AFTER_INLINING
2320 || get_availability () >= AVAIL_INTERPOSABLE
))
2324 /* References within one comdat group are always bound in a group. */
2326 && symtab
->state
>= IPA_SSA_AFTER_INLINING
2327 && get_comdat_group ()
2328 && get_comdat_group () == ref
->get_comdat_group ())
2334 /* Return true if symbol should be output to the symbol table. */
2337 symtab_node::output_to_lto_symbol_table_p (void)
2339 /* Only externally visible symbols matter. */
2340 if (!TREE_PUBLIC (decl
))
2342 if (!real_symbol_p ())
2344 /* FIXME: variables probably should not be considered as real symbols at
2346 if (VAR_P (decl
) && DECL_HARD_REGISTER (decl
))
2348 /* FIXME: Builtins corresponding to real functions probably should have
2349 symbol table entries. */
2350 if (TREE_CODE (decl
) == FUNCTION_DECL
&& fndecl_built_in_p (decl
))
2353 /* We have real symbol that should be in symbol table. However try to trim
2354 down the refernces to libraries bit more because linker will otherwise
2355 bring unnecesary object files into the final link.
2356 FIXME: The following checks can easily be confused i.e. by self recursive
2357 function or self-referring variable. */
2359 /* We keep external functions in symtab for sake of inlining
2360 and devirtualization. We do not want to see them in symbol table as
2361 references unless they are really used. */
2362 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (this);
2363 if (cnode
&& (!definition
|| DECL_EXTERNAL (decl
))
2367 /* Ignore all references from external vars initializers - they are not really
2368 part of the compilation unit until they are used by folding. Some symbols,
2369 like references to external construction vtables cannot be referred to at
2370 all. We decide this at can_refer_decl_in_current_unit_p. */
2371 if (!definition
|| DECL_EXTERNAL (decl
))
2374 struct ipa_ref
*ref
;
2375 for (i
= 0; iterate_referring (i
, ref
); i
++)
2377 if (ref
->use
== IPA_REF_ALIAS
)
2379 if (is_a
<cgraph_node
*> (ref
->referring
))
2381 if (!DECL_EXTERNAL (ref
->referring
->decl
))