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