a9a0ab64cf9938179204127196d0c8fd27ba20b9
[gcc.git] / gcc / symtab.c
1 /* Symbol table.
2 Copyright (C) 2012-2015 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 "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "timevar.h"
30 #include "cgraph.h"
31 #include "lto-streamer.h"
32 #include "print-tree.h"
33 #include "varasm.h"
34 #include "langhooks.h"
35 #include "output.h"
36 #include "ipa-utils.h"
37 #include "calls.h"
38
39 static const char *ipa_ref_use_name[] = {"read","write","addr","alias","chkp"};
40
41 const char * const ld_plugin_symbol_resolution_names[]=
42 {
43 "",
44 "undef",
45 "prevailing_def",
46 "prevailing_def_ironly",
47 "preempted_reg",
48 "preempted_ir",
49 "resolved_ir",
50 "resolved_exec",
51 "resolved_dyn",
52 "prevailing_def_ironly_exp"
53 };
54
55 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at ALIAS
56 until we find an identifier that is not itself a transparent alias. */
57
58 static inline tree
59 ultimate_transparent_alias_target (tree alias)
60 {
61 tree target = alias;
62
63 while (IDENTIFIER_TRANSPARENT_ALIAS (target))
64 {
65 gcc_checking_assert (TREE_CHAIN (target));
66 target = TREE_CHAIN (target);
67 }
68 gcc_checking_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
69 && ! TREE_CHAIN (target));
70
71 return target;
72 }
73
74
75 /* Hash asmnames ignoring the user specified marks. */
76
77 hashval_t
78 symbol_table::decl_assembler_name_hash (const_tree asmname)
79 {
80 if (IDENTIFIER_POINTER (asmname)[0] == '*')
81 {
82 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
83 size_t ulp_len = strlen (user_label_prefix);
84
85 if (ulp_len == 0)
86 ;
87 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
88 decl_str += ulp_len;
89
90 return htab_hash_string (decl_str);
91 }
92
93 return htab_hash_string (IDENTIFIER_POINTER (asmname));
94 }
95
96 /* Return true if assembler names NAME1 and NAME2 leads to the same symbol
97 name. */
98
99 bool
100 symbol_table::assembler_names_equal_p (const char *name1, const char *name2)
101 {
102 if (name1 != name2)
103 {
104 if (name1[0] == '*')
105 {
106 size_t ulp_len = strlen (user_label_prefix);
107
108 name1 ++;
109
110 if (ulp_len == 0)
111 ;
112 else if (strncmp (name1, user_label_prefix, ulp_len) == 0)
113 name1 += ulp_len;
114 else
115 return false;
116 }
117 if (name2[0] == '*')
118 {
119 size_t ulp_len = strlen (user_label_prefix);
120
121 name2 ++;
122
123 if (ulp_len == 0)
124 ;
125 else if (strncmp (name2, user_label_prefix, ulp_len) == 0)
126 name2 += ulp_len;
127 else
128 return false;
129 }
130 return !strcmp (name1, name2);
131 }
132 return true;
133 }
134
135 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
136
137 bool
138 symbol_table::decl_assembler_name_equal (tree decl, const_tree asmname)
139 {
140 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
141 const char *decl_str;
142 const char *asmname_str;
143
144 if (decl_asmname == asmname)
145 return true;
146
147 decl_str = IDENTIFIER_POINTER (decl_asmname);
148 asmname_str = IDENTIFIER_POINTER (asmname);
149 return assembler_names_equal_p (decl_str, asmname_str);
150 }
151
152
153 /* Returns nonzero if P1 and P2 are equal. */
154
155 /* Insert NODE to assembler name hash. */
156
157 void
158 symbol_table::insert_to_assembler_name_hash (symtab_node *node,
159 bool with_clones)
160 {
161 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
162 return;
163 gcc_checking_assert (!node->previous_sharing_asm_name
164 && !node->next_sharing_asm_name);
165 if (assembler_name_hash)
166 {
167 symtab_node **aslot;
168 cgraph_node *cnode;
169 tree decl = node->decl;
170
171 tree name = DECL_ASSEMBLER_NAME (node->decl);
172
173 /* C++ FE can produce decls without associated assembler name and insert
174 them to symtab to hold section or TLS information. */
175 if (!name)
176 return;
177
178 hashval_t hash = decl_assembler_name_hash (name);
179 aslot = assembler_name_hash->find_slot_with_hash (name, hash, INSERT);
180 gcc_assert (*aslot != node);
181 node->next_sharing_asm_name = (symtab_node *)*aslot;
182 if (*aslot != NULL)
183 (*aslot)->previous_sharing_asm_name = node;
184 *aslot = node;
185
186 /* Update also possible inline clones sharing a decl. */
187 cnode = dyn_cast <cgraph_node *> (node);
188 if (cnode && cnode->clones && with_clones)
189 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
190 if (cnode->decl == decl)
191 insert_to_assembler_name_hash (cnode, true);
192 }
193
194 }
195
196 /* Remove NODE from assembler name hash. */
197
198 void
199 symbol_table::unlink_from_assembler_name_hash (symtab_node *node,
200 bool with_clones)
201 {
202 if (assembler_name_hash)
203 {
204 cgraph_node *cnode;
205 tree decl = node->decl;
206
207 if (node->next_sharing_asm_name)
208 node->next_sharing_asm_name->previous_sharing_asm_name
209 = node->previous_sharing_asm_name;
210 if (node->previous_sharing_asm_name)
211 {
212 node->previous_sharing_asm_name->next_sharing_asm_name
213 = node->next_sharing_asm_name;
214 }
215 else
216 {
217 tree name = DECL_ASSEMBLER_NAME (node->decl);
218 symtab_node **slot;
219
220 if (!name)
221 return;
222
223 hashval_t hash = decl_assembler_name_hash (name);
224 slot = assembler_name_hash->find_slot_with_hash (name, hash,
225 NO_INSERT);
226 gcc_assert (*slot == node);
227 if (!node->next_sharing_asm_name)
228 assembler_name_hash->clear_slot (slot);
229 else
230 *slot = node->next_sharing_asm_name;
231 }
232 node->next_sharing_asm_name = NULL;
233 node->previous_sharing_asm_name = NULL;
234
235 /* Update also possible inline clones sharing a decl. */
236 cnode = dyn_cast <cgraph_node *> (node);
237 if (cnode && cnode->clones && with_clones)
238 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
239 if (cnode->decl == decl)
240 unlink_from_assembler_name_hash (cnode, true);
241 }
242 }
243
244 /* Arrange node to be first in its entry of assembler_name_hash. */
245
246 void
247 symbol_table::symtab_prevail_in_asm_name_hash (symtab_node *node)
248 {
249 unlink_from_assembler_name_hash (node, false);
250 insert_to_assembler_name_hash (node, false);
251 }
252
253 /* Initalize asm name hash unless. */
254
255 void
256 symbol_table::symtab_initialize_asm_name_hash (void)
257 {
258 symtab_node *node;
259 if (!assembler_name_hash)
260 {
261 assembler_name_hash = hash_table<asmname_hasher>::create_ggc (10);
262 FOR_EACH_SYMBOL (node)
263 insert_to_assembler_name_hash (node, false);
264 }
265 }
266
267 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
268
269 void
270 symbol_table::change_decl_assembler_name (tree decl, tree name)
271 {
272 symtab_node *node = NULL;
273
274 /* We can have user ASM names on things, like global register variables, that
275 are not in the symbol table. */
276 if ((TREE_CODE (decl) == VAR_DECL
277 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
278 || TREE_CODE (decl) == FUNCTION_DECL)
279 node = symtab_node::get (decl);
280 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
281 {
282 SET_DECL_ASSEMBLER_NAME (decl, name);
283 if (node)
284 insert_to_assembler_name_hash (node, true);
285 }
286 else
287 {
288 if (name == DECL_ASSEMBLER_NAME (decl))
289 return;
290
291 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
292 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
293 : NULL);
294 if (node)
295 unlink_from_assembler_name_hash (node, true);
296
297 const char *old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
298 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
299 && DECL_RTL_SET_P (decl))
300 warning (0, "%D renamed after being referenced in assembly", decl);
301
302 SET_DECL_ASSEMBLER_NAME (decl, name);
303 if (alias)
304 {
305 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
306 TREE_CHAIN (name) = alias;
307 }
308 /* If we change assembler name, also all transparent aliases must
309 be updated. There are three kinds - those having same assembler name,
310 those being renamed in varasm.c and weakref being renamed by the
311 assembler. */
312 if (node)
313 {
314 insert_to_assembler_name_hash (node, true);
315 ipa_ref *ref;
316 for (unsigned i = 0; node->iterate_direct_aliases (i, ref); i++)
317 {
318 struct symtab_node *alias = ref->referring;
319 if (alias->transparent_alias && !alias->weakref
320 && symbol_table::assembler_names_equal_p
321 (old_name, IDENTIFIER_POINTER (
322 DECL_ASSEMBLER_NAME (alias->decl))))
323 change_decl_assembler_name (alias->decl, name);
324 else if (alias->transparent_alias
325 && IDENTIFIER_TRANSPARENT_ALIAS (alias->decl))
326 {
327 gcc_assert (TREE_CHAIN (DECL_ASSEMBLER_NAME (alias->decl))
328 && IDENTIFIER_TRANSPARENT_ALIAS
329 (DECL_ASSEMBLER_NAME (alias->decl)));
330
331 TREE_CHAIN (DECL_ASSEMBLER_NAME (alias->decl)) =
332 ultimate_transparent_alias_target
333 (DECL_ASSEMBLER_NAME (node->decl));
334 }
335 #ifdef ASM_OUTPUT_WEAKREF
336 else gcc_assert (!alias->transparent_alias || alias->weakref);
337 #else
338 else gcc_assert (!alias->transparent_alias);
339 #endif
340 }
341 gcc_assert (!node->transparent_alias || !node->definition
342 || node->weakref
343 || TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
344 || symbol_table::assembler_names_equal_p
345 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
346 IDENTIFIER_POINTER
347 (DECL_ASSEMBLER_NAME
348 (node->get_alias_target ()->decl))));
349 }
350 }
351 }
352
353 /* Hash sections by their names. */
354
355 hashval_t
356 section_name_hasher::hash (section_hash_entry *n)
357 {
358 return htab_hash_string (n->name);
359 }
360
361 /* Return true if section P1 name equals to P2. */
362
363 bool
364 section_name_hasher::equal (section_hash_entry *n1, const char *name)
365 {
366 return n1->name == name || !strcmp (n1->name, name);
367 }
368
369 /* Add node into symbol table. This function is not used directly, but via
370 cgraph/varpool node creation routines. */
371
372 void
373 symtab_node::register_symbol (void)
374 {
375 symtab->register_symbol (this);
376
377 if (!decl->decl_with_vis.symtab_node)
378 decl->decl_with_vis.symtab_node = this;
379
380 ref_list.clear ();
381
382 /* Be sure to do this last; C++ FE might create new nodes via
383 DECL_ASSEMBLER_NAME langhook! */
384 symtab->insert_to_assembler_name_hash (this, false);
385 }
386
387 /* Remove NODE from same comdat group. */
388
389 void
390 symtab_node::remove_from_same_comdat_group (void)
391 {
392 if (same_comdat_group)
393 {
394 symtab_node *prev;
395 for (prev = same_comdat_group;
396 prev->same_comdat_group != this;
397 prev = prev->same_comdat_group)
398 ;
399 if (same_comdat_group == prev)
400 prev->same_comdat_group = NULL;
401 else
402 prev->same_comdat_group = same_comdat_group;
403 same_comdat_group = NULL;
404 set_comdat_group (NULL);
405 }
406 }
407
408 /* Remove node from symbol table. This function is not used directly, but via
409 cgraph/varpool node removal routines. */
410
411 void
412 symtab_node::unregister (void)
413 {
414 remove_all_references ();
415 remove_all_referring ();
416
417 /* Remove reference to section. */
418 set_section_for_node (NULL);
419
420 remove_from_same_comdat_group ();
421
422 symtab->unregister (this);
423
424 /* During LTO symtab merging we temporarily corrupt decl to symtab node
425 hash. */
426 gcc_assert (decl->decl_with_vis.symtab_node || in_lto_p);
427 if (decl->decl_with_vis.symtab_node == this)
428 {
429 symtab_node *replacement_node = NULL;
430 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
431 replacement_node = cnode->find_replacement ();
432 decl->decl_with_vis.symtab_node = replacement_node;
433 }
434 if (!is_a <varpool_node *> (this) || !DECL_HARD_REGISTER (decl))
435 symtab->unlink_from_assembler_name_hash (this, false);
436 if (in_init_priority_hash)
437 symtab->init_priority_hash->remove (this);
438 }
439
440
441 /* Remove symbol from symbol table. */
442
443 void
444 symtab_node::remove (void)
445 {
446 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
447 cnode->remove ();
448 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
449 vnode->remove ();
450 }
451
452 /* Add NEW_ to the same comdat group that OLD is in. */
453
454 void
455 symtab_node::add_to_same_comdat_group (symtab_node *old_node)
456 {
457 gcc_assert (old_node->get_comdat_group ());
458 gcc_assert (!same_comdat_group);
459 gcc_assert (this != old_node);
460
461 set_comdat_group (old_node->get_comdat_group ());
462 same_comdat_group = old_node;
463 if (!old_node->same_comdat_group)
464 old_node->same_comdat_group = this;
465 else
466 {
467 symtab_node *n;
468 for (n = old_node->same_comdat_group;
469 n->same_comdat_group != old_node;
470 n = n->same_comdat_group)
471 ;
472 n->same_comdat_group = this;
473 }
474 }
475
476 /* Dissolve the same_comdat_group list in which NODE resides. */
477
478 void
479 symtab_node::dissolve_same_comdat_group_list (void)
480 {
481 symtab_node *n = this;
482 symtab_node *next;
483
484 if (!same_comdat_group)
485 return;
486 do
487 {
488 next = n->same_comdat_group;
489 n->same_comdat_group = NULL;
490 /* Clear comdat_group for comdat locals, since
491 make_decl_local doesn't. */
492 if (!TREE_PUBLIC (n->decl))
493 n->set_comdat_group (NULL);
494 n = next;
495 }
496 while (n != this);
497 }
498
499 /* Return printable assembler name of NODE.
500 This function is used only for debugging. When assembler name
501 is unknown go with identifier name. */
502
503 const char *
504 symtab_node::asm_name () const
505 {
506 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
507 return lang_hooks.decl_printable_name (decl, 2);
508 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
509 }
510
511 /* Return printable identifier name. */
512
513 const char *
514 symtab_node::name () const
515 {
516 return lang_hooks.decl_printable_name (decl, 2);
517 }
518
519 /* Return ipa reference from this symtab_node to
520 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
521 of the use. */
522
523 ipa_ref *
524 symtab_node::create_reference (symtab_node *referred_node,
525 enum ipa_ref_use use_type)
526 {
527 return create_reference (referred_node, use_type, NULL);
528 }
529
530
531 /* Return ipa reference from this symtab_node to
532 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
533 of the use and STMT the statement (if it exists). */
534
535 ipa_ref *
536 symtab_node::create_reference (symtab_node *referred_node,
537 enum ipa_ref_use use_type, gimple *stmt)
538 {
539 ipa_ref *ref = NULL, *ref2 = NULL;
540 ipa_ref_list *list, *list2;
541 ipa_ref_t *old_references;
542
543 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
544 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
545
546 list = &ref_list;
547 old_references = vec_safe_address (list->references);
548 vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
549 ref = &list->references->last ();
550
551 list2 = &referred_node->ref_list;
552
553 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
554 if(use_type == IPA_REF_ALIAS)
555 {
556 list2->referring.safe_insert (0, ref);
557 ref->referred_index = 0;
558
559 for (unsigned int i = 1; i < list2->referring.length (); i++)
560 list2->referring[i]->referred_index = i;
561 }
562 else
563 {
564 list2->referring.safe_push (ref);
565 ref->referred_index = list2->referring.length () - 1;
566 }
567
568 ref->referring = this;
569 ref->referred = referred_node;
570 ref->stmt = stmt;
571 ref->lto_stmt_uid = 0;
572 ref->use = use_type;
573 ref->speculative = 0;
574
575 /* If vector was moved in memory, update pointers. */
576 if (old_references != list->references->address ())
577 {
578 int i;
579 for (i = 0; iterate_reference(i, ref2); i++)
580 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
581 }
582 return ref;
583 }
584
585 /* If VAL is a reference to a function or a variable, add a reference from
586 this symtab_node to the corresponding symbol table node. USE_TYPE specify
587 type of the use and STMT the statement (if it exists). Return the new
588 reference or NULL if none was created. */
589
590 ipa_ref *
591 symtab_node::maybe_create_reference (tree val, enum ipa_ref_use use_type,
592 gimple *stmt)
593 {
594 STRIP_NOPS (val);
595 if (TREE_CODE (val) != ADDR_EXPR)
596 return NULL;
597 val = get_base_var (val);
598 if (val && (TREE_CODE (val) == FUNCTION_DECL
599 || TREE_CODE (val) == VAR_DECL))
600 {
601 symtab_node *referred = symtab_node::get (val);
602 gcc_checking_assert (referred);
603 return create_reference (referred, use_type, stmt);
604 }
605 return NULL;
606 }
607
608 /* Clone all references from symtab NODE to this symtab_node. */
609
610 void
611 symtab_node::clone_references (symtab_node *node)
612 {
613 ipa_ref *ref = NULL, *ref2 = NULL;
614 int i;
615 for (i = 0; node->iterate_reference (i, ref); i++)
616 {
617 bool speculative = ref->speculative;
618 unsigned int stmt_uid = ref->lto_stmt_uid;
619
620 ref2 = create_reference (ref->referred, ref->use, ref->stmt);
621 ref2->speculative = speculative;
622 ref2->lto_stmt_uid = stmt_uid;
623 }
624 }
625
626 /* Clone all referring from symtab NODE to this symtab_node. */
627
628 void
629 symtab_node::clone_referring (symtab_node *node)
630 {
631 ipa_ref *ref = NULL, *ref2 = NULL;
632 int i;
633 for (i = 0; node->iterate_referring(i, ref); i++)
634 {
635 bool speculative = ref->speculative;
636 unsigned int stmt_uid = ref->lto_stmt_uid;
637
638 ref2 = ref->referring->create_reference (this, ref->use, ref->stmt);
639 ref2->speculative = speculative;
640 ref2->lto_stmt_uid = stmt_uid;
641 }
642 }
643
644 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
645
646 ipa_ref *
647 symtab_node::clone_reference (ipa_ref *ref, gimple *stmt)
648 {
649 bool speculative = ref->speculative;
650 unsigned int stmt_uid = ref->lto_stmt_uid;
651 ipa_ref *ref2;
652
653 ref2 = create_reference (ref->referred, ref->use, stmt);
654 ref2->speculative = speculative;
655 ref2->lto_stmt_uid = stmt_uid;
656 return ref2;
657 }
658
659 /* Find the structure describing a reference to REFERRED_NODE
660 and associated with statement STMT. */
661
662 ipa_ref *
663 symtab_node::find_reference (symtab_node *referred_node,
664 gimple *stmt, unsigned int lto_stmt_uid)
665 {
666 ipa_ref *r = NULL;
667 int i;
668
669 for (i = 0; iterate_reference (i, r); i++)
670 if (r->referred == referred_node
671 && !r->speculative
672 && ((stmt && r->stmt == stmt)
673 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
674 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
675 return r;
676 return NULL;
677 }
678
679 /* Remove all references that are associated with statement STMT. */
680
681 void
682 symtab_node::remove_stmt_references (gimple *stmt)
683 {
684 ipa_ref *r = NULL;
685 int i = 0;
686
687 while (iterate_reference (i, r))
688 if (r->stmt == stmt)
689 r->remove_reference ();
690 else
691 i++;
692 }
693
694 /* Remove all stmt references in non-speculative references.
695 Those are not maintained during inlining & clonning.
696 The exception are speculative references that are updated along
697 with callgraph edges associated with them. */
698
699 void
700 symtab_node::clear_stmts_in_references (void)
701 {
702 ipa_ref *r = NULL;
703 int i;
704
705 for (i = 0; iterate_reference (i, r); i++)
706 if (!r->speculative)
707 {
708 r->stmt = NULL;
709 r->lto_stmt_uid = 0;
710 }
711 }
712
713 /* Remove all references in ref list. */
714
715 void
716 symtab_node::remove_all_references (void)
717 {
718 while (vec_safe_length (ref_list.references))
719 ref_list.references->last ().remove_reference ();
720 vec_free (ref_list.references);
721 }
722
723 /* Remove all referring items in ref list. */
724
725 void
726 symtab_node::remove_all_referring (void)
727 {
728 while (ref_list.referring.length ())
729 ref_list.referring.last ()->remove_reference ();
730 ref_list.referring.release ();
731 }
732
733 /* Dump references in ref list to FILE. */
734
735 void
736 symtab_node::dump_references (FILE *file)
737 {
738 ipa_ref *ref = NULL;
739 int i;
740 for (i = 0; iterate_reference (i, ref); i++)
741 {
742 fprintf (file, "%s/%i (%s)",
743 ref->referred->asm_name (),
744 ref->referred->order,
745 ipa_ref_use_name [ref->use]);
746 if (ref->speculative)
747 fprintf (file, " (speculative)");
748 }
749 fprintf (file, "\n");
750 }
751
752 /* Dump referring in list to FILE. */
753
754 void
755 symtab_node::dump_referring (FILE *file)
756 {
757 ipa_ref *ref = NULL;
758 int i;
759 for (i = 0; iterate_referring(i, ref); i++)
760 {
761 fprintf (file, "%s/%i (%s)",
762 ref->referring->asm_name (),
763 ref->referring->order,
764 ipa_ref_use_name [ref->use]);
765 if (ref->speculative)
766 fprintf (file, " (speculative)");
767 }
768 fprintf (file, "\n");
769 }
770
771 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
772
773 /* Dump base fields of symtab nodes to F. Not to be used directly. */
774
775 void
776 symtab_node::dump_base (FILE *f)
777 {
778 static const char * const visibility_types[] = {
779 "default", "protected", "hidden", "internal"
780 };
781
782 fprintf (f, "%s/%i (%s)", asm_name (), order, name ());
783 dump_addr (f, " @", (void *)this);
784 fprintf (f, "\n Type: %s", symtab_type_names[type]);
785
786 if (definition)
787 fprintf (f, " definition");
788 if (analyzed)
789 fprintf (f, " analyzed");
790 if (alias)
791 fprintf (f, " alias");
792 if (transparent_alias)
793 fprintf (f, " transparent_alias");
794 if (weakref)
795 fprintf (f, " weakref");
796 if (cpp_implicit_alias)
797 fprintf (f, " cpp_implicit_alias");
798 if (alias_target)
799 fprintf (f, " target:%s",
800 DECL_P (alias_target)
801 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
802 (alias_target))
803 : IDENTIFIER_POINTER (alias_target));
804 if (body_removed)
805 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
806 fprintf (f, "\n Visibility:");
807 if (in_other_partition)
808 fprintf (f, " in_other_partition");
809 if (used_from_other_partition)
810 fprintf (f, " used_from_other_partition");
811 if (force_output)
812 fprintf (f, " force_output");
813 if (forced_by_abi)
814 fprintf (f, " forced_by_abi");
815 if (externally_visible)
816 fprintf (f, " externally_visible");
817 if (no_reorder)
818 fprintf (f, " no_reorder");
819 if (resolution != LDPR_UNKNOWN)
820 fprintf (f, " %s",
821 ld_plugin_symbol_resolution_names[(int)resolution]);
822 if (TREE_ASM_WRITTEN (decl))
823 fprintf (f, " asm_written");
824 if (DECL_EXTERNAL (decl))
825 fprintf (f, " external");
826 if (TREE_PUBLIC (decl))
827 fprintf (f, " public");
828 if (DECL_COMMON (decl))
829 fprintf (f, " common");
830 if (DECL_WEAK (decl))
831 fprintf (f, " weak");
832 if (DECL_DLLIMPORT_P (decl))
833 fprintf (f, " dll_import");
834 if (DECL_COMDAT (decl))
835 fprintf (f, " comdat");
836 if (get_comdat_group ())
837 fprintf (f, " comdat_group:%s",
838 IDENTIFIER_POINTER (get_comdat_group_id ()));
839 if (DECL_ONE_ONLY (decl))
840 fprintf (f, " one_only");
841 if (get_section ())
842 fprintf (f, " section:%s",
843 get_section ());
844 if (implicit_section)
845 fprintf (f," (implicit_section)");
846 if (DECL_VISIBILITY_SPECIFIED (decl))
847 fprintf (f, " visibility_specified");
848 if (DECL_VISIBILITY (decl))
849 fprintf (f, " visibility:%s",
850 visibility_types [DECL_VISIBILITY (decl)]);
851 if (DECL_VIRTUAL_P (decl))
852 fprintf (f, " virtual");
853 if (DECL_ARTIFICIAL (decl))
854 fprintf (f, " artificial");
855 if (TREE_CODE (decl) == FUNCTION_DECL)
856 {
857 if (DECL_STATIC_CONSTRUCTOR (decl))
858 fprintf (f, " constructor");
859 if (DECL_STATIC_DESTRUCTOR (decl))
860 fprintf (f, " destructor");
861 }
862 fprintf (f, "\n");
863
864 if (same_comdat_group)
865 fprintf (f, " Same comdat group as: %s/%i\n",
866 same_comdat_group->asm_name (),
867 same_comdat_group->order);
868 if (next_sharing_asm_name)
869 fprintf (f, " next sharing asm name: %i\n",
870 next_sharing_asm_name->order);
871 if (previous_sharing_asm_name)
872 fprintf (f, " previous sharing asm name: %i\n",
873 previous_sharing_asm_name->order);
874
875 if (address_taken)
876 fprintf (f, " Address is taken.\n");
877 if (aux)
878 {
879 fprintf (f, " Aux:");
880 dump_addr (f, " @", (void *)aux);
881 }
882
883 fprintf (f, " References: ");
884 dump_references (f);
885 fprintf (f, " Referring: ");
886 dump_referring (f);
887 if (lto_file_data)
888 fprintf (f, " Read from file: %s\n",
889 lto_file_data->file_name);
890 }
891
892 /* Dump symtab node to F. */
893
894 void
895 symtab_node::dump (FILE *f)
896 {
897 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
898 cnode->dump (f);
899 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
900 vnode->dump (f);
901 }
902
903 /* Dump symbol table to F. */
904
905 void
906 symtab_node::dump_table (FILE *f)
907 {
908 symtab_node *node;
909 fprintf (f, "Symbol table:\n\n");
910 FOR_EACH_SYMBOL (node)
911 node->dump (f);
912 }
913
914
915 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
916 Return NULL if there's no such node. */
917
918 symtab_node *
919 symtab_node::get_for_asmname (const_tree asmname)
920 {
921 symtab_node *node;
922
923 symtab->symtab_initialize_asm_name_hash ();
924 hashval_t hash = symtab->decl_assembler_name_hash (asmname);
925 symtab_node **slot
926 = symtab->assembler_name_hash->find_slot_with_hash (asmname, hash,
927 NO_INSERT);
928
929 if (slot)
930 {
931 node = *slot;
932 return node;
933 }
934 return NULL;
935 }
936
937 /* Dump symtab node NODE to stderr. */
938
939 DEBUG_FUNCTION void
940 symtab_node::debug (void)
941 {
942 dump (stderr);
943 }
944
945 /* Verify common part of symtab nodes. */
946
947 DEBUG_FUNCTION bool
948 symtab_node::verify_base (void)
949 {
950 bool error_found = false;
951 symtab_node *hashed_node;
952
953 if (is_a <cgraph_node *> (this))
954 {
955 if (TREE_CODE (decl) != FUNCTION_DECL)
956 {
957 error ("function symbol is not function");
958 error_found = true;
959 }
960 }
961 else if (is_a <varpool_node *> (this))
962 {
963 if (TREE_CODE (decl) != VAR_DECL)
964 {
965 error ("variable symbol is not variable");
966 error_found = true;
967 }
968 }
969 else
970 {
971 error ("node has unknown type");
972 error_found = true;
973 }
974
975 if (symtab->state != LTO_STREAMING)
976 {
977 hashed_node = symtab_node::get (decl);
978 if (!hashed_node)
979 {
980 error ("node not found node->decl->decl_with_vis.symtab_node");
981 error_found = true;
982 }
983 if (hashed_node != this
984 && (!is_a <cgraph_node *> (this)
985 || !dyn_cast <cgraph_node *> (this)->clone_of
986 || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl))
987 {
988 error ("node differs from node->decl->decl_with_vis.symtab_node");
989 error_found = true;
990 }
991 }
992 if (symtab->assembler_name_hash)
993 {
994 hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
995 if (hashed_node && hashed_node->previous_sharing_asm_name)
996 {
997 error ("assembler name hash list corrupted");
998 error_found = true;
999 }
1000 while (hashed_node)
1001 {
1002 if (hashed_node == this)
1003 break;
1004 hashed_node = hashed_node->next_sharing_asm_name;
1005 }
1006 if (!hashed_node
1007 && !(is_a <varpool_node *> (this)
1008 && DECL_HARD_REGISTER (decl)))
1009 {
1010 error ("node not found in symtab assembler name hash");
1011 error_found = true;
1012 }
1013 }
1014 if (previous_sharing_asm_name
1015 && previous_sharing_asm_name->next_sharing_asm_name != this)
1016 {
1017 error ("double linked list of assembler names corrupted");
1018 error_found = true;
1019 }
1020 if (body_removed && definition)
1021 {
1022 error ("node has body_removed but is definition");
1023 error_found = true;
1024 }
1025 if (analyzed && !definition)
1026 {
1027 error ("node is analyzed byt it is not a definition");
1028 error_found = true;
1029 }
1030 if (cpp_implicit_alias && !alias)
1031 {
1032 error ("node is alias but not implicit alias");
1033 error_found = true;
1034 }
1035 if (alias && !definition && !weakref)
1036 {
1037 error ("node is alias but not definition");
1038 error_found = true;
1039 }
1040 if (weakref && !transparent_alias)
1041 {
1042 error ("node is weakref but not an transparent_alias");
1043 error_found = true;
1044 }
1045 if (transparent_alias && !alias)
1046 {
1047 error ("node is transparent_alias but not an alias");
1048 error_found = true;
1049 }
1050 if (same_comdat_group)
1051 {
1052 symtab_node *n = same_comdat_group;
1053
1054 if (!n->get_comdat_group ())
1055 {
1056 error ("node is in same_comdat_group list but has no comdat_group");
1057 error_found = true;
1058 }
1059 if (n->get_comdat_group () != get_comdat_group ())
1060 {
1061 error ("same_comdat_group list across different groups");
1062 error_found = true;
1063 }
1064 if (n->type != type)
1065 {
1066 error ("mixing different types of symbol in same comdat groups is not supported");
1067 error_found = true;
1068 }
1069 if (n == this)
1070 {
1071 error ("node is alone in a comdat group");
1072 error_found = true;
1073 }
1074 do
1075 {
1076 if (!n->same_comdat_group)
1077 {
1078 error ("same_comdat_group is not a circular list");
1079 error_found = true;
1080 break;
1081 }
1082 n = n->same_comdat_group;
1083 }
1084 while (n != this);
1085 if (comdat_local_p ())
1086 {
1087 ipa_ref *ref = NULL;
1088
1089 for (int i = 0; iterate_referring (i, ref); ++i)
1090 {
1091 if (!in_same_comdat_group_p (ref->referring))
1092 {
1093 error ("comdat-local symbol referred to by %s outside its "
1094 "comdat",
1095 identifier_to_locale (ref->referring->name()));
1096 error_found = true;
1097 }
1098 }
1099 }
1100 }
1101 if (implicit_section && !get_section ())
1102 {
1103 error ("implicit_section flag is set but section isn't");
1104 error_found = true;
1105 }
1106 if (get_section () && get_comdat_group ()
1107 && !implicit_section
1108 && !lookup_attribute ("section", DECL_ATTRIBUTES (decl)))
1109 {
1110 error ("Both section and comdat group is set");
1111 error_found = true;
1112 }
1113 /* TODO: Add string table for sections, so we do not keep holding duplicated
1114 strings. */
1115 if (alias && definition
1116 && get_section () != get_alias_target ()->get_section ()
1117 && (!get_section()
1118 || !get_alias_target ()->get_section ()
1119 || strcmp (get_section(),
1120 get_alias_target ()->get_section ())))
1121 {
1122 error ("Alias and target's section differs");
1123 get_alias_target ()->dump (stderr);
1124 error_found = true;
1125 }
1126 if (alias && definition
1127 && get_comdat_group () != get_alias_target ()->get_comdat_group ())
1128 {
1129 error ("Alias and target's comdat groups differs");
1130 get_alias_target ()->dump (stderr);
1131 error_found = true;
1132 }
1133 if (transparent_alias && definition && !weakref)
1134 {
1135 symtab_node *to = get_alias_target ();
1136 const char *name1
1137 = IDENTIFIER_POINTER (
1138 ultimate_transparent_alias_target (DECL_ASSEMBLER_NAME (decl)));
1139 const char *name2
1140 = IDENTIFIER_POINTER (
1141 ultimate_transparent_alias_target (DECL_ASSEMBLER_NAME (to->decl)));
1142 if (!symbol_table::assembler_names_equal_p (name1, name2))
1143 {
1144 error ("Transparent alias and target's assembler names differs");
1145 get_alias_target ()->dump (stderr);
1146 error_found = true;
1147 }
1148 }
1149 if (transparent_alias && definition
1150 && get_alias_target()->transparent_alias && get_alias_target()->analyzed)
1151 {
1152 error ("Chained transparent aliases");
1153 get_alias_target ()->dump (stderr);
1154 error_found = true;
1155 }
1156
1157 return error_found;
1158 }
1159
1160 /* Verify consistency of NODE. */
1161
1162 DEBUG_FUNCTION void
1163 symtab_node::verify (void)
1164 {
1165 if (seen_error ())
1166 return;
1167
1168 timevar_push (TV_CGRAPH_VERIFY);
1169 if (cgraph_node *node = dyn_cast <cgraph_node *> (this))
1170 node->verify_node ();
1171 else
1172 if (verify_base ())
1173 {
1174 debug ();
1175 internal_error ("symtab_node::verify failed");
1176 }
1177 timevar_pop (TV_CGRAPH_VERIFY);
1178 }
1179
1180 /* Verify symbol table for internal consistency. */
1181
1182 DEBUG_FUNCTION void
1183 symtab_node::verify_symtab_nodes (void)
1184 {
1185 symtab_node *node;
1186 hash_map<tree, symtab_node *> comdat_head_map (251);
1187
1188 FOR_EACH_SYMBOL (node)
1189 {
1190 node->verify ();
1191 if (node->get_comdat_group ())
1192 {
1193 symtab_node **entry, *s;
1194 bool existed;
1195
1196 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1197 &existed);
1198 if (!existed)
1199 *entry = node;
1200 else if (!DECL_EXTERNAL (node->decl))
1201 {
1202 for (s = (*entry)->same_comdat_group;
1203 s != NULL && s != node && s != *entry;
1204 s = s->same_comdat_group)
1205 ;
1206 if (!s || s == *entry)
1207 {
1208 error ("Two symbols with same comdat_group are not linked by "
1209 "the same_comdat_group list.");
1210 (*entry)->debug ();
1211 node->debug ();
1212 internal_error ("symtab_node::verify failed");
1213 }
1214 }
1215 }
1216 }
1217 }
1218
1219 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1220 but other code such as notice_global_symbol generates rtl. */
1221
1222 void
1223 symtab_node::make_decl_local (void)
1224 {
1225 rtx rtl, symbol;
1226
1227 if (weakref)
1228 {
1229 weakref = false;
1230 IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl)) = 0;
1231 TREE_CHAIN (DECL_ASSEMBLER_NAME (decl)) = NULL_TREE;
1232 symtab->change_decl_assembler_name
1233 (decl, DECL_ASSEMBLER_NAME (get_alias_target ()->decl));
1234 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
1235 DECL_ATTRIBUTES (decl));
1236 }
1237 /* Avoid clearing comdat_groups on comdat-local decls. */
1238 else if (TREE_PUBLIC (decl) == 0)
1239 return;
1240
1241 /* Localizing a symbol also make all its transparent aliases local. */
1242 ipa_ref *ref;
1243 for (unsigned i = 0; iterate_direct_aliases (i, ref); i++)
1244 {
1245 struct symtab_node *alias = ref->referring;
1246 if (alias->transparent_alias)
1247 alias->make_decl_local ();
1248 }
1249
1250 if (TREE_CODE (decl) == VAR_DECL)
1251 {
1252 DECL_COMMON (decl) = 0;
1253 /* ADDRESSABLE flag is not defined for public symbols. */
1254 TREE_ADDRESSABLE (decl) = 1;
1255 TREE_STATIC (decl) = 1;
1256 }
1257 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1258
1259 DECL_COMDAT (decl) = 0;
1260 DECL_WEAK (decl) = 0;
1261 DECL_EXTERNAL (decl) = 0;
1262 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1263 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1264 TREE_PUBLIC (decl) = 0;
1265 DECL_DLLIMPORT_P (decl) = 0;
1266 if (!DECL_RTL_SET_P (decl))
1267 return;
1268
1269 /* Update rtl flags. */
1270 make_decl_rtl (decl);
1271
1272 rtl = DECL_RTL (decl);
1273 if (!MEM_P (rtl))
1274 return;
1275
1276 symbol = XEXP (rtl, 0);
1277 if (GET_CODE (symbol) != SYMBOL_REF)
1278 return;
1279
1280 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1281 }
1282
1283 /* Walk the alias chain to return the symbol NODE is alias of.
1284 If NODE is not an alias, return NODE.
1285 Assumes NODE is known to be alias. */
1286
1287 symtab_node *
1288 symtab_node::ultimate_alias_target_1 (enum availability *availability)
1289 {
1290 bool transparent_p = false;
1291
1292 /* To determine visibility of the target, we follow ELF semantic of aliases.
1293 Here alias is an alternative assembler name of a given definition. Its
1294 availability prevails the availability of its target (i.e. static alias of
1295 weak definition is available.
1296
1297 Transaparent alias is just alternative anme of a given symbol used within
1298 one compilation unit and is translated prior hitting the object file. It
1299 inherits the visibility of its target.
1300 Weakref is a different animal (and noweak definition is weak).
1301
1302 If we ever get into supporting targets with different semantics, a target
1303 hook will be needed here. */
1304
1305 if (availability)
1306 {
1307 transparent_p = transparent_alias;
1308 if (!transparent_p)
1309 *availability = get_availability ();
1310 else
1311 *availability = AVAIL_NOT_AVAILABLE;
1312 }
1313
1314 symtab_node *node = this;
1315 while (node)
1316 {
1317 if (node->alias && node->analyzed)
1318 node = node->get_alias_target ();
1319 else
1320 {
1321 if (!availability || (!transparent_p && node->analyzed))
1322 ;
1323 else if (node->analyzed && !node->transparent_alias)
1324 *availability = node->get_availability ();
1325 else
1326 *availability = AVAIL_NOT_AVAILABLE;
1327 return node;
1328 }
1329 if (node && availability && transparent_p
1330 && node->transparent_alias)
1331 {
1332 *availability = node->get_availability ();
1333 transparent_p = false;
1334 }
1335 }
1336 if (availability)
1337 *availability = AVAIL_NOT_AVAILABLE;
1338 return NULL;
1339 }
1340
1341 /* C++ FE sometimes change linkage flags after producing same body aliases.
1342
1343 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1344 are obviously equivalent. The way it is doing so is however somewhat
1345 kludgy and interferes with the visibility code. As a result we need to
1346 copy the visibility from the target to get things right. */
1347
1348 void
1349 symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target)
1350 {
1351 if (is_a <cgraph_node *> (this))
1352 {
1353 DECL_DECLARED_INLINE_P (decl)
1354 = DECL_DECLARED_INLINE_P (target->decl);
1355 DECL_DISREGARD_INLINE_LIMITS (decl)
1356 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1357 }
1358 /* FIXME: It is not really clear why those flags should not be copied for
1359 functions, too. */
1360 else
1361 {
1362 DECL_WEAK (decl) = DECL_WEAK (target->decl);
1363 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1364 DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl);
1365 }
1366 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (target->decl);
1367 if (TREE_PUBLIC (decl))
1368 {
1369 tree group;
1370
1371 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1372 DECL_COMDAT (decl) = DECL_COMDAT (target->decl);
1373 group = target->get_comdat_group ();
1374 set_comdat_group (group);
1375 if (group && !same_comdat_group)
1376 add_to_same_comdat_group (target);
1377 }
1378 externally_visible = target->externally_visible;
1379 }
1380
1381 /* Set section, do not recurse into aliases.
1382 When one wants to change section of symbol and its aliases,
1383 use set_section. */
1384
1385 void
1386 symtab_node::set_section_for_node (const char *section)
1387 {
1388 const char *current = get_section ();
1389 section_hash_entry **slot;
1390
1391 if (current == section
1392 || (current && section
1393 && !strcmp (current, section)))
1394 return;
1395
1396 if (current)
1397 {
1398 x_section->ref_count--;
1399 if (!x_section->ref_count)
1400 {
1401 hashval_t hash = htab_hash_string (x_section->name);
1402 slot = symtab->section_hash->find_slot_with_hash (x_section->name,
1403 hash, INSERT);
1404 ggc_free (x_section);
1405 symtab->section_hash->clear_slot (slot);
1406 }
1407 x_section = NULL;
1408 }
1409 if (!section)
1410 {
1411 implicit_section = false;
1412 return;
1413 }
1414 if (!symtab->section_hash)
1415 symtab->section_hash = hash_table<section_name_hasher>::create_ggc (10);
1416 slot = symtab->section_hash->find_slot_with_hash (section,
1417 htab_hash_string (section),
1418 INSERT);
1419 if (*slot)
1420 x_section = (section_hash_entry *)*slot;
1421 else
1422 {
1423 int len = strlen (section);
1424 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1425 x_section->name = ggc_vec_alloc<char> (len + 1);
1426 memcpy (x_section->name, section, len + 1);
1427 }
1428 x_section->ref_count++;
1429 }
1430
1431 /* Worker for set_section. */
1432
1433 bool
1434 symtab_node::set_section (symtab_node *n, void *s)
1435 {
1436 n->set_section_for_node ((char *)s);
1437 return false;
1438 }
1439
1440 /* Set section of symbol and its aliases. */
1441
1442 void
1443 symtab_node::set_section (const char *section)
1444 {
1445 gcc_assert (!this->alias);
1446 call_for_symbol_and_aliases
1447 (symtab_node::set_section, const_cast<char *>(section), true);
1448 }
1449
1450 /* Return the initialization priority. */
1451
1452 priority_type
1453 symtab_node::get_init_priority ()
1454 {
1455 if (!this->in_init_priority_hash)
1456 return DEFAULT_INIT_PRIORITY;
1457
1458 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1459 return h ? h->init : DEFAULT_INIT_PRIORITY;
1460 }
1461
1462 /* Return the finalization priority. */
1463
1464 priority_type
1465 cgraph_node::get_fini_priority ()
1466 {
1467 if (!this->in_init_priority_hash)
1468 return DEFAULT_INIT_PRIORITY;
1469 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1470 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1471 }
1472
1473 /* Return the initialization and finalization priority information for
1474 DECL. If there is no previous priority information, a freshly
1475 allocated structure is returned. */
1476
1477 symbol_priority_map *
1478 symtab_node::priority_info (void)
1479 {
1480 if (!symtab->init_priority_hash)
1481 symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13);
1482
1483 bool existed;
1484 symbol_priority_map *h
1485 = &symtab->init_priority_hash->get_or_insert (this, &existed);
1486 if (!existed)
1487 {
1488 h->init = DEFAULT_INIT_PRIORITY;
1489 h->fini = DEFAULT_INIT_PRIORITY;
1490 in_init_priority_hash = true;
1491 }
1492
1493 return h;
1494 }
1495
1496 /* Set initialization priority to PRIORITY. */
1497
1498 void
1499 symtab_node::set_init_priority (priority_type priority)
1500 {
1501 symbol_priority_map *h;
1502
1503 if (is_a <cgraph_node *> (this))
1504 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1505
1506 if (priority == DEFAULT_INIT_PRIORITY)
1507 {
1508 gcc_assert (get_init_priority() == priority);
1509 return;
1510 }
1511 h = priority_info ();
1512 h->init = priority;
1513 }
1514
1515 /* Set fialization priority to PRIORITY. */
1516
1517 void
1518 cgraph_node::set_fini_priority (priority_type priority)
1519 {
1520 symbol_priority_map *h;
1521
1522 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1523
1524 if (priority == DEFAULT_INIT_PRIORITY)
1525 {
1526 gcc_assert (get_fini_priority() == priority);
1527 return;
1528 }
1529 h = priority_info ();
1530 h->fini = priority;
1531 }
1532
1533 /* Worker for symtab_resolve_alias. */
1534
1535 bool
1536 symtab_node::set_implicit_section (symtab_node *n,
1537 void *data ATTRIBUTE_UNUSED)
1538 {
1539 n->implicit_section = true;
1540 return false;
1541 }
1542
1543 /* Add reference recording that symtab node is alias of TARGET.
1544 The function can fail in the case of aliasing cycles; in this case
1545 it returns false. */
1546
1547 bool
1548 symtab_node::resolve_alias (symtab_node *target, bool transparent)
1549 {
1550 symtab_node *n;
1551
1552 gcc_assert (!analyzed && !vec_safe_length (ref_list.references));
1553
1554 /* Never let cycles to creep into the symbol table alias references;
1555 those will make alias walkers to be infinite. */
1556 for (n = target; n && n->alias;
1557 n = n->analyzed ? n->get_alias_target () : NULL)
1558 if (n == this)
1559 {
1560 if (is_a <cgraph_node *> (this))
1561 error ("function %q+D part of alias cycle", decl);
1562 else if (is_a <varpool_node *> (this))
1563 error ("variable %q+D part of alias cycle", decl);
1564 else
1565 gcc_unreachable ();
1566 alias = false;
1567 return false;
1568 }
1569
1570 /* "analyze" the node - i.e. mark the reference. */
1571 definition = true;
1572 alias = true;
1573 analyzed = true;
1574 transparent |= transparent_alias;
1575 transparent_alias = transparent;
1576 if (transparent)
1577 while (target->transparent_alias && target->analyzed)
1578 target = target->get_alias_target ();
1579 create_reference (target, IPA_REF_ALIAS, NULL);
1580
1581 /* Add alias into the comdat group of its target unless it is already there. */
1582 if (same_comdat_group)
1583 remove_from_same_comdat_group ();
1584 set_comdat_group (NULL);
1585 if (target->get_comdat_group ())
1586 add_to_same_comdat_group (target);
1587
1588 if ((get_section () != target->get_section ()
1589 || target->get_comdat_group ()) && get_section () && !implicit_section)
1590 {
1591 error ("section of alias %q+D must match section of its target", decl);
1592 }
1593 call_for_symbol_and_aliases (symtab_node::set_section,
1594 const_cast<char *>(target->get_section ()), true);
1595 if (target->implicit_section)
1596 call_for_symbol_and_aliases (set_implicit_section, NULL, true);
1597
1598 /* Alias targets become redundant after alias is resolved into an reference.
1599 We do not want to keep it around or we would have to mind updating them
1600 when renaming symbols. */
1601 alias_target = NULL;
1602
1603 if (!transparent && cpp_implicit_alias && symtab->state >= CONSTRUCTION)
1604 fixup_same_cpp_alias_visibility (target);
1605
1606 /* If alias has address taken, so does the target. */
1607 if (address_taken)
1608 target->ultimate_alias_target ()->address_taken = true;
1609
1610 /* All non-transparent aliases of THIS are now in fact aliases of TARGET.
1611 If alias is transparent, also all transparent aliases of THIS are now
1612 aliases of TARGET.
1613 Also merge same comdat group lists. */
1614 ipa_ref *ref;
1615 for (unsigned i = 0; iterate_direct_aliases (i, ref);)
1616 {
1617 struct symtab_node *alias_alias = ref->referring;
1618 if (alias_alias->get_comdat_group ())
1619 {
1620 alias_alias->remove_from_same_comdat_group ();
1621 alias_alias->set_comdat_group (NULL);
1622 if (target->get_comdat_group ())
1623 alias_alias->add_to_same_comdat_group (target);
1624 }
1625 if (!alias_alias->transparent_alias || transparent)
1626 {
1627 alias_alias->remove_all_references ();
1628 alias_alias->create_reference (target, IPA_REF_ALIAS, NULL);
1629 }
1630 else i++;
1631 }
1632 return true;
1633 }
1634
1635 /* Worker searching noninterposable alias. */
1636
1637 bool
1638 symtab_node::noninterposable_alias (symtab_node *node, void *data)
1639 {
1640 if (!node->transparent_alias && decl_binds_to_current_def_p (node->decl))
1641 {
1642 symtab_node *fn = node->ultimate_alias_target ();
1643
1644 /* Ensure that the alias is well formed this may not be the case
1645 of user defined aliases and currently it is not always the case
1646 of C++ same body aliases (that is a bug). */
1647 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
1648 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
1649 || (TREE_CODE (node->decl) == FUNCTION_DECL
1650 && flags_from_decl_or_type (node->decl)
1651 != flags_from_decl_or_type (fn->decl))
1652 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
1653 return false;
1654 *(symtab_node **)data = node;
1655 return true;
1656 }
1657 return false;
1658 }
1659
1660 /* If node can not be overwriten by static or dynamic linker to point to
1661 different definition, return NODE. Otherwise look for alias with such
1662 property and if none exists, introduce new one. */
1663
1664 symtab_node *
1665 symtab_node::noninterposable_alias (void)
1666 {
1667 tree new_decl;
1668 symtab_node *new_node = NULL;
1669
1670 /* First try to look up existing alias or base object
1671 (if that is already non-overwritable). */
1672 symtab_node *node = ultimate_alias_target ();
1673 gcc_assert (!node->alias && !node->weakref);
1674 node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias,
1675 (void *)&new_node, true);
1676 if (new_node)
1677 return new_node;
1678 #ifndef ASM_OUTPUT_DEF
1679 /* If aliases aren't supported by the assembler, fail. */
1680 return NULL;
1681 #endif
1682
1683 /* Otherwise create a new one. */
1684 new_decl = copy_node (node->decl);
1685 DECL_DLLIMPORT_P (new_decl) = 0;
1686 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1687 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1688 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1689 DECL_INITIAL (new_decl) = NULL;
1690 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1691 SET_DECL_RTL (new_decl, NULL);
1692
1693 /* Update the properties. */
1694 DECL_EXTERNAL (new_decl) = 0;
1695 TREE_PUBLIC (new_decl) = 0;
1696 DECL_COMDAT (new_decl) = 0;
1697 DECL_WEAK (new_decl) = 0;
1698
1699 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1700 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1701 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1702 {
1703 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1704 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1705 new_node = cgraph_node::create_alias (new_decl, node->decl);
1706 }
1707 else
1708 {
1709 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1710 DECL_INITIAL (new_decl) = error_mark_node;
1711 new_node = varpool_node::create_alias (new_decl, node->decl);
1712 }
1713 new_node->resolve_alias (node);
1714 gcc_assert (decl_binds_to_current_def_p (new_decl)
1715 && targetm.binds_local_p (new_decl));
1716 return new_node;
1717 }
1718
1719 /* Return true if symtab node and TARGET represents
1720 semantically equivalent symbols. */
1721
1722 bool
1723 symtab_node::semantically_equivalent_p (symtab_node *target)
1724 {
1725 enum availability avail;
1726 symtab_node *ba;
1727 symtab_node *bb;
1728
1729 /* Equivalent functions are equivalent. */
1730 if (decl == target->decl)
1731 return true;
1732
1733 /* If symbol is not overwritable by different implementation,
1734 walk to the base object it defines. */
1735 ba = ultimate_alias_target (&avail);
1736 if (avail >= AVAIL_AVAILABLE)
1737 {
1738 if (target == ba)
1739 return true;
1740 }
1741 else
1742 ba = this;
1743 bb = target->ultimate_alias_target (&avail);
1744 if (avail >= AVAIL_AVAILABLE)
1745 {
1746 if (this == bb)
1747 return true;
1748 }
1749 else
1750 bb = target;
1751 return bb == ba;
1752 }
1753
1754 /* Classify symbol symtab node for partitioning. */
1755
1756 enum symbol_partitioning_class
1757 symtab_node::get_partitioning_class (void)
1758 {
1759 /* Inline clones are always duplicated.
1760 This include external delcarations. */
1761 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
1762
1763 if (DECL_ABSTRACT_P (decl))
1764 return SYMBOL_EXTERNAL;
1765
1766 if (cnode && cnode->global.inlined_to)
1767 return SYMBOL_DUPLICATE;
1768
1769 /* Transparent aliases are always duplicated. */
1770 if (transparent_alias)
1771 return definition ? SYMBOL_DUPLICATE : SYMBOL_EXTERNAL;
1772
1773 /* External declarations are external. */
1774 if (DECL_EXTERNAL (decl))
1775 return SYMBOL_EXTERNAL;
1776
1777 if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
1778 {
1779 if (alias && definition && !ultimate_alias_target ()->definition)
1780 return SYMBOL_EXTERNAL;
1781 /* Constant pool references use local symbol names that can not
1782 be promoted global. We should never put into a constant pool
1783 objects that can not be duplicated across partitions. */
1784 if (DECL_IN_CONSTANT_POOL (decl))
1785 return SYMBOL_DUPLICATE;
1786 if (DECL_HARD_REGISTER (decl))
1787 return SYMBOL_DUPLICATE;
1788 gcc_checking_assert (vnode->definition);
1789 }
1790 /* Functions that are cloned may stay in callgraph even if they are unused.
1791 Handle them as external; compute_ltrans_boundary take care to make
1792 proper things to happen (i.e. to make them appear in the boundary but
1793 with body streamed, so clone can me materialized). */
1794 else if (!dyn_cast <cgraph_node *> (this)->function_symbol ()->definition)
1795 return SYMBOL_EXTERNAL;
1796
1797 /* Linker discardable symbols are duplicated to every use unless they are
1798 keyed. */
1799 if (DECL_ONE_ONLY (decl)
1800 && !force_output
1801 && !forced_by_abi
1802 && !used_from_object_file_p ())
1803 return SYMBOL_DUPLICATE;
1804
1805 return SYMBOL_PARTITION;
1806 }
1807
1808 /* Return true when symbol is known to be non-zero. */
1809
1810 bool
1811 symtab_node::nonzero_address ()
1812 {
1813 /* Weakrefs may be NULL when their target is not defined. */
1814 if (alias && weakref)
1815 {
1816 if (analyzed)
1817 {
1818 symtab_node *target = ultimate_alias_target ();
1819
1820 if (target->alias && target->weakref)
1821 return false;
1822 /* We can not recurse to target::nonzero. It is possible that the
1823 target is used only via the alias.
1824 We may walk references and look for strong use, but we do not know
1825 if this strong use will survive to final binary, so be
1826 conservative here.
1827 ??? Maybe we could do the lookup during late optimization that
1828 could be useful to eliminate the NULL pointer checks in LTO
1829 programs. */
1830 if (target->definition && !DECL_EXTERNAL (target->decl))
1831 return true;
1832 if (target->resolution != LDPR_UNKNOWN
1833 && target->resolution != LDPR_UNDEF
1834 && !target->can_be_discarded_p ()
1835 && flag_delete_null_pointer_checks)
1836 return true;
1837 return false;
1838 }
1839 else
1840 return false;
1841 }
1842
1843 /* With !flag_delete_null_pointer_checks we assume that symbols may
1844 bind to NULL. This is on by default on embedded targets only.
1845
1846 Otherwise all non-WEAK symbols must be defined and thus non-NULL or
1847 linking fails. Important case of WEAK we want to do well are comdats.
1848 Those are handled by later check for definition.
1849
1850 When parsing, beware the cases when WEAK attribute is added later. */
1851 if (!DECL_WEAK (decl)
1852 && flag_delete_null_pointer_checks)
1853 {
1854 refuse_visibility_changes = true;
1855 return true;
1856 }
1857
1858 /* If target is defined and not extern, we know it will be output and thus
1859 it will bind to non-NULL.
1860 Play safe for flag_delete_null_pointer_checks where weak definition maye
1861 be re-defined by NULL. */
1862 if (definition && !DECL_EXTERNAL (decl)
1863 && (flag_delete_null_pointer_checks || !DECL_WEAK (decl)))
1864 {
1865 if (!DECL_WEAK (decl))
1866 refuse_visibility_changes = true;
1867 return true;
1868 }
1869
1870 /* As the last resort, check the resolution info. */
1871 if (resolution != LDPR_UNKNOWN
1872 && resolution != LDPR_UNDEF
1873 && !can_be_discarded_p ()
1874 && flag_delete_null_pointer_checks)
1875 return true;
1876 return false;
1877 }
1878
1879 /* Return 0 if symbol is known to have different address than S2,
1880 Return 1 if symbol is known to have same address as S2,
1881 return 2 otherwise. */
1882 int
1883 symtab_node::equal_address_to (symtab_node *s2)
1884 {
1885 enum availability avail1, avail2;
1886
1887 /* A Shortcut: equivalent symbols are always equivalent. */
1888 if (this == s2)
1889 return 1;
1890
1891 /* For non-interposable aliases, lookup and compare their actual definitions.
1892 Also check if the symbol needs to bind to given definition. */
1893 symtab_node *rs1 = ultimate_alias_target (&avail1);
1894 symtab_node *rs2 = s2->ultimate_alias_target (&avail2);
1895 bool binds_local1 = rs1->analyzed && decl_binds_to_current_def_p (this->decl);
1896 bool binds_local2 = rs2->analyzed && decl_binds_to_current_def_p (s2->decl);
1897 bool really_binds_local1 = binds_local1;
1898 bool really_binds_local2 = binds_local2;
1899
1900 /* Addresses of vtables and virtual functions can not be used by user
1901 code and are used only within speculation. In this case we may make
1902 symbol equivalent to its alias even if interposition may break this
1903 rule. Doing so will allow us to turn speculative inlining into
1904 non-speculative more agressively. */
1905 if (DECL_VIRTUAL_P (this->decl) && avail1 >= AVAIL_AVAILABLE)
1906 binds_local1 = true;
1907 if (DECL_VIRTUAL_P (s2->decl) && avail2 >= AVAIL_AVAILABLE)
1908 binds_local2 = true;
1909
1910 /* If both definitions are available we know that even if they are bound
1911 to other unit they must be defined same way and therefore we can use
1912 equivalence test. */
1913 if (rs1 != rs2 && avail1 >= AVAIL_AVAILABLE && avail2 >= AVAIL_AVAILABLE)
1914 binds_local1 = binds_local2 = true;
1915
1916 if ((binds_local1 ? rs1 : this)
1917 == (binds_local2 ? rs2 : s2))
1918 {
1919 /* We made use of the fact that alias is not weak. */
1920 if (binds_local1 && rs1 != this)
1921 refuse_visibility_changes = true;
1922 if (binds_local2 && rs2 != s2)
1923 s2->refuse_visibility_changes = true;
1924 return 1;
1925 }
1926
1927 /* If both symbols may resolve to NULL, we can not really prove them different. */
1928 if (!nonzero_address () && !s2->nonzero_address ())
1929 return 2;
1930
1931 /* Except for NULL, functions and variables never overlap. */
1932 if (TREE_CODE (decl) != TREE_CODE (s2->decl))
1933 return 0;
1934
1935 /* If one of the symbols is unresolved alias, punt. */
1936 if (rs1->alias || rs2->alias)
1937 return 2;
1938
1939 /* If we have a non-interposale definition of at least one of the symbols
1940 and the other symbol is different, we know other unit can not interpose
1941 it to the first symbol; all aliases of the definition needs to be
1942 present in the current unit. */
1943 if (((really_binds_local1 || really_binds_local2)
1944 /* If we have both definitions and they are different, we know they
1945 will be different even in units they binds to. */
1946 || (binds_local1 && binds_local2))
1947 && rs1 != rs2)
1948 {
1949 /* We make use of the fact that one symbol is not alias of the other
1950 and that the definition is non-interposable. */
1951 refuse_visibility_changes = true;
1952 s2->refuse_visibility_changes = true;
1953 rs1->refuse_visibility_changes = true;
1954 rs2->refuse_visibility_changes = true;
1955 return 0;
1956 }
1957
1958 /* TODO: Alias oracle basically assume that addresses of global variables
1959 are different unless they are declared as alias of one to another.
1960 We probably should be consistent and use this fact here, too, and update
1961 alias oracle to use this predicate. */
1962
1963 return 2;
1964 }
1965
1966 /* Worker for call_for_symbol_and_aliases. */
1967
1968 bool
1969 symtab_node::call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *,
1970 void *),
1971 void *data,
1972 bool include_overwritable)
1973 {
1974 ipa_ref *ref;
1975 FOR_EACH_ALIAS (this, ref)
1976 {
1977 symtab_node *alias = ref->referring;
1978 if (include_overwritable
1979 || alias->get_availability () > AVAIL_INTERPOSABLE)
1980 if (alias->call_for_symbol_and_aliases (callback, data,
1981 include_overwritable))
1982 return true;
1983 }
1984 return false;
1985 }
1986
1987 /* Return true if address of N is possibly compared. */
1988
1989 static bool
1990 address_matters_1 (symtab_node *n, void *)
1991 {
1992 struct ipa_ref *ref;
1993
1994 if (!n->address_can_be_compared_p ())
1995 return false;
1996 if (n->externally_visible || n->force_output)
1997 return true;
1998
1999 for (unsigned int i = 0; n->iterate_referring (i, ref); i++)
2000 if (ref->address_matters_p ())
2001 return true;
2002 return false;
2003 }
2004
2005 /* Return true if symbol's address may possibly be compared to other
2006 symbol's address. */
2007
2008 bool
2009 symtab_node::address_matters_p ()
2010 {
2011 gcc_assert (!alias);
2012 return call_for_symbol_and_aliases (address_matters_1, NULL, true);
2013 }
2014
2015 /* Return true if symbol's alignment may be increased. */
2016
2017 bool
2018 symtab_node::can_increase_alignment_p (void)
2019 {
2020 symtab_node *target = ultimate_alias_target ();
2021
2022 /* For now support only variables. */
2023 if (TREE_CODE (decl) != VAR_DECL)
2024 return false;
2025
2026 /* With -fno-toplevel-reorder we may have already output the constant. */
2027 if (TREE_ASM_WRITTEN (target->decl))
2028 return false;
2029
2030 /* If target is already placed in an anchor, we can not touch its
2031 alignment. */
2032 if (DECL_RTL_SET_P (target->decl)
2033 && MEM_P (DECL_RTL (target->decl))
2034 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (DECL_RTL (target->decl), 0)))
2035 return false;
2036
2037 /* Constant pool entries may be shared. */
2038 if (DECL_IN_CONSTANT_POOL (target->decl))
2039 return false;
2040
2041 /* We cannot change alignment of symbols that may bind to symbols
2042 in other translation unit that may contain a definition with lower
2043 alignment. */
2044 if (!decl_binds_to_current_def_p (decl))
2045 return false;
2046
2047 /* When compiling partition, be sure the symbol is not output by other
2048 partition. */
2049 if (flag_ltrans
2050 && (target->in_other_partition
2051 || target->get_partitioning_class () == SYMBOL_DUPLICATE))
2052 return false;
2053
2054 /* Do not override the alignment as specified by the ABI when the used
2055 attribute is set. */
2056 if (DECL_PRESERVE_P (decl) || DECL_PRESERVE_P (target->decl))
2057 return false;
2058
2059 /* Do not override explicit alignment set by the user when an explicit
2060 section name is also used. This is a common idiom used by many
2061 software projects. */
2062 if (DECL_SECTION_NAME (target->decl) != NULL && !target->implicit_section)
2063 return false;
2064
2065 return true;
2066 }
2067
2068 /* Worker for symtab_node::increase_alignment. */
2069
2070 static bool
2071 increase_alignment_1 (symtab_node *n, void *v)
2072 {
2073 unsigned int align = (size_t)v;
2074 if (DECL_ALIGN (n->decl) < align
2075 && n->can_increase_alignment_p ())
2076 {
2077 DECL_ALIGN (n->decl) = align;
2078 DECL_USER_ALIGN (n->decl) = 1;
2079 }
2080 return false;
2081 }
2082
2083 /* Increase alignment of THIS to ALIGN. */
2084
2085 void
2086 symtab_node::increase_alignment (unsigned int align)
2087 {
2088 gcc_assert (can_increase_alignment_p () && align < MAX_OFILE_ALIGNMENT);
2089 ultimate_alias_target()->call_for_symbol_and_aliases (increase_alignment_1,
2090 (void *)(size_t) align,
2091 true);
2092 gcc_assert (DECL_ALIGN (decl) >= align);
2093 }
2094
2095 /* Helper for symtab_node::definition_alignment. */
2096
2097 static bool
2098 get_alignment_1 (symtab_node *n, void *v)
2099 {
2100 *((unsigned int *)v) = MAX (*((unsigned int *)v), DECL_ALIGN (n->decl));
2101 return false;
2102 }
2103
2104 /* Return desired alignment of the definition. This is NOT alignment useful
2105 to access THIS, because THIS may be interposable and DECL_ALIGN should
2106 be used instead. It however must be guaranteed when output definition
2107 of THIS. */
2108
2109 unsigned int
2110 symtab_node::definition_alignment ()
2111 {
2112 unsigned int align = 0;
2113 gcc_assert (!alias);
2114 call_for_symbol_and_aliases (get_alignment_1, &align, true);
2115 return align;
2116 }