2013-05-30 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / varpool.c
blob7915a95fb50450ab0bcf2883b3d02c08abae84cf
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)
244 symtab_resolve_alias
245 ((symtab_node) node, (symtab_node) varpool_get_node (node->symbol.alias_target));
246 else if (DECL_INITIAL (decl))
247 record_references_in_initializer (decl, node->symbol.analyzed);
248 node->symbol.analyzed = true;
251 /* Assemble thunks and aliases associated to NODE. */
253 static void
254 assemble_aliases (struct varpool_node *node)
256 int i;
257 struct ipa_ref *ref;
258 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
259 if (ref->use == IPA_REF_ALIAS)
261 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
262 do_assemble_alias (alias->symbol.decl,
263 DECL_ASSEMBLER_NAME (node->symbol.decl));
264 assemble_aliases (alias);
268 /* Output one variable, if necessary. Return whether we output it. */
270 bool
271 varpool_assemble_decl (struct varpool_node *node)
273 tree decl = node->symbol.decl;
275 /* Aliases are outout when their target is produced or by
276 output_weakrefs. */
277 if (node->symbol.alias)
278 return false;
280 /* Constant pool is output from RTL land when the reference
281 survive till this level. */
282 if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl))
283 return false;
285 /* Decls with VALUE_EXPR should not be in the varpool at all. They
286 are not real variables, but just info for debugging and codegen.
287 Unfortunately at the moment emutls is not updating varpool correctly
288 after turning real vars into value_expr vars. */
289 if (DECL_HAS_VALUE_EXPR_P (decl)
290 && !targetm.have_tls)
291 return false;
293 /* Hard register vars do not need to be output. */
294 if (DECL_HARD_REGISTER (decl))
295 return false;
297 gcc_checking_assert (!TREE_ASM_WRITTEN (decl)
298 && TREE_CODE (decl) == VAR_DECL
299 && !DECL_HAS_VALUE_EXPR_P (decl));
301 if (!node->symbol.in_other_partition
302 && !DECL_EXTERNAL (decl))
304 assemble_variable (decl, 0, 1, 0);
305 gcc_assert (TREE_ASM_WRITTEN (decl));
306 node->symbol.definition = true;
307 assemble_aliases (node);
308 return true;
311 return false;
314 /* Add NODE to queue starting at FIRST.
315 The queue is linked via AUX pointers and terminated by pointer to 1. */
317 static void
318 enqueue_node (struct varpool_node *node, struct varpool_node **first)
320 if (node->symbol.aux)
321 return;
322 gcc_checking_assert (*first);
323 node->symbol.aux = *first;
324 *first = node;
327 /* Optimization of function bodies might've rendered some variables as
328 unnecessary so we want to avoid these from being compiled. Re-do
329 reachability starting from variables that are either externally visible
330 or was referred from the asm output routines. */
332 static void
333 varpool_remove_unreferenced_decls (void)
335 struct varpool_node *next, *node;
336 struct varpool_node *first = (struct varpool_node *)(void *)1;
337 int i;
338 struct ipa_ref *ref;
340 if (seen_error ())
341 return;
343 if (cgraph_dump_file)
344 fprintf (cgraph_dump_file, "Trivially needed variables:");
345 FOR_EACH_DEFINED_VARIABLE (node)
347 if (node->symbol.analyzed
348 && (!varpool_can_remove_if_no_refs (node)
349 /* We just expanded all function bodies. See if any of
350 them needed the variable. */
351 || DECL_RTL_SET_P (node->symbol.decl)))
353 enqueue_node (node, &first);
354 if (cgraph_dump_file)
355 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
358 while (first != (struct varpool_node *)(void *)1)
360 node = first;
361 first = (struct varpool_node *)first->symbol.aux;
363 if (node->symbol.same_comdat_group)
365 symtab_node next;
366 for (next = node->symbol.same_comdat_group;
367 next != (symtab_node)node;
368 next = next->symbol.same_comdat_group)
370 varpool_node *vnext = dyn_cast <varpool_node> (next);
371 if (vnext && vnext->symbol.analyzed)
372 enqueue_node (vnext, &first);
375 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
377 varpool_node *vnode = dyn_cast <varpool_node> (ref->referred);
378 if (vnode
379 && (!DECL_EXTERNAL (ref->referred->symbol.decl)
380 || vnode->symbol.alias)
381 && vnode->symbol.analyzed)
382 enqueue_node (vnode, &first);
385 if (cgraph_dump_file)
386 fprintf (cgraph_dump_file, "\nRemoving variables:");
387 for (node = varpool_first_defined_variable (); node; node = next)
389 next = varpool_next_defined_variable (node);
390 if (!node->symbol.aux)
392 if (cgraph_dump_file)
393 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
394 varpool_remove_node (node);
397 if (cgraph_dump_file)
398 fprintf (cgraph_dump_file, "\n");
401 /* For variables in named sections make sure get_variable_section
402 is called before we switch to those sections. Then section
403 conflicts between read-only and read-only requiring relocations
404 sections can be resolved. */
405 void
406 varpool_finalize_named_section_flags (struct varpool_node *node)
408 if (!TREE_ASM_WRITTEN (node->symbol.decl)
409 && !node->symbol.alias
410 && !node->symbol.in_other_partition
411 && !DECL_EXTERNAL (node->symbol.decl)
412 && TREE_CODE (node->symbol.decl) == VAR_DECL
413 && !DECL_HAS_VALUE_EXPR_P (node->symbol.decl)
414 && DECL_SECTION_NAME (node->symbol.decl))
415 get_variable_section (node->symbol.decl, false);
418 /* Output all variables enqueued to be assembled. */
419 bool
420 varpool_output_variables (void)
422 bool changed = false;
423 struct varpool_node *node;
425 if (seen_error ())
426 return false;
428 varpool_remove_unreferenced_decls ();
430 timevar_push (TV_VAROUT);
432 FOR_EACH_DEFINED_VARIABLE (node)
433 varpool_finalize_named_section_flags (node);
435 FOR_EACH_DEFINED_VARIABLE (node)
436 if (varpool_assemble_decl (node))
437 changed = true;
438 timevar_pop (TV_VAROUT);
439 return changed;
442 /* Create a new global variable of type TYPE. */
443 tree
444 add_new_static_var (tree type)
446 tree new_decl;
447 struct varpool_node *new_node;
449 new_decl = create_tmp_var_raw (type, NULL);
450 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
451 TREE_READONLY (new_decl) = 0;
452 TREE_STATIC (new_decl) = 1;
453 TREE_USED (new_decl) = 1;
454 DECL_CONTEXT (new_decl) = NULL_TREE;
455 DECL_ABSTRACT (new_decl) = 0;
456 lang_hooks.dup_lang_specific_decl (new_decl);
457 new_node = varpool_node_for_decl (new_decl);
458 varpool_finalize_decl (new_decl);
460 return new_node->symbol.decl;
463 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
464 Extra name aliases are output whenever DECL is output. */
466 struct varpool_node *
467 varpool_create_variable_alias (tree alias, tree decl)
469 struct varpool_node *alias_node;
471 gcc_assert (TREE_CODE (decl) == VAR_DECL);
472 gcc_assert (TREE_CODE (alias) == VAR_DECL);
473 alias_node = varpool_node_for_decl (alias);
474 alias_node->symbol.alias = true;
475 alias_node->symbol.definition = true;
476 alias_node->symbol.alias_target = decl;
477 return alias_node;
480 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
481 Extra name aliases are output whenever DECL is output. */
483 struct varpool_node *
484 varpool_extra_name_alias (tree alias, tree decl)
486 struct varpool_node *alias_node;
488 #ifndef ASM_OUTPUT_DEF
489 /* If aliases aren't supported by the assembler, fail. */
490 return NULL;
491 #endif
492 alias_node = varpool_create_variable_alias (alias, decl);
493 alias_node->symbol.cpp_implicit_alias = true;
495 /* Extra name alias mechanizm creates aliases really late
496 via DECL_ASSEMBLER_NAME mechanizm.
497 This is unfortunate because they are not going through the
498 standard channels. Ensure they get output. */
499 if (cpp_implicit_aliases_done)
500 symtab_resolve_alias ((symtab_node)alias_node,
501 (symtab_node)varpool_node_for_decl (decl));
502 return alias_node;
505 /* Call calback on NODE and aliases associated to NODE.
506 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
507 skipped. */
509 bool
510 varpool_for_node_and_aliases (struct varpool_node *node,
511 bool (*callback) (struct varpool_node *, void *),
512 void *data,
513 bool include_overwritable)
515 int i;
516 struct ipa_ref *ref;
518 if (callback (node, data))
519 return true;
520 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
521 if (ref->use == IPA_REF_ALIAS)
523 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
524 if (include_overwritable
525 || cgraph_variable_initializer_availability (alias) > AVAIL_OVERWRITABLE)
526 if (varpool_for_node_and_aliases (alias, callback, data,
527 include_overwritable))
528 return true;
530 return false;