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