Daily bump.
[official-gcc.git] / gcc / varpool.c
blobdc869e2512d18dcd5083cfe7bcff398424d074d8
1 /* Callgraph handling code.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "varasm.h"
27 #include "cgraph.h"
28 #include "langhooks.h"
29 #include "diagnostic-core.h"
30 #include "hashtab.h"
31 #include "timevar.h"
32 #include "debug.h"
33 #include "target.h"
34 #include "output.h"
35 #include "gimple-expr.h"
36 #include "flags.h"
37 #include "pointer-set.h"
39 /* List of hooks triggered on varpool_node events. */
40 struct varpool_node_hook_list {
41 varpool_node_hook hook;
42 void *data;
43 struct varpool_node_hook_list *next;
46 /* List of hooks triggered when a node is removed. */
47 struct varpool_node_hook_list *first_varpool_node_removal_hook;
48 /* List of hooks triggered when an variable is inserted. */
49 struct varpool_node_hook_list *first_varpool_variable_insertion_hook;
51 /* Register HOOK to be called with DATA on each removed node. */
52 struct varpool_node_hook_list *
53 varpool_add_node_removal_hook (varpool_node_hook hook, void *data)
55 struct varpool_node_hook_list *entry;
56 struct varpool_node_hook_list **ptr = &first_varpool_node_removal_hook;
58 entry = (struct varpool_node_hook_list *) xmalloc (sizeof (*entry));
59 entry->hook = hook;
60 entry->data = data;
61 entry->next = NULL;
62 while (*ptr)
63 ptr = &(*ptr)->next;
64 *ptr = entry;
65 return entry;
68 /* Remove ENTRY from the list of hooks called on removing nodes. */
69 void
70 varpool_remove_node_removal_hook (struct varpool_node_hook_list *entry)
72 struct varpool_node_hook_list **ptr = &first_varpool_node_removal_hook;
74 while (*ptr != entry)
75 ptr = &(*ptr)->next;
76 *ptr = entry->next;
77 free (entry);
80 /* Call all node removal hooks. */
81 static void
82 varpool_call_node_removal_hooks (varpool_node *node)
84 struct varpool_node_hook_list *entry = first_varpool_node_removal_hook;
85 while (entry)
87 entry->hook (node, entry->data);
88 entry = entry->next;
92 /* Register HOOK to be called with DATA on each inserted node. */
93 struct varpool_node_hook_list *
94 varpool_add_variable_insertion_hook (varpool_node_hook hook, void *data)
96 struct varpool_node_hook_list *entry;
97 struct varpool_node_hook_list **ptr = &first_varpool_variable_insertion_hook;
99 entry = (struct varpool_node_hook_list *) xmalloc (sizeof (*entry));
100 entry->hook = hook;
101 entry->data = data;
102 entry->next = NULL;
103 while (*ptr)
104 ptr = &(*ptr)->next;
105 *ptr = entry;
106 return entry;
109 /* Remove ENTRY from the list of hooks called on inserted nodes. */
110 void
111 varpool_remove_variable_insertion_hook (struct varpool_node_hook_list *entry)
113 struct varpool_node_hook_list **ptr = &first_varpool_variable_insertion_hook;
115 while (*ptr != entry)
116 ptr = &(*ptr)->next;
117 *ptr = entry->next;
118 free (entry);
121 /* Call all node insertion hooks. */
122 void
123 varpool_call_variable_insertion_hooks (varpool_node *node)
125 struct varpool_node_hook_list *entry = first_varpool_variable_insertion_hook;
126 while (entry)
128 entry->hook (node, entry->data);
129 entry = entry->next;
133 /* Allocate new callgraph node and insert it into basic data structures. */
135 varpool_node *
136 varpool_create_empty_node (void)
138 varpool_node *node = ggc_alloc_cleared_varpool_node ();
139 node->type = SYMTAB_VARIABLE;
140 return node;
143 /* Return varpool node assigned to DECL. Create new one when needed. */
144 varpool_node *
145 varpool_node_for_decl (tree decl)
147 varpool_node *node = varpool_get_node (decl);
148 gcc_checking_assert (TREE_CODE (decl) == VAR_DECL);
149 if (node)
150 return node;
152 node = varpool_create_empty_node ();
153 node->decl = decl;
154 symtab_register_node (node);
155 return node;
158 /* Remove node from the varpool. */
159 void
160 varpool_remove_node (varpool_node *node)
162 tree init;
163 varpool_call_node_removal_hooks (node);
164 symtab_unregister_node (node);
166 /* Because we remove references from external functions before final compilation,
167 we may end up removing useful constructors.
168 FIXME: We probably want to trace boundaries better. */
169 if (cgraph_state == CGRAPH_LTO_STREAMING)
171 else if ((init = ctor_for_folding (node->decl)) == error_mark_node)
172 varpool_remove_initializer (node);
173 else
174 DECL_INITIAL (node->decl) = init;
175 ggc_free (node);
178 /* Renove node initializer when it is no longer needed. */
179 void
180 varpool_remove_initializer (varpool_node *node)
182 if (DECL_INITIAL (node->decl)
183 && !DECL_IN_CONSTANT_POOL (node->decl)
184 /* Keep vtables for BINFO folding. */
185 && !DECL_VIRTUAL_P (node->decl)
186 /* FIXME: http://gcc.gnu.org/PR55395 */
187 && debug_info_level == DINFO_LEVEL_NONE
188 /* When doing declaration merging we have duplicate
189 entries for given decl. Do not attempt to remove
190 the boides, or we will end up remiving
191 wrong one. */
192 && cgraph_state != CGRAPH_LTO_STREAMING)
193 DECL_INITIAL (node->decl) = error_mark_node;
196 /* Dump given cgraph node. */
197 void
198 dump_varpool_node (FILE *f, varpool_node *node)
200 dump_symtab_base (f, node);
201 fprintf (f, " Availability: %s\n",
202 cgraph_function_flags_ready
203 ? cgraph_availability_names[cgraph_variable_initializer_availability (node)]
204 : "not-ready");
205 fprintf (f, " Varpool flags:");
206 if (DECL_INITIAL (node->decl))
207 fprintf (f, " initialized");
208 if (node->output)
209 fprintf (f, " output");
210 if (TREE_READONLY (node->decl))
211 fprintf (f, " read-only");
212 if (ctor_for_folding (node->decl) != error_mark_node)
213 fprintf (f, " const-value-known");
214 fprintf (f, "\n");
217 /* Dump the variable pool. */
218 void
219 dump_varpool (FILE *f)
221 varpool_node *node;
223 fprintf (f, "variable pool:\n\n");
224 FOR_EACH_VARIABLE (node)
225 dump_varpool_node (f, node);
228 /* Dump the variable pool to stderr. */
230 DEBUG_FUNCTION void
231 debug_varpool (void)
233 dump_varpool (stderr);
236 /* Given an assembler name, lookup node. */
237 varpool_node *
238 varpool_node_for_asm (tree asmname)
240 if (symtab_node *node = symtab_node_for_asm (asmname))
241 return dyn_cast <varpool_node> (node);
242 else
243 return NULL;
246 /* Return if DECL is constant and its initial value is known (so we can do
247 constant folding using DECL_INITIAL (decl)).
248 Return ERROR_MARK_NODE when value is unknown. */
250 tree
251 ctor_for_folding (tree decl)
253 varpool_node *node, *real_node;
254 tree real_decl;
256 if (TREE_CODE (decl) != VAR_DECL
257 && TREE_CODE (decl) != CONST_DECL)
258 return error_mark_node;
260 if (TREE_CODE (decl) == CONST_DECL
261 || DECL_IN_CONSTANT_POOL (decl))
262 return DECL_INITIAL (decl);
264 if (TREE_THIS_VOLATILE (decl))
265 return error_mark_node;
267 /* Do not care about automatic variables. Those are never initialized
268 anyway, because gimplifier exapnds the code. */
269 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
271 gcc_assert (!TREE_PUBLIC (decl));
272 return error_mark_node;
275 gcc_assert (TREE_CODE (decl) == VAR_DECL);
277 node = varpool_get_node (decl);
278 if (node)
280 real_node = varpool_variable_node (node);
281 real_decl = real_node->decl;
283 else
284 real_decl = decl;
286 /* See if we are dealing with alias.
287 In most cases alias is just alternative symbol pointing to a given
288 constructor. This allows us to use interposition rules of DECL
289 constructor of REAL_NODE. However weakrefs are special by being just
290 alternative name of their target (if defined). */
291 if (decl != real_decl)
293 gcc_assert (!DECL_INITIAL (decl)
294 || DECL_INITIAL (decl) == error_mark_node);
295 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
297 node = varpool_alias_target (node);
298 decl = node->decl;
302 /* Vtables are defined by their types and must match no matter of interposition
303 rules. */
304 if (DECL_VIRTUAL_P (real_decl))
306 gcc_checking_assert (TREE_READONLY (real_decl));
307 if (DECL_INITIAL (real_decl))
308 return DECL_INITIAL (real_decl);
309 else
311 /* The C++ front end creates VAR_DECLs for vtables of typeinfo
312 classes not defined in the current TU so that it can refer
313 to them from typeinfo objects. Avoid returning NULL_TREE. */
314 gcc_checking_assert (!COMPLETE_TYPE_P (DECL_CONTEXT (real_decl)));
315 return error_mark_node;
319 /* If there is no constructor, we have nothing to do. */
320 if (DECL_INITIAL (real_decl) == error_mark_node)
321 return error_mark_node;
323 /* Non-readonly alias of readonly variable is also de-facto readonly,
324 because the variable itself is in readonly section.
325 We also honnor READONLY flag on alias assuming that user knows
326 what he is doing. */
327 if (!TREE_READONLY (decl) && !TREE_READONLY (real_decl))
328 return error_mark_node;
330 /* Variables declared 'const' without an initializer
331 have zero as the initializer if they may not be
332 overridden at link or run time. */
333 if (!DECL_INITIAL (real_decl)
334 && (DECL_EXTERNAL (decl) || decl_replaceable_p (decl)))
335 return error_mark_node;
337 /* Variables declared `const' with an initializer are considered
338 to not be overwritable with different initializer by default.
340 ??? Previously we behaved so for scalar variables but not for array
341 accesses. */
342 return DECL_INITIAL (real_decl);
345 /* Add the variable DECL to the varpool.
346 Unlike varpool_finalize_decl function is intended to be used
347 by middle end and allows insertion of new variable at arbitrary point
348 of compilation. */
349 void
350 varpool_add_new_variable (tree decl)
352 varpool_node *node;
353 varpool_finalize_decl (decl);
354 node = varpool_node_for_decl (decl);
355 varpool_call_variable_insertion_hooks (node);
356 if (varpool_externally_visible_p (node))
357 node->externally_visible = true;
360 /* Return variable availability. See cgraph.h for description of individual
361 return values. */
362 enum availability
363 cgraph_variable_initializer_availability (varpool_node *node)
365 gcc_assert (cgraph_function_flags_ready);
366 if (!node->definition)
367 return AVAIL_NOT_AVAILABLE;
368 if (!TREE_PUBLIC (node->decl))
369 return AVAIL_AVAILABLE;
370 if (DECL_IN_CONSTANT_POOL (node->decl)
371 || DECL_VIRTUAL_P (node->decl))
372 return AVAIL_AVAILABLE;
373 if (node->alias && node->weakref)
375 enum availability avail;
377 cgraph_variable_initializer_availability
378 (varpool_variable_node (node, &avail));
379 return avail;
381 /* If the variable can be overwritten, return OVERWRITABLE. Takes
382 care of at least one notable extension - the COMDAT variables
383 used to share template instantiations in C++. */
384 if (decl_replaceable_p (node->decl)
385 || DECL_EXTERNAL (node->decl))
386 return AVAIL_OVERWRITABLE;
387 return AVAIL_AVAILABLE;
390 void
391 varpool_analyze_node (varpool_node *node)
393 tree decl = node->decl;
395 /* When reading back varpool at LTO time, we re-construct the queue in order
396 to have "needed" list right by inserting all needed nodes into varpool.
397 We however don't want to re-analyze already analyzed nodes. */
398 if (!node->analyzed)
400 gcc_assert (!in_lto_p || cgraph_function_flags_ready);
401 /* Compute the alignment early so function body expanders are
402 already informed about increased alignment. */
403 align_variable (decl, 0);
405 if (node->alias)
406 symtab_resolve_alias
407 (node, varpool_get_node (node->alias_target));
408 else if (DECL_INITIAL (decl))
409 record_references_in_initializer (decl, node->analyzed);
410 node->analyzed = true;
413 /* Assemble thunks and aliases associated to NODE. */
415 static void
416 assemble_aliases (varpool_node *node)
418 int i;
419 struct ipa_ref *ref;
420 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
421 if (ref->use == IPA_REF_ALIAS)
423 varpool_node *alias = ipa_ref_referring_varpool_node (ref);
424 do_assemble_alias (alias->decl,
425 DECL_ASSEMBLER_NAME (node->decl));
426 assemble_aliases (alias);
430 /* Output one variable, if necessary. Return whether we output it. */
432 bool
433 varpool_assemble_decl (varpool_node *node)
435 tree decl = node->decl;
437 /* Aliases are outout when their target is produced or by
438 output_weakrefs. */
439 if (node->alias)
440 return false;
442 /* Constant pool is output from RTL land when the reference
443 survive till this level. */
444 if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl))
445 return false;
447 /* Decls with VALUE_EXPR should not be in the varpool at all. They
448 are not real variables, but just info for debugging and codegen.
449 Unfortunately at the moment emutls is not updating varpool correctly
450 after turning real vars into value_expr vars. */
451 if (DECL_HAS_VALUE_EXPR_P (decl)
452 && !targetm.have_tls)
453 return false;
455 /* Hard register vars do not need to be output. */
456 if (DECL_HARD_REGISTER (decl))
457 return false;
459 gcc_checking_assert (!TREE_ASM_WRITTEN (decl)
460 && TREE_CODE (decl) == VAR_DECL
461 && !DECL_HAS_VALUE_EXPR_P (decl));
463 if (!node->in_other_partition
464 && !DECL_EXTERNAL (decl))
466 assemble_variable (decl, 0, 1, 0);
467 gcc_assert (TREE_ASM_WRITTEN (decl));
468 node->definition = true;
469 assemble_aliases (node);
470 return true;
473 return false;
476 /* Add NODE to queue starting at FIRST.
477 The queue is linked via AUX pointers and terminated by pointer to 1. */
479 static void
480 enqueue_node (varpool_node *node, varpool_node **first)
482 if (node->aux)
483 return;
484 gcc_checking_assert (*first);
485 node->aux = *first;
486 *first = node;
489 /* Optimization of function bodies might've rendered some variables as
490 unnecessary so we want to avoid these from being compiled. Re-do
491 reachability starting from variables that are either externally visible
492 or was referred from the asm output routines. */
494 static void
495 varpool_remove_unreferenced_decls (void)
497 varpool_node *next, *node;
498 varpool_node *first = (varpool_node *)(void *)1;
499 int i;
500 struct ipa_ref *ref;
501 struct pointer_set_t *referenced = pointer_set_create ();
503 if (seen_error ())
504 return;
506 if (cgraph_dump_file)
507 fprintf (cgraph_dump_file, "Trivially needed variables:");
508 FOR_EACH_DEFINED_VARIABLE (node)
510 if (node->analyzed
511 && (!varpool_can_remove_if_no_refs (node)
512 /* We just expanded all function bodies. See if any of
513 them needed the variable. */
514 || DECL_RTL_SET_P (node->decl)))
516 enqueue_node (node, &first);
517 if (cgraph_dump_file)
518 fprintf (cgraph_dump_file, " %s", node->asm_name ());
521 while (first != (varpool_node *)(void *)1)
523 node = first;
524 first = (varpool_node *)first->aux;
526 if (node->same_comdat_group)
528 symtab_node *next;
529 for (next = node->same_comdat_group;
530 next != node;
531 next = next->same_comdat_group)
533 varpool_node *vnext = dyn_cast <varpool_node> (next);
534 if (vnext && vnext->analyzed && !symtab_comdat_local_p (next))
535 enqueue_node (vnext, &first);
538 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
540 varpool_node *vnode = dyn_cast <varpool_node> (ref->referred);
541 if (vnode
542 && !vnode->in_other_partition
543 && (!DECL_EXTERNAL (ref->referred->decl)
544 || vnode->alias)
545 && vnode->analyzed)
546 enqueue_node (vnode, &first);
547 else
548 pointer_set_insert (referenced, node);
551 if (cgraph_dump_file)
552 fprintf (cgraph_dump_file, "\nRemoving variables:");
553 for (node = varpool_first_defined_variable (); node; node = next)
555 next = varpool_next_defined_variable (node);
556 if (!node->aux)
558 if (cgraph_dump_file)
559 fprintf (cgraph_dump_file, " %s", node->asm_name ());
560 if (pointer_set_contains (referenced, node))
561 varpool_remove_initializer (node);
562 else
563 varpool_remove_node (node);
566 pointer_set_destroy (referenced);
567 if (cgraph_dump_file)
568 fprintf (cgraph_dump_file, "\n");
571 /* For variables in named sections make sure get_variable_section
572 is called before we switch to those sections. Then section
573 conflicts between read-only and read-only requiring relocations
574 sections can be resolved. */
575 void
576 varpool_finalize_named_section_flags (varpool_node *node)
578 if (!TREE_ASM_WRITTEN (node->decl)
579 && !node->alias
580 && !node->in_other_partition
581 && !DECL_EXTERNAL (node->decl)
582 && TREE_CODE (node->decl) == VAR_DECL
583 && !DECL_HAS_VALUE_EXPR_P (node->decl)
584 && DECL_SECTION_NAME (node->decl))
585 get_variable_section (node->decl, false);
588 /* Output all variables enqueued to be assembled. */
589 bool
590 varpool_output_variables (void)
592 bool changed = false;
593 varpool_node *node;
595 if (seen_error ())
596 return false;
598 varpool_remove_unreferenced_decls ();
600 timevar_push (TV_VAROUT);
602 FOR_EACH_DEFINED_VARIABLE (node)
603 varpool_finalize_named_section_flags (node);
605 FOR_EACH_DEFINED_VARIABLE (node)
606 if (varpool_assemble_decl (node))
607 changed = true;
608 timevar_pop (TV_VAROUT);
609 return changed;
612 /* Create a new global variable of type TYPE. */
613 tree
614 add_new_static_var (tree type)
616 tree new_decl;
617 varpool_node *new_node;
619 new_decl = create_tmp_var_raw (type, NULL);
620 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
621 TREE_READONLY (new_decl) = 0;
622 TREE_STATIC (new_decl) = 1;
623 TREE_USED (new_decl) = 1;
624 DECL_CONTEXT (new_decl) = NULL_TREE;
625 DECL_ABSTRACT (new_decl) = 0;
626 lang_hooks.dup_lang_specific_decl (new_decl);
627 new_node = varpool_node_for_decl (new_decl);
628 varpool_finalize_decl (new_decl);
630 return new_node->decl;
633 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
634 Extra name aliases are output whenever DECL is output. */
636 varpool_node *
637 varpool_create_variable_alias (tree alias, tree decl)
639 varpool_node *alias_node;
641 gcc_assert (TREE_CODE (decl) == VAR_DECL);
642 gcc_assert (TREE_CODE (alias) == VAR_DECL);
643 alias_node = varpool_node_for_decl (alias);
644 alias_node->alias = true;
645 alias_node->definition = true;
646 alias_node->alias_target = decl;
647 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
648 alias_node->weakref = true;
649 return alias_node;
652 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
653 Extra name aliases are output whenever DECL is output. */
655 varpool_node *
656 varpool_extra_name_alias (tree alias, tree decl)
658 varpool_node *alias_node;
660 #ifndef ASM_OUTPUT_DEF
661 /* If aliases aren't supported by the assembler, fail. */
662 return NULL;
663 #endif
664 alias_node = varpool_create_variable_alias (alias, decl);
665 alias_node->cpp_implicit_alias = true;
667 /* Extra name alias mechanizm creates aliases really late
668 via DECL_ASSEMBLER_NAME mechanizm.
669 This is unfortunate because they are not going through the
670 standard channels. Ensure they get output. */
671 if (cpp_implicit_aliases_done)
672 symtab_resolve_alias (alias_node,
673 varpool_node_for_decl (decl));
674 return alias_node;
677 /* Call calback on NODE and aliases associated to NODE.
678 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
679 skipped. */
681 bool
682 varpool_for_node_and_aliases (varpool_node *node,
683 bool (*callback) (varpool_node *, void *),
684 void *data,
685 bool include_overwritable)
687 int i;
688 struct ipa_ref *ref;
690 if (callback (node, data))
691 return true;
692 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
693 if (ref->use == IPA_REF_ALIAS)
695 varpool_node *alias = ipa_ref_referring_varpool_node (ref);
696 if (include_overwritable
697 || cgraph_variable_initializer_availability (alias) > AVAIL_OVERWRITABLE)
698 if (varpool_for_node_and_aliases (alias, callback, data,
699 include_overwritable))
700 return true;
702 return false;