Merge aosp-toolchain/gcc/gcc-4_9 changes.
[official-gcc.git] / gcc-4_8 / gcc / varpool.c
blob417be4a4785852ce14e7c17ced750bb7fd364589
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 "toplev.h"
38 #include "flags.h"
39 #include "l-ipo.h"
41 /* Return varpool node assigned to DECL. Create new one when needed. */
42 struct varpool_node *
43 varpool_node_for_decl (tree decl)
45 struct varpool_node *node = varpool_get_node (decl);
46 gcc_assert (TREE_CODE (decl) == VAR_DECL
47 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl) || in_lto_p));
48 if (node)
49 return node;
51 node = ggc_alloc_cleared_varpool_node ();
52 node->module_id = current_module_id;
53 node->symbol.type = SYMTAB_VARIABLE;
54 node->symbol.decl = decl;
55 symtab_register_node ((symtab_node)node);
56 return node;
59 /* Remove node from the varpool. */
60 void
61 varpool_remove_node (struct varpool_node *node)
63 varpool_remove_link_node (node);
64 symtab_unregister_node ((symtab_node)node);
65 if (DECL_INITIAL (node->symbol.decl)
66 && !DECL_IN_CONSTANT_POOL (node->symbol.decl)
67 /* Keep vtables for BINFO folding. */
68 && !DECL_VIRTUAL_P (node->symbol.decl)
69 /* FIXME: http://gcc.gnu.org/PR55395 */
70 && debug_info_level == DINFO_LEVEL_NONE)
71 DECL_INITIAL (node->symbol.decl) = error_mark_node;
72 ggc_free (node);
75 /* Dump given cgraph node. */
76 void
77 dump_varpool_node (FILE *f, struct varpool_node *node)
79 dump_symtab_base (f, (symtab_node)node);
80 fprintf (f, " Availability: %s\n",
81 cgraph_function_flags_ready
82 ? cgraph_availability_names[cgraph_variable_initializer_availability (node)]
83 : "not-ready");
84 fprintf (f, " Varpool flags:");
85 if (DECL_INITIAL (node->symbol.decl))
86 fprintf (f, " initialized");
87 if (node->analyzed)
88 fprintf (f, " analyzed");
89 if (node->finalized)
90 fprintf (f, " finalized");
91 if (node->output)
92 fprintf (f, " output");
93 fprintf (f, "\n");
96 /* Dump the variable pool. */
97 void
98 dump_varpool (FILE *f)
100 struct varpool_node *node;
102 fprintf (f, "variable pool:\n\n");
103 FOR_EACH_VARIABLE (node)
104 dump_varpool_node (f, node);
107 /* Dump the variable pool to stderr. */
109 DEBUG_FUNCTION void
110 debug_varpool (void)
112 dump_varpool (stderr);
115 /* Given an assembler name, lookup node. */
117 struct varpool_node *
118 varpool_node_for_asm (tree asmname)
120 if (symtab_node node = symtab_node_for_asm (asmname))
121 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
122 return vnode;
123 return NULL;
126 /* Determine if variable DECL is needed. That is, visible to something
127 either outside this translation unit, something magic in the system
128 configury */
129 bool
130 decide_is_variable_needed (struct varpool_node *node, tree decl)
132 if (DECL_EXTERNAL (decl))
133 return false;
135 /* If the user told us it is used, then it must be so. */
136 if (node->symbol.force_output)
137 return true;
139 /* Externally visible variables must be output. The exception is
140 COMDAT variables that must be output only when they are needed. */
141 if (TREE_PUBLIC (decl)
142 && !DECL_COMDAT (decl))
143 return true;
145 return false;
148 /* Return if DECL is constant and its initial value is known (so we can do
149 constant folding using DECL_INITIAL (decl)). */
151 bool
152 const_value_known_p (tree decl)
154 if (TREE_CODE (decl) != VAR_DECL
155 &&TREE_CODE (decl) != CONST_DECL)
156 return false;
158 if (TREE_CODE (decl) == CONST_DECL
159 || DECL_IN_CONSTANT_POOL (decl))
160 return true;
162 gcc_assert (TREE_CODE (decl) == VAR_DECL);
164 if (!TREE_READONLY (decl) || TREE_THIS_VOLATILE (decl))
165 return false;
167 /* Gimplifier takes away constructors of local vars */
168 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
169 return DECL_INITIAL (decl) != NULL;
171 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
173 /* Variables declared 'const' without an initializer
174 have zero as the initializer if they may not be
175 overridden at link or run time. */
176 if (!DECL_INITIAL (decl)
177 && (DECL_EXTERNAL (decl)
178 || decl_replaceable_p (decl)))
179 return false;
181 /* Variables declared `const' with an initializer are considered
182 to not be overwritable with different initializer by default.
184 ??? Previously we behaved so for scalar variables but not for array
185 accesses. */
186 return true;
189 /* Add the variable DECL to the varpool.
190 Unlike varpool_finalize_decl function is intended to be used
191 by middle end and allows insertion of new variable at arbitrary point
192 of compilation. */
193 void
194 varpool_add_new_variable (tree decl)
196 struct varpool_node *node;
197 varpool_finalize_decl (decl);
198 node = varpool_node_for_decl (decl);
199 if (varpool_externally_visible_p (node, false))
200 node->symbol.externally_visible = true;
203 /* Return variable availability. See cgraph.h for description of individual
204 return values. */
205 enum availability
206 cgraph_variable_initializer_availability (struct varpool_node *node)
208 gcc_assert (cgraph_function_flags_ready);
209 if (!node->finalized)
210 return AVAIL_NOT_AVAILABLE;
211 if (!TREE_PUBLIC (node->symbol.decl))
212 return AVAIL_AVAILABLE;
213 /* If the variable can be overwritten, return OVERWRITABLE. Takes
214 care of at least two notable extensions - the COMDAT variables
215 used to share template instantiations in C++. */
216 if (!decl_replaceable_p (node->symbol.decl))
217 return AVAIL_OVERWRITABLE;
218 return AVAIL_AVAILABLE;
221 void
222 varpool_analyze_node (struct varpool_node *node)
224 tree decl = node->symbol.decl;
226 /* When reading back varpool at LTO time, we re-construct the queue in order
227 to have "needed" list right by inserting all needed nodes into varpool.
228 We however don't want to re-analyze already analyzed nodes. */
229 if (!node->analyzed)
231 gcc_assert (!in_lto_p || cgraph_function_flags_ready);
232 /* Compute the alignment early so function body expanders are
233 already informed about increased alignment. */
234 align_variable (decl, 0);
236 if (node->alias && node->alias_of)
238 struct varpool_node *tgt = varpool_node_for_decl (node->alias_of);
239 struct varpool_node *n;
241 for (n = tgt; n && n->alias;
242 n = n->analyzed ? varpool_alias_aliased_node (n) : NULL)
243 if (n == node)
245 error ("variable %q+D part of alias cycle", node->symbol.decl);
246 node->alias = false;
247 continue;
249 if (!vec_safe_length (node->symbol.ref_list.references))
250 ipa_record_reference ((symtab_node)node, (symtab_node)tgt, IPA_REF_ALIAS, NULL);
251 if (node->extra_name_alias)
253 DECL_WEAK (node->symbol.decl) = DECL_WEAK (node->alias_of);
254 DECL_EXTERNAL (node->symbol.decl) = DECL_EXTERNAL (node->alias_of);
255 DECL_VISIBILITY (node->symbol.decl) = DECL_VISIBILITY (node->alias_of);
256 fixup_same_cpp_alias_visibility ((symtab_node) node,
257 (symtab_node) tgt, node->alias_of);
260 else if (DECL_INITIAL (decl))
261 record_references_in_initializer (decl, node->analyzed);
262 node->analyzed = true;
265 /* Assemble thunks and aliases associated to NODE. */
267 static void
268 assemble_aliases (struct varpool_node *node)
270 int i;
271 struct ipa_ref *ref;
272 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
273 if (ref->use == IPA_REF_ALIAS)
275 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
276 do_assemble_alias (alias->symbol.decl,
277 DECL_ASSEMBLER_NAME (alias->alias_of));
278 assemble_aliases (alias);
282 /* Output one variable, if necessary. Return whether we output it. */
284 bool
285 varpool_assemble_decl (struct varpool_node *node)
287 tree decl = node->symbol.decl;
289 /* Aliases are outout when their target is produced or by
290 output_weakrefs. */
291 if (node->alias)
292 return false;
294 /* Constant pool is output from RTL land when the reference
295 survive till this level. */
296 if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl))
297 return false;
299 /* Decls with VALUE_EXPR should not be in the varpool at all. They
300 are not real variables, but just info for debugging and codegen.
301 Unfortunately at the moment emutls is not updating varpool correctly
302 after turning real vars into value_expr vars. */
303 if (DECL_HAS_VALUE_EXPR_P (decl)
304 && !targetm.have_tls)
305 return false;
307 /* Hard register vars do not need to be output. */
308 if (DECL_HARD_REGISTER (decl))
309 return false;
311 gcc_checking_assert (!TREE_ASM_WRITTEN (decl)
312 && TREE_CODE (decl) == VAR_DECL
313 && !DECL_HAS_VALUE_EXPR_P (decl));
315 if (!node->symbol.in_other_partition
316 && !DECL_EXTERNAL (decl))
318 assemble_variable (decl, 0, 1, 0);
319 gcc_assert (TREE_ASM_WRITTEN (decl));
320 node->finalized = 1;
321 assemble_aliases (node);
322 return true;
325 return false;
328 /* Add NODE to queue starting at FIRST.
329 The queue is linked via AUX pointers and terminated by pointer to 1. */
331 static void
332 enqueue_node (struct varpool_node *node, struct varpool_node **first)
334 if (node->symbol.aux)
335 return;
336 gcc_checking_assert (*first);
337 node->symbol.aux = *first;
338 *first = node;
341 /* Optimization of function bodies might've rendered some variables as
342 unnecessary so we want to avoid these from being compiled. Re-do
343 reachability starting from variables that are either externally visible
344 or was referred from the asm output routines. */
346 static void
347 varpool_remove_unreferenced_decls (void)
349 struct varpool_node *next, *node;
350 struct varpool_node *first = (struct varpool_node *)(void *)1;
351 int i;
352 struct ipa_ref *ref;
354 if (seen_error ())
355 return;
357 if (cgraph_dump_file)
358 fprintf (cgraph_dump_file, "Trivially needed variables:");
359 FOR_EACH_DEFINED_VARIABLE (node)
361 if (node->analyzed
362 && (!varpool_can_remove_if_no_refs (node)
363 /* We just expanded all function bodies. See if any of
364 them needed the variable. */
365 || DECL_RTL_SET_P (node->symbol.decl)))
367 enqueue_node (node, &first);
368 if (cgraph_dump_file)
369 fprintf (cgraph_dump_file, " %s/%d", varpool_node_asm_name (node), node->symbol.order);
372 while (first != (struct varpool_node *)(void *)1)
374 node = first;
375 first = (struct varpool_node *)first->symbol.aux;
377 if (node->symbol.same_comdat_group)
379 symtab_node next;
380 for (next = node->symbol.same_comdat_group;
381 next != (symtab_node)node;
382 next = next->symbol.same_comdat_group)
384 varpool_node *vnext = dyn_cast <varpool_node> (next);
385 if (vnext && vnext->analyzed)
386 enqueue_node (vnext, &first);
389 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
391 varpool_node *vnode = dyn_cast <varpool_node> (ref->referred);
392 if (vnode
393 && (!DECL_EXTERNAL (ref->referred->symbol.decl)
394 || vnode->alias)
395 && vnode->analyzed)
396 enqueue_node (vnode, &first);
399 if (cgraph_dump_file)
400 fprintf (cgraph_dump_file, "\nRemoving variables:");
401 for (node = varpool_first_defined_variable (); node; node = next)
403 next = varpool_next_defined_variable (node);
404 if (!node->symbol.aux)
406 if (cgraph_dump_file)
407 fprintf (cgraph_dump_file, " %s/%d", varpool_node_asm_name (node), node->symbol.order);
408 varpool_remove_node (node);
411 if (cgraph_dump_file)
412 fprintf (cgraph_dump_file, "\n");
415 /* For variables in named sections make sure get_variable_section
416 is called before we switch to those sections. Then section
417 conflicts between read-only and read-only requiring relocations
418 sections can be resolved. */
419 void
420 varpool_finalize_named_section_flags (struct varpool_node *node)
422 if (!TREE_ASM_WRITTEN (node->symbol.decl)
423 && !node->alias
424 && !node->symbol.in_other_partition
425 && !DECL_EXTERNAL (node->symbol.decl)
426 && TREE_CODE (node->symbol.decl) == VAR_DECL
427 && !DECL_HAS_VALUE_EXPR_P (node->symbol.decl)
428 && DECL_SECTION_NAME (node->symbol.decl))
429 get_variable_section (node->symbol.decl, false);
432 /* Output all variables enqueued to be assembled. */
433 bool
434 varpool_output_variables (void)
436 bool changed = false;
437 struct varpool_node *node;
439 if (seen_error ())
440 return false;
442 varpool_remove_unreferenced_decls ();
444 timevar_push (TV_VAROUT);
446 FOR_EACH_DEFINED_VARIABLE (node)
447 varpool_finalize_named_section_flags (node);
449 FOR_EACH_DEFINED_VARIABLE (node)
450 if (varpool_assemble_decl (node))
451 changed = true;
452 timevar_pop (TV_VAROUT);
453 return changed;
456 /* Create a new global variable of type TYPE. */
457 tree
458 add_new_static_var (tree type)
460 tree new_decl;
461 struct varpool_node *new_node;
463 new_decl = create_tmp_var_raw (type, NULL);
464 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
465 TREE_READONLY (new_decl) = 0;
466 TREE_STATIC (new_decl) = 1;
467 TREE_USED (new_decl) = 1;
468 DECL_CONTEXT (new_decl) = NULL_TREE;
469 DECL_ABSTRACT (new_decl) = 0;
470 lang_hooks.dup_lang_specific_decl (new_decl);
471 new_node = varpool_node_for_decl (new_decl);
472 varpool_finalize_decl (new_decl);
474 return new_node->symbol.decl;
477 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
478 Extra name aliases are output whenever DECL is output. */
480 struct varpool_node *
481 varpool_create_variable_alias (tree alias, tree decl)
483 struct varpool_node *alias_node;
485 gcc_assert (TREE_CODE (decl) == VAR_DECL);
486 gcc_assert (TREE_CODE (alias) == VAR_DECL);
487 alias_node = varpool_node_for_decl (alias);
488 alias_node->alias = 1;
489 alias_node->finalized = 1;
490 alias_node->alias_of = decl;
492 /* Extra name alias mechanizm creates aliases really late
493 via DECL_ASSEMBLER_NAME mechanizm.
494 This is unfortunate because they are not going through the
495 standard channels. Ensure they get output. */
496 if (cgraph_state >= CGRAPH_STATE_IPA)
498 varpool_analyze_node (alias_node);
499 if (TREE_PUBLIC (alias))
500 alias_node->symbol.externally_visible = true;
502 return alias_node;
505 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
506 Extra name aliases are output whenever DECL is output. */
508 struct varpool_node *
509 varpool_extra_name_alias (tree alias, tree decl)
511 struct varpool_node *alias_node;
513 #ifndef ASM_OUTPUT_DEF
514 /* If aliases aren't supported by the assembler, fail. */
515 return NULL;
516 #endif
517 alias_node = varpool_create_variable_alias (alias, decl);
518 alias_node->extra_name_alias = true;
519 return alias_node;
522 /* Call calback on NODE and aliases associated to NODE.
523 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
524 skipped. */
526 bool
527 varpool_for_node_and_aliases (struct varpool_node *node,
528 bool (*callback) (struct varpool_node *, void *),
529 void *data,
530 bool include_overwritable)
532 int i;
533 struct ipa_ref *ref;
535 if (callback (node, data))
536 return true;
537 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
538 if (ref->use == IPA_REF_ALIAS)
540 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
541 if (include_overwritable
542 || cgraph_variable_initializer_availability (alias) > AVAIL_OVERWRITABLE)
543 if (varpool_for_node_and_aliases (alias, callback, data,
544 include_overwritable))
545 return true;
547 return false;