2014-07-29 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / varpool.c
blob74117e2d60bd0c2853eacf163ff9f1eb816c51ca
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"
38 #include "tree-ssa-alias.h"
39 #include "gimple.h"
40 #include "lto-streamer.h"
42 const char * const tls_model_names[]={"none", "tls-emulated", "tls-real",
43 "tls-global-dynamic", "tls-local-dynamic",
44 "tls-initial-exec", "tls-local-exec"};
46 /* List of hooks triggered on varpool_node events. */
47 struct varpool_node_hook_list {
48 varpool_node_hook hook;
49 void *data;
50 struct varpool_node_hook_list *next;
53 /* List of hooks triggered when a node is removed. */
54 struct varpool_node_hook_list *first_varpool_node_removal_hook;
55 /* List of hooks triggered when an variable is inserted. */
56 struct varpool_node_hook_list *first_varpool_variable_insertion_hook;
58 /* Register HOOK to be called with DATA on each removed node. */
59 struct varpool_node_hook_list *
60 varpool_add_node_removal_hook (varpool_node_hook hook, void *data)
62 struct varpool_node_hook_list *entry;
63 struct varpool_node_hook_list **ptr = &first_varpool_node_removal_hook;
65 entry = (struct varpool_node_hook_list *) xmalloc (sizeof (*entry));
66 entry->hook = hook;
67 entry->data = data;
68 entry->next = NULL;
69 while (*ptr)
70 ptr = &(*ptr)->next;
71 *ptr = entry;
72 return entry;
75 /* Remove ENTRY from the list of hooks called on removing nodes. */
76 void
77 varpool_remove_node_removal_hook (struct varpool_node_hook_list *entry)
79 struct varpool_node_hook_list **ptr = &first_varpool_node_removal_hook;
81 while (*ptr != entry)
82 ptr = &(*ptr)->next;
83 *ptr = entry->next;
84 free (entry);
87 /* Call all node removal hooks. */
88 static void
89 varpool_call_node_removal_hooks (varpool_node *node)
91 struct varpool_node_hook_list *entry = first_varpool_node_removal_hook;
92 while (entry)
94 entry->hook (node, entry->data);
95 entry = entry->next;
99 /* Register HOOK to be called with DATA on each inserted node. */
100 struct varpool_node_hook_list *
101 varpool_add_variable_insertion_hook (varpool_node_hook hook, void *data)
103 struct varpool_node_hook_list *entry;
104 struct varpool_node_hook_list **ptr = &first_varpool_variable_insertion_hook;
106 entry = (struct varpool_node_hook_list *) xmalloc (sizeof (*entry));
107 entry->hook = hook;
108 entry->data = data;
109 entry->next = NULL;
110 while (*ptr)
111 ptr = &(*ptr)->next;
112 *ptr = entry;
113 return entry;
116 /* Remove ENTRY from the list of hooks called on inserted nodes. */
117 void
118 varpool_remove_variable_insertion_hook (struct varpool_node_hook_list *entry)
120 struct varpool_node_hook_list **ptr = &first_varpool_variable_insertion_hook;
122 while (*ptr != entry)
123 ptr = &(*ptr)->next;
124 *ptr = entry->next;
125 free (entry);
128 /* Call all node insertion hooks. */
129 void
130 varpool_call_variable_insertion_hooks (varpool_node *node)
132 struct varpool_node_hook_list *entry = first_varpool_variable_insertion_hook;
133 while (entry)
135 entry->hook (node, entry->data);
136 entry = entry->next;
140 /* Allocate new callgraph node and insert it into basic data structures. */
142 varpool_node *
143 varpool_node::create_empty (void)
145 varpool_node *node = ggc_cleared_alloc<varpool_node> ();
146 node->type = SYMTAB_VARIABLE;
147 return node;
150 /* Return varpool node assigned to DECL. Create new one when needed. */
151 varpool_node *
152 varpool_node::get_create (tree decl)
154 varpool_node *node = varpool_node::get (decl);
155 gcc_checking_assert (TREE_CODE (decl) == VAR_DECL);
156 if (node)
157 return node;
159 node = varpool_node::create_empty ();
160 node->decl = decl;
161 node->register_symbol ();
162 return node;
165 /* Remove variable from symbol table. */
167 void
168 varpool_node::remove (void)
170 varpool_call_node_removal_hooks (this);
171 unregister ();
173 /* When streaming we can have multiple nodes associated with decl. */
174 if (cgraph_state == CGRAPH_LTO_STREAMING)
176 /* Keep constructor when it may be used for folding. We remove
177 references to external variables before final compilation. */
178 else if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node
179 && !ctor_useable_for_folding_p ())
180 remove_initializer ();
181 ggc_free (this);
184 /* Remove node initializer when it is no longer needed. */
185 void
186 varpool_node::remove_initializer (void)
188 if (DECL_INITIAL (decl)
189 && !DECL_IN_CONSTANT_POOL (decl)
190 /* Keep vtables for BINFO folding. */
191 && !DECL_VIRTUAL_P (decl)
192 /* FIXME: http://gcc.gnu.org/PR55395 */
193 && debug_info_level == DINFO_LEVEL_NONE
194 /* When doing declaration merging we have duplicate
195 entries for given decl. Do not attempt to remove
196 the boides, or we will end up remiving
197 wrong one. */
198 && cgraph_state != CGRAPH_LTO_STREAMING)
199 DECL_INITIAL (decl) = error_mark_node;
202 /* Dump given varpool node to F. */
203 void
204 varpool_node::dump (FILE *f)
206 dump_base (f);
207 fprintf (f, " Availability: %s\n",
208 cgraph_function_flags_ready
209 ? cgraph_availability_names[get_availability ()]
210 : "not-ready");
211 fprintf (f, " Varpool flags:");
212 if (DECL_INITIAL (decl))
213 fprintf (f, " initialized");
214 if (output)
215 fprintf (f, " output");
216 if (used_by_single_function)
217 fprintf (f, " used-by-single-function");
218 if (TREE_READONLY (decl))
219 fprintf (f, " read-only");
220 if (ctor_useable_for_folding_p ())
221 fprintf (f, " const-value-known");
222 if (writeonly)
223 fprintf (f, " write-only");
224 if (tls_model)
225 fprintf (f, " %s", tls_model_names [tls_model]);
226 fprintf (f, "\n");
230 /* Dump given varpool node to stderr. */
231 void varpool_node::debug (void)
233 varpool_node::dump (stderr);
236 /* Dump the variable pool to F. */
237 void
238 varpool_node::dump_varpool (FILE *f)
240 varpool_node *node;
242 fprintf (f, "variable pool:\n\n");
243 FOR_EACH_VARIABLE (node)
244 node->dump (f);
247 /* Dump the variable pool to stderr. */
249 DEBUG_FUNCTION void
250 varpool_node::debug_varpool (void)
252 dump_varpool (stderr);
255 /* Given an assembler name, lookup node. */
256 varpool_node *
257 varpool_node::get_for_asmname (tree asmname)
259 if (symtab_node *node = symtab_node_for_asm (asmname))
260 return dyn_cast <varpool_node *> (node);
261 else
262 return NULL;
265 /* When doing LTO, read variable's constructor from disk if
266 it is not already present. */
268 tree
269 varpool_node::get_constructor (void)
271 struct lto_file_decl_data *file_data;
272 const char *data, *name;
273 size_t len;
275 if (DECL_INITIAL (decl) != error_mark_node
276 || !in_lto_p)
277 return DECL_INITIAL (decl);
279 timevar_push (TV_IPA_LTO_CTORS_IN);
281 file_data = lto_file_data;
282 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
284 /* We may have renamed the declaration, e.g., a static function. */
285 name = lto_get_decl_name_mapping (file_data, name);
287 data = lto_get_section_data (file_data, LTO_section_function_body,
288 name, &len);
289 if (!data)
290 fatal_error ("%s: section %s is missing",
291 file_data->file_name,
292 name);
294 lto_input_variable_constructor (file_data, this, data);
295 lto_stats.num_function_bodies++;
296 lto_free_section_data (file_data, LTO_section_function_body, name,
297 data, len);
298 lto_free_function_in_decl_state_for_node (this);
299 timevar_pop (TV_IPA_LTO_CTORS_IN);
300 return DECL_INITIAL (decl);
303 /* Return true if variable has constructor that can be used for folding. */
305 bool
306 varpool_node::ctor_useable_for_folding_p (void)
308 varpool_node *real_node = this;
310 if (real_node->alias && real_node->definition)
311 real_node = ultimate_alias_target ();
313 if (TREE_CODE (decl) == CONST_DECL
314 || DECL_IN_CONSTANT_POOL (decl))
315 return true;
316 if (TREE_THIS_VOLATILE (decl))
317 return false;
319 /* If we do not have a constructor, we can't use it. */
320 if (DECL_INITIAL (real_node->decl) == error_mark_node
321 && !real_node->lto_file_data)
322 return false;
324 /* Vtables are defined by their types and must match no matter of interposition
325 rules. */
326 if (DECL_VIRTUAL_P (decl))
328 /* The C++ front end creates VAR_DECLs for vtables of typeinfo
329 classes not defined in the current TU so that it can refer
330 to them from typeinfo objects. Avoid returning NULL_TREE. */
331 return DECL_INITIAL (real_node->decl) != NULL;
334 /* Alias of readonly variable is also readonly, since the variable is stored
335 in readonly memory. We also accept readonly aliases of non-readonly
336 locations assuming that user knows what he is asking for. */
337 if (!TREE_READONLY (decl) && !TREE_READONLY (real_node->decl))
338 return false;
340 /* Variables declared 'const' without an initializer
341 have zero as the initializer if they may not be
342 overridden at link or run time. */
343 if (!DECL_INITIAL (real_node->decl)
344 && (DECL_EXTERNAL (decl) || decl_replaceable_p (decl)))
345 return false;
347 /* Variables declared `const' with an initializer are considered
348 to not be overwritable with different initializer by default.
350 ??? Previously we behaved so for scalar variables but not for array
351 accesses. */
352 return true;
355 /* If DECLARATION is constant variable and its initial value is known
356 (so we can do constant folding), return its constructor (DECL_INITIAL).
357 This may be an expression or NULL when DECL is initialized to 0.
358 Return ERROR_MARK_NODE otherwise.
360 In LTO this may actually trigger reading the constructor from disk.
361 For this reason varpool_ctor_useable_for_folding_p should be used when
362 the actual constructor value is not needed. */
364 tree
365 ctor_for_folding (tree decl)
367 varpool_node *node, *real_node;
368 tree real_decl;
370 if (TREE_CODE (decl) != VAR_DECL
371 && TREE_CODE (decl) != CONST_DECL)
372 return error_mark_node;
374 if (TREE_CODE (decl) == CONST_DECL
375 || DECL_IN_CONSTANT_POOL (decl))
376 return DECL_INITIAL (decl);
378 if (TREE_THIS_VOLATILE (decl))
379 return error_mark_node;
381 /* Do not care about automatic variables. Those are never initialized
382 anyway, because gimplifier exapnds the code. */
383 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
385 gcc_assert (!TREE_PUBLIC (decl));
386 return error_mark_node;
389 gcc_assert (TREE_CODE (decl) == VAR_DECL);
391 real_node = node = varpool_node::get (decl);
392 if (node)
394 real_node = node->ultimate_alias_target ();
395 real_decl = real_node->decl;
397 else
398 real_decl = decl;
400 /* See if we are dealing with alias.
401 In most cases alias is just alternative symbol pointing to a given
402 constructor. This allows us to use interposition rules of DECL
403 constructor of REAL_NODE. However weakrefs are special by being just
404 alternative name of their target (if defined). */
405 if (decl != real_decl)
407 gcc_assert (!DECL_INITIAL (decl)
408 || DECL_INITIAL (decl) == error_mark_node);
409 if (node->weakref)
411 node = node->get_alias_target ();
412 decl = node->decl;
416 if ((!DECL_VIRTUAL_P (real_decl)
417 || DECL_INITIAL (real_decl) == error_mark_node
418 || !DECL_INITIAL (real_decl))
419 && (!node || !node->ctor_useable_for_folding_p ()))
420 return error_mark_node;
422 /* OK, we can return constructor. See if we need to fetch it from disk
423 in LTO mode. */
424 if (DECL_INITIAL (real_decl) != error_mark_node
425 || !in_lto_p)
426 return DECL_INITIAL (real_decl);
427 return real_node->get_constructor ();
430 /* Add the variable DECL to the varpool.
431 Unlike varpool_finalize_decl function is intended to be used
432 by middle end and allows insertion of new variable at arbitrary point
433 of compilation. */
434 void
435 varpool_add_new_variable (tree decl)
437 varpool_node *node;
438 varpool_node::finalize_decl (decl);
439 node = varpool_node::get_create (decl);
440 varpool_call_variable_insertion_hooks (node);
441 if (node->externally_visible_p ())
442 node->externally_visible = true;
445 /* Return variable availability. See cgraph.h for description of individual
446 return values. */
447 enum availability
448 varpool_node::get_availability (void)
450 if (!definition)
451 return AVAIL_NOT_AVAILABLE;
452 if (!TREE_PUBLIC (decl))
453 return AVAIL_AVAILABLE;
454 if (DECL_IN_CONSTANT_POOL (decl)
455 || DECL_VIRTUAL_P (decl))
456 return AVAIL_AVAILABLE;
457 if (alias && weakref)
459 enum availability avail;
461 ultimate_alias_target (&avail)->get_availability ();
462 return avail;
464 /* If the variable can be overwritten, return OVERWRITABLE. Takes
465 care of at least one notable extension - the COMDAT variables
466 used to share template instantiations in C++. */
467 if (decl_replaceable_p (decl)
468 || DECL_EXTERNAL (decl))
469 return AVAIL_INTERPOSABLE;
470 return AVAIL_AVAILABLE;
473 void
474 varpool_node::analyze (void)
476 /* When reading back varpool at LTO time, we re-construct the queue in order
477 to have "needed" list right by inserting all needed nodes into varpool.
478 We however don't want to re-analyze already analyzed nodes. */
479 if (!analyzed)
481 gcc_assert (!in_lto_p || cgraph_function_flags_ready);
482 /* Compute the alignment early so function body expanders are
483 already informed about increased alignment. */
484 align_variable (decl, 0);
486 if (alias)
487 resolve_alias (varpool_node::get (alias_target));
488 else if (DECL_INITIAL (decl))
489 record_references_in_initializer (decl, analyzed);
490 analyzed = true;
493 /* Assemble thunks and aliases associated to varpool node. */
495 void
496 varpool_node::assemble_aliases (void)
498 struct ipa_ref *ref;
500 FOR_EACH_ALIAS (this, ref)
502 varpool_node *alias = dyn_cast <varpool_node *> (ref->referring);
503 do_assemble_alias (alias->decl,
504 DECL_ASSEMBLER_NAME (decl));
505 alias->assemble_aliases ();
509 /* Output one variable, if necessary. Return whether we output it. */
511 bool
512 varpool_node::assemble_decl (void)
514 /* Aliases are outout when their target is produced or by
515 output_weakrefs. */
516 if (alias)
517 return false;
519 /* Constant pool is output from RTL land when the reference
520 survive till this level. */
521 if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl))
522 return false;
524 /* Decls with VALUE_EXPR should not be in the varpool at all. They
525 are not real variables, but just info for debugging and codegen.
526 Unfortunately at the moment emutls is not updating varpool correctly
527 after turning real vars into value_expr vars. */
528 if (DECL_HAS_VALUE_EXPR_P (decl)
529 && !targetm.have_tls)
530 return false;
532 /* Hard register vars do not need to be output. */
533 if (DECL_HARD_REGISTER (decl))
534 return false;
536 gcc_checking_assert (!TREE_ASM_WRITTEN (decl)
537 && TREE_CODE (decl) == VAR_DECL
538 && !DECL_HAS_VALUE_EXPR_P (decl));
540 if (!in_other_partition
541 && !DECL_EXTERNAL (decl))
543 get_constructor ();
544 assemble_variable (decl, 0, 1, 0);
545 gcc_assert (TREE_ASM_WRITTEN (decl));
546 gcc_assert (definition);
547 assemble_aliases ();
548 return true;
551 return false;
554 /* Add NODE to queue starting at FIRST.
555 The queue is linked via AUX pointers and terminated by pointer to 1. */
557 static void
558 enqueue_node (varpool_node *node, varpool_node **first)
560 if (node->aux)
561 return;
562 gcc_checking_assert (*first);
563 node->aux = *first;
564 *first = node;
567 /* Optimization of function bodies might've rendered some variables as
568 unnecessary so we want to avoid these from being compiled. Re-do
569 reachability starting from variables that are either externally visible
570 or was referred from the asm output routines. */
572 static void
573 varpool_remove_unreferenced_decls (void)
575 varpool_node *next, *node;
576 varpool_node *first = (varpool_node *)(void *)1;
577 int i;
578 struct ipa_ref *ref = NULL;
579 struct pointer_set_t *referenced = pointer_set_create ();
581 if (seen_error ())
582 return;
584 if (cgraph_dump_file)
585 fprintf (cgraph_dump_file, "Trivially needed variables:");
586 FOR_EACH_DEFINED_VARIABLE (node)
588 if (node->analyzed
589 && (!node->can_remove_if_no_refs_p ()
590 /* We just expanded all function bodies. See if any of
591 them needed the variable. */
592 || DECL_RTL_SET_P (node->decl)))
594 enqueue_node (node, &first);
595 if (cgraph_dump_file)
596 fprintf (cgraph_dump_file, " %s", node->asm_name ());
599 while (first != (varpool_node *)(void *)1)
601 node = first;
602 first = (varpool_node *)first->aux;
604 if (node->same_comdat_group)
606 symtab_node *next;
607 for (next = node->same_comdat_group;
608 next != node;
609 next = next->same_comdat_group)
611 varpool_node *vnext = dyn_cast <varpool_node *> (next);
612 if (vnext && vnext->analyzed && !next->comdat_local_p ())
613 enqueue_node (vnext, &first);
616 for (i = 0; node->iterate_reference (i, ref); i++)
618 varpool_node *vnode = dyn_cast <varpool_node *> (ref->referred);
619 if (vnode
620 && !vnode->in_other_partition
621 && (!DECL_EXTERNAL (ref->referred->decl)
622 || vnode->alias)
623 && vnode->analyzed)
624 enqueue_node (vnode, &first);
625 else
626 pointer_set_insert (referenced, node);
629 if (cgraph_dump_file)
630 fprintf (cgraph_dump_file, "\nRemoving variables:");
631 for (node = varpool_first_defined_variable (); node; node = next)
633 next = varpool_next_defined_variable (node);
634 if (!node->aux)
636 if (cgraph_dump_file)
637 fprintf (cgraph_dump_file, " %s", node->asm_name ());
638 if (pointer_set_contains (referenced, node))
639 node->remove_initializer ();
640 else
641 node->remove ();
644 pointer_set_destroy (referenced);
645 if (cgraph_dump_file)
646 fprintf (cgraph_dump_file, "\n");
649 /* For variables in named sections make sure get_variable_section
650 is called before we switch to those sections. Then section
651 conflicts between read-only and read-only requiring relocations
652 sections can be resolved. */
653 void
654 varpool_node::finalize_named_section_flags (void)
656 if (!TREE_ASM_WRITTEN (decl)
657 && !alias
658 && !in_other_partition
659 && !DECL_EXTERNAL (decl)
660 && TREE_CODE (decl) == VAR_DECL
661 && !DECL_HAS_VALUE_EXPR_P (decl)
662 && get_section ())
663 get_variable_section (decl, false);
666 /* Output all variables enqueued to be assembled. */
667 bool
668 varpool_node::output_variables (void)
670 bool changed = false;
671 varpool_node *node;
673 if (seen_error ())
674 return false;
676 varpool_remove_unreferenced_decls ();
678 timevar_push (TV_VAROUT);
680 FOR_EACH_DEFINED_VARIABLE (node)
681 node->finalize_named_section_flags ();
683 FOR_EACH_DEFINED_VARIABLE (node)
684 if (node->assemble_decl ())
685 changed = true;
686 timevar_pop (TV_VAROUT);
687 return changed;
690 /* Create a new global variable of type TYPE. */
691 tree
692 add_new_static_var (tree type)
694 tree new_decl;
695 varpool_node *new_node;
697 new_decl = create_tmp_var_raw (type, NULL);
698 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
699 TREE_READONLY (new_decl) = 0;
700 TREE_STATIC (new_decl) = 1;
701 TREE_USED (new_decl) = 1;
702 DECL_CONTEXT (new_decl) = NULL_TREE;
703 DECL_ABSTRACT (new_decl) = 0;
704 lang_hooks.dup_lang_specific_decl (new_decl);
705 new_node = varpool_node::get_create (new_decl);
706 varpool_node::finalize_decl (new_decl);
708 return new_node->decl;
711 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
712 Extra name aliases are output whenever DECL is output. */
714 varpool_node *
715 varpool_node::create_alias (tree alias, tree decl)
717 varpool_node *alias_node;
719 gcc_assert (TREE_CODE (decl) == VAR_DECL);
720 gcc_assert (TREE_CODE (alias) == VAR_DECL);
721 alias_node = varpool_node::get_create (alias);
722 alias_node->alias = true;
723 alias_node->definition = true;
724 alias_node->alias_target = decl;
725 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
726 alias_node->weakref = true;
727 return alias_node;
730 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
731 Extra name aliases are output whenever DECL is output. */
733 varpool_node *
734 varpool_node::create_extra_name_alias (tree alias, tree decl)
736 varpool_node *alias_node;
738 #ifndef ASM_OUTPUT_DEF
739 /* If aliases aren't supported by the assembler, fail. */
740 return NULL;
741 #endif
742 alias_node = varpool_node::create_alias (alias, decl);
743 alias_node->cpp_implicit_alias = true;
745 /* Extra name alias mechanizm creates aliases really late
746 via DECL_ASSEMBLER_NAME mechanizm.
747 This is unfortunate because they are not going through the
748 standard channels. Ensure they get output. */
749 if (cpp_implicit_aliases_done)
750 alias_node->resolve_alias (varpool_node::get_create (decl));
751 return alias_node;
754 /* Call calback on varpool symbol and aliases associated to varpool symbol.
755 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
756 skipped. */
758 bool
759 varpool_node::call_for_node_and_aliases (bool (*callback) (varpool_node *,
760 void *),
761 void *data,
762 bool include_overwritable)
764 struct ipa_ref *ref;
766 if (callback (this, data))
767 return true;
769 FOR_EACH_ALIAS (this, ref)
771 varpool_node *alias = dyn_cast <varpool_node *> (ref->referring);
772 if (include_overwritable
773 || alias->get_availability () > AVAIL_INTERPOSABLE)
774 if (alias->call_for_node_and_aliases (callback, data,
775 include_overwritable))
776 return true;
778 return false;