* xcoffout.c (xcoff_tls_data_section_name): Define.
[official-gcc.git] / gcc / varpool.c
blob87f29a02f09b2a977825d793cce35bfc12504d89
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 /* dbxout output constant initializers for readonly vars. */
67 && (!host_integerp (DECL_INITIAL (node->symbol.decl), 0)
68 || !TREE_READONLY (node->symbol.decl)))
69 DECL_INITIAL (node->symbol.decl) = error_mark_node;
70 ggc_free (node);
73 /* Dump given cgraph node. */
74 void
75 dump_varpool_node (FILE *f, struct varpool_node *node)
77 dump_symtab_base (f, (symtab_node)node);
78 fprintf (f, " Availability: %s\n",
79 cgraph_function_flags_ready
80 ? cgraph_availability_names[cgraph_variable_initializer_availability (node)]
81 : "not-ready");
82 fprintf (f, " Varpool flags:");
83 if (DECL_INITIAL (node->symbol.decl))
84 fprintf (f, " initialized");
85 if (node->analyzed)
86 fprintf (f, " analyzed");
87 if (node->finalized)
88 fprintf (f, " finalized");
89 if (node->output)
90 fprintf (f, " output");
91 fprintf (f, "\n");
94 /* Dump the variable pool. */
95 void
96 dump_varpool (FILE *f)
98 struct varpool_node *node;
100 fprintf (f, "variable pool:\n\n");
101 FOR_EACH_VARIABLE (node)
102 dump_varpool_node (f, node);
105 /* Dump the variable pool to stderr. */
107 DEBUG_FUNCTION void
108 debug_varpool (void)
110 dump_varpool (stderr);
113 /* Given an assembler name, lookup node. */
114 struct varpool_node *
115 varpool_node_for_asm (tree asmname)
117 if (symtab_node node = symtab_node_for_asm (asmname))
118 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
119 return vnode;
120 return NULL;
123 /* Determine if variable DECL is needed. That is, visible to something
124 either outside this translation unit, something magic in the system
125 configury */
126 bool
127 decide_is_variable_needed (struct varpool_node *node, tree decl)
129 if (DECL_EXTERNAL (decl))
130 return false;
132 /* If the user told us it is used, then it must be so. */
133 if (node->symbol.force_output)
134 return true;
136 /* Externally visible variables must be output. The exception is
137 COMDAT variables that must be output only when they are needed. */
138 if (TREE_PUBLIC (decl)
139 && !DECL_COMDAT (decl))
140 return true;
142 return false;
145 /* Return if DECL is constant and its initial value is known (so we can do
146 constant folding using DECL_INITIAL (decl)). */
148 bool
149 const_value_known_p (tree decl)
151 if (TREE_CODE (decl) != VAR_DECL
152 &&TREE_CODE (decl) != CONST_DECL)
153 return false;
155 if (TREE_CODE (decl) == CONST_DECL
156 || DECL_IN_CONSTANT_POOL (decl))
157 return true;
159 gcc_assert (TREE_CODE (decl) == VAR_DECL);
161 if (!TREE_READONLY (decl) || TREE_THIS_VOLATILE (decl))
162 return false;
164 /* Gimplifier takes away constructors of local vars */
165 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
166 return DECL_INITIAL (decl) != NULL;
168 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
170 /* Variables declared 'const' without an initializer
171 have zero as the initializer if they may not be
172 overridden at link or run time. */
173 if (!DECL_INITIAL (decl)
174 && (DECL_EXTERNAL (decl)
175 || decl_replaceable_p (decl)))
176 return false;
178 /* Variables declared `const' with an initializer are considered
179 to not be overwritable with different initializer by default.
181 ??? Previously we behaved so for scalar variables but not for array
182 accesses. */
183 return true;
186 /* Add the variable DECL to the varpool.
187 Unlike varpool_finalize_decl function is intended to be used
188 by middle end and allows insertion of new variable at arbitrary point
189 of compilation. */
190 void
191 varpool_add_new_variable (tree decl)
193 struct varpool_node *node;
194 varpool_finalize_decl (decl);
195 node = varpool_node_for_decl (decl);
196 if (varpool_externally_visible_p (node, false))
197 node->symbol.externally_visible = true;
200 /* Return variable availability. See cgraph.h for description of individual
201 return values. */
202 enum availability
203 cgraph_variable_initializer_availability (struct varpool_node *node)
205 gcc_assert (cgraph_function_flags_ready);
206 if (!node->finalized)
207 return AVAIL_NOT_AVAILABLE;
208 if (!TREE_PUBLIC (node->symbol.decl))
209 return AVAIL_AVAILABLE;
210 /* If the variable can be overwritten, return OVERWRITABLE. Takes
211 care of at least two notable extensions - the COMDAT variables
212 used to share template instantiations in C++. */
213 if (!decl_replaceable_p (node->symbol.decl))
214 return AVAIL_OVERWRITABLE;
215 return AVAIL_AVAILABLE;
218 void
219 varpool_analyze_node (struct varpool_node *node)
221 tree decl = node->symbol.decl;
223 /* When reading back varpool at LTO time, we re-construct the queue in order
224 to have "needed" list right by inserting all needed nodes into varpool.
225 We however don't want to re-analyze already analyzed nodes. */
226 if (!node->analyzed)
228 gcc_assert (!in_lto_p || cgraph_function_flags_ready);
229 /* Compute the alignment early so function body expanders are
230 already informed about increased alignment. */
231 align_variable (decl, 0);
233 if (node->alias && node->alias_of)
235 struct varpool_node *tgt = varpool_node_for_decl (node->alias_of);
236 struct varpool_node *n;
238 for (n = tgt; n && n->alias;
239 n = n->analyzed ? varpool_alias_aliased_node (n) : NULL)
240 if (n == node)
242 error ("variable %q+D part of alias cycle", node->symbol.decl);
243 node->alias = false;
244 continue;
246 if (!vec_safe_length (node->symbol.ref_list.references))
247 ipa_record_reference ((symtab_node)node, (symtab_node)tgt, IPA_REF_ALIAS, NULL);
248 if (node->extra_name_alias)
250 DECL_WEAK (node->symbol.decl) = DECL_WEAK (node->alias_of);
251 DECL_EXTERNAL (node->symbol.decl) = DECL_EXTERNAL (node->alias_of);
252 DECL_VISIBILITY (node->symbol.decl) = DECL_VISIBILITY (node->alias_of);
253 fixup_same_cpp_alias_visibility ((symtab_node) node,
254 (symtab_node) tgt, node->alias_of);
257 else if (DECL_INITIAL (decl))
258 record_references_in_initializer (decl, node->analyzed);
259 node->analyzed = true;
262 /* Assemble thunks and aliases associated to NODE. */
264 static void
265 assemble_aliases (struct varpool_node *node)
267 int i;
268 struct ipa_ref *ref;
269 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
270 if (ref->use == IPA_REF_ALIAS)
272 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
273 do_assemble_alias (alias->symbol.decl,
274 DECL_ASSEMBLER_NAME (alias->alias_of));
275 assemble_aliases (alias);
279 /* Output one variable, if necessary. Return whether we output it. */
281 bool
282 varpool_assemble_decl (struct varpool_node *node)
284 tree decl = node->symbol.decl;
286 /* Aliases are outout when their target is produced or by
287 output_weakrefs. */
288 if (node->alias)
289 return false;
291 /* Constant pool is output from RTL land when the reference
292 survive till this level. */
293 if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl))
294 return false;
296 /* Decls with VALUE_EXPR should not be in the varpool at all. They
297 are not real variables, but just info for debugging and codegen.
298 Unfortunately at the moment emutls is not updating varpool correctly
299 after turning real vars into value_expr vars. */
300 if (DECL_HAS_VALUE_EXPR_P (decl)
301 && !targetm.have_tls)
302 return false;
304 /* Hard register vars do not need to be output. */
305 if (DECL_HARD_REGISTER (decl))
306 return false;
308 gcc_checking_assert (!TREE_ASM_WRITTEN (decl)
309 && TREE_CODE (decl) == VAR_DECL
310 && !DECL_HAS_VALUE_EXPR_P (decl));
312 if (!node->symbol.in_other_partition
313 && !DECL_EXTERNAL (decl))
315 assemble_variable (decl, 0, 1, 0);
316 gcc_assert (TREE_ASM_WRITTEN (decl));
317 node->finalized = 1;
318 assemble_aliases (node);
319 return true;
322 return false;
325 /* Add NODE to queue starting at FIRST.
326 The queue is linked via AUX pointers and terminated by pointer to 1. */
328 static void
329 enqueue_node (struct varpool_node *node, struct varpool_node **first)
331 if (node->symbol.aux)
332 return;
333 gcc_checking_assert (*first);
334 node->symbol.aux = *first;
335 *first = node;
338 /* Optimization of function bodies might've rendered some variables as
339 unnecessary so we want to avoid these from being compiled. Re-do
340 reachability starting from variables that are either externally visible
341 or was referred from the asm output routines. */
343 static void
344 varpool_remove_unreferenced_decls (void)
346 struct varpool_node *next, *node;
347 struct varpool_node *first = (struct varpool_node *)(void *)1;
348 int i;
349 struct ipa_ref *ref;
351 if (seen_error ())
352 return;
354 if (cgraph_dump_file)
355 fprintf (cgraph_dump_file, "Trivially needed variables:");
356 FOR_EACH_DEFINED_VARIABLE (node)
358 if (node->analyzed
359 && (!varpool_can_remove_if_no_refs (node)
360 /* We just expanded all function bodies. See if any of
361 them needed the variable. */
362 || (!DECL_EXTERNAL (node->symbol.decl)
363 && DECL_RTL_SET_P (node->symbol.decl))))
365 enqueue_node (node, &first);
366 if (cgraph_dump_file)
367 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
370 while (first != (struct varpool_node *)(void *)1)
372 node = first;
373 first = (struct varpool_node *)first->symbol.aux;
375 if (node->symbol.same_comdat_group)
377 symtab_node next;
378 for (next = node->symbol.same_comdat_group;
379 next != (symtab_node)node;
380 next = next->symbol.same_comdat_group)
382 varpool_node *vnext = dyn_cast <varpool_node> (next);
383 if (vnext && vnext->analyzed)
384 enqueue_node (vnext, &first);
387 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
389 varpool_node *vnode = dyn_cast <varpool_node> (ref->referred);
390 if (vnode
391 && (!DECL_EXTERNAL (ref->referred->symbol.decl)
392 || vnode->alias)
393 && vnode->analyzed)
394 enqueue_node (vnode, &first);
397 if (cgraph_dump_file)
398 fprintf (cgraph_dump_file, "\nRemoving variables:");
399 for (node = varpool_first_defined_variable (); node; node = next)
401 next = varpool_next_defined_variable (node);
402 if (!node->symbol.aux)
404 if (cgraph_dump_file)
405 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
406 varpool_remove_node (node);
409 if (cgraph_dump_file)
410 fprintf (cgraph_dump_file, "\n");
413 /* For variables in named sections make sure get_variable_section
414 is called before we switch to those sections. Then section
415 conflicts between read-only and read-only requiring relocations
416 sections can be resolved. */
417 void
418 varpool_finalize_named_section_flags (struct varpool_node *node)
420 if (!TREE_ASM_WRITTEN (node->symbol.decl)
421 && !node->alias
422 && !node->symbol.in_other_partition
423 && !DECL_EXTERNAL (node->symbol.decl)
424 && TREE_CODE (node->symbol.decl) == VAR_DECL
425 && !DECL_HAS_VALUE_EXPR_P (node->symbol.decl)
426 && DECL_SECTION_NAME (node->symbol.decl))
427 get_variable_section (node->symbol.decl, false);
430 /* Output all variables enqueued to be assembled. */
431 bool
432 varpool_output_variables (void)
434 bool changed = false;
435 struct varpool_node *node;
437 if (seen_error ())
438 return false;
440 varpool_remove_unreferenced_decls ();
442 timevar_push (TV_VAROUT);
444 FOR_EACH_DEFINED_VARIABLE (node)
445 varpool_finalize_named_section_flags (node);
447 FOR_EACH_DEFINED_VARIABLE (node)
448 if (varpool_assemble_decl (node))
449 changed = true;
450 timevar_pop (TV_VAROUT);
451 return changed;
454 /* Create a new global variable of type TYPE. */
455 tree
456 add_new_static_var (tree type)
458 tree new_decl;
459 struct varpool_node *new_node;
461 new_decl = create_tmp_var_raw (type, NULL);
462 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
463 TREE_READONLY (new_decl) = 0;
464 TREE_STATIC (new_decl) = 1;
465 TREE_USED (new_decl) = 1;
466 DECL_CONTEXT (new_decl) = NULL_TREE;
467 DECL_ABSTRACT (new_decl) = 0;
468 lang_hooks.dup_lang_specific_decl (new_decl);
469 new_node = varpool_node_for_decl (new_decl);
470 varpool_finalize_decl (new_decl);
472 return new_node->symbol.decl;
475 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
476 Extra name aliases are output whenever DECL is output. */
478 struct varpool_node *
479 varpool_create_variable_alias (tree alias, tree decl)
481 struct varpool_node *alias_node;
483 gcc_assert (TREE_CODE (decl) == VAR_DECL);
484 gcc_assert (TREE_CODE (alias) == VAR_DECL);
485 alias_node = varpool_node_for_decl (alias);
486 alias_node->alias = 1;
487 alias_node->finalized = 1;
488 alias_node->alias_of = decl;
490 /* Extra name alias mechanizm creates aliases really late
491 via DECL_ASSEMBLER_NAME mechanizm.
492 This is unfortunate because they are not going through the
493 standard channels. Ensure they get output. */
494 if (cgraph_state >= CGRAPH_STATE_IPA)
496 varpool_analyze_node (alias_node);
497 if (TREE_PUBLIC (alias))
498 alias_node->symbol.externally_visible = true;
500 return alias_node;
503 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
504 Extra name aliases are output whenever DECL is output. */
506 struct varpool_node *
507 varpool_extra_name_alias (tree alias, tree decl)
509 struct varpool_node *alias_node;
511 #ifndef ASM_OUTPUT_DEF
512 /* If aliases aren't supported by the assembler, fail. */
513 return NULL;
514 #endif
515 alias_node = varpool_create_variable_alias (alias, decl);
516 alias_node->extra_name_alias = true;
517 return alias_node;
520 /* Call calback on NODE and aliases associated to NODE.
521 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
522 skipped. */
524 bool
525 varpool_for_node_and_aliases (struct varpool_node *node,
526 bool (*callback) (struct varpool_node *, void *),
527 void *data,
528 bool include_overwritable)
530 int i;
531 struct ipa_ref *ref;
533 if (callback (node, data))
534 return true;
535 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
536 if (ref->use == IPA_REF_ALIAS)
538 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
539 if (include_overwritable
540 || cgraph_variable_initializer_availability (alias) > AVAIL_OVERWRITABLE)
541 if (varpool_for_node_and_aliases (alias, callback, data,
542 include_overwritable))
543 return true;
545 return false;