]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/symtab.c
IPA REF alias refactoring
[thirdparty/gcc.git] / gcc / symtab.c
1 /* Symbol table.
2 Copyright (C) 2012-2014 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
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/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "print-tree.h"
28 #include "varasm.h"
29 #include "function.h"
30 #include "emit-rtl.h"
31 #include "basic-block.h"
32 #include "tree-ssa-alias.h"
33 #include "internal-fn.h"
34 #include "gimple-expr.h"
35 #include "is-a.h"
36 #include "gimple.h"
37 #include "tree-inline.h"
38 #include "langhooks.h"
39 #include "hashtab.h"
40 #include "cgraph.h"
41 #include "diagnostic.h"
42 #include "timevar.h"
43 #include "lto-streamer.h"
44 #include "output.h"
45 #include "ipa-utils.h"
46
47 static const char *ipa_ref_use_name[] = {"read","write","addr","alias"};
48
49 const char * const ld_plugin_symbol_resolution_names[]=
50 {
51 "",
52 "undef",
53 "prevailing_def",
54 "prevailing_def_ironly",
55 "preempted_reg",
56 "preempted_ir",
57 "resolved_ir",
58 "resolved_exec",
59 "resolved_dyn",
60 "prevailing_def_ironly_exp"
61 };
62
63
64 /* Hash table used to hold sectoons. */
65 static GTY((param_is (section_hash_entry))) htab_t section_hash;
66
67 /* Hash table used to convert assembler names into nodes. */
68 static GTY((param_is (symtab_node))) htab_t assembler_name_hash;
69
70 /* Map from a symbol to initialization/finalization priorities. */
71 struct GTY(()) symbol_priority_map {
72 symtab_node *symbol;
73 priority_type init;
74 priority_type fini;
75 };
76
77 /* Hash table used to hold init priorities. */
78 static GTY ((param_is (struct symbol_priority_map)))
79 htab_t init_priority_hash;
80
81 /* Linked list of symbol table nodes. */
82 symtab_node *symtab_nodes;
83
84 /* The order index of the next symtab node to be created. This is
85 used so that we can sort the cgraph nodes in order by when we saw
86 them, to support -fno-toplevel-reorder. */
87 int symtab_order;
88
89 /* Hash asmnames ignoring the user specified marks. */
90
91 static hashval_t
92 decl_assembler_name_hash (const_tree asmname)
93 {
94 if (IDENTIFIER_POINTER (asmname)[0] == '*')
95 {
96 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
97 size_t ulp_len = strlen (user_label_prefix);
98
99 if (ulp_len == 0)
100 ;
101 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
102 decl_str += ulp_len;
103
104 return htab_hash_string (decl_str);
105 }
106
107 return htab_hash_string (IDENTIFIER_POINTER (asmname));
108 }
109
110
111 /* Returns a hash code for P. */
112
113 static hashval_t
114 hash_node_by_assembler_name (const void *p)
115 {
116 const symtab_node *n = (const symtab_node *) p;
117 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
118 }
119
120 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
121
122 static bool
123 decl_assembler_name_equal (tree decl, const_tree asmname)
124 {
125 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
126 const char *decl_str;
127 const char *asmname_str;
128 bool test = false;
129
130 if (decl_asmname == asmname)
131 return true;
132
133 decl_str = IDENTIFIER_POINTER (decl_asmname);
134 asmname_str = IDENTIFIER_POINTER (asmname);
135
136
137 /* If the target assembler name was set by the user, things are trickier.
138 We have a leading '*' to begin with. After that, it's arguable what
139 is the correct thing to do with -fleading-underscore. Arguably, we've
140 historically been doing the wrong thing in assemble_alias by always
141 printing the leading underscore. Since we're not changing that, make
142 sure user_label_prefix follows the '*' before matching. */
143 if (decl_str[0] == '*')
144 {
145 size_t ulp_len = strlen (user_label_prefix);
146
147 decl_str ++;
148
149 if (ulp_len == 0)
150 test = true;
151 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
152 decl_str += ulp_len, test=true;
153 else
154 decl_str --;
155 }
156 if (asmname_str[0] == '*')
157 {
158 size_t ulp_len = strlen (user_label_prefix);
159
160 asmname_str ++;
161
162 if (ulp_len == 0)
163 test = true;
164 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
165 asmname_str += ulp_len, test=true;
166 else
167 asmname_str --;
168 }
169
170 if (!test)
171 return false;
172 return strcmp (decl_str, asmname_str) == 0;
173 }
174
175
176 /* Returns nonzero if P1 and P2 are equal. */
177
178 static int
179 eq_assembler_name (const void *p1, const void *p2)
180 {
181 const symtab_node *n1 = (const symtab_node *) p1;
182 const_tree name = (const_tree)p2;
183 return (decl_assembler_name_equal (n1->decl, name));
184 }
185
186 /* Insert NODE to assembler name hash. */
187
188 static void
189 insert_to_assembler_name_hash (symtab_node *node, bool with_clones)
190 {
191 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
192 return;
193 gcc_checking_assert (!node->previous_sharing_asm_name
194 && !node->next_sharing_asm_name);
195 if (assembler_name_hash)
196 {
197 void **aslot;
198 struct cgraph_node *cnode;
199 tree decl = node->decl;
200
201 tree name = DECL_ASSEMBLER_NAME (node->decl);
202
203 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
204 decl_assembler_name_hash (name),
205 INSERT);
206 gcc_assert (*aslot != node);
207 node->next_sharing_asm_name = (symtab_node *)*aslot;
208 if (*aslot != NULL)
209 ((symtab_node *)*aslot)->previous_sharing_asm_name = node;
210 *aslot = node;
211
212 /* Update also possible inline clones sharing a decl. */
213 cnode = dyn_cast <cgraph_node *> (node);
214 if (cnode && cnode->clones && with_clones)
215 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
216 if (cnode->decl == decl)
217 insert_to_assembler_name_hash (cnode, true);
218 }
219
220 }
221
222 /* Remove NODE from assembler name hash. */
223
224 static void
225 unlink_from_assembler_name_hash (symtab_node *node, bool with_clones)
226 {
227 if (assembler_name_hash)
228 {
229 struct cgraph_node *cnode;
230 tree decl = node->decl;
231
232 if (node->next_sharing_asm_name)
233 node->next_sharing_asm_name->previous_sharing_asm_name
234 = node->previous_sharing_asm_name;
235 if (node->previous_sharing_asm_name)
236 {
237 node->previous_sharing_asm_name->next_sharing_asm_name
238 = node->next_sharing_asm_name;
239 }
240 else
241 {
242 tree name = DECL_ASSEMBLER_NAME (node->decl);
243 void **slot;
244 slot = htab_find_slot_with_hash (assembler_name_hash, name,
245 decl_assembler_name_hash (name),
246 NO_INSERT);
247 gcc_assert (*slot == node);
248 if (!node->next_sharing_asm_name)
249 htab_clear_slot (assembler_name_hash, slot);
250 else
251 *slot = node->next_sharing_asm_name;
252 }
253 node->next_sharing_asm_name = NULL;
254 node->previous_sharing_asm_name = NULL;
255
256 /* Update also possible inline clones sharing a decl. */
257 cnode = dyn_cast <cgraph_node *> (node);
258 if (cnode && cnode->clones && with_clones)
259 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
260 if (cnode->decl == decl)
261 unlink_from_assembler_name_hash (cnode, true);
262 }
263 }
264
265 /* Arrange node to be first in its entry of assembler_name_hash. */
266
267 void
268 symtab_prevail_in_asm_name_hash (symtab_node *node)
269 {
270 unlink_from_assembler_name_hash (node, false);
271 insert_to_assembler_name_hash (node, false);
272 }
273
274
275 /* Add node into symbol table. This function is not used directly, but via
276 cgraph/varpool node creation routines. */
277
278 void
279 symtab_register_node (symtab_node *node)
280 {
281 node->next = symtab_nodes;
282 node->previous = NULL;
283 if (symtab_nodes)
284 symtab_nodes->previous = node;
285 symtab_nodes = node;
286
287 if (!node->decl->decl_with_vis.symtab_node)
288 node->decl->decl_with_vis.symtab_node = node;
289
290 node->ref_list.clear ();
291
292 node->order = symtab_order++;
293
294 /* Be sure to do this last; C++ FE might create new nodes via
295 DECL_ASSEMBLER_NAME langhook! */
296 insert_to_assembler_name_hash (node, false);
297 }
298
299 /* Remove NODE from same comdat group. */
300
301 void
302 symtab_remove_from_same_comdat_group (symtab_node *node)
303 {
304 if (node->same_comdat_group)
305 {
306 symtab_node *prev;
307 for (prev = node->same_comdat_group;
308 prev->same_comdat_group != node;
309 prev = prev->same_comdat_group)
310 ;
311 if (node->same_comdat_group == prev)
312 prev->same_comdat_group = NULL;
313 else
314 prev->same_comdat_group = node->same_comdat_group;
315 node->same_comdat_group = NULL;
316 }
317 }
318
319 /* Remove node from symbol table. This function is not used directly, but via
320 cgraph/varpool node removal routines. */
321
322 void
323 symtab_unregister_node (symtab_node *node)
324 {
325 node->remove_all_references ();
326 node->remove_all_referring ();
327
328 /* Remove reference to section. */
329 node->set_section_for_node (NULL);
330
331 symtab_remove_from_same_comdat_group (node);
332
333 if (node->previous)
334 node->previous->next = node->next;
335 else
336 symtab_nodes = node->next;
337 if (node->next)
338 node->next->previous = node->previous;
339 node->next = NULL;
340 node->previous = NULL;
341
342 /* During LTO symtab merging we temporarily corrupt decl to symtab node
343 hash. */
344 gcc_assert (node->decl->decl_with_vis.symtab_node || in_lto_p);
345 if (node->decl->decl_with_vis.symtab_node == node)
346 {
347 symtab_node *replacement_node = NULL;
348 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
349 replacement_node = cgraph_find_replacement_node (cnode);
350 node->decl->decl_with_vis.symtab_node = replacement_node;
351 }
352 if (!is_a <varpool_node *> (node) || !DECL_HARD_REGISTER (node->decl))
353 unlink_from_assembler_name_hash (node, false);
354 if (node->in_init_priority_hash)
355 {
356 struct symbol_priority_map in;
357 void **slot;
358 in.symbol = node;
359
360 slot = htab_find_slot (init_priority_hash, &in, NO_INSERT);
361 if (slot)
362 htab_clear_slot (init_priority_hash, slot);
363 }
364 }
365
366
367 /* Remove symtab NODE from the symbol table. */
368
369 void
370 symtab_remove_node (symtab_node *node)
371 {
372 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
373 cgraph_remove_node (cnode);
374 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
375 varpool_remove_node (vnode);
376 }
377
378 /* Initalize asm name hash unless. */
379
380 void
381 symtab_initialize_asm_name_hash (void)
382 {
383 symtab_node *node;
384 if (!assembler_name_hash)
385 {
386 assembler_name_hash =
387 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
388 NULL);
389 FOR_EACH_SYMBOL (node)
390 insert_to_assembler_name_hash (node, false);
391 }
392 }
393
394 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
395 Return NULL if there's no such node. */
396
397 symtab_node *
398 symtab_node_for_asm (const_tree asmname)
399 {
400 symtab_node *node;
401 void **slot;
402
403 symtab_initialize_asm_name_hash ();
404 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
405 decl_assembler_name_hash (asmname),
406 NO_INSERT);
407
408 if (slot)
409 {
410 node = (symtab_node *) *slot;
411 return node;
412 }
413 return NULL;
414 }
415
416 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
417
418 void
419 change_decl_assembler_name (tree decl, tree name)
420 {
421 symtab_node *node = NULL;
422
423 /* We can have user ASM names on things, like global register variables, that
424 are not in the symbol table. */
425 if ((TREE_CODE (decl) == VAR_DECL
426 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
427 || TREE_CODE (decl) == FUNCTION_DECL)
428 node = symtab_get_node (decl);
429 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
430 {
431 SET_DECL_ASSEMBLER_NAME (decl, name);
432 if (node)
433 insert_to_assembler_name_hash (node, true);
434 }
435 else
436 {
437 if (name == DECL_ASSEMBLER_NAME (decl))
438 return;
439
440 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
441 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
442 : NULL);
443 if (node)
444 unlink_from_assembler_name_hash (node, true);
445 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
446 && DECL_RTL_SET_P (decl))
447 warning (0, "%D renamed after being referenced in assembly", decl);
448
449 SET_DECL_ASSEMBLER_NAME (decl, name);
450 if (alias)
451 {
452 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
453 TREE_CHAIN (name) = alias;
454 }
455 if (node)
456 insert_to_assembler_name_hash (node, true);
457 }
458 }
459
460 /* Add NEW_ to the same comdat group that OLD is in. */
461
462 void
463 symtab_add_to_same_comdat_group (symtab_node *new_node,
464 symtab_node *old_node)
465 {
466 gcc_assert (old_node->get_comdat_group ());
467 gcc_assert (!new_node->same_comdat_group);
468 gcc_assert (new_node != old_node);
469
470 new_node->set_comdat_group (old_node->get_comdat_group ());
471 new_node->same_comdat_group = old_node;
472 if (!old_node->same_comdat_group)
473 old_node->same_comdat_group = new_node;
474 else
475 {
476 symtab_node *n;
477 for (n = old_node->same_comdat_group;
478 n->same_comdat_group != old_node;
479 n = n->same_comdat_group)
480 ;
481 n->same_comdat_group = new_node;
482 }
483 }
484
485 /* Dissolve the same_comdat_group list in which NODE resides. */
486
487 void
488 symtab_dissolve_same_comdat_group_list (symtab_node *node)
489 {
490 symtab_node *n = node;
491 symtab_node *next;
492
493 if (!node->same_comdat_group)
494 return;
495 do
496 {
497 next = n->same_comdat_group;
498 n->same_comdat_group = NULL;
499 /* Clear comdat_group for comdat locals, since
500 make_decl_local doesn't. */
501 if (!TREE_PUBLIC (n->decl))
502 n->set_comdat_group (NULL);
503 n = next;
504 }
505 while (n != node);
506 }
507
508 /* Return printable assembler name of NODE.
509 This function is used only for debugging. When assembler name
510 is unknown go with identifier name. */
511
512 const char *
513 symtab_node::asm_name () const
514 {
515 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
516 return lang_hooks.decl_printable_name (decl, 2);
517 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
518 }
519
520 /* Return printable identifier name. */
521
522 const char *
523 symtab_node::name () const
524 {
525 return lang_hooks.decl_printable_name (decl, 2);
526 }
527
528 /* Return ipa reference from this symtab_node to
529 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
530 of the use. */
531
532 struct ipa_ref *
533 symtab_node::add_reference (symtab_node *referred_node,
534 enum ipa_ref_use use_type)
535 {
536 return add_reference (referred_node, use_type, NULL);
537 }
538
539
540 /* Return ipa reference from this symtab_node to
541 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
542 of the use and STMT the statement (if it exists). */
543
544 struct ipa_ref *
545 symtab_node::add_reference (symtab_node *referred_node,
546 enum ipa_ref_use use_type, gimple stmt)
547 {
548 struct ipa_ref *ref = NULL, *ref2 = NULL;
549 struct ipa_ref_list *list, *list2;
550 ipa_ref_t *old_references;
551
552 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
553 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
554
555 list = &ref_list;
556 old_references = vec_safe_address (list->references);
557 vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
558 ref = &list->references->last ();
559
560 list2 = &referred_node->ref_list;
561
562 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
563 if(use_type == IPA_REF_ALIAS)
564 {
565 list2->referring.safe_insert (0, ref);
566 ref->referred_index = 0;
567
568 for (unsigned int i = 1; i < list2->referring.length (); i++)
569 list2->referring[i]->referred_index = i;
570 }
571 else
572 {
573 list2->referring.safe_push (ref);
574 ref->referred_index = list2->referring.length () - 1;
575 }
576
577 ref->referring = this;
578 ref->referred = referred_node;
579 ref->stmt = stmt;
580 ref->lto_stmt_uid = 0;
581 ref->use = use_type;
582 ref->speculative = 0;
583
584 /* If vector was moved in memory, update pointers. */
585 if (old_references != list->references->address ())
586 {
587 int i;
588 for (i = 0; iterate_reference(i, ref2); i++)
589 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
590 }
591 return ref;
592 }
593
594 /* If VAL is a reference to a function or a variable, add a reference from
595 this symtab_node to the corresponding symbol table node. USE_TYPE specify
596 type of the use and STMT the statement (if it exists). Return the new
597 reference or NULL if none was created. */
598
599 struct ipa_ref *
600 symtab_node::maybe_add_reference (tree val, enum ipa_ref_use use_type,
601 gimple stmt)
602 {
603 STRIP_NOPS (val);
604 if (TREE_CODE (val) != ADDR_EXPR)
605 return NULL;
606 val = get_base_var (val);
607 if (val && (TREE_CODE (val) == FUNCTION_DECL
608 || TREE_CODE (val) == VAR_DECL))
609 {
610 symtab_node *referred = symtab_get_node (val);
611 gcc_checking_assert (referred);
612 return add_reference (referred, use_type, stmt);
613 }
614 return NULL;
615 }
616
617 /* Clone all references from symtab NODE to this symtab_node. */
618
619 void
620 symtab_node::clone_references (struct symtab_node *node)
621 {
622 struct ipa_ref *ref = NULL, *ref2 = NULL;
623 int i;
624 for (i = 0; node->iterate_reference (i, ref); i++)
625 {
626 bool speculative = ref->speculative;
627 unsigned int stmt_uid = ref->lto_stmt_uid;
628
629 ref2 = add_reference (ref->referred, ref->use, ref->stmt);
630 ref2->speculative = speculative;
631 ref2->lto_stmt_uid = stmt_uid;
632 }
633 }
634
635 /* Clone all referring from symtab NODE to this symtab_node. */
636
637 void
638 symtab_node::clone_referring (struct symtab_node *node)
639 {
640 struct ipa_ref *ref = NULL, *ref2 = NULL;
641 int i;
642 for (i = 0; node->iterate_referring(i, ref); i++)
643 {
644 bool speculative = ref->speculative;
645 unsigned int stmt_uid = ref->lto_stmt_uid;
646
647 ref2 = ref->referring->add_reference (this, ref->use, ref->stmt);
648 ref2->speculative = speculative;
649 ref2->lto_stmt_uid = stmt_uid;
650 }
651 }
652
653 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
654
655 struct ipa_ref *
656 symtab_node::clone_reference (struct ipa_ref *ref, gimple stmt)
657 {
658 bool speculative = ref->speculative;
659 unsigned int stmt_uid = ref->lto_stmt_uid;
660 struct ipa_ref *ref2;
661
662 ref2 = add_reference (ref->referred, ref->use, stmt);
663 ref2->speculative = speculative;
664 ref2->lto_stmt_uid = stmt_uid;
665 return ref2;
666 }
667
668 /* Find the structure describing a reference to REFERRED_NODE
669 and associated with statement STMT. */
670
671 struct ipa_ref *
672 symtab_node::find_reference (symtab_node *referred_node,
673 gimple stmt, unsigned int lto_stmt_uid)
674 {
675 struct ipa_ref *r = NULL;
676 int i;
677
678 for (i = 0; iterate_reference (i, r); i++)
679 if (r->referred == referred_node
680 && !r->speculative
681 && ((stmt && r->stmt == stmt)
682 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
683 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
684 return r;
685 return NULL;
686 }
687
688 /* Remove all references that are associated with statement STMT. */
689
690 void
691 symtab_node::remove_stmt_references (gimple stmt)
692 {
693 struct ipa_ref *r = NULL;
694 int i = 0;
695
696 while (iterate_reference (i, r))
697 if (r->stmt == stmt)
698 r->remove_reference ();
699 else
700 i++;
701 }
702
703 /* Remove all stmt references in non-speculative references.
704 Those are not maintained during inlining & clonning.
705 The exception are speculative references that are updated along
706 with callgraph edges associated with them. */
707
708 void
709 symtab_node::clear_stmts_in_references (void)
710 {
711 struct ipa_ref *r = NULL;
712 int i;
713
714 for (i = 0; iterate_reference (i, r); i++)
715 if (!r->speculative)
716 {
717 r->stmt = NULL;
718 r->lto_stmt_uid = 0;
719 }
720 }
721
722 /* Remove all references in ref list. */
723
724 void
725 symtab_node::remove_all_references (void)
726 {
727 while (vec_safe_length (ref_list.references))
728 ref_list.references->last ().remove_reference ();
729 vec_free (ref_list.references);
730 }
731
732 /* Remove all referring items in ref list. */
733
734 void
735 symtab_node::remove_all_referring (void)
736 {
737 while (ref_list.referring.length ())
738 ref_list.referring.last ()->remove_reference ();
739 ref_list.referring.release ();
740 }
741
742 /* Dump references in ref list to FILE. */
743
744 void
745 symtab_node::dump_references (FILE *file)
746 {
747 struct ipa_ref *ref = NULL;
748 int i;
749 for (i = 0; iterate_reference (i, ref); i++)
750 {
751 fprintf (file, "%s/%i (%s)",
752 ref->referred->asm_name (),
753 ref->referred->order,
754 ipa_ref_use_name [ref->use]);
755 if (ref->speculative)
756 fprintf (file, " (speculative)");
757 }
758 fprintf (file, "\n");
759 }
760
761 /* Dump referring in list to FILE. */
762
763 void
764 symtab_node::dump_referring (FILE *file)
765 {
766 struct ipa_ref *ref = NULL;
767 int i;
768 for (i = 0; iterate_referring(i, ref); i++)
769 {
770 fprintf (file, "%s/%i (%s)",
771 ref->referring->asm_name (),
772 ref->referring->order,
773 ipa_ref_use_name [ref->use]);
774 if (ref->speculative)
775 fprintf (file, " (speculative)");
776 }
777 fprintf (file, "\n");
778 }
779
780 /* Return true if list contains an alias. */
781 bool
782 symtab_node::has_aliases_p (void)
783 {
784 struct ipa_ref *ref = NULL;
785 int i;
786
787 for (i = 0; iterate_referring (i, ref); i++)
788 if (ref->use == IPA_REF_ALIAS)
789 return true;
790 return false;
791 }
792
793 /* Iterates I-th reference in the list, REF is also set. */
794
795 struct ipa_ref *
796 symtab_node::iterate_reference (unsigned i, struct ipa_ref *&ref)
797 {
798 vec_safe_iterate (ref_list.references, i, &ref);
799
800 return ref;
801 }
802
803 /* Iterates I-th referring item in the list, REF is also set. */
804
805 struct ipa_ref *
806 symtab_node::iterate_referring (unsigned i, struct ipa_ref *&ref)
807 {
808 ref_list.referring.iterate (i, &ref);
809
810 return ref;
811 }
812
813 /* Iterates I-th referring alias item in the list, REF is also set. */
814
815 struct ipa_ref *
816 symtab_node::iterate_direct_aliases (unsigned i, struct ipa_ref *&ref)
817 {
818 ref_list.referring.iterate (i, &ref);
819
820 if (ref && ref->use != IPA_REF_ALIAS)
821 return NULL;
822
823 return ref;
824 }
825
826
827 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
828
829 /* Dump base fields of symtab nodes. Not to be used directly. */
830
831 void
832 dump_symtab_base (FILE *f, symtab_node *node)
833 {
834 static const char * const visibility_types[] = {
835 "default", "protected", "hidden", "internal"
836 };
837
838 fprintf (f, "%s/%i (%s)",
839 node->asm_name (),
840 node->order,
841 node->name ());
842 dump_addr (f, " @", (void *)node);
843 fprintf (f, "\n Type: %s", symtab_type_names[node->type]);
844
845 if (node->definition)
846 fprintf (f, " definition");
847 if (node->analyzed)
848 fprintf (f, " analyzed");
849 if (node->alias)
850 fprintf (f, " alias");
851 if (node->weakref)
852 fprintf (f, " weakref");
853 if (node->cpp_implicit_alias)
854 fprintf (f, " cpp_implicit_alias");
855 if (node->alias_target)
856 fprintf (f, " target:%s",
857 DECL_P (node->alias_target)
858 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
859 (node->alias_target))
860 : IDENTIFIER_POINTER (node->alias_target));
861 if (node->body_removed)
862 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
863 fprintf (f, "\n Visibility:");
864 if (node->in_other_partition)
865 fprintf (f, " in_other_partition");
866 if (node->used_from_other_partition)
867 fprintf (f, " used_from_other_partition");
868 if (node->force_output)
869 fprintf (f, " force_output");
870 if (node->forced_by_abi)
871 fprintf (f, " forced_by_abi");
872 if (node->externally_visible)
873 fprintf (f, " externally_visible");
874 if (node->resolution != LDPR_UNKNOWN)
875 fprintf (f, " %s",
876 ld_plugin_symbol_resolution_names[(int)node->resolution]);
877 if (TREE_ASM_WRITTEN (node->decl))
878 fprintf (f, " asm_written");
879 if (DECL_EXTERNAL (node->decl))
880 fprintf (f, " external");
881 if (TREE_PUBLIC (node->decl))
882 fprintf (f, " public");
883 if (DECL_COMMON (node->decl))
884 fprintf (f, " common");
885 if (DECL_WEAK (node->decl))
886 fprintf (f, " weak");
887 if (DECL_DLLIMPORT_P (node->decl))
888 fprintf (f, " dll_import");
889 if (DECL_COMDAT (node->decl))
890 fprintf (f, " comdat");
891 if (node->get_comdat_group ())
892 fprintf (f, " comdat_group:%s",
893 IDENTIFIER_POINTER (node->get_comdat_group_id ()));
894 if (DECL_ONE_ONLY (node->decl))
895 fprintf (f, " one_only");
896 if (node->get_section ())
897 fprintf (f, " section:%s",
898 node->get_section ());
899 if (node->implicit_section)
900 fprintf (f," (implicit_section)");
901 if (DECL_VISIBILITY_SPECIFIED (node->decl))
902 fprintf (f, " visibility_specified");
903 if (DECL_VISIBILITY (node->decl))
904 fprintf (f, " visibility:%s",
905 visibility_types [DECL_VISIBILITY (node->decl)]);
906 if (DECL_VIRTUAL_P (node->decl))
907 fprintf (f, " virtual");
908 if (DECL_ARTIFICIAL (node->decl))
909 fprintf (f, " artificial");
910 if (TREE_CODE (node->decl) == FUNCTION_DECL)
911 {
912 if (DECL_STATIC_CONSTRUCTOR (node->decl))
913 fprintf (f, " constructor");
914 if (DECL_STATIC_DESTRUCTOR (node->decl))
915 fprintf (f, " destructor");
916 }
917 fprintf (f, "\n");
918
919 if (node->same_comdat_group)
920 fprintf (f, " Same comdat group as: %s/%i\n",
921 node->same_comdat_group->asm_name (),
922 node->same_comdat_group->order);
923 if (node->next_sharing_asm_name)
924 fprintf (f, " next sharing asm name: %i\n",
925 node->next_sharing_asm_name->order);
926 if (node->previous_sharing_asm_name)
927 fprintf (f, " previous sharing asm name: %i\n",
928 node->previous_sharing_asm_name->order);
929
930 if (node->address_taken)
931 fprintf (f, " Address is taken.\n");
932 if (node->aux)
933 {
934 fprintf (f, " Aux:");
935 dump_addr (f, " @", (void *)node->aux);
936 }
937
938 fprintf (f, " References: ");
939 node->dump_references (f);
940 fprintf (f, " Referring: ");
941 node->dump_referring (f);
942 if (node->lto_file_data)
943 fprintf (f, " Read from file: %s\n",
944 node->lto_file_data->file_name);
945 }
946
947 /* Dump symtab node. */
948
949 void
950 dump_symtab_node (FILE *f, symtab_node *node)
951 {
952 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
953 dump_cgraph_node (f, cnode);
954 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
955 dump_varpool_node (f, vnode);
956 }
957
958 /* Dump symbol table. */
959
960 void
961 dump_symtab (FILE *f)
962 {
963 symtab_node *node;
964 fprintf (f, "Symbol table:\n\n");
965 FOR_EACH_SYMBOL (node)
966 dump_symtab_node (f, node);
967 }
968
969 /* Dump symtab node NODE to stderr. */
970
971 DEBUG_FUNCTION void
972 debug_symtab_node (symtab_node *node)
973 {
974 dump_symtab_node (stderr, node);
975 }
976
977 /* Dump symbol table to stderr. */
978
979 DEBUG_FUNCTION void
980 debug_symtab (void)
981 {
982 dump_symtab (stderr);
983 }
984
985 /* Verify common part of symtab nodes. */
986
987 DEBUG_FUNCTION bool
988 verify_symtab_base (symtab_node *node)
989 {
990 bool error_found = false;
991 symtab_node *hashed_node;
992
993 if (is_a <cgraph_node *> (node))
994 {
995 if (TREE_CODE (node->decl) != FUNCTION_DECL)
996 {
997 error ("function symbol is not function");
998 error_found = true;
999 }
1000 }
1001 else if (is_a <varpool_node *> (node))
1002 {
1003 if (TREE_CODE (node->decl) != VAR_DECL)
1004 {
1005 error ("variable symbol is not variable");
1006 error_found = true;
1007 }
1008 }
1009 else
1010 {
1011 error ("node has unknown type");
1012 error_found = true;
1013 }
1014
1015 if (cgraph_state != CGRAPH_LTO_STREAMING)
1016 {
1017 hashed_node = symtab_get_node (node->decl);
1018 if (!hashed_node)
1019 {
1020 error ("node not found node->decl->decl_with_vis.symtab_node");
1021 error_found = true;
1022 }
1023 if (hashed_node != node
1024 && (!is_a <cgraph_node *> (node)
1025 || !dyn_cast <cgraph_node *> (node)->clone_of
1026 || dyn_cast <cgraph_node *> (node)->clone_of->decl
1027 != node->decl))
1028 {
1029 error ("node differs from node->decl->decl_with_vis.symtab_node");
1030 error_found = true;
1031 }
1032 }
1033 if (assembler_name_hash)
1034 {
1035 hashed_node = symtab_node_for_asm (DECL_ASSEMBLER_NAME (node->decl));
1036 if (hashed_node && hashed_node->previous_sharing_asm_name)
1037 {
1038 error ("assembler name hash list corrupted");
1039 error_found = true;
1040 }
1041 while (hashed_node)
1042 {
1043 if (hashed_node == node)
1044 break;
1045 hashed_node = hashed_node->next_sharing_asm_name;
1046 }
1047 if (!hashed_node
1048 && !(is_a <varpool_node *> (node)
1049 || DECL_HARD_REGISTER (node->decl)))
1050 {
1051 error ("node not found in symtab assembler name hash");
1052 error_found = true;
1053 }
1054 }
1055 if (node->previous_sharing_asm_name
1056 && node->previous_sharing_asm_name->next_sharing_asm_name != node)
1057 {
1058 error ("double linked list of assembler names corrupted");
1059 error_found = true;
1060 }
1061 if (node->analyzed && !node->definition)
1062 {
1063 error ("node is analyzed byt it is not a definition");
1064 error_found = true;
1065 }
1066 if (node->cpp_implicit_alias && !node->alias)
1067 {
1068 error ("node is alias but not implicit alias");
1069 error_found = true;
1070 }
1071 if (node->alias && !node->definition
1072 && !node->weakref)
1073 {
1074 error ("node is alias but not definition");
1075 error_found = true;
1076 }
1077 if (node->weakref && !node->alias)
1078 {
1079 error ("node is weakref but not an alias");
1080 error_found = true;
1081 }
1082 if (node->same_comdat_group)
1083 {
1084 symtab_node *n = node->same_comdat_group;
1085
1086 if (!n->get_comdat_group ())
1087 {
1088 error ("node is in same_comdat_group list but has no comdat_group");
1089 error_found = true;
1090 }
1091 if (n->get_comdat_group () != node->get_comdat_group ())
1092 {
1093 error ("same_comdat_group list across different groups");
1094 error_found = true;
1095 }
1096 if (!n->definition)
1097 {
1098 error ("Node has same_comdat_group but it is not a definition");
1099 error_found = true;
1100 }
1101 if (n->type != node->type)
1102 {
1103 error ("mixing different types of symbol in same comdat groups is not supported");
1104 error_found = true;
1105 }
1106 if (n == node)
1107 {
1108 error ("node is alone in a comdat group");
1109 error_found = true;
1110 }
1111 do
1112 {
1113 if (!n->same_comdat_group)
1114 {
1115 error ("same_comdat_group is not a circular list");
1116 error_found = true;
1117 break;
1118 }
1119 n = n->same_comdat_group;
1120 }
1121 while (n != node);
1122 if (symtab_comdat_local_p (node))
1123 {
1124 struct ipa_ref *ref = NULL;
1125
1126 for (int i = 0; node->iterate_referring (i, ref); ++i)
1127 {
1128 if (!symtab_in_same_comdat_p (ref->referring, node))
1129 {
1130 error ("comdat-local symbol referred to by %s outside its "
1131 "comdat",
1132 identifier_to_locale (ref->referring->name()));
1133 error_found = true;
1134 }
1135 }
1136 }
1137 }
1138 if (node->implicit_section && !node->get_section ())
1139 {
1140 error ("implicit_section flag is set but section isn't");
1141 error_found = true;
1142 }
1143 if (node->get_section () && node->get_comdat_group ()
1144 && !node->implicit_section)
1145 {
1146 error ("Both section and comdat group is set");
1147 error_found = true;
1148 }
1149 /* TODO: Add string table for sections, so we do not keep holding duplicated
1150 strings. */
1151 if (node->alias && node->definition
1152 && node->get_section () != symtab_alias_target (node)->get_section ()
1153 && (!node->get_section()
1154 || !symtab_alias_target (node)->get_section ()
1155 || strcmp (node->get_section(),
1156 symtab_alias_target (node)->get_section ())))
1157 {
1158 error ("Alias and target's section differs");
1159 dump_symtab_node (stderr, symtab_alias_target (node));
1160 error_found = true;
1161 }
1162 if (node->alias && node->definition
1163 && node->get_comdat_group () != symtab_alias_target (node)->get_comdat_group ())
1164 {
1165 error ("Alias and target's comdat groups differs");
1166 dump_symtab_node (stderr, symtab_alias_target (node));
1167 error_found = true;
1168 }
1169
1170 return error_found;
1171 }
1172
1173 /* Verify consistency of NODE. */
1174
1175 DEBUG_FUNCTION void
1176 verify_symtab_node (symtab_node *node)
1177 {
1178 if (seen_error ())
1179 return;
1180
1181 timevar_push (TV_CGRAPH_VERIFY);
1182 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1183 verify_cgraph_node (cnode);
1184 else
1185 if (verify_symtab_base (node))
1186 {
1187 dump_symtab_node (stderr, node);
1188 internal_error ("verify_symtab_node failed");
1189 }
1190 timevar_pop (TV_CGRAPH_VERIFY);
1191 }
1192
1193 /* Verify symbol table for internal consistency. */
1194
1195 DEBUG_FUNCTION void
1196 verify_symtab (void)
1197 {
1198 symtab_node *node;
1199 hash_map<tree, symtab_node *> comdat_head_map (251);
1200
1201 FOR_EACH_SYMBOL (node)
1202 {
1203 verify_symtab_node (node);
1204 if (node->get_comdat_group ())
1205 {
1206 symtab_node **entry, *s;
1207 bool existed;
1208
1209 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1210 &existed);
1211 if (!existed)
1212 *entry = node;
1213 else
1214 for (s = (*entry)->same_comdat_group; s != NULL && s != node; s = s->same_comdat_group)
1215 if (!s || s == *entry)
1216 {
1217 error ("Two symbols with same comdat_group are not linked by the same_comdat_group list.");
1218 dump_symtab_node (stderr, *entry);
1219 dump_symtab_node (stderr, s);
1220 internal_error ("verify_symtab failed");
1221 }
1222 }
1223 }
1224 }
1225
1226 /* Return true when RESOLUTION indicate that linker will use
1227 the symbol from non-LTO object files. */
1228
1229 bool
1230 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
1231 {
1232 return (resolution == LDPR_PREVAILING_DEF
1233 || resolution == LDPR_PREEMPTED_REG
1234 || resolution == LDPR_RESOLVED_EXEC
1235 || resolution == LDPR_RESOLVED_DYN);
1236 }
1237
1238 /* Return true when NODE is known to be used from other (non-LTO) object file.
1239 Known only when doing LTO via linker plugin. */
1240
1241 bool
1242 symtab_used_from_object_file_p (symtab_node *node)
1243 {
1244 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
1245 return false;
1246 if (resolution_used_from_other_file_p (node->resolution))
1247 return true;
1248 return false;
1249 }
1250
1251 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1252 but other code such as notice_global_symbol generates rtl. */
1253
1254 void
1255 symtab_make_decl_local (tree decl)
1256 {
1257 rtx rtl, symbol;
1258
1259 /* Avoid clearing comdat_groups on comdat-local decls. */
1260 if (TREE_PUBLIC (decl) == 0)
1261 return;
1262
1263 if (TREE_CODE (decl) == VAR_DECL)
1264 DECL_COMMON (decl) = 0;
1265 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1266
1267 DECL_COMDAT (decl) = 0;
1268 DECL_WEAK (decl) = 0;
1269 DECL_EXTERNAL (decl) = 0;
1270 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1271 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1272 TREE_PUBLIC (decl) = 0;
1273 if (!DECL_RTL_SET_P (decl))
1274 return;
1275
1276 /* Update rtl flags. */
1277 make_decl_rtl (decl);
1278
1279 rtl = DECL_RTL (decl);
1280 if (!MEM_P (rtl))
1281 return;
1282
1283 symbol = XEXP (rtl, 0);
1284 if (GET_CODE (symbol) != SYMBOL_REF)
1285 return;
1286
1287 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1288 }
1289
1290 /* Return availability of NODE. */
1291
1292 enum availability
1293 symtab_node_availability (symtab_node *node)
1294 {
1295 if (is_a <cgraph_node *> (node))
1296 return cgraph_function_body_availability (cgraph (node));
1297 else
1298 return cgraph_variable_initializer_availability (varpool (node));
1299 }
1300
1301 /* Given NODE, walk the alias chain to return the symbol NODE is alias of.
1302 If NODE is not an alias, return NODE.
1303 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
1304
1305 symtab_node *
1306 symtab_alias_ultimate_target (symtab_node *node, enum availability *availability)
1307 {
1308 bool weakref_p = false;
1309
1310 if (!node->alias)
1311 {
1312 if (availability)
1313 *availability = symtab_node_availability (node);
1314 return node;
1315 }
1316
1317 /* To determine visibility of the target, we follow ELF semantic of aliases.
1318 Here alias is an alternative assembler name of a given definition. Its
1319 availability prevails the availability of its target (i.e. static alias of
1320 weak definition is available.
1321
1322 Weakref is a different animal (and not part of ELF per se). It is just
1323 alternative name of a given symbol used within one complation unit
1324 and is translated prior hitting the object file. It inherits the
1325 visibility of its target (i.e. weakref of non-overwritable definition
1326 is non-overwritable, while weakref of weak definition is weak).
1327
1328 If we ever get into supporting targets with different semantics, a target
1329 hook will be needed here. */
1330
1331 if (availability)
1332 {
1333 weakref_p = node->weakref;
1334 if (!weakref_p)
1335 *availability = symtab_node_availability (node);
1336 else
1337 *availability = AVAIL_LOCAL;
1338 }
1339 while (node)
1340 {
1341 if (node->alias && node->analyzed)
1342 node = symtab_alias_target (node);
1343 else
1344 {
1345 if (!availability)
1346 ;
1347 else if (node->analyzed)
1348 {
1349 if (weakref_p)
1350 {
1351 enum availability a = symtab_node_availability (node);
1352 if (a < *availability)
1353 *availability = a;
1354 }
1355 }
1356 else
1357 *availability = AVAIL_NOT_AVAILABLE;
1358 return node;
1359 }
1360 if (node && availability && weakref_p)
1361 {
1362 enum availability a = symtab_node_availability (node);
1363 if (a < *availability)
1364 *availability = a;
1365 weakref_p = node->weakref;
1366 }
1367 }
1368 if (availability)
1369 *availability = AVAIL_NOT_AVAILABLE;
1370 return NULL;
1371 }
1372
1373 /* C++ FE sometimes change linkage flags after producing same body aliases.
1374
1375 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1376 are obviously equivalent. The way it is doing so is however somewhat
1377 kludgy and interferes with the visibility code. As a result we need to
1378 copy the visibility from the target to get things right. */
1379
1380 void
1381 fixup_same_cpp_alias_visibility (symtab_node *node, symtab_node *target)
1382 {
1383 if (is_a <cgraph_node *> (node))
1384 {
1385 DECL_DECLARED_INLINE_P (node->decl)
1386 = DECL_DECLARED_INLINE_P (target->decl);
1387 DECL_DISREGARD_INLINE_LIMITS (node->decl)
1388 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1389 }
1390 /* FIXME: It is not really clear why those flags should not be copied for
1391 functions, too. */
1392 else
1393 {
1394 DECL_WEAK (node->decl) = DECL_WEAK (target->decl);
1395 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1396 DECL_VISIBILITY (node->decl) = DECL_VISIBILITY (target->decl);
1397 }
1398 DECL_VIRTUAL_P (node->decl) = DECL_VIRTUAL_P (target->decl);
1399 if (TREE_PUBLIC (node->decl))
1400 {
1401 tree group;
1402
1403 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1404 DECL_COMDAT (node->decl) = DECL_COMDAT (target->decl);
1405 group = target->get_comdat_group ();
1406 node->set_comdat_group (group);
1407 if (group
1408 && !node->same_comdat_group)
1409 symtab_add_to_same_comdat_group (node, target);
1410 }
1411 node->externally_visible = target->externally_visible;
1412 }
1413
1414 /* Hash sections by their names. */
1415
1416 static hashval_t
1417 hash_section_hash_entry (const void *p)
1418 {
1419 const section_hash_entry *n = (const section_hash_entry *) p;
1420 return htab_hash_string (n->name);
1421 }
1422
1423 /* Return true if section P1 name equals to P2. */
1424
1425 static int
1426 eq_sections (const void *p1, const void *p2)
1427 {
1428 const section_hash_entry *n1 = (const section_hash_entry *) p1;
1429 const char *name = (const char *)p2;
1430 return n1->name == name || !strcmp (n1->name, name);
1431 }
1432
1433 /* Set section, do not recurse into aliases.
1434 When one wants to change section of symbol and its aliases,
1435 use set_section */
1436
1437 void
1438 symtab_node::set_section_for_node (const char *section)
1439 {
1440 const char *current = get_section ();
1441 void **slot;
1442
1443 if (current == section
1444 || (current && section
1445 && !strcmp (current, section)))
1446 return;
1447
1448 if (current)
1449 {
1450 x_section->ref_count--;
1451 if (!x_section->ref_count)
1452 {
1453 slot = htab_find_slot_with_hash (section_hash, x_section->name,
1454 htab_hash_string (x_section->name),
1455 INSERT);
1456 ggc_free (x_section);
1457 htab_clear_slot (section_hash, slot);
1458 }
1459 x_section = NULL;
1460 }
1461 if (!section)
1462 {
1463 implicit_section = false;
1464 return;
1465 }
1466 if (!section_hash)
1467 section_hash = htab_create_ggc (10, hash_section_hash_entry,
1468 eq_sections, NULL);
1469 slot = htab_find_slot_with_hash (section_hash, section,
1470 htab_hash_string (section),
1471 INSERT);
1472 if (*slot)
1473 x_section = (section_hash_entry *)*slot;
1474 else
1475 {
1476 int len = strlen (section);
1477 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1478 x_section->name = ggc_vec_alloc<char> (len + 1);
1479 memcpy (x_section->name, section, len + 1);
1480 }
1481 x_section->ref_count++;
1482 }
1483
1484 /* Worker for set_section. */
1485
1486 static bool
1487 set_section_1 (struct symtab_node *n, void *s)
1488 {
1489 n->set_section_for_node ((char *)s);
1490 return false;
1491 }
1492
1493 /* Set section of symbol and its aliases. */
1494
1495 void
1496 symtab_node::set_section (const char *section)
1497 {
1498 gcc_assert (!this->alias);
1499 symtab_for_node_and_aliases (this, set_section_1, const_cast<char *>(section), true);
1500 }
1501
1502 /* Return the initialization priority. */
1503
1504 priority_type
1505 symtab_node::get_init_priority ()
1506 {
1507 struct symbol_priority_map *h;
1508 struct symbol_priority_map in;
1509
1510 if (!this->in_init_priority_hash)
1511 return DEFAULT_INIT_PRIORITY;
1512 in.symbol = this;
1513 h = (struct symbol_priority_map *) htab_find (init_priority_hash, &in);
1514 return h ? h->init : DEFAULT_INIT_PRIORITY;
1515 }
1516
1517 /* Return the finalization priority. */
1518
1519 priority_type
1520 cgraph_node::get_fini_priority ()
1521 {
1522 struct symbol_priority_map *h;
1523 struct symbol_priority_map in;
1524
1525 if (!this->in_init_priority_hash)
1526 return DEFAULT_INIT_PRIORITY;
1527 in.symbol = this;
1528 h = (struct symbol_priority_map *) htab_find (init_priority_hash, &in);
1529 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1530 }
1531
1532 /* Return true if the from tree in both priority maps are equal. */
1533
1534 int
1535 symbol_priority_map_eq (const void *va, const void *vb)
1536 {
1537 const struct symbol_priority_map *const a = (const struct symbol_priority_map *) va,
1538 *const b = (const struct symbol_priority_map *) vb;
1539 return (a->symbol == b->symbol);
1540 }
1541
1542 /* Hash a from symbol in a symbol_priority_map. */
1543
1544 unsigned int
1545 symbol_priority_map_hash (const void *item)
1546 {
1547 return htab_hash_pointer (((const struct symbol_priority_map *)item)->symbol);
1548 }
1549
1550 /* Return the initialization and finalization priority information for
1551 DECL. If there is no previous priority information, a freshly
1552 allocated structure is returned. */
1553
1554 static struct symbol_priority_map *
1555 symbol_priority_info (struct symtab_node *symbol)
1556 {
1557 struct symbol_priority_map in;
1558 struct symbol_priority_map *h;
1559 void **loc;
1560
1561 in.symbol = symbol;
1562 if (!init_priority_hash)
1563 init_priority_hash = htab_create_ggc (512, symbol_priority_map_hash,
1564 symbol_priority_map_eq, 0);
1565
1566 loc = htab_find_slot (init_priority_hash, &in, INSERT);
1567 h = (struct symbol_priority_map *) *loc;
1568 if (!h)
1569 {
1570 h = ggc_cleared_alloc<symbol_priority_map> ();
1571 *loc = h;
1572 h->symbol = symbol;
1573 h->init = DEFAULT_INIT_PRIORITY;
1574 h->fini = DEFAULT_INIT_PRIORITY;
1575 symbol->in_init_priority_hash = true;
1576 }
1577
1578 return h;
1579 }
1580
1581 /* Set initialization priority to PRIORITY. */
1582
1583 void
1584 symtab_node::set_init_priority (priority_type priority)
1585 {
1586 struct symbol_priority_map *h;
1587
1588 if (is_a <cgraph_node *> (this))
1589 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1590
1591 if (priority == DEFAULT_INIT_PRIORITY)
1592 {
1593 gcc_assert (get_init_priority() == priority);
1594 return;
1595 }
1596 h = symbol_priority_info (this);
1597 h->init = priority;
1598 }
1599
1600 /* Set fialization priority to PRIORITY. */
1601
1602 void
1603 cgraph_node::set_fini_priority (priority_type priority)
1604 {
1605 struct symbol_priority_map *h;
1606
1607 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1608
1609 if (priority == DEFAULT_INIT_PRIORITY)
1610 {
1611 gcc_assert (get_fini_priority() == priority);
1612 return;
1613 }
1614 h = symbol_priority_info (this);
1615 h->fini = priority;
1616 }
1617
1618 /* Worker for symtab_resolve_alias. */
1619
1620 static bool
1621 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
1622 {
1623 n->implicit_section = true;
1624 return false;
1625 }
1626
1627 /* Add reference recording that NODE is alias of TARGET.
1628 The function can fail in the case of aliasing cycles; in this case
1629 it returns false. */
1630
1631 bool
1632 symtab_resolve_alias (symtab_node *node, symtab_node *target)
1633 {
1634 symtab_node *n;
1635
1636 gcc_assert (!node->analyzed
1637 && !vec_safe_length (node->ref_list.references));
1638
1639 /* Never let cycles to creep into the symbol table alias references;
1640 those will make alias walkers to be infinite. */
1641 for (n = target; n && n->alias;
1642 n = n->analyzed ? symtab_alias_target (n) : NULL)
1643 if (n == node)
1644 {
1645 if (is_a <cgraph_node *> (node))
1646 error ("function %q+D part of alias cycle", node->decl);
1647 else if (is_a <varpool_node *> (node))
1648 error ("variable %q+D part of alias cycle", node->decl);
1649 else
1650 gcc_unreachable ();
1651 node->alias = false;
1652 return false;
1653 }
1654
1655 /* "analyze" the node - i.e. mark the reference. */
1656 node->definition = true;
1657 node->alias = true;
1658 node->analyzed = true;
1659 node->add_reference (target, IPA_REF_ALIAS, NULL);
1660
1661 /* Add alias into the comdat group of its target unless it is already there. */
1662 if (node->same_comdat_group)
1663 symtab_remove_from_same_comdat_group (node);
1664 node->set_comdat_group (NULL);
1665 if (target->get_comdat_group ())
1666 symtab_add_to_same_comdat_group (node, target);
1667
1668 if ((node->get_section () != target->get_section ()
1669 || target->get_comdat_group ())
1670 && node->get_section () && !node->implicit_section)
1671 {
1672 error ("section of alias %q+D must match section of its target",
1673 node->decl);
1674 }
1675 symtab_for_node_and_aliases (node, set_section_1,
1676 const_cast<char *>(target->get_section ()), true);
1677 if (target->implicit_section)
1678 symtab_for_node_and_aliases (node,
1679 set_implicit_section, NULL, true);
1680
1681 /* Alias targets become redundant after alias is resolved into an reference.
1682 We do not want to keep it around or we would have to mind updating them
1683 when renaming symbols. */
1684 node->alias_target = NULL;
1685
1686 if (node->cpp_implicit_alias && cgraph_state >= CGRAPH_STATE_CONSTRUCTION)
1687 fixup_same_cpp_alias_visibility (node, target);
1688
1689 /* If alias has address taken, so does the target. */
1690 if (node->address_taken)
1691 symtab_alias_ultimate_target (target, NULL)->address_taken = true;
1692 return true;
1693 }
1694
1695 /* Call calback on NODE and aliases associated to NODE.
1696 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1697 skipped. */
1698
1699 bool
1700 symtab_for_node_and_aliases (symtab_node *node,
1701 bool (*callback) (symtab_node *, void *),
1702 void *data,
1703 bool include_overwritable)
1704 {
1705 int i;
1706 struct ipa_ref *ref;
1707
1708 if (callback (node, data))
1709 return true;
1710 for (i = 0; node->iterate_referring (i, ref); i++)
1711 if (ref->use == IPA_REF_ALIAS)
1712 {
1713 symtab_node *alias = ref->referring;
1714 if (include_overwritable
1715 || symtab_node_availability (alias) > AVAIL_OVERWRITABLE)
1716 if (symtab_for_node_and_aliases (alias, callback, data,
1717 include_overwritable))
1718 return true;
1719 }
1720 return false;
1721 }
1722
1723 /* Worker searching nonoverwritable alias. */
1724
1725 static bool
1726 symtab_nonoverwritable_alias_1 (symtab_node *node, void *data)
1727 {
1728 if (decl_binds_to_current_def_p (node->decl))
1729 {
1730 *(symtab_node **)data = node;
1731 return true;
1732 }
1733 return false;
1734 }
1735
1736 /* If NODE can not be overwriten by static or dynamic linker to point to different
1737 definition, return NODE. Otherwise look for alias with such property and if
1738 none exists, introduce new one. */
1739
1740 symtab_node *
1741 symtab_nonoverwritable_alias (symtab_node *node)
1742 {
1743 tree new_decl;
1744 symtab_node *new_node = NULL;
1745
1746 /* First try to look up existing alias or base object
1747 (if that is already non-overwritable). */
1748 node = symtab_alias_ultimate_target (node, NULL);
1749 gcc_assert (!node->alias && !node->weakref);
1750 symtab_for_node_and_aliases (node, symtab_nonoverwritable_alias_1,
1751 (void *)&new_node, true);
1752 if (new_node)
1753 return new_node;
1754 #ifndef ASM_OUTPUT_DEF
1755 /* If aliases aren't supported by the assembler, fail. */
1756 return NULL;
1757 #endif
1758
1759 /* Otherwise create a new one. */
1760 new_decl = copy_node (node->decl);
1761 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1762 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1763 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1764 DECL_INITIAL (new_decl) = NULL;
1765 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1766 SET_DECL_RTL (new_decl, NULL);
1767
1768 /* Update the properties. */
1769 DECL_EXTERNAL (new_decl) = 0;
1770 TREE_PUBLIC (new_decl) = 0;
1771 DECL_COMDAT (new_decl) = 0;
1772 DECL_WEAK (new_decl) = 0;
1773
1774 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1775 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1776 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1777 {
1778 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1779 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1780 new_node = cgraph_create_function_alias
1781 (new_decl, node->decl);
1782 }
1783 else
1784 {
1785 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1786 DECL_INITIAL (new_decl) = error_mark_node;
1787 new_node = varpool_create_variable_alias (new_decl, node->decl);
1788 }
1789 symtab_resolve_alias (new_node, node);
1790 gcc_assert (decl_binds_to_current_def_p (new_decl)
1791 && targetm.binds_local_p (new_decl));
1792 return new_node;
1793 }
1794
1795 /* Return true if A and B represents semantically equivalent symbols. */
1796
1797 bool
1798 symtab_semantically_equivalent_p (symtab_node *a,
1799 symtab_node *b)
1800 {
1801 enum availability avail;
1802 symtab_node *ba;
1803 symtab_node *bb;
1804
1805 /* Equivalent functions are equivalent. */
1806 if (a->decl == b->decl)
1807 return true;
1808
1809 /* If symbol is not overwritable by different implementation,
1810 walk to the base object it defines. */
1811 ba = symtab_alias_ultimate_target (a, &avail);
1812 if (avail >= AVAIL_AVAILABLE)
1813 {
1814 if (ba == b)
1815 return true;
1816 }
1817 else
1818 ba = a;
1819 bb = symtab_alias_ultimate_target (b, &avail);
1820 if (avail >= AVAIL_AVAILABLE)
1821 {
1822 if (a == bb)
1823 return true;
1824 }
1825 else
1826 bb = b;
1827 return bb == ba;
1828 }
1829
1830 /* Classify symbol NODE for partitioning. */
1831
1832 enum symbol_partitioning_class
1833 symtab_get_symbol_partitioning_class (symtab_node *node)
1834 {
1835 /* Inline clones are always duplicated.
1836 This include external delcarations. */
1837 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1838
1839 if (DECL_ABSTRACT (node->decl))
1840 return SYMBOL_EXTERNAL;
1841
1842 if (cnode && cnode->global.inlined_to)
1843 return SYMBOL_DUPLICATE;
1844
1845 /* Weakref aliases are always duplicated. */
1846 if (node->weakref)
1847 return SYMBOL_DUPLICATE;
1848
1849 /* External declarations are external. */
1850 if (DECL_EXTERNAL (node->decl))
1851 return SYMBOL_EXTERNAL;
1852
1853 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
1854 {
1855 /* Constant pool references use local symbol names that can not
1856 be promoted global. We should never put into a constant pool
1857 objects that can not be duplicated across partitions. */
1858 if (DECL_IN_CONSTANT_POOL (node->decl))
1859 return SYMBOL_DUPLICATE;
1860 gcc_checking_assert (vnode->definition);
1861 }
1862 /* Functions that are cloned may stay in callgraph even if they are unused.
1863 Handle them as external; compute_ltrans_boundary take care to make
1864 proper things to happen (i.e. to make them appear in the boundary but
1865 with body streamed, so clone can me materialized). */
1866 else if (!cgraph (node)->definition)
1867 return SYMBOL_EXTERNAL;
1868
1869 /* Linker discardable symbols are duplicated to every use unless they are
1870 keyed. */
1871 if (DECL_ONE_ONLY (node->decl)
1872 && !node->force_output
1873 && !node->forced_by_abi
1874 && !symtab_used_from_object_file_p (node))
1875 return SYMBOL_DUPLICATE;
1876
1877 return SYMBOL_PARTITION;
1878 }
1879 #include "gt-symtab.h"