* cgraph.h (symtab_node_base): Add definition, alias and analyzed
[official-gcc.git] / gcc / varpool.c
blob1916b76d96031d536ba1dc7557f63f9a096140ba
1 /* Callgraph handling code.
2 Copyright (C) 2003-2013 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 "cgraph.h"
27 #include "langhooks.h"
28 #include "diagnostic-core.h"
29 #include "hashtab.h"
30 #include "ggc.h"
31 #include "timevar.h"
32 #include "debug.h"
33 #include "target.h"
34 #include "output.h"
35 #include "gimple.h"
36 #include "tree-flow.h"
37 #include "flags.h"
39 /* Return varpool node assigned to DECL. Create new one when needed. */
40 struct varpool_node *
41 varpool_node_for_decl (tree decl)
43 struct varpool_node *node = varpool_get_node (decl);
44 gcc_assert (TREE_CODE (decl) == VAR_DECL
45 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl) || in_lto_p));
46 if (node)
47 return node;
49 node = ggc_alloc_cleared_varpool_node ();
50 node->symbol.type = SYMTAB_VARIABLE;
51 node->symbol.decl = decl;
52 symtab_register_node ((symtab_node)node);
53 return node;
56 /* Remove node from the varpool. */
57 void
58 varpool_remove_node (struct varpool_node *node)
60 symtab_unregister_node ((symtab_node)node);
62 /* Because we remove references from external functions before final compilation,
63 we may end up removing useful constructors.
64 FIXME: We probably want to trace boundaries better. */
65 if (!const_value_known_p (node->symbol.decl))
66 varpool_remove_initializer (node);
67 ggc_free (node);
70 /* Renove node initializer when it is no longer needed. */
71 void
72 varpool_remove_initializer (struct varpool_node *node)
74 if (DECL_INITIAL (node->symbol.decl)
75 && !DECL_IN_CONSTANT_POOL (node->symbol.decl)
76 /* Keep vtables for BINFO folding. */
77 && !DECL_VIRTUAL_P (node->symbol.decl)
78 /* FIXME: http://gcc.gnu.org/PR55395 */
79 && debug_info_level == DINFO_LEVEL_NONE)
80 DECL_INITIAL (node->symbol.decl) = error_mark_node;
83 /* Dump given cgraph node. */
84 void
85 dump_varpool_node (FILE *f, struct varpool_node *node)
87 dump_symtab_base (f, (symtab_node)node);
88 fprintf (f, " Availability: %s\n",
89 cgraph_function_flags_ready
90 ? cgraph_availability_names[cgraph_variable_initializer_availability (node)]
91 : "not-ready");
92 fprintf (f, " Varpool flags:");
93 if (DECL_INITIAL (node->symbol.decl))
94 fprintf (f, " initialized");
95 if (node->output)
96 fprintf (f, " output");
97 if (TREE_READONLY (node->symbol.decl))
98 fprintf (f, " read-only");
99 if (const_value_known_p (node->symbol.decl))
100 fprintf (f, " const-value-known");
101 fprintf (f, "\n");
104 /* Dump the variable pool. */
105 void
106 dump_varpool (FILE *f)
108 struct varpool_node *node;
110 fprintf (f, "variable pool:\n\n");
111 FOR_EACH_VARIABLE (node)
112 dump_varpool_node (f, node);
115 /* Dump the variable pool to stderr. */
117 DEBUG_FUNCTION void
118 debug_varpool (void)
120 dump_varpool (stderr);
123 /* Given an assembler name, lookup node. */
124 struct varpool_node *
125 varpool_node_for_asm (tree asmname)
127 if (symtab_node node = symtab_node_for_asm (asmname))
128 return dyn_cast <varpool_node> (node);
129 else
130 return NULL;
133 /* Determine if variable DECL is needed. That is, visible to something
134 either outside this translation unit, something magic in the system
135 configury */
136 bool
137 decide_is_variable_needed (struct varpool_node *node, tree decl)
139 if (DECL_EXTERNAL (decl))
140 return false;
142 /* If the user told us it is used, then it must be so. */
143 if (node->symbol.force_output)
144 return true;
146 /* Externally visible variables must be output. The exception is
147 COMDAT variables that must be output only when they are needed. */
148 if (TREE_PUBLIC (decl)
149 && !DECL_COMDAT (decl))
150 return true;
152 return false;
155 /* Return if DECL is constant and its initial value is known (so we can do
156 constant folding using DECL_INITIAL (decl)). */
158 bool
159 const_value_known_p (tree decl)
161 if (TREE_CODE (decl) != VAR_DECL
162 &&TREE_CODE (decl) != CONST_DECL)
163 return false;
165 if (TREE_CODE (decl) == CONST_DECL
166 || DECL_IN_CONSTANT_POOL (decl))
167 return true;
169 gcc_assert (TREE_CODE (decl) == VAR_DECL);
171 if (!TREE_READONLY (decl) || TREE_THIS_VOLATILE (decl))
172 return false;
174 /* Gimplifier takes away constructors of local vars */
175 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
176 return DECL_INITIAL (decl) != NULL;
178 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
180 /* Variables declared 'const' without an initializer
181 have zero as the initializer if they may not be
182 overridden at link or run time. */
183 if (!DECL_INITIAL (decl)
184 && (DECL_EXTERNAL (decl)
185 || decl_replaceable_p (decl)))
186 return false;
188 /* Variables declared `const' with an initializer are considered
189 to not be overwritable with different initializer by default.
191 ??? Previously we behaved so for scalar variables but not for array
192 accesses. */
193 return true;
196 /* Add the variable DECL to the varpool.
197 Unlike varpool_finalize_decl function is intended to be used
198 by middle end and allows insertion of new variable at arbitrary point
199 of compilation. */
200 void
201 varpool_add_new_variable (tree decl)
203 struct varpool_node *node;
204 varpool_finalize_decl (decl);
205 node = varpool_node_for_decl (decl);
206 if (varpool_externally_visible_p (node))
207 node->symbol.externally_visible = true;
210 /* Return variable availability. See cgraph.h for description of individual
211 return values. */
212 enum availability
213 cgraph_variable_initializer_availability (struct varpool_node *node)
215 gcc_assert (cgraph_function_flags_ready);
216 if (!node->symbol.definition)
217 return AVAIL_NOT_AVAILABLE;
218 if (!TREE_PUBLIC (node->symbol.decl))
219 return AVAIL_AVAILABLE;
220 /* If the variable can be overwritten, return OVERWRITABLE. Takes
221 care of at least one notable extension - the COMDAT variables
222 used to share template instantiations in C++. */
223 if (!decl_replaceable_p (node->symbol.decl))
224 return AVAIL_OVERWRITABLE;
225 return AVAIL_AVAILABLE;
228 void
229 varpool_analyze_node (struct varpool_node *node)
231 tree decl = node->symbol.decl;
233 /* When reading back varpool at LTO time, we re-construct the queue in order
234 to have "needed" list right by inserting all needed nodes into varpool.
235 We however don't want to re-analyze already analyzed nodes. */
236 if (!node->symbol.analyzed)
238 gcc_assert (!in_lto_p || cgraph_function_flags_ready);
239 /* Compute the alignment early so function body expanders are
240 already informed about increased alignment. */
241 align_variable (decl, 0);
243 if (node->symbol.alias && node->alias_of)
245 struct varpool_node *tgt = varpool_node_for_decl (node->alias_of);
246 struct varpool_node *n;
248 for (n = tgt; n && n->symbol.alias;
249 n = n->symbol.analyzed ? varpool_alias_target (n) : NULL)
250 if (n == node)
252 error ("variable %q+D part of alias cycle", node->symbol.decl);
253 node->symbol.alias = false;
254 continue;
256 if (!vec_safe_length (node->symbol.ref_list.references))
257 ipa_record_reference ((symtab_node)node, (symtab_node)tgt, IPA_REF_ALIAS, NULL);
258 if (node->extra_name_alias)
260 DECL_WEAK (node->symbol.decl) = DECL_WEAK (node->alias_of);
261 DECL_EXTERNAL (node->symbol.decl) = DECL_EXTERNAL (node->alias_of);
262 DECL_VISIBILITY (node->symbol.decl) = DECL_VISIBILITY (node->alias_of);
263 fixup_same_cpp_alias_visibility ((symtab_node) node,
264 (symtab_node) tgt, node->alias_of);
267 else if (DECL_INITIAL (decl))
268 record_references_in_initializer (decl, node->symbol.analyzed);
269 node->symbol.analyzed = true;
272 /* Assemble thunks and aliases associated to NODE. */
274 static void
275 assemble_aliases (struct varpool_node *node)
277 int i;
278 struct ipa_ref *ref;
279 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
280 if (ref->use == IPA_REF_ALIAS)
282 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
283 do_assemble_alias (alias->symbol.decl,
284 DECL_ASSEMBLER_NAME (alias->alias_of));
285 assemble_aliases (alias);
289 /* Output one variable, if necessary. Return whether we output it. */
291 bool
292 varpool_assemble_decl (struct varpool_node *node)
294 tree decl = node->symbol.decl;
296 /* Aliases are outout when their target is produced or by
297 output_weakrefs. */
298 if (node->symbol.alias)
299 return false;
301 /* Constant pool is output from RTL land when the reference
302 survive till this level. */
303 if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl))
304 return false;
306 /* Decls with VALUE_EXPR should not be in the varpool at all. They
307 are not real variables, but just info for debugging and codegen.
308 Unfortunately at the moment emutls is not updating varpool correctly
309 after turning real vars into value_expr vars. */
310 if (DECL_HAS_VALUE_EXPR_P (decl)
311 && !targetm.have_tls)
312 return false;
314 /* Hard register vars do not need to be output. */
315 if (DECL_HARD_REGISTER (decl))
316 return false;
318 gcc_checking_assert (!TREE_ASM_WRITTEN (decl)
319 && TREE_CODE (decl) == VAR_DECL
320 && !DECL_HAS_VALUE_EXPR_P (decl));
322 if (!node->symbol.in_other_partition
323 && !DECL_EXTERNAL (decl))
325 assemble_variable (decl, 0, 1, 0);
326 gcc_assert (TREE_ASM_WRITTEN (decl));
327 node->symbol.definition = true;
328 assemble_aliases (node);
329 return true;
332 return false;
335 /* Add NODE to queue starting at FIRST.
336 The queue is linked via AUX pointers and terminated by pointer to 1. */
338 static void
339 enqueue_node (struct varpool_node *node, struct varpool_node **first)
341 if (node->symbol.aux)
342 return;
343 gcc_checking_assert (*first);
344 node->symbol.aux = *first;
345 *first = node;
348 /* Optimization of function bodies might've rendered some variables as
349 unnecessary so we want to avoid these from being compiled. Re-do
350 reachability starting from variables that are either externally visible
351 or was referred from the asm output routines. */
353 static void
354 varpool_remove_unreferenced_decls (void)
356 struct varpool_node *next, *node;
357 struct varpool_node *first = (struct varpool_node *)(void *)1;
358 int i;
359 struct ipa_ref *ref;
361 if (seen_error ())
362 return;
364 if (cgraph_dump_file)
365 fprintf (cgraph_dump_file, "Trivially needed variables:");
366 FOR_EACH_DEFINED_VARIABLE (node)
368 if (node->symbol.analyzed
369 && (!varpool_can_remove_if_no_refs (node)
370 /* We just expanded all function bodies. See if any of
371 them needed the variable. */
372 || DECL_RTL_SET_P (node->symbol.decl)))
374 enqueue_node (node, &first);
375 if (cgraph_dump_file)
376 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
379 while (first != (struct varpool_node *)(void *)1)
381 node = first;
382 first = (struct varpool_node *)first->symbol.aux;
384 if (node->symbol.same_comdat_group)
386 symtab_node next;
387 for (next = node->symbol.same_comdat_group;
388 next != (symtab_node)node;
389 next = next->symbol.same_comdat_group)
391 varpool_node *vnext = dyn_cast <varpool_node> (next);
392 if (vnext && vnext->symbol.analyzed)
393 enqueue_node (vnext, &first);
396 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
398 varpool_node *vnode = dyn_cast <varpool_node> (ref->referred);
399 if (vnode
400 && (!DECL_EXTERNAL (ref->referred->symbol.decl)
401 || vnode->symbol.alias)
402 && vnode->symbol.analyzed)
403 enqueue_node (vnode, &first);
406 if (cgraph_dump_file)
407 fprintf (cgraph_dump_file, "\nRemoving variables:");
408 for (node = varpool_first_defined_variable (); node; node = next)
410 next = varpool_next_defined_variable (node);
411 if (!node->symbol.aux)
413 if (cgraph_dump_file)
414 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
415 varpool_remove_node (node);
418 if (cgraph_dump_file)
419 fprintf (cgraph_dump_file, "\n");
422 /* For variables in named sections make sure get_variable_section
423 is called before we switch to those sections. Then section
424 conflicts between read-only and read-only requiring relocations
425 sections can be resolved. */
426 void
427 varpool_finalize_named_section_flags (struct varpool_node *node)
429 if (!TREE_ASM_WRITTEN (node->symbol.decl)
430 && !node->symbol.alias
431 && !node->symbol.in_other_partition
432 && !DECL_EXTERNAL (node->symbol.decl)
433 && TREE_CODE (node->symbol.decl) == VAR_DECL
434 && !DECL_HAS_VALUE_EXPR_P (node->symbol.decl)
435 && DECL_SECTION_NAME (node->symbol.decl))
436 get_variable_section (node->symbol.decl, false);
439 /* Output all variables enqueued to be assembled. */
440 bool
441 varpool_output_variables (void)
443 bool changed = false;
444 struct varpool_node *node;
446 if (seen_error ())
447 return false;
449 varpool_remove_unreferenced_decls ();
451 timevar_push (TV_VAROUT);
453 FOR_EACH_DEFINED_VARIABLE (node)
454 varpool_finalize_named_section_flags (node);
456 FOR_EACH_DEFINED_VARIABLE (node)
457 if (varpool_assemble_decl (node))
458 changed = true;
459 timevar_pop (TV_VAROUT);
460 return changed;
463 /* Create a new global variable of type TYPE. */
464 tree
465 add_new_static_var (tree type)
467 tree new_decl;
468 struct varpool_node *new_node;
470 new_decl = create_tmp_var_raw (type, NULL);
471 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
472 TREE_READONLY (new_decl) = 0;
473 TREE_STATIC (new_decl) = 1;
474 TREE_USED (new_decl) = 1;
475 DECL_CONTEXT (new_decl) = NULL_TREE;
476 DECL_ABSTRACT (new_decl) = 0;
477 lang_hooks.dup_lang_specific_decl (new_decl);
478 new_node = varpool_node_for_decl (new_decl);
479 varpool_finalize_decl (new_decl);
481 return new_node->symbol.decl;
484 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
485 Extra name aliases are output whenever DECL is output. */
487 struct varpool_node *
488 varpool_create_variable_alias (tree alias, tree decl)
490 struct varpool_node *alias_node;
492 gcc_assert (TREE_CODE (decl) == VAR_DECL);
493 gcc_assert (TREE_CODE (alias) == VAR_DECL);
494 alias_node = varpool_node_for_decl (alias);
495 alias_node->symbol.alias = true;
496 alias_node->symbol.definition = true;
497 alias_node->alias_of = decl;
499 /* Extra name alias mechanizm creates aliases really late
500 via DECL_ASSEMBLER_NAME mechanizm.
501 This is unfortunate because they are not going through the
502 standard channels. Ensure they get output. */
503 if (cgraph_state >= CGRAPH_STATE_IPA)
505 varpool_analyze_node (alias_node);
506 if (TREE_PUBLIC (alias))
507 alias_node->symbol.externally_visible = true;
509 return alias_node;
512 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
513 Extra name aliases are output whenever DECL is output. */
515 struct varpool_node *
516 varpool_extra_name_alias (tree alias, tree decl)
518 struct varpool_node *alias_node;
520 #ifndef ASM_OUTPUT_DEF
521 /* If aliases aren't supported by the assembler, fail. */
522 return NULL;
523 #endif
524 alias_node = varpool_create_variable_alias (alias, decl);
525 alias_node->extra_name_alias = true;
526 return alias_node;
529 /* Call calback on NODE and aliases associated to NODE.
530 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
531 skipped. */
533 bool
534 varpool_for_node_and_aliases (struct varpool_node *node,
535 bool (*callback) (struct varpool_node *, void *),
536 void *data,
537 bool include_overwritable)
539 int i;
540 struct ipa_ref *ref;
542 if (callback (node, data))
543 return true;
544 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
545 if (ref->use == IPA_REF_ALIAS)
547 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
548 if (include_overwritable
549 || cgraph_variable_initializer_availability (alias) > AVAIL_OVERWRITABLE)
550 if (varpool_for_node_and_aliases (alias, callback, data,
551 include_overwritable))
552 return true;
554 return false;