Use rtx_insn for various target.def hooks
[official-gcc.git] / gcc / varpool.c
blob72971d61b53e70cacb466a280e0569484b633dd0
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 "tree-ssa-alias.h"
38 #include "gimple.h"
39 #include "lto-streamer.h"
40 #include "hash-set.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 /* Register HOOK to be called with DATA on each removed node. */
54 varpool_node_hook_list *
55 symbol_table::add_varpool_removal_hook (varpool_node_hook hook, void *data)
57 varpool_node_hook_list *entry;
58 varpool_node_hook_list **ptr = &m_first_varpool_removal_hook;
60 entry = (varpool_node_hook_list *) xmalloc (sizeof (*entry));
61 entry->hook = hook;
62 entry->data = data;
63 entry->next = NULL;
64 while (*ptr)
65 ptr = &(*ptr)->next;
66 *ptr = entry;
67 return entry;
70 /* Remove ENTRY from the list of hooks called on removing nodes. */
71 void
72 symbol_table::remove_varpool_removal_hook (varpool_node_hook_list *entry)
74 varpool_node_hook_list **ptr = &m_first_varpool_removal_hook;
76 while (*ptr != entry)
77 ptr = &(*ptr)->next;
78 *ptr = entry->next;
79 free (entry);
82 /* Call all node removal hooks. */
83 void
84 symbol_table::call_varpool_removal_hooks (varpool_node *node)
86 varpool_node_hook_list *entry = m_first_varpool_removal_hook;
87 while (entry)
89 entry->hook (node, entry->data);
90 entry = entry->next;
94 /* Register HOOK to be called with DATA on each inserted node. */
95 varpool_node_hook_list *
96 symbol_table::add_varpool_insertion_hook (varpool_node_hook hook, void *data)
98 varpool_node_hook_list *entry;
99 varpool_node_hook_list **ptr = &m_first_varpool_insertion_hook;
101 entry = (varpool_node_hook_list *) xmalloc (sizeof (*entry));
102 entry->hook = hook;
103 entry->data = data;
104 entry->next = NULL;
105 while (*ptr)
106 ptr = &(*ptr)->next;
107 *ptr = entry;
108 return entry;
111 /* Remove ENTRY from the list of hooks called on inserted nodes. */
112 void
113 symbol_table::remove_varpool_insertion_hook (varpool_node_hook_list *entry)
115 varpool_node_hook_list **ptr = &m_first_varpool_insertion_hook;
117 while (*ptr != entry)
118 ptr = &(*ptr)->next;
119 *ptr = entry->next;
120 free (entry);
123 /* Call all node insertion hooks. */
124 void
125 symbol_table::call_varpool_insertion_hooks (varpool_node *node)
127 varpool_node_hook_list *entry = m_first_varpool_insertion_hook;
128 while (entry)
130 entry->hook (node, entry->data);
131 entry = entry->next;
135 /* Allocate new callgraph node and insert it into basic data structures. */
137 varpool_node *
138 varpool_node::create_empty (void)
140 varpool_node *node = ggc_cleared_alloc<varpool_node> ();
141 node->type = SYMTAB_VARIABLE;
142 return node;
145 /* Return varpool node assigned to DECL. Create new one when needed. */
146 varpool_node *
147 varpool_node::get_create (tree decl)
149 varpool_node *node = varpool_node::get (decl);
150 gcc_checking_assert (TREE_CODE (decl) == VAR_DECL);
151 if (node)
152 return node;
154 node = varpool_node::create_empty ();
155 node->decl = decl;
156 node->register_symbol ();
157 return node;
160 /* Remove variable from symbol table. */
162 void
163 varpool_node::remove (void)
165 symtab->call_varpool_removal_hooks (this);
166 unregister ();
168 /* When streaming we can have multiple nodes associated with decl. */
169 if (symtab->state == LTO_STREAMING)
171 /* Keep constructor when it may be used for folding. We remove
172 references to external variables before final compilation. */
173 else if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node
174 && !ctor_useable_for_folding_p ())
175 remove_initializer ();
176 ggc_free (this);
179 /* Remove node initializer when it is no longer needed. */
180 void
181 varpool_node::remove_initializer (void)
183 if (DECL_INITIAL (decl)
184 && !DECL_IN_CONSTANT_POOL (decl)
185 /* Keep vtables for BINFO folding. */
186 && !DECL_VIRTUAL_P (decl)
187 /* FIXME: http://gcc.gnu.org/PR55395 */
188 && debug_info_level == DINFO_LEVEL_NONE
189 /* When doing declaration merging we have duplicate
190 entries for given decl. Do not attempt to remove
191 the boides, or we will end up remiving
192 wrong one. */
193 && symtab->state != LTO_STREAMING)
194 DECL_INITIAL (decl) = error_mark_node;
197 /* Dump given varpool node to F. */
198 void
199 varpool_node::dump (FILE *f)
201 dump_base (f);
202 fprintf (f, " Availability: %s\n",
203 symtab->function_flags_ready
204 ? cgraph_availability_names[get_availability ()]
205 : "not-ready");
206 fprintf (f, " Varpool flags:");
207 if (DECL_INITIAL (decl))
208 fprintf (f, " initialized");
209 if (output)
210 fprintf (f, " output");
211 if (used_by_single_function)
212 fprintf (f, " used-by-single-function");
213 if (TREE_READONLY (decl))
214 fprintf (f, " read-only");
215 if (ctor_useable_for_folding_p ())
216 fprintf (f, " const-value-known");
217 if (writeonly)
218 fprintf (f, " write-only");
219 if (tls_model)
220 fprintf (f, " %s", tls_model_names [tls_model]);
221 fprintf (f, "\n");
225 /* Dump given varpool node to stderr. */
226 void varpool_node::debug (void)
228 varpool_node::dump (stderr);
231 /* Dump the variable pool to F. */
232 void
233 varpool_node::dump_varpool (FILE *f)
235 varpool_node *node;
237 fprintf (f, "variable pool:\n\n");
238 FOR_EACH_VARIABLE (node)
239 node->dump (f);
242 /* Dump the variable pool to stderr. */
244 DEBUG_FUNCTION void
245 varpool_node::debug_varpool (void)
247 dump_varpool (stderr);
250 /* Given an assembler name, lookup node. */
251 varpool_node *
252 varpool_node::get_for_asmname (tree asmname)
254 if (symtab_node *node = symtab_node::get_for_asmname (asmname))
255 return dyn_cast <varpool_node *> (node);
256 else
257 return NULL;
260 /* When doing LTO, read variable's constructor from disk if
261 it is not already present. */
263 tree
264 varpool_node::get_constructor (void)
266 lto_file_decl_data *file_data;
267 const char *data, *name;
268 size_t len;
270 if (DECL_INITIAL (decl) != error_mark_node
271 || !in_lto_p)
272 return DECL_INITIAL (decl);
274 timevar_push (TV_IPA_LTO_CTORS_IN);
276 file_data = lto_file_data;
277 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
279 /* We may have renamed the declaration, e.g., a static function. */
280 name = lto_get_decl_name_mapping (file_data, name);
282 data = lto_get_section_data (file_data, LTO_section_function_body,
283 name, &len);
284 if (!data)
285 fatal_error ("%s: section %s is missing",
286 file_data->file_name,
287 name);
289 lto_input_variable_constructor (file_data, this, data);
290 lto_stats.num_function_bodies++;
291 lto_free_section_data (file_data, LTO_section_function_body, name,
292 data, len);
293 lto_free_function_in_decl_state_for_node (this);
294 timevar_pop (TV_IPA_LTO_CTORS_IN);
295 return DECL_INITIAL (decl);
298 /* Return true if variable has constructor that can be used for folding. */
300 bool
301 varpool_node::ctor_useable_for_folding_p (void)
303 varpool_node *real_node = this;
305 if (real_node->alias && real_node->definition)
306 real_node = ultimate_alias_target ();
308 if (TREE_CODE (decl) == CONST_DECL
309 || DECL_IN_CONSTANT_POOL (decl))
310 return true;
311 if (TREE_THIS_VOLATILE (decl))
312 return false;
314 /* If we do not have a constructor, we can't use it. */
315 if (DECL_INITIAL (real_node->decl) == error_mark_node
316 && !real_node->lto_file_data)
317 return false;
319 /* Vtables are defined by their types and must match no matter of interposition
320 rules. */
321 if (DECL_VIRTUAL_P (decl))
323 /* The C++ front end creates VAR_DECLs for vtables of typeinfo
324 classes not defined in the current TU so that it can refer
325 to them from typeinfo objects. Avoid returning NULL_TREE. */
326 return DECL_INITIAL (real_node->decl) != NULL;
329 /* Alias of readonly variable is also readonly, since the variable is stored
330 in readonly memory. We also accept readonly aliases of non-readonly
331 locations assuming that user knows what he is asking for. */
332 if (!TREE_READONLY (decl) && !TREE_READONLY (real_node->decl))
333 return false;
335 /* Variables declared 'const' without an initializer
336 have zero as the initializer if they may not be
337 overridden at link or run time.
339 It is actually requirement for C++ compiler to optimize const variables
340 consistently. As a GNU extension, do not enfore this rule for user defined
341 weak variables, so we support interposition on:
342 static const int dummy = 0;
343 extern const int foo __attribute__((__weak__, __alias__("dummy")));
345 if ((!DECL_INITIAL (real_node->decl)
346 || (DECL_WEAK (decl) && !DECL_COMDAT (decl)))
347 && (DECL_EXTERNAL (decl) || decl_replaceable_p (decl)))
348 return false;
350 /* Variables declared `const' with an initializer are considered
351 to not be overwritable with different initializer by default.
353 ??? Previously we behaved so for scalar variables but not for array
354 accesses. */
355 return true;
358 /* If DECLARATION is constant variable and its initial value is known
359 (so we can do constant folding), return its constructor (DECL_INITIAL).
360 This may be an expression or NULL when DECL is initialized to 0.
361 Return ERROR_MARK_NODE otherwise.
363 In LTO this may actually trigger reading the constructor from disk.
364 For this reason varpool_ctor_useable_for_folding_p should be used when
365 the actual constructor value is not needed. */
367 tree
368 ctor_for_folding (tree decl)
370 varpool_node *node, *real_node;
371 tree real_decl;
373 if (TREE_CODE (decl) != VAR_DECL
374 && TREE_CODE (decl) != CONST_DECL)
375 return error_mark_node;
377 if (TREE_CODE (decl) == CONST_DECL
378 || DECL_IN_CONSTANT_POOL (decl))
379 return DECL_INITIAL (decl);
381 if (TREE_THIS_VOLATILE (decl))
382 return error_mark_node;
384 /* Do not care about automatic variables. Those are never initialized
385 anyway, because gimplifier exapnds the code. */
386 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
388 gcc_assert (!TREE_PUBLIC (decl));
389 return error_mark_node;
392 gcc_assert (TREE_CODE (decl) == VAR_DECL);
394 real_node = node = varpool_node::get (decl);
395 if (node)
397 real_node = node->ultimate_alias_target ();
398 real_decl = real_node->decl;
400 else
401 real_decl = decl;
403 /* See if we are dealing with alias.
404 In most cases alias is just alternative symbol pointing to a given
405 constructor. This allows us to use interposition rules of DECL
406 constructor of REAL_NODE. However weakrefs are special by being just
407 alternative name of their target (if defined). */
408 if (decl != real_decl)
410 gcc_assert (!DECL_INITIAL (decl)
411 || (node->alias && node->get_alias_target () == real_node)
412 || DECL_INITIAL (decl) == error_mark_node);
413 if (node->weakref)
415 node = node->get_alias_target ();
416 decl = node->decl;
420 if ((!DECL_VIRTUAL_P (real_decl)
421 || DECL_INITIAL (real_decl) == error_mark_node
422 || !DECL_INITIAL (real_decl))
423 && (!node || !node->ctor_useable_for_folding_p ()))
424 return error_mark_node;
426 /* OK, we can return constructor. See if we need to fetch it from disk
427 in LTO mode. */
428 if (DECL_INITIAL (real_decl) != error_mark_node
429 || !in_lto_p)
430 return DECL_INITIAL (real_decl);
431 return real_node->get_constructor ();
434 /* Add the variable DECL to the varpool.
435 Unlike finalize_decl function is intended to be used
436 by middle end and allows insertion of new variable at arbitrary point
437 of compilation. */
438 void
439 varpool_node::add (tree decl)
441 varpool_node *node;
442 varpool_node::finalize_decl (decl);
443 node = varpool_node::get_create (decl);
444 symtab->call_varpool_insertion_hooks (node);
445 if (node->externally_visible_p ())
446 node->externally_visible = true;
449 /* Return variable availability. See cgraph.h for description of individual
450 return values. */
451 enum availability
452 varpool_node::get_availability (void)
454 if (!definition)
455 return AVAIL_NOT_AVAILABLE;
456 if (!TREE_PUBLIC (decl))
457 return AVAIL_AVAILABLE;
458 if (DECL_IN_CONSTANT_POOL (decl)
459 || DECL_VIRTUAL_P (decl))
460 return AVAIL_AVAILABLE;
461 if (alias && weakref)
463 enum availability avail;
465 ultimate_alias_target (&avail)->get_availability ();
466 return avail;
468 /* If the variable can be overwritten, return OVERWRITABLE. Takes
469 care of at least one notable extension - the COMDAT variables
470 used to share template instantiations in C++. */
471 if (decl_replaceable_p (decl)
472 || DECL_EXTERNAL (decl))
473 return AVAIL_INTERPOSABLE;
474 return AVAIL_AVAILABLE;
477 void
478 varpool_node::analyze (void)
480 /* When reading back varpool at LTO time, we re-construct the queue in order
481 to have "needed" list right by inserting all needed nodes into varpool.
482 We however don't want to re-analyze already analyzed nodes. */
483 if (!analyzed)
485 gcc_assert (!in_lto_p || symtab->function_flags_ready);
486 /* Compute the alignment early so function body expanders are
487 already informed about increased alignment. */
488 align_variable (decl, 0);
490 if (alias)
491 resolve_alias (varpool_node::get (alias_target));
492 else if (DECL_INITIAL (decl))
493 record_references_in_initializer (decl, analyzed);
494 analyzed = true;
497 /* Assemble thunks and aliases associated to varpool node. */
499 void
500 varpool_node::assemble_aliases (void)
502 ipa_ref *ref;
504 FOR_EACH_ALIAS (this, ref)
506 varpool_node *alias = dyn_cast <varpool_node *> (ref->referring);
507 do_assemble_alias (alias->decl,
508 DECL_ASSEMBLER_NAME (decl));
509 alias->assemble_aliases ();
513 /* Output one variable, if necessary. Return whether we output it. */
515 bool
516 varpool_node::assemble_decl (void)
518 /* Aliases are outout when their target is produced or by
519 output_weakrefs. */
520 if (alias)
521 return false;
523 /* Constant pool is output from RTL land when the reference
524 survive till this level. */
525 if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl))
526 return false;
528 /* Decls with VALUE_EXPR should not be in the varpool at all. They
529 are not real variables, but just info for debugging and codegen.
530 Unfortunately at the moment emutls is not updating varpool correctly
531 after turning real vars into value_expr vars. */
532 if (DECL_HAS_VALUE_EXPR_P (decl)
533 && !targetm.have_tls)
534 return false;
536 /* Hard register vars do not need to be output. */
537 if (DECL_HARD_REGISTER (decl))
538 return false;
540 gcc_checking_assert (!TREE_ASM_WRITTEN (decl)
541 && TREE_CODE (decl) == VAR_DECL
542 && !DECL_HAS_VALUE_EXPR_P (decl));
544 if (!in_other_partition
545 && !DECL_EXTERNAL (decl))
547 get_constructor ();
548 assemble_variable (decl, 0, 1, 0);
549 gcc_assert (TREE_ASM_WRITTEN (decl));
550 gcc_assert (definition);
551 assemble_aliases ();
552 return true;
555 return false;
558 /* Add NODE to queue starting at FIRST.
559 The queue is linked via AUX pointers and terminated by pointer to 1. */
561 static void
562 enqueue_node (varpool_node *node, varpool_node **first)
564 if (node->aux)
565 return;
566 gcc_checking_assert (*first);
567 node->aux = *first;
568 *first = node;
571 /* Optimization of function bodies might've rendered some variables as
572 unnecessary so we want to avoid these from being compiled. Re-do
573 reachability starting from variables that are either externally visible
574 or was referred from the asm output routines. */
576 void
577 symbol_table::remove_unreferenced_decls (void)
579 varpool_node *next, *node;
580 varpool_node *first = (varpool_node *)(void *)1;
581 int i;
582 ipa_ref *ref = NULL;
583 hash_set<varpool_node *> referenced;
585 if (seen_error ())
586 return;
588 if (dump_file)
589 fprintf (dump_file, "Trivially needed variables:");
590 FOR_EACH_DEFINED_VARIABLE (node)
592 if (node->analyzed
593 && (!node->can_remove_if_no_refs_p ()
594 /* We just expanded all function bodies. See if any of
595 them needed the variable. */
596 || DECL_RTL_SET_P (node->decl)))
598 enqueue_node (node, &first);
599 if (dump_file)
600 fprintf (dump_file, " %s", node->asm_name ());
603 while (first != (varpool_node *)(void *)1)
605 node = first;
606 first = (varpool_node *)first->aux;
608 if (node->same_comdat_group)
610 symtab_node *next;
611 for (next = node->same_comdat_group;
612 next != node;
613 next = next->same_comdat_group)
615 varpool_node *vnext = dyn_cast <varpool_node *> (next);
616 if (vnext && vnext->analyzed && !next->comdat_local_p ())
617 enqueue_node (vnext, &first);
620 for (i = 0; node->iterate_reference (i, ref); i++)
622 varpool_node *vnode = dyn_cast <varpool_node *> (ref->referred);
623 if (vnode
624 && !vnode->in_other_partition
625 && (!DECL_EXTERNAL (ref->referred->decl)
626 || vnode->alias)
627 && vnode->analyzed)
628 enqueue_node (vnode, &first);
629 else
630 referenced.add (node);
633 if (dump_file)
634 fprintf (dump_file, "\nRemoving variables:");
635 for (node = first_defined_variable (); node; node = next)
637 next = next_defined_variable (node);
638 if (!node->aux)
640 if (dump_file)
641 fprintf (dump_file, " %s", node->asm_name ());
642 if (referenced.contains(node))
643 node->remove_initializer ();
644 else
645 node->remove ();
649 if (dump_file)
650 fprintf (dump_file, "\n");
653 /* For variables in named sections make sure get_variable_section
654 is called before we switch to those sections. Then section
655 conflicts between read-only and read-only requiring relocations
656 sections can be resolved. */
657 void
658 varpool_node::finalize_named_section_flags (void)
660 if (!TREE_ASM_WRITTEN (decl)
661 && !alias
662 && !in_other_partition
663 && !DECL_EXTERNAL (decl)
664 && TREE_CODE (decl) == VAR_DECL
665 && !DECL_HAS_VALUE_EXPR_P (decl)
666 && get_section ())
667 get_variable_section (decl, false);
670 /* Output all variables enqueued to be assembled. */
671 bool
672 symbol_table::output_variables (void)
674 bool changed = false;
675 varpool_node *node;
677 if (seen_error ())
678 return false;
680 remove_unreferenced_decls ();
682 timevar_push (TV_VAROUT);
684 FOR_EACH_DEFINED_VARIABLE (node)
685 node->finalize_named_section_flags ();
687 FOR_EACH_DEFINED_VARIABLE (node)
688 if (node->assemble_decl ())
689 changed = true;
690 timevar_pop (TV_VAROUT);
691 return changed;
694 /* Create a new global variable of type TYPE. */
695 tree
696 add_new_static_var (tree type)
698 tree new_decl;
699 varpool_node *new_node;
701 new_decl = create_tmp_var_raw (type, NULL);
702 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
703 TREE_READONLY (new_decl) = 0;
704 TREE_STATIC (new_decl) = 1;
705 TREE_USED (new_decl) = 1;
706 DECL_CONTEXT (new_decl) = NULL_TREE;
707 DECL_ABSTRACT (new_decl) = 0;
708 lang_hooks.dup_lang_specific_decl (new_decl);
709 new_node = varpool_node::get_create (new_decl);
710 varpool_node::finalize_decl (new_decl);
712 return new_node->decl;
715 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
716 Extra name aliases are output whenever DECL is output. */
718 varpool_node *
719 varpool_node::create_alias (tree alias, tree decl)
721 varpool_node *alias_node;
723 gcc_assert (TREE_CODE (decl) == VAR_DECL);
724 gcc_assert (TREE_CODE (alias) == VAR_DECL);
725 alias_node = varpool_node::get_create (alias);
726 alias_node->alias = true;
727 alias_node->definition = true;
728 alias_node->alias_target = decl;
729 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
730 alias_node->weakref = true;
731 return alias_node;
734 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
735 Extra name aliases are output whenever DECL is output. */
737 varpool_node *
738 varpool_node::create_extra_name_alias (tree alias, tree decl)
740 varpool_node *alias_node;
742 #ifndef ASM_OUTPUT_DEF
743 /* If aliases aren't supported by the assembler, fail. */
744 return NULL;
745 #endif
746 alias_node = varpool_node::create_alias (alias, decl);
747 alias_node->cpp_implicit_alias = true;
749 /* Extra name alias mechanizm creates aliases really late
750 via DECL_ASSEMBLER_NAME mechanizm.
751 This is unfortunate because they are not going through the
752 standard channels. Ensure they get output. */
753 if (symtab->cpp_implicit_aliases_done)
754 alias_node->resolve_alias (varpool_node::get_create (decl));
755 return alias_node;
758 /* Call calback on varpool symbol and aliases associated to varpool symbol.
759 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
760 skipped. */
762 bool
763 varpool_node::call_for_node_and_aliases (bool (*callback) (varpool_node *,
764 void *),
765 void *data,
766 bool include_overwritable)
768 ipa_ref *ref;
770 if (callback (this, data))
771 return true;
773 FOR_EACH_ALIAS (this, ref)
775 varpool_node *alias = dyn_cast <varpool_node *> (ref->referring);
776 if (include_overwritable
777 || alias->get_availability () > AVAIL_INTERPOSABLE)
778 if (alias->call_for_node_and_aliases (callback, data,
779 include_overwritable))
780 return true;
782 return false;