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