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