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