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