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