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