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