twe(4): Add two missing error checks.
[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
199   /* When streaming we can have multiple nodes associated with decl.  */
200   if (symtab->state == LTO_STREAMING)
201     ;
202   /* Keep constructor when it may be used for folding. We remove
203      references to external variables before final compilation.  */
204   else if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node
205            && !ctor_useable_for_folding_p ())
206     remove_initializer ();
207
208   unregister ();
209   ggc_free (this);
210 }
211
212 /* Remove node initializer when it is no longer needed.  */
213 void
214 varpool_node::remove_initializer (void)
215 {
216   if (DECL_INITIAL (decl)
217       && !DECL_IN_CONSTANT_POOL (decl)
218       /* Keep vtables for BINFO folding.  */
219       && !DECL_VIRTUAL_P (decl)
220       /* FIXME: http://gcc.gnu.org/PR55395 */
221       && debug_info_level == DINFO_LEVEL_NONE
222       /* When doing declaration merging we have duplicate
223          entries for given decl.  Do not attempt to remove
224          the boides, or we will end up remiving
225          wrong one.  */
226       && symtab->state != LTO_STREAMING)
227     DECL_INITIAL (decl) = error_mark_node;
228 }
229
230 /* Dump given varpool node to F.  */
231 void
232 varpool_node::dump (FILE *f)
233 {
234   dump_base (f);
235   fprintf (f, "  Availability: %s\n",
236            symtab->function_flags_ready
237            ? cgraph_availability_names[get_availability ()]
238            : "not-ready");
239   fprintf (f, "  Varpool flags:");
240   if (DECL_INITIAL (decl))
241     fprintf (f, " initialized");
242   if (output)
243     fprintf (f, " output");
244   if (used_by_single_function)
245     fprintf (f, " used-by-single-function");
246   if (need_bounds_init)
247     fprintf (f, " need-bounds-init");
248   if (TREE_READONLY (decl))
249     fprintf (f, " read-only");
250   if (ctor_useable_for_folding_p ())
251     fprintf (f, " const-value-known");
252   if (writeonly)
253     fprintf (f, " write-only");
254   if (tls_model)
255     fprintf (f, " tls-%s", tls_model_names [tls_model]);
256   fprintf (f, "\n");
257 }
258
259
260 /* Dump given varpool node to stderr.  */
261 void varpool_node::debug (void)
262 {
263   varpool_node::dump (stderr);
264 }
265
266 /* Dump the variable pool to F.  */
267 void
268 varpool_node::dump_varpool (FILE *f)
269 {
270   varpool_node *node;
271
272   fprintf (f, "variable pool:\n\n");
273   FOR_EACH_VARIABLE (node)
274     node->dump (f);
275 }
276
277 /* Dump the variable pool to stderr.  */
278
279 DEBUG_FUNCTION void
280 varpool_node::debug_varpool (void)
281 {
282   dump_varpool (stderr);
283 }
284
285 /* Given an assembler name, lookup node.  */
286 varpool_node *
287 varpool_node::get_for_asmname (tree asmname)
288 {
289   if (symtab_node *node = symtab_node::get_for_asmname (asmname))
290     return dyn_cast <varpool_node *> (node);
291   else
292     return NULL;
293 }
294
295 /* When doing LTO, read variable's constructor from disk if
296    it is not already present.  */
297
298 tree
299 varpool_node::get_constructor (void)
300 {
301   lto_file_decl_data *file_data;
302   const char *data, *name;
303   size_t len;
304
305   if (DECL_INITIAL (decl) != error_mark_node
306       || !in_lto_p)
307     return DECL_INITIAL (decl);
308
309   timevar_push (TV_IPA_LTO_CTORS_IN);
310
311   file_data = lto_file_data;
312   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
313
314   /* We may have renamed the declaration, e.g., a static function.  */
315   name = lto_get_decl_name_mapping (file_data, name);
316
317   data = lto_get_section_data (file_data, LTO_section_function_body,
318                                name, &len);
319   if (!data)
320     fatal_error (input_location, "%s: section %s is missing",
321                  file_data->file_name,
322                  name);
323
324   lto_input_variable_constructor (file_data, this, data);
325   lto_stats.num_function_bodies++;
326   lto_free_section_data (file_data, LTO_section_function_body, name,
327                          data, len);
328   lto_free_function_in_decl_state_for_node (this);
329   timevar_pop (TV_IPA_LTO_CTORS_IN);
330   return DECL_INITIAL (decl);
331 }
332
333 /* Return true if variable has constructor that can be used for folding.  */
334
335 bool
336 varpool_node::ctor_useable_for_folding_p (void)
337 {
338   varpool_node *real_node = this;
339
340   if (real_node->alias && real_node->definition)
341     real_node = ultimate_alias_target ();
342
343   if (TREE_CODE (decl) == CONST_DECL
344       || DECL_IN_CONSTANT_POOL (decl))
345     return true;
346   if (TREE_THIS_VOLATILE (decl))
347     return false;
348
349   /* If we do not have a constructor, we can't use it.  */
350   if (DECL_INITIAL (real_node->decl) == error_mark_node
351       && !real_node->lto_file_data)
352     return false;
353
354   /* Avoid attempts to load constructors that was not streamed.  */
355   if (flag_ltrans && DECL_INITIAL (real_node->decl) == error_mark_node
356       && real_node->body_removed)
357     return false;
358
359   /* Vtables are defined by their types and must match no matter of interposition
360      rules.  */
361   if (DECL_VIRTUAL_P (decl))
362     {
363       /* The C++ front end creates VAR_DECLs for vtables of typeinfo
364          classes not defined in the current TU so that it can refer
365          to them from typeinfo objects.  Avoid returning NULL_TREE.  */
366       return DECL_INITIAL (real_node->decl) != NULL;
367     }
368
369   /* Alias of readonly variable is also readonly, since the variable is stored
370      in readonly memory.  We also accept readonly aliases of non-readonly
371      locations assuming that user knows what he is asking for.  */
372   if (!TREE_READONLY (decl) && !TREE_READONLY (real_node->decl))
373     return false;
374
375   /* Variables declared 'const' without an initializer
376      have zero as the initializer if they may not be
377      overridden at link or run time.
378
379      It is actually requirement for C++ compiler to optimize const variables
380      consistently. As a GNU extension, do not enfore this rule for user defined
381      weak variables, so we support interposition on:
382      static const int dummy = 0;
383      extern const int foo __attribute__((__weak__, __alias__("dummy"))); 
384    */
385   if ((!DECL_INITIAL (real_node->decl)
386        || (DECL_WEAK (decl) && !DECL_COMDAT (decl)))
387       && (DECL_EXTERNAL (decl) || decl_replaceable_p (decl)))
388     return false;
389
390   /* Variables declared `const' with an initializer are considered
391      to not be overwritable with different initializer by default. 
392
393      ??? Previously we behaved so for scalar variables but not for array
394      accesses.  */
395   return true;
396 }
397
398 /* If DECLARATION is constant variable and its initial value is known
399    (so we can do constant folding), return its constructor (DECL_INITIAL).
400    This may be an expression or NULL when DECL is initialized to 0.
401    Return ERROR_MARK_NODE otherwise.
402
403    In LTO this may actually trigger reading the constructor from disk.
404    For this reason varpool_ctor_useable_for_folding_p should be used when
405    the actual constructor value is not needed.  */
406
407 tree
408 ctor_for_folding (tree decl)
409 {
410   varpool_node *node, *real_node;
411   tree real_decl;
412
413   if (TREE_CODE (decl) != VAR_DECL
414       && TREE_CODE (decl) != CONST_DECL)
415     return error_mark_node;
416
417   /* Static constant bounds are created to be
418      used instead of constants and therefore
419      do not let folding it.  */
420   if (POINTER_BOUNDS_P (decl))
421     return error_mark_node;
422
423   if (TREE_CODE (decl) == CONST_DECL
424       || DECL_IN_CONSTANT_POOL (decl))
425     return DECL_INITIAL (decl);
426
427   if (TREE_THIS_VOLATILE (decl))
428     return error_mark_node;
429
430   /* Do not care about automatic variables.  Those are never initialized
431      anyway, because gimplifier exapnds the code.  */
432   if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
433     {
434       gcc_assert (!TREE_PUBLIC (decl));
435       return error_mark_node;
436     }
437
438   gcc_assert (TREE_CODE (decl) == VAR_DECL);
439
440   real_node = node = varpool_node::get (decl);
441   if (node)
442     {
443       real_node = node->ultimate_alias_target ();
444       real_decl = real_node->decl;
445     }
446   else
447     real_decl = decl;
448
449   /* See if we are dealing with alias.
450      In most cases alias is just alternative symbol pointing to a given
451      constructor.  This allows us to use interposition rules of DECL
452      constructor of REAL_NODE.  However weakrefs are special by being just
453      alternative name of their target (if defined).  */
454   if (decl != real_decl)
455     {
456       gcc_assert (!DECL_INITIAL (decl)
457                   || (node->alias && node->get_alias_target () == real_node)
458                   || DECL_INITIAL (decl) == error_mark_node);
459       if (node->weakref)
460         {
461           node = node->get_alias_target ();
462           decl = node->decl;
463         }
464     }
465
466   if ((!DECL_VIRTUAL_P (real_decl)
467        || DECL_INITIAL (real_decl) == error_mark_node
468        || !DECL_INITIAL (real_decl))
469       && (!node || !node->ctor_useable_for_folding_p ()))
470     return error_mark_node;
471
472   /* OK, we can return constructor.  See if we need to fetch it from disk
473      in LTO mode.  */
474   if (DECL_INITIAL (real_decl) != error_mark_node
475       || !in_lto_p)
476     return DECL_INITIAL (real_decl);
477   return real_node->get_constructor ();
478 }
479
480 /* Add the variable DECL to the varpool.
481    Unlike finalize_decl function is intended to be used
482    by middle end and allows insertion of new variable at arbitrary point
483    of compilation.  */
484 void
485 varpool_node::add (tree decl)
486 {
487   varpool_node *node;
488   varpool_node::finalize_decl (decl);
489   node = varpool_node::get_create (decl);
490   symtab->call_varpool_insertion_hooks (node);
491   if (node->externally_visible_p ())
492     node->externally_visible = true;
493   if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
494     node->no_reorder = 1;
495 }
496
497 /* Return variable availability.  See cgraph.h for description of individual
498    return values.  */
499 enum availability
500 varpool_node::get_availability (void)
501 {
502   if (!definition)
503     return AVAIL_NOT_AVAILABLE;
504   if (!TREE_PUBLIC (decl))
505     return AVAIL_AVAILABLE;
506   if (DECL_IN_CONSTANT_POOL (decl)
507       || DECL_VIRTUAL_P (decl))
508     return AVAIL_AVAILABLE;
509   if (alias && weakref)
510     {
511       enum availability avail;
512
513       ultimate_alias_target (&avail)->get_availability ();
514       return avail;
515     }
516   /* If the variable can be overwritten, return OVERWRITABLE.  Takes
517      care of at least one notable extension - the COMDAT variables
518      used to share template instantiations in C++.  */
519   if (decl_replaceable_p (decl)
520       || DECL_EXTERNAL (decl))
521     return AVAIL_INTERPOSABLE;
522   return AVAIL_AVAILABLE;
523 }
524
525 void
526 varpool_node::analyze (void)
527 {
528   /* When reading back varpool at LTO time, we re-construct the queue in order
529      to have "needed" list right by inserting all needed nodes into varpool.
530      We however don't want to re-analyze already analyzed nodes.  */
531   if (!analyzed)
532     {
533       gcc_assert (!in_lto_p || symtab->function_flags_ready);
534       /* Compute the alignment early so function body expanders are
535          already informed about increased alignment.  */
536       align_variable (decl, 0);
537     }
538   if (alias)
539     resolve_alias (varpool_node::get (alias_target));
540   else if (DECL_INITIAL (decl))
541     record_references_in_initializer (decl, analyzed);
542   analyzed = true;
543 }
544
545 /* Assemble thunks and aliases associated to varpool node.  */
546
547 void
548 varpool_node::assemble_aliases (void)
549 {
550   ipa_ref *ref;
551
552   FOR_EACH_ALIAS (this, ref)
553     {
554       varpool_node *alias = dyn_cast <varpool_node *> (ref->referring);
555       do_assemble_alias (alias->decl,
556                          DECL_ASSEMBLER_NAME (decl));
557       alias->assemble_aliases ();
558     }
559 }
560
561 /* Output one variable, if necessary.  Return whether we output it.  */
562
563 bool
564 varpool_node::assemble_decl (void)
565 {
566   /* Aliases are outout when their target is produced or by
567      output_weakrefs.  */
568   if (alias)
569     return false;
570
571   /* Constant pool is output from RTL land when the reference
572      survive till this level.  */
573   if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl))
574     return false;
575
576   /* Decls with VALUE_EXPR should not be in the varpool at all.  They
577      are not real variables, but just info for debugging and codegen.
578      Unfortunately at the moment emutls is not updating varpool correctly
579      after turning real vars into value_expr vars.  */
580   if (DECL_HAS_VALUE_EXPR_P (decl)
581       && !targetm.have_tls)
582     return false;
583
584   /* Hard register vars do not need to be output.  */
585   if (DECL_HARD_REGISTER (decl))
586     return false;
587
588   gcc_checking_assert (!TREE_ASM_WRITTEN (decl)
589                        && TREE_CODE (decl) == VAR_DECL
590                        && !DECL_HAS_VALUE_EXPR_P (decl));
591
592   if (!in_other_partition
593       && !DECL_EXTERNAL (decl))
594     {
595       get_constructor ();
596       assemble_variable (decl, 0, 1, 0);
597       gcc_assert (TREE_ASM_WRITTEN (decl));
598       gcc_assert (definition);
599       assemble_aliases ();
600       return true;
601     }
602
603   return false;
604 }
605
606 /* Add NODE to queue starting at FIRST. 
607    The queue is linked via AUX pointers and terminated by pointer to 1.  */
608
609 static void
610 enqueue_node (varpool_node *node, varpool_node **first)
611 {
612   if (node->aux)
613     return;
614   gcc_checking_assert (*first);
615   node->aux = *first;
616   *first = node;
617 }
618
619 /* Optimization of function bodies might've rendered some variables as
620    unnecessary so we want to avoid these from being compiled.  Re-do
621    reachability starting from variables that are either externally visible
622    or was referred from the asm output routines.  */
623
624 void
625 symbol_table::remove_unreferenced_decls (void)
626 {
627   varpool_node *next, *node;
628   varpool_node *first = (varpool_node *)(void *)1;
629   int i;
630   ipa_ref *ref = NULL;
631   hash_set<varpool_node *> referenced;
632
633   if (seen_error ())
634     return;
635
636   if (dump_file)
637     fprintf (dump_file, "Trivially needed variables:");
638   FOR_EACH_DEFINED_VARIABLE (node)
639     {
640       if (node->analyzed
641           && (!node->can_remove_if_no_refs_p ()
642               /* We just expanded all function bodies.  See if any of
643                  them needed the variable.  */
644               || DECL_RTL_SET_P (node->decl)))
645         {
646           enqueue_node (node, &first);
647           if (dump_file)
648             fprintf (dump_file, " %s", node->asm_name ());
649         }
650     }
651   while (first != (varpool_node *)(void *)1)
652     {
653       node = first;
654       first = (varpool_node *)first->aux;
655
656       if (node->same_comdat_group)
657         {
658           symtab_node *next;
659           for (next = node->same_comdat_group;
660                next != node;
661                next = next->same_comdat_group)
662             {
663               varpool_node *vnext = dyn_cast <varpool_node *> (next);
664               if (vnext && vnext->analyzed && !next->comdat_local_p ())
665                 enqueue_node (vnext, &first);
666             }
667         }
668       for (i = 0; node->iterate_reference (i, ref); i++)
669         {
670           varpool_node *vnode = dyn_cast <varpool_node *> (ref->referred);
671           if (vnode
672               && !vnode->in_other_partition
673               && (!DECL_EXTERNAL (ref->referred->decl)
674                   || vnode->alias)
675               && vnode->analyzed)
676             enqueue_node (vnode, &first);
677           else
678             referenced.add (node);
679         }
680     }
681   if (dump_file)
682     fprintf (dump_file, "\nRemoving variables:");
683   for (node = first_defined_variable (); node; node = next)
684     {
685       next = next_defined_variable (node);
686       if (!node->aux && !node->no_reorder)
687         {
688           if (dump_file)
689             fprintf (dump_file, " %s", node->asm_name ());
690           if (referenced.contains(node))
691             node->remove_initializer ();
692           else
693             node->remove ();
694         }
695     }
696
697   if (dump_file)
698     fprintf (dump_file, "\n");
699 }
700
701 /* For variables in named sections make sure get_variable_section
702    is called before we switch to those sections.  Then section
703    conflicts between read-only and read-only requiring relocations
704    sections can be resolved.  */
705 void
706 varpool_node::finalize_named_section_flags (void)
707 {
708   if (!TREE_ASM_WRITTEN (decl)
709       && !alias
710       && !in_other_partition
711       && !DECL_EXTERNAL (decl)
712       && TREE_CODE (decl) == VAR_DECL
713       && !DECL_HAS_VALUE_EXPR_P (decl)
714       && get_section ())
715     get_variable_section (decl, false);
716 }
717
718 /* Output all variables enqueued to be assembled.  */
719 bool
720 symbol_table::output_variables (void)
721 {
722   bool changed = false;
723   varpool_node *node;
724
725   if (seen_error ())
726     return false;
727
728   remove_unreferenced_decls ();
729
730   timevar_push (TV_VAROUT);
731
732   FOR_EACH_VARIABLE (node)
733     if (!node->definition)
734       assemble_undefined_decl (node->decl);
735   FOR_EACH_DEFINED_VARIABLE (node)
736     {
737       /* Handled in output_in_order.  */
738       if (node->no_reorder)
739         continue;
740
741       node->finalize_named_section_flags ();
742     }
743
744   FOR_EACH_DEFINED_VARIABLE (node)
745     {
746       /* Handled in output_in_order.  */
747       if (node->no_reorder)
748         continue;
749       if (node->assemble_decl ())
750         changed = true;
751     }
752   timevar_pop (TV_VAROUT);
753   return changed;
754 }
755
756 /* Create a new global variable of type TYPE.  */
757 tree
758 add_new_static_var (tree type)
759 {
760   tree new_decl;
761   varpool_node *new_node;
762
763   new_decl = create_tmp_var_raw (type);
764   DECL_NAME (new_decl) = create_tmp_var_name (NULL);
765   TREE_READONLY (new_decl) = 0;
766   TREE_STATIC (new_decl) = 1;
767   TREE_USED (new_decl) = 1;
768   DECL_CONTEXT (new_decl) = NULL_TREE;
769   DECL_ABSTRACT_P (new_decl) = false;
770   lang_hooks.dup_lang_specific_decl (new_decl);
771   new_node = varpool_node::get_create (new_decl);
772   varpool_node::finalize_decl (new_decl);
773
774   return new_node->decl;
775 }
776
777 /* Attempt to mark ALIAS as an alias to DECL.  Return TRUE if successful.
778    Extra name aliases are output whenever DECL is output.  */
779
780 varpool_node *
781 varpool_node::create_alias (tree alias, tree decl)
782 {
783   varpool_node *alias_node;
784
785   gcc_assert (TREE_CODE (decl) == VAR_DECL);
786   gcc_assert (TREE_CODE (alias) == VAR_DECL);
787   alias_node = varpool_node::get_create (alias);
788   alias_node->alias = true;
789   alias_node->definition = true;
790   alias_node->alias_target = decl;
791   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
792     alias_node->weakref = true;
793   return alias_node;
794 }
795
796 /* Attempt to mark ALIAS as an alias to DECL.  Return TRUE if successful.
797    Extra name aliases are output whenever DECL is output.  */
798
799 varpool_node *
800 varpool_node::create_extra_name_alias (tree alias, tree decl)
801 {
802   varpool_node *alias_node;
803
804 #ifndef ASM_OUTPUT_DEF
805   /* If aliases aren't supported by the assembler, fail.  */
806   return NULL;
807 #endif
808   alias_node = varpool_node::create_alias (alias, decl);
809   alias_node->cpp_implicit_alias = true;
810
811   /* Extra name alias mechanizm creates aliases really late
812      via DECL_ASSEMBLER_NAME mechanizm.
813      This is unfortunate because they are not going through the
814      standard channels.  Ensure they get output.  */
815   if (symtab->cpp_implicit_aliases_done)
816     alias_node->resolve_alias (varpool_node::get_create (decl));
817   return alias_node;
818 }
819
820 /* Call calback on varpool symbol and aliases associated to varpool symbol.
821    When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
822    skipped. */
823
824 bool
825 varpool_node::call_for_node_and_aliases (bool (*callback) (varpool_node *,
826                                                            void *),
827                                          void *data,
828                                          bool include_overwritable)
829 {
830   ipa_ref *ref;
831
832   if (callback (this, data))
833     return true;
834
835   FOR_EACH_ALIAS (this, ref)
836     {
837       varpool_node *alias = dyn_cast <varpool_node *> (ref->referring);
838       if (include_overwritable
839           || alias->get_availability () > AVAIL_INTERPOSABLE)
840         if (alias->call_for_node_and_aliases (callback, data,
841                                               include_overwritable))
842           return true;
843     }
844   return false;
845 }