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