1 /* Callgraph handling code.
2 Copyright (C) 2003-2014 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"
28 #include "langhooks.h"
29 #include "diagnostic-core.h"
35 #include "gimple-expr.h"
37 #include "pointer-set.h"
39 /* List of hooks triggered on varpool_node events. */
40 struct varpool_node_hook_list
{
41 varpool_node_hook hook
;
43 struct varpool_node_hook_list
*next
;
46 /* List of hooks triggered when a node is removed. */
47 struct varpool_node_hook_list
*first_varpool_node_removal_hook
;
48 /* List of hooks triggered when an variable is inserted. */
49 struct varpool_node_hook_list
*first_varpool_variable_insertion_hook
;
51 /* Register HOOK to be called with DATA on each removed node. */
52 struct varpool_node_hook_list
*
53 varpool_add_node_removal_hook (varpool_node_hook hook
, void *data
)
55 struct varpool_node_hook_list
*entry
;
56 struct varpool_node_hook_list
**ptr
= &first_varpool_node_removal_hook
;
58 entry
= (struct varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
68 /* Remove ENTRY from the list of hooks called on removing nodes. */
70 varpool_remove_node_removal_hook (struct varpool_node_hook_list
*entry
)
72 struct varpool_node_hook_list
**ptr
= &first_varpool_node_removal_hook
;
80 /* Call all node removal hooks. */
82 varpool_call_node_removal_hooks (varpool_node
*node
)
84 struct varpool_node_hook_list
*entry
= first_varpool_node_removal_hook
;
87 entry
->hook (node
, entry
->data
);
92 /* Register HOOK to be called with DATA on each inserted node. */
93 struct varpool_node_hook_list
*
94 varpool_add_variable_insertion_hook (varpool_node_hook hook
, void *data
)
96 struct varpool_node_hook_list
*entry
;
97 struct varpool_node_hook_list
**ptr
= &first_varpool_variable_insertion_hook
;
99 entry
= (struct varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
109 /* Remove ENTRY from the list of hooks called on inserted nodes. */
111 varpool_remove_variable_insertion_hook (struct varpool_node_hook_list
*entry
)
113 struct varpool_node_hook_list
**ptr
= &first_varpool_variable_insertion_hook
;
115 while (*ptr
!= entry
)
121 /* Call all node insertion hooks. */
123 varpool_call_variable_insertion_hooks (varpool_node
*node
)
125 struct varpool_node_hook_list
*entry
= first_varpool_variable_insertion_hook
;
128 entry
->hook (node
, entry
->data
);
133 /* Allocate new callgraph node and insert it into basic data structures. */
136 varpool_create_empty_node (void)
138 varpool_node
*node
= ggc_cleared_alloc
<varpool_node
> ();
139 node
->type
= SYMTAB_VARIABLE
;
143 /* Return varpool node assigned to DECL. Create new one when needed. */
145 varpool_node_for_decl (tree decl
)
147 varpool_node
*node
= varpool_get_node (decl
);
148 gcc_checking_assert (TREE_CODE (decl
) == VAR_DECL
);
152 node
= varpool_create_empty_node ();
154 symtab_register_node (node
);
158 /* Remove node from the varpool. */
160 varpool_remove_node (varpool_node
*node
)
163 varpool_call_node_removal_hooks (node
);
164 symtab_unregister_node (node
);
166 /* Because we remove references from external functions before final compilation,
167 we may end up removing useful constructors.
168 FIXME: We probably want to trace boundaries better. */
169 if (cgraph_state
== CGRAPH_LTO_STREAMING
)
171 else if ((init
= ctor_for_folding (node
->decl
)) == error_mark_node
)
172 varpool_remove_initializer (node
);
174 DECL_INITIAL (node
->decl
) = init
;
178 /* Renove node initializer when it is no longer needed. */
180 varpool_remove_initializer (varpool_node
*node
)
182 if (DECL_INITIAL (node
->decl
)
183 && !DECL_IN_CONSTANT_POOL (node
->decl
)
184 /* Keep vtables for BINFO folding. */
185 && !DECL_VIRTUAL_P (node
->decl
)
186 /* FIXME: http://gcc.gnu.org/PR55395 */
187 && debug_info_level
== DINFO_LEVEL_NONE
188 /* When doing declaration merging we have duplicate
189 entries for given decl. Do not attempt to remove
190 the boides, or we will end up remiving
192 && cgraph_state
!= CGRAPH_LTO_STREAMING
)
193 DECL_INITIAL (node
->decl
) = error_mark_node
;
196 /* Dump given cgraph node. */
198 dump_varpool_node (FILE *f
, varpool_node
*node
)
200 dump_symtab_base (f
, node
);
201 fprintf (f
, " Availability: %s\n",
202 cgraph_function_flags_ready
203 ? cgraph_availability_names
[cgraph_variable_initializer_availability (node
)]
205 fprintf (f
, " Varpool flags:");
206 if (DECL_INITIAL (node
->decl
))
207 fprintf (f
, " initialized");
209 fprintf (f
, " output");
210 if (TREE_READONLY (node
->decl
))
211 fprintf (f
, " read-only");
212 if (ctor_for_folding (node
->decl
) != error_mark_node
)
213 fprintf (f
, " const-value-known");
215 fprintf (f
, " write-only");
219 /* Dump the variable pool. */
221 dump_varpool (FILE *f
)
225 fprintf (f
, "variable pool:\n\n");
226 FOR_EACH_VARIABLE (node
)
227 dump_varpool_node (f
, node
);
230 /* Dump the variable pool to stderr. */
235 dump_varpool (stderr
);
238 /* Given an assembler name, lookup node. */
240 varpool_node_for_asm (tree asmname
)
242 if (symtab_node
*node
= symtab_node_for_asm (asmname
))
243 return dyn_cast
<varpool_node
*> (node
);
248 /* Return if DECL is constant and its initial value is known (so we can do
249 constant folding using DECL_INITIAL (decl)).
250 Return ERROR_MARK_NODE when value is unknown. */
253 ctor_for_folding (tree decl
)
255 varpool_node
*node
, *real_node
;
258 if (TREE_CODE (decl
) != VAR_DECL
259 && TREE_CODE (decl
) != CONST_DECL
)
260 return error_mark_node
;
262 if (TREE_CODE (decl
) == CONST_DECL
263 || DECL_IN_CONSTANT_POOL (decl
))
264 return DECL_INITIAL (decl
);
266 if (TREE_THIS_VOLATILE (decl
))
267 return error_mark_node
;
269 /* Do not care about automatic variables. Those are never initialized
270 anyway, because gimplifier exapnds the code. */
271 if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
273 gcc_assert (!TREE_PUBLIC (decl
));
274 return error_mark_node
;
277 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
279 node
= varpool_get_node (decl
);
282 real_node
= varpool_variable_node (node
);
283 real_decl
= real_node
->decl
;
288 /* See if we are dealing with alias.
289 In most cases alias is just alternative symbol pointing to a given
290 constructor. This allows us to use interposition rules of DECL
291 constructor of REAL_NODE. However weakrefs are special by being just
292 alternative name of their target (if defined). */
293 if (decl
!= real_decl
)
295 gcc_assert (!DECL_INITIAL (decl
)
296 || DECL_INITIAL (decl
) == error_mark_node
);
297 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
)))
299 node
= varpool_alias_target (node
);
304 /* Vtables are defined by their types and must match no matter of interposition
306 if (DECL_VIRTUAL_P (real_decl
))
308 gcc_checking_assert (TREE_READONLY (real_decl
));
309 if (DECL_INITIAL (real_decl
))
310 return DECL_INITIAL (real_decl
);
313 /* The C++ front end creates VAR_DECLs for vtables of typeinfo
314 classes not defined in the current TU so that it can refer
315 to them from typeinfo objects. Avoid returning NULL_TREE. */
316 gcc_checking_assert (!COMPLETE_TYPE_P (DECL_CONTEXT (real_decl
)));
317 return error_mark_node
;
321 /* If there is no constructor, we have nothing to do. */
322 if (DECL_INITIAL (real_decl
) == error_mark_node
)
323 return error_mark_node
;
325 /* Non-readonly alias of readonly variable is also de-facto readonly,
326 because the variable itself is in readonly section.
327 We also honnor READONLY flag on alias assuming that user knows
329 if (!TREE_READONLY (decl
) && !TREE_READONLY (real_decl
))
330 return error_mark_node
;
332 /* Variables declared 'const' without an initializer
333 have zero as the initializer if they may not be
334 overridden at link or run time. */
335 if (!DECL_INITIAL (real_decl
)
336 && (DECL_EXTERNAL (decl
) || decl_replaceable_p (decl
)))
337 return error_mark_node
;
339 /* Variables declared `const' with an initializer are considered
340 to not be overwritable with different initializer by default.
342 ??? Previously we behaved so for scalar variables but not for array
344 return DECL_INITIAL (real_decl
);
347 /* Add the variable DECL to the varpool.
348 Unlike varpool_finalize_decl function is intended to be used
349 by middle end and allows insertion of new variable at arbitrary point
352 varpool_add_new_variable (tree decl
)
355 varpool_finalize_decl (decl
);
356 node
= varpool_node_for_decl (decl
);
357 varpool_call_variable_insertion_hooks (node
);
358 if (varpool_externally_visible_p (node
))
359 node
->externally_visible
= true;
362 /* Return variable availability. See cgraph.h for description of individual
365 cgraph_variable_initializer_availability (varpool_node
*node
)
367 if (!node
->definition
)
368 return AVAIL_NOT_AVAILABLE
;
369 if (!TREE_PUBLIC (node
->decl
))
370 return AVAIL_AVAILABLE
;
371 if (DECL_IN_CONSTANT_POOL (node
->decl
)
372 || DECL_VIRTUAL_P (node
->decl
))
373 return AVAIL_AVAILABLE
;
374 if (node
->alias
&& node
->weakref
)
376 enum availability avail
;
378 cgraph_variable_initializer_availability
379 (varpool_variable_node (node
, &avail
));
382 /* If the variable can be overwritten, return OVERWRITABLE. Takes
383 care of at least one notable extension - the COMDAT variables
384 used to share template instantiations in C++. */
385 if (decl_replaceable_p (node
->decl
)
386 || DECL_EXTERNAL (node
->decl
))
387 return AVAIL_OVERWRITABLE
;
388 return AVAIL_AVAILABLE
;
392 varpool_analyze_node (varpool_node
*node
)
394 tree decl
= node
->decl
;
396 /* When reading back varpool at LTO time, we re-construct the queue in order
397 to have "needed" list right by inserting all needed nodes into varpool.
398 We however don't want to re-analyze already analyzed nodes. */
401 gcc_assert (!in_lto_p
|| cgraph_function_flags_ready
);
402 /* Compute the alignment early so function body expanders are
403 already informed about increased alignment. */
404 align_variable (decl
, 0);
408 (node
, varpool_get_node (node
->alias_target
));
409 else if (DECL_INITIAL (decl
))
410 record_references_in_initializer (decl
, node
->analyzed
);
411 node
->analyzed
= true;
414 /* Assemble thunks and aliases associated to NODE. */
417 assemble_aliases (varpool_node
*node
)
421 for (i
= 0; ipa_ref_list_referring_iterate (&node
->ref_list
, i
, ref
); i
++)
422 if (ref
->use
== IPA_REF_ALIAS
)
424 varpool_node
*alias
= ipa_ref_referring_varpool_node (ref
);
425 do_assemble_alias (alias
->decl
,
426 DECL_ASSEMBLER_NAME (node
->decl
));
427 assemble_aliases (alias
);
431 /* Output one variable, if necessary. Return whether we output it. */
434 varpool_assemble_decl (varpool_node
*node
)
436 tree decl
= node
->decl
;
438 /* Aliases are outout when their target is produced or by
443 /* Constant pool is output from RTL land when the reference
444 survive till this level. */
445 if (DECL_IN_CONSTANT_POOL (decl
) && TREE_ASM_WRITTEN (decl
))
448 /* Decls with VALUE_EXPR should not be in the varpool at all. They
449 are not real variables, but just info for debugging and codegen.
450 Unfortunately at the moment emutls is not updating varpool correctly
451 after turning real vars into value_expr vars. */
452 if (DECL_HAS_VALUE_EXPR_P (decl
)
453 && !targetm
.have_tls
)
456 /* Hard register vars do not need to be output. */
457 if (DECL_HARD_REGISTER (decl
))
460 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
)
461 && TREE_CODE (decl
) == VAR_DECL
462 && !DECL_HAS_VALUE_EXPR_P (decl
));
464 if (!node
->in_other_partition
465 && !DECL_EXTERNAL (decl
))
467 assemble_variable (decl
, 0, 1, 0);
468 gcc_assert (TREE_ASM_WRITTEN (decl
));
469 node
->definition
= true;
470 assemble_aliases (node
);
477 /* Add NODE to queue starting at FIRST.
478 The queue is linked via AUX pointers and terminated by pointer to 1. */
481 enqueue_node (varpool_node
*node
, varpool_node
**first
)
485 gcc_checking_assert (*first
);
490 /* Optimization of function bodies might've rendered some variables as
491 unnecessary so we want to avoid these from being compiled. Re-do
492 reachability starting from variables that are either externally visible
493 or was referred from the asm output routines. */
496 varpool_remove_unreferenced_decls (void)
498 varpool_node
*next
, *node
;
499 varpool_node
*first
= (varpool_node
*)(void *)1;
502 struct pointer_set_t
*referenced
= pointer_set_create ();
507 if (cgraph_dump_file
)
508 fprintf (cgraph_dump_file
, "Trivially needed variables:");
509 FOR_EACH_DEFINED_VARIABLE (node
)
512 && (!varpool_can_remove_if_no_refs (node
)
513 /* We just expanded all function bodies. See if any of
514 them needed the variable. */
515 || DECL_RTL_SET_P (node
->decl
)))
517 enqueue_node (node
, &first
);
518 if (cgraph_dump_file
)
519 fprintf (cgraph_dump_file
, " %s", node
->asm_name ());
522 while (first
!= (varpool_node
*)(void *)1)
525 first
= (varpool_node
*)first
->aux
;
527 if (node
->same_comdat_group
)
530 for (next
= node
->same_comdat_group
;
532 next
= next
->same_comdat_group
)
534 varpool_node
*vnext
= dyn_cast
<varpool_node
*> (next
);
535 if (vnext
&& vnext
->analyzed
&& !symtab_comdat_local_p (next
))
536 enqueue_node (vnext
, &first
);
539 for (i
= 0; ipa_ref_list_reference_iterate (&node
->ref_list
, i
, ref
); i
++)
541 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (ref
->referred
);
543 && !vnode
->in_other_partition
544 && (!DECL_EXTERNAL (ref
->referred
->decl
)
547 enqueue_node (vnode
, &first
);
549 pointer_set_insert (referenced
, node
);
552 if (cgraph_dump_file
)
553 fprintf (cgraph_dump_file
, "\nRemoving variables:");
554 for (node
= varpool_first_defined_variable (); node
; node
= next
)
556 next
= varpool_next_defined_variable (node
);
559 if (cgraph_dump_file
)
560 fprintf (cgraph_dump_file
, " %s", node
->asm_name ());
561 if (pointer_set_contains (referenced
, node
))
562 varpool_remove_initializer (node
);
564 varpool_remove_node (node
);
567 pointer_set_destroy (referenced
);
568 if (cgraph_dump_file
)
569 fprintf (cgraph_dump_file
, "\n");
572 /* For variables in named sections make sure get_variable_section
573 is called before we switch to those sections. Then section
574 conflicts between read-only and read-only requiring relocations
575 sections can be resolved. */
577 varpool_finalize_named_section_flags (varpool_node
*node
)
579 if (!TREE_ASM_WRITTEN (node
->decl
)
581 && !node
->in_other_partition
582 && !DECL_EXTERNAL (node
->decl
)
583 && TREE_CODE (node
->decl
) == VAR_DECL
584 && !DECL_HAS_VALUE_EXPR_P (node
->decl
)
585 && node
->get_section ())
586 get_variable_section (node
->decl
, false);
589 /* Output all variables enqueued to be assembled. */
591 varpool_output_variables (void)
593 bool changed
= false;
599 varpool_remove_unreferenced_decls ();
601 timevar_push (TV_VAROUT
);
603 FOR_EACH_DEFINED_VARIABLE (node
)
604 varpool_finalize_named_section_flags (node
);
606 FOR_EACH_DEFINED_VARIABLE (node
)
607 if (varpool_assemble_decl (node
))
609 timevar_pop (TV_VAROUT
);
613 /* Create a new global variable of type TYPE. */
615 add_new_static_var (tree type
)
618 varpool_node
*new_node
;
620 new_decl
= create_tmp_var_raw (type
, NULL
);
621 DECL_NAME (new_decl
) = create_tmp_var_name (NULL
);
622 TREE_READONLY (new_decl
) = 0;
623 TREE_STATIC (new_decl
) = 1;
624 TREE_USED (new_decl
) = 1;
625 DECL_CONTEXT (new_decl
) = NULL_TREE
;
626 DECL_ABSTRACT (new_decl
) = 0;
627 lang_hooks
.dup_lang_specific_decl (new_decl
);
628 new_node
= varpool_node_for_decl (new_decl
);
629 varpool_finalize_decl (new_decl
);
631 return new_node
->decl
;
634 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
635 Extra name aliases are output whenever DECL is output. */
638 varpool_create_variable_alias (tree alias
, tree decl
)
640 varpool_node
*alias_node
;
642 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
643 gcc_assert (TREE_CODE (alias
) == VAR_DECL
);
644 alias_node
= varpool_node_for_decl (alias
);
645 alias_node
->alias
= true;
646 alias_node
->definition
= true;
647 alias_node
->alias_target
= decl
;
648 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias
)) != NULL
)
649 alias_node
->weakref
= true;
653 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
654 Extra name aliases are output whenever DECL is output. */
657 varpool_extra_name_alias (tree alias
, tree decl
)
659 varpool_node
*alias_node
;
661 #ifndef ASM_OUTPUT_DEF
662 /* If aliases aren't supported by the assembler, fail. */
665 alias_node
= varpool_create_variable_alias (alias
, decl
);
666 alias_node
->cpp_implicit_alias
= true;
668 /* Extra name alias mechanizm creates aliases really late
669 via DECL_ASSEMBLER_NAME mechanizm.
670 This is unfortunate because they are not going through the
671 standard channels. Ensure they get output. */
672 if (cpp_implicit_aliases_done
)
673 symtab_resolve_alias (alias_node
,
674 varpool_node_for_decl (decl
));
678 /* Call calback on NODE and aliases associated to NODE.
679 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
683 varpool_for_node_and_aliases (varpool_node
*node
,
684 bool (*callback
) (varpool_node
*, void *),
686 bool include_overwritable
)
691 if (callback (node
, data
))
693 for (i
= 0; ipa_ref_list_referring_iterate (&node
->ref_list
, i
, ref
); i
++)
694 if (ref
->use
== IPA_REF_ALIAS
)
696 varpool_node
*alias
= ipa_ref_referring_varpool_node (ref
);
697 if (include_overwritable
698 || cgraph_variable_initializer_availability (alias
) > AVAIL_OVERWRITABLE
)
699 if (varpool_for_node_and_aliases (alias
, callback
, data
,
700 include_overwritable
))