* varasm.c (pending_assemble_externals_processed): Guard
[official-gcc.git] / gcc / varpool.c
blob11c9e45233377a3a2c08f908cd6c1ef9bcb513ad
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_for_decl (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 if (DECL_INITIAL (node->symbol.decl)
63 && !DECL_IN_CONSTANT_POOL (node->symbol.decl)
64 /* Keep vtables for BINFO folding. */
65 && !DECL_VIRTUAL_P (node->symbol.decl)
66 /* FIXME: http://gcc.gnu.org/PR55395 */
67 && debug_info_level == DINFO_LEVEL_NONE)
68 DECL_INITIAL (node->symbol.decl) = error_mark_node;
69 ggc_free (node);
72 /* Dump given cgraph node. */
73 void
74 dump_varpool_node (FILE *f, struct varpool_node *node)
76 dump_symtab_base (f, (symtab_node)node);
77 fprintf (f, " Availability: %s\n",
78 cgraph_function_flags_ready
79 ? cgraph_availability_names[cgraph_variable_initializer_availability (node)]
80 : "not-ready");
81 fprintf (f, " Varpool flags:");
82 if (DECL_INITIAL (node->symbol.decl))
83 fprintf (f, " initialized");
84 if (node->analyzed)
85 fprintf (f, " analyzed");
86 if (node->finalized)
87 fprintf (f, " finalized");
88 if (node->output)
89 fprintf (f, " output");
90 fprintf (f, "\n");
93 /* Dump the variable pool. */
94 void
95 dump_varpool (FILE *f)
97 struct varpool_node *node;
99 fprintf (f, "variable pool:\n\n");
100 FOR_EACH_VARIABLE (node)
101 dump_varpool_node (f, node);
104 /* Dump the variable pool to stderr. */
106 DEBUG_FUNCTION void
107 debug_varpool (void)
109 dump_varpool (stderr);
112 /* Given an assembler name, lookup node. */
113 struct varpool_node *
114 varpool_node_for_asm (tree asmname)
116 if (symtab_node node = symtab_node_for_asm (asmname))
117 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
118 return vnode;
119 return NULL;
122 /* Determine if variable DECL is needed. That is, visible to something
123 either outside this translation unit, something magic in the system
124 configury */
125 bool
126 decide_is_variable_needed (struct varpool_node *node, tree decl)
128 if (DECL_EXTERNAL (decl))
129 return false;
131 /* If the user told us it is used, then it must be so. */
132 if (node->symbol.force_output)
133 return true;
135 /* Externally visible variables must be output. The exception is
136 COMDAT variables that must be output only when they are needed. */
137 if (TREE_PUBLIC (decl)
138 && !DECL_COMDAT (decl))
139 return true;
141 return false;
144 /* Return if DECL is constant and its initial value is known (so we can do
145 constant folding using DECL_INITIAL (decl)). */
147 bool
148 const_value_known_p (tree decl)
150 if (TREE_CODE (decl) != VAR_DECL
151 &&TREE_CODE (decl) != CONST_DECL)
152 return false;
154 if (TREE_CODE (decl) == CONST_DECL
155 || DECL_IN_CONSTANT_POOL (decl))
156 return true;
158 gcc_assert (TREE_CODE (decl) == VAR_DECL);
160 if (!TREE_READONLY (decl) || TREE_THIS_VOLATILE (decl))
161 return false;
163 /* Gimplifier takes away constructors of local vars */
164 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
165 return DECL_INITIAL (decl) != NULL;
167 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
169 /* Variables declared 'const' without an initializer
170 have zero as the initializer if they may not be
171 overridden at link or run time. */
172 if (!DECL_INITIAL (decl)
173 && (DECL_EXTERNAL (decl)
174 || decl_replaceable_p (decl)))
175 return false;
177 /* Variables declared `const' with an initializer are considered
178 to not be overwritable with different initializer by default.
180 ??? Previously we behaved so for scalar variables but not for array
181 accesses. */
182 return true;
185 /* Add the variable DECL to the varpool.
186 Unlike varpool_finalize_decl function is intended to be used
187 by middle end and allows insertion of new variable at arbitrary point
188 of compilation. */
189 void
190 varpool_add_new_variable (tree decl)
192 struct varpool_node *node;
193 varpool_finalize_decl (decl);
194 node = varpool_node_for_decl (decl);
195 if (varpool_externally_visible_p (node, false))
196 node->symbol.externally_visible = true;
199 /* Return variable availability. See cgraph.h for description of individual
200 return values. */
201 enum availability
202 cgraph_variable_initializer_availability (struct varpool_node *node)
204 gcc_assert (cgraph_function_flags_ready);
205 if (!node->finalized)
206 return AVAIL_NOT_AVAILABLE;
207 if (!TREE_PUBLIC (node->symbol.decl))
208 return AVAIL_AVAILABLE;
209 /* If the variable can be overwritten, return OVERWRITABLE. Takes
210 care of at least two notable extensions - the COMDAT variables
211 used to share template instantiations in C++. */
212 if (!decl_replaceable_p (node->symbol.decl))
213 return AVAIL_OVERWRITABLE;
214 return AVAIL_AVAILABLE;
217 void
218 varpool_analyze_node (struct varpool_node *node)
220 tree decl = node->symbol.decl;
222 /* When reading back varpool at LTO time, we re-construct the queue in order
223 to have "needed" list right by inserting all needed nodes into varpool.
224 We however don't want to re-analyze already analyzed nodes. */
225 if (!node->analyzed)
227 gcc_assert (!in_lto_p || cgraph_function_flags_ready);
228 /* Compute the alignment early so function body expanders are
229 already informed about increased alignment. */
230 align_variable (decl, 0);
232 if (node->alias && node->alias_of)
234 struct varpool_node *tgt = varpool_node_for_decl (node->alias_of);
235 struct varpool_node *n;
237 for (n = tgt; n && n->alias;
238 n = n->analyzed ? varpool_alias_aliased_node (n) : NULL)
239 if (n == node)
241 error ("variable %q+D part of alias cycle", node->symbol.decl);
242 node->alias = false;
243 continue;
245 if (!vec_safe_length (node->symbol.ref_list.references))
246 ipa_record_reference ((symtab_node)node, (symtab_node)tgt, IPA_REF_ALIAS, NULL);
247 if (node->extra_name_alias)
249 DECL_WEAK (node->symbol.decl) = DECL_WEAK (node->alias_of);
250 DECL_EXTERNAL (node->symbol.decl) = DECL_EXTERNAL (node->alias_of);
251 DECL_VISIBILITY (node->symbol.decl) = DECL_VISIBILITY (node->alias_of);
252 fixup_same_cpp_alias_visibility ((symtab_node) node,
253 (symtab_node) tgt, node->alias_of);
256 else if (DECL_INITIAL (decl))
257 record_references_in_initializer (decl, node->analyzed);
258 node->analyzed = true;
261 /* Assemble thunks and aliases associated to NODE. */
263 static void
264 assemble_aliases (struct varpool_node *node)
266 int i;
267 struct ipa_ref *ref;
268 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
269 if (ref->use == IPA_REF_ALIAS)
271 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
272 do_assemble_alias (alias->symbol.decl,
273 DECL_ASSEMBLER_NAME (alias->alias_of));
274 assemble_aliases (alias);
278 /* Output one variable, if necessary. Return whether we output it. */
280 bool
281 varpool_assemble_decl (struct varpool_node *node)
283 tree decl = node->symbol.decl;
285 /* Aliases are outout when their target is produced or by
286 output_weakrefs. */
287 if (node->alias)
288 return false;
290 /* Constant pool is output from RTL land when the reference
291 survive till this level. */
292 if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl))
293 return false;
295 /* Decls with VALUE_EXPR should not be in the varpool at all. They
296 are not real variables, but just info for debugging and codegen.
297 Unfortunately at the moment emutls is not updating varpool correctly
298 after turning real vars into value_expr vars. */
299 if (DECL_HAS_VALUE_EXPR_P (decl)
300 && !targetm.have_tls)
301 return false;
303 /* Hard register vars do not need to be output. */
304 if (DECL_HARD_REGISTER (decl))
305 return false;
307 gcc_checking_assert (!TREE_ASM_WRITTEN (decl)
308 && TREE_CODE (decl) == VAR_DECL
309 && !DECL_HAS_VALUE_EXPR_P (decl));
311 if (!node->symbol.in_other_partition
312 && !DECL_EXTERNAL (decl))
314 assemble_variable (decl, 0, 1, 0);
315 gcc_assert (TREE_ASM_WRITTEN (decl));
316 node->finalized = 1;
317 assemble_aliases (node);
318 return true;
321 return false;
324 /* Add NODE to queue starting at FIRST.
325 The queue is linked via AUX pointers and terminated by pointer to 1. */
327 static void
328 enqueue_node (struct varpool_node *node, struct varpool_node **first)
330 if (node->symbol.aux)
331 return;
332 gcc_checking_assert (*first);
333 node->symbol.aux = *first;
334 *first = node;
337 /* Optimization of function bodies might've rendered some variables as
338 unnecessary so we want to avoid these from being compiled. Re-do
339 reachability starting from variables that are either externally visible
340 or was referred from the asm output routines. */
342 static void
343 varpool_remove_unreferenced_decls (void)
345 struct varpool_node *next, *node;
346 struct varpool_node *first = (struct varpool_node *)(void *)1;
347 int i;
348 struct ipa_ref *ref;
350 if (seen_error ())
351 return;
353 if (cgraph_dump_file)
354 fprintf (cgraph_dump_file, "Trivially needed variables:");
355 FOR_EACH_DEFINED_VARIABLE (node)
357 if (node->analyzed
358 && (!varpool_can_remove_if_no_refs (node)
359 /* We just expanded all function bodies. See if any of
360 them needed the variable. */
361 || DECL_RTL_SET_P (node->symbol.decl)))
363 enqueue_node (node, &first);
364 if (cgraph_dump_file)
365 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
368 while (first != (struct varpool_node *)(void *)1)
370 node = first;
371 first = (struct varpool_node *)first->symbol.aux;
373 if (node->symbol.same_comdat_group)
375 symtab_node next;
376 for (next = node->symbol.same_comdat_group;
377 next != (symtab_node)node;
378 next = next->symbol.same_comdat_group)
380 varpool_node *vnext = dyn_cast <varpool_node> (next);
381 if (vnext && vnext->analyzed)
382 enqueue_node (vnext, &first);
385 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
387 varpool_node *vnode = dyn_cast <varpool_node> (ref->referred);
388 if (vnode
389 && (!DECL_EXTERNAL (ref->referred->symbol.decl)
390 || vnode->alias)
391 && vnode->analyzed)
392 enqueue_node (vnode, &first);
395 if (cgraph_dump_file)
396 fprintf (cgraph_dump_file, "\nRemoving variables:");
397 for (node = varpool_first_defined_variable (); node; node = next)
399 next = varpool_next_defined_variable (node);
400 if (!node->symbol.aux)
402 if (cgraph_dump_file)
403 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
404 varpool_remove_node (node);
407 if (cgraph_dump_file)
408 fprintf (cgraph_dump_file, "\n");
411 /* For variables in named sections make sure get_variable_section
412 is called before we switch to those sections. Then section
413 conflicts between read-only and read-only requiring relocations
414 sections can be resolved. */
415 void
416 varpool_finalize_named_section_flags (struct varpool_node *node)
418 if (!TREE_ASM_WRITTEN (node->symbol.decl)
419 && !node->alias
420 && !node->symbol.in_other_partition
421 && !DECL_EXTERNAL (node->symbol.decl)
422 && TREE_CODE (node->symbol.decl) == VAR_DECL
423 && !DECL_HAS_VALUE_EXPR_P (node->symbol.decl)
424 && DECL_SECTION_NAME (node->symbol.decl))
425 get_variable_section (node->symbol.decl, false);
428 /* Output all variables enqueued to be assembled. */
429 bool
430 varpool_output_variables (void)
432 bool changed = false;
433 struct varpool_node *node;
435 if (seen_error ())
436 return false;
438 varpool_remove_unreferenced_decls ();
440 timevar_push (TV_VAROUT);
442 FOR_EACH_DEFINED_VARIABLE (node)
443 varpool_finalize_named_section_flags (node);
445 FOR_EACH_DEFINED_VARIABLE (node)
446 if (varpool_assemble_decl (node))
447 changed = true;
448 timevar_pop (TV_VAROUT);
449 return changed;
452 /* Create a new global variable of type TYPE. */
453 tree
454 add_new_static_var (tree type)
456 tree new_decl;
457 struct varpool_node *new_node;
459 new_decl = create_tmp_var_raw (type, NULL);
460 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
461 TREE_READONLY (new_decl) = 0;
462 TREE_STATIC (new_decl) = 1;
463 TREE_USED (new_decl) = 1;
464 DECL_CONTEXT (new_decl) = NULL_TREE;
465 DECL_ABSTRACT (new_decl) = 0;
466 lang_hooks.dup_lang_specific_decl (new_decl);
467 new_node = varpool_node_for_decl (new_decl);
468 varpool_finalize_decl (new_decl);
470 return new_node->symbol.decl;
473 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
474 Extra name aliases are output whenever DECL is output. */
476 struct varpool_node *
477 varpool_create_variable_alias (tree alias, tree decl)
479 struct varpool_node *alias_node;
481 gcc_assert (TREE_CODE (decl) == VAR_DECL);
482 gcc_assert (TREE_CODE (alias) == VAR_DECL);
483 alias_node = varpool_node_for_decl (alias);
484 alias_node->alias = 1;
485 alias_node->finalized = 1;
486 alias_node->alias_of = decl;
488 /* Extra name alias mechanizm creates aliases really late
489 via DECL_ASSEMBLER_NAME mechanizm.
490 This is unfortunate because they are not going through the
491 standard channels. Ensure they get output. */
492 if (cgraph_state >= CGRAPH_STATE_IPA)
494 varpool_analyze_node (alias_node);
495 if (TREE_PUBLIC (alias))
496 alias_node->symbol.externally_visible = true;
498 return alias_node;
501 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
502 Extra name aliases are output whenever DECL is output. */
504 struct varpool_node *
505 varpool_extra_name_alias (tree alias, tree decl)
507 struct varpool_node *alias_node;
509 #ifndef ASM_OUTPUT_DEF
510 /* If aliases aren't supported by the assembler, fail. */
511 return NULL;
512 #endif
513 alias_node = varpool_create_variable_alias (alias, decl);
514 alias_node->extra_name_alias = true;
515 return alias_node;
518 /* Call calback on NODE and aliases associated to NODE.
519 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
520 skipped. */
522 bool
523 varpool_for_node_and_aliases (struct varpool_node *node,
524 bool (*callback) (struct varpool_node *, void *),
525 void *data,
526 bool include_overwritable)
528 int i;
529 struct ipa_ref *ref;
531 if (callback (node, data))
532 return true;
533 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
534 if (ref->use == IPA_REF_ALIAS)
536 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
537 if (include_overwritable
538 || cgraph_variable_initializer_availability (alias) > AVAIL_OVERWRITABLE)
539 if (varpool_for_node_and_aliases (alias, callback, data,
540 include_overwritable))
541 return true;
543 return false;