OSDN Git Service

2012-01-27 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / varpool.c
1 /* Callgraph handling code.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2010, 2011
3    Free Software Foundation, Inc.
4    Contributed by Jan Hubicka
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "cgraph.h"
28 #include "langhooks.h"
29 #include "diagnostic-core.h"
30 #include "hashtab.h"
31 #include "ggc.h"
32 #include "timevar.h"
33 #include "debug.h"
34 #include "target.h"
35 #include "output.h"
36 #include "gimple.h"
37 #include "tree-flow.h"
38 #include "flags.h"
39
40 /*  This file contains basic routines manipulating variable pool.
41
42     Varpool acts as interface in between the front-end and middle-end
43     and drives the decision process on what variables and when are
44     going to be compiled.
45
46     The varpool nodes are allocated lazily for declarations
47     either by frontend or at callgraph construction time.
48     All variables supposed to be output into final file needs to be
49     explicitly marked by frontend via VARPOOL_FINALIZE_DECL function.  */
50
51 /* Hash table used to convert declarations into nodes.  */
52 static GTY((param_is (struct varpool_node))) htab_t varpool_hash;
53
54 /* The linked list of cgraph varpool nodes.
55    Linked via node->next pointer.  */
56 struct varpool_node *varpool_nodes;
57
58 /* Queue of cgraph nodes scheduled to be lowered and output.
59    The queue is maintained via mark_needed_node, linked via node->next_needed
60    pointer.
61
62    LAST_NEEDED_NODE points to the end of queue, so it can be
63    maintained in forward order.  GTY is needed to make it friendly to
64    PCH.
65
66    During compilation we construct the queue of needed variables
67    twice: first time it is during cgraph construction, second time it is at the
68    end of compilation in VARPOOL_REMOVE_UNREFERENCED_DECLS so we can avoid
69    optimized out variables being output.
70
71    Each variable is thus first analyzed and then later possibly output.
72    FIRST_UNANALYZED_NODE points to first node in queue that was not analyzed
73    yet and is moved via VARPOOL_ANALYZE_PENDING_DECLS.  */
74
75 struct varpool_node *varpool_nodes_queue;
76 static GTY(()) struct varpool_node *varpool_last_needed_node;
77 static GTY(()) struct varpool_node *varpool_first_unanalyzed_node;
78
79 /* Lists all assembled variables to be sent to debugger output later on.  */
80 static GTY(()) struct varpool_node *varpool_assembled_nodes_queue;
81
82 /* Return name of the node used in debug output.  */
83 const char *
84 varpool_node_name (struct varpool_node *node)
85 {
86   return lang_hooks.decl_printable_name (node->decl, 2);
87 }
88
89 /* Returns a hash code for P.  */
90 static hashval_t
91 hash_varpool_node (const void *p)
92 {
93   const struct varpool_node *n = (const struct varpool_node *) p;
94   return (hashval_t) DECL_UID (n->decl);
95 }
96
97 /* Returns nonzero if P1 and P2 are equal.  */
98 static int
99 eq_varpool_node (const void *p1, const void *p2)
100 {
101   const struct varpool_node *n1 =
102     (const struct varpool_node *) p1;
103   const struct varpool_node *n2 =
104     (const struct varpool_node *) p2;
105   return DECL_UID (n1->decl) == DECL_UID (n2->decl);
106 }
107
108 /* Return varpool node assigned to DECL without creating new one.  */
109 struct varpool_node *
110 varpool_get_node (const_tree decl)
111 {
112   struct varpool_node key, **slot;
113
114   gcc_assert (TREE_CODE (decl) == VAR_DECL
115               && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
116
117   if (!varpool_hash)
118     return NULL;
119   key.decl = CONST_CAST2 (tree, const_tree, decl);
120   slot = (struct varpool_node **)
121     htab_find_slot (varpool_hash, &key, NO_INSERT);
122   if (!slot)
123     return NULL;
124   return *slot;
125 }
126
127 /* Return varpool node assigned to DECL.  Create new one when needed.  */
128 struct varpool_node *
129 varpool_node (tree decl)
130 {
131   struct varpool_node key, *node, **slot;
132
133   gcc_assert (TREE_CODE (decl) == VAR_DECL
134               && (TREE_STATIC (decl) || DECL_EXTERNAL (decl) || in_lto_p));
135
136   if (!varpool_hash)
137     varpool_hash = htab_create_ggc (10, hash_varpool_node,
138                                            eq_varpool_node, NULL);
139   key.decl = decl;
140   slot = (struct varpool_node **)
141     htab_find_slot (varpool_hash, &key, INSERT);
142   if (*slot)
143     return *slot;
144   node = ggc_alloc_cleared_varpool_node ();
145   node->decl = decl;
146   node->order = cgraph_order++;
147   node->next = varpool_nodes;
148   ipa_empty_ref_list (&node->ref_list);
149   if (varpool_nodes)
150     varpool_nodes->prev = node;
151   varpool_nodes = node;
152   *slot = node;
153   return node;
154 }
155
156 /* Remove node from the varpool.  */
157 void
158 varpool_remove_node (struct varpool_node *node)
159 {
160   void **slot;
161   slot = htab_find_slot (varpool_hash, node, NO_INSERT);
162   gcc_assert (*slot == node);
163   htab_clear_slot (varpool_hash, slot);
164   gcc_assert (!varpool_assembled_nodes_queue);
165   if (node->next)
166     node->next->prev = node->prev;
167   if (node->prev)
168     node->prev->next = node->next;
169   else
170     {
171       gcc_assert (varpool_nodes == node);
172       varpool_nodes = node->next;
173     }
174   if (varpool_first_unanalyzed_node == node)
175     varpool_first_unanalyzed_node = node->next_needed;
176   if (node->next_needed)
177     node->next_needed->prev_needed = node->prev_needed;
178   else if (node->prev_needed)
179     {
180       gcc_assert (varpool_last_needed_node);
181       varpool_last_needed_node = node->prev_needed;
182     }
183   if (node->prev_needed)
184     node->prev_needed->next_needed = node->next_needed;
185   else if (node->next_needed)
186     {
187       gcc_assert (varpool_nodes_queue == node);
188       varpool_nodes_queue = node->next_needed;
189     }
190   if (node->same_comdat_group)
191     {
192       struct varpool_node *prev;
193       for (prev = node->same_comdat_group;
194            prev->same_comdat_group != node;
195            prev = prev->same_comdat_group)
196         ;
197       if (node->same_comdat_group == prev)
198         prev->same_comdat_group = NULL;
199       else
200         prev->same_comdat_group = node->same_comdat_group;
201       node->same_comdat_group = NULL;
202     }
203   ipa_remove_all_references (&node->ref_list);
204   ipa_remove_all_refering (&node->ref_list);
205   ggc_free (node);
206 }
207
208 /* Dump given cgraph node.  */
209 void
210 dump_varpool_node (FILE *f, struct varpool_node *node)
211 {
212   fprintf (f, "%s:", varpool_node_name (node));
213   fprintf (f, " availability:%s",
214            cgraph_function_flags_ready
215            ? cgraph_availability_names[cgraph_variable_initializer_availability (node)]
216            : "not-ready");
217   if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
218     fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
219   if (DECL_INITIAL (node->decl))
220     fprintf (f, " initialized");
221   if (TREE_ASM_WRITTEN (node->decl))
222     fprintf (f, " (asm written)");
223   if (node->needed)
224     fprintf (f, " needed");
225   if (node->analyzed)
226     fprintf (f, " analyzed");
227   if (node->finalized)
228     fprintf (f, " finalized");
229   if (node->output)
230     fprintf (f, " output");
231   if (node->externally_visible)
232     fprintf (f, " externally_visible");
233   if (node->resolution != LDPR_UNKNOWN)
234     fprintf (f, " %s",
235              ld_plugin_symbol_resolution_names[(int)node->resolution]);
236   if (node->in_other_partition)
237     fprintf (f, " in_other_partition");
238   else if (node->used_from_other_partition)
239     fprintf (f, " used_from_other_partition");
240   fprintf (f, "\n");
241   fprintf (f, "  References: ");
242   ipa_dump_references (f, &node->ref_list);
243   fprintf (f, "  Refering this var: ");
244   ipa_dump_refering (f, &node->ref_list);
245 }
246
247 /* Dump the variable pool.  */
248 void
249 dump_varpool (FILE *f)
250 {
251   struct varpool_node *node;
252
253   fprintf (f, "variable pool:\n\n");
254   for (node = varpool_nodes; node; node = node->next)
255     dump_varpool_node (f, node);
256 }
257
258 /* Dump the variable pool to stderr.  */
259
260 DEBUG_FUNCTION void
261 debug_varpool (void)
262 {
263   dump_varpool (stderr);
264 }
265
266 /* Given an assembler name, lookup node.  */
267 struct varpool_node *
268 varpool_node_for_asm (tree asmname)
269 {
270   struct varpool_node *node;
271
272   for (node = varpool_nodes; node ; node = node->next)
273     if (decl_assembler_name_equal (node->decl, asmname))
274       return node;
275
276   return NULL;
277 }
278
279 /* Helper function for finalization code - add node into lists so it will
280    be analyzed and compiled.  */
281 static void
282 varpool_enqueue_needed_node (struct varpool_node *node)
283 {
284   if (varpool_last_needed_node)
285     {
286       varpool_last_needed_node->next_needed = node;
287       node->prev_needed = varpool_last_needed_node;
288     }
289   varpool_last_needed_node = node;
290   node->next_needed = NULL;
291   if (!varpool_nodes_queue)
292     varpool_nodes_queue = node;
293   if (!varpool_first_unanalyzed_node)
294     varpool_first_unanalyzed_node = node;
295   notice_global_symbol (node->decl);
296 }
297
298 /* Notify finalize_compilation_unit that given node is reachable
299    or needed.  */
300 void
301 varpool_mark_needed_node (struct varpool_node *node)
302 {
303   if (!node->needed && node->finalized
304       && !TREE_ASM_WRITTEN (node->decl))
305     varpool_enqueue_needed_node (node);
306   node->needed = 1;
307 }
308
309 /* Reset the queue of needed nodes.  */
310 void
311 varpool_reset_queue (void)
312 {
313   varpool_last_needed_node = NULL;
314   varpool_nodes_queue = NULL;
315   varpool_first_unanalyzed_node = NULL;
316 }
317
318 /* Determine if variable DECL is needed.  That is, visible to something
319    either outside this translation unit, something magic in the system
320    configury */
321 bool
322 decide_is_variable_needed (struct varpool_node *node, tree decl)
323 {
324   /* If the user told us it is used, then it must be so.  */
325   if (node->force_output)
326     return true;
327
328   gcc_assert (!DECL_EXTERNAL (decl));
329
330   /* Externally visible variables must be output.  The exception is
331      COMDAT variables that must be output only when they are needed.  */
332   if (TREE_PUBLIC (decl)
333       && !DECL_COMDAT (decl)
334       && !DECL_EXTERNAL (decl))
335     return true;
336
337   /* When not reordering top level variables, we have to assume that
338      we are going to keep everything.  */
339   if (!flag_toplevel_reorder)
340     return true;
341   return false;
342 }
343
344 /* Return if DECL is constant and its initial value is known (so we can do
345    constant folding using DECL_INITIAL (decl)).  */
346
347 bool
348 const_value_known_p (tree decl)
349 {
350   if (TREE_CODE (decl) != VAR_DECL
351       &&TREE_CODE (decl) != CONST_DECL)
352     return false;
353
354   if (TREE_CODE (decl) == CONST_DECL
355       || DECL_IN_CONSTANT_POOL (decl))
356     return true;
357
358   gcc_assert (TREE_CODE (decl) == VAR_DECL);
359
360   if (!TREE_READONLY (decl) || TREE_THIS_VOLATILE (decl))
361     return false;
362
363   /* Gimplifier takes away constructors of local vars  */
364   if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
365     return DECL_INITIAL (decl) != NULL;
366
367   gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
368
369   /* Variables declared 'const' without an initializer
370      have zero as the initializer if they may not be
371      overridden at link or run time.  */
372   if (!DECL_INITIAL (decl)
373       && (DECL_EXTERNAL (decl)
374           || decl_replaceable_p (decl)))
375     return false;
376
377   /* Variables declared `const' with an initializer are considered
378      to not be overwritable with different initializer by default. 
379
380      ??? Previously we behaved so for scalar variables but not for array
381      accesses.  */
382   return true;
383 }
384
385 /* Mark DECL as finalized.  By finalizing the declaration, frontend instruct the
386    middle end to output the variable to asm file, if needed or externally
387    visible.  */
388 void
389 varpool_finalize_decl (tree decl)
390 {
391   struct varpool_node *node = varpool_node (decl);
392
393   gcc_assert (TREE_STATIC (decl));
394
395   /* The first declaration of a variable that comes through this function
396      decides whether it is global (in C, has external linkage)
397      or local (in C, has internal linkage).  So do nothing more
398      if this function has already run.  */
399   if (node->finalized)
400     {
401       if (cgraph_global_info_ready)
402         varpool_assemble_pending_decls ();
403       return;
404     }
405   if (node->needed)
406     varpool_enqueue_needed_node (node);
407   node->finalized = true;
408   if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl))
409     node->force_output = true;
410
411   if (decide_is_variable_needed (node, decl))
412     varpool_mark_needed_node (node);
413   if (cgraph_global_info_ready)
414     varpool_assemble_pending_decls ();
415 }
416
417 /* Add the variable DECL to the varpool.
418    Unlike varpool_finalize_decl function is intended to be used
419    by middle end and allows insertion of new variable at arbitrary point
420    of compilation.  */
421 void
422 varpool_add_new_variable (tree decl)
423 {
424   struct varpool_node *node;
425   varpool_finalize_decl (decl);
426   node = varpool_node (decl);
427   if (varpool_externally_visible_p (node, false))
428     node->externally_visible = true;
429 }
430
431 /* Return variable availability.  See cgraph.h for description of individual
432    return values.  */
433 enum availability
434 cgraph_variable_initializer_availability (struct varpool_node *node)
435 {
436   gcc_assert (cgraph_function_flags_ready);
437   if (!node->finalized)
438     return AVAIL_NOT_AVAILABLE;
439   if (!TREE_PUBLIC (node->decl))
440     return AVAIL_AVAILABLE;
441   /* If the variable can be overwritten, return OVERWRITABLE.  Takes
442      care of at least two notable extensions - the COMDAT variables
443      used to share template instantiations in C++.  */
444   if (!decl_replaceable_p (node->decl))
445     return AVAIL_OVERWRITABLE;
446   return AVAIL_AVAILABLE;
447 }
448
449 /* Walk the decls we marked as necessary and see if they reference new
450    variables or functions and add them into the worklists.  */
451 bool
452 varpool_analyze_pending_decls (void)
453 {
454   bool changed = false;
455
456   timevar_push (TV_VARPOOL);
457   while (varpool_first_unanalyzed_node)
458     {
459       struct varpool_node *node = varpool_first_unanalyzed_node, *next;
460       tree decl = node->decl;
461       bool analyzed = node->analyzed;
462
463       varpool_first_unanalyzed_node->analyzed = true;
464
465       varpool_first_unanalyzed_node = varpool_first_unanalyzed_node->next_needed;
466
467       /* When reading back varpool at LTO time, we re-construct the queue in order
468          to have "needed" list right by inserting all needed nodes into varpool.
469          We however don't want to re-analyze already analyzed nodes.  */
470       if (!analyzed)
471         {
472           gcc_assert (!in_lto_p || cgraph_function_flags_ready);
473           /* Compute the alignment early so function body expanders are
474              already informed about increased alignment.  */
475           align_variable (decl, 0);
476         }
477       if (node->alias && node->alias_of)
478         {
479           struct varpool_node *tgt = varpool_node (node->alias_of);
480           if (!VEC_length (ipa_ref_t, node->ref_list.references))
481             ipa_record_reference (NULL, node, NULL, tgt, IPA_REF_ALIAS, NULL);
482           /* C++ FE sometimes change linkage flags after producing same body aliases.  */
483           if (node->extra_name_alias)
484             {
485               DECL_WEAK (node->decl) = DECL_WEAK (node->alias_of);
486               TREE_PUBLIC (node->decl) = TREE_PUBLIC (node->alias_of);
487               DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (node->alias_of);
488               DECL_VISIBILITY (node->decl) = DECL_VISIBILITY (node->alias_of);
489               if (TREE_PUBLIC (node->decl))
490                 {
491                   DECL_COMDAT (node->decl) = DECL_COMDAT (node->alias_of);
492                   DECL_COMDAT_GROUP (node->decl) = DECL_COMDAT_GROUP (node->alias_of);
493                   if (DECL_ONE_ONLY (node->alias_of) && !node->same_comdat_group)
494                     {
495                       node->same_comdat_group = tgt;
496                       if (!tgt->same_comdat_group)
497                         tgt->same_comdat_group = node;
498                       else
499                         {
500                           struct varpool_node *n;
501                           for (n = tgt->same_comdat_group;
502                                n->same_comdat_group != tgt;
503                                n = n->same_comdat_group)
504                             ;
505                           n->same_comdat_group = node;
506                         }
507                     }
508                 }
509             }
510         }
511       else if (DECL_INITIAL (decl))
512         record_references_in_initializer (decl, analyzed);
513       if (node->same_comdat_group)
514         {
515           for (next = node->same_comdat_group;
516                next != node;
517                next = next->same_comdat_group)
518             varpool_mark_needed_node (next);
519         }
520       changed = true;
521     }
522   timevar_pop (TV_VARPOOL);
523   return changed;
524 }
525
526 /* Assemble thunks and aliases asociated to NODE.  */
527
528 static void
529 assemble_aliases (struct varpool_node *node)
530 {
531   int i;
532   struct ipa_ref *ref;
533   for (i = 0; ipa_ref_list_refering_iterate (&node->ref_list, i, ref); i++)
534     if (ref->use == IPA_REF_ALIAS)
535       {
536         struct varpool_node *alias = ipa_ref_refering_varpool_node (ref);
537         assemble_alias (alias->decl,
538                         DECL_ASSEMBLER_NAME (alias->alias_of));
539         assemble_aliases (alias);
540       }
541 }
542
543 /* Output one variable, if necessary.  Return whether we output it.  */
544 bool
545 varpool_assemble_decl (struct varpool_node *node)
546 {
547   tree decl = node->decl;
548
549   if (!TREE_ASM_WRITTEN (decl)
550       && !node->alias
551       && !node->in_other_partition
552       && !DECL_EXTERNAL (decl)
553       && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl)))
554     {
555       assemble_variable (decl, 0, 1, 0);
556       if (TREE_ASM_WRITTEN (decl))
557         {
558           node->next_needed = varpool_assembled_nodes_queue;
559           node->prev_needed = NULL;
560           if (varpool_assembled_nodes_queue)
561             varpool_assembled_nodes_queue->prev_needed = node;
562           varpool_assembled_nodes_queue = node;
563           node->finalized = 1;
564           assemble_aliases (node);
565           return true;
566         }
567     }
568
569   return false;
570 }
571
572 /* Optimization of function bodies might've rendered some variables as
573    unnecessary so we want to avoid these from being compiled.
574
575    This is done by pruning the queue and keeping only the variables that
576    really appear needed (ie they are either externally visible or referenced
577    by compiled function). Re-doing the reachability analysis on variables
578    brings back the remaining variables referenced by these.  */
579 void
580 varpool_remove_unreferenced_decls (void)
581 {
582   struct varpool_node *next, *node = varpool_nodes_queue;
583
584   varpool_reset_queue ();
585
586   if (seen_error ())
587     return;
588
589   while (node)
590     {
591       next = node->next_needed;
592       node->needed = 0;
593
594       if (node->analyzed
595           && (!varpool_can_remove_if_no_refs (node)
596               /* We just expanded all function bodies.  See if any of
597                  them needed the variable.  */
598               || DECL_RTL_SET_P (node->decl)))
599         varpool_mark_needed_node (node);
600
601       node = next;
602     }
603   /* Make sure we mark alias targets as used targets.  */
604   finish_aliases_1 ();
605   varpool_analyze_pending_decls ();
606 }
607
608 /* For variables in named sections make sure get_variable_section
609    is called before we switch to those sections.  Then section
610    conflicts between read-only and read-only requiring relocations
611    sections can be resolved.  */
612 void
613 varpool_finalize_named_section_flags (struct varpool_node *node)
614 {
615   if (!TREE_ASM_WRITTEN (node->decl)
616       && !node->alias
617       && !node->in_other_partition
618       && !DECL_EXTERNAL (node->decl)
619       && TREE_CODE (node->decl) == VAR_DECL
620       && !DECL_HAS_VALUE_EXPR_P (node->decl)
621       && DECL_SECTION_NAME (node->decl))
622     get_variable_section (node->decl, false);
623 }
624
625 /* Output all variables enqueued to be assembled.  */
626 bool
627 varpool_assemble_pending_decls (void)
628 {
629   bool changed = false;
630   struct varpool_node *node;
631
632   if (seen_error ())
633     return false;
634
635   timevar_push (TV_VAROUT);
636   /* EH might mark decls as needed during expansion.  This should be safe since
637      we don't create references to new function, but it should not be used
638      elsewhere.  */
639   varpool_analyze_pending_decls ();
640
641   for (node = varpool_nodes_queue; node; node = node->next_needed)
642     varpool_finalize_named_section_flags (node);
643
644   while (varpool_nodes_queue)
645     {
646       struct varpool_node *node = varpool_nodes_queue;
647
648       varpool_nodes_queue = varpool_nodes_queue->next_needed;
649       if (varpool_assemble_decl (node))
650         changed = true;
651       else
652         {
653           node->prev_needed = NULL;
654           node->next_needed = NULL;
655         }
656     }
657   /* varpool_nodes_queue is now empty, clear the pointer to the last element
658      in the queue.  */
659   varpool_last_needed_node = NULL;
660   timevar_pop (TV_VAROUT);
661   return changed;
662 }
663
664 /* Remove all elements from the queue so we can re-use it for debug output.  */
665 void
666 varpool_empty_needed_queue (void)
667 {
668   /* EH might mark decls as needed during expansion.  This should be safe since
669      we don't create references to new function, but it should not be used
670      elsewhere.  */
671   varpool_analyze_pending_decls ();
672
673   while (varpool_nodes_queue)
674     {
675       struct varpool_node *node = varpool_nodes_queue;
676       varpool_nodes_queue = varpool_nodes_queue->next_needed;
677       node->next_needed = NULL;
678       node->prev_needed = NULL;
679     }
680   /* varpool_nodes_queue is now empty, clear the pointer to the last element
681      in the queue.  */
682   varpool_last_needed_node = NULL;
683 }
684
685 /* Create a new global variable of type TYPE.  */
686 tree
687 add_new_static_var (tree type)
688 {
689   tree new_decl;
690   struct varpool_node *new_node;
691
692   new_decl = create_tmp_var (type, NULL);
693   DECL_NAME (new_decl) = create_tmp_var_name (NULL);
694   TREE_READONLY (new_decl) = 0;
695   TREE_STATIC (new_decl) = 1;
696   TREE_USED (new_decl) = 1;
697   DECL_CONTEXT (new_decl) = NULL_TREE;
698   DECL_ABSTRACT (new_decl) = 0;
699   lang_hooks.dup_lang_specific_decl (new_decl);
700   create_var_ann (new_decl);
701   new_node = varpool_node (new_decl);
702   varpool_mark_needed_node (new_node);
703   add_referenced_var (new_decl);
704   varpool_finalize_decl (new_decl);
705
706   return new_node->decl;
707 }
708
709 /* Attempt to mark ALIAS as an alias to DECL.  Return TRUE if successful.
710    Extra name aliases are output whenever DECL is output.  */
711
712 struct varpool_node *
713 varpool_create_variable_alias (tree alias, tree decl)
714 {
715   struct varpool_node *alias_node;
716
717   gcc_assert (TREE_CODE (decl) == VAR_DECL);
718   gcc_assert (TREE_CODE (alias) == VAR_DECL);
719   alias_node = varpool_node (alias);
720   alias_node->alias = 1;
721   if (!DECL_EXTERNAL (alias))
722     alias_node->finalized = 1;
723   alias_node->alias_of = decl;
724   if ((!DECL_EXTERNAL (alias)
725        && decide_is_variable_needed (alias_node, alias))
726       || alias_node->needed)
727     varpool_mark_needed_node (alias_node);
728   return alias_node;
729 }
730
731 /* Attempt to mark ALIAS as an alias to DECL.  Return TRUE if successful.
732    Extra name aliases are output whenever DECL is output.  */
733
734 struct varpool_node *
735 varpool_extra_name_alias (tree alias, tree decl)
736 {
737   struct varpool_node *alias_node;
738
739 #ifndef ASM_OUTPUT_DEF
740   /* If aliases aren't supported by the assembler, fail.  */
741   return NULL;
742 #endif
743   alias_node = varpool_create_variable_alias (alias, decl);
744   alias_node->extra_name_alias = true;
745   return alias_node;
746 }
747
748 /* Return true when NODE is known to be used from other (non-LTO) object file.
749    Known only when doing LTO via linker plugin.  */
750
751 bool
752 varpool_used_from_object_file_p (struct varpool_node *node)
753 {
754   if (!TREE_PUBLIC (node->decl))
755     return false;
756   if (resolution_used_from_other_file_p (node->resolution))
757     return true;
758   return false;
759 }
760
761 /* Call calback on NODE and aliases asociated to NODE. 
762    When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
763    skipped. */
764
765 bool
766 varpool_for_node_and_aliases (struct varpool_node *node,
767                               bool (*callback) (struct varpool_node *, void *),
768                               void *data,
769                               bool include_overwritable)
770 {
771   int i;
772   struct ipa_ref *ref;
773
774   if (callback (node, data))
775     return true;
776   for (i = 0; ipa_ref_list_refering_iterate (&node->ref_list, i, ref); i++)
777     if (ref->use == IPA_REF_ALIAS)
778       {
779         struct varpool_node *alias = ipa_ref_refering_varpool_node (ref);
780         if (include_overwritable
781             || cgraph_variable_initializer_availability (alias) > AVAIL_OVERWRITABLE)
782           if (varpool_for_node_and_aliases (alias, callback, data,
783                                            include_overwritable))
784             return true;
785       }
786   return false;
787 }
788 #include "gt-varpool.h"