1 /* Callgraph handling code.
2    Copyright (C) 2003-2021 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 "tree.h"
27 #include "gimple.h"
28 #include "timevar.h"
29 #include "cgraph.h"
30 #include "lto-streamer.h"
31 #include "varasm.h"
32 #include "debug.h"
33 #include "output.h"
34 #include "omp-offload.h"
35 #include "context.h"
36 #include "stringpool.h"
37 #include "attribs.h"
38 #include "tree-pass.h"
39 
40 const char * const tls_model_names[]={"none", "emulated",
41 				      "global-dynamic", "local-dynamic",
42 				      "initial-exec", "local-exec"};
43 
44 /* List of hooks triggered on varpool_node events.  */
45 struct varpool_node_hook_list {
46   varpool_node_hook hook;
47   void *data;
48   struct varpool_node_hook_list *next;
49 };
50 
51 /* Register HOOK to be called with DATA on each removed node.  */
52 varpool_node_hook_list *
add_varpool_removal_hook(varpool_node_hook hook,void * data)53 symbol_table::add_varpool_removal_hook (varpool_node_hook hook, void *data)
54 {
55   varpool_node_hook_list *entry;
56   varpool_node_hook_list **ptr = &m_first_varpool_removal_hook;
57 
58   entry = (varpool_node_hook_list *) xmalloc (sizeof (*entry));
59   entry->hook = hook;
60   entry->data = data;
61   entry->next = NULL;
62   while (*ptr)
63     ptr = &(*ptr)->next;
64   *ptr = entry;
65   return entry;
66 }
67 
68 /* Remove ENTRY from the list of hooks called on removing nodes.  */
69 void
remove_varpool_removal_hook(varpool_node_hook_list * entry)70 symbol_table::remove_varpool_removal_hook (varpool_node_hook_list *entry)
71 {
72   varpool_node_hook_list **ptr = &m_first_varpool_removal_hook;
73 
74   while (*ptr != entry)
75     ptr = &(*ptr)->next;
76   *ptr = entry->next;
77   free (entry);
78 }
79 
80 /* Call all node removal hooks.  */
81 void
call_varpool_removal_hooks(varpool_node * node)82 symbol_table::call_varpool_removal_hooks (varpool_node *node)
83 {
84   varpool_node_hook_list *entry = m_first_varpool_removal_hook;
85   while (entry)
86   {
87     entry->hook (node, entry->data);
88     entry = entry->next;
89   }
90 }
91 
92 /* Register HOOK to be called with DATA on each inserted node.  */
93 varpool_node_hook_list *
add_varpool_insertion_hook(varpool_node_hook hook,void * data)94 symbol_table::add_varpool_insertion_hook (varpool_node_hook hook, void *data)
95 {
96   varpool_node_hook_list *entry;
97   varpool_node_hook_list **ptr = &m_first_varpool_insertion_hook;
98 
99   entry = (varpool_node_hook_list *) xmalloc (sizeof (*entry));
100   entry->hook = hook;
101   entry->data = data;
102   entry->next = NULL;
103   while (*ptr)
104     ptr = &(*ptr)->next;
105   *ptr = entry;
106   return entry;
107 }
108 
109 /* Remove ENTRY from the list of hooks called on inserted nodes.  */
110 void
remove_varpool_insertion_hook(varpool_node_hook_list * entry)111 symbol_table::remove_varpool_insertion_hook (varpool_node_hook_list *entry)
112 {
113   varpool_node_hook_list **ptr = &m_first_varpool_insertion_hook;
114 
115   while (*ptr != entry)
116     ptr = &(*ptr)->next;
117   *ptr = entry->next;
118   free (entry);
119 }
120 
121 /* Call all node insertion hooks.  */
122 void
call_varpool_insertion_hooks(varpool_node * node)123 symbol_table::call_varpool_insertion_hooks (varpool_node *node)
124 {
125   varpool_node_hook_list *entry = m_first_varpool_insertion_hook;
126   while (entry)
127   {
128     entry->hook (node, entry->data);
129     entry = entry->next;
130   }
131 }
132 
133 /* Allocate new callgraph node and insert it into basic data structures.  */
134 
135 varpool_node *
create_empty(void)136 varpool_node::create_empty (void)
137 {
138   return new (ggc_alloc<varpool_node> ()) varpool_node ();
139 }
140 
141 /* Return varpool node assigned to DECL.  Create new one when needed.  */
142 varpool_node *
get_create(tree decl)143 varpool_node::get_create (tree decl)
144 {
145   varpool_node *node = varpool_node::get (decl);
146   gcc_checking_assert (VAR_P (decl));
147   if (node)
148     return node;
149 
150   node = varpool_node::create_empty ();
151   node->decl = decl;
152 
153   if ((flag_openacc || flag_openmp)
154       && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
155     {
156       node->offloadable = 1;
157       if (ENABLE_OFFLOADING && !DECL_EXTERNAL (decl))
158 	{
159 	  g->have_offload = true;
160 	  if (!in_lto_p)
161 	    vec_safe_push (offload_vars, decl);
162 	}
163     }
164 
165   node->register_symbol ();
166   return node;
167 }
168 
169 /* Remove variable from symbol table.  */
170 
171 void
remove(void)172 varpool_node::remove (void)
173 {
174   symtab->call_varpool_removal_hooks (this);
175   if (lto_file_data)
176     {
177       lto_free_function_in_decl_state_for_node (this);
178       lto_file_data = NULL;
179     }
180 
181   /* When streaming we can have multiple nodes associated with decl.  */
182   if (symtab->state == LTO_STREAMING)
183     ;
184   /* Keep constructor when it may be used for folding. We remove
185      references to external variables before final compilation.  */
186   else if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node
187 	   && !ctor_useable_for_folding_p ())
188     remove_initializer ();
189 
190   unregister (NULL);
191   ggc_free (this);
192 }
193 
194 /* Remove node initializer when it is no longer needed.  */
195 void
remove_initializer(void)196 varpool_node::remove_initializer (void)
197 {
198   if (DECL_INITIAL (decl)
199       && !DECL_IN_CONSTANT_POOL (decl)
200       /* Keep vtables for BINFO folding.  */
201       && !DECL_VIRTUAL_P (decl)
202       /* FIXME: http://gcc.gnu.org/PR55395 */
203       && debug_info_level == DINFO_LEVEL_NONE
204       /* When doing declaration merging we have duplicate
205 	 entries for given decl.  Do not attempt to remove
206 	 the bodies, or we will end up removing
207 	 wrong one.  */
208       && symtab->state != LTO_STREAMING)
209     DECL_INITIAL (decl) = error_mark_node;
210 }
211 
212 /* Dump given varpool node to F.  */
213 void
dump(FILE * f)214 varpool_node::dump (FILE *f)
215 {
216   dump_base (f);
217   fprintf (f, "  Availability: %s\n",
218 	   symtab->function_flags_ready
219 	   ? cgraph_availability_names[get_availability ()]
220 	   : "not-ready");
221   fprintf (f, "  Varpool flags:");
222   if (DECL_INITIAL (decl))
223     fprintf (f, " initialized");
224   if (output)
225     fprintf (f, " output");
226   if (used_by_single_function)
227     fprintf (f, " used-by-single-function");
228   if (TREE_READONLY (decl))
229     fprintf (f, " read-only");
230   if (ctor_useable_for_folding_p ())
231     fprintf (f, " const-value-known");
232   if (writeonly)
233     fprintf (f, " write-only");
234   if (tls_model)
235     fprintf (f, " tls-%s", tls_model_names [tls_model]);
236   fprintf (f, "\n");
237 }
238 
239 
240 /* Dump given varpool node to stderr.  */
debug(void)241 void varpool_node::debug (void)
242 {
243   varpool_node::dump (stderr);
244 }
245 
246 /* Dump the variable pool to F.  */
247 void
dump_varpool(FILE * f)248 varpool_node::dump_varpool (FILE *f)
249 {
250   varpool_node *node;
251 
252   fprintf (f, "variable pool:\n\n");
253   FOR_EACH_VARIABLE (node)
254     node->dump (f);
255 }
256 
257 /* Dump the variable pool to stderr.  */
258 
259 DEBUG_FUNCTION void
debug_varpool(void)260 varpool_node::debug_varpool (void)
261 {
262   dump_varpool (stderr);
263 }
264 
265 /* Given an assembler name, lookup node.  */
266 varpool_node *
get_for_asmname(tree asmname)267 varpool_node::get_for_asmname (tree asmname)
268 {
269   if (symtab_node *node = symtab_node::get_for_asmname (asmname))
270     return dyn_cast <varpool_node *> (node);
271   else
272     return NULL;
273 }
274 
275 /* When doing LTO, read variable's constructor from disk if
276    it is not already present.  */
277 
278 tree
get_constructor(void)279 varpool_node::get_constructor (void)
280 {
281   lto_file_decl_data *file_data;
282   const char *data, *name;
283   size_t len;
284 
285   if (DECL_INITIAL (decl) != error_mark_node
286       || !in_lto_p
287       || !lto_file_data)
288     return DECL_INITIAL (decl);
289 
290   timevar_push (TV_IPA_LTO_CTORS_IN);
291 
292   file_data = lto_file_data;
293   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
294 
295   /* We may have renamed the declaration, e.g., a static function.  */
296   name = lto_get_decl_name_mapping (file_data, name);
297   struct lto_in_decl_state *decl_state
298 	 = lto_get_function_in_decl_state (file_data, decl);
299 
300   data = lto_get_section_data (file_data, LTO_section_function_body,
301 			       name, order - file_data->order_base,
302 			       &len, decl_state->compressed);
303   if (!data)
304     fatal_error (input_location, "%s: section %s.%d is missing",
305 		 file_data->file_name,
306 		 name, order - file_data->order_base);
307 
308   if (!quiet_flag)
309     fprintf (stderr, " in:%s", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
310   lto_input_variable_constructor (file_data, this, data);
311   gcc_assert (DECL_INITIAL (decl) != error_mark_node);
312   lto_stats.num_function_bodies++;
313   lto_free_section_data (file_data, LTO_section_function_body, name,
314 			 data, len, decl_state->compressed);
315   lto_free_function_in_decl_state_for_node (this);
316   timevar_pop (TV_IPA_LTO_CTORS_IN);
317   return DECL_INITIAL (decl);
318 }
319 
320 /* Return true if variable has constructor that can be used for folding.  */
321 
322 bool
ctor_useable_for_folding_p(void)323 varpool_node::ctor_useable_for_folding_p (void)
324 {
325   varpool_node *real_node = this;
326 
327   if (real_node->alias && real_node->definition)
328     real_node = ultimate_alias_target ();
329 
330   if (TREE_CODE (decl) == CONST_DECL
331       || DECL_IN_CONSTANT_POOL (decl))
332     return true;
333   if (TREE_THIS_VOLATILE (decl))
334     return false;
335 
336   /* Avoid attempts to load constructors that was not streamed.  */
337   if (in_lto_p && DECL_INITIAL (real_node->decl) == error_mark_node
338       && real_node->body_removed)
339     return false;
340 
341   /* If we do not have a constructor, we can't use it.  */
342   if (DECL_INITIAL (real_node->decl) == error_mark_node
343       && !real_node->lto_file_data)
344     return false;
345 
346   /* Vtables are defined by their types and must match no matter of interposition
347      rules.  */
348   if (DECL_VIRTUAL_P (decl))
349     {
350       /* The C++ front end creates VAR_DECLs for vtables of typeinfo
351 	 classes not defined in the current TU so that it can refer
352 	 to them from typeinfo objects.  Avoid returning NULL_TREE.  */
353       return DECL_INITIAL (real_node->decl) != NULL;
354     }
355 
356   /* An alias of a read-only variable is also read-only, since the variable
357      is stored in read-only memory.  We also accept read-only aliases of
358      non-read-only locations assuming that the user knows what he is asking
359      for.  */
360   if (!TREE_READONLY (decl) && !TREE_READONLY (real_node->decl))
361     return false;
362 
363   /* Variables declared 'const' without an initializer
364      have zero as the initializer if they may not be
365      overridden at link or run time.
366 
367      It is actually requirement for C++ compiler to optimize const variables
368      consistently. As a GNU extension, do not enforce this rule for user defined
369      weak variables, so we support interposition on:
370      static const int dummy = 0;
371      extern const int foo __attribute__((__weak__, __alias__("dummy")));
372    */
373   if ((!DECL_INITIAL (real_node->decl)
374        || (DECL_WEAK (decl) && !DECL_COMDAT (decl)))
375       && (DECL_EXTERNAL (decl) || decl_replaceable_p (decl)))
376     return false;
377 
378   /* Variables declared `const' with an initializer are considered
379      to not be overwritable with different initializer by default.
380 
381      ??? Previously we behaved so for scalar variables but not for array
382      accesses.  */
383   return true;
384 }
385 
386 /* If DECLARATION is constant variable and its initial value is known
387    (so we can do constant folding), return its constructor (DECL_INITIAL).
388    This may be an expression or NULL when DECL is initialized to 0.
389    Return ERROR_MARK_NODE otherwise.
390 
391    In LTO this may actually trigger reading the constructor from disk.
392    For this reason varpool_ctor_useable_for_folding_p should be used when
393    the actual constructor value is not needed.  */
394 
395 tree
ctor_for_folding(tree decl)396 ctor_for_folding (tree decl)
397 {
398   varpool_node *node, *real_node;
399   tree real_decl;
400 
401   if (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
402     return error_mark_node;
403 
404   if (TREE_CODE (decl) == CONST_DECL
405       || DECL_IN_CONSTANT_POOL (decl))
406     return DECL_INITIAL (decl);
407 
408   if (TREE_THIS_VOLATILE (decl))
409     return error_mark_node;
410 
411   /* Do not care about automatic variables.  Those are never initialized
412      anyway, because gimplifier expands the code.  */
413   if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
414     {
415       gcc_assert (!TREE_PUBLIC (decl));
416       /* Unless this is called during FE folding.  */
417       if (cfun
418 	  && (cfun->curr_properties & (PROP_gimple | PROP_rtl)) == 0
419 	  && TREE_READONLY (decl)
420 	  && !TREE_SIDE_EFFECTS (decl)
421 	  && DECL_INITIAL (decl))
422 	return DECL_INITIAL (decl);
423       return error_mark_node;
424     }
425 
426   gcc_assert (VAR_P (decl));
427 
428   real_node = node = varpool_node::get (decl);
429   if (node)
430     {
431       real_node = node->ultimate_alias_target ();
432       real_decl = real_node->decl;
433     }
434   else
435     real_decl = decl;
436 
437   /* See if we are dealing with alias.
438      In most cases alias is just alternative symbol pointing to a given
439      constructor.  This allows us to use interposition rules of DECL
440      constructor of REAL_NODE.  However weakrefs are special by being just
441      alternative name of their target (if defined).  */
442   if (decl != real_decl)
443     {
444       gcc_assert (!DECL_INITIAL (decl)
445 		  || (node->alias && node->get_alias_target () == real_node)
446 		  || DECL_INITIAL (decl) == error_mark_node);
447       while (node->transparent_alias && node->analyzed)
448 	{
449 	  node = node->get_alias_target ();
450 	  decl = node->decl;
451 	}
452     }
453 
454   if ((!DECL_VIRTUAL_P (real_decl)
455        || DECL_INITIAL (real_decl) == error_mark_node
456        || !DECL_INITIAL (real_decl))
457       && (!node || !node->ctor_useable_for_folding_p ()))
458     return error_mark_node;
459 
460   /* OK, we can return constructor.  See if we need to fetch it from disk
461      in LTO mode.  */
462   if (DECL_INITIAL (real_decl) != error_mark_node
463       || !in_lto_p)
464     return DECL_INITIAL (real_decl);
465   return real_node->get_constructor ();
466 }
467 
468 /* Add the variable DECL to the varpool.
469    Unlike finalize_decl function is intended to be used
470    by middle end and allows insertion of new variable at arbitrary point
471    of compilation.  */
472 void
add(tree decl)473 varpool_node::add (tree decl)
474 {
475   varpool_node *node;
476   varpool_node::finalize_decl (decl);
477   node = varpool_node::get_create (decl);
478   symtab->call_varpool_insertion_hooks (node);
479   if (node->externally_visible_p ())
480     node->externally_visible = true;
481   if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
482     node->no_reorder = 1;
483 }
484 
485 /* Return variable availability.  See cgraph.h for description of individual
486    return values.  */
487 enum availability
get_availability(symtab_node * ref)488 varpool_node::get_availability (symtab_node *ref)
489 {
490   if (!definition && !in_other_partition)
491     return AVAIL_NOT_AVAILABLE;
492   if (!TREE_PUBLIC (decl))
493     return AVAIL_AVAILABLE;
494   if (DECL_IN_CONSTANT_POOL (decl)
495       || DECL_VIRTUAL_P (decl))
496     return AVAIL_AVAILABLE;
497   if (transparent_alias && definition)
498     {
499       enum availability avail;
500 
501       ultimate_alias_target (&avail, ref);
502       return avail;
503     }
504   /* If this is a reference from symbol itself and there are no aliases, we
505      may be sure that the symbol was not interposed by something else because
506      the symbol itself would be unreachable otherwise.  */
507   if ((this == ref && !has_aliases_p ())
508       || (ref && get_comdat_group ()
509           && get_comdat_group () == ref->get_comdat_group ()))
510     return AVAIL_AVAILABLE;
511   /* If the variable can be overwritten, return OVERWRITABLE.  Takes
512      care of at least one notable extension - the COMDAT variables
513      used to share template instantiations in C++.  */
514   if (decl_replaceable_p (decl)
515       || DECL_EXTERNAL (decl))
516     return AVAIL_INTERPOSABLE;
517   return AVAIL_AVAILABLE;
518 }
519 
520 void
analyze(void)521 varpool_node::analyze (void)
522 {
523   /* When reading back varpool at LTO time, we re-construct the queue in order
524      to have "needed" list right by inserting all needed nodes into varpool.
525      We however don't want to re-analyze already analyzed nodes.  */
526   if (!analyzed)
527     {
528       gcc_assert (!in_lto_p || symtab->function_flags_ready);
529       /* Compute the alignment early so function body expanders are
530 	 already informed about increased alignment.  */
531       align_variable (decl, 0);
532     }
533   if (alias)
534     resolve_alias (varpool_node::get (alias_target));
535   else if (DECL_INITIAL (decl))
536     record_references_in_initializer (decl, analyzed);
537   analyzed = true;
538 }
539 
540 /* Assemble thunks and aliases associated to varpool node.  */
541 
542 void
assemble_aliases(void)543 varpool_node::assemble_aliases (void)
544 {
545   ipa_ref *ref;
546 
547   FOR_EACH_ALIAS (this, ref)
548     {
549       varpool_node *alias = dyn_cast <varpool_node *> (ref->referring);
550       if (alias->symver)
551 	do_assemble_symver (alias->decl,
552 			    DECL_ASSEMBLER_NAME (decl));
553       else if (!alias->transparent_alias)
554 	do_assemble_alias (alias->decl,
555 			   DECL_ASSEMBLER_NAME (decl));
556       alias->assemble_aliases ();
557     }
558 }
559 
560 /* Output one variable, if necessary.  Return whether we output it.  */
561 
562 bool
assemble_decl(void)563 varpool_node::assemble_decl (void)
564 {
565   /* Aliases are output when their target is produced or by
566      output_weakrefs.  */
567   if (alias)
568     return false;
569 
570   /* Constant pool is output from RTL land when the reference
571      survive till this level.  */
572   if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl))
573     return false;
574 
575   /* Decls with VALUE_EXPR should not be in the varpool at all.  They
576      are not real variables, but just info for debugging and codegen.
577      Unfortunately at the moment emutls is not updating varpool correctly
578      after turning real vars into value_expr vars.  */
579   if (DECL_HAS_VALUE_EXPR_P (decl)
580       && !targetm.have_tls)
581     return false;
582 
583   /* Hard register vars do not need to be output.  */
584   if (DECL_HARD_REGISTER (decl))
585     return false;
586 
587   gcc_checking_assert (!TREE_ASM_WRITTEN (decl)
588 		       && VAR_P (decl)
589 		       && !DECL_HAS_VALUE_EXPR_P (decl));
590 
591   if (!in_other_partition
592       && !DECL_EXTERNAL (decl))
593     {
594       get_constructor ();
595       assemble_variable (decl, 0, 1, 0);
596       gcc_assert (TREE_ASM_WRITTEN (decl));
597       gcc_assert (definition);
598       assemble_aliases ();
599       /* After the parser has generated debugging information, augment
600 	 this information with any new location/etc information that may
601 	 have become available after the compilation proper.  */
602       debug_hooks->late_global_decl (decl);
603       return true;
604     }
605 
606   return false;
607 }
608 
609 /* Add NODE to queue starting at FIRST.
610    The queue is linked via AUX pointers and terminated by pointer to 1.  */
611 
612 static void
enqueue_node(varpool_node * node,varpool_node ** first)613 enqueue_node (varpool_node *node, varpool_node **first)
614 {
615   if (node->aux)
616     return;
617   gcc_checking_assert (*first);
618   node->aux = *first;
619   *first = node;
620 }
621 
622 /* Optimization of function bodies might've rendered some variables as
623    unnecessary so we want to avoid these from being compiled.  Re-do
624    reachability starting from variables that are either externally visible
625    or was referred from the asm output routines.  */
626 
627 void
remove_unreferenced_decls(void)628 symbol_table::remove_unreferenced_decls (void)
629 {
630   varpool_node *next, *node;
631   varpool_node *first = (varpool_node *)(void *)1;
632   int i;
633   ipa_ref *ref = NULL;
634   hash_set<varpool_node *> referenced;
635 
636   if (seen_error ())
637     return;
638 
639   if (dump_file)
640     fprintf (dump_file, "Trivially needed variables:");
641   FOR_EACH_DEFINED_VARIABLE (node)
642     {
643       if (node->analyzed
644 	  && (!node->can_remove_if_no_refs_p ()
645 	      /* We just expanded all function bodies.  See if any of
646 		 them needed the variable.  */
647 	      || DECL_RTL_SET_P (node->decl)))
648 	{
649 	  enqueue_node (node, &first);
650 	  if (dump_file)
651 	    fprintf (dump_file, " %s", node->dump_asm_name ());
652 	}
653     }
654   while (first != (varpool_node *)(void *)1)
655     {
656       node = first;
657       first = (varpool_node *)first->aux;
658 
659       if (node->same_comdat_group)
660 	{
661 	  symtab_node *next;
662 	  for (next = node->same_comdat_group;
663 	       next != node;
664 	       next = next->same_comdat_group)
665 	    {
666 	      varpool_node *vnext = dyn_cast <varpool_node *> (next);
667 	      if (vnext && vnext->analyzed && !next->comdat_local_p ())
668 		enqueue_node (vnext, &first);
669 	    }
670 	}
671       for (i = 0; node->iterate_reference (i, ref); i++)
672 	{
673 	  varpool_node *vnode = dyn_cast <varpool_node *> (ref->referred);
674 	  if (vnode
675 	      && !vnode->in_other_partition
676 	      && (!DECL_EXTERNAL (ref->referred->decl)
677 		  || vnode->alias)
678 	      && vnode->analyzed)
679 	    enqueue_node (vnode, &first);
680 	  else
681 	    {
682 	      referenced.add (vnode);
683 	      while (vnode && vnode->alias && vnode->definition)
684 		{
685 		  vnode = vnode->get_alias_target ();
686 	          referenced.add (vnode);
687 		}
688 	    }
689 	}
690     }
691   if (dump_file)
692     fprintf (dump_file, "\nRemoving variables:");
693   for (node = first_defined_variable (); node; node = next)
694     {
695       next = next_defined_variable (node);
696       if (!node->aux && !node->no_reorder)
697 	{
698 	  if (dump_file)
699 	    fprintf (dump_file, " %s", node->dump_asm_name ());
700 	  if (referenced.contains(node))
701 	    node->remove_initializer ();
702 	  else
703 	    node->remove ();
704 	}
705     }
706 
707   if (dump_file)
708     fprintf (dump_file, "\n");
709 }
710 
711 /* For variables in named sections make sure get_variable_section
712    is called before we switch to those sections.  Then section
713    conflicts between read-only and read-only requiring relocations
714    sections can be resolved.  */
715 void
finalize_named_section_flags(void)716 varpool_node::finalize_named_section_flags (void)
717 {
718   if (!TREE_ASM_WRITTEN (decl)
719       && !alias
720       && !in_other_partition
721       && !DECL_EXTERNAL (decl)
722       && VAR_P (decl)
723       && !DECL_HAS_VALUE_EXPR_P (decl)
724       && get_section ())
725     get_variable_section (decl, false);
726 }
727 
728 /* Output all variables enqueued to be assembled.  */
729 bool
output_variables(void)730 symbol_table::output_variables (void)
731 {
732   bool changed = false;
733   varpool_node *node;
734 
735   if (seen_error ())
736     return false;
737 
738   remove_unreferenced_decls ();
739 
740   timevar_push (TV_VAROUT);
741 
742   FOR_EACH_DEFINED_VARIABLE (node)
743     {
744       /* Handled in output_in_order.  */
745       if (node->no_reorder)
746 	continue;
747 
748       node->finalize_named_section_flags ();
749     }
750 
751   /* There is a similar loop in output_in_order.  Please keep them in sync.  */
752   FOR_EACH_VARIABLE (node)
753     {
754       /* Handled in output_in_order.  */
755       if (node->no_reorder)
756 	continue;
757       if (DECL_HARD_REGISTER (node->decl)
758 	  || DECL_HAS_VALUE_EXPR_P (node->decl))
759 	continue;
760       if (node->definition)
761 	changed |= node->assemble_decl ();
762       else
763 	assemble_undefined_decl (node->decl);
764     }
765   timevar_pop (TV_VAROUT);
766   return changed;
767 }
768 
769 /* Attempt to mark ALIAS as an alias to DECL.  Return TRUE if successful.
770    Extra name aliases are output whenever DECL is output.  */
771 
772 varpool_node *
create_alias(tree alias,tree decl)773 varpool_node::create_alias (tree alias, tree decl)
774 {
775   varpool_node *alias_node;
776 
777   gcc_assert (VAR_P (decl));
778   gcc_assert (VAR_P (alias));
779   alias_node = varpool_node::get_create (alias);
780   alias_node->alias = true;
781   alias_node->definition = true;
782   alias_node->alias_target = decl;
783   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
784     alias_node->weakref = alias_node->transparent_alias = true;
785   return alias_node;
786 }
787 
788 /* Attempt to mark ALIAS as an alias to DECL.  Return TRUE if successful.
789    Extra name aliases are output whenever DECL is output.  */
790 
791 varpool_node *
create_extra_name_alias(tree alias,tree decl)792 varpool_node::create_extra_name_alias (tree alias, tree decl)
793 {
794   varpool_node *alias_node;
795 
796   /* If aliases aren't supported by the assembler, fail.  */
797   if (!TARGET_SUPPORTS_ALIASES)
798     return NULL;
799 
800   alias_node = varpool_node::create_alias (alias, decl);
801   alias_node->cpp_implicit_alias = true;
802 
803   /* Extra name alias mechanism creates aliases really late
804      via DECL_ASSEMBLER_NAME mechanism.
805      This is unfortunate because they are not going through the
806      standard channels.  Ensure they get output.  */
807   if (symtab->cpp_implicit_aliases_done)
808     alias_node->resolve_alias (varpool_node::get_create (decl));
809   return alias_node;
810 }
811 
812 /* Worker for call_for_symbol_and_aliases.  */
813 
814 bool
call_for_symbol_and_aliases_1(bool (* callback)(varpool_node *,void *),void * data,bool include_overwritable)815 varpool_node::call_for_symbol_and_aliases_1 (bool (*callback) (varpool_node *,
816 							       void *),
817 					     void *data,
818 					     bool include_overwritable)
819 {
820   ipa_ref *ref;
821 
822   FOR_EACH_ALIAS (this, ref)
823     {
824       varpool_node *alias = dyn_cast <varpool_node *> (ref->referring);
825       if (include_overwritable
826 	  || alias->get_availability () > AVAIL_INTERPOSABLE)
827 	if (alias->call_for_symbol_and_aliases (callback, data,
828 					        include_overwritable))
829 	  return true;
830     }
831   return false;
832 }
833