2 Copyright (C) 2012-2013 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 "tree-inline.h"
28 #include "langhooks.h"
32 #include "diagnostic.h"
34 #include "lto-streamer.h"
37 const char * const ld_plugin_symbol_resolution_names
[]=
42 "prevailing_def_ironly",
48 "prevailing_def_ironly_exp"
51 /* Hash table used to convert declarations into nodes. */
52 static GTY((param_is (symtab_node_base
))) htab_t symtab_hash
;
53 /* Hash table used to convert assembler names into nodes. */
54 static GTY((param_is (symtab_node_base
))) htab_t assembler_name_hash
;
56 /* Linked list of symbol table nodes. */
57 symtab_node symtab_nodes
;
59 /* The order index of the next symtab node to be created. This is
60 used so that we can sort the cgraph nodes in order by when we saw
61 them, to support -fno-toplevel-reorder. */
64 /* Returns a hash code for P. */
67 hash_node (const void *p
)
69 const_symtab_node n
= (const_symtab_node
) p
;
70 return (hashval_t
) DECL_UID (n
->decl
);
74 /* Returns nonzero if P1 and P2 are equal. */
77 eq_node (const void *p1
, const void *p2
)
79 const_symtab_node n1
= (const_symtab_node
) p1
;
80 const_symtab_node n2
= (const_symtab_node
) p2
;
81 return DECL_UID (n1
->decl
) == DECL_UID (n2
->decl
);
84 /* Returns a hash code for P. */
87 hash_node_by_assembler_name (const void *p
)
89 const_symtab_node n
= (const_symtab_node
) p
;
90 return (hashval_t
) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n
->decl
));
93 /* Returns nonzero if P1 and P2 are equal. */
96 eq_assembler_name (const void *p1
, const void *p2
)
98 const_symtab_node n1
= (const_symtab_node
) p1
;
99 const_tree name
= (const_tree
)p2
;
100 return (decl_assembler_name_equal (n1
->decl
, name
));
103 /* Insert NODE to assembler name hash. */
106 insert_to_assembler_name_hash (symtab_node node
, bool with_clones
)
108 if (is_a
<varpool_node
> (node
) && DECL_HARD_REGISTER (node
->decl
))
110 gcc_checking_assert (!node
->previous_sharing_asm_name
111 && !node
->next_sharing_asm_name
);
112 if (assembler_name_hash
)
115 struct cgraph_node
*cnode
;
116 tree decl
= node
->decl
;
118 tree name
= DECL_ASSEMBLER_NAME (node
->decl
);
120 aslot
= htab_find_slot_with_hash (assembler_name_hash
, name
,
121 decl_assembler_name_hash (name
),
123 gcc_assert (*aslot
!= node
);
124 node
->next_sharing_asm_name
= (symtab_node
)*aslot
;
126 ((symtab_node
)*aslot
)->previous_sharing_asm_name
= node
;
129 /* Update also possible inline clones sharing a decl. */
130 cnode
= dyn_cast
<cgraph_node
> (node
);
131 if (cnode
&& cnode
->clones
&& with_clones
)
132 for (cnode
= cnode
->clones
; cnode
; cnode
= cnode
->next_sibling_clone
)
133 if (cnode
->decl
== decl
)
134 insert_to_assembler_name_hash (cnode
, true);
139 /* Remove NODE from assembler name hash. */
142 unlink_from_assembler_name_hash (symtab_node node
, bool with_clones
)
144 if (assembler_name_hash
)
146 struct cgraph_node
*cnode
;
147 tree decl
= node
->decl
;
149 if (node
->next_sharing_asm_name
)
150 node
->next_sharing_asm_name
->previous_sharing_asm_name
151 = node
->previous_sharing_asm_name
;
152 if (node
->previous_sharing_asm_name
)
154 node
->previous_sharing_asm_name
->next_sharing_asm_name
155 = node
->next_sharing_asm_name
;
159 tree name
= DECL_ASSEMBLER_NAME (node
->decl
);
161 slot
= htab_find_slot_with_hash (assembler_name_hash
, name
,
162 decl_assembler_name_hash (name
),
164 gcc_assert (*slot
== node
);
165 if (!node
->next_sharing_asm_name
)
166 htab_clear_slot (assembler_name_hash
, slot
);
168 *slot
= node
->next_sharing_asm_name
;
170 node
->next_sharing_asm_name
= NULL
;
171 node
->previous_sharing_asm_name
= NULL
;
173 /* Update also possible inline clones sharing a decl. */
174 cnode
= dyn_cast
<cgraph_node
> (node
);
175 if (cnode
&& cnode
->clones
&& with_clones
)
176 for (cnode
= cnode
->clones
; cnode
; cnode
= cnode
->next_sibling_clone
)
177 if (cnode
->decl
== decl
)
178 unlink_from_assembler_name_hash (cnode
, true);
182 /* Arrange node to be first in its entry of assembler_name_hash. */
185 symtab_prevail_in_asm_name_hash (symtab_node node
)
187 unlink_from_assembler_name_hash (node
, false);
188 insert_to_assembler_name_hash (node
, false);
192 /* Add node into symbol table. This function is not used directly, but via
193 cgraph/varpool node creation routines. */
196 symtab_register_node (symtab_node node
)
198 struct symtab_node_base key
;
201 node
->next
= symtab_nodes
;
202 node
->previous
= NULL
;
204 symtab_nodes
->previous
= node
;
208 symtab_hash
= htab_create_ggc (10, hash_node
, eq_node
, NULL
);
209 key
.decl
= node
->decl
;
210 slot
= (symtab_node
*) htab_find_slot (symtab_hash
, &key
, INSERT
);
214 ipa_empty_ref_list (&node
->ref_list
);
216 node
->order
= symtab_order
++;
218 /* Be sure to do this last; C++ FE might create new nodes via
219 DECL_ASSEMBLER_NAME langhook! */
220 insert_to_assembler_name_hash (node
, false);
223 /* Make NODE to be the one symtab hash is pointing to. Used when reshaping tree
227 symtab_insert_node_to_hashtable (symtab_node node
)
229 struct symtab_node_base key
;
233 symtab_hash
= htab_create_ggc (10, hash_node
, eq_node
, NULL
);
234 key
.decl
= node
->decl
;
235 slot
= (symtab_node
*) htab_find_slot (symtab_hash
, &key
, INSERT
);
239 /* Remove node from symbol table. This function is not used directly, but via
240 cgraph/varpool node removal routines. */
243 symtab_unregister_node (symtab_node node
)
246 ipa_remove_all_references (&node
->ref_list
);
247 ipa_remove_all_referring (&node
->ref_list
);
249 if (node
->same_comdat_group
)
252 for (prev
= node
->same_comdat_group
;
253 prev
->same_comdat_group
!= node
;
254 prev
= prev
->same_comdat_group
)
256 if (node
->same_comdat_group
== prev
)
257 prev
->same_comdat_group
= NULL
;
259 prev
->same_comdat_group
= node
->same_comdat_group
;
260 node
->same_comdat_group
= NULL
;
264 node
->previous
->next
= node
->next
;
266 symtab_nodes
= node
->next
;
268 node
->next
->previous
= node
->previous
;
270 node
->previous
= NULL
;
272 slot
= htab_find_slot (symtab_hash
, node
, NO_INSERT
);
274 /* During LTO symtab merging we temporarily corrupt decl to symtab node
276 gcc_assert ((slot
&& *slot
) || in_lto_p
);
277 if (slot
&& *slot
&& *slot
== node
)
279 symtab_node replacement_node
= NULL
;
280 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
281 replacement_node
= cgraph_find_replacement_node (cnode
);
282 if (!replacement_node
)
283 htab_clear_slot (symtab_hash
, slot
);
285 *slot
= replacement_node
;
287 if (!is_a
<varpool_node
> (node
) || !DECL_HARD_REGISTER (node
->decl
))
288 unlink_from_assembler_name_hash (node
, false);
291 /* Return symbol table node associated with DECL, if any,
292 and NULL otherwise. */
295 symtab_get_node (const_tree decl
)
298 struct symtab_node_base key
;
300 #ifdef ENABLE_CHECKING
301 /* Check that we are called for sane type of object - functions
302 and static or external variables. */
303 gcc_checking_assert (TREE_CODE (decl
) == FUNCTION_DECL
304 || (TREE_CODE (decl
) == VAR_DECL
305 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)
312 key
.decl
= CONST_CAST2 (tree
, const_tree
, decl
);
314 slot
= (symtab_node
*) htab_find_slot (symtab_hash
, &key
,
322 /* Remove symtab NODE from the symbol table. */
325 symtab_remove_node (symtab_node node
)
327 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
328 cgraph_remove_node (cnode
);
329 else if (varpool_node
*vnode
= dyn_cast
<varpool_node
> (node
))
330 varpool_remove_node (vnode
);
333 /* Initalize asm name hash unless. */
336 symtab_initialize_asm_name_hash (void)
339 if (!assembler_name_hash
)
341 assembler_name_hash
=
342 htab_create_ggc (10, hash_node_by_assembler_name
, eq_assembler_name
,
344 FOR_EACH_SYMBOL (node
)
345 insert_to_assembler_name_hash (node
, false);
349 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
350 Return NULL if there's no such node. */
353 symtab_node_for_asm (const_tree asmname
)
358 symtab_initialize_asm_name_hash ();
359 slot
= htab_find_slot_with_hash (assembler_name_hash
, asmname
,
360 decl_assembler_name_hash (asmname
),
365 node
= (symtab_node
) *slot
;
371 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
374 change_decl_assembler_name (tree decl
, tree name
)
376 symtab_node node
= NULL
;
378 /* We can have user ASM names on things, like global register variables, that
379 are not in the symbol table. */
380 if ((TREE_CODE (decl
) == VAR_DECL
381 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
382 || TREE_CODE (decl
) == FUNCTION_DECL
)
383 node
= symtab_get_node (decl
);
384 if (!DECL_ASSEMBLER_NAME_SET_P (decl
))
386 SET_DECL_ASSEMBLER_NAME (decl
, name
);
388 insert_to_assembler_name_hash (node
, true);
392 if (name
== DECL_ASSEMBLER_NAME (decl
))
395 tree alias
= (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl
))
396 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl
))
399 unlink_from_assembler_name_hash (node
, true);
400 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
))
401 && DECL_RTL_SET_P (decl
))
402 warning (0, "%D renamed after being referenced in assembly", decl
);
404 SET_DECL_ASSEMBLER_NAME (decl
, name
);
407 IDENTIFIER_TRANSPARENT_ALIAS (name
) = 1;
408 TREE_CHAIN (DECL_ASSEMBLER_NAME (name
)) = alias
;
411 insert_to_assembler_name_hash (node
, true);
415 /* Add NEW_ to the same comdat group that OLD is in. */
418 symtab_add_to_same_comdat_group (symtab_node new_node
,
419 symtab_node old_node
)
421 gcc_assert (DECL_ONE_ONLY (old_node
->decl
));
422 gcc_assert (!new_node
->same_comdat_group
);
423 gcc_assert (new_node
!= old_node
);
425 DECL_COMDAT_GROUP (new_node
->decl
) = DECL_COMDAT_GROUP (old_node
->decl
);
426 new_node
->same_comdat_group
= old_node
;
427 if (!old_node
->same_comdat_group
)
428 old_node
->same_comdat_group
= new_node
;
432 for (n
= old_node
->same_comdat_group
;
433 n
->same_comdat_group
!= old_node
;
434 n
= n
->same_comdat_group
)
436 n
->same_comdat_group
= new_node
;
440 /* Dissolve the same_comdat_group list in which NODE resides. */
443 symtab_dissolve_same_comdat_group_list (symtab_node node
)
445 symtab_node n
= node
, next
;
447 if (!node
->same_comdat_group
)
451 next
= n
->same_comdat_group
;
452 n
->same_comdat_group
= NULL
;
458 /* Return printable assembler name of NODE.
459 This function is used only for debugging. When assembler name
460 is unknown go with identifier name. */
463 symtab_node_asm_name (symtab_node node
)
465 if (!DECL_ASSEMBLER_NAME_SET_P (node
->decl
))
466 return lang_hooks
.decl_printable_name (node
->decl
, 2);
467 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node
->decl
));
470 /* Return printable identifier name. */
473 symtab_node_name (symtab_node node
)
475 return lang_hooks
.decl_printable_name (node
->decl
, 2);
478 static const char * const symtab_type_names
[] = {"symbol", "function", "variable"};
480 /* Dump base fields of symtab nodes. Not to be used directly. */
483 dump_symtab_base (FILE *f
, symtab_node node
)
485 static const char * const visibility_types
[] = {
486 "default", "protected", "hidden", "internal"
489 fprintf (f
, "%s/%i (%s)",
490 symtab_node_asm_name (node
),
492 symtab_node_name (node
));
493 dump_addr (f
, " @", (void *)node
);
494 fprintf (f
, "\n Type: %s", symtab_type_names
[node
->type
]);
496 if (node
->definition
)
497 fprintf (f
, " definition");
499 fprintf (f
, " analyzed");
501 fprintf (f
, " alias");
503 fprintf (f
, " weakref");
504 if (node
->cpp_implicit_alias
)
505 fprintf (f
, " cpp_implicit_alias");
506 if (node
->alias_target
)
507 fprintf (f
, " target:%s",
508 DECL_P (node
->alias_target
)
509 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
510 (node
->alias_target
))
511 : IDENTIFIER_POINTER (node
->alias_target
));
512 fprintf (f
, "\n Visibility:");
513 if (node
->in_other_partition
)
514 fprintf (f
, " in_other_partition");
515 if (node
->used_from_other_partition
)
516 fprintf (f
, " used_from_other_partition");
517 if (node
->force_output
)
518 fprintf (f
, " force_output");
519 if (node
->forced_by_abi
)
520 fprintf (f
, " forced_by_abi");
521 if (node
->externally_visible
)
522 fprintf (f
, " externally_visible");
523 if (node
->resolution
!= LDPR_UNKNOWN
)
525 ld_plugin_symbol_resolution_names
[(int)node
->resolution
]);
526 if (TREE_ASM_WRITTEN (node
->decl
))
527 fprintf (f
, " asm_written");
528 if (DECL_EXTERNAL (node
->decl
))
529 fprintf (f
, " external");
530 if (TREE_PUBLIC (node
->decl
))
531 fprintf (f
, " public");
532 if (DECL_COMMON (node
->decl
))
533 fprintf (f
, " common");
534 if (DECL_WEAK (node
->decl
))
535 fprintf (f
, " weak");
536 if (DECL_DLLIMPORT_P (node
->decl
))
537 fprintf (f
, " dll_import");
538 if (DECL_COMDAT (node
->decl
))
539 fprintf (f
, " comdat");
540 if (DECL_COMDAT_GROUP (node
->decl
))
541 fprintf (f
, " comdat_group:%s",
542 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (node
->decl
)));
543 if (DECL_ONE_ONLY (node
->decl
))
544 fprintf (f
, " one_only");
545 if (DECL_SECTION_NAME (node
->decl
))
546 fprintf (f
, " section_name:%s",
547 TREE_STRING_POINTER (DECL_SECTION_NAME (node
->decl
)));
548 if (DECL_VISIBILITY_SPECIFIED (node
->decl
))
549 fprintf (f
, " visibility_specified");
550 if (DECL_VISIBILITY (node
->decl
))
551 fprintf (f
, " visibility:%s",
552 visibility_types
[DECL_VISIBILITY (node
->decl
)]);
553 if (DECL_VIRTUAL_P (node
->decl
))
554 fprintf (f
, " virtual");
555 if (DECL_ARTIFICIAL (node
->decl
))
556 fprintf (f
, " artificial");
557 if (TREE_CODE (node
->decl
) == FUNCTION_DECL
)
559 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
560 fprintf (f
, " constructor");
561 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
562 fprintf (f
, " destructor");
566 if (node
->same_comdat_group
)
567 fprintf (f
, " Same comdat group as: %s/%i\n",
568 symtab_node_asm_name (node
->same_comdat_group
),
569 node
->same_comdat_group
->order
);
570 if (node
->next_sharing_asm_name
)
571 fprintf (f
, " next sharing asm name: %i\n",
572 node
->next_sharing_asm_name
->order
);
573 if (node
->previous_sharing_asm_name
)
574 fprintf (f
, " previous sharing asm name: %i\n",
575 node
->previous_sharing_asm_name
->order
);
577 if (node
->address_taken
)
578 fprintf (f
, " Address is taken.\n");
581 fprintf (f
, " Aux:");
582 dump_addr (f
, " @", (void *)node
->aux
);
585 fprintf (f
, " References: ");
586 ipa_dump_references (f
, &node
->ref_list
);
587 fprintf (f
, " Referring: ");
588 ipa_dump_referring (f
, &node
->ref_list
);
589 if (node
->lto_file_data
)
590 fprintf (f
, " Read from file: %s\n",
591 node
->lto_file_data
->file_name
);
594 /* Dump symtab node. */
597 dump_symtab_node (FILE *f
, symtab_node node
)
599 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
600 dump_cgraph_node (f
, cnode
);
601 else if (varpool_node
*vnode
= dyn_cast
<varpool_node
> (node
))
602 dump_varpool_node (f
, vnode
);
605 /* Dump symbol table. */
608 dump_symtab (FILE *f
)
611 fprintf (f
, "Symbol table:\n\n");
612 FOR_EACH_SYMBOL (node
)
613 dump_symtab_node (f
, node
);
616 /* Dump symtab node NODE to stderr. */
619 debug_symtab_node (symtab_node node
)
621 dump_symtab_node (stderr
, node
);
624 /* Dump symbol table to stderr. */
629 dump_symtab (stderr
);
632 /* Verify common part of symtab nodes. */
635 verify_symtab_base (symtab_node node
)
637 bool error_found
= false;
638 symtab_node hashed_node
;
640 if (is_a
<cgraph_node
> (node
))
642 if (TREE_CODE (node
->decl
) != FUNCTION_DECL
)
644 error ("function symbol is not function");
648 else if (is_a
<varpool_node
> (node
))
650 if (TREE_CODE (node
->decl
) != VAR_DECL
)
652 error ("variable symbol is not variable");
658 error ("node has unknown type");
662 if (cgraph_state
!= CGRAPH_LTO_STREAMING
)
664 hashed_node
= symtab_get_node (node
->decl
);
667 error ("node not found in symtab decl hashtable");
670 if (hashed_node
!= node
671 && (!is_a
<cgraph_node
> (node
)
672 || !dyn_cast
<cgraph_node
> (node
)->clone_of
673 || dyn_cast
<cgraph_node
> (node
)->clone_of
->decl
676 error ("node differs from symtab decl hashtable");
680 if (assembler_name_hash
)
682 hashed_node
= symtab_node_for_asm (DECL_ASSEMBLER_NAME (node
->decl
));
683 if (hashed_node
&& hashed_node
->previous_sharing_asm_name
)
685 error ("assembler name hash list corrupted");
690 if (hashed_node
== node
)
692 hashed_node
= hashed_node
->next_sharing_asm_name
;
695 && !(is_a
<varpool_node
> (node
)
696 || DECL_HARD_REGISTER (node
->decl
)))
698 error ("node not found in symtab assembler name hash");
702 if (node
->previous_sharing_asm_name
703 && node
->previous_sharing_asm_name
->next_sharing_asm_name
!= node
)
705 error ("double linked list of assembler names corrupted");
708 if (node
->analyzed
&& !node
->definition
)
710 error ("node is analyzed byt it is not a definition");
713 if (node
->cpp_implicit_alias
&& !node
->alias
)
715 error ("node is alias but not implicit alias");
718 if (node
->alias
&& !node
->definition
721 error ("node is alias but not definition");
724 if (node
->weakref
&& !node
->alias
)
726 error ("node is weakref but not an alias");
729 if (node
->same_comdat_group
)
731 symtab_node n
= node
->same_comdat_group
;
733 if (!DECL_ONE_ONLY (n
->decl
))
735 error ("non-DECL_ONE_ONLY node in a same_comdat_group list");
738 if (n
->type
!= node
->type
)
740 error ("mixing different types of symbol in same comdat groups is not supported");
745 error ("node is alone in a comdat group");
750 if (!n
->same_comdat_group
)
752 error ("same_comdat_group is not a circular list");
756 n
= n
->same_comdat_group
;
763 /* Verify consistency of NODE. */
766 verify_symtab_node (symtab_node node
)
771 timevar_push (TV_CGRAPH_VERIFY
);
772 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
773 verify_cgraph_node (cnode
);
775 if (verify_symtab_base (node
))
777 dump_symtab_node (stderr
, node
);
778 internal_error ("verify_symtab_node failed");
780 timevar_pop (TV_CGRAPH_VERIFY
);
783 /* Verify symbol table for internal consistency. */
789 FOR_EACH_SYMBOL (node
)
790 verify_symtab_node (node
);
793 /* Return true when RESOLUTION indicate that linker will use
794 the symbol from non-LTO object files. */
797 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution
)
799 return (resolution
== LDPR_PREVAILING_DEF
800 || resolution
== LDPR_PREEMPTED_REG
801 || resolution
== LDPR_RESOLVED_EXEC
802 || resolution
== LDPR_RESOLVED_DYN
);
805 /* Return true when NODE is known to be used from other (non-LTO) object file.
806 Known only when doing LTO via linker plugin. */
809 symtab_used_from_object_file_p (symtab_node node
)
811 if (!TREE_PUBLIC (node
->decl
) || DECL_EXTERNAL (node
->decl
))
813 if (resolution_used_from_other_file_p (node
->resolution
))
818 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
819 but other code such as notice_global_symbol generates rtl. */
822 symtab_make_decl_local (tree decl
)
826 if (TREE_CODE (decl
) == VAR_DECL
)
827 DECL_COMMON (decl
) = 0;
828 else gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
830 if (DECL_ONE_ONLY (decl
) || DECL_COMDAT (decl
))
832 DECL_SECTION_NAME (decl
) = 0;
833 DECL_COMDAT (decl
) = 0;
835 DECL_COMDAT_GROUP (decl
) = 0;
836 DECL_WEAK (decl
) = 0;
837 DECL_EXTERNAL (decl
) = 0;
838 DECL_VISIBILITY_SPECIFIED (decl
) = 0;
839 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
840 TREE_PUBLIC (decl
) = 0;
841 if (!DECL_RTL_SET_P (decl
))
844 /* Update rtl flags. */
845 make_decl_rtl (decl
);
847 rtl
= DECL_RTL (decl
);
851 symbol
= XEXP (rtl
, 0);
852 if (GET_CODE (symbol
) != SYMBOL_REF
)
855 SYMBOL_REF_WEAK (symbol
) = DECL_WEAK (decl
);
858 /* Return availability of NODE. */
861 symtab_node_availability (symtab_node node
)
863 if (is_a
<cgraph_node
> (node
))
864 return cgraph_function_body_availability (cgraph (node
));
866 return cgraph_variable_initializer_availability (varpool (node
));
869 /* Given NODE, walk the alias chain to return the symbol NODE is alias of.
870 If NODE is not an alias, return NODE.
871 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
874 symtab_alias_ultimate_target (symtab_node node
, enum availability
*availability
)
876 bool weakref_p
= false;
881 *availability
= symtab_node_availability (node
);
885 /* To determine visibility of the target, we follow ELF semantic of aliases.
886 Here alias is an alternative assembler name of a given definition. Its
887 availability prevails the availability of its target (i.e. static alias of
888 weak definition is available.
890 Weakref is a different animal (and not part of ELF per se). It is just
891 alternative name of a given symbol used within one complation unit
892 and is translated prior hitting the object file. It inherits the
893 visibility of its target (i.e. weakref of non-overwritable definition
894 is non-overwritable, while weakref of weak definition is weak).
896 If we ever get into supporting targets with different semantics, a target
897 hook will be needed here. */
901 weakref_p
= node
->weakref
;
903 *availability
= symtab_node_availability (node
);
905 *availability
= AVAIL_LOCAL
;
909 if (node
->alias
&& node
->analyzed
)
910 node
= symtab_alias_target (node
);
915 else if (node
->analyzed
)
919 enum availability a
= symtab_node_availability (node
);
920 if (a
< *availability
)
925 *availability
= AVAIL_NOT_AVAILABLE
;
928 if (node
&& availability
&& weakref_p
)
930 enum availability a
= symtab_node_availability (node
);
931 if (a
< *availability
)
933 weakref_p
= node
->weakref
;
937 *availability
= AVAIL_NOT_AVAILABLE
;
941 /* C++ FE sometimes change linkage flags after producing same body aliases.
943 FIXME: C++ produce implicit aliases for virtual functions and vtables that
944 are obviously equivalent. The way it is doing so is however somewhat
945 kludgy and interferes with the visibility code. As a result we need to
946 copy the visibility from the target to get things right. */
949 fixup_same_cpp_alias_visibility (symtab_node node
, symtab_node target
)
951 if (is_a
<cgraph_node
> (node
))
953 DECL_DECLARED_INLINE_P (node
->decl
)
954 = DECL_DECLARED_INLINE_P (target
->decl
);
955 DECL_DISREGARD_INLINE_LIMITS (node
->decl
)
956 = DECL_DISREGARD_INLINE_LIMITS (target
->decl
);
958 /* FIXME: It is not really clear why those flags should not be copied for
962 DECL_WEAK (node
->decl
) = DECL_WEAK (target
->decl
);
963 DECL_EXTERNAL (node
->decl
) = DECL_EXTERNAL (target
->decl
);
964 DECL_VISIBILITY (node
->decl
) = DECL_VISIBILITY (target
->decl
);
966 DECL_VIRTUAL_P (node
->decl
) = DECL_VIRTUAL_P (target
->decl
);
967 if (TREE_PUBLIC (node
->decl
))
969 DECL_EXTERNAL (node
->decl
) = DECL_EXTERNAL (target
->decl
);
970 DECL_COMDAT (node
->decl
) = DECL_COMDAT (target
->decl
);
971 DECL_COMDAT_GROUP (node
->decl
)
972 = DECL_COMDAT_GROUP (target
->decl
);
973 if (DECL_ONE_ONLY (target
->decl
)
974 && !node
->same_comdat_group
)
975 symtab_add_to_same_comdat_group (node
, target
);
977 node
->externally_visible
= target
->externally_visible
;
980 /* Add reference recording that NODE is alias of TARGET.
981 The function can fail in the case of aliasing cycles; in this case
985 symtab_resolve_alias (symtab_node node
, symtab_node target
)
989 gcc_assert (!node
->analyzed
990 && !vec_safe_length (node
->ref_list
.references
));
992 /* Never let cycles to creep into the symbol table alias references;
993 those will make alias walkers to be infinite. */
994 for (n
= target
; n
&& n
->alias
;
995 n
= n
->analyzed
? symtab_alias_target (n
) : NULL
)
998 if (is_a
<cgraph_node
> (node
))
999 error ("function %q+D part of alias cycle", node
->decl
);
1000 else if (is_a
<varpool_node
> (node
))
1001 error ("variable %q+D part of alias cycle", node
->decl
);
1004 node
->alias
= false;
1008 /* "analyze" the node - i.e. mark the reference. */
1009 node
->definition
= true;
1011 node
->analyzed
= true;
1012 ipa_record_reference (node
, target
, IPA_REF_ALIAS
, NULL
);
1014 /* Alias targets become reudndant after alias is resolved into an reference.
1015 We do not want to keep it around or we would have to mind updating them
1016 when renaming symbols. */
1017 node
->alias_target
= NULL
;
1019 if (node
->cpp_implicit_alias
&& cgraph_state
>= CGRAPH_STATE_CONSTRUCTION
)
1020 fixup_same_cpp_alias_visibility (node
, target
);
1022 /* If alias has address taken, so does the target. */
1023 if (node
->address_taken
)
1024 symtab_alias_ultimate_target (target
, NULL
)->address_taken
= true;
1028 /* Call calback on NODE and aliases associated to NODE.
1029 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1033 symtab_for_node_and_aliases (symtab_node node
,
1034 bool (*callback
) (symtab_node
, void *),
1036 bool include_overwritable
)
1039 struct ipa_ref
*ref
;
1041 if (callback (node
, data
))
1043 for (i
= 0; ipa_ref_list_referring_iterate (&node
->ref_list
, i
, ref
); i
++)
1044 if (ref
->use
== IPA_REF_ALIAS
)
1046 symtab_node alias
= ref
->referring
;
1047 if (include_overwritable
1048 || symtab_node_availability (alias
) > AVAIL_OVERWRITABLE
)
1049 if (symtab_for_node_and_aliases (alias
, callback
, data
,
1050 include_overwritable
))
1056 /* Worker searching nonoverwritable alias. */
1059 symtab_nonoverwritable_alias_1 (symtab_node node
, void *data
)
1061 if (decl_binds_to_current_def_p (node
->decl
))
1063 *(symtab_node
*)data
= node
;
1069 /* If NODE can not be overwriten by static or dynamic linker to point to different
1070 definition, return NODE. Otherwise look for alias with such property and if
1071 none exists, introduce new one. */
1074 symtab_nonoverwritable_alias (symtab_node node
)
1077 symtab_node new_node
= NULL
;
1079 /* First try to look up existing alias or base object
1080 (if that is already non-overwritable). */
1081 node
= symtab_alias_ultimate_target (node
, NULL
);
1082 gcc_assert (!node
->alias
&& !node
->weakref
);
1083 symtab_for_node_and_aliases (node
, symtab_nonoverwritable_alias_1
,
1084 (void *)&new_node
, true);
1087 #ifndef ASM_OUTPUT_DEF
1088 /* If aliases aren't supported by the assembler, fail. */
1092 /* Otherwise create a new one. */
1093 new_decl
= copy_node (node
->decl
);
1094 DECL_NAME (new_decl
) = clone_function_name (node
->decl
, "localalias");
1095 if (TREE_CODE (new_decl
) == FUNCTION_DECL
)
1096 DECL_STRUCT_FUNCTION (new_decl
) = NULL
;
1097 DECL_INITIAL (new_decl
) = NULL
;
1098 SET_DECL_ASSEMBLER_NAME (new_decl
, DECL_NAME (new_decl
));
1099 SET_DECL_RTL (new_decl
, NULL
);
1101 /* Update the properties. */
1102 DECL_EXTERNAL (new_decl
) = 0;
1103 if (DECL_ONE_ONLY (node
->decl
))
1104 DECL_SECTION_NAME (new_decl
) = NULL
;
1105 DECL_COMDAT_GROUP (new_decl
) = 0;
1106 TREE_PUBLIC (new_decl
) = 0;
1107 DECL_COMDAT (new_decl
) = 0;
1108 DECL_WEAK (new_decl
) = 0;
1109 DECL_VIRTUAL_P (new_decl
) = 0;
1110 if (TREE_CODE (new_decl
) == FUNCTION_DECL
)
1112 DECL_STATIC_CONSTRUCTOR (new_decl
) = 0;
1113 DECL_STATIC_DESTRUCTOR (new_decl
) = 0;
1114 new_node
= cgraph_create_function_alias
1115 (new_decl
, node
->decl
);
1118 new_node
= varpool_create_variable_alias (new_decl
,
1120 symtab_resolve_alias (new_node
, node
);
1121 gcc_assert (decl_binds_to_current_def_p (new_decl
));
1125 /* Return true if A and B represents semantically equivalent symbols. */
1128 symtab_semantically_equivalent_p (symtab_node a
,
1131 enum availability avail
;
1134 /* Equivalent functions are equivalent. */
1135 if (a
->decl
== b
->decl
)
1138 /* If symbol is not overwritable by different implementation,
1139 walk to the base object it defines. */
1140 ba
= symtab_alias_ultimate_target (a
, &avail
);
1141 if (avail
>= AVAIL_AVAILABLE
)
1148 bb
= symtab_alias_ultimate_target (b
, &avail
);
1149 if (avail
>= AVAIL_AVAILABLE
)
1158 #include "gt-symtab.h"