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
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
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/>. */
23 #include "coretypes.h"
27 #include "langhooks.h"
28 #include "diagnostic-core.h"
36 #include "tree-flow.h"
39 /* Return varpool node assigned to DECL. Create new one when needed. */
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
));
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
);
56 /* Remove node from the varpool. */
58 varpool_remove_node (struct varpool_node
*node
)
60 symtab_unregister_node ((symtab_node
)node
);
62 /* Because we remove references from external functions before final compilation,
63 we may end up removing useful constructors.
64 FIXME: We probably want to trace boundaries better. */
65 if (!const_value_known_p (node
->symbol
.decl
))
66 varpool_remove_initializer (node
);
70 /* Renove node initializer when it is no longer needed. */
72 varpool_remove_initializer (struct varpool_node
*node
)
74 if (DECL_INITIAL (node
->symbol
.decl
)
75 && !DECL_IN_CONSTANT_POOL (node
->symbol
.decl
)
76 /* Keep vtables for BINFO folding. */
77 && !DECL_VIRTUAL_P (node
->symbol
.decl
)
78 /* FIXME: http://gcc.gnu.org/PR55395 */
79 && debug_info_level
== DINFO_LEVEL_NONE
)
80 DECL_INITIAL (node
->symbol
.decl
) = error_mark_node
;
83 /* Dump given cgraph node. */
85 dump_varpool_node (FILE *f
, struct varpool_node
*node
)
87 dump_symtab_base (f
, (symtab_node
)node
);
88 fprintf (f
, " Availability: %s\n",
89 cgraph_function_flags_ready
90 ? cgraph_availability_names
[cgraph_variable_initializer_availability (node
)]
92 fprintf (f
, " Varpool flags:");
93 if (DECL_INITIAL (node
->symbol
.decl
))
94 fprintf (f
, " initialized");
96 fprintf (f
, " output");
97 if (TREE_READONLY (node
->symbol
.decl
))
98 fprintf (f
, " read-only");
99 if (const_value_known_p (node
->symbol
.decl
))
100 fprintf (f
, " const-value-known");
104 /* Dump the variable pool. */
106 dump_varpool (FILE *f
)
108 struct varpool_node
*node
;
110 fprintf (f
, "variable pool:\n\n");
111 FOR_EACH_VARIABLE (node
)
112 dump_varpool_node (f
, node
);
115 /* Dump the variable pool to stderr. */
120 dump_varpool (stderr
);
123 /* Given an assembler name, lookup node. */
124 struct varpool_node
*
125 varpool_node_for_asm (tree asmname
)
127 if (symtab_node node
= symtab_node_for_asm (asmname
))
128 return dyn_cast
<varpool_node
> (node
);
133 /* Return if DECL is constant and its initial value is known (so we can do
134 constant folding using DECL_INITIAL (decl)). */
137 const_value_known_p (tree decl
)
139 if (TREE_CODE (decl
) != VAR_DECL
140 &&TREE_CODE (decl
) != CONST_DECL
)
143 if (TREE_CODE (decl
) == CONST_DECL
144 || DECL_IN_CONSTANT_POOL (decl
))
147 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
149 if (!TREE_READONLY (decl
) || TREE_THIS_VOLATILE (decl
))
152 /* Gimplifier takes away constructors of local vars */
153 if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
154 return DECL_INITIAL (decl
) != NULL
;
156 gcc_assert (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
));
158 /* Variables declared 'const' without an initializer
159 have zero as the initializer if they may not be
160 overridden at link or run time. */
161 if (!DECL_INITIAL (decl
)
162 && (DECL_EXTERNAL (decl
)
163 || decl_replaceable_p (decl
)))
166 /* Variables declared `const' with an initializer are considered
167 to not be overwritable with different initializer by default.
169 ??? Previously we behaved so for scalar variables but not for array
174 /* Add the variable DECL to the varpool.
175 Unlike varpool_finalize_decl function is intended to be used
176 by middle end and allows insertion of new variable at arbitrary point
179 varpool_add_new_variable (tree decl
)
181 struct varpool_node
*node
;
182 varpool_finalize_decl (decl
);
183 node
= varpool_node_for_decl (decl
);
184 if (varpool_externally_visible_p (node
))
185 node
->symbol
.externally_visible
= true;
188 /* Return variable availability. See cgraph.h for description of individual
191 cgraph_variable_initializer_availability (struct varpool_node
*node
)
193 gcc_assert (cgraph_function_flags_ready
);
194 if (!node
->symbol
.definition
)
195 return AVAIL_NOT_AVAILABLE
;
196 if (!TREE_PUBLIC (node
->symbol
.decl
))
197 return AVAIL_AVAILABLE
;
198 /* If the variable can be overwritten, return OVERWRITABLE. Takes
199 care of at least one notable extension - the COMDAT variables
200 used to share template instantiations in C++. */
201 if (!decl_replaceable_p (node
->symbol
.decl
))
202 return AVAIL_OVERWRITABLE
;
203 return AVAIL_AVAILABLE
;
207 varpool_analyze_node (struct varpool_node
*node
)
209 tree decl
= node
->symbol
.decl
;
211 /* When reading back varpool at LTO time, we re-construct the queue in order
212 to have "needed" list right by inserting all needed nodes into varpool.
213 We however don't want to re-analyze already analyzed nodes. */
214 if (!node
->symbol
.analyzed
)
216 gcc_assert (!in_lto_p
|| cgraph_function_flags_ready
);
217 /* Compute the alignment early so function body expanders are
218 already informed about increased alignment. */
219 align_variable (decl
, 0);
221 if (node
->symbol
.alias
)
223 ((symtab_node
) node
, (symtab_node
) varpool_get_node (node
->symbol
.alias_target
));
224 else if (DECL_INITIAL (decl
))
225 record_references_in_initializer (decl
, node
->symbol
.analyzed
);
226 node
->symbol
.analyzed
= true;
229 /* Assemble thunks and aliases associated to NODE. */
232 assemble_aliases (struct varpool_node
*node
)
236 for (i
= 0; ipa_ref_list_referring_iterate (&node
->symbol
.ref_list
, i
, ref
); i
++)
237 if (ref
->use
== IPA_REF_ALIAS
)
239 struct varpool_node
*alias
= ipa_ref_referring_varpool_node (ref
);
240 do_assemble_alias (alias
->symbol
.decl
,
241 DECL_ASSEMBLER_NAME (node
->symbol
.decl
));
242 assemble_aliases (alias
);
246 /* Output one variable, if necessary. Return whether we output it. */
249 varpool_assemble_decl (struct varpool_node
*node
)
251 tree decl
= node
->symbol
.decl
;
253 /* Aliases are outout when their target is produced or by
255 if (node
->symbol
.alias
)
258 /* Constant pool is output from RTL land when the reference
259 survive till this level. */
260 if (DECL_IN_CONSTANT_POOL (decl
) && TREE_ASM_WRITTEN (decl
))
263 /* Decls with VALUE_EXPR should not be in the varpool at all. They
264 are not real variables, but just info for debugging and codegen.
265 Unfortunately at the moment emutls is not updating varpool correctly
266 after turning real vars into value_expr vars. */
267 if (DECL_HAS_VALUE_EXPR_P (decl
)
268 && !targetm
.have_tls
)
271 /* Hard register vars do not need to be output. */
272 if (DECL_HARD_REGISTER (decl
))
275 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
)
276 && TREE_CODE (decl
) == VAR_DECL
277 && !DECL_HAS_VALUE_EXPR_P (decl
));
279 if (!node
->symbol
.in_other_partition
280 && !DECL_EXTERNAL (decl
))
282 assemble_variable (decl
, 0, 1, 0);
283 gcc_assert (TREE_ASM_WRITTEN (decl
));
284 node
->symbol
.definition
= true;
285 assemble_aliases (node
);
292 /* Add NODE to queue starting at FIRST.
293 The queue is linked via AUX pointers and terminated by pointer to 1. */
296 enqueue_node (struct varpool_node
*node
, struct varpool_node
**first
)
298 if (node
->symbol
.aux
)
300 gcc_checking_assert (*first
);
301 node
->symbol
.aux
= *first
;
305 /* Optimization of function bodies might've rendered some variables as
306 unnecessary so we want to avoid these from being compiled. Re-do
307 reachability starting from variables that are either externally visible
308 or was referred from the asm output routines. */
311 varpool_remove_unreferenced_decls (void)
313 struct varpool_node
*next
, *node
;
314 struct varpool_node
*first
= (struct varpool_node
*)(void *)1;
321 if (cgraph_dump_file
)
322 fprintf (cgraph_dump_file
, "Trivially needed variables:");
323 FOR_EACH_DEFINED_VARIABLE (node
)
325 if (node
->symbol
.analyzed
326 && (!varpool_can_remove_if_no_refs (node
)
327 /* We just expanded all function bodies. See if any of
328 them needed the variable. */
329 || DECL_RTL_SET_P (node
->symbol
.decl
)))
331 enqueue_node (node
, &first
);
332 if (cgraph_dump_file
)
333 fprintf (cgraph_dump_file
, " %s", varpool_node_asm_name (node
));
336 while (first
!= (struct varpool_node
*)(void *)1)
339 first
= (struct varpool_node
*)first
->symbol
.aux
;
341 if (node
->symbol
.same_comdat_group
)
344 for (next
= node
->symbol
.same_comdat_group
;
345 next
!= (symtab_node
)node
;
346 next
= next
->symbol
.same_comdat_group
)
348 varpool_node
*vnext
= dyn_cast
<varpool_node
> (next
);
349 if (vnext
&& vnext
->symbol
.analyzed
)
350 enqueue_node (vnext
, &first
);
353 for (i
= 0; ipa_ref_list_reference_iterate (&node
->symbol
.ref_list
, i
, ref
); i
++)
355 varpool_node
*vnode
= dyn_cast
<varpool_node
> (ref
->referred
);
357 && (!DECL_EXTERNAL (ref
->referred
->symbol
.decl
)
358 || vnode
->symbol
.alias
)
359 && vnode
->symbol
.analyzed
)
360 enqueue_node (vnode
, &first
);
363 if (cgraph_dump_file
)
364 fprintf (cgraph_dump_file
, "\nRemoving variables:");
365 for (node
= varpool_first_defined_variable (); node
; node
= next
)
367 next
= varpool_next_defined_variable (node
);
368 if (!node
->symbol
.aux
)
370 if (cgraph_dump_file
)
371 fprintf (cgraph_dump_file
, " %s", varpool_node_asm_name (node
));
372 varpool_remove_node (node
);
375 if (cgraph_dump_file
)
376 fprintf (cgraph_dump_file
, "\n");
379 /* For variables in named sections make sure get_variable_section
380 is called before we switch to those sections. Then section
381 conflicts between read-only and read-only requiring relocations
382 sections can be resolved. */
384 varpool_finalize_named_section_flags (struct varpool_node
*node
)
386 if (!TREE_ASM_WRITTEN (node
->symbol
.decl
)
387 && !node
->symbol
.alias
388 && !node
->symbol
.in_other_partition
389 && !DECL_EXTERNAL (node
->symbol
.decl
)
390 && TREE_CODE (node
->symbol
.decl
) == VAR_DECL
391 && !DECL_HAS_VALUE_EXPR_P (node
->symbol
.decl
)
392 && DECL_SECTION_NAME (node
->symbol
.decl
))
393 get_variable_section (node
->symbol
.decl
, false);
396 /* Output all variables enqueued to be assembled. */
398 varpool_output_variables (void)
400 bool changed
= false;
401 struct varpool_node
*node
;
406 varpool_remove_unreferenced_decls ();
408 timevar_push (TV_VAROUT
);
410 FOR_EACH_DEFINED_VARIABLE (node
)
411 varpool_finalize_named_section_flags (node
);
413 FOR_EACH_DEFINED_VARIABLE (node
)
414 if (varpool_assemble_decl (node
))
416 timevar_pop (TV_VAROUT
);
420 /* Create a new global variable of type TYPE. */
422 add_new_static_var (tree type
)
425 struct varpool_node
*new_node
;
427 new_decl
= create_tmp_var_raw (type
, NULL
);
428 DECL_NAME (new_decl
) = create_tmp_var_name (NULL
);
429 TREE_READONLY (new_decl
) = 0;
430 TREE_STATIC (new_decl
) = 1;
431 TREE_USED (new_decl
) = 1;
432 DECL_CONTEXT (new_decl
) = NULL_TREE
;
433 DECL_ABSTRACT (new_decl
) = 0;
434 lang_hooks
.dup_lang_specific_decl (new_decl
);
435 new_node
= varpool_node_for_decl (new_decl
);
436 varpool_finalize_decl (new_decl
);
438 return new_node
->symbol
.decl
;
441 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
442 Extra name aliases are output whenever DECL is output. */
444 struct varpool_node
*
445 varpool_create_variable_alias (tree alias
, tree decl
)
447 struct varpool_node
*alias_node
;
449 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
450 gcc_assert (TREE_CODE (alias
) == VAR_DECL
);
451 alias_node
= varpool_node_for_decl (alias
);
452 alias_node
->symbol
.alias
= true;
453 alias_node
->symbol
.definition
= true;
454 alias_node
->symbol
.alias_target
= decl
;
458 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
459 Extra name aliases are output whenever DECL is output. */
461 struct varpool_node
*
462 varpool_extra_name_alias (tree alias
, tree decl
)
464 struct varpool_node
*alias_node
;
466 #ifndef ASM_OUTPUT_DEF
467 /* If aliases aren't supported by the assembler, fail. */
470 alias_node
= varpool_create_variable_alias (alias
, decl
);
471 alias_node
->symbol
.cpp_implicit_alias
= true;
473 /* Extra name alias mechanizm creates aliases really late
474 via DECL_ASSEMBLER_NAME mechanizm.
475 This is unfortunate because they are not going through the
476 standard channels. Ensure they get output. */
477 if (cpp_implicit_aliases_done
)
478 symtab_resolve_alias ((symtab_node
)alias_node
,
479 (symtab_node
)varpool_node_for_decl (decl
));
483 /* Call calback on NODE and aliases associated to NODE.
484 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
488 varpool_for_node_and_aliases (struct varpool_node
*node
,
489 bool (*callback
) (struct varpool_node
*, void *),
491 bool include_overwritable
)
496 if (callback (node
, data
))
498 for (i
= 0; ipa_ref_list_referring_iterate (&node
->symbol
.ref_list
, i
, ref
); i
++)
499 if (ref
->use
== IPA_REF_ALIAS
)
501 struct varpool_node
*alias
= ipa_ref_referring_varpool_node (ref
);
502 if (include_overwritable
503 || cgraph_variable_initializer_availability (alias
) > AVAIL_OVERWRITABLE
)
504 if (varpool_for_node_and_aliases (alias
, callback
, data
,
505 include_overwritable
))