Remove unused variable and field
[official-gcc.git] / gcc / varpool.c
blobb426757ec8463feef14a304fd45313931790067d
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 /* Allocate new callgraph node and insert it into basic data structures. */
41 struct varpool_node *
42 varpool_create_empty_node (void)
44 struct varpool_node *node = ggc_alloc_cleared_varpool_node ();
45 node->symbol.type = SYMTAB_VARIABLE;
46 return node;
49 /* Return varpool node assigned to DECL. Create new one when needed. */
50 struct varpool_node *
51 varpool_node_for_decl (tree decl)
53 struct varpool_node *node = varpool_get_node (decl);
54 gcc_checking_assert (TREE_CODE (decl) == VAR_DECL);
55 if (node)
56 return node;
58 node = varpool_create_empty_node ();
59 node->symbol.decl = decl;
60 symtab_register_node ((symtab_node)node);
61 return node;
64 /* Remove node from the varpool. */
65 void
66 varpool_remove_node (struct varpool_node *node)
68 symtab_unregister_node ((symtab_node)node);
69 tree init;
71 /* Because we remove references from external functions before final compilation,
72 we may end up removing useful constructors.
73 FIXME: We probably want to trace boundaries better. */
74 if ((init = ctor_for_folding (node->symbol.decl)) == error_mark_node)
75 varpool_remove_initializer (node);
76 else
77 DECL_INITIAL (node->symbol.decl) = init;
78 ggc_free (node);
81 /* Renove node initializer when it is no longer needed. */
82 void
83 varpool_remove_initializer (struct varpool_node *node)
85 if (DECL_INITIAL (node->symbol.decl)
86 && !DECL_IN_CONSTANT_POOL (node->symbol.decl)
87 /* Keep vtables for BINFO folding. */
88 && !DECL_VIRTUAL_P (node->symbol.decl)
89 /* FIXME: http://gcc.gnu.org/PR55395 */
90 && debug_info_level == DINFO_LEVEL_NONE
91 /* When doing declaration merging we have duplicate
92 entries for given decl. Do not attempt to remove
93 the boides, or we will end up remiving
94 wrong one. */
95 && cgraph_state != CGRAPH_LTO_STREAMING)
96 DECL_INITIAL (node->symbol.decl) = error_mark_node;
99 /* Dump given cgraph node. */
100 void
101 dump_varpool_node (FILE *f, struct varpool_node *node)
103 dump_symtab_base (f, (symtab_node)node);
104 fprintf (f, " Availability: %s\n",
105 cgraph_function_flags_ready
106 ? cgraph_availability_names[cgraph_variable_initializer_availability (node)]
107 : "not-ready");
108 fprintf (f, " Varpool flags:");
109 if (DECL_INITIAL (node->symbol.decl))
110 fprintf (f, " initialized");
111 if (node->output)
112 fprintf (f, " output");
113 if (TREE_READONLY (node->symbol.decl))
114 fprintf (f, " read-only");
115 if (ctor_for_folding (node->symbol.decl) != error_mark_node)
116 fprintf (f, " const-value-known");
117 fprintf (f, "\n");
120 /* Dump the variable pool. */
121 void
122 dump_varpool (FILE *f)
124 struct varpool_node *node;
126 fprintf (f, "variable pool:\n\n");
127 FOR_EACH_VARIABLE (node)
128 dump_varpool_node (f, node);
131 /* Dump the variable pool to stderr. */
133 DEBUG_FUNCTION void
134 debug_varpool (void)
136 dump_varpool (stderr);
139 /* Given an assembler name, lookup node. */
140 struct varpool_node *
141 varpool_node_for_asm (tree asmname)
143 if (symtab_node node = symtab_node_for_asm (asmname))
144 return dyn_cast <varpool_node> (node);
145 else
146 return NULL;
149 /* Return if DECL is constant and its initial value is known (so we can do
150 constant folding using DECL_INITIAL (decl)).
151 Return ERROR_MARK_NODE when value is unknown. */
153 tree
154 ctor_for_folding (tree decl)
156 struct varpool_node *node, *real_node;
157 tree real_decl;
159 if (TREE_CODE (decl) != VAR_DECL
160 && TREE_CODE (decl) != CONST_DECL)
161 return error_mark_node;
163 if (TREE_CODE (decl) == CONST_DECL
164 || DECL_IN_CONSTANT_POOL (decl))
165 return DECL_INITIAL (decl);
167 if (TREE_THIS_VOLATILE (decl))
168 return error_mark_node;
170 /* Do not care about automatic variables. Those are never initialized
171 anyway, because gimplifier exapnds the code*/
172 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
174 gcc_assert (!TREE_PUBLIC (decl));
175 return error_mark_node;
178 gcc_assert (TREE_CODE (decl) == VAR_DECL);
180 node = varpool_get_node (decl);
181 if (node)
183 real_node = varpool_variable_node (node);
184 real_decl = real_node->symbol.decl;
186 else
187 real_decl = decl;
189 /* See if we are dealing with alias.
190 In most cases alias is just alternative symbol pointing to a given
191 constructor. This allows us to use interposition rules of DECL
192 constructor of REAL_NODE. However weakrefs are special by being just
193 alternative name of their target (if defined). */
194 if (decl != real_decl)
196 gcc_assert (!DECL_INITIAL (decl)
197 || DECL_INITIAL (decl) == error_mark_node);
198 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
200 node = varpool_alias_target (node);
201 decl = node->symbol.decl;
205 /* Vtables are defined by their types and must match no matter of interposition
206 rules. */
207 if (DECL_VIRTUAL_P (real_decl))
209 gcc_checking_assert (TREE_READONLY (real_decl));
210 return DECL_INITIAL (real_decl);
213 /* If thre is no constructor, we have nothing to do. */
214 if (DECL_INITIAL (real_decl) == error_mark_node)
215 return error_mark_node;
217 /* Non-readonly alias of readonly variable is also de-facto readonly,
218 because the variable itself is in readonly section.
219 We also honnor READONLY flag on alias assuming that user knows
220 what he is doing. */
221 if (!TREE_READONLY (decl) && !TREE_READONLY (real_decl))
222 return error_mark_node;
224 /* Variables declared 'const' without an initializer
225 have zero as the initializer if they may not be
226 overridden at link or run time. */
227 if (!DECL_INITIAL (real_decl)
228 && (DECL_EXTERNAL (decl) || decl_replaceable_p (decl)))
229 return error_mark_node;
231 /* Variables declared `const' with an initializer are considered
232 to not be overwritable with different initializer by default.
234 ??? Previously we behaved so for scalar variables but not for array
235 accesses. */
236 return DECL_INITIAL (real_decl);
239 /* Add the variable DECL to the varpool.
240 Unlike varpool_finalize_decl function is intended to be used
241 by middle end and allows insertion of new variable at arbitrary point
242 of compilation. */
243 void
244 varpool_add_new_variable (tree decl)
246 struct varpool_node *node;
247 varpool_finalize_decl (decl);
248 node = varpool_node_for_decl (decl);
249 if (varpool_externally_visible_p (node))
250 node->symbol.externally_visible = true;
253 /* Return variable availability. See cgraph.h for description of individual
254 return values. */
255 enum availability
256 cgraph_variable_initializer_availability (struct varpool_node *node)
258 gcc_assert (cgraph_function_flags_ready);
259 if (!node->symbol.definition)
260 return AVAIL_NOT_AVAILABLE;
261 if (!TREE_PUBLIC (node->symbol.decl))
262 return AVAIL_AVAILABLE;
263 /* If the variable can be overwritten, return OVERWRITABLE. Takes
264 care of at least one notable extension - the COMDAT variables
265 used to share template instantiations in C++. */
266 if (!decl_replaceable_p (node->symbol.decl))
267 return AVAIL_OVERWRITABLE;
268 return AVAIL_AVAILABLE;
271 void
272 varpool_analyze_node (struct varpool_node *node)
274 tree decl = node->symbol.decl;
276 /* When reading back varpool at LTO time, we re-construct the queue in order
277 to have "needed" list right by inserting all needed nodes into varpool.
278 We however don't want to re-analyze already analyzed nodes. */
279 if (!node->symbol.analyzed)
281 gcc_assert (!in_lto_p || cgraph_function_flags_ready);
282 /* Compute the alignment early so function body expanders are
283 already informed about increased alignment. */
284 align_variable (decl, 0);
286 if (node->symbol.alias)
287 symtab_resolve_alias
288 ((symtab_node) node, (symtab_node) varpool_get_node (node->symbol.alias_target));
289 else if (DECL_INITIAL (decl))
290 record_references_in_initializer (decl, node->symbol.analyzed);
291 node->symbol.analyzed = true;
294 /* Assemble thunks and aliases associated to NODE. */
296 static void
297 assemble_aliases (struct varpool_node *node)
299 int i;
300 struct ipa_ref *ref;
301 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
302 if (ref->use == IPA_REF_ALIAS)
304 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
305 do_assemble_alias (alias->symbol.decl,
306 DECL_ASSEMBLER_NAME (node->symbol.decl));
307 assemble_aliases (alias);
311 /* Output one variable, if necessary. Return whether we output it. */
313 bool
314 varpool_assemble_decl (struct varpool_node *node)
316 tree decl = node->symbol.decl;
318 /* Aliases are outout when their target is produced or by
319 output_weakrefs. */
320 if (node->symbol.alias)
321 return false;
323 /* Constant pool is output from RTL land when the reference
324 survive till this level. */
325 if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl))
326 return false;
328 /* Decls with VALUE_EXPR should not be in the varpool at all. They
329 are not real variables, but just info for debugging and codegen.
330 Unfortunately at the moment emutls is not updating varpool correctly
331 after turning real vars into value_expr vars. */
332 if (DECL_HAS_VALUE_EXPR_P (decl)
333 && !targetm.have_tls)
334 return false;
336 /* Hard register vars do not need to be output. */
337 if (DECL_HARD_REGISTER (decl))
338 return false;
340 gcc_checking_assert (!TREE_ASM_WRITTEN (decl)
341 && TREE_CODE (decl) == VAR_DECL
342 && !DECL_HAS_VALUE_EXPR_P (decl));
344 if (!node->symbol.in_other_partition
345 && !DECL_EXTERNAL (decl))
347 assemble_variable (decl, 0, 1, 0);
348 gcc_assert (TREE_ASM_WRITTEN (decl));
349 node->symbol.definition = true;
350 assemble_aliases (node);
351 return true;
354 return false;
357 /* Add NODE to queue starting at FIRST.
358 The queue is linked via AUX pointers and terminated by pointer to 1. */
360 static void
361 enqueue_node (struct varpool_node *node, struct varpool_node **first)
363 if (node->symbol.aux)
364 return;
365 gcc_checking_assert (*first);
366 node->symbol.aux = *first;
367 *first = node;
370 /* Optimization of function bodies might've rendered some variables as
371 unnecessary so we want to avoid these from being compiled. Re-do
372 reachability starting from variables that are either externally visible
373 or was referred from the asm output routines. */
375 static void
376 varpool_remove_unreferenced_decls (void)
378 struct varpool_node *next, *node;
379 struct varpool_node *first = (struct varpool_node *)(void *)1;
380 int i;
381 struct ipa_ref *ref;
383 if (seen_error ())
384 return;
386 if (cgraph_dump_file)
387 fprintf (cgraph_dump_file, "Trivially needed variables:");
388 FOR_EACH_DEFINED_VARIABLE (node)
390 if (node->symbol.analyzed
391 && (!varpool_can_remove_if_no_refs (node)
392 /* We just expanded all function bodies. See if any of
393 them needed the variable. */
394 || DECL_RTL_SET_P (node->symbol.decl)))
396 enqueue_node (node, &first);
397 if (cgraph_dump_file)
398 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
401 while (first != (struct varpool_node *)(void *)1)
403 node = first;
404 first = (struct varpool_node *)first->symbol.aux;
406 if (node->symbol.same_comdat_group)
408 symtab_node next;
409 for (next = node->symbol.same_comdat_group;
410 next != (symtab_node)node;
411 next = next->symbol.same_comdat_group)
413 varpool_node *vnext = dyn_cast <varpool_node> (next);
414 if (vnext && vnext->symbol.analyzed)
415 enqueue_node (vnext, &first);
418 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
420 varpool_node *vnode = dyn_cast <varpool_node> (ref->referred);
421 if (vnode
422 && (!DECL_EXTERNAL (ref->referred->symbol.decl)
423 || vnode->symbol.alias)
424 && vnode->symbol.analyzed)
425 enqueue_node (vnode, &first);
428 if (cgraph_dump_file)
429 fprintf (cgraph_dump_file, "\nRemoving variables:");
430 for (node = varpool_first_defined_variable (); node; node = next)
432 next = varpool_next_defined_variable (node);
433 if (!node->symbol.aux)
435 if (cgraph_dump_file)
436 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
437 varpool_remove_node (node);
440 if (cgraph_dump_file)
441 fprintf (cgraph_dump_file, "\n");
444 /* For variables in named sections make sure get_variable_section
445 is called before we switch to those sections. Then section
446 conflicts between read-only and read-only requiring relocations
447 sections can be resolved. */
448 void
449 varpool_finalize_named_section_flags (struct varpool_node *node)
451 if (!TREE_ASM_WRITTEN (node->symbol.decl)
452 && !node->symbol.alias
453 && !node->symbol.in_other_partition
454 && !DECL_EXTERNAL (node->symbol.decl)
455 && TREE_CODE (node->symbol.decl) == VAR_DECL
456 && !DECL_HAS_VALUE_EXPR_P (node->symbol.decl)
457 && DECL_SECTION_NAME (node->symbol.decl))
458 get_variable_section (node->symbol.decl, false);
461 /* Output all variables enqueued to be assembled. */
462 bool
463 varpool_output_variables (void)
465 bool changed = false;
466 struct varpool_node *node;
468 if (seen_error ())
469 return false;
471 varpool_remove_unreferenced_decls ();
473 timevar_push (TV_VAROUT);
475 FOR_EACH_DEFINED_VARIABLE (node)
476 varpool_finalize_named_section_flags (node);
478 FOR_EACH_DEFINED_VARIABLE (node)
479 if (varpool_assemble_decl (node))
480 changed = true;
481 timevar_pop (TV_VAROUT);
482 return changed;
485 /* Create a new global variable of type TYPE. */
486 tree
487 add_new_static_var (tree type)
489 tree new_decl;
490 struct varpool_node *new_node;
492 new_decl = create_tmp_var_raw (type, NULL);
493 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
494 TREE_READONLY (new_decl) = 0;
495 TREE_STATIC (new_decl) = 1;
496 TREE_USED (new_decl) = 1;
497 DECL_CONTEXT (new_decl) = NULL_TREE;
498 DECL_ABSTRACT (new_decl) = 0;
499 lang_hooks.dup_lang_specific_decl (new_decl);
500 new_node = varpool_node_for_decl (new_decl);
501 varpool_finalize_decl (new_decl);
503 return new_node->symbol.decl;
506 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
507 Extra name aliases are output whenever DECL is output. */
509 struct varpool_node *
510 varpool_create_variable_alias (tree alias, tree decl)
512 struct varpool_node *alias_node;
514 gcc_assert (TREE_CODE (decl) == VAR_DECL);
515 gcc_assert (TREE_CODE (alias) == VAR_DECL);
516 alias_node = varpool_node_for_decl (alias);
517 alias_node->symbol.alias = true;
518 alias_node->symbol.definition = true;
519 alias_node->symbol.alias_target = decl;
520 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
521 alias_node->symbol.weakref = true;
522 return alias_node;
525 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
526 Extra name aliases are output whenever DECL is output. */
528 struct varpool_node *
529 varpool_extra_name_alias (tree alias, tree decl)
531 struct varpool_node *alias_node;
533 #ifndef ASM_OUTPUT_DEF
534 /* If aliases aren't supported by the assembler, fail. */
535 return NULL;
536 #endif
537 alias_node = varpool_create_variable_alias (alias, decl);
538 alias_node->symbol.cpp_implicit_alias = true;
540 /* Extra name alias mechanizm creates aliases really late
541 via DECL_ASSEMBLER_NAME mechanizm.
542 This is unfortunate because they are not going through the
543 standard channels. Ensure they get output. */
544 if (cpp_implicit_aliases_done)
545 symtab_resolve_alias ((symtab_node)alias_node,
546 (symtab_node)varpool_node_for_decl (decl));
547 return alias_node;
550 /* Call calback on NODE and aliases associated to NODE.
551 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
552 skipped. */
554 bool
555 varpool_for_node_and_aliases (struct varpool_node *node,
556 bool (*callback) (struct varpool_node *, void *),
557 void *data,
558 bool include_overwritable)
560 int i;
561 struct ipa_ref *ref;
563 if (callback (node, data))
564 return true;
565 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
566 if (ref->use == IPA_REF_ALIAS)
568 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
569 if (include_overwritable
570 || cgraph_variable_initializer_availability (alias) > AVAIL_OVERWRITABLE)
571 if (varpool_for_node_and_aliases (alias, callback, data,
572 include_overwritable))
573 return true;
575 return false;