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