2012-05-01 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / varpool.c
blob103b5b5f810b8e4587b82697ea2ee2df7e4b4e2c
1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "cgraph.h"
28 #include "langhooks.h"
29 #include "diagnostic-core.h"
30 #include "hashtab.h"
31 #include "ggc.h"
32 #include "timevar.h"
33 #include "debug.h"
34 #include "target.h"
35 #include "output.h"
36 #include "gimple.h"
37 #include "tree-flow.h"
38 #include "flags.h"
40 /* Return varpool node assigned to DECL. Create new one when needed. */
41 struct varpool_node *
42 varpool_node (tree decl)
44 struct varpool_node *node = varpool_get_node (decl);
45 gcc_assert (TREE_CODE (decl) == VAR_DECL
46 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl) || in_lto_p));
47 if (node)
48 return node;
50 node = ggc_alloc_cleared_varpool_node ();
51 node->symbol.type = SYMTAB_VARIABLE;
52 node->symbol.decl = decl;
53 symtab_register_node ((symtab_node)node);
54 return node;
57 /* Remove node from the varpool. */
58 void
59 varpool_remove_node (struct varpool_node *node)
61 symtab_unregister_node ((symtab_node)node);
62 ggc_free (node);
65 /* Dump given cgraph node. */
66 void
67 dump_varpool_node (FILE *f, struct varpool_node *node)
69 dump_symtab_base (f, (symtab_node)node);
70 fprintf (f, " Availability: %s\n",
71 cgraph_function_flags_ready
72 ? cgraph_availability_names[cgraph_variable_initializer_availability (node)]
73 : "not-ready");
74 fprintf (f, " Varpool flags:");
75 if (DECL_INITIAL (node->symbol.decl))
76 fprintf (f, " initialized");
77 if (node->analyzed)
78 fprintf (f, " analyzed");
79 if (node->finalized)
80 fprintf (f, " finalized");
81 if (node->output)
82 fprintf (f, " output");
83 fprintf (f, "\n");
86 /* Dump the variable pool. */
87 void
88 dump_varpool (FILE *f)
90 struct varpool_node *node;
92 fprintf (f, "variable pool:\n\n");
93 FOR_EACH_VARIABLE (node)
94 dump_varpool_node (f, node);
97 /* Dump the variable pool to stderr. */
99 DEBUG_FUNCTION void
100 debug_varpool (void)
102 dump_varpool (stderr);
105 /* Given an assembler name, lookup node. */
106 struct varpool_node *
107 varpool_node_for_asm (tree asmname)
109 symtab_node node = symtab_node_for_asm (asmname);
110 if (node && symtab_variable_p (node))
111 return varpool (node);
112 return NULL;
115 /* Determine if variable DECL is needed. That is, visible to something
116 either outside this translation unit, something magic in the system
117 configury */
118 bool
119 decide_is_variable_needed (struct varpool_node *node, tree decl)
121 /* If the user told us it is used, then it must be so. */
122 if (node->symbol.force_output)
123 return true;
125 gcc_assert (!DECL_EXTERNAL (decl));
127 /* Externally visible variables must be output. The exception is
128 COMDAT variables that must be output only when they are needed. */
129 if (TREE_PUBLIC (decl)
130 && !DECL_COMDAT (decl)
131 && !DECL_EXTERNAL (decl))
132 return true;
134 return false;
137 /* Return if DECL is constant and its initial value is known (so we can do
138 constant folding using DECL_INITIAL (decl)). */
140 bool
141 const_value_known_p (tree decl)
143 if (TREE_CODE (decl) != VAR_DECL
144 &&TREE_CODE (decl) != CONST_DECL)
145 return false;
147 if (TREE_CODE (decl) == CONST_DECL
148 || DECL_IN_CONSTANT_POOL (decl))
149 return true;
151 gcc_assert (TREE_CODE (decl) == VAR_DECL);
153 if (!TREE_READONLY (decl) || TREE_THIS_VOLATILE (decl))
154 return false;
156 /* Gimplifier takes away constructors of local vars */
157 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
158 return DECL_INITIAL (decl) != NULL;
160 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
162 /* Variables declared 'const' without an initializer
163 have zero as the initializer if they may not be
164 overridden at link or run time. */
165 if (!DECL_INITIAL (decl)
166 && (DECL_EXTERNAL (decl)
167 || decl_replaceable_p (decl)))
168 return false;
170 /* Variables declared `const' with an initializer are considered
171 to not be overwritable with different initializer by default.
173 ??? Previously we behaved so for scalar variables but not for array
174 accesses. */
175 return true;
178 /* Add the variable DECL to the varpool.
179 Unlike varpool_finalize_decl function is intended to be used
180 by middle end and allows insertion of new variable at arbitrary point
181 of compilation. */
182 void
183 varpool_add_new_variable (tree decl)
185 struct varpool_node *node;
186 varpool_finalize_decl (decl);
187 node = varpool_node (decl);
188 if (varpool_externally_visible_p (node, false))
189 node->symbol.externally_visible = true;
192 /* Return variable availability. See cgraph.h for description of individual
193 return values. */
194 enum availability
195 cgraph_variable_initializer_availability (struct varpool_node *node)
197 gcc_assert (cgraph_function_flags_ready);
198 if (!node->finalized)
199 return AVAIL_NOT_AVAILABLE;
200 if (!TREE_PUBLIC (node->symbol.decl))
201 return AVAIL_AVAILABLE;
202 /* If the variable can be overwritten, return OVERWRITABLE. Takes
203 care of at least two notable extensions - the COMDAT variables
204 used to share template instantiations in C++. */
205 if (!decl_replaceable_p (node->symbol.decl))
206 return AVAIL_OVERWRITABLE;
207 return AVAIL_AVAILABLE;
210 void
211 varpool_analyze_node (struct varpool_node *node)
213 tree decl = node->symbol.decl;
215 /* When reading back varpool at LTO time, we re-construct the queue in order
216 to have "needed" list right by inserting all needed nodes into varpool.
217 We however don't want to re-analyze already analyzed nodes. */
218 if (!node->analyzed)
220 gcc_assert (!in_lto_p || cgraph_function_flags_ready);
221 /* Compute the alignment early so function body expanders are
222 already informed about increased alignment. */
223 align_variable (decl, 0);
225 if (node->alias && node->alias_of)
227 struct varpool_node *tgt = varpool_node (node->alias_of);
228 struct varpool_node *n;
230 for (n = tgt; n && n->alias;
231 n = n->analyzed ? varpool_alias_aliased_node (n) : NULL)
232 if (n == node)
234 error ("variable %q+D part of alias cycle", node->symbol.decl);
235 node->alias = false;
236 continue;
238 if (!VEC_length (ipa_ref_t, node->symbol.ref_list.references))
239 ipa_record_reference ((symtab_node)node, (symtab_node)tgt, IPA_REF_ALIAS, NULL);
240 if (node->extra_name_alias)
242 DECL_WEAK (node->symbol.decl) = DECL_WEAK (node->alias_of);
243 DECL_EXTERNAL (node->symbol.decl) = DECL_EXTERNAL (node->alias_of);
244 DECL_VISIBILITY (node->symbol.decl) = DECL_VISIBILITY (node->alias_of);
245 fixup_same_cpp_alias_visibility ((symtab_node) node,
246 (symtab_node) tgt, node->alias_of);
249 else if (DECL_INITIAL (decl))
250 record_references_in_initializer (decl, node->analyzed);
251 node->analyzed = true;
254 /* Assemble thunks and aliases asociated to NODE. */
256 static void
257 assemble_aliases (struct varpool_node *node)
259 int i;
260 struct ipa_ref *ref;
261 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
262 if (ref->use == IPA_REF_ALIAS)
264 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
265 assemble_alias (alias->symbol.decl,
266 DECL_ASSEMBLER_NAME (alias->alias_of));
267 assemble_aliases (alias);
271 /* Output one variable, if necessary. Return whether we output it. */
272 bool
273 varpool_assemble_decl (struct varpool_node *node)
275 tree decl = node->symbol.decl;
277 if (!TREE_ASM_WRITTEN (decl)
278 && !node->alias
279 && !node->symbol.in_other_partition
280 && !DECL_EXTERNAL (decl)
281 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl)))
283 assemble_variable (decl, 0, 1, 0);
284 if (TREE_ASM_WRITTEN (decl))
286 node->finalized = 1;
287 assemble_aliases (node);
288 return true;
292 return false;
295 /* Add NODE to queue starting at FIRST.
296 The queue is linked via AUX pointers and terminated by pointer to 1. */
298 static void
299 enqueue_node (struct varpool_node *node, struct varpool_node **first)
301 if (node->symbol.aux)
302 return;
303 gcc_checking_assert (*first);
304 node->symbol.aux = *first;
305 *first = node;
308 /* Optimization of function bodies might've rendered some variables as
309 unnecessary so we want to avoid these from being compiled. Re-do
310 reachability starting from variables that are either externally visible
311 or was referred from the asm output routines. */
313 static void
314 varpool_remove_unreferenced_decls (void)
316 struct varpool_node *next, *node;
317 struct varpool_node *first = (struct varpool_node *)(void *)1;
318 int i;
319 struct ipa_ref *ref;
321 if (seen_error ())
322 return;
324 if (cgraph_dump_file)
325 fprintf (cgraph_dump_file, "Trivially needed variables:");
326 finish_aliases_1 ();
327 FOR_EACH_DEFINED_VARIABLE (node)
329 if (node->analyzed
330 && (!varpool_can_remove_if_no_refs (node)
331 /* We just expanded all function bodies. See if any of
332 them needed the variable. */
333 || DECL_RTL_SET_P (node->symbol.decl)))
335 enqueue_node (node, &first);
336 if (cgraph_dump_file)
337 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
340 while (first != (struct varpool_node *)(void *)1)
342 node = first;
343 first = (struct varpool_node *)first->symbol.aux;
345 if (node->symbol.same_comdat_group)
347 symtab_node next;
348 for (next = node->symbol.same_comdat_group;
349 next != (symtab_node)node;
350 next = next->symbol.same_comdat_group)
351 if (symtab_variable_p (next)
352 && varpool (next)->analyzed)
353 enqueue_node (varpool (next), &first);
355 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
356 if (symtab_variable_p (ref->referred)
357 && varpool (ref->referred)->analyzed)
358 enqueue_node (varpool (ref->referred), &first);
360 if (cgraph_dump_file)
361 fprintf (cgraph_dump_file, "\nRemoving variables:");
362 for (node = varpool_first_defined_variable (); node; node = next)
364 next = varpool_next_defined_variable (node);
365 if (!node->symbol.aux)
367 if (cgraph_dump_file)
368 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
369 varpool_remove_node (node);
372 if (cgraph_dump_file)
373 fprintf (cgraph_dump_file, "\n");
376 /* For variables in named sections make sure get_variable_section
377 is called before we switch to those sections. Then section
378 conflicts between read-only and read-only requiring relocations
379 sections can be resolved. */
380 void
381 varpool_finalize_named_section_flags (struct varpool_node *node)
383 if (!TREE_ASM_WRITTEN (node->symbol.decl)
384 && !node->alias
385 && !node->symbol.in_other_partition
386 && !DECL_EXTERNAL (node->symbol.decl)
387 && TREE_CODE (node->symbol.decl) == VAR_DECL
388 && !DECL_HAS_VALUE_EXPR_P (node->symbol.decl)
389 && DECL_SECTION_NAME (node->symbol.decl))
390 get_variable_section (node->symbol.decl, false);
393 /* Output all variables enqueued to be assembled. */
394 bool
395 varpool_output_variables (void)
397 bool changed = false;
398 struct varpool_node *node;
400 if (seen_error ())
401 return false;
403 varpool_remove_unreferenced_decls ();
405 timevar_push (TV_VAROUT);
407 FOR_EACH_DEFINED_VARIABLE (node)
408 varpool_finalize_named_section_flags (node);
410 FOR_EACH_DEFINED_VARIABLE (node)
411 if (varpool_assemble_decl (node))
412 changed = true;
413 timevar_pop (TV_VAROUT);
414 return changed;
417 /* Create a new global variable of type TYPE. */
418 tree
419 add_new_static_var (tree type)
421 tree new_decl;
422 struct varpool_node *new_node;
424 new_decl = create_tmp_var (type, NULL);
425 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
426 TREE_READONLY (new_decl) = 0;
427 TREE_STATIC (new_decl) = 1;
428 TREE_USED (new_decl) = 1;
429 DECL_CONTEXT (new_decl) = NULL_TREE;
430 DECL_ABSTRACT (new_decl) = 0;
431 lang_hooks.dup_lang_specific_decl (new_decl);
432 create_var_ann (new_decl);
433 new_node = varpool_node (new_decl);
434 add_referenced_var (new_decl);
435 varpool_finalize_decl (new_decl);
437 return new_node->symbol.decl;
440 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
441 Extra name aliases are output whenever DECL is output. */
443 struct varpool_node *
444 varpool_create_variable_alias (tree alias, tree decl)
446 struct varpool_node *alias_node;
448 gcc_assert (TREE_CODE (decl) == VAR_DECL);
449 gcc_assert (TREE_CODE (alias) == VAR_DECL);
450 alias_node = varpool_node (alias);
451 alias_node->alias = 1;
452 alias_node->finalized = 1;
453 alias_node->alias_of = decl;
455 /* Extra name alias mechanizm creates aliases really late
456 via DECL_ASSEMBLER_NAME mechanizm.
457 This is unfortunate because they are not going through the
458 standard channels. Ensure they get output. */
459 if (cgraph_state >= CGRAPH_STATE_IPA)
461 varpool_analyze_node (alias_node);
462 if (TREE_PUBLIC (alias))
463 alias_node->symbol.externally_visible = true;
465 return alias_node;
468 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
469 Extra name aliases are output whenever DECL is output. */
471 struct varpool_node *
472 varpool_extra_name_alias (tree alias, tree decl)
474 struct varpool_node *alias_node;
476 #ifndef ASM_OUTPUT_DEF
477 /* If aliases aren't supported by the assembler, fail. */
478 return NULL;
479 #endif
480 alias_node = varpool_create_variable_alias (alias, decl);
481 alias_node->extra_name_alias = true;
482 return alias_node;
485 /* Call calback on NODE and aliases asociated to NODE.
486 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
487 skipped. */
489 bool
490 varpool_for_node_and_aliases (struct varpool_node *node,
491 bool (*callback) (struct varpool_node *, void *),
492 void *data,
493 bool include_overwritable)
495 int i;
496 struct ipa_ref *ref;
498 if (callback (node, data))
499 return true;
500 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
501 if (ref->use == IPA_REF_ALIAS)
503 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
504 if (include_overwritable
505 || cgraph_variable_initializer_availability (alias) > AVAIL_OVERWRITABLE)
506 if (varpool_for_node_and_aliases (alias, callback, data,
507 include_overwritable))
508 return true;
510 return false;