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