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