2 Copyright (C) 2012-2014 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"
27 #include "print-tree.h"
31 #include "basic-block.h"
32 #include "tree-ssa-alias.h"
33 #include "internal-fn.h"
34 #include "gimple-expr.h"
37 #include "tree-inline.h"
38 #include "langhooks.h"
41 #include "diagnostic.h"
43 #include "lto-streamer.h"
46 const char * const ld_plugin_symbol_resolution_names
[]=
51 "prevailing_def_ironly",
57 "prevailing_def_ironly_exp"
60 /* Hash table used to convert assembler names into nodes. */
61 static GTY((param_is (symtab_node
))) htab_t assembler_name_hash
;
63 /* Linked list of symbol table nodes. */
64 symtab_node
*symtab_nodes
;
66 /* The order index of the next symtab node to be created. This is
67 used so that we can sort the cgraph nodes in order by when we saw
68 them, to support -fno-toplevel-reorder. */
71 /* Hash asmnames ignoring the user specified marks. */
74 decl_assembler_name_hash (const_tree asmname
)
76 if (IDENTIFIER_POINTER (asmname
)[0] == '*')
78 const char *decl_str
= IDENTIFIER_POINTER (asmname
) + 1;
79 size_t ulp_len
= strlen (user_label_prefix
);
83 else if (strncmp (decl_str
, user_label_prefix
, ulp_len
) == 0)
86 return htab_hash_string (decl_str
);
89 return htab_hash_string (IDENTIFIER_POINTER (asmname
));
93 /* Returns a hash code for P. */
96 hash_node_by_assembler_name (const void *p
)
98 const symtab_node
*n
= (const symtab_node
*) p
;
99 return (hashval_t
) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n
->decl
));
102 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
105 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
;
112 if (decl_asmname
== asmname
)
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
);
133 else if (strncmp (decl_str
, user_label_prefix
, ulp_len
) == 0)
134 decl_str
+= ulp_len
, test
=true;
138 if (asmname_str
[0] == '*')
140 size_t ulp_len
= strlen (user_label_prefix
);
146 else if (strncmp (asmname_str
, user_label_prefix
, ulp_len
) == 0)
147 asmname_str
+= ulp_len
, test
=true;
154 return strcmp (decl_str
, asmname_str
) == 0;
158 /* Returns nonzero if P1 and P2 are equal. */
161 eq_assembler_name (const void *p1
, const void *p2
)
163 const symtab_node
*n1
= (const symtab_node
*) p1
;
164 const_tree name
= (const_tree
)p2
;
165 return (decl_assembler_name_equal (n1
->decl
, name
));
168 /* Insert NODE to assembler name hash. */
171 insert_to_assembler_name_hash (symtab_node
*node
, bool with_clones
)
173 if (is_a
<varpool_node
*> (node
) && DECL_HARD_REGISTER (node
->decl
))
175 gcc_checking_assert (!node
->previous_sharing_asm_name
176 && !node
->next_sharing_asm_name
);
177 if (assembler_name_hash
)
180 struct cgraph_node
*cnode
;
181 tree decl
= node
->decl
;
183 tree name
= DECL_ASSEMBLER_NAME (node
->decl
);
185 aslot
= htab_find_slot_with_hash (assembler_name_hash
, name
,
186 decl_assembler_name_hash (name
),
188 gcc_assert (*aslot
!= node
);
189 node
->next_sharing_asm_name
= (symtab_node
*)*aslot
;
191 ((symtab_node
*)*aslot
)->previous_sharing_asm_name
= node
;
194 /* Update also possible inline clones sharing a decl. */
195 cnode
= dyn_cast
<cgraph_node
*> (node
);
196 if (cnode
&& cnode
->clones
&& with_clones
)
197 for (cnode
= cnode
->clones
; cnode
; cnode
= cnode
->next_sibling_clone
)
198 if (cnode
->decl
== decl
)
199 insert_to_assembler_name_hash (cnode
, true);
204 /* Remove NODE from assembler name hash. */
207 unlink_from_assembler_name_hash (symtab_node
*node
, bool with_clones
)
209 if (assembler_name_hash
)
211 struct cgraph_node
*cnode
;
212 tree decl
= node
->decl
;
214 if (node
->next_sharing_asm_name
)
215 node
->next_sharing_asm_name
->previous_sharing_asm_name
216 = node
->previous_sharing_asm_name
;
217 if (node
->previous_sharing_asm_name
)
219 node
->previous_sharing_asm_name
->next_sharing_asm_name
220 = node
->next_sharing_asm_name
;
224 tree name
= DECL_ASSEMBLER_NAME (node
->decl
);
226 slot
= htab_find_slot_with_hash (assembler_name_hash
, name
,
227 decl_assembler_name_hash (name
),
229 gcc_assert (*slot
== node
);
230 if (!node
->next_sharing_asm_name
)
231 htab_clear_slot (assembler_name_hash
, 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 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);
257 /* Add node into symbol table. This function is not used directly, but via
258 cgraph/varpool node creation routines. */
261 symtab_register_node (symtab_node
*node
)
263 node
->next
= symtab_nodes
;
264 node
->previous
= NULL
;
266 symtab_nodes
->previous
= node
;
269 if (!node
->decl
->decl_with_vis
.symtab_node
)
270 node
->decl
->decl_with_vis
.symtab_node
= node
;
272 ipa_empty_ref_list (&node
->ref_list
);
274 node
->order
= symtab_order
++;
276 /* Be sure to do this last; C++ FE might create new nodes via
277 DECL_ASSEMBLER_NAME langhook! */
278 insert_to_assembler_name_hash (node
, false);
281 /* Remove NODE from same comdat group. */
284 symtab_remove_from_same_comdat_group (symtab_node
*node
)
286 if (node
->same_comdat_group
)
289 for (prev
= node
->same_comdat_group
;
290 prev
->same_comdat_group
!= node
;
291 prev
= prev
->same_comdat_group
)
293 if (node
->same_comdat_group
== prev
)
294 prev
->same_comdat_group
= NULL
;
296 prev
->same_comdat_group
= node
->same_comdat_group
;
297 node
->same_comdat_group
= NULL
;
301 /* Remove node from symbol table. This function is not used directly, but via
302 cgraph/varpool node removal routines. */
305 symtab_unregister_node (symtab_node
*node
)
307 ipa_remove_all_references (&node
->ref_list
);
308 ipa_remove_all_referring (&node
->ref_list
);
310 symtab_remove_from_same_comdat_group (node
);
313 node
->previous
->next
= node
->next
;
315 symtab_nodes
= node
->next
;
317 node
->next
->previous
= node
->previous
;
319 node
->previous
= NULL
;
321 /* During LTO symtab merging we temporarily corrupt decl to symtab node
323 gcc_assert (node
->decl
->decl_with_vis
.symtab_node
|| in_lto_p
);
324 if (node
->decl
->decl_with_vis
.symtab_node
== node
)
326 symtab_node
*replacement_node
= NULL
;
327 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
328 replacement_node
= cgraph_find_replacement_node (cnode
);
329 node
->decl
->decl_with_vis
.symtab_node
= replacement_node
;
331 if (!is_a
<varpool_node
*> (node
) || !DECL_HARD_REGISTER (node
->decl
))
332 unlink_from_assembler_name_hash (node
, false);
336 /* Remove symtab NODE from the symbol table. */
339 symtab_remove_node (symtab_node
*node
)
341 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
342 cgraph_remove_node (cnode
);
343 else if (varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
))
344 varpool_remove_node (vnode
);
347 /* Initalize asm name hash unless. */
350 symtab_initialize_asm_name_hash (void)
353 if (!assembler_name_hash
)
355 assembler_name_hash
=
356 htab_create_ggc (10, hash_node_by_assembler_name
, eq_assembler_name
,
358 FOR_EACH_SYMBOL (node
)
359 insert_to_assembler_name_hash (node
, false);
363 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
364 Return NULL if there's no such node. */
367 symtab_node_for_asm (const_tree asmname
)
372 symtab_initialize_asm_name_hash ();
373 slot
= htab_find_slot_with_hash (assembler_name_hash
, asmname
,
374 decl_assembler_name_hash (asmname
),
379 node
= (symtab_node
*) *slot
;
385 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
388 change_decl_assembler_name (tree decl
, tree name
)
390 symtab_node
*node
= NULL
;
392 /* We can have user ASM names on things, like global register variables, that
393 are not in the symbol table. */
394 if ((TREE_CODE (decl
) == VAR_DECL
395 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
396 || TREE_CODE (decl
) == FUNCTION_DECL
)
397 node
= symtab_get_node (decl
);
398 if (!DECL_ASSEMBLER_NAME_SET_P (decl
))
400 SET_DECL_ASSEMBLER_NAME (decl
, name
);
402 insert_to_assembler_name_hash (node
, true);
406 if (name
== DECL_ASSEMBLER_NAME (decl
))
409 tree alias
= (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl
))
410 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl
))
413 unlink_from_assembler_name_hash (node
, true);
414 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
))
415 && DECL_RTL_SET_P (decl
))
416 warning (0, "%D renamed after being referenced in assembly", decl
);
418 SET_DECL_ASSEMBLER_NAME (decl
, name
);
421 IDENTIFIER_TRANSPARENT_ALIAS (name
) = 1;
422 TREE_CHAIN (name
) = alias
;
425 insert_to_assembler_name_hash (node
, true);
429 /* Add NEW_ to the same comdat group that OLD is in. */
432 symtab_add_to_same_comdat_group (symtab_node
*new_node
,
433 symtab_node
*old_node
)
435 gcc_assert (old_node
->get_comdat_group ());
436 gcc_assert (!new_node
->same_comdat_group
);
437 gcc_assert (new_node
!= old_node
);
439 new_node
->set_comdat_group (old_node
->get_comdat_group ());
440 new_node
->same_comdat_group
= old_node
;
441 if (!old_node
->same_comdat_group
)
442 old_node
->same_comdat_group
= new_node
;
446 for (n
= old_node
->same_comdat_group
;
447 n
->same_comdat_group
!= old_node
;
448 n
= n
->same_comdat_group
)
450 n
->same_comdat_group
= new_node
;
454 /* Dissolve the same_comdat_group list in which NODE resides. */
457 symtab_dissolve_same_comdat_group_list (symtab_node
*node
)
459 symtab_node
*n
= node
;
462 if (!node
->same_comdat_group
)
466 next
= n
->same_comdat_group
;
467 n
->same_comdat_group
= NULL
;
468 /* Clear comdat_group for comdat locals, since
469 make_decl_local doesn't. */
470 if (!TREE_PUBLIC (n
->decl
))
471 n
->set_comdat_group (NULL
);
477 /* Return printable assembler name of NODE.
478 This function is used only for debugging. When assembler name
479 is unknown go with identifier name. */
482 symtab_node::asm_name () const
484 if (!DECL_ASSEMBLER_NAME_SET_P (decl
))
485 return lang_hooks
.decl_printable_name (decl
, 2);
486 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
489 /* Return printable identifier name. */
492 symtab_node::name () const
494 return lang_hooks
.decl_printable_name (decl
, 2);
497 static const char * const symtab_type_names
[] = {"symbol", "function", "variable"};
499 /* Dump base fields of symtab nodes. Not to be used directly. */
502 dump_symtab_base (FILE *f
, symtab_node
*node
)
504 static const char * const visibility_types
[] = {
505 "default", "protected", "hidden", "internal"
508 fprintf (f
, "%s/%i (%s)",
512 dump_addr (f
, " @", (void *)node
);
513 fprintf (f
, "\n Type: %s", symtab_type_names
[node
->type
]);
515 if (node
->definition
)
516 fprintf (f
, " definition");
518 fprintf (f
, " analyzed");
520 fprintf (f
, " alias");
522 fprintf (f
, " weakref");
523 if (node
->cpp_implicit_alias
)
524 fprintf (f
, " cpp_implicit_alias");
525 if (node
->alias_target
)
526 fprintf (f
, " target:%s",
527 DECL_P (node
->alias_target
)
528 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
529 (node
->alias_target
))
530 : IDENTIFIER_POINTER (node
->alias_target
));
531 if (node
->body_removed
)
532 fprintf (f
, "\n Body removed by symtab_remove_unreachable_nodes");
533 fprintf (f
, "\n Visibility:");
534 if (node
->in_other_partition
)
535 fprintf (f
, " in_other_partition");
536 if (node
->used_from_other_partition
)
537 fprintf (f
, " used_from_other_partition");
538 if (node
->force_output
)
539 fprintf (f
, " force_output");
540 if (node
->forced_by_abi
)
541 fprintf (f
, " forced_by_abi");
542 if (node
->externally_visible
)
543 fprintf (f
, " externally_visible");
544 if (node
->resolution
!= LDPR_UNKNOWN
)
546 ld_plugin_symbol_resolution_names
[(int)node
->resolution
]);
547 if (TREE_ASM_WRITTEN (node
->decl
))
548 fprintf (f
, " asm_written");
549 if (DECL_EXTERNAL (node
->decl
))
550 fprintf (f
, " external");
551 if (TREE_PUBLIC (node
->decl
))
552 fprintf (f
, " public");
553 if (DECL_COMMON (node
->decl
))
554 fprintf (f
, " common");
555 if (DECL_WEAK (node
->decl
))
556 fprintf (f
, " weak");
557 if (DECL_DLLIMPORT_P (node
->decl
))
558 fprintf (f
, " dll_import");
559 if (DECL_COMDAT (node
->decl
))
560 fprintf (f
, " comdat");
561 if (node
->get_comdat_group ())
562 fprintf (f
, " comdat_group:%s",
563 IDENTIFIER_POINTER (node
->get_comdat_group ()));
564 if (DECL_ONE_ONLY (node
->decl
))
565 fprintf (f
, " one_only");
566 if (DECL_SECTION_NAME (node
->decl
))
567 fprintf (f
, " section_name:%s",
568 TREE_STRING_POINTER (DECL_SECTION_NAME (node
->decl
)));
569 if (DECL_VISIBILITY_SPECIFIED (node
->decl
))
570 fprintf (f
, " visibility_specified");
571 if (DECL_VISIBILITY (node
->decl
))
572 fprintf (f
, " visibility:%s",
573 visibility_types
[DECL_VISIBILITY (node
->decl
)]);
574 if (DECL_VIRTUAL_P (node
->decl
))
575 fprintf (f
, " virtual");
576 if (DECL_ARTIFICIAL (node
->decl
))
577 fprintf (f
, " artificial");
578 if (TREE_CODE (node
->decl
) == FUNCTION_DECL
)
580 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
581 fprintf (f
, " constructor");
582 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
583 fprintf (f
, " destructor");
587 if (node
->same_comdat_group
)
588 fprintf (f
, " Same comdat group as: %s/%i\n",
589 node
->same_comdat_group
->asm_name (),
590 node
->same_comdat_group
->order
);
591 if (node
->next_sharing_asm_name
)
592 fprintf (f
, " next sharing asm name: %i\n",
593 node
->next_sharing_asm_name
->order
);
594 if (node
->previous_sharing_asm_name
)
595 fprintf (f
, " previous sharing asm name: %i\n",
596 node
->previous_sharing_asm_name
->order
);
598 if (node
->address_taken
)
599 fprintf (f
, " Address is taken.\n");
602 fprintf (f
, " Aux:");
603 dump_addr (f
, " @", (void *)node
->aux
);
606 fprintf (f
, " References: ");
607 ipa_dump_references (f
, &node
->ref_list
);
608 fprintf (f
, " Referring: ");
609 ipa_dump_referring (f
, &node
->ref_list
);
610 if (node
->lto_file_data
)
611 fprintf (f
, " Read from file: %s\n",
612 node
->lto_file_data
->file_name
);
615 /* Dump symtab node. */
618 dump_symtab_node (FILE *f
, symtab_node
*node
)
620 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
621 dump_cgraph_node (f
, cnode
);
622 else if (varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
))
623 dump_varpool_node (f
, vnode
);
626 /* Dump symbol table. */
629 dump_symtab (FILE *f
)
632 fprintf (f
, "Symbol table:\n\n");
633 FOR_EACH_SYMBOL (node
)
634 dump_symtab_node (f
, node
);
637 /* Dump symtab node NODE to stderr. */
640 debug_symtab_node (symtab_node
*node
)
642 dump_symtab_node (stderr
, node
);
645 /* Dump symbol table to stderr. */
650 dump_symtab (stderr
);
653 /* Verify common part of symtab nodes. */
656 verify_symtab_base (symtab_node
*node
)
658 bool error_found
= false;
659 symtab_node
*hashed_node
;
661 if (is_a
<cgraph_node
*> (node
))
663 if (TREE_CODE (node
->decl
) != FUNCTION_DECL
)
665 error ("function symbol is not function");
669 else if (is_a
<varpool_node
*> (node
))
671 if (TREE_CODE (node
->decl
) != VAR_DECL
)
673 error ("variable symbol is not variable");
679 error ("node has unknown type");
683 if (cgraph_state
!= CGRAPH_LTO_STREAMING
)
685 hashed_node
= symtab_get_node (node
->decl
);
688 error ("node not found node->decl->decl_with_vis.symtab_node");
691 if (hashed_node
!= node
692 && (!is_a
<cgraph_node
*> (node
)
693 || !dyn_cast
<cgraph_node
*> (node
)->clone_of
694 || dyn_cast
<cgraph_node
*> (node
)->clone_of
->decl
697 error ("node differs from node->decl->decl_with_vis.symtab_node");
701 if (assembler_name_hash
)
703 hashed_node
= symtab_node_for_asm (DECL_ASSEMBLER_NAME (node
->decl
));
704 if (hashed_node
&& hashed_node
->previous_sharing_asm_name
)
706 error ("assembler name hash list corrupted");
711 if (hashed_node
== node
)
713 hashed_node
= hashed_node
->next_sharing_asm_name
;
716 && !(is_a
<varpool_node
*> (node
)
717 || DECL_HARD_REGISTER (node
->decl
)))
719 error ("node not found in symtab assembler name hash");
723 if (node
->previous_sharing_asm_name
724 && node
->previous_sharing_asm_name
->next_sharing_asm_name
!= node
)
726 error ("double linked list of assembler names corrupted");
729 if (node
->analyzed
&& !node
->definition
)
731 error ("node is analyzed byt it is not a definition");
734 if (node
->cpp_implicit_alias
&& !node
->alias
)
736 error ("node is alias but not implicit alias");
739 if (node
->alias
&& !node
->definition
742 error ("node is alias but not definition");
745 if (node
->weakref
&& !node
->alias
)
747 error ("node is weakref but not an alias");
750 if (node
->same_comdat_group
)
752 symtab_node
*n
= node
->same_comdat_group
;
754 if (!n
->get_comdat_group ())
756 error ("node is in same_comdat_group list but has no comdat_group");
759 if (n
->get_comdat_group () != node
->get_comdat_group ())
761 error ("same_comdat_group list across different groups");
766 error ("Node has same_comdat_group but it is not a definition");
769 if (n
->type
!= node
->type
)
771 error ("mixing different types of symbol in same comdat groups is not supported");
776 error ("node is alone in a comdat group");
781 if (!n
->same_comdat_group
)
783 error ("same_comdat_group is not a circular list");
787 n
= n
->same_comdat_group
;
790 if (symtab_comdat_local_p (node
))
792 struct ipa_ref_list
*refs
= &node
->ref_list
;
794 for (int i
= 0; ipa_ref_list_referring_iterate (refs
, i
, ref
); ++i
)
796 if (!symtab_in_same_comdat_p (ref
->referring
, node
))
798 error ("comdat-local symbol referred to by %s outside its "
800 identifier_to_locale (ref
->referring
->name()));
809 /* Verify consistency of NODE. */
812 verify_symtab_node (symtab_node
*node
)
817 timevar_push (TV_CGRAPH_VERIFY
);
818 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
819 verify_cgraph_node (cnode
);
821 if (verify_symtab_base (node
))
823 dump_symtab_node (stderr
, node
);
824 internal_error ("verify_symtab_node failed");
826 timevar_pop (TV_CGRAPH_VERIFY
);
829 /* Verify symbol table for internal consistency. */
835 FOR_EACH_SYMBOL (node
)
836 verify_symtab_node (node
);
839 /* Return true when RESOLUTION indicate that linker will use
840 the symbol from non-LTO object files. */
843 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution
)
845 return (resolution
== LDPR_PREVAILING_DEF
846 || resolution
== LDPR_PREEMPTED_REG
847 || resolution
== LDPR_RESOLVED_EXEC
848 || resolution
== LDPR_RESOLVED_DYN
);
851 /* Return true when NODE is known to be used from other (non-LTO) object file.
852 Known only when doing LTO via linker plugin. */
855 symtab_used_from_object_file_p (symtab_node
*node
)
857 if (!TREE_PUBLIC (node
->decl
) || DECL_EXTERNAL (node
->decl
))
859 if (resolution_used_from_other_file_p (node
->resolution
))
864 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
865 but other code such as notice_global_symbol generates rtl. */
868 symtab_make_decl_local (tree decl
)
872 /* Avoid clearing comdat_groups on comdat-local decls. */
873 if (TREE_PUBLIC (decl
) == 0)
876 if (TREE_CODE (decl
) == VAR_DECL
)
877 DECL_COMMON (decl
) = 0;
878 else gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
880 if (DECL_COMDAT (decl
))
882 DECL_SECTION_NAME (decl
) = 0;
883 DECL_COMDAT (decl
) = 0;
885 DECL_WEAK (decl
) = 0;
886 DECL_EXTERNAL (decl
) = 0;
887 DECL_VISIBILITY_SPECIFIED (decl
) = 0;
888 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
889 TREE_PUBLIC (decl
) = 0;
890 if (!DECL_RTL_SET_P (decl
))
893 /* Update rtl flags. */
894 make_decl_rtl (decl
);
896 rtl
= DECL_RTL (decl
);
900 symbol
= XEXP (rtl
, 0);
901 if (GET_CODE (symbol
) != SYMBOL_REF
)
904 SYMBOL_REF_WEAK (symbol
) = DECL_WEAK (decl
);
907 /* Return availability of NODE. */
910 symtab_node_availability (symtab_node
*node
)
912 if (is_a
<cgraph_node
*> (node
))
913 return cgraph_function_body_availability (cgraph (node
));
915 return cgraph_variable_initializer_availability (varpool (node
));
918 /* Given NODE, walk the alias chain to return the symbol NODE is alias of.
919 If NODE is not an alias, return NODE.
920 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
923 symtab_alias_ultimate_target (symtab_node
*node
, enum availability
*availability
)
925 bool weakref_p
= false;
930 *availability
= symtab_node_availability (node
);
934 /* To determine visibility of the target, we follow ELF semantic of aliases.
935 Here alias is an alternative assembler name of a given definition. Its
936 availability prevails the availability of its target (i.e. static alias of
937 weak definition is available.
939 Weakref is a different animal (and not part of ELF per se). It is just
940 alternative name of a given symbol used within one complation unit
941 and is translated prior hitting the object file. It inherits the
942 visibility of its target (i.e. weakref of non-overwritable definition
943 is non-overwritable, while weakref of weak definition is weak).
945 If we ever get into supporting targets with different semantics, a target
946 hook will be needed here. */
950 weakref_p
= node
->weakref
;
952 *availability
= symtab_node_availability (node
);
954 *availability
= AVAIL_LOCAL
;
958 if (node
->alias
&& node
->analyzed
)
959 node
= symtab_alias_target (node
);
964 else if (node
->analyzed
)
968 enum availability a
= symtab_node_availability (node
);
969 if (a
< *availability
)
974 *availability
= AVAIL_NOT_AVAILABLE
;
977 if (node
&& availability
&& weakref_p
)
979 enum availability a
= symtab_node_availability (node
);
980 if (a
< *availability
)
982 weakref_p
= node
->weakref
;
986 *availability
= AVAIL_NOT_AVAILABLE
;
990 /* C++ FE sometimes change linkage flags after producing same body aliases.
992 FIXME: C++ produce implicit aliases for virtual functions and vtables that
993 are obviously equivalent. The way it is doing so is however somewhat
994 kludgy and interferes with the visibility code. As a result we need to
995 copy the visibility from the target to get things right. */
998 fixup_same_cpp_alias_visibility (symtab_node
*node
, symtab_node
*target
)
1000 if (is_a
<cgraph_node
*> (node
))
1002 DECL_DECLARED_INLINE_P (node
->decl
)
1003 = DECL_DECLARED_INLINE_P (target
->decl
);
1004 DECL_DISREGARD_INLINE_LIMITS (node
->decl
)
1005 = DECL_DISREGARD_INLINE_LIMITS (target
->decl
);
1007 /* FIXME: It is not really clear why those flags should not be copied for
1011 DECL_WEAK (node
->decl
) = DECL_WEAK (target
->decl
);
1012 DECL_EXTERNAL (node
->decl
) = DECL_EXTERNAL (target
->decl
);
1013 DECL_VISIBILITY (node
->decl
) = DECL_VISIBILITY (target
->decl
);
1015 DECL_VIRTUAL_P (node
->decl
) = DECL_VIRTUAL_P (target
->decl
);
1016 if (TREE_PUBLIC (node
->decl
))
1020 DECL_EXTERNAL (node
->decl
) = DECL_EXTERNAL (target
->decl
);
1021 DECL_COMDAT (node
->decl
) = DECL_COMDAT (target
->decl
);
1022 group
= target
->get_comdat_group ();
1023 node
->set_comdat_group (group
);
1025 && !node
->same_comdat_group
)
1026 symtab_add_to_same_comdat_group (node
, target
);
1028 node
->externally_visible
= target
->externally_visible
;
1031 /* Add reference recording that NODE is alias of TARGET.
1032 The function can fail in the case of aliasing cycles; in this case
1033 it returns false. */
1036 symtab_resolve_alias (symtab_node
*node
, symtab_node
*target
)
1040 gcc_assert (!node
->analyzed
1041 && !vec_safe_length (node
->ref_list
.references
));
1043 /* Never let cycles to creep into the symbol table alias references;
1044 those will make alias walkers to be infinite. */
1045 for (n
= target
; n
&& n
->alias
;
1046 n
= n
->analyzed
? symtab_alias_target (n
) : NULL
)
1049 if (is_a
<cgraph_node
*> (node
))
1050 error ("function %q+D part of alias cycle", node
->decl
);
1051 else if (is_a
<varpool_node
*> (node
))
1052 error ("variable %q+D part of alias cycle", node
->decl
);
1055 node
->alias
= false;
1059 /* "analyze" the node - i.e. mark the reference. */
1060 node
->definition
= true;
1062 node
->analyzed
= true;
1063 ipa_record_reference (node
, target
, IPA_REF_ALIAS
, NULL
);
1065 /* Alias targets become reudndant after alias is resolved into an reference.
1066 We do not want to keep it around or we would have to mind updating them
1067 when renaming symbols. */
1068 node
->alias_target
= NULL
;
1070 if (node
->cpp_implicit_alias
&& cgraph_state
>= CGRAPH_STATE_CONSTRUCTION
)
1071 fixup_same_cpp_alias_visibility (node
, target
);
1073 /* If alias has address taken, so does the target. */
1074 if (node
->address_taken
)
1075 symtab_alias_ultimate_target (target
, NULL
)->address_taken
= true;
1079 /* Call calback on NODE and aliases associated to NODE.
1080 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1084 symtab_for_node_and_aliases (symtab_node
*node
,
1085 bool (*callback
) (symtab_node
*, void *),
1087 bool include_overwritable
)
1090 struct ipa_ref
*ref
;
1092 if (callback (node
, data
))
1094 for (i
= 0; ipa_ref_list_referring_iterate (&node
->ref_list
, i
, ref
); i
++)
1095 if (ref
->use
== IPA_REF_ALIAS
)
1097 symtab_node
*alias
= ref
->referring
;
1098 if (include_overwritable
1099 || symtab_node_availability (alias
) > AVAIL_OVERWRITABLE
)
1100 if (symtab_for_node_and_aliases (alias
, callback
, data
,
1101 include_overwritable
))
1107 /* Worker searching nonoverwritable alias. */
1110 symtab_nonoverwritable_alias_1 (symtab_node
*node
, void *data
)
1112 if (decl_binds_to_current_def_p (node
->decl
))
1114 *(symtab_node
**)data
= node
;
1120 /* If NODE can not be overwriten by static or dynamic linker to point to different
1121 definition, return NODE. Otherwise look for alias with such property and if
1122 none exists, introduce new one. */
1125 symtab_nonoverwritable_alias (symtab_node
*node
)
1128 symtab_node
*new_node
= NULL
;
1130 /* First try to look up existing alias or base object
1131 (if that is already non-overwritable). */
1132 node
= symtab_alias_ultimate_target (node
, NULL
);
1133 gcc_assert (!node
->alias
&& !node
->weakref
);
1134 symtab_for_node_and_aliases (node
, symtab_nonoverwritable_alias_1
,
1135 (void *)&new_node
, true);
1138 #ifndef ASM_OUTPUT_DEF
1139 /* If aliases aren't supported by the assembler, fail. */
1143 /* Otherwise create a new one. */
1144 new_decl
= copy_node (node
->decl
);
1145 DECL_NAME (new_decl
) = clone_function_name (node
->decl
, "localalias");
1146 if (TREE_CODE (new_decl
) == FUNCTION_DECL
)
1147 DECL_STRUCT_FUNCTION (new_decl
) = NULL
;
1148 DECL_INITIAL (new_decl
) = NULL
;
1149 SET_DECL_ASSEMBLER_NAME (new_decl
, DECL_NAME (new_decl
));
1150 SET_DECL_RTL (new_decl
, NULL
);
1152 /* Update the properties. */
1153 DECL_EXTERNAL (new_decl
) = 0;
1154 TREE_PUBLIC (new_decl
) = 0;
1155 DECL_COMDAT (new_decl
) = 0;
1156 DECL_WEAK (new_decl
) = 0;
1157 DECL_VIRTUAL_P (new_decl
) = 0;
1158 if (TREE_CODE (new_decl
) == FUNCTION_DECL
)
1160 DECL_STATIC_CONSTRUCTOR (new_decl
) = 0;
1161 DECL_STATIC_DESTRUCTOR (new_decl
) = 0;
1162 new_node
= cgraph_create_function_alias
1163 (new_decl
, node
->decl
);
1167 TREE_READONLY (new_decl
) = TREE_READONLY (node
->decl
);
1168 new_node
= varpool_create_variable_alias (new_decl
, node
->decl
);
1170 symtab_resolve_alias (new_node
, node
);
1171 gcc_assert (decl_binds_to_current_def_p (new_decl
)
1172 && targetm
.binds_local_p (new_decl
));
1176 /* Return true if A and B represents semantically equivalent symbols. */
1179 symtab_semantically_equivalent_p (symtab_node
*a
,
1182 enum availability avail
;
1186 /* Equivalent functions are equivalent. */
1187 if (a
->decl
== b
->decl
)
1190 /* If symbol is not overwritable by different implementation,
1191 walk to the base object it defines. */
1192 ba
= symtab_alias_ultimate_target (a
, &avail
);
1193 if (avail
>= AVAIL_AVAILABLE
)
1200 bb
= symtab_alias_ultimate_target (b
, &avail
);
1201 if (avail
>= AVAIL_AVAILABLE
)
1211 /* Classify symbol NODE for partitioning. */
1213 enum symbol_partitioning_class
1214 symtab_get_symbol_partitioning_class (symtab_node
*node
)
1216 /* Inline clones are always duplicated.
1217 This include external delcarations. */
1218 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
1220 if (DECL_ABSTRACT (node
->decl
))
1221 return SYMBOL_EXTERNAL
;
1223 if (cnode
&& cnode
->global
.inlined_to
)
1224 return SYMBOL_DUPLICATE
;
1226 /* Weakref aliases are always duplicated. */
1228 return SYMBOL_DUPLICATE
;
1230 /* External declarations are external. */
1231 if (DECL_EXTERNAL (node
->decl
))
1232 return SYMBOL_EXTERNAL
;
1234 if (varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
))
1236 /* Constant pool references use local symbol names that can not
1237 be promoted global. We should never put into a constant pool
1238 objects that can not be duplicated across partitions. */
1239 if (DECL_IN_CONSTANT_POOL (node
->decl
))
1240 return SYMBOL_DUPLICATE
;
1241 gcc_checking_assert (vnode
->definition
);
1243 /* Functions that are cloned may stay in callgraph even if they are unused.
1244 Handle them as external; compute_ltrans_boundary take care to make
1245 proper things to happen (i.e. to make them appear in the boundary but
1246 with body streamed, so clone can me materialized). */
1247 else if (!cgraph (node
)->definition
)
1248 return SYMBOL_EXTERNAL
;
1250 /* Linker discardable symbols are duplicated to every use unless they are
1252 if (DECL_ONE_ONLY (node
->decl
)
1253 && !node
->force_output
1254 && !node
->forced_by_abi
1255 && !symtab_used_from_object_file_p (node
))
1256 return SYMBOL_DUPLICATE
;
1258 return SYMBOL_PARTITION
;
1260 #include "gt-symtab.h"