Daily bump.
[official-gcc.git] / gcc / varpool.c
blobdbf519e6137c2fec12529d277e5600eb408ec472
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);
61 if (DECL_INITIAL (node->symbol.decl)
62 && !DECL_IN_CONSTANT_POOL (node->symbol.decl)
63 /* Keep vtables for BINFO folding. */
64 && !DECL_VIRTUAL_P (node->symbol.decl)
65 /* FIXME: http://gcc.gnu.org/PR55395 */
66 && debug_info_level == DINFO_LEVEL_NONE)
67 DECL_INITIAL (node->symbol.decl) = error_mark_node;
68 ggc_free (node);
71 /* Dump given cgraph node. */
72 void
73 dump_varpool_node (FILE *f, struct varpool_node *node)
75 dump_symtab_base (f, (symtab_node)node);
76 fprintf (f, " Availability: %s\n",
77 cgraph_function_flags_ready
78 ? cgraph_availability_names[cgraph_variable_initializer_availability (node)]
79 : "not-ready");
80 fprintf (f, " Varpool flags:");
81 if (DECL_INITIAL (node->symbol.decl))
82 fprintf (f, " initialized");
83 if (node->analyzed)
84 fprintf (f, " analyzed");
85 if (node->finalized)
86 fprintf (f, " finalized");
87 if (node->output)
88 fprintf (f, " output");
89 fprintf (f, "\n");
92 /* Dump the variable pool. */
93 void
94 dump_varpool (FILE *f)
96 struct varpool_node *node;
98 fprintf (f, "variable pool:\n\n");
99 FOR_EACH_VARIABLE (node)
100 dump_varpool_node (f, node);
103 /* Dump the variable pool to stderr. */
105 DEBUG_FUNCTION void
106 debug_varpool (void)
108 dump_varpool (stderr);
111 /* Given an assembler name, lookup node. */
112 struct varpool_node *
113 varpool_node_for_asm (tree asmname)
115 if (symtab_node node = symtab_node_for_asm (asmname))
116 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
117 return vnode;
118 return NULL;
121 /* Determine if variable DECL is needed. That is, visible to something
122 either outside this translation unit, something magic in the system
123 configury */
124 bool
125 decide_is_variable_needed (struct varpool_node *node, tree decl)
127 if (DECL_EXTERNAL (decl))
128 return false;
130 /* If the user told us it is used, then it must be so. */
131 if (node->symbol.force_output)
132 return true;
134 /* Externally visible variables must be output. The exception is
135 COMDAT variables that must be output only when they are needed. */
136 if (TREE_PUBLIC (decl)
137 && !DECL_COMDAT (decl))
138 return true;
140 return false;
143 /* Return if DECL is constant and its initial value is known (so we can do
144 constant folding using DECL_INITIAL (decl)). */
146 bool
147 const_value_known_p (tree decl)
149 if (TREE_CODE (decl) != VAR_DECL
150 &&TREE_CODE (decl) != CONST_DECL)
151 return false;
153 if (TREE_CODE (decl) == CONST_DECL
154 || DECL_IN_CONSTANT_POOL (decl))
155 return true;
157 gcc_assert (TREE_CODE (decl) == VAR_DECL);
159 if (!TREE_READONLY (decl) || TREE_THIS_VOLATILE (decl))
160 return false;
162 /* Gimplifier takes away constructors of local vars */
163 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
164 return DECL_INITIAL (decl) != NULL;
166 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
168 /* Variables declared 'const' without an initializer
169 have zero as the initializer if they may not be
170 overridden at link or run time. */
171 if (!DECL_INITIAL (decl)
172 && (DECL_EXTERNAL (decl)
173 || decl_replaceable_p (decl)))
174 return false;
176 /* Variables declared `const' with an initializer are considered
177 to not be overwritable with different initializer by default.
179 ??? Previously we behaved so for scalar variables but not for array
180 accesses. */
181 return true;
184 /* Add the variable DECL to the varpool.
185 Unlike varpool_finalize_decl function is intended to be used
186 by middle end and allows insertion of new variable at arbitrary point
187 of compilation. */
188 void
189 varpool_add_new_variable (tree decl)
191 struct varpool_node *node;
192 varpool_finalize_decl (decl);
193 node = varpool_node_for_decl (decl);
194 if (varpool_externally_visible_p (node))
195 node->symbol.externally_visible = true;
198 /* Return variable availability. See cgraph.h for description of individual
199 return values. */
200 enum availability
201 cgraph_variable_initializer_availability (struct varpool_node *node)
203 gcc_assert (cgraph_function_flags_ready);
204 if (!node->finalized)
205 return AVAIL_NOT_AVAILABLE;
206 if (!TREE_PUBLIC (node->symbol.decl))
207 return AVAIL_AVAILABLE;
208 /* If the variable can be overwritten, return OVERWRITABLE. Takes
209 care of at least two notable extensions - the COMDAT variables
210 used to share template instantiations in C++. */
211 if (!decl_replaceable_p (node->symbol.decl))
212 return AVAIL_OVERWRITABLE;
213 return AVAIL_AVAILABLE;
216 void
217 varpool_analyze_node (struct varpool_node *node)
219 tree decl = node->symbol.decl;
221 /* When reading back varpool at LTO time, we re-construct the queue in order
222 to have "needed" list right by inserting all needed nodes into varpool.
223 We however don't want to re-analyze already analyzed nodes. */
224 if (!node->analyzed)
226 gcc_assert (!in_lto_p || cgraph_function_flags_ready);
227 /* Compute the alignment early so function body expanders are
228 already informed about increased alignment. */
229 align_variable (decl, 0);
231 if (node->alias && node->alias_of)
233 struct varpool_node *tgt = varpool_node_for_decl (node->alias_of);
234 struct varpool_node *n;
236 for (n = tgt; n && n->alias;
237 n = n->analyzed ? varpool_alias_aliased_node (n) : NULL)
238 if (n == node)
240 error ("variable %q+D part of alias cycle", node->symbol.decl);
241 node->alias = false;
242 continue;
244 if (!vec_safe_length (node->symbol.ref_list.references))
245 ipa_record_reference ((symtab_node)node, (symtab_node)tgt, IPA_REF_ALIAS, NULL);
246 if (node->extra_name_alias)
248 DECL_WEAK (node->symbol.decl) = DECL_WEAK (node->alias_of);
249 DECL_EXTERNAL (node->symbol.decl) = DECL_EXTERNAL (node->alias_of);
250 DECL_VISIBILITY (node->symbol.decl) = DECL_VISIBILITY (node->alias_of);
251 fixup_same_cpp_alias_visibility ((symtab_node) node,
252 (symtab_node) tgt, node->alias_of);
255 else if (DECL_INITIAL (decl))
256 record_references_in_initializer (decl, node->analyzed);
257 node->analyzed = true;
260 /* Assemble thunks and aliases associated to NODE. */
262 static void
263 assemble_aliases (struct varpool_node *node)
265 int i;
266 struct ipa_ref *ref;
267 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
268 if (ref->use == IPA_REF_ALIAS)
270 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
271 do_assemble_alias (alias->symbol.decl,
272 DECL_ASSEMBLER_NAME (alias->alias_of));
273 assemble_aliases (alias);
277 /* Output one variable, if necessary. Return whether we output it. */
279 bool
280 varpool_assemble_decl (struct varpool_node *node)
282 tree decl = node->symbol.decl;
284 /* Aliases are outout when their target is produced or by
285 output_weakrefs. */
286 if (node->alias)
287 return false;
289 /* Constant pool is output from RTL land when the reference
290 survive till this level. */
291 if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl))
292 return false;
294 /* Decls with VALUE_EXPR should not be in the varpool at all. They
295 are not real variables, but just info for debugging and codegen.
296 Unfortunately at the moment emutls is not updating varpool correctly
297 after turning real vars into value_expr vars. */
298 if (DECL_HAS_VALUE_EXPR_P (decl)
299 && !targetm.have_tls)
300 return false;
302 /* Hard register vars do not need to be output. */
303 if (DECL_HARD_REGISTER (decl))
304 return false;
306 gcc_checking_assert (!TREE_ASM_WRITTEN (decl)
307 && TREE_CODE (decl) == VAR_DECL
308 && !DECL_HAS_VALUE_EXPR_P (decl));
310 if (!node->symbol.in_other_partition
311 && !DECL_EXTERNAL (decl))
313 assemble_variable (decl, 0, 1, 0);
314 gcc_assert (TREE_ASM_WRITTEN (decl));
315 node->finalized = 1;
316 assemble_aliases (node);
317 return true;
320 return false;
323 /* Add NODE to queue starting at FIRST.
324 The queue is linked via AUX pointers and terminated by pointer to 1. */
326 static void
327 enqueue_node (struct varpool_node *node, struct varpool_node **first)
329 if (node->symbol.aux)
330 return;
331 gcc_checking_assert (*first);
332 node->symbol.aux = *first;
333 *first = node;
336 /* Optimization of function bodies might've rendered some variables as
337 unnecessary so we want to avoid these from being compiled. Re-do
338 reachability starting from variables that are either externally visible
339 or was referred from the asm output routines. */
341 static void
342 varpool_remove_unreferenced_decls (void)
344 struct varpool_node *next, *node;
345 struct varpool_node *first = (struct varpool_node *)(void *)1;
346 int i;
347 struct ipa_ref *ref;
349 if (seen_error ())
350 return;
352 if (cgraph_dump_file)
353 fprintf (cgraph_dump_file, "Trivially needed variables:");
354 FOR_EACH_DEFINED_VARIABLE (node)
356 if (node->analyzed
357 && (!varpool_can_remove_if_no_refs (node)
358 /* We just expanded all function bodies. See if any of
359 them needed the variable. */
360 || DECL_RTL_SET_P (node->symbol.decl)))
362 enqueue_node (node, &first);
363 if (cgraph_dump_file)
364 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
367 while (first != (struct varpool_node *)(void *)1)
369 node = first;
370 first = (struct varpool_node *)first->symbol.aux;
372 if (node->symbol.same_comdat_group)
374 symtab_node next;
375 for (next = node->symbol.same_comdat_group;
376 next != (symtab_node)node;
377 next = next->symbol.same_comdat_group)
379 varpool_node *vnext = dyn_cast <varpool_node> (next);
380 if (vnext && vnext->analyzed)
381 enqueue_node (vnext, &first);
384 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
386 varpool_node *vnode = dyn_cast <varpool_node> (ref->referred);
387 if (vnode
388 && (!DECL_EXTERNAL (ref->referred->symbol.decl)
389 || vnode->alias)
390 && vnode->analyzed)
391 enqueue_node (vnode, &first);
394 if (cgraph_dump_file)
395 fprintf (cgraph_dump_file, "\nRemoving variables:");
396 for (node = varpool_first_defined_variable (); node; node = next)
398 next = varpool_next_defined_variable (node);
399 if (!node->symbol.aux)
401 if (cgraph_dump_file)
402 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
403 varpool_remove_node (node);
406 if (cgraph_dump_file)
407 fprintf (cgraph_dump_file, "\n");
410 /* For variables in named sections make sure get_variable_section
411 is called before we switch to those sections. Then section
412 conflicts between read-only and read-only requiring relocations
413 sections can be resolved. */
414 void
415 varpool_finalize_named_section_flags (struct varpool_node *node)
417 if (!TREE_ASM_WRITTEN (node->symbol.decl)
418 && !node->alias
419 && !node->symbol.in_other_partition
420 && !DECL_EXTERNAL (node->symbol.decl)
421 && TREE_CODE (node->symbol.decl) == VAR_DECL
422 && !DECL_HAS_VALUE_EXPR_P (node->symbol.decl)
423 && DECL_SECTION_NAME (node->symbol.decl))
424 get_variable_section (node->symbol.decl, false);
427 /* Output all variables enqueued to be assembled. */
428 bool
429 varpool_output_variables (void)
431 bool changed = false;
432 struct varpool_node *node;
434 if (seen_error ())
435 return false;
437 varpool_remove_unreferenced_decls ();
439 timevar_push (TV_VAROUT);
441 FOR_EACH_DEFINED_VARIABLE (node)
442 varpool_finalize_named_section_flags (node);
444 FOR_EACH_DEFINED_VARIABLE (node)
445 if (varpool_assemble_decl (node))
446 changed = true;
447 timevar_pop (TV_VAROUT);
448 return changed;
451 /* Create a new global variable of type TYPE. */
452 tree
453 add_new_static_var (tree type)
455 tree new_decl;
456 struct varpool_node *new_node;
458 new_decl = create_tmp_var_raw (type, NULL);
459 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
460 TREE_READONLY (new_decl) = 0;
461 TREE_STATIC (new_decl) = 1;
462 TREE_USED (new_decl) = 1;
463 DECL_CONTEXT (new_decl) = NULL_TREE;
464 DECL_ABSTRACT (new_decl) = 0;
465 lang_hooks.dup_lang_specific_decl (new_decl);
466 new_node = varpool_node_for_decl (new_decl);
467 varpool_finalize_decl (new_decl);
469 return new_node->symbol.decl;
472 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
473 Extra name aliases are output whenever DECL is output. */
475 struct varpool_node *
476 varpool_create_variable_alias (tree alias, tree decl)
478 struct varpool_node *alias_node;
480 gcc_assert (TREE_CODE (decl) == VAR_DECL);
481 gcc_assert (TREE_CODE (alias) == VAR_DECL);
482 alias_node = varpool_node_for_decl (alias);
483 alias_node->alias = 1;
484 alias_node->finalized = 1;
485 alias_node->alias_of = decl;
487 /* Extra name alias mechanizm creates aliases really late
488 via DECL_ASSEMBLER_NAME mechanizm.
489 This is unfortunate because they are not going through the
490 standard channels. Ensure they get output. */
491 if (cgraph_state >= CGRAPH_STATE_IPA)
493 varpool_analyze_node (alias_node);
494 if (TREE_PUBLIC (alias))
495 alias_node->symbol.externally_visible = true;
497 return alias_node;
500 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
501 Extra name aliases are output whenever DECL is output. */
503 struct varpool_node *
504 varpool_extra_name_alias (tree alias, tree decl)
506 struct varpool_node *alias_node;
508 #ifndef ASM_OUTPUT_DEF
509 /* If aliases aren't supported by the assembler, fail. */
510 return NULL;
511 #endif
512 alias_node = varpool_create_variable_alias (alias, decl);
513 alias_node->extra_name_alias = true;
514 return alias_node;
517 /* Call calback on NODE and aliases associated to NODE.
518 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
519 skipped. */
521 bool
522 varpool_for_node_and_aliases (struct varpool_node *node,
523 bool (*callback) (struct varpool_node *, void *),
524 void *data,
525 bool include_overwritable)
527 int i;
528 struct ipa_ref *ref;
530 if (callback (node, data))
531 return true;
532 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
533 if (ref->use == IPA_REF_ALIAS)
535 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
536 if (include_overwritable
537 || cgraph_variable_initializer_availability (alias) > AVAIL_OVERWRITABLE)
538 if (varpool_for_node_and_aliases (alias, callback, data,
539 include_overwritable))
540 return true;
542 return false;