PR tree-optimization/54321
[official-gcc.git] / gcc / varpool.c
blobb0063c1632894511fd683bc908a0b753ee1d3dad
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 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 symtab_node node = symtab_node_for_asm (asmname);
118 if (node && symtab_variable_p (node))
119 return varpool (node);
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 (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 (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_length (ipa_ref_t, 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 gcc_checking_assert (!TREE_ASM_WRITTEN (decl)
305 && TREE_CODE (decl) == VAR_DECL
306 && !DECL_HAS_VALUE_EXPR_P (decl));
308 if (!node->symbol.in_other_partition
309 && !DECL_EXTERNAL (decl))
311 assemble_variable (decl, 0, 1, 0);
312 gcc_assert (TREE_ASM_WRITTEN (decl));
313 node->finalized = 1;
314 assemble_aliases (node);
315 return true;
318 return false;
321 /* Add NODE to queue starting at FIRST.
322 The queue is linked via AUX pointers and terminated by pointer to 1. */
324 static void
325 enqueue_node (struct varpool_node *node, struct varpool_node **first)
327 if (node->symbol.aux)
328 return;
329 gcc_checking_assert (*first);
330 node->symbol.aux = *first;
331 *first = node;
334 /* Optimization of function bodies might've rendered some variables as
335 unnecessary so we want to avoid these from being compiled. Re-do
336 reachability starting from variables that are either externally visible
337 or was referred from the asm output routines. */
339 static void
340 varpool_remove_unreferenced_decls (void)
342 struct varpool_node *next, *node;
343 struct varpool_node *first = (struct varpool_node *)(void *)1;
344 int i;
345 struct ipa_ref *ref;
347 if (seen_error ())
348 return;
350 if (cgraph_dump_file)
351 fprintf (cgraph_dump_file, "Trivially needed variables:");
352 FOR_EACH_DEFINED_VARIABLE (node)
354 if (node->analyzed
355 && (!varpool_can_remove_if_no_refs (node)
356 /* We just expanded all function bodies. See if any of
357 them needed the variable. */
358 || (!DECL_EXTERNAL (node->symbol.decl)
359 && DECL_RTL_SET_P (node->symbol.decl))))
361 enqueue_node (node, &first);
362 if (cgraph_dump_file)
363 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
366 while (first != (struct varpool_node *)(void *)1)
368 node = first;
369 first = (struct varpool_node *)first->symbol.aux;
371 if (node->symbol.same_comdat_group)
373 symtab_node next;
374 for (next = node->symbol.same_comdat_group;
375 next != (symtab_node)node;
376 next = next->symbol.same_comdat_group)
377 if (symtab_variable_p (next)
378 && varpool (next)->analyzed)
379 enqueue_node (varpool (next), &first);
381 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
382 if (symtab_variable_p (ref->referred)
383 && (!DECL_EXTERNAL (ref->referred->symbol.decl)
384 || varpool (ref->referred)->alias)
385 && varpool (ref->referred)->analyzed)
386 enqueue_node (varpool (ref->referred), &first);
388 if (cgraph_dump_file)
389 fprintf (cgraph_dump_file, "\nRemoving variables:");
390 for (node = varpool_first_defined_variable (); node; node = next)
392 next = varpool_next_defined_variable (node);
393 if (!node->symbol.aux)
395 if (cgraph_dump_file)
396 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
397 varpool_remove_node (node);
400 if (cgraph_dump_file)
401 fprintf (cgraph_dump_file, "\n");
404 /* For variables in named sections make sure get_variable_section
405 is called before we switch to those sections. Then section
406 conflicts between read-only and read-only requiring relocations
407 sections can be resolved. */
408 void
409 varpool_finalize_named_section_flags (struct varpool_node *node)
411 if (!TREE_ASM_WRITTEN (node->symbol.decl)
412 && !node->alias
413 && !node->symbol.in_other_partition
414 && !DECL_EXTERNAL (node->symbol.decl)
415 && TREE_CODE (node->symbol.decl) == VAR_DECL
416 && !DECL_HAS_VALUE_EXPR_P (node->symbol.decl)
417 && DECL_SECTION_NAME (node->symbol.decl))
418 get_variable_section (node->symbol.decl, false);
421 /* Output all variables enqueued to be assembled. */
422 bool
423 varpool_output_variables (void)
425 bool changed = false;
426 struct varpool_node *node;
428 if (seen_error ())
429 return false;
431 varpool_remove_unreferenced_decls ();
433 timevar_push (TV_VAROUT);
435 FOR_EACH_DEFINED_VARIABLE (node)
436 varpool_finalize_named_section_flags (node);
438 FOR_EACH_DEFINED_VARIABLE (node)
439 if (varpool_assemble_decl (node))
440 changed = true;
441 timevar_pop (TV_VAROUT);
442 return changed;
445 /* Create a new global variable of type TYPE. */
446 tree
447 add_new_static_var (tree type)
449 tree new_decl;
450 struct varpool_node *new_node;
452 new_decl = create_tmp_var_raw (type, NULL);
453 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
454 TREE_READONLY (new_decl) = 0;
455 TREE_STATIC (new_decl) = 1;
456 TREE_USED (new_decl) = 1;
457 DECL_CONTEXT (new_decl) = NULL_TREE;
458 DECL_ABSTRACT (new_decl) = 0;
459 lang_hooks.dup_lang_specific_decl (new_decl);
460 new_node = varpool_node (new_decl);
461 varpool_finalize_decl (new_decl);
463 return new_node->symbol.decl;
466 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
467 Extra name aliases are output whenever DECL is output. */
469 struct varpool_node *
470 varpool_create_variable_alias (tree alias, tree decl)
472 struct varpool_node *alias_node;
474 gcc_assert (TREE_CODE (decl) == VAR_DECL);
475 gcc_assert (TREE_CODE (alias) == VAR_DECL);
476 alias_node = varpool_node (alias);
477 alias_node->alias = 1;
478 alias_node->finalized = 1;
479 alias_node->alias_of = decl;
481 /* Extra name alias mechanizm creates aliases really late
482 via DECL_ASSEMBLER_NAME mechanizm.
483 This is unfortunate because they are not going through the
484 standard channels. Ensure they get output. */
485 if (cgraph_state >= CGRAPH_STATE_IPA)
487 varpool_analyze_node (alias_node);
488 if (TREE_PUBLIC (alias))
489 alias_node->symbol.externally_visible = true;
491 return alias_node;
494 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
495 Extra name aliases are output whenever DECL is output. */
497 struct varpool_node *
498 varpool_extra_name_alias (tree alias, tree decl)
500 struct varpool_node *alias_node;
502 #ifndef ASM_OUTPUT_DEF
503 /* If aliases aren't supported by the assembler, fail. */
504 return NULL;
505 #endif
506 alias_node = varpool_create_variable_alias (alias, decl);
507 alias_node->extra_name_alias = true;
508 return alias_node;
511 /* Call calback on NODE and aliases associated to NODE.
512 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
513 skipped. */
515 bool
516 varpool_for_node_and_aliases (struct varpool_node *node,
517 bool (*callback) (struct varpool_node *, void *),
518 void *data,
519 bool include_overwritable)
521 int i;
522 struct ipa_ref *ref;
524 if (callback (node, data))
525 return true;
526 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
527 if (ref->use == IPA_REF_ALIAS)
529 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
530 if (include_overwritable
531 || cgraph_variable_initializer_availability (alias) > AVAIL_OVERWRITABLE)
532 if (varpool_for_node_and_aliases (alias, callback, data,
533 include_overwritable))
534 return true;
536 return false;